From 6d6b7bdbc327528d155f0422ef096846559844c0 Mon Sep 17 00:00:00 2001 From: german77 Date: Thu, 22 Dec 2022 01:07:46 -0600 Subject: input_common: Implement joycon nfc --- src/input_common/helpers/joycon_protocol/nfc.cpp | 414 +++++++++++++++++++++++ 1 file changed, 414 insertions(+) create mode 100644 src/input_common/helpers/joycon_protocol/nfc.cpp (limited to 'src/input_common/helpers/joycon_protocol/nfc.cpp') diff --git a/src/input_common/helpers/joycon_protocol/nfc.cpp b/src/input_common/helpers/joycon_protocol/nfc.cpp new file mode 100644 index 000000000..69b2bfe05 --- /dev/null +++ b/src/input_common/helpers/joycon_protocol/nfc.cpp @@ -0,0 +1,414 @@ +// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include "common/logging/log.h" +#include "input_common/helpers/joycon_protocol/nfc.h" + +namespace InputCommon::Joycon { + +NfcProtocol::NfcProtocol(std::shared_ptr handle) : JoyconCommonProtocol(handle) {} + +DriverResult NfcProtocol::EnableNfc() { + LOG_INFO(Input, "Enable NFC"); + DriverResult result{DriverResult::Success}; + SetBlocking(); + + if (result == DriverResult::Success) { + result = SetReportMode(ReportMode::NFC_IR_MODE_60HZ); + } + if (result == DriverResult::Success) { + result = EnableMCU(true); + } + if (result == DriverResult::Success) { + result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::Standby); + } + if (result == DriverResult::Success) { + const MCUConfig config{ + .command = MCUCommand::ConfigureMCU, + .sub_command = MCUSubCommand::SetMCUMode, + .mode = MCUMode::NFC, + .crc = {}, + }; + + result = ConfigureMCU(config); + } + + SetNonBlocking(); + return result; +} + +DriverResult NfcProtocol::DisableNfc() { + LOG_DEBUG(Input, "Disable NFC"); + DriverResult result{DriverResult::Success}; + SetBlocking(); + + if (result == DriverResult::Success) { + result = EnableMCU(false); + } + + is_enabled = false; + + SetNonBlocking(); + return result; +} + +DriverResult NfcProtocol::StartNFCPollingMode() { + LOG_DEBUG(Input, "Start NFC pooling Mode"); + DriverResult result{DriverResult::Success}; + TagFoundData tag_data{}; + SetBlocking(); + + if (result == DriverResult::Success) { + result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::NFC); + } + if (result == DriverResult::Success) { + result = WaitUntilNfcIsReady(); + } + if (result == DriverResult::Success) { + is_enabled = true; + } + + SetNonBlocking(); + return result; +} + +DriverResult NfcProtocol::ScanAmiibo(std::vector& data) { + LOG_DEBUG(Input, "Start NFC pooling Mode"); + DriverResult result{DriverResult::Success}; + TagFoundData tag_data{}; + SetBlocking(); + + if (result == DriverResult::Success) { + result = StartPolling(tag_data); + } + if (result == DriverResult::Success) { + result = ReadTag(tag_data); + } + if (result == DriverResult::Success) { + result = WaitUntilNfcIsReady(); + } + if (result == DriverResult::Success) { + result = StartPolling(tag_data); + } + if (result == DriverResult::Success) { + result = GetAmiiboData(data); + } + + SetNonBlocking(); + return result; +} + +bool NfcProtocol::HasAmiibo() { + DriverResult result{DriverResult::Success}; + TagFoundData tag_data{}; + SetBlocking(); + + if (result == DriverResult::Success) { + result = StartPolling(tag_data); + } + + SetNonBlocking(); + return result == DriverResult::Success; +} + +DriverResult NfcProtocol::WaitUntilNfcIsReady() { + constexpr std::size_t timeout_limit = 10; + std::vector output; + std::size_t tries = 0; + + do { + auto result = SendStartWaitingRecieveRequest(output); + + if (result != DriverResult::Success) { + return result; + } + if (tries++ > timeout_limit) { + return DriverResult::Timeout; + } + } while (output[49] != 0x2a || (output[51] << 8) + output[50] != 0x0500 || output[55] != 0x31 || + output[56] != 0x00); + + return DriverResult::Success; +} + +DriverResult NfcProtocol::StartPolling(TagFoundData& data) { + LOG_DEBUG(Input, "Start Polling for tag"); + constexpr std::size_t timeout_limit = 7; + std::vector output; + std::size_t tries = 0; + + do { + const auto result = SendStartPollingRequest(output); + if (result != DriverResult::Success) { + return result; + } + if (tries++ > timeout_limit) { + return DriverResult::Timeout; + } + } while (output[49] != 0x2a || (output[51] << 8) + output[50] != 0x0500 || output[56] != 0x09); + + data.type = output[62]; + data.uuid.resize(output[64]); + memcpy(data.uuid.data(), output.data() + 65, data.uuid.size()); + + return DriverResult::Success; +} + +DriverResult NfcProtocol::ReadTag(const TagFoundData& data) { + constexpr std::size_t timeout_limit = 10; + std::vector output; + std::size_t tries = 0; + + std::string uuid_string = ""; + for (auto& content : data.uuid) { + uuid_string += " " + fmt::format("{:02x}", content); + } + + LOG_INFO(Input, "Tag detected, type={}, uuid={}", data.type, uuid_string); + + tries = 0; + std::size_t ntag_pages = 0; + // Read Tag data +loop1: + while (true) { + auto result = SendReadAmiiboRequest(output, ntag_pages); + + int attempt = 0; + while (1) { + if (attempt != 0) { + result = GetMCUDataResponse(ReportMode::NFC_IR_MODE_60HZ, output); + } + if ((output[49] == 0x3a || output[49] == 0x2a) && output[56] == 0x07) { + return DriverResult::ErrorReadingData; + } + if (output[49] == 0x3a && output[51] == 0x07 && output[52] == 0x01) { + if (data.type != 2) { + goto loop1; + } + switch (output[74]) { + case 0: + ntag_pages = 135; + break; + case 3: + ntag_pages = 45; + break; + case 4: + ntag_pages = 231; + break; + default: + return DriverResult::ErrorReadingData; + } + goto loop1; + } + if (output[49] == 0x2a && output[56] == 0x04) { + // finished + SendStopPollingRequest(output); + return DriverResult::Success; + } + if (output[49] == 0x2a) { + goto loop1; + } + if (attempt++ > 6) { + goto loop1; + } + } + + if (result != DriverResult::Success) { + return result; + } + if (tries++ > timeout_limit) { + return DriverResult::Timeout; + } + } + + return DriverResult::Success; +} + +DriverResult NfcProtocol::GetAmiiboData(std::vector& ntag_data) { + constexpr std::size_t timeout_limit = 10; + std::vector output; + std::size_t tries = 0; + + std::size_t ntag_pages = 135; + std::size_t ntag_buffer_pos = 0; + // Read Tag data +loop1: + while (true) { + auto result = SendReadAmiiboRequest(output, ntag_pages); + + int attempt = 0; + while (1) { + if (attempt != 0) { + result = GetMCUDataResponse(ReportMode::NFC_IR_MODE_60HZ, output); + } + if ((output[49] == 0x3a || output[49] == 0x2a) && output[56] == 0x07) { + return DriverResult::ErrorReadingData; + } + if (output[49] == 0x3a && output[51] == 0x07) { + std::size_t payload_size = (output[54] << 8 | output[55]) & 0x7FF; + if (output[52] == 0x01) { + memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 116, + payload_size - 60); + ntag_buffer_pos += payload_size - 60; + } else { + memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 56, payload_size); + } + goto loop1; + } + if (output[49] == 0x2a && output[56] == 0x04) { + LOG_INFO(Input, "Finished reading amiibo"); + return DriverResult::Success; + } + if (output[49] == 0x2a) { + goto loop1; + } + if (attempt++ > 4) { + goto loop1; + } + } + + if (result != DriverResult::Success) { + return result; + } + if (tries++ > timeout_limit) { + return DriverResult::Timeout; + } + } + + return DriverResult::Success; +} + +DriverResult NfcProtocol::SendStartPollingRequest(std::vector& output) { + NFCRequestState request{ + .sub_command = MCUSubCommand::ReadDeviceMode, + .command_argument = NFCReadCommand::StartPolling, + .packet_id = 0x0, + .packet_flag = MCUPacketFlag::LastCommandPacket, + .data_length = sizeof(NFCPollingCommandData), + .nfc_polling = + { + .enable_mifare = 0x01, + .unknown_1 = 0x00, + .unknown_2 = 0x00, + .unknown_3 = 0x2c, + .unknown_4 = 0x01, + }, + .crc = {}, + }; + + std::vector request_data(sizeof(NFCRequestState)); + memcpy(request_data.data(), &request, sizeof(NFCRequestState)); + request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); + return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); +} + +DriverResult NfcProtocol::SendStopPollingRequest(std::vector& output) { + NFCRequestState request{ + .sub_command = MCUSubCommand::ReadDeviceMode, + .command_argument = NFCReadCommand::StopPolling, + .packet_id = 0x0, + .packet_flag = MCUPacketFlag::LastCommandPacket, + .data_length = 0, + .raw_data = {}, + .crc = {}, + }; + + std::vector request_data(sizeof(NFCRequestState)); + memcpy(request_data.data(), &request, sizeof(NFCRequestState)); + request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); + return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); +} + +DriverResult NfcProtocol::SendStartWaitingRecieveRequest(std::vector& output) { + NFCRequestState request{ + .sub_command = MCUSubCommand::ReadDeviceMode, + .command_argument = NFCReadCommand::StartWaitingRecieve, + .packet_id = 0x0, + .packet_flag = MCUPacketFlag::LastCommandPacket, + .data_length = 0, + .raw_data = {}, + .crc = {}, + }; + + std::vector request_data(sizeof(NFCRequestState)); + memcpy(request_data.data(), &request, sizeof(NFCRequestState)); + request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); + return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); +} + +DriverResult NfcProtocol::SendReadAmiiboRequest(std::vector& output, std::size_t ntag_pages) { + NFCRequestState request{ + .sub_command = MCUSubCommand::ReadDeviceMode, + .command_argument = NFCReadCommand::Ntag, + .packet_id = 0x0, + .packet_flag = MCUPacketFlag::LastCommandPacket, + .data_length = sizeof(NFCReadCommandData), + .nfc_read = + { + .unknown = 0xd0, + .uuid_length = 0x07, + .unknown_2 = 0x00, + .uid = {}, + .tag_type = NFCTagType::AllTags, + .read_block = GetReadBlockCommand(ntag_pages), + }, + .crc = {}, + }; + + std::vector request_data(sizeof(NFCRequestState)); + memcpy(request_data.data(), &request, sizeof(NFCRequestState)); + request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); + return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); +} + +NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const { + if (pages == 0) { + return { + .block_count = 1, + }; + } + + if (pages == 45) { + return { + .block_count = 1, + .blocks = + { + NFCReadBlock{0x00, 0x2C}, + }, + }; + } + + if (pages == 135) { + return { + .block_count = 3, + .blocks = + { + NFCReadBlock{0x00, 0x3b}, + {0x3c, 0x77}, + {0x78, 0x86}, + }, + }; + } + + if (pages == 231) { + return { + .block_count = 4, + .blocks = + { + NFCReadBlock{0x00, 0x3b}, + {0x3c, 0x77}, + {0x78, 0x83}, + {0xb4, 0xe6}, + }, + }; + } + + return {}; +} + +bool NfcProtocol::IsEnabled() { + return is_enabled; +} + +} // namespace InputCommon::Joycon -- cgit v1.2.3 From e1a3bda4d9881cb99c36b64733b814a3bb437f13 Mon Sep 17 00:00:00 2001 From: german77 Date: Fri, 23 Dec 2022 08:32:02 -0600 Subject: Address review comments --- src/input_common/helpers/joycon_protocol/nfc.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/input_common/helpers/joycon_protocol/nfc.cpp') diff --git a/src/input_common/helpers/joycon_protocol/nfc.cpp b/src/input_common/helpers/joycon_protocol/nfc.cpp index 69b2bfe05..8755e310b 100644 --- a/src/input_common/helpers/joycon_protocol/nfc.cpp +++ b/src/input_common/helpers/joycon_protocol/nfc.cpp @@ -7,7 +7,8 @@ namespace InputCommon::Joycon { -NfcProtocol::NfcProtocol(std::shared_ptr handle) : JoyconCommonProtocol(handle) {} +NfcProtocol::NfcProtocol(std::shared_ptr handle) + : JoyconCommonProtocol(std::move(handle)) {} DriverResult NfcProtocol::EnableNfc() { LOG_INFO(Input, "Enable NFC"); @@ -160,9 +161,9 @@ DriverResult NfcProtocol::ReadTag(const TagFoundData& data) { std::vector output; std::size_t tries = 0; - std::string uuid_string = ""; + std::string uuid_string; for (auto& content : data.uuid) { - uuid_string += " " + fmt::format("{:02x}", content); + uuid_string += fmt::format(" {:02x}", content); } LOG_INFO(Input, "Tag detected, type={}, uuid={}", data.type, uuid_string); @@ -407,7 +408,7 @@ NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const { return {}; } -bool NfcProtocol::IsEnabled() { +bool NfcProtocol::IsEnabled() const { return is_enabled; } -- cgit v1.2.3 From 340f15d1fa79594dbe12a6e19140ba012751b533 Mon Sep 17 00:00:00 2001 From: german77 Date: Fri, 13 Jan 2023 23:29:05 -0600 Subject: input_common: Address byte review --- src/input_common/helpers/joycon_protocol/nfc.cpp | 183 +++++++++++------------ 1 file changed, 84 insertions(+), 99 deletions(-) (limited to 'src/input_common/helpers/joycon_protocol/nfc.cpp') diff --git a/src/input_common/helpers/joycon_protocol/nfc.cpp b/src/input_common/helpers/joycon_protocol/nfc.cpp index 8755e310b..5c0f71722 100644 --- a/src/input_common/helpers/joycon_protocol/nfc.cpp +++ b/src/input_common/helpers/joycon_protocol/nfc.cpp @@ -12,8 +12,8 @@ NfcProtocol::NfcProtocol(std::shared_ptr handle) DriverResult NfcProtocol::EnableNfc() { LOG_INFO(Input, "Enable NFC"); + ScopedSetBlocking sb(this); DriverResult result{DriverResult::Success}; - SetBlocking(); if (result == DriverResult::Success) { result = SetReportMode(ReportMode::NFC_IR_MODE_60HZ); @@ -35,14 +35,13 @@ DriverResult NfcProtocol::EnableNfc() { result = ConfigureMCU(config); } - SetNonBlocking(); return result; } DriverResult NfcProtocol::DisableNfc() { LOG_DEBUG(Input, "Disable NFC"); + ScopedSetBlocking sb(this); DriverResult result{DriverResult::Success}; - SetBlocking(); if (result == DriverResult::Success) { result = EnableMCU(false); @@ -50,15 +49,14 @@ DriverResult NfcProtocol::DisableNfc() { is_enabled = false; - SetNonBlocking(); return result; } DriverResult NfcProtocol::StartNFCPollingMode() { LOG_DEBUG(Input, "Start NFC pooling Mode"); + ScopedSetBlocking sb(this); DriverResult result{DriverResult::Success}; TagFoundData tag_data{}; - SetBlocking(); if (result == DriverResult::Success) { result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::NFC); @@ -70,15 +68,14 @@ DriverResult NfcProtocol::StartNFCPollingMode() { is_enabled = true; } - SetNonBlocking(); return result; } DriverResult NfcProtocol::ScanAmiibo(std::vector& data) { LOG_DEBUG(Input, "Start NFC pooling Mode"); + ScopedSetBlocking sb(this); DriverResult result{DriverResult::Success}; TagFoundData tag_data{}; - SetBlocking(); if (result == DriverResult::Success) { result = StartPolling(tag_data); @@ -96,20 +93,18 @@ DriverResult NfcProtocol::ScanAmiibo(std::vector& data) { result = GetAmiiboData(data); } - SetNonBlocking(); return result; } bool NfcProtocol::HasAmiibo() { + ScopedSetBlocking sb(this); DriverResult result{DriverResult::Success}; TagFoundData tag_data{}; - SetBlocking(); if (result == DriverResult::Success) { result = StartPolling(tag_data); } - SetNonBlocking(); return result == DriverResult::Success; } @@ -169,55 +164,53 @@ DriverResult NfcProtocol::ReadTag(const TagFoundData& data) { LOG_INFO(Input, "Tag detected, type={}, uuid={}", data.type, uuid_string); tries = 0; - std::size_t ntag_pages = 0; + NFCPages ntag_pages = NFCPages::Block0; // Read Tag data -loop1: while (true) { auto result = SendReadAmiiboRequest(output, ntag_pages); + const auto mcu_report = static_cast(output[49]); + const auto nfc_status = static_cast(output[56]); - int attempt = 0; - while (1) { - if (attempt != 0) { - result = GetMCUDataResponse(ReportMode::NFC_IR_MODE_60HZ, output); + if (result != DriverResult::Success) { + return result; + } + + if ((mcu_report == MCUReport::NFCReadData || mcu_report == MCUReport::NFCState) && + nfc_status == NFCStatus::TagLost) { + return DriverResult::ErrorReadingData; + } + + if (mcu_report == MCUReport::NFCReadData && output[51] == 0x07 && output[52] == 0x01) { + if (data.type != 2) { + continue; } - if ((output[49] == 0x3a || output[49] == 0x2a) && output[56] == 0x07) { + switch (output[74]) { + case 0: + ntag_pages = NFCPages::Block135; + break; + case 3: + ntag_pages = NFCPages::Block45; + break; + case 4: + ntag_pages = NFCPages::Block231; + break; + default: return DriverResult::ErrorReadingData; } - if (output[49] == 0x3a && output[51] == 0x07 && output[52] == 0x01) { - if (data.type != 2) { - goto loop1; - } - switch (output[74]) { - case 0: - ntag_pages = 135; - break; - case 3: - ntag_pages = 45; - break; - case 4: - ntag_pages = 231; - break; - default: - return DriverResult::ErrorReadingData; - } - goto loop1; - } - if (output[49] == 0x2a && output[56] == 0x04) { - // finished - SendStopPollingRequest(output); - return DriverResult::Success; - } - if (output[49] == 0x2a) { - goto loop1; - } - if (attempt++ > 6) { - goto loop1; - } + continue; } - if (result != DriverResult::Success) { - return result; + if (mcu_report == MCUReport::NFCState && nfc_status == NFCStatus::LastPackage) { + // finished + SendStopPollingRequest(output); + return DriverResult::Success; + } + + // Ignore other state reports + if (mcu_report == MCUReport::NFCState) { + continue; } + if (tries++ > timeout_limit) { return DriverResult::Timeout; } @@ -231,47 +224,44 @@ DriverResult NfcProtocol::GetAmiiboData(std::vector& ntag_data) { std::vector output; std::size_t tries = 0; - std::size_t ntag_pages = 135; + NFCPages ntag_pages = NFCPages::Block135; std::size_t ntag_buffer_pos = 0; // Read Tag data -loop1: while (true) { auto result = SendReadAmiiboRequest(output, ntag_pages); + const auto mcu_report = static_cast(output[49]); + const auto nfc_status = static_cast(output[56]); - int attempt = 0; - while (1) { - if (attempt != 0) { - result = GetMCUDataResponse(ReportMode::NFC_IR_MODE_60HZ, output); - } - if ((output[49] == 0x3a || output[49] == 0x2a) && output[56] == 0x07) { - return DriverResult::ErrorReadingData; - } - if (output[49] == 0x3a && output[51] == 0x07) { - std::size_t payload_size = (output[54] << 8 | output[55]) & 0x7FF; - if (output[52] == 0x01) { - memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 116, - payload_size - 60); - ntag_buffer_pos += payload_size - 60; - } else { - memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 56, payload_size); - } - goto loop1; - } - if (output[49] == 0x2a && output[56] == 0x04) { - LOG_INFO(Input, "Finished reading amiibo"); - return DriverResult::Success; - } - if (output[49] == 0x2a) { - goto loop1; - } - if (attempt++ > 4) { - goto loop1; + if (result != DriverResult::Success) { + return result; + } + + if ((mcu_report == MCUReport::NFCReadData || mcu_report == MCUReport::NFCState) && + nfc_status == NFCStatus::TagLost) { + return DriverResult::ErrorReadingData; + } + + if (mcu_report == MCUReport::NFCReadData && output[51] == 0x07) { + std::size_t payload_size = (output[54] << 8 | output[55]) & 0x7FF; + if (output[52] == 0x01) { + memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 116, payload_size - 60); + ntag_buffer_pos += payload_size - 60; + } else { + memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 56, payload_size); } + continue; } - if (result != DriverResult::Success) { - return result; + if (mcu_report == MCUReport::NFCState && nfc_status == NFCStatus::LastPackage) { + LOG_INFO(Input, "Finished reading amiibo"); + return DriverResult::Success; + } + + // Ignore other state reports + if (mcu_report == MCUReport::NFCState) { + continue; } + if (tries++ > timeout_limit) { return DriverResult::Timeout; } @@ -298,7 +288,7 @@ DriverResult NfcProtocol::SendStartPollingRequest(std::vector& output) { .crc = {}, }; - std::vector request_data(sizeof(NFCRequestState)); + std::array request_data{}; memcpy(request_data.data(), &request, sizeof(NFCRequestState)); request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); @@ -315,7 +305,7 @@ DriverResult NfcProtocol::SendStopPollingRequest(std::vector& output) { .crc = {}, }; - std::vector request_data(sizeof(NFCRequestState)); + std::array request_data{}; memcpy(request_data.data(), &request, sizeof(NFCRequestState)); request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); @@ -338,7 +328,7 @@ DriverResult NfcProtocol::SendStartWaitingRecieveRequest(std::vector& output return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); } -DriverResult NfcProtocol::SendReadAmiiboRequest(std::vector& output, std::size_t ntag_pages) { +DriverResult NfcProtocol::SendReadAmiiboRequest(std::vector& output, NFCPages ntag_pages) { NFCRequestState request{ .sub_command = MCUSubCommand::ReadDeviceMode, .command_argument = NFCReadCommand::Ntag, @@ -357,20 +347,19 @@ DriverResult NfcProtocol::SendReadAmiiboRequest(std::vector& output, std::si .crc = {}, }; - std::vector request_data(sizeof(NFCRequestState)); + std::array request_data{}; memcpy(request_data.data(), &request, sizeof(NFCRequestState)); request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36); return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output); } -NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const { - if (pages == 0) { +NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(NFCPages pages) const { + switch (pages) { + case NFCPages::Block0: return { .block_count = 1, }; - } - - if (pages == 45) { + case NFCPages::Block45: return { .block_count = 1, .blocks = @@ -378,9 +367,7 @@ NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const { NFCReadBlock{0x00, 0x2C}, }, }; - } - - if (pages == 135) { + case NFCPages::Block135: return { .block_count = 3, .blocks = @@ -390,9 +377,7 @@ NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const { {0x78, 0x86}, }, }; - } - - if (pages == 231) { + case NFCPages::Block231: return { .block_count = 4, .blocks = @@ -403,9 +388,9 @@ NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const { {0xb4, 0xe6}, }, }; - } - - return {}; + default: + return {}; + }; } bool NfcProtocol::IsEnabled() const { -- cgit v1.2.3