diff --git a/jctool/FormJoy.h b/jctool/FormJoy.h index 67f2c5c..5acf86e 100644 --- a/jctool/FormJoy.h +++ b/jctool/FormJoy.h @@ -359,7 +359,7 @@ public ref class FormJoy : public System::Windows::Forms::Form private: System::Windows::Forms::Label^ lbl_mainStickHelp; private: System::Windows::Forms::NumericUpDown^ numeric_StickParamRangeRatio2; private: System::Windows::Forms::NumericUpDown^ numeric_StickParamDeadzone2; - + public: System::Windows::Forms::TextBox^ txtBox_NFCTag; private: System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(images::typeid)); @@ -517,6 +517,7 @@ public ref class FormJoy : public System::Windows::Forms::Form this->radioBtn_IR120p = (gcnew System::Windows::Forms::RadioButton()); this->radioBtn_IR240p = (gcnew System::Windows::Forms::RadioButton()); this->grpBox_nfc = (gcnew System::Windows::Forms::GroupBox()); + this->txtBox_NFCTag = (gcnew System::Windows::Forms::TextBox()); this->txtBox_nfcUid = (gcnew System::Windows::Forms::TextBox()); this->btn_NFC = (gcnew System::Windows::Forms::Button()); this->lbl_nfcHelp = (gcnew System::Windows::Forms::Label()); @@ -2856,6 +2857,7 @@ public ref class FormJoy : public System::Windows::Forms::Form // this->grpBox_nfc->BackColor = System::Drawing::Color::FromArgb(static_cast(static_cast(70)), static_cast(static_cast(70)), static_cast(static_cast(70))); + this->grpBox_nfc->Controls->Add(this->txtBox_NFCTag); this->grpBox_nfc->Controls->Add(this->txtBox_nfcUid); this->grpBox_nfc->Controls->Add(this->btn_NFC); this->grpBox_nfc->Controls->Add(this->lbl_nfcHelp); @@ -2865,11 +2867,31 @@ public ref class FormJoy : public System::Windows::Forms::Form this->grpBox_nfc->Location = System::Drawing::Point(724, 445); this->grpBox_nfc->Margin = System::Windows::Forms::Padding(0, 0, 14, 0); this->grpBox_nfc->Name = L"grpBox_nfc"; - this->grpBox_nfc->Size = System::Drawing::Size(220, 215); + this->grpBox_nfc->Size = System::Drawing::Size(220, 399); this->grpBox_nfc->TabIndex = 35; this->grpBox_nfc->TabStop = false; this->grpBox_nfc->Text = L"NFC Simple Tag Info"; // + // txtBox_NFCTag + // + this->txtBox_NFCTag->BackColor = System::Drawing::Color::FromArgb(static_cast(static_cast(70)), static_cast(static_cast(70)), + static_cast(static_cast(70))); + this->txtBox_NFCTag->BorderStyle = System::Windows::Forms::BorderStyle::None; + this->txtBox_NFCTag->Font = (gcnew System::Drawing::Font(L"Lucida Console", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, + static_cast(161))); + this->txtBox_NFCTag->ForeColor = System::Drawing::Color::FromArgb(static_cast(static_cast(9)), static_cast(static_cast(255)), + static_cast(static_cast(206))); + this->txtBox_NFCTag->Location = System::Drawing::Point(8, 218); + this->txtBox_NFCTag->Margin = System::Windows::Forms::Padding(0); + this->txtBox_NFCTag->Multiline = true; + this->txtBox_NFCTag->Name = L"txtBox_NFCTag"; + this->txtBox_NFCTag->ReadOnly = true; + this->txtBox_NFCTag->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; + this->txtBox_NFCTag->Size = System::Drawing::Size(203, 165); + this->txtBox_NFCTag->TabIndex = 37; + this->txtBox_NFCTag->TabStop = false; + this->txtBox_NFCTag->Text = L"Tag contents"; + // // txtBox_nfcUid // this->txtBox_nfcUid->BackColor = System::Drawing::Color::FromArgb(static_cast(static_cast(70)), static_cast(static_cast(70)), @@ -2886,7 +2908,7 @@ public ref class FormJoy : public System::Windows::Forms::Form this->txtBox_nfcUid->Size = System::Drawing::Size(203, 37); this->txtBox_nfcUid->TabIndex = 36; this->txtBox_nfcUid->TabStop = false; - this->txtBox_nfcUid->Text = L"Type:\r\nUID:"; + this->txtBox_nfcUid->Text = L"UID:\r\nType:"; // // btn_NFC // @@ -4229,20 +4251,20 @@ public ref class FormJoy : public System::Windows::Forms::Form // Apply body color switch (handle_ok) { - case 1: - MyImage = (cli::safe_cast(resources->GetObject(L"base64_l_joy_body"))); - break; - case 2: - MyImage = (cli::safe_cast(resources->GetObject(L"base64_r_joy_body"))); - break; - case 3: - MyImage = (cli::safe_cast(resources->GetObject(L"base64_pro_body"))); - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_grips_l"))); - MyImageLayer2 = (cli::safe_cast(resources->GetObject(L"base64_pro_grips_r"))); - break; - default: - MyImage = (cli::safe_cast(resources->GetObject(L"base64_pro_body"))); - break; + case 1: + MyImage = (cli::safe_cast(resources->GetObject(L"base64_l_joy_body"))); + break; + case 2: + MyImage = (cli::safe_cast(resources->GetObject(L"base64_r_joy_body"))); + break; + case 3: + MyImage = (cli::safe_cast(resources->GetObject(L"base64_pro_body"))); + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_grips_l"))); + MyImageLayer2 = (cli::safe_cast(resources->GetObject(L"base64_pro_grips_r"))); + break; + default: + MyImage = (cli::safe_cast(resources->GetObject(L"base64_pro_body"))); + break; } // Skip slow SetPixel(). Reduce latency pixel set latency from 842us -> 260ns. System::Drawing::Imaging::BitmapData^ bmd = MyImage->LockBits(System::Drawing::Rectangle(0, 0, MyImage->Width, MyImage->Height), System::Drawing::Imaging::ImageLockMode::ReadOnly, MyImage->PixelFormat); @@ -4328,19 +4350,19 @@ public ref class FormJoy : public System::Windows::Forms::Form // Apply buttons color switch (handle_ok) { - case 1: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_l_joy_buttons"))); - break; - case 2: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_r_joy_buttons"))); - break; - case 3: - MyImage = drawLayeredImage(MyImage, MyImageLayer); // Apply grips layer - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_buttons"))); - break; - default: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_buttons"))); - break; + case 1: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_l_joy_buttons"))); + break; + case 2: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_r_joy_buttons"))); + break; + case 3: + MyImage = drawLayeredImage(MyImage, MyImageLayer); // Apply grips layer + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_buttons"))); + break; + default: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_buttons"))); + break; } // Skip slow SetPixel(). Reduce latency pixel set latency from 842us -> 260ns. bmd = MyImageLayer->LockBits(System::Drawing::Rectangle(0, 0, MyImageLayer->Width, MyImageLayer->Height), System::Drawing::Imaging::ImageLockMode::ReadOnly, MyImageLayer->PixelFormat); @@ -4358,18 +4380,18 @@ public ref class FormJoy : public System::Windows::Forms::Form // Apply outlines switch (handle_ok) { - case 1: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_l_joy_lines"))); - break; - case 2: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_r_joy_lines"))); - break; - case 3: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_lines"))); - break; - default: - MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_lines"))); - break; + case 1: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_l_joy_lines"))); + break; + case 2: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_r_joy_lines"))); + break; + case 3: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_lines"))); + break; + default: + MyImageLayer = (cli::safe_cast(resources->GetObject(L"base64_pro_lines"))); + break; } MyImage = drawLayeredImage(MyImage, MyImageLayer); @@ -6170,35 +6192,35 @@ public ref class FormJoy : public System::Windows::Forms::Form // Get error switch (res) { - case 1: - error_msg = "1ID31"; - break; - case 2: - error_msg = "2MCUON"; - break; - case 3: - error_msg = "3MCUONBUSY"; - break; - case 4: - error_msg = "4MCUMODESET"; - break; - case 5: - error_msg = "5MCUSETBUSY"; - break; - case 6: - error_msg = "6IRMODESET"; - break; - case 7: - error_msg = "7IRSETBUSY"; - break; - case 8: - error_msg = "8IRCFG"; - break; - case 9: - error_msg = "9IRFCFG"; - break; - default: - break; + case 1: + error_msg = "1ID31"; + break; + case 2: + error_msg = "2MCUON"; + break; + case 3: + error_msg = "3MCUONBUSY"; + break; + case 4: + error_msg = "4MCUMODESET"; + break; + case 5: + error_msg = "5MCUSETBUSY"; + break; + case 6: + error_msg = "6IRMODESET"; + break; + case 7: + error_msg = "7IRSETBUSY"; + break; + case 8: + error_msg = "8IRCFG"; + break; + case 9: + error_msg = "9IRFCFG"; + break; + default: + break; } if (res > 0) this->lbl_IRStatus->Text = "Status: Error " + error_msg + "!"; @@ -6347,30 +6369,32 @@ public ref class FormJoy : public System::Windows::Forms::Form // Get error switch (res) { - case 1: - error_msg = "1ID31"; - break; - case 2: - error_msg = "2MCUON"; - break; - case 3: - error_msg = "3MCUONBUSY"; - break; - case 4: - error_msg = "4MCUMODESET"; - break; - case 5: - error_msg = "5MCUSETBUSY"; - break; - case 6: - error_msg = "6NFCPOLL"; - break; - default: - break; + case 1: + error_msg = "1ID31"; + break; + case 2: + error_msg = "2MCUON"; + break; + case 3: + error_msg = "3MCUONBUSY"; + break; + case 4: + error_msg = "4MCUMODESET"; + break; + case 5: + error_msg = "5MCUSETBUSY"; + break; + case 6: + error_msg = "6NFCPOLL"; + break; + default: + break; } if (res > 0) this->txtBox_nfcUid->Text = "Error " + error_msg + "!"; } + this->btn_NFC->Text = "Scan"; + enable_NFCScanning = false; } @@ -6623,6 +6647,53 @@ public ref class FormJoy : public System::Windows::Forms::Form else return false; } + + + public: System::Void show_ntag_contents(u8* ntag_buf, u8 ntag_pages) { + String^ ntag_temp_string = ""; + for (int i = 0; i < ntag_pages; i++) { + ntag_temp_string += String::Format("{0:X2}: ", i); + for (int j = 0; j < 4; j++) + ntag_temp_string += String::Format("{0:X2} ", ntag_buf[i * 4 + j]); + ntag_temp_string += "|"; + for (int j = 0; j < 4; j++) { + if (ntag_buf[i * 4 + j] < 0x20 || ntag_buf[i * 4 + j] > 0x7e) + ntag_temp_string += "."; + else + ntag_temp_string += String::Format("{0:S}", Convert::ToChar(ntag_buf[i * 4 + j])); + } + ntag_temp_string += "|"; + if (i != (ntag_pages - 1)) + ntag_temp_string += "\r\n"; + if (i == 4) { + switch (ntag_pages) { + case 45: + this->txtBox_nfcUid->Text += "213 "; + break; + case 135: + this->txtBox_nfcUid->Text += "215 "; + break; + case 231: + this->txtBox_nfcUid->Text += "216 "; + break; + default: + this->txtBox_nfcUid->Text += "???"; + break; + } + switch (ntag_buf[16]) { + case 0xA5: + this->txtBox_nfcUid->Text += "(Amiibo)"; + break; + case 0x01: + this->txtBox_nfcUid->Text += "(NDEF)"; + break; + default: + break; + } + } + } + this->txtBox_NFCTag->Text = ntag_temp_string; + } }; } diff --git a/jctool/jctool.cpp b/jctool/jctool.cpp index 86b9a64..57a2dd8 100644 --- a/jctool/jctool.cpp +++ b/jctool/jctool.cpp @@ -2243,328 +2243,56 @@ int nfc_tag_info() { // Kudos to Eric Betts (https://github.com/bettse) // // for nfc comm starters // ///////////////////////////////////////////////////// - while (enable_NFCScanning) { - int res; - u8 buf[0x170]; - u8 buf2[0x170]; - static int output_buffer_length = 49; - int error_reading = 0; - int res_get = 0; - // Set input report to x31 + int res; + u8 buf[0x170]; + u8 buf2[0x170]; + static int output_buffer_length = 49; + int error_reading = 0; + int res_get = 0; + u8 tag_uid_buf[10]; + u8 tag_uid_size = 0; + u8 ntag_buffer[924]; + u16 ntag_buffer_pos = 0; + u8 ntag_pages = 0; // Max 231 + u8 tag_type = 0; + u16 payload_size = 0; + bool ntag_init_done = false; + + memset(tag_uid_buf, 0, sizeof(tag_uid_buf)); + memset(ntag_buffer, 0, sizeof(ntag_buffer)); + + // Set input report to x31 + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 1; + hdr->timer = timming_byte & 0xF; + timming_byte++; + pkt->subcmd = 0x03; + pkt->subcmd_arg.arg1 = 0x31; + res = hid_write(handle, buf, output_buffer_length); + int retries = 0; while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 1; - hdr->timer = timming_byte & 0xF; - timming_byte++; - pkt->subcmd = 0x03; - pkt->subcmd_arg.arg1 = 0x31; - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (*(u16*)&buf[0xD] == 0x0380) - goto step1; + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (*(u16*)&buf[0xD] == 0x0380) + goto step1; - retries++; - if (retries > 8 || res == 0) - break; - } - error_reading++; - if (error_reading > 7) { - res_get = 1; - goto step9; - } + retries++; + if (retries > 8 || res == 0) + break; } - - step1: - // Enable MCU - error_reading = 0; - while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 1; - hdr->timer = timming_byte & 0xF; - timming_byte++; - pkt->subcmd = 0x22; - pkt->subcmd_arg.arg1 = 0x1; - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (*(u16*)&buf[0xD] == 0x2280) - goto step2; - - retries++; - if (retries > 8 || res == 0) - break; - } - error_reading++; - if (error_reading > 7) { - res_get = 2; - goto step9; - } + error_reading++; + if (error_reading > 7) { + res_get = 1; + goto step9; } + } - step2: - // Request MCU mode status - error_reading = 0; - while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 0x11; - hdr->timer = timming_byte & 0xF; - timming_byte++; - pkt->subcmd = 0x01; - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (buf[0] == 0x31) { - //if (buf[49] == 0x01 && buf[56] == 0x06) // MCU state is Initializing - // *(u16*)buf[52]LE x04 in lower than 3.89fw, x05 in 3.89 - // *(u16*)buf[54]LE x12 in lower than 3.89fw, x18 in 3.89 - // buf[56]: mcu mode state - if (buf[49] == 0x01 && buf[56] == 0x01) // Mcu mode is Standby - goto step3; - } - retries++; - if (retries > 8 || res == 0) - break; - } - error_reading++; - if (error_reading > 7) { - res_get = 3; - goto step9; - } - } - - step3: - // Set MCU mode - error_reading = 0; - while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 0x01; - hdr->timer = timming_byte & 0xF; - timming_byte++; - pkt->subcmd = 0x21; - - pkt->subcmd_21_21.mcu_cmd = 0x21; // Set MCU mode cmd - pkt->subcmd_21_21.mcu_subcmd = 0x00; // Set MCU mode cmd - pkt->subcmd_21_21.mcu_mode = 0x04; // MCU mode - 1: Standby, 4: NFC, 5: IR, 6: Initializing/FW Update? - - buf[48] = mcu_crc8_calc(buf + 12, 36); - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (buf[0] == 0x21) { - // *(u16*)buf[18]LE x04 in lower than 3.89fw, x05 in 3.89 - // *(u16*)buf[20]LE x12 in lower than 3.89fw, x18 in 3.89 - if (buf[15] == 0x01 && buf[22] == 0x01) // Mcu mode is standby - goto step4; - } - retries++; - if (retries > 8 || res == 0) - break; - } - error_reading++; - if (error_reading > 7) { - res_get = 4; - goto step9; - } - } - - step4: - // Request MCU mode status - error_reading = 0; - while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 0x11; - hdr->timer = timming_byte & 0xF; - timming_byte++; - pkt->subcmd = 0x01; - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (buf[0] == 0x31) { - // *(u16*)buf[52]LE x04 in lower than 3.89fw, x05 in 3.89 - // *(u16*)buf[54]LE x12 in lower than 3.89fw, x18 in 3.89 - if (buf[49] == 0x01 && buf[56] == 0x04) // Mcu mode is NFC - goto step5; - } - retries++; - if (retries > 8 || res == 0) - break; - } - error_reading++; - if (error_reading > 7) { - res_get = 5; - goto step9; - } - } - - step5: - // Request NFC mode status - error_reading = 0; - while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 0x11; - hdr->timer = timming_byte & 0xF; - timming_byte++; - - pkt->subcmd = 0x02; - pkt->subcmd_arg.arg1 = 0x04; // 0: Cancel all, 4: StartWaitingReceive - pkt->subcmd_arg.arg2 = 0x00; // Count of the currecnt packet if the cmd is a series of packets. - buf[13] = 0x00; - buf[14] = 0x08; // 8: Last cmd packet, 0: More cmd packet should be expected - buf[15] = 0x00; // Length of data after cmd header - - buf[47] = mcu_crc8_calc(buf + 11, 36); //Without the last byte - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (buf[0] == 0x31) { - //if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[55] == 0x31 && buf[56] == 0x0b)// buf[56] == 0x0b: Initializing/Busy - if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[55] == 0x31 && buf[56] == 0x00) // buf[56] == 0x00: Awaiting cmd - goto step6; - } - retries++; - if (retries > 4 || res == 0) - break; - } - error_reading++; - if (error_reading > 9) { - res_get = 6; - goto step9; - } - } - - step6: - // Request NFC mode status - while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 0x11; - hdr->timer = timming_byte & 0xF; - timming_byte++; - - pkt->subcmd = 0x02; - pkt->subcmd_arg.arg1 = 0x01; // 1: Start polling, 2: Stop polling, - pkt->subcmd_arg.arg2 = 0x00; // Count of the currecnt packet if the cmd is a series of packets. - buf[13] = 0x00; - buf[14] = 0x08; // 8: Last cmd packet, 0: More cmd packet should be expected - buf[15] = 0x05; // Length of data after cmd header - buf[16] = 0x01; // Enable Mifare support - //buf[17] = 0x00; - //buf[18] = 0x00; - buf[19] = 0x2c; // Some values work (0x07) other don't. - //buf[20] = 0x01; - - buf[47] = mcu_crc8_calc(buf + 11, 36); //Without the last byte - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - if (!enable_NFCScanning) - goto step7; - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (buf[0] == 0x31) { - // buf[49] == 0x2a: NFC MCU input report - // buf[50] shows when there's error? - // buf[51] == 0x05: NFC - // buf[54] always 9? - // buf[55] always x31? - // buf[56]: MCU/NFC state - // buf[62]: nfc tag type - // buf[62]: MSB size? - // buf[64]: size of following data and it's the last NFC header byte - if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[56] == 0x09) { // buf[56] == 0x09: Tag detected - //for (int i = 0; i < 8; i++) { - // printf("%02X ", buf[57 + i]); - //} - //printf(" | "); - //FormJoy::myform1->txtBox_nfcUid->Text = String::Format("{0:d},{1:d},{2:d},{3:d},{4:d}, Type: {5:d}, {6:d}\r\nUID: ", buf[57], buf[58], buf[59], buf[60], buf[61], buf[62], buf[63]); - FormJoy::myform1->txtBox_nfcUid->Text = String::Format("Type: {0:s}\r\nUID: ", buf[62] == 0x2 ? "NTAG" : "MIFARE"); - for (int i = 0; i < buf[64]; i++) { - if (i < buf[64] - 1) { - //printf("%02X:", buf[65 + i]); - FormJoy::myform1->txtBox_nfcUid->Text += String::Format("{0:X2}:", buf[65 + i]); - } - else { - //printf("%02X", buf[65 + i]); - FormJoy::myform1->txtBox_nfcUid->Text += String::Format("{0:X2}", buf[65 + i]); - } - } - //printf("\n"); - Application::DoEvents(); - goto step7; - } - } - retries++; - if (retries > 4 || res == 0) { - Application::DoEvents(); - break; - } - } - } - - step7: - // Read NTAG contents - // TODO: - while (0) { - memset(buf2, 0, sizeof(buf2)); - auto hdr = (brcm_hdr *)buf2; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 0x11; - hdr->timer = timming_byte & 0xF; - timming_byte++; - - pkt->subcmd = 0x02; - pkt->subcmd_arg.arg1 = 0x06; // 6: Read Ntag data, 0xf: Read mifare data - pkt->subcmd_arg.arg2 = 0x00; - buf2[13] = 0x00; - buf2[14] = 0x08; - buf2[15] = 0x0D; // Length of data after cmd header - buf2[16] = 0xd0; - buf2[17] = 0x07; - for (int i = 0; i < 7; i++) - buf2[18 + i] = buf[65 + i]; - buf2[25] = 0x00; //val != 0 - buf2[26] = 0x01; //block count - buf2[27] = 0x05; //block - buf2[28] = 0x13; //? - - buf2[47] = mcu_crc8_calc(buf2 + 11, 36); - buf2[48] = 0xFF; - res = hid_write(handle, buf2, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf2, sizeof(buf2), 64); - if (buf2[0] == 0x31) { - // mode set to 7: Image transfer - if (buf2[49] == 0x2a && buf2[51] == 0x05 && buf[56] == 0x00) - goto step9;//////////////////////// - } - retries++; - if (retries > 4 || res == 0) - break; - } - } - - step9: - // Disable MCU +step1: + // Enable MCU + error_reading = 0; + while (1) { memset(buf, 0, sizeof(buf)); auto hdr = (brcm_hdr *)buf; auto pkt = (brcm_cmd_01 *)(hdr + 1); @@ -2572,43 +2300,459 @@ int nfc_tag_info() { hdr->timer = timming_byte & 0xF; timming_byte++; pkt->subcmd = 0x22; - pkt->subcmd_arg.arg1 = 0x00; + pkt->subcmd_arg.arg1 = 0x1; res = hid_write(handle, buf, output_buffer_length); - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - - - // Set input report to x3f + int retries = 0; while (1) { - memset(buf, 0, sizeof(buf)); - auto hdr = (brcm_hdr *)buf; - auto pkt = (brcm_cmd_01 *)(hdr + 1); - hdr->cmd = 1; - hdr->timer = timming_byte & 0xF; - timming_byte++; - pkt->subcmd = 0x03; - pkt->subcmd_arg.arg1 = 0x3f; - res = hid_write(handle, buf, output_buffer_length); - int retries = 0; - while (1) { - res = hid_read_timeout(handle, buf, sizeof(buf), 64); - if (*(u16*)&buf[0xD] == 0x0380) - goto stepf; + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (*(u16*)&buf[0xD] == 0x2280) + goto step2; - retries++; - if (retries > 8 || res == 0) + retries++; + if (retries > 8 || res == 0) + break; + } + error_reading++; + if (error_reading > 7) { + res_get = 2; + goto step9; + } + } + +step2: + // Request MCU mode status + error_reading = 0; + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x11; + hdr->timer = timming_byte & 0xF; + timming_byte++; + pkt->subcmd = 0x01; + res = hid_write(handle, buf, output_buffer_length); + int retries = 0; + while (1) { + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (buf[0] == 0x31) { + //if (buf[49] == 0x01 && buf[56] == 0x06) // MCU state is Initializing + // *(u16*)buf[52]LE x04 in lower than 3.89fw, x05 in 3.89 + // *(u16*)buf[54]LE x12 in lower than 3.89fw, x18 in 3.89 + // buf[56]: mcu mode state + if (buf[49] == 0x01 && buf[56] == 0x01) // Mcu mode is Standby + goto step3; + } + retries++; + if (retries > 8 || res == 0) + break; + } + error_reading++; + if (error_reading > 7) { + res_get = 3; + goto step9; + } + } + +step3: + // Set MCU mode + error_reading = 0; + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x01; + hdr->timer = timming_byte & 0xF; + timming_byte++; + pkt->subcmd = 0x21; + + pkt->subcmd_21_21.mcu_cmd = 0x21; // Set MCU mode cmd + pkt->subcmd_21_21.mcu_subcmd = 0x00; // Set MCU mode cmd + pkt->subcmd_21_21.mcu_mode = 0x04; // MCU mode - 1: Standby, 4: NFC, 5: IR, 6: Initializing/FW Update? + + buf[48] = mcu_crc8_calc(buf + 12, 36); + res = hid_write(handle, buf, output_buffer_length); + int retries = 0; + while (1) { + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (buf[0] == 0x21) { + // *(u16*)buf[18]LE x04 in lower than 3.89fw, x05 in 3.89 + // *(u16*)buf[20]LE x12 in lower than 3.89fw, x18 in 3.89 + if (buf[15] == 0x01 && buf[22] == 0x01) // Mcu mode is standby + goto step4; + } + retries++; + if (retries > 8 || res == 0) + break; + } + error_reading++; + if (error_reading > 7) { + res_get = 4; + goto step9; + } + } + +step4: + // Request MCU mode status + error_reading = 0; + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x11; + hdr->timer = timming_byte & 0xF; + timming_byte++; + pkt->subcmd = 0x01; + res = hid_write(handle, buf, output_buffer_length); + int retries = 0; + while (1) { + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (buf[0] == 0x31) { + // *(u16*)buf[52]LE x04 in lower than 3.89fw, x05 in 3.89 + // *(u16*)buf[54]LE x12 in lower than 3.89fw, x18 in 3.89 + if (buf[49] == 0x01 && buf[56] == 0x04) // Mcu mode is NFC + goto step5; + } + retries++; + if (retries > 8 || res == 0) + break; + } + error_reading++; + if (error_reading > 7) { + res_get = 5; + goto step9; + } + } + +step5: + // Request NFC mode status + error_reading = 0; + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x11; + hdr->timer = timming_byte & 0xF; + timming_byte++; + + pkt->subcmd = 0x02; + pkt->subcmd_arg.arg1 = 0x04; // 0: Cancel all, 4: StartWaitingReceive + pkt->subcmd_arg.arg2 = 0x00; // Count of the currecnt packet if the cmd is a series of packets. + buf[13] = 0x00; + buf[14] = 0x08; // 8: Last cmd packet, 0: More cmd packet should be expected + buf[15] = 0x00; // Length of data after cmd header + + buf[47] = mcu_crc8_calc(buf + 11, 36); //Without the last byte + res = hid_write(handle, buf, output_buffer_length - 1); + int retries = 0; + while (1) { + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (buf[0] == 0x31) { + if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[55] == 0x31 && buf[56] == 0x0b)// buf[56] == 0x0b: Initializing/Busy + break; + if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[55] == 0x31 && buf[56] == 0x00) // buf[56] == 0x00: Awaiting cmd + goto step6; + } + retries++; + if (retries > 4 || res == 0) + break; + } + error_reading++; + if (error_reading > 9) { + res_get = 6; + goto step9; + } + } + +step6: + // Request NFC mode status + error_reading = 0; + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x11; + hdr->timer = timming_byte & 0xF; + timming_byte++; + + pkt->subcmd = 0x02; + pkt->subcmd_arg.arg1 = 0x01; // 1: Start polling, 2: Stop polling, + pkt->subcmd_arg.arg2 = 0x00; // Count of the currecnt packet if the cmd is a series of packets. + buf[13] = 0x00; + buf[14] = 0x08; // 8: Last cmd packet, 0: More cmd packet should be expected + buf[15] = 0x05; // Length of data after cmd header + buf[16] = 0x01; // 1: Enable Mifare support + buf[17] = 0x00; // Unknown. + buf[18] = 0x00; // Unknown. + buf[19] = 0x2c; // Unknown. Some values work (0x07) other don't. + buf[20] = 0x01; // Unknown. This is not needed but Switch sends it. + + buf[47] = mcu_crc8_calc(buf + 11, 36); //Without the last byte + res = hid_write(handle, buf, output_buffer_length - 1); + int retries = 0; + while (1) { + if (!enable_NFCScanning) + goto step7; + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (buf[0] == 0x31) { + // buf[49] == 0x2a: NFC MCU input report + // buf[50] shows when there's error? + // buf[51] == 0x05: NFC + // buf[54] always 9? + // buf[55] always x31? + // buf[56]: MCU/NFC state + // buf[62]: nfc tag IC + // buf[63]: nfc tag Type + // buf[64]: size of following data and it's the last NFC header byte + if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[56] == 0x09) { // buf[56] == 0x09: Tag detected + tag_uid_size = buf[64]; + FormJoy::myform1->txtBox_nfcUid->Text = "UID: "; + for (int i = 0; i < tag_uid_size; i++) { + if (i < tag_uid_size - 1) { + tag_type = buf[62]; // Save tag type + tag_uid_buf[i] = buf[65 + i]; // Save UID + FormJoy::myform1->txtBox_nfcUid->Text += String::Format("{0:X2}:", buf[65 + i]); + } + else { + FormJoy::myform1->txtBox_nfcUid->Text += String::Format("{0:X2}", buf[65 + i]); + } + } + FormJoy::myform1->txtBox_nfcUid->Text += String::Format("\r\nType: {0:s}", buf[62] == 0x2 ? "NTAG" : "MIFARE"); + Application::DoEvents(); + goto step7; + } + else if (buf[49] == 0x2a) break; } - error_reading++; - if (error_reading > 7) { - goto stepf; + retries++; + if (retries > 4 || res == 0) { + Application::DoEvents(); + break; } } - stepf: - if (res_get > 0) - return res_get; - Sleep(30); + error_reading++; + if (error_reading > 100) { + res_get = 7; + if (ntag_init_done) + FormJoy::myform1->txtBox_NFCTag->Text = String::Format("Tag lost!"); + else + FormJoy::myform1->txtBox_NFCTag->Text = String::Format("No Tag detected!"); + goto step9; + } } +step7: + // Read NTAG contents + error_reading = 0; + while (1) { + memset(buf2, 0, sizeof(buf2)); + auto hdr = (brcm_hdr *)buf2; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x11; + hdr->timer = timming_byte & 0xF; + timming_byte++; + + pkt->subcmd = 0x02; + pkt->subcmd_arg.arg1 = 0x06; // 6: Read Ntag data, 0xf: Read mifare data + buf2[12] = 0x00; + buf2[13] = 0x00; + buf2[14] = 0x08; + buf2[15] = 0x13; // Length of data after cmd header + + buf2[16] = 0xd0; // Unknown + buf2[17] = 0x07; // Unknown or UID lentgh? + buf2[18] = 0x00; // Only for Mifare cmds or should have a UID? + + buf2[19] = 0x00; //should have a UID? + buf2[20] = 0x00; //should have a UID? + buf2[21] = 0x00; //should have a UID? + buf2[22] = 0x00; //should have a UID? + buf2[23] = 0x00; //should have a UID? + buf2[24] = 0x00; //should have a UID? + + buf2[25] = 0x00; // 1: Ntag215 only. 0: All tags, otherwise error x48 (Invalid format error) + + // https://www.tagnfc.com/en/info/11-nfc-tags-specs + + // If the following is selected wrongly, error x3e (Read error) + switch (ntag_pages) { + case 0: + buf2[26] = 0x01; + break; + // Ntag213 + case 45: + // The following 7 bytes should be decided with max the current ntag pages and what we want to read. + buf2[26] = 0x01; // How many blocks to read. Each block should be <= 60 pages (240 bytes)? Min/Max values are 1/4, otherwise error x40 (Argument error) + + buf2[27] = 0x00; // Block 1 starting page + buf2[28] = 0x2C; // Block 1 ending page + buf2[29] = 0x00; // Block 2 starting page + buf2[30] = 0x00; // Block 2 ending page + buf2[31] = 0x00; // Block 3 starting page + buf2[32] = 0x00; // Block 3 ending page + buf2[33] = 0x00; // Block 4 starting page + buf2[34] = 0x00; // Block 4 ending page + break; + // Ntag215 + case 135: + // The following 7 bytes should be decided with max the current ntag pages and what we want to read. + buf2[26] = 0x03; // How many page ranges to read. Each range should be <= 60 pages (240 bytes)? Max value is 4. + + buf2[27] = 0x00; // Block 1 starting page + buf2[28] = 0x3b; // Block 1 ending page + buf2[29] = 0x3c; // Block 2 starting page + buf2[30] = 0x77; // Block 2 ending page + buf2[31] = 0x78; // Block 3 starting page + buf2[32] = 0x86; // Block 3 ending page + buf2[33] = 0x00; // Block 4 starting page + buf2[34] = 0x00; // Block 4 ending page + break; + case 231: + // The following 7 bytes should be decided with max the current ntag pages and what we want to read. + buf2[26] = 0x04; // How many page ranges to read. Each range should be <= 60 pages (240 bytes)? Max value is 4. + + buf2[27] = 0x00; // Block 1 starting page + buf2[28] = 0x3b; // Block 1 ending page + buf2[29] = 0x3c; // Block 2 starting page + buf2[30] = 0x77; // Block 2 ending page + buf2[31] = 0x78; // Block 3 starting page + buf2[32] = 0xB3; // Block 3 ending page + buf2[33] = 0xB4; // Block 4 starting page + buf2[34] = 0xE6; // Block 4 ending page + break; + default: + break; + } + + buf2[47] = mcu_crc8_calc(buf2 + 11, 36); + + res = hid_write(handle, buf2, output_buffer_length - 1); + + int retries = 0; + while (1) { + res = hid_read_timeout(handle, buf2, sizeof(buf2), 64); + if (buf2[0] == 0x31) { + if ((buf2[49] == 0x3a || buf2[49] == 0x2a) && buf2[56] == 0x07) { + FormJoy::myform1->txtBox_NFCTag->Text = String::Format("Error {0:X2}!", buf2[50]); + goto step9;/////////// + } + else if (buf2[49] == 0x3a && buf2[51] == 0x07) { + if (ntag_init_done) { + payload_size = (buf2[54] << 8 | buf2[55]) & 0x7FF; + if (buf2[52] == 0x01) { + memcpy(ntag_buffer + ntag_buffer_pos, buf2 + 116, payload_size - 60); + ntag_buffer_pos += payload_size - 60; + } + else { + memcpy(ntag_buffer + ntag_buffer_pos, buf2 + 56, payload_size); + } + } + else if (buf2[52] == 0x01) { + if (tag_type == 2) { + switch (buf2[74]) { + case 0: + ntag_pages = 135; + break; + case 3: + ntag_pages = 45; + break; + case 4: + ntag_pages = 231; + break; + default: + goto step9;/////////// + break; + } + } + } + break; + } + else if (buf2[49] == 0x2a && buf2[56] == 0x04) { // finished + if (ntag_init_done) { + FormJoy::myform1->show_ntag_contents(ntag_buffer, ntag_pages); + Application::DoEvents(); + goto step9;/////////// + } + ntag_init_done = true; + + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 0x11; + hdr->timer = timming_byte & 0xF; + timming_byte++; + + pkt->subcmd = 0x02; + pkt->subcmd_arg.arg1 = 0x02; // 0: Cancel all, 4: StartWaitingReceive + pkt->subcmd_arg.arg2 = 0x00; // Count of the currecnt packet if the cmd is a series of packets. + buf[13] = 0x00; + buf[14] = 0x08; // 8: Last cmd packet, 0: More cmd packet should be expected + buf[15] = 0x00; // Length of data after cmd header + + buf[47] = mcu_crc8_calc(buf + 11, 36); //Without the last byte + res = hid_write(handle, buf, output_buffer_length - 1); + Sleep(200); + goto step5; + } + else if (buf2[49] == 0x2a) + break; + } + retries++; + if (retries > 4 || res == 0) + break; + } + error_reading++; + if (error_reading > 9) { + res_get = 8; + if (buf[62] == 0x4) + FormJoy::myform1->txtBox_NFCTag->Text = String::Format("Mifare reading is not supported for now.."); + goto step9; + } + } + +step9: + // Disable MCU + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 1; + hdr->timer = timming_byte & 0xF; + timming_byte++; + pkt->subcmd = 0x22; + pkt->subcmd_arg.arg1 = 0x00; + res = hid_write(handle, buf, output_buffer_length); + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + + + // Set input report to x3f + while (1) { + memset(buf, 0, sizeof(buf)); + auto hdr = (brcm_hdr *)buf; + auto pkt = (brcm_cmd_01 *)(hdr + 1); + hdr->cmd = 1; + hdr->timer = timming_byte & 0xF; + timming_byte++; + pkt->subcmd = 0x03; + pkt->subcmd_arg.arg1 = 0x3f; + res = hid_write(handle, buf, output_buffer_length); + int retries = 0; + while (1) { + res = hid_read_timeout(handle, buf, sizeof(buf), 64); + if (*(u16*)&buf[0xD] == 0x0380) + goto stepf; + + retries++; + if (retries > 8 || res == 0) + break; + } + error_reading++; + if (error_reading > 7) { + goto stepf; + } + } +stepf: + if (res_get > 0) + return res_get; + return 0; } @@ -2774,17 +2918,17 @@ int device_connection(){ // Joy-Con (L) if (handle = hid_open(0x57e, 0x2006, nullptr)) { handle_ok = 1; - return 1; + return handle_ok; } // Joy-Con (R) if (handle = hid_open(0x57e, 0x2007, nullptr)) { handle_ok = 2; - return 2; + return handle_ok; } // Pro Controller if (handle = hid_open(0x57e, 0x2009, nullptr)) { handle_ok = 3; - return 3; + return handle_ok; } // Nothing found else {