summaryrefslogtreecommitdiff
path: root/src/input_common/helpers/joycon_protocol/nfc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/helpers/joycon_protocol/nfc.cpp')
-rw-r--r--src/input_common/helpers/joycon_protocol/nfc.cpp414
1 files changed, 414 insertions, 0 deletions
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 @@
1// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include <thread>
5#include "common/logging/log.h"
6#include "input_common/helpers/joycon_protocol/nfc.h"
7
8namespace InputCommon::Joycon {
9
10NfcProtocol::NfcProtocol(std::shared_ptr<JoyconHandle> handle) : JoyconCommonProtocol(handle) {}
11
12DriverResult NfcProtocol::EnableNfc() {
13 LOG_INFO(Input, "Enable NFC");
14 DriverResult result{DriverResult::Success};
15 SetBlocking();
16
17 if (result == DriverResult::Success) {
18 result = SetReportMode(ReportMode::NFC_IR_MODE_60HZ);
19 }
20 if (result == DriverResult::Success) {
21 result = EnableMCU(true);
22 }
23 if (result == DriverResult::Success) {
24 result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::Standby);
25 }
26 if (result == DriverResult::Success) {
27 const MCUConfig config{
28 .command = MCUCommand::ConfigureMCU,
29 .sub_command = MCUSubCommand::SetMCUMode,
30 .mode = MCUMode::NFC,
31 .crc = {},
32 };
33
34 result = ConfigureMCU(config);
35 }
36
37 SetNonBlocking();
38 return result;
39}
40
41DriverResult NfcProtocol::DisableNfc() {
42 LOG_DEBUG(Input, "Disable NFC");
43 DriverResult result{DriverResult::Success};
44 SetBlocking();
45
46 if (result == DriverResult::Success) {
47 result = EnableMCU(false);
48 }
49
50 is_enabled = false;
51
52 SetNonBlocking();
53 return result;
54}
55
56DriverResult NfcProtocol::StartNFCPollingMode() {
57 LOG_DEBUG(Input, "Start NFC pooling Mode");
58 DriverResult result{DriverResult::Success};
59 TagFoundData tag_data{};
60 SetBlocking();
61
62 if (result == DriverResult::Success) {
63 result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::NFC);
64 }
65 if (result == DriverResult::Success) {
66 result = WaitUntilNfcIsReady();
67 }
68 if (result == DriverResult::Success) {
69 is_enabled = true;
70 }
71
72 SetNonBlocking();
73 return result;
74}
75
76DriverResult NfcProtocol::ScanAmiibo(std::vector<u8>& data) {
77 LOG_DEBUG(Input, "Start NFC pooling Mode");
78 DriverResult result{DriverResult::Success};
79 TagFoundData tag_data{};
80 SetBlocking();
81
82 if (result == DriverResult::Success) {
83 result = StartPolling(tag_data);
84 }
85 if (result == DriverResult::Success) {
86 result = ReadTag(tag_data);
87 }
88 if (result == DriverResult::Success) {
89 result = WaitUntilNfcIsReady();
90 }
91 if (result == DriverResult::Success) {
92 result = StartPolling(tag_data);
93 }
94 if (result == DriverResult::Success) {
95 result = GetAmiiboData(data);
96 }
97
98 SetNonBlocking();
99 return result;
100}
101
102bool NfcProtocol::HasAmiibo() {
103 DriverResult result{DriverResult::Success};
104 TagFoundData tag_data{};
105 SetBlocking();
106
107 if (result == DriverResult::Success) {
108 result = StartPolling(tag_data);
109 }
110
111 SetNonBlocking();
112 return result == DriverResult::Success;
113}
114
115DriverResult NfcProtocol::WaitUntilNfcIsReady() {
116 constexpr std::size_t timeout_limit = 10;
117 std::vector<u8> output;
118 std::size_t tries = 0;
119
120 do {
121 auto result = SendStartWaitingRecieveRequest(output);
122
123 if (result != DriverResult::Success) {
124 return result;
125 }
126 if (tries++ > timeout_limit) {
127 return DriverResult::Timeout;
128 }
129 } while (output[49] != 0x2a || (output[51] << 8) + output[50] != 0x0500 || output[55] != 0x31 ||
130 output[56] != 0x00);
131
132 return DriverResult::Success;
133}
134
135DriverResult NfcProtocol::StartPolling(TagFoundData& data) {
136 LOG_DEBUG(Input, "Start Polling for tag");
137 constexpr std::size_t timeout_limit = 7;
138 std::vector<u8> output;
139 std::size_t tries = 0;
140
141 do {
142 const auto result = SendStartPollingRequest(output);
143 if (result != DriverResult::Success) {
144 return result;
145 }
146 if (tries++ > timeout_limit) {
147 return DriverResult::Timeout;
148 }
149 } while (output[49] != 0x2a || (output[51] << 8) + output[50] != 0x0500 || output[56] != 0x09);
150
151 data.type = output[62];
152 data.uuid.resize(output[64]);
153 memcpy(data.uuid.data(), output.data() + 65, data.uuid.size());
154
155 return DriverResult::Success;
156}
157
158DriverResult NfcProtocol::ReadTag(const TagFoundData& data) {
159 constexpr std::size_t timeout_limit = 10;
160 std::vector<u8> output;
161 std::size_t tries = 0;
162
163 std::string uuid_string = "";
164 for (auto& content : data.uuid) {
165 uuid_string += " " + fmt::format("{:02x}", content);
166 }
167
168 LOG_INFO(Input, "Tag detected, type={}, uuid={}", data.type, uuid_string);
169
170 tries = 0;
171 std::size_t ntag_pages = 0;
172 // Read Tag data
173loop1:
174 while (true) {
175 auto result = SendReadAmiiboRequest(output, ntag_pages);
176
177 int attempt = 0;
178 while (1) {
179 if (attempt != 0) {
180 result = GetMCUDataResponse(ReportMode::NFC_IR_MODE_60HZ, output);
181 }
182 if ((output[49] == 0x3a || output[49] == 0x2a) && output[56] == 0x07) {
183 return DriverResult::ErrorReadingData;
184 }
185 if (output[49] == 0x3a && output[51] == 0x07 && output[52] == 0x01) {
186 if (data.type != 2) {
187 goto loop1;
188 }
189 switch (output[74]) {
190 case 0:
191 ntag_pages = 135;
192 break;
193 case 3:
194 ntag_pages = 45;
195 break;
196 case 4:
197 ntag_pages = 231;
198 break;
199 default:
200 return DriverResult::ErrorReadingData;
201 }
202 goto loop1;
203 }
204 if (output[49] == 0x2a && output[56] == 0x04) {
205 // finished
206 SendStopPollingRequest(output);
207 return DriverResult::Success;
208 }
209 if (output[49] == 0x2a) {
210 goto loop1;
211 }
212 if (attempt++ > 6) {
213 goto loop1;
214 }
215 }
216
217 if (result != DriverResult::Success) {
218 return result;
219 }
220 if (tries++ > timeout_limit) {
221 return DriverResult::Timeout;
222 }
223 }
224
225 return DriverResult::Success;
226}
227
228DriverResult NfcProtocol::GetAmiiboData(std::vector<u8>& ntag_data) {
229 constexpr std::size_t timeout_limit = 10;
230 std::vector<u8> output;
231 std::size_t tries = 0;
232
233 std::size_t ntag_pages = 135;
234 std::size_t ntag_buffer_pos = 0;
235 // Read Tag data
236loop1:
237 while (true) {
238 auto result = SendReadAmiiboRequest(output, ntag_pages);
239
240 int attempt = 0;
241 while (1) {
242 if (attempt != 0) {
243 result = GetMCUDataResponse(ReportMode::NFC_IR_MODE_60HZ, output);
244 }
245 if ((output[49] == 0x3a || output[49] == 0x2a) && output[56] == 0x07) {
246 return DriverResult::ErrorReadingData;
247 }
248 if (output[49] == 0x3a && output[51] == 0x07) {
249 std::size_t payload_size = (output[54] << 8 | output[55]) & 0x7FF;
250 if (output[52] == 0x01) {
251 memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 116,
252 payload_size - 60);
253 ntag_buffer_pos += payload_size - 60;
254 } else {
255 memcpy(ntag_data.data() + ntag_buffer_pos, output.data() + 56, payload_size);
256 }
257 goto loop1;
258 }
259 if (output[49] == 0x2a && output[56] == 0x04) {
260 LOG_INFO(Input, "Finished reading amiibo");
261 return DriverResult::Success;
262 }
263 if (output[49] == 0x2a) {
264 goto loop1;
265 }
266 if (attempt++ > 4) {
267 goto loop1;
268 }
269 }
270
271 if (result != DriverResult::Success) {
272 return result;
273 }
274 if (tries++ > timeout_limit) {
275 return DriverResult::Timeout;
276 }
277 }
278
279 return DriverResult::Success;
280}
281
282DriverResult NfcProtocol::SendStartPollingRequest(std::vector<u8>& output) {
283 NFCRequestState request{
284 .sub_command = MCUSubCommand::ReadDeviceMode,
285 .command_argument = NFCReadCommand::StartPolling,
286 .packet_id = 0x0,
287 .packet_flag = MCUPacketFlag::LastCommandPacket,
288 .data_length = sizeof(NFCPollingCommandData),
289 .nfc_polling =
290 {
291 .enable_mifare = 0x01,
292 .unknown_1 = 0x00,
293 .unknown_2 = 0x00,
294 .unknown_3 = 0x2c,
295 .unknown_4 = 0x01,
296 },
297 .crc = {},
298 };
299
300 std::vector<u8> request_data(sizeof(NFCRequestState));
301 memcpy(request_data.data(), &request, sizeof(NFCRequestState));
302 request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
303 return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output);
304}
305
306DriverResult NfcProtocol::SendStopPollingRequest(std::vector<u8>& output) {
307 NFCRequestState request{
308 .sub_command = MCUSubCommand::ReadDeviceMode,
309 .command_argument = NFCReadCommand::StopPolling,
310 .packet_id = 0x0,
311 .packet_flag = MCUPacketFlag::LastCommandPacket,
312 .data_length = 0,
313 .raw_data = {},
314 .crc = {},
315 };
316
317 std::vector<u8> request_data(sizeof(NFCRequestState));
318 memcpy(request_data.data(), &request, sizeof(NFCRequestState));
319 request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
320 return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output);
321}
322
323DriverResult NfcProtocol::SendStartWaitingRecieveRequest(std::vector<u8>& output) {
324 NFCRequestState request{
325 .sub_command = MCUSubCommand::ReadDeviceMode,
326 .command_argument = NFCReadCommand::StartWaitingRecieve,
327 .packet_id = 0x0,
328 .packet_flag = MCUPacketFlag::LastCommandPacket,
329 .data_length = 0,
330 .raw_data = {},
331 .crc = {},
332 };
333
334 std::vector<u8> request_data(sizeof(NFCRequestState));
335 memcpy(request_data.data(), &request, sizeof(NFCRequestState));
336 request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
337 return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output);
338}
339
340DriverResult NfcProtocol::SendReadAmiiboRequest(std::vector<u8>& output, std::size_t ntag_pages) {
341 NFCRequestState request{
342 .sub_command = MCUSubCommand::ReadDeviceMode,
343 .command_argument = NFCReadCommand::Ntag,
344 .packet_id = 0x0,
345 .packet_flag = MCUPacketFlag::LastCommandPacket,
346 .data_length = sizeof(NFCReadCommandData),
347 .nfc_read =
348 {
349 .unknown = 0xd0,
350 .uuid_length = 0x07,
351 .unknown_2 = 0x00,
352 .uid = {},
353 .tag_type = NFCTagType::AllTags,
354 .read_block = GetReadBlockCommand(ntag_pages),
355 },
356 .crc = {},
357 };
358
359 std::vector<u8> request_data(sizeof(NFCRequestState));
360 memcpy(request_data.data(), &request, sizeof(NFCRequestState));
361 request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
362 return SendMCUData(ReportMode::NFC_IR_MODE_60HZ, SubCommand::STATE, request_data, output);
363}
364
365NFCReadBlockCommand NfcProtocol::GetReadBlockCommand(std::size_t pages) const {
366 if (pages == 0) {
367 return {
368 .block_count = 1,
369 };
370 }
371
372 if (pages == 45) {
373 return {
374 .block_count = 1,
375 .blocks =
376 {
377 NFCReadBlock{0x00, 0x2C},
378 },
379 };
380 }
381
382 if (pages == 135) {
383 return {
384 .block_count = 3,
385 .blocks =
386 {
387 NFCReadBlock{0x00, 0x3b},
388 {0x3c, 0x77},
389 {0x78, 0x86},
390 },
391 };
392 }
393
394 if (pages == 231) {
395 return {
396 .block_count = 4,
397 .blocks =
398 {
399 NFCReadBlock{0x00, 0x3b},
400 {0x3c, 0x77},
401 {0x78, 0x83},
402 {0xb4, 0xe6},
403 },
404 };
405 }
406
407 return {};
408}
409
410bool NfcProtocol::IsEnabled() {
411 return is_enabled;
412}
413
414} // namespace InputCommon::Joycon