yuzu/src/input_common/helpers/joycon_protocol/nfc.cpp

411 lines
12 KiB
C++
Raw Normal View History

2022-12-22 02:07:46 -05:00
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <thread>
#include "common/logging/log.h"
#include "input_common/helpers/joycon_protocol/nfc.h"
namespace InputCommon::Joycon {
2022-12-23 09:32:02 -05:00
NfcProtocol::NfcProtocol(std::shared_ptr<JoyconHandle> handle)
: JoyconCommonProtocol(std::move(handle)) {}
2022-12-22 02:07:46 -05:00
DriverResult NfcProtocol::EnableNfc() {
LOG_INFO(Input, "Enable NFC");
2023-01-14 00:29:05 -05:00
ScopedSetBlocking sb(this);
2022-12-22 02:07:46 -05:00
DriverResult result{DriverResult::Success};
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);
}
return result;
}
DriverResult NfcProtocol::DisableNfc() {
LOG_DEBUG(Input, "Disable NFC");
2023-01-14 00:29:05 -05:00
ScopedSetBlocking sb(this);
2022-12-22 02:07:46 -05:00
DriverResult result{DriverResult::Success};
if (result == DriverResult::Success) {
result = EnableMCU(false);
}
is_enabled = false;
return result;
}
DriverResult NfcProtocol::StartNFCPollingMode() {
LOG_DEBUG(Input, "Start NFC pooling Mode");
2023-01-14 00:29:05 -05:00
ScopedSetBlocking sb(this);
2022-12-22 02:07:46 -05:00
DriverResult result{DriverResult::Success};
TagFoundData tag_data{};
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;
}
return result;
}
DriverResult NfcProtocol::ScanAmiibo(std::vector<u8>& data) {
if (update_counter++ < AMIIBO_UPDATE_DELAY) {
return DriverResult::Delayed;
}
update_counter = 0;
2022-12-22 02:07:46 -05:00
LOG_DEBUG(Input, "Start NFC pooling Mode");
2023-01-14 00:29:05 -05:00
ScopedSetBlocking sb(this);
2022-12-22 02:07:46 -05:00
DriverResult result{DriverResult::Success};
TagFoundData tag_data{};
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, 7);
2022-12-22 02:07:46 -05:00
}
if (result == DriverResult::Success) {
result = GetAmiiboData(data);
}
return result;
}
bool NfcProtocol::HasAmiibo() {
2023-01-14 00:29:05 -05:00
ScopedSetBlocking sb(this);
2022-12-22 02:07:46 -05:00
DriverResult result{DriverResult::Success};
TagFoundData tag_data{};
if (result == DriverResult::Success) {
result = StartPolling(tag_data);
}
return result == DriverResult::Success;
}
DriverResult NfcProtocol::WaitUntilNfcIsReady() {
constexpr std::size_t timeout_limit = 10;
MCUCommandResponse output{};
2022-12-22 02:07:46 -05:00
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.mcu_report != MCUReport::NFCState ||
(output.mcu_data[1] << 8) + output.mcu_data[0] != 0x0500 ||
output.mcu_data[5] != 0x31 || output.mcu_data[6] != 0x00);
2022-12-22 02:07:46 -05:00
return DriverResult::Success;
}
DriverResult NfcProtocol::StartPolling(TagFoundData& data, std::size_t timeout_limit) {
2022-12-22 02:07:46 -05:00
LOG_DEBUG(Input, "Start Polling for tag");
MCUCommandResponse output{};
2022-12-22 02:07:46 -05:00
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.mcu_report != MCUReport::NFCState ||
(output.mcu_data[1] << 8) + output.mcu_data[0] != 0x0500 ||
output.mcu_data[6] != 0x09);
2022-12-22 02:07:46 -05:00
data.type = output.mcu_data[12];
data.uuid.resize(output.mcu_data[14]);
memcpy(data.uuid.data(), output.mcu_data.data() + 15, data.uuid.size());
2022-12-22 02:07:46 -05:00
return DriverResult::Success;
}
DriverResult NfcProtocol::ReadTag(const TagFoundData& data) {
constexpr std::size_t timeout_limit = 10;
MCUCommandResponse output{};
2022-12-22 02:07:46 -05:00
std::size_t tries = 0;
2022-12-23 09:32:02 -05:00
std::string uuid_string;
2022-12-22 02:07:46 -05:00
for (auto& content : data.uuid) {
2022-12-23 09:32:02 -05:00
uuid_string += fmt::format(" {:02x}", content);
2022-12-22 02:07:46 -05:00
}
LOG_INFO(Input, "Tag detected, type={}, uuid={}", data.type, uuid_string);
tries = 0;
2023-01-14 00:29:05 -05:00
NFCPages ntag_pages = NFCPages::Block0;
2022-12-22 02:07:46 -05:00
// Read Tag data
while (true) {
auto result = SendReadAmiiboRequest(output, ntag_pages);
const auto nfc_status = static_cast<NFCStatus>(output.mcu_data[6]);
2022-12-22 02:07:46 -05:00
2023-01-14 00:29:05 -05:00
if (result != DriverResult::Success) {
return result;
}
if ((output.mcu_report == MCUReport::NFCReadData ||
output.mcu_report == MCUReport::NFCState) &&
2023-01-14 00:29:05 -05:00
nfc_status == NFCStatus::TagLost) {
return DriverResult::ErrorReadingData;
}
if (output.mcu_report == MCUReport::NFCReadData && output.mcu_data[1] == 0x07 &&
output.mcu_data[2] == 0x01) {
2023-01-14 00:29:05 -05:00
if (data.type != 2) {
continue;
2022-12-22 02:07:46 -05:00
}
switch (output.mcu_data[24]) {
2023-01-14 00:29:05 -05:00
case 0:
ntag_pages = NFCPages::Block135;
break;
case 3:
ntag_pages = NFCPages::Block45;
break;
case 4:
ntag_pages = NFCPages::Block231;
break;
default:
2022-12-22 02:07:46 -05:00
return DriverResult::ErrorReadingData;
}
2023-01-14 00:29:05 -05:00
continue;
2022-12-22 02:07:46 -05:00
}
if (output.mcu_report == MCUReport::NFCState && nfc_status == NFCStatus::LastPackage) {
2023-01-14 00:29:05 -05:00
// finished
SendStopPollingRequest(output);
return DriverResult::Success;
}
// Ignore other state reports
if (output.mcu_report == MCUReport::NFCState) {
2023-01-14 00:29:05 -05:00
continue;
2022-12-22 02:07:46 -05:00
}
2023-01-14 00:29:05 -05:00
2022-12-22 02:07:46 -05:00
if (tries++ > timeout_limit) {
return DriverResult::Timeout;
}
}
return DriverResult::Success;
}
DriverResult NfcProtocol::GetAmiiboData(std::vector<u8>& ntag_data) {
constexpr std::size_t timeout_limit = 10;
MCUCommandResponse output{};
2022-12-22 02:07:46 -05:00
std::size_t tries = 0;
2023-01-14 00:29:05 -05:00
NFCPages ntag_pages = NFCPages::Block135;
2022-12-22 02:07:46 -05:00
std::size_t ntag_buffer_pos = 0;
// Read Tag data
while (true) {
auto result = SendReadAmiiboRequest(output, ntag_pages);
const auto nfc_status = static_cast<NFCStatus>(output.mcu_data[6]);
2022-12-22 02:07:46 -05:00
2023-01-14 00:29:05 -05:00
if (result != DriverResult::Success) {
return result;
}
if ((output.mcu_report == MCUReport::NFCReadData ||
output.mcu_report == MCUReport::NFCState) &&
2023-01-14 00:29:05 -05:00
nfc_status == NFCStatus::TagLost) {
return DriverResult::ErrorReadingData;
}
if (output.mcu_report == MCUReport::NFCReadData && output.mcu_data[1] == 0x07) {
std::size_t payload_size = (output.mcu_data[4] << 8 | output.mcu_data[5]) & 0x7FF;
if (output.mcu_data[2] == 0x01) {
memcpy(ntag_data.data() + ntag_buffer_pos, output.mcu_data.data() + 66,
payload_size - 60);
2023-01-14 00:29:05 -05:00
ntag_buffer_pos += payload_size - 60;
} else {
memcpy(ntag_data.data() + ntag_buffer_pos, output.mcu_data.data() + 6,
payload_size);
2022-12-22 02:07:46 -05:00
}
2023-01-14 00:29:05 -05:00
continue;
2022-12-22 02:07:46 -05:00
}
if (output.mcu_report == MCUReport::NFCState && nfc_status == NFCStatus::LastPackage) {
2023-01-14 00:29:05 -05:00
LOG_INFO(Input, "Finished reading amiibo");
return DriverResult::Success;
}
// Ignore other state reports
if (output.mcu_report == MCUReport::NFCState) {
2023-01-14 00:29:05 -05:00
continue;
2022-12-22 02:07:46 -05:00
}
2023-01-14 00:29:05 -05:00
2022-12-22 02:07:46 -05:00
if (tries++ > timeout_limit) {
return DriverResult::Timeout;
}
}
return DriverResult::Success;
}
DriverResult NfcProtocol::SendStartPollingRequest(MCUCommandResponse& output) {
2022-12-22 02:07:46 -05:00
NFCRequestState request{
.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 = {},
};
2023-01-14 00:29:05 -05:00
std::array<u8, sizeof(NFCRequestState)> request_data{};
2022-12-22 02:07:46 -05:00
memcpy(request_data.data(), &request, sizeof(NFCRequestState));
request_data[36] = CalculateMCU_CRC8(request_data.data(), 36);
return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, MCUSubCommand::ReadDeviceMode, request_data,
output);
2022-12-22 02:07:46 -05:00
}
DriverResult NfcProtocol::SendStopPollingRequest(MCUCommandResponse& output) {
2022-12-22 02:07:46 -05:00
NFCRequestState request{
.command_argument = NFCReadCommand::StopPolling,
.packet_id = 0x0,
.packet_flag = MCUPacketFlag::LastCommandPacket,
.data_length = 0,
.raw_data = {},
.crc = {},
};
2023-01-14 00:29:05 -05:00
std::array<u8, sizeof(NFCRequestState)> request_data{};
2022-12-22 02:07:46 -05:00
memcpy(request_data.data(), &request, sizeof(NFCRequestState));
request_data[36] = CalculateMCU_CRC8(request_data.data(), 36);
return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, MCUSubCommand::ReadDeviceMode, request_data,
output);
2022-12-22 02:07:46 -05:00
}
DriverResult NfcProtocol::SendStartWaitingRecieveRequest(MCUCommandResponse& output) {
2022-12-22 02:07:46 -05:00
NFCRequestState request{
.command_argument = NFCReadCommand::StartWaitingRecieve,
.packet_id = 0x0,
.packet_flag = MCUPacketFlag::LastCommandPacket,
.data_length = 0,
.raw_data = {},
.crc = {},
};
std::vector<u8> request_data(sizeof(NFCRequestState));
memcpy(request_data.data(), &request, sizeof(NFCRequestState));
request_data[36] = CalculateMCU_CRC8(request_data.data(), 36);
return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, MCUSubCommand::ReadDeviceMode, request_data,
output);
2022-12-22 02:07:46 -05:00
}
DriverResult NfcProtocol::SendReadAmiiboRequest(MCUCommandResponse& output, NFCPages ntag_pages) {
2022-12-22 02:07:46 -05:00
NFCRequestState request{
.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 = {},
};
2023-01-14 00:29:05 -05:00
std::array<u8, sizeof(NFCRequestState)> request_data{};
2022-12-22 02:07:46 -05:00
memcpy(request_data.data(), &request, sizeof(NFCRequestState));
request_data[36] = CalculateMCU_CRC8(request_data.data(), 36);
return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, MCUSubCommand::ReadDeviceMode, request_data,
output);
2022-12-22 02:07:46 -05:00
}
2023-01-14 00:29:05 -05:00
NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(NFCPages pages) const {
switch (pages) {
case NFCPages::Block0:
2022-12-22 02:07:46 -05:00
return {
.block_count = 1,
};
2023-01-14 00:29:05 -05:00
case NFCPages::Block45:
2022-12-22 02:07:46 -05:00
return {
.block_count = 1,
.blocks =
{
NFCReadBlock{0x00, 0x2C},
},
};
2023-01-14 00:29:05 -05:00
case NFCPages::Block135:
2022-12-22 02:07:46 -05:00
return {
.block_count = 3,
.blocks =
{
NFCReadBlock{0x00, 0x3b},
{0x3c, 0x77},
{0x78, 0x86},
},
};
2023-01-14 00:29:05 -05:00
case NFCPages::Block231:
2022-12-22 02:07:46 -05:00
return {
.block_count = 4,
.blocks =
{
NFCReadBlock{0x00, 0x3b},
{0x3c, 0x77},
{0x78, 0x83},
{0xb4, 0xe6},
},
};
2023-01-14 00:29:05 -05:00
default:
return {};
};
2022-12-22 02:07:46 -05:00
}
2022-12-23 09:32:02 -05:00
bool NfcProtocol::IsEnabled() const {
2022-12-22 02:07:46 -05:00
return is_enabled;
}
} // namespace InputCommon::Joycon