From 3150ca6d8ab8ce821a1ee2aed5163b0e09c0d0f8 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 27 Jul 2023 09:10:17 -0600 Subject: [PATCH 001/106] feat: add ndef send example --- .gitignore | 1 + examples/NDEFSend/Makefile | 16 ++++++ examples/NDEFSend/NDEFSend.ino | 91 ++++++++++++++++++++++++++++++++++ src/Electroniccats_PN7150.h | 1 + src/T4T_NDEF_emu.h | 1 + 5 files changed, 110 insertions(+) create mode 100644 examples/NDEFSend/Makefile create mode 100644 examples/NDEFSend/NDEFSend.ino diff --git a/.gitignore b/.gitignore index cad5e63..5956980 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ src/sync.ffs_db .DS_Store +.vscode/ \ No newline at end of file diff --git a/examples/NDEFSend/Makefile b/examples/NDEFSend/Makefile new file mode 100644 index 0000000..b1a36d9 --- /dev/null +++ b/examples/NDEFSend/Makefile @@ -0,0 +1,16 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +all: compile upload \ No newline at end of file diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino new file mode 100644 index 0000000..c4c5af4 --- /dev/null +++ b/examples/NDEFSend/NDEFSend.ino @@ -0,0 +1,91 @@ +#include +#include "ndef_helper.h" + +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) + +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Interface to save data for multiple tags + +uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer P2P + +unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; + +const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF + 0x01, // TYPE LENGTH + 0x07, // PAYLOAD LENTGH + 'T', // TYPE + 0x02, // Status + 'e', 'n', // Language + 'T', 'e', 's', 't'}; + +void setup() { + Serial.begin(9600); + while (!Serial) + ; + Serial.println("NDEF Message with PN7150"); + + if(T4T_NDEF_EMU_SetMessage((unsigned char *) NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void*)*ndefPush_Cb)) { + Serial.println("Set message ok\r\n"); + } else { + Serial.println("Set message error\r\n"); + } + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board + Serial.println("Error while setting up the mode, check connections!"); + while (1) + ; + } + + if (nfc.ConfigureSettings()) { + Serial.println("The Configure Settings failed!"); + while (1) + ; + } + + if (nfc.ConfigMode(mode)) { + Serial.println("The Configure Mode failed!!"); + while (1) + ; + } + + mode = 3; + resetMode(); + Serial.println("Waiting for an NDEF device..."); +} + +void loop() { + if(nfc.CardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? + if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes + switch (Cmd[1]) { + case 0xA4: //Something tries to select a file, meaning that it is a reader + Serial.println("Reader detected!"); + break; + + case 0xB0: //SFI + Serial.println("0xB0 detected!"); + break; + + case 0xD0: //... + Serial.println("0xD0 detected!"); + break; + + default: + break; + } + nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); + } + } +} + +void resetMode() { // Reset the configuration mode after each reading + Serial.println("\nRe-initializing..."); + nfc.ConfigMode(mode); + nfc.StartDiscovery(mode); +} + +void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { + Serial.println("--- NDEF Record sent"); +} \ No newline at end of file diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index e6fdeef..56f661b 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -22,6 +22,7 @@ // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library //#include "RW_NDEF.h" #include "P2P_NDEF.h" +#include "T4T_NDEF_emu.h" #if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards #include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 diff --git a/src/T4T_NDEF_emu.h b/src/T4T_NDEF_emu.h index 09363ae..9e318b4 100644 --- a/src/T4T_NDEF_emu.h +++ b/src/T4T_NDEF_emu.h @@ -13,4 +13,5 @@ */ void T4T_NDEF_EMU_Reset(void); +bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); From e86131c1db36e96e7f3a279c7c49cd629746b1ef Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 27 Jul 2023 09:57:20 -0600 Subject: [PATCH 002/106] fix: configure mode failed --- examples/NDEFSend/NDEFSend.ino | 139 +++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 52 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index c4c5af4..870f9d6 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -1,14 +1,20 @@ #include + #include "ndef_helper.h" #define PN7150_IRQ (11) #define PN7150_VEN (13) #define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Interface to save data for multiple tags +// Function prototypes +void resetMode(); +void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); +void displayDeviceInfo(); + +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Interface to save data for multiple tags -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer P2P +uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer P2P unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; @@ -22,61 +28,49 @@ const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF void setup() { Serial.begin(9600); - while (!Serial) - ; - Serial.println("NDEF Message with PN7150"); + while (!Serial) + ; + Serial.println("Send NDEF Message with PN7150"); - if(T4T_NDEF_EMU_SetMessage((unsigned char *) NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void*)*ndefPush_Cb)) { - Serial.println("Set message ok\r\n"); + if (T4T_NDEF_EMU_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { + Serial.println("Set message ok\r\n"); } else { Serial.println("Set message error\r\n"); - } - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { // Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (1) - ; - } - - if (nfc.ConfigureSettings()) { - Serial.println("The Configure Settings failed!"); - while (1) - ; - } - - if (nfc.ConfigMode(mode)) { - Serial.println("The Configure Mode failed!!"); - while (1) - ; - } - - mode = 3; - resetMode(); - Serial.println("Waiting for an NDEF device..."); + } + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board + Serial.println("Error while setting up the mode, check connections!"); + while (1) + ; + } + + if (nfc.ConfigureSettings()) { + Serial.println("The Configure Settings failed!"); + while (1) + ; + } + + if (nfc.ConfigMode(mode)) { + Serial.println("The Configure Mode failed!!"); + while (1) + ; + } + + nfc.StartDiscovery(mode); + Serial.println("Waiting for an NDEF device..."); } void loop() { - if(nfc.CardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? - if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes - switch (Cmd[1]) { - case 0xA4: //Something tries to select a file, meaning that it is a reader - Serial.println("Reader detected!"); - break; - - case 0xB0: //SFI - Serial.println("0xB0 detected!"); - break; - - case 0xD0: //... - Serial.println("0xD0 detected!"); - break; - - default: - break; - } - nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); - } + if (nfc.CardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? + if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes + if (Cmd[1] == 0xA4) { + Serial.println("Reader detected!"); + displayDeviceInfo(); + } + // Send back status OK to the reader means that the command has been received + nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); + } } } @@ -88,4 +82,45 @@ void resetMode() { // Reset the configuration mode after each reading void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { Serial.println("--- NDEF Record sent"); +} + +void displayDeviceInfo() { + Serial.println(); + Serial.print("RfInterface: "); + switch (RfInterface.Interface) { + case INTF_ISODEP: + Serial.println("ISO-DEP"); + break; + case INTF_NFCDEP: + Serial.println("NFC-DEP"); + break; + case INTF_TAGCMD: + Serial.println("TAG"); + break; + case INTF_FRAME: + Serial.println("FRAME"); + break; + case INTF_UNDETERMINED: + Serial.println("UNDETERMINED"); + break; + default: + Serial.println("UNKNOWN"); + break; + } + + Serial.print("Mode: "); + switch (RfInterface.ModeTech) { + case MODE_POLL: + Serial.println("POLL"); + break; + case MODE_LISTEN: + Serial.println("LISTEN"); + break; + case MODE_MASK: + Serial.println("MASK"); + break; + default: + Serial.println("UNKNOWN"); + break; + } } \ No newline at end of file From ed94d673daeac66f253f7cf2105cadff85029dc7 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 27 Jul 2023 10:52:54 -0600 Subject: [PATCH 003/106] test: p2p mode --- examples/NDEFSend/NDEFSend.ino | 56 ++++++++++++++++++++++++++++++++-- src/Electroniccats_PN7150.cpp | 2 ++ src/Electroniccats_PN7150.h | 1 + src/RW_NDEF.h | 1 + 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 870f9d6..2ad7235 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -7,6 +7,8 @@ #define PN7150_ADDR (0x28) // Function prototypes +void checkReaders(); +void p2pMode(); void resetMode(); void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); void displayDeviceInfo(); @@ -38,6 +40,20 @@ void setup() { Serial.println("Set message error\r\n"); } + if (RW_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { + Serial.println("Set message ok\r\n"); + } else { + Serial.println("Set message error\r\n"); + } + + if (P2P_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { + Serial.println("Set message ok\r\n"); + } else { + Serial.println("Set message error\r\n"); + } + + P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); + Serial.println("Initializing..."); if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); @@ -62,18 +78,53 @@ void setup() { } void loop() { + // checkReaders(); + p2pMode(); +} + +void checkReaders() { if (nfc.CardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("Reader detected!"); displayDeviceInfo(); } - // Send back status OK to the reader means that the command has been received nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); } } } +void p2pMode() { + Serial.print("."); + + if (!nfc.WaitForDiscoveryNotification(&RfInterface, 1000)) { // Waiting to detect + Serial.println(); + displayDeviceInfo(); + + if (RfInterface.Interface == INTF_NFCDEP || RfInterface.Interface == INTF_ISODEP) { + if ((RfInterface.ModeTech & MODE_LISTEN) == MODE_LISTEN) { + Serial.println(" - P2P TARGET MODE: Activated from remote Initiator"); + } else { + Serial.println(" - P2P INITIATOR MODE: Remote Target activated"); + } + + /* Process with SNEP for NDEF exchange */ + nfc.ProcessP2pMode(RfInterface); + Serial.println("Peer lost!"); + } else { + Serial.println("Wrong discovery!"); + } + + // Wait for removal + nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + Serial.println("Device removed!"); + + nfc.StopDiscovery(); + resetMode(); + } + delay(500); +} + void resetMode() { // Reset the configuration mode after each reading Serial.println("\nRe-initializing..."); nfc.ConfigMode(mode); @@ -85,7 +136,6 @@ void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { } void displayDeviceInfo() { - Serial.println(); Serial.print("RfInterface: "); switch (RfInterface.Interface) { case INTF_ISODEP: @@ -123,4 +173,6 @@ void displayDeviceInfo() { Serial.println("UNKNOWN"); break; } + + Serial.println(); } \ No newline at end of file diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 49dca96..7ec9b2d 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -761,9 +761,11 @@ void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) /* Get frame from remote peer */ while (status == SUCCESS) { + Serial.println("here"); /* is DATA_PACKET ? */ if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { + Serial.println("here "); uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize; /* Handle P2P communication */ diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 56f661b..f3b7b26 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -23,6 +23,7 @@ //#include "RW_NDEF.h" #include "P2P_NDEF.h" #include "T4T_NDEF_emu.h" +#include "RW_NDEF.h" #if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards #include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 diff --git a/src/RW_NDEF.h b/src/RW_NDEF.h index 615a434..0080434 100644 --- a/src/RW_NDEF.h +++ b/src/RW_NDEF.h @@ -34,3 +34,4 @@ extern RW_NDEF_Callback_t *pRW_NDEF_PushCb; void RW_NDEF_Reset(unsigned char type); void RW_NDEF_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); void RW_NDEF_Write_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); +bool RW_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); From 387dd3f7e001a863628ac7a032e569b12ba175ac Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 28 Jul 2023 10:57:47 -0600 Subject: [PATCH 004/106] fix: restore check readers --- examples/NDEFSend/Makefile | 2 +- examples/NDEFSend/NDEFSend.ino | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/NDEFSend/Makefile b/examples/NDEFSend/Makefile index b1a36d9..396a25b 100644 --- a/examples/NDEFSend/Makefile +++ b/examples/NDEFSend/Makefile @@ -1,5 +1,5 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat -MONITOR_PORT = /dev/cu.usbmodem1101 +MONITOR_PORT = /dev/cu.usbmodem11101 compile: arduino-cli compile --fqbn $(BOARD_TAG) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 2ad7235..e3d56e7 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -46,13 +46,13 @@ void setup() { Serial.println("Set message error\r\n"); } - if (P2P_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { - Serial.println("Set message ok\r\n"); - } else { - Serial.println("Set message error\r\n"); - } + // if (P2P_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { + // Serial.println("Set message ok\r\n"); + // } else { + // Serial.println("Set message error\r\n"); + // } - P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); + // P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); Serial.println("Initializing..."); if (nfc.connectNCI()) { // Wake up the board @@ -78,8 +78,8 @@ void setup() { } void loop() { - // checkReaders(); - p2pMode(); + checkReaders(); + // p2pMode(); } void checkReaders() { From 31c0e7dd6c97938c1dea73e0a1e5b858338d5784 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 28 Jul 2023 11:55:48 -0600 Subject: [PATCH 005/106] feat: add begin method --- examples/NDEFSend/NDEFSend.ino | 12 ++++++++++-- src/Electroniccats_PN7150.cpp | 6 ++++-- src/Electroniccats_PN7150.h | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index e3d56e7..ff863e0 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -55,6 +55,13 @@ void setup() { // P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); Serial.println("Initializing..."); + + if (nfc.begin()) { + Serial.println("Error initializing PN7150"); + while (1) + ; + } + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); while (1) @@ -83,10 +90,12 @@ void loop() { } void checkReaders() { + Serial.print("."); if (nfc.CardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("Reader detected!"); + nfc.ReadNdef(RfInterface); displayDeviceInfo(); } nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); @@ -118,8 +127,6 @@ void p2pMode() { // Wait for removal nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("Device removed!"); - - nfc.StopDiscovery(); resetMode(); } delay(500); @@ -127,6 +134,7 @@ void p2pMode() { void resetMode() { // Reset the configuration mode after each reading Serial.println("\nRe-initializing..."); + // nfc.StopDiscovery(); nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 7ec9b2d..7b1356d 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -466,6 +466,10 @@ bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char #ifdef DEBUG2 Serial.println("[DEBUG] cardModeReceive exec"); #endif + #ifdef DEBUG3 + Serial.println("[DEBUG] cardModeReceive exec"); + #endif + bool status = NFC_ERROR; uint8_t Ans[MAX_NCI_FRAME_SIZE]; @@ -761,11 +765,9 @@ void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) /* Get frame from remote peer */ while (status == SUCCESS) { - Serial.println("here"); /* is DATA_PACKET ? */ if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { - Serial.println("here "); uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize; /* Handle P2P communication */ diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index f3b7b26..8a011cd 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -25,6 +25,8 @@ #include "T4T_NDEF_emu.h" #include "RW_NDEF.h" +#define DEBUG3 + #if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards #include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 #else From e9aae7309b78a3b4dcabc01ff380062465336323 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 28 Jul 2023 12:16:08 -0600 Subject: [PATCH 006/106] feat: send test message --- examples/NDEFSend/NDEFSend.ino | 3 ++- src/Electroniccats_PN7150.cpp | 9 +++++---- src/T4T_NDEF_emu.cpp | 1 + src/T4T_NDEF_emu.h | 2 ++ 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index ff863e0..7416cfe 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -95,7 +95,8 @@ void checkReaders() { if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("Reader detected!"); - nfc.ReadNdef(RfInterface); + // nfc.ReadNdef(RfInterface); + nfc.ProcessCardMode(RfInterface); displayDeviceInfo(); } nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 7b1356d..4548e27 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -396,12 +396,13 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) /* Reset Card emulation state */ T4T_NDEF_EMU_Reset(); - (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); + // (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); getMessage(2000); //NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_2S) == NXPNCI_SUCCESS while (rxMessageLength > 0) { + getMessage(2000); /* is RF_DEACTIVATE_NTF ? */ if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) { @@ -409,14 +410,14 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { /* Restart the discovery loop */ //NxpNci_HostTransceive(NCIStopDiscovery, sizeof(NCIStopDiscovery), Answer, sizeof(Answer), &AnswerSize); - (void)writeData(NCIStartDiscovery, sizeof(NCIStopDiscovery)); + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); getMessage(); do { if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x06)) break; //NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_100MS); - (void)writeData(rxBuffer, rxMessageLength); + //(void)writeData(rxBuffer, rxMessageLength); getMessage(100); } while (rxMessageLength != 0); //NxpNci_HostTransceive(NCIStartDiscovery, NCIStartDiscovery_length, Answer, sizeof(Answer), &AnswerSize); @@ -424,7 +425,7 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) getMessage(); } /* Come back to discovery state */ - break; + // break; } /* is DATA_PACKET ? */ else if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index 461d887..c61f376 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -92,6 +92,7 @@ void T4T_NDEF_EMU_Reset(void) void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *pRsp, unsigned short *pRsp_size) { bool eStatus = false; + Serial.println("here"); if (!memcmp(pCmd, T4T_NDEF_EMU_APP_Select, sizeof(T4T_NDEF_EMU_APP_Select))) { diff --git a/src/T4T_NDEF_emu.h b/src/T4T_NDEF_emu.h index 9e318b4..2506a90 100644 --- a/src/T4T_NDEF_emu.h +++ b/src/T4T_NDEF_emu.h @@ -12,6 +12,8 @@ * arising from its use. */ +#include + void T4T_NDEF_EMU_Reset(void); bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); From f8b123e28a8756a16abfa6d19afb1f8532017d0b Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 28 Jul 2023 14:47:06 -0600 Subject: [PATCH 007/106] test: change payload size --- examples/NDEFSend/NDEFSend.ino | 40 +++++++++++++++++----------------- src/T4T_NDEF_emu.cpp | 1 - 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 7416cfe..33cb517 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -20,13 +20,13 @@ uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; -const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF - 0x01, // TYPE LENGTH - 0x07, // PAYLOAD LENTGH - 'T', // TYPE - 0x02, // Status - 'e', 'n', // Language - 'T', 'e', 's', 't'}; +const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF + 0x01, // Type length (1 byte for "T") + 0x08, // Payload length + 'U', // Type -> 'T' for text, 'U' for URI + 0x02, // Status + 'e', 'n', // Language + 'H', 'e', 'l', 'l', 'o'}; // Message Payload void setup() { Serial.begin(9600); @@ -40,19 +40,19 @@ void setup() { Serial.println("Set message error\r\n"); } - if (RW_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { - Serial.println("Set message ok\r\n"); - } else { - Serial.println("Set message error\r\n"); - } + if (RW_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { + Serial.println("Set message ok\r\n"); + } else { + Serial.println("Set message error\r\n"); + } - // if (P2P_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { - // Serial.println("Set message ok\r\n"); - // } else { - // Serial.println("Set message error\r\n"); - // } + // if (P2P_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { + // Serial.println("Set message ok\r\n"); + // } else { + // Serial.println("Set message error\r\n"); + // } - // P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); + // P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); Serial.println("Initializing..."); @@ -86,7 +86,7 @@ void setup() { void loop() { checkReaders(); - // p2pMode(); + // p2pMode(); } void checkReaders() { @@ -108,7 +108,7 @@ void p2pMode() { Serial.print("."); if (!nfc.WaitForDiscoveryNotification(&RfInterface, 1000)) { // Waiting to detect - Serial.println(); + Serial.println(); displayDeviceInfo(); if (RfInterface.Interface == INTF_NFCDEP || RfInterface.Interface == INTF_ISODEP) { diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index c61f376..461d887 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -92,7 +92,6 @@ void T4T_NDEF_EMU_Reset(void) void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *pRsp, unsigned short *pRsp_size) { bool eStatus = false; - Serial.println("here"); if (!memcmp(pCmd, T4T_NDEF_EMU_APP_Select, sizeof(T4T_NDEF_EMU_APP_Select))) { From b38ffe351a2db51028065e774239a3f2046678d9 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 08:44:49 -0600 Subject: [PATCH 008/106] feat: remove unnecessary callbacks --- examples/NDEFSend/Makefile | 2 +- examples/NDEFSend/NDEFSend.ino | 112 +++------------------------------ 2 files changed, 8 insertions(+), 106 deletions(-) diff --git a/examples/NDEFSend/Makefile b/examples/NDEFSend/Makefile index 396a25b..b1a36d9 100644 --- a/examples/NDEFSend/Makefile +++ b/examples/NDEFSend/Makefile @@ -1,5 +1,5 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat -MONITOR_PORT = /dev/cu.usbmodem11101 +MONITOR_PORT = /dev/cu.usbmodem1101 compile: arduino-cli compile --fqbn $(BOARD_TAG) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 33cb517..7daad63 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -8,10 +8,7 @@ // Function prototypes void checkReaders(); -void p2pMode(); -void resetMode(); -void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); -void displayDeviceInfo(); +void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 RfIntf_t RfInterface; // Interface to save data for multiple tags @@ -21,9 +18,9 @@ uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF - 0x01, // Type length (1 byte for "T") + 0x01, // Type length (1 byte) 0x08, // Payload length - 'U', // Type -> 'T' for text, 'U' for URI + 'T', // Type -> 'T' for text, 'U' for URI 0x02, // Status 'e', 'n', // Language 'H', 'e', 'l', 'l', 'o'}; // Message Payload @@ -34,26 +31,12 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - if (T4T_NDEF_EMU_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { - Serial.println("Set message ok\r\n"); + if (T4T_NDEF_EMU_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*sendMessageCallback)) { + Serial.println("Set message success!"); } else { - Serial.println("Set message error\r\n"); + Serial.println("Set message failed!"); } - if (RW_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { - Serial.println("Set message ok\r\n"); - } else { - Serial.println("Set message error\r\n"); - } - - // if (P2P_NDEF_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*ndefPush_Cb)) { - // Serial.println("Set message ok\r\n"); - // } else { - // Serial.println("Set message error\r\n"); - // } - - // P2P_NDEF_RegisterPullCallback((void *)*ndefPush_Cb); - Serial.println("Initializing..."); if (nfc.begin()) { @@ -86,7 +69,6 @@ void setup() { void loop() { checkReaders(); - // p2pMode(); } void checkReaders() { @@ -95,93 +77,13 @@ void checkReaders() { if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("Reader detected!"); - // nfc.ReadNdef(RfInterface); nfc.ProcessCardMode(RfInterface); - displayDeviceInfo(); } nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); } } } -void p2pMode() { - Serial.print("."); - - if (!nfc.WaitForDiscoveryNotification(&RfInterface, 1000)) { // Waiting to detect - Serial.println(); - displayDeviceInfo(); - - if (RfInterface.Interface == INTF_NFCDEP || RfInterface.Interface == INTF_ISODEP) { - if ((RfInterface.ModeTech & MODE_LISTEN) == MODE_LISTEN) { - Serial.println(" - P2P TARGET MODE: Activated from remote Initiator"); - } else { - Serial.println(" - P2P INITIATOR MODE: Remote Target activated"); - } - - /* Process with SNEP for NDEF exchange */ - nfc.ProcessP2pMode(RfInterface); - Serial.println("Peer lost!"); - } else { - Serial.println("Wrong discovery!"); - } - - // Wait for removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("Device removed!"); - resetMode(); - } - delay(500); -} - -void resetMode() { // Reset the configuration mode after each reading - Serial.println("\nRe-initializing..."); - // nfc.StopDiscovery(); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); -} - -void ndefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { +void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { Serial.println("--- NDEF Record sent"); } - -void displayDeviceInfo() { - Serial.print("RfInterface: "); - switch (RfInterface.Interface) { - case INTF_ISODEP: - Serial.println("ISO-DEP"); - break; - case INTF_NFCDEP: - Serial.println("NFC-DEP"); - break; - case INTF_TAGCMD: - Serial.println("TAG"); - break; - case INTF_FRAME: - Serial.println("FRAME"); - break; - case INTF_UNDETERMINED: - Serial.println("UNDETERMINED"); - break; - default: - Serial.println("UNKNOWN"); - break; - } - - Serial.print("Mode: "); - switch (RfInterface.ModeTech) { - case MODE_POLL: - Serial.println("POLL"); - break; - case MODE_LISTEN: - Serial.println("LISTEN"); - break; - case MODE_MASK: - Serial.println("MASK"); - break; - default: - Serial.println("UNKNOWN"); - break; - } - - Serial.println(); -} \ No newline at end of file From 136f2e2f2ccd9efa1a15cc19ad24e9d5dc7d68c3 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 09:22:50 -0600 Subject: [PATCH 009/106] test: uri record payload --- examples/NDEFSend/Makefile | 5 ++++- examples/NDEFSend/NDEFSend.ino | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/examples/NDEFSend/Makefile b/examples/NDEFSend/Makefile index b1a36d9..4ab2c77 100644 --- a/examples/NDEFSend/Makefile +++ b/examples/NDEFSend/Makefile @@ -13,4 +13,7 @@ monitor: clean: arduino-cli cache clean -all: compile upload \ No newline at end of file +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 7daad63..87f1e4f 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -17,13 +17,23 @@ uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; -const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF - 0x01, // Type length (1 byte) - 0x08, // Payload length - 'T', // Type -> 'T' for text, 'U' for URI - 0x02, // Status - 'e', 'n', // Language - 'H', 'e', 'l', 'l', 'o'}; // Message Payload +const char uri[] = "google.com"; + +// const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF +// 0x01, // Type length (1 byte) +// 0x08, // Payload length +// 'U', // Type -> 'T' for text, 'U' for URI +// 0x02, // Status +// 'e', 'n', // Language +// 'H', 'e', 'l', 'l', 'o'}; // Message Payload + +const char ndefMessage[] = {0xD1, + 0x01, + sizeof(uri) + 1, + 'U', + 0x02, + // 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm'}; + uri[0], uri[1], uri[2], uri[3], uri[4], uri[5], uri[6], uri[7], uri[8], uri[9], uri[10]}; void setup() { Serial.begin(9600); @@ -31,7 +41,7 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - if (T4T_NDEF_EMU_SetMessage((unsigned char *)NDEF_MESSAGE, sizeof(NDEF_MESSAGE), (void *)*sendMessageCallback)) { + if (T4T_NDEF_EMU_SetMessage((unsigned char *)ndefMessage, sizeof(ndefMessage), (void *)*sendMessageCallback)) { Serial.println("Set message success!"); } else { Serial.println("Set message failed!"); From 4f3347cfda83c1f4f9443988ae1885b9c4a2a71d Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 10:32:19 -0600 Subject: [PATCH 010/106] fix: remove debug 3 flag --- examples/NDEFSend/NDEFSend.ino | 7 ++++--- src/Electroniccats_PN7150.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 87f1e4f..321f9ad 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -74,7 +74,7 @@ void setup() { } nfc.StartDiscovery(mode); - Serial.println("Waiting for an NDEF device..."); + Serial.print("Waiting for an NDEF device"); } void loop() { @@ -86,14 +86,15 @@ void checkReaders() { if (nfc.CardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { - Serial.println("Reader detected!"); + Serial.println("\nReader detected!"); nfc.ProcessCardMode(RfInterface); } nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); + Serial.print("Waiting for an NDEF device"); } } } void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { - Serial.println("--- NDEF Record sent"); + Serial.println("NDEF Record sent!"); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 8a011cd..07e971e 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -25,7 +25,7 @@ #include "T4T_NDEF_emu.h" #include "RW_NDEF.h" -#define DEBUG3 +// #define DEBUG3 #if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards #include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 From 7d33119e76ed1faaf0b4c92ca77d164bd004259c Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 12:03:03 -0600 Subject: [PATCH 011/106] feat: add ndef receive example --- examples/NDEFReceive/Makefile | 19 ++ examples/NDEFReceive/NDEFReceive.ino | 372 +++++++++++++++++++++++++++ src/Electroniccats_PN7150.cpp | 8 +- src/Electroniccats_PN7150.h | 1 + src/RW_NDEF.h | 1 + 5 files changed, 397 insertions(+), 4 deletions(-) create mode 100644 examples/NDEFReceive/Makefile create mode 100644 examples/NDEFReceive/NDEFReceive.ino diff --git a/examples/NDEFReceive/Makefile b/examples/NDEFReceive/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/NDEFReceive/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino new file mode 100644 index 0000000..046a881 --- /dev/null +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -0,0 +1,372 @@ +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) + +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Intarface to save data for multiple tags + +uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation + +const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF + 0x01, // Type length (1 byte) + 0x08, // Payload length + 'T', // Type -> 'T' for text, 'U' for URI + 0x02, // Status + 'e', 'n', // Language + 'H', 'e', 'l', 'l', 'o'}; // Message Payload + +void ResetMode() { // Reset the configuration mode after each reading + Serial.println("Re-initializing..."); + nfc.ConfigMode(mode); + nfc.StartDiscovery(mode); +} + +void PrintBuf(const byte *data, const uint32_t numBytes) { // Print hex data buffer in format + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + Serial.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + Serial.print(F("0")); + Serial.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + Serial.print(F(" ")); + } + } + Serial.println(); +} +void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field + char tmp[16]; + while (1) { + switch (RfIntf.Protocol) { // Indetify card protocol + case PROT_T1T: + case PROT_T2T: + case PROT_T3T: + case PROT_ISODEP: + Serial.print(" - POLL MODE: Remote activated tag type: "); + Serial.println(RfIntf.Protocol); + break; + case PROT_ISO15693: + Serial.println(" - POLL MODE: Remote ISO15693 card activated"); + break; + case PROT_MIFARE: + Serial.println(" - POLL MODE: Remote MIFARE card activated"); + break; + default: + Serial.println(" - POLL MODE: Undetermined target"); + return; + } + + switch (RfIntf.ModeTech) { // Indetify card technology + case (MODE_POLL | TECH_PASSIVE_NFCA): + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[0]); + Serial.print(tmp); + Serial.print(" "); + sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[1]); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); + + if (RfIntf.Info.NFC_APP.SelResLen != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SelRes[0]); + Serial.print(tmp); + Serial.println(" "); + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCB): + if (RfIntf.Info.NFC_BPP.SensResLen != 0) { + Serial.print("\tSENS_RES = "); + PrintBuf(RfIntf.Info.NFC_BPP.SensRes, RfIntf.Info.NFC_BPP.SensResLen); + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCF): + Serial.print("\tBitrate = "); + Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); + + if (RfIntf.Info.NFC_FPP.SensResLen != 0) { + Serial.print("\tSENS_RES = "); + PrintBuf(RfIntf.Info.NFC_FPP.SensRes, RfIntf.Info.NFC_FPP.SensResLen); + } + break; + + case (MODE_POLL | TECH_PASSIVE_15693): + Serial.print("\tID = "); + PrintBuf(RfIntf.Info.NFC_VPP.ID, sizeof(RfIntf.Info.NFC_VPP.ID)); + + Serial.print("\ntAFI = "); + Serial.println(RfIntf.Info.NFC_VPP.AFI); + + Serial.print("\tDSFID = "); + Serial.println(RfIntf.Info.NFC_VPP.DSFID, HEX); + break; + + default: + break; + } + if (RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology + if (nfc.ReaderActivateNext(&RfIntf) == NFC_ERROR) break; + } else + break; + } +} + +void setup() { + Serial.begin(9600); + while (!Serial) + ; + Serial.println("Detect NFC tags with PN7150"); + + RW_NDEF_RegisterPullCallback((void *)*ndefPull_Cb); + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board + Serial.println("Error while setting up the mode, check connections!"); + while (1) + ; + } + + if (nfc.ConfigureSettings()) { + Serial.println("The Configure Settings is failed!"); + while (1) + ; + } + + if (nfc.ConfigMode(mode)) { // Set up the configuration mode + Serial.println("The Configure Mode is failed!!"); + while (1) + ; + } + nfc.StartDiscovery(mode); // NCI Discovery mode + Serial.println("Waiting for an Card ..."); +} + +void loop() { + if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards + displayCardInfo(RfInterface); + displayDeviceInfo(); + switch (RfInterface.Protocol) { + case PROT_T1T: + case PROT_T2T: + case PROT_T3T: + case PROT_ISODEP: + nfc.ProcessReaderMode(RfInterface, READ_NDEF); + break; + + case PROT_ISO15693: + break; + + case PROT_MIFARE: + nfc.ProcessReaderMode(RfInterface, READ_NDEF); + break; + + default: + break; + } + + //* It can detect multiple cards at the same time if they use the same protocol + if (RfInterface.MoreTags) { + nfc.ReaderActivateNext(&RfInterface); + } + //* Wait for card removal + nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + Serial.println("CARD REMOVED!"); + + nfc.StopDiscovery(); + nfc.StartDiscovery(mode); + } + ResetMode(); + delay(500); +} + +void displayDeviceInfo() { + Serial.println(); + Serial.print("RfInterface: "); + switch (RfInterface.Interface) { + case INTF_ISODEP: + Serial.println("ISO-DEP"); + break; + case INTF_NFCDEP: + Serial.println("NFC-DEP"); + break; + case INTF_TAGCMD: + Serial.println("TAG"); + break; + case INTF_FRAME: + Serial.println("FRAME"); + break; + case INTF_UNDETERMINED: + Serial.println("UNDETERMINED"); + break; + default: + Serial.println("UNKNOWN"); + break; + } + + Serial.print("Protocol: "); + switch (RfInterface.Protocol) { + case PROT_UNDETERMINED: + Serial.println("UNDETERMINED"); + break; + case PROT_T1T: + Serial.println("T1T"); + break; + case PROT_T2T: + Serial.println("T2T"); + break; + case PROT_T3T: + Serial.println("T3T"); + break; + case PROT_ISODEP: + Serial.println("ISO-DEP"); + break; + case PROT_NFCDEP: + Serial.println("NFC-DEP"); + break; + case PROT_ISO15693: + Serial.println("ISO15693"); + break; + case PROT_MIFARE: + Serial.println("MIFARE"); + break; + default: + Serial.println("UNKNOWN"); + break; + } + + Serial.print("Mode: "); + switch (RfInterface.ModeTech) { + case MODE_POLL: + Serial.println("POLL"); + break; + case MODE_LISTEN: + Serial.println("LISTEN"); + break; + case MODE_MASK: + Serial.println("MASK"); + break; + default: + Serial.println("UNKNOWN"); + break; + } +} + +void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { + unsigned char *pNdefRecord = pNdefMessage; + NdefRecord_t NdefRecord; + unsigned char save; + + Serial.println("Processing Callback"); + + if (pNdefMessage == NULL) { + Serial.println("--- Provisioned buffer size too small or NDEF message empty"); + return; + } + + while (pNdefRecord != NULL) { + Serial.println("--- NDEF record received:"); + + NdefRecord = DetectNdefRecordType(pNdefRecord); + + switch (NdefRecord.recordType) { + case MEDIA_VCARD: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("vCard:"); + // Serial.println(NdefRecord.recordPayload); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_TEXT: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("Text record: "); + // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_URI: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("URI record: "); + // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); + Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case MEDIA_HANDOVER_WIFI: { + unsigned char index = 0, i; + + Serial.println("--- Received WIFI credentials:"); + if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) + index += 4; + while (index < NdefRecord.recordPayloadSize) { + if (NdefRecord.recordPayload[index] == 0x10) { + if (NdefRecord.recordPayload[index + 1] == 0x45) { + Serial.print("- SSID = "); + for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) + Serial.print(NdefRecord.recordPayload[index + 4 + i]); + Serial.println(""); + } else if (NdefRecord.recordPayload[index + 1] == 0x03) { + Serial.print("- Authenticate Type = "); + Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); + } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { + Serial.print("- Encryption Type = "); + Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); + } else if (NdefRecord.recordPayload[index + 1] == 0x27) { + Serial.print("- Network key = "); + for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) + Serial.print("#"); + Serial.println(""); + } + index += 4 + NdefRecord.recordPayload[index + 3]; + } else + continue; + } + } break; + + case WELL_KNOWN_HANDOVER_SELECT: + Serial.print("Handover select version "); + Serial.print(NdefRecord.recordPayload[0] >> 4); + Serial.println(NdefRecord.recordPayload[0] & 0xF); + break; + + case WELL_KNOWN_HANDOVER_REQUEST: + Serial.print("Handover request version "); + Serial.print(NdefRecord.recordPayload[0] >> 4); + Serial.println(NdefRecord.recordPayload[0] & 0xF); + break; + + case MEDIA_HANDOVER_BT: + Serial.print("BT Handover payload = "); + // Serial.print(NdefRecord.recordPayload); + // Serial.println(NdefRecord.recordPayloadSize); + break; + + case MEDIA_HANDOVER_BLE: + Serial.print("BLE Handover payload = "); + // Serial.print(NdefRecord.recordPayload); + // Serial.println(NdefRecord.recordPayloadSize); + break; + + case MEDIA_HANDOVER_BLE_SECURE: + Serial.print("BLE secure Handover payload = "); + // Serial.println(NdefRecord.recordPayload, NdefRecord.recordPayloadSize); + break; + + default: + Serial.println("Unsupported NDEF record, cannot parse"); + break; + } + pNdefRecord = GetNextRecord(pNdefRecord); + } + + Serial.println(""); +} diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 4548e27..f872085 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -16,10 +16,10 @@ * */ #include "Electroniccats_PN7150.h" -#include "P2P_NDEF.h" -#include "ndef_helper.h" -#include "RW_NDEF.h" -#include "T4T_NDEF_emu.h" +// #include "P2P_NDEF.h" +// #include "ndef_helper.h" +// #include "RW_NDEF.h" +// #include "T4T_NDEF_emu.h" uint8_t gNextTag_Protocol = PROT_UNDETERMINED; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 07e971e..01e904c 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -24,6 +24,7 @@ #include "P2P_NDEF.h" #include "T4T_NDEF_emu.h" #include "RW_NDEF.h" +#include "ndef_helper.h" // #define DEBUG3 diff --git a/src/RW_NDEF.h b/src/RW_NDEF.h index 0080434..225dab9 100644 --- a/src/RW_NDEF.h +++ b/src/RW_NDEF.h @@ -35,3 +35,4 @@ void RW_NDEF_Reset(unsigned char type); void RW_NDEF_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); void RW_NDEF_Write_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); bool RW_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); +void RW_NDEF_RegisterPullCallback(void *pCb); From 3485e0260325689cc44efdad9c48051aa0ea1715 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 12:58:55 -0600 Subject: [PATCH 012/106] test: remove reinterpret cast --- examples/NDEFReceive/NDEFReceive.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 046a881..34642de 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -288,7 +288,7 @@ void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("Text record: "); // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); + // Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; @@ -297,7 +297,7 @@ void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("URI record: "); // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); - Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); + // Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; From 0e1f349b6700ad1470a59eeabeb28e848b1e393f Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 13:04:34 -0600 Subject: [PATCH 013/106] test: restore reinterpret cast --- examples/NDEFReceive/NDEFReceive.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 34642de..046a881 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -288,7 +288,7 @@ void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("Text record: "); // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); - // Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; @@ -297,7 +297,7 @@ void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("URI record: "); // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); - // Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); + Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; From 4c2bb072e22e7478e27f63a57055724dd976b4bc Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 1 Aug 2023 14:44:00 -0600 Subject: [PATCH 014/106] fix: redeclaration of ndef helper --- examples/NDEFSend/NDEFSend.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 321f9ad..698ed07 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -1,7 +1,5 @@ #include -#include "ndef_helper.h" - #define PN7150_IRQ (11) #define PN7150_VEN (13) #define PN7150_ADDR (0x28) From 84e1a9654f4521d59257a7bf3a4de240bf1867a5 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 2 Aug 2023 09:42:25 -0600 Subject: [PATCH 015/106] refactor: get firmware version --- src/Electroniccats_PN7150.cpp | 2803 +++++++++++++++------------------ src/Electroniccats_PN7150.h | 254 ++- 2 files changed, 1430 insertions(+), 1627 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index f872085..98d0128 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -26,1871 +26,1684 @@ uint8_t gNextTag_Protocol = PROT_UNDETERMINED; uint8_t NCIStartDiscovery_length = 0; uint8_t NCIStartDiscovery[30]; -unsigned char DiscoveryTechnologiesCE[] = { //Emulation +unsigned char DiscoveryTechnologiesCE[] = { // Emulation MODE_LISTEN | MODE_POLL}; -unsigned char DiscoveryTechnologiesRW[] = { //Read & Write +unsigned char DiscoveryTechnologiesRW[] = { // Read & Write MODE_POLL | TECH_PASSIVE_NFCA, MODE_POLL | TECH_PASSIVE_NFCF, MODE_POLL | TECH_PASSIVE_NFCB, MODE_POLL | TECH_PASSIVE_15693}; -unsigned char DiscoveryTechnologiesP2P[] = { //P2P +unsigned char DiscoveryTechnologiesP2P[] = { // P2P MODE_POLL | TECH_PASSIVE_NFCA, MODE_POLL | TECH_PASSIVE_NFCF, /* Only one POLL ACTIVE mode can be enabled, if both are defined only NFCF applies */ MODE_POLL | TECH_ACTIVE_NFCA, - //MODE_POLL | TECH_ACTIVE_NFCF, + // MODE_POLL | TECH_ACTIVE_NFCF, - //MODE_LISTEN | TECH_PASSIVE_NFCA, + // MODE_LISTEN | TECH_PASSIVE_NFCA, MODE_LISTEN | TECH_PASSIVE_NFCF, MODE_LISTEN | TECH_ACTIVE_NFCA, MODE_LISTEN | TECH_ACTIVE_NFCF}; Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, - uint8_t I2Caddress, TwoWire *wire) : _IRQpin(IRQpin), _VENpin(VENpin), _I2Caddress(I2Caddress), _wire(wire) -{ - pinMode(_IRQpin, INPUT); - if (_VENpin != 255) - pinMode(_VENpin, OUTPUT); + uint8_t I2Caddress, TwoWire *wire) : _IRQpin(IRQpin), _VENpin(VENpin), _I2Caddress(I2Caddress), _wire(wire) { + pinMode(_IRQpin, INPUT); + if (_VENpin != 255) + pinMode(_VENpin, OUTPUT); } -uint8_t Electroniccats_PN7150::begin() -{ - _wire->begin(); - if (_VENpin != 255) { - digitalWrite(_VENpin, HIGH); - delay(1); - digitalWrite(_VENpin, LOW); - delay(1); - digitalWrite(_VENpin, HIGH); - delay(3); - } - return SUCCESS; +uint8_t Electroniccats_PN7150::begin() { + _wire->begin(); + if (_VENpin != 255) { + digitalWrite(_VENpin, HIGH); + delay(1); + digitalWrite(_VENpin, LOW); + delay(1); + digitalWrite(_VENpin, HIGH); + delay(3); + } + return SUCCESS; } -bool Electroniccats_PN7150::hasMessage() const -{ - return (HIGH == digitalRead(_IRQpin)); // PN7150 indicates it has data by driving IRQ signal HIGH +bool Electroniccats_PN7150::hasMessage() const { + return (HIGH == digitalRead(_IRQpin)); // PN7150 indicates it has data by driving IRQ signal HIGH } -uint8_t Electroniccats_PN7150::writeData(uint8_t txBuffer[], uint32_t txBufferLevel) const -{ - uint32_t nmbrBytesWritten = 0; - _wire->beginTransmission((uint8_t)_I2Caddress); //configura transmision - nmbrBytesWritten = _wire->write(txBuffer, (size_t)(txBufferLevel)); //carga en buffer - #ifdef DEBUG2 - Serial.println("[DEBUG] written bytes = 0x"+String(nmbrBytesWritten,HEX)); - #endif - if (nmbrBytesWritten == txBufferLevel) - { - byte resultCode; - resultCode = _wire->endTransmission(); //envio de datos segun yo - #ifdef DEBUG2 - Serial.println("[DEBUG] write data code = 0x"+String(resultCode,HEX)); - #endif - return resultCode; - } - else - { - return 4; // Could not properly copy data to I2C buffer, so treat as other error, see i2c_t3 - } +uint8_t Electroniccats_PN7150::writeData(uint8_t txBuffer[], uint32_t txBufferLevel) const { + uint32_t nmbrBytesWritten = 0; + _wire->beginTransmission((uint8_t)_I2Caddress); // configura transmision + nmbrBytesWritten = _wire->write(txBuffer, (size_t)(txBufferLevel)); // carga en buffer +#ifdef DEBUG2 + Serial.println("[DEBUG] written bytes = 0x" + String(nmbrBytesWritten, HEX)); +#endif + if (nmbrBytesWritten == txBufferLevel) { + byte resultCode; + resultCode = _wire->endTransmission(); // envio de datos segun yo +#ifdef DEBUG2 + Serial.println("[DEBUG] write data code = 0x" + String(resultCode, HEX)); +#endif + return resultCode; + } else { + return 4; // Could not properly copy data to I2C buffer, so treat as other error, see i2c_t3 + } } -uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const -{ - uint32_t bytesReceived; // keeps track of how many bytes we actually received - if (hasMessage()) - { // only try to read something if the PN7150 indicates it has something - bytesReceived = _wire->requestFrom(_I2Caddress, (uint8_t)3); // first reading the header, as this contains how long the payload will be - //Imprimir datos de bytes received, tratar de extraer con funcion read - //Leer e inyectar directo al buffer los siguientes 3 - #ifdef DEBUG2 - Serial.println("[DEBUG] bytesReceived = 0x"+String(bytesReceived,HEX)); - #endif - rxBuffer[0] = _wire->read(); - rxBuffer[1] = _wire->read(); - rxBuffer[2] = _wire->read(); - #ifdef DEBUG2 - for(int i=0;i<3;i++){ - Serial.println("[DEBUG] Byte["+String(i)+"] = 0x"+String(rxBuffer[i],HEX)); - } - #endif - uint8_t payloadLength = rxBuffer[2]; - if (payloadLength > 0) - { - bytesReceived += _wire->requestFrom(_I2Caddress, (uint8_t)payloadLength); // then reading the payload, if any - #ifdef DEBUG2 - Serial.println("[DEBUG] payload bytes = 0x"+String(bytesReceived-3,HEX)); - #endif - uint32_t index = 3; - while (index < bytesReceived) - { - rxBuffer[index] = _wire->read(); - #ifdef DEBUG2 - Serial.println("[DEBUG] payload["+String(index)+"] = 0x"+String(rxBuffer[index],HEX)); - #endif - index++; - } - index = 0; - } +uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const { + uint32_t bytesReceived; // keeps track of how many bytes we actually received + if (hasMessage()) { // only try to read something if the PN7150 indicates it has something + bytesReceived = _wire->requestFrom(_I2Caddress, (uint8_t)3); // first reading the header, as this contains how long the payload will be +// Imprimir datos de bytes received, tratar de extraer con funcion read +// Leer e inyectar directo al buffer los siguientes 3 +#ifdef DEBUG2 + Serial.println("[DEBUG] bytesReceived = 0x" + String(bytesReceived, HEX)); +#endif + rxBuffer[0] = _wire->read(); + rxBuffer[1] = _wire->read(); + rxBuffer[2] = _wire->read(); +#ifdef DEBUG2 + for (int i = 0; i < 3; i++) { + Serial.println("[DEBUG] Byte[" + String(i) + "] = 0x" + String(rxBuffer[i], HEX)); } - else - { - bytesReceived = 0; +#endif + uint8_t payloadLength = rxBuffer[2]; + if (payloadLength > 0) { + bytesReceived += _wire->requestFrom(_I2Caddress, (uint8_t)payloadLength); // then reading the payload, if any +#ifdef DEBUG2 + Serial.println("[DEBUG] payload bytes = 0x" + String(bytesReceived - 3, HEX)); +#endif + uint32_t index = 3; + while (index < bytesReceived) { + rxBuffer[index] = _wire->read(); +#ifdef DEBUG2 + Serial.println("[DEBUG] payload[" + String(index) + "] = 0x" + String(rxBuffer[index], HEX)); +#endif + index++; + } + index = 0; } - return bytesReceived; + } else { + bytesReceived = 0; + } + return bytesReceived; } -bool Electroniccats_PN7150::isTimeOut() const -{ - return ((millis() - timeOutStartTime) >= timeOut); +bool Electroniccats_PN7150::isTimeOut() const { + return ((millis() - timeOutStartTime) >= timeOut); } -void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) -{ - timeOutStartTime = millis(); - timeOut = theTimeOut; +void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) { + timeOutStartTime = millis(); + timeOut = theTimeOut; } -bool Electroniccats_PN7150::getMessage(uint16_t timeout) -{ // check for message using timeout, 5 milisec as default - setTimeOut(timeout); - rxMessageLength = 0; - while (!isTimeOut()) - { - rxMessageLength = readData(rxBuffer); - if (rxMessageLength) - break; - else if (timeout == 1337) - setTimeOut(timeout); - } - return rxMessageLength; +bool Electroniccats_PN7150::getMessage(uint16_t timeout) { // check for message using timeout, 5 milisec as default + setTimeOut(timeout); + rxMessageLength = 0; + while (!isTimeOut()) { + rxMessageLength = readData(rxBuffer); + if (rxMessageLength) + break; + else if (timeout == 1337) + setTimeOut(timeout); + } + return rxMessageLength; } -uint8_t Electroniccats_PN7150::wakeupNCI() -{ // the device has to wake up using a core reset - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; - uint16_t NbBytes = 0; - - // Reset RF settings restauration flag - (void)writeData(NCICoreReset, 4); - getMessage(15); - NbBytes = rxMessageLength; - if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) - { - return ERROR; - } - getMessage(); - NbBytes = rxMessageLength; - if (NbBytes != 0) - { - //NCI_PRINT_BUF("NCI << ", Answer, NbBytes); - // Is CORE_GENERIC_ERROR_NTF ? - if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) - { - /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ - //if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; - } - else - { - return ERROR; - } +uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; + uint16_t NbBytes = 0; + + // Reset RF settings restauration flag + (void)writeData(NCICoreReset, 4); + getMessage(15); + NbBytes = rxMessageLength; + if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { + return ERROR; + } + getMessage(); + NbBytes = rxMessageLength; + if (NbBytes != 0) { + // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); + // Is CORE_GENERIC_ERROR_NTF ? + if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { + /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ + // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; + } else { + return ERROR; } - return SUCCESS; + } + return SUCCESS; } -uint8_t Electroniccats_PN7150::connectNCI() -{ - uint8_t i = 2; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - - // Open connection to NXPNCI - begin(); - // Loop until NXPNCI answers - while (wakeupNCI() != SUCCESS) - { - if (i-- == 0) - return ERROR; - delay(500); - } - - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - return ERROR; - - // Retrieve NXP-NCI NFC Controller generation - if (rxBuffer[17 + rxBuffer[8]] == 0x08) - gNfcController_generation = 1; - else if (rxBuffer[17 + rxBuffer[8]] == 0x10) - gNfcController_generation = 2; - - // Retrieve NXP-NCI NFC Controller FW version - gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; //0xROM_CODE_V - gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; //0xFW_MAJOR_NO - gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; //0xFW_MINOR_NO - #ifdef DEBUG - Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); - Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); - Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); - Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); - #endif +uint8_t Electroniccats_PN7150::connectNCI() { + uint8_t i = 2; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + + // Open connection to NXPNCI + begin(); + // Loop until NXPNCI answers + while (wakeupNCI() != SUCCESS) { + if (i-- == 0) + return ERROR; + delay(500); + } + + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) + return ERROR; + + // Retrieve NXP-NCI NFC Controller generation + if (rxBuffer[17 + rxBuffer[8]] == 0x08) + gNfcController_generation = 1; + else if (rxBuffer[17 + rxBuffer[8]] == 0x10) + gNfcController_generation = 2; + + // Retrieve NXP-NCI NFC Controller FW version + gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V + gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO + gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO +#ifdef DEBUG + Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); + Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); + Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); + Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); +#endif - return SUCCESS; + return SUCCESS; } -int Electroniccats_PN7150::GetFwVersion() -{ - return ((gNfcController_fw_version[0] & 0xFF) << 16) | ((gNfcController_fw_version[1] & 0xFF) << 8) | (gNfcController_fw_version[2] & 0xFF); +int Electroniccats_PN7150::getFirmwareVersion() { + return ((gNfcController_fw_version[0] & 0xFF) << 16) | ((gNfcController_fw_version[1] & 0xFF) << 8) | (gNfcController_fw_version[2] & 0xFF); } -uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) -{ - unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU - : MODE_P2P); - - uint8_t Command[MAX_NCI_FRAME_SIZE]; +int Electroniccats_PN7150::GetFwVersion() { + return getFirmwareVersion(); +} - uint8_t Item = 0; - uint8_t NCIDiscoverMap[] = {0x21, 0x00}; +uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { + unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU + : MODE_P2P); - //Emulation mode - const uint8_t DM_CARDEMU[] = {0x4, 0x2, 0x2}; - const uint8_t R_CARDEMU[] = {0x1, 0x3, 0x0, 0x1, 0x4}; + uint8_t Command[MAX_NCI_FRAME_SIZE]; - //RW Mode - const uint8_t DM_RW[] = {0x1, 0x1, 0x1, 0x2, 0x1, 0x1, 0x3, 0x1, 0x1, 0x4, 0x1, 0x2, 0x80, 0x01, 0x80}; - uint8_t NCIPropAct[] = {0x2F, 0x02, 0x00}; + uint8_t Item = 0; + uint8_t NCIDiscoverMap[] = {0x21, 0x00}; - //P2P Support - const uint8_t DM_P2P[] = {0x5, 0x3, 0x3}; - const uint8_t R_P2P[] = {0x1, 0x3, 0x0, 0x1, 0x5}; - uint8_t NCISetConfig_NFC[] = {0x20, 0x02, 0x1F, 0x02, 0x29, 0x0D, 0x46, 0x66, 0x6D, 0x01, 0x01, 0x11, 0x03, 0x02, 0x00, 0x01, 0x04, 0x01, 0xFA, 0x61, 0x0D, 0x46, 0x66, 0x6D, 0x01, 0x01, 0x11, 0x03, 0x02, 0x00, 0x01, 0x04, 0x01, 0xFA}; + // Emulation mode + const uint8_t DM_CARDEMU[] = {0x4, 0x2, 0x2}; + const uint8_t R_CARDEMU[] = {0x1, 0x3, 0x0, 0x1, 0x4}; - uint8_t NCIRouting[] = {0x21, 0x01, 0x07, 0x00, 0x01}; - uint8_t NCISetConfig_NFCA_SELRSP[] = {0x20, 0x02, 0x04, 0x01, 0x32, 0x01, 0x00}; + // RW Mode + const uint8_t DM_RW[] = {0x1, 0x1, 0x1, 0x2, 0x1, 0x1, 0x3, 0x1, 0x1, 0x4, 0x1, 0x2, 0x80, 0x01, 0x80}; + uint8_t NCIPropAct[] = {0x2F, 0x02, 0x00}; - if (mode == 0) - return SUCCESS; + // P2P Support + const uint8_t DM_P2P[] = {0x5, 0x3, 0x3}; + const uint8_t R_P2P[] = {0x1, 0x3, 0x0, 0x1, 0x5}; + uint8_t NCISetConfig_NFC[] = {0x20, 0x02, 0x1F, 0x02, 0x29, 0x0D, 0x46, 0x66, 0x6D, 0x01, 0x01, 0x11, 0x03, 0x02, 0x00, 0x01, 0x04, 0x01, 0xFA, 0x61, 0x0D, 0x46, 0x66, 0x6D, 0x01, 0x01, 0x11, 0x03, 0x02, 0x00, 0x01, 0x04, 0x01, 0xFA}; - /* Enable Proprietary interface for T4T card presence check procedure */ - if (modeSE == 1) - { - if (mode == MODE_RW) - { - (void)writeData(NCIPropAct, sizeof(NCIPropAct)); - getMessage(); + uint8_t NCIRouting[] = {0x21, 0x01, 0x07, 0x00, 0x01}; + uint8_t NCISetConfig_NFCA_SELRSP[] = {0x20, 0x02, 0x04, 0x01, 0x32, 0x01, 0x00}; - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00)) - return ERROR; - } - } + if (mode == 0) + return SUCCESS; - //* Building Discovery Map command - Item = 0; + /* Enable Proprietary interface for T4T card presence check procedure */ + if (modeSE == 1) { + if (mode == MODE_RW) { + (void)writeData(NCIPropAct, sizeof(NCIPropAct)); + getMessage(); - if ((mode & MODE_CARDEMU and modeSE == 2) || (mode & MODE_P2P and modeSE == 3)) - { - memcpy(&Command[4 + (3 * Item)], (modeSE == 2 ? DM_CARDEMU : DM_P2P), sizeof((modeSE == 2 ? DM_CARDEMU : DM_P2P))); - Item++; + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00)) + return ERROR; } - if (mode & MODE_RW and modeSE == 1) - { - memcpy(&Command[4 + (3 * Item)], DM_RW, sizeof(DM_RW)); - Item += sizeof(DM_RW) / 3; + } + + //* Building Discovery Map command + Item = 0; + + if ((mode & MODE_CARDEMU and modeSE == 2) || (mode & MODE_P2P and modeSE == 3)) { + memcpy(&Command[4 + (3 * Item)], (modeSE == 2 ? DM_CARDEMU : DM_P2P), sizeof((modeSE == 2 ? DM_CARDEMU : DM_P2P))); + Item++; + } + if (mode & MODE_RW and modeSE == 1) { + memcpy(&Command[4 + (3 * Item)], DM_RW, sizeof(DM_RW)); + Item += sizeof(DM_RW) / 3; + } + if (Item != 0) { + memcpy(Command, NCIDiscoverMap, sizeof(NCIDiscoverMap)); + Command[2] = 1 + (Item * 3); + Command[3] = Item; + (void)writeData(Command, 3 + Command[2]); + getMessage(10); + if ((rxBuffer[0] != 0x41) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { + return ERROR; } - if (Item != 0) - { - memcpy(Command, NCIDiscoverMap, sizeof(NCIDiscoverMap)); - Command[2] = 1 + (Item * 3); - Command[3] = Item; - (void)writeData(Command, 3 + Command[2]); - getMessage(10); - if ((rxBuffer[0] != 0x41) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) - { - return ERROR; - } + } + + // Configuring routing + Item = 0; + + if (modeSE == 2 || modeSE == 3) { // Emulation or P2P + memcpy(&Command[5 + (5 * Item)], (modeSE == 2 ? R_CARDEMU : R_P2P), sizeof((modeSE == 2 ? R_CARDEMU : R_P2P))); + Item++; + + if (Item != 0) { + memcpy(Command, NCIRouting, sizeof(NCIRouting)); + Command[2] = 2 + (Item * 5); + Command[4] = Item; + (void)writeData(Command, 3 + Command[2]); + getMessage(); + if ((rxBuffer[0] != 0x41) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) + return ERROR; } + NCISetConfig_NFCA_SELRSP[6] += (modeSE == 2 ? 0x20 : 0x40); - // Configuring routing - Item = 0; - - if (modeSE == 2 || modeSE == 3) - { //Emulation or P2P - memcpy(&Command[5 + (5 * Item)], (modeSE == 2 ? R_CARDEMU : R_P2P), sizeof((modeSE == 2 ? R_CARDEMU : R_P2P))); - Item++; - - if (Item != 0) - { - memcpy(Command, NCIRouting, sizeof(NCIRouting)); - Command[2] = 2 + (Item * 5); - Command[4] = Item; - (void)writeData(Command, 3 + Command[2]); - getMessage(); - if ((rxBuffer[0] != 0x41) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - return ERROR; - } - NCISetConfig_NFCA_SELRSP[6] += (modeSE == 2 ? 0x20 : 0x40); - - if (NCISetConfig_NFCA_SELRSP[6] != 0x00) - { - (void)writeData(NCISetConfig_NFCA_SELRSP, sizeof(NCISetConfig_NFCA_SELRSP)); - getMessage(); + if (NCISetConfig_NFCA_SELRSP[6] != 0x00) { + (void)writeData(NCISetConfig_NFCA_SELRSP, sizeof(NCISetConfig_NFCA_SELRSP)); + getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00)) - return ERROR; - else - return SUCCESS; - } + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00)) + return ERROR; + else + return SUCCESS; + } - if (mode & MODE_P2P and modeSE == 3) - { - (void)writeData(NCISetConfig_NFC, sizeof(NCISetConfig_NFC)); - getMessage(); + if (mode & MODE_P2P and modeSE == 3) { + (void)writeData(NCISetConfig_NFC, sizeof(NCISetConfig_NFC)); + getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00)) - return ERROR; - } + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00)) + return ERROR; } - return SUCCESS; + } + return SUCCESS; } -uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) -{ - unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) - : sizeof(DiscoveryTechnologiesP2P)); - - NCIStartDiscovery_length = 0; - NCIStartDiscovery[0] = 0x21; - NCIStartDiscovery[1] = 0x03; - NCIStartDiscovery[2] = (TechTabSize * 2) + 1; - NCIStartDiscovery[3] = TechTabSize; - for (uint8_t i = 0; i < TechTabSize; i++) - { - NCIStartDiscovery[(i * 2) + 4] = (modeSE == 1 ? DiscoveryTechnologiesRW[i] : modeSE == 2 ? DiscoveryTechnologiesCE[i] - : DiscoveryTechnologiesP2P[i]); - - NCIStartDiscovery[(i * 2) + 5] = 0x01; - } - - NCIStartDiscovery_length = (TechTabSize * 2) + 4; - (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); - getMessage(); - - if ((rxBuffer[0] != 0x41) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) - return ERROR; - else - return SUCCESS; +uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { + unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) + : sizeof(DiscoveryTechnologiesP2P)); + + NCIStartDiscovery_length = 0; + NCIStartDiscovery[0] = 0x21; + NCIStartDiscovery[1] = 0x03; + NCIStartDiscovery[2] = (TechTabSize * 2) + 1; + NCIStartDiscovery[3] = TechTabSize; + for (uint8_t i = 0; i < TechTabSize; i++) { + NCIStartDiscovery[(i * 2) + 4] = (modeSE == 1 ? DiscoveryTechnologiesRW[i] : modeSE == 2 ? DiscoveryTechnologiesCE[i] + : DiscoveryTechnologiesP2P[i]); + + NCIStartDiscovery[(i * 2) + 5] = 0x01; + } + + NCIStartDiscovery_length = (TechTabSize * 2) + 4; + (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); + getMessage(); + + if ((rxBuffer[0] != 0x41) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) + return ERROR; + else + return SUCCESS; } -void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) -{ - uint8_t Answer[MAX_NCI_FRAME_SIZE]; +void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { + uint8_t Answer[MAX_NCI_FRAME_SIZE]; - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; - bool FirstCmd = true; + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; + bool FirstCmd = true; - /* Reset Card emulation state */ - T4T_NDEF_EMU_Reset(); + /* Reset Card emulation state */ + T4T_NDEF_EMU_Reset(); - // (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); - getMessage(2000); - //NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_2S) == NXPNCI_SUCCESS + // (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); + getMessage(2000); + // NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_2S) == NXPNCI_SUCCESS - while (rxMessageLength > 0) - { - getMessage(2000); - /* is RF_DEACTIVATE_NTF ? */ - if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) - { - if (FirstCmd) - { - /* Restart the discovery loop */ - //NxpNci_HostTransceive(NCIStopDiscovery, sizeof(NCIStopDiscovery), Answer, sizeof(Answer), &AnswerSize); - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - do - { - if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x06)) - break; - //NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_100MS); - //(void)writeData(rxBuffer, rxMessageLength); - getMessage(100); - } while (rxMessageLength != 0); - //NxpNci_HostTransceive(NCIStartDiscovery, NCIStartDiscovery_length, Answer, sizeof(Answer), &AnswerSize); - (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); - getMessage(); - } - /* Come back to discovery state */ - // break; - } - /* is DATA_PACKET ? */ - else if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) - { - /* DATA_PACKET */ - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize; + while (rxMessageLength > 0) { + getMessage(2000); + /* is RF_DEACTIVATE_NTF ? */ + if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) { + if (FirstCmd) { + /* Restart the discovery loop */ + // NxpNci_HostTransceive(NCIStopDiscovery, sizeof(NCIStopDiscovery), Answer, sizeof(Answer), &AnswerSize); + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); + do { + if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x06)) + break; + // NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_100MS); + //(void)writeData(rxBuffer, rxMessageLength); + getMessage(100); + } while (rxMessageLength != 0); + // NxpNci_HostTransceive(NCIStartDiscovery, NCIStartDiscovery_length, Answer, sizeof(Answer), &AnswerSize); + (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); + getMessage(); + } + /* Come back to discovery state */ + // break; + } + /* is DATA_PACKET ? */ + else if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { + /* DATA_PACKET */ + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize; - T4T_NDEF_EMU_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + T4T_NDEF_EMU_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; - //NxpNci_HostTransceive(Cmd, CmdSize+3, Answer, sizeof(Answer), &AnswerSize); - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - } - FirstCmd = false; + // NxpNci_HostTransceive(Cmd, CmdSize+3, Answer, sizeof(Answer), &AnswerSize); + (void)writeData(Cmd, CmdSize + 3); + getMessage(); } + FirstCmd = false; + } } -bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) -{ - bool status; - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - - /* Compute and send DATA_PACKET */ - Cmd[0] = 0x00; - Cmd[1] = 0x00; - Cmd[2] = DataSize; - memcpy(&Cmd[3], pData, DataSize); - (void)writeData(Cmd, DataSize + 3); - return status; +bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { + bool status; + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + + /* Compute and send DATA_PACKET */ + Cmd[0] = 0x00; + Cmd[1] = 0x00; + Cmd[2] = DataSize; + memcpy(&Cmd[3], pData, DataSize); + (void)writeData(Cmd, DataSize + 3); + return status; } -bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) -{ - #ifdef DEBUG2 - Serial.println("[DEBUG] cardModeReceive exec"); - #endif - #ifdef DEBUG3 - Serial.println("[DEBUG] cardModeReceive exec"); - #endif - - bool status = NFC_ERROR; - uint8_t Ans[MAX_NCI_FRAME_SIZE]; +bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { +#ifdef DEBUG2 + Serial.println("[DEBUG] cardModeReceive exec"); +#endif +#ifdef DEBUG3 + Serial.println("[DEBUG] cardModeReceive exec"); +#endif - (void)writeData(Ans, 255); - getMessage(2000); + bool status = NFC_ERROR; + uint8_t Ans[MAX_NCI_FRAME_SIZE]; - /* Is data packet ? */ - if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) - { + (void)writeData(Ans, 255); + getMessage(2000); - #ifdef DEBUG2 - Serial.println(rxBuffer[2]); - #endif - *pDataSize = rxBuffer[2]; - memcpy(pData, &rxBuffer[3], *pDataSize); - status = NFC_SUCCESS; - } - else - { - status = NFC_ERROR; - } - return status; + /* Is data packet ? */ + if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { +#ifdef DEBUG2 + Serial.println(rxBuffer[2]); +#endif + *pDataSize = rxBuffer[2]; + memcpy(pData, &rxBuffer[3], *pDataSize); + status = NFC_SUCCESS; + } else { + status = NFC_ERROR; + } + return status; } -void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) -{ - uint8_t i, temp; +void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { + uint8_t i, temp; - switch (pRfIntf->ModeTech) - { + switch (pRfIntf->ModeTech) { case (MODE_POLL | TECH_PASSIVE_NFCA): - memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); - temp = 2; - pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); + temp = 2; + pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; + temp++; + memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); + temp += pBuf[2]; + pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; + temp++; + + if (pRfIntf->Info.NFC_APP.SelResLen == 1) + pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; + + temp += 4; + if (pBuf[temp] != 0) { temp++; - memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); - temp += pBuf[2]; - pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; - temp++; - - if (pRfIntf->Info.NFC_APP.SelResLen == 1) - pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; - - temp += 4; - if (pBuf[temp] != 0) - { - temp++; - pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; - memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); - } - else - { - pRfIntf->Info.NFC_APP.RatsLen = 0; - } - break; + pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); + } else { + pRfIntf->Info.NFC_APP.RatsLen = 0; + } + break; case (MODE_POLL | TECH_PASSIVE_NFCB): - pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; - memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); - temp = pBuf[0] + 4; - if (pBuf[temp] != 0) - { - temp++; - pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; - memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); - } - else - { - pRfIntf->Info.NFC_BPP.AttribResLen = 0; - } - break; + pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; + memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); + temp = pBuf[0] + 4; + if (pBuf[temp] != 0) { + temp++; + pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); + } else { + pRfIntf->Info.NFC_BPP.AttribResLen = 0; + } + break; case (MODE_POLL | TECH_PASSIVE_NFCF): - pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; - pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; - memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); - break; + pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; + pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; + memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); + break; case (MODE_POLL | TECH_PASSIVE_15693): - pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; - pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; + pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; + pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; - for (i = 0; i < 8; i++) - pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; + for (i = 0; i < 8; i++) + pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; - break; + break; default: - break; - } + break; + } } -bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) -{ - bool status = ERROR; - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; +bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { + bool status = ERROR; + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - /* Compute and send DATA_PACKET */ - Cmd[0] = 0x00; - Cmd[1] = 0x00; - Cmd[2] = CommandSize; - memcpy(&Cmd[3], pCommand, CommandSize); + /* Compute and send DATA_PACKET */ + Cmd[0] = 0x00; + Cmd[1] = 0x00; + Cmd[2] = CommandSize; + memcpy(&Cmd[3], pCommand, CommandSize); - (void)writeData(Cmd, CommandSize + 3); - getMessage(); - getMessage(1000); - /* Wait for Answer 1S */ + (void)writeData(Cmd, CommandSize + 3); + getMessage(); + getMessage(1000); + /* Wait for Answer 1S */ - if ((rxBuffer[0] == 0x0) && (rxBuffer[1] == 0x0)) - status = SUCCESS; + if ((rxBuffer[0] == 0x0) && (rxBuffer[1] == 0x0)) + status = SUCCESS; - *pAnswerSize = rxBuffer[2]; - memcpy(pAnswer, &rxBuffer[3], *pAnswerSize); + *pAnswerSize = rxBuffer[2]; + memcpy(pAnswer, &rxBuffer[3], *pAnswerSize); - return status; + return status; } -bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) -{ - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; +bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; - //P2P Support - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; - uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; - uint8_t saved_NTF[7]; + // P2P Support + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; + uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; + uint8_t saved_NTF[7]; - gNextTag_Protocol = PROT_UNDETERMINED; - bool getFlag = false; + gNextTag_Protocol = PROT_UNDETERMINED; + bool getFlag = false; wait: - do - { - getFlag = getMessage( - tout > 0 ? tout : 1337 - ); //Infinite loop, waiting for response - } while (((rxBuffer[0] != 0x61) || ((rxBuffer[1] != 0x05) && (rxBuffer[1] != 0x03))) && (getFlag == true)); - gNextTag_Protocol = PROT_UNDETERMINED; - - /* Is RF_INTF_ACTIVATED_NTF ? */ - if (rxBuffer[1] == 0x05) - { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - pRfIntf->MoreTags = false; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - - //P2P - /* Verifying if not a P2P device also presenting T4T emulation */ - if ((pRfIntf->Interface == INTF_ISODEP) && (pRfIntf->Protocol == PROT_ISODEP) && ((pRfIntf->ModeTech & MODE_LISTEN) != MODE_LISTEN)) - { - memcpy(saved_NTF, rxBuffer, sizeof(saved_NTF)); - while (1) - { - /* Restart the discovery loop */ - (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); - getMessage(); - getMessage(100); - /* Wait for discovery */ - do - { - getMessage(1000); //Infinite loop, waiting for response - } while ((rxMessageLength == 4) && (rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)); - - if ((rxMessageLength != 0) && (rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)) - { - /* Is same device detected ? */ - if (memcmp(saved_NTF, rxBuffer, sizeof(saved_NTF)) == 0) - break; - /* Is P2P detected ? */ - if (rxBuffer[5] == PROT_NFCDEP) - { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - pRfIntf->MoreTags = false; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - break; - } - } - else - { - if (rxMessageLength != 0) - { - /* Flush any other notification */ - while (rxMessageLength != 0) - getMessage(100); - - /* Restart the discovery loop */ - (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); - getMessage(); - getMessage(100); - } - goto wait; - } - } + do { + getFlag = getMessage( + tout > 0 ? tout : 1337); // Infinite loop, waiting for response + } while (((rxBuffer[0] != 0x61) || ((rxBuffer[1] != 0x05) && (rxBuffer[1] != 0x03))) && (getFlag == true)); + gNextTag_Protocol = PROT_UNDETERMINED; + + /* Is RF_INTF_ACTIVATED_NTF ? */ + if (rxBuffer[1] == 0x05) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + pRfIntf->MoreTags = false; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + + // P2P + /* Verifying if not a P2P device also presenting T4T emulation */ + if ((pRfIntf->Interface == INTF_ISODEP) && (pRfIntf->Protocol == PROT_ISODEP) && ((pRfIntf->ModeTech & MODE_LISTEN) != MODE_LISTEN)) { + memcpy(saved_NTF, rxBuffer, sizeof(saved_NTF)); + while (1) { + /* Restart the discovery loop */ + (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); + getMessage(); + getMessage(100); + /* Wait for discovery */ + do { + getMessage(1000); // Infinite loop, waiting for response + } while ((rxMessageLength == 4) && (rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)); + + if ((rxMessageLength != 0) && (rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)) { + /* Is same device detected ? */ + if (memcmp(saved_NTF, rxBuffer, sizeof(saved_NTF)) == 0) + break; + /* Is P2P detected ? */ + if (rxBuffer[5] == PROT_NFCDEP) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + pRfIntf->MoreTags = false; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + break; + } + } else { + if (rxMessageLength != 0) { + /* Flush any other notification */ + while (rxMessageLength != 0) + getMessage(100); + + /* Restart the discovery loop */ + (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); + getMessage(); + getMessage(100); + } + goto wait; } + } } + } else { /* RF_DISCOVER_NTF */ + pRfIntf->Interface = INTF_UNDETERMINED; + pRfIntf->Protocol = rxBuffer[4]; + pRfIntf->ModeTech = rxBuffer[5]; + pRfIntf->MoreTags = true; + + /* Get next NTF for further activation */ + do { + if (!getMessage(100)) + return ERROR; + } while ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x03)); + gNextTag_Protocol = rxBuffer[4]; + + /* Remaining NTF ? */ + + while (rxBuffer[rxMessageLength - 1] == 0x02) + getMessage(100); + + /* In case of multiple cards, select the first one */ + NCIRfDiscoverSelect[4] = pRfIntf->Protocol; + if (pRfIntf->Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_ISODEP; + else if (pRfIntf->Protocol == PROT_NFCDEP) + NCIRfDiscoverSelect[5] = INTF_NFCDEP; + else if (pRfIntf->Protocol == PROT_MIFARE) + NCIRfDiscoverSelect[5] = INTF_TAGCMD; else - { /* RF_DISCOVER_NTF */ - pRfIntf->Interface = INTF_UNDETERMINED; - pRfIntf->Protocol = rxBuffer[4]; - pRfIntf->ModeTech = rxBuffer[5]; - pRfIntf->MoreTags = true; - - /* Get next NTF for further activation */ - do - { - if (!getMessage(100)) - return ERROR; - } while ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x03)); - gNextTag_Protocol = rxBuffer[4]; - - /* Remaining NTF ? */ - - while (rxBuffer[rxMessageLength - 1] == 0x02) - getMessage(100); + NCIRfDiscoverSelect[5] = INTF_FRAME; - /* In case of multiple cards, select the first one */ - NCIRfDiscoverSelect[4] = pRfIntf->Protocol; - if (pRfIntf->Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (pRfIntf->Protocol == PROT_NFCDEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (pRfIntf->Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; - else - NCIRfDiscoverSelect[5] = INTF_FRAME; - - (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); - getMessage(100); + (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); + getMessage(100); - if ((rxBuffer[0] == 0x41) || (rxBuffer[1] == 0x04) || (rxBuffer[3] == 0x00)) - { - (void)writeData(rxBuffer, rxMessageLength); - getMessage(100); + if ((rxBuffer[0] == 0x41) || (rxBuffer[1] == 0x04) || (rxBuffer[3] == 0x00)) { + (void)writeData(rxBuffer, rxMessageLength); + getMessage(100); - if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) - { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - } + if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + } - /* In case of P2P target detected but lost, inform application to restart discovery */ - else if (pRfIntf->Protocol == PROT_NFCDEP) - { - /* Restart the discovery loop */ - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - getMessage(100); + /* In case of P2P target detected but lost, inform application to restart discovery */ + else if (pRfIntf->Protocol == PROT_NFCDEP) { + /* Restart the discovery loop */ + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); + getMessage(100); - (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); - getMessage(); + (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); + getMessage(); - goto wait; - } - } + goto wait; + } } + } - /* In case of unknown target align protocol information */ - if (pRfIntf->Interface == INTF_UNDETERMINED) - pRfIntf->Protocol = PROT_UNDETERMINED; + /* In case of unknown target align protocol information */ + if (pRfIntf->Interface == INTF_UNDETERMINED) + pRfIntf->Protocol = PROT_UNDETERMINED; - return SUCCESS; + return SUCCESS; } -void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) -{ - uint8_t status = ERROR; - bool restart = false; - uint8_t NCILlcpSymm[] = {0x00, 0x00, 0x02, 0x00, 0x00}; - uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; - - /* Reset P2P_NDEF state */ - P2P_NDEF_Reset(); - - /* Is Initiator mode ? */ - if ((RfIntf.ModeTech & MODE_LISTEN) != MODE_LISTEN) - { - /* Initiate communication (SYMM PDU) */ - (void)writeData(NCILlcpSymm, sizeof(NCILlcpSymm)); - getMessage(); +void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) { + uint8_t status = ERROR; + bool restart = false; + uint8_t NCILlcpSymm[] = {0x00, 0x00, 0x02, 0x00, 0x00}; + uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; - /* Save status for discovery restart */ - restart = true; - } - status = ERROR; - getMessage(2000); - if (rxMessageLength > 0) - status = SUCCESS; + /* Reset P2P_NDEF state */ + P2P_NDEF_Reset(); - /* Get frame from remote peer */ - while (status == SUCCESS) - { - /* is DATA_PACKET ? */ - if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) - { - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize; - /* Handle P2P communication */ - P2P_NDEF_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - /* Compute DATA_PACKET to answer */ - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - status = ERROR; - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - if (rxMessageLength > 0) - status = SUCCESS; - } - /* is CORE_INTERFACE_ERROR_NTF ?*/ - else if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x08)) - { - /* Come back to discovery state */ - break; - } - /* is RF_DEACTIVATE_NTF ? */ - else if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) - { - /* Come back to discovery state */ - break; - } - /* is RF_DISCOVERY_NTF ? */ - else if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) - { - do - { - if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) - { - if ((rxBuffer[6] & MODE_LISTEN) != MODE_LISTEN) - restart = true; - else - restart = false; - } - status = ERROR; - (void)writeData(rxBuffer, rxMessageLength); - getMessage(); - if (rxMessageLength > 0) - status = SUCCESS; - } while (rxMessageLength != 0); - /* Come back to discovery state */ - break; - } + /* Is Initiator mode ? */ + if ((RfIntf.ModeTech & MODE_LISTEN) != MODE_LISTEN) { + /* Initiate communication (SYMM PDU) */ + (void)writeData(NCILlcpSymm, sizeof(NCILlcpSymm)); + getMessage(); - /* Wait for next frame from remote P2P, or notification event */ + /* Save status for discovery restart */ + restart = true; + } + status = ERROR; + getMessage(2000); + if (rxMessageLength > 0) + status = SUCCESS; + + /* Get frame from remote peer */ + while (status == SUCCESS) { + /* is DATA_PACKET ? */ + if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize; + /* Handle P2P communication */ + P2P_NDEF_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + /* Compute DATA_PACKET to answer */ + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + status = ERROR; + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + if (rxMessageLength > 0) + status = SUCCESS; + } + /* is CORE_INTERFACE_ERROR_NTF ?*/ + else if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x08)) { + /* Come back to discovery state */ + break; + } + /* is RF_DEACTIVATE_NTF ? */ + else if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) { + /* Come back to discovery state */ + break; + } + /* is RF_DISCOVERY_NTF ? */ + else if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) { + do { + if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) { + if ((rxBuffer[6] & MODE_LISTEN) != MODE_LISTEN) + restart = true; + else + restart = false; + } status = ERROR; (void)writeData(rxBuffer, rxMessageLength); getMessage(); if (rxMessageLength > 0) - status = SUCCESS; + status = SUCCESS; + } while (rxMessageLength != 0); + /* Come back to discovery state */ + break; } - /* Is Initiator mode ? */ - if (restart) - { - /* Communication ended, restart discovery loop */ - (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); - getMessage(); - getMessage(100); - } -} - -void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) -{ - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize = 0; + /* Wait for next frame from remote P2P, or notification event */ + status = ERROR; + (void)writeData(rxBuffer, rxMessageLength); + getMessage(); + if (rxMessageLength > 0) + status = SUCCESS; + } - RW_NDEF_Reset(RfIntf.Protocol); + /* Is Initiator mode ? */ + if (restart) { + /* Communication ended, restart discovery loop */ + (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); + getMessage(); + getMessage(100); + } +} - while (1) - { - RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - if (CmdSize == 0) - { - /// End of the Read operation - break; - } - else - { - // Compute and send DATA_PACKET - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - getMessage(1000); - - // Manage chaining in case of T4T - if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) - { - uint8_t tmp[MAX_NCI_FRAME_SIZE]; - uint8_t tmpSize = 0; - while (rxBuffer[0] == 0x10) - { - memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); - tmpSize += rxBuffer[2]; - getMessage(100); - } - memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); - tmpSize += rxBuffer[2]; - //* Compute all chained frame into one unique answer - memcpy(&rxBuffer[3], tmp, tmpSize); - rxBuffer[2] = tmpSize; - } +void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize = 0; + + RW_NDEF_Reset(RfIntf.Protocol); + + while (1) { + RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + if (CmdSize == 0) { + /// End of the Read operation + break; + } else { + // Compute and send DATA_PACKET + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + getMessage(1000); + + // Manage chaining in case of T4T + if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { + uint8_t tmp[MAX_NCI_FRAME_SIZE]; + uint8_t tmpSize = 0; + while (rxBuffer[0] == 0x10) { + memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); + tmpSize += rxBuffer[2]; + getMessage(100); } + memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); + tmpSize += rxBuffer[2]; + //* Compute all chained frame into one unique answer + memcpy(&rxBuffer[3], tmp, tmpSize); + rxBuffer[2] = tmpSize; + } } + } } -void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) -{ - - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize = 0; - - RW_NDEF_Reset(RfIntf.Protocol); - - while (1) - { - RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - if (CmdSize == 0) - { - // End of the Write operation - break; - } - else - { - // Compute and send DATA_PACKET - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - getMessage(2000); - } +void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize = 0; + + RW_NDEF_Reset(RfIntf.Protocol); + + while (1) { + RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + if (CmdSize == 0) { + // End of the Write operation + break; + } else { + // Compute and send DATA_PACKET + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + getMessage(2000); } + } } -void Electroniccats_PN7150::ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) -{ - switch (Operation) - { +void Electroniccats_PN7150::ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) { + switch (Operation) { case READ_NDEF: - ReadNdef(RfIntf); - break; + ReadNdef(RfIntf); + break; case WRITE_NDEF: - WriteNdef(RfIntf); - break; + WriteNdef(RfIntf); + break; case PRESENCE_CHECK: - PresenceCheck(RfIntf); - break; + PresenceCheck(RfIntf); + break; default: - break; - } + break; + } } -void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) -{ - bool status; - uint8_t i; - - uint8_t NCIPresCheckT1T[] = {0x00, 0x00, 0x07, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t NCIPresCheckT2T[] = {0x00, 0x00, 0x02, 0x30, 0x00}; - uint8_t NCIPresCheckT3T[] = {0x21, 0x08, 0x04, 0xFF, 0xFF, 0x00, 0x01}; - uint8_t NCIPresCheckIsoDep[] = {0x2F, 0x11, 0x00}; - uint8_t NCIPresCheckIso15693[] = {0x00, 0x00, 0x0B, 0x26, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCISelectMIFARE[] = {0x21, 0x04, 0x03, 0x01, 0x80, 0x80}; - - switch (RfIntf.Protocol) - { +void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { + bool status; + uint8_t i; + + uint8_t NCIPresCheckT1T[] = {0x00, 0x00, 0x07, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t NCIPresCheckT2T[] = {0x00, 0x00, 0x02, 0x30, 0x00}; + uint8_t NCIPresCheckT3T[] = {0x21, 0x08, 0x04, 0xFF, 0xFF, 0x00, 0x01}; + uint8_t NCIPresCheckIsoDep[] = {0x2F, 0x11, 0x00}; + uint8_t NCIPresCheckIso15693[] = {0x00, 0x00, 0x0B, 0x26, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCISelectMIFARE[] = {0x21, 0x04, 0x03, 0x01, 0x80, 0x80}; + + switch (RfIntf.Protocol) { case PROT_T1T: - do - { - delay(500); - (void)writeData(NCIPresCheckT1T, sizeof(NCIPresCheckT1T)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)); - break; + do { + delay(500); + (void)writeData(NCIPresCheckT1T, sizeof(NCIPresCheckT1T)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)); + break; case PROT_T2T: - do - { - delay(500); - (void)writeData(NCIPresCheckT2T, sizeof(NCIPresCheckT2T)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[2] == 0x11)); - break; + do { + delay(500); + (void)writeData(NCIPresCheckT2T, sizeof(NCIPresCheckT2T)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[2] == 0x11)); + break; case PROT_T3T: - do - { - delay(500); - (void)writeData(NCIPresCheckT3T, sizeof(NCIPresCheckT3T)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x08) && ((rxBuffer[3] == 0x00) || (rxBuffer[4] > 0x00))); - break; + do { + delay(500); + (void)writeData(NCIPresCheckT3T, sizeof(NCIPresCheckT3T)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x08) && ((rxBuffer[3] == 0x00) || (rxBuffer[4] > 0x00))); + break; case PROT_ISODEP: - do - { - delay(500); - (void)writeData(NCIPresCheckIsoDep, sizeof(NCIPresCheckIsoDep)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x6F) && (rxBuffer[1] == 0x11) && (rxBuffer[2] == 0x01) && (rxBuffer[3] == 0x01)); - break; + do { + delay(500); + (void)writeData(NCIPresCheckIsoDep, sizeof(NCIPresCheckIsoDep)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x6F) && (rxBuffer[1] == 0x11) && (rxBuffer[2] == 0x01) && (rxBuffer[3] == 0x01)); + break; case PROT_ISO15693: - do - { - delay(500); - for (i = 0; i < 8; i++) - NCIPresCheckIso15693[i + 6] = RfIntf.Info.NFC_VPP.ID[7 - i]; - (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); - getMessage(); - getMessage(100); - status = ERROR; - if (rxMessageLength) - status = SUCCESS; - } while ((status == SUCCESS) && (rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[rxMessageLength - 1] == 0x00)); - break; + do { + delay(500); + for (i = 0; i < 8; i++) + NCIPresCheckIso15693[i + 6] = RfIntf.Info.NFC_VPP.ID[7 - i]; + (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); + getMessage(); + getMessage(100); + status = ERROR; + if (rxMessageLength) + status = SUCCESS; + } while ((status == SUCCESS) && (rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[rxMessageLength - 1] == 0x00)); + break; case PROT_MIFARE: - do - { - delay(500); - /* Deactivate target */ - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); + do { + delay(500); + /* Deactivate target */ + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - /* Reactivate target */ - (void)writeData(NCISelectMIFARE, sizeof(NCISelectMIFARE)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)); - break; + /* Reactivate target */ + (void)writeData(NCISelectMIFARE, sizeof(NCISelectMIFARE)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)); + break; default: - /* Nothing to do */ - break; - } + /* Nothing to do */ + break; + } } -bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) -{ - uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; +bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { + uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; - /* First de-activate the target */ - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); + /* First de-activate the target */ + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - /* Then re-activate the target */ - NCIActivate[4] = pRfIntf->Protocol; - NCIActivate[5] = pRfIntf->Interface; + /* Then re-activate the target */ + NCIActivate[4] = pRfIntf->Protocol; + NCIActivate[5] = pRfIntf->Interface; - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) - return ERROR; - return SUCCESS; + if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) + return ERROR; + return SUCCESS; } -bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) -{ - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; +bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; - bool status = ERROR; + bool status = ERROR; - pRfIntf->MoreTags = false; + pRfIntf->MoreTags = false; - if (gNextTag_Protocol == PROT_UNDETERMINED) - { - pRfIntf->Interface = INTF_UNDETERMINED; - pRfIntf->Protocol = PROT_UNDETERMINED; - return ERROR; - } + if (gNextTag_Protocol == PROT_UNDETERMINED) { + pRfIntf->Interface = INTF_UNDETERMINED; + pRfIntf->Protocol = PROT_UNDETERMINED; + return ERROR; + } - /* First disconnect current tag */ - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); + /* First disconnect current tag */ + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); - if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) - return ERROR; - getMessage(100); + if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) + return ERROR; + getMessage(100); - if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) - return ERROR; + if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) + return ERROR; - NCIRfDiscoverSelect[4] = gNextTag_Protocol; - if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (gNextTag_Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; - else - NCIRfDiscoverSelect[5] = INTF_FRAME; + NCIRfDiscoverSelect[4] = gNextTag_Protocol; + if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_ISODEP; + else if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_NFCDEP; + else if (gNextTag_Protocol == PROT_MIFARE) + NCIRfDiscoverSelect[5] = INTF_TAGCMD; + else + NCIRfDiscoverSelect[5] = INTF_FRAME; - (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); - getMessage(); + (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); + getMessage(); - if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) - { - getMessage(100); - if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) - { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - status = SUCCESS; - } + if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { + getMessage(100); + if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + status = SUCCESS; } + } - return status; + return status; } -bool Electroniccats_PN7150::StopDiscovery(void) -{ - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; +bool Electroniccats_PN7150::StopDiscovery(void) { + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - getMessage(1000); + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); + getMessage(1000); - return SUCCESS; + return SUCCESS; } -bool Electroniccats_PN7150::ConfigureSettings(void) -{ - +bool Electroniccats_PN7150::ConfigureSettings(void) { #if NXP_CORE_CONF - /* NCI standard dedicated settings - * Refer to NFC Forum NCI standard for more details - */ - uint8_t NxpNci_CORE_CONF[] = { - 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ - 0x00, 0x02, 0x00, 0x01 /* TOTAL_DURATION */ - }; + /* NCI standard dedicated settings + * Refer to NFC Forum NCI standard for more details + */ + uint8_t NxpNci_CORE_CONF[] = { + 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ + 0x00, 0x02, 0x00, 0x01 /* TOTAL_DURATION */ + }; #endif #if NXP_CORE_CONF_EXTN - /* NXP-NCI extension dedicated setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_CONF_EXTN[] = { - 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ - 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ - 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ - }; + /* NXP-NCI extension dedicated setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_CONF_EXTN[] = { + 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ + 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ + 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ + }; #endif #if NXP_CORE_STANDBY - /* NXP-NCI standby enable setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ + /* NXP-NCI standby enable setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ #endif #if NXP_TVDD_CONF - /* NXP-NCI TVDD configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; + /* NXP-NCI TVDD configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ #if (NXP_TVDD_CONF == 1) - /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; + /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; #else - /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; + /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; #endif #endif #if NXP_RF_CONF - /* NXP-NCI RF configuration - * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - /* Following configuration is the default settings of PN7120 NFC Controller */ - uint8_t NxpNci_RF_CONF_1stGen[] = { - 0x20, 0x02, 0x38, 0x07, - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ - }; - - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ - /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ - uint8_t NxpNci_RF_CONF_2ndGen[] = { - 0x20, 0x02, 0x94, 0x11, - 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ - 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ - }; + /* NXP-NCI RF configuration + * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + /* Following configuration is the default settings of PN7120 NFC Controller */ + uint8_t NxpNci_RF_CONF_1stGen[] = { + 0x20, 0x02, 0x38, 0x07, + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ + }; + + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ + /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ + uint8_t NxpNci_RF_CONF_2ndGen[] = { + 0x20, 0x02, 0x94, 0x11, + 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ + 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ + }; #endif #if NXP_CLK_CONF - /* NXP-NCI CLOCK configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ + /* NXP-NCI CLOCK configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ #if (NXP_CLK_CONF == 1) - /* Xtal configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ - }; + /* Xtal configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ + }; #else - /* PLL configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ - 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ - }; + /* PLL configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ + 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ + }; #endif #endif - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - bool gRfSettingsRestored_flag = false; + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + bool gRfSettingsRestored_flag = false; #if (NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t *NxpNci_CONF; - uint16_t NxpNci_CONF_size = 0; + uint8_t *NxpNci_CONF; + uint16_t NxpNci_CONF_size = 0; #endif #if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t currentTS[32] = __TIMESTAMP__; - uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; - uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; + uint8_t currentTS[32] = __TIMESTAMP__; + uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; + uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; #endif - bool isResetRequired = false; + bool isResetRequired = false; - /* Apply settings */ + /* Apply settings */ #if NXP_CORE_CONF - if (sizeof(NxpNci_CORE_CONF) != 0) - { - isResetRequired = true; - (void)writeData(NxpNci_CORE_CONF, sizeof(NxpNci_CORE_CONF)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (sizeof(NxpNci_CORE_CONF) != 0) { + isResetRequired = true; + (void)writeData(NxpNci_CORE_CONF, sizeof(NxpNci_CORE_CONF)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF"); + Serial.println("NxpNci_CORE_CONF"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_CORE_STANDBY - if (sizeof(NxpNci_CORE_STANDBY) != 0) - { - - (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); - getMessage(); - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) - { + if (sizeof(NxpNci_CORE_STANDBY) != 0) { + (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); + getMessage(); + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_STANDBY"); + Serial.println("NxpNci_CORE_STANDBY"); #endif - return ERROR; - } + return ERROR; } + } #endif - /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) - or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ + /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) + or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ #if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - /* First read timestamp stored in NFC Controller */ - if (gNfcController_generation == 1) - NCIReadTS[5] = 0x0F; - (void)writeData(NCIReadTS, sizeof(NCIReadTS)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) - { + /* First read timestamp stored in NFC Controller */ + if (gNfcController_generation == 1) + NCIReadTS[5] = 0x0F; + (void)writeData(NCIReadTS, sizeof(NCIReadTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("read timestamp "); + Serial.println("read timestamp "); #endif - return ERROR; - } - /* Then compare with current build timestamp, and check RF setting restauration flag */ - /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) - { - // No change, nothing to do - } - else - { - */ - /* Apply settings */ + return ERROR; + } + /* Then compare with current build timestamp, and check RF setting restauration flag */ + /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) + { + // No change, nothing to do + } + else + { + */ + /* Apply settings */ #if NXP_CORE_CONF_EXTN - if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) - { - (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) { + (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF_EXTN"); + Serial.println("NxpNci_CORE_CONF_EXTN"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_CLK_CONF - if (sizeof(NxpNci_CLK_CONF) != 0) - { - isResetRequired = true; + if (sizeof(NxpNci_CLK_CONF) != 0) { + isResetRequired = true; - (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); - getMessage(); - //NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); + getMessage(); + // NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CLK_CONF"); + Serial.println("NxpNci_CLK_CONF"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_TVDD_CONF - if (NxpNci_CONF_size != 0) - { - - (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); + Serial.println("NxpNci_CONF_size"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_RF_CONF - if (NxpNci_CONF_size != 0) - { - - (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); + Serial.println("NxpNci_CONF_size"); #endif - return ERROR; - } + return ERROR; } + } #endif - /* Store curent timestamp to NFC Controller memory for further checks */ - if (gNfcController_generation == 1) - NCIWriteTS[5] = 0x0F; - memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); - (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + /* Store curent timestamp to NFC Controller memory for further checks */ + if (gNfcController_generation == 1) + NCIWriteTS[5] = 0x0F; + memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); + (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NFC Controller memory"); + Serial.println("NFC Controller memory"); #endif - return ERROR; - } - //} + return ERROR; + } + //} #endif - if (isResetRequired) - { - /* Reset the NFC Controller to insure new settings apply */ - (void)writeData(NCICoreReset, sizeof(NCICoreReset)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) - { + if (isResetRequired) { + /* Reset the NFC Controller to insure new settings apply */ + (void)writeData(NCICoreReset, sizeof(NCICoreReset)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("insure new settings apply"); + Serial.println("insure new settings apply"); #endif - return ERROR; - } + return ERROR; + } - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - { + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("insure new settings apply 2"); + Serial.println("insure new settings apply 2"); #endif - return ERROR; - } + return ERROR; } - return SUCCESS; + } + return SUCCESS; } -bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) -{ - +bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) { #if NXP_CORE_CONF - /* NCI standard dedicated settings - * Refer to NFC Forum NCI standard for more details - */ - uint8_t NxpNci_CORE_CONF[20] = { - 0x20, 0x02, 0x05, 0x01, // CORE_SET_CONFIG_CMD - 0x00, 0x02, 0x00, 0x01 // TOTAL_DURATION - }; - - if(uidlen == 0) - uidlen = 8; - else { - uidlen+= 10; - memcpy(&NxpNci_CORE_CONF[0], uidcf, uidlen); - } + /* NCI standard dedicated settings + * Refer to NFC Forum NCI standard for more details + */ + uint8_t NxpNci_CORE_CONF[20] = { + 0x20, 0x02, 0x05, 0x01, // CORE_SET_CONFIG_CMD + 0x00, 0x02, 0x00, 0x01 // TOTAL_DURATION + }; + + if (uidlen == 0) + uidlen = 8; + else { + uidlen += 10; + memcpy(&NxpNci_CORE_CONF[0], uidcf, uidlen); + } #endif #if NXP_CORE_CONF_EXTN - /* NXP-NCI extension dedicated setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_CONF_EXTN[] = { - 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ - 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ - 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ - }; + /* NXP-NCI extension dedicated setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_CONF_EXTN[] = { + 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ + 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ + 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ + }; #endif #if NXP_CORE_STANDBY - /* NXP-NCI standby enable setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ + /* NXP-NCI standby enable setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ #endif #if NXP_TVDD_CONF - /* NXP-NCI TVDD configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; + /* NXP-NCI TVDD configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ #if (NXP_TVDD_CONF == 1) - /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; + /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; #else - /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; + /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; #endif #endif #if NXP_RF_CONF - /* NXP-NCI RF configuration - * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - /* Following configuration is the default settings of PN7120 NFC Controller */ - uint8_t NxpNci_RF_CONF_1stGen[] = { - 0x20, 0x02, 0x38, 0x07, - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ - }; - - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ - /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ - uint8_t NxpNci_RF_CONF_2ndGen[] = { - 0x20, 0x02, 0x94, 0x11, - 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ - 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ - }; + /* NXP-NCI RF configuration + * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + /* Following configuration is the default settings of PN7120 NFC Controller */ + uint8_t NxpNci_RF_CONF_1stGen[] = { + 0x20, 0x02, 0x38, 0x07, + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ + }; + + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ + /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ + uint8_t NxpNci_RF_CONF_2ndGen[] = { + 0x20, 0x02, 0x94, 0x11, + 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ + 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ + }; #endif #if NXP_CLK_CONF - /* NXP-NCI CLOCK configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ + /* NXP-NCI CLOCK configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ #if (NXP_CLK_CONF == 1) - /* Xtal configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ - }; + /* Xtal configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ + }; #else - /* PLL configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ - 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ - }; + /* PLL configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ + 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ + }; #endif #endif - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - bool gRfSettingsRestored_flag = false; + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + bool gRfSettingsRestored_flag = false; #if (NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t *NxpNci_CONF; - uint16_t NxpNci_CONF_size = 0; + uint8_t *NxpNci_CONF; + uint16_t NxpNci_CONF_size = 0; #endif #if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t currentTS[32] = __TIMESTAMP__; - uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; - uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; + uint8_t currentTS[32] = __TIMESTAMP__; + uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; + uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; #endif - bool isResetRequired = false; + bool isResetRequired = false; - /* Apply settings */ + /* Apply settings */ #if NXP_CORE_CONF - if (uidlen != 0) //sizeof(NxpNci_CORE_CONF) != 0) - { - isResetRequired = true; - (void)writeData(NxpNci_CORE_CONF, uidlen); //sizeof(NxpNci_CORE_CONF)); - getMessage(100); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (uidlen != 0) // sizeof(NxpNci_CORE_CONF) != 0) + { + isResetRequired = true; + (void)writeData(NxpNci_CORE_CONF, uidlen); // sizeof(NxpNci_CORE_CONF)); + getMessage(100); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF"); + Serial.println("NxpNci_CORE_CONF"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_CORE_STANDBY - if (sizeof(NxpNci_CORE_STANDBY) != 0) - { - - (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); - getMessage(); - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) - { + if (sizeof(NxpNci_CORE_STANDBY) != 0) { + (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); + getMessage(); + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_STANDBY"); + Serial.println("NxpNci_CORE_STANDBY"); #endif - return ERROR; - } + return ERROR; } + } #endif - /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) - or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ + /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) + or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ #if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - /* First read timestamp stored in NFC Controller */ - if (gNfcController_generation == 1) - NCIReadTS[5] = 0x0F; - (void)writeData(NCIReadTS, sizeof(NCIReadTS)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) - { + /* First read timestamp stored in NFC Controller */ + if (gNfcController_generation == 1) + NCIReadTS[5] = 0x0F; + (void)writeData(NCIReadTS, sizeof(NCIReadTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("read timestamp "); + Serial.println("read timestamp "); #endif - return ERROR; - } - /* Then compare with current build timestamp, and check RF setting restauration flag */ - /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) - { - // No change, nothing to do - } - else - { - */ - /* Apply settings */ + return ERROR; + } + /* Then compare with current build timestamp, and check RF setting restauration flag */ + /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) + { + // No change, nothing to do + } + else + { + */ + /* Apply settings */ #if NXP_CORE_CONF_EXTN - if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) - { - (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) { + (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF_EXTN"); + Serial.println("NxpNci_CORE_CONF_EXTN"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_CLK_CONF - if (sizeof(NxpNci_CLK_CONF) != 0) - { - isResetRequired = true; + if (sizeof(NxpNci_CLK_CONF) != 0) { + isResetRequired = true; - (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); - getMessage(); - //NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); + getMessage(); + // NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CLK_CONF"); + Serial.println("NxpNci_CLK_CONF"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_TVDD_CONF - if (NxpNci_CONF_size != 0) - { - - (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); + Serial.println("NxpNci_CONF_size"); #endif - return ERROR; - } + return ERROR; } + } #endif #if NXP_RF_CONF - if (NxpNci_CONF_size != 0) - { - - (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); + Serial.println("NxpNci_CONF_size"); #endif - return ERROR; - } + return ERROR; } + } #endif - /* Store curent timestamp to NFC Controller memory for further checks */ - if (gNfcController_generation == 1) - NCIWriteTS[5] = 0x0F; - memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); - (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) - { + /* Store curent timestamp to NFC Controller memory for further checks */ + if (gNfcController_generation == 1) + NCIWriteTS[5] = 0x0F; + memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); + (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NFC Controller memory"); + Serial.println("NFC Controller memory"); #endif - return ERROR; - } - //} + return ERROR; + } + //} #endif - if (isResetRequired) - { - /* Reset the NFC Controller to insure new settings apply */ - (void)writeData(NCICoreReset, sizeof(NCICoreReset)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) - { + if (isResetRequired) { + /* Reset the NFC Controller to insure new settings apply */ + (void)writeData(NCICoreReset, sizeof(NCICoreReset)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("insure new settings apply"); + Serial.println("insure new settings apply"); #endif - return ERROR; - } + return ERROR; + } - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - { + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) { #ifdef SerialUSB - Serial.println("insure new settings apply 2"); + Serial.println("insure new settings apply 2"); #endif - return ERROR; - } + return ERROR; } - return SUCCESS; + } + return SUCCESS; } -//#if defined P2P_SUPPORT || defined RW_SUPPORT -void Electroniccats_PN7150::NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) -{ - unsigned char *pNdefRecord = pNdefMessage; - NdefRecord_t NdefRecord; - unsigned char save; - - if (pNdefMessage == NULL) - { - Serial.println("--- Provisioned buffer size too small or NDEF message empty"); - return; - } - - while (pNdefRecord != NULL) - { - Serial.println("--- NDEF record received:"); - - NdefRecord = DetectNdefRecordType(pNdefRecord); - - switch (NdefRecord.recordType) - { - case MEDIA_VCARD: - { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("vCard:"); - //Serial.println(NdefRecord.recordPayload); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; +// #if defined P2P_SUPPORT || defined RW_SUPPORT +void Electroniccats_PN7150::NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { + unsigned char *pNdefRecord = pNdefMessage; + NdefRecord_t NdefRecord; + unsigned char save; + + if (pNdefMessage == NULL) { + Serial.println("--- Provisioned buffer size too small or NDEF message empty"); + return; + } + + while (pNdefRecord != NULL) { + Serial.println("--- NDEF record received:"); + + NdefRecord = DetectNdefRecordType(pNdefRecord); + + switch (NdefRecord.recordType) { + case MEDIA_VCARD: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("vCard:"); + // Serial.println(NdefRecord.recordPayload); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_TEXT: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("Text record:"); + // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_URI: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("URI record: "); + // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case MEDIA_HANDOVER_WIFI: { + unsigned char index = 0, i; + + Serial.println("--- Received WIFI credentials:"); + if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) + index += 4; + while (index < NdefRecord.recordPayloadSize) { + if (NdefRecord.recordPayload[index] == 0x10) { + if (NdefRecord.recordPayload[index + 1] == 0x45) { + Serial.print("- SSID = "); + for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) + Serial.print(NdefRecord.recordPayload[index + 4 + i]); + Serial.println(""); + } else if (NdefRecord.recordPayload[index + 1] == 0x03) { + Serial.print("- Authenticate Type = "); + Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); + } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { + Serial.print("- Encryption Type = "); + Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); + } else if (NdefRecord.recordPayload[index + 1] == 0x27) { + Serial.print("- Network key = "); + for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) + Serial.print("#"); + Serial.println(""); + } + index += 4 + NdefRecord.recordPayload[index + 3]; + } else + continue; } - break; + } break; - case WELL_KNOWN_SIMPLE_TEXT: - { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("Text record:"); - //Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } + case WELL_KNOWN_HANDOVER_SELECT: + Serial.print("Handover select version "); + Serial.print(NdefRecord.recordPayload[0] >> 4); + Serial.println(NdefRecord.recordPayload[0] & 0xF); break; - case WELL_KNOWN_SIMPLE_URI: - { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("URI record: "); - //Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } + case WELL_KNOWN_HANDOVER_REQUEST: + Serial.print("Handover request version "); + Serial.print(NdefRecord.recordPayload[0] >> 4); + Serial.println(NdefRecord.recordPayload[0] & 0xF); break; - case MEDIA_HANDOVER_WIFI: - { - unsigned char index = 0, i; - - Serial.println("--- Received WIFI credentials:"); - if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) - index += 4; - while (index < NdefRecord.recordPayloadSize) - { - if (NdefRecord.recordPayload[index] == 0x10) - { - if (NdefRecord.recordPayload[index + 1] == 0x45) - { - Serial.print("- SSID = "); - for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) - Serial.print(NdefRecord.recordPayload[index + 4 + i]); - Serial.println(""); - } - else if (NdefRecord.recordPayload[index + 1] == 0x03) - { - Serial.print("- Authenticate Type = "); - Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); - } - else if (NdefRecord.recordPayload[index + 1] == 0x0f) - { - Serial.print("- Encryption Type = "); - Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); - } - else if (NdefRecord.recordPayload[index + 1] == 0x27) - { - Serial.print("- Network key = "); - for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) - Serial.print("#"); - Serial.println(""); - } - index += 4 + NdefRecord.recordPayload[index + 3]; - } - else - continue; - } - } + case MEDIA_HANDOVER_BT: + Serial.print("BT Handover payload = "); + // Serial.print(NdefRecord.recordPayload); + // Serial.println(NdefRecord.recordPayloadSize); break; - case WELL_KNOWN_HANDOVER_SELECT: - Serial.print("Handover select version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); - break; - - case WELL_KNOWN_HANDOVER_REQUEST: - Serial.print("Handover request version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); - break; - - case MEDIA_HANDOVER_BT: - Serial.print("BT Handover payload = "); - //Serial.print(NdefRecord.recordPayload); - //Serial.println(NdefRecord.recordPayloadSize); - break; - - case MEDIA_HANDOVER_BLE: - Serial.print("BLE Handover payload = "); - //Serial.print(NdefRecord.recordPayload); - //Serial.println(NdefRecord.recordPayloadSize); - break; + case MEDIA_HANDOVER_BLE: + Serial.print("BLE Handover payload = "); + // Serial.print(NdefRecord.recordPayload); + // Serial.println(NdefRecord.recordPayloadSize); + break; - case MEDIA_HANDOVER_BLE_SECURE: - Serial.print(" BLE secure Handover payload = "); - //Serial.println(NdefRecord.recordPayload, NdefRecord.recordPayloadSize); - break; + case MEDIA_HANDOVER_BLE_SECURE: + Serial.print(" BLE secure Handover payload = "); + // Serial.println(NdefRecord.recordPayload, NdefRecord.recordPayloadSize); + break; - default: - Serial.println("Unsupported NDEF record, cannot parse"); - break; - } - pNdefRecord = GetNextRecord(pNdefRecord); + default: + Serial.println("Unsupported NDEF record, cannot parse"); + break; } + pNdefRecord = GetNextRecord(pNdefRecord); + } - Serial.println(""); + Serial.println(""); } -//#endif // if defined P2P_SUPPORT || defined RW_SUPPORT - -//#if defined P2P_SUPPORT || defined CARDEMU_SUPPORT -const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF - 0x01, // TYPE LENGTH - 0x07, // PAYLOAD LENTGH - 'T', // TYPE - 0x02, // Status - 'e', 'n', // Language +// #endif // if defined P2P_SUPPORT || defined RW_SUPPORT + +// #if defined P2P_SUPPORT || defined CARDEMU_SUPPORT +const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF + 0x01, // TYPE LENGTH + 0x07, // PAYLOAD LENTGH + 'T', // TYPE + 0x02, // Status + 'e', 'n', // Language 'T', 'e', 's', 't'}; -void Electroniccats_PN7150::NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) -{ - Serial.println("--- NDEF Record sent"); +void Electroniccats_PN7150::NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { + Serial.println("--- NDEF Record sent"); } -//#endif // if defined P2P_SUPPORT || defined CARDEMU_SUPPORT - -bool Electroniccats_PN7150::NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) -{ - uint8_t NCIPrbs_1stGen[] = {0x2F, 0x30, 0x04, 0x00, 0x00, 0x01, 0x01}; - uint8_t NCIPrbs_2ndGen[] = {0x2F, 0x30, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; - uint8_t *NxpNci_cmd; - uint16_t NxpNci_cmd_size = 0; - - if (gNfcController_generation == 1) - { - NxpNci_cmd = NCIPrbs_1stGen; - NxpNci_cmd_size = sizeof(NCIPrbs_1stGen); - NxpNci_cmd[3] = type; - NxpNci_cmd[4] = bitrate; - } - else if (gNfcController_generation == 2) - { - NxpNci_cmd = NCIPrbs_2ndGen; - NxpNci_cmd_size = sizeof(NCIPrbs_2ndGen); - NxpNci_cmd[5] = type; - NxpNci_cmd[6] = bitrate; - } - - if (NxpNci_cmd_size != 0) - { - (void)writeData(NxpNci_cmd, sizeof(NxpNci_cmd)); - getMessage(); - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x30) || (rxBuffer[3] != 0x00)) - return ERROR; - } - else - { - return ERROR; - } +// #endif // if defined P2P_SUPPORT || defined CARDEMU_SUPPORT + +bool Electroniccats_PN7150::NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) { + uint8_t NCIPrbs_1stGen[] = {0x2F, 0x30, 0x04, 0x00, 0x00, 0x01, 0x01}; + uint8_t NCIPrbs_2ndGen[] = {0x2F, 0x30, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; + uint8_t *NxpNci_cmd; + uint16_t NxpNci_cmd_size = 0; + + if (gNfcController_generation == 1) { + NxpNci_cmd = NCIPrbs_1stGen; + NxpNci_cmd_size = sizeof(NCIPrbs_1stGen); + NxpNci_cmd[3] = type; + NxpNci_cmd[4] = bitrate; + } else if (gNfcController_generation == 2) { + NxpNci_cmd = NCIPrbs_2ndGen; + NxpNci_cmd_size = sizeof(NCIPrbs_2ndGen); + NxpNci_cmd[5] = type; + NxpNci_cmd[6] = bitrate; + } + + if (NxpNci_cmd_size != 0) { + (void)writeData(NxpNci_cmd, sizeof(NxpNci_cmd)); + getMessage(); + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x30) || (rxBuffer[3] != 0x00)) + return ERROR; + } else { + return ERROR; + } - return SUCCESS; + return SUCCESS; } -bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn(void) -{ - uint8_t NCIRfOn[] = {0x2F, 0x3D, 0x02, 0x20, 0x01}; +bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn(void) { + uint8_t NCIRfOn[] = {0x2F, 0x3D, 0x02, 0x20, 0x01}; - (void)writeData(NCIRfOn, sizeof(NCIRfOn)); - getMessage(); - //NxpNci_HostTransceive(NCIRfOn, sizeof(NCIRfOn), Answer, sizeof(Answer), &AnswerSize); - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x3D) || (rxBuffer[3] != 0x00)) - return ERROR; + (void)writeData(NCIRfOn, sizeof(NCIRfOn)); + getMessage(); + // NxpNci_HostTransceive(NCIRfOn, sizeof(NCIRfOn), Answer, sizeof(Answer), &AnswerSize); + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x3D) || (rxBuffer[3] != 0x00)) + return ERROR; - return SUCCESS; + return SUCCESS; } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 01e904c..d4875b7 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -18,18 +18,18 @@ * */ -#include // Gives us access to all typical Arduino types and functions - // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library -//#include "RW_NDEF.h" +#include // Gives us access to all typical Arduino types and functions + // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library +// #include "RW_NDEF.h" #include "P2P_NDEF.h" -#include "T4T_NDEF_emu.h" #include "RW_NDEF.h" +#include "T4T_NDEF_emu.h" #include "ndef_helper.h" // #define DEBUG3 -#if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards -#include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 +#if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards +#include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 #else #include #endif @@ -41,8 +41,8 @@ #define NXP_CORE_CONF 1 #define NXP_CORE_STANDBY 1 #define NXP_CORE_CONF_EXTN 1 -#define NXP_CLK_CONF 1 // 1=Xtal, 2=PLL -#define NXP_TVDD_CONF 2 // 1=CFG1, 2=CFG2 +#define NXP_CLK_CONF 1 // 1=Xtal, 2=PLL +#define NXP_TVDD_CONF 2 // 1=CFG1, 2=CFG2 #define NXP_RF_CONF 1 #define NFC_FACTORY_TEST 1 @@ -99,7 +99,7 @@ #define INTF_NFCDEP 0x3 #define INTF_TAGCMD 0x80 -#define MaxPayloadSize 255 // See NCI specification V1.0, section 3.1 +#define MaxPayloadSize 255 // See NCI specification V1.0, section 3.1 #define MsgHeaderSize 3 /***** Factory Test dedicated APIs *********************************************/ @@ -108,22 +108,20 @@ /* * Definition of technology types */ -typedef enum -{ - NFC_A, - NFC_B, - NFC_F +typedef enum { + NFC_A, + NFC_B, + NFC_F } NxpNci_TechType_t; /* * Definition of bitrate */ -typedef enum -{ - BR_106, - BR_212, - BR_424, - BR_848 +typedef enum { + BR_106, + BR_212, + BR_424, + BR_848 } NxpNci_Bitrate_t; #endif /* @@ -131,72 +129,65 @@ typedef enum */ /* POLL passive type A */ -struct RfIntf_info_APP_t -{ - unsigned char SensRes[2]; - unsigned char NfcIdLen; - unsigned char NfcId[10]; - unsigned char SelResLen; - unsigned char SelRes[1]; - unsigned char RatsLen; - unsigned char Rats[20]; +struct RfIntf_info_APP_t { + unsigned char SensRes[2]; + unsigned char NfcIdLen; + unsigned char NfcId[10]; + unsigned char SelResLen; + unsigned char SelRes[1]; + unsigned char RatsLen; + unsigned char Rats[20]; }; /* POLL passive type B */ -struct RfIntf_info_BPP_t -{ - unsigned char SensResLen; - unsigned char SensRes[12]; - unsigned char AttribResLen; - unsigned char AttribRes[17]; +struct RfIntf_info_BPP_t { + unsigned char SensResLen; + unsigned char SensRes[12]; + unsigned char AttribResLen; + unsigned char AttribRes[17]; }; /* POLL passive type F */ -struct RfIntf_info_FPP_t -{ - unsigned char BitRate; - unsigned char SensResLen; - unsigned char SensRes[18]; +struct RfIntf_info_FPP_t { + unsigned char BitRate; + unsigned char SensResLen; + unsigned char SensRes[18]; }; /* POLL passive type ISO15693 */ -struct RfIntf_info_VPP_t -{ - unsigned char AFI; - unsigned char DSFID; - unsigned char ID[8]; +struct RfIntf_info_VPP_t { + unsigned char AFI; + unsigned char DSFID; + unsigned char ID[8]; }; -typedef union -{ - RfIntf_info_APP_t NFC_APP; - RfIntf_info_BPP_t NFC_BPP; - RfIntf_info_FPP_t NFC_FPP; - RfIntf_info_VPP_t NFC_VPP; +typedef union { + RfIntf_info_APP_t NFC_APP; + RfIntf_info_BPP_t NFC_BPP; + RfIntf_info_FPP_t NFC_FPP; + RfIntf_info_VPP_t NFC_VPP; } RfIntf_Info_t; /* * Definition of discovered remote device properties */ -struct RfIntf_t -{ - unsigned char Interface; - unsigned char Protocol; - unsigned char ModeTech; - bool MoreTags; - RfIntf_Info_t Info; +struct RfIntf_t { + unsigned char Interface; + unsigned char Protocol; + unsigned char ModeTech; + bool MoreTags; + RfIntf_Info_t Info; }; /* * Definition of operations handled when processing Reader mode */ -typedef enum -{ +typedef enum { #ifndef NO_NDEF_SUPPORT - READ_NDEF, - WRITE_NDEF, + READ_NDEF, + WRITE_NDEF, #endif - PRESENCE_CHECK + PRESENCE_CHECK } RW_Operation_t; /* @@ -205,95 +196,94 @@ typedef enum /* POLL passive type A */ typedef struct { - unsigned char SensRes[2]; - unsigned char NfcIdLen; - unsigned char NfcId[10]; - unsigned char SelResLen; - unsigned char SelRes[1]; - unsigned char RatsLen; - unsigned char Rats[20]; + unsigned char SensRes[2]; + unsigned char NfcIdLen; + unsigned char NfcId[10]; + unsigned char SelResLen; + unsigned char SelRes[1]; + unsigned char RatsLen; + unsigned char Rats[20]; } NxpNci_RfIntf_info_APP_t; /* POLL passive type B */ typedef struct { - unsigned char SensResLen; - unsigned char SensRes[12]; - unsigned char AttribResLen; - unsigned char AttribRes[17]; + unsigned char SensResLen; + unsigned char SensRes[12]; + unsigned char AttribResLen; + unsigned char AttribRes[17]; } NxpNci_RfIntf_info_BPP_t; /* POLL passive type F */ typedef struct { - unsigned char BitRate; - unsigned char SensResLen; - unsigned char SensRes[18]; + unsigned char BitRate; + unsigned char SensResLen; + unsigned char SensRes[18]; } NxpNci_RfIntf_info_FPP_t; /* POLL passive type ISO15693 */ typedef struct { - unsigned char AFI; - unsigned char DSFID; - unsigned char ID[8]; + unsigned char AFI; + unsigned char DSFID; + unsigned char ID[8]; } NxpNci_RfIntf_info_VPP_t; -typedef union -{ - NxpNci_RfIntf_info_APP_t NFC_APP; - NxpNci_RfIntf_info_BPP_t NFC_BPP; - NxpNci_RfIntf_info_FPP_t NFC_FPP; - NxpNci_RfIntf_info_VPP_t NFC_VPP; +typedef union { + NxpNci_RfIntf_info_APP_t NFC_APP; + NxpNci_RfIntf_info_BPP_t NFC_BPP; + NxpNci_RfIntf_info_FPP_t NFC_FPP; + NxpNci_RfIntf_info_VPP_t NFC_VPP; } NxpNci_RfIntf_Info_t; -class Electroniccats_PN7150 -{ -private: - uint8_t _IRQpin, _VENpin, _I2Caddress; - TwoWire *_wire; - uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message - void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command - bool isTimeOut() const; - bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses - unsigned long timeOut; - unsigned long timeOutStartTime; - uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length - uint8_t gNfcController_generation = 0; - uint8_t gNfcController_fw_version[3] = {0}; +class Electroniccats_PN7150 { + private: + uint8_t _IRQpin, _VENpin, _I2Caddress; + TwoWire *_wire; + uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message + void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command + bool isTimeOut() const; + bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses + unsigned long timeOut; + unsigned long timeOutStartTime; + uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length + uint8_t gNfcController_generation = 0; + uint8_t gNfcController_fw_version[3] = {0}; -public: - Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); - int GetFwVersion(); - uint8_t begin(void); - uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) - uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read - bool hasMessage() const; - uint8_t ConfigMode(uint8_t modeSE); - uint8_t StartDiscovery(uint8_t modeSE); - uint8_t connectNCI(); - uint8_t wakeupNCI(); - bool CardModeSend(unsigned char *pData, unsigned char DataSize); - bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); - bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); - void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); - bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); - bool StopDiscovery(void); - void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); - void PresenceCheck(RfIntf_t RfIntf); - bool ReaderReActivate(RfIntf_t *pRfIntf); - void PrintBuf(const byte *data, const uint32_t numBytes); - bool ReaderActivateNext(RfIntf_t *pRfIntf); - bool ConfigureSettings(void); - bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); - void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); - void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); - bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); - bool NxpNci_FactoryTest_RfOn(void); - void ProcessP2pMode(RfIntf_t RfIntf); - void ReadNdef(RfIntf_t RfIntf); - void WriteNdef(RfIntf_t RfIntf); - void ProcessCardMode(RfIntf_t RfIntf); + public: + Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); + int getFirmwareVersion(); + int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead + uint8_t begin(void); + uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) + uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read + bool hasMessage() const; + uint8_t ConfigMode(uint8_t modeSE); + uint8_t StartDiscovery(uint8_t modeSE); + uint8_t connectNCI(); + uint8_t wakeupNCI(); + bool CardModeSend(unsigned char *pData, unsigned char DataSize); + bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); + bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); + void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); + bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); + bool StopDiscovery(void); + void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); + void PresenceCheck(RfIntf_t RfIntf); + bool ReaderReActivate(RfIntf_t *pRfIntf); + void PrintBuf(const byte *data, const uint32_t numBytes); + bool ReaderActivateNext(RfIntf_t *pRfIntf); + bool ConfigureSettings(void); + bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); + void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); + void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); + bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); + bool NxpNci_FactoryTest_RfOn(void); + void ProcessP2pMode(RfIntf_t RfIntf); + void ReadNdef(RfIntf_t RfIntf); + void WriteNdef(RfIntf_t RfIntf); + void ProcessCardMode(RfIntf_t RfIntf); }; #endif From 7a52e7cf1439ef6f188006620bef5734cb6be0e2 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 14:23:05 -0600 Subject: [PATCH 016/106] refactor: config mode --- examples/DetectTags/DetectTags.ino | 4 ++-- examples/DetectingReaders/DetectingReaders.ino | 2 +- examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino | 4 ++-- .../ISO14443-3A_write_block/ISO14443-3A_write_block.ino | 4 ++-- examples/ISO15693_read_block/ISO15693_read_block.ino | 4 ++-- examples/ISO15693_write_block/ISO15693_write_block.ino | 4 ++-- .../MifareClassic_read_block/MifareClassic_read_block.ino | 4 ++-- .../MifareClassic_write_block/MifareClassic_write_block.ino | 4 ++-- examples/NDEFReceive/NDEFReceive.ino | 4 ++-- examples/NDEFSend/NDEFSend.ino | 2 +- examples/P2P_Discovery/P2P_Discovery.ino | 4 ++-- src/Electroniccats_PN7150.cpp | 6 +++++- src/Electroniccats_PN7150.h | 3 ++- 13 files changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index e262d72..690a8ba 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -24,7 +24,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -139,7 +139,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index 4a4113d..b365514 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -39,7 +39,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 3135fe8..4f0345b 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -82,7 +82,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode failed!!"); while (1); } diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 259c89d..ddd12f1 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -98,7 +98,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode failed!!"); while (1); } diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index 4b1e680..51f0b4e 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -83,7 +83,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index a6765fc..7a834b4 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -41,7 +41,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } @@ -51,7 +51,7 @@ void setup(){ void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 23f7bc6..4f862bb 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -91,7 +91,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 69f5376..434d5d3 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -30,7 +30,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -128,7 +128,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 046a881..0d043ed 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -18,7 +18,7 @@ const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF void ResetMode() { // Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -138,7 +138,7 @@ void setup() { ; } - if (nfc.ConfigMode(mode)) { // Set up the configuration mode + if (nfc.configMode(mode)) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1) ; diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 698ed07..ea137fc 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -65,7 +65,7 @@ void setup() { ; } - if (nfc.ConfigMode(mode)) { + if (nfc.configMode(mode)) { Serial.println("The Configure Mode failed!!"); while (1) ; diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index df9fded..b8ea299 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -24,7 +24,7 @@ uint8_t mode = 3; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); + nfc.configMode(mode); nfc.StartDiscovery(mode); } @@ -45,7 +45,7 @@ void setup(){ while (1); } - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + if(nfc.configMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode failed!!"); while (1); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 98d0128..c4e22b3 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -225,7 +225,7 @@ int Electroniccats_PN7150::GetFwVersion() { return getFirmwareVersion(); } -uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { +uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU : MODE_P2P); @@ -325,6 +325,10 @@ uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { return SUCCESS; } +uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { + return Electroniccats_PN7150::configMode(modeSE); +} + uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) : sizeof(DiscoveryTechnologiesP2P)); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index d4875b7..08a230e 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -259,7 +259,8 @@ class Electroniccats_PN7150 { uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read bool hasMessage() const; - uint8_t ConfigMode(uint8_t modeSE); + uint8_t configMode(uint8_t modeSE); + uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead uint8_t StartDiscovery(uint8_t modeSE); uint8_t connectNCI(); uint8_t wakeupNCI(); From 309529589a78c353787f9210f170c57194a952b8 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 14:37:17 -0600 Subject: [PATCH 017/106] refactor: start discovery --- examples/DetectTags/DetectTags.ino | 6 +++--- examples/DetectingReaders/DetectingReaders.ino | 2 +- examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino | 6 +++--- .../ISO14443-3A_write_block/ISO14443-3A_write_block.ino | 6 +++--- examples/ISO15693_read_block/ISO15693_read_block.ino | 6 +++--- examples/ISO15693_write_block/ISO15693_write_block.ino | 6 +++--- .../MifareClassic_read_block/MifareClassic_read_block.ino | 6 +++--- .../MifareClassic_write_block/MifareClassic_write_block.ino | 6 +++--- examples/NDEFReceive/NDEFReceive.ino | 6 +++--- examples/NDEFSend/NDEFSend.ino | 2 +- examples/P2P_Discovery/P2P_Discovery.ino | 4 ++-- src/Electroniccats_PN7150.cpp | 6 +++++- src/Electroniccats_PN7150.h | 3 ++- 13 files changed, 35 insertions(+), 30 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 690a8ba..4aa68ed 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -25,7 +25,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -143,7 +143,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an Card ..."); } @@ -178,7 +178,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index b365514..bf64734 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -43,7 +43,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an Reader Card ..."); } diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 4f0345b..afcb03d 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -86,7 +86,7 @@ void setup(){ Serial.println("The Configure Mode failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } @@ -131,7 +131,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index ddd12f1..d4135b8 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -102,7 +102,7 @@ void setup(){ Serial.println("The Configure Mode failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } @@ -147,7 +147,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index 51f0b4e..cf47a3a 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -87,7 +87,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO15693 Card ..."); } @@ -128,7 +128,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 7a834b4..87a88c0 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -45,14 +45,14 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO15693 Card ..."); } void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -141,7 +141,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 4f862bb..64c73b4 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -95,7 +95,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an Mifare Classic Card ..."); } @@ -140,7 +140,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 434d5d3..a25808f 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -31,7 +31,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -132,7 +132,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an Mifare Classic Card ..."); } @@ -177,7 +177,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 0d043ed..1bacb99 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -19,7 +19,7 @@ const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF void ResetMode() { // Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } void PrintBuf(const byte *data, const uint32_t numBytes) { // Print hex data buffer in format @@ -143,7 +143,7 @@ void setup() { while (1) ; } - nfc.StartDiscovery(mode); // NCI Discovery mode + nfc.startDiscovery(mode); // NCI Discovery mode Serial.println("Waiting for an Card ..."); } @@ -179,7 +179,7 @@ void loop() { Serial.println("CARD REMOVED!"); nfc.StopDiscovery(); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index ea137fc..530f179 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -71,7 +71,7 @@ void setup() { ; } - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); Serial.print("Waiting for an NDEF device"); } diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index b8ea299..528ffd2 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -25,7 +25,7 @@ uint8_t mode = 3; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(mode); } @@ -49,7 +49,7 @@ void setup(){ Serial.println("The Configure Mode failed!!"); while (1); } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for a P2P device..."); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index c4e22b3..38db2b8 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -329,7 +329,7 @@ uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { return Electroniccats_PN7150::configMode(modeSE); } -uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { +uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) : sizeof(DiscoveryTechnologiesP2P)); @@ -355,6 +355,10 @@ uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { return SUCCESS; } +uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { + return Electroniccats_PN7150::startDiscovery(modeSE); +} + void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { uint8_t Answer[MAX_NCI_FRAME_SIZE]; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 08a230e..702bf92 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -261,7 +261,8 @@ class Electroniccats_PN7150 { bool hasMessage() const; uint8_t configMode(uint8_t modeSE); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead - uint8_t StartDiscovery(uint8_t modeSE); + uint8_t startDiscovery(uint8_t modeSE); + uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead uint8_t connectNCI(); uint8_t wakeupNCI(); bool CardModeSend(unsigned char *pData, unsigned char DataSize); From 7fdfb74a5eef5d925dcf481df0782f26363e152b Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 14:45:48 -0600 Subject: [PATCH 018/106] refactor: card mode send --- examples/DetectingReaders/DetectingReaders.ino | 2 +- examples/NDEFSend/NDEFSend.ino | 2 +- src/Electroniccats_PN7150.cpp | 6 +++++- src/Electroniccats_PN7150.h | 3 ++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index bf64734..7f7d889 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -64,7 +64,7 @@ void loop(){ default: break; } - nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); + nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); } } } diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 530f179..e88bfcc 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -87,7 +87,7 @@ void checkReaders() { Serial.println("\nReader detected!"); nfc.ProcessCardMode(RfInterface); } - nfc.CardModeSend(STATUSOK, sizeof(STATUSOK)); + nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); Serial.print("Waiting for an NDEF device"); } } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 38db2b8..6f93604 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -415,7 +415,7 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { } } -bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { +bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char DataSize) { bool status; uint8_t Cmd[MAX_NCI_FRAME_SIZE]; @@ -428,6 +428,10 @@ bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char Dat return status; } +bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { + return Electroniccats_PN7150::cardModeSend(pData, DataSize); +} + bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { #ifdef DEBUG2 Serial.println("[DEBUG] cardModeReceive exec"); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 702bf92..2d8812d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -265,7 +265,8 @@ class Electroniccats_PN7150 { uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead uint8_t connectNCI(); uint8_t wakeupNCI(); - bool CardModeSend(unsigned char *pData, unsigned char DataSize); + bool cardModeSend(unsigned char *pData, unsigned char DataSize); + bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); From 47b270aa965ea7e6a96957eaacd46bb2320046f7 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 15:46:43 -0600 Subject: [PATCH 019/106] refactor: card mode receive --- .../DetectingReaders/DetectingReaders.ino | 2 +- examples/NDEFSend/NDEFSend.ino | 2 +- src/Electroniccats_PN7150.cpp | 183 +++++++++--------- src/Electroniccats_PN7150.h | 9 +- 4 files changed, 103 insertions(+), 93 deletions(-) diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index 7f7d889..f4f076c 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -48,7 +48,7 @@ void setup(){ } void loop(){ - if(nfc.CardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? + if(nfc.cardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes switch (Cmd[1]) { case 0xA4: //Something tries to select a file, meaning that it is a reader diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index e88bfcc..a48d11d 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -81,7 +81,7 @@ void loop() { void checkReaders() { Serial.print("."); - if (nfc.CardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? + if (nfc.cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("\nReader detected!"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 6f93604..e1c2faf 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -69,6 +69,28 @@ uint8_t Electroniccats_PN7150::begin() { return SUCCESS; } +bool Electroniccats_PN7150::isTimeOut() const { + return ((millis() - timeOutStartTime) >= timeOut); +} + +void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) { + timeOutStartTime = millis(); + timeOut = theTimeOut; +} + +bool Electroniccats_PN7150::getMessage(uint16_t timeout) { // check for message using timeout, 5 milisec as default + setTimeOut(timeout); + rxMessageLength = 0; + while (!isTimeOut()) { + rxMessageLength = readData(rxBuffer); + if (rxMessageLength) + break; + else if (timeout == 1337) + setTimeOut(timeout); + } + return rxMessageLength; +} + bool Electroniccats_PN7150::hasMessage() const { return (HIGH == digitalRead(_IRQpin)); // PN7150 indicates it has data by driving IRQ signal HIGH } @@ -131,96 +153,11 @@ uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const { return bytesReceived; } -bool Electroniccats_PN7150::isTimeOut() const { - return ((millis() - timeOutStartTime) >= timeOut); -} - -void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) { - timeOutStartTime = millis(); - timeOut = theTimeOut; -} - -bool Electroniccats_PN7150::getMessage(uint16_t timeout) { // check for message using timeout, 5 milisec as default - setTimeOut(timeout); - rxMessageLength = 0; - while (!isTimeOut()) { - rxMessageLength = readData(rxBuffer); - if (rxMessageLength) - break; - else if (timeout == 1337) - setTimeOut(timeout); - } - return rxMessageLength; -} - -uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; - uint16_t NbBytes = 0; - - // Reset RF settings restauration flag - (void)writeData(NCICoreReset, 4); - getMessage(15); - NbBytes = rxMessageLength; - if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { - return ERROR; - } - getMessage(); - NbBytes = rxMessageLength; - if (NbBytes != 0) { - // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); - // Is CORE_GENERIC_ERROR_NTF ? - if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { - /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ - // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; - } else { - return ERROR; - } - } - return SUCCESS; -} - -uint8_t Electroniccats_PN7150::connectNCI() { - uint8_t i = 2; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - - // Open connection to NXPNCI - begin(); - // Loop until NXPNCI answers - while (wakeupNCI() != SUCCESS) { - if (i-- == 0) - return ERROR; - delay(500); - } - - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - return ERROR; - - // Retrieve NXP-NCI NFC Controller generation - if (rxBuffer[17 + rxBuffer[8]] == 0x08) - gNfcController_generation = 1; - else if (rxBuffer[17 + rxBuffer[8]] == 0x10) - gNfcController_generation = 2; - - // Retrieve NXP-NCI NFC Controller FW version - gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V - gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO - gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO -#ifdef DEBUG - Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); - Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); - Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); - Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); -#endif - - return SUCCESS; -} - int Electroniccats_PN7150::getFirmwareVersion() { return ((gNfcController_fw_version[0] & 0xFF) << 16) | ((gNfcController_fw_version[1] & 0xFF) << 8) | (gNfcController_fw_version[2] & 0xFF); } +// Deprecated, use getFirmwareVersion() instead int Electroniccats_PN7150::GetFwVersion() { return getFirmwareVersion(); } @@ -325,6 +262,7 @@ uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { return SUCCESS; } +// Deprecated, use configMode() instead uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { return Electroniccats_PN7150::configMode(modeSE); } @@ -355,10 +293,75 @@ uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { return SUCCESS; } +// Deprecated, use startDiscovery() instead uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { return Electroniccats_PN7150::startDiscovery(modeSE); } +uint8_t Electroniccats_PN7150::connectNCI() { + uint8_t i = 2; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + + // Open connection to NXPNCI + begin(); + // Loop until NXPNCI answers + while (wakeupNCI() != SUCCESS) { + if (i-- == 0) + return ERROR; + delay(500); + } + + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) + return ERROR; + + // Retrieve NXP-NCI NFC Controller generation + if (rxBuffer[17 + rxBuffer[8]] == 0x08) + gNfcController_generation = 1; + else if (rxBuffer[17 + rxBuffer[8]] == 0x10) + gNfcController_generation = 2; + + // Retrieve NXP-NCI NFC Controller FW version + gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V + gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO + gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO +#ifdef DEBUG + Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); + Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); + Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); + Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); +#endif + + return SUCCESS; +} + +uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; + uint16_t NbBytes = 0; + + // Reset RF settings restauration flag + (void)writeData(NCICoreReset, 4); + getMessage(15); + NbBytes = rxMessageLength; + if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { + return ERROR; + } + getMessage(); + NbBytes = rxMessageLength; + if (NbBytes != 0) { + // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); + // Is CORE_GENERIC_ERROR_NTF ? + if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { + /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ + // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; + } else { + return ERROR; + } + } + return SUCCESS; +} + void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { uint8_t Answer[MAX_NCI_FRAME_SIZE]; @@ -428,11 +431,12 @@ bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char Dat return status; } +// Deprecated, use cardModeSend() instead bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { return Electroniccats_PN7150::cardModeSend(pData, DataSize); } -bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { +bool Electroniccats_PN7150::cardModeReceive(unsigned char *pData, unsigned char *pDataSize) { #ifdef DEBUG2 Serial.println("[DEBUG] cardModeReceive exec"); #endif @@ -460,6 +464,11 @@ bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char return status; } +// Deprecated, use cardModeReceive() instead +bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { + return Electroniccats_PN7150::cardModeReceive(pData, pDataSize); +} + void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 2d8812d..3707ad1 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -253,12 +253,12 @@ class Electroniccats_PN7150 { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); - int getFirmwareVersion(); - int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t begin(void); + bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read - bool hasMessage() const; + int getFirmwareVersion(); + int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t configMode(uint8_t modeSE); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead uint8_t startDiscovery(uint8_t modeSE); @@ -267,7 +267,8 @@ class Electroniccats_PN7150 { uint8_t wakeupNCI(); bool cardModeSend(unsigned char *pData, unsigned char DataSize); bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead - bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); + bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); + bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); From 0586fa734a0523e9f534275bc21a8829fe55c66e Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 15:54:26 -0600 Subject: [PATCH 020/106] refactor: wait for discovery notification --- examples/DetectTags/DetectTags.ino | 4 +- .../ISO14443-3A_read_block.ino | 4 +- .../ISO14443-3A_write_block.ino | 4 +- .../ISO15693_read_block.ino | 4 +- .../ISO15693_write_block.ino | 4 +- .../MifareClassic_read_block.ino | 4 +- .../MifareClassic_write_block.ino | 4 +- examples/NDEFReceive/NDEFReceive.ino | 4 +- examples/P2P_Discovery/P2P_Discovery.ino | 2 +- src/Electroniccats_PN7150.cpp | 294 +++++++++--------- src/Electroniccats_PN7150.h | 7 +- 11 files changed, 173 insertions(+), 162 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 4aa68ed..3622247 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -148,7 +148,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards displayCardInfo(RfInterface); switch(RfInterface.Protocol) { case PROT_T1T: @@ -177,7 +177,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index afcb03d..fad42ab 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -91,7 +91,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_T2T: Serial.println(" - Found ISO14443-3A(T2T) card"); @@ -130,7 +130,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index d4135b8..36cbb94 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -107,7 +107,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_T2T: Serial.println(" - Found ISO14443-3A(T2T) card"); @@ -146,7 +146,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index cf47a3a..33908e0 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -92,7 +92,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_ISO15693: Serial.println(" - Found ISO15693 card"); @@ -127,7 +127,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 87a88c0..88edb16 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -105,7 +105,7 @@ void PCD_ISO15693_scenario (void){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_ISO15693: Serial.println(" - Found ISO15693 card"); @@ -140,7 +140,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 64c73b4..f1afd50 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -100,7 +100,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_MIFARE: Serial.println(" - Found MIFARE card"); @@ -139,7 +139,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index a25808f..80190aa 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -137,7 +137,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_MIFARE: Serial.println(" - Found MIFARE card"); @@ -176,7 +176,7 @@ void loop(){ nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 1bacb99..588283a 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -148,7 +148,7 @@ void setup() { } void loop() { - if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards + if (!nfc.waitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards displayCardInfo(RfInterface); displayDeviceInfo(); switch (RfInterface.Protocol) { @@ -178,7 +178,7 @@ void loop() { nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - nfc.StopDiscovery(); + nfc.stopDiscovery(); nfc.startDiscovery(mode); } ResetMode(); diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index 528ffd2..d8c5c3a 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -54,7 +54,7 @@ void setup(){ } void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect if (RfInterface.Interface == INTF_NFCDEP) { if ((RfInterface.ModeTech & MODE_LISTEN) == MODE_LISTEN) Serial.println(" - P2P TARGET MODE: Activated from remote Initiator"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index e1c2faf..40423ca 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -298,6 +298,158 @@ uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { return Electroniccats_PN7150::startDiscovery(modeSE); } +bool Electroniccats_PN7150::stopDiscovery() { + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; + + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); + getMessage(1000); + + return SUCCESS; +} + +// Deprecated, use stopDiscovery() instead +bool Electroniccats_PN7150::StopDiscovery() { + return Electroniccats_PN7150::stopDiscovery(); +} + +bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; + + // P2P Support + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; + uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; + uint8_t saved_NTF[7]; + + gNextTag_Protocol = PROT_UNDETERMINED; + bool getFlag = false; +wait: + do { + getFlag = getMessage( + tout > 0 ? tout : 1337); // Infinite loop, waiting for response + } while (((rxBuffer[0] != 0x61) || ((rxBuffer[1] != 0x05) && (rxBuffer[1] != 0x03))) && (getFlag == true)); + gNextTag_Protocol = PROT_UNDETERMINED; + + /* Is RF_INTF_ACTIVATED_NTF ? */ + if (rxBuffer[1] == 0x05) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + pRfIntf->MoreTags = false; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + + // P2P + /* Verifying if not a P2P device also presenting T4T emulation */ + if ((pRfIntf->Interface == INTF_ISODEP) && (pRfIntf->Protocol == PROT_ISODEP) && ((pRfIntf->ModeTech & MODE_LISTEN) != MODE_LISTEN)) { + memcpy(saved_NTF, rxBuffer, sizeof(saved_NTF)); + while (1) { + /* Restart the discovery loop */ + (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); + getMessage(); + getMessage(100); + /* Wait for discovery */ + do { + getMessage(1000); // Infinite loop, waiting for response + } while ((rxMessageLength == 4) && (rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)); + + if ((rxMessageLength != 0) && (rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)) { + /* Is same device detected ? */ + if (memcmp(saved_NTF, rxBuffer, sizeof(saved_NTF)) == 0) + break; + /* Is P2P detected ? */ + if (rxBuffer[5] == PROT_NFCDEP) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + pRfIntf->MoreTags = false; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + break; + } + } else { + if (rxMessageLength != 0) { + /* Flush any other notification */ + while (rxMessageLength != 0) + getMessage(100); + + /* Restart the discovery loop */ + (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); + getMessage(); + getMessage(100); + } + goto wait; + } + } + } + } else { /* RF_DISCOVER_NTF */ + pRfIntf->Interface = INTF_UNDETERMINED; + pRfIntf->Protocol = rxBuffer[4]; + pRfIntf->ModeTech = rxBuffer[5]; + pRfIntf->MoreTags = true; + + /* Get next NTF for further activation */ + do { + if (!getMessage(100)) + return ERROR; + } while ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x03)); + gNextTag_Protocol = rxBuffer[4]; + + /* Remaining NTF ? */ + + while (rxBuffer[rxMessageLength - 1] == 0x02) + getMessage(100); + + /* In case of multiple cards, select the first one */ + NCIRfDiscoverSelect[4] = pRfIntf->Protocol; + if (pRfIntf->Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_ISODEP; + else if (pRfIntf->Protocol == PROT_NFCDEP) + NCIRfDiscoverSelect[5] = INTF_NFCDEP; + else if (pRfIntf->Protocol == PROT_MIFARE) + NCIRfDiscoverSelect[5] = INTF_TAGCMD; + else + NCIRfDiscoverSelect[5] = INTF_FRAME; + + (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); + getMessage(100); + + if ((rxBuffer[0] == 0x41) || (rxBuffer[1] == 0x04) || (rxBuffer[3] == 0x00)) { + (void)writeData(rxBuffer, rxMessageLength); + getMessage(100); + + if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + } + + /* In case of P2P target detected but lost, inform application to restart discovery */ + else if (pRfIntf->Protocol == PROT_NFCDEP) { + /* Restart the discovery loop */ + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); + getMessage(100); + + (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); + getMessage(); + + goto wait; + } + } + } + + /* In case of unknown target align protocol information */ + if (pRfIntf->Interface == INTF_UNDETERMINED) + pRfIntf->Protocol = PROT_UNDETERMINED; + + return SUCCESS; +} + +// Deprecaded, use waitForDiscoveryNotification() instead +bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { + return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); +} + uint8_t Electroniccats_PN7150::connectNCI() { uint8_t i = 2; uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; @@ -553,138 +705,6 @@ bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char return status; } -bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; - - // P2P Support - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; - uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; - uint8_t saved_NTF[7]; - - gNextTag_Protocol = PROT_UNDETERMINED; - bool getFlag = false; -wait: - do { - getFlag = getMessage( - tout > 0 ? tout : 1337); // Infinite loop, waiting for response - } while (((rxBuffer[0] != 0x61) || ((rxBuffer[1] != 0x05) && (rxBuffer[1] != 0x03))) && (getFlag == true)); - gNextTag_Protocol = PROT_UNDETERMINED; - - /* Is RF_INTF_ACTIVATED_NTF ? */ - if (rxBuffer[1] == 0x05) { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - pRfIntf->MoreTags = false; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - - // P2P - /* Verifying if not a P2P device also presenting T4T emulation */ - if ((pRfIntf->Interface == INTF_ISODEP) && (pRfIntf->Protocol == PROT_ISODEP) && ((pRfIntf->ModeTech & MODE_LISTEN) != MODE_LISTEN)) { - memcpy(saved_NTF, rxBuffer, sizeof(saved_NTF)); - while (1) { - /* Restart the discovery loop */ - (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); - getMessage(); - getMessage(100); - /* Wait for discovery */ - do { - getMessage(1000); // Infinite loop, waiting for response - } while ((rxMessageLength == 4) && (rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)); - - if ((rxMessageLength != 0) && (rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)) { - /* Is same device detected ? */ - if (memcmp(saved_NTF, rxBuffer, sizeof(saved_NTF)) == 0) - break; - /* Is P2P detected ? */ - if (rxBuffer[5] == PROT_NFCDEP) { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - pRfIntf->MoreTags = false; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - break; - } - } else { - if (rxMessageLength != 0) { - /* Flush any other notification */ - while (rxMessageLength != 0) - getMessage(100); - - /* Restart the discovery loop */ - (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); - getMessage(); - getMessage(100); - } - goto wait; - } - } - } - } else { /* RF_DISCOVER_NTF */ - pRfIntf->Interface = INTF_UNDETERMINED; - pRfIntf->Protocol = rxBuffer[4]; - pRfIntf->ModeTech = rxBuffer[5]; - pRfIntf->MoreTags = true; - - /* Get next NTF for further activation */ - do { - if (!getMessage(100)) - return ERROR; - } while ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x03)); - gNextTag_Protocol = rxBuffer[4]; - - /* Remaining NTF ? */ - - while (rxBuffer[rxMessageLength - 1] == 0x02) - getMessage(100); - - /* In case of multiple cards, select the first one */ - NCIRfDiscoverSelect[4] = pRfIntf->Protocol; - if (pRfIntf->Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (pRfIntf->Protocol == PROT_NFCDEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (pRfIntf->Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; - else - NCIRfDiscoverSelect[5] = INTF_FRAME; - - (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); - getMessage(100); - - if ((rxBuffer[0] == 0x41) || (rxBuffer[1] == 0x04) || (rxBuffer[3] == 0x00)) { - (void)writeData(rxBuffer, rxMessageLength); - getMessage(100); - - if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); - } - - /* In case of P2P target detected but lost, inform application to restart discovery */ - else if (pRfIntf->Protocol == PROT_NFCDEP) { - /* Restart the discovery loop */ - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - getMessage(100); - - (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); - getMessage(); - - goto wait; - } - } - } - - /* In case of unknown target align protocol information */ - if (pRfIntf->Interface == INTF_UNDETERMINED) - pRfIntf->Protocol = PROT_UNDETERMINED; - - return SUCCESS; -} - void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) { uint8_t status = ERROR; bool restart = false; @@ -1010,16 +1030,6 @@ bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { return status; } -bool Electroniccats_PN7150::StopDiscovery(void) { - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; - - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - getMessage(1000); - - return SUCCESS; -} - bool Electroniccats_PN7150::ConfigureSettings(void) { #if NXP_CORE_CONF /* NCI standard dedicated settings diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 3707ad1..6d509e4 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -263,20 +263,21 @@ class Electroniccats_PN7150 { uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead uint8_t startDiscovery(uint8_t modeSE); uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead + bool stopDiscovery(); + bool StopDiscovery(); // Deprecated, use stopDiscovery() instead + bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); + bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); // Deprecated, use waitForDiscoveryNotification() instead uint8_t connectNCI(); uint8_t wakeupNCI(); bool cardModeSend(unsigned char *pData, unsigned char DataSize); bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead - bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); - bool StopDiscovery(void); void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); void PresenceCheck(RfIntf_t RfIntf); bool ReaderReActivate(RfIntf_t *pRfIntf); - void PrintBuf(const byte *data, const uint32_t numBytes); bool ReaderActivateNext(RfIntf_t *pRfIntf); bool ConfigureSettings(void); bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); From efaf25b1f9659ccf38bb2a4ab456b2192961f6ac Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 16:02:42 -0600 Subject: [PATCH 021/106] refactor: process card mode --- examples/NDEFSend/NDEFSend.ino | 2 +- src/Electroniccats_PN7150.cpp | 104 +++++++++++++++++---------------- src/Electroniccats_PN7150.h | 3 +- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index a48d11d..f94a18c 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -85,7 +85,7 @@ void checkReaders() { if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("\nReader detected!"); - nfc.ProcessCardMode(RfInterface); + nfc.processCardMode(RfInterface); } nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); Serial.print("Waiting for an NDEF device"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 40423ca..cd51a83 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -514,7 +514,58 @@ uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using return SUCCESS; } -void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { +bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char DataSize) { + bool status; + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + + /* Compute and send DATA_PACKET */ + Cmd[0] = 0x00; + Cmd[1] = 0x00; + Cmd[2] = DataSize; + memcpy(&Cmd[3], pData, DataSize); + (void)writeData(Cmd, DataSize + 3); + return status; +} + +// Deprecated, use cardModeSend() instead +bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { + return Electroniccats_PN7150::cardModeSend(pData, DataSize); +} + +bool Electroniccats_PN7150::cardModeReceive(unsigned char *pData, unsigned char *pDataSize) { +#ifdef DEBUG2 + Serial.println("[DEBUG] cardModeReceive exec"); +#endif +#ifdef DEBUG3 + Serial.println("[DEBUG] cardModeReceive exec"); +#endif + + bool status = NFC_ERROR; + uint8_t Ans[MAX_NCI_FRAME_SIZE]; + + (void)writeData(Ans, 255); + getMessage(2000); + + /* Is data packet ? */ + if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { +#ifdef DEBUG2 + Serial.println(rxBuffer[2]); +#endif + *pDataSize = rxBuffer[2]; + memcpy(pData, &rxBuffer[3], *pDataSize); + status = NFC_SUCCESS; + } else { + status = NFC_ERROR; + } + return status; +} + +// Deprecated, use cardModeReceive() instead +bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { + return Electroniccats_PN7150::cardModeReceive(pData, pDataSize); +} + +void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { uint8_t Answer[MAX_NCI_FRAME_SIZE]; uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; @@ -570,55 +621,8 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { } } -bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char DataSize) { - bool status; - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - - /* Compute and send DATA_PACKET */ - Cmd[0] = 0x00; - Cmd[1] = 0x00; - Cmd[2] = DataSize; - memcpy(&Cmd[3], pData, DataSize); - (void)writeData(Cmd, DataSize + 3); - return status; -} - -// Deprecated, use cardModeSend() instead -bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { - return Electroniccats_PN7150::cardModeSend(pData, DataSize); -} - -bool Electroniccats_PN7150::cardModeReceive(unsigned char *pData, unsigned char *pDataSize) { -#ifdef DEBUG2 - Serial.println("[DEBUG] cardModeReceive exec"); -#endif -#ifdef DEBUG3 - Serial.println("[DEBUG] cardModeReceive exec"); -#endif - - bool status = NFC_ERROR; - uint8_t Ans[MAX_NCI_FRAME_SIZE]; - - (void)writeData(Ans, 255); - getMessage(2000); - - /* Is data packet ? */ - if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { -#ifdef DEBUG2 - Serial.println(rxBuffer[2]); -#endif - *pDataSize = rxBuffer[2]; - memcpy(pData, &rxBuffer[3], *pDataSize); - status = NFC_SUCCESS; - } else { - status = NFC_ERROR; - } - return status; -} - -// Deprecated, use cardModeReceive() instead -bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { - return Electroniccats_PN7150::cardModeReceive(pData, pDataSize); +void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { + Electroniccats_PN7150::processCardMode(RfIntf); } void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 6d509e4..cab9da3 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -273,6 +273,8 @@ class Electroniccats_PN7150 { bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead + void processCardMode(RfIntf_t RfIntf); + void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode() instead void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); @@ -288,7 +290,6 @@ class Electroniccats_PN7150 { void ProcessP2pMode(RfIntf_t RfIntf); void ReadNdef(RfIntf_t RfIntf); void WriteNdef(RfIntf_t RfIntf); - void ProcessCardMode(RfIntf_t RfIntf); }; #endif From b05ee8084cae30a23585f678ed56d2152cff950b Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 16:05:04 -0600 Subject: [PATCH 022/106] refactor: fill interface info --- src/Electroniccats_PN7150.cpp | 14 +++++++++----- src/Electroniccats_PN7150.h | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index cd51a83..de351d9 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -336,7 +336,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->Protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; pRfIntf->MoreTags = false; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); // P2P /* Verifying if not a P2P device also presenting T4T emulation */ @@ -362,7 +362,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->Protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; pRfIntf->MoreTags = false; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); break; } } else { @@ -420,7 +420,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->Interface = rxBuffer[4]; pRfIntf->Protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); } /* In case of P2P target detected but lost, inform application to restart discovery */ @@ -625,7 +625,7 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { Electroniccats_PN7150::processCardMode(RfIntf); } -void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { +void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; switch (pRfIntf->ModeTech) { @@ -685,6 +685,10 @@ void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) } } +void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { + Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); +} + bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { bool status = ERROR; uint8_t Cmd[MAX_NCI_FRAME_SIZE]; @@ -1026,7 +1030,7 @@ bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { pRfIntf->Interface = rxBuffer[4]; pRfIntf->Protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; - FillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); status = SUCCESS; } } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index cab9da3..febd92c 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -275,6 +275,7 @@ class Electroniccats_PN7150 { bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead void processCardMode(RfIntf_t RfIntf); void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode() instead + void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); From fa120b91dd23af376bf64454d9ca547ec6244d4c Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 16:17:53 -0600 Subject: [PATCH 023/106] refactor: process reader mode --- examples/DetectTags/DetectTags.ino | 6 +-- .../ISO14443-3A_read_block.ino | 2 +- .../ISO14443-3A_write_block.ino | 2 +- .../ISO15693_read_block.ino | 2 +- .../ISO15693_write_block.ino | 2 +- .../MifareClassic_read_block.ino | 2 +- .../MifareClassic_write_block.ino | 2 +- examples/NDEFReceive/NDEFReceive.ino | 6 +-- src/Electroniccats_PN7150.cpp | 41 +++++++++++-------- src/Electroniccats_PN7150.h | 3 +- 10 files changed, 38 insertions(+), 30 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 3622247..3f3f61a 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -155,14 +155,14 @@ void loop(){ case PROT_T2T: case PROT_T3T: case PROT_ISODEP: - nfc.ProcessReaderMode(RfInterface, READ_NDEF); + nfc.processReaderMode(RfInterface, READ_NDEF); break; case PROT_ISO15693: break; case PROT_MIFARE: - nfc.ProcessReaderMode(RfInterface, READ_NDEF); + nfc.processReaderMode(RfInterface, READ_NDEF); break; default: @@ -174,7 +174,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index fad42ab..6c80ed8 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -127,7 +127,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 36cbb94..857720d 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -143,7 +143,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index 33908e0..82c0a84 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -124,7 +124,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 88edb16..d8cc75d 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -137,7 +137,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index f1afd50..d132f95 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -136,7 +136,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 80190aa..5322c49 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -173,7 +173,7 @@ void loop(){ nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 588283a..fd7d51f 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -156,14 +156,14 @@ void loop() { case PROT_T2T: case PROT_T3T: case PROT_ISODEP: - nfc.ProcessReaderMode(RfInterface, READ_NDEF); + nfc.processReaderMode(RfInterface, READ_NDEF); break; case PROT_ISO15693: break; case PROT_MIFARE: - nfc.ProcessReaderMode(RfInterface, READ_NDEF); + nfc.processReaderMode(RfInterface, READ_NDEF); break; default: @@ -175,7 +175,7 @@ void loop() { nfc.ReaderActivateNext(&RfInterface); } //* Wait for card removal - nfc.ProcessReaderMode(RfInterface, PRESENCE_CHECK); + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index de351d9..5a203de 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -445,7 +445,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint return SUCCESS; } -// Deprecaded, use waitForDiscoveryNotification() instead +// Deprecated, use waitForDiscoveryNotification() instead bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); } @@ -621,10 +621,32 @@ void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { } } +// Deprecated, use processCardMode() instead void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { Electroniccats_PN7150::processCardMode(RfIntf); } +void Electroniccats_PN7150::processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) { + switch (Operation) { + case READ_NDEF: + ReadNdef(RfIntf); + break; + case WRITE_NDEF: + WriteNdef(RfIntf); + break; + case PRESENCE_CHECK: + PresenceCheck(RfIntf); + break; + default: + break; + } +} + +// Deprecated, use processReaderMode() instead +void Electroniccats_PN7150::ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) { + Electroniccats_PN7150::processReaderMode(RfIntf, Operation); +} + void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; @@ -685,6 +707,7 @@ void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) } } +// Deprecated, use fillInterfaceInfo() instead void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); } @@ -864,22 +887,6 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } -void Electroniccats_PN7150::ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) { - switch (Operation) { - case READ_NDEF: - ReadNdef(RfIntf); - break; - case WRITE_NDEF: - WriteNdef(RfIntf); - break; - case PRESENCE_CHECK: - PresenceCheck(RfIntf); - break; - default: - break; - } -} - void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { bool status; uint8_t i; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index febd92c..fa32947 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -275,10 +275,11 @@ class Electroniccats_PN7150 { bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead void processCardMode(RfIntf_t RfIntf); void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode() instead + void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); + void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); - void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); void PresenceCheck(RfIntf_t RfIntf); bool ReaderReActivate(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); From 717dada0c919bb90b35ecacf2522b94d56ff029b Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 16:24:54 -0600 Subject: [PATCH 024/106] refactor: process p2p mode --- examples/P2P_Discovery/P2P_Discovery.ino | 2 +- src/Electroniccats_PN7150.cpp | 179 ++++++++++++----------- src/Electroniccats_PN7150.h | 3 +- 3 files changed, 95 insertions(+), 89 deletions(-) diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index d8c5c3a..eaec8a9 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -62,7 +62,7 @@ void loop(){ Serial.println(" - P2P INITIATOR MODE: Remote Target activated"); /* Process with SNEP for NDEF exchange */ - nfc.ProcessP2pMode(RfInterface); + nfc.processP2pMode(RfInterface); Serial.println("Peer lost!"); } ResetMode(); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 5a203de..d710439 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -647,6 +647,98 @@ void Electroniccats_PN7150::ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Op Electroniccats_PN7150::processReaderMode(RfIntf, Operation); } +void Electroniccats_PN7150::processP2pMode(RfIntf_t RfIntf) { + uint8_t status = ERROR; + bool restart = false; + uint8_t NCILlcpSymm[] = {0x00, 0x00, 0x02, 0x00, 0x00}; + uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; + + /* Reset P2P_NDEF state */ + P2P_NDEF_Reset(); + + /* Is Initiator mode ? */ + if ((RfIntf.ModeTech & MODE_LISTEN) != MODE_LISTEN) { + /* Initiate communication (SYMM PDU) */ + (void)writeData(NCILlcpSymm, sizeof(NCILlcpSymm)); + getMessage(); + + /* Save status for discovery restart */ + restart = true; + } + status = ERROR; + getMessage(2000); + if (rxMessageLength > 0) + status = SUCCESS; + + /* Get frame from remote peer */ + while (status == SUCCESS) { + /* is DATA_PACKET ? */ + if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize; + /* Handle P2P communication */ + P2P_NDEF_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + /* Compute DATA_PACKET to answer */ + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + status = ERROR; + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + if (rxMessageLength > 0) + status = SUCCESS; + } + /* is CORE_INTERFACE_ERROR_NTF ?*/ + else if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x08)) { + /* Come back to discovery state */ + break; + } + /* is RF_DEACTIVATE_NTF ? */ + else if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) { + /* Come back to discovery state */ + break; + } + /* is RF_DISCOVERY_NTF ? */ + else if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) { + do { + if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) { + if ((rxBuffer[6] & MODE_LISTEN) != MODE_LISTEN) + restart = true; + else + restart = false; + } + status = ERROR; + (void)writeData(rxBuffer, rxMessageLength); + getMessage(); + if (rxMessageLength > 0) + status = SUCCESS; + } while (rxMessageLength != 0); + /* Come back to discovery state */ + break; + } + + /* Wait for next frame from remote P2P, or notification event */ + status = ERROR; + (void)writeData(rxBuffer, rxMessageLength); + getMessage(); + if (rxMessageLength > 0) + status = SUCCESS; + } + + /* Is Initiator mode ? */ + if (restart) { + /* Communication ended, restart discovery loop */ + (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); + getMessage(); + getMessage(100); + } +} + +// Deprecated, use processP2pMode() instead +void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) { + Electroniccats_PN7150::processP2pMode(RfIntf); +} + void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; @@ -736,93 +828,6 @@ bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char return status; } -void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) { - uint8_t status = ERROR; - bool restart = false; - uint8_t NCILlcpSymm[] = {0x00, 0x00, 0x02, 0x00, 0x00}; - uint8_t NCIRestartDiscovery[] = {0x21, 0x06, 0x01, 0x03}; - - /* Reset P2P_NDEF state */ - P2P_NDEF_Reset(); - - /* Is Initiator mode ? */ - if ((RfIntf.ModeTech & MODE_LISTEN) != MODE_LISTEN) { - /* Initiate communication (SYMM PDU) */ - (void)writeData(NCILlcpSymm, sizeof(NCILlcpSymm)); - getMessage(); - - /* Save status for discovery restart */ - restart = true; - } - status = ERROR; - getMessage(2000); - if (rxMessageLength > 0) - status = SUCCESS; - - /* Get frame from remote peer */ - while (status == SUCCESS) { - /* is DATA_PACKET ? */ - if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize; - /* Handle P2P communication */ - P2P_NDEF_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - /* Compute DATA_PACKET to answer */ - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - status = ERROR; - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - if (rxMessageLength > 0) - status = SUCCESS; - } - /* is CORE_INTERFACE_ERROR_NTF ?*/ - else if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x08)) { - /* Come back to discovery state */ - break; - } - /* is RF_DEACTIVATE_NTF ? */ - else if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) { - /* Come back to discovery state */ - break; - } - /* is RF_DISCOVERY_NTF ? */ - else if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) { - do { - if ((rxBuffer[0] == 0x61) && ((rxBuffer[1] == 0x05) || (rxBuffer[1] == 0x03))) { - if ((rxBuffer[6] & MODE_LISTEN) != MODE_LISTEN) - restart = true; - else - restart = false; - } - status = ERROR; - (void)writeData(rxBuffer, rxMessageLength); - getMessage(); - if (rxMessageLength > 0) - status = SUCCESS; - } while (rxMessageLength != 0); - /* Come back to discovery state */ - break; - } - - /* Wait for next frame from remote P2P, or notification event */ - status = ERROR; - (void)writeData(rxBuffer, rxMessageLength); - getMessage(); - if (rxMessageLength > 0) - status = SUCCESS; - } - - /* Is Initiator mode ? */ - if (restart) { - /* Communication ended, restart discovery loop */ - (void)writeData(NCIRestartDiscovery, sizeof(NCIRestartDiscovery)); - getMessage(); - getMessage(100); - } -} - void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index fa32947..233262f 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -277,6 +277,8 @@ class Electroniccats_PN7150 { void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode() instead void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead + void processP2pMode(RfIntf_t RfIntf); + void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); @@ -289,7 +291,6 @@ class Electroniccats_PN7150 { void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); bool NxpNci_FactoryTest_RfOn(void); - void ProcessP2pMode(RfIntf_t RfIntf); void ReadNdef(RfIntf_t RfIntf); void WriteNdef(RfIntf_t RfIntf); }; From b621f062b83bb521ac2089891733f8c9fb73898d Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 16:41:48 -0600 Subject: [PATCH 025/106] refactor: presence check --- src/Electroniccats_PN7150.cpp | 175 +++++++++++++++++----------------- src/Electroniccats_PN7150.h | 3 +- 2 files changed, 92 insertions(+), 86 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index d710439..190828b 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -635,7 +635,7 @@ void Electroniccats_PN7150::processReaderMode(RfIntf_t RfIntf, RW_Operation_t Op WriteNdef(RfIntf); break; case PRESENCE_CHECK: - PresenceCheck(RfIntf); + presenceCheck(RfIntf); break; default: break; @@ -739,6 +739,95 @@ void Electroniccats_PN7150::ProcessP2pMode(RfIntf_t RfIntf) { Electroniccats_PN7150::processP2pMode(RfIntf); } +void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { + bool status; + uint8_t i; + + uint8_t NCIPresCheckT1T[] = {0x00, 0x00, 0x07, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t NCIPresCheckT2T[] = {0x00, 0x00, 0x02, 0x30, 0x00}; + uint8_t NCIPresCheckT3T[] = {0x21, 0x08, 0x04, 0xFF, 0xFF, 0x00, 0x01}; + uint8_t NCIPresCheckIsoDep[] = {0x2F, 0x11, 0x00}; + uint8_t NCIPresCheckIso15693[] = {0x00, 0x00, 0x0B, 0x26, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCISelectMIFARE[] = {0x21, 0x04, 0x03, 0x01, 0x80, 0x80}; + + switch (RfIntf.Protocol) { + case PROT_T1T: + do { + delay(500); + (void)writeData(NCIPresCheckT1T, sizeof(NCIPresCheckT1T)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)); + break; + + case PROT_T2T: + do { + delay(500); + (void)writeData(NCIPresCheckT2T, sizeof(NCIPresCheckT2T)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[2] == 0x11)); + break; + + case PROT_T3T: + do { + delay(500); + (void)writeData(NCIPresCheckT3T, sizeof(NCIPresCheckT3T)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x08) && ((rxBuffer[3] == 0x00) || (rxBuffer[4] > 0x00))); + break; + + case PROT_ISODEP: + do { + delay(500); + (void)writeData(NCIPresCheckIsoDep, sizeof(NCIPresCheckIsoDep)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x6F) && (rxBuffer[1] == 0x11) && (rxBuffer[2] == 0x01) && (rxBuffer[3] == 0x01)); + break; + + case PROT_ISO15693: + do { + delay(500); + for (i = 0; i < 8; i++) + NCIPresCheckIso15693[i + 6] = RfIntf.Info.NFC_VPP.ID[7 - i]; + (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); + getMessage(); + getMessage(100); + status = ERROR; + if (rxMessageLength) + status = SUCCESS; + } while ((status == SUCCESS) && (rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[rxMessageLength - 1] == 0x00)); + break; + + case PROT_MIFARE: + do { + delay(500); + /* Deactivate target */ + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); + + /* Reactivate target */ + (void)writeData(NCISelectMIFARE, sizeof(NCISelectMIFARE)); + getMessage(); + getMessage(100); + } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)); + break; + + default: + /* Nothing to do */ + break; + } +} + +// Deprecated, use presenceCheck() instead +void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { + Electroniccats_PN7150::presenceCheck(RfIntf); +} + void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; @@ -892,90 +981,6 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } -void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { - bool status; - uint8_t i; - - uint8_t NCIPresCheckT1T[] = {0x00, 0x00, 0x07, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t NCIPresCheckT2T[] = {0x00, 0x00, 0x02, 0x30, 0x00}; - uint8_t NCIPresCheckT3T[] = {0x21, 0x08, 0x04, 0xFF, 0xFF, 0x00, 0x01}; - uint8_t NCIPresCheckIsoDep[] = {0x2F, 0x11, 0x00}; - uint8_t NCIPresCheckIso15693[] = {0x00, 0x00, 0x0B, 0x26, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCISelectMIFARE[] = {0x21, 0x04, 0x03, 0x01, 0x80, 0x80}; - - switch (RfIntf.Protocol) { - case PROT_T1T: - do { - delay(500); - (void)writeData(NCIPresCheckT1T, sizeof(NCIPresCheckT1T)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)); - break; - - case PROT_T2T: - do { - delay(500); - (void)writeData(NCIPresCheckT2T, sizeof(NCIPresCheckT2T)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[2] == 0x11)); - break; - - case PROT_T3T: - do { - delay(500); - (void)writeData(NCIPresCheckT3T, sizeof(NCIPresCheckT3T)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x08) && ((rxBuffer[3] == 0x00) || (rxBuffer[4] > 0x00))); - break; - - case PROT_ISODEP: - do { - delay(500); - (void)writeData(NCIPresCheckIsoDep, sizeof(NCIPresCheckIsoDep)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x6F) && (rxBuffer[1] == 0x11) && (rxBuffer[2] == 0x01) && (rxBuffer[3] == 0x01)); - break; - - case PROT_ISO15693: - do { - delay(500); - for (i = 0; i < 8; i++) - NCIPresCheckIso15693[i + 6] = RfIntf.Info.NFC_VPP.ID[7 - i]; - (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); - getMessage(); - getMessage(100); - status = ERROR; - if (rxMessageLength) - status = SUCCESS; - } while ((status == SUCCESS) && (rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00) && (rxBuffer[rxMessageLength - 1] == 0x00)); - break; - - case PROT_MIFARE: - do { - delay(500); - /* Deactivate target */ - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - /* Reactivate target */ - (void)writeData(NCISelectMIFARE, sizeof(NCISelectMIFARE)); - getMessage(); - getMessage(100); - } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)); - break; - - default: - /* Nothing to do */ - break; - } -} - bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 233262f..a388438 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -279,10 +279,11 @@ class Electroniccats_PN7150 { void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead void processP2pMode(RfIntf_t RfIntf); void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead + void presenceCheck(RfIntf_t RfIntf); + void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use presenceCheck() instead void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); - void PresenceCheck(RfIntf_t RfIntf); bool ReaderReActivate(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); bool ConfigureSettings(void); From 7a2a71abeaae43d661a4b6f7c7a87a8d1187b7ce Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 11:04:16 -0600 Subject: [PATCH 026/106] refactor: reader tag cmd --- .../ISO14443-3A_read_block/ISO14443-3A_read_block.ino | 2 +- .../ISO14443-3A_write_block.ino | 4 ++-- examples/ISO15693_read_block/ISO15693_read_block.ino | 2 +- examples/ISO15693_write_block/ISO15693_write_block.ino | 4 ++-- .../MifareClassic_read_block.ino | 4 ++-- .../MifareClassic_write_block.ino | 10 +++++----- src/Electroniccats_PN7150.cpp | 7 ++++++- src/Electroniccats_PN7150.h | 5 +++-- 8 files changed, 22 insertions(+), 16 deletions(-) diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 6c80ed8..5c76189 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -52,7 +52,7 @@ void PCD_ISO14443_3A_scenario (void){ /* Read block */ unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A}; - status = nfc.ReaderTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ Serial.print("Error reading block: "); Serial.print(ReadBlock[1],HEX); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 857720d..ed0ed6d 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -55,7 +55,7 @@ void PCD_ISO14443_3A_scenario (void){ unsigned char WriteBlock[] = {0xA2, BLK_NB_ISO14443_3A, DATA_WRITE_ISO14443_3A}; // Write - status = nfc.ReaderTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); + status = nfc.readerTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) { Serial.print("Error writing block: "); @@ -68,7 +68,7 @@ void PCD_ISO14443_3A_scenario (void){ Serial.println(WriteBlock[1]); //Read block - status = nfc.ReaderTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ Serial.print("Error reading block: "); Serial.print(ReadBlock[1],HEX); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index 82c0a84..e0ff57d 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -53,7 +53,7 @@ void PCD_ISO15693_scenario (void){ unsigned char RespSize; unsigned char ReadBlock[] = {0x02, 0x20, BLK_NB_ISO15693}; - status = nfc.ReaderTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ Serial.print("Error reading block: "); Serial.print(ReadBlock[2],HEX); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index d8cc75d..1fa4a08 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -77,7 +77,7 @@ void PCD_ISO15693_scenario (void){ unsigned char WriteBlock[] = {0x02, 0x21, BLK_NB_ISO15693, DATA_WRITE_ISO15693}; // Write - status = nfc.ReaderTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); + status = nfc.readerTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) { Serial.print("Error writing block: "); @@ -90,7 +90,7 @@ void PCD_ISO15693_scenario (void){ Serial.println(WriteBlock[2]); // Read - status = nfc.ReaderTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ Serial.print("Error reading block: "); Serial.print(ReadBlock[2],HEX); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index d132f95..ce80aac 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -59,12 +59,12 @@ void PCD_MIFARE_scenario (void){ unsigned char Read[] = {0x10, 0x30, BLK_NB_MFC}; /* Authenticate */ - status = nfc.ReaderTagCmd(Auth, sizeof(Auth), Resp, &RespSize); + status = nfc.readerTagCmd(Auth, sizeof(Auth), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) Serial.println("Auth error!"); /* Read block */ - status = nfc.ReaderTagCmd(Read, sizeof(Read), Resp, &RespSize); + status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) Serial.print("Error reading sector!"); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 5322c49..2d823ba 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -65,14 +65,14 @@ uint8_t PCD_MIFARE_scenario (void){ unsigned char WritePart2[] = {0x10, DATA_WRITE_MFC}; /* Authenticate */ - status = nfc.ReaderTagCmd(Auth, sizeof(Auth), Resp, &RespSize); + status = nfc.readerTagCmd(Auth, sizeof(Auth), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ Serial.println("Auth error!"); return 1; } /* Read block */ - status = nfc.ReaderTagCmd(Read, sizeof(Read), Resp, &RespSize); + status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ Serial.print("Error reading block!"); return 2; @@ -86,18 +86,18 @@ uint8_t PCD_MIFARE_scenario (void){ PrintBuf(Resp+1, RespSize-2); /* Write block */ - status = nfc.ReaderTagCmd(WritePart1, sizeof(WritePart1), Resp, &RespSize); + status = nfc.readerTagCmd(WritePart1, sizeof(WritePart1), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ Serial.print("Error writing block!"); return 3; } - status = nfc.ReaderTagCmd(WritePart2, sizeof(WritePart2), Resp, &RespSize); + status = nfc.readerTagCmd(WritePart2, sizeof(WritePart2), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ Serial.print("Error writing block!"); return 4; } /* Read block again to see te changes*/ - status = nfc.ReaderTagCmd(Read, sizeof(Read), Resp, &RespSize); + status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) { Serial.print("Error reading block!"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 190828b..34548d8 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -893,7 +893,7 @@ void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); } -bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { +bool Electroniccats_PN7150::readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { bool status = ERROR; uint8_t Cmd[MAX_NCI_FRAME_SIZE]; @@ -917,6 +917,11 @@ bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char return status; } +// Deprecated, use readerTagCmd() instead +bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { + return Electroniccats_PN7150::readerTagCmd(pCommand, CommandSize, pAnswer, pAnswerSize); +} + void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index a388438..ea7c617 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -282,8 +282,9 @@ class Electroniccats_PN7150 { void presenceCheck(RfIntf_t RfIntf); void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use presenceCheck() instead void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); - void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); - bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); + void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); // Deprecated, use fillInterfaceInfo() instead + bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); + bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead bool ReaderReActivate(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); bool ConfigureSettings(void); From bca4d4ac89970e99a08d9dfe333f497324e819a0 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 11:06:31 -0600 Subject: [PATCH 027/106] refactor: reader re activate --- src/Electroniccats_PN7150.cpp | 49 +++++++++++++++++++---------------- src/Electroniccats_PN7150.h | 3 ++- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 34548d8..fbc5dc0 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -922,6 +922,33 @@ bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char return Electroniccats_PN7150::readerTagCmd(pCommand, CommandSize, pAnswer, pAnswerSize); } +bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { + uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; + + /* First de-activate the target */ + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); + + /* Then re-activate the target */ + NCIActivate[4] = pRfIntf->Protocol; + NCIActivate[5] = pRfIntf->Interface; + + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); + + if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) + return ERROR; + return SUCCESS; +} + +// Deprecated, use readerReActivate() instead +bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { + return Electroniccats_PN7150::readerReActivate(pRfIntf); +} + void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; @@ -986,28 +1013,6 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } -bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { - uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; - - /* First de-activate the target */ - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - /* Then re-activate the target */ - NCIActivate[4] = pRfIntf->Protocol; - NCIActivate[5] = pRfIntf->Interface; - - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) - return ERROR; - return SUCCESS; -} - bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index ea7c617..2f54fbe 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -285,7 +285,8 @@ class Electroniccats_PN7150 { void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); // Deprecated, use fillInterfaceInfo() instead bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead - bool ReaderReActivate(RfIntf_t *pRfIntf); + bool readerReActivate(RfIntf_t *pRfIntf); + bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool ReaderActivateNext(RfIntf_t *pRfIntf); bool ConfigureSettings(void); bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); From 34989a2d75cb6609f7cfbe2f005a474d3c5b6c6e Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 11:07:53 -0600 Subject: [PATCH 028/106] refactor: reader activate next --- examples/DetectTags/DetectTags.ino | 4 +- .../ISO14443-3A_read_block.ino | 2 +- .../ISO14443-3A_write_block.ino | 2 +- .../ISO15693_read_block.ino | 2 +- .../ISO15693_write_block.ino | 2 +- .../MifareClassic_read_block.ino | 2 +- .../MifareClassic_write_block.ino | 2 +- examples/NDEFReceive/NDEFReceive.ino | 4 +- src/Electroniccats_PN7150.cpp | 109 +++++++++--------- src/Electroniccats_PN7150.h | 3 +- 10 files changed, 69 insertions(+), 63 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 3f3f61a..8e348f8 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -117,7 +117,7 @@ void displayCardInfo(RfIntf_t RfIntf){ //Funtion in charge to show the card/s in break; } if(RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology - if(nfc.ReaderActivateNext(&RfIntf) == NFC_ERROR) break; + if(nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; } else break; } @@ -171,7 +171,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 5c76189..20a1028 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -124,7 +124,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index ed0ed6d..54232ff 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -140,7 +140,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index e0ff57d..f804a47 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -121,7 +121,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 1fa4a08..bbb0a0d 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -134,7 +134,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index ce80aac..4ccaa1a 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -133,7 +133,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 2d823ba..65ce76e 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -170,7 +170,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index fd7d51f..4d6794c 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -111,7 +111,7 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s break; } if (RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology - if (nfc.ReaderActivateNext(&RfIntf) == NFC_ERROR) break; + if (nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; } else break; } @@ -172,7 +172,7 @@ void loop() { //* It can detect multiple cards at the same time if they use the same protocol if (RfInterface.MoreTags) { - nfc.ReaderActivateNext(&RfInterface); + nfc.readerActivateNext(&RfInterface); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index fbc5dc0..a418c4f 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -949,6 +949,63 @@ bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { return Electroniccats_PN7150::readerReActivate(pRfIntf); } +bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; + + bool status = ERROR; + + pRfIntf->MoreTags = false; + + if (gNextTag_Protocol == PROT_UNDETERMINED) { + pRfIntf->Interface = INTF_UNDETERMINED; + pRfIntf->Protocol = PROT_UNDETERMINED; + return ERROR; + } + + /* First disconnect current tag */ + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); + getMessage(); + + if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) + return ERROR; + getMessage(100); + + if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) + return ERROR; + + NCIRfDiscoverSelect[4] = gNextTag_Protocol; + if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_ISODEP; + else if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_NFCDEP; + else if (gNextTag_Protocol == PROT_MIFARE) + NCIRfDiscoverSelect[5] = INTF_TAGCMD; + else + NCIRfDiscoverSelect[5] = INTF_FRAME; + + (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); + getMessage(); + + if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { + getMessage(100); + if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + status = SUCCESS; + } + } + + return status; +} + +// Deprecated, use readerActivateNext() instead +bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { + return Electroniccats_PN7150::readerActivateNext(pRfIntf); +} + void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; @@ -1013,58 +1070,6 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } -bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; - - bool status = ERROR; - - pRfIntf->MoreTags = false; - - if (gNextTag_Protocol == PROT_UNDETERMINED) { - pRfIntf->Interface = INTF_UNDETERMINED; - pRfIntf->Protocol = PROT_UNDETERMINED; - return ERROR; - } - - /* First disconnect current tag */ - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - - if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) - return ERROR; - getMessage(100); - - if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) - return ERROR; - - NCIRfDiscoverSelect[4] = gNextTag_Protocol; - if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (gNextTag_Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; - else - NCIRfDiscoverSelect[5] = INTF_FRAME; - - (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); - getMessage(); - - if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { - getMessage(100); - if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - status = SUCCESS; - } - } - - return status; -} - bool Electroniccats_PN7150::ConfigureSettings(void) { #if NXP_CORE_CONF /* NCI standard dedicated settings diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 2f54fbe..871ed5c 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -287,7 +287,8 @@ class Electroniccats_PN7150 { bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead bool readerReActivate(RfIntf_t *pRfIntf); bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead - bool ReaderActivateNext(RfIntf_t *pRfIntf); + bool readerActivateNext(RfIntf_t *pRfIntf); + bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead bool ConfigureSettings(void); bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); From 233584ccf49a8a403cb91b4dc8b30b0cde8b564c Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 11:22:48 -0600 Subject: [PATCH 029/106] refactor: configure settings (void) --- examples/DetectTags/DetectTags.ino | 2 +- .../DetectingReaders/DetectingReaders.ino | 2 +- .../ISO14443-3A_read_block.ino | 2 +- .../ISO14443-3A_write_block.ino | 2 +- .../ISO15693_read_block.ino | 2 +- .../ISO15693_write_block.ino | 2 +- .../MifareClassic_read_block.ino | 2 +- .../MifareClassic_write_block.ino | 2 +- examples/NDEFReceive/NDEFReceive.ino | 2 +- examples/NDEFSend/NDEFSend.ino | 2 +- examples/P2P_Discovery/P2P_Discovery.ino | 2 +- src/Electroniccats_PN7150.cpp | 775 +++++++++--------- src/Electroniccats_PN7150.h | 3 +- 13 files changed, 402 insertions(+), 398 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 8e348f8..442738e 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -134,7 +134,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1); } diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index f4f076c..1d8d589 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -34,7 +34,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1); } diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 20a1028..1a5719f 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -77,7 +77,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings failed!"); while (1); } diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 54232ff..c6c3bba 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -93,7 +93,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings failed!"); while (1); } diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index f804a47..051eb84 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -78,7 +78,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1); } diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index bbb0a0d..7643f05 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -36,7 +36,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1); } diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 4ccaa1a..1797e67 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -86,7 +86,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1); } diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 65ce76e..5a3b22e 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -123,7 +123,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1); } diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 4d6794c..f29a881 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -132,7 +132,7 @@ void setup() { ; } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); while (1) ; diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index f94a18c..9baa6c3 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -59,7 +59,7 @@ void setup() { ; } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings failed!"); while (1) ; diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index eaec8a9..f4754cf 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -40,7 +40,7 @@ void setup(){ while (1); } - if (nfc.ConfigureSettings()) { + if (nfc.configureSettings()) { Serial.println("The Configure Settings failed!"); while (1); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a418c4f..a0f8a78 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -267,6 +267,275 @@ uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { return Electroniccats_PN7150::configMode(modeSE); } +bool Electroniccats_PN7150::configureSettings(void) { +#if NXP_CORE_CONF + /* NCI standard dedicated settings + * Refer to NFC Forum NCI standard for more details + */ + uint8_t NxpNci_CORE_CONF[] = { + 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ + 0x00, 0x02, 0x00, 0x01 /* TOTAL_DURATION */ + }; +#endif + +#if NXP_CORE_CONF_EXTN + /* NXP-NCI extension dedicated setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_CONF_EXTN[] = { + 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ + 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ + 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ + }; +#endif + +#if NXP_CORE_STANDBY + /* NXP-NCI standby enable setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ +#endif + +#if NXP_TVDD_CONF + /* NXP-NCI TVDD configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; + + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ +#if (NXP_TVDD_CONF == 1) + /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; +#else + /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; +#endif +#endif + +#if NXP_RF_CONF + /* NXP-NCI RF configuration + * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + /* Following configuration is the default settings of PN7120 NFC Controller */ + uint8_t NxpNci_RF_CONF_1stGen[] = { + 0x20, 0x02, 0x38, 0x07, + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ + }; + + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ + /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ + uint8_t NxpNci_RF_CONF_2ndGen[] = { + 0x20, 0x02, 0x94, 0x11, + 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ + 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ + }; +#endif + +#if NXP_CLK_CONF + /* NXP-NCI CLOCK configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ +#if (NXP_CLK_CONF == 1) + /* Xtal configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ + }; +#else + /* PLL configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ + 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ + }; +#endif +#endif + + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + bool gRfSettingsRestored_flag = false; + +#if (NXP_TVDD_CONF | NXP_RF_CONF) + uint8_t *NxpNci_CONF; + uint16_t NxpNci_CONF_size = 0; +#endif +#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) + uint8_t currentTS[32] = __TIMESTAMP__; + uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; + uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; +#endif + bool isResetRequired = false; + + /* Apply settings */ +#if NXP_CORE_CONF + if (sizeof(NxpNci_CORE_CONF) != 0) { + isResetRequired = true; + (void)writeData(NxpNci_CORE_CONF, sizeof(NxpNci_CORE_CONF)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CORE_CONF"); +#endif + return ERROR; + } + } +#endif + +#if NXP_CORE_STANDBY + if (sizeof(NxpNci_CORE_STANDBY) != 0) { + (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); + getMessage(); + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CORE_STANDBY"); +#endif + return ERROR; + } + } +#endif + + /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) + or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ +#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) + /* First read timestamp stored in NFC Controller */ + if (gNfcController_generation == 1) + NCIReadTS[5] = 0x0F; + (void)writeData(NCIReadTS, sizeof(NCIReadTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("read timestamp "); +#endif + return ERROR; + } + /* Then compare with current build timestamp, and check RF setting restauration flag */ + /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) + { + // No change, nothing to do + } + else + { + */ + /* Apply settings */ +#if NXP_CORE_CONF_EXTN + if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) { + (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CORE_CONF_EXTN"); +#endif + return ERROR; + } + } +#endif + +#if NXP_CLK_CONF + if (sizeof(NxpNci_CLK_CONF) != 0) { + isResetRequired = true; + + (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); + getMessage(); + // NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CLK_CONF"); +#endif + return ERROR; + } + } +#endif + +#if NXP_TVDD_CONF + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CONF_size"); +#endif + return ERROR; + } + } +#endif + +#if NXP_RF_CONF + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CONF_size"); +#endif + return ERROR; + } + } +#endif + /* Store curent timestamp to NFC Controller memory for further checks */ + if (gNfcController_generation == 1) + NCIWriteTS[5] = 0x0F; + memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); + (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NFC Controller memory"); +#endif + return ERROR; + } + //} +#endif + + if (isResetRequired) { + /* Reset the NFC Controller to insure new settings apply */ + (void)writeData(NCICoreReset, sizeof(NCICoreReset)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("insure new settings apply"); +#endif + return ERROR; + } + + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("insure new settings apply 2"); +#endif + return ERROR; + } + } + return SUCCESS; +} + +// Deprecated, use configureSettings(void) instead +bool Electroniccats_PN7150::ConfigureSettings(void) { return configureSettings(); } + uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) : sizeof(DiscoveryTechnologiesP2P)); @@ -927,413 +1196,147 @@ bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; /* First de-activate the target */ - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - /* Then re-activate the target */ - NCIActivate[4] = pRfIntf->Protocol; - NCIActivate[5] = pRfIntf->Interface; - - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) - return ERROR; - return SUCCESS; -} - -// Deprecated, use readerReActivate() instead -bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { - return Electroniccats_PN7150::readerReActivate(pRfIntf); -} - -bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; - - bool status = ERROR; - - pRfIntf->MoreTags = false; - - if (gNextTag_Protocol == PROT_UNDETERMINED) { - pRfIntf->Interface = INTF_UNDETERMINED; - pRfIntf->Protocol = PROT_UNDETERMINED; - return ERROR; - } - - /* First disconnect current tag */ - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - - if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) - return ERROR; - getMessage(100); - - if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) - return ERROR; - - NCIRfDiscoverSelect[4] = gNextTag_Protocol; - if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (gNextTag_Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; - else - NCIRfDiscoverSelect[5] = INTF_FRAME; - - (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); - getMessage(); - - if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { - getMessage(100); - if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - status = SUCCESS; - } - } - - return status; -} - -// Deprecated, use readerActivateNext() instead -bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { - return Electroniccats_PN7150::readerActivateNext(pRfIntf); -} - -void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize = 0; - - RW_NDEF_Reset(RfIntf.Protocol); - - while (1) { - RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - if (CmdSize == 0) { - /// End of the Read operation - break; - } else { - // Compute and send DATA_PACKET - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - getMessage(1000); - - // Manage chaining in case of T4T - if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { - uint8_t tmp[MAX_NCI_FRAME_SIZE]; - uint8_t tmpSize = 0; - while (rxBuffer[0] == 0x10) { - memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); - tmpSize += rxBuffer[2]; - getMessage(100); - } - memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); - tmpSize += rxBuffer[2]; - //* Compute all chained frame into one unique answer - memcpy(&rxBuffer[3], tmp, tmpSize); - rxBuffer[2] = tmpSize; - } - } - } -} - -void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize = 0; - - RW_NDEF_Reset(RfIntf.Protocol); - - while (1) { - RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - if (CmdSize == 0) { - // End of the Write operation - break; - } else { - // Compute and send DATA_PACKET - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - getMessage(2000); - } - } -} - -bool Electroniccats_PN7150::ConfigureSettings(void) { -#if NXP_CORE_CONF - /* NCI standard dedicated settings - * Refer to NFC Forum NCI standard for more details - */ - uint8_t NxpNci_CORE_CONF[] = { - 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ - 0x00, 0x02, 0x00, 0x01 /* TOTAL_DURATION */ - }; -#endif - -#if NXP_CORE_CONF_EXTN - /* NXP-NCI extension dedicated setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_CONF_EXTN[] = { - 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ - 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ - 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ - }; -#endif - -#if NXP_CORE_STANDBY - /* NXP-NCI standby enable setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ -#endif - -#if NXP_TVDD_CONF - /* NXP-NCI TVDD configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; - - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ -#if (NXP_TVDD_CONF == 1) - /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; -#else - /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; -#endif -#endif - -#if NXP_RF_CONF - /* NXP-NCI RF configuration - * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - /* Following configuration is the default settings of PN7120 NFC Controller */ - uint8_t NxpNci_RF_CONF_1stGen[] = { - 0x20, 0x02, 0x38, 0x07, - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ - }; - - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ - /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ - uint8_t NxpNci_RF_CONF_2ndGen[] = { - 0x20, 0x02, 0x94, 0x11, - 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ - 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ - }; -#endif - -#if NXP_CLK_CONF - /* NXP-NCI CLOCK configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ -#if (NXP_CLK_CONF == 1) - /* Xtal configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ - }; -#else - /* PLL configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ - 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ - }; -#endif -#endif + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - bool gRfSettingsRestored_flag = false; + /* Then re-activate the target */ + NCIActivate[4] = pRfIntf->Protocol; + NCIActivate[5] = pRfIntf->Interface; -#if (NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t *NxpNci_CONF; - uint16_t NxpNci_CONF_size = 0; -#endif -#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t currentTS[32] = __TIMESTAMP__; - uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; - uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; -#endif - bool isResetRequired = false; + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - /* Apply settings */ -#if NXP_CORE_CONF - if (sizeof(NxpNci_CORE_CONF) != 0) { - isResetRequired = true; - (void)writeData(NxpNci_CORE_CONF, sizeof(NxpNci_CORE_CONF)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF"); -#endif - return ERROR; - } - } -#endif + if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) + return ERROR; + return SUCCESS; +} -#if NXP_CORE_STANDBY - if (sizeof(NxpNci_CORE_STANDBY) != 0) { - (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); - getMessage(); - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CORE_STANDBY"); -#endif - return ERROR; - } +// Deprecated, use readerReActivate() instead +bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { + return Electroniccats_PN7150::readerReActivate(pRfIntf); +} + +bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; + + bool status = ERROR; + + pRfIntf->MoreTags = false; + + if (gNextTag_Protocol == PROT_UNDETERMINED) { + pRfIntf->Interface = INTF_UNDETERMINED; + pRfIntf->Protocol = PROT_UNDETERMINED; + return ERROR; } -#endif - /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) - or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ -#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - /* First read timestamp stored in NFC Controller */ - if (gNfcController_generation == 1) - NCIReadTS[5] = 0x0F; - (void)writeData(NCIReadTS, sizeof(NCIReadTS)); + /* First disconnect current tag */ + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("read timestamp "); -#endif + + if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) return ERROR; - } - /* Then compare with current build timestamp, and check RF setting restauration flag */ - /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) - { - // No change, nothing to do - } + getMessage(100); + + if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) + return ERROR; + + NCIRfDiscoverSelect[4] = gNextTag_Protocol; + if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_ISODEP; + else if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_NFCDEP; + else if (gNextTag_Protocol == PROT_MIFARE) + NCIRfDiscoverSelect[5] = INTF_TAGCMD; else - { - */ - /* Apply settings */ -#if NXP_CORE_CONF_EXTN - if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) { - (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF_EXTN"); -#endif - return ERROR; - } - } -#endif + NCIRfDiscoverSelect[5] = INTF_FRAME; -#if NXP_CLK_CONF - if (sizeof(NxpNci_CLK_CONF) != 0) { - isResetRequired = true; + (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); + getMessage(); - (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); - getMessage(); - // NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CLK_CONF"); -#endif - return ERROR; + if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { + getMessage(100); + if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + status = SUCCESS; } } -#endif -#if NXP_TVDD_CONF - if (NxpNci_CONF_size != 0) { - (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); -#endif - return ERROR; - } - } -#endif + return status; +} -#if NXP_RF_CONF - if (NxpNci_CONF_size != 0) { - (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); -#endif - return ERROR; +// Deprecated, use readerActivateNext() instead +bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { + return Electroniccats_PN7150::readerActivateNext(pRfIntf); +} + +void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize = 0; + + RW_NDEF_Reset(RfIntf.Protocol); + + while (1) { + RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + if (CmdSize == 0) { + /// End of the Read operation + break; + } else { + // Compute and send DATA_PACKET + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + getMessage(1000); + + // Manage chaining in case of T4T + if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { + uint8_t tmp[MAX_NCI_FRAME_SIZE]; + uint8_t tmpSize = 0; + while (rxBuffer[0] == 0x10) { + memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); + tmpSize += rxBuffer[2]; + getMessage(100); + } + memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); + tmpSize += rxBuffer[2]; + //* Compute all chained frame into one unique answer + memcpy(&rxBuffer[3], tmp, tmpSize); + rxBuffer[2] = tmpSize; + } } } -#endif - /* Store curent timestamp to NFC Controller memory for further checks */ - if (gNfcController_generation == 1) - NCIWriteTS[5] = 0x0F; - memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); - (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NFC Controller memory"); -#endif - return ERROR; - } - //} -#endif +} - if (isResetRequired) { - /* Reset the NFC Controller to insure new settings apply */ - (void)writeData(NCICoreReset, sizeof(NCICoreReset)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("insure new settings apply"); -#endif - return ERROR; - } +void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize = 0; - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("insure new settings apply 2"); -#endif - return ERROR; + RW_NDEF_Reset(RfIntf.Protocol); + + while (1) { + RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + if (CmdSize == 0) { + // End of the Write operation + break; + } else { + // Compute and send DATA_PACKET + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + getMessage(2000); } } - return SUCCESS; } bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) { diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 871ed5c..d318ecb 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -261,6 +261,8 @@ class Electroniccats_PN7150 { int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t configMode(uint8_t modeSE); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead + bool configureSettings(void); + bool ConfigureSettings(void); // Deprecated, use configureSettings() instead uint8_t startDiscovery(uint8_t modeSE); uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead bool stopDiscovery(); @@ -289,7 +291,6 @@ class Electroniccats_PN7150 { bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead - bool ConfigureSettings(void); bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); From 084ac0d1baa21fd1587e42c2472387304d15840e Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 11:55:46 -0600 Subject: [PATCH 030/106] refactor: configure settings (params) --- src/Electroniccats_PN7150.cpp | 967 +++++++++++++++++----------------- src/Electroniccats_PN7150.h | 5 +- 2 files changed, 490 insertions(+), 482 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a0f8a78..162676b 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -534,7 +534,289 @@ bool Electroniccats_PN7150::configureSettings(void) { } // Deprecated, use configureSettings(void) instead -bool Electroniccats_PN7150::ConfigureSettings(void) { return configureSettings(); } +bool Electroniccats_PN7150::ConfigureSettings(void) { + return Electroniccats_PN7150::configureSettings(); +} + +bool Electroniccats_PN7150::configureSettings(uint8_t *uidcf, uint8_t uidlen) { +#if NXP_CORE_CONF + /* NCI standard dedicated settings + * Refer to NFC Forum NCI standard for more details + */ + uint8_t NxpNci_CORE_CONF[20] = { + 0x20, 0x02, 0x05, 0x01, // CORE_SET_CONFIG_CMD + 0x00, 0x02, 0x00, 0x01 // TOTAL_DURATION + }; + + if (uidlen == 0) + uidlen = 8; + else { + uidlen += 10; + memcpy(&NxpNci_CORE_CONF[0], uidcf, uidlen); + } + +#endif + +#if NXP_CORE_CONF_EXTN + /* NXP-NCI extension dedicated setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_CONF_EXTN[] = { + 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ + 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ + 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ + }; +#endif + +#if NXP_CORE_STANDBY + /* NXP-NCI standby enable setting + * Refer to NFC controller User Manual for more details + */ + uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ +#endif + +#if NXP_TVDD_CONF + /* NXP-NCI TVDD configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; + + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ +#if (NXP_TVDD_CONF == 1) + /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; +#else + /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ + uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; +#endif +#endif + +#if NXP_RF_CONF + /* NXP-NCI RF configuration + * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details + */ + /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ + /* Following configuration is the default settings of PN7120 NFC Controller */ + uint8_t NxpNci_RF_CONF_1stGen[] = { + 0x20, 0x02, 0x38, 0x07, + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ + }; + + /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ + /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ + uint8_t NxpNci_RF_CONF_2ndGen[] = { + 0x20, 0x02, 0x94, 0x11, + 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ + 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ + 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ + 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ + 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ + 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ + }; +#endif + +#if NXP_CLK_CONF + /* NXP-NCI CLOCK configuration + * Refer to NFC controller Hardware Design Guide document for more details + */ +#if (NXP_CLK_CONF == 1) + /* Xtal configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ + }; +#else + /* PLL configuration */ + uint8_t NxpNci_CLK_CONF[] = { + 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ + 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ + 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ + }; +#endif +#endif + + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + bool gRfSettingsRestored_flag = false; + +#if (NXP_TVDD_CONF | NXP_RF_CONF) + uint8_t *NxpNci_CONF; + uint16_t NxpNci_CONF_size = 0; +#endif +#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) + uint8_t currentTS[32] = __TIMESTAMP__; + uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; + uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; +#endif + bool isResetRequired = false; + + /* Apply settings */ +#if NXP_CORE_CONF + if (uidlen != 0) // sizeof(NxpNci_CORE_CONF) != 0) + { + isResetRequired = true; + (void)writeData(NxpNci_CORE_CONF, uidlen); // sizeof(NxpNci_CORE_CONF)); + getMessage(100); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CORE_CONF"); +#endif + return ERROR; + } + } +#endif + +#if NXP_CORE_STANDBY + if (sizeof(NxpNci_CORE_STANDBY) != 0) { + (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); + getMessage(); + if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CORE_STANDBY"); +#endif + return ERROR; + } + } +#endif + + /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) + or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ +#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) + /* First read timestamp stored in NFC Controller */ + if (gNfcController_generation == 1) + NCIReadTS[5] = 0x0F; + (void)writeData(NCIReadTS, sizeof(NCIReadTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("read timestamp "); +#endif + return ERROR; + } + /* Then compare with current build timestamp, and check RF setting restauration flag */ + /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) + { + // No change, nothing to do + } + else + { + */ + /* Apply settings */ +#if NXP_CORE_CONF_EXTN + if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) { + (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CORE_CONF_EXTN"); +#endif + return ERROR; + } + } +#endif + +#if NXP_CLK_CONF + if (sizeof(NxpNci_CLK_CONF) != 0) { + isResetRequired = true; + + (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); + getMessage(); + // NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CLK_CONF"); +#endif + return ERROR; + } + } +#endif + +#if NXP_TVDD_CONF + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CONF_size"); +#endif + return ERROR; + } + } +#endif + +#if NXP_RF_CONF + if (NxpNci_CONF_size != 0) { + (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NxpNci_CONF_size"); +#endif + return ERROR; + } + } +#endif + /* Store curent timestamp to NFC Controller memory for further checks */ + if (gNfcController_generation == 1) + NCIWriteTS[5] = 0x0F; + memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); + (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { +#ifdef SerialUSB + Serial.println("NFC Controller memory"); +#endif + return ERROR; + } + //} +#endif + + if (isResetRequired) { + /* Reset the NFC Controller to insure new settings apply */ + (void)writeData(NCICoreReset, sizeof(NCICoreReset)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("insure new settings apply"); +#endif + return ERROR; + } + + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) { +#ifdef SerialUSB + Serial.println("insure new settings apply 2"); +#endif + return ERROR; + } + } + return SUCCESS; +} + +// Deprecated, use configureSettings() instead +bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) { + return Electroniccats_PN7150::configureSettings(uidcf, uidlen); +} uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) @@ -1084,534 +1366,259 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { getMessage(); getMessage(100); } while ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x05)); - break; - - default: - /* Nothing to do */ - break; - } -} - -// Deprecated, use presenceCheck() instead -void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { - Electroniccats_PN7150::presenceCheck(RfIntf); -} - -void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { - uint8_t i, temp; - - switch (pRfIntf->ModeTech) { - case (MODE_POLL | TECH_PASSIVE_NFCA): - memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); - temp = 2; - pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; - temp++; - memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); - temp += pBuf[2]; - pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; - temp++; - - if (pRfIntf->Info.NFC_APP.SelResLen == 1) - pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; - - temp += 4; - if (pBuf[temp] != 0) { - temp++; - pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; - memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); - } else { - pRfIntf->Info.NFC_APP.RatsLen = 0; - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCB): - pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; - memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); - temp = pBuf[0] + 4; - if (pBuf[temp] != 0) { - temp++; - pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; - memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); - } else { - pRfIntf->Info.NFC_BPP.AttribResLen = 0; - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCF): - pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; - pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; - memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); - break; - - case (MODE_POLL | TECH_PASSIVE_15693): - pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; - pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; - - for (i = 0; i < 8; i++) - pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; - - break; - - default: - break; - } -} - -// Deprecated, use fillInterfaceInfo() instead -void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { - Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); -} - -bool Electroniccats_PN7150::readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { - bool status = ERROR; - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - - /* Compute and send DATA_PACKET */ - Cmd[0] = 0x00; - Cmd[1] = 0x00; - Cmd[2] = CommandSize; - memcpy(&Cmd[3], pCommand, CommandSize); - - (void)writeData(Cmd, CommandSize + 3); - getMessage(); - getMessage(1000); - /* Wait for Answer 1S */ - - if ((rxBuffer[0] == 0x0) && (rxBuffer[1] == 0x0)) - status = SUCCESS; - - *pAnswerSize = rxBuffer[2]; - memcpy(pAnswer, &rxBuffer[3], *pAnswerSize); - - return status; -} - -// Deprecated, use readerTagCmd() instead -bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { - return Electroniccats_PN7150::readerTagCmd(pCommand, CommandSize, pAnswer, pAnswerSize); -} - -bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { - uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; - - /* First de-activate the target */ - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - /* Then re-activate the target */ - NCIActivate[4] = pRfIntf->Protocol; - NCIActivate[5] = pRfIntf->Interface; - - (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); - getMessage(); - getMessage(100); - - if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) - return ERROR; - return SUCCESS; -} - -// Deprecated, use readerReActivate() instead -bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { - return Electroniccats_PN7150::readerReActivate(pRfIntf); -} - -bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { - uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; - - bool status = ERROR; - - pRfIntf->MoreTags = false; - - if (gNextTag_Protocol == PROT_UNDETERMINED) { - pRfIntf->Interface = INTF_UNDETERMINED; - pRfIntf->Protocol = PROT_UNDETERMINED; - return ERROR; - } - - /* First disconnect current tag */ - (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); - getMessage(); - - if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) - return ERROR; - getMessage(100); - - if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) - return ERROR; - - NCIRfDiscoverSelect[4] = gNextTag_Protocol; - if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (gNextTag_Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (gNextTag_Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; - else - NCIRfDiscoverSelect[5] = INTF_FRAME; - - (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); - getMessage(); + break; - if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { - getMessage(100); - if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { - pRfIntf->Interface = rxBuffer[4]; - pRfIntf->Protocol = rxBuffer[5]; - pRfIntf->ModeTech = rxBuffer[6]; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - status = SUCCESS; - } + default: + /* Nothing to do */ + break; } - - return status; } -// Deprecated, use readerActivateNext() instead -bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { - return Electroniccats_PN7150::readerActivateNext(pRfIntf); +// Deprecated, use presenceCheck() instead +void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { + Electroniccats_PN7150::presenceCheck(RfIntf); } -void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize = 0; +void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { + uint8_t i, temp; - RW_NDEF_Reset(RfIntf.Protocol); + switch (pRfIntf->ModeTech) { + case (MODE_POLL | TECH_PASSIVE_NFCA): + memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); + temp = 2; + pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; + temp++; + memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); + temp += pBuf[2]; + pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; + temp++; - while (1) { - RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - if (CmdSize == 0) { - /// End of the Read operation - break; - } else { - // Compute and send DATA_PACKET - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; + if (pRfIntf->Info.NFC_APP.SelResLen == 1) + pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - getMessage(1000); + temp += 4; + if (pBuf[temp] != 0) { + temp++; + pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); + } else { + pRfIntf->Info.NFC_APP.RatsLen = 0; + } + break; - // Manage chaining in case of T4T - if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { - uint8_t tmp[MAX_NCI_FRAME_SIZE]; - uint8_t tmpSize = 0; - while (rxBuffer[0] == 0x10) { - memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); - tmpSize += rxBuffer[2]; - getMessage(100); - } - memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); - tmpSize += rxBuffer[2]; - //* Compute all chained frame into one unique answer - memcpy(&rxBuffer[3], tmp, tmpSize); - rxBuffer[2] = tmpSize; + case (MODE_POLL | TECH_PASSIVE_NFCB): + pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; + memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); + temp = pBuf[0] + 4; + if (pBuf[temp] != 0) { + temp++; + pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); + } else { + pRfIntf->Info.NFC_BPP.AttribResLen = 0; } - } - } -} + break; -void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { - uint8_t Cmd[MAX_NCI_FRAME_SIZE]; - uint16_t CmdSize = 0; + case (MODE_POLL | TECH_PASSIVE_NFCF): + pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; + pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; + memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); + break; - RW_NDEF_Reset(RfIntf.Protocol); + case (MODE_POLL | TECH_PASSIVE_15693): + pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; + pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; + + for (i = 0; i < 8; i++) + pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; - while (1) { - RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); - if (CmdSize == 0) { - // End of the Write operation break; - } else { - // Compute and send DATA_PACKET - Cmd[0] = 0x00; - Cmd[1] = (CmdSize & 0xFF00) >> 8; - Cmd[2] = CmdSize & 0x00FF; - (void)writeData(Cmd, CmdSize + 3); - getMessage(); - getMessage(2000); - } + default: + break; } } -bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) { -#if NXP_CORE_CONF - /* NCI standard dedicated settings - * Refer to NFC Forum NCI standard for more details - */ - uint8_t NxpNci_CORE_CONF[20] = { - 0x20, 0x02, 0x05, 0x01, // CORE_SET_CONFIG_CMD - 0x00, 0x02, 0x00, 0x01 // TOTAL_DURATION - }; +// Deprecated, use fillInterfaceInfo() instead +void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { + Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); +} - if (uidlen == 0) - uidlen = 8; - else { - uidlen += 10; - memcpy(&NxpNci_CORE_CONF[0], uidcf, uidlen); - } +bool Electroniccats_PN7150::readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { + bool status = ERROR; + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; -#endif + /* Compute and send DATA_PACKET */ + Cmd[0] = 0x00; + Cmd[1] = 0x00; + Cmd[2] = CommandSize; + memcpy(&Cmd[3], pCommand, CommandSize); -#if NXP_CORE_CONF_EXTN - /* NXP-NCI extension dedicated setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_CONF_EXTN[] = { - 0x20, 0x02, 0x0D, 0x03, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x40, 0x01, 0x00, /* TAG_DETECTOR_CFG */ - 0xA0, 0x41, 0x01, 0x04, /* TAG_DETECTOR_THRESHOLD_CFG */ - 0xA0, 0x43, 0x01, 0x00 /* TAG_DETECTOR_FALLBACK_CNT_CFG */ - }; -#endif + (void)writeData(Cmd, CommandSize + 3); + getMessage(); + getMessage(1000); + /* Wait for Answer 1S */ -#if NXP_CORE_STANDBY - /* NXP-NCI standby enable setting - * Refer to NFC controller User Manual for more details - */ - uint8_t NxpNci_CORE_STANDBY[] = {0x2F, 0x00, 0x01, 0x01}; /* last byte indicates enable/disable */ -#endif + if ((rxBuffer[0] == 0x0) && (rxBuffer[1] == 0x0)) + status = SUCCESS; -#if NXP_TVDD_CONF - /* NXP-NCI TVDD configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - uint8_t NxpNci_TVDD_CONF_1stGen[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x13, 0x01, 0x00}; + *pAnswerSize = rxBuffer[2]; + memcpy(pAnswer, &rxBuffer[3], *pAnswerSize); - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ -#if (NXP_TVDD_CONF == 1) - /* CFG1: Vbat is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x02, 0x09, 0x00}; -#else - /* CFG2: external 5V is used to generate the VDD(TX) through TXLDO */ - uint8_t NxpNci_TVDD_CONF_2ndGen[] = {0x20, 0x02, 0x07, 0x01, 0xA0, 0x0E, 0x03, 0x06, 0x64, 0x00}; -#endif -#endif + return status; +} -#if NXP_RF_CONF - /* NXP-NCI RF configuration - * Refer to NFC controller Antenna Design and Tuning Guidelines document for more details - */ - /* RF configuration related to 1st generation of NXP-NCI controller (e.g PN7120) */ - /* Following configuration is the default settings of PN7120 NFC Controller */ - uint8_t NxpNci_RF_CONF_1stGen[] = { - 0x20, 0x02, 0x38, 0x07, - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x01, 0x00, 0xF1, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x44, 0xA3, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0xDC, 0x50, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x06, 0x03, 0x00, 0x70, /* RF_CLIF_CFG_TARGET CLIF_TRANSCEIVE_CONTROL_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x16, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_UNDERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x03, 0x06, 0x15, 0x00, /* RF_CLIF_CFG_TARGET CLIF_TX_OVERSHOOT_CONFIG_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x4A, 0x53, 0x07, 0x01, 0x1B /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_SHAPE_CONTROL_REG */ - }; +// Deprecated, use readerTagCmd() instead +bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { + return Electroniccats_PN7150::readerTagCmd(pCommand, CommandSize, pAnswer, pAnswerSize); +} - /* RF configuration related to 2nd generation of NXP-NCI controller (e.g PN7150)*/ - /* Following configuration relates to performance optimization of OM5578/PN7150 NFC Controller demo kit */ - uint8_t NxpNci_RF_CONF_2ndGen[] = { - 0x20, 0x02, 0x94, 0x11, - 0xA0, 0x0D, 0x06, 0x04, 0x35, 0x90, 0x01, 0xF4, 0x01, /* RF_CLIF_CFG_INITIATOR CLIF_AGC_INPUT_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x30, 0x01, 0x90, 0x03, 0x00, /* RF_CLIF_CFG_TARGET CLIF_SIGPRO_ADCBCM_THRESHOLD_REG */ - 0xA0, 0x0D, 0x06, 0x06, 0x42, 0x02, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TARGET CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x20, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_TECHNO_I_TX15693 CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x04, 0x22, 0x44, 0x23, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x22, 0x2D, 0x50, 0x34, 0x0C, 0x00, /* RF_CLIF_CFG_TECHNO_I_RX15693 CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x32, 0x42, 0xF8, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXA CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x2D, 0x24, 0x37, 0x0C, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x34, 0x33, 0x86, 0x80, 0x00, 0x70, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_AGC_CONFIG0_REG */ - 0xA0, 0x0D, 0x04, 0x34, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXA_P CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x42, 0x2D, 0x15, 0x45, 0x0D, 0x00, /* RF_CLIF_CFG_BR_848_I_RXA CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x04, 0x46, 0x44, 0x22, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_ANA_RX_REG */ - 0xA0, 0x0D, 0x06, 0x46, 0x2D, 0x05, 0x59, 0x0E, 0x00, /* RF_CLIF_CFG_BR_106_I_RXB CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x44, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_106_I_TXB CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x56, 0x2D, 0x05, 0x9F, 0x0C, 0x00, /* RF_CLIF_CFG_BR_212_I_RXF_P CLIF_SIGPRO_RM_CONFIG1_REG */ - 0xA0, 0x0D, 0x06, 0x54, 0x42, 0x88, 0x00, 0xFF, 0xFF, /* RF_CLIF_CFG_BR_212_I_TXF CLIF_ANA_TX_AMPLITUDE_REG */ - 0xA0, 0x0D, 0x06, 0x0A, 0x33, 0x80, 0x86, 0x00, 0x70 /* RF_CLIF_CFG_I_ACTIVE CLIF_AGC_CONFIG0_REG */ - }; -#endif +bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { + uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; -#if NXP_CLK_CONF - /* NXP-NCI CLOCK configuration - * Refer to NFC controller Hardware Design Guide document for more details - */ -#if (NXP_CLK_CONF == 1) - /* Xtal configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x05, 0x01, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x08 /* CLOCK_SEL_CFG */ - }; -#else - /* PLL configuration */ - uint8_t NxpNci_CLK_CONF[] = { - 0x20, 0x02, 0x09, 0x02, /* CORE_SET_CONFIG_CMD */ - 0xA0, 0x03, 0x01, 0x11, /* CLOCK_SEL_CFG */ - 0xA0, 0x04, 0x01, 0x01 /* CLOCK_TO_CFG */ - }; -#endif -#endif + /* First de-activate the target */ + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x00}; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - bool gRfSettingsRestored_flag = false; + /* Then re-activate the target */ + NCIActivate[4] = pRfIntf->Protocol; + NCIActivate[5] = pRfIntf->Interface; -#if (NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t *NxpNci_CONF; - uint16_t NxpNci_CONF_size = 0; -#endif -#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - uint8_t currentTS[32] = __TIMESTAMP__; - uint8_t NCIReadTS[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x14}; - uint8_t NCIWriteTS[7 + 32] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x14, 0x20}; -#endif - bool isResetRequired = false; + (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); + getMessage(); + getMessage(100); - /* Apply settings */ -#if NXP_CORE_CONF - if (uidlen != 0) // sizeof(NxpNci_CORE_CONF) != 0) - { - isResetRequired = true; - (void)writeData(NxpNci_CORE_CONF, uidlen); // sizeof(NxpNci_CORE_CONF)); - getMessage(100); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF"); -#endif - return ERROR; - } - } -#endif + if ((rxBuffer[0] != 0x61) || (rxBuffer[1] != 0x05)) + return ERROR; + return SUCCESS; +} -#if NXP_CORE_STANDBY - if (sizeof(NxpNci_CORE_STANDBY) != 0) { - (void)(writeData(NxpNci_CORE_STANDBY, sizeof(NxpNci_CORE_STANDBY))); - getMessage(); - if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CORE_STANDBY"); -#endif - return ERROR; - } +// Deprecated, use readerReActivate() instead +bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { + return Electroniccats_PN7150::readerReActivate(pRfIntf); +} + +bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { + uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; + + bool status = ERROR; + + pRfIntf->MoreTags = false; + + if (gNextTag_Protocol == PROT_UNDETERMINED) { + pRfIntf->Interface = INTF_UNDETERMINED; + pRfIntf->Protocol = PROT_UNDETERMINED; + return ERROR; } -#endif - /* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp) - or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */ -#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF) - /* First read timestamp stored in NFC Controller */ - if (gNfcController_generation == 1) - NCIReadTS[5] = 0x0F; - (void)writeData(NCIReadTS, sizeof(NCIReadTS)); + /* First disconnect current tag */ + (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x03) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("read timestamp "); -#endif + + if ((rxBuffer[0] != 0x41) && (rxBuffer[1] != 0x06) && (rxBuffer[3] != 0x00)) return ERROR; - } - /* Then compare with current build timestamp, and check RF setting restauration flag */ - /*if(!memcmp(&rxBuffer[8], currentTS, sizeof(currentTS)) && (gRfSettingsRestored_flag == false)) - { - // No change, nothing to do - } + getMessage(100); + + if ((rxBuffer[0] != 0x61) && (rxBuffer[1] != 0x06)) + return ERROR; + + NCIRfDiscoverSelect[4] = gNextTag_Protocol; + if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_ISODEP; + else if (gNextTag_Protocol == PROT_ISODEP) + NCIRfDiscoverSelect[5] = INTF_NFCDEP; + else if (gNextTag_Protocol == PROT_MIFARE) + NCIRfDiscoverSelect[5] = INTF_TAGCMD; else - { - */ - /* Apply settings */ -#if NXP_CORE_CONF_EXTN - if (sizeof(NxpNci_CORE_CONF_EXTN) != 0) { - (void)writeData(NxpNci_CORE_CONF_EXTN, sizeof(NxpNci_CORE_CONF_EXTN)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF_EXTN"); -#endif - return ERROR; - } - } -#endif + NCIRfDiscoverSelect[5] = INTF_FRAME; -#if NXP_CLK_CONF - if (sizeof(NxpNci_CLK_CONF) != 0) { - isResetRequired = true; + (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); + getMessage(); - (void)writeData(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF)); - getMessage(); - // NxpNci_HostTransceive(NxpNci_CLK_CONF, sizeof(NxpNci_CLK_CONF), Answer, sizeof(Answer), &AnswerSize); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CLK_CONF"); -#endif - return ERROR; + if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x04) && (rxBuffer[3] == 0x00)) { + getMessage(100); + if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { + pRfIntf->Interface = rxBuffer[4]; + pRfIntf->Protocol = rxBuffer[5]; + pRfIntf->ModeTech = rxBuffer[6]; + fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + status = SUCCESS; } } -#endif -#if NXP_TVDD_CONF - if (NxpNci_CONF_size != 0) { - (void)writeData(NxpNci_TVDD_CONF_2ndGen, sizeof(NxpNci_TVDD_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); -#endif - return ERROR; - } - } -#endif + return status; +} -#if NXP_RF_CONF - if (NxpNci_CONF_size != 0) { - (void)writeData(NxpNci_RF_CONF_2ndGen, sizeof(NxpNci_RF_CONF_2ndGen)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NxpNci_CONF_size"); -#endif - return ERROR; +// Deprecated, use readerActivateNext() instead +bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { + return Electroniccats_PN7150::readerActivateNext(pRfIntf); +} + +void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize = 0; + + RW_NDEF_Reset(RfIntf.Protocol); + + while (1) { + RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + if (CmdSize == 0) { + /// End of the Read operation + break; + } else { + // Compute and send DATA_PACKET + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + getMessage(1000); + + // Manage chaining in case of T4T + if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { + uint8_t tmp[MAX_NCI_FRAME_SIZE]; + uint8_t tmpSize = 0; + while (rxBuffer[0] == 0x10) { + memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); + tmpSize += rxBuffer[2]; + getMessage(100); + } + memcpy(&tmp[tmpSize], &rxBuffer[3], rxBuffer[2]); + tmpSize += rxBuffer[2]; + //* Compute all chained frame into one unique answer + memcpy(&rxBuffer[3], tmp, tmpSize); + rxBuffer[2] = tmpSize; + } } } -#endif - /* Store curent timestamp to NFC Controller memory for further checks */ - if (gNfcController_generation == 1) - NCIWriteTS[5] = 0x0F; - memcpy(&NCIWriteTS[7], currentTS, sizeof(currentTS)); - (void)writeData(NCIWriteTS, sizeof(NCIWriteTS)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { -#ifdef SerialUSB - Serial.println("NFC Controller memory"); -#endif - return ERROR; - } - //} -#endif +} - if (isResetRequired) { - /* Reset the NFC Controller to insure new settings apply */ - (void)writeData(NCICoreReset, sizeof(NCICoreReset)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("insure new settings apply"); -#endif - return ERROR; - } +void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { + uint8_t Cmd[MAX_NCI_FRAME_SIZE]; + uint16_t CmdSize = 0; - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) { -#ifdef SerialUSB - Serial.println("insure new settings apply 2"); -#endif - return ERROR; + RW_NDEF_Reset(RfIntf.Protocol); + + while (1) { + RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); + if (CmdSize == 0) { + // End of the Write operation + break; + } else { + // Compute and send DATA_PACKET + Cmd[0] = 0x00; + Cmd[1] = (CmdSize & 0xFF00) >> 8; + Cmd[2] = CmdSize & 0x00FF; + + (void)writeData(Cmd, CmdSize + 3); + getMessage(); + getMessage(2000); } } - return SUCCESS; } // #if defined P2P_SUPPORT || defined RW_SUPPORT diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index d318ecb..a04da40 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -262,7 +262,9 @@ class Electroniccats_PN7150 { uint8_t configMode(uint8_t modeSE); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead bool configureSettings(void); - bool ConfigureSettings(void); // Deprecated, use configureSettings() instead + bool ConfigureSettings(void); // Deprecated, use configureSettings(void) instead + bool configureSettings(uint8_t *nfcuid, uint8_t uidlen); + bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); // Deprecated, use configureSettings() instead uint8_t startDiscovery(uint8_t modeSE); uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead bool stopDiscovery(); @@ -291,7 +293,6 @@ class Electroniccats_PN7150 { bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead - bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); From 5de7eb73a8d4ed4ea5e7da59ff2905bf51a843e4 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 12:26:38 -0600 Subject: [PATCH 031/106] refactor: nci factory test prbs --- src/Electroniccats_PN7150.cpp | 19 ++++++------------- src/Electroniccats_PN7150.h | 3 ++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 162676b..8c8e96d 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1621,7 +1621,6 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } -// #if defined P2P_SUPPORT || defined RW_SUPPORT void Electroniccats_PN7150::NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { unsigned char *pNdefRecord = pNdefMessage; NdefRecord_t NdefRecord; @@ -1731,23 +1730,12 @@ void Electroniccats_PN7150::NdefPull_Cb(unsigned char *pNdefMessage, unsigned sh Serial.println(""); } -// #endif // if defined P2P_SUPPORT || defined RW_SUPPORT - -// #if defined P2P_SUPPORT || defined CARDEMU_SUPPORT -const char NDEF_MESSAGE[] = {0xD1, // MB/ME/CF/1/IL/TNF - 0x01, // TYPE LENGTH - 0x07, // PAYLOAD LENTGH - 'T', // TYPE - 0x02, // Status - 'e', 'n', // Language - 'T', 'e', 's', 't'}; void Electroniccats_PN7150::NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { Serial.println("--- NDEF Record sent"); } -// #endif // if defined P2P_SUPPORT || defined CARDEMU_SUPPORT -bool Electroniccats_PN7150::NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) { +bool Electroniccats_PN7150::nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) { uint8_t NCIPrbs_1stGen[] = {0x2F, 0x30, 0x04, 0x00, 0x00, 0x01, 0x01}; uint8_t NCIPrbs_2ndGen[] = {0x2F, 0x30, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; uint8_t *NxpNci_cmd; @@ -1777,6 +1765,11 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpN return SUCCESS; } +// Deprecated, use nciFactoryTestPrbs instead +bool Electroniccats_PN7150::NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) { + return Electroniccats_PN7150::nciFactoryTestPrbs(type, bitrate); +} + bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn(void) { uint8_t NCIRfOn[] = {0x2F, 0x3D, 0x02, 0x20, 0x01}; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index a04da40..cddc0f4 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -295,7 +295,8 @@ class Electroniccats_PN7150 { bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); - bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); + bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); + bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool NxpNci_FactoryTest_RfOn(void); void ReadNdef(RfIntf_t RfIntf); void WriteNdef(RfIntf_t RfIntf); From d27531a37a6c3f9aa30415cdcd6941d18286b307 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 12:31:35 -0600 Subject: [PATCH 032/106] feat: remove ndef callbacks --- src/Electroniccats_PN7150.cpp | 114 ---------------------------------- src/Electroniccats_PN7150.h | 2 - 2 files changed, 116 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 8c8e96d..17fc8c2 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1621,120 +1621,6 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } -void Electroniccats_PN7150::NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { - unsigned char *pNdefRecord = pNdefMessage; - NdefRecord_t NdefRecord; - unsigned char save; - - if (pNdefMessage == NULL) { - Serial.println("--- Provisioned buffer size too small or NDEF message empty"); - return; - } - - while (pNdefRecord != NULL) { - Serial.println("--- NDEF record received:"); - - NdefRecord = DetectNdefRecordType(pNdefRecord); - - switch (NdefRecord.recordType) { - case MEDIA_VCARD: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("vCard:"); - // Serial.println(NdefRecord.recordPayload); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_TEXT: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("Text record:"); - // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_URI: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("URI record: "); - // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } break; - - case MEDIA_HANDOVER_WIFI: { - unsigned char index = 0, i; - - Serial.println("--- Received WIFI credentials:"); - if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) - index += 4; - while (index < NdefRecord.recordPayloadSize) { - if (NdefRecord.recordPayload[index] == 0x10) { - if (NdefRecord.recordPayload[index + 1] == 0x45) { - Serial.print("- SSID = "); - for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) - Serial.print(NdefRecord.recordPayload[index + 4 + i]); - Serial.println(""); - } else if (NdefRecord.recordPayload[index + 1] == 0x03) { - Serial.print("- Authenticate Type = "); - Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); - } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { - Serial.print("- Encryption Type = "); - Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); - } else if (NdefRecord.recordPayload[index + 1] == 0x27) { - Serial.print("- Network key = "); - for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) - Serial.print("#"); - Serial.println(""); - } - index += 4 + NdefRecord.recordPayload[index + 3]; - } else - continue; - } - } break; - - case WELL_KNOWN_HANDOVER_SELECT: - Serial.print("Handover select version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); - break; - - case WELL_KNOWN_HANDOVER_REQUEST: - Serial.print("Handover request version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); - break; - - case MEDIA_HANDOVER_BT: - Serial.print("BT Handover payload = "); - // Serial.print(NdefRecord.recordPayload); - // Serial.println(NdefRecord.recordPayloadSize); - break; - - case MEDIA_HANDOVER_BLE: - Serial.print("BLE Handover payload = "); - // Serial.print(NdefRecord.recordPayload); - // Serial.println(NdefRecord.recordPayloadSize); - break; - - case MEDIA_HANDOVER_BLE_SECURE: - Serial.print(" BLE secure Handover payload = "); - // Serial.println(NdefRecord.recordPayload, NdefRecord.recordPayloadSize); - break; - - default: - Serial.println("Unsupported NDEF record, cannot parse"); - break; - } - pNdefRecord = GetNextRecord(pNdefRecord); - } - - Serial.println(""); -} - -void Electroniccats_PN7150::NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { - Serial.println("--- NDEF Record sent"); -} - bool Electroniccats_PN7150::nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) { uint8_t NCIPrbs_1stGen[] = {0x2F, 0x30, 0x04, 0x00, 0x00, 0x01, 0x01}; uint8_t NCIPrbs_2ndGen[] = {0x2F, 0x30, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index cddc0f4..8f743cf 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -293,8 +293,6 @@ class Electroniccats_PN7150 { bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead - void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize); - void NdefPush_Cb(unsigned char *pNdefRecord, unsigned short NdefRecordSize); bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool NxpNci_FactoryTest_RfOn(void); From 9c036bca850da3674f627f6159f09338e8ae36ab Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 12:35:00 -0600 Subject: [PATCH 033/106] refactor: nci factory test rf on --- src/Electroniccats_PN7150.cpp | 7 ++++++- src/Electroniccats_PN7150.h | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 17fc8c2..b4d59e4 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1656,7 +1656,7 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpN return Electroniccats_PN7150::nciFactoryTestPrbs(type, bitrate); } -bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn(void) { +bool Electroniccats_PN7150::nciFactoryTestRfOn() { uint8_t NCIRfOn[] = {0x2F, 0x3D, 0x02, 0x20, 0x01}; (void)writeData(NCIRfOn, sizeof(NCIRfOn)); @@ -1667,3 +1667,8 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn(void) { return SUCCESS; } + +// Deprecated, use nciFactoryTestRfOn instead +bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { + return Electroniccats_PN7150::nciFactoryTestRfOn(); +} \ No newline at end of file diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 8f743cf..f610aed 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -295,7 +295,8 @@ class Electroniccats_PN7150 { bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead - bool NxpNci_FactoryTest_RfOn(void); + bool nciFactoryTestRfOn(); + bool NxpNci_FactoryTest_RfOn(); void ReadNdef(RfIntf_t RfIntf); void WriteNdef(RfIntf_t RfIntf); }; From c809e1a2eeba5437bf6f115ccde4be89918dc7ea Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 12:49:43 -0600 Subject: [PATCH 034/106] refactor: read ndef --- src/Electroniccats_PN7150.cpp | 9 +++++++-- src/Electroniccats_PN7150.h | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index b4d59e4..c22b918 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1180,7 +1180,7 @@ void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { void Electroniccats_PN7150::processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) { switch (Operation) { case READ_NDEF: - ReadNdef(RfIntf); + readNdef(RfIntf); break; case WRITE_NDEF: WriteNdef(RfIntf); @@ -1557,7 +1557,7 @@ bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { return Electroniccats_PN7150::readerActivateNext(pRfIntf); } -void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { +void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; @@ -1597,6 +1597,11 @@ void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { } } +// Deprecated, use readNdef() instead +void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { + Electroniccats_PN7150::readNdef(RfIntf); +} + void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index f610aed..f706c44 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -293,11 +293,12 @@ class Electroniccats_PN7150 { bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead + void readNdef(RfIntf_t RfIntf); + void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdef() instead bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool nciFactoryTestRfOn(); - bool NxpNci_FactoryTest_RfOn(); - void ReadNdef(RfIntf_t RfIntf); + bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead void WriteNdef(RfIntf_t RfIntf); }; From 034f4446ae0c432db9a13c7b60b7b0a410a7d04f Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 12:51:13 -0600 Subject: [PATCH 035/106] refactor: write ndef --- src/Electroniccats_PN7150.cpp | 8 ++++++-- src/Electroniccats_PN7150.h | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index c22b918..d5ece3e 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1183,7 +1183,7 @@ void Electroniccats_PN7150::processReaderMode(RfIntf_t RfIntf, RW_Operation_t Op readNdef(RfIntf); break; case WRITE_NDEF: - WriteNdef(RfIntf); + writeNdef(RfIntf); break; case PRESENCE_CHECK: presenceCheck(RfIntf); @@ -1602,7 +1602,7 @@ void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { Electroniccats_PN7150::readNdef(RfIntf); } -void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { +void Electroniccats_PN7150::writeNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; @@ -1626,6 +1626,10 @@ void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { } } +void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { + Electroniccats_PN7150::writeNdef(RfIntf); +} + bool Electroniccats_PN7150::nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate) { uint8_t NCIPrbs_1stGen[] = {0x2F, 0x30, 0x04, 0x00, 0x00, 0x01, 0x01}; uint8_t NCIPrbs_2ndGen[] = {0x2F, 0x30, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index f706c44..0b369ab 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -295,11 +295,12 @@ class Electroniccats_PN7150 { bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead void readNdef(RfIntf_t RfIntf); void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdef() instead + void writeNdef(RfIntf_t RfIntf); + void WriteNdef(RfIntf_t RfIntf); // Deprecated, use writeNdef() instead bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead - void WriteNdef(RfIntf_t RfIntf); }; #endif From 4bbeb73f344ab72794956cb785b34805f99fa66a Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 16:28:20 -0600 Subject: [PATCH 036/106] feat: add mode class --- examples/NDEFReceive/NDEFReceive.ino | 1 + examples/NDEFSend/NDEFSend.ino | 4 ++++ src/Electroniccats_PN7150.cpp | 3 ++- src/Electroniccats_PN7150.h | 2 ++ src/Mode.cpp | 18 ++++++++++++++++++ src/Mode.h | 21 +++++++++++++++++++++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/Mode.cpp create mode 100644 src/Mode.h diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index f29a881..20b93bb 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -36,6 +36,7 @@ void PrintBuf(const byte *data, const uint32_t numBytes) { // Print hex data bu } Serial.println(); } + void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field char tmp[16]; while (1) { diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 9baa6c3..bb8cb92 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -72,6 +72,10 @@ void setup() { } nfc.startDiscovery(mode); + nfc.mode.set(nfc.mode.P2P); + Serial.println("Mode: " + String(nfc.mode.get())); + nfc.mode.set(2); + Serial.println("Mode: " + String(nfc.mode.get())); Serial.print("Waiting for an NDEF device"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index d5ece3e..cb5124a 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1680,4 +1680,5 @@ bool Electroniccats_PN7150::nciFactoryTestRfOn() { // Deprecated, use nciFactoryTestRfOn instead bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { return Electroniccats_PN7150::nciFactoryTestRfOn(); -} \ No newline at end of file +} + diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 0b369ab..6ff6cf3 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -25,6 +25,7 @@ #include "RW_NDEF.h" #include "T4T_NDEF_emu.h" #include "ndef_helper.h" +#include "Mode.h" // #define DEBUG3 @@ -253,6 +254,7 @@ class Electroniccats_PN7150 { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); + Mode mode; uint8_t begin(void); bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) diff --git a/src/Mode.cpp b/src/Mode.cpp new file mode 100644 index 0000000..cc76d9f --- /dev/null +++ b/src/Mode.cpp @@ -0,0 +1,18 @@ +#include "Mode.h" + +Mode::Mode() { + mode = 1; +} + +bool Mode::set(unsigned int mode) { + if (mode < 1 || mode > 3) { + return false; + } + + this->mode = mode; + return true; +} + +int Mode::get() { + return mode; +} diff --git a/src/Mode.h b/src/Mode.h new file mode 100644 index 0000000..a526aa1 --- /dev/null +++ b/src/Mode.h @@ -0,0 +1,21 @@ +#ifndef Mode_H +#define Mode_H + +#include "Arduino.h" + +class Mode { + private: + int mode; + + public: + Mode(); + bool set(unsigned int mode); + int get(); + enum { + READER_WRITER = 1, + EMULATION = 2, + P2P = 3 + }; +}; + +#endif \ No newline at end of file From 072852bd9c278ec5e155974ac041c987c090fd0b Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 16:52:11 -0600 Subject: [PATCH 037/106] feat: add get and set mode --- examples/NDEFSend/NDEFSend.ino | 8 ++++---- src/Electroniccats_PN7150.cpp | 7 +++++++ src/Electroniccats_PN7150.h | 2 ++ src/Mode.cpp | 2 +- src/Mode.h | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index bb8cb92..059ab1a 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -72,10 +72,10 @@ void setup() { } nfc.startDiscovery(mode); - nfc.mode.set(nfc.mode.P2P); - Serial.println("Mode: " + String(nfc.mode.get())); - nfc.mode.set(2); - Serial.println("Mode: " + String(nfc.mode.get())); + nfc.setMode(nfc.mode.READER_WRITER); + Serial.println("Mode: " + String(nfc.getMode())); + nfc.setMode(nfc.mode.EMULATION); + Serial.println("Mode: " + String(nfc.getMode())); Serial.print("Waiting for an NDEF device"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index cb5124a..3ed1495 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1682,3 +1682,10 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { return Electroniccats_PN7150::nciFactoryTestRfOn(); } +bool Electroniccats_PN7150::setMode(int mode) { + return this->mode.set(mode); +} + +int Electroniccats_PN7150::getMode() { + return this->mode.get(); +} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 6ff6cf3..6772029 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -303,6 +303,8 @@ class Electroniccats_PN7150 { bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead + bool setMode(int mode); + int getMode(); }; #endif diff --git a/src/Mode.cpp b/src/Mode.cpp index cc76d9f..db2a5a2 100644 --- a/src/Mode.cpp +++ b/src/Mode.cpp @@ -4,7 +4,7 @@ Mode::Mode() { mode = 1; } -bool Mode::set(unsigned int mode) { +bool Mode::set(int mode) { if (mode < 1 || mode > 3) { return false; } diff --git a/src/Mode.h b/src/Mode.h index a526aa1..0fbb4d9 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -9,7 +9,7 @@ class Mode { public: Mode(); - bool set(unsigned int mode); + bool set(int mode); int get(); enum { READER_WRITER = 1, From 7dbe536d7f5db1b22924b6e3ebd6aa64cc29d3b0 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 8 Aug 2023 21:02:38 -0600 Subject: [PATCH 038/106] feat: add mode struct --- examples/NDEFSend/NDEFSend.ino | 1 + src/Electroniccats_PN7150.cpp | 8 -------- src/Electroniccats_PN7150.h | 6 ++---- src/Mode.cpp | 18 +++++++++--------- src/Mode.h | 26 +++++++++++++++----------- 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 059ab1a..af0eb7c 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -72,6 +72,7 @@ void setup() { } nfc.startDiscovery(mode); + nfc.setMode(nfc.mode.READER_WRITER); Serial.println("Mode: " + String(nfc.getMode())); nfc.setMode(nfc.mode.EMULATION); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 3ed1495..3e56bf5 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1681,11 +1681,3 @@ bool Electroniccats_PN7150::nciFactoryTestRfOn() { bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { return Electroniccats_PN7150::nciFactoryTestRfOn(); } - -bool Electroniccats_PN7150::setMode(int mode) { - return this->mode.set(mode); -} - -int Electroniccats_PN7150::getMode() { - return this->mode.get(); -} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 6772029..3dc1c9d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -238,7 +238,7 @@ typedef union { NxpNci_RfIntf_info_VPP_t NFC_VPP; } NxpNci_RfIntf_Info_t; -class Electroniccats_PN7150 { +class Electroniccats_PN7150 : public Mode { private: uint8_t _IRQpin, _VENpin, _I2Caddress; TwoWire *_wire; @@ -254,7 +254,7 @@ class Electroniccats_PN7150 { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); - Mode mode; + // Mode mode; uint8_t begin(void); bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) @@ -303,8 +303,6 @@ class Electroniccats_PN7150 { bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead - bool setMode(int mode); - int getMode(); }; #endif diff --git a/src/Mode.cpp b/src/Mode.cpp index db2a5a2..6744506 100644 --- a/src/Mode.cpp +++ b/src/Mode.cpp @@ -1,18 +1,18 @@ #include "Mode.h" Mode::Mode() { - mode = 1; + this->_mode = 1; } -bool Mode::set(int mode) { - if (mode < 1 || mode > 3) { - return false; - } +bool Mode::setMode(int mode) { + if (mode < 1 || mode > 3) { + return false; + } - this->mode = mode; - return true; + this->_mode = mode; + return true; } -int Mode::get() { - return mode; +int Mode::getMode() { + return this->_mode; } diff --git a/src/Mode.h b/src/Mode.h index 0fbb4d9..6da14fc 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -3,19 +3,23 @@ #include "Arduino.h" +struct Mode_t { + enum { + READER_WRITER = 1, + EMULATION = 2, + P2P = 3 + }; +}; + class Mode { - private: - int mode; + private: + int _mode; - public: - Mode(); - bool set(int mode); - int get(); - enum { - READER_WRITER = 1, - EMULATION = 2, - P2P = 3 - }; + public: + Mode(); + Mode_t mode; + bool setMode(int mode); + int getMode(); }; #endif \ No newline at end of file From 871b1ec0d69e633e1e9fe4df01b93269ae5468b5 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 09:31:43 -0600 Subject: [PATCH 039/106] feat: add reset function --- examples/NDEFReceive/NDEFReceive.ino | 3 ++- examples/NDEFSend/NDEFSend.ino | 8 +++----- src/Electroniccats_PN7150.cpp | 16 ++++++++++++++-- src/Electroniccats_PN7150.h | 1 + src/Mode.cpp | 2 +- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 20b93bb..6f254ea 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -182,7 +182,8 @@ void loop() { nfc.stopDiscovery(); nfc.startDiscovery(mode); } - ResetMode(); + // ResetMode(); + nfc.reset(); delay(500); } diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index af0eb7c..579e6cd 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -73,10 +73,6 @@ void setup() { nfc.startDiscovery(mode); - nfc.setMode(nfc.mode.READER_WRITER); - Serial.println("Mode: " + String(nfc.getMode())); - nfc.setMode(nfc.mode.EMULATION); - Serial.println("Mode: " + String(nfc.getMode())); Serial.print("Waiting for an NDEF device"); } @@ -85,7 +81,8 @@ void loop() { } void checkReaders() { - Serial.print("."); + // Serial.print("."); + unsigned long startTime = millis(); if (nfc.cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { @@ -96,6 +93,7 @@ void checkReaders() { Serial.print("Waiting for an NDEF device"); } } + Serial.println("Elapsed time: " + String(millis() - startTime) + "ms"); } void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 3e56bf5..766e8fb 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -163,8 +163,12 @@ int Electroniccats_PN7150::GetFwVersion() { } uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { - unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU - : MODE_P2P); + // TODO: refactor modeSE to mode + unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU : MODE_P2P); + + if (!Electroniccats_PN7150::setMode(modeSE)) { + return ERROR; + } uint8_t Command[MAX_NCI_FRAME_SIZE]; @@ -1091,6 +1095,8 @@ bool Electroniccats_PN7150::cardModeReceive(unsigned char *pData, unsigned char Serial.println("[DEBUG] cardModeReceive exec"); #endif + delay(1); + bool status = NFC_ERROR; uint8_t Ans[MAX_NCI_FRAME_SIZE]; @@ -1681,3 +1687,9 @@ bool Electroniccats_PN7150::nciFactoryTestRfOn() { bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { return Electroniccats_PN7150::nciFactoryTestRfOn(); } + +bool Electroniccats_PN7150::reset() { + int mode = Electroniccats_PN7150::getMode(); + Electroniccats_PN7150::configMode(mode); + Electroniccats_PN7150::startDiscovery(mode); +} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 3dc1c9d..8bf4506 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -303,6 +303,7 @@ class Electroniccats_PN7150 : public Mode { bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead + bool reset(); }; #endif diff --git a/src/Mode.cpp b/src/Mode.cpp index 6744506..cd4c174 100644 --- a/src/Mode.cpp +++ b/src/Mode.cpp @@ -1,7 +1,7 @@ #include "Mode.h" Mode::Mode() { - this->_mode = 1; + this->_mode = mode.READER_WRITER; } bool Mode::setMode(int mode) { From 3714204cdf6cc086b02cd32575ea82270c08e578 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 09:45:35 -0600 Subject: [PATCH 040/106] fix: return value for reset function --- src/Electroniccats_PN7150.cpp | 2 ++ src/Mode.cpp | 12 ++++++++++++ src/Mode.h | 3 +++ 3 files changed, 17 insertions(+) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 766e8fb..096b280 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1692,4 +1692,6 @@ bool Electroniccats_PN7150::reset() { int mode = Electroniccats_PN7150::getMode(); Electroniccats_PN7150::configMode(mode); Electroniccats_PN7150::startDiscovery(mode); + + return SUCCESS; } diff --git a/src/Mode.cpp b/src/Mode.cpp index cd4c174..d40ac43 100644 --- a/src/Mode.cpp +++ b/src/Mode.cpp @@ -13,6 +13,18 @@ bool Mode::setMode(int mode) { return true; } +void Mode::setReaderWriterMode() { + this->_mode = mode.READER_WRITER; +} + +void Mode::setEmulationMode() { + this->_mode = mode.EMULATION; +} + +void Mode::setP2PMode() { + this->_mode = mode.P2P; +} + int Mode::getMode() { return this->_mode; } diff --git a/src/Mode.h b/src/Mode.h index 6da14fc..0434ead 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -19,6 +19,9 @@ class Mode { Mode(); Mode_t mode; bool setMode(int mode); + void setReaderWriterMode(); + void setEmulationMode(); + void setP2PMode(); int getMode(); }; From bae1fdc5c7503397e6c967212a728065852dfcbe Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 10:07:26 -0600 Subject: [PATCH 041/106] feat: add config mode without params --- examples/NDEFSend/NDEFSend.ino | 6 ++++-- src/Electroniccats_PN7150.cpp | 7 ++++++- src/Electroniccats_PN7150.h | 3 ++- src/Mode.h | 4 +++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 579e6cd..a534af9 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -65,8 +65,10 @@ void setup() { ; } - if (nfc.configMode(mode)) { - Serial.println("The Configure Mode failed!!"); + nfc.setEmulationMode(); + + if (nfc.configMode()) { + Serial.println("The Configure Mode failed!"); while (1) ; } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 096b280..9309d0f 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -266,7 +266,12 @@ uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { return SUCCESS; } -// Deprecated, use configMode() instead +uint8_t Electroniccats_PN7150::configMode() { + int mode = Electroniccats_PN7150::getMode(); + return Electroniccats_PN7150::configMode(mode); +} + +// Deprecated, use configMode(void) instead uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { return Electroniccats_PN7150::configMode(modeSE); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 8bf4506..408f49c 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -262,7 +262,8 @@ class Electroniccats_PN7150 : public Mode { int getFirmwareVersion(); int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t configMode(uint8_t modeSE); - uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead + uint8_t configMode(void); + uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode(void) instead bool configureSettings(void); bool ConfigureSettings(void); // Deprecated, use configureSettings(void) instead bool configureSettings(uint8_t *nfcuid, uint8_t uidlen); diff --git a/src/Mode.h b/src/Mode.h index 0434ead..450d0f1 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -15,10 +15,12 @@ class Mode { private: int _mode; + protected: + bool setMode(int mode); + public: Mode(); Mode_t mode; - bool setMode(int mode); void setReaderWriterMode(); void setEmulationMode(); void setP2PMode(); From bc4940ce2de47a8f64617784902ba3cdf2fad47e Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 12:00:54 -0600 Subject: [PATCH 042/106] feat: add start discovery without params --- examples/DetectTags/DetectTags.ino | 229 +++++++++++------------ examples/DetectTags/Makefile | 19 ++ examples/DetectTagsOld/DetectTagsOld.ino | 185 ++++++++++++++++++ examples/DetectTagsOld/Makefile | 19 ++ examples/NDEFSend/NDEFSend.ino | 2 +- src/Electroniccats_PN7150.cpp | 18 +- src/Electroniccats_PN7150.h | 3 +- src/Mode.h | 2 +- 8 files changed, 354 insertions(+), 123 deletions(-) create mode 100644 examples/DetectTags/Makefile create mode 100644 examples/DetectTagsOld/DetectTagsOld.ino create mode 100644 examples/DetectTagsOld/Makefile diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 442738e..55ee41b 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -1,185 +1,180 @@ /** - * Example detect tags and show their unique ID - * Authors: + * Example detect tags and show their unique ID + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * March 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Intarface to save data for multiple tags -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation - -void ResetMode(){ //Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.configMode(mode); - nfc.startDiscovery(mode); -} - -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) - { + Serial.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { Serial.print(F(" ")); } } Serial.println(); } -void displayCardInfo(RfIntf_t RfIntf){ //Funtion in charge to show the card/s in te field + +void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field char tmp[16]; - while (1){ - switch(RfIntf.Protocol){ //Indetify card protocol - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: + while (1) { + switch (RfIntf.Protocol) { // Indetify card protocol + case PROT_T1T: + case PROT_T2T: + case PROT_T3T: + case PROT_ISODEP: Serial.print(" - POLL MODE: Remote activated tag type: "); Serial.println(RfIntf.Protocol); break; - case PROT_ISO15693: + case PROT_ISO15693: Serial.println(" - POLL MODE: Remote ISO15693 card activated"); break; - case PROT_MIFARE: + case PROT_MIFARE: Serial.println(" - POLL MODE: Remote MIFARE card activated"); break; - default: + default: Serial.println(" - POLL MODE: Undetermined target"); return; } - switch(RfIntf.ModeTech) { //Indetify card technology + switch (RfIntf.ModeTech) { // Indetify card technology case (MODE_POLL | TECH_PASSIVE_NFCA): - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); - - if(RfIntf.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); Serial.println(" "); - - } - break; - + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[0]); + Serial.print(tmp); + Serial.print(" "); + sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[1]); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); + + if (RfIntf.Info.NFC_APP.SelResLen != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SelRes[0]); + Serial.print(tmp); + Serial.println(" "); + } + break; + case (MODE_POLL | TECH_PASSIVE_NFCB): - if(RfIntf.Info.NFC_BPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_BPP.SensRes,RfIntf.Info.NFC_BPP.SensResLen); - } - break; - + if (RfIntf.Info.NFC_BPP.SensResLen != 0) { + Serial.print("\tSENS_RES = "); + PrintBuf(RfIntf.Info.NFC_BPP.SensRes, RfIntf.Info.NFC_BPP.SensResLen); + } + break; + case (MODE_POLL | TECH_PASSIVE_NFCF): - Serial.print("\tBitrate = "); - Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); - - if(RfIntf.Info.NFC_FPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_FPP.SensRes,RfIntf.Info.NFC_FPP.SensResLen); - } - break; - + Serial.print("\tBitrate = "); + Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); + + if (RfIntf.Info.NFC_FPP.SensResLen != 0) { + Serial.print("\tSENS_RES = "); + PrintBuf(RfIntf.Info.NFC_FPP.SensRes, RfIntf.Info.NFC_FPP.SensResLen); + } + break; + case (MODE_POLL | TECH_PASSIVE_15693): - Serial.print("\tID = "); - PrintBuf(RfIntf.Info.NFC_VPP.ID,sizeof(RfIntf.Info.NFC_VPP.ID)); - - Serial.print("\ntAFI = "); - Serial.println(RfIntf.Info.NFC_VPP.AFI); - - Serial.print("\tDSFID = "); - Serial.println(RfIntf.Info.NFC_VPP.DSFID,HEX); - break; - + Serial.print("\tID = "); + PrintBuf(RfIntf.Info.NFC_VPP.ID, sizeof(RfIntf.Info.NFC_VPP.ID)); + + Serial.print("\ntAFI = "); + Serial.println(RfIntf.Info.NFC_VPP.AFI); + + Serial.print("\tDSFID = "); + Serial.println(RfIntf.Info.NFC_VPP.DSFID, HEX); + break; + default: - break; - } - if(RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology - if(nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; + break; } - else break; + if (RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology + if (nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; + } else + break; } } -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); + while (!Serial) + ; Serial.println("Detect NFC tags with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); - while (1); + while (1) + ; } - - if(nfc.configMode(mode)){ //Set up the configuration mode + + // Read/Write mode as default + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); - while (1); + while (1) + ; } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(); // NCI Discovery mode Serial.println("Waiting for an Card ..."); } -void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards +void loop() { + if (!nfc.waitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards displayCardInfo(RfInterface); - switch(RfInterface.Protocol) { + switch (RfInterface.Protocol) { case PROT_T1T: case PROT_T2T: case PROT_T3T: case PROT_ISODEP: - nfc.processReaderMode(RfInterface, READ_NDEF); - break; - + nfc.processReaderMode(RfInterface, READ_NDEF); + break; + case PROT_ISO15693: - break; - + break; + case PROT_MIFARE: - nfc.processReaderMode(RfInterface, READ_NDEF); - break; - + nfc.processReaderMode(RfInterface, READ_NDEF); + break; + default: - break; + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + + //* It can detect multiple cards at the same time if they use the same protocol + if (RfInterface.MoreTags) { + nfc.readerActivateNext(&RfInterface); } - //* Wait for card removal + //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - - nfc.stopDiscovery(); - nfc.startDiscovery(mode); } - ResetMode(); + nfc.reset(); delay(500); } diff --git a/examples/DetectTags/Makefile b/examples/DetectTags/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/DetectTags/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/DetectTagsOld/DetectTagsOld.ino b/examples/DetectTagsOld/DetectTagsOld.ino new file mode 100644 index 0000000..442738e --- /dev/null +++ b/examples/DetectTagsOld/DetectTagsOld.ino @@ -0,0 +1,185 @@ +/** + * Example detect tags and show their unique ID + * Authors: + * Salvador Mendoza - @Netxing - salmg.net + * For Electronic Cats - electroniccats.com + * + * March 2020 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (15) +#define PN7150_VEN (14) +#define PN7150_ADDR (0x28) + +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; //Intarface to save data for multiple tags + +uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation + +void ResetMode(){ //Reset the configuration mode after each reading + Serial.println("Re-initializing..."); + nfc.configMode(mode); + nfc.startDiscovery(mode); +} + +void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format + uint32_t szPos; + for (szPos=0; szPos < numBytes; szPos++) + { + Serial.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + Serial.print(F("0")); + Serial.print(data[szPos]&0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) + { + Serial.print(F(" ")); + } + } + Serial.println(); +} +void displayCardInfo(RfIntf_t RfIntf){ //Funtion in charge to show the card/s in te field + char tmp[16]; + while (1){ + switch(RfIntf.Protocol){ //Indetify card protocol + case PROT_T1T: + case PROT_T2T: + case PROT_T3T: + case PROT_ISODEP: + Serial.print(" - POLL MODE: Remote activated tag type: "); + Serial.println(RfIntf.Protocol); + break; + case PROT_ISO15693: + Serial.println(" - POLL MODE: Remote ISO15693 card activated"); + break; + case PROT_MIFARE: + Serial.println(" - POLL MODE: Remote MIFARE card activated"); + break; + default: + Serial.println(" - POLL MODE: Undetermined target"); + return; + } + + switch(RfIntf.ModeTech) { //Indetify card technology + case (MODE_POLL | TECH_PASSIVE_NFCA): + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SensRes[0]); + Serial.print(tmp); Serial.print(" "); + sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SensRes[1]); + Serial.print(tmp); Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); + + if(RfIntf.Info.NFC_APP.SelResLen != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SelRes[0]); + Serial.print(tmp); Serial.println(" "); + + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCB): + if(RfIntf.Info.NFC_BPP.SensResLen != 0) { + Serial.print("\tSENS_RES = "); + PrintBuf(RfIntf.Info.NFC_BPP.SensRes,RfIntf.Info.NFC_BPP.SensResLen); + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCF): + Serial.print("\tBitrate = "); + Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); + + if(RfIntf.Info.NFC_FPP.SensResLen != 0) { + Serial.print("\tSENS_RES = "); + PrintBuf(RfIntf.Info.NFC_FPP.SensRes,RfIntf.Info.NFC_FPP.SensResLen); + } + break; + + case (MODE_POLL | TECH_PASSIVE_15693): + Serial.print("\tID = "); + PrintBuf(RfIntf.Info.NFC_VPP.ID,sizeof(RfIntf.Info.NFC_VPP.ID)); + + Serial.print("\ntAFI = "); + Serial.println(RfIntf.Info.NFC_VPP.AFI); + + Serial.print("\tDSFID = "); + Serial.println(RfIntf.Info.NFC_VPP.DSFID,HEX); + break; + + default: + break; + } + if(RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology + if(nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; + } + else break; + } +} + +void setup(){ + Serial.begin(9600); + while(!Serial); + Serial.println("Detect NFC tags with PN7150"); + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { //Wake up the board + Serial.println("Error while setting up the mode, check connections!"); + while (1); + } + + if (nfc.configureSettings()) { + Serial.println("The Configure Settings is failed!"); + while (1); + } + + if(nfc.configMode(mode)){ //Set up the configuration mode + Serial.println("The Configure Mode is failed!!"); + while (1); + } + nfc.startDiscovery(mode); //NCI Discovery mode + Serial.println("Waiting for an Card ..."); +} + +void loop(){ + if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + displayCardInfo(RfInterface); + switch(RfInterface.Protocol) { + case PROT_T1T: + case PROT_T2T: + case PROT_T3T: + case PROT_ISODEP: + nfc.processReaderMode(RfInterface, READ_NDEF); + break; + + case PROT_ISO15693: + break; + + case PROT_MIFARE: + nfc.processReaderMode(RfInterface, READ_NDEF); + break; + + default: + break; + } + + //* It can detect multiple cards at the same time if they use the same protocol + if(RfInterface.MoreTags) { + nfc.readerActivateNext(&RfInterface); + } + //* Wait for card removal + nfc.processReaderMode(RfInterface, PRESENCE_CHECK); + Serial.println("CARD REMOVED!"); + + nfc.stopDiscovery(); + nfc.startDiscovery(mode); + } + ResetMode(); + delay(500); +} diff --git a/examples/DetectTagsOld/Makefile b/examples/DetectTagsOld/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/DetectTagsOld/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index a534af9..ce6efb0 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -73,7 +73,7 @@ void setup() { ; } - nfc.startDiscovery(mode); + nfc.startDiscovery(); Serial.print("Waiting for an NDEF device"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 9309d0f..d5c704e 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -163,11 +163,11 @@ int Electroniccats_PN7150::GetFwVersion() { } uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { - // TODO: refactor modeSE to mode unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU : MODE_P2P); + // Update internal mode if (!Electroniccats_PN7150::setMode(modeSE)) { - return ERROR; + return ERROR; // Invalid mode, out of range } uint8_t Command[MAX_NCI_FRAME_SIZE]; @@ -828,6 +828,12 @@ bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) { } uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { + int mode = Electroniccats_PN7150::getMode(); + if (mode != modeSE) { + Electroniccats_PN7150::setMode(modeSE); + Electroniccats_PN7150::configMode(); + } + unsigned char TechTabSize = (modeSE == 1 ? sizeof(DiscoveryTechnologiesRW) : modeSE == 2 ? sizeof(DiscoveryTechnologiesCE) : sizeof(DiscoveryTechnologiesP2P)); @@ -853,7 +859,12 @@ uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { return SUCCESS; } -// Deprecated, use startDiscovery() instead +uint8_t Electroniccats_PN7150::startDiscovery() { + int mode = Electroniccats_PN7150::getMode(); + return Electroniccats_PN7150::startDiscovery(mode); +} + +// Deprecated, use startDiscovery(void) instead uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { return Electroniccats_PN7150::startDiscovery(modeSE); } @@ -1695,6 +1706,7 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { bool Electroniccats_PN7150::reset() { int mode = Electroniccats_PN7150::getMode(); + Electroniccats_PN7150::stopDiscovery(); Electroniccats_PN7150::configMode(mode); Electroniccats_PN7150::startDiscovery(mode); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 408f49c..2366669 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -269,7 +269,8 @@ class Electroniccats_PN7150 : public Mode { bool configureSettings(uint8_t *nfcuid, uint8_t uidlen); bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); // Deprecated, use configureSettings() instead uint8_t startDiscovery(uint8_t modeSE); - uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead + uint8_t startDiscovery(void); + uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery(void) instead bool stopDiscovery(); bool StopDiscovery(); // Deprecated, use stopDiscovery() instead bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); diff --git a/src/Mode.h b/src/Mode.h index 450d0f1..6ff6759 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -16,7 +16,7 @@ class Mode { int _mode; protected: - bool setMode(int mode); + bool setMode(int mode); // Only for internal use public: Mode(); From 998dee34fdda88d69e2948148e31ff546e7720ce Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 12:47:58 -0600 Subject: [PATCH 043/106] refactor: move set modes to the main class --- examples/NDEFSend/NDEFSend.ino | 10 +++++----- src/Electroniccats_PN7150.cpp | 36 ++++++++++++++++++++++++++++------ src/Electroniccats_PN7150.h | 3 +++ src/Mode.cpp | 12 ------------ src/Mode.h | 3 --- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index ce6efb0..88910a3 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -67,11 +67,11 @@ void setup() { nfc.setEmulationMode(); - if (nfc.configMode()) { - Serial.println("The Configure Mode failed!"); - while (1) - ; - } + // if (nfc.configMode()) { + // Serial.println("The Configure Mode failed!"); + // while (1) + // ; + // } nfc.startDiscovery(); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index d5c704e..e27f545 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -162,14 +162,20 @@ int Electroniccats_PN7150::GetFwVersion() { return getFirmwareVersion(); } +/// @brief Update the internal mode, stop discovery, and build the command to configure the PN7150 chip based on the input mode +/// @param modeSE +/// @return SUCCESS or ERROR uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { - unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU : MODE_P2P); - + unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU + : MODE_P2P); + // Update internal mode if (!Electroniccats_PN7150::setMode(modeSE)) { return ERROR; // Invalid mode, out of range } + Electroniccats_PN7150::stopDiscovery(); + uint8_t Command[MAX_NCI_FRAME_SIZE]; uint8_t Item = 0; @@ -1705,10 +1711,28 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { } bool Electroniccats_PN7150::reset() { - int mode = Electroniccats_PN7150::getMode(); - Electroniccats_PN7150::stopDiscovery(); - Electroniccats_PN7150::configMode(mode); - Electroniccats_PN7150::startDiscovery(mode); + if (Electroniccats_PN7150::configMode()) { + return ERROR; + } + + if (Electroniccats_PN7150::startDiscovery()) { + return ERROR; + } return SUCCESS; } + +void Electroniccats_PN7150::setReaderWriterMode() { + Electroniccats_PN7150::setMode(mode.READER_WRITER); + Electroniccats_PN7150::reset(); +} + +void Electroniccats_PN7150::setEmulationMode() { + Electroniccats_PN7150::setMode(mode.EMULATION); + Electroniccats_PN7150::reset(); +} + +void Electroniccats_PN7150::setP2PMode() { + Electroniccats_PN7150::setMode(mode.P2P); + Electroniccats_PN7150::reset(); +} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 2366669..e2b6906 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -306,6 +306,9 @@ class Electroniccats_PN7150 : public Mode { bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead bool reset(); + void setReaderWriterMode(); + void setEmulationMode(); + void setP2PMode(); }; #endif diff --git a/src/Mode.cpp b/src/Mode.cpp index d40ac43..cd4c174 100644 --- a/src/Mode.cpp +++ b/src/Mode.cpp @@ -13,18 +13,6 @@ bool Mode::setMode(int mode) { return true; } -void Mode::setReaderWriterMode() { - this->_mode = mode.READER_WRITER; -} - -void Mode::setEmulationMode() { - this->_mode = mode.EMULATION; -} - -void Mode::setP2PMode() { - this->_mode = mode.P2P; -} - int Mode::getMode() { return this->_mode; } diff --git a/src/Mode.h b/src/Mode.h index 6ff6759..319c5cf 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -21,9 +21,6 @@ class Mode { public: Mode(); Mode_t mode; - void setReaderWriterMode(); - void setEmulationMode(); - void setP2PMode(); int getMode(); }; From e3f1d709095a72cce11c600de47d4a63c9c81eb8 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 15:13:23 -0600 Subject: [PATCH 044/106] fix: use true instead of success --- examples/NDEFSend/NDEFSend.ino | 15 +++++++-------- src/Electroniccats_PN7150.cpp | 27 ++++++++++++++++++--------- src/Electroniccats_PN7150.h | 6 +++--- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 88910a3..a8da1e0 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -11,8 +11,6 @@ void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSi Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 RfIntf_t RfInterface; // Interface to save data for multiple tags -uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation, 3 = Peer to peer P2P - unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; const char uri[] = "google.com"; @@ -65,17 +63,18 @@ void setup() { ; } + // Needed to detect readers nfc.setEmulationMode(); - // if (nfc.configMode()) { - // Serial.println("The Configure Mode failed!"); - // while (1) - // ; - // } + if (nfc.configMode()) { + Serial.println("The Configure Mode failed!"); + while (1) + ; + } nfc.startDiscovery(); - Serial.print("Waiting for an NDEF device"); + Serial.println("Waiting for an NDEF device"); } void loop() { diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index e27f545..bc56d7c 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1712,27 +1712,36 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { bool Electroniccats_PN7150::reset() { if (Electroniccats_PN7150::configMode()) { - return ERROR; + return false; } if (Electroniccats_PN7150::startDiscovery()) { - return ERROR; + return false; } - return SUCCESS; + return true; } -void Electroniccats_PN7150::setReaderWriterMode() { +bool Electroniccats_PN7150::setReaderWriterMode() { Electroniccats_PN7150::setMode(mode.READER_WRITER); - Electroniccats_PN7150::reset(); + if (!Electroniccats_PN7150::reset()) { + return false; + } + return true; } -void Electroniccats_PN7150::setEmulationMode() { +bool Electroniccats_PN7150::setEmulationMode() { Electroniccats_PN7150::setMode(mode.EMULATION); - Electroniccats_PN7150::reset(); + if (!Electroniccats_PN7150::reset()) { + return false; + } + return true; } -void Electroniccats_PN7150::setP2PMode() { +bool Electroniccats_PN7150::setP2PMode() { Electroniccats_PN7150::setMode(mode.P2P); - Electroniccats_PN7150::reset(); + if (!Electroniccats_PN7150::reset()) { + return false; + } + return true; } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index e2b6906..c20ed5c 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -306,9 +306,9 @@ class Electroniccats_PN7150 : public Mode { bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead bool reset(); - void setReaderWriterMode(); - void setEmulationMode(); - void setP2PMode(); + bool setReaderWriterMode(); + bool setEmulationMode(); + bool setP2PMode(); }; #endif From 7a723aba2d764bbdefa9213b155464dea829b77b Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 17:41:12 -0600 Subject: [PATCH 045/106] feat: add remote device struct pointer --- examples/NDEFReceive/NDEFReceive.ino | 22 ++++++++++++++++++++++ src/Electroniccats_PN7150.cpp | 1 + src/Electroniccats_PN7150.h | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 6f254ea..50c45b6 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -211,6 +211,28 @@ void displayDeviceInfo() { break; } + Serial.print("Interface: "); + switch (nfc.remoteDevice->Interface) { + case INTF_ISODEP: + Serial.println("ISO-DEP"); + break; + case INTF_NFCDEP: + Serial.println("NFC-DEP"); + break; + case INTF_TAGCMD: + Serial.println("TAG"); + break; + case INTF_FRAME: + Serial.println("FRAME"); + break; + case INTF_UNDETERMINED: + Serial.println("UNDETERMINED"); + break; + default: + Serial.println("UNKNOWN"); + break; + } + Serial.print("Protocol: "); switch (RfInterface.Protocol) { case PROT_UNDETERMINED: diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index bc56d7c..2536a7b 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -892,6 +892,7 @@ bool Electroniccats_PN7150::StopDiscovery() { bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; + this->remoteDevice = pRfIntf; // P2P Support uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index c20ed5c..e1290c7 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -254,7 +254,7 @@ class Electroniccats_PN7150 : public Mode { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); - // Mode mode; + RfIntf_t *remoteDevice; uint8_t begin(void); bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) From be2f6d4e6b72060f10774082bd6a68fc6dfb68bf Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 9 Aug 2023 19:03:29 -0600 Subject: [PATCH 046/106] feat: add get interface type function --- examples/NDEFReceive/NDEFReceive.ino | 2 +- src/Electroniccats_PN7150.cpp | 4 ++ src/Electroniccats_PN7150.h | 60 ++----------------------- src/RemoteDevice.h | 66 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 58 deletions(-) create mode 100644 src/RemoteDevice.h diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 50c45b6..c219028 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -212,7 +212,7 @@ void displayDeviceInfo() { } Serial.print("Interface: "); - switch (nfc.remoteDevice->Interface) { + switch (nfc.getInterfaceType()) { case INTF_ISODEP: Serial.println("ISO-DEP"); break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 2536a7b..6d5c4f0 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1746,3 +1746,7 @@ bool Electroniccats_PN7150::setP2PMode() { } return true; } + +unsigned char Electroniccats_PN7150::getInterfaceType() { + return this->remoteDevice->Interface; +} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index e1290c7..91fe522 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -20,12 +20,12 @@ #include // Gives us access to all typical Arduino types and functions // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library -// #include "RW_NDEF.h" #include "P2P_NDEF.h" #include "RW_NDEF.h" #include "T4T_NDEF_emu.h" #include "ndef_helper.h" #include "Mode.h" +#include "RemoteDevice.h" // #define DEBUG3 @@ -125,60 +125,6 @@ typedef enum { BR_848 } NxpNci_Bitrate_t; #endif -/* - * Definition of discovered remote device properties information - */ - -/* POLL passive type A */ -struct RfIntf_info_APP_t { - unsigned char SensRes[2]; - unsigned char NfcIdLen; - unsigned char NfcId[10]; - unsigned char SelResLen; - unsigned char SelRes[1]; - unsigned char RatsLen; - unsigned char Rats[20]; -}; - -/* POLL passive type B */ -struct RfIntf_info_BPP_t { - unsigned char SensResLen; - unsigned char SensRes[12]; - unsigned char AttribResLen; - unsigned char AttribRes[17]; -}; - -/* POLL passive type F */ -struct RfIntf_info_FPP_t { - unsigned char BitRate; - unsigned char SensResLen; - unsigned char SensRes[18]; -}; - -/* POLL passive type ISO15693 */ -struct RfIntf_info_VPP_t { - unsigned char AFI; - unsigned char DSFID; - unsigned char ID[8]; -}; - -typedef union { - RfIntf_info_APP_t NFC_APP; - RfIntf_info_BPP_t NFC_BPP; - RfIntf_info_FPP_t NFC_FPP; - RfIntf_info_VPP_t NFC_VPP; -} RfIntf_Info_t; - -/* - * Definition of discovered remote device properties - */ -struct RfIntf_t { - unsigned char Interface; - unsigned char Protocol; - unsigned char ModeTech; - bool MoreTags; - RfIntf_Info_t Info; -}; /* * Definition of operations handled when processing Reader mode @@ -238,7 +184,7 @@ typedef union { NxpNci_RfIntf_info_VPP_t NFC_VPP; } NxpNci_RfIntf_Info_t; -class Electroniccats_PN7150 : public Mode { +class Electroniccats_PN7150 : public Mode, public RemoteDevice { private: uint8_t _IRQpin, _VENpin, _I2Caddress; TwoWire *_wire; @@ -254,7 +200,6 @@ class Electroniccats_PN7150 : public Mode { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); - RfIntf_t *remoteDevice; uint8_t begin(void); bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) @@ -309,6 +254,7 @@ class Electroniccats_PN7150 : public Mode { bool setReaderWriterMode(); bool setEmulationMode(); bool setP2PMode(); + unsigned char getInterfaceType(); }; #endif diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h new file mode 100644 index 0000000..6731717 --- /dev/null +++ b/src/RemoteDevice.h @@ -0,0 +1,66 @@ +#ifndef RemoteDevice_H +#define RemoteDevice_H + +#include "Arduino.h" + +/* + * Definition of discovered remote device properties information + */ + +/* POLL passive type A */ +struct RfIntf_info_APP_t { + unsigned char SensRes[2]; + unsigned char NfcIdLen; + unsigned char NfcId[10]; + unsigned char SelResLen; + unsigned char SelRes[1]; + unsigned char RatsLen; + unsigned char Rats[20]; +}; + +/* POLL passive type B */ +struct RfIntf_info_BPP_t { + unsigned char SensResLen; + unsigned char SensRes[12]; + unsigned char AttribResLen; + unsigned char AttribRes[17]; +}; + +/* POLL passive type F */ +struct RfIntf_info_FPP_t { + unsigned char BitRate; + unsigned char SensResLen; + unsigned char SensRes[18]; +}; + +/* POLL passive type ISO15693 */ +struct RfIntf_info_VPP_t { + unsigned char AFI; + unsigned char DSFID; + unsigned char ID[8]; +}; + +typedef union { + RfIntf_info_APP_t NFC_APP; + RfIntf_info_BPP_t NFC_BPP; + RfIntf_info_FPP_t NFC_FPP; + RfIntf_info_VPP_t NFC_VPP; +} RfIntf_Info_t; + +/* + * Definition of discovered remote device properties + */ +struct RfIntf_t { + unsigned char Interface; + unsigned char Protocol; + unsigned char ModeTech; + bool MoreTags; + RfIntf_Info_t Info; +}; + +class RemoteDevice { + protected: + RfIntf_t *remoteDevice; +}; + +#endif \ No newline at end of file From 6ff2653c2e3131e83f66d2e03490752e6c077fb1 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 10 Aug 2023 12:04:49 -0600 Subject: [PATCH 047/106] feat: add remote device attributes as camel case --- examples/NDEFReceive/NDEFReceive.ino | 2 +- src/Electroniccats_PN7150.cpp | 110 +++++++++++++++++++++++++-- src/Electroniccats_PN7150.h | 6 +- src/RemoteDevice.h | 53 ++++++++++++- 4 files changed, 158 insertions(+), 13 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index c219028..d22f8ce 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -212,7 +212,7 @@ void displayDeviceInfo() { } Serial.print("Interface: "); - switch (nfc.getInterfaceType()) { + switch (nfc.remoteDevice.interface) { case INTF_ISODEP: Serial.println("ISO-DEP"); break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 6d5c4f0..e06b918 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -892,7 +892,6 @@ bool Electroniccats_PN7150::StopDiscovery() { bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; - this->remoteDevice = pRfIntf; // P2P Support uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; @@ -911,10 +910,15 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* Is RF_INTF_ACTIVATED_NTF ? */ if (rxBuffer[1] == 0x05) { pRfIntf->Interface = rxBuffer[4]; + this->remoteDevice.interface = rxBuffer[4]; pRfIntf->Protocol = rxBuffer[5]; + this->remoteDevice.protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; + this->remoteDevice.modeTech = rxBuffer[6]; pRfIntf->MoreTags = false; + this->remoteDevice.moreTags = false; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(&rxBuffer[10]); // P2P /* Verifying if not a P2P device also presenting T4T emulation */ @@ -937,10 +941,15 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* Is P2P detected ? */ if (rxBuffer[5] == PROT_NFCDEP) { pRfIntf->Interface = rxBuffer[4]; + this->remoteDevice.interface = rxBuffer[4]; pRfIntf->Protocol = rxBuffer[5]; + this->remoteDevice.protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; + this->remoteDevice.modeTech = rxBuffer[6]; pRfIntf->MoreTags = false; + this->remoteDevice.moreTags = false; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(&rxBuffer[10]); break; } } else { @@ -960,9 +969,13 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } } else { /* RF_DISCOVER_NTF */ pRfIntf->Interface = INTF_UNDETERMINED; + this->remoteDevice.interface = INTF_UNDETERMINED; pRfIntf->Protocol = rxBuffer[4]; + this->remoteDevice.protocol = rxBuffer[4]; pRfIntf->ModeTech = rxBuffer[5]; + this->remoteDevice.modeTech = rxBuffer[5]; pRfIntf->MoreTags = true; + this->remoteDevice.moreTags = true; /* Get next NTF for further activation */ do { @@ -996,9 +1009,13 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { pRfIntf->Interface = rxBuffer[4]; + this->remoteDevice.interface = rxBuffer[4]; pRfIntf->Protocol = rxBuffer[5]; + this->remoteDevice.protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; + this->remoteDevice.modeTech = rxBuffer[6]; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(&rxBuffer[10]); } /* In case of P2P target detected but lost, inform application to restart discovery */ @@ -1017,8 +1034,10 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } /* In case of unknown target align protocol information */ - if (pRfIntf->Interface == INTF_UNDETERMINED) + if (pRfIntf->Interface == INTF_UNDETERMINED) { pRfIntf->Protocol = PROT_UNDETERMINED; + this->remoteDevice.protocol = PROT_UNDETERMINED; + } return SUCCESS; } @@ -1468,7 +1487,88 @@ void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) } } -// Deprecated, use fillInterfaceInfo() instead +void Electroniccats_PN7150::fillInterfaceInfo(uint8_t *pBuf) { + uint8_t i, temp; + + switch (this->remoteDevice.modeTech) { + case (MODE_POLL | TECH_PASSIVE_NFCA): + // memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); + memcpy(this->remoteDevice.info.nfcAPP.sensRes, &pBuf[0], 2); + temp = 2; + // pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; + this->remoteDevice.info.nfcAPP.nfcIdLen = pBuf[temp]; + temp++; + // memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); + memcpy(this->remoteDevice.info.nfcAPP.nfcId, &pBuf[3], this->remoteDevice.info.nfcAPP.nfcIdLen); + temp += pBuf[2]; + // pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; + this->remoteDevice.info.nfcAPP.selResLen = pBuf[temp]; + temp++; + + if (this->remoteDevice.info.nfcAPP.selResLen == 1) { + // pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; + this->remoteDevice.info.nfcAPP.selRes[0] = pBuf[temp]; + } + + temp += 4; + if (pBuf[temp] != 0) { + temp++; + // pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; + this->remoteDevice.info.nfcAPP.ratsLen = pBuf[temp]; + // memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); + memcpy(this->remoteDevice.info.nfcAPP.rats, &pBuf[temp + 1], pBuf[temp]); + } else { + // pRfIntf->Info.NFC_APP.RatsLen = 0; + this->remoteDevice.info.nfcAPP.ratsLen = 0; + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCB): + // pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; + this->remoteDevice.info.nfcBPP.sensResLen = pBuf[0]; + // memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); + memcpy(this->remoteDevice.info.nfcBPP.sensRes, &pBuf[1], this->remoteDevice.info.nfcBPP.sensResLen); + temp = pBuf[0] + 4; + if (pBuf[temp] != 0) { + temp++; + // pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; + this->remoteDevice.info.nfcBPP.attribResLen = pBuf[temp]; + // memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); + memcpy(this->remoteDevice.info.nfcBPP.attribRes, &pBuf[temp + 1], pBuf[temp]); + } else { + // pRfIntf->Info.NFC_BPP.AttribResLen = 0; + this->remoteDevice.info.nfcBPP.attribResLen = 0; + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCF): + // pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; + this->remoteDevice.info.nfcFPP.bitRate = pBuf[0]; + // pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; + this->remoteDevice.info.nfcFPP.sensResLen = pBuf[1]; + // memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); + memcpy(this->remoteDevice.info.nfcFPP.sensRes, &pBuf[2], this->remoteDevice.info.nfcFPP.sensResLen); + break; + + case (MODE_POLL | TECH_PASSIVE_15693): + // pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; + this->remoteDevice.info.nfcVPP.afi = pBuf[0]; + // pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; + this->remoteDevice.info.nfcVPP.dsfid = pBuf[1]; + + for (i = 0; i < 8; i++) { + // pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; + this->remoteDevice.info.nfcVPP.id[7 - i] = pBuf[2 + i]; + } + + break; + + default: + break; + } +} + +// Deprecated, use fillInterfaceInfo(uint8_t *pBuf) instead void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); } @@ -1746,7 +1846,3 @@ bool Electroniccats_PN7150::setP2PMode() { } return true; } - -unsigned char Electroniccats_PN7150::getInterfaceType() { - return this->remoteDevice->Interface; -} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 91fe522..7af93d1 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -197,6 +197,9 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length uint8_t gNfcController_generation = 0; uint8_t gNfcController_fw_version[3] = {0}; + void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); + void fillInterfaceInfo(uint8_t *pBuf); + void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); // Deprecated, use fillInterfaceInfo(uint8_t *pBuf) instead public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); @@ -234,8 +237,6 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead void presenceCheck(RfIntf_t RfIntf); void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use presenceCheck() instead - void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); - void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); // Deprecated, use fillInterfaceInfo() instead bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead bool readerReActivate(RfIntf_t *pRfIntf); @@ -254,7 +255,6 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { bool setReaderWriterMode(); bool setEmulationMode(); bool setP2PMode(); - unsigned char getInterfaceType(); }; #endif diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 6731717..bf8b72c 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -18,6 +18,17 @@ struct RfIntf_info_APP_t { unsigned char Rats[20]; }; +/* POLL passive type A camelCase */ +struct RfIntfInfoAppCC_t { + unsigned char sensRes[2]; + unsigned char nfcIdLen; + unsigned char nfcId[10]; + unsigned char selResLen; + unsigned char selRes[1]; + unsigned char ratsLen; + unsigned char rats[20]; +}; + /* POLL passive type B */ struct RfIntf_info_BPP_t { unsigned char SensResLen; @@ -26,6 +37,14 @@ struct RfIntf_info_BPP_t { unsigned char AttribRes[17]; }; +/* POLL passive type B camelCase */ +struct RfIntfInfoBppCC_t { + unsigned char sensResLen; + unsigned char sensRes[12]; + unsigned char attribResLen; + unsigned char attribRes[17]; +}; + /* POLL passive type F */ struct RfIntf_info_FPP_t { unsigned char BitRate; @@ -33,6 +52,13 @@ struct RfIntf_info_FPP_t { unsigned char SensRes[18]; }; +/* POLL passive type F camelCase */ +struct RfIntfInfoFppCC_t { + unsigned char bitRate; + unsigned char sensResLen; + unsigned char sensRes[18]; +}; + /* POLL passive type ISO15693 */ struct RfIntf_info_VPP_t { unsigned char AFI; @@ -40,6 +66,13 @@ struct RfIntf_info_VPP_t { unsigned char ID[8]; }; +/* POLL passive type ISO15693 camelCase */ +struct RfIntfInfoVppCC_t { + unsigned char afi; + unsigned char dsfid; + unsigned char id[8]; +}; + typedef union { RfIntf_info_APP_t NFC_APP; RfIntf_info_BPP_t NFC_BPP; @@ -47,6 +80,13 @@ typedef union { RfIntf_info_VPP_t NFC_VPP; } RfIntf_Info_t; +typedef union { + RfIntfInfoAppCC_t nfcAPP; + RfIntfInfoBppCC_t nfcBPP; + RfIntfInfoFppCC_t nfcFPP; + RfIntfInfoVppCC_t nfcVPP; +} RfIntfInfoCC_t; + /* * Definition of discovered remote device properties */ @@ -58,9 +98,18 @@ struct RfIntf_t { RfIntf_Info_t Info; }; +// Definition of discovered remote device properties using camelCase +struct RfIntfCC_t { + unsigned char interface; + unsigned char protocol; + unsigned char modeTech; + bool moreTags; + RfIntfInfoCC_t info; +}; + class RemoteDevice { - protected: - RfIntf_t *remoteDevice; + public: + RfIntfCC_t remoteDevice; }; #endif \ No newline at end of file From 76651acbdb7895fa17b8e2433b05148283892e45 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 10 Aug 2023 15:54:56 -0600 Subject: [PATCH 048/106] feat: add read ndef message function --- examples/NDEFReceive/NDEFReceive.ino | 155 ++++----------------------- src/Electroniccats_PN7150.cpp | 14 ++- src/Electroniccats_PN7150.h | 9 +- 3 files changed, 41 insertions(+), 137 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index d22f8ce..b5924ad 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -4,9 +4,7 @@ #define PN7150_ADDR (0x28) Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Intarface to save data for multiple tags - -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation +RfIntf_t RfIntf; // Intarface to save data for multiple tags const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 0x01, // Type length (1 byte) @@ -16,12 +14,6 @@ const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 'e', 'n', // Language 'H', 'e', 'l', 'l', 'o'}; // Message Payload -void ResetMode() { // Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.configMode(mode); - nfc.startDiscovery(mode); -} - void PrintBuf(const byte *data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; for (szPos = 0; szPos < numBytes; szPos++) { @@ -37,87 +29,6 @@ void PrintBuf(const byte *data, const uint32_t numBytes) { // Print hex data bu Serial.println(); } -void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field - char tmp[16]; - while (1) { - switch (RfIntf.Protocol) { // Indetify card protocol - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: - Serial.print(" - POLL MODE: Remote activated tag type: "); - Serial.println(RfIntf.Protocol); - break; - case PROT_ISO15693: - Serial.println(" - POLL MODE: Remote ISO15693 card activated"); - break; - case PROT_MIFARE: - Serial.println(" - POLL MODE: Remote MIFARE card activated"); - break; - default: - Serial.println(" - POLL MODE: Undetermined target"); - return; - } - - switch (RfIntf.ModeTech) { // Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); - Serial.print(" "); - sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); - Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); - - if (RfIntf.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); - Serial.println(" "); - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCB): - if (RfIntf.Info.NFC_BPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_BPP.SensRes, RfIntf.Info.NFC_BPP.SensResLen); - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCF): - Serial.print("\tBitrate = "); - Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); - - if (RfIntf.Info.NFC_FPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_FPP.SensRes, RfIntf.Info.NFC_FPP.SensResLen); - } - break; - - case (MODE_POLL | TECH_PASSIVE_15693): - Serial.print("\tID = "); - PrintBuf(RfIntf.Info.NFC_VPP.ID, sizeof(RfIntf.Info.NFC_VPP.ID)); - - Serial.print("\ntAFI = "); - Serial.println(RfIntf.Info.NFC_VPP.AFI); - - Serial.print("\tDSFID = "); - Serial.println(RfIntf.Info.NFC_VPP.DSFID, HEX); - break; - - default: - break; - } - if (RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology - if (nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; - } else - break; - } -} - void setup() { Serial.begin(9600); while (!Serial) @@ -139,78 +50,58 @@ void setup() { ; } - if (nfc.configMode(mode)) { // Set up the configuration mode + nfc.setReaderWriterMode(); + + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1) ; } - nfc.startDiscovery(mode); // NCI Discovery mode - Serial.println("Waiting for an Card ..."); + nfc.startDiscovery(); // NCI Discovery mode + Serial.println("Waiting for a Card..."); } void loop() { - if (!nfc.waitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards - displayCardInfo(RfInterface); + if (!nfc.waitForDiscoveryNotification(&RfIntf)) { // Waiting to detect cards displayDeviceInfo(); - switch (RfInterface.Protocol) { + switch (nfc.remoteDevice.protocol) { case PROT_T1T: case PROT_T2T: case PROT_T3T: case PROT_ISODEP: - nfc.processReaderMode(RfInterface, READ_NDEF); + // nfc.processReaderMode(RfIntf, READ_NDEF); // TODO: update it to use the new API + nfc.readNdefMessage(); break; case PROT_ISO15693: break; case PROT_MIFARE: - nfc.processReaderMode(RfInterface, READ_NDEF); + // nfc.processReaderMode(RfIntf, READ_NDEF); + nfc.readNdefMessage(); break; default: break; } - //* It can detect multiple cards at the same time if they use the same protocol - if (RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + // It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.moreTags) { + nfc.readerActivateNext(&RfIntf); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("CARD REMOVED!"); - - nfc.stopDiscovery(); - nfc.startDiscovery(mode); + // Wait for card removal + Serial.println("Remove the Card"); + nfc.processReaderMode(RfIntf, PRESENCE_CHECK); + Serial.println("Card removed!"); } - // ResetMode(); + Serial.println("Restarting..."); nfc.reset(); + Serial.println("Waiting for a Card..."); delay(500); } void displayDeviceInfo() { Serial.println(); - Serial.print("RfInterface: "); - switch (RfInterface.Interface) { - case INTF_ISODEP: - Serial.println("ISO-DEP"); - break; - case INTF_NFCDEP: - Serial.println("NFC-DEP"); - break; - case INTF_TAGCMD: - Serial.println("TAG"); - break; - case INTF_FRAME: - Serial.println("FRAME"); - break; - case INTF_UNDETERMINED: - Serial.println("UNDETERMINED"); - break; - default: - Serial.println("UNKNOWN"); - break; - } - Serial.print("Interface: "); switch (nfc.remoteDevice.interface) { case INTF_ISODEP: @@ -234,7 +125,7 @@ void displayDeviceInfo() { } Serial.print("Protocol: "); - switch (RfInterface.Protocol) { + switch (nfc.remoteDevice.protocol) { case PROT_UNDETERMINED: Serial.println("UNDETERMINED"); break; @@ -265,7 +156,7 @@ void displayDeviceInfo() { } Serial.print("Mode: "); - switch (RfInterface.ModeTech) { + switch (nfc.remoteDevice.modeTech) { case MODE_POLL: Serial.println("POLL"); break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index e06b918..e5f6b8c 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1042,6 +1042,10 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint return SUCCESS; } +bool Electroniccats_PN7150::waitForDiscoveryNotification(uint8_t tout) { + return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfIntf, tout); +} + // Deprecated, use waitForDiscoveryNotification() instead bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); @@ -1690,7 +1694,8 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; - RW_NDEF_Reset(RfIntf.Protocol); + // RW_NDEF_Reset(RfIntf.Protocol); + RW_NDEF_Reset(this->remoteDevice.protocol); while (1) { RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); @@ -1708,7 +1713,8 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { getMessage(1000); // Manage chaining in case of T4T - if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { + // if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { + if (this->remoteDevice.interface == INTF_ISODEP && rxBuffer[0] == 0x10) { uint8_t tmp[MAX_NCI_FRAME_SIZE]; uint8_t tmpSize = 0; while (rxBuffer[0] == 0x10) { @@ -1726,6 +1732,10 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { } } +void Electroniccats_PN7150::readNdefMessage(void) { + Electroniccats_PN7150::readNdef(this->dummyRfIntf); +} + // Deprecated, use readNdef() instead void Electroniccats_PN7150::ReadNdef(RfIntf_t RfIntf) { Electroniccats_PN7150::readNdef(RfIntf); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 7af93d1..be7720b 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -20,12 +20,12 @@ #include // Gives us access to all typical Arduino types and functions // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library +#include "Mode.h" #include "P2P_NDEF.h" #include "RW_NDEF.h" +#include "RemoteDevice.h" #include "T4T_NDEF_emu.h" #include "ndef_helper.h" -#include "Mode.h" -#include "RemoteDevice.h" // #define DEBUG3 @@ -188,6 +188,7 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { private: uint8_t _IRQpin, _VENpin, _I2Caddress; TwoWire *_wire; + RfIntf_t dummyRfIntf; uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command bool isTimeOut() const; @@ -222,6 +223,7 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { bool stopDiscovery(); bool StopDiscovery(); // Deprecated, use stopDiscovery() instead bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); + bool waitForDiscoveryNotification(uint8_t tout = 0); bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); // Deprecated, use waitForDiscoveryNotification() instead uint8_t connectNCI(); uint8_t wakeupNCI(); @@ -244,7 +246,8 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { bool readerActivateNext(RfIntf_t *pRfIntf); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead void readNdef(RfIntf_t RfIntf); - void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdef() instead + void readNdefMessage(); + void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead void writeNdef(RfIntf_t RfIntf); void WriteNdef(RfIntf_t RfIntf); // Deprecated, use writeNdef() instead bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); From 4786bdecba7b30aa90be4ec9f4493c8ae66ce76e Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 10 Aug 2023 16:06:30 -0600 Subject: [PATCH 049/106] feat: add write ndef message function --- examples/NDEFReceive/NDEFReceive.ino | 8 ++++---- src/Electroniccats_PN7150.cpp | 15 +++++++++------ src/Electroniccats_PN7150.h | 3 ++- src/RemoteDevice.h | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index b5924ad..31d01c6 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -4,7 +4,7 @@ #define PN7150_ADDR (0x28) Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfIntf; // Intarface to save data for multiple tags +RfIntf_t RfIntf; // Intarface to save data for multiple tags const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 0x01, // Type length (1 byte) @@ -86,7 +86,7 @@ void loop() { } // It can detect multiple cards at the same time if they use the same protocol - if (nfc.remoteDevice.moreTags) { + if (nfc.remoteDevice.hasMoreTags) { nfc.readerActivateNext(&RfIntf); } // Wait for card removal @@ -203,7 +203,7 @@ void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("Text record: "); // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; @@ -212,7 +212,7 @@ void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("URI record: "); // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); - Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); + Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index e5f6b8c..f61f604 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -916,7 +916,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->ModeTech = rxBuffer[6]; this->remoteDevice.modeTech = rxBuffer[6]; pRfIntf->MoreTags = false; - this->remoteDevice.moreTags = false; + this->remoteDevice.hasMoreTags = false; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); fillInterfaceInfo(&rxBuffer[10]); @@ -947,7 +947,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->ModeTech = rxBuffer[6]; this->remoteDevice.modeTech = rxBuffer[6]; pRfIntf->MoreTags = false; - this->remoteDevice.moreTags = false; + this->remoteDevice.hasMoreTags = false; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); fillInterfaceInfo(&rxBuffer[10]); break; @@ -975,7 +975,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->ModeTech = rxBuffer[5]; this->remoteDevice.modeTech = rxBuffer[5]; pRfIntf->MoreTags = true; - this->remoteDevice.moreTags = true; + this->remoteDevice.hasMoreTags = true; /* Get next NTF for further activation */ do { @@ -1694,7 +1694,6 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; - // RW_NDEF_Reset(RfIntf.Protocol); RW_NDEF_Reset(this->remoteDevice.protocol); while (1) { @@ -1713,7 +1712,6 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { getMessage(1000); // Manage chaining in case of T4T - // if ((RfIntf.Interface = INTF_ISODEP) && rxBuffer[0] == 0x10) { if (this->remoteDevice.interface == INTF_ISODEP && rxBuffer[0] == 0x10) { uint8_t tmp[MAX_NCI_FRAME_SIZE]; uint8_t tmpSize = 0; @@ -1745,7 +1743,7 @@ void Electroniccats_PN7150::writeNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; - RW_NDEF_Reset(RfIntf.Protocol); + RW_NDEF_Reset(this->remoteDevice.protocol); while (1) { RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); @@ -1765,6 +1763,11 @@ void Electroniccats_PN7150::writeNdef(RfIntf_t RfIntf) { } } +void Electroniccats_PN7150::writeNdefMessage(void) { + Electroniccats_PN7150::writeNdef(this->dummyRfIntf); +} + +// Deprecated, use writeNdefMessage() instead void Electroniccats_PN7150::WriteNdef(RfIntf_t RfIntf) { Electroniccats_PN7150::writeNdef(RfIntf); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index be7720b..df38246 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -249,7 +249,8 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { void readNdefMessage(); void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead void writeNdef(RfIntf_t RfIntf); - void WriteNdef(RfIntf_t RfIntf); // Deprecated, use writeNdef() instead + void writeNdefMessage(); + void WriteNdef(RfIntf_t RfIntf); // Deprecated, use writeNdefMessage() instead bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); bool NxpNci_FactoryTest_Prbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); // Deprecated, use nciFactoryTestPrbs() instead bool nciFactoryTestRfOn(); diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index bf8b72c..d4c18d0 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -103,7 +103,7 @@ struct RfIntfCC_t { unsigned char interface; unsigned char protocol; unsigned char modeTech; - bool moreTags; + bool hasMoreTags; RfIntfInfoCC_t info; }; From 6cd633351aeb9729645fde6df9f2c4de3a2b6cf0 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 10 Aug 2023 16:20:22 -0600 Subject: [PATCH 050/106] feat: add activate next tag discovery --- examples/NDEFReceive/NDEFReceive.ino | 5 +++-- src/Electroniccats_PN7150.cpp | 23 +++++++++++++++++------ src/Electroniccats_PN7150.h | 5 +++-- src/RemoteDevice.h | 2 +- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 31d01c6..01aa25a 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -86,8 +86,9 @@ void loop() { } // It can detect multiple cards at the same time if they use the same protocol - if (nfc.remoteDevice.hasMoreTags) { - nfc.readerActivateNext(&RfIntf); + if (nfc.remoteDevice.moreTagsAvailable) { + nfc.activateNextTagDiscovery(); + Serial.println("Multiple cards are detected!"); } // Wait for card removal Serial.println("Remove the Card"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index f61f604..2f0307e 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -916,7 +916,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->ModeTech = rxBuffer[6]; this->remoteDevice.modeTech = rxBuffer[6]; pRfIntf->MoreTags = false; - this->remoteDevice.hasMoreTags = false; + this->remoteDevice.moreTagsAvailable = false; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); fillInterfaceInfo(&rxBuffer[10]); @@ -947,7 +947,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->ModeTech = rxBuffer[6]; this->remoteDevice.modeTech = rxBuffer[6]; pRfIntf->MoreTags = false; - this->remoteDevice.hasMoreTags = false; + this->remoteDevice.moreTagsAvailable = false; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); fillInterfaceInfo(&rxBuffer[10]); break; @@ -975,7 +975,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint pRfIntf->ModeTech = rxBuffer[5]; this->remoteDevice.modeTech = rxBuffer[5]; pRfIntf->MoreTags = true; - this->remoteDevice.hasMoreTags = true; + this->remoteDevice.moreTagsAvailable = true; /* Get next NTF for further activation */ do { @@ -1043,7 +1043,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } bool Electroniccats_PN7150::waitForDiscoveryNotification(uint8_t tout) { - return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfIntf, tout); + return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfInterface, tout); } // Deprecated, use waitForDiscoveryNotification() instead @@ -1640,10 +1640,13 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { bool status = ERROR; pRfIntf->MoreTags = false; + this->remoteDevice.moreTagsAvailable = false; if (gNextTag_Protocol == PROT_UNDETERMINED) { pRfIntf->Interface = INTF_UNDETERMINED; + this->remoteDevice.interface = INTF_UNDETERMINED; pRfIntf->Protocol = PROT_UNDETERMINED; + this->remoteDevice.protocol = PROT_UNDETERMINED; return ERROR; } @@ -1675,9 +1678,13 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { getMessage(100); if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { pRfIntf->Interface = rxBuffer[4]; + this->remoteDevice.interface = rxBuffer[4]; pRfIntf->Protocol = rxBuffer[5]; + this->remoteDevice.protocol = rxBuffer[5]; pRfIntf->ModeTech = rxBuffer[6]; + this->remoteDevice.modeTech = rxBuffer[6]; fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + fillInterfaceInfo(&rxBuffer[10]); status = SUCCESS; } } @@ -1685,6 +1692,10 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { return status; } +bool Electroniccats_PN7150::activateNextTagDiscovery() { + return Electroniccats_PN7150::readerActivateNext(&this->dummyRfInterface); +} + // Deprecated, use readerActivateNext() instead bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { return Electroniccats_PN7150::readerActivateNext(pRfIntf); @@ -1731,7 +1742,7 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { } void Electroniccats_PN7150::readNdefMessage(void) { - Electroniccats_PN7150::readNdef(this->dummyRfIntf); + Electroniccats_PN7150::readNdef(this->dummyRfInterface); } // Deprecated, use readNdef() instead @@ -1764,7 +1775,7 @@ void Electroniccats_PN7150::writeNdef(RfIntf_t RfIntf) { } void Electroniccats_PN7150::writeNdefMessage(void) { - Electroniccats_PN7150::writeNdef(this->dummyRfIntf); + Electroniccats_PN7150::writeNdef(this->dummyRfInterface); } // Deprecated, use writeNdefMessage() instead diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index df38246..64f5d3b 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -188,7 +188,7 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { private: uint8_t _IRQpin, _VENpin, _I2Caddress; TwoWire *_wire; - RfIntf_t dummyRfIntf; + RfIntf_t dummyRfInterface; uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command bool isTimeOut() const; @@ -244,7 +244,8 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { bool readerReActivate(RfIntf_t *pRfIntf); bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); - bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use readerActivateNext() instead + bool activateNextTagDiscovery(); + bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead void readNdef(RfIntf_t RfIntf); void readNdefMessage(); void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index d4c18d0..0100d40 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -103,7 +103,7 @@ struct RfIntfCC_t { unsigned char interface; unsigned char protocol; unsigned char modeTech; - bool hasMoreTags; + bool moreTagsAvailable; RfIntfInfoCC_t info; }; From 7d183db9e160038505b8a1f6d62d04b09a69e3f5 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 10 Aug 2023 16:46:08 -0600 Subject: [PATCH 051/106] feat: add wait for tag removal --- examples/NDEFReceive/NDEFReceive.ino | 15 ++++++--------- src/Electroniccats_PN7150.cpp | 13 +++++++++---- src/Electroniccats_PN7150.h | 3 ++- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 01aa25a..f06c1d7 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -4,7 +4,6 @@ #define PN7150_ADDR (0x28) Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfIntf; // Intarface to save data for multiple tags const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 0x01, // Type length (1 byte) @@ -40,13 +39,13 @@ void setup() { Serial.println("Initializing..."); if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1) + while (true) ; } if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); - while (1) + while (true) ; } @@ -54,7 +53,7 @@ void setup() { if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); - while (1) + while (true) ; } nfc.startDiscovery(); // NCI Discovery mode @@ -62,14 +61,13 @@ void setup() { } void loop() { - if (!nfc.waitForDiscoveryNotification(&RfIntf)) { // Waiting to detect cards + if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards displayDeviceInfo(); switch (nfc.remoteDevice.protocol) { case PROT_T1T: case PROT_T2T: case PROT_T3T: case PROT_ISODEP: - // nfc.processReaderMode(RfIntf, READ_NDEF); // TODO: update it to use the new API nfc.readNdefMessage(); break; @@ -77,7 +75,6 @@ void loop() { break; case PROT_MIFARE: - // nfc.processReaderMode(RfIntf, READ_NDEF); nfc.readNdefMessage(); break; @@ -90,11 +87,11 @@ void loop() { nfc.activateNextTagDiscovery(); Serial.println("Multiple cards are detected!"); } - // Wait for card removal Serial.println("Remove the Card"); - nfc.processReaderMode(RfIntf, PRESENCE_CHECK); + nfc.waitForTagRemoval(); Serial.println("Card removed!"); } + Serial.println("Restarting..."); nfc.reset(); Serial.println("Waiting for a Card..."); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 2f0307e..a5459cf 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1354,7 +1354,7 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; uint8_t NCISelectMIFARE[] = {0x21, 0x04, 0x03, 0x01, 0x80, 0x80}; - switch (RfIntf.Protocol) { + switch (this->remoteDevice.protocol) { case PROT_T1T: do { delay(500); @@ -1394,8 +1394,9 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { case PROT_ISO15693: do { delay(500); - for (i = 0; i < 8; i++) - NCIPresCheckIso15693[i + 6] = RfIntf.Info.NFC_VPP.ID[7 - i]; + for (i = 0; i < 8; i++) { + NCIPresCheckIso15693[i + 6] = this->remoteDevice.info.nfcVPP.id[7 - i]; + } (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); getMessage(); getMessage(100); @@ -1426,7 +1427,11 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { } } -// Deprecated, use presenceCheck() instead +void Electroniccats_PN7150::waitForTagRemoval() { + Electroniccats_PN7150::presenceCheck(this->dummyRfInterface); +} + +// Deprecated, use waitForTagRemoval() instead void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { Electroniccats_PN7150::presenceCheck(RfIntf); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 64f5d3b..6c63455 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -238,7 +238,8 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { void processP2pMode(RfIntf_t RfIntf); void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead void presenceCheck(RfIntf_t RfIntf); - void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use presenceCheck() instead + void waitForTagRemoval(); + void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use waitForTagRemoval() instead bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead bool readerReActivate(RfIntf_t *pRfIntf); From b9dfde490b9e2eb2a423bf27df530a475243b35b Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 11 Aug 2023 08:40:31 -0600 Subject: [PATCH 052/106] test: disable nxp structs --- examples/NDEFReceive/NDEFReceive.ino | 1 + src/Electroniccats_PN7150.h | 77 ++++++++++++++-------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index f06c1d7..179c0ae 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -64,6 +64,7 @@ void loop() { if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards displayDeviceInfo(); switch (nfc.remoteDevice.protocol) { + // TODO: add getProtocol() to the API and make the available protocols more accessible case PROT_T1T: case PROT_T2T: case PROT_T3T: diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 6c63455..9dcf5f5 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -27,6 +27,7 @@ #include "T4T_NDEF_emu.h" #include "ndef_helper.h" +// #define DEBGU2 // #define DEBUG3 #if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 : Special, more optimized I2C library for Teensy boards @@ -141,48 +142,48 @@ typedef enum { * Definition of discovered remote device properties information */ /* POLL passive type A */ -typedef struct -{ - unsigned char SensRes[2]; - unsigned char NfcIdLen; - unsigned char NfcId[10]; - unsigned char SelResLen; - unsigned char SelRes[1]; - unsigned char RatsLen; - unsigned char Rats[20]; -} NxpNci_RfIntf_info_APP_t; +// typedef struct +// { +// unsigned char SensRes[2]; +// unsigned char NfcIdLen; +// unsigned char NfcId[10]; +// unsigned char SelResLen; +// unsigned char SelRes[1]; +// unsigned char RatsLen; +// unsigned char Rats[20]; +// } NxpNci_RfIntf_info_APP_t; -/* POLL passive type B */ -typedef struct -{ - unsigned char SensResLen; - unsigned char SensRes[12]; - unsigned char AttribResLen; - unsigned char AttribRes[17]; -} NxpNci_RfIntf_info_BPP_t; +// /* POLL passive type B */ +// typedef struct +// { +// unsigned char SensResLen; +// unsigned char SensRes[12]; +// unsigned char AttribResLen; +// unsigned char AttribRes[17]; +// } NxpNci_RfIntf_info_BPP_t; -/* POLL passive type F */ -typedef struct -{ - unsigned char BitRate; - unsigned char SensResLen; - unsigned char SensRes[18]; -} NxpNci_RfIntf_info_FPP_t; +// /* POLL passive type F */ +// typedef struct +// { +// unsigned char BitRate; +// unsigned char SensResLen; +// unsigned char SensRes[18]; +// } NxpNci_RfIntf_info_FPP_t; -/* POLL passive type ISO15693 */ -typedef struct -{ - unsigned char AFI; - unsigned char DSFID; - unsigned char ID[8]; -} NxpNci_RfIntf_info_VPP_t; +// /* POLL passive type ISO15693 */ +// typedef struct +// { +// unsigned char AFI; +// unsigned char DSFID; +// unsigned char ID[8]; +// } NxpNci_RfIntf_info_VPP_t; -typedef union { - NxpNci_RfIntf_info_APP_t NFC_APP; - NxpNci_RfIntf_info_BPP_t NFC_BPP; - NxpNci_RfIntf_info_FPP_t NFC_FPP; - NxpNci_RfIntf_info_VPP_t NFC_VPP; -} NxpNci_RfIntf_Info_t; +// typedef union { +// NxpNci_RfIntf_info_APP_t NFC_APP; +// NxpNci_RfIntf_info_BPP_t NFC_BPP; +// NxpNci_RfIntf_info_FPP_t NFC_FPP; +// NxpNci_RfIntf_info_VPP_t NFC_VPP; +// } NxpNci_RfIntf_Info_t; class Electroniccats_PN7150 : public Mode, public RemoteDevice { private: From 28e875e6b048dcbfe90f8030510ac1f2a7436a94 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 11 Aug 2023 16:33:58 -0600 Subject: [PATCH 053/106] feat: add protocol class --- examples/NDEFReceive/NDEFReceive.ino | 16 ++++++++-------- src/Electroniccats_PN7150.h | 14 +++++++------- src/Protocol.h | 18 ++++++++++++++++++ src/RemoteDevice.h | 2 ++ 4 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 src/Protocol.h diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 179c0ae..c2bcf1d 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -125,28 +125,28 @@ void displayDeviceInfo() { Serial.print("Protocol: "); switch (nfc.remoteDevice.protocol) { - case PROT_UNDETERMINED: + case nfc.protocol.UNDETERMINED: Serial.println("UNDETERMINED"); break; - case PROT_T1T: + case nfc.protocol.T1T: Serial.println("T1T"); break; - case PROT_T2T: + case nfc.protocol.T2T: Serial.println("T2T"); break; - case PROT_T3T: + case nfc.protocol.T3T: Serial.println("T3T"); break; - case PROT_ISODEP: + case nfc.protocol.ISODEP: Serial.println("ISO-DEP"); break; - case PROT_NFCDEP: + case nfc.protocol.NFCDEP: Serial.println("NFC-DEP"); break; - case PROT_ISO15693: + case nfc.protocol.ISO15693: Serial.println("ISO15693"); break; - case PROT_MIFARE: + case nfc.protocol.MIFARE: Serial.println("MIFARE"); break; default: diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 9dcf5f5..0dfec65 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -84,13 +84,13 @@ * Flag definition used as Protocol values */ #define PROT_UNDETERMINED 0x0 -#define PROT_T1T 0x1 -#define PROT_T2T 0x2 -#define PROT_T3T 0x3 -#define PROT_ISODEP 0x4 -#define PROT_NFCDEP 0x5 -#define PROT_ISO15693 0x6 -#define PROT_MIFARE 0x80 +#define PROT_T1T 0x1 // NFC FORUM Type 1 Tag (based on Topaz/Jewel) ISO 14443A +#define PROT_T2T 0x2 // NFC Type 2 Tag ISO 14443B +#define PROT_T3T 0x3 // NFC Type 3 Tag Sony FeliCa standard +#define PROT_ISODEP 0x4 // NFC Type 4 Tag ISO14443-4 +#define PROT_NFCDEP 0x5 // NFC Data Exchange Protocol +#define PROT_ISO15693 0x6 // NFC-V +#define PROT_MIFARE 0x80 // NFC Type 5 MIFARE Classic /* * Flag definition used as Interface values diff --git a/src/Protocol.h b/src/Protocol.h new file mode 100644 index 0000000..0d89814 --- /dev/null +++ b/src/Protocol.h @@ -0,0 +1,18 @@ +#ifndef Protocol_H +#define Protocol_H + +class Protocol { + public: + enum Value { + UNDETERMINED = 0x0, + T1T = 0x1, // NFC FORUM Type 1 Tag (based on Topaz/Jewel) ISO 14443A + T2T = 0x2, // NFC Type 2 Tag ISO 14443B + T3T = 0x3, // NFC Type 3 Tag Sony FeliCa standard + ISODEP = 0x4, // NFC Type 4 Tag ISO14443-4 + NFCDEP = 0x5, // NFC Data Exchange Protocol + ISO15693 = 0x6, // NFC-V + MIFARE = 0x80 // NFC Type 5 MIFARE Classic + }; +}; + +#endif \ No newline at end of file diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 0100d40..2c981bc 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -2,6 +2,7 @@ #define RemoteDevice_H #include "Arduino.h" +#include "Protocol.h" /* * Definition of discovered remote device properties information @@ -110,6 +111,7 @@ struct RfIntfCC_t { class RemoteDevice { public: RfIntfCC_t remoteDevice; + Protocol protocol; }; #endif \ No newline at end of file From 2bde551679db2c6675a0011be73bd15a1c0eae86 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 11 Aug 2023 17:09:11 -0600 Subject: [PATCH 054/106] feat: add technology and mode technology classes --- examples/NDEFReceive/NDEFReceive.ino | 18 +++---- src/Electroniccats_PN7150.h | 76 ---------------------------- src/ModeTech.h | 20 ++++++++ src/Protocol.h | 26 +++++++--- src/RemoteDevice.h | 4 ++ src/Technology.h | 26 ++++++++++ 6 files changed, 78 insertions(+), 92 deletions(-) create mode 100644 src/ModeTech.h create mode 100644 src/Technology.h diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index c2bcf1d..dddf729 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -154,19 +154,19 @@ void displayDeviceInfo() { break; } - Serial.print("Mode: "); + Serial.print("Technology: "); switch (nfc.remoteDevice.modeTech) { - case MODE_POLL: - Serial.println("POLL"); + case nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCA: + Serial.println("PASSIVE NFC A"); break; - case MODE_LISTEN: - Serial.println("LISTEN"); + case nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCB: + Serial.println("PASSIVE NFC B"); break; - case MODE_MASK: - Serial.println("MASK"); + case nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCF: + Serial.println("PASSIVE NFC F"); break; - default: - Serial.println("UNKNOWN"); + case nfc.modeTech.POLL | nfc.tech.PASSIVE_15693: + Serial.println("PASSIVE 15693"); break; } } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 0dfec65..fd58f8d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -63,35 +63,6 @@ #define MODE_P2P (1 << 1) #define MODE_RW (1 << 2) -/* - * Flag definition used as Mode values - */ -#define MODE_POLL 0x00 -#define MODE_LISTEN 0x80 -#define MODE_MASK 0xF0 - -/* - * Flag definition used as Technologies values - */ -#define TECH_PASSIVE_NFCA 0 -#define TECH_PASSIVE_NFCB 1 -#define TECH_PASSIVE_NFCF 2 -#define TECH_ACTIVE_NFCA 3 -#define TECH_ACTIVE_NFCF 5 -#define TECH_PASSIVE_15693 6 - -/* - * Flag definition used as Protocol values - */ -#define PROT_UNDETERMINED 0x0 -#define PROT_T1T 0x1 // NFC FORUM Type 1 Tag (based on Topaz/Jewel) ISO 14443A -#define PROT_T2T 0x2 // NFC Type 2 Tag ISO 14443B -#define PROT_T3T 0x3 // NFC Type 3 Tag Sony FeliCa standard -#define PROT_ISODEP 0x4 // NFC Type 4 Tag ISO14443-4 -#define PROT_NFCDEP 0x5 // NFC Data Exchange Protocol -#define PROT_ISO15693 0x6 // NFC-V -#define PROT_MIFARE 0x80 // NFC Type 5 MIFARE Classic - /* * Flag definition used as Interface values */ @@ -138,53 +109,6 @@ typedef enum { PRESENCE_CHECK } RW_Operation_t; -/* - * Definition of discovered remote device properties information - */ -/* POLL passive type A */ -// typedef struct -// { -// unsigned char SensRes[2]; -// unsigned char NfcIdLen; -// unsigned char NfcId[10]; -// unsigned char SelResLen; -// unsigned char SelRes[1]; -// unsigned char RatsLen; -// unsigned char Rats[20]; -// } NxpNci_RfIntf_info_APP_t; - -// /* POLL passive type B */ -// typedef struct -// { -// unsigned char SensResLen; -// unsigned char SensRes[12]; -// unsigned char AttribResLen; -// unsigned char AttribRes[17]; -// } NxpNci_RfIntf_info_BPP_t; - -// /* POLL passive type F */ -// typedef struct -// { -// unsigned char BitRate; -// unsigned char SensResLen; -// unsigned char SensRes[18]; -// } NxpNci_RfIntf_info_FPP_t; - -// /* POLL passive type ISO15693 */ -// typedef struct -// { -// unsigned char AFI; -// unsigned char DSFID; -// unsigned char ID[8]; -// } NxpNci_RfIntf_info_VPP_t; - -// typedef union { -// NxpNci_RfIntf_info_APP_t NFC_APP; -// NxpNci_RfIntf_info_BPP_t NFC_BPP; -// NxpNci_RfIntf_info_FPP_t NFC_FPP; -// NxpNci_RfIntf_info_VPP_t NFC_VPP; -// } NxpNci_RfIntf_Info_t; - class Electroniccats_PN7150 : public Mode, public RemoteDevice { private: uint8_t _IRQpin, _VENpin, _I2Caddress; diff --git a/src/ModeTech.h b/src/ModeTech.h new file mode 100644 index 0000000..bc4d4fa --- /dev/null +++ b/src/ModeTech.h @@ -0,0 +1,20 @@ +#ifndef ModeTech_H +#define ModeTech_H + +/* + * Flag definition used as Mode values + */ +#define MODE_POLL 0x00 +#define MODE_LISTEN 0x80 +#define MODE_MASK 0xF0 + +class ModeTech { + public: + enum Value { + POLL = 0x00, + LISTEN = 0x80, + MASK = 0xF0 + }; +}; + +#endif \ No newline at end of file diff --git a/src/Protocol.h b/src/Protocol.h index 0d89814..a5e41a3 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -1,17 +1,29 @@ #ifndef Protocol_H #define Protocol_H +/* + * Flag definition used as Protocol values + */ +#define PROT_UNDETERMINED 0x0 +#define PROT_T1T 0x1 // NFC FORUM Type 1 Tag (based on Topaz/Jewel) ISO 14443A +#define PROT_T2T 0x2 // NFC Type 2 Tag ISO 14443B +#define PROT_T3T 0x3 // NFC Type 3 Tag Sony FeliCa standard +#define PROT_ISODEP 0x4 // NFC Type 4 Tag ISO14443-4 +#define PROT_NFCDEP 0x5 // NFC Data Exchange Protocol +#define PROT_ISO15693 0x6 // NFC-V +#define PROT_MIFARE 0x80 // NFC Type 5 MIFARE Classic + class Protocol { public: enum Value { UNDETERMINED = 0x0, - T1T = 0x1, // NFC FORUM Type 1 Tag (based on Topaz/Jewel) ISO 14443A - T2T = 0x2, // NFC Type 2 Tag ISO 14443B - T3T = 0x3, // NFC Type 3 Tag Sony FeliCa standard - ISODEP = 0x4, // NFC Type 4 Tag ISO14443-4 - NFCDEP = 0x5, // NFC Data Exchange Protocol - ISO15693 = 0x6, // NFC-V - MIFARE = 0x80 // NFC Type 5 MIFARE Classic + T1T = 0x1, + T2T = 0x2, + T3T = 0x3, + ISODEP = 0x4, + NFCDEP = 0x5, + ISO15693 = 0x6, + MIFARE = 0x80 }; }; diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 2c981bc..d8b511c 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -3,6 +3,8 @@ #include "Arduino.h" #include "Protocol.h" +#include "Technology.h" +#include "ModeTech.h" /* * Definition of discovered remote device properties information @@ -112,6 +114,8 @@ class RemoteDevice { public: RfIntfCC_t remoteDevice; Protocol protocol; + Technology tech; + ModeTech modeTech; }; #endif \ No newline at end of file diff --git a/src/Technology.h b/src/Technology.h new file mode 100644 index 0000000..4bea4b2 --- /dev/null +++ b/src/Technology.h @@ -0,0 +1,26 @@ +#ifndef Technology_H +#define Technology_H + +/* + * Flag definition used as Technologies values + */ +#define TECH_PASSIVE_NFCA 0 +#define TECH_PASSIVE_NFCB 1 +#define TECH_PASSIVE_NFCF 2 +#define TECH_ACTIVE_NFCA 3 +#define TECH_ACTIVE_NFCF 5 +#define TECH_PASSIVE_15693 6 + +class Technology { + public: + enum Value { + PASSIVE_NFCA = 0, + PASSIVE_NFCB = 1, + PASSIVE_NFCF = 2, + ACTIVE_NFCA = 3, + ACTIVE_NFCF = 5, + PASSIVE_15693 = 6 + }; +}; + +#endif \ No newline at end of file From 6cb62aaf661ab0edc63252f025c8df5e7f568d15 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 14 Aug 2023 12:56:50 -0600 Subject: [PATCH 055/106] feat: add getter and setter for device properties --- examples/NDEFReceive/NDEFReceive.ino | 13 +- src/Electroniccats_PN7150.cpp | 225 ++++++--------------------- src/Electroniccats_PN7150.h | 16 +- src/Interface.h | 24 +++ src/RemoteDevice.cpp | 99 ++++++++++++ src/RemoteDevice.h | 20 ++- 6 files changed, 197 insertions(+), 200 deletions(-) create mode 100644 src/Interface.h create mode 100644 src/RemoteDevice.cpp diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index dddf729..b257425 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -3,7 +3,8 @@ #define PN7150_VEN (13) #define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +// Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 0x01, // Type length (1 byte) @@ -63,7 +64,7 @@ void setup() { void loop() { if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards displayDeviceInfo(); - switch (nfc.remoteDevice.protocol) { + switch (nfc.remoteDevice.getProtocol()) { // TODO: add getProtocol() to the API and make the available protocols more accessible case PROT_T1T: case PROT_T2T: @@ -84,7 +85,7 @@ void loop() { } // It can detect multiple cards at the same time if they use the same protocol - if (nfc.remoteDevice.moreTagsAvailable) { + if (nfc.remoteDevice.hasMoreTags()) { nfc.activateNextTagDiscovery(); Serial.println("Multiple cards are detected!"); } @@ -102,7 +103,7 @@ void loop() { void displayDeviceInfo() { Serial.println(); Serial.print("Interface: "); - switch (nfc.remoteDevice.interface) { + switch (nfc.remoteDevice.getInterface()) { case INTF_ISODEP: Serial.println("ISO-DEP"); break; @@ -124,7 +125,7 @@ void displayDeviceInfo() { } Serial.print("Protocol: "); - switch (nfc.remoteDevice.protocol) { + switch (nfc.remoteDevice.getProtocol()) { case nfc.protocol.UNDETERMINED: Serial.println("UNDETERMINED"); break; @@ -155,7 +156,7 @@ void displayDeviceInfo() { } Serial.print("Technology: "); - switch (nfc.remoteDevice.modeTech) { + switch (nfc.remoteDevice.getModeTech()) { case nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCA: Serial.println("PASSIVE NFC A"); break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a5459cf..c852fa7 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -910,15 +910,20 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* Is RF_INTF_ACTIVATED_NTF ? */ if (rxBuffer[1] == 0x05) { pRfIntf->Interface = rxBuffer[4]; - this->remoteDevice.interface = rxBuffer[4]; + // this->remoteDeviceStruct.interface = rxBuffer[4]; + this->remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - this->remoteDevice.protocol = rxBuffer[5]; + // this->remoteDeviceStruct.protocol = rxBuffer[5]; + this->remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - this->remoteDevice.modeTech = rxBuffer[6]; + // this->remoteDeviceStruct.modeTech = rxBuffer[6]; + this->remoteDevice.setModeTech(rxBuffer[6]); pRfIntf->MoreTags = false; - this->remoteDevice.moreTagsAvailable = false; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - fillInterfaceInfo(&rxBuffer[10]); + // this->remoteDeviceStruct.moreTagsAvailable = false; + this->remoteDevice.setMoreTagsAvailable(false); + // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + // fillInterfaceInfo(&rxBuffer[10]); + this->remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); // P2P /* Verifying if not a P2P device also presenting T4T emulation */ @@ -941,15 +946,16 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* Is P2P detected ? */ if (rxBuffer[5] == PROT_NFCDEP) { pRfIntf->Interface = rxBuffer[4]; - this->remoteDevice.interface = rxBuffer[4]; + this->remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - this->remoteDevice.protocol = rxBuffer[5]; + this->remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - this->remoteDevice.modeTech = rxBuffer[6]; + this->remoteDevice.setModeTech(rxBuffer[6]); pRfIntf->MoreTags = false; - this->remoteDevice.moreTagsAvailable = false; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - fillInterfaceInfo(&rxBuffer[10]); + this->remoteDevice.setMoreTagsAvailable(false); + // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + // fillInterfaceInfo(&rxBuffer[10]); + this->remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); break; } } else { @@ -969,13 +975,13 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } } else { /* RF_DISCOVER_NTF */ pRfIntf->Interface = INTF_UNDETERMINED; - this->remoteDevice.interface = INTF_UNDETERMINED; + this->remoteDevice.setInterface(INTF_UNDETERMINED); pRfIntf->Protocol = rxBuffer[4]; - this->remoteDevice.protocol = rxBuffer[4]; + this->remoteDevice.setProtocol(rxBuffer[4]); pRfIntf->ModeTech = rxBuffer[5]; - this->remoteDevice.modeTech = rxBuffer[5]; + this->remoteDevice.setModeTech(rxBuffer[5]); pRfIntf->MoreTags = true; - this->remoteDevice.moreTagsAvailable = true; + this->remoteDevice.setMoreTagsAvailable(true); /* Get next NTF for further activation */ do { @@ -1009,17 +1015,18 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { pRfIntf->Interface = rxBuffer[4]; - this->remoteDevice.interface = rxBuffer[4]; + this->remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - this->remoteDevice.protocol = rxBuffer[5]; + this->remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - this->remoteDevice.modeTech = rxBuffer[6]; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - fillInterfaceInfo(&rxBuffer[10]); + this->remoteDevice.setModeTech(rxBuffer[6]); + // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + // fillInterfaceInfo(&rxBuffer[10]); + this->remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); } /* In case of P2P target detected but lost, inform application to restart discovery */ - else if (pRfIntf->Protocol == PROT_NFCDEP) { + else if (this->remoteDevice.getProtocol() == PROT_NFCDEP) { /* Restart the discovery loop */ (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); getMessage(); @@ -1036,7 +1043,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* In case of unknown target align protocol information */ if (pRfIntf->Interface == INTF_UNDETERMINED) { pRfIntf->Protocol = PROT_UNDETERMINED; - this->remoteDevice.protocol = PROT_UNDETERMINED; + this->remoteDevice.setProtocol(protocol.UNDETERMINED); } return SUCCESS; @@ -1354,7 +1361,7 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; uint8_t NCISelectMIFARE[] = {0x21, 0x04, 0x03, 0x01, 0x80, 0x80}; - switch (this->remoteDevice.protocol) { + switch (remoteDevice.getProtocol()) { case PROT_T1T: do { delay(500); @@ -1395,7 +1402,7 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { do { delay(500); for (i = 0; i < 8; i++) { - NCIPresCheckIso15693[i + 6] = this->remoteDevice.info.nfcVPP.id[7 - i]; + NCIPresCheckIso15693[i + 6] = remoteDevice.getVPPID(7 - i); } (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); getMessage(); @@ -1436,147 +1443,6 @@ void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { Electroniccats_PN7150::presenceCheck(RfIntf); } -void Electroniccats_PN7150::fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { - uint8_t i, temp; - - switch (pRfIntf->ModeTech) { - case (MODE_POLL | TECH_PASSIVE_NFCA): - memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); - temp = 2; - pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; - temp++; - memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); - temp += pBuf[2]; - pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; - temp++; - - if (pRfIntf->Info.NFC_APP.SelResLen == 1) - pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; - - temp += 4; - if (pBuf[temp] != 0) { - temp++; - pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; - memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); - } else { - pRfIntf->Info.NFC_APP.RatsLen = 0; - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCB): - pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; - memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); - temp = pBuf[0] + 4; - if (pBuf[temp] != 0) { - temp++; - pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; - memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); - } else { - pRfIntf->Info.NFC_BPP.AttribResLen = 0; - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCF): - pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; - pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; - memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); - break; - - case (MODE_POLL | TECH_PASSIVE_15693): - pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; - pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; - - for (i = 0; i < 8; i++) - pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; - - break; - - default: - break; - } -} - -void Electroniccats_PN7150::fillInterfaceInfo(uint8_t *pBuf) { - uint8_t i, temp; - - switch (this->remoteDevice.modeTech) { - case (MODE_POLL | TECH_PASSIVE_NFCA): - // memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); - memcpy(this->remoteDevice.info.nfcAPP.sensRes, &pBuf[0], 2); - temp = 2; - // pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; - this->remoteDevice.info.nfcAPP.nfcIdLen = pBuf[temp]; - temp++; - // memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); - memcpy(this->remoteDevice.info.nfcAPP.nfcId, &pBuf[3], this->remoteDevice.info.nfcAPP.nfcIdLen); - temp += pBuf[2]; - // pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; - this->remoteDevice.info.nfcAPP.selResLen = pBuf[temp]; - temp++; - - if (this->remoteDevice.info.nfcAPP.selResLen == 1) { - // pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; - this->remoteDevice.info.nfcAPP.selRes[0] = pBuf[temp]; - } - - temp += 4; - if (pBuf[temp] != 0) { - temp++; - // pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; - this->remoteDevice.info.nfcAPP.ratsLen = pBuf[temp]; - // memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); - memcpy(this->remoteDevice.info.nfcAPP.rats, &pBuf[temp + 1], pBuf[temp]); - } else { - // pRfIntf->Info.NFC_APP.RatsLen = 0; - this->remoteDevice.info.nfcAPP.ratsLen = 0; - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCB): - // pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; - this->remoteDevice.info.nfcBPP.sensResLen = pBuf[0]; - // memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); - memcpy(this->remoteDevice.info.nfcBPP.sensRes, &pBuf[1], this->remoteDevice.info.nfcBPP.sensResLen); - temp = pBuf[0] + 4; - if (pBuf[temp] != 0) { - temp++; - // pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; - this->remoteDevice.info.nfcBPP.attribResLen = pBuf[temp]; - // memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); - memcpy(this->remoteDevice.info.nfcBPP.attribRes, &pBuf[temp + 1], pBuf[temp]); - } else { - // pRfIntf->Info.NFC_BPP.AttribResLen = 0; - this->remoteDevice.info.nfcBPP.attribResLen = 0; - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCF): - // pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; - this->remoteDevice.info.nfcFPP.bitRate = pBuf[0]; - // pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; - this->remoteDevice.info.nfcFPP.sensResLen = pBuf[1]; - // memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); - memcpy(this->remoteDevice.info.nfcFPP.sensRes, &pBuf[2], this->remoteDevice.info.nfcFPP.sensResLen); - break; - - case (MODE_POLL | TECH_PASSIVE_15693): - // pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; - this->remoteDevice.info.nfcVPP.afi = pBuf[0]; - // pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; - this->remoteDevice.info.nfcVPP.dsfid = pBuf[1]; - - for (i = 0; i < 8; i++) { - // pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; - this->remoteDevice.info.nfcVPP.id[7 - i] = pBuf[2 + i]; - } - - break; - - default: - break; - } -} - // Deprecated, use fillInterfaceInfo(uint8_t *pBuf) instead void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); @@ -1645,13 +1511,13 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { bool status = ERROR; pRfIntf->MoreTags = false; - this->remoteDevice.moreTagsAvailable = false; + remoteDevice.setMoreTagsAvailable(false); - if (gNextTag_Protocol == PROT_UNDETERMINED) { + if (gNextTag_Protocol == protocol.UNDETERMINED) { pRfIntf->Interface = INTF_UNDETERMINED; - this->remoteDevice.interface = INTF_UNDETERMINED; + remoteDevice.setInterface(interface.UNDETERMINED); pRfIntf->Protocol = PROT_UNDETERMINED; - this->remoteDevice.protocol = PROT_UNDETERMINED; + remoteDevice.setProtocol(protocol.UNDETERMINED); return ERROR; } @@ -1672,7 +1538,7 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { else if (gNextTag_Protocol == PROT_ISODEP) NCIRfDiscoverSelect[5] = INTF_NFCDEP; else if (gNextTag_Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; + NCIRfDiscoverSelect[5] = INTF_TAGCMD; // TODO: check this else NCIRfDiscoverSelect[5] = INTF_FRAME; @@ -1683,13 +1549,14 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { getMessage(100); if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { pRfIntf->Interface = rxBuffer[4]; - this->remoteDevice.interface = rxBuffer[4]; + remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - this->remoteDevice.protocol = rxBuffer[5]; + remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - this->remoteDevice.modeTech = rxBuffer[6]; - fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - fillInterfaceInfo(&rxBuffer[10]); + remoteDevice.setModeTech(rxBuffer[6]); + // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); + // fillInterfaceInfo(&rxBuffer[10]); + remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); status = SUCCESS; } } @@ -1710,7 +1577,7 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; - RW_NDEF_Reset(this->remoteDevice.protocol); + RW_NDEF_Reset(remoteDevice.getProtocol()); while (1) { RW_NDEF_Read_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); @@ -1728,7 +1595,7 @@ void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { getMessage(1000); // Manage chaining in case of T4T - if (this->remoteDevice.interface == INTF_ISODEP && rxBuffer[0] == 0x10) { + if (remoteDevice.getInterface() == INTF_ISODEP && rxBuffer[0] == 0x10) { uint8_t tmp[MAX_NCI_FRAME_SIZE]; uint8_t tmpSize = 0; while (rxBuffer[0] == 0x10) { @@ -1759,7 +1626,7 @@ void Electroniccats_PN7150::writeNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; - RW_NDEF_Reset(this->remoteDevice.protocol); + RW_NDEF_Reset(remoteDevice.getProtocol()); while (1) { RW_NDEF_Write_Next(&rxBuffer[3], rxBuffer[2], &Cmd[3], (unsigned short *)&CmdSize); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index fd58f8d..9da1f6d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -63,15 +63,6 @@ #define MODE_P2P (1 << 1) #define MODE_RW (1 << 2) -/* - * Flag definition used as Interface values - */ -#define INTF_UNDETERMINED 0x0 -#define INTF_FRAME 0x1 -#define INTF_ISODEP 0x2 -#define INTF_NFCDEP 0x3 -#define INTF_TAGCMD 0x80 - #define MaxPayloadSize 255 // See NCI specification V1.0, section 3.1 #define MsgHeaderSize 3 @@ -109,7 +100,7 @@ typedef enum { PRESENCE_CHECK } RW_Operation_t; -class Electroniccats_PN7150 : public Mode, public RemoteDevice { +class Electroniccats_PN7150 : public Mode { private: uint8_t _IRQpin, _VENpin, _I2Caddress; TwoWire *_wire; @@ -130,6 +121,11 @@ class Electroniccats_PN7150 : public Mode, public RemoteDevice { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); uint8_t begin(void); + RemoteDevice remoteDevice; + Protocol protocol; + Technology tech; + ModeTech modeTech; + Interface interface; bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read diff --git a/src/Interface.h b/src/Interface.h new file mode 100644 index 0000000..ff04c5f --- /dev/null +++ b/src/Interface.h @@ -0,0 +1,24 @@ +#ifndef Interface_H +#define Interface_H + +/* + * Flag definition used as Interface values + */ +#define INTF_UNDETERMINED 0x0 +#define INTF_FRAME 0x1 +#define INTF_ISODEP 0x2 +#define INTF_NFCDEP 0x3 +#define INTF_TAGCMD 0x80 + +class Interface { + public: + enum Value { + UNDETERMINED = 0x0, + FRAME = 0x1, + ISODEP = 0x2, + NFCDEP = 0x3, + TAGCMD = 0x80 + }; +}; + +#endif \ No newline at end of file diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp new file mode 100644 index 0000000..34d354b --- /dev/null +++ b/src/RemoteDevice.cpp @@ -0,0 +1,99 @@ +#include "RemoteDevice.h" + +unsigned char RemoteDevice::getInterface() const { + return this->remoteDeviceStruct.interface; +} + +unsigned char RemoteDevice::getProtocol() const { + return this->remoteDeviceStruct.protocol; +} + +unsigned char RemoteDevice::getModeTech() const { + return this->remoteDeviceStruct.modeTech; +} + +bool RemoteDevice::hasMoreTags() const { + return this->remoteDeviceStruct.moreTagsAvailable; +} + +// TODO: validate memory access +unsigned char RemoteDevice::getVPPID(int position) const { + return this->remoteDeviceStruct.info.nfcVPP.id[position]; +} + +void RemoteDevice::setInterface(unsigned char interface) { + this->remoteDeviceStruct.interface = interface; +} + +void RemoteDevice::setProtocol(unsigned char protocol) { + this->remoteDeviceStruct.protocol = protocol; +} + +void RemoteDevice::setModeTech(unsigned char modeTech) { + this->remoteDeviceStruct.modeTech = modeTech; +} + +void RemoteDevice::setMoreTagsAvailable(bool moreTags) { + this->remoteDeviceStruct.moreTagsAvailable = moreTags; +} + +void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { + uint8_t i, temp; + + switch (pRfIntf->ModeTech) { + case (MODE_POLL | TECH_PASSIVE_NFCA): + memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); + memcpy(remoteDeviceStruct.info.nfcAPP.sensRes, &pBuf[0], 2); + temp = 2; + pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; + temp++; + memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); + temp += pBuf[2]; + pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; + temp++; + + if (pRfIntf->Info.NFC_APP.SelResLen == 1) + pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; + + temp += 4; + if (pBuf[temp] != 0) { + temp++; + pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); + } else { + pRfIntf->Info.NFC_APP.RatsLen = 0; + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCB): + pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; + memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); + temp = pBuf[0] + 4; + if (pBuf[temp] != 0) { + temp++; + pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; + memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); + } else { + pRfIntf->Info.NFC_BPP.AttribResLen = 0; + } + break; + + case (MODE_POLL | TECH_PASSIVE_NFCF): + pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; + pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; + memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); + break; + + case (MODE_POLL | TECH_PASSIVE_15693): + pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; + pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; + + for (i = 0; i < 8; i++) + pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; + + break; + + default: + break; + } +} \ No newline at end of file diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index d8b511c..87e9ff6 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -2,9 +2,10 @@ #define RemoteDevice_H #include "Arduino.h" +#include "Interface.h" +#include "ModeTech.h" #include "Protocol.h" #include "Technology.h" -#include "ModeTech.h" /* * Definition of discovered remote device properties information @@ -111,11 +112,20 @@ struct RfIntfCC_t { }; class RemoteDevice { + protected: + RfIntfCC_t remoteDeviceStruct; + public: - RfIntfCC_t remoteDevice; - Protocol protocol; - Technology tech; - ModeTech modeTech; + unsigned char getInterface() const; + unsigned char getProtocol() const; + unsigned char getModeTech() const; + bool hasMoreTags() const; + unsigned char getVPPID(int position) const; + void setInterface(unsigned char interface); + void setProtocol(unsigned char protocol); + void setModeTech(unsigned char modeTech); + void setMoreTagsAvailable(bool moreTags); + void setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); }; #endif \ No newline at end of file From 73baecf43aa624d2b8e301b4fae0aef2dcc3ec1b Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 14 Aug 2023 14:57:46 -0600 Subject: [PATCH 056/106] feat: remove fill interface info --- examples/DetectTagsOld/DetectTagsOld.ino | 4 +- examples/NDEFReceive/NDEFReceive.ino | 32 ++++------ src/Electroniccats_PN7150.cpp | 75 ++++++++++-------------- src/Electroniccats_PN7150.h | 3 - src/RemoteDevice.cpp | 26 +++++++- src/RemoteDevice.h | 2 +- 6 files changed, 69 insertions(+), 73 deletions(-) diff --git a/examples/DetectTagsOld/DetectTagsOld.ino b/examples/DetectTagsOld/DetectTagsOld.ino index 442738e..66cbdbe 100644 --- a/examples/DetectTagsOld/DetectTagsOld.ino +++ b/examples/DetectTagsOld/DetectTagsOld.ino @@ -13,8 +13,8 @@ */ #include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) +#define PN7150_IRQ (11) +#define PN7150_VEN (13) #define PN7150_ADDR (0x28) Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index b257425..2d3a48b 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -62,24 +62,18 @@ void setup() { } void loop() { - if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards + if (!nfc.waitForDiscoveryNotification()) { // Wait for a card displayDeviceInfo(); switch (nfc.remoteDevice.getProtocol()) { - // TODO: add getProtocol() to the API and make the available protocols more accessible - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: + // Read NDEF message from NFC Forum Type 1, 2, 3, 4, 5 tags + case nfc.protocol.T1T: + case nfc.protocol.T2T: + case nfc.protocol.T3T: + case nfc.protocol.ISODEP: + case nfc.protocol.MIFARE: nfc.readNdefMessage(); break; - - case PROT_ISO15693: - break; - - case PROT_MIFARE: - nfc.readNdefMessage(); - break; - + case nfc.protocol.ISO15693: default: break; } @@ -104,19 +98,19 @@ void displayDeviceInfo() { Serial.println(); Serial.print("Interface: "); switch (nfc.remoteDevice.getInterface()) { - case INTF_ISODEP: + case nfc.interface.ISODEP: Serial.println("ISO-DEP"); break; - case INTF_NFCDEP: + case nfc.interface.NFCDEP: Serial.println("NFC-DEP"); break; - case INTF_TAGCMD: + case nfc.interface.TAGCMD: Serial.println("TAG"); break; - case INTF_FRAME: + case nfc.interface.FRAME: Serial.println("FRAME"); break; - case INTF_UNDETERMINED: + case nfc.interface.UNDETERMINED: Serial.println("UNDETERMINED"); break; default: diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index c852fa7..c236d55 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -891,7 +891,7 @@ bool Electroniccats_PN7150::StopDiscovery() { } bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { - uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, PROT_ISODEP, INTF_ISODEP}; + uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, protocol.ISODEP, interface.ISODEP}; // P2P Support uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; @@ -910,20 +910,14 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* Is RF_INTF_ACTIVATED_NTF ? */ if (rxBuffer[1] == 0x05) { pRfIntf->Interface = rxBuffer[4]; - // this->remoteDeviceStruct.interface = rxBuffer[4]; - this->remoteDevice.setInterface(rxBuffer[4]); + remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - // this->remoteDeviceStruct.protocol = rxBuffer[5]; - this->remoteDevice.setProtocol(rxBuffer[5]); + remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - // this->remoteDeviceStruct.modeTech = rxBuffer[6]; - this->remoteDevice.setModeTech(rxBuffer[6]); + remoteDevice.setModeTech(rxBuffer[6]); pRfIntf->MoreTags = false; - // this->remoteDeviceStruct.moreTagsAvailable = false; - this->remoteDevice.setMoreTagsAvailable(false); - // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - // fillInterfaceInfo(&rxBuffer[10]); - this->remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); + remoteDevice.setMoreTagsAvailable(false); + remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); // P2P /* Verifying if not a P2P device also presenting T4T emulation */ @@ -946,16 +940,14 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint /* Is P2P detected ? */ if (rxBuffer[5] == PROT_NFCDEP) { pRfIntf->Interface = rxBuffer[4]; - this->remoteDevice.setInterface(rxBuffer[4]); + remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - this->remoteDevice.setProtocol(rxBuffer[5]); + remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - this->remoteDevice.setModeTech(rxBuffer[6]); + remoteDevice.setModeTech(rxBuffer[6]); pRfIntf->MoreTags = false; - this->remoteDevice.setMoreTagsAvailable(false); - // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - // fillInterfaceInfo(&rxBuffer[10]); - this->remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); + remoteDevice.setMoreTagsAvailable(false); + remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); break; } } else { @@ -975,13 +967,13 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } } else { /* RF_DISCOVER_NTF */ pRfIntf->Interface = INTF_UNDETERMINED; - this->remoteDevice.setInterface(INTF_UNDETERMINED); + remoteDevice.setInterface(interface.UNDETERMINED); pRfIntf->Protocol = rxBuffer[4]; - this->remoteDevice.setProtocol(rxBuffer[4]); + remoteDevice.setProtocol(rxBuffer[4]); pRfIntf->ModeTech = rxBuffer[5]; - this->remoteDevice.setModeTech(rxBuffer[5]); + remoteDevice.setModeTech(rxBuffer[5]); pRfIntf->MoreTags = true; - this->remoteDevice.setMoreTagsAvailable(true); + remoteDevice.setMoreTagsAvailable(true); /* Get next NTF for further activation */ do { @@ -996,15 +988,15 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint getMessage(100); /* In case of multiple cards, select the first one */ - NCIRfDiscoverSelect[4] = pRfIntf->Protocol; - if (pRfIntf->Protocol == PROT_ISODEP) - NCIRfDiscoverSelect[5] = INTF_ISODEP; - else if (pRfIntf->Protocol == PROT_NFCDEP) - NCIRfDiscoverSelect[5] = INTF_NFCDEP; - else if (pRfIntf->Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; + NCIRfDiscoverSelect[4] = remoteDevice.getProtocol(); + if (remoteDevice.getProtocol() == protocol.ISODEP) + NCIRfDiscoverSelect[5] = interface.ISODEP; + else if (remoteDevice.getProtocol() == protocol.NFCDEP) + NCIRfDiscoverSelect[5] = interface.NFCDEP; + else if (remoteDevice.getProtocol() == protocol.MIFARE) + NCIRfDiscoverSelect[5] = interface.TAGCMD; else - NCIRfDiscoverSelect[5] = INTF_FRAME; + NCIRfDiscoverSelect[5] = interface.FRAME; (void)writeData(NCIRfDiscoverSelect, sizeof(NCIRfDiscoverSelect)); getMessage(100); @@ -1015,18 +1007,16 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint if ((rxBuffer[0] == 0x61) || (rxBuffer[1] == 0x05)) { pRfIntf->Interface = rxBuffer[4]; - this->remoteDevice.setInterface(rxBuffer[4]); + remoteDevice.setInterface(rxBuffer[4]); pRfIntf->Protocol = rxBuffer[5]; - this->remoteDevice.setProtocol(rxBuffer[5]); + remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; - this->remoteDevice.setModeTech(rxBuffer[6]); - // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - // fillInterfaceInfo(&rxBuffer[10]); - this->remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); + remoteDevice.setModeTech(rxBuffer[6]); + remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); } /* In case of P2P target detected but lost, inform application to restart discovery */ - else if (this->remoteDevice.getProtocol() == PROT_NFCDEP) { + else if (remoteDevice.getProtocol() == protocol.NFCDEP) { /* Restart the discovery loop */ (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); getMessage(); @@ -1041,9 +1031,9 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } /* In case of unknown target align protocol information */ - if (pRfIntf->Interface == INTF_UNDETERMINED) { + if (remoteDevice.getInterface() == interface.UNDETERMINED) { pRfIntf->Protocol = PROT_UNDETERMINED; - this->remoteDevice.setProtocol(protocol.UNDETERMINED); + remoteDevice.setProtocol(protocol.UNDETERMINED); } return SUCCESS; @@ -1443,11 +1433,6 @@ void Electroniccats_PN7150::PresenceCheck(RfIntf_t RfIntf) { Electroniccats_PN7150::presenceCheck(RfIntf); } -// Deprecated, use fillInterfaceInfo(uint8_t *pBuf) instead -void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { - Electroniccats_PN7150::fillInterfaceInfo(pRfIntf, pBuf); -} - bool Electroniccats_PN7150::readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize) { bool status = ERROR; uint8_t Cmd[MAX_NCI_FRAME_SIZE]; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 9da1f6d..22d5f75 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -114,9 +114,6 @@ class Electroniccats_PN7150 : public Mode { uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length uint8_t gNfcController_generation = 0; uint8_t gNfcController_fw_version[3] = {0}; - void fillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); - void fillInterfaceInfo(uint8_t *pBuf); - void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); // Deprecated, use fillInterfaceInfo(uint8_t *pBuf) instead public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index 34d354b..caf2e25 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -40,56 +40,76 @@ void RemoteDevice::setMoreTagsAvailable(bool moreTags) { void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; - switch (pRfIntf->ModeTech) { + switch (remoteDeviceStruct.modeTech) { case (MODE_POLL | TECH_PASSIVE_NFCA): memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); memcpy(remoteDeviceStruct.info.nfcAPP.sensRes, &pBuf[0], 2); temp = 2; pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; + remoteDeviceStruct.info.nfcAPP.nfcIdLen = pBuf[temp]; temp++; memcpy(pRfIntf->Info.NFC_APP.NfcId, &pBuf[3], pRfIntf->Info.NFC_APP.NfcIdLen); + memcpy(remoteDeviceStruct.info.nfcAPP.nfcId, &pBuf[3], remoteDeviceStruct.info.nfcAPP.nfcIdLen); temp += pBuf[2]; pRfIntf->Info.NFC_APP.SelResLen = pBuf[temp]; + remoteDeviceStruct.info.nfcAPP.selResLen = pBuf[temp]; temp++; - if (pRfIntf->Info.NFC_APP.SelResLen == 1) + if (remoteDeviceStruct.info.nfcAPP.selResLen == 1) { pRfIntf->Info.NFC_APP.SelRes[0] = pBuf[temp]; + remoteDeviceStruct.info.nfcAPP.selRes[0] = pBuf[temp]; + } temp += 4; if (pBuf[temp] != 0) { temp++; pRfIntf->Info.NFC_APP.RatsLen = pBuf[temp]; + remoteDeviceStruct.info.nfcAPP.ratsLen = pBuf[temp]; memcpy(pRfIntf->Info.NFC_APP.Rats, &pBuf[temp + 1], pBuf[temp]); + memcpy(remoteDeviceStruct.info.nfcAPP.rats, &pBuf[temp + 1], pBuf[temp]); } else { pRfIntf->Info.NFC_APP.RatsLen = 0; + remoteDeviceStruct.info.nfcAPP.ratsLen = 0; } break; case (MODE_POLL | TECH_PASSIVE_NFCB): pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; + remoteDeviceStruct.info.nfcBPP.sensResLen = pBuf[0]; memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); + memcpy(remoteDeviceStruct.info.nfcBPP.sensRes, &pBuf[1], remoteDeviceStruct.info.nfcBPP.sensResLen); temp = pBuf[0] + 4; if (pBuf[temp] != 0) { temp++; pRfIntf->Info.NFC_BPP.AttribResLen = pBuf[temp]; + remoteDeviceStruct.info.nfcBPP.attribResLen = pBuf[temp]; memcpy(pRfIntf->Info.NFC_BPP.AttribRes, &pBuf[temp + 1], pBuf[temp]); + memcpy(remoteDeviceStruct.info.nfcBPP.attribRes, &pBuf[temp + 1], pBuf[temp]); } else { pRfIntf->Info.NFC_BPP.AttribResLen = 0; + remoteDeviceStruct.info.nfcBPP.attribResLen = 0; } break; case (MODE_POLL | TECH_PASSIVE_NFCF): pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; + remoteDeviceStruct.info.nfcFPP.bitRate = pBuf[0]; pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; + remoteDeviceStruct.info.nfcFPP.sensResLen = pBuf[1]; memcpy(pRfIntf->Info.NFC_FPP.SensRes, &pBuf[2], pRfIntf->Info.NFC_FPP.SensResLen); + memcpy(remoteDeviceStruct.info.nfcFPP.sensRes, &pBuf[2], remoteDeviceStruct.info.nfcFPP.sensResLen); break; case (MODE_POLL | TECH_PASSIVE_15693): pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; + remoteDeviceStruct.info.nfcVPP.afi = pBuf[0]; pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; + remoteDeviceStruct.info.nfcVPP.dsfid = pBuf[1]; - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i++) { pRfIntf->Info.NFC_VPP.ID[7 - i] = pBuf[2 + i]; + remoteDeviceStruct.info.nfcVPP.id[7 - i] = pBuf[2 + i]; + } break; diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 87e9ff6..cf3d4aa 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -112,7 +112,7 @@ struct RfIntfCC_t { }; class RemoteDevice { - protected: + private: RfIntfCC_t remoteDeviceStruct; public: From 24b8b82e9c20f401806c2aeea749f866ae5b9a66 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 14 Aug 2023 16:54:37 -0600 Subject: [PATCH 057/106] fix: reset not working on type 4 tags - add stop discovery at the begining of the reset method - update detect tags example - add info properties getters --- examples/DetectTags/DetectTags.ino | 84 ++++++++++++++++-------------- src/Electroniccats_PN7150.cpp | 6 ++- src/RemoteDevice.cpp | 17 ++++-- src/RemoteDevice.h | 33 +++++++----- 4 files changed, 84 insertions(+), 56 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 55ee41b..f082e4a 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -35,21 +35,38 @@ void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data bu Serial.println(); } +String getHexRepresentation(const byte* data, const uint32_t numBytes) { + String hexString; + for (uint32_t szPos = 0; szPos < numBytes; szPos++) { + hexString += "0x"; + if (data[szPos] <= 0xF) + hexString += "0"; + hexString += String(data[szPos] & 0xFF, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + hexString += " "; + } + } + return hexString; +} + void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field char tmp[16]; + int index = 0; + static String nfcID; + while (1) { - switch (RfIntf.Protocol) { // Indetify card protocol - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: + switch (nfc.remoteDevice.getProtocol()) { // Indetify card protocol + case nfc.protocol.T1T: + case nfc.protocol.T2T: + case nfc.protocol.T3T: + case nfc.protocol.ISODEP: Serial.print(" - POLL MODE: Remote activated tag type: "); - Serial.println(RfIntf.Protocol); + Serial.println(nfc.remoteDevice.getProtocol()); break; - case PROT_ISO15693: + case nfc.protocol.ISO15693: Serial.println(" - POLL MODE: Remote ISO15693 card activated"); break; - case PROT_MIFARE: + case nfc.protocol.MIFARE: Serial.println(" - POLL MODE: Remote MIFARE card activated"); break; default: @@ -57,18 +74,21 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s return; } - switch (RfIntf.ModeTech) { // Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology + case (nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCA): Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[0]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getAPPSensRes(index)); Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SensRes[1]); + index++; + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getAPPSensRes(index)); Serial.print(tmp); Serial.println(" "); Serial.print("\tNFCID = "); - PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); + nfcID = getHexRepresentation(nfc.remoteDevice.getAPPID(), nfc.remoteDevice.getAPPIDLen()); + // nfcID = getHexRepresentation(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); + Serial.println(nfcID); if (RfIntf.Info.NFC_APP.SelResLen != 0) { Serial.print("\tSEL_RES = "); @@ -146,35 +166,21 @@ void setup() { } void loop() { - if (!nfc.waitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards + if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards displayCardInfo(RfInterface); - switch (RfInterface.Protocol) { - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: - nfc.processReaderMode(RfInterface, READ_NDEF); - break; - case PROT_ISO15693: - break; - - case PROT_MIFARE: - nfc.processReaderMode(RfInterface, READ_NDEF); - break; - - default: - break; - } - - //* It can detect multiple cards at the same time if they use the same protocol - if (RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + // It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + nfc.activateNextTagDiscovery(); + Serial.println("Multiple cards are detected!"); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("CARD REMOVED!"); + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); + Serial.println("Card removed!"); } - nfc.reset(); + + Serial.println("Restarting..."); + nfc.reset(); + Serial.println("Waiting for a Card..."); delay(500); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index c236d55..c086626 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1392,7 +1392,7 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { do { delay(500); for (i = 0; i < 8; i++) { - NCIPresCheckIso15693[i + 6] = remoteDevice.getVPPID(7 - i); + NCIPresCheckIso15693[i + 6] = remoteDevice.getVPPID()[7 - i]; } (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); getMessage(); @@ -1693,6 +1693,10 @@ bool Electroniccats_PN7150::NxpNci_FactoryTest_RfOn() { } bool Electroniccats_PN7150::reset() { + if (Electroniccats_PN7150::stopDiscovery()) { + return false; + } + if (Electroniccats_PN7150::configMode()) { return false; } diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index caf2e25..25ee902 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -16,9 +16,20 @@ bool RemoteDevice::hasMoreTags() const { return this->remoteDeviceStruct.moreTagsAvailable; } -// TODO: validate memory access -unsigned char RemoteDevice::getVPPID(int position) const { - return this->remoteDeviceStruct.info.nfcVPP.id[position]; +unsigned char RemoteDevice::getAPPSensRes(int index) const { + return this->remoteDeviceStruct.info.nfcAPP.sensRes[index]; +} + +const unsigned char* RemoteDevice::getAPPID() const { + return this->remoteDeviceStruct.info.nfcAPP.nfcId; +} + +unsigned char RemoteDevice::getAPPIDLen() const { + return this->remoteDeviceStruct.info.nfcAPP.nfcIdLen; +} + +const unsigned char* RemoteDevice::getVPPID() const { + return this->remoteDeviceStruct.info.nfcVPP.id; } void RemoteDevice::setInterface(unsigned char interface) { diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index cf3d4aa..7ed1fcc 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -14,53 +14,53 @@ /* POLL passive type A */ struct RfIntf_info_APP_t { unsigned char SensRes[2]; - unsigned char NfcIdLen; unsigned char NfcId[10]; - unsigned char SelResLen; + unsigned char NfcIdLen; unsigned char SelRes[1]; - unsigned char RatsLen; + unsigned char SelResLen; unsigned char Rats[20]; + unsigned char RatsLen; }; /* POLL passive type A camelCase */ struct RfIntfInfoAppCC_t { unsigned char sensRes[2]; - unsigned char nfcIdLen; unsigned char nfcId[10]; - unsigned char selResLen; + unsigned char nfcIdLen; unsigned char selRes[1]; - unsigned char ratsLen; + unsigned char selResLen; unsigned char rats[20]; + unsigned char ratsLen; }; /* POLL passive type B */ struct RfIntf_info_BPP_t { - unsigned char SensResLen; unsigned char SensRes[12]; - unsigned char AttribResLen; + unsigned char SensResLen; unsigned char AttribRes[17]; + unsigned char AttribResLen; }; /* POLL passive type B camelCase */ struct RfIntfInfoBppCC_t { - unsigned char sensResLen; unsigned char sensRes[12]; - unsigned char attribResLen; + unsigned char sensResLen; unsigned char attribRes[17]; + unsigned char attribResLen; }; /* POLL passive type F */ struct RfIntf_info_FPP_t { unsigned char BitRate; - unsigned char SensResLen; unsigned char SensRes[18]; + unsigned char SensResLen; }; /* POLL passive type F camelCase */ struct RfIntfInfoFppCC_t { unsigned char bitRate; - unsigned char sensResLen; unsigned char sensRes[18]; + unsigned char sensResLen; }; /* POLL passive type ISO15693 */ @@ -120,7 +120,14 @@ class RemoteDevice { unsigned char getProtocol() const; unsigned char getModeTech() const; bool hasMoreTags() const; - unsigned char getVPPID(int position) const; + unsigned char getAPPSensRes(int index) const; + const unsigned char* getAPPID() const; + unsigned char getAPPIDLen() const; + unsigned char getAPPSelRes(int index) const; + unsigned char getAPPSelResLen() const; + unsigned char getAPPRats(int index) const; + unsigned char getAPPRatsLen() const; + const unsigned char* getVPPID() const; void setInterface(unsigned char interface); void setProtocol(unsigned char protocol); void setModeTech(unsigned char modeTech); From eb824f748e8919c604b61b1b94e07be1fe470fe9 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 14 Aug 2023 19:58:34 -0600 Subject: [PATCH 058/106] feat: add nfc A property getters --- examples/DetectTags/DetectTags.ino | 22 ++++++++++------------ src/RemoteDevice.cpp | 25 +++++++++++++++++++++++-- src/RemoteDevice.h | 10 +++++++--- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index f082e4a..818ca0f 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -52,6 +52,8 @@ String getHexRepresentation(const byte* data, const uint32_t numBytes) { void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field char tmp[16]; int index = 0; + static String sensRes; + static String sensResLen; static String nfcID; while (1) { @@ -77,24 +79,19 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology case (nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCA): Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X", nfc.remoteDevice.getAPPSensRes(index)); - Serial.print(tmp); - Serial.print(" "); - index++; - sprintf(tmp, "0x%.2X", nfc.remoteDevice.getAPPSensRes(index)); - Serial.print(tmp); - Serial.println(" "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getAPPSensRes(), nfc.remoteDevice.getAPPSensResLen())); + Serial.println("\tSENS_RES Length = " + String(nfc.remoteDevice.getAPPSensResLen()) + " bytes"); Serial.print("\tNFCID = "); nfcID = getHexRepresentation(nfc.remoteDevice.getAPPID(), nfc.remoteDevice.getAPPIDLen()); - // nfcID = getHexRepresentation(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); Serial.println(nfcID); + Serial.println("\tNFCID Length = " + String(nfc.remoteDevice.getAPPIDLen()) + " bytes"); - if (RfIntf.Info.NFC_APP.SelResLen != 0) { + // if (RfIntf.Info.NFC_APP.SelResLen != 0) { + if (nfc.remoteDevice.getAPPSelResLen() != 0) { Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X", RfIntf.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); - Serial.println(" "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getAPPSelRes(), nfc.remoteDevice.getAPPSelResLen())); + Serial.println("\tSEL_RES Length; = " + String(nfc.remoteDevice.getAPPSelResLen()) + " bytes"); } break; @@ -166,6 +163,7 @@ void setup() { } void loop() { + // TODO: invert the logic to make it more readable if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards displayCardInfo(RfInterface); diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index 25ee902..ea1540e 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -16,8 +16,12 @@ bool RemoteDevice::hasMoreTags() const { return this->remoteDeviceStruct.moreTagsAvailable; } -unsigned char RemoteDevice::getAPPSensRes(int index) const { - return this->remoteDeviceStruct.info.nfcAPP.sensRes[index]; +const unsigned char* RemoteDevice::getAPPSensRes() const { + return this->remoteDeviceStruct.info.nfcAPP.sensRes; +} + +unsigned char RemoteDevice::getAPPSensResLen() const { + return this->remoteDeviceStruct.info.nfcAPP.sensResLen; } const unsigned char* RemoteDevice::getAPPID() const { @@ -28,6 +32,22 @@ unsigned char RemoteDevice::getAPPIDLen() const { return this->remoteDeviceStruct.info.nfcAPP.nfcIdLen; } +const unsigned char* RemoteDevice::getAPPSelRes() const { + return this->remoteDeviceStruct.info.nfcAPP.selRes; +} + +unsigned char RemoteDevice::getAPPSelResLen() const { + return this->remoteDeviceStruct.info.nfcAPP.selResLen; +} + +const unsigned char* RemoteDevice::getAPPRats() const { + return this->remoteDeviceStruct.info.nfcAPP.rats; +} + +unsigned char RemoteDevice::getAPPRatsLen() const { + return this->remoteDeviceStruct.info.nfcAPP.ratsLen; +} + const unsigned char* RemoteDevice::getVPPID() const { return this->remoteDeviceStruct.info.nfcVPP.id; } @@ -55,6 +75,7 @@ void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { case (MODE_POLL | TECH_PASSIVE_NFCA): memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); memcpy(remoteDeviceStruct.info.nfcAPP.sensRes, &pBuf[0], 2); + remoteDeviceStruct.info.nfcAPP.sensResLen = 2; temp = 2; pRfIntf->Info.NFC_APP.NfcIdLen = pBuf[temp]; remoteDeviceStruct.info.nfcAPP.nfcIdLen = pBuf[temp]; diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 7ed1fcc..0846e2c 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -25,6 +25,7 @@ struct RfIntf_info_APP_t { /* POLL passive type A camelCase */ struct RfIntfInfoAppCC_t { unsigned char sensRes[2]; + unsigned char sensResLen; unsigned char nfcId[10]; unsigned char nfcIdLen; unsigned char selRes[1]; @@ -120,14 +121,17 @@ class RemoteDevice { unsigned char getProtocol() const; unsigned char getModeTech() const; bool hasMoreTags() const; - unsigned char getAPPSensRes(int index) const; + const unsigned char* getAPPSensRes() const; + unsigned char getAPPSensResLen() const; const unsigned char* getAPPID() const; unsigned char getAPPIDLen() const; - unsigned char getAPPSelRes(int index) const; + const unsigned char* getAPPSelRes() const; unsigned char getAPPSelResLen() const; - unsigned char getAPPRats(int index) const; + const unsigned char* getAPPRats() const; unsigned char getAPPRatsLen() const; const unsigned char* getVPPID() const; + /**************************************/ + const unsigned char* getSensRes() const; void setInterface(unsigned char interface); void setProtocol(unsigned char protocol); void setModeTech(unsigned char modeTech); From 5a253a0cd8d7fc4fb2e3bfef68c3d208f3b4484c Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 15 Aug 2023 11:11:14 -0600 Subject: [PATCH 059/106] feat: add generic sens res getter --- examples/DetectTags/DetectTags.ino | 5 +--- src/Electroniccats_PN7150.h | 2 +- src/RemoteDevice.cpp | 45 ++++++++++++++++++++++++++++++ src/RemoteDevice.h | 6 +++- src/{Technology.h => Tech.h} | 5 ++-- 5 files changed, 55 insertions(+), 8 deletions(-) rename src/{Technology.h => Tech.h} (87%) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 818ca0f..a0f23b0 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -79,19 +79,16 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology case (nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCA): Serial.print("\tSENS_RES = "); - Serial.println(getHexRepresentation(nfc.remoteDevice.getAPPSensRes(), nfc.remoteDevice.getAPPSensResLen())); - Serial.println("\tSENS_RES Length = " + String(nfc.remoteDevice.getAPPSensResLen()) + " bytes"); + Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); Serial.print("\tNFCID = "); nfcID = getHexRepresentation(nfc.remoteDevice.getAPPID(), nfc.remoteDevice.getAPPIDLen()); Serial.println(nfcID); - Serial.println("\tNFCID Length = " + String(nfc.remoteDevice.getAPPIDLen()) + " bytes"); // if (RfIntf.Info.NFC_APP.SelResLen != 0) { if (nfc.remoteDevice.getAPPSelResLen() != 0) { Serial.print("\tSEL_RES = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getAPPSelRes(), nfc.remoteDevice.getAPPSelResLen())); - Serial.println("\tSEL_RES Length; = " + String(nfc.remoteDevice.getAPPSelResLen()) + " bytes"); } break; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 22d5f75..313c4d0 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -120,7 +120,7 @@ class Electroniccats_PN7150 : public Mode { uint8_t begin(void); RemoteDevice remoteDevice; Protocol protocol; - Technology tech; + Tech tech; ModeTech modeTech; Interface interface; bool hasMessage() const; diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index ea1540e..fe38034 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -52,6 +52,51 @@ const unsigned char* RemoteDevice::getVPPID() const { return this->remoteDeviceStruct.info.nfcVPP.id; } +const unsigned char* RemoteDevice::getSensRes() const { + switch (remoteDeviceStruct.modeTech) { + case (modeTech.POLL | tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.sensRes; + break; + + case (modeTech.POLL | tech.PASSIVE_NFCB): + return this->remoteDeviceStruct.info.nfcBPP.sensRes; + break; + + case (modeTech.POLL | tech.PASSIVE_NFCF): + return this->remoteDeviceStruct.info.nfcFPP.sensRes; + break; + + case (modeTech.POLL | tech.PASSIVE_NFCV): + return this->remoteDeviceStruct.info.nfcVPP.id; + break; + + default: + return NULL; + break; + } +} + +unsigned char RemoteDevice::getSensResLen() const { + switch (remoteDeviceStruct.modeTech) { + case (modeTech.POLL | tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.sensResLen; + break; + + case (modeTech.POLL | tech.PASSIVE_NFCB): + return this->remoteDeviceStruct.info.nfcBPP.sensResLen; + break; + + case (modeTech.POLL | tech.PASSIVE_NFCF): + return this->remoteDeviceStruct.info.nfcFPP.sensResLen; + break; + + case (modeTech.POLL | tech.PASSIVE_NFCV): + default: + return 0; + break; + } +} + void RemoteDevice::setInterface(unsigned char interface) { this->remoteDeviceStruct.interface = interface; } diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 0846e2c..5080e54 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -5,7 +5,7 @@ #include "Interface.h" #include "ModeTech.h" #include "Protocol.h" -#include "Technology.h" +#include "Tech.h" /* * Definition of discovered remote device properties information @@ -115,6 +115,8 @@ struct RfIntfCC_t { class RemoteDevice { private: RfIntfCC_t remoteDeviceStruct; + Tech tech; + ModeTech modeTech; public: unsigned char getInterface() const; @@ -131,7 +133,9 @@ class RemoteDevice { unsigned char getAPPRatsLen() const; const unsigned char* getVPPID() const; /**************************************/ + // TODO: add generic getters const unsigned char* getSensRes() const; + unsigned char getSensResLen() const; void setInterface(unsigned char interface); void setProtocol(unsigned char protocol); void setModeTech(unsigned char modeTech); diff --git a/src/Technology.h b/src/Tech.h similarity index 87% rename from src/Technology.h rename to src/Tech.h index 4bea4b2..6757fbd 100644 --- a/src/Technology.h +++ b/src/Tech.h @@ -11,7 +11,7 @@ #define TECH_ACTIVE_NFCF 5 #define TECH_PASSIVE_15693 6 -class Technology { +class Tech { public: enum Value { PASSIVE_NFCA = 0, @@ -19,7 +19,8 @@ class Technology { PASSIVE_NFCF = 2, ACTIVE_NFCA = 3, ACTIVE_NFCF = 5, - PASSIVE_15693 = 6 + PASSIVE_15693 = 6, + PASSIVE_NFCV = 6 }; }; From aab29457d822a170dab9ae252e89f29ba5e56038 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 15 Aug 2023 17:28:07 -0600 Subject: [PATCH 060/106] feat: add all device info generic getters --- examples/DetectTags/DetectTags.ino | 77 +++++---- examples/NDEFReceive/NDEFReceive.ino | 2 +- src/Electroniccats_PN7150.cpp | 4 +- src/RemoteDevice.cpp | 230 ++++++++++++++++++++++----- src/RemoteDevice.h | 26 +-- 5 files changed, 249 insertions(+), 90 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index a0f23b0..2f5e101 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -37,6 +37,11 @@ void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data bu String getHexRepresentation(const byte* data, const uint32_t numBytes) { String hexString; + + if (numBytes == 0) { + hexString = "null"; + } + for (uint32_t szPos = 0; szPos < numBytes; szPos++) { hexString += "0x"; if (data[szPos] <= 0xF) @@ -51,12 +56,8 @@ String getHexRepresentation(const byte* data, const uint32_t numBytes) { void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field char tmp[16]; - int index = 0; - static String sensRes; - static String sensResLen; - static String nfcID; - while (1) { + while (true) { switch (nfc.remoteDevice.getProtocol()) { // Indetify card protocol case nfc.protocol.T1T: case nfc.protocol.T2T: @@ -77,56 +78,64 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s } switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology - case (nfc.modeTech.POLL | nfc.tech.PASSIVE_NFCA): - Serial.print("\tSENS_RES = "); + case (nfc.tech.PASSIVE_NFCA): + Serial.println("Technology: NFC-A"); + Serial.print("\tSENS RES = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); - Serial.print("\tNFCID = "); - nfcID = getHexRepresentation(nfc.remoteDevice.getAPPID(), nfc.remoteDevice.getAPPIDLen()); - Serial.println(nfcID); + Serial.print("\tNFC ID = "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getNFCID(), nfc.remoteDevice.getNFCIDLen())); + + Serial.print("\tSEL RES = "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getSelRes(), nfc.remoteDevice.getSelResLen())); - // if (RfIntf.Info.NFC_APP.SelResLen != 0) { - if (nfc.remoteDevice.getAPPSelResLen() != 0) { - Serial.print("\tSEL_RES = "); - Serial.println(getHexRepresentation(nfc.remoteDevice.getAPPSelRes(), nfc.remoteDevice.getAPPSelResLen())); - } break; - case (MODE_POLL | TECH_PASSIVE_NFCB): - if (RfIntf.Info.NFC_BPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_BPP.SensRes, RfIntf.Info.NFC_BPP.SensResLen); - } + case (nfc.tech.PASSIVE_NFCB): + Serial.println("Technology: NFC-B"); + Serial.print("\tSENS RES = "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); + + Serial.println("\tAttrib RES = "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getAttribRes(), nfc.remoteDevice.getAttribResLen())); + break; - case (MODE_POLL | TECH_PASSIVE_NFCF): + case (nfc.tech.PASSIVE_NFCF): + Serial.println("Technology: NFC-F"); + Serial.print("\tSENS RES = "); + Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); + Serial.print("\tBitrate = "); - Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); + Serial.println((nfc.remoteDevice.getBitRate() == 1) ? "212" : "424"); - if (RfIntf.Info.NFC_FPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_FPP.SensRes, RfIntf.Info.NFC_FPP.SensResLen); - } break; - case (MODE_POLL | TECH_PASSIVE_15693): + case (nfc.tech.PASSIVE_NFCV): + Serial.println("Technology: NFC-V"); Serial.print("\tID = "); - PrintBuf(RfIntf.Info.NFC_VPP.ID, sizeof(RfIntf.Info.NFC_VPP.ID)); + Serial.println(getHexRepresentation(nfc.remoteDevice.getID(), sizeof(nfc.remoteDevice.getID()))); Serial.print("\ntAFI = "); - Serial.println(RfIntf.Info.NFC_VPP.AFI); + Serial.println(nfc.remoteDevice.getAFI()); - Serial.print("\tDSFID = "); - Serial.println(RfIntf.Info.NFC_VPP.DSFID, HEX); + Serial.print("\tDSF ID = "); + Serial.println(nfc.remoteDevice.getDSFID(), HEX); break; default: break; } - if (RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology - if (nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; - } else + + // It can detect multiple cards at the same time if they are the same technology + if (nfc.remoteDevice.hasMoreTags()) { + Serial.println("Multiple cards are detected!"); + if (!nfc.activateNextTagDiscovery()) { + break; // Can't activate next tag + } + } else { break; + } } } diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 2d3a48b..ee4ff85 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -78,7 +78,7 @@ void loop() { break; } - // It can detect multiple cards at the same time if they use the same protocol + // It can detect multiple cards at the same time if they are the same technology if (nfc.remoteDevice.hasMoreTags()) { nfc.activateNextTagDiscovery(); Serial.println("Multiple cards are detected!"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index c086626..01e891b 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1392,7 +1392,7 @@ void Electroniccats_PN7150::presenceCheck(RfIntf_t RfIntf) { do { delay(500); for (i = 0; i < 8; i++) { - NCIPresCheckIso15693[i + 6] = remoteDevice.getVPPID()[7 - i]; + NCIPresCheckIso15693[i + 6] = remoteDevice.getID()[7 - i]; } (void)writeData(NCIPresCheckIso15693, sizeof(NCIPresCheckIso15693)); getMessage(); @@ -1550,7 +1550,7 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { } bool Electroniccats_PN7150::activateNextTagDiscovery() { - return Electroniccats_PN7150::readerActivateNext(&this->dummyRfInterface); + return !Electroniccats_PN7150::readerActivateNext(&this->dummyRfInterface); } // Deprecated, use readerActivateNext() instead diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index fe38034..d630404 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -16,87 +16,233 @@ bool RemoteDevice::hasMoreTags() const { return this->remoteDeviceStruct.moreTagsAvailable; } -const unsigned char* RemoteDevice::getAPPSensRes() const { - return this->remoteDeviceStruct.info.nfcAPP.sensRes; -} +// Getters for device information + +const unsigned char* RemoteDevice::getSensRes() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.sensRes; + break; + + case (tech.PASSIVE_NFCB): + return this->remoteDeviceStruct.info.nfcBPP.sensRes; + break; -unsigned char RemoteDevice::getAPPSensResLen() const { - return this->remoteDeviceStruct.info.nfcAPP.sensResLen; + case (tech.PASSIVE_NFCF): + return this->remoteDeviceStruct.info.nfcFPP.sensRes; + break; + + case (tech.PASSIVE_NFCV): + return this->remoteDeviceStruct.info.nfcVPP.id; + break; + + default: + return NULL; + break; + } } -const unsigned char* RemoteDevice::getAPPID() const { - return this->remoteDeviceStruct.info.nfcAPP.nfcId; +unsigned char RemoteDevice::getSensResLen() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.sensResLen; + break; + + case (tech.PASSIVE_NFCB): + return this->remoteDeviceStruct.info.nfcBPP.sensResLen; + break; + + case (tech.PASSIVE_NFCF): + return this->remoteDeviceStruct.info.nfcFPP.sensResLen; + break; + + case (tech.PASSIVE_NFCV): + default: + return 0; + break; + } } -unsigned char RemoteDevice::getAPPIDLen() const { - return this->remoteDeviceStruct.info.nfcAPP.nfcIdLen; +const unsigned char* RemoteDevice::getNFCID() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.nfcId; + break; + + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return NULL; + break; + } } -const unsigned char* RemoteDevice::getAPPSelRes() const { - return this->remoteDeviceStruct.info.nfcAPP.selRes; +unsigned char RemoteDevice::getNFCIDLen() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.nfcIdLen; + break; + + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return 0; + break; + } } -unsigned char RemoteDevice::getAPPSelResLen() const { - return this->remoteDeviceStruct.info.nfcAPP.selResLen; +const unsigned char* RemoteDevice::getSelRes() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.selRes; + break; + + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return NULL; + break; + } } -const unsigned char* RemoteDevice::getAPPRats() const { - return this->remoteDeviceStruct.info.nfcAPP.rats; +unsigned char RemoteDevice::getSelResLen() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.selResLen; + break; + + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return 0; + break; + } } -unsigned char RemoteDevice::getAPPRatsLen() const { - return this->remoteDeviceStruct.info.nfcAPP.ratsLen; +const unsigned char* RemoteDevice::getRats() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.rats; + break; + + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return NULL; + break; + } } -const unsigned char* RemoteDevice::getVPPID() const { - return this->remoteDeviceStruct.info.nfcVPP.id; +unsigned char RemoteDevice::getRatsLen() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCA): + return this->remoteDeviceStruct.info.nfcAPP.ratsLen; + break; + + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return 0; + break; + } } -const unsigned char* RemoteDevice::getSensRes() const { +const unsigned char* RemoteDevice::getAttribRes() const { switch (remoteDeviceStruct.modeTech) { - case (modeTech.POLL | tech.PASSIVE_NFCA): - return this->remoteDeviceStruct.info.nfcAPP.sensRes; + case (tech.PASSIVE_NFCB): + return this->remoteDeviceStruct.info.nfcBPP.attribRes; break; - - case (modeTech.POLL | tech.PASSIVE_NFCB): - return this->remoteDeviceStruct.info.nfcBPP.sensRes; + + case (tech.PASSIVE_NFCA): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return NULL; break; + } +} - case (modeTech.POLL | tech.PASSIVE_NFCF): - return this->remoteDeviceStruct.info.nfcFPP.sensRes; +unsigned char RemoteDevice::getAttribResLen() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCB): + return this->remoteDeviceStruct.info.nfcBPP.attribResLen; break; - case (modeTech.POLL | tech.PASSIVE_NFCV): - return this->remoteDeviceStruct.info.nfcVPP.id; + case (tech.PASSIVE_NFCA): + case (tech.PASSIVE_NFCF): + case (tech.PASSIVE_NFCV): + default: + return 0; + break; + } +} + +unsigned char RemoteDevice::getBitRate() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCF): + return this->remoteDeviceStruct.info.nfcFPP.bitRate; break; + case (tech.PASSIVE_NFCA): + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCV): default: return NULL; break; } } -unsigned char RemoteDevice::getSensResLen() const { +unsigned char RemoteDevice::getAFI() const { switch (remoteDeviceStruct.modeTech) { - case (modeTech.POLL | tech.PASSIVE_NFCA): - return this->remoteDeviceStruct.info.nfcAPP.sensResLen; + case (tech.PASSIVE_NFCV): + return this->remoteDeviceStruct.info.nfcVPP.afi; break; - - case (modeTech.POLL | tech.PASSIVE_NFCB): - return this->remoteDeviceStruct.info.nfcBPP.sensResLen; + + case (tech.PASSIVE_NFCA): + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + default: + return 0; break; + } +} - case (modeTech.POLL | tech.PASSIVE_NFCF): - return this->remoteDeviceStruct.info.nfcFPP.sensResLen; +unsigned char RemoteDevice::getDSFID() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCV): + return this->remoteDeviceStruct.info.nfcVPP.dsfid; break; - case (modeTech.POLL | tech.PASSIVE_NFCV): + case (tech.PASSIVE_NFCA): + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): default: return 0; break; } } +const unsigned char* RemoteDevice::getID() const { + switch (remoteDeviceStruct.modeTech) { + case (tech.PASSIVE_NFCV): + return this->remoteDeviceStruct.info.nfcVPP.id; + break; + + case (tech.PASSIVE_NFCA): + case (tech.PASSIVE_NFCB): + case (tech.PASSIVE_NFCF): + default: + return NULL; + break; + } +} + void RemoteDevice::setInterface(unsigned char interface) { this->remoteDeviceStruct.interface = interface; } @@ -117,7 +263,7 @@ void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; switch (remoteDeviceStruct.modeTech) { - case (MODE_POLL | TECH_PASSIVE_NFCA): + case (tech.PASSIVE_NFCA): memcpy(pRfIntf->Info.NFC_APP.SensRes, &pBuf[0], 2); memcpy(remoteDeviceStruct.info.nfcAPP.sensRes, &pBuf[0], 2); remoteDeviceStruct.info.nfcAPP.sensResLen = 2; @@ -150,7 +296,7 @@ void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { } break; - case (MODE_POLL | TECH_PASSIVE_NFCB): + case (tech.PASSIVE_NFCB): pRfIntf->Info.NFC_BPP.SensResLen = pBuf[0]; remoteDeviceStruct.info.nfcBPP.sensResLen = pBuf[0]; memcpy(pRfIntf->Info.NFC_BPP.SensRes, &pBuf[1], pRfIntf->Info.NFC_BPP.SensResLen); @@ -168,7 +314,7 @@ void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { } break; - case (MODE_POLL | TECH_PASSIVE_NFCF): + case (tech.PASSIVE_NFCF): pRfIntf->Info.NFC_FPP.BitRate = pBuf[0]; remoteDeviceStruct.info.nfcFPP.bitRate = pBuf[0]; pRfIntf->Info.NFC_FPP.SensResLen = pBuf[1]; @@ -177,7 +323,7 @@ void RemoteDevice::setInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { memcpy(remoteDeviceStruct.info.nfcFPP.sensRes, &pBuf[2], remoteDeviceStruct.info.nfcFPP.sensResLen); break; - case (MODE_POLL | TECH_PASSIVE_15693): + case (tech.PASSIVE_NFCV): pRfIntf->Info.NFC_VPP.AFI = pBuf[0]; remoteDeviceStruct.info.nfcVPP.afi = pBuf[0]; pRfIntf->Info.NFC_VPP.DSFID = pBuf[1]; diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 5080e54..39b7c36 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -119,23 +119,27 @@ class RemoteDevice { ModeTech modeTech; public: + // Getters for device properties unsigned char getInterface() const; unsigned char getProtocol() const; unsigned char getModeTech() const; bool hasMoreTags() const; - const unsigned char* getAPPSensRes() const; - unsigned char getAPPSensResLen() const; - const unsigned char* getAPPID() const; - unsigned char getAPPIDLen() const; - const unsigned char* getAPPSelRes() const; - unsigned char getAPPSelResLen() const; - const unsigned char* getAPPRats() const; - unsigned char getAPPRatsLen() const; - const unsigned char* getVPPID() const; - /**************************************/ - // TODO: add generic getters + // Getters for device information properties const unsigned char* getSensRes() const; unsigned char getSensResLen() const; + const unsigned char* getNFCID() const; + unsigned char getNFCIDLen() const; + const unsigned char* getSelRes() const; + unsigned char getSelResLen() const; + const unsigned char* getRats() const; + unsigned char getRatsLen() const; + const unsigned char* getAttribRes() const; + unsigned char getAttribResLen() const; + unsigned char getBitRate() const; + unsigned char getAFI() const; + unsigned char getDSFID() const; + const unsigned char* getID() const; + // Setters void setInterface(unsigned char interface); void setProtocol(unsigned char protocol); void setModeTech(unsigned char modeTech); From dc51880f3275c00798bd77f0c3bdd36e1a17036c Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 16 Aug 2023 12:07:55 -0600 Subject: [PATCH 061/106] feat: remove rf interface struct --- examples/DetectTags/DetectTags.ino | 122 +++++++++++++---------------- 1 file changed, 55 insertions(+), 67 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 2f5e101..0131910 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -18,21 +18,59 @@ #define PN7150_ADDR (0x28) Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Intarface to save data for multiple tags -void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format - uint32_t szPos; - for (szPos = 0; szPos < numBytes; szPos++) { - Serial.print(F("0x")); - // Append leading 0 for small values - if (data[szPos] <= 0xF) - Serial.print(F("0")); - Serial.print(data[szPos] & 0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) { - Serial.print(F(" ")); +// Function prototypes +String getHexRepresentation(const byte* data, const uint32_t numBytes); +void displayCardInfo(); + +void setup() { + Serial.begin(9600); + while (!Serial) + ; + Serial.println("Detect NFC tags with PN7150"); + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board + Serial.println("Error while setting up the mode, check connections!"); + while (1) + ; + } + + if (nfc.configureSettings()) { + Serial.println("The Configure Settings is failed!"); + while (1) + ; + } + + // Read/Write mode as default + if (nfc.configMode()) { // Set up the configuration mode + Serial.println("The Configure Mode is failed!!"); + while (1) + ; + } + nfc.startDiscovery(); // NCI Discovery mode + Serial.println("Waiting for an Card ..."); +} + +void loop() { + // TODO: invert the logic to make it more readable + if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards + displayCardInfo(); + + // It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + nfc.activateNextTagDiscovery(); + Serial.println("Multiple cards are detected!"); } + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); + Serial.println("Card removed!"); } - Serial.println(); + + Serial.println("Restarting..."); + nfc.reset(); + Serial.println("Waiting for a Card..."); + delay(500); } String getHexRepresentation(const byte* data, const uint32_t numBytes) { @@ -54,7 +92,7 @@ String getHexRepresentation(const byte* data, const uint32_t numBytes) { return hexString; } -void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s in te field +void displayCardInfo() { // Funtion in charge to show the card/s in te field char tmp[16]; while (true) { @@ -79,7 +117,7 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology case (nfc.tech.PASSIVE_NFCA): - Serial.println("Technology: NFC-A"); + Serial.println("\tTechnology: NFC-A"); Serial.print("\tSENS RES = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); @@ -92,7 +130,7 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s break; case (nfc.tech.PASSIVE_NFCB): - Serial.println("Technology: NFC-B"); + Serial.println("\tTechnology: NFC-B"); Serial.print("\tSENS RES = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); @@ -102,7 +140,7 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s break; case (nfc.tech.PASSIVE_NFCF): - Serial.println("Technology: NFC-F"); + Serial.println("\tTechnology: NFC-F"); Serial.print("\tSENS RES = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getSensRes(), nfc.remoteDevice.getSensResLen())); @@ -112,7 +150,7 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s break; case (nfc.tech.PASSIVE_NFCV): - Serial.println("Technology: NFC-V"); + Serial.println("\tTechnology: NFC-V"); Serial.print("\tID = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getID(), sizeof(nfc.remoteDevice.getID()))); @@ -138,53 +176,3 @@ void displayCardInfo(RfIntf_t RfIntf) { // Funtion in charge to show the card/s } } } - -void setup() { - Serial.begin(9600); - while (!Serial) - ; - Serial.println("Detect NFC tags with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { // Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (1) - ; - } - - if (nfc.configureSettings()) { - Serial.println("The Configure Settings is failed!"); - while (1) - ; - } - - // Read/Write mode as default - if (nfc.configMode()) { // Set up the configuration mode - Serial.println("The Configure Mode is failed!!"); - while (1) - ; - } - nfc.startDiscovery(); // NCI Discovery mode - Serial.println("Waiting for an Card ..."); -} - -void loop() { - // TODO: invert the logic to make it more readable - if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards - displayCardInfo(RfInterface); - - // It can detect multiple cards at the same time if they use the same protocol - if (nfc.remoteDevice.hasMoreTags()) { - nfc.activateNextTagDiscovery(); - Serial.println("Multiple cards are detected!"); - } - Serial.println("Remove the Card"); - nfc.waitForTagRemoval(); - Serial.println("Card removed!"); - } - - Serial.println("Restarting..."); - nfc.reset(); - Serial.println("Waiting for a Card..."); - delay(500); -} From 17fa8917ed1d5f43562bf48389bae97da9612642 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 16 Aug 2023 16:45:47 -0600 Subject: [PATCH 062/106] feat: add set msg callback method --- examples/NDEFReceive/NDEFReceive.ino | 32 ++++++---------------------- examples/NDEFSend/NDEFSend.ino | 2 +- src/Electroniccats_PN7150.cpp | 4 ++++ src/Electroniccats_PN7150.h | 1 + 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index ee4ff85..fdb074e 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -3,39 +3,21 @@ #define PN7150_VEN (13) #define PN7150_ADDR (0x28) +// Function prototypes +void displayDeviceInfo(); +void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize); + // Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); -const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF - 0x01, // Type length (1 byte) - 0x08, // Payload length - 'T', // Type -> 'T' for text, 'U' for URI - 0x02, // Status - 'e', 'n', // Language - 'H', 'e', 'l', 'l', 'o'}; // Message Payload - -void PrintBuf(const byte *data, const uint32_t numBytes) { // Print hex data buffer in format - uint32_t szPos; - for (szPos = 0; szPos < numBytes; szPos++) { - Serial.print(F("0x")); - // Append leading 0 for small values - if (data[szPos] <= 0xF) - Serial.print(F("0")); - Serial.print(data[szPos] & 0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) { - Serial.print(F(" ")); - } - } - Serial.println(); -} - void setup() { Serial.begin(9600); while (!Serial) ; Serial.println("Detect NFC tags with PN7150"); - RW_NDEF_RegisterPullCallback((void *)*ndefPull_Cb); + // Register a callback function to be called when an NDEF message is received + nfc.setSendMsgCallback(ndefCallback); Serial.println("Initializing..."); if (nfc.connectNCI()) { // Wake up the board @@ -166,7 +148,7 @@ void displayDeviceInfo() { } } -void ndefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { +void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { unsigned char *pNdefRecord = pNdefMessage; NdefRecord_t NdefRecord; unsigned char save; diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index a8da1e0..73dc800 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -82,7 +82,7 @@ void loop() { } void checkReaders() { - // Serial.print("."); + Serial.print(""); unsigned long startTime = millis(); if (nfc.cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 01e891b..493b930 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1731,3 +1731,7 @@ bool Electroniccats_PN7150::setP2PMode() { } return true; } + +void Electroniccats_PN7150::setSendMsgCallback(RW_NDEF_Callback_t function) { + RW_NDEF_RegisterPullCallback((void *)*function); +} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 313c4d0..04f3cee 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -179,6 +179,7 @@ class Electroniccats_PN7150 : public Mode { bool setReaderWriterMode(); bool setEmulationMode(); bool setP2PMode(); + void setSendMsgCallback(RW_NDEF_Callback_t function); }; #endif From 8d1c3f7e5c13cf6f320d0a2257cbe44133eebaf8 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 16 Aug 2023 17:52:20 -0600 Subject: [PATCH 063/106] feat: add custom callback --- examples/NDEFReceive/NDEFReceive.ino | 8 +- src/Electroniccats_PN7150.cpp | 5 +- src/Electroniccats_PN7150.h | 2 +- src/RW_NDEF.cpp | 131 +++++++++++++-------------- src/RW_NDEF.h | 37 ++++---- src/RW_NDEF_MIFARE.cpp | 7 +- 6 files changed, 99 insertions(+), 91 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index fdb074e..1fd1a30 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -6,6 +6,7 @@ // Function prototypes void displayDeviceInfo(); void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize); +void customNdefCallback(); // Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); @@ -17,7 +18,8 @@ void setup() { Serial.println("Detect NFC tags with PN7150"); // Register a callback function to be called when an NDEF message is received - nfc.setSendMsgCallback(ndefCallback); + // RW_NDEF_RegisterPullCallback((void *)*ndefCallback); + nfc.setSendMsgCallback(customNdefCallback); Serial.println("Initializing..."); if (nfc.connectNCI()) { // Wake up the board @@ -261,3 +263,7 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { Serial.println(""); } + +void customNdefCallback() { + Serial.println("Processing Callback..."); +} diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 493b930..2e5885f 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1732,6 +1732,7 @@ bool Electroniccats_PN7150::setP2PMode() { return true; } -void Electroniccats_PN7150::setSendMsgCallback(RW_NDEF_Callback_t function) { - RW_NDEF_RegisterPullCallback((void *)*function); +void Electroniccats_PN7150::setSendMsgCallback(CustomCallback_t function) { + // RW_NDEF_RegisterPullCallback((void *)*function); + registerNdefReceivedCallback(function); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 04f3cee..23ccdbc 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -179,7 +179,7 @@ class Electroniccats_PN7150 : public Mode { bool setReaderWriterMode(); bool setEmulationMode(); bool setP2PMode(); - void setSendMsgCallback(RW_NDEF_Callback_t function); + void setSendMsgCallback(CustomCallback_t function); }; #endif diff --git a/src/RW_NDEF.cpp b/src/RW_NDEF.cpp index b12cc50..62968ee 100644 --- a/src/RW_NDEF.cpp +++ b/src/RW_NDEF.cpp @@ -1,25 +1,24 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ -//#ifdef RW_SUPPORT -//#ifndef NO_NDEF_SUPPORT #include "RW_NDEF.h" + +#include "RW_NDEF_MIFARE.h" #include "RW_NDEF_T1T.h" #include "RW_NDEF_T2T.h" #include "RW_NDEF_T3T.h" #include "RW_NDEF_T4T.h" -#include "RW_NDEF_MIFARE.h" /* Allocate buffer for NDEF operations */ unsigned char NdefBuffer[RW_MAX_NDEF_FILE_SIZE]; @@ -31,77 +30,71 @@ unsigned short RW_NdefMessage_size; RW_NDEF_Callback_t *pRW_NDEF_PullCb; RW_NDEF_Callback_t *pRW_NDEF_PushCb; +CustomCallback_t *ndefReceivedCallback; static RW_NDEF_Fct_t *pReadFct = NULL; static RW_NDEF_Fct_t *pWriteFct = NULL; -bool RW_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) -{ - if (Message_size <= RW_MAX_NDEF_FILE_SIZE) - { - pRW_NdefMessage = pMessage; - RW_NdefMessage_size = Message_size; - pRW_NDEF_PushCb = (RW_NDEF_Callback_t *)pCb; - return true; - } - else - { - RW_NdefMessage_size = 0; - pRW_NDEF_PushCb = NULL; - return false; - } +bool RW_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) { + if (Message_size <= RW_MAX_NDEF_FILE_SIZE) { + pRW_NdefMessage = pMessage; + RW_NdefMessage_size = Message_size; + pRW_NDEF_PushCb = (RW_NDEF_Callback_t *)pCb; + return true; + } else { + RW_NdefMessage_size = 0; + pRW_NDEF_PushCb = NULL; + return false; + } +} + +void RW_NDEF_RegisterPullCallback(void *pCb) { + pRW_NDEF_PullCb = (RW_NDEF_Callback_t *)pCb; } -void RW_NDEF_RegisterPullCallback(void *pCb) -{ - pRW_NDEF_PullCb = (RW_NDEF_Callback_t *)pCb; +void registerNdefReceivedCallback(CustomCallback_t function) { + ndefReceivedCallback = function; } -void RW_NDEF_Reset(unsigned char type) -{ - pReadFct = NULL; - pWriteFct = NULL; +void RW_NDEF_Reset(unsigned char type) { + pReadFct = NULL; + pWriteFct = NULL; - switch (type) - { + switch (type) { case RW_NDEF_TYPE_T1T: - RW_NDEF_T1T_Reset(); - pReadFct = RW_NDEF_T1T_Read_Next; - break; + RW_NDEF_T1T_Reset(); + pReadFct = RW_NDEF_T1T_Read_Next; + break; case RW_NDEF_TYPE_T2T: - RW_NDEF_T2T_Reset(); - pReadFct = RW_NDEF_T2T_Read_Next; - pWriteFct = RW_NDEF_T2T_Write_Next; - break; + RW_NDEF_T2T_Reset(); + pReadFct = RW_NDEF_T2T_Read_Next; + pWriteFct = RW_NDEF_T2T_Write_Next; + break; case RW_NDEF_TYPE_T3T: - RW_NDEF_T3T_Reset(); - pReadFct = RW_NDEF_T3T_Read_Next; - break; + RW_NDEF_T3T_Reset(); + pReadFct = RW_NDEF_T3T_Read_Next; + break; case RW_NDEF_TYPE_T4T: - RW_NDEF_T4T_Reset(); - pReadFct = RW_NDEF_T4T_Read_Next; - pWriteFct = RW_NDEF_T4T_Write_Next; - break; + RW_NDEF_T4T_Reset(); + pReadFct = RW_NDEF_T4T_Read_Next; + pWriteFct = RW_NDEF_T4T_Write_Next; + break; case RW_NDEF_TYPE_MIFARE: - RW_NDEF_MIFARE_Reset(); - pReadFct = RW_NDEF_MIFARE_Read_Next; - pWriteFct = RW_NDEF_MIFARE_Write_Next; - break; + RW_NDEF_MIFARE_Reset(); + pReadFct = RW_NDEF_MIFARE_Read_Next; + pWriteFct = RW_NDEF_MIFARE_Write_Next; + break; default: - break; - } + break; + } } -void RW_NDEF_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size) -{ - if (pReadFct != NULL) - pReadFct(pCmd, Cmd_size, Rsp, pRsp_size); +void RW_NDEF_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size) { + if (pReadFct != NULL) + pReadFct(pCmd, Cmd_size, Rsp, pRsp_size); } -void RW_NDEF_Write_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size) -{ - if (pWriteFct != NULL) - pWriteFct(pCmd, Cmd_size, Rsp, pRsp_size); +void RW_NDEF_Write_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size) { + if (pWriteFct != NULL) + pWriteFct(pCmd, Cmd_size, Rsp, pRsp_size); } -//#endif -//#endif diff --git a/src/RW_NDEF.h b/src/RW_NDEF.h index 225dab9..ad40c13 100644 --- a/src/RW_NDEF.h +++ b/src/RW_NDEF.h @@ -1,28 +1,29 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ #include #define RW_MAX_NDEF_FILE_SIZE 500 extern unsigned char NdefBuffer[RW_MAX_NDEF_FILE_SIZE]; -typedef void RW_NDEF_Callback_t (unsigned char*, unsigned short); +typedef void RW_NDEF_Callback_t(unsigned char *, unsigned short); +typedef void CustomCallback_t(void); -#define RW_NDEF_TYPE_T1T 0x1 -#define RW_NDEF_TYPE_T2T 0x2 -#define RW_NDEF_TYPE_T3T 0x3 -#define RW_NDEF_TYPE_T4T 0x4 +#define RW_NDEF_TYPE_T1T 0x1 +#define RW_NDEF_TYPE_T2T 0x2 +#define RW_NDEF_TYPE_T3T 0x3 +#define RW_NDEF_TYPE_T4T 0x4 #define RW_NDEF_TYPE_MIFARE 0x80 extern unsigned char *pRW_NdefMessage; @@ -30,9 +31,11 @@ extern unsigned short RW_NdefMessage_size; extern RW_NDEF_Callback_t *pRW_NDEF_PullCb; extern RW_NDEF_Callback_t *pRW_NDEF_PushCb; +extern CustomCallback_t *ndefReceivedCallback; void RW_NDEF_Reset(unsigned char type); void RW_NDEF_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); void RW_NDEF_Write_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); bool RW_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); void RW_NDEF_RegisterPullCallback(void *pCb); +void registerNdefReceivedCallback(CustomCallback_t function); diff --git a/src/RW_NDEF_MIFARE.cpp b/src/RW_NDEF_MIFARE.cpp index 0de89fd..6df55fb 100644 --- a/src/RW_NDEF_MIFARE.cpp +++ b/src/RW_NDEF_MIFARE.cpp @@ -158,8 +158,13 @@ void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsi memcpy(RW_NDEF_MIFARE_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_MIFARE_Ndef.MessageSize); /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) + if (pRW_NDEF_PullCb != NULL) { pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); + } + + if (ndefReceivedCallback != NULL) { + ndefReceivedCallback(); // Run custom callback + } } else { From 767085eeb93b17f95c2cc1666cc76a45d45c9efd Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 17 Aug 2023 16:46:30 -0600 Subject: [PATCH 064/106] fix: wifi password and vcard payload prints --- examples/NDEFReceive/NDEFReceive.ino | 50 +++++++++++++++++++--------- src/RW_NDEF_MIFARE.cpp | 12 ++++++- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 1fd1a30..72b8b46 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -3,10 +3,21 @@ #define PN7150_VEN (13) #define PN7150_ADDR (0x28) +#define PRINTF Serial.print + +void print_buf(const char *prefix, const uint8_t *data, size_t size) { + PRINTF(prefix); + for (size_t loop = 0; loop < size; loop++) { + PRINTF(data[loop], HEX); + PRINTF(" "); + } + PRINTF("\n"); +} + // Function prototypes void displayDeviceInfo(); -void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize); void customNdefCallback(); +void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize); // Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); @@ -18,7 +29,7 @@ void setup() { Serial.println("Detect NFC tags with PN7150"); // Register a callback function to be called when an NDEF message is received - // RW_NDEF_RegisterPullCallback((void *)*ndefCallback); + RW_NDEF_RegisterPullCallback((void *)*ndefCallback); nfc.setSendMsgCallback(customNdefCallback); Serial.println("Initializing..."); @@ -150,6 +161,10 @@ void displayDeviceInfo() { } } +void customNdefCallback() { + Serial.println("Processing Callback..."); +} + void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { unsigned char *pNdefRecord = pNdefMessage; NdefRecord_t NdefRecord; @@ -172,7 +187,7 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("vCard:"); - // Serial.println(NdefRecord.recordPayload); + Serial.println(reinterpret_cast(NdefRecord.recordPayload)); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; @@ -180,7 +195,6 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("Text record: "); - // Serial.println(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]); Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; @@ -189,7 +203,6 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; Serial.print("URI record: "); - // Serial.println(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1]); Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; } break; @@ -204,9 +217,11 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { if (NdefRecord.recordPayload[index] == 0x10) { if (NdefRecord.recordPayload[index + 1] == 0x45) { Serial.print("- SSID = "); - for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) - Serial.print(NdefRecord.recordPayload[index + 4 + i]); - Serial.println(""); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); + // for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) { + // // Serial.print(NdefRecord.recordPayload[index + 4 + i]); + // Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + i])); + // } } else if (NdefRecord.recordPayload[index + 1] == 0x03) { Serial.print("- Authenticate Type = "); Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); @@ -215,9 +230,11 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); } else if (NdefRecord.recordPayload[index + 1] == 0x27) { Serial.print("- Network key = "); - for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) - Serial.print("#"); - Serial.println(""); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); + // for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) { + // Serial.print(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + i])); + // } + // Serial.println(""); } index += 4 + NdefRecord.recordPayload[index + 3]; } else @@ -241,6 +258,13 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { Serial.print("BT Handover payload = "); // Serial.print(NdefRecord.recordPayload); // Serial.println(NdefRecord.recordPayloadSize); + print_buf("", NdefRecord.recordPayload, NdefRecord.recordPayloadSize); + // Serial.print(reinterpret_cast(&NdefRecord.recordPayload[0])); + unsigned char i; + for (i = 0; i < NdefRecord.recordPayloadSize; i++) { + Serial.print(reinterpret_cast(&NdefRecord.recordPayload[i])); + } + Serial.println(""); break; case MEDIA_HANDOVER_BLE: @@ -263,7 +287,3 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { Serial.println(""); } - -void customNdefCallback() { - Serial.println("Processing Callback..."); -} diff --git a/src/RW_NDEF_MIFARE.cpp b/src/RW_NDEF_MIFARE.cpp index 6df55fb..3392a82 100644 --- a/src/RW_NDEF_MIFARE.cpp +++ b/src/RW_NDEF_MIFARE.cpp @@ -149,6 +149,11 @@ void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsi { if (pRW_NDEF_PullCb != NULL) pRW_NDEF_PullCb(NULL, 0); + + if (ndefReceivedCallback != NULL) { + ndefReceivedCallback(); // Run custom callback + } + break; } @@ -191,8 +196,13 @@ void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsi memcpy(&RW_NDEF_MIFARE_Ndef.pMessage[RW_NDEF_MIFARE_Ndef.MessagePtr], pRsp + 1, RW_NDEF_MIFARE_Ndef.MessageSize - RW_NDEF_MIFARE_Ndef.MessagePtr); /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) + if (pRW_NDEF_PullCb != NULL) { pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); + } + + if (ndefReceivedCallback != NULL) { + ndefReceivedCallback(); // Run custom callback + } } else { From 236bed7baf5b4a4049c1b7ae5a710782f314b971 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 11:25:45 -0600 Subject: [PATCH 065/106] feat: get bluetooth name and address --- examples/NDEFReceive/Makefile | 2 +- examples/NDEFReceive/NDEFReceive.ino | 83 ++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/examples/NDEFReceive/Makefile b/examples/NDEFReceive/Makefile index 4ab2c77..5e80373 100644 --- a/examples/NDEFReceive/Makefile +++ b/examples/NDEFReceive/Makefile @@ -16,4 +16,4 @@ clean: wait: sleep 2 -all: compile upload wait monitor \ No newline at end of file +all: compile upload \ No newline at end of file diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFReceive/NDEFReceive.ino index 72b8b46..b189ddb 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFReceive/NDEFReceive.ino @@ -3,17 +3,6 @@ #define PN7150_VEN (13) #define PN7150_ADDR (0x28) -#define PRINTF Serial.print - -void print_buf(const char *prefix, const uint8_t *data, size_t size) { - PRINTF(prefix); - for (size_t loop = 0; loop < size; loop++) { - PRINTF(data[loop], HEX); - PRINTF(" "); - } - PRINTF("\n"); -} - // Function prototypes void displayDeviceInfo(); void customNdefCallback(); @@ -161,6 +150,28 @@ void displayDeviceInfo() { } } +String getHexRepresentation(const byte *data, const uint32_t numBytes) { + String hexString; + + if (numBytes == 0) { + hexString = "null"; + } + + for (uint32_t szPos = 0; szPos < numBytes; szPos++) { + // hexString += "0x"; + if (data[szPos] <= 0xF) + hexString += "0"; + // hexString += String(data[szPos] & 0xFF, HEX); + String hexValue = String(data[szPos] & 0xFF, HEX); + hexValue.toUpperCase(); // Convierte a mayúsculas + hexString += hexValue; + if ((numBytes > 1) && (szPos != numBytes - 1)) { + hexString += ":"; + } + } + return hexString; +} + void customNdefCallback() { Serial.println("Processing Callback..."); } @@ -169,6 +180,11 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { unsigned char *pNdefRecord = pNdefMessage; NdefRecord_t NdefRecord; unsigned char save; + String decodedURL; + String SSID; + String bluetoothName; + String bluetoothAddress; + String reversedBluetoothAddress; Serial.println("Processing Callback"); @@ -217,7 +233,11 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { if (NdefRecord.recordPayload[index] == 0x10) { if (NdefRecord.recordPayload[index + 1] == 0x45) { Serial.print("- SSID = "); + // SSID = decodeURL((char *)&NdefRecord.recordPayload[index + 4]); Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); + Serial.println(SSID); + Serial.print("- SSID = "); + Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4 + 0], NdefRecord.recordPayload[index + 3])); // for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) { // // Serial.print(NdefRecord.recordPayload[index + 4 + i]); // Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + i])); @@ -231,6 +251,8 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { } else if (NdefRecord.recordPayload[index + 1] == 0x27) { Serial.print("- Network key = "); Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); + Serial.print("- Network key = "); + Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); // for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) { // Serial.print(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + i])); // } @@ -255,27 +277,42 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { break; case MEDIA_HANDOVER_BT: - Serial.print("BT Handover payload = "); - // Serial.print(NdefRecord.recordPayload); - // Serial.println(NdefRecord.recordPayloadSize); - print_buf("", NdefRecord.recordPayload, NdefRecord.recordPayloadSize); - // Serial.print(reinterpret_cast(&NdefRecord.recordPayload[0])); - unsigned char i; - for (i = 0; i < NdefRecord.recordPayloadSize; i++) { - Serial.print(reinterpret_cast(&NdefRecord.recordPayload[i])); + Serial.print("Payload size: "); + Serial.println(NdefRecord.recordPayloadSize); + Serial.print("Bluetooth Handover payload = "); + Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + Serial.print("Bluetooth name: '"); + bluetoothName = ""; + for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { + if (NdefRecord.recordPayload[i] == 0x04) { + break; + } + + // Serial.write(NdefRecord.recordPayload[i]); + // Serial.println(" = " + getHexRepresentation(&NdefRecord.recordPayload[i], 1)); + bluetoothName += (char)NdefRecord.recordPayload[i]; + } + Serial.println(bluetoothName + "'"); + + Serial.print("Bluetooth address: '"); + bluetoothAddress = ""; + for (unsigned int i = 7; i >= 2; i--) { + bluetoothAddress += getHexRepresentation(&NdefRecord.recordPayload[i], 1); + if (i > 2) { + bluetoothAddress += ":"; + } } - Serial.println(""); + Serial.println(bluetoothAddress + "'"); break; case MEDIA_HANDOVER_BLE: Serial.print("BLE Handover payload = "); - // Serial.print(NdefRecord.recordPayload); - // Serial.println(NdefRecord.recordPayloadSize); + Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); break; case MEDIA_HANDOVER_BLE_SECURE: Serial.print("BLE secure Handover payload = "); - // Serial.println(NdefRecord.recordPayload, NdefRecord.recordPayloadSize); + Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); break; default: From b8d2913a606e8c1377b94c89344784306c09e940 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 12:14:48 -0600 Subject: [PATCH 066/106] feat: remove commented code --- examples/{NDEFReceive => NDEFRead}/Makefile | 0 .../NDEFReceive.ino => NDEFRead/NDEFRead.ino} | 28 +- src/Electroniccats_PN7150.cpp | 18 +- src/P2P_NDEF.cpp | 395 ++++++------ src/P2P_NDEF.h | 26 +- src/RW_NDEF_MIFARE.cpp | 592 ++++++++---------- src/RW_NDEF_MIFARE.h | 24 +- src/RW_NDEF_T1T.cpp | 266 ++++---- src/RW_NDEF_T1T.h | 24 +- src/RW_NDEF_T2T.cpp | 362 +++++------ src/RW_NDEF_T2T.h | 24 +- src/RW_NDEF_T3T.cpp | 182 +++--- src/RW_NDEF_T3T.h | 24 +- src/RW_NDEF_T4T.cpp | 559 ++++++++--------- src/RW_NDEF_T4T.h | 24 +- src/T4T_NDEF_emu.cpp | 243 ++++--- src/ndef_helper.cpp | 316 ++++------ src/ndef_helper.h | 61 +- src/tool.cpp | 54 +- 19 files changed, 1469 insertions(+), 1753 deletions(-) rename examples/{NDEFReceive => NDEFRead}/Makefile (100%) rename examples/{NDEFReceive/NDEFReceive.ino => NDEFRead/NDEFRead.ino} (88%) diff --git a/examples/NDEFReceive/Makefile b/examples/NDEFRead/Makefile similarity index 100% rename from examples/NDEFReceive/Makefile rename to examples/NDEFRead/Makefile diff --git a/examples/NDEFReceive/NDEFReceive.ino b/examples/NDEFRead/NDEFRead.ino similarity index 88% rename from examples/NDEFReceive/NDEFReceive.ino rename to examples/NDEFRead/NDEFRead.ino index b189ddb..df4a58e 100644 --- a/examples/NDEFReceive/NDEFReceive.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -180,11 +180,9 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { unsigned char *pNdefRecord = pNdefMessage; NdefRecord_t NdefRecord; unsigned char save; - String decodedURL; String SSID; String bluetoothName; String bluetoothAddress; - String reversedBluetoothAddress; Serial.println("Processing Callback"); @@ -233,15 +231,8 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { if (NdefRecord.recordPayload[index] == 0x10) { if (NdefRecord.recordPayload[index + 1] == 0x45) { Serial.print("- SSID = "); - // SSID = decodeURL((char *)&NdefRecord.recordPayload[index + 4]); Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); Serial.println(SSID); - Serial.print("- SSID = "); - Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4 + 0], NdefRecord.recordPayload[index + 3])); - // for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) { - // // Serial.print(NdefRecord.recordPayload[index + 4 + i]); - // Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + i])); - // } } else if (NdefRecord.recordPayload[index + 1] == 0x03) { Serial.print("- Authenticate Type = "); Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); @@ -253,10 +244,6 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); Serial.print("- Network key = "); Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); - // for (i = 0; i < NdefRecord.recordPayload[index + 3]; i++) { - // Serial.print(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + i])); - // } - // Serial.println(""); } index += 4 + NdefRecord.recordPayload[index + 3]; } else @@ -277,24 +264,21 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { break; case MEDIA_HANDOVER_BT: - Serial.print("Payload size: "); + Serial.print("- Payload size: "); Serial.println(NdefRecord.recordPayloadSize); - Serial.print("Bluetooth Handover payload = "); + Serial.print("- Bluetooth Handover payload = "); Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); - Serial.print("Bluetooth name: '"); + Serial.print("- Bluetooth name: '"); bluetoothName = ""; for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { if (NdefRecord.recordPayload[i] == 0x04) { break; } - - // Serial.write(NdefRecord.recordPayload[i]); - // Serial.println(" = " + getHexRepresentation(&NdefRecord.recordPayload[i], 1)); bluetoothName += (char)NdefRecord.recordPayload[i]; } Serial.println(bluetoothName + "'"); - Serial.print("Bluetooth address: '"); + Serial.print("- Bluetooth address: '"); bluetoothAddress = ""; for (unsigned int i = 7; i >= 2; i--) { bluetoothAddress += getHexRepresentation(&NdefRecord.recordPayload[i], 1); @@ -306,12 +290,12 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { break; case MEDIA_HANDOVER_BLE: - Serial.print("BLE Handover payload = "); + Serial.print("- BLE Handover payload = "); Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); break; case MEDIA_HANDOVER_BLE_SECURE: - Serial.print("BLE secure Handover payload = "); + Serial.print("- BLE secure Handover payload = "); Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 2e5885f..a79b9ee 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -16,10 +16,6 @@ * */ #include "Electroniccats_PN7150.h" -// #include "P2P_NDEF.h" -// #include "ndef_helper.h" -// #include "RW_NDEF.h" -// #include "T4T_NDEF_emu.h" uint8_t gNextTag_Protocol = PROT_UNDETERMINED; @@ -1174,9 +1170,7 @@ void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { /* Reset Card emulation state */ T4T_NDEF_EMU_Reset(); - // (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); getMessage(2000); - // NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_2S) == NXPNCI_SUCCESS while (rxMessageLength > 0) { getMessage(2000); @@ -1184,22 +1178,17 @@ void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { if ((rxBuffer[0] == 0x61) && (rxBuffer[1] == 0x06)) { if (FirstCmd) { /* Restart the discovery loop */ - // NxpNci_HostTransceive(NCIStopDiscovery, sizeof(NCIStopDiscovery), Answer, sizeof(Answer), &AnswerSize); (void)writeData(NCIStopDiscovery, sizeof(NCIStopDiscovery)); getMessage(); do { if ((rxBuffer[0] == 0x41) && (rxBuffer[1] == 0x06)) break; - // NxpNci_WaitForReception(Answer, sizeof(Answer), &AnswerSize, TIMEOUT_100MS); - //(void)writeData(rxBuffer, rxMessageLength); getMessage(100); } while (rxMessageLength != 0); - // NxpNci_HostTransceive(NCIStartDiscovery, NCIStartDiscovery_length, Answer, sizeof(Answer), &AnswerSize); (void)writeData(NCIStartDiscovery, NCIStartDiscovery_length); getMessage(); } /* Come back to discovery state */ - // break; } /* is DATA_PACKET ? */ else if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { @@ -1213,7 +1202,6 @@ void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { Cmd[1] = (CmdSize & 0xFF00) >> 8; Cmd[2] = CmdSize & 0x00FF; - // NxpNci_HostTransceive(Cmd, CmdSize+3, Answer, sizeof(Answer), &AnswerSize); (void)writeData(Cmd, CmdSize + 3); getMessage(); } @@ -1523,7 +1511,7 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { else if (gNextTag_Protocol == PROT_ISODEP) NCIRfDiscoverSelect[5] = INTF_NFCDEP; else if (gNextTag_Protocol == PROT_MIFARE) - NCIRfDiscoverSelect[5] = INTF_TAGCMD; // TODO: check this + NCIRfDiscoverSelect[5] = INTF_TAGCMD; else NCIRfDiscoverSelect[5] = INTF_FRAME; @@ -1539,8 +1527,6 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { remoteDevice.setProtocol(rxBuffer[5]); pRfIntf->ModeTech = rxBuffer[6]; remoteDevice.setModeTech(rxBuffer[6]); - // fillInterfaceInfo(pRfIntf, &rxBuffer[10]); - // fillInterfaceInfo(&rxBuffer[10]); remoteDevice.setInfo(pRfIntf, &rxBuffer[10]); status = SUCCESS; } @@ -1680,7 +1666,6 @@ bool Electroniccats_PN7150::nciFactoryTestRfOn() { (void)writeData(NCIRfOn, sizeof(NCIRfOn)); getMessage(); - // NxpNci_HostTransceive(NCIRfOn, sizeof(NCIRfOn), Answer, sizeof(Answer), &AnswerSize); if ((rxBuffer[0] != 0x4F) || (rxBuffer[1] != 0x3D) || (rxBuffer[3] != 0x00)) return ERROR; @@ -1733,6 +1718,5 @@ bool Electroniccats_PN7150::setP2PMode() { } void Electroniccats_PN7150::setSendMsgCallback(CustomCallback_t function) { - // RW_NDEF_RegisterPullCallback((void *)*function); registerNdefReceivedCallback(function); } diff --git a/src/P2P_NDEF.cpp b/src/P2P_NDEF.cpp index 7317273..7da6d0d 100644 --- a/src/P2P_NDEF.cpp +++ b/src/P2P_NDEF.cpp @@ -1,21 +1,22 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef P2P_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef P2P_SUPPORT #include "P2P_NDEF.h" +#include "tool.h" + /* Well-known LLCP SAP Values */ #define SAP_SDP 1 #define SAP_SNEP 4 @@ -65,27 +66,26 @@ unsigned short NdefMessage_size = 0; /* Defines at which frequency the symmetry is exchange (in ms) */ #define SYMM_FREQ 500 -typedef enum -{ - Idle, - Initial, - DelayingPush, - SnepClientConnecting, - SnepClientConnected, - NdefMsgSent +typedef enum { + Idle, + Initial, + DelayingPush, + SnepClientConnecting, + SnepClientConnected, + NdefMsgSent } P2P_SnepClient_state_t; typedef struct { - unsigned char Dsap; - unsigned char Pdu; - unsigned char Ssap; - unsigned char Version; - unsigned short Miux; - unsigned short Wks; - unsigned char Lto; - unsigned char Rw; - unsigned char Sn[30]; + unsigned char Dsap; + unsigned char Pdu; + unsigned char Ssap; + unsigned char Version; + unsigned short Miux; + unsigned short Wks; + unsigned char Lto; + unsigned char Rw; + unsigned char Sn[30]; } P2P_NDEF_LlcpHeader_t; typedef void P2P_NDEF_Callback_t(unsigned char *, unsigned short); @@ -95,211 +95,182 @@ static P2P_NDEF_Callback_t *pP2P_NDEF_PushCb = NULL; static P2P_NDEF_Callback_t *pP2P_NDEF_PullCb = NULL; static unsigned short P2P_SnepClient_DelayCount = NDEF_PUSH_DELAY_COUNT; -static void ParseLlcp(unsigned char *pBuf, unsigned short BufSize, P2P_NDEF_LlcpHeader_t *pLlcpHeader) -{ - uint8_t i = 2; - - pLlcpHeader->Dsap = pBuf[0] >> 2; - pLlcpHeader->Pdu = ((pBuf[0] & 3) << 2) + (pBuf[1] >> 6); - pLlcpHeader->Ssap = pBuf[1] & 0x3F; - - while (i < BufSize) - { - switch (pBuf[i]) - { - case VERSION: - pLlcpHeader->Version = pBuf[i + 2]; - break; - case MIUX: - pLlcpHeader->Miux = (pBuf[i + 2] << 8) + pBuf[i + 3]; - break; - case WKS: - pLlcpHeader->Wks = (pBuf[i + 2] << 8) + pBuf[i + 3]; - break; - case LTO: - pLlcpHeader->Lto = pBuf[i + 2]; - break; - case RW: - pLlcpHeader->Rw = pBuf[i + 2]; - break; - case SN: - memcpy(pLlcpHeader->Sn, &pBuf[i + 2], pBuf[i + 1] < sizeof(pLlcpHeader->Sn) ? pBuf[i + 1] : sizeof(pLlcpHeader->Sn)); - break; - default: - break; - } - i += pBuf[i + 1] + 2; +static void ParseLlcp(unsigned char *pBuf, unsigned short BufSize, P2P_NDEF_LlcpHeader_t *pLlcpHeader) { + uint8_t i = 2; + + pLlcpHeader->Dsap = pBuf[0] >> 2; + pLlcpHeader->Pdu = ((pBuf[0] & 3) << 2) + (pBuf[1] >> 6); + pLlcpHeader->Ssap = pBuf[1] & 0x3F; + + while (i < BufSize) { + switch (pBuf[i]) { + case VERSION: + pLlcpHeader->Version = pBuf[i + 2]; + break; + case MIUX: + pLlcpHeader->Miux = (pBuf[i + 2] << 8) + pBuf[i + 3]; + break; + case WKS: + pLlcpHeader->Wks = (pBuf[i + 2] << 8) + pBuf[i + 3]; + break; + case LTO: + pLlcpHeader->Lto = pBuf[i + 2]; + break; + case RW: + pLlcpHeader->Rw = pBuf[i + 2]; + break; + case SN: + memcpy(pLlcpHeader->Sn, &pBuf[i + 2], pBuf[i + 1] < sizeof(pLlcpHeader->Sn) ? pBuf[i + 1] : sizeof(pLlcpHeader->Sn)); + break; + default: + break; } + i += pBuf[i + 1] + 2; + } } -static void FillLlcp(P2P_NDEF_LlcpHeader_t LlcpHeader, unsigned char *pBuf) -{ - pBuf[0] = (LlcpHeader.Ssap << 2) + ((LlcpHeader.Pdu >> 2) & 3); - pBuf[1] = (LlcpHeader.Pdu << 6) + LlcpHeader.Dsap; +static void FillLlcp(P2P_NDEF_LlcpHeader_t LlcpHeader, unsigned char *pBuf) { + pBuf[0] = (LlcpHeader.Ssap << 2) + ((LlcpHeader.Pdu >> 2) & 3); + pBuf[1] = (LlcpHeader.Pdu << 6) + LlcpHeader.Dsap; } -bool P2P_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) -{ - if (Message_size <= P2P_NDEF_MAX_NDEF_MESSAGE_SIZE) - { - pNdefMessage = pMessage; - NdefMessage_size = Message_size; - pP2P_NDEF_PushCb = (P2P_NDEF_Callback_t *)pCb; - /* Trigger sending dynamically new message */ - if (eP2P_SnepClient_State == NdefMsgSent) - { - eP2P_SnepClient_State = SnepClientConnected; - } - return true; - } - else - { - NdefMessage_size = 0; - pP2P_NDEF_PushCb = NULL; - return false; +bool P2P_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) { + if (Message_size <= P2P_NDEF_MAX_NDEF_MESSAGE_SIZE) { + pNdefMessage = pMessage; + NdefMessage_size = Message_size; + pP2P_NDEF_PushCb = (P2P_NDEF_Callback_t *)pCb; + /* Trigger sending dynamically new message */ + if (eP2P_SnepClient_State == NdefMsgSent) { + eP2P_SnepClient_State = SnepClientConnected; } + return true; + } else { + NdefMessage_size = 0; + pP2P_NDEF_PushCb = NULL; + return false; + } } -void P2P_NDEF_RegisterPullCallback(void *pCb) -{ - pP2P_NDEF_PullCb = (P2P_NDEF_Callback_t *)pCb; +void P2P_NDEF_RegisterPullCallback(void *pCb) { + pP2P_NDEF_PullCb = (P2P_NDEF_Callback_t *)pCb; } -void P2P_NDEF_Reset(void) -{ - if (NdefMessage_size != 0) - { - eP2P_SnepClient_State = Initial; - } - else - { - eP2P_SnepClient_State = Idle; - } +void P2P_NDEF_Reset(void) { + if (NdefMessage_size != 0) { + eP2P_SnepClient_State = Initial; + } else { + eP2P_SnepClient_State = Idle; + } } -void P2P_NDEF_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *pRsp, unsigned short *pRsp_size) -{ - P2P_NDEF_LlcpHeader_t LlcpHeader; +void P2P_NDEF_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *pRsp, unsigned short *pRsp_size) { + P2P_NDEF_LlcpHeader_t LlcpHeader; - /* Initialize answer */ - *pRsp_size = 0; + /* Initialize answer */ + *pRsp_size = 0; - ParseLlcp(pCmd, Cmd_size, &LlcpHeader); + ParseLlcp(pCmd, Cmd_size, &LlcpHeader); - switch (LlcpHeader.Pdu) - { + switch (LlcpHeader.Pdu) { case CONNECT: - /* Is connection from SNEP Client ? */ - if ((LlcpHeader.Dsap == SAP_SNEP) || (memcmp(LlcpHeader.Sn, "urn:nfc:sn:snep", 15) == 0)) - { - /* Only accept the connection is application is registered for NDEF reception */ - if (pP2P_NDEF_PullCb != NULL) - { - LlcpHeader.Pdu = CC; - FillLlcp(LlcpHeader, pRsp); - *pRsp_size = 2; - } - } - else - { - /* Refuse any other connection request */ - LlcpHeader.Pdu = DM; - FillLlcp(LlcpHeader, pRsp); - *pRsp_size = 2; + /* Is connection from SNEP Client ? */ + if ((LlcpHeader.Dsap == SAP_SNEP) || (memcmp(LlcpHeader.Sn, "urn:nfc:sn:snep", 15) == 0)) { + /* Only accept the connection is application is registered for NDEF reception */ + if (pP2P_NDEF_PullCb != NULL) { + LlcpHeader.Pdu = CC; + FillLlcp(LlcpHeader, pRsp); + *pRsp_size = 2; } - break; + } else { + /* Refuse any other connection request */ + LlcpHeader.Pdu = DM; + FillLlcp(LlcpHeader, pRsp); + *pRsp_size = 2; + } + break; case I: - /* Is SNEP PUT ? */ - if ((pCmd[3] == SNEP_VER10) && (pCmd[4] == SNEP_PUT)) - { - /* Notify application of the NDEF reception */ - if (pP2P_NDEF_PullCb != NULL) - pP2P_NDEF_PullCb(&pCmd[9], pCmd[8]); - - /* Acknowledge the PUT request */ - LlcpHeader.Pdu = I; - FillLlcp(LlcpHeader, pRsp); - pRsp[2] = (pCmd[2] >> 4) + 1; // N(R) - memcpy(&pRsp[3], SNEP_PUT_SUCCESS, sizeof(SNEP_PUT_SUCCESS)); - *pRsp_size = 9; - } - break; + /* Is SNEP PUT ? */ + if ((pCmd[3] == SNEP_VER10) && (pCmd[4] == SNEP_PUT)) { + /* Notify application of the NDEF reception */ + if (pP2P_NDEF_PullCb != NULL) + pP2P_NDEF_PullCb(&pCmd[9], pCmd[8]); + + /* Acknowledge the PUT request */ + LlcpHeader.Pdu = I; + FillLlcp(LlcpHeader, pRsp); + pRsp[2] = (pCmd[2] >> 4) + 1; // N(R) + memcpy(&pRsp[3], SNEP_PUT_SUCCESS, sizeof(SNEP_PUT_SUCCESS)); + *pRsp_size = 9; + } + break; case CC: - /* Connection to remote SNEP server completed, send NDEF message inside SNEP PUT request */ - eP2P_SnepClient_State = SnepClientConnected; - break; + /* Connection to remote SNEP server completed, send NDEF message inside SNEP PUT request */ + eP2P_SnepClient_State = SnepClientConnected; + break; default: + break; + } + + /* No answer was set */ + if (*pRsp_size == 0) { + switch (eP2P_SnepClient_State) { + case Initial: + if ((pP2P_NDEF_PullCb == NULL) || (NDEF_PUSH_DELAY_COUNT == 0)) { + memcpy(pRsp, LLCP_CONNECT_SNEP, sizeof(LLCP_CONNECT_SNEP)); + *pRsp_size = sizeof(LLCP_CONNECT_SNEP); + eP2P_SnepClient_State = SnepClientConnecting; + } else { + P2P_SnepClient_DelayCount = 1; + eP2P_SnepClient_State = DelayingPush; + /* Wait then send a SYMM */ + Sleep(SYMM_FREQ); + memcpy(pRsp, LLCP_SYMM, sizeof(LLCP_SYMM)); + *pRsp_size = sizeof(LLCP_SYMM); + } break; - } - /* No answer was set */ - if (*pRsp_size == 0) - { - switch (eP2P_SnepClient_State) - { - case Initial: - if ((pP2P_NDEF_PullCb == NULL) || (NDEF_PUSH_DELAY_COUNT == 0)) - { - memcpy(pRsp, LLCP_CONNECT_SNEP, sizeof(LLCP_CONNECT_SNEP)); - *pRsp_size = sizeof(LLCP_CONNECT_SNEP); - eP2P_SnepClient_State = SnepClientConnecting; - } - else - { - P2P_SnepClient_DelayCount = 1; - eP2P_SnepClient_State = DelayingPush; - /* Wait then send a SYMM */ - Sleep(SYMM_FREQ); - memcpy(pRsp, LLCP_SYMM, sizeof(LLCP_SYMM)); - *pRsp_size = sizeof(LLCP_SYMM); - } - break; - - case DelayingPush: - if (P2P_SnepClient_DelayCount == NDEF_PUSH_DELAY_COUNT) - { - memcpy(pRsp, LLCP_CONNECT_SNEP, sizeof(LLCP_CONNECT_SNEP)); - *pRsp_size = sizeof(LLCP_CONNECT_SNEP); - eP2P_SnepClient_State = SnepClientConnecting; - } - else - { - P2P_SnepClient_DelayCount++; - /* Wait then send a SYMM */ - Sleep(SYMM_FREQ); - memcpy(pRsp, LLCP_SYMM, sizeof(LLCP_SYMM)); - *pRsp_size = sizeof(LLCP_SYMM); - } - break; - - case SnepClientConnected: - LlcpHeader.Pdu = I; - FillLlcp(LlcpHeader, pRsp); - pRsp[2] = 0; // N(R) - pRsp[3] = SNEP_VER10; - pRsp[4] = SNEP_PUT; - pRsp[5] = 0; - pRsp[6] = 0; - pRsp[7] = 0; - pRsp[8] = (unsigned char)NdefMessage_size; - memcpy(&pRsp[9], pNdefMessage, NdefMessage_size); - *pRsp_size = 9 + NdefMessage_size; - eP2P_SnepClient_State = NdefMsgSent; - /* Notify application of the NDEF push */ - if (pP2P_NDEF_PushCb != NULL) - pP2P_NDEF_PushCb(pNdefMessage, NdefMessage_size); - break; - - default: - /* Wait then send a SYMM */ - Sleep(SYMM_FREQ); - memcpy(pRsp, LLCP_SYMM, sizeof(LLCP_SYMM)); - *pRsp_size = sizeof(LLCP_SYMM); - break; + case DelayingPush: + if (P2P_SnepClient_DelayCount == NDEF_PUSH_DELAY_COUNT) { + memcpy(pRsp, LLCP_CONNECT_SNEP, sizeof(LLCP_CONNECT_SNEP)); + *pRsp_size = sizeof(LLCP_CONNECT_SNEP); + eP2P_SnepClient_State = SnepClientConnecting; + } else { + P2P_SnepClient_DelayCount++; + /* Wait then send a SYMM */ + Sleep(SYMM_FREQ); + memcpy(pRsp, LLCP_SYMM, sizeof(LLCP_SYMM)); + *pRsp_size = sizeof(LLCP_SYMM); } + break; + + case SnepClientConnected: + LlcpHeader.Pdu = I; + FillLlcp(LlcpHeader, pRsp); + pRsp[2] = 0; // N(R) + pRsp[3] = SNEP_VER10; + pRsp[4] = SNEP_PUT; + pRsp[5] = 0; + pRsp[6] = 0; + pRsp[7] = 0; + pRsp[8] = (unsigned char)NdefMessage_size; + memcpy(&pRsp[9], pNdefMessage, NdefMessage_size); + *pRsp_size = 9 + NdefMessage_size; + eP2P_SnepClient_State = NdefMsgSent; + /* Notify application of the NDEF push */ + if (pP2P_NDEF_PushCb != NULL) + pP2P_NDEF_PushCb(pNdefMessage, NdefMessage_size); + break; + + default: + /* Wait then send a SYMM */ + Sleep(SYMM_FREQ); + memcpy(pRsp, LLCP_SYMM, sizeof(LLCP_SYMM)); + *pRsp_size = sizeof(LLCP_SYMM); + break; } + } } -//#endif +// #endif diff --git a/src/P2P_NDEF.h b/src/P2P_NDEF.h index 865a191..b67ebd8 100644 --- a/src/P2P_NDEF.h +++ b/src/P2P_NDEF.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ #include #define P2P_NDEF_MAX_NDEF_MESSAGE_SIZE 240 @@ -19,4 +19,4 @@ void P2P_NDEF_Reset(void); void P2P_NDEF_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); void P2P_NDEF_RegisterPullCallback(void *pCb); bool P2P_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); -//void Sleep (unsigned int ms); +// void Sleep (unsigned int ms); diff --git a/src/RW_NDEF_MIFARE.cpp b/src/RW_NDEF_MIFARE.cpp index 3392a82..c5b163c 100644 --- a/src/RW_NDEF_MIFARE.cpp +++ b/src/RW_NDEF_MIFARE.cpp @@ -1,21 +1,21 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef RW_SUPPORT -//#ifndef NO_NDEF_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef RW_SUPPORT +// #ifndef NO_NDEF_SUPPORT #include "RW_NDEF.h" +#include "tool.h" /* TODO: Only simplified scenario is implemented yet: @@ -26,357 +26,309 @@ #define MIFARE_NFC_CLUSTER 0x03 #define MIFARE_NDEF_TLV 0x03 -typedef enum -{ - Initial, - Authenticated0, - Reading_GPB, - Authenticated, - Reading_FirstBlk, - Reading_Data, - Writing_Data1, - Writing_Data2, - Writing_Data +typedef enum { + Initial, + Authenticated0, + Reading_GPB, + Authenticated, + Reading_FirstBlk, + Reading_Data, + Writing_Data1, + Writing_Data2, + Writing_Data } RW_NDEF_MIFARE_state_t; typedef struct { - bool WriteOp; - unsigned char BlkNb; - unsigned short MessagePtr; - unsigned short MessageSize; - unsigned char *pMessage; + bool WriteOp; + unsigned char BlkNb; + unsigned short MessagePtr; + unsigned short MessageSize; + unsigned char *pMessage; } RW_NDEF_MIFARE_Ndef_t; static RW_NDEF_MIFARE_state_t eRW_NDEF_MIFARE_State = Initial; static RW_NDEF_MIFARE_Ndef_t RW_NDEF_MIFARE_Ndef; -void RW_NDEF_MIFARE_Reset(void) -{ - eRW_NDEF_MIFARE_State = Initial; - RW_NDEF_MIFARE_Ndef.pMessage = NdefBuffer; +void RW_NDEF_MIFARE_Reset(void) { + eRW_NDEF_MIFARE_State = Initial; + RW_NDEF_MIFARE_Ndef.pMessage = NdefBuffer; } -void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_MIFARE_State) - { + switch (eRW_NDEF_MIFARE_State) { case Initial: - /* Authenticating first sector */ - pCmd[0] = 0x40; - pCmd[1] = 0x00; - pCmd[2] = 0x00; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Authenticated0; - break; + /* Authenticating first sector */ + pCmd[0] = 0x40; + pCmd[1] = 0x00; + pCmd[2] = 0x00; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Authenticated0; + break; case Authenticated0: - if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Read GPB */ - pCmd[0] = 0x10; - pCmd[1] = 0x30; - pCmd[2] = 0x01; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Reading_GPB; - } - break; + if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Read GPB */ + pCmd[0] = 0x10; + pCmd[1] = 0x30; + pCmd[2] = 0x01; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Reading_GPB; + } + break; case Reading_GPB: - if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF format ?*/ - if ((pRsp[3] == MIFARE_NFC_CLUSTER) && (pRsp[4] == MIFARE_FUNCTION_CLUSTER)) - { - pCmd[0] = 0x40; - pCmd[1] = 0x01; - pCmd[2] = 0x01; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Authenticated; - - RW_NDEF_MIFARE_Ndef.BlkNb = 4; - } + if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF format ?*/ + if ((pRsp[3] == MIFARE_NFC_CLUSTER) && (pRsp[4] == MIFARE_FUNCTION_CLUSTER)) { + pCmd[0] = 0x40; + pCmd[1] = 0x01; + pCmd[2] = 0x01; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Authenticated; + + RW_NDEF_MIFARE_Ndef.BlkNb = 4; } - break; + } + break; case Authenticated: - if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* First NDEF data block to read ?*/ - if (RW_NDEF_MIFARE_Ndef.BlkNb == 4) - { - eRW_NDEF_MIFARE_State = Reading_FirstBlk; - } - else - { - RW_NDEF_MIFARE_Ndef.BlkNb++; - eRW_NDEF_MIFARE_State = Reading_Data; - } - - /* Read block */ - pCmd[0] = 0x10; - pCmd[1] = 0x30; - pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; - *pCmd_size = 3; + if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) { + /* First NDEF data block to read ?*/ + if (RW_NDEF_MIFARE_Ndef.BlkNb == 4) { + eRW_NDEF_MIFARE_State = Reading_FirstBlk; + } else { + RW_NDEF_MIFARE_Ndef.BlkNb++; + eRW_NDEF_MIFARE_State = Reading_Data; } - break; + + /* Read block */ + pCmd[0] = 0x10; + pCmd[1] = 0x30; + pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; + *pCmd_size = 3; + } + break; case Reading_FirstBlk: - if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) - { - unsigned char Tmp = 1; - /* If not NDEF Type skip TLV */ - while (pRsp[Tmp] != MIFARE_NDEF_TLV) - { - Tmp += 2 + pRsp[Tmp + 1]; - if (Tmp > Rsp_size) - return; - } - - if (pRsp[Tmp + 1] == 0xFF) - { - RW_NDEF_MIFARE_Ndef.MessageSize = (pRsp[Tmp + 2] << 8) + pRsp[Tmp + 3]; - Tmp += 2; - } - else - RW_NDEF_MIFARE_Ndef.MessageSize = pRsp[Tmp + 1]; - - /* If provisioned buffer is not large enough or message is empty, notify the application and stop reading */ - if ((RW_NDEF_MIFARE_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) || (RW_NDEF_MIFARE_Ndef.MessageSize == 0)) - { - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(NULL, 0); - - if (ndefReceivedCallback != NULL) { - ndefReceivedCallback(); // Run custom callback - } - - break; - } - - /* Is NDEF read already completed ? */ - if (RW_NDEF_MIFARE_Ndef.MessageSize <= ((Rsp_size - 1) - Tmp - 2)) - { - memcpy(RW_NDEF_MIFARE_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_MIFARE_Ndef.MessageSize); - - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) { - pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); - } - - if (ndefReceivedCallback != NULL) { - ndefReceivedCallback(); // Run custom callback - } - } - else - { - RW_NDEF_MIFARE_Ndef.MessagePtr = (Rsp_size - 1) - Tmp - 2; - memcpy(RW_NDEF_MIFARE_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_MIFARE_Ndef.MessagePtr); - RW_NDEF_MIFARE_Ndef.BlkNb++; - - /* Read next block */ - pCmd[0] = 0x10; - pCmd[1] = 0x30; - pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Reading_Data; - } + if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) { + unsigned char Tmp = 1; + /* If not NDEF Type skip TLV */ + while (pRsp[Tmp] != MIFARE_NDEF_TLV) { + Tmp += 2 + pRsp[Tmp + 1]; + if (Tmp > Rsp_size) + return; } - break; + + if (pRsp[Tmp + 1] == 0xFF) { + RW_NDEF_MIFARE_Ndef.MessageSize = (pRsp[Tmp + 2] << 8) + pRsp[Tmp + 3]; + Tmp += 2; + } else + RW_NDEF_MIFARE_Ndef.MessageSize = pRsp[Tmp + 1]; + + /* If provisioned buffer is not large enough or message is empty, notify the application and stop reading */ + if ((RW_NDEF_MIFARE_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) || (RW_NDEF_MIFARE_Ndef.MessageSize == 0)) { + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(NULL, 0); + + if (ndefReceivedCallback != NULL) { + ndefReceivedCallback(); // Run custom callback + } + + break; + } + + /* Is NDEF read already completed ? */ + if (RW_NDEF_MIFARE_Ndef.MessageSize <= ((Rsp_size - 1) - Tmp - 2)) { + memcpy(RW_NDEF_MIFARE_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_MIFARE_Ndef.MessageSize); + + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) { + pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); + } + + if (ndefReceivedCallback != NULL) { + ndefReceivedCallback(); // Run custom callback + } + } else { + RW_NDEF_MIFARE_Ndef.MessagePtr = (Rsp_size - 1) - Tmp - 2; + memcpy(RW_NDEF_MIFARE_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_MIFARE_Ndef.MessagePtr); + RW_NDEF_MIFARE_Ndef.BlkNb++; + + /* Read next block */ + pCmd[0] = 0x10; + pCmd[1] = 0x30; + pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Reading_Data; + } + } + break; case Reading_Data: - if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF read already completed ? */ - if ((RW_NDEF_MIFARE_Ndef.MessageSize - RW_NDEF_MIFARE_Ndef.MessagePtr) < 16) - { - memcpy(&RW_NDEF_MIFARE_Ndef.pMessage[RW_NDEF_MIFARE_Ndef.MessagePtr], pRsp + 1, RW_NDEF_MIFARE_Ndef.MessageSize - RW_NDEF_MIFARE_Ndef.MessagePtr); - - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) { - pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); - } - - if (ndefReceivedCallback != NULL) { - ndefReceivedCallback(); // Run custom callback - } - } - else - { - memcpy(&RW_NDEF_MIFARE_Ndef.pMessage[RW_NDEF_MIFARE_Ndef.MessagePtr], pRsp + 1, 16); - RW_NDEF_MIFARE_Ndef.MessagePtr += 16; - RW_NDEF_MIFARE_Ndef.BlkNb++; - - /* Is Blk on next sector ?*/ - if (((RW_NDEF_MIFARE_Ndef.BlkNb + 1) % 4) == 0) - { - /* Authenticate next block */ - pCmd[0] = 0x40; - pCmd[1] = (RW_NDEF_MIFARE_Ndef.BlkNb + 1) / 4; - pCmd[2] = 0x01; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Authenticated; - } - else - { - /* Read next block */ - pCmd[0] = 0x10; - pCmd[1] = 0x30; - pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; - *pCmd_size = 3; - } - } + if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF read already completed ? */ + if ((RW_NDEF_MIFARE_Ndef.MessageSize - RW_NDEF_MIFARE_Ndef.MessagePtr) < 16) { + memcpy(&RW_NDEF_MIFARE_Ndef.pMessage[RW_NDEF_MIFARE_Ndef.MessagePtr], pRsp + 1, RW_NDEF_MIFARE_Ndef.MessageSize - RW_NDEF_MIFARE_Ndef.MessagePtr); + + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) { + pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); + } + + if (ndefReceivedCallback != NULL) { + ndefReceivedCallback(); // Run custom callback + } + } else { + memcpy(&RW_NDEF_MIFARE_Ndef.pMessage[RW_NDEF_MIFARE_Ndef.MessagePtr], pRsp + 1, 16); + RW_NDEF_MIFARE_Ndef.MessagePtr += 16; + RW_NDEF_MIFARE_Ndef.BlkNb++; + + /* Is Blk on next sector ?*/ + if (((RW_NDEF_MIFARE_Ndef.BlkNb + 1) % 4) == 0) { + /* Authenticate next block */ + pCmd[0] = 0x40; + pCmd[1] = (RW_NDEF_MIFARE_Ndef.BlkNb + 1) / 4; + pCmd[2] = 0x01; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Authenticated; + } else { + /* Read next block */ + pCmd[0] = 0x10; + pCmd[1] = 0x30; + pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; + *pCmd_size = 3; + } } - break; + } + break; default: - break; - } + break; + } } -void RW_NDEF_MIFARE_Write_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_MIFARE_Write_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_MIFARE_State) - { + switch (eRW_NDEF_MIFARE_State) { case Initial: - /* Authenticating first sector */ - pCmd[0] = 0x40; - pCmd[1] = 0x00; - pCmd[2] = 0x00; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Authenticated0; - break; + /* Authenticating first sector */ + pCmd[0] = 0x40; + pCmd[1] = 0x00; + pCmd[2] = 0x00; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Authenticated0; + break; case Authenticated0: - if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Read MAD */ - pCmd[0] = 0x10; - pCmd[1] = 0x30; - pCmd[2] = 0x01; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Reading_GPB; - } - break; + if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Read MAD */ + pCmd[0] = 0x10; + pCmd[1] = 0x30; + pCmd[2] = 0x01; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Reading_GPB; + } + break; case Reading_GPB: - if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF format ?*/ - if ((pRsp[3] == MIFARE_NFC_CLUSTER) && (pRsp[4] == MIFARE_FUNCTION_CLUSTER)) - { - pCmd[0] = 0x40; - pCmd[1] = 0x01; - pCmd[2] = 0x01; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Writing_Data1; - RW_NDEF_MIFARE_Ndef.MessagePtr = 0; - RW_NDEF_MIFARE_Ndef.BlkNb = 4; - } + if ((Rsp_size == 18) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF format ?*/ + if ((pRsp[3] == MIFARE_NFC_CLUSTER) && (pRsp[4] == MIFARE_FUNCTION_CLUSTER)) { + pCmd[0] = 0x40; + pCmd[1] = 0x01; + pCmd[2] = 0x01; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Writing_Data1; + RW_NDEF_MIFARE_Ndef.MessagePtr = 0; + RW_NDEF_MIFARE_Ndef.BlkNb = 4; } - break; + } + break; case Writing_Data1: - if ((pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF write already completed ? */ - if (RW_NdefMessage_size <= RW_NDEF_MIFARE_Ndef.MessagePtr) - { - /* Notify application of the NDEF send completion */ - if (pRW_NDEF_PushCb != NULL) - pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); - } - else if (((RW_NDEF_MIFARE_Ndef.BlkNb + 1) % 4) == 0) - { - /* Authenticate next block */ - pCmd[0] = 0x40; - pCmd[1] = (RW_NDEF_MIFARE_Ndef.BlkNb + 1) / 4; - pCmd[2] = 0x01; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Writing_Data1; - RW_NDEF_MIFARE_Ndef.BlkNb++; - } - else - { - pCmd[0] = 0x10; - pCmd[1] = 0xA0; - pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; - *pCmd_size = 3; - eRW_NDEF_MIFARE_State = Writing_Data2; - } + if ((pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF write already completed ? */ + if (RW_NdefMessage_size <= RW_NDEF_MIFARE_Ndef.MessagePtr) { + /* Notify application of the NDEF send completion */ + if (pRW_NDEF_PushCb != NULL) + pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); + } else if (((RW_NDEF_MIFARE_Ndef.BlkNb + 1) % 4) == 0) { + /* Authenticate next block */ + pCmd[0] = 0x40; + pCmd[1] = (RW_NDEF_MIFARE_Ndef.BlkNb + 1) / 4; + pCmd[2] = 0x01; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Writing_Data1; + RW_NDEF_MIFARE_Ndef.BlkNb++; + } else { + pCmd[0] = 0x10; + pCmd[1] = 0xA0; + pCmd[2] = RW_NDEF_MIFARE_Ndef.BlkNb; + *pCmd_size = 3; + eRW_NDEF_MIFARE_State = Writing_Data2; } - break; + } + break; case Writing_Data2: - if ((Rsp_size == 3) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* First block to write ? */ - if (RW_NDEF_MIFARE_Ndef.BlkNb == 4) - { - pCmd[0] = 0x10; - pCmd[1] = 0x00; - pCmd[2] = 0x00; - pCmd[3] = 0x03; - if (RW_NdefMessage_size > 0xFF) - { - pCmd[4] = 0xFF; - pCmd[5] = (RW_NdefMessage_size & 0xFF00) >> 8; - pCmd[6] = RW_NdefMessage_size & 0xFF; - memcpy(&pCmd[7], pRW_NdefMessage, 10); - RW_NDEF_MIFARE_Ndef.MessagePtr = 10; - } - else - { - pCmd[4] = (unsigned char)RW_NdefMessage_size; - memcpy(&pCmd[5], pRW_NdefMessage, 12); - RW_NDEF_MIFARE_Ndef.MessagePtr = 12; - } - } - else - { - pCmd[0] = 0x10; - memcpy(&pCmd[1], pRW_NdefMessage + RW_NDEF_MIFARE_Ndef.MessagePtr, 16); - RW_NDEF_MIFARE_Ndef.MessagePtr += 16; - } - *pCmd_size = 17; - RW_NDEF_MIFARE_Ndef.BlkNb++; - eRW_NDEF_MIFARE_State = Writing_Data1; + if ((Rsp_size == 3) && (pRsp[Rsp_size - 1] == 0x00)) { + /* First block to write ? */ + if (RW_NDEF_MIFARE_Ndef.BlkNb == 4) { + pCmd[0] = 0x10; + pCmd[1] = 0x00; + pCmd[2] = 0x00; + pCmd[3] = 0x03; + if (RW_NdefMessage_size > 0xFF) { + pCmd[4] = 0xFF; + pCmd[5] = (RW_NdefMessage_size & 0xFF00) >> 8; + pCmd[6] = RW_NdefMessage_size & 0xFF; + memcpy(&pCmd[7], pRW_NdefMessage, 10); + RW_NDEF_MIFARE_Ndef.MessagePtr = 10; + } else { + pCmd[4] = (unsigned char)RW_NdefMessage_size; + memcpy(&pCmd[5], pRW_NdefMessage, 12); + RW_NDEF_MIFARE_Ndef.MessagePtr = 12; + } + } else { + pCmd[0] = 0x10; + memcpy(&pCmd[1], pRW_NdefMessage + RW_NDEF_MIFARE_Ndef.MessagePtr, 16); + RW_NDEF_MIFARE_Ndef.MessagePtr += 16; } - break; + *pCmd_size = 17; + RW_NDEF_MIFARE_Ndef.BlkNb++; + eRW_NDEF_MIFARE_State = Writing_Data1; + } + break; case Writing_Data: - if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF write already completed ? */ - if (RW_NdefMessage_size <= RW_NDEF_MIFARE_Ndef.MessagePtr) - { - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PushCb != NULL) - pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); - } - else - { - /* Write NDEF content */ - pCmd[0] = 0xA2; - pCmd[1] = RW_NDEF_MIFARE_Ndef.BlkNb; - memcpy(&pCmd[2], pRW_NdefMessage + RW_NDEF_MIFARE_Ndef.MessagePtr, 4); - *pCmd_size = 6; - - RW_NDEF_MIFARE_Ndef.MessagePtr += 4; - RW_NDEF_MIFARE_Ndef.BlkNb++; - } + if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF write already completed ? */ + if (RW_NdefMessage_size <= RW_NDEF_MIFARE_Ndef.MessagePtr) { + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PushCb != NULL) + pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); + } else { + /* Write NDEF content */ + pCmd[0] = 0xA2; + pCmd[1] = RW_NDEF_MIFARE_Ndef.BlkNb; + memcpy(&pCmd[2], pRW_NdefMessage + RW_NDEF_MIFARE_Ndef.MessagePtr, 4); + *pCmd_size = 6; + + RW_NDEF_MIFARE_Ndef.MessagePtr += 4; + RW_NDEF_MIFARE_Ndef.BlkNb++; } - break; + } + break; default: - break; - } + break; + } } -//#endif -//#endif +// #endif +// #endif diff --git a/src/RW_NDEF_MIFARE.h b/src/RW_NDEF_MIFARE.h index ff7c50b..13b312b 100644 --- a/src/RW_NDEF_MIFARE.h +++ b/src/RW_NDEF_MIFARE.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ void RW_NDEF_MIFARE_Reset(void); void RW_NDEF_MIFARE_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); diff --git a/src/RW_NDEF_T1T.cpp b/src/RW_NDEF_T1T.cpp index 7168cdb..f3cfe0a 100644 --- a/src/RW_NDEF_T1T.cpp +++ b/src/RW_NDEF_T1T.cpp @@ -1,21 +1,21 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef RW_SUPPORT -//#ifndef NO_NDEF_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef RW_SUPPORT +// #ifndef NO_NDEF_SUPPORT #include "RW_NDEF.h" +#include "tool.h" #define T1T_MAGIC_NUMBER 0xE1 #define T1T_NDEF_TLV 0x03 @@ -24,152 +24,136 @@ const unsigned char T1T_RID[] = {0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; const unsigned char T1T_RALL[] = {0x00, 0x00, 0x00}; const unsigned char T1T_READ8[] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; -typedef enum -{ - Initial, - Getting_ID, - Reading_CardContent, - Reading_NDEF +typedef enum { + Initial, + Getting_ID, + Reading_CardContent, + Reading_NDEF } RW_NDEF_T1T_state_t; typedef struct { - unsigned char HR0; - unsigned char HR1; - unsigned char UID[4]; - unsigned char BlkNb; - unsigned short MessagePtr; - unsigned short MessageSize; - unsigned char *pMessage; + unsigned char HR0; + unsigned char HR1; + unsigned char UID[4]; + unsigned char BlkNb; + unsigned short MessagePtr; + unsigned short MessageSize; + unsigned char *pMessage; } RW_NDEF_T1T_Ndef_t; static RW_NDEF_T1T_state_t eRW_NDEF_T1T_State = Initial; static RW_NDEF_T1T_Ndef_t RW_NDEF_T1T_Ndef; -void RW_NDEF_T1T_Reset(void) -{ - eRW_NDEF_T1T_State = Initial; - RW_NDEF_T1T_Ndef.pMessage = NdefBuffer; +void RW_NDEF_T1T_Reset(void) { + eRW_NDEF_T1T_State = Initial; + RW_NDEF_T1T_Ndef.pMessage = NdefBuffer; } -void RW_NDEF_T1T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_T1T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_T1T_State) - { + switch (eRW_NDEF_T1T_State) { case Initial: - /* Send T1T_RID */ - memcpy(pCmd, T1T_RID, sizeof(T1T_RID)); - *pCmd_size = 7; - eRW_NDEF_T1T_State = Getting_ID; - break; + /* Send T1T_RID */ + memcpy(pCmd, T1T_RID, sizeof(T1T_RID)); + *pCmd_size = 7; + eRW_NDEF_T1T_State = Getting_ID; + break; case Getting_ID: - /* Is CC Read and Is Ndef ?*/ - if ((Rsp_size == 7) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Fill File structure */ - RW_NDEF_T1T_Ndef.HR0 = pRsp[0]; - RW_NDEF_T1T_Ndef.HR1 = pRsp[1]; - memcpy(RW_NDEF_T1T_Ndef.UID, &pRsp[2], sizeof(RW_NDEF_T1T_Ndef.UID)); - - /* Read full card content */ - memcpy(pCmd, T1T_RALL, sizeof(T1T_RALL)); - memcpy(&pCmd[3], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID)); - *pCmd_size = sizeof(T1T_RALL) + sizeof(RW_NDEF_T1T_Ndef.UID); - eRW_NDEF_T1T_State = Reading_CardContent; - } - break; + /* Is CC Read and Is Ndef ?*/ + if ((Rsp_size == 7) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Fill File structure */ + RW_NDEF_T1T_Ndef.HR0 = pRsp[0]; + RW_NDEF_T1T_Ndef.HR1 = pRsp[1]; + memcpy(RW_NDEF_T1T_Ndef.UID, &pRsp[2], sizeof(RW_NDEF_T1T_Ndef.UID)); + + /* Read full card content */ + memcpy(pCmd, T1T_RALL, sizeof(T1T_RALL)); + memcpy(&pCmd[3], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID)); + *pCmd_size = sizeof(T1T_RALL) + sizeof(RW_NDEF_T1T_Ndef.UID); + eRW_NDEF_T1T_State = Reading_CardContent; + } + break; case Reading_CardContent: - /* Is Read success ?*/ - if ((Rsp_size == 123) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Check CC */ - if (pRsp[10] == T1T_MAGIC_NUMBER) - { - unsigned char Tmp = 14; - unsigned char data_size; - - /* If not NDEF Type skip TLV */ - while (pRsp[Tmp] != T1T_NDEF_TLV) - { - Tmp += 2 + pRsp[Tmp + 1]; - if (Tmp > Rsp_size) - return; - } - - RW_NDEF_T1T_Ndef.MessageSize = pRsp[Tmp + 1]; - data_size = (Rsp_size - 1) - 16 - Tmp - 2; - - /* If provisioned buffer is not large enough, notify the application and stop reading */ - if (RW_NDEF_T1T_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) - { - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(NULL, 0); - break; - } - - /* Is NDEF read already completed ? */ - if (RW_NDEF_T1T_Ndef.MessageSize <= data_size) - { - memcpy(RW_NDEF_T1T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T1T_Ndef.MessageSize); - - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(RW_NDEF_T1T_Ndef.pMessage, RW_NDEF_T1T_Ndef.MessageSize); - } - else - { - RW_NDEF_T1T_Ndef.MessagePtr = data_size; - memcpy(RW_NDEF_T1T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T1T_Ndef.MessagePtr); - RW_NDEF_T1T_Ndef.BlkNb = 0x10; - - /* Read NDEF content */ - memcpy(pCmd, T1T_READ8, sizeof(T1T_READ8)); - pCmd[1] = RW_NDEF_T1T_Ndef.BlkNb; - memcpy(&pCmd[10], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID)); - *pCmd_size = sizeof(T1T_READ8) + sizeof(RW_NDEF_T1T_Ndef.UID); - - eRW_NDEF_T1T_State = Reading_NDEF; - } - } + /* Is Read success ?*/ + if ((Rsp_size == 123) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Check CC */ + if (pRsp[10] == T1T_MAGIC_NUMBER) { + unsigned char Tmp = 14; + unsigned char data_size; + + /* If not NDEF Type skip TLV */ + while (pRsp[Tmp] != T1T_NDEF_TLV) { + Tmp += 2 + pRsp[Tmp + 1]; + if (Tmp > Rsp_size) + return; + } + + RW_NDEF_T1T_Ndef.MessageSize = pRsp[Tmp + 1]; + data_size = (Rsp_size - 1) - 16 - Tmp - 2; + + /* If provisioned buffer is not large enough, notify the application and stop reading */ + if (RW_NDEF_T1T_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) { + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(NULL, 0); + break; + } + + /* Is NDEF read already completed ? */ + if (RW_NDEF_T1T_Ndef.MessageSize <= data_size) { + memcpy(RW_NDEF_T1T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T1T_Ndef.MessageSize); + + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(RW_NDEF_T1T_Ndef.pMessage, RW_NDEF_T1T_Ndef.MessageSize); + } else { + RW_NDEF_T1T_Ndef.MessagePtr = data_size; + memcpy(RW_NDEF_T1T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T1T_Ndef.MessagePtr); + RW_NDEF_T1T_Ndef.BlkNb = 0x10; + + /* Read NDEF content */ + memcpy(pCmd, T1T_READ8, sizeof(T1T_READ8)); + pCmd[1] = RW_NDEF_T1T_Ndef.BlkNb; + memcpy(&pCmd[10], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID)); + *pCmd_size = sizeof(T1T_READ8) + sizeof(RW_NDEF_T1T_Ndef.UID); + + eRW_NDEF_T1T_State = Reading_NDEF; + } } - break; + } + break; case Reading_NDEF: - /* Is Read success ?*/ - if ((Rsp_size == 10) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF read already completed ? */ - if ((RW_NDEF_T1T_Ndef.MessageSize - RW_NDEF_T1T_Ndef.MessagePtr) < 8) - { - memcpy(&RW_NDEF_T1T_Ndef.pMessage[RW_NDEF_T1T_Ndef.MessagePtr], &pRsp[1], RW_NDEF_T1T_Ndef.MessageSize - RW_NDEF_T1T_Ndef.MessagePtr); - - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(RW_NDEF_T1T_Ndef.pMessage, RW_NDEF_T1T_Ndef.MessageSize); - } - else - { - memcpy(&RW_NDEF_T1T_Ndef.pMessage[RW_NDEF_T1T_Ndef.MessagePtr], &pRsp[1], 8); - RW_NDEF_T1T_Ndef.MessagePtr += 8; - RW_NDEF_T1T_Ndef.BlkNb++; - - /* Read NDEF content */ - memcpy(pCmd, T1T_READ8, sizeof(T1T_READ8)); - pCmd[1] = RW_NDEF_T1T_Ndef.BlkNb; - memcpy(&pCmd[10], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID)); - *pCmd_size = sizeof(T1T_READ8) + sizeof(RW_NDEF_T1T_Ndef.UID); - } + /* Is Read success ?*/ + if ((Rsp_size == 10) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF read already completed ? */ + if ((RW_NDEF_T1T_Ndef.MessageSize - RW_NDEF_T1T_Ndef.MessagePtr) < 8) { + memcpy(&RW_NDEF_T1T_Ndef.pMessage[RW_NDEF_T1T_Ndef.MessagePtr], &pRsp[1], RW_NDEF_T1T_Ndef.MessageSize - RW_NDEF_T1T_Ndef.MessagePtr); + + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(RW_NDEF_T1T_Ndef.pMessage, RW_NDEF_T1T_Ndef.MessageSize); + } else { + memcpy(&RW_NDEF_T1T_Ndef.pMessage[RW_NDEF_T1T_Ndef.MessagePtr], &pRsp[1], 8); + RW_NDEF_T1T_Ndef.MessagePtr += 8; + RW_NDEF_T1T_Ndef.BlkNb++; + + /* Read NDEF content */ + memcpy(pCmd, T1T_READ8, sizeof(T1T_READ8)); + pCmd[1] = RW_NDEF_T1T_Ndef.BlkNb; + memcpy(&pCmd[10], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID)); + *pCmd_size = sizeof(T1T_READ8) + sizeof(RW_NDEF_T1T_Ndef.UID); } - break; + } + break; default: - break; - } + break; + } } -//#endif -//#endif +// #endif +// #endif diff --git a/src/RW_NDEF_T1T.h b/src/RW_NDEF_T1T.h index f64a0f0..1ca0641 100644 --- a/src/RW_NDEF_T1T.h +++ b/src/RW_NDEF_T1T.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ void RW_NDEF_T1T_Reset(void); void RW_NDEF_T1T_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); diff --git a/src/RW_NDEF_T2T.cpp b/src/RW_NDEF_T2T.cpp index bca1452..b242c04 100644 --- a/src/RW_NDEF_T2T.cpp +++ b/src/RW_NDEF_T2T.cpp @@ -1,240 +1,212 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef RW_SUPPORT -//#ifndef NO_NDEF_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef RW_SUPPORT +// #ifndef NO_NDEF_SUPPORT #include "RW_NDEF.h" +#include "tool.h" /* TODO: No support for tag larger than 1024 bytes (requiring SECTOR_SELECT command use) */ #define T2T_MAGIC_NUMBER 0xE1 #define T2T_NDEF_TLV 0x03 -typedef enum -{ - Initial, - Reading_CC, - Reading_Data, - Reading_NDEF, - Writing_Data +typedef enum { + Initial, + Reading_CC, + Reading_Data, + Reading_NDEF, + Writing_Data } RW_NDEF_T2T_state_t; typedef struct { - unsigned char BlkNb; - unsigned short MessagePtr; - unsigned short MessageSize; - unsigned char *pMessage; + unsigned char BlkNb; + unsigned short MessagePtr; + unsigned short MessageSize; + unsigned char *pMessage; } RW_NDEF_T2T_Ndef_t; static RW_NDEF_T2T_state_t eRW_NDEF_T2T_State = Initial; static RW_NDEF_T2T_Ndef_t RW_NDEF_T2T_Ndef; -void RW_NDEF_T2T_Reset(void) -{ - eRW_NDEF_T2T_State = Initial; - RW_NDEF_T2T_Ndef.pMessage = NdefBuffer; +void RW_NDEF_T2T_Reset(void) { + eRW_NDEF_T2T_State = Initial; + RW_NDEF_T2T_Ndef.pMessage = NdefBuffer; } -void RW_NDEF_T2T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_T2T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_T2T_State) - { + switch (eRW_NDEF_T2T_State) { case Initial: - /* Read CC */ + /* Read CC */ + pCmd[0] = 0x30; + pCmd[1] = 0x03; + *pCmd_size = 2; + eRW_NDEF_T2T_State = Reading_CC; + break; + + case Reading_CC: + /* Is CC Read and Is Ndef ?*/ + if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00) && (pRsp[0] == T2T_MAGIC_NUMBER)) { + /* Read First data */ pCmd[0] = 0x30; - pCmd[1] = 0x03; + pCmd[1] = 0x04; *pCmd_size = 2; - eRW_NDEF_T2T_State = Reading_CC; - break; - case Reading_CC: - /* Is CC Read and Is Ndef ?*/ - if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00) && (pRsp[0] == T2T_MAGIC_NUMBER)) - { - /* Read First data */ - pCmd[0] = 0x30; - pCmd[1] = 0x04; - *pCmd_size = 2; - - eRW_NDEF_T2T_State = Reading_Data; - } - break; + eRW_NDEF_T2T_State = Reading_Data; + } + break; case Reading_Data: - /* Is Read success ?*/ - if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00)) - { - unsigned char Tmp = 0; - /* If not NDEF Type skip TLV */ - while (pRsp[Tmp] != T2T_NDEF_TLV) - { - Tmp += 2 + pRsp[Tmp + 1]; - if (Tmp > Rsp_size) - return; - } - - if (pRsp[Tmp + 1] == 0xFF) - { - RW_NDEF_T2T_Ndef.MessageSize = (pRsp[Tmp + 2] << 8) + pRsp[Tmp + 3]; - Tmp += 2; - } - else - RW_NDEF_T2T_Ndef.MessageSize = pRsp[Tmp + 1]; - - /* If provisioned buffer is not large enough or message is empty, notify the application and stop reading */ - if ((RW_NDEF_T2T_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) || (RW_NDEF_T2T_Ndef.MessageSize == 0)) - { - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(NULL, 0); - break; - } - - /* Is NDEF read already completed ? */ - if (RW_NDEF_T2T_Ndef.MessageSize <= ((Rsp_size - 1) - Tmp - 2)) - { - memcpy(RW_NDEF_T2T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T2T_Ndef.MessageSize); - - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(RW_NDEF_T2T_Ndef.pMessage, RW_NDEF_T2T_Ndef.MessageSize); - } - else - { - RW_NDEF_T2T_Ndef.MessagePtr = (Rsp_size - 1) - Tmp - 2; - memcpy(RW_NDEF_T2T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T2T_Ndef.MessagePtr); - RW_NDEF_T2T_Ndef.BlkNb = 8; - - /* Read NDEF content */ - pCmd[0] = 0x30; - pCmd[1] = RW_NDEF_T2T_Ndef.BlkNb; - *pCmd_size = 2; - eRW_NDEF_T2T_State = Reading_NDEF; - } + /* Is Read success ?*/ + if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00)) { + unsigned char Tmp = 0; + /* If not NDEF Type skip TLV */ + while (pRsp[Tmp] != T2T_NDEF_TLV) { + Tmp += 2 + pRsp[Tmp + 1]; + if (Tmp > Rsp_size) + return; + } + + if (pRsp[Tmp + 1] == 0xFF) { + RW_NDEF_T2T_Ndef.MessageSize = (pRsp[Tmp + 2] << 8) + pRsp[Tmp + 3]; + Tmp += 2; + } else + RW_NDEF_T2T_Ndef.MessageSize = pRsp[Tmp + 1]; + + /* If provisioned buffer is not large enough or message is empty, notify the application and stop reading */ + if ((RW_NDEF_T2T_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) || (RW_NDEF_T2T_Ndef.MessageSize == 0)) { + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(NULL, 0); + break; + } + + /* Is NDEF read already completed ? */ + if (RW_NDEF_T2T_Ndef.MessageSize <= ((Rsp_size - 1) - Tmp - 2)) { + memcpy(RW_NDEF_T2T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T2T_Ndef.MessageSize); + + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(RW_NDEF_T2T_Ndef.pMessage, RW_NDEF_T2T_Ndef.MessageSize); + } else { + RW_NDEF_T2T_Ndef.MessagePtr = (Rsp_size - 1) - Tmp - 2; + memcpy(RW_NDEF_T2T_Ndef.pMessage, &pRsp[Tmp + 2], RW_NDEF_T2T_Ndef.MessagePtr); + RW_NDEF_T2T_Ndef.BlkNb = 8; + + /* Read NDEF content */ + pCmd[0] = 0x30; + pCmd[1] = RW_NDEF_T2T_Ndef.BlkNb; + *pCmd_size = 2; + eRW_NDEF_T2T_State = Reading_NDEF; } - break; + } + break; case Reading_NDEF: - /* Is Read success ?*/ - if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF read already completed ? */ - if ((RW_NDEF_T2T_Ndef.MessageSize - RW_NDEF_T2T_Ndef.MessagePtr) < 16) - { - memcpy(&RW_NDEF_T2T_Ndef.pMessage[RW_NDEF_T2T_Ndef.MessagePtr], pRsp, RW_NDEF_T2T_Ndef.MessageSize - RW_NDEF_T2T_Ndef.MessagePtr); - - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(RW_NDEF_T2T_Ndef.pMessage, RW_NDEF_T2T_Ndef.MessageSize); - } - else - { - memcpy(&RW_NDEF_T2T_Ndef.pMessage[RW_NDEF_T2T_Ndef.MessagePtr], pRsp, 16); - RW_NDEF_T2T_Ndef.MessagePtr += 16; - RW_NDEF_T2T_Ndef.BlkNb += 4; - - /* Read NDEF content */ - pCmd[0] = 0x30; - pCmd[1] = RW_NDEF_T2T_Ndef.BlkNb; - *pCmd_size = 2; - } + /* Is Read success ?*/ + if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF read already completed ? */ + if ((RW_NDEF_T2T_Ndef.MessageSize - RW_NDEF_T2T_Ndef.MessagePtr) < 16) { + memcpy(&RW_NDEF_T2T_Ndef.pMessage[RW_NDEF_T2T_Ndef.MessagePtr], pRsp, RW_NDEF_T2T_Ndef.MessageSize - RW_NDEF_T2T_Ndef.MessagePtr); + + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(RW_NDEF_T2T_Ndef.pMessage, RW_NDEF_T2T_Ndef.MessageSize); + } else { + memcpy(&RW_NDEF_T2T_Ndef.pMessage[RW_NDEF_T2T_Ndef.MessagePtr], pRsp, 16); + RW_NDEF_T2T_Ndef.MessagePtr += 16; + RW_NDEF_T2T_Ndef.BlkNb += 4; + + /* Read NDEF content */ + pCmd[0] = 0x30; + pCmd[1] = RW_NDEF_T2T_Ndef.BlkNb; + *pCmd_size = 2; } - break; + } + break; default: - break; - } + break; + } } -void RW_NDEF_T2T_Write_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_T2T_Write_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_T2T_State) - { + switch (eRW_NDEF_T2T_State) { case Initial: - /* Read CC */ - pCmd[0] = 0x30; - pCmd[1] = 0x03; - *pCmd_size = 2; - eRW_NDEF_T2T_State = Reading_CC; - break; + /* Read CC */ + pCmd[0] = 0x30; + pCmd[1] = 0x03; + *pCmd_size = 2; + eRW_NDEF_T2T_State = Reading_CC; + break; case Reading_CC: - /* Is CC Read, Is Ndef and is R/W ?*/ - if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00) && (pRsp[0] == T2T_MAGIC_NUMBER) && (pRsp[3] == 0x00)) - { - /* Is size enough ? */ - if (pRsp[2] * 8 >= RW_NdefMessage_size) - { - /* Write First data */ - pCmd[0] = 0xA2; - pCmd[1] = 0x04; - pCmd[2] = 0x03; - if (RW_NdefMessage_size > 0xFF) - { - pCmd[3] = 0xFF; - pCmd[4] = (RW_NdefMessage_size & 0xFF00) >> 8; - pCmd[5] = RW_NdefMessage_size & 0xFF; - RW_NDEF_T2T_Ndef.MessagePtr = 0; - } - else - { - pCmd[3] = (unsigned char)RW_NdefMessage_size; - memcpy(&pCmd[4], pRW_NdefMessage, 2); - RW_NDEF_T2T_Ndef.MessagePtr = 2; - } - RW_NDEF_T2T_Ndef.BlkNb = 5; - *pCmd_size = 6; - eRW_NDEF_T2T_State = Writing_Data; - } + /* Is CC Read, Is Ndef and is R/W ?*/ + if ((Rsp_size == 17) && (pRsp[Rsp_size - 1] == 0x00) && (pRsp[0] == T2T_MAGIC_NUMBER) && (pRsp[3] == 0x00)) { + /* Is size enough ? */ + if (pRsp[2] * 8 >= RW_NdefMessage_size) { + /* Write First data */ + pCmd[0] = 0xA2; + pCmd[1] = 0x04; + pCmd[2] = 0x03; + if (RW_NdefMessage_size > 0xFF) { + pCmd[3] = 0xFF; + pCmd[4] = (RW_NdefMessage_size & 0xFF00) >> 8; + pCmd[5] = RW_NdefMessage_size & 0xFF; + RW_NDEF_T2T_Ndef.MessagePtr = 0; + } else { + pCmd[3] = (unsigned char)RW_NdefMessage_size; + memcpy(&pCmd[4], pRW_NdefMessage, 2); + RW_NDEF_T2T_Ndef.MessagePtr = 2; + } + RW_NDEF_T2T_Ndef.BlkNb = 5; + *pCmd_size = 6; + eRW_NDEF_T2T_State = Writing_Data; } - break; + } + break; case Writing_Data: - /* Is Write success ?*/ - if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) - { - /* Is NDEF write already completed ? */ - if (RW_NdefMessage_size <= RW_NDEF_T2T_Ndef.MessagePtr) - { - /* Notify application of the NDEF send completion */ - if (pRW_NDEF_PushCb != NULL) - pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); - } - else - { - /* Write NDEF content */ - pCmd[0] = 0xA2; - pCmd[1] = RW_NDEF_T2T_Ndef.BlkNb; - memcpy(&pCmd[2], pRW_NdefMessage + RW_NDEF_T2T_Ndef.MessagePtr, 4); - *pCmd_size = 6; - - RW_NDEF_T2T_Ndef.MessagePtr += 4; - RW_NDEF_T2T_Ndef.BlkNb++; - } + /* Is Write success ?*/ + if ((Rsp_size == 2) && (pRsp[Rsp_size - 1] == 0x00)) { + /* Is NDEF write already completed ? */ + if (RW_NdefMessage_size <= RW_NDEF_T2T_Ndef.MessagePtr) { + /* Notify application of the NDEF send completion */ + if (pRW_NDEF_PushCb != NULL) + pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); + } else { + /* Write NDEF content */ + pCmd[0] = 0xA2; + pCmd[1] = RW_NDEF_T2T_Ndef.BlkNb; + memcpy(&pCmd[2], pRW_NdefMessage + RW_NDEF_T2T_Ndef.MessagePtr, 4); + *pCmd_size = 6; + + RW_NDEF_T2T_Ndef.MessagePtr += 4; + RW_NDEF_T2T_Ndef.BlkNb++; } - break; + } + break; default: - break; - } + break; + } } -//#endif -//#endif +// #endif +// #endif diff --git a/src/RW_NDEF_T2T.h b/src/RW_NDEF_T2T.h index b6bb928..106c08f 100644 --- a/src/RW_NDEF_T2T.h +++ b/src/RW_NDEF_T2T.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ void RW_NDEF_T2T_Reset(void); void RW_NDEF_T2T_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); diff --git a/src/RW_NDEF_T3T.cpp b/src/RW_NDEF_T3T.cpp index a9e58c1..56d1ab2 100644 --- a/src/RW_NDEF_T3T.cpp +++ b/src/RW_NDEF_T3T.cpp @@ -1,127 +1,115 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef RW_SUPPORT -//#ifndef NO_NDEF_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef RW_SUPPORT +// #ifndef NO_NDEF_SUPPORT #include "RW_NDEF.h" +#include "tool.h" #define T3T_MAGIC_NUMBER 0xE1 #define T3T_NDEF_TLV 0x03 unsigned char T3T_Check[] = {0x10, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0B, 0x00, 0x1, 0x80, 0x00}; -typedef enum -{ - Initial, - Getting_AttributeInfo, - Reading_CardContent +typedef enum { + Initial, + Getting_AttributeInfo, + Reading_CardContent } RW_NDEF_T3T_state_t; -typedef struct -{ - unsigned char IDm[8]; - unsigned char BlkNb; - unsigned short Ptr; - unsigned short Size; - unsigned char *p; +typedef struct { + unsigned char IDm[8]; + unsigned char BlkNb; + unsigned short Ptr; + unsigned short Size; + unsigned char *p; } RW_NDEF_T3T_Ndef_t; static RW_NDEF_T3T_state_t eRW_NDEF_T3T_State = Initial; static RW_NDEF_T3T_Ndef_t RW_NDEF_T3T_Ndef; -void RW_NDEF_T3T_Reset(void) -{ - eRW_NDEF_T3T_State = Initial; - RW_NDEF_T3T_Ndef.p = NdefBuffer; +void RW_NDEF_T3T_Reset(void) { + eRW_NDEF_T3T_State = Initial; + RW_NDEF_T3T_Ndef.p = NdefBuffer; } -void RW_NDEF_T3T_SetIDm(unsigned char *pIDm) -{ - memcpy(RW_NDEF_T3T_Ndef.IDm, pIDm, sizeof(RW_NDEF_T3T_Ndef.IDm)); - memcpy(&T3T_Check[2], pIDm, sizeof(RW_NDEF_T3T_Ndef.IDm)); +void RW_NDEF_T3T_SetIDm(unsigned char *pIDm) { + memcpy(RW_NDEF_T3T_Ndef.IDm, pIDm, sizeof(RW_NDEF_T3T_Ndef.IDm)); + memcpy(&T3T_Check[2], pIDm, sizeof(RW_NDEF_T3T_Ndef.IDm)); } -void RW_NDEF_T3T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_T3T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_T3T_State) - { + switch (eRW_NDEF_T3T_State) { case Initial: - /* Get AttributeInfo */ - memcpy(pCmd, T3T_Check, sizeof(T3T_Check)); - *pCmd_size = sizeof(T3T_Check); - eRW_NDEF_T3T_State = Getting_AttributeInfo; - break; + /* Get AttributeInfo */ + memcpy(pCmd, T3T_Check, sizeof(T3T_Check)); + *pCmd_size = sizeof(T3T_Check); + eRW_NDEF_T3T_State = Getting_AttributeInfo; + break; case Getting_AttributeInfo: - /* Is Check success ?*/ - if ((pRsp[Rsp_size - 1] == 0x00) && (pRsp[1] == 0x07) && (pRsp[10] == 0x00) && (pRsp[11] == 0x00)) - { - /* Fill File structure */ - RW_NDEF_T3T_Ndef.Size = (pRsp[24] << 16) + (pRsp[25] << 16) + pRsp[26]; - - /* If provisioned buffer is not large enough or size is null, notify the application and stop reading */ - if ((RW_NDEF_T3T_Ndef.Size > RW_MAX_NDEF_FILE_SIZE) || (RW_NDEF_T3T_Ndef.Size == 0)) - { - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(NULL, 0); - break; - } - - RW_NDEF_T3T_Ndef.Ptr = 0; - RW_NDEF_T3T_Ndef.BlkNb = 1; - - /* Read first NDEF block */ - memcpy(pCmd, T3T_Check, sizeof(T3T_Check)); - pCmd[15] = 0x01; - *pCmd_size = sizeof(T3T_Check); - eRW_NDEF_T3T_State = Reading_CardContent; + /* Is Check success ?*/ + if ((pRsp[Rsp_size - 1] == 0x00) && (pRsp[1] == 0x07) && (pRsp[10] == 0x00) && (pRsp[11] == 0x00)) { + /* Fill File structure */ + RW_NDEF_T3T_Ndef.Size = (pRsp[24] << 16) + (pRsp[25] << 16) + pRsp[26]; + + /* If provisioned buffer is not large enough or size is null, notify the application and stop reading */ + if ((RW_NDEF_T3T_Ndef.Size > RW_MAX_NDEF_FILE_SIZE) || (RW_NDEF_T3T_Ndef.Size == 0)) { + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(NULL, 0); + break; } - break; + + RW_NDEF_T3T_Ndef.Ptr = 0; + RW_NDEF_T3T_Ndef.BlkNb = 1; + + /* Read first NDEF block */ + memcpy(pCmd, T3T_Check, sizeof(T3T_Check)); + pCmd[15] = 0x01; + *pCmd_size = sizeof(T3T_Check); + eRW_NDEF_T3T_State = Reading_CardContent; + } + break; case Reading_CardContent: - /* Is Check success ?*/ - if ((pRsp[Rsp_size - 1] == 0x00) && (pRsp[1] == 0x07) && (pRsp[10] == 0x00) && (pRsp[11] == 0x00)) - { - /* Is NDEF message read completed ?*/ - if ((RW_NDEF_T3T_Ndef.Size - RW_NDEF_T3T_Ndef.Ptr) <= 16) - { - memcpy(&RW_NDEF_T3T_Ndef.p[RW_NDEF_T3T_Ndef.Ptr], &pRsp[13], (RW_NDEF_T3T_Ndef.Size - RW_NDEF_T3T_Ndef.Ptr)); - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(RW_NDEF_T3T_Ndef.p, RW_NDEF_T3T_Ndef.Size); - } - else - { - memcpy(&RW_NDEF_T3T_Ndef.p[RW_NDEF_T3T_Ndef.Ptr], &pRsp[13], 16); - RW_NDEF_T3T_Ndef.Ptr += 16; - RW_NDEF_T3T_Ndef.BlkNb++; - - /* Read next NDEF block */ - memcpy(pCmd, T3T_Check, sizeof(T3T_Check)); - pCmd[15] = RW_NDEF_T3T_Ndef.BlkNb; - *pCmd_size = sizeof(T3T_Check); - } + /* Is Check success ?*/ + if ((pRsp[Rsp_size - 1] == 0x00) && (pRsp[1] == 0x07) && (pRsp[10] == 0x00) && (pRsp[11] == 0x00)) { + /* Is NDEF message read completed ?*/ + if ((RW_NDEF_T3T_Ndef.Size - RW_NDEF_T3T_Ndef.Ptr) <= 16) { + memcpy(&RW_NDEF_T3T_Ndef.p[RW_NDEF_T3T_Ndef.Ptr], &pRsp[13], (RW_NDEF_T3T_Ndef.Size - RW_NDEF_T3T_Ndef.Ptr)); + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(RW_NDEF_T3T_Ndef.p, RW_NDEF_T3T_Ndef.Size); + } else { + memcpy(&RW_NDEF_T3T_Ndef.p[RW_NDEF_T3T_Ndef.Ptr], &pRsp[13], 16); + RW_NDEF_T3T_Ndef.Ptr += 16; + RW_NDEF_T3T_Ndef.BlkNb++; + + /* Read next NDEF block */ + memcpy(pCmd, T3T_Check, sizeof(T3T_Check)); + pCmd[15] = RW_NDEF_T3T_Ndef.BlkNb; + *pCmd_size = sizeof(T3T_Check); } - break; + } + break; default: - break; - } + break; + } } -//#endif -//#endif +// #endif +// #endif diff --git a/src/RW_NDEF_T3T.h b/src/RW_NDEF_T3T.h index a1fdc23..18b639f 100644 --- a/src/RW_NDEF_T3T.h +++ b/src/RW_NDEF_T3T.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ void RW_NDEF_T3T_Reset(void); void RW_NDEF_T3T_SetIDm(unsigned char *pIDm); diff --git a/src/RW_NDEF_T4T.cpp b/src/RW_NDEF_T4T.cpp index 97b4619..7244d2e 100644 --- a/src/RW_NDEF_T4T.cpp +++ b/src/RW_NDEF_T4T.cpp @@ -1,21 +1,21 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef RW_SUPPORT -//#ifndef NO_NDEF_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef RW_SUPPORT +// #ifndef NO_NDEF_SUPPORT #include "RW_NDEF.h" +#include "tool.h" const unsigned char RW_NDEF_T4T_APP_Select20[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01, 0x00}; const unsigned char RW_NDEF_T4T_APP_Select10[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00}; @@ -28,339 +28,304 @@ const unsigned char RW_NDEF_T4T_OK[] = {0x90, 0x00}; #define WRITE_SZ 54 -typedef enum -{ - Initial, - Selecting_NDEF_Application20, - Selecting_NDEF_Application10, - Selecting_CC, - Reading_CC, - Selecting_NDEF, - Reading_NDEF_Size, - Reading_NDEF, - Writing_NDEF, - Writing_NDEFsize, - Write_NDEFcomplete +typedef enum { + Initial, + Selecting_NDEF_Application20, + Selecting_NDEF_Application10, + Selecting_CC, + Reading_CC, + Selecting_NDEF, + Reading_NDEF_Size, + Reading_NDEF, + Writing_NDEF, + Writing_NDEFsize, + Write_NDEFcomplete } RW_NDEF_T4T_state_t; typedef struct { - unsigned char MappingVersion; - unsigned short MLe; - unsigned short MLc; - unsigned char FileID[2]; - unsigned short MaxNdefFileSize; - unsigned char RdAccess; - unsigned char WrAccess; - unsigned short MessagePtr; - unsigned short MessageSize; - unsigned char *pMessage; + unsigned char MappingVersion; + unsigned short MLe; + unsigned short MLc; + unsigned char FileID[2]; + unsigned short MaxNdefFileSize; + unsigned char RdAccess; + unsigned char WrAccess; + unsigned short MessagePtr; + unsigned short MessageSize; + unsigned char *pMessage; } RW_NDEF_T4T_Ndef_t; static RW_NDEF_T4T_state_t eRW_NDEF_T4T_State = Initial; static RW_NDEF_T4T_Ndef_t RW_NDEF_T4T_Ndef; -void RW_NDEF_T4T_Reset(void) -{ - eRW_NDEF_T4T_State = Initial; - RW_NDEF_T4T_Ndef.pMessage = NdefBuffer; +void RW_NDEF_T4T_Reset(void) { + eRW_NDEF_T4T_State = Initial; + RW_NDEF_T4T_Ndef.pMessage = NdefBuffer; } -void RW_NDEF_T4T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_T4T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_T4T_State) - { + switch (eRW_NDEF_T4T_State) { case Initial: - /* Select NDEF Application in version 2.0 */ - memcpy(pCmd, RW_NDEF_T4T_APP_Select20, sizeof(RW_NDEF_T4T_APP_Select20)); - *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select20); - eRW_NDEF_T4T_State = Selecting_NDEF_Application20; - break; + /* Select NDEF Application in version 2.0 */ + memcpy(pCmd, RW_NDEF_T4T_APP_Select20, sizeof(RW_NDEF_T4T_APP_Select20)); + *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select20); + eRW_NDEF_T4T_State = Selecting_NDEF_Application20; + break; case Selecting_NDEF_Application20: - /* Is NDEF Application Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Select CC */ - memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); - *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); - eRW_NDEF_T4T_State = Selecting_CC; - } - else - { - /* Select NDEF Application in version 1.0 */ - memcpy(pCmd, RW_NDEF_T4T_APP_Select10, sizeof(RW_NDEF_T4T_APP_Select10)); - *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select10); - eRW_NDEF_T4T_State = Selecting_NDEF_Application10; - } - break; + /* Is NDEF Application Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Select CC */ + memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); + *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); + eRW_NDEF_T4T_State = Selecting_CC; + } else { + /* Select NDEF Application in version 1.0 */ + memcpy(pCmd, RW_NDEF_T4T_APP_Select10, sizeof(RW_NDEF_T4T_APP_Select10)); + *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select10); + eRW_NDEF_T4T_State = Selecting_NDEF_Application10; + } + break; case Selecting_NDEF_Application10: - /* Is NDEF Application Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Select CC */ - memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); - pCmd[3] = 0x00; - *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); - eRW_NDEF_T4T_State = Selecting_CC; - } - break; + /* Is NDEF Application Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Select CC */ + memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); + pCmd[3] = 0x00; + *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); + eRW_NDEF_T4T_State = Selecting_CC; + } + break; case Selecting_CC: - /* Is CC Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Read CC */ - memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); - *pCmd_size = sizeof(RW_NDEF_T4T_Read); - eRW_NDEF_T4T_State = Reading_CC; - } - break; + /* Is CC Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Read CC */ + memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); + *pCmd_size = sizeof(RW_NDEF_T4T_Read); + eRW_NDEF_T4T_State = Reading_CC; + } + break; case Reading_CC: - /* Is CC Read ?*/ - if ((!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) && (Rsp_size == 15 + 2)) - { - /* Fill CC structure */ - RW_NDEF_T4T_Ndef.MappingVersion = pRsp[2]; - RW_NDEF_T4T_Ndef.MLe = (pRsp[3] << 8) + pRsp[4]; - RW_NDEF_T4T_Ndef.MLc = (pRsp[5] << 8) + pRsp[6]; - RW_NDEF_T4T_Ndef.FileID[0] = pRsp[9]; - RW_NDEF_T4T_Ndef.FileID[1] = pRsp[10]; - RW_NDEF_T4T_Ndef.MaxNdefFileSize = (pRsp[11] << 8) + pRsp[12]; - RW_NDEF_T4T_Ndef.RdAccess = pRsp[13]; - RW_NDEF_T4T_Ndef.WrAccess = pRsp[14]; - - /* Select NDEF */ - memcpy(pCmd, RW_NDEF_T4T_NDEF_Select, sizeof(RW_NDEF_T4T_NDEF_Select)); - if (RW_NDEF_T4T_Ndef.MappingVersion == 0x10) - pCmd[3] = 0x00; - pCmd[5] = RW_NDEF_T4T_Ndef.FileID[0]; - pCmd[6] = RW_NDEF_T4T_Ndef.FileID[1]; - *pCmd_size = sizeof(RW_NDEF_T4T_NDEF_Select); - eRW_NDEF_T4T_State = Selecting_NDEF; - } - break; + /* Is CC Read ?*/ + if ((!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) && (Rsp_size == 15 + 2)) { + /* Fill CC structure */ + RW_NDEF_T4T_Ndef.MappingVersion = pRsp[2]; + RW_NDEF_T4T_Ndef.MLe = (pRsp[3] << 8) + pRsp[4]; + RW_NDEF_T4T_Ndef.MLc = (pRsp[5] << 8) + pRsp[6]; + RW_NDEF_T4T_Ndef.FileID[0] = pRsp[9]; + RW_NDEF_T4T_Ndef.FileID[1] = pRsp[10]; + RW_NDEF_T4T_Ndef.MaxNdefFileSize = (pRsp[11] << 8) + pRsp[12]; + RW_NDEF_T4T_Ndef.RdAccess = pRsp[13]; + RW_NDEF_T4T_Ndef.WrAccess = pRsp[14]; + + /* Select NDEF */ + memcpy(pCmd, RW_NDEF_T4T_NDEF_Select, sizeof(RW_NDEF_T4T_NDEF_Select)); + if (RW_NDEF_T4T_Ndef.MappingVersion == 0x10) + pCmd[3] = 0x00; + pCmd[5] = RW_NDEF_T4T_Ndef.FileID[0]; + pCmd[6] = RW_NDEF_T4T_Ndef.FileID[1]; + *pCmd_size = sizeof(RW_NDEF_T4T_NDEF_Select); + eRW_NDEF_T4T_State = Selecting_NDEF; + } + break; case Selecting_NDEF: - /* Is NDEF Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Get NDEF file size */ - memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); - *pCmd_size = sizeof(RW_NDEF_T4T_Read); - pCmd[4] = 2; - eRW_NDEF_T4T_State = Reading_NDEF_Size; - } - break; + /* Is NDEF Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Get NDEF file size */ + memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); + *pCmd_size = sizeof(RW_NDEF_T4T_Read); + pCmd[4] = 2; + eRW_NDEF_T4T_State = Reading_NDEF_Size; + } + break; case Reading_NDEF_Size: - /* Is Read Success ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - RW_NDEF_T4T_Ndef.MessageSize = (pRsp[0] << 8) + pRsp[1]; - - /* If provisioned buffer is not large enough, notify the application and stop reading */ - if (RW_NDEF_T4T_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) - { - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(NULL, 0); - break; - } - - RW_NDEF_T4T_Ndef.MessagePtr = 0; - - /* Read NDEF data */ - memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); - pCmd[3] = 2; - pCmd[4] = (RW_NDEF_T4T_Ndef.MessageSize > RW_NDEF_T4T_Ndef.MLe - 1) ? RW_NDEF_T4T_Ndef.MLe - 1 : (unsigned char)RW_NDEF_T4T_Ndef.MessageSize; - *pCmd_size = sizeof(RW_NDEF_T4T_Read); - eRW_NDEF_T4T_State = Reading_NDEF; + /* Is Read Success ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + RW_NDEF_T4T_Ndef.MessageSize = (pRsp[0] << 8) + pRsp[1]; + + /* If provisioned buffer is not large enough, notify the application and stop reading */ + if (RW_NDEF_T4T_Ndef.MessageSize > RW_MAX_NDEF_FILE_SIZE) { + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(NULL, 0); + break; } - break; + + RW_NDEF_T4T_Ndef.MessagePtr = 0; + + /* Read NDEF data */ + memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); + pCmd[3] = 2; + pCmd[4] = (RW_NDEF_T4T_Ndef.MessageSize > RW_NDEF_T4T_Ndef.MLe - 1) ? RW_NDEF_T4T_Ndef.MLe - 1 : (unsigned char)RW_NDEF_T4T_Ndef.MessageSize; + *pCmd_size = sizeof(RW_NDEF_T4T_Read); + eRW_NDEF_T4T_State = Reading_NDEF; + } + break; case Reading_NDEF: - /* Is Read Success ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - memcpy(&RW_NDEF_T4T_Ndef.pMessage[RW_NDEF_T4T_Ndef.MessagePtr], pRsp, Rsp_size - 2); - RW_NDEF_T4T_Ndef.MessagePtr += Rsp_size - 2; - - /* Is NDEF message read completed ?*/ - if (RW_NDEF_T4T_Ndef.MessagePtr == RW_NDEF_T4T_Ndef.MessageSize) - { - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PullCb != NULL) - pRW_NDEF_PullCb(RW_NDEF_T4T_Ndef.pMessage, RW_NDEF_T4T_Ndef.MessageSize); - } - else - { - /* Read NDEF data */ - memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); - pCmd[3] = RW_NDEF_T4T_Ndef.MessagePtr + 2; - pCmd[4] = ((RW_NDEF_T4T_Ndef.MessageSize - RW_NDEF_T4T_Ndef.MessagePtr) > RW_NDEF_T4T_Ndef.MLe - 1) ? RW_NDEF_T4T_Ndef.MLe - 1 : (unsigned char)(RW_NDEF_T4T_Ndef.MessageSize - RW_NDEF_T4T_Ndef.MessagePtr); - *pCmd_size = sizeof(RW_NDEF_T4T_Read); - } + /* Is Read Success ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + memcpy(&RW_NDEF_T4T_Ndef.pMessage[RW_NDEF_T4T_Ndef.MessagePtr], pRsp, Rsp_size - 2); + RW_NDEF_T4T_Ndef.MessagePtr += Rsp_size - 2; + + /* Is NDEF message read completed ?*/ + if (RW_NDEF_T4T_Ndef.MessagePtr == RW_NDEF_T4T_Ndef.MessageSize) { + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PullCb != NULL) + pRW_NDEF_PullCb(RW_NDEF_T4T_Ndef.pMessage, RW_NDEF_T4T_Ndef.MessageSize); + } else { + /* Read NDEF data */ + memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); + pCmd[3] = RW_NDEF_T4T_Ndef.MessagePtr + 2; + pCmd[4] = ((RW_NDEF_T4T_Ndef.MessageSize - RW_NDEF_T4T_Ndef.MessagePtr) > RW_NDEF_T4T_Ndef.MLe - 1) ? RW_NDEF_T4T_Ndef.MLe - 1 : (unsigned char)(RW_NDEF_T4T_Ndef.MessageSize - RW_NDEF_T4T_Ndef.MessagePtr); + *pCmd_size = sizeof(RW_NDEF_T4T_Read); } - break; + } + break; default: - break; - } + break; + } } -void RW_NDEF_T4T_Write_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) -{ - /* By default no further command to be sent */ - *pCmd_size = 0; +void RW_NDEF_T4T_Write_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size) { + /* By default no further command to be sent */ + *pCmd_size = 0; - switch (eRW_NDEF_T4T_State) - { + switch (eRW_NDEF_T4T_State) { case Initial: - /* Select NDEF Application in version 2.0 */ - memcpy(pCmd, RW_NDEF_T4T_APP_Select20, sizeof(RW_NDEF_T4T_APP_Select20)); - *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select20); - eRW_NDEF_T4T_State = Selecting_NDEF_Application20; - break; + /* Select NDEF Application in version 2.0 */ + memcpy(pCmd, RW_NDEF_T4T_APP_Select20, sizeof(RW_NDEF_T4T_APP_Select20)); + *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select20); + eRW_NDEF_T4T_State = Selecting_NDEF_Application20; + break; case Selecting_NDEF_Application20: - /* Is NDEF Application Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Select CC */ - memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); - *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); - eRW_NDEF_T4T_State = Selecting_CC; - } - else - { - /* Select NDEF Application in version 1.0 */ - memcpy(pCmd, RW_NDEF_T4T_APP_Select10, sizeof(RW_NDEF_T4T_APP_Select10)); - *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select10); - eRW_NDEF_T4T_State = Selecting_NDEF_Application10; - } - break; + /* Is NDEF Application Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Select CC */ + memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); + *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); + eRW_NDEF_T4T_State = Selecting_CC; + } else { + /* Select NDEF Application in version 1.0 */ + memcpy(pCmd, RW_NDEF_T4T_APP_Select10, sizeof(RW_NDEF_T4T_APP_Select10)); + *pCmd_size = sizeof(RW_NDEF_T4T_APP_Select10); + eRW_NDEF_T4T_State = Selecting_NDEF_Application10; + } + break; case Selecting_NDEF_Application10: - /* Is NDEF Application Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Select CC */ - memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); - pCmd[3] = 0x00; - *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); - eRW_NDEF_T4T_State = Selecting_CC; - } - break; + /* Is NDEF Application Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Select CC */ + memcpy(pCmd, RW_NDEF_T4T_CC_Select, sizeof(RW_NDEF_T4T_CC_Select)); + pCmd[3] = 0x00; + *pCmd_size = sizeof(RW_NDEF_T4T_CC_Select); + eRW_NDEF_T4T_State = Selecting_CC; + } + break; case Selecting_CC: - /* Is CC Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Read CC */ - memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); - *pCmd_size = sizeof(RW_NDEF_T4T_Read); - eRW_NDEF_T4T_State = Reading_CC; - } - break; + /* Is CC Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Read CC */ + memcpy(pCmd, RW_NDEF_T4T_Read, sizeof(RW_NDEF_T4T_Read)); + *pCmd_size = sizeof(RW_NDEF_T4T_Read); + eRW_NDEF_T4T_State = Reading_CC; + } + break; case Reading_CC: - /* Is CC Read ?*/ - if ((!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) && (Rsp_size == 15 + 2)) - { - /* Fill CC structure */ - RW_NDEF_T4T_Ndef.MappingVersion = pRsp[2]; - RW_NDEF_T4T_Ndef.MLe = (pRsp[3] << 8) + pRsp[4]; - RW_NDEF_T4T_Ndef.MLc = (pRsp[5] << 8) + pRsp[6]; - RW_NDEF_T4T_Ndef.FileID[0] = pRsp[9]; - RW_NDEF_T4T_Ndef.FileID[1] = pRsp[10]; - RW_NDEF_T4T_Ndef.MaxNdefFileSize = (pRsp[11] << 8) + pRsp[12]; - RW_NDEF_T4T_Ndef.RdAccess = pRsp[13]; - RW_NDEF_T4T_Ndef.WrAccess = pRsp[14]; - - /* Select NDEF */ - memcpy(pCmd, RW_NDEF_T4T_NDEF_Select, sizeof(RW_NDEF_T4T_NDEF_Select)); - if (RW_NDEF_T4T_Ndef.MappingVersion == 0x10) - pCmd[3] = 0x00; - pCmd[5] = RW_NDEF_T4T_Ndef.FileID[0]; - pCmd[6] = RW_NDEF_T4T_Ndef.FileID[1]; - *pCmd_size = sizeof(RW_NDEF_T4T_NDEF_Select); - eRW_NDEF_T4T_State = Selecting_NDEF; - } - break; + /* Is CC Read ?*/ + if ((!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) && (Rsp_size == 15 + 2)) { + /* Fill CC structure */ + RW_NDEF_T4T_Ndef.MappingVersion = pRsp[2]; + RW_NDEF_T4T_Ndef.MLe = (pRsp[3] << 8) + pRsp[4]; + RW_NDEF_T4T_Ndef.MLc = (pRsp[5] << 8) + pRsp[6]; + RW_NDEF_T4T_Ndef.FileID[0] = pRsp[9]; + RW_NDEF_T4T_Ndef.FileID[1] = pRsp[10]; + RW_NDEF_T4T_Ndef.MaxNdefFileSize = (pRsp[11] << 8) + pRsp[12]; + RW_NDEF_T4T_Ndef.RdAccess = pRsp[13]; + RW_NDEF_T4T_Ndef.WrAccess = pRsp[14]; + + /* Select NDEF */ + memcpy(pCmd, RW_NDEF_T4T_NDEF_Select, sizeof(RW_NDEF_T4T_NDEF_Select)); + if (RW_NDEF_T4T_Ndef.MappingVersion == 0x10) + pCmd[3] = 0x00; + pCmd[5] = RW_NDEF_T4T_Ndef.FileID[0]; + pCmd[6] = RW_NDEF_T4T_Ndef.FileID[1]; + *pCmd_size = sizeof(RW_NDEF_T4T_NDEF_Select); + eRW_NDEF_T4T_State = Selecting_NDEF; + } + break; case Selecting_NDEF: - /* Is NDEF Selected ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Clearing NDEF message size*/ - memcpy(pCmd, RW_NDEF_T4T_Write, sizeof(RW_NDEF_T4T_Write)); - pCmd[4] = 2; - pCmd[5] = 0; - pCmd[6] = 0; - *pCmd_size = sizeof(RW_NDEF_T4T_Write) + 2; - RW_NDEF_T4T_Ndef.MessagePtr = 0; - eRW_NDEF_T4T_State = Writing_NDEF; - } - break; + /* Is NDEF Selected ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Clearing NDEF message size*/ + memcpy(pCmd, RW_NDEF_T4T_Write, sizeof(RW_NDEF_T4T_Write)); + pCmd[4] = 2; + pCmd[5] = 0; + pCmd[6] = 0; + *pCmd_size = sizeof(RW_NDEF_T4T_Write) + 2; + RW_NDEF_T4T_Ndef.MessagePtr = 0; + eRW_NDEF_T4T_State = Writing_NDEF; + } + break; case Writing_NDEF: - /* Is Write Success ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Writing NDEF message */ - memcpy(pCmd, RW_NDEF_T4T_Write, sizeof(RW_NDEF_T4T_Write)); - pCmd[2] = (RW_NDEF_T4T_Ndef.MessagePtr + 2) >> 8; - pCmd[3] = (RW_NDEF_T4T_Ndef.MessagePtr + 2) & 0xFF; - if ((RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr) < WRITE_SZ) - { - pCmd[4] = (RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr); - memcpy(&pCmd[5], pRW_NdefMessage + RW_NDEF_T4T_Ndef.MessagePtr, (RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr)); - *pCmd_size = sizeof(RW_NDEF_T4T_Write) + (RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr); - eRW_NDEF_T4T_State = Writing_NDEFsize; - } - else - { - pCmd[4] = WRITE_SZ; - memcpy(&pCmd[5], pRW_NdefMessage + RW_NDEF_T4T_Ndef.MessagePtr, WRITE_SZ); - *pCmd_size = sizeof(RW_NDEF_T4T_Write) + WRITE_SZ; - RW_NDEF_T4T_Ndef.MessagePtr += WRITE_SZ; - eRW_NDEF_T4T_State = Writing_NDEF; - } + /* Is Write Success ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Writing NDEF message */ + memcpy(pCmd, RW_NDEF_T4T_Write, sizeof(RW_NDEF_T4T_Write)); + pCmd[2] = (RW_NDEF_T4T_Ndef.MessagePtr + 2) >> 8; + pCmd[3] = (RW_NDEF_T4T_Ndef.MessagePtr + 2) & 0xFF; + if ((RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr) < WRITE_SZ) { + pCmd[4] = (RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr); + memcpy(&pCmd[5], pRW_NdefMessage + RW_NDEF_T4T_Ndef.MessagePtr, (RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr)); + *pCmd_size = sizeof(RW_NDEF_T4T_Write) + (RW_NdefMessage_size - RW_NDEF_T4T_Ndef.MessagePtr); + eRW_NDEF_T4T_State = Writing_NDEFsize; + } else { + pCmd[4] = WRITE_SZ; + memcpy(&pCmd[5], pRW_NdefMessage + RW_NDEF_T4T_Ndef.MessagePtr, WRITE_SZ); + *pCmd_size = sizeof(RW_NDEF_T4T_Write) + WRITE_SZ; + RW_NDEF_T4T_Ndef.MessagePtr += WRITE_SZ; + eRW_NDEF_T4T_State = Writing_NDEF; } - break; + } + break; case Writing_NDEFsize: - /* Is Write Success ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - memcpy(pCmd, RW_NDEF_T4T_Write, sizeof(RW_NDEF_T4T_Write)); - pCmd[4] = 2; - pCmd[5] = RW_NdefMessage_size >> 8; - pCmd[6] = RW_NdefMessage_size & 0xFF; - *pCmd_size = sizeof(RW_NDEF_T4T_Write) + 2; - eRW_NDEF_T4T_State = Write_NDEFcomplete; - } - break; + /* Is Write Success ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + memcpy(pCmd, RW_NDEF_T4T_Write, sizeof(RW_NDEF_T4T_Write)); + pCmd[4] = 2; + pCmd[5] = RW_NdefMessage_size >> 8; + pCmd[6] = RW_NdefMessage_size & 0xFF; + *pCmd_size = sizeof(RW_NDEF_T4T_Write) + 2; + eRW_NDEF_T4T_State = Write_NDEFcomplete; + } + break; case Write_NDEFcomplete: - /* Is Write Success ?*/ - if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) - { - /* Notify application of the NDEF reception */ - if (pRW_NDEF_PushCb != NULL) - pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); - } - break; + /* Is Write Success ?*/ + if (!memcmp(&pRsp[Rsp_size - 2], RW_NDEF_T4T_OK, sizeof(RW_NDEF_T4T_OK))) { + /* Notify application of the NDEF reception */ + if (pRW_NDEF_PushCb != NULL) + pRW_NDEF_PushCb(pRW_NdefMessage, RW_NdefMessage_size); + } + break; default: - break; - } + break; + } } - -//#endif -//#endif diff --git a/src/RW_NDEF_T4T.h b/src/RW_NDEF_T4T.h index 064245c..e17955b 100644 --- a/src/RW_NDEF_T4T.h +++ b/src/RW_NDEF_T4T.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ void RW_NDEF_T4T_Reset(void); void RW_NDEF_T4T_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index 461d887..12fffff 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -1,22 +1,23 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ - -//#ifdef CARDEMU_SUPPORT -//#ifndef NO_NDEF_SUPPORT -#include "tool.h" + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +// #ifdef CARDEMU_SUPPORT +// #ifndef NO_NDEF_SUPPORT #include "T4T_NDEF_emu.h" +#include "tool.h" + const unsigned char T4T_NDEF_EMU_APP_Select[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01, 0x00}; const unsigned char T4T_NDEF_EMU_CC[] = {0x00, 0x0F, 0x20, 0x00, 0xFF, 0x00, 0xFF, 0x04, 0x06, 0xE1, 0x04, 0x00, 0xFF, 0x00, 0x00}; const unsigned char T4T_NDEF_EMU_CC_Select[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x03}; @@ -31,13 +32,12 @@ unsigned short T4T_NdefMessage_size = 0; unsigned char T4T_NdefMessageWritten[256]; -typedef enum -{ - Ready, - NDEF_Application_Selected, - CC_Selected, - NDEF_Selected, - DESFire_prod +typedef enum { + Ready, + NDEF_Application_Selected, + CC_Selected, + NDEF_Selected, + DESFire_prod } T4T_NDEF_EMU_state_t; typedef void T4T_NDEF_EMU_Callback_t(unsigned char *, unsigned short); @@ -46,130 +46,99 @@ static T4T_NDEF_EMU_state_t eT4T_NDEF_EMU_State = Ready; static T4T_NDEF_EMU_Callback_t *pT4T_NDEF_EMU_PushCb = NULL; -static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, unsigned char length) -{ - if (offset == 0) - { - pRsp[0] = (T4T_NdefMessage_size & 0xFF00) >> 8; - pRsp[1] = (T4T_NdefMessage_size & 0x00FF); - if (length > 2) - memcpy(&pRsp[2], &pT4T_NdefMessage[0], length - 2); - } - else if (offset == 1) - { - pRsp[0] = (T4T_NdefMessage_size & 0x00FF); - if (length > 1) - memcpy(&pRsp[1], &pT4T_NdefMessage[0], length - 1); - } - else - { - memcpy(pRsp, &pT4T_NdefMessage[offset - 2], length); - } - - /* Did we reached the end of NDEF message ?*/ - if ((offset + length) >= (T4T_NdefMessage_size + 2)) - { - /* Notify application of the NDEF send */ - if (pT4T_NDEF_EMU_PushCb != NULL) - pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_size); - } +static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, unsigned char length) { + if (offset == 0) { + pRsp[0] = (T4T_NdefMessage_size & 0xFF00) >> 8; + pRsp[1] = (T4T_NdefMessage_size & 0x00FF); + if (length > 2) + memcpy(&pRsp[2], &pT4T_NdefMessage[0], length - 2); + } else if (offset == 1) { + pRsp[0] = (T4T_NdefMessage_size & 0x00FF); + if (length > 1) + memcpy(&pRsp[1], &pT4T_NdefMessage[0], length - 1); + } else { + memcpy(pRsp, &pT4T_NdefMessage[offset - 2], length); + } + + /* Did we reached the end of NDEF message ?*/ + if ((offset + length) >= (T4T_NdefMessage_size + 2)) { + /* Notify application of the NDEF send */ + if (pT4T_NDEF_EMU_PushCb != NULL) + pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_size); + } } -bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) -{ - pT4T_NdefMessage = pMessage; - T4T_NdefMessage_size = Message_size; - pT4T_NDEF_EMU_PushCb = (T4T_NDEF_EMU_Callback_t *)pCb; +bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) { + pT4T_NdefMessage = pMessage; + T4T_NdefMessage_size = Message_size; + pT4T_NDEF_EMU_PushCb = (T4T_NDEF_EMU_Callback_t *)pCb; - return true; + return true; } -void T4T_NDEF_EMU_Reset(void) -{ - eT4T_NDEF_EMU_State = Ready; +void T4T_NDEF_EMU_Reset(void) { + eT4T_NDEF_EMU_State = Ready; } -void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *pRsp, unsigned short *pRsp_size) -{ - bool eStatus = false; - - if (!memcmp(pCmd, T4T_NDEF_EMU_APP_Select, sizeof(T4T_NDEF_EMU_APP_Select))) - { - *pRsp_size = 0; - eStatus = true; - eT4T_NDEF_EMU_State = NDEF_Application_Selected; +void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *pRsp, unsigned short *pRsp_size) { + bool eStatus = false; + + if (!memcmp(pCmd, T4T_NDEF_EMU_APP_Select, sizeof(T4T_NDEF_EMU_APP_Select))) { + *pRsp_size = 0; + eStatus = true; + eT4T_NDEF_EMU_State = NDEF_Application_Selected; + } else if (!memcmp(pCmd, T4T_NDEF_EMU_CC_Select, sizeof(T4T_NDEF_EMU_CC_Select))) { + if (eT4T_NDEF_EMU_State == NDEF_Application_Selected) { + *pRsp_size = 0; + eStatus = true; + eT4T_NDEF_EMU_State = CC_Selected; } - else if (!memcmp(pCmd, T4T_NDEF_EMU_CC_Select, sizeof(T4T_NDEF_EMU_CC_Select))) - { - if (eT4T_NDEF_EMU_State == NDEF_Application_Selected) - { - *pRsp_size = 0; - eStatus = true; - eT4T_NDEF_EMU_State = CC_Selected; - } + } else if (!memcmp(pCmd, T4T_NDEF_EMU_NDEF_Select, sizeof(T4T_NDEF_EMU_NDEF_Select))) { + *pRsp_size = 0; + eStatus = true; + eT4T_NDEF_EMU_State = NDEF_Selected; + } else if (!memcmp(pCmd, T4T_NDEF_EMU_Read, sizeof(T4T_NDEF_EMU_Read))) { + if (eT4T_NDEF_EMU_State == CC_Selected) { + unsigned short offset = (pCmd[2] << 8) + pCmd[3]; + unsigned char length = pCmd[4]; + + if (length <= (sizeof(T4T_NDEF_EMU_CC) + offset + 2)) { + memcpy(pRsp, &T4T_NDEF_EMU_CC[offset], length); + *pRsp_size = length; + eStatus = true; + } + } else if (eT4T_NDEF_EMU_State == NDEF_Selected) { + unsigned short offset = (pCmd[2] << 8) + pCmd[3]; + unsigned char length = pCmd[4]; + + if (length <= (T4T_NdefMessage_size + offset + 2)) { + T4T_NDEF_EMU_FillRsp(pRsp, offset, length); + *pRsp_size = length; + eStatus = true; + } } - else if (!memcmp(pCmd, T4T_NDEF_EMU_NDEF_Select, sizeof(T4T_NDEF_EMU_NDEF_Select))) - { + } else if (!memcmp(pCmd, T4T_NDEF_EMU_Write, sizeof(T4T_NDEF_EMU_Write))) { + if (eT4T_NDEF_EMU_State == NDEF_Selected) { + unsigned short offset = (pCmd[2] << 8) + pCmd[3]; + unsigned char length = pCmd[4]; + if (offset + length <= sizeof(T4T_NdefMessageWritten)) { + memcpy(&T4T_NdefMessageWritten[offset - 2], &pCmd[5], length); + pT4T_NdefMessage = T4T_NdefMessageWritten; + T4T_NdefMessage_size = (pCmd[5] << 8) + pCmd[6]; *pRsp_size = 0; eStatus = true; - eT4T_NDEF_EMU_State = NDEF_Selected; - } - else if (!memcmp(pCmd, T4T_NDEF_EMU_Read, sizeof(T4T_NDEF_EMU_Read))) - { - if (eT4T_NDEF_EMU_State == CC_Selected) - { - unsigned short offset = (pCmd[2] << 8) + pCmd[3]; - unsigned char length = pCmd[4]; - - if (length <= (sizeof(T4T_NDEF_EMU_CC) + offset + 2)) - { - memcpy(pRsp, &T4T_NDEF_EMU_CC[offset], length); - *pRsp_size = length; - eStatus = true; - } - } - else if (eT4T_NDEF_EMU_State == NDEF_Selected) - { - unsigned short offset = (pCmd[2] << 8) + pCmd[3]; - unsigned char length = pCmd[4]; - - if (length <= (T4T_NdefMessage_size + offset + 2)) - { - T4T_NDEF_EMU_FillRsp(pRsp, offset, length); - *pRsp_size = length; - eStatus = true; - } - } - } - else if (!memcmp(pCmd, T4T_NDEF_EMU_Write, sizeof(T4T_NDEF_EMU_Write))) - { - if (eT4T_NDEF_EMU_State == NDEF_Selected) - { - - unsigned short offset = (pCmd[2] << 8) + pCmd[3]; - unsigned char length = pCmd[4]; - if (offset + length <= sizeof(T4T_NdefMessageWritten)) - { - memcpy(&T4T_NdefMessageWritten[offset - 2], &pCmd[5], length); - pT4T_NdefMessage = T4T_NdefMessageWritten; - T4T_NdefMessage_size = (pCmd[5] << 8) + pCmd[6]; - *pRsp_size = 0; - eStatus = true; - } - } - } - - if (eStatus == true) - { - memcpy(&pRsp[*pRsp_size], T4T_NDEF_EMU_OK, sizeof(T4T_NDEF_EMU_OK)); - *pRsp_size += sizeof(T4T_NDEF_EMU_OK); - } - else - { - memcpy(pRsp, T4T_NDEF_EMU_NOK, sizeof(T4T_NDEF_EMU_NOK)); - *pRsp_size = sizeof(T4T_NDEF_EMU_NOK); - T4T_NDEF_EMU_Reset(); + } } + } + + if (eStatus == true) { + memcpy(&pRsp[*pRsp_size], T4T_NDEF_EMU_OK, sizeof(T4T_NDEF_EMU_OK)); + *pRsp_size += sizeof(T4T_NDEF_EMU_OK); + } else { + memcpy(pRsp, T4T_NDEF_EMU_NOK, sizeof(T4T_NDEF_EMU_NOK)); + *pRsp_size = sizeof(T4T_NDEF_EMU_NOK); + T4T_NDEF_EMU_Reset(); + } } -//#endif -//#endif +// #endif +// #endif diff --git a/src/ndef_helper.cpp b/src/ndef_helper.cpp index d734462..bf5d445 100644 --- a/src/ndef_helper.cpp +++ b/src/ndef_helper.cpp @@ -1,226 +1,178 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ + +#include "ndef_helper.h" -#include #include +#include #include -#include "ndef_helper.h" -const char *ndef_helper_WifiAuth(unsigned char auth) -{ - switch (auth) - { +const char *ndef_helper_WifiAuth(unsigned char auth) { + switch (auth) { case 0x01: - return "Open"; + return "Open"; case 0x02: - return "WPA-Personal"; + return "WPA-Personal"; case 0x04: - return "Shared"; + return "Shared"; case 0x08: - return "WPA-Enterprise"; + return "WPA-Enterprise"; case 0x10: - return "WPA2-Enterprise"; + return "WPA2-Enterprise"; case 0x20: - return "WPA2-Personal"; + return "WPA2-Personal"; default: - return "unknown"; - } + return "unknown"; + } } -const char *ndef_helper_WifiEnc(unsigned char enc) -{ - switch (enc) - { +const char *ndef_helper_WifiEnc(unsigned char enc) { + switch (enc) { case 0x01: - return "None"; + return "None"; case 0x02: - return "WEP"; + return "WEP"; case 0x04: - return "TKIP"; + return "TKIP"; case 0x08: - return "AES"; + return "AES"; case 0x10: - return "AES/TKIP"; + return "AES/TKIP"; default: - return "unknown"; - } + return "unknown"; + } } -const char *ndef_helper_UriHead(unsigned char head) -{ - switch (head) - { +const char *ndef_helper_UriHead(unsigned char head) { + switch (head) { case 0: - return (""); + return (""); case 1: - return ("http://www."); + return ("http://www."); case 2: - return ("https://www."); + return ("https://www."); case 3: - return ("http://"); + return ("http://"); case 4: - return ("https://"); + return ("https://"); case 5: - return ("tel:"); + return ("tel:"); case 6: - return ("mailto:"); + return ("mailto:"); default: - return ("!!!unknown!!!"); - } + return ("!!!unknown!!!"); + } } -NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord) -{ - NdefRecord_t record; +NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord) { + NdefRecord_t record; - uint8_t typeField; + uint8_t typeField; - /* Short or normal record ?*/ - if (pNdefRecord[0] & NDEF_RECORD_SR_MASK) - { - record.recordPayloadSize = pNdefRecord[2]; - typeField = 3; - } - else - { - record.recordPayloadSize = (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5]; - typeField = 6; - } + /* Short or normal record ?*/ + if (pNdefRecord[0] & NDEF_RECORD_SR_MASK) { + record.recordPayloadSize = pNdefRecord[2]; + typeField = 3; + } else { + record.recordPayloadSize = (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5]; + typeField = 6; + } - /* ID present ?*/ - if (pNdefRecord[0] & NDEF_RECORD_IL_MASK) - { - record.recordPayload = pNdefRecord + typeField + pNdefRecord[1] + 1 + pNdefRecord[typeField]; - typeField++; - } - else - { - record.recordPayload = pNdefRecord + typeField + pNdefRecord[1]; - } + /* ID present ?*/ + if (pNdefRecord[0] & NDEF_RECORD_IL_MASK) { + record.recordPayload = pNdefRecord + typeField + pNdefRecord[1] + 1 + pNdefRecord[typeField]; + typeField++; + } else { + record.recordPayload = pNdefRecord + typeField + pNdefRecord[1]; + } - /* Well known Record Type ?*/ - if ((pNdefRecord[0] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) - { - if (pNdefRecord[1] == 0x1) - { - switch (pNdefRecord[typeField]) - { - case 'T': - record.recordType = WELL_KNOWN_SIMPLE_TEXT; - break; - case 'U': - record.recordType = WELL_KNOWN_SIMPLE_URI; - break; - default: - record.recordType = UNSUPPORTED_NDEF_RECORD; - break; - } - } - else if (pNdefRecord[1] == 0x2) - { - if (memcmp(&pNdefRecord[typeField], "Sp", pNdefRecord[1]) == 0x0) - { - record.recordType = WELL_KNOWN_SMART_POSTER; - } - else if (memcmp(&pNdefRecord[typeField], "Hs", pNdefRecord[1]) == 0x0) - { - record.recordType = WELL_KNOWN_HANDOVER_SELECT; - } - else if (memcmp(&pNdefRecord[typeField], "Hr", pNdefRecord[1]) == 0x0) - { - record.recordType = WELL_KNOWN_HANDOVER_REQUEST; - } - else if (memcmp(&pNdefRecord[typeField], "ac", pNdefRecord[1]) == 0x0) - { - record.recordType = WELL_KNOWN_ALTERNATIVE_CARRIER; - } - else if (memcmp(&pNdefRecord[typeField], "cr", pNdefRecord[1]) == 0x0) - { - record.recordType = WELL_KNOWN_COLLISION_RESOLUTION; - } - else - { - record.recordType = UNSUPPORTED_NDEF_RECORD; - } - } - } - /* Media Record Type ?*/ - else if ((pNdefRecord[0] & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) - { - if ((memcmp(&pNdefRecord[typeField], "text/x-vCard", pNdefRecord[1]) == 0x0) || - (memcmp(&pNdefRecord[typeField], "text/vcard", pNdefRecord[1]) == 0x0)) - { - record.recordType = MEDIA_VCARD; - } - else if (memcmp(&pNdefRecord[typeField], "application/vnd.wfa.wsc", pNdefRecord[1]) == 0x0) - { - record.recordType = MEDIA_HANDOVER_WIFI; - } - else if (memcmp(&pNdefRecord[typeField], "application/vnd.bluetooth.ep.oob", pNdefRecord[1]) == 0x0) - { - record.recordType = MEDIA_HANDOVER_BT; - } - else if (memcmp(&pNdefRecord[typeField], "application/vnd.bluetooth.le.oob", pNdefRecord[1]) == 0x0) - { - record.recordType = MEDIA_HANDOVER_BLE; - } - else if (memcmp(&pNdefRecord[typeField], "application/vnd.bluetooth.secure.le.oob", pNdefRecord[1]) == 0x0) - { - record.recordType = MEDIA_HANDOVER_BLE_SECURE; - } - else - { - record.recordType = UNSUPPORTED_NDEF_RECORD; - } - } - /* Absolute URI Record Type ?*/ - else if ((pNdefRecord[0] & NDEF_RECORD_TNF_MASK) == NDEF_ABSOLUTE_URI) - { - record.recordType = ABSOLUTE_URI; - } - else - { + /* Well known Record Type ?*/ + if ((pNdefRecord[0] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + if (pNdefRecord[1] == 0x1) { + switch (pNdefRecord[typeField]) { + case 'T': + record.recordType = WELL_KNOWN_SIMPLE_TEXT; + break; + case 'U': + record.recordType = WELL_KNOWN_SIMPLE_URI; + break; + default: + record.recordType = UNSUPPORTED_NDEF_RECORD; + break; + } + } else if (pNdefRecord[1] == 0x2) { + if (memcmp(&pNdefRecord[typeField], "Sp", pNdefRecord[1]) == 0x0) { + record.recordType = WELL_KNOWN_SMART_POSTER; + } else if (memcmp(&pNdefRecord[typeField], "Hs", pNdefRecord[1]) == 0x0) { + record.recordType = WELL_KNOWN_HANDOVER_SELECT; + } else if (memcmp(&pNdefRecord[typeField], "Hr", pNdefRecord[1]) == 0x0) { + record.recordType = WELL_KNOWN_HANDOVER_REQUEST; + } else if (memcmp(&pNdefRecord[typeField], "ac", pNdefRecord[1]) == 0x0) { + record.recordType = WELL_KNOWN_ALTERNATIVE_CARRIER; + } else if (memcmp(&pNdefRecord[typeField], "cr", pNdefRecord[1]) == 0x0) { + record.recordType = WELL_KNOWN_COLLISION_RESOLUTION; + } else { record.recordType = UNSUPPORTED_NDEF_RECORD; + } } + } + /* Media Record Type ?*/ + else if ((pNdefRecord[0] & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { + if ((memcmp(&pNdefRecord[typeField], "text/x-vCard", pNdefRecord[1]) == 0x0) || + (memcmp(&pNdefRecord[typeField], "text/vcard", pNdefRecord[1]) == 0x0)) { + record.recordType = MEDIA_VCARD; + } else if (memcmp(&pNdefRecord[typeField], "application/vnd.wfa.wsc", pNdefRecord[1]) == 0x0) { + record.recordType = MEDIA_HANDOVER_WIFI; + } else if (memcmp(&pNdefRecord[typeField], "application/vnd.bluetooth.ep.oob", pNdefRecord[1]) == 0x0) { + record.recordType = MEDIA_HANDOVER_BT; + } else if (memcmp(&pNdefRecord[typeField], "application/vnd.bluetooth.le.oob", pNdefRecord[1]) == 0x0) { + record.recordType = MEDIA_HANDOVER_BLE; + } else if (memcmp(&pNdefRecord[typeField], "application/vnd.bluetooth.secure.le.oob", pNdefRecord[1]) == 0x0) { + record.recordType = MEDIA_HANDOVER_BLE_SECURE; + } else { + record.recordType = UNSUPPORTED_NDEF_RECORD; + } + } + /* Absolute URI Record Type ?*/ + else if ((pNdefRecord[0] & NDEF_RECORD_TNF_MASK) == NDEF_ABSOLUTE_URI) { + record.recordType = ABSOLUTE_URI; + } else { + record.recordType = UNSUPPORTED_NDEF_RECORD; + } - return record; + return record; } -unsigned char *GetNextRecord(unsigned char *pNdefRecord) -{ - unsigned char *temp = NULL; +unsigned char *GetNextRecord(unsigned char *pNdefRecord) { + unsigned char *temp = NULL; - /* Message End ?*/ - if (!(pNdefRecord[0] & NDEF_RECORD_ME_MASK)) - { - /* Short or normal record ?*/ - if (pNdefRecord[0] & NDEF_RECORD_SR_MASK) - { - /* ID present ?*/ - if (pNdefRecord[0] & NDEF_RECORD_IL_MASK) - temp = (pNdefRecord + 4 + pNdefRecord[1] + pNdefRecord[2] + pNdefRecord[3]); - else - temp = (pNdefRecord + 3 + pNdefRecord[1] + pNdefRecord[2]); - } - else - { - /* ID present ?*/ - if (pNdefRecord[0] & NDEF_RECORD_IL_MASK) - temp = (pNdefRecord + 7 + pNdefRecord[1] + (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5] + pNdefRecord[6]); - else - temp = (pNdefRecord + 6 + pNdefRecord[1] + (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5]); - } + /* Message End ?*/ + if (!(pNdefRecord[0] & NDEF_RECORD_ME_MASK)) { + /* Short or normal record ?*/ + if (pNdefRecord[0] & NDEF_RECORD_SR_MASK) { + /* ID present ?*/ + if (pNdefRecord[0] & NDEF_RECORD_IL_MASK) + temp = (pNdefRecord + 4 + pNdefRecord[1] + pNdefRecord[2] + pNdefRecord[3]); + else + temp = (pNdefRecord + 3 + pNdefRecord[1] + pNdefRecord[2]); + } else { + /* ID present ?*/ + if (pNdefRecord[0] & NDEF_RECORD_IL_MASK) + temp = (pNdefRecord + 7 + pNdefRecord[1] + (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5] + pNdefRecord[6]); + else + temp = (pNdefRecord + 6 + pNdefRecord[1] + (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5]); } - return temp; + } + return temp; } diff --git a/src/ndef_helper.h b/src/ndef_helper.h index dc4c316..47f3b05 100644 --- a/src/ndef_helper.h +++ b/src/ndef_helper.h @@ -1,16 +1,16 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ #define NDEF_EMPTY 0x00 #define NDEF_WELL_KNOWN 0x01 @@ -28,29 +28,28 @@ #define NDEF_RECORD_IL_MASK 0x08 #define NDEF_RECORD_TNF_MASK 0x07 -typedef enum -{ - WELL_KNOWN_SIMPLE_TEXT, - WELL_KNOWN_SIMPLE_URI, - WELL_KNOWN_SMART_POSTER, - WELL_KNOWN_HANDOVER_SELECT, - WELL_KNOWN_HANDOVER_REQUEST, - WELL_KNOWN_ALTERNATIVE_CARRIER, - WELL_KNOWN_COLLISION_RESOLUTION, - MEDIA_VCARD, - MEDIA_HANDOVER_WIFI, - MEDIA_HANDOVER_BT, - MEDIA_HANDOVER_BLE, - MEDIA_HANDOVER_BLE_SECURE, - ABSOLUTE_URI, - UNSUPPORTED_NDEF_RECORD = 0xFF +typedef enum { + WELL_KNOWN_SIMPLE_TEXT, + WELL_KNOWN_SIMPLE_URI, + WELL_KNOWN_SMART_POSTER, + WELL_KNOWN_HANDOVER_SELECT, + WELL_KNOWN_HANDOVER_REQUEST, + WELL_KNOWN_ALTERNATIVE_CARRIER, + WELL_KNOWN_COLLISION_RESOLUTION, + MEDIA_VCARD, + MEDIA_HANDOVER_WIFI, + MEDIA_HANDOVER_BT, + MEDIA_HANDOVER_BLE, + MEDIA_HANDOVER_BLE_SECURE, + ABSOLUTE_URI, + UNSUPPORTED_NDEF_RECORD = 0xFF } NdefRecordType_e; typedef struct { - NdefRecordType_e recordType; - unsigned char *recordPayload; - unsigned int recordPayloadSize; + NdefRecordType_e recordType; + unsigned char *recordPayload; + unsigned int recordPayloadSize; } NdefRecord_t; const char *ndef_helper_WifiAuth(unsigned char auth); diff --git a/src/tool.cpp b/src/tool.cpp index ed4419d..619a572 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -1,40 +1,36 @@ /* -* Copyright (c), NXP Semiconductors Caen / France -* -* (C)NXP Semiconductors -* All rights are reserved. Reproduction in whole or in part is -* prohibited without the written consent of the copyright owner. -* NXP reserves the right to make changes without notice at any time. -* NXP makes no warranty, expressed, implied or statutory, including but -* not limited to any implied warranty of merchantability or fitness for any -*particular purpose, or that the use will not infringe any third party patent, -* copyright or trademark. NXP must not be liable for any loss or damage -* arising from its use. -*/ + * Copyright (c), NXP Semiconductors Caen / France + * + * (C)NXP Semiconductors + * All rights are reserved. Reproduction in whole or in part is + * prohibited without the written consent of the copyright owner. + * NXP reserves the right to make changes without notice at any time. + * NXP makes no warranty, expressed, implied or statutory, including but + * not limited to any implied warranty of merchantability or fitness for any + *particular purpose, or that the use will not infringe any third party patent, + * copyright or trademark. NXP must not be liable for any loss or damage + * arising from its use. + */ -//#include +// #include /* LOOP_REF and CLOCK_CALL_TIME must be adapted according to the CPU execution time */ #define LOOP_REF 6800 #define CLOCK_CALL_TIME 40 -void Sleep(unsigned int ms) -{ - int i; +void Sleep(unsigned int ms) { + int i; #ifndef DEBUG_SEMIHOSTING - for (i = 0; i < (ms * LOOP_REF); i++) - asm("nop"); + for (i = 0; i < (ms * LOOP_REF); i++) + asm("nop"); #else - if (ms <= CLOCK_CALL_TIME) - { - for (i = 0; i < (ms * LOOP_REF); i++) - asm("nop"); - } - else - { - clock_t time = clock() + ((ms - CLOCK_CALL_TIME) / 10); - while (clock() <= time) - ; - } + if (ms <= CLOCK_CALL_TIME) { + for (i = 0; i < (ms * LOOP_REF); i++) + asm("nop"); + } else { + clock_t time = clock() + ((ms - CLOCK_CALL_TIME) / 10); + while (clock() <= time) + ; + } #endif } From e725f4cf416e99d6155f7c366aed54c73a99008a Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 12:54:12 -0600 Subject: [PATCH 067/106] feat: add is reader detected --- examples/NDEFSend/NDEFSend.ino | 11 ++++++++++- src/Electroniccats_PN7150.cpp | 15 +++++++++++++++ src/Electroniccats_PN7150.h | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 73dc800..d8a2937 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -78,7 +78,8 @@ void setup() { } void loop() { - checkReaders(); + // checkReaders(); + checkReadersv2(); } void checkReaders() { @@ -97,6 +98,14 @@ void checkReaders() { Serial.println("Elapsed time: " + String(millis() - startTime) + "ms"); } +void checkReadersv2() { + Serial.print("."); + + if (nfc.isReaderDetected()) { + Serial.println("\nReader detected!"); + } +} + void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { Serial.println("NDEF Record sent!"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a79b9ee..58a06a9 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1720,3 +1720,18 @@ bool Electroniccats_PN7150::setP2PMode() { void Electroniccats_PN7150::setSendMsgCallback(CustomCallback_t function) { registerNdefReceivedCallback(function); } + +bool Electroniccats_PN7150::isReaderDetected() { + unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; + + if (cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? + if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes + if (Cmd[1] == 0xA4) { + return true; + } + cardModeSend(STATUSOK, sizeof(STATUSOK)); + } + } + + return false; +} diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 23ccdbc..2ce4c3b 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -180,6 +180,7 @@ class Electroniccats_PN7150 : public Mode { bool setEmulationMode(); bool setP2PMode(); void setSendMsgCallback(CustomCallback_t function); + bool isReaderDetected(); }; #endif From 98cebc25a8c26c48bbbbaffc314add5dcae7ef0f Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 13:29:43 -0600 Subject: [PATCH 068/106] feat: add send message and close communication --- examples/NDEFSend/NDEFSend.ino | 57 ++++++++++++---------------------- src/Electroniccats_PN7150.cpp | 28 ++++++++++++----- src/Electroniccats_PN7150.h | 5 ++- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index d8a2937..77fce7a 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -15,21 +15,21 @@ unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; const char uri[] = "google.com"; -// const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF -// 0x01, // Type length (1 byte) -// 0x08, // Payload length -// 'U', // Type -> 'T' for text, 'U' for URI -// 0x02, // Status -// 'e', 'n', // Language -// 'H', 'e', 'l', 'l', 'o'}; // Message Payload - -const char ndefMessage[] = {0xD1, - 0x01, - sizeof(uri) + 1, - 'U', - 0x02, - // 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm'}; - uri[0], uri[1], uri[2], uri[3], uri[4], uri[5], uri[6], uri[7], uri[8], uri[9], uri[10]}; +const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF + 0x01, // Type length (1 byte) + 0x08, // Payload length + 'T', // Type -> 'T' for text, 'U' for URI + 0x02, // Status + 'e', 'n', // Language + 'H', 'e', 'l', 'l', 'o'}; // Message Payload + +// const char ndefMessage[] = {0xD1, +// 0x01, +// sizeof(uri) + 1, +// 'U', +// 0x02, +// 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm'}; +// uri[0], uri[1], uri[2], uri[3], uri[4], uri[5], uri[6], uri[7], uri[8], uri[9], uri[10]}; void setup() { Serial.begin(9600); @@ -74,36 +74,19 @@ void setup() { nfc.startDiscovery(); - Serial.println("Waiting for an NDEF device"); + Serial.println("Waiting for an NDEF device..."); } void loop() { - // checkReaders(); - checkReadersv2(); -} - -void checkReaders() { - Serial.print(""); unsigned long startTime = millis(); - if (nfc.cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? - if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes - if (Cmd[1] == 0xA4) { - Serial.println("\nReader detected!"); - nfc.processCardMode(RfInterface); - } - nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); - Serial.print("Waiting for an NDEF device"); - } - } - Serial.println("Elapsed time: " + String(millis() - startTime) + "ms"); -} - -void checkReadersv2() { - Serial.print("."); if (nfc.isReaderDetected()) { Serial.println("\nReader detected!"); + nfc.sendMessage(); + nfc.closeCommunication(); } + + Serial.println("Elapsed time: " + String(millis() - startTime) + "ms"); } void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 58a06a9..47b53b8 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1209,7 +1209,11 @@ void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { } } -// Deprecated, use processCardMode() instead +void Electroniccats_PN7150::processCardMode(void) { + Electroniccats_PN7150::processCardMode(this->dummyRfInterface); +} + +// Deprecated, use processCardMode(void) instead void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { Electroniccats_PN7150::processCardMode(RfIntf); } @@ -1722,16 +1726,26 @@ void Electroniccats_PN7150::setSendMsgCallback(CustomCallback_t function) { } bool Electroniccats_PN7150::isReaderDetected() { - unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; + static unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; + bool status = false; - if (cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? - if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes + if (cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? + if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { - return true; + status = true; } - cardModeSend(STATUSOK, sizeof(STATUSOK)); + Electroniccats_PN7150::closeCommunication(); } } - return false; + return status; +} + +void Electroniccats_PN7150::closeCommunication() { + unsigned char STATUSOK[] = {0x90, 0x00}; + Electroniccats_PN7150::cardModeSend(STATUSOK, sizeof(STATUSOK)); +} + +void Electroniccats_PN7150::sendMessage() { + Electroniccats_PN7150::processCardMode(); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 2ce4c3b..cebe43d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -150,7 +150,8 @@ class Electroniccats_PN7150 : public Mode { bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead void processCardMode(RfIntf_t RfIntf); - void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode() instead + void processCardMode(void); + void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode(void) instead void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead void processP2pMode(RfIntf_t RfIntf); @@ -181,6 +182,8 @@ class Electroniccats_PN7150 : public Mode { bool setP2PMode(); void setSendMsgCallback(CustomCallback_t function); bool isReaderDetected(); + void closeCommunication(); + void sendMessage(); }; #endif From 2febce480da50ab453b9264ba81dfb9b2ed246f8 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 13:35:07 -0600 Subject: [PATCH 069/106] refactor: move close communication to send message --- examples/NDEFSend/NDEFSend.ino | 8 +++----- src/Electroniccats_PN7150.cpp | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 77fce7a..77343b0 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -74,19 +74,17 @@ void setup() { nfc.startDiscovery(); - Serial.println("Waiting for an NDEF device..."); + Serial.print("Waiting for an NDEF device"); } void loop() { - unsigned long startTime = millis(); + Serial.print("."); if (nfc.isReaderDetected()) { Serial.println("\nReader detected!"); nfc.sendMessage(); - nfc.closeCommunication(); + Serial.print("\nWaiting for an NDEF device"); } - - Serial.println("Elapsed time: " + String(millis() - startTime) + "ms"); } void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 47b53b8..a790b6e 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1748,4 +1748,5 @@ void Electroniccats_PN7150::closeCommunication() { void Electroniccats_PN7150::sendMessage() { Electroniccats_PN7150::processCardMode(); + Electroniccats_PN7150::closeCommunication(); } From 27317c06381a96a69b959db1752368cff1790c4f Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 16:44:24 -0600 Subject: [PATCH 070/106] feat: begin function calls all needed configurations --- examples/NDEFRead/Makefile | 2 +- examples/NDEFRead/NDEFRead.ino | 21 ++++------------- examples/NDEFSend/NDEFSend.ino | 20 ----------------- src/Electroniccats_PN7150.cpp | 41 +++++++++++++++++++++++++++++++++- src/Electroniccats_PN7150.h | 1 + 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/examples/NDEFRead/Makefile b/examples/NDEFRead/Makefile index 5e80373..4ab2c77 100644 --- a/examples/NDEFRead/Makefile +++ b/examples/NDEFRead/Makefile @@ -16,4 +16,4 @@ clean: wait: sleep 2 -all: compile upload \ No newline at end of file +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index df4a58e..b0a3ce6 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -22,26 +22,15 @@ void setup() { nfc.setSendMsgCallback(customNdefCallback); Serial.println("Initializing..."); - if (nfc.connectNCI()) { // Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (true) - ; - } - - if (nfc.configureSettings()) { - Serial.println("The Configure Settings is failed!"); - while (true) + + if (nfc.begin()) { + Serial.println("Error initializing PN7150"); + while (1) ; } nfc.setReaderWriterMode(); - if (nfc.configMode()) { // Set up the configuration mode - Serial.println("The Configure Mode is failed!!"); - while (true) - ; - } - nfc.startDiscovery(); // NCI Discovery mode Serial.println("Waiting for a Card..."); } @@ -158,10 +147,8 @@ String getHexRepresentation(const byte *data, const uint32_t numBytes) { } for (uint32_t szPos = 0; szPos < numBytes; szPos++) { - // hexString += "0x"; if (data[szPos] <= 0xF) hexString += "0"; - // hexString += String(data[szPos] & 0xFF, HEX); String hexValue = String(data[szPos] & 0xFF, HEX); hexValue.toUpperCase(); // Convierte a mayúsculas hexString += hexValue; diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 77343b0..5960c66 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -51,29 +51,9 @@ void setup() { ; } - if (nfc.connectNCI()) { // Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (1) - ; - } - - if (nfc.configureSettings()) { - Serial.println("The Configure Settings failed!"); - while (1) - ; - } - // Needed to detect readers nfc.setEmulationMode(); - if (nfc.configMode()) { - Serial.println("The Configure Mode failed!"); - while (1) - ; - } - - nfc.startDiscovery(); - Serial.print("Waiting for an NDEF device"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a790b6e..a2f2827 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -50,6 +50,8 @@ Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, pinMode(_IRQpin, INPUT); if (_VENpin != 255) pinMode(_VENpin, OUTPUT); + + this->_hasBeenInitialized = false; } uint8_t Electroniccats_PN7150::begin() { @@ -62,6 +64,25 @@ uint8_t Electroniccats_PN7150::begin() { digitalWrite(_VENpin, HIGH); delay(3); } + + if (connectNCI()) { + return ERROR; + } + + if (configureSettings()) { + return ERROR; + } + + if (configMode()) { + return ERROR; + } + + if (startDiscovery()) { + return ERROR; + } + + this->_hasBeenInitialized = true; + return SUCCESS; } @@ -1048,8 +1069,22 @@ uint8_t Electroniccats_PN7150::connectNCI() { uint8_t i = 2; uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + // Check if begin function has been called + if (this->_hasBeenInitialized) { + return SUCCESS; + } + // Open connection to NXPNCI - begin(); + _wire->begin(); + if (_VENpin != 255) { + digitalWrite(_VENpin, HIGH); + delay(1); + digitalWrite(_VENpin, LOW); + delay(1); + digitalWrite(_VENpin, HIGH); + delay(3); + } + // Loop until NXPNCI answers while (wakeupNCI() != SUCCESS) { if (i-- == 0) @@ -1686,6 +1721,10 @@ bool Electroniccats_PN7150::reset() { return false; } + if (configureSettings()) { + return false; + } + if (Electroniccats_PN7150::configMode()) { return false; } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index cebe43d..8b25ba9 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -102,6 +102,7 @@ typedef enum { class Electroniccats_PN7150 : public Mode { private: + bool _hasBeenInitialized; uint8_t _IRQpin, _VENpin, _I2Caddress; TwoWire *_wire; RfIntf_t dummyRfInterface; From 79b94eb9507692e235ee9f8d319c1382cb674976 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 18 Aug 2023 17:13:55 -0600 Subject: [PATCH 071/106] feat: add is tag detected --- examples/NDEFRead/NDEFRead.ino | 6 +++--- src/Electroniccats_PN7150.cpp | 10 +++++++--- src/Electroniccats_PN7150.h | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index b0a3ce6..854b3f5 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -35,7 +35,7 @@ void setup() { } void loop() { - if (!nfc.waitForDiscoveryNotification()) { // Wait for a card + if (nfc.isTagDetected()) { // Wait for a card displayDeviceInfo(); switch (nfc.remoteDevice.getProtocol()) { // Read NDEF message from NFC Forum Type 1, 2, 3, 4, 5 tags @@ -59,10 +59,10 @@ void loop() { Serial.println("Remove the Card"); nfc.waitForTagRemoval(); Serial.println("Card removed!"); + Serial.println("Restarting..."); + nfc.reset(); // TODO: type 4 tags cause discovery failed after reset } - Serial.println("Restarting..."); - nfc.reset(); Serial.println("Waiting for a Card..."); delay(500); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a2f2827..69f4862 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -430,7 +430,7 @@ bool Electroniccats_PN7150::configureSettings(void) { getMessage(); if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x02) || (rxBuffer[3] != 0x00) || (rxBuffer[4] != 0x00)) { #ifdef SerialUSB - Serial.println("NxpNci_CORE_CONF"); + Serial.println("NxpNci_CORE_CONF :D"); #endif return ERROR; } @@ -1060,7 +1060,11 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(uint8_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfInterface, tout); } -// Deprecated, use waitForDiscoveryNotification() instead +bool Electroniccats_PN7150::isTagDetected() { + return !Electroniccats_PN7150::waitForDiscoveryNotification(500); +} + +// Deprecated, use isTagDetected() instead bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); } @@ -1721,7 +1725,7 @@ bool Electroniccats_PN7150::reset() { return false; } - if (configureSettings()) { + if (Electroniccats_PN7150::configureSettings()) { return false; } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 8b25ba9..38dc50f 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -143,7 +143,8 @@ class Electroniccats_PN7150 : public Mode { bool StopDiscovery(); // Deprecated, use stopDiscovery() instead bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); bool waitForDiscoveryNotification(uint8_t tout = 0); - bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); // Deprecated, use waitForDiscoveryNotification() instead + bool isTagDetected(); + bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); // Deprecated, use isTagDetected() instead uint8_t connectNCI(); uint8_t wakeupNCI(); bool cardModeSend(unsigned char *pData, unsigned char DataSize); From b333ef12681915ec77f96267b00698305604bda9 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 21 Aug 2023 10:19:06 -0600 Subject: [PATCH 072/106] fix: type 4 tags cause discovery failed after reset --- examples/NDEFRead/NDEFRead.ino | 13 ++++++++----- src/Electroniccats_PN7150.cpp | 7 +++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 854b3f5..c752b5b 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -22,7 +22,7 @@ void setup() { nfc.setSendMsgCallback(customNdefCallback); Serial.println("Initializing..."); - + if (nfc.begin()) { Serial.println("Error initializing PN7150"); while (1) @@ -31,7 +31,9 @@ void setup() { nfc.setReaderWriterMode(); - Serial.println("Waiting for a Card..."); + Serial.println(nfc.remoteDevice.getProtocol()); + + Serial.print("Waiting for a Card..."); } void loop() { @@ -60,10 +62,11 @@ void loop() { nfc.waitForTagRemoval(); Serial.println("Card removed!"); Serial.println("Restarting..."); - nfc.reset(); // TODO: type 4 tags cause discovery failed after reset + nfc.reset(); + Serial.print("Waiting for a Card"); } - Serial.println("Waiting for a Card..."); + Serial.print("."); delay(500); } @@ -150,7 +153,7 @@ String getHexRepresentation(const byte *data, const uint32_t numBytes) { if (data[szPos] <= 0xF) hexString += "0"; String hexValue = String(data[szPos] & 0xFF, HEX); - hexValue.toUpperCase(); // Convierte a mayúsculas + hexValue.toUpperCase(); // Convierte a mayúsculas hexString += hexValue; if ((numBytes > 1) && (szPos != numBytes - 1)) { hexString += ":"; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 69f4862..03f2235 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1725,8 +1725,11 @@ bool Electroniccats_PN7150::reset() { return false; } - if (Electroniccats_PN7150::configureSettings()) { - return false; + // Configure settings only if we have not detected a tag yet + if (remoteDevice.getProtocol() == protocol.UNDETERMINED) { + if (Electroniccats_PN7150::configureSettings()) { + return false; + } } if (Electroniccats_PN7150::configMode()) { From 4bd11b878941c47e974341d46a0a323e72a28d7c Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 22 Aug 2023 13:01:00 -0600 Subject: [PATCH 073/106] feat: add update ndef message callback --- examples/NDEFRead/NDEFRead.ino | 6 +- src/Electroniccats_PN7150.cpp | 135 +++++++++++++++++++++++++++++++++ src/Electroniccats_PN7150.h | 1 + src/RW_NDEF.cpp | 5 ++ src/RW_NDEF.h | 2 + src/RW_NDEF_MIFARE.cpp | 21 ++++- 6 files changed, 163 insertions(+), 7 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index c752b5b..0ebac6f 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -18,7 +18,7 @@ void setup() { Serial.println("Detect NFC tags with PN7150"); // Register a callback function to be called when an NDEF message is received - RW_NDEF_RegisterPullCallback((void *)*ndefCallback); + // RW_NDEF_RegisterPullCallback((void *)*ndefCallback); nfc.setSendMsgCallback(customNdefCallback); Serial.println("Initializing..."); @@ -31,8 +31,6 @@ void setup() { nfc.setReaderWriterMode(); - Serial.println(nfc.remoteDevice.getProtocol()); - Serial.print("Waiting for a Card..."); } @@ -153,7 +151,7 @@ String getHexRepresentation(const byte *data, const uint32_t numBytes) { if (data[szPos] <= 0xF) hexString += "0"; String hexValue = String(data[szPos] & 0xFF, HEX); - hexValue.toUpperCase(); // Convierte a mayúsculas + hexValue.toUpperCase(); hexString += hexValue; if ((numBytes > 1) && (szPos != numBytes - 1)) { hexString += ":"; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 03f2235..c12c904 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -55,6 +55,9 @@ Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, } uint8_t Electroniccats_PN7150::begin() { + // Register callbacks + registerUpdateNdefMessageCallback(Electroniccats_PN7150::updateNdefMessage); + _wire->begin(); if (_VENpin != 255) { digitalWrite(_VENpin, HIGH); @@ -86,6 +89,138 @@ uint8_t Electroniccats_PN7150::begin() { return SUCCESS; } +void Electroniccats_PN7150::updateNdefMessage(unsigned char *message, unsigned short messageSize) { + Serial.println("here :D"); + unsigned char *pNdefRecord = message; + NdefRecord_t NdefRecord; + unsigned char save; + String SSID; + String bluetoothName; + String bluetoothAddress; + + if (message == NULL) { + Serial.println("--- Provisioned buffer size too small or NDEF message empty"); + return; + } + + while (pNdefRecord != NULL) { + Serial.println("--- NDEF record received:"); + + NdefRecord = DetectNdefRecordType(pNdefRecord); + + switch (NdefRecord.recordType) { + case MEDIA_VCARD: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("vCard:"); + Serial.println(reinterpret_cast(NdefRecord.recordPayload)); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_TEXT: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("Text record: "); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_URI: { + save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + Serial.print("URI record: "); + Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); + NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + } break; + + case MEDIA_HANDOVER_WIFI: { + unsigned char index = 0, i; + + Serial.println("--- Received WIFI credentials:"); + if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) + index += 4; + while (index < NdefRecord.recordPayloadSize) { + if (NdefRecord.recordPayload[index] == 0x10) { + if (NdefRecord.recordPayload[index + 1] == 0x45) { + Serial.print("- SSID = "); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); + Serial.println(SSID); + } else if (NdefRecord.recordPayload[index + 1] == 0x03) { + Serial.print("- Authenticate Type = "); + Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); + } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { + Serial.print("- Encryption Type = "); + Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); + } else if (NdefRecord.recordPayload[index + 1] == 0x27) { + Serial.print("- Network key = "); + Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); + Serial.print("- Network key = "); + // Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); + } + index += 4 + NdefRecord.recordPayload[index + 3]; + } else + continue; + } + } break; + + case WELL_KNOWN_HANDOVER_SELECT: + Serial.print("Handover select version "); + Serial.print(NdefRecord.recordPayload[0] >> 4); + Serial.println(NdefRecord.recordPayload[0] & 0xF); + break; + + case WELL_KNOWN_HANDOVER_REQUEST: + Serial.print("Handover request version "); + Serial.print(NdefRecord.recordPayload[0] >> 4); + Serial.println(NdefRecord.recordPayload[0] & 0xF); + break; + + case MEDIA_HANDOVER_BT: + Serial.print("- Payload size: "); + Serial.println(NdefRecord.recordPayloadSize); + Serial.print("- Bluetooth Handover payload = "); + // Serial.println(Electroniccats_PN7150::getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + Serial.print("- Bluetooth name: '"); + bluetoothName = ""; + for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { + if (NdefRecord.recordPayload[i] == 0x04) { + break; + } + bluetoothName += (char)NdefRecord.recordPayload[i]; + } + Serial.println(bluetoothName + "'"); + + Serial.print("- Bluetooth address: '"); + bluetoothAddress = ""; + for (unsigned int i = 7; i >= 2; i--) { + // bluetoothAddress += Electroniccats_PN7150::getHexRepresentation(&NdefRecord.recordPayload[i], 1); + if (i > 2) { + bluetoothAddress += ":"; + } + } + Serial.println(bluetoothAddress + "'"); + break; + + case MEDIA_HANDOVER_BLE: + Serial.print("- BLE Handover payload = "); + // Serial.println(Electroniccats_PN7150::getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + break; + + case MEDIA_HANDOVER_BLE_SECURE: + Serial.print("- BLE secure Handover payload = "); + // Serial.println(Electroniccats_PN7150::getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + break; + + default: + Serial.println("Unsupported NDEF record, cannot parse"); + break; + } + pNdefRecord = GetNextRecord(pNdefRecord); + } + + Serial.println(""); +} + bool Electroniccats_PN7150::isTimeOut() const { return ((millis() - timeOutStartTime) >= timeOut); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 38dc50f..099a103 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -107,6 +107,7 @@ class Electroniccats_PN7150 : public Mode { TwoWire *_wire; RfIntf_t dummyRfInterface; uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message + static void updateNdefMessage(unsigned char *message, unsigned short messageSize); void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command bool isTimeOut() const; bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses diff --git a/src/RW_NDEF.cpp b/src/RW_NDEF.cpp index 62968ee..a8b6951 100644 --- a/src/RW_NDEF.cpp +++ b/src/RW_NDEF.cpp @@ -30,6 +30,7 @@ unsigned short RW_NdefMessage_size; RW_NDEF_Callback_t *pRW_NDEF_PullCb; RW_NDEF_Callback_t *pRW_NDEF_PushCb; +RW_NDEF_Callback_t *updateNdefMessageCallback; CustomCallback_t *ndefReceivedCallback; static RW_NDEF_Fct_t *pReadFct = NULL; @@ -52,6 +53,10 @@ void RW_NDEF_RegisterPullCallback(void *pCb) { pRW_NDEF_PullCb = (RW_NDEF_Callback_t *)pCb; } +void registerUpdateNdefMessageCallback(RW_NDEF_Callback_t function) { + updateNdefMessageCallback = function; +} + void registerNdefReceivedCallback(CustomCallback_t function) { ndefReceivedCallback = function; } diff --git a/src/RW_NDEF.h b/src/RW_NDEF.h index ad40c13..71898ee 100644 --- a/src/RW_NDEF.h +++ b/src/RW_NDEF.h @@ -31,6 +31,7 @@ extern unsigned short RW_NdefMessage_size; extern RW_NDEF_Callback_t *pRW_NDEF_PullCb; extern RW_NDEF_Callback_t *pRW_NDEF_PushCb; +extern RW_NDEF_Callback_t *updateNdefMessageCallback; extern CustomCallback_t *ndefReceivedCallback; void RW_NDEF_Reset(unsigned char type); @@ -38,4 +39,5 @@ void RW_NDEF_Read_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned ch void RW_NDEF_Write_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); bool RW_NDEF_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); void RW_NDEF_RegisterPullCallback(void *pCb); +void registerUpdateNdefMessageCallback(RW_NDEF_Callback_t function); void registerNdefReceivedCallback(CustomCallback_t function); diff --git a/src/RW_NDEF_MIFARE.cpp b/src/RW_NDEF_MIFARE.cpp index c5b163c..0f6fa21 100644 --- a/src/RW_NDEF_MIFARE.cpp +++ b/src/RW_NDEF_MIFARE.cpp @@ -134,8 +134,13 @@ void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsi if (pRW_NDEF_PullCb != NULL) pRW_NDEF_PullCb(NULL, 0); + // Run custom callbacks + if (updateNdefMessageCallback != NULL) { + updateNdefMessageCallback(NULL, 0); + } + if (ndefReceivedCallback != NULL) { - ndefReceivedCallback(); // Run custom callback + ndefReceivedCallback(); } break; @@ -150,8 +155,13 @@ void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsi pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); } + // Run custom callbacks + if (updateNdefMessageCallback != NULL) { + updateNdefMessageCallback(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); + } + if (ndefReceivedCallback != NULL) { - ndefReceivedCallback(); // Run custom callback + ndefReceivedCallback(); } } else { RW_NDEF_MIFARE_Ndef.MessagePtr = (Rsp_size - 1) - Tmp - 2; @@ -179,8 +189,13 @@ void RW_NDEF_MIFARE_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsi pRW_NDEF_PullCb(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); } + // Run custom callbacks + if (updateNdefMessageCallback != NULL) { + updateNdefMessageCallback(RW_NDEF_MIFARE_Ndef.pMessage, RW_NDEF_MIFARE_Ndef.MessageSize); + } + if (ndefReceivedCallback != NULL) { - ndefReceivedCallback(); // Run custom callback + ndefReceivedCallback(); } } else { memcpy(&RW_NDEF_MIFARE_Ndef.pMessage[RW_NDEF_MIFARE_Ndef.MessagePtr], pRsp + 1, 16); From a98e46faa1b98950cbe58873511ba6586468e370 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 22 Aug 2023 16:11:12 -0600 Subject: [PATCH 074/106] feat: add ndef message class --- examples/NDEFRead/NDEFRead.ino | 3 ++- src/Electroniccats_PN7150.cpp | 10 +++++----- src/Electroniccats_PN7150.h | 9 +++++---- src/NdefMessage.cpp | 36 ++++++++++++++++++++++++++++++++++ src/NdefMessage.h | 21 ++++++++++++++++++++ 5 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/NdefMessage.cpp create mode 100644 src/NdefMessage.h diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 0ebac6f..e0eeac5 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -10,6 +10,7 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize); // Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); +NdefMessage message; void setup() { Serial.begin(9600); @@ -29,8 +30,8 @@ void setup() { ; } + message.begin(); nfc.setReaderWriterMode(); - Serial.print("Waiting for a Card..."); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index c12c904..1d7d822 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -56,7 +56,7 @@ Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t Electroniccats_PN7150::begin() { // Register callbacks - registerUpdateNdefMessageCallback(Electroniccats_PN7150::updateNdefMessage); + // registerUpdateNdefMessageCallback(Electroniccats_PN7150::updateNdefMessage); _wire->begin(); if (_VENpin != 255) { @@ -91,7 +91,7 @@ uint8_t Electroniccats_PN7150::begin() { void Electroniccats_PN7150::updateNdefMessage(unsigned char *message, unsigned short messageSize) { Serial.println("here :D"); - unsigned char *pNdefRecord = message; + unsigned char *pNdefMessage = message; NdefRecord_t NdefRecord; unsigned char save; String SSID; @@ -103,10 +103,10 @@ void Electroniccats_PN7150::updateNdefMessage(unsigned char *message, unsigned s return; } - while (pNdefRecord != NULL) { + while (pNdefMessage != NULL) { Serial.println("--- NDEF record received:"); - NdefRecord = DetectNdefRecordType(pNdefRecord); + NdefRecord = DetectNdefRecordType(pNdefMessage); switch (NdefRecord.recordType) { case MEDIA_VCARD: { @@ -215,7 +215,7 @@ void Electroniccats_PN7150::updateNdefMessage(unsigned char *message, unsigned s Serial.println("Unsupported NDEF record, cannot parse"); break; } - pNdefRecord = GetNextRecord(pNdefRecord); + pNdefMessage = GetNextRecord(pNdefMessage); } Serial.println(""); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 099a103..e250b1d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -26,6 +26,7 @@ #include "RemoteDevice.h" #include "T4T_NDEF_emu.h" #include "ndef_helper.h" +#include "NdefMessage.h" // #define DEBGU2 // #define DEBUG3 @@ -107,15 +108,15 @@ class Electroniccats_PN7150 : public Mode { TwoWire *_wire; RfIntf_t dummyRfInterface; uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message - static void updateNdefMessage(unsigned char *message, unsigned short messageSize); - void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command - bool isTimeOut() const; - bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses unsigned long timeOut; unsigned long timeOutStartTime; uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length uint8_t gNfcController_generation = 0; uint8_t gNfcController_fw_version[3] = {0}; + static void updateNdefMessage(unsigned char *message, unsigned short messageSize); + void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command + bool isTimeOut() const; + bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp new file mode 100644 index 0000000..86ee477 --- /dev/null +++ b/src/NdefMessage.cpp @@ -0,0 +1,36 @@ +#include "NdefMessage.h" + +unsigned char *NdefMessage::content; +unsigned short NdefMessage::contentSize; + +NdefMessage::NdefMessage() { + content = NULL; + contentSize = 0; +} + +void NdefMessage::begin() { + registerUpdateNdefMessageCallback(NdefMessage::update); +} + +void NdefMessage::update(unsigned char *message, unsigned short messageSize) { + Serial.println("Updating message..."); + if (content != NULL) { + free(content); + } + content = (unsigned char *)malloc(messageSize); + memcpy(content, message, messageSize); + contentSize = messageSize; +} + +unsigned char *NdefMessage::getContent() { + return content; +} + +unsigned short NdefMessage::getContentSize() { + return contentSize; +} + +void NdefMessage::setContent(unsigned char *content, unsigned short contentSize) { + content = content; + contentSize = contentSize; +} \ No newline at end of file diff --git a/src/NdefMessage.h b/src/NdefMessage.h new file mode 100644 index 0000000..1ca7e29 --- /dev/null +++ b/src/NdefMessage.h @@ -0,0 +1,21 @@ +#ifndef NdefMessage_H +#define NdefMessage_H + +#include +#include "RW_NDEF.h" + +class NdefMessage { + private: + static unsigned char *content; + static unsigned short contentSize; + static void update(unsigned char *message, unsigned short messageSize); + + public: + NdefMessage(); + void begin(); + static unsigned char *getContent(); + static unsigned short getContentSize(); + static void setContent(unsigned char *content, unsigned short contentSize); +}; + +#endif \ No newline at end of file From 51d794fa54eb5a86577942db71dea15646318357 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 22 Aug 2023 17:33:17 -0600 Subject: [PATCH 075/106] feat: update ndef read example with new methods - get record - get next record - is empty - is not empty --- examples/NDEFRead/NDEFRead.ino | 226 ++++++++++++++++++++++++++------- src/Electroniccats_PN7150.h | 2 +- src/NdefMessage.cpp | 24 +++- src/NdefMessage.h | 16 ++- 4 files changed, 210 insertions(+), 58 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index e0eeac5..5196b29 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -162,79 +162,75 @@ String getHexRepresentation(const byte *data, const uint32_t numBytes) { } void customNdefCallback() { - Serial.println("Processing Callback..."); -} - -void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { - unsigned char *pNdefRecord = pNdefMessage; - NdefRecord_t NdefRecord; + NdefRecord_t record; unsigned char save; String SSID; String bluetoothName; String bluetoothAddress; - Serial.println("Processing Callback"); + Serial.println("Processing Callback..."); - if (pNdefMessage == NULL) { + if (message.isEmpty()) { Serial.println("--- Provisioned buffer size too small or NDEF message empty"); return; } - while (pNdefRecord != NULL) { + while (message.isNotEmpty()) { Serial.println("--- NDEF record received:"); - NdefRecord = DetectNdefRecordType(pNdefRecord); + // Get a new record every time we call getRecord() + record = message.getRecord(); - switch (NdefRecord.recordType) { + switch (record.recordType) { case MEDIA_VCARD: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + save = record.recordPayload[record.recordPayloadSize]; + record.recordPayload[record.recordPayloadSize] = '\0'; Serial.print("vCard:"); - Serial.println(reinterpret_cast(NdefRecord.recordPayload)); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + Serial.println(reinterpret_cast(record.recordPayload)); + record.recordPayload[record.recordPayloadSize] = save; } break; case WELL_KNOWN_SIMPLE_TEXT: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + save = record.recordPayload[record.recordPayloadSize]; + record.recordPayload[record.recordPayloadSize] = '\0'; Serial.print("Text record: "); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); + record.recordPayload[record.recordPayloadSize] = save; } break; case WELL_KNOWN_SIMPLE_URI: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; + save = record.recordPayload[record.recordPayloadSize]; + record.recordPayload[record.recordPayloadSize] = '\0'; Serial.print("URI record: "); - Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; + Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); + record.recordPayload[record.recordPayloadSize] = save; } break; case MEDIA_HANDOVER_WIFI: { unsigned char index = 0, i; Serial.println("--- Received WIFI credentials:"); - if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) + if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) index += 4; - while (index < NdefRecord.recordPayloadSize) { - if (NdefRecord.recordPayload[index] == 0x10) { - if (NdefRecord.recordPayload[index + 1] == 0x45) { + while (index < record.recordPayloadSize) { + if (record.recordPayload[index] == 0x10) { + if (record.recordPayload[index + 1] == 0x45) { Serial.print("- SSID = "); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); + Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); Serial.println(SSID); - } else if (NdefRecord.recordPayload[index + 1] == 0x03) { + } else if (record.recordPayload[index + 1] == 0x03) { Serial.print("- Authenticate Type = "); - Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); - } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { + Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); + } else if (record.recordPayload[index + 1] == 0x0f) { Serial.print("- Encryption Type = "); - Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); - } else if (NdefRecord.recordPayload[index + 1] == 0x27) { + Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); + } else if (record.recordPayload[index + 1] == 0x27) { Serial.print("- Network key = "); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); + Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); Serial.print("- Network key = "); - Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); + Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); } - index += 4 + NdefRecord.recordPayload[index + 3]; + index += 4 + record.recordPayload[index + 3]; } else continue; } @@ -242,35 +238,35 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { case WELL_KNOWN_HANDOVER_SELECT: Serial.print("Handover select version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); + Serial.print(record.recordPayload[0] >> 4); + Serial.println(record.recordPayload[0] & 0xF); break; case WELL_KNOWN_HANDOVER_REQUEST: Serial.print("Handover request version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); + Serial.print(record.recordPayload[0] >> 4); + Serial.println(record.recordPayload[0] & 0xF); break; case MEDIA_HANDOVER_BT: Serial.print("- Payload size: "); - Serial.println(NdefRecord.recordPayloadSize); + Serial.println(record.recordPayloadSize); Serial.print("- Bluetooth Handover payload = "); - Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); Serial.print("- Bluetooth name: '"); bluetoothName = ""; - for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { - if (NdefRecord.recordPayload[i] == 0x04) { + for (unsigned int i = 10; i < record.recordPayloadSize; i++) { + if (record.recordPayload[i] == 0x04) { break; } - bluetoothName += (char)NdefRecord.recordPayload[i]; + bluetoothName += (char)record.recordPayload[i]; } Serial.println(bluetoothName + "'"); Serial.print("- Bluetooth address: '"); bluetoothAddress = ""; for (unsigned int i = 7; i >= 2; i--) { - bluetoothAddress += getHexRepresentation(&NdefRecord.recordPayload[i], 1); + bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); if (i > 2) { bluetoothAddress += ":"; } @@ -280,20 +276,152 @@ void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { case MEDIA_HANDOVER_BLE: Serial.print("- BLE Handover payload = "); - Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); break; case MEDIA_HANDOVER_BLE_SECURE: Serial.print("- BLE secure Handover payload = "); - Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); + Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); break; default: Serial.println("Unsupported NDEF record, cannot parse"); break; } - pNdefRecord = GetNextRecord(pNdefRecord); } Serial.println(""); } + +// void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { +// unsigned char *pNdefRecord = pNdefMessage; +// NdefRecord_t NdefRecord; +// unsigned char save; +// String SSID; +// String bluetoothName; +// String bluetoothAddress; + +// Serial.println("Processing Callback"); + +// if (pNdefMessage == NULL) { +// Serial.println("--- Provisioned buffer size too small or NDEF message empty"); +// return; +// } + +// while (pNdefRecord != NULL) { +// Serial.println("--- NDEF record received:"); + +// NdefRecord = DetectNdefRecordType(pNdefRecord); + +// switch (NdefRecord.recordType) { +// case MEDIA_VCARD: { +// save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; +// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; +// Serial.print("vCard:"); +// Serial.println(reinterpret_cast(NdefRecord.recordPayload)); +// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; +// } break; + +// case WELL_KNOWN_SIMPLE_TEXT: { +// save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; +// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; +// Serial.print("Text record: "); +// Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); +// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; +// } break; + +// case WELL_KNOWN_SIMPLE_URI: { +// save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; +// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; +// Serial.print("URI record: "); +// Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); +// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; +// } break; + +// case MEDIA_HANDOVER_WIFI: { +// unsigned char index = 0, i; + +// Serial.println("--- Received WIFI credentials:"); +// if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) +// index += 4; +// while (index < NdefRecord.recordPayloadSize) { +// if (NdefRecord.recordPayload[index] == 0x10) { +// if (NdefRecord.recordPayload[index + 1] == 0x45) { +// Serial.print("- SSID = "); +// Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); +// Serial.println(SSID); +// } else if (NdefRecord.recordPayload[index + 1] == 0x03) { +// Serial.print("- Authenticate Type = "); +// Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); +// } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { +// Serial.print("- Encryption Type = "); +// Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); +// } else if (NdefRecord.recordPayload[index + 1] == 0x27) { +// Serial.print("- Network key = "); +// Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); +// Serial.print("- Network key = "); +// Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); +// } +// index += 4 + NdefRecord.recordPayload[index + 3]; +// } else +// continue; +// } +// } break; + +// case WELL_KNOWN_HANDOVER_SELECT: +// Serial.print("Handover select version "); +// Serial.print(NdefRecord.recordPayload[0] >> 4); +// Serial.println(NdefRecord.recordPayload[0] & 0xF); +// break; + +// case WELL_KNOWN_HANDOVER_REQUEST: +// Serial.print("Handover request version "); +// Serial.print(NdefRecord.recordPayload[0] >> 4); +// Serial.println(NdefRecord.recordPayload[0] & 0xF); +// break; + +// case MEDIA_HANDOVER_BT: +// Serial.print("- Payload size: "); +// Serial.println(NdefRecord.recordPayloadSize); +// Serial.print("- Bluetooth Handover payload = "); +// Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); +// Serial.print("- Bluetooth name: '"); +// bluetoothName = ""; +// for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { +// if (NdefRecord.recordPayload[i] == 0x04) { +// break; +// } +// bluetoothName += (char)NdefRecord.recordPayload[i]; +// } +// Serial.println(bluetoothName + "'"); + +// Serial.print("- Bluetooth address: '"); +// bluetoothAddress = ""; +// for (unsigned int i = 7; i >= 2; i--) { +// bluetoothAddress += getHexRepresentation(&NdefRecord.recordPayload[i], 1); +// if (i > 2) { +// bluetoothAddress += ":"; +// } +// } +// Serial.println(bluetoothAddress + "'"); +// break; + +// case MEDIA_HANDOVER_BLE: +// Serial.print("- BLE Handover payload = "); +// Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); +// break; + +// case MEDIA_HANDOVER_BLE_SECURE: +// Serial.print("- BLE secure Handover payload = "); +// Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); +// break; + +// default: +// Serial.println("Unsupported NDEF record, cannot parse"); +// break; +// } +// pNdefRecord = GetNextRecord(pNdefRecord); +// } + +// Serial.println(""); +// } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index e250b1d..8770381 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -25,7 +25,7 @@ #include "RW_NDEF.h" #include "RemoteDevice.h" #include "T4T_NDEF_emu.h" -#include "ndef_helper.h" +// #include "ndef_helper.h" #include "NdefMessage.h" // #define DEBGU2 diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 86ee477..0ec5b1a 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -31,6 +31,24 @@ unsigned short NdefMessage::getContentSize() { } void NdefMessage::setContent(unsigned char *content, unsigned short contentSize) { - content = content; - contentSize = contentSize; -} \ No newline at end of file + NdefMessage::content = content; + NdefMessage::contentSize = contentSize; +} + +NdefRecord_t NdefMessage::getRecord() { + NdefRecord_t ndefRecord = DetectNdefRecordType(content); + NdefMessage::getNextRecord(); + return ndefRecord; +} + +void NdefMessage::getNextRecord() { + content = GetNextRecord(content); +} + +bool NdefMessage::isEmpty() { + return NdefMessage::getContent() == NULL; +} + +bool NdefMessage::isNotEmpty() { + return NdefMessage::getContent() != NULL; +} diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 1ca7e29..2aaba63 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -2,20 +2,26 @@ #define NdefMessage_H #include + #include "RW_NDEF.h" +#include "ndef_helper.h" class NdefMessage { private: static unsigned char *content; static unsigned short contentSize; - static void update(unsigned char *message, unsigned short messageSize); + static void update(unsigned char *message, unsigned short messageSize); + void getNextRecord(); public: NdefMessage(); - void begin(); - static unsigned char *getContent(); - static unsigned short getContentSize(); - static void setContent(unsigned char *content, unsigned short contentSize); + void begin(); + static unsigned char *getContent(); + static unsigned short getContentSize(); + static void setContent(unsigned char *content, unsigned short contentSize); + NdefRecord_t getRecord(); + bool isEmpty(); + bool isNotEmpty(); }; #endif \ No newline at end of file From 48525c79f2ad645a307585fd8ca0d12b2fa771af Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 22 Aug 2023 21:39:59 -0600 Subject: [PATCH 076/106] refactor: add display record info --- examples/NDEFRead/NDEFRead.ino | 416 ++++++++++++--------------------- src/NdefMessage.cpp | 5 +- src/NdefMessage.h | 1 + 3 files changed, 148 insertions(+), 274 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 5196b29..d06c8fa 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -4,9 +4,10 @@ #define PN7150_ADDR (0x28) // Function prototypes +String getHexRepresentation(const byte *data, const uint32_t dataSize); void displayDeviceInfo(); -void customNdefCallback(); -void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize); +void displayRecordInfo(NdefRecord_t record); +void ndefCallback(); // Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); @@ -19,14 +20,13 @@ void setup() { Serial.println("Detect NFC tags with PN7150"); // Register a callback function to be called when an NDEF message is received - // RW_NDEF_RegisterPullCallback((void *)*ndefCallback); - nfc.setSendMsgCallback(customNdefCallback); + nfc.setSendMsgCallback(ndefCallback); Serial.println("Initializing..."); if (nfc.begin()) { Serial.println("Error initializing PN7150"); - while (1) + while (true) ; } @@ -36,7 +36,7 @@ void setup() { } void loop() { - if (nfc.isTagDetected()) { // Wait for a card + if (nfc.isTagDetected()) { displayDeviceInfo(); switch (nfc.remoteDevice.getProtocol()) { // Read NDEF message from NFC Forum Type 1, 2, 3, 4, 5 tags @@ -69,6 +69,26 @@ void loop() { delay(500); } +String getHexRepresentation(const byte *data, const uint32_t dataSize) { + String hexString; + + if (dataSize == 0) { + hexString = "null"; + } + + for (uint32_t index = 0; index < dataSize; index++) { + if (data[index] <= 0xF) + hexString += "0"; + String hexValue = String(data[index] & 0xFF, HEX); + hexValue.toUpperCase(); + hexString += hexValue; + if ((dataSize > 1) && (index != dataSize - 1)) { + hexString += ":"; + } + } + return hexString; +} + void displayDeviceInfo() { Serial.println(); Serial.print("Interface: "); @@ -141,33 +161,126 @@ void displayDeviceInfo() { } } -String getHexRepresentation(const byte *data, const uint32_t numBytes) { - String hexString; +void displayRecordInfo(NdefRecord_t record) { + unsigned char save; + String SSID; + String bluetoothName; + String bluetoothAddress; + Serial.println("--- NDEF record received:"); + + switch (record.recordType) { + case MEDIA_VCARD: { + save = record.recordPayload[record.recordPayloadSize]; + record.recordPayload[record.recordPayloadSize] = '\0'; + Serial.print("vCard:"); + Serial.println(reinterpret_cast(record.recordPayload)); + record.recordPayload[record.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_TEXT: { + save = record.recordPayload[record.recordPayloadSize]; + record.recordPayload[record.recordPayloadSize] = '\0'; + Serial.print("Text record: "); + Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); + record.recordPayload[record.recordPayloadSize] = save; + } break; + + case WELL_KNOWN_SIMPLE_URI: { + save = record.recordPayload[record.recordPayloadSize]; + record.recordPayload[record.recordPayloadSize] = '\0'; + Serial.print("URI record: "); + Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); + record.recordPayload[record.recordPayloadSize] = save; + } break; + + case MEDIA_HANDOVER_WIFI: { + unsigned char index = 0, i; + + Serial.println("--- Received WIFI credentials:"); + if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) + index += 4; + while (index < record.recordPayloadSize) { + if (record.recordPayload[index] == 0x10) { + if (record.recordPayload[index + 1] == 0x45) { + Serial.print("- SSID = "); + Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); + Serial.println(SSID); + } else if (record.recordPayload[index + 1] == 0x03) { + Serial.print("- Authenticate Type = "); + Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); + } else if (record.recordPayload[index + 1] == 0x0f) { + Serial.print("- Encryption Type = "); + Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); + } else if (record.recordPayload[index + 1] == 0x27) { + Serial.print("- Network key = "); + Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); + Serial.print("- Network key = "); + Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); + } + index += 4 + record.recordPayload[index + 3]; + } else + continue; + } + } break; + + case WELL_KNOWN_HANDOVER_SELECT: + Serial.print("Handover select version "); + Serial.print(record.recordPayload[0] >> 4); + Serial.println(record.recordPayload[0] & 0xF); + break; - if (numBytes == 0) { - hexString = "null"; - } + case WELL_KNOWN_HANDOVER_REQUEST: + Serial.print("Handover request version "); + Serial.print(record.recordPayload[0] >> 4); + Serial.println(record.recordPayload[0] & 0xF); + break; - for (uint32_t szPos = 0; szPos < numBytes; szPos++) { - if (data[szPos] <= 0xF) - hexString += "0"; - String hexValue = String(data[szPos] & 0xFF, HEX); - hexValue.toUpperCase(); - hexString += hexValue; - if ((numBytes > 1) && (szPos != numBytes - 1)) { - hexString += ":"; - } + case MEDIA_HANDOVER_BT: + Serial.print("- Payload size: "); + Serial.println(record.recordPayloadSize); + Serial.print("- Bluetooth Handover payload = "); + Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + Serial.print("- Bluetooth name: '"); + bluetoothName = ""; + for (unsigned int i = 10; i < record.recordPayloadSize; i++) { + if (record.recordPayload[i] == 0x04) { + break; + } + bluetoothName += (char)record.recordPayload[i]; + } + Serial.println(bluetoothName + "'"); + + Serial.print("- Bluetooth address: '"); + bluetoothAddress = ""; + for (unsigned int i = 7; i >= 2; i--) { + bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); + if (i > 2) { + bluetoothAddress += ":"; + } + } + Serial.println(bluetoothAddress + "'"); + break; + + case MEDIA_HANDOVER_BLE: + Serial.print("- BLE Handover payload = "); + Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + break; + + case MEDIA_HANDOVER_BLE_SECURE: + Serial.print("- BLE secure Handover payload = "); + Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + break; + + default: + Serial.println("Unsupported NDEF record, cannot parse"); + break; } - return hexString; + + Serial.println(""); } -void customNdefCallback() { +void ndefCallback() { NdefRecord_t record; - unsigned char save; - String SSID; - String bluetoothName; - String bluetoothAddress; - Serial.println("Processing Callback..."); if (message.isEmpty()) { @@ -175,253 +288,10 @@ void customNdefCallback() { return; } - while (message.isNotEmpty()) { - Serial.println("--- NDEF record received:"); - + // TODO: replace with record.isNotEmpty() when implemented + while (message.hasRecord()) { // Get a new record every time we call getRecord() record = message.getRecord(); - - switch (record.recordType) { - case MEDIA_VCARD: { - save = record.recordPayload[record.recordPayloadSize]; - record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("vCard:"); - Serial.println(reinterpret_cast(record.recordPayload)); - record.recordPayload[record.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_TEXT: { - save = record.recordPayload[record.recordPayloadSize]; - record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("Text record: "); - Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); - record.recordPayload[record.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_URI: { - save = record.recordPayload[record.recordPayloadSize]; - record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("URI record: "); - Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); - record.recordPayload[record.recordPayloadSize] = save; - } break; - - case MEDIA_HANDOVER_WIFI: { - unsigned char index = 0, i; - - Serial.println("--- Received WIFI credentials:"); - if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) - index += 4; - while (index < record.recordPayloadSize) { - if (record.recordPayload[index] == 0x10) { - if (record.recordPayload[index + 1] == 0x45) { - Serial.print("- SSID = "); - Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); - Serial.println(SSID); - } else if (record.recordPayload[index + 1] == 0x03) { - Serial.print("- Authenticate Type = "); - Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); - } else if (record.recordPayload[index + 1] == 0x0f) { - Serial.print("- Encryption Type = "); - Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); - } else if (record.recordPayload[index + 1] == 0x27) { - Serial.print("- Network key = "); - Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); - Serial.print("- Network key = "); - Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); - } - index += 4 + record.recordPayload[index + 3]; - } else - continue; - } - } break; - - case WELL_KNOWN_HANDOVER_SELECT: - Serial.print("Handover select version "); - Serial.print(record.recordPayload[0] >> 4); - Serial.println(record.recordPayload[0] & 0xF); - break; - - case WELL_KNOWN_HANDOVER_REQUEST: - Serial.print("Handover request version "); - Serial.print(record.recordPayload[0] >> 4); - Serial.println(record.recordPayload[0] & 0xF); - break; - - case MEDIA_HANDOVER_BT: - Serial.print("- Payload size: "); - Serial.println(record.recordPayloadSize); - Serial.print("- Bluetooth Handover payload = "); - Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - Serial.print("- Bluetooth name: '"); - bluetoothName = ""; - for (unsigned int i = 10; i < record.recordPayloadSize; i++) { - if (record.recordPayload[i] == 0x04) { - break; - } - bluetoothName += (char)record.recordPayload[i]; - } - Serial.println(bluetoothName + "'"); - - Serial.print("- Bluetooth address: '"); - bluetoothAddress = ""; - for (unsigned int i = 7; i >= 2; i--) { - bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); - if (i > 2) { - bluetoothAddress += ":"; - } - } - Serial.println(bluetoothAddress + "'"); - break; - - case MEDIA_HANDOVER_BLE: - Serial.print("- BLE Handover payload = "); - Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - break; - - case MEDIA_HANDOVER_BLE_SECURE: - Serial.print("- BLE secure Handover payload = "); - Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - break; - - default: - Serial.println("Unsupported NDEF record, cannot parse"); - break; - } + displayRecordInfo(record); } - - Serial.println(""); } - -// void ndefCallback(unsigned char *pNdefMessage, unsigned short NdefMessageSize) { -// unsigned char *pNdefRecord = pNdefMessage; -// NdefRecord_t NdefRecord; -// unsigned char save; -// String SSID; -// String bluetoothName; -// String bluetoothAddress; - -// Serial.println("Processing Callback"); - -// if (pNdefMessage == NULL) { -// Serial.println("--- Provisioned buffer size too small or NDEF message empty"); -// return; -// } - -// while (pNdefRecord != NULL) { -// Serial.println("--- NDEF record received:"); - -// NdefRecord = DetectNdefRecordType(pNdefRecord); - -// switch (NdefRecord.recordType) { -// case MEDIA_VCARD: { -// save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; -// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; -// Serial.print("vCard:"); -// Serial.println(reinterpret_cast(NdefRecord.recordPayload)); -// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; -// } break; - -// case WELL_KNOWN_SIMPLE_TEXT: { -// save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; -// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; -// Serial.print("Text record: "); -// Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); -// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; -// } break; - -// case WELL_KNOWN_SIMPLE_URI: { -// save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; -// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; -// Serial.print("URI record: "); -// Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); -// NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; -// } break; - -// case MEDIA_HANDOVER_WIFI: { -// unsigned char index = 0, i; - -// Serial.println("--- Received WIFI credentials:"); -// if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) -// index += 4; -// while (index < NdefRecord.recordPayloadSize) { -// if (NdefRecord.recordPayload[index] == 0x10) { -// if (NdefRecord.recordPayload[index + 1] == 0x45) { -// Serial.print("- SSID = "); -// Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); -// Serial.println(SSID); -// } else if (NdefRecord.recordPayload[index + 1] == 0x03) { -// Serial.print("- Authenticate Type = "); -// Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); -// } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { -// Serial.print("- Encryption Type = "); -// Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); -// } else if (NdefRecord.recordPayload[index + 1] == 0x27) { -// Serial.print("- Network key = "); -// Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); -// Serial.print("- Network key = "); -// Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); -// } -// index += 4 + NdefRecord.recordPayload[index + 3]; -// } else -// continue; -// } -// } break; - -// case WELL_KNOWN_HANDOVER_SELECT: -// Serial.print("Handover select version "); -// Serial.print(NdefRecord.recordPayload[0] >> 4); -// Serial.println(NdefRecord.recordPayload[0] & 0xF); -// break; - -// case WELL_KNOWN_HANDOVER_REQUEST: -// Serial.print("Handover request version "); -// Serial.print(NdefRecord.recordPayload[0] >> 4); -// Serial.println(NdefRecord.recordPayload[0] & 0xF); -// break; - -// case MEDIA_HANDOVER_BT: -// Serial.print("- Payload size: "); -// Serial.println(NdefRecord.recordPayloadSize); -// Serial.print("- Bluetooth Handover payload = "); -// Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); -// Serial.print("- Bluetooth name: '"); -// bluetoothName = ""; -// for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { -// if (NdefRecord.recordPayload[i] == 0x04) { -// break; -// } -// bluetoothName += (char)NdefRecord.recordPayload[i]; -// } -// Serial.println(bluetoothName + "'"); - -// Serial.print("- Bluetooth address: '"); -// bluetoothAddress = ""; -// for (unsigned int i = 7; i >= 2; i--) { -// bluetoothAddress += getHexRepresentation(&NdefRecord.recordPayload[i], 1); -// if (i > 2) { -// bluetoothAddress += ":"; -// } -// } -// Serial.println(bluetoothAddress + "'"); -// break; - -// case MEDIA_HANDOVER_BLE: -// Serial.print("- BLE Handover payload = "); -// Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); -// break; - -// case MEDIA_HANDOVER_BLE_SECURE: -// Serial.print("- BLE secure Handover payload = "); -// Serial.println(getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); -// break; - -// default: -// Serial.println("Unsupported NDEF record, cannot parse"); -// break; -// } -// pNdefRecord = GetNextRecord(pNdefRecord); -// } - -// Serial.println(""); -// } diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 0ec5b1a..926e468 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -13,7 +13,6 @@ void NdefMessage::begin() { } void NdefMessage::update(unsigned char *message, unsigned short messageSize) { - Serial.println("Updating message..."); if (content != NULL) { free(content); } @@ -52,3 +51,7 @@ bool NdefMessage::isEmpty() { bool NdefMessage::isNotEmpty() { return NdefMessage::getContent() != NULL; } + +bool NdefMessage::hasRecord() { + return NdefMessage::isNotEmpty(); +} diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 2aaba63..3002698 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -22,6 +22,7 @@ class NdefMessage { NdefRecord_t getRecord(); bool isEmpty(); bool isNotEmpty(); + bool hasRecord(); }; #endif \ No newline at end of file From ec99d67a4b8b0b6406d09d5bb813d9ab369d9128 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 09:51:03 -0600 Subject: [PATCH 077/106] feat: add record class --- examples/NDEFRead/NDEFRead.ino | 207 +++++++++++++++++---------------- src/Electroniccats_PN7150.h | 3 +- src/NdefRecord.cpp | 39 +++++++ src/NdefRecord.h | 23 ++++ src/ndef_helper.h | 4 + 5 files changed, 173 insertions(+), 103 deletions(-) create mode 100644 src/NdefRecord.cpp create mode 100644 src/NdefRecord.h diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index d06c8fa..fdb5328 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -161,115 +161,115 @@ void displayDeviceInfo() { } } -void displayRecordInfo(NdefRecord_t record) { +void displayRecordInfo(NdefRecord record) { unsigned char save; String SSID; String bluetoothName; String bluetoothAddress; Serial.println("--- NDEF record received:"); - switch (record.recordType) { - case MEDIA_VCARD: { - save = record.recordPayload[record.recordPayloadSize]; - record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("vCard:"); - Serial.println(reinterpret_cast(record.recordPayload)); - record.recordPayload[record.recordPayloadSize] = save; - } break; + switch (record.getType()) { + // case MEDIA_VCARD: { + // save = record.recordPayload[record.recordPayloadSize]; + // record.recordPayload[record.recordPayloadSize] = '\0'; + // Serial.print("vCard:"); + // Serial.println(reinterpret_cast(record.recordPayload)); + // record.recordPayload[record.recordPayloadSize] = save; + // } break; case WELL_KNOWN_SIMPLE_TEXT: { - save = record.recordPayload[record.recordPayloadSize]; - record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("Text record: "); - Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); - record.recordPayload[record.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_URI: { - save = record.recordPayload[record.recordPayloadSize]; - record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("URI record: "); - Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); - record.recordPayload[record.recordPayloadSize] = save; + // save = record.recordPayload[record.recordPayloadSize]; + // record.recordPayload[record.recordPayloadSize] = '\0'; + Serial.print("Text record: " + record.getText()); + // Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); + // record.recordPayload[record.recordPayloadSize] = save; } break; - case MEDIA_HANDOVER_WIFI: { - unsigned char index = 0, i; - - Serial.println("--- Received WIFI credentials:"); - if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) - index += 4; - while (index < record.recordPayloadSize) { - if (record.recordPayload[index] == 0x10) { - if (record.recordPayload[index + 1] == 0x45) { - Serial.print("- SSID = "); - Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); - Serial.println(SSID); - } else if (record.recordPayload[index + 1] == 0x03) { - Serial.print("- Authenticate Type = "); - Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); - } else if (record.recordPayload[index + 1] == 0x0f) { - Serial.print("- Encryption Type = "); - Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); - } else if (record.recordPayload[index + 1] == 0x27) { - Serial.print("- Network key = "); - Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); - Serial.print("- Network key = "); - Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); - } - index += 4 + record.recordPayload[index + 3]; - } else - continue; - } - } break; - - case WELL_KNOWN_HANDOVER_SELECT: - Serial.print("Handover select version "); - Serial.print(record.recordPayload[0] >> 4); - Serial.println(record.recordPayload[0] & 0xF); - break; - - case WELL_KNOWN_HANDOVER_REQUEST: - Serial.print("Handover request version "); - Serial.print(record.recordPayload[0] >> 4); - Serial.println(record.recordPayload[0] & 0xF); - break; - - case MEDIA_HANDOVER_BT: - Serial.print("- Payload size: "); - Serial.println(record.recordPayloadSize); - Serial.print("- Bluetooth Handover payload = "); - Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - Serial.print("- Bluetooth name: '"); - bluetoothName = ""; - for (unsigned int i = 10; i < record.recordPayloadSize; i++) { - if (record.recordPayload[i] == 0x04) { - break; - } - bluetoothName += (char)record.recordPayload[i]; - } - Serial.println(bluetoothName + "'"); - - Serial.print("- Bluetooth address: '"); - bluetoothAddress = ""; - for (unsigned int i = 7; i >= 2; i--) { - bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); - if (i > 2) { - bluetoothAddress += ":"; - } - } - Serial.println(bluetoothAddress + "'"); - break; - - case MEDIA_HANDOVER_BLE: - Serial.print("- BLE Handover payload = "); - Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - break; - - case MEDIA_HANDOVER_BLE_SECURE: - Serial.print("- BLE secure Handover payload = "); - Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - break; + // case WELL_KNOWN_SIMPLE_URI: { + // save = record.recordPayload[record.recordPayloadSize]; + // record.recordPayload[record.recordPayloadSize] = '\0'; + // Serial.print("URI record: "); + // Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); + // record.recordPayload[record.recordPayloadSize] = save; + // } break; + + // case MEDIA_HANDOVER_WIFI: { + // unsigned char index = 0, i; + + // Serial.println("--- Received WIFI credentials:"); + // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) + // index += 4; + // while (index < record.recordPayloadSize) { + // if (record.recordPayload[index] == 0x10) { + // if (record.recordPayload[index + 1] == 0x45) { + // Serial.print("- SSID = "); + // Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); + // Serial.println(SSID); + // } else if (record.recordPayload[index + 1] == 0x03) { + // Serial.print("- Authenticate Type = "); + // Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); + // } else if (record.recordPayload[index + 1] == 0x0f) { + // Serial.print("- Encryption Type = "); + // Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); + // } else if (record.recordPayload[index + 1] == 0x27) { + // Serial.print("- Network key = "); + // Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); + // Serial.print("- Network key = "); + // Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); + // } + // index += 4 + record.recordPayload[index + 3]; + // } else + // continue; + // } + // } break; + + // case WELL_KNOWN_HANDOVER_SELECT: + // Serial.print("Handover select version "); + // Serial.print(record.recordPayload[0] >> 4); + // Serial.println(record.recordPayload[0] & 0xF); + // break; + + // case WELL_KNOWN_HANDOVER_REQUEST: + // Serial.print("Handover request version "); + // Serial.print(record.recordPayload[0] >> 4); + // Serial.println(record.recordPayload[0] & 0xF); + // break; + + // case MEDIA_HANDOVER_BT: + // Serial.print("- Payload size: "); + // Serial.println(record.recordPayloadSize); + // Serial.print("- Bluetooth Handover payload = "); + // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + // Serial.print("- Bluetooth name: '"); + // bluetoothName = ""; + // for (unsigned int i = 10; i < record.recordPayloadSize; i++) { + // if (record.recordPayload[i] == 0x04) { + // break; + // } + // bluetoothName += (char)record.recordPayload[i]; + // } + // Serial.println(bluetoothName + "'"); + + // Serial.print("- Bluetooth address: '"); + // bluetoothAddress = ""; + // for (unsigned int i = 7; i >= 2; i--) { + // bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); + // if (i > 2) { + // bluetoothAddress += ":"; + // } + // } + // Serial.println(bluetoothAddress + "'"); + // break; + + // case MEDIA_HANDOVER_BLE: + // Serial.print("- BLE Handover payload = "); + // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + // break; + + // case MEDIA_HANDOVER_BLE_SECURE: + // Serial.print("- BLE secure Handover payload = "); + // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + // break; default: Serial.println("Unsupported NDEF record, cannot parse"); @@ -279,10 +279,14 @@ void displayRecordInfo(NdefRecord_t record) { Serial.println(""); } +/// @brief Callback function called when an NDEF message is received void ndefCallback() { NdefRecord_t record; + NdefRecord record2; Serial.println("Processing Callback..."); + // message is automatically updated when a new NDEF message is received + // only if we call message.begin() in setup() if (message.isEmpty()) { Serial.println("--- Provisioned buffer size too small or NDEF message empty"); return; @@ -291,7 +295,8 @@ void ndefCallback() { // TODO: replace with record.isNotEmpty() when implemented while (message.hasRecord()) { // Get a new record every time we call getRecord() - record = message.getRecord(); - displayRecordInfo(record); + // record = message.getRecord(); + record2.create(message.getRecord()); + displayRecordInfo(record2); } } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 8770381..31d2330 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -22,11 +22,10 @@ // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library #include "Mode.h" #include "P2P_NDEF.h" -#include "RW_NDEF.h" #include "RemoteDevice.h" #include "T4T_NDEF_emu.h" -// #include "ndef_helper.h" #include "NdefMessage.h" +#include "NdefRecord.h" // #define DEBGU2 // #define DEBUG3 diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp new file mode 100644 index 0000000..a57b5ac --- /dev/null +++ b/src/NdefRecord.cpp @@ -0,0 +1,39 @@ +#include "NdefRecord.h" + +NdefRecord::NdefRecord() { + type = UNSUPPORTED_NDEF_RECORD; + payload = NULL; + payloadSize = 0; +} + +void NdefRecord::create(NdefRecord_t record) { + this->type = record.recordType; + this->payload = record.recordPayload; + this->payloadSize = record.recordPayloadSize; +} + +NdefRecordType_e NdefRecord::getType() { + return this->type; +} + +unsigned char NdefRecord::getPayload() { + return this->payload[getPayloadSize()]; +} + +unsigned short NdefRecord::getPayloadSize() { + return this->payloadSize; +} + +String NdefRecord::getText() { + unsigned char save = payload[payloadSize]; + payload[payloadSize] = '\0'; + String text = ""; + + if (getType() == WELL_KNOWN_SIMPLE_TEXT) { + text = reinterpret_cast(&payload[payload[0] + 1]); + } + + payload[payloadSize] = save; + + return text; +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h new file mode 100644 index 0000000..5e4d957 --- /dev/null +++ b/src/NdefRecord.h @@ -0,0 +1,23 @@ +#ifndef NdefRecord_H +#define NdefRecord_H + +#include + +#include "ndef_helper.h" + +class NdefRecord { + private: + NdefRecordType_e type; + unsigned char *payload; + unsigned short payloadSize; + + public: + NdefRecord(); + void create(NdefRecord_t record); + NdefRecordType_e getType(); + unsigned char getPayload(); + unsigned short getPayloadSize(); + String getText(); +}; + +#endif \ No newline at end of file diff --git a/src/ndef_helper.h b/src/ndef_helper.h index 47f3b05..1d5a3dd 100644 --- a/src/ndef_helper.h +++ b/src/ndef_helper.h @@ -11,6 +11,8 @@ * copyright or trademark. NXP must not be liable for any loss or damage * arising from its use. */ +#ifndef NdefHelper_H +#define NdefHelper_H #define NDEF_EMPTY 0x00 #define NDEF_WELL_KNOWN 0x01 @@ -57,3 +59,5 @@ const char *ndef_helper_WifiEnc(unsigned char enc); const char *ndef_helper_UriHead(unsigned char head); NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord); unsigned char *GetNextRecord(unsigned char *pNdefRecord); + +#endif \ No newline at end of file From 8de2fdc5529c46a60db5c4bcaa27df4efe3cd972 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 12:25:25 -0600 Subject: [PATCH 078/106] feat: add empty validation in get records --- examples/NDEFRead/NDEFRead.ino | 208 +++++++++++++++++---------------- src/NdefMessage.cpp | 5 + src/NdefRecord.cpp | 8 ++ src/NdefRecord.h | 3 + src/ndef_helper.cpp | 7 ++ 5 files changed, 128 insertions(+), 103 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index fdb5328..9fc5e07 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -162,117 +162,123 @@ void displayDeviceInfo() { } void displayRecordInfo(NdefRecord record) { + if (record.isEmpty()) { + return; + } + unsigned char save; String SSID; String bluetoothName; String bluetoothAddress; Serial.println("--- NDEF record received:"); + Serial.println("\tRecord size: " + String(record.getPayloadSize()) + " bytes"); switch (record.getType()) { - // case MEDIA_VCARD: { - // save = record.recordPayload[record.recordPayloadSize]; - // record.recordPayload[record.recordPayloadSize] = '\0'; - // Serial.print("vCard:"); - // Serial.println(reinterpret_cast(record.recordPayload)); - // record.recordPayload[record.recordPayloadSize] = save; - // } break; + // case MEDIA_VCARD: { + // save = record.recordPayload[record.recordPayloadSize]; + // record.recordPayload[record.recordPayloadSize] = '\0'; + // Serial.print("vCard:"); + // Serial.println(reinterpret_cast(record.recordPayload)); + // record.recordPayload[record.recordPayloadSize] = save; + // } break; case WELL_KNOWN_SIMPLE_TEXT: { // save = record.recordPayload[record.recordPayloadSize]; // record.recordPayload[record.recordPayloadSize] = '\0'; - Serial.print("Text record: " + record.getText()); + Serial.println("\tWell known simple text"); + Serial.println("\tText record: " + record.getText()); // Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); // record.recordPayload[record.recordPayloadSize] = save; } break; - // case WELL_KNOWN_SIMPLE_URI: { - // save = record.recordPayload[record.recordPayloadSize]; - // record.recordPayload[record.recordPayloadSize] = '\0'; - // Serial.print("URI record: "); - // Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); - // record.recordPayload[record.recordPayloadSize] = save; - // } break; - - // case MEDIA_HANDOVER_WIFI: { - // unsigned char index = 0, i; - - // Serial.println("--- Received WIFI credentials:"); - // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) - // index += 4; - // while (index < record.recordPayloadSize) { - // if (record.recordPayload[index] == 0x10) { - // if (record.recordPayload[index + 1] == 0x45) { - // Serial.print("- SSID = "); - // Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); - // Serial.println(SSID); - // } else if (record.recordPayload[index + 1] == 0x03) { - // Serial.print("- Authenticate Type = "); - // Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); - // } else if (record.recordPayload[index + 1] == 0x0f) { - // Serial.print("- Encryption Type = "); - // Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); - // } else if (record.recordPayload[index + 1] == 0x27) { - // Serial.print("- Network key = "); - // Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); - // Serial.print("- Network key = "); - // Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); - // } - // index += 4 + record.recordPayload[index + 3]; - // } else - // continue; - // } - // } break; - - // case WELL_KNOWN_HANDOVER_SELECT: - // Serial.print("Handover select version "); - // Serial.print(record.recordPayload[0] >> 4); - // Serial.println(record.recordPayload[0] & 0xF); - // break; - - // case WELL_KNOWN_HANDOVER_REQUEST: - // Serial.print("Handover request version "); - // Serial.print(record.recordPayload[0] >> 4); - // Serial.println(record.recordPayload[0] & 0xF); - // break; - - // case MEDIA_HANDOVER_BT: - // Serial.print("- Payload size: "); - // Serial.println(record.recordPayloadSize); - // Serial.print("- Bluetooth Handover payload = "); - // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - // Serial.print("- Bluetooth name: '"); - // bluetoothName = ""; - // for (unsigned int i = 10; i < record.recordPayloadSize; i++) { - // if (record.recordPayload[i] == 0x04) { - // break; - // } - // bluetoothName += (char)record.recordPayload[i]; - // } - // Serial.println(bluetoothName + "'"); - - // Serial.print("- Bluetooth address: '"); - // bluetoothAddress = ""; - // for (unsigned int i = 7; i >= 2; i--) { - // bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); - // if (i > 2) { - // bluetoothAddress += ":"; - // } - // } - // Serial.println(bluetoothAddress + "'"); - // break; - - // case MEDIA_HANDOVER_BLE: - // Serial.print("- BLE Handover payload = "); - // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - // break; - - // case MEDIA_HANDOVER_BLE_SECURE: - // Serial.print("- BLE secure Handover payload = "); - // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - // break; + // case WELL_KNOWN_SIMPLE_URI: { + // save = record.recordPayload[record.recordPayloadSize]; + // record.recordPayload[record.recordPayloadSize] = '\0'; + // Serial.print("URI record: "); + // Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); + // record.recordPayload[record.recordPayloadSize] = save; + // } break; + + // case MEDIA_HANDOVER_WIFI: { + // unsigned char index = 0, i; + + // Serial.println("--- Received WIFI credentials:"); + // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) + // index += 4; + // while (index < record.recordPayloadSize) { + // if (record.recordPayload[index] == 0x10) { + // if (record.recordPayload[index + 1] == 0x45) { + // Serial.print("- SSID = "); + // Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); + // Serial.println(SSID); + // } else if (record.recordPayload[index + 1] == 0x03) { + // Serial.print("- Authenticate Type = "); + // Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); + // } else if (record.recordPayload[index + 1] == 0x0f) { + // Serial.print("- Encryption Type = "); + // Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); + // } else if (record.recordPayload[index + 1] == 0x27) { + // Serial.print("- Network key = "); + // Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); + // Serial.print("- Network key = "); + // Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); + // } + // index += 4 + record.recordPayload[index + 3]; + // } else + // continue; + // } + // } break; + + // case WELL_KNOWN_HANDOVER_SELECT: + // Serial.print("Handover select version "); + // Serial.print(record.recordPayload[0] >> 4); + // Serial.println(record.recordPayload[0] & 0xF); + // break; + + // case WELL_KNOWN_HANDOVER_REQUEST: + // Serial.print("Handover request version "); + // Serial.print(record.recordPayload[0] >> 4); + // Serial.println(record.recordPayload[0] & 0xF); + // break; + + // case MEDIA_HANDOVER_BT: + // Serial.print("- Payload size: "); + // Serial.println(record.recordPayloadSize); + // Serial.print("- Bluetooth Handover payload = "); + // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + // Serial.print("- Bluetooth name: '"); + // bluetoothName = ""; + // for (unsigned int i = 10; i < record.recordPayloadSize; i++) { + // if (record.recordPayload[i] == 0x04) { + // break; + // } + // bluetoothName += (char)record.recordPayload[i]; + // } + // Serial.println(bluetoothName + "'"); + + // Serial.print("- Bluetooth address: '"); + // bluetoothAddress = ""; + // for (unsigned int i = 7; i >= 2; i--) { + // bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); + // if (i > 2) { + // bluetoothAddress += ":"; + // } + // } + // Serial.println(bluetoothAddress + "'"); + // break; + + // case MEDIA_HANDOVER_BLE: + // Serial.print("- BLE Handover payload = "); + // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + // break; + + // case MEDIA_HANDOVER_BLE_SECURE: + // Serial.print("- BLE secure Handover payload = "); + // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); + // break; default: - Serial.println("Unsupported NDEF record, cannot parse"); + Serial.println("\tUnsupported NDEF record, cannot parse"); break; } @@ -281,8 +287,7 @@ void displayRecordInfo(NdefRecord record) { /// @brief Callback function called when an NDEF message is received void ndefCallback() { - NdefRecord_t record; - NdefRecord record2; + NdefRecord record; Serial.println("Processing Callback..."); // message is automatically updated when a new NDEF message is received @@ -292,11 +297,8 @@ void ndefCallback() { return; } - // TODO: replace with record.isNotEmpty() when implemented - while (message.hasRecord()) { - // Get a new record every time we call getRecord() - // record = message.getRecord(); - record2.create(message.getRecord()); - displayRecordInfo(record2); - } + do { + record.create(message.getRecord()); // Get a new record every time we call getRecord() + displayRecordInfo(record); + } while (record.isNotEmpty()); } diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 926e468..0bfc013 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -36,6 +36,11 @@ void NdefMessage::setContent(unsigned char *content, unsigned short contentSize) NdefRecord_t NdefMessage::getRecord() { NdefRecord_t ndefRecord = DetectNdefRecordType(content); + + if (NdefMessage::isEmpty()) { + return ndefRecord; + } + NdefMessage::getNextRecord(); return ndefRecord; } diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index a57b5ac..69d7f40 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -12,6 +12,14 @@ void NdefRecord::create(NdefRecord_t record) { this->payloadSize = record.recordPayloadSize; } +bool NdefRecord::isEmpty() { + return this->payload == NULL; +} + +bool NdefRecord::isNotEmpty() { + return this->payload != NULL; +} + NdefRecordType_e NdefRecord::getType() { return this->type; } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 5e4d957..f3e50da 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -7,6 +7,7 @@ class NdefRecord { private: + NdefRecord_t content; NdefRecordType_e type; unsigned char *payload; unsigned short payloadSize; @@ -14,6 +15,8 @@ class NdefRecord { public: NdefRecord(); void create(NdefRecord_t record); + bool isEmpty(); + bool isNotEmpty(); NdefRecordType_e getType(); unsigned char getPayload(); unsigned short getPayloadSize(); diff --git a/src/ndef_helper.cpp b/src/ndef_helper.cpp index bf5d445..2c55d53 100644 --- a/src/ndef_helper.cpp +++ b/src/ndef_helper.cpp @@ -78,6 +78,13 @@ const char *ndef_helper_UriHead(unsigned char head) { NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord) { NdefRecord_t record; + if (pNdefRecord == NULL) { + record.recordType = UNSUPPORTED_NDEF_RECORD; + record.recordPayload = NULL; + record.recordPayloadSize = 0; + return record; + } + uint8_t typeField; /* Short or normal record ?*/ From 6fd49caa6942804d10d72015c92e9e1bb3528c78 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 13:02:16 -0600 Subject: [PATCH 079/106] feat: add get bluetooth name --- examples/NDEFRead/NDEFRead.ino | 49 ++++++++++++------------------- src/NdefRecord.cpp | 53 +++++++++++++++++++++------------- src/NdefRecord.h | 3 +- 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 9fc5e07..6659af5 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -171,7 +171,6 @@ void displayRecordInfo(NdefRecord record) { String bluetoothName; String bluetoothAddress; Serial.println("--- NDEF record received:"); - Serial.println("\tRecord size: " + String(record.getPayloadSize()) + " bytes"); switch (record.getType()) { // case MEDIA_VCARD: { @@ -183,12 +182,8 @@ void displayRecordInfo(NdefRecord record) { // } break; case WELL_KNOWN_SIMPLE_TEXT: { - // save = record.recordPayload[record.recordPayloadSize]; - // record.recordPayload[record.recordPayloadSize] = '\0'; Serial.println("\tWell known simple text"); - Serial.println("\tText record: " + record.getText()); - // Serial.println(reinterpret_cast(&record.recordPayload[record.recordPayload[0] + 1])); - // record.recordPayload[record.recordPayloadSize] = save; + Serial.println("\t- Text record: " + record.getText()); } break; // case WELL_KNOWN_SIMPLE_URI: { @@ -241,31 +236,23 @@ void displayRecordInfo(NdefRecord record) { // Serial.println(record.recordPayload[0] & 0xF); // break; - // case MEDIA_HANDOVER_BT: - // Serial.print("- Payload size: "); - // Serial.println(record.recordPayloadSize); - // Serial.print("- Bluetooth Handover payload = "); - // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - // Serial.print("- Bluetooth name: '"); - // bluetoothName = ""; - // for (unsigned int i = 10; i < record.recordPayloadSize; i++) { - // if (record.recordPayload[i] == 0x04) { - // break; - // } - // bluetoothName += (char)record.recordPayload[i]; - // } - // Serial.println(bluetoothName + "'"); - - // Serial.print("- Bluetooth address: '"); - // bluetoothAddress = ""; - // for (unsigned int i = 7; i >= 2; i--) { - // bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); - // if (i > 2) { - // bluetoothAddress += ":"; - // } - // } - // Serial.println(bluetoothAddress + "'"); - // break; + case MEDIA_HANDOVER_BT: + Serial.println("\tBluetooth handover"); + Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); + Serial.print("\t- Bluetooth Handover payload = "); + Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); + Serial.print("\t- Bluetooth name: '" + record.getBluetoothName() + "'"); + + // Serial.print("- Bluetooth address: '"); + // bluetoothAddress = ""; + // for (unsigned int i = 7; i >= 2; i--) { + // bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); + // if (i > 2) { + // bluetoothAddress += ":"; + // } + // } + // Serial.println(bluetoothAddress + "'"); + break; // case MEDIA_HANDOVER_BLE: // Serial.print("- BLE Handover payload = "); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 69d7f40..fa9adc9 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -1,47 +1,60 @@ #include "NdefRecord.h" NdefRecord::NdefRecord() { - type = UNSUPPORTED_NDEF_RECORD; - payload = NULL; - payloadSize = 0; + type = UNSUPPORTED_NDEF_RECORD; + payload = NULL; + payloadSize = 0; } void NdefRecord::create(NdefRecord_t record) { - this->type = record.recordType; - this->payload = record.recordPayload; - this->payloadSize = record.recordPayloadSize; + this->type = record.recordType; + this->payload = record.recordPayload; + this->payloadSize = record.recordPayloadSize; } bool NdefRecord::isEmpty() { - return this->payload == NULL; + return this->payload == NULL; } bool NdefRecord::isNotEmpty() { - return this->payload != NULL; + return this->payload != NULL; } NdefRecordType_e NdefRecord::getType() { - return this->type; + return this->type; } -unsigned char NdefRecord::getPayload() { - return this->payload[getPayloadSize()]; +unsigned char *NdefRecord::getPayload() { + return this->payload; } unsigned short NdefRecord::getPayloadSize() { - return this->payloadSize; + return this->payloadSize; } String NdefRecord::getText() { - unsigned char save = payload[payloadSize]; - payload[payloadSize] = '\0'; - String text = ""; + unsigned char save = payload[payloadSize]; + payload[payloadSize] = '\0'; + String text = ""; - if (getType() == WELL_KNOWN_SIMPLE_TEXT) { - text = reinterpret_cast(&payload[payload[0] + 1]); - } + if (getType() == WELL_KNOWN_SIMPLE_TEXT) { + text = reinterpret_cast(&payload[payload[0] + 1]); + } - payload[payloadSize] = save; + payload[payloadSize] = save; - return text; + return text; +} + +String NdefRecord::getBluetoothName() { + String bluetoothName = ""; + + for (unsigned int i = 10; i < payloadSize; i++) { + if (payload[i] == 0x04) { + break; + } + bluetoothName += (char)payload[i]; + } + + return bluetoothName; } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index f3e50da..2bebc1f 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -18,9 +18,10 @@ class NdefRecord { bool isEmpty(); bool isNotEmpty(); NdefRecordType_e getType(); - unsigned char getPayload(); + unsigned char *getPayload(); unsigned short getPayloadSize(); String getText(); + String getBluetoothName(); }; #endif \ No newline at end of file From 5884c5ef06806ab4dd672a6215dfeb00da7c669d Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 15:58:41 -0600 Subject: [PATCH 080/106] feat: add get bluetooth address --- examples/NDEFRead/NDEFRead.ino | 16 +++------------- src/NdefRecord.cpp | 35 +++++++++++++++++++++++++++++++++- src/NdefRecord.h | 2 ++ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 6659af5..2e811f7 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -168,7 +168,6 @@ void displayRecordInfo(NdefRecord record) { unsigned char save; String SSID; - String bluetoothName; String bluetoothAddress; Serial.println("--- NDEF record received:"); @@ -239,19 +238,10 @@ void displayRecordInfo(NdefRecord record) { case MEDIA_HANDOVER_BT: Serial.println("\tBluetooth handover"); Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); - Serial.print("\t- Bluetooth Handover payload = "); + Serial.print("\t- Payload = "); Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); - Serial.print("\t- Bluetooth name: '" + record.getBluetoothName() + "'"); - - // Serial.print("- Bluetooth address: '"); - // bluetoothAddress = ""; - // for (unsigned int i = 7; i >= 2; i--) { - // bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1); - // if (i > 2) { - // bluetoothAddress += ":"; - // } - // } - // Serial.println(bluetoothAddress + "'"); + Serial.println("\t- Bluetooth name: " + record.getBluetoothName()); + Serial.println("\t- Bluetooth address: " + record.getBluetoothAddress()); break; // case MEDIA_HANDOVER_BLE: diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index fa9adc9..ab6cc73 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -12,6 +12,26 @@ void NdefRecord::create(NdefRecord_t record) { this->payloadSize = record.recordPayloadSize; } +String NdefRecord::getHexRepresentation(const byte *data, const uint32_t dataSize) { + String hexString; + + if (dataSize == 0) { + hexString = "null"; + } + + for (uint32_t index = 0; index < dataSize; index++) { + if (data[index] <= 0xF) + hexString += "0"; + String hexValue = String(data[index] & 0xFF, HEX); + hexValue.toUpperCase(); + hexString += hexValue; + if ((dataSize > 1) && (index != dataSize - 1)) { + hexString += ":"; + } + } + return hexString; +} + bool NdefRecord::isEmpty() { return this->payload == NULL; } @@ -48,7 +68,7 @@ String NdefRecord::getText() { String NdefRecord::getBluetoothName() { String bluetoothName = ""; - + for (unsigned int i = 10; i < payloadSize; i++) { if (payload[i] == 0x04) { break; @@ -58,3 +78,16 @@ String NdefRecord::getBluetoothName() { return bluetoothName; } + +String NdefRecord::getBluetoothAddress() { + String bluetoothAddress = ""; + + for (unsigned int i = 7; i >= 2; i--) { + bluetoothAddress += getHexRepresentation(&payload[i], 1); + if (i > 2) { + bluetoothAddress += ":"; + } + } + + return bluetoothAddress; +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 2bebc1f..d75aec5 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -11,6 +11,7 @@ class NdefRecord { NdefRecordType_e type; unsigned char *payload; unsigned short payloadSize; + String getHexRepresentation(const byte *data, const uint32_t dataSize); public: NdefRecord(); @@ -22,6 +23,7 @@ class NdefRecord { unsigned short getPayloadSize(); String getText(); String getBluetoothName(); + String getBluetoothAddress(); }; #endif \ No newline at end of file From 299a95af6d60f79fa3983ca4a2d65ff9d9145563 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 16:44:35 -0600 Subject: [PATCH 081/106] feat: enable ble handovers --- examples/NDEFRead/NDEFRead.ino | 36 ++++++++++++++++------------------ src/NdefRecord.cpp | 8 ++++++++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 2e811f7..7a42c20 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -166,9 +166,6 @@ void displayRecordInfo(NdefRecord record) { return; } - unsigned char save; - String SSID; - String bluetoothAddress; Serial.println("--- NDEF record received:"); switch (record.getType()) { @@ -235,24 +232,25 @@ void displayRecordInfo(NdefRecord record) { // Serial.println(record.recordPayload[0] & 0xF); // break; - case MEDIA_HANDOVER_BT: - Serial.println("\tBluetooth handover"); - Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); - Serial.print("\t- Payload = "); - Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); - Serial.println("\t- Bluetooth name: " + record.getBluetoothName()); - Serial.println("\t- Bluetooth address: " + record.getBluetoothAddress()); - break; + case MEDIA_HANDOVER_BT: + Serial.println("\tBluetooth handover"); + Serial.println("\t- Bluetooth name: " + record.getBluetoothName()); + Serial.println("\t- Bluetooth address: " + record.getBluetoothAddress()); + break; - // case MEDIA_HANDOVER_BLE: - // Serial.print("- BLE Handover payload = "); - // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - // break; + case MEDIA_HANDOVER_BLE: + Serial.print("\tBLE Handover"); + Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); + Serial.print("\t- Payload = "); + Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); + break; - // case MEDIA_HANDOVER_BLE_SECURE: - // Serial.print("- BLE secure Handover payload = "); - // Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize)); - // break; + case MEDIA_HANDOVER_BLE_SECURE: + Serial.print("\tBLE secure Handover"); + Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); + Serial.print("\t- Payload = "); + Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); + break; default: Serial.println("\tUnsupported NDEF record, cannot parse"); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index ab6cc73..d93fa87 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -69,6 +69,10 @@ String NdefRecord::getText() { String NdefRecord::getBluetoothName() { String bluetoothName = ""; + if (getType() != MEDIA_HANDOVER_BT) { + return bluetoothName; + } + for (unsigned int i = 10; i < payloadSize; i++) { if (payload[i] == 0x04) { break; @@ -82,6 +86,10 @@ String NdefRecord::getBluetoothName() { String NdefRecord::getBluetoothAddress() { String bluetoothAddress = ""; + if (getType() != MEDIA_HANDOVER_BT) { + return bluetoothAddress; + } + for (unsigned int i = 7; i >= 2; i--) { bluetoothAddress += getHexRepresentation(&payload[i], 1); if (i > 2) { From d66d913d6abb8b3c05fd4526d85c93d958eb580b Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 17:29:53 -0600 Subject: [PATCH 082/106] feat: add get wifi ssid --- examples/NDEFRead/NDEFRead.ino | 59 +++++++++++++++++----------------- src/NdefRecord.cpp | 31 ++++++++++++++---- src/NdefRecord.h | 1 + 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 7a42c20..fc225e8 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -190,35 +190,36 @@ void displayRecordInfo(NdefRecord record) { // record.recordPayload[record.recordPayloadSize] = save; // } break; - // case MEDIA_HANDOVER_WIFI: { - // unsigned char index = 0, i; - - // Serial.println("--- Received WIFI credentials:"); - // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) - // index += 4; - // while (index < record.recordPayloadSize) { - // if (record.recordPayload[index] == 0x10) { - // if (record.recordPayload[index + 1] == 0x45) { - // Serial.print("- SSID = "); - // Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); - // Serial.println(SSID); - // } else if (record.recordPayload[index + 1] == 0x03) { - // Serial.print("- Authenticate Type = "); - // Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); - // } else if (record.recordPayload[index + 1] == 0x0f) { - // Serial.print("- Encryption Type = "); - // Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); - // } else if (record.recordPayload[index + 1] == 0x27) { - // Serial.print("- Network key = "); - // Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); - // Serial.print("- Network key = "); - // Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); - // } - // index += 4 + record.recordPayload[index + 3]; - // } else - // continue; - // } - // } break; + case MEDIA_HANDOVER_WIFI: { + Serial.println("\tReceived WIFI credentials:"); + Serial.println("\t- SSID: " + record.getWiFiSSID()); + // unsigned char index = 0, i; + + // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) + // index += 4; + // while (index < record.recordPayloadSize) { + // if (record.recordPayload[index] == 0x10) { + // if (record.recordPayload[index + 1] == 0x45) { + // Serial.print("- SSID = "); + // Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); + // Serial.println(SSID); + // } else if (record.recordPayload[index + 1] == 0x03) { + // Serial.print("- Authenticate Type = "); + // Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); + // } else if (record.recordPayload[index + 1] == 0x0f) { + // Serial.print("- Encryption Type = "); + // Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); + // } else if (record.recordPayload[index + 1] == 0x27) { + // Serial.print("- Network key = "); + // Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); + // Serial.print("- Network key = "); + // Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); + // } + // index += 4 + record.recordPayload[index + 3]; + // } else + // continue; + // } + } break; // case WELL_KNOWN_HANDOVER_SELECT: // Serial.print("Handover select version "); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index d93fa87..8da529f 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -13,7 +13,7 @@ void NdefRecord::create(NdefRecord_t record) { } String NdefRecord::getHexRepresentation(const byte *data, const uint32_t dataSize) { - String hexString; + String hexString; if (dataSize == 0) { hexString = "null"; @@ -69,9 +69,9 @@ String NdefRecord::getText() { String NdefRecord::getBluetoothName() { String bluetoothName = ""; - if (getType() != MEDIA_HANDOVER_BT) { - return bluetoothName; - } + if (getType() != MEDIA_HANDOVER_BT) { + return bluetoothName; + } for (unsigned int i = 10; i < payloadSize; i++) { if (payload[i] == 0x04) { @@ -86,9 +86,9 @@ String NdefRecord::getBluetoothName() { String NdefRecord::getBluetoothAddress() { String bluetoothAddress = ""; - if (getType() != MEDIA_HANDOVER_BT) { - return bluetoothAddress; - } + if (getType() != MEDIA_HANDOVER_BT) { + return bluetoothAddress; + } for (unsigned int i = 7; i >= 2; i--) { bluetoothAddress += getHexRepresentation(&payload[i], 1); @@ -99,3 +99,20 @@ String NdefRecord::getBluetoothAddress() { return bluetoothAddress; } + +String NdefRecord::getWiFiSSID() { + String ssid = ""; + + if (getType() != MEDIA_HANDOVER_WIFI) { + return ssid; + } + + for (unsigned int i = 0; i < payloadSize; i++) { + if (payload[i] == 0x45) { + ssid = reinterpret_cast(&payload[i + 3]); + break; + } + } + + return ssid; +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h index d75aec5..b36d225 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -24,6 +24,7 @@ class NdefRecord { String getText(); String getBluetoothName(); String getBluetoothAddress(); + String getWiFiSSID(); }; #endif \ No newline at end of file From 9d740e582e391c5d82191c976d8266160fb18824 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 23 Aug 2023 17:43:17 -0600 Subject: [PATCH 083/106] feat: add get authentication type --- examples/NDEFRead/NDEFRead.ino | 1 + src/NdefRecord.cpp | 17 +++++++++++++++++ src/NdefRecord.h | 1 + 3 files changed, 19 insertions(+) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index fc225e8..a07e862 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -193,6 +193,7 @@ void displayRecordInfo(NdefRecord record) { case MEDIA_HANDOVER_WIFI: { Serial.println("\tReceived WIFI credentials:"); Serial.println("\t- SSID: " + record.getWiFiSSID()); + Serial.println("\t- Authentication type: " + record.getWiFiAuthenticationType()); // unsigned char index = 0, i; // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 8da529f..e319419 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -116,3 +116,20 @@ String NdefRecord::getWiFiSSID() { return ssid; } + +String NdefRecord::getWiFiAuthenticationType() { + String authenticationType = ""; + + if (getType() != MEDIA_HANDOVER_WIFI) { + return authenticationType; + } + + for (unsigned int i = 0; i < payloadSize; i++) { + if (payload[i] == 0x03) { + authenticationType = ndef_helper_WifiAuth(payload[i + 2]); + break; + } + } + + return authenticationType; +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h index b36d225..0ad3a49 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -25,6 +25,7 @@ class NdefRecord { String getBluetoothName(); String getBluetoothAddress(); String getWiFiSSID(); + String getWiFiAuthenticationType(); // TODO: not working properly }; #endif \ No newline at end of file From c052398c00223d846600ffa771af5ed645bc3b66 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 24 Aug 2023 09:18:11 -0600 Subject: [PATCH 084/106] feat: add all wifi credentials --- examples/NDEFRead/NDEFRead.ino | 65 +++++++++++++++-------------- src/NdefRecord.cpp | 74 +++++++++++++++++++++++++++++----- src/NdefRecord.h | 4 +- 3 files changed, 100 insertions(+), 43 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index a07e862..d794113 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -190,37 +190,40 @@ void displayRecordInfo(NdefRecord record) { // record.recordPayload[record.recordPayloadSize] = save; // } break; - case MEDIA_HANDOVER_WIFI: { - Serial.println("\tReceived WIFI credentials:"); - Serial.println("\t- SSID: " + record.getWiFiSSID()); - Serial.println("\t- Authentication type: " + record.getWiFiAuthenticationType()); - // unsigned char index = 0, i; - - // if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e)) - // index += 4; - // while (index < record.recordPayloadSize) { - // if (record.recordPayload[index] == 0x10) { - // if (record.recordPayload[index + 1] == 0x45) { - // Serial.print("- SSID = "); - // Serial.println(reinterpret_cast(&record.recordPayload[index + 4 + 0])); - // Serial.println(SSID); - // } else if (record.recordPayload[index + 1] == 0x03) { - // Serial.print("- Authenticate Type = "); - // Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5])); - // } else if (record.recordPayload[index + 1] == 0x0f) { - // Serial.print("- Encryption Type = "); - // Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5])); - // } else if (record.recordPayload[index + 1] == 0x27) { - // Serial.print("- Network key = "); - // Serial.println(reinterpret_cast(&record.recordPayload[index + 4])); - // Serial.print("- Network key = "); - // Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3])); - // } - // index += 4 + record.recordPayload[index + 3]; - // } else - // continue; - // } - } break; + case MEDIA_HANDOVER_WIFI: { + Serial.println("\tReceived WIFI credentials:"); + Serial.println("\t- SSID: " + record.getWiFiSSID()); + Serial.println("\t- Network key: " + record.getWiFiNetworkKey()); + Serial.println("\t- Authentication type: " + record.getWiFiAuthenticationType()); + Serial.println("\t- Encryption type: " + record.getWiFiEncryptionType()); + // Serial.println("Old method:"); + // unsigned char index = 0, i; + + // if ((record.getPayload()[index] == 0x10) && (record.getPayload()[index + 1] == 0x0e)) + // index += 4; + // while (index < record.getPayloadSize()) { + // if (record.getPayload()[index] == 0x10) { + // if (record.getPayload()[index + 1] == 0x45) { + // Serial.print("- SSID = "); + // Serial.println(reinterpret_cast(&record.getPayload()[index + 4 + 0])); + // // Serial.println(SSID); + // } else if (record.getPayload()[index + 1] == 0x03) { + // Serial.print("- Authenticate Type = "); + // Serial.println(ndef_helper_WifiAuth(record.getPayload()[index + 5])); + // } else if (record.getPayload()[index + 1] == 0x0f) { + // Serial.print("- Encryption Type = "); + // Serial.println(ndef_helper_WifiEnc(record.getPayload()[index + 5])); + // } else if (record.getPayload()[index + 1] == 0x27) { + // Serial.print("- Network key = "); + // Serial.println(reinterpret_cast(&record.getPayload()[index + 4])); + // Serial.print("- Network key = "); + // Serial.println(getHexRepresentation(&record.getPayload()[index + 4], record.getPayload()[index + 3])); + // } + // index += 4 + record.getPayload()[index + 3]; + // } else + // continue; + // } + } break; // case WELL_KNOWN_HANDOVER_SELECT: // Serial.print("Handover select version "); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index e319419..fe0aa3a 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -56,6 +56,7 @@ String NdefRecord::getText() { unsigned char save = payload[payloadSize]; payload[payloadSize] = '\0'; String text = ""; + Serial.println("here"); if (getType() == WELL_KNOWN_SIMPLE_TEXT) { text = reinterpret_cast(&payload[payload[0] + 1]); @@ -118,18 +119,69 @@ String NdefRecord::getWiFiSSID() { } String NdefRecord::getWiFiAuthenticationType() { - String authenticationType = ""; + String authenticationType = ""; + unsigned char index = 0, i; - if (getType() != MEDIA_HANDOVER_WIFI) { - return authenticationType; - } + if (getType() != MEDIA_HANDOVER_WIFI) { + return authenticationType; + } + + if ((getPayload()[index] == 0x10) && (getPayload()[index + 1] == 0x0e)) { + index += 4; + } + + while (index < getPayloadSize()) { + if (getPayload()[index] == 0x10) { + if (getPayload()[index + 1] == 0x03) { + authenticationType = ndef_helper_WifiAuth(getPayload()[index + 5]); + } + index += 4 + getPayload()[index + 3]; + } + } + + return authenticationType; +} + +String NdefRecord::getWiFiEncryptionType() { + String encryptionType = ""; + unsigned char index = 0, i; + + if (getType() != MEDIA_HANDOVER_WIFI) { + return encryptionType; + } - for (unsigned int i = 0; i < payloadSize; i++) { - if (payload[i] == 0x03) { - authenticationType = ndef_helper_WifiAuth(payload[i + 2]); - break; - } - } + if ((getPayload()[index] == 0x10) && (getPayload()[index + 1] == 0x0e)) { + index += 4; + } - return authenticationType; + while (index < getPayloadSize()) { + if (getPayload()[index] == 0x10) { + if (getPayload()[index + 1] == 0x0f) { + encryptionType = ndef_helper_WifiEnc(getPayload()[index + 5]); + } + index += 4 + getPayload()[index + 3]; + } + } +} + +String NdefRecord::getWiFiNetworkKey() { + String networkKey = ""; + unsigned char index = 0, i; + + if (getType() != MEDIA_HANDOVER_WIFI) { + return networkKey; + } + + if ((getPayload()[index] == 0x10) && (getPayload()[index + 1] == 0x0e)) { + index += 4; + } + + while (index < getPayloadSize()) { + if (getPayload()[index] == 0x10) { + if (getPayload()[index + 1] == 0x27) { + networkKey = reinterpret_cast(&getPayload()[index + 4]); + } + index += 4 + getPayload()[index + 3]; + } + } } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 0ad3a49..9f618e5 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -25,7 +25,9 @@ class NdefRecord { String getBluetoothName(); String getBluetoothAddress(); String getWiFiSSID(); - String getWiFiAuthenticationType(); // TODO: not working properly + String getWiFiAuthenticationType(); + String getWiFiEncryptionType(); + String getWiFiNetworkKey(); }; #endif \ No newline at end of file From 857da18811ab478add1c3bf5d719b0dc99b43492 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 24 Aug 2023 09:34:15 -0600 Subject: [PATCH 085/106] feat: get handover version --- examples/NDEFRead/NDEFRead.ino | 47 +++++++--------------------------- src/NdefRecord.cpp | 1 - 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index d794113..2a7e5e0 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -196,46 +196,17 @@ void displayRecordInfo(NdefRecord record) { Serial.println("\t- Network key: " + record.getWiFiNetworkKey()); Serial.println("\t- Authentication type: " + record.getWiFiAuthenticationType()); Serial.println("\t- Encryption type: " + record.getWiFiEncryptionType()); - // Serial.println("Old method:"); - // unsigned char index = 0, i; - - // if ((record.getPayload()[index] == 0x10) && (record.getPayload()[index + 1] == 0x0e)) - // index += 4; - // while (index < record.getPayloadSize()) { - // if (record.getPayload()[index] == 0x10) { - // if (record.getPayload()[index + 1] == 0x45) { - // Serial.print("- SSID = "); - // Serial.println(reinterpret_cast(&record.getPayload()[index + 4 + 0])); - // // Serial.println(SSID); - // } else if (record.getPayload()[index + 1] == 0x03) { - // Serial.print("- Authenticate Type = "); - // Serial.println(ndef_helper_WifiAuth(record.getPayload()[index + 5])); - // } else if (record.getPayload()[index + 1] == 0x0f) { - // Serial.print("- Encryption Type = "); - // Serial.println(ndef_helper_WifiEnc(record.getPayload()[index + 5])); - // } else if (record.getPayload()[index + 1] == 0x27) { - // Serial.print("- Network key = "); - // Serial.println(reinterpret_cast(&record.getPayload()[index + 4])); - // Serial.print("- Network key = "); - // Serial.println(getHexRepresentation(&record.getPayload()[index + 4], record.getPayload()[index + 3])); - // } - // index += 4 + record.getPayload()[index + 3]; - // } else - // continue; - // } } break; - // case WELL_KNOWN_HANDOVER_SELECT: - // Serial.print("Handover select version "); - // Serial.print(record.recordPayload[0] >> 4); - // Serial.println(record.recordPayload[0] & 0xF); - // break; - - // case WELL_KNOWN_HANDOVER_REQUEST: - // Serial.print("Handover request version "); - // Serial.print(record.recordPayload[0] >> 4); - // Serial.println(record.recordPayload[0] & 0xF); - // break; + case WELL_KNOWN_HANDOVER_SELECT: + Serial.print("\tHandover select version: "); + Serial.print(String(record.getPayload() >> 4) + "." + String(record.getPayload() & 0xF)); + break; + + case WELL_KNOWN_HANDOVER_REQUEST: + Serial.print("\tHandover request version: "); + Serial.print(String(record.getPayload() >> 4) + "." + String(record.getPayload() & 0xF)); + break; case MEDIA_HANDOVER_BT: Serial.println("\tBluetooth handover"); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index fe0aa3a..aae480b 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -56,7 +56,6 @@ String NdefRecord::getText() { unsigned char save = payload[payloadSize]; payload[payloadSize] = '\0'; String text = ""; - Serial.println("here"); if (getType() == WELL_KNOWN_SIMPLE_TEXT) { text = reinterpret_cast(&payload[payload[0] + 1]); From d847e633ceb0147ba36a13c203b8c182f18c8d82 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 24 Aug 2023 12:28:33 -0600 Subject: [PATCH 086/106] fix: handover version --- examples/MifareClassic_write_block/Makefile | 21 +++++++++++++++++++++ examples/NDEFRead/Makefile | 1 + examples/NDEFRead/NDEFRead.ino | 21 +++++++++++++-------- src/NdefRecord.cpp | 8 ++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 examples/MifareClassic_write_block/Makefile diff --git a/examples/MifareClassic_write_block/Makefile b/examples/MifareClassic_write_block/Makefile new file mode 100644 index 0000000..b401ac6 --- /dev/null +++ b/examples/MifareClassic_write_block/Makefile @@ -0,0 +1,21 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +# BOARD_TAG = arduino:mbed_rp2040:pico +# BOARD_TAG = rp2040:rp2040:generic +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/NDEFRead/Makefile b/examples/NDEFRead/Makefile index 4ab2c77..bdfe399 100644 --- a/examples/NDEFRead/Makefile +++ b/examples/NDEFRead/Makefile @@ -1,4 +1,5 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat +# BOARD_TAG = rp2040:rp2040:generic MONITOR_PORT = /dev/cu.usbmodem1101 compile: diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 2a7e5e0..e1557c4 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -166,6 +166,7 @@ void displayRecordInfo(NdefRecord record) { return; } + uint8_t *payload = record.getPayload(); Serial.println("--- NDEF record received:"); switch (record.getType()) { @@ -198,15 +199,19 @@ void displayRecordInfo(NdefRecord record) { Serial.println("\t- Encryption type: " + record.getWiFiEncryptionType()); } break; - case WELL_KNOWN_HANDOVER_SELECT: - Serial.print("\tHandover select version: "); - Serial.print(String(record.getPayload() >> 4) + "." + String(record.getPayload() & 0xF)); - break; + case WELL_KNOWN_HANDOVER_SELECT: + Serial.print("\tHandover select version: "); + Serial.print(*payload >> 4); + Serial.print("."); + Serial.println(*payload & 0xF); + break; - case WELL_KNOWN_HANDOVER_REQUEST: - Serial.print("\tHandover request version: "); - Serial.print(String(record.getPayload() >> 4) + "." + String(record.getPayload() & 0xF)); - break; + case WELL_KNOWN_HANDOVER_REQUEST: + Serial.print("\tHandover request version: "); + Serial.print(*payload >> 4); + Serial.print("."); + Serial.println(*payload & 0xF); + break; case MEDIA_HANDOVER_BT: Serial.println("\tBluetooth handover"); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index aae480b..2a79d0f 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -53,6 +53,9 @@ unsigned short NdefRecord::getPayloadSize() { } String NdefRecord::getText() { + Serial.println("here1"); + Serial.println(payloadSize); + Serial.println(sizeof(payload)); unsigned char save = payload[payloadSize]; payload[payloadSize] = '\0'; String text = ""; @@ -62,6 +65,7 @@ String NdefRecord::getText() { } payload[payloadSize] = save; + Serial.println("here2"); return text; } @@ -161,6 +165,8 @@ String NdefRecord::getWiFiEncryptionType() { index += 4 + getPayload()[index + 3]; } } + + return encryptionType; } String NdefRecord::getWiFiNetworkKey() { @@ -183,4 +189,6 @@ String NdefRecord::getWiFiNetworkKey() { index += 4 + getPayload()[index + 3]; } } + + return networkKey; } From bd86a95202c39107342c622a9de6331c5734837b Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 24 Aug 2023 17:20:04 -0600 Subject: [PATCH 087/106] feat: get vcard content --- examples/NDEFRead/NDEFRead.ino | 14 +++++++------- src/NdefRecord.cpp | 31 ++++++++++++++++++++----------- src/NdefRecord.h | 2 ++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index e1557c4..f5fb02b 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -163,6 +163,7 @@ void displayDeviceInfo() { void displayRecordInfo(NdefRecord record) { if (record.isEmpty()) { + Serial.println("No more records, exiting..."); return; } @@ -170,13 +171,12 @@ void displayRecordInfo(NdefRecord record) { Serial.println("--- NDEF record received:"); switch (record.getType()) { - // case MEDIA_VCARD: { - // save = record.recordPayload[record.recordPayloadSize]; - // record.recordPayload[record.recordPayloadSize] = '\0'; - // Serial.print("vCard:"); - // Serial.println(reinterpret_cast(record.recordPayload)); - // record.recordPayload[record.recordPayloadSize] = save; - // } break; + case MEDIA_VCARD: { + unsigned char save = record.getPayload()[record.getPayloadSize()]; + record.getPayload()[record.getPayloadSize()] = '\0'; + Serial.println("vCard:"); + Serial.println(record.getVCardContent()); + } break; case WELL_KNOWN_SIMPLE_TEXT: { Serial.println("\tWell known simple text"); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 2a79d0f..de534ae 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -4,6 +4,7 @@ NdefRecord::NdefRecord() { type = UNSUPPORTED_NDEF_RECORD; payload = NULL; payloadSize = 0; + newString = "null"; } void NdefRecord::create(NdefRecord_t record) { @@ -53,25 +54,21 @@ unsigned short NdefRecord::getPayloadSize() { } String NdefRecord::getText() { - Serial.println("here1"); - Serial.println(payloadSize); - Serial.println(sizeof(payload)); unsigned char save = payload[payloadSize]; payload[payloadSize] = '\0'; - String text = ""; + String text = newString; if (getType() == WELL_KNOWN_SIMPLE_TEXT) { text = reinterpret_cast(&payload[payload[0] + 1]); } payload[payloadSize] = save; - Serial.println("here2"); return text; } String NdefRecord::getBluetoothName() { - String bluetoothName = ""; + String bluetoothName = newString; if (getType() != MEDIA_HANDOVER_BT) { return bluetoothName; @@ -88,7 +85,7 @@ String NdefRecord::getBluetoothName() { } String NdefRecord::getBluetoothAddress() { - String bluetoothAddress = ""; + String bluetoothAddress = newString; if (getType() != MEDIA_HANDOVER_BT) { return bluetoothAddress; @@ -105,7 +102,7 @@ String NdefRecord::getBluetoothAddress() { } String NdefRecord::getWiFiSSID() { - String ssid = ""; + String ssid = newString; if (getType() != MEDIA_HANDOVER_WIFI) { return ssid; @@ -122,7 +119,7 @@ String NdefRecord::getWiFiSSID() { } String NdefRecord::getWiFiAuthenticationType() { - String authenticationType = ""; + String authenticationType = newString; unsigned char index = 0, i; if (getType() != MEDIA_HANDOVER_WIFI) { @@ -146,7 +143,7 @@ String NdefRecord::getWiFiAuthenticationType() { } String NdefRecord::getWiFiEncryptionType() { - String encryptionType = ""; + String encryptionType = newString; unsigned char index = 0, i; if (getType() != MEDIA_HANDOVER_WIFI) { @@ -170,7 +167,7 @@ String NdefRecord::getWiFiEncryptionType() { } String NdefRecord::getWiFiNetworkKey() { - String networkKey = ""; + String networkKey = newString; unsigned char index = 0, i; if (getType() != MEDIA_HANDOVER_WIFI) { @@ -192,3 +189,15 @@ String NdefRecord::getWiFiNetworkKey() { return networkKey; } + +String NdefRecord::getVCardContent() { + String content = newString; + + if (getType() != MEDIA_VCARD) { + return content; + } + + content = reinterpret_cast(getPayload()); + + return content; +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 9f618e5..0fddcab 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -12,6 +12,7 @@ class NdefRecord { unsigned char *payload; unsigned short payloadSize; String getHexRepresentation(const byte *data, const uint32_t dataSize); + String newString; public: NdefRecord(); @@ -28,6 +29,7 @@ class NdefRecord { String getWiFiAuthenticationType(); String getWiFiEncryptionType(); String getWiFiNetworkKey(); + String getVCardContent(); }; #endif \ No newline at end of file From 25bfa88eba414bf98ae589c092463e580c827276 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 25 Aug 2023 08:56:00 -0600 Subject: [PATCH 088/106] feat: get simple uri content --- examples/NDEFRead/NDEFRead.ino | 67 ++++++++++++++++------------------ src/NdefRecord.cpp | 29 ++++++++++++--- src/NdefRecord.h | 2 +- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index f5fb02b..f1d7676 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -4,10 +4,10 @@ #define PN7150_ADDR (0x28) // Function prototypes +void ndefCallback(); String getHexRepresentation(const byte *data, const uint32_t dataSize); void displayDeviceInfo(); -void displayRecordInfo(NdefRecord_t record); -void ndefCallback(); +void displayRecordInfo(NdefRecord record); // Create a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); @@ -69,6 +69,25 @@ void loop() { delay(500); } +/// @brief Callback function called when an NDEF message is received +void ndefCallback() { + NdefRecord record; + Serial.println("Processing Callback..."); + + // message is automatically updated when a new NDEF message is received + // only if we call message.begin() in setup() + if (message.isEmpty()) { + Serial.println("--- Provisioned buffer size too small or NDEF message empty"); + return; + } + + // Show NDEF message details + do { + record.create(message.getRecord()); // Get a new record every time we call getRecord() + displayRecordInfo(record); + } while (record.isNotEmpty()); +} + String getHexRepresentation(const byte *data, const uint32_t dataSize) { String hexString; @@ -171,33 +190,29 @@ void displayRecordInfo(NdefRecord record) { Serial.println("--- NDEF record received:"); switch (record.getType()) { - case MEDIA_VCARD: { - unsigned char save = record.getPayload()[record.getPayloadSize()]; - record.getPayload()[record.getPayloadSize()] = '\0'; + case MEDIA_VCARD: Serial.println("vCard:"); Serial.println(record.getVCardContent()); - } break; + break; - case WELL_KNOWN_SIMPLE_TEXT: { + case WELL_KNOWN_SIMPLE_TEXT: Serial.println("\tWell known simple text"); Serial.println("\t- Text record: " + record.getText()); - } break; + break; - // case WELL_KNOWN_SIMPLE_URI: { - // save = record.recordPayload[record.recordPayloadSize]; - // record.recordPayload[record.recordPayloadSize] = '\0'; - // Serial.print("URI record: "); - // Serial.println(reinterpret_cast(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1])); - // record.recordPayload[record.recordPayloadSize] = save; - // } break; + case WELL_KNOWN_SIMPLE_URI: + Serial.println("\tWell known simple URI"); + Serial.print("\t- URI record: "); + Serial.println(record.getUri()); + break; - case MEDIA_HANDOVER_WIFI: { + case MEDIA_HANDOVER_WIFI: Serial.println("\tReceived WIFI credentials:"); Serial.println("\t- SSID: " + record.getWiFiSSID()); Serial.println("\t- Network key: " + record.getWiFiNetworkKey()); Serial.println("\t- Authentication type: " + record.getWiFiAuthenticationType()); Serial.println("\t- Encryption type: " + record.getWiFiEncryptionType()); - } break; + break; case WELL_KNOWN_HANDOVER_SELECT: Serial.print("\tHandover select version: "); @@ -240,21 +255,3 @@ void displayRecordInfo(NdefRecord record) { Serial.println(""); } - -/// @brief Callback function called when an NDEF message is received -void ndefCallback() { - NdefRecord record; - Serial.println("Processing Callback..."); - - // message is automatically updated when a new NDEF message is received - // only if we call message.begin() in setup() - if (message.isEmpty()) { - Serial.println("--- Provisioned buffer size too small or NDEF message empty"); - return; - } - - do { - record.create(message.getRecord()); // Get a new record every time we call getRecord() - displayRecordInfo(record); - } while (record.isNotEmpty()); -} diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index de534ae..494aaf9 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -1,10 +1,10 @@ #include "NdefRecord.h" NdefRecord::NdefRecord() { - type = UNSUPPORTED_NDEF_RECORD; - payload = NULL; - payloadSize = 0; - newString = "null"; + this->type = UNSUPPORTED_NDEF_RECORD; + this->payload = NULL; + this->payloadSize = 0; + this->newString = "null"; } void NdefRecord::create(NdefRecord_t record) { @@ -17,7 +17,7 @@ String NdefRecord::getHexRepresentation(const byte *data, const uint32_t dataSiz String hexString; if (dataSize == 0) { - hexString = "null"; + hexString = newString; } for (uint32_t index = 0; index < dataSize; index++) { @@ -72,6 +72,8 @@ String NdefRecord::getBluetoothName() { if (getType() != MEDIA_HANDOVER_BT) { return bluetoothName; + } else { + bluetoothName = ""; } for (unsigned int i = 10; i < payloadSize; i++) { @@ -89,6 +91,8 @@ String NdefRecord::getBluetoothAddress() { if (getType() != MEDIA_HANDOVER_BT) { return bluetoothAddress; + } else { + bluetoothAddress = ""; } for (unsigned int i = 7; i >= 2; i--) { @@ -201,3 +205,18 @@ String NdefRecord::getVCardContent() { return content; } + +String NdefRecord::getUri() { + String uri = newString; + + if (getType() != WELL_KNOWN_SIMPLE_URI) { + return uri; + } + + unsigned char save = payload[payloadSize]; + payload[payloadSize] = '\0'; + uri = reinterpret_cast(ndef_helper_UriHead(payload[0]), &payload[1]); + payload[payloadSize] = save; + + return uri; +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 0fddcab..e364148 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -2,7 +2,6 @@ #define NdefRecord_H #include - #include "ndef_helper.h" class NdefRecord { @@ -30,6 +29,7 @@ class NdefRecord { String getWiFiEncryptionType(); String getWiFiNetworkKey(); String getVCardContent(); + String getUri(); }; #endif \ No newline at end of file From 4fd277321ec49b59dcb5f4d98318904606635e5f Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 25 Aug 2023 18:12:02 -0600 Subject: [PATCH 089/106] docs: add comment headers --- examples/DetectTags/DetectTags.ino | 2 + .../DetectingReaders/DetectingReaders.ino | 73 +++++++------------ examples/DetectingReaders/Makefile | 19 +++++ .../DetectingReadersOld.ino | 71 ++++++++++++++++++ examples/DetectingReadersOld/Makefile | 19 +++++ examples/NDEFRead/NDEFRead.ino | 14 ++++ examples/NDEFSend/NDEFSend.ino | 18 ++++- src/Electroniccats_PN7150.cpp | 2 +- src/Electroniccats_PN7150.h | 6 +- src/Interface.h | 13 ++++ src/Mode.cpp | 13 ++++ src/Mode.h | 13 ++++ src/ModeTech.h | 13 ++++ src/NdefMessage.cpp | 13 ++++ src/NdefMessage.h | 13 ++++ src/NdefRecord.cpp | 13 ++++ src/NdefRecord.h | 13 ++++ src/Protocol.h | 13 ++++ src/RemoteDevice.cpp | 13 ++++ src/RemoteDevice.h | 13 ++++ src/Tech.h | 13 ++++ 21 files changed, 326 insertions(+), 54 deletions(-) create mode 100644 examples/DetectingReaders/Makefile create mode 100644 examples/DetectingReadersOld/DetectingReadersOld.ino create mode 100644 examples/DetectingReadersOld/Makefile diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 0131910..906bd82 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -3,6 +3,8 @@ * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com + * + * Updated by Francisco Torres - Electronic Cats - electroniccats.com * * March 2020 * diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index 1d8d589..4034e4d 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -12,59 +12,40 @@ * Distributed as-is; no warranty is given. */ -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) +#include -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) -unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Interface to save data for multiple tags -uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation - -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); - Serial.println("Detect NFC readers with PN7150"); - Serial.println("Initializing..."); + while (!Serial) + ; + Serial.println("Send NDEF Message with PN7150"); - if (nfc.connectNCI()) { //Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (1); - } - - if (nfc.configureSettings()) { - Serial.println("The Configure Settings is failed!"); - while (1); - } - - if(nfc.configMode(mode)){ //Set up the configuration mode - Serial.println("The Configure Mode is failed!!"); - while (1); + Serial.println("Initializing..."); + + if (nfc.begin()) { + Serial.println("Error initializing PN7150"); + while (1) + ; } - nfc.startDiscovery(mode); //NCI Discovery mode - Serial.println("Waiting for an Reader Card ..."); + + // Needed to detect readers + nfc.setEmulationMode(); + Serial.print("Waiting for a reader..."); } -void loop(){ - if(nfc.cardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? - if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes - switch (Cmd[1]) { - case 0xA4: //Something tries to select a file, meaning that it is a reader - Serial.println("Reader detected!"); - break; - - case 0xB0: //SFI - break; - - case 0xD0: //... - break; - - default: - break; - } - nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); - } +void loop() { + Serial.print("."); + + if (nfc.isReaderDetected()) { + nfc.sendMessage(); + Serial.println("\nReader detected!"); + Serial.print("\nWaiting for a reader"); } } diff --git a/examples/DetectingReaders/Makefile b/examples/DetectingReaders/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/DetectingReaders/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/DetectingReadersOld/DetectingReadersOld.ino b/examples/DetectingReadersOld/DetectingReadersOld.ino new file mode 100644 index 0000000..43c320e --- /dev/null +++ b/examples/DetectingReadersOld/DetectingReadersOld.ino @@ -0,0 +1,71 @@ +/** + * Example detect tags and show their unique ID + * Authors: + * Salvador Mendoza - @Netxing - salmg.net + * For Electronic Cats - electroniccats.com + * + * March 2020 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) + +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 + +unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; + +uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation + +void setup(){ + Serial.begin(9600); + while(!Serial); + Serial.println("Detect NFC readers with PN7150"); + Serial.println("Initializing..."); + + if (nfc.connectNCI()) { //Wake up the board + Serial.println("Error while setting up the mode, check connections!"); + while (1); + } + + if (nfc.configureSettings()) { + Serial.println("The Configure Settings is failed!"); + while (1); + } + + if(nfc.configMode(mode)){ //Set up the configuration mode + Serial.println("The Configure Mode is failed!!"); + while (1); + } + nfc.startDiscovery(mode); //NCI Discovery mode + Serial.println("Waiting for an Reader Card ..."); +} + +void loop(){ + Serial.print("."); + if(nfc.cardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? + if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes + switch (Cmd[1]) { + case 0xA4: //Something tries to select a file, meaning that it is a reader + Serial.println("Reader detected!"); + break; + + case 0xB0: //SFI + break; + + case 0xD0: //... + break; + + default: + break; + } + nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); + } + } +} diff --git a/examples/DetectingReadersOld/Makefile b/examples/DetectingReadersOld/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/DetectingReadersOld/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index f1d7676..30239e7 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -1,3 +1,17 @@ +/** + * Example to read NDEF messages + * Authors: + * Salvador Mendoza - @Netxing - salmg.net + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #include "Electroniccats_PN7150.h" #define PN7150_IRQ (11) #define PN7150_VEN (13) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 5960c66..7107381 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -1,3 +1,17 @@ +/** + * Example to send NDEF messages + * Authors: + * Salvador Mendoza - @Netxing - salmg.net + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #include #define PN7150_IRQ (11) @@ -5,14 +19,11 @@ #define PN7150_ADDR (0x28) // Function prototypes -void checkReaders(); void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 RfIntf_t RfInterface; // Interface to save data for multiple tags -unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; - const char uri[] = "google.com"; const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF @@ -53,7 +64,6 @@ void setup() { // Needed to detect readers nfc.setEmulationMode(); - Serial.print("Waiting for an NDEF device"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 1d7d822..12068ea 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -4,7 +4,7 @@ * Salvador Mendoza - @Netxing - salmg.net * Andres Sabas - Electronic Cats - Electroniccats.com * - * November 2020 + * August 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 31d2330..6a45029 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -2,11 +2,11 @@ #define Electroniccats_PN7150_H /** * NXP PN7150 Driver - * Porting uthors: + * Porting authors: * Salvador Mendoza - @Netxing - salmg.net - * Andres Sabas - Electronic Cats - Electroniccats.com + * Andres Sabas - Electronic Cats - electroniccats.com * - * November 2020 + * August 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, diff --git a/src/Interface.h b/src/Interface.h index ff04c5f..7bd1a73 100644 --- a/src/Interface.h +++ b/src/Interface.h @@ -1,3 +1,16 @@ +/** + * Library to get the interface type of the NFC card + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef Interface_H #define Interface_H diff --git a/src/Mode.cpp b/src/Mode.cpp index cd4c174..40b665f 100644 --- a/src/Mode.cpp +++ b/src/Mode.cpp @@ -1,3 +1,16 @@ +/** + * Library to manage the mode of the NFC chip + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #include "Mode.h" Mode::Mode() { diff --git a/src/Mode.h b/src/Mode.h index 319c5cf..70cad10 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -1,3 +1,16 @@ +/** + * Library to manage the mode of the NFC chip + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef Mode_H #define Mode_H diff --git a/src/ModeTech.h b/src/ModeTech.h index bc4d4fa..ad432b4 100644 --- a/src/ModeTech.h +++ b/src/ModeTech.h @@ -1,3 +1,16 @@ +/** + * Library to get the mode tech of the NFC card + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef ModeTech_H #define ModeTech_H diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 0bfc013..c7961ad 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -1,3 +1,16 @@ +/** + * Library to manage the NDEF message + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #include "NdefMessage.h" unsigned char *NdefMessage::content; diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 3002698..9db9dd6 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -1,3 +1,16 @@ +/** + * Library to manage the NDEF message + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef NdefMessage_H #define NdefMessage_H diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 494aaf9..bf788e1 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -1,3 +1,16 @@ +/** + * Library to manage the NDEF record + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #include "NdefRecord.h" NdefRecord::NdefRecord() { diff --git a/src/NdefRecord.h b/src/NdefRecord.h index e364148..5eddb0c 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -1,3 +1,16 @@ +/** + * Library to manage the NDEF record + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef NdefRecord_H #define NdefRecord_H diff --git a/src/Protocol.h b/src/Protocol.h index a5e41a3..eefefec 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -1,3 +1,16 @@ +/** + * Library to get the protocol of the NFC card + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef Protocol_H #define Protocol_H diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index d630404..29e9bcd 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -1,3 +1,16 @@ +/** + * Library to manage the remote device properties, a remote device can be a tag or a reader + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #include "RemoteDevice.h" unsigned char RemoteDevice::getInterface() const { diff --git a/src/RemoteDevice.h b/src/RemoteDevice.h index 39b7c36..a066ab0 100644 --- a/src/RemoteDevice.h +++ b/src/RemoteDevice.h @@ -1,3 +1,16 @@ +/** + * Library to manage the remote device properties, a remote device can be a tag or a reader + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef RemoteDevice_H #define RemoteDevice_H diff --git a/src/Tech.h b/src/Tech.h index 6757fbd..df91eaf 100644 --- a/src/Tech.h +++ b/src/Tech.h @@ -1,3 +1,16 @@ +/** + * Library to get the technology of the NFC card + * Authors: + * Francisco Torres - Electronic Cats - electroniccats.com + * + * August 2023 + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, + * please buy us a round! + * Distributed as-is; no warranty is given. + */ + #ifndef Technology_H #define Technology_H From 55da7eb208f299b35f78bf05663192d619cf100a Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 25 Aug 2023 18:24:37 -0600 Subject: [PATCH 090/106] feat: add handle card emulation --- examples/DetectingReaders/DetectingReaders.ino | 7 +++---- examples/NDEFSend/NDEFSend.ino | 1 - src/Electroniccats_PN7150.cpp | 8 ++++++-- src/Electroniccats_PN7150.h | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index 4034e4d..955ccfc 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -19,14 +19,12 @@ #define PN7150_ADDR (0x28) Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Interface to save data for multiple tags void setup() { Serial.begin(9600); while (!Serial) ; - Serial.println("Send NDEF Message with PN7150"); - + Serial.println("Detect NFC readers with PN7150"); Serial.println("Initializing..."); if (nfc.begin()) { @@ -44,8 +42,9 @@ void loop() { Serial.print("."); if (nfc.isReaderDetected()) { - nfc.sendMessage(); Serial.println("\nReader detected!"); + nfc.handleCardEmulation(); + nfc.closeCommunication(); Serial.print("\nWaiting for a reader"); } } diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 7107381..b0e7320 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -22,7 +22,6 @@ void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Interface to save data for multiple tags const char uri[] = "google.com"; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 12068ea..bc1af74 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1387,7 +1387,11 @@ void Electroniccats_PN7150::processCardMode(void) { Electroniccats_PN7150::processCardMode(this->dummyRfInterface); } -// Deprecated, use processCardMode(void) instead +void Electroniccats_PN7150::handleCardEmulation() { + Electroniccats_PN7150::processCardMode(); +} + +// Deprecated, use handleCardEmulation() instead void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { Electroniccats_PN7150::processCardMode(RfIntf); } @@ -1928,6 +1932,6 @@ void Electroniccats_PN7150::closeCommunication() { } void Electroniccats_PN7150::sendMessage() { - Electroniccats_PN7150::processCardMode(); + Electroniccats_PN7150::handleCardEmulation(); Electroniccats_PN7150::closeCommunication(); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 6a45029..76f6bdd 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -154,7 +154,8 @@ class Electroniccats_PN7150 : public Mode { bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead void processCardMode(RfIntf_t RfIntf); void processCardMode(void); - void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use processCardMode(void) instead + void handleCardEmulation(); + void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead void processP2pMode(RfIntf_t RfIntf); From 3c7b8c663e39557242e5e0156e839af228e1c479 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 28 Aug 2023 11:17:40 -0600 Subject: [PATCH 091/106] fix: warning alerts - unused variables - unused functions - memory overflow on wait for discovery notification --- examples/DetectTags/DetectTags.ino | 3 +- .../DetectingReaders/DetectingReaders.ino | 2 +- examples/NDEFRead/Makefile | 2 +- examples/NDEFSend/NDEFSend.ino | 11 +- src/Electroniccats_PN7150.cpp | 141 +----------------- src/Electroniccats_PN7150.h | 6 +- src/NdefRecord.cpp | 6 +- 7 files changed, 14 insertions(+), 157 deletions(-) diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 906bd82..7b0b586 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -55,8 +55,7 @@ void setup() { } void loop() { - // TODO: invert the logic to make it more readable - if (!nfc.waitForDiscoveryNotification()) { // Waiting to detect cards + if (nfc.isTagDetected()) { displayCardInfo(); // It can detect multiple cards at the same time if they use the same protocol diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index 955ccfc..b3a55fb 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -29,7 +29,7 @@ void setup() { if (nfc.begin()) { Serial.println("Error initializing PN7150"); - while (1) + while (true) ; } diff --git a/examples/NDEFRead/Makefile b/examples/NDEFRead/Makefile index bdfe399..67609a6 100644 --- a/examples/NDEFRead/Makefile +++ b/examples/NDEFRead/Makefile @@ -3,7 +3,7 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat MONITOR_PORT = /dev/cu.usbmodem1101 compile: - arduino-cli compile --fqbn $(BOARD_TAG) + arduino-cli compile --fqbn $(BOARD_TAG) --warnings all upload: arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index b0e7320..7726cc9 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -33,14 +33,6 @@ const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 'e', 'n', // Language 'H', 'e', 'l', 'l', 'o'}; // Message Payload -// const char ndefMessage[] = {0xD1, -// 0x01, -// sizeof(uri) + 1, -// 'U', -// 0x02, -// 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm'}; -// uri[0], uri[1], uri[2], uri[3], uri[4], uri[5], uri[6], uri[7], uri[8], uri[9], uri[10]}; - void setup() { Serial.begin(9600); while (!Serial) @@ -57,7 +49,7 @@ void setup() { if (nfc.begin()) { Serial.println("Error initializing PN7150"); - while (1) + while (true) ; } @@ -71,6 +63,7 @@ void loop() { if (nfc.isReaderDetected()) { Serial.println("\nReader detected!"); + Serial.println("Sending NDEF message..."); nfc.sendMessage(); Serial.print("\nWaiting for an NDEF device"); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index bc1af74..4929518 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -55,9 +55,6 @@ Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, } uint8_t Electroniccats_PN7150::begin() { - // Register callbacks - // registerUpdateNdefMessageCallback(Electroniccats_PN7150::updateNdefMessage); - _wire->begin(); if (_VENpin != 255) { digitalWrite(_VENpin, HIGH); @@ -89,138 +86,6 @@ uint8_t Electroniccats_PN7150::begin() { return SUCCESS; } -void Electroniccats_PN7150::updateNdefMessage(unsigned char *message, unsigned short messageSize) { - Serial.println("here :D"); - unsigned char *pNdefMessage = message; - NdefRecord_t NdefRecord; - unsigned char save; - String SSID; - String bluetoothName; - String bluetoothAddress; - - if (message == NULL) { - Serial.println("--- Provisioned buffer size too small or NDEF message empty"); - return; - } - - while (pNdefMessage != NULL) { - Serial.println("--- NDEF record received:"); - - NdefRecord = DetectNdefRecordType(pNdefMessage); - - switch (NdefRecord.recordType) { - case MEDIA_VCARD: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("vCard:"); - Serial.println(reinterpret_cast(NdefRecord.recordPayload)); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_TEXT: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("Text record: "); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[NdefRecord.recordPayload[0] + 1])); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } break; - - case WELL_KNOWN_SIMPLE_URI: { - save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize]; - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0'; - Serial.print("URI record: "); - Serial.println(reinterpret_cast(ndef_helper_UriHead(NdefRecord.recordPayload[0]), &NdefRecord.recordPayload[1])); - NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save; - } break; - - case MEDIA_HANDOVER_WIFI: { - unsigned char index = 0, i; - - Serial.println("--- Received WIFI credentials:"); - if ((NdefRecord.recordPayload[index] == 0x10) && (NdefRecord.recordPayload[index + 1] == 0x0e)) - index += 4; - while (index < NdefRecord.recordPayloadSize) { - if (NdefRecord.recordPayload[index] == 0x10) { - if (NdefRecord.recordPayload[index + 1] == 0x45) { - Serial.print("- SSID = "); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4 + 0])); - Serial.println(SSID); - } else if (NdefRecord.recordPayload[index + 1] == 0x03) { - Serial.print("- Authenticate Type = "); - Serial.println(ndef_helper_WifiAuth(NdefRecord.recordPayload[index + 5])); - } else if (NdefRecord.recordPayload[index + 1] == 0x0f) { - Serial.print("- Encryption Type = "); - Serial.println(ndef_helper_WifiEnc(NdefRecord.recordPayload[index + 5])); - } else if (NdefRecord.recordPayload[index + 1] == 0x27) { - Serial.print("- Network key = "); - Serial.println(reinterpret_cast(&NdefRecord.recordPayload[index + 4])); - Serial.print("- Network key = "); - // Serial.println(getHexRepresentation(&NdefRecord.recordPayload[index + 4], NdefRecord.recordPayload[index + 3])); - } - index += 4 + NdefRecord.recordPayload[index + 3]; - } else - continue; - } - } break; - - case WELL_KNOWN_HANDOVER_SELECT: - Serial.print("Handover select version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); - break; - - case WELL_KNOWN_HANDOVER_REQUEST: - Serial.print("Handover request version "); - Serial.print(NdefRecord.recordPayload[0] >> 4); - Serial.println(NdefRecord.recordPayload[0] & 0xF); - break; - - case MEDIA_HANDOVER_BT: - Serial.print("- Payload size: "); - Serial.println(NdefRecord.recordPayloadSize); - Serial.print("- Bluetooth Handover payload = "); - // Serial.println(Electroniccats_PN7150::getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); - Serial.print("- Bluetooth name: '"); - bluetoothName = ""; - for (unsigned int i = 10; i < NdefRecord.recordPayloadSize; i++) { - if (NdefRecord.recordPayload[i] == 0x04) { - break; - } - bluetoothName += (char)NdefRecord.recordPayload[i]; - } - Serial.println(bluetoothName + "'"); - - Serial.print("- Bluetooth address: '"); - bluetoothAddress = ""; - for (unsigned int i = 7; i >= 2; i--) { - // bluetoothAddress += Electroniccats_PN7150::getHexRepresentation(&NdefRecord.recordPayload[i], 1); - if (i > 2) { - bluetoothAddress += ":"; - } - } - Serial.println(bluetoothAddress + "'"); - break; - - case MEDIA_HANDOVER_BLE: - Serial.print("- BLE Handover payload = "); - // Serial.println(Electroniccats_PN7150::getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); - break; - - case MEDIA_HANDOVER_BLE_SECURE: - Serial.print("- BLE secure Handover payload = "); - // Serial.println(Electroniccats_PN7150::getHexRepresentation(NdefRecord.recordPayload, NdefRecord.recordPayloadSize)); - break; - - default: - Serial.println("Unsupported NDEF record, cannot parse"); - break; - } - pNdefMessage = GetNextRecord(pNdefMessage); - } - - Serial.println(""); -} - bool Electroniccats_PN7150::isTimeOut() const { return ((millis() - timeOutStartTime) >= timeOut); } @@ -1042,7 +907,7 @@ bool Electroniccats_PN7150::StopDiscovery() { return Electroniccats_PN7150::stopDiscovery(); } -bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { +bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout) { uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, protocol.ISODEP, interface.ISODEP}; // P2P Support @@ -1191,7 +1056,7 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint return SUCCESS; } -bool Electroniccats_PN7150::waitForDiscoveryNotification(uint8_t tout) { +bool Electroniccats_PN7150::waitForDiscoveryNotification(uint16_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfInterface, tout); } @@ -1200,7 +1065,7 @@ bool Electroniccats_PN7150::isTagDetected() { } // Deprecated, use isTagDetected() instead -bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout) { +bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 76f6bdd..d653d78 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -142,10 +142,10 @@ class Electroniccats_PN7150 : public Mode { uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery(void) instead bool stopDiscovery(); bool StopDiscovery(); // Deprecated, use stopDiscovery() instead - bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); - bool waitForDiscoveryNotification(uint8_t tout = 0); + bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); + bool waitForDiscoveryNotification(uint16_t tout = 0); bool isTagDetected(); - bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); // Deprecated, use isTagDetected() instead + bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); // Deprecated, use isTagDetected() instead uint8_t connectNCI(); uint8_t wakeupNCI(); bool cardModeSend(unsigned char *pData, unsigned char DataSize); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index bf788e1..fe91f83 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -137,7 +137,7 @@ String NdefRecord::getWiFiSSID() { String NdefRecord::getWiFiAuthenticationType() { String authenticationType = newString; - unsigned char index = 0, i; + unsigned char index = 0; if (getType() != MEDIA_HANDOVER_WIFI) { return authenticationType; @@ -161,7 +161,7 @@ String NdefRecord::getWiFiAuthenticationType() { String NdefRecord::getWiFiEncryptionType() { String encryptionType = newString; - unsigned char index = 0, i; + unsigned char index = 0; if (getType() != MEDIA_HANDOVER_WIFI) { return encryptionType; @@ -185,7 +185,7 @@ String NdefRecord::getWiFiEncryptionType() { String NdefRecord::getWiFiNetworkKey() { String networkKey = newString; - unsigned char index = 0, i; + unsigned char index = 0; if (getType() != MEDIA_HANDOVER_WIFI) { return networkKey; From 42affccec537059df6fb5456ddca42c33b7fd9ec Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 28 Aug 2023 14:28:05 -0600 Subject: [PATCH 092/106] feat: update keywords txt with new content --- examples/NDEFSend/NDEFSend.ino | 2 - keywords.txt | 261 ++++++++++++++++++++++++++++++--- library.properties | 2 +- src/Electroniccats_PN7150.cpp | 2 +- src/Electroniccats_PN7150.h | 5 +- 5 files changed, 241 insertions(+), 31 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 7726cc9..15b18e0 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -23,8 +23,6 @@ void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSi Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 -const char uri[] = "google.com"; - const char ndefMessage[] = {0xD1, // MB/ME/CF/1/IL/TNF 0x01, // Type length (1 byte) 0x08, // Payload length diff --git a/keywords.txt b/keywords.txt index 69ce991..15fce2c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,35 +1,248 @@ -####################################### +############################################################################## # Syntax Coloring Map For ElectronicCats PN7150 +############################################################################## +# Class and data types (KEYWORD1) +############################################################################## + +Electroniccats_PN7150 KEYWORD1 +nfc KEYWORD1 +NdefMessage KEYWORD1 +message KEYWORD1 +NdefRecord KEYWORD1 +record KEYWORD1 +remoteDevice KEYWORD1 +protocol KEYWORD1 +tech KEYWORD1 +modeTech KEYWORD1 +interface KEYWORD1 + +############################################################################## +# Methods and Functions (KEYWORD2) +############################################################################## +# # Electroniccats_PN7150.h ####################################### -# # Class (KEYWORD1) + +GetFwVersion KEYWORD2 +begin KEYWORD2 +hasMessage KEYWORD2 +writeData KEYWORD2 +readData KEYWORD2 +getFirmwareVersion KEYWORD2 +GetFwVersion KEYWORD2 +configMode KEYWORD2 +ConfigMode KEYWORD2 +configureSettings KEYWORD2 +ConfigureSettings KEYWORD2 +startDiscovery KEYWORD2 +StartDiscovery KEYWORD2 +stopDiscovery KEYWORD2 +StopDiscovery KEYWORD2 +waitForDiscoveryNotification KEYWORD2 +WaitForDiscoveryNotification KEYWORD2 +isTagDetected KEYWORD2 +connectNCI KEYWORD2 +wakeupNCI KEYWORD2 +cardModeSend KEYWORD2 +CardModeSend KEYWORD2 +cardModeReceive KEYWORD2 +CardModeReceive KEYWORD2 +processCardMode KEYWORD2 +ProcessCardMode KEYWORD2 +processReaderMode KEYWORD2 +ProcessReaderMode KEYWORD2 +processP2pMode KEYWORD2 +ProcessP2pMode KEYWORD2 +presenceCheck KEYWORD2 +PresenceCheck KEYWORD2 +waitForTagRemoval KEYWORD2 +readerTagCmd KEYWORD2 +ReaderTagCmd KEYWORD2 +readerReActivate KEYWORD2 +ReaderReActivate KEYWORD2 +readerActivateNext KEYWORD2 +ReaderActivateNext KEYWORD2 +activateNextTagDiscovery KEYWORD2 +readNdef KEYWORD2 +readNdefMessage KEYWORD2 +ReadNdef KEYWORD2 +writeNdef KEYWORD2 +writeNdefMessage KEYWORD2 +WriteNdef KEYWORD2 +nciFactoryTestPrbs KEYWORD2 +NxpNci_FactoryTest_Prbs KEYWORD2 +nciFactoryTestRfOn KEYWORD2 +NxpNci_FactoryTest_RfOn KEYWORD2 +reset KEYWORD2 +setReaderWriterMode KEYWORD2 +setEmulationMode KEYWORD2 +setP2PMode KEYWORD2 +setSendMsgCallback KEYWORD2 +isReaderDetected KEYWORD2 +closeCommunication KEYWORD2 +sendMessage KEYWORD2 +ndefCallback KEYWORD2 + +####################################### +## Mode.h ####################################### -Electroniccats_PN7150 KEYWORD1 +getMode KEYWORD2 ####################################### -# Methods and Functions (KEYWORD2) +## NdefMessage.h ####################################### -GetFwVersion KEYWORD2 -begin KEYWORD2 -writeData KEYWORD2 -readData KEYWORD2 -hasMessage KEYWORD2 -ConfigMode KEYWORD2 -StartDiscovery KEYWORD2 -connectNCI KEYWORD2 -wakeupNCI KEYWORD2 -CardModeSend KEYWORD2 -CardModeReceive KEYWORD2 -WaitForDiscoveryNotification KEYWORD2 -FillInterfaceInfo KEYWORD2 -ReaderTagCmd KEYWORD2 -StopDiscovery KEYWORD2 -ProcessReaderMode KEYWORD2 -PresenceCheck KEYWORD2 -ReaderReActivate KEYWORD2 -PrintBuf KEYWORD2 -ReaderActivateNext KEYWORD2 +getContent KEYWORD2 +getContentSize KEYWORD2 +setContent KEYWORD2 +getRecord KEYWORD2 +isEmpty KEYWORD2 +isNotEmpty KEYWORD2 +hasRecord KEYWORD2 + +####################################### +## NdefRecord.h +####################################### +create KEYWORD2 +getType KEYWORD2 +getPayload KEYWORD2 +getPayloadSize KEYWORD2 +getText KEYWORD2 +getBluetoothName KEYWORD2 +getBluetoothAddress KEYWORD2 +getWiFiSSID KEYWORD2 +getWiFiAuthenticationType KEYWORD2 +getWiFiEncryptionType KEYWORD2 +getWiFiNetworkKey KEYWORD2 +getVCardContent KEYWORD2 +getUri KEYWORD2 + +####################################### +## NdefRecord.h +####################################### + +getInterface KEYWORD2 +getProtocol KEYWORD2 +getModeTech KEYWORD2 +hasMoreTags KEYWORD2 +getSensRes KEYWORD2 +getSensResLen KEYWORD2 +getNFCID KEYWORD2 +getNFCIDLen KEYWORD2 +getSelRes KEYWORD2 +getSelResLen KEYWORD2 +getRats KEYWORD2 +getRatsLen KEYWORD2 +getAttribRes KEYWORD2 +getAttribResLen KEYWORD2 +getBitRate KEYWORD2 +getAFI KEYWORD2 +getDSFID KEYWORD2 +getID KEYWORD2 +setInterface KEYWORD2 +setProtocol KEYWORD2 +setProtocol KEYWORD2 +setModeTech KEYWORD2 +setMoreTagsAvailable KEYWORD2 +setInfo KEYWORD2 + +############################################################################## +# Constants (LITERAL1) +############################################################################## +## Interface.h +####################################### + +INTF_UNDETERMINED LITERAL1 +INTF_FRAME LITERAL1 +INTF_ISODEP LITERAL1 +INTF_NFCDEP LITERAL1 +INTF_TAGCMD LITERAL1 + +UNDETERMINED LITERAL1 +FRAME LITERAL1 +ISODEP LITERAL1 +NFCDEP LITERAL1 +TAGCMD LITERAL1 + +####################################### +## Mode.h ####################################### +READER_WRITER LITERAL1 +EMULATION LITERAL1 +P2P LITERAL1 + +####################################### +## ModeTech.h +####################################### + +MODE_POLL LITERAL1 +MODE_LISTEN LITERAL1 +MODE_MASK LITERAL1 + +POLL LITERAL1 +LISTEN LITERAL1 +MASK LITERAL1 + +####################################### +## Protocol.h +####################################### + +PROT_UNDETERMINED LITERAL1 +PROT_T1T LITERAL1 +PROT_T2T LITERAL1 +PROT_T3T LITERAL1 +PROT_ISODEP LITERAL1 +PROT_MIFARE LITERAL1 +PROT_ISO15693 LITERAL1 + +UNDETERMINED LITERAL1 +T1T LITERAL1 +T2T LITERAL1 +T3T LITERAL1 +ISODEP LITERAL1 +MIFARE LITERAL1 +ISO15693 LITERAL1 + +####################################### +## Tech.h +####################################### + +TECH_ACTIVE_NFCA LITERAL1 +TECH_PASSIVE_NFCB LITERAL1 +TECH_PASSIVE_NFCF LITERAL1 +TECH_ACTIVE_NFCA LITERAL1 +TECH_ACTIVE_NFCF LITERAL1 +TECH_PASSIVE_15693 LITERAL1 + +PASSIVE_NFCA LITERAL1 +PASSIVE_NFCB LITERAL1 +PASSIVE_NFCF LITERAL1 +ACTIVE_NFCA LITERAL1 +ACTIVE_NFCF LITERAL1 +PASSIVE_15693 LITERAL1 +PASSIVE_NFCV LITERAL1 + +####################################### +## ndef_helper.h +####################################### + +WELL_KNOWN_SIMPLE_TEXT LITERAL1 +WELL_KNOWN_SIMPLE_URI LITERAL1 +WELL_KNOWN_SMART_POSTER LITERAL1 +WELL_KNOWN_HANDOVER_SELECT LITERAL1 +WELL_KNOWN_HANDOVER_REQUEST LITERAL1 +WELL_KNOWN_ALTERNATIVE_CARRIER LITERAL1 +WELL_KNOWN_COLLISION_RESOLUTION LITERAL1 +MEDIA_VCARD LITERAL1 +MEDIA_HANDOVER_WIFI LITERAL1 +MEDIA_HANDOVER_BT LITERAL1 +MEDIA_HANDOVER_BLE LITERAL1 +MEDIA_HANDOVER_BLE_SECURE LITERAL1 +ABSOLUTE_URI LITERAL1 +UNSUPPORTED_NDEF_RECORD LITERAL1 + +recordType LITERAL1 +recordPayload LITERAL1 +recordPayloadSize LITERAL1 \ No newline at end of file diff --git a/library.properties b/library.properties index 26a6423..adec0e7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Electronic Cats PN7150 -version=1.8.0 +version=2.0.0 author=Electronic Cats and Salvador Mendoza maintainer=Electronic Cats sentence=Arduino library for SPI and I2C access to the PN7150 RFID/Near Field Communication chip. diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 4929518..e14c18d 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -2,7 +2,7 @@ * NXP PN7150 Driver * Porting authors: * Salvador Mendoza - @Netxing - salmg.net - * Andres Sabas - Electronic Cats - Electroniccats.com + * Andres Sabas - Electronic Cats - electroniccats.com * * August 2023 * diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index d653d78..aded486 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -112,7 +112,6 @@ class Electroniccats_PN7150 : public Mode { uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length uint8_t gNfcController_generation = 0; uint8_t gNfcController_fw_version[3] = {0}; - static void updateNdefMessage(unsigned char *message, unsigned short messageSize); void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command bool isTimeOut() const; bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses @@ -161,15 +160,15 @@ class Electroniccats_PN7150 : public Mode { void processP2pMode(RfIntf_t RfIntf); void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead void presenceCheck(RfIntf_t RfIntf); - void waitForTagRemoval(); void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use waitForTagRemoval() instead + void waitForTagRemoval(); bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead bool readerReActivate(RfIntf_t *pRfIntf); bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); - bool activateNextTagDiscovery(); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead + bool activateNextTagDiscovery(); void readNdef(RfIntf_t RfIntf); void readNdefMessage(); void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead From 1e03614a9111be5f3d630b443ba097c7aa75e0ab Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 30 Aug 2023 18:18:05 -0600 Subject: [PATCH 093/106] feat: update api reference with new methods --- API.md | 1559 +++++++++++++++++++++++++++- README.md | 4 + examples/DetectTags/DetectTags.ino | 2 +- examples/NDEFRead/NDEFRead.ino | 18 +- src/Electroniccats_PN7150.cpp | 162 +-- src/Electroniccats_PN7150.h | 58 +- src/NdefMessage.h | 8 +- src/NdefRecord.cpp | 10 +- src/NdefRecord.h | 41 +- src/RemoteDevice.cpp | 3 - src/ndef_helper.h | 3 +- 11 files changed, 1671 insertions(+), 197 deletions(-) diff --git a/API.md b/API.md index 6cb0b5c..7d86586 100644 --- a/API.md +++ b/API.md @@ -2,7 +2,7 @@ The `Electroniccats_PN7150` class enables Arduino library for I2C access to the PN7150 RFID/Near Field Communication chip. -### Class: `Electroniccats_PN7150` +## Class: `Electroniccats_PN7150` Include and instantiate the Electroniccats_PN7150 class. Creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 @@ -26,7 +26,7 @@ Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); #define PN7150_VEN (7) #define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 void setup(){ Serial.begin(115200); @@ -41,86 +41,1559 @@ void setup(){ int setupNFC(){ Serial.println("Initializing..."); - int setupOK = nfc.connectNCI(); //Wake up the board + int setupOK = nfc.connectNCI(); // Wake up the board if (!setupOK){ - setupOK = nfc.ConfigMode(mode); //Set up the configuration mode - if (!setupOK) setupOK = nfc.StartDiscovery(mode); //NCI Discovery mode + setupOK = nfc.configMode(); // Set up the configuration mode + if (!setupOK) setupOK = nfc.startDiscovery(); // NCI Discovery mode } return setupOK; } ``` -### Method: `GetFwVersion` -Get Firmware Version. +## Electroniccats_PN7150 Methods -```c -uint8_t GetFwVersion(); +### Method: `getFirmwareVersion` + +Get the firmware version of the NXP-NCI controller. + +```cpp +int getFirmwareVersion(); +``` + +#### Example + +```cpp +int version = nfc.getFirmwareVersion(); +``` + +### Method: `connectNCI` + +Initialize the connection with the NXP-NCI controller and updates the firmware version that can be obtained with the `getFirmwareVersion` method. + +```cppp +uint8_t connectNCI(); +``` + +Returns `0` if the connection is established correctly, otherwise returns `1`. + +#### Example + +```cpp +uint8_t statusNFC = nfc.connectNCI(); + +if (!statusNFC) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Method: `configMode` + +Configure the device mode. Reader/Writer as default. + +```cpp +uint8_t configMode(); +``` + +Returns `0` if the mode is configured correctly, otherwise returns `1`. + +#### Example + +```cpp +uint8_t status = nfc.configMode(); + +if (!status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Method: `setReaderWriterMode` + +Configure the device mode to Reader/Writer. + +```cpp +bool setReaderWriterMode(); +``` + +Returns `true` if the mode is configured correctly, otherwise returns `false`. + +#### Example + +```cpp +bool status = nfc.setReaderWriterMode(); + +if (status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Method: `setEmulationMode` + +Configure the device mode to Card Emulation. + +```cpp +bool setEmulationMode(); +``` + +Returns `true` if the mode is configured correctly, otherwise returns `false`. + +#### Example + +```cpp +bool status = nfc.setEmulationMode(); + +if (status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Method: `setP2PMode` + +Configure the device mode to Peer to Peer. + +```cpp +bool setP2PMode(); +``` + +Returns `true` if the mode is configured correctly, otherwise returns `false`. + +#### Example + +```cpp +bool status = nfc.setP2PMode(); + +if (status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Method: `getMode` + +Get the current device mode. + +```cpp +int getMode(); +``` + +#### Example + +```cpp +switch (nfc.getMode()) { + case nfc.mode.READER_WRITER: + Serial.println("Reader/Writer mode"); + break; + case nfc.mode.EMULATION: + Serial.println("Card Emulation mode"); + break; + case nfc.mode.P2P: + Serial.println("Peer to Peer mode"); + break; + default: + Serial.println("Unknown mode"); + break; +} +``` + +### Method: `configureSettings` + +Configure some aspects of the NFC controller, such as the hardware configuration, RF (radio frequency) configuration, and other settings. + +```cpp +bool configureSettings(); +``` + +A custom NFC UID can be configured by passing the UID and its length as parameters. + +```cpp +bool configureSettings(uint8_t *nfcuid, uint8_t uidlen); +``` + +Returns `0` if the parameters are configured correctly, otherwise returns `1`. + +#### Example 1 + +```cpp +bool status = nfc.configureSettings(); + +if (!status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +#### Example 2 + +```cpp +uint8_t nfcuid[] = {0x08, 0x00, 0x00, 0x00, 0x00, 0x00}; +uint8_t uidlen = 6; + +bool status = nfc.configureSettings(nfcuid, uidlen); + +if (!status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Method: `startDiscovery` + +Start the discovery mode for the device. + +```cpp +uint8_t startDiscovery(); +``` + +Returns `0` if the mode is configured correctly, otherwise returns `1`. + +#### Example + +```cpp +uint8_t status = nfc.startDiscovery(); + +if (!status) { + Serial.println("Set up is ok"); +} else { + Serial.println("Error while setting up mode, check connections!"); +} +``` + +### Methods: `stopDiscovery` + +Stop the discovery process of the device. + +```cpp +bool stopDiscovery(); +``` + +Returns `0`. Never returns `1`. + +#### Example + +```cpp +nfc.stopDiscovery(); +``` + +### Method: `isTagDetected` + +Returns `true` if a tag is detected, otherwise returns `false`. The timeout is set to 500ms by default and can be changed by passing a parameter. + +```cpp +bool isTagDetected(uint16_t tout = 500); +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.println("Tag detected!"); + // Do something +} +``` + +### Method: `cardModeSend` + +Send a data packet in card mode. + +```cpp +bool cardModeSend(unsigned char *pData, unsigned char DataSize); +``` + +### Method: `cardModeReceive` + +Receive a data packet from a card. + +```cpp +bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); +``` + +### Method: `handleCardEmulation` + +Resets the card emulation state and processes the card mode for card emulation. + +```cpp +void handleCardEmulation(); +``` + +### Method: `waitForTagRemoval` + +Waits for the tag to be removed. + +```cpp +void waitForTagRemoval(); +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); + Serial.println("Card removed!"); +} +``` + +### Method: `readerTagCmd` + +Sends a command to the reader. + +```cpp +bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); +``` + +### Method: `readerReActivate` + +Reactivates a target after it has been deactivated. + +```cpp +bool readerReActivate(); +``` + +### Method: `activateNextTagDiscovery` + +Activates the next tag discovered. + +```cpp +bool activateNextTagDiscovery(); +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + // It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + Serial.println("Multiple cards are detected!"); + nfc.activateNextTagDiscovery(); + } +} +``` + +### Method: `readNdefMessage` + +Reads the NDEF message from the tag. + +```cpp +void readNdefMessage(); +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + nfc.readNdefMessage(); +} +``` + +### Method: `writeNdefMessage` + +Writes the NDEF message to the tag. + +```cpp +void writeNdefMessage(); +``` + +### Method: `nciFactoryTestPrbs` + +Performs a factory test for the NCI controller. + +```cpp +bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); +``` + +### Method: `nciFactoryTestRfOn` + +Performs a factory test for the NCI controller. + +```cpp +bool nciFactoryTestRfOn(); +``` + +### Method: `reset` + +Stops the discovery process, configures the device mode if it is necessary and starts the discovery process again. + +```cpp +bool reset(); +``` + +#### Example + +```cpp +nfc.reset(); +``` + +### Method: `setReadMsgCallback` + +Registers a callback function to be called when an NDEF message is received. + +```cpp +void setReadMsgCallback(CustomCallback_t function); +``` + +#### Example + +```cpp +void messageReceived() { + Serial.println("Message received!"); +} + +void setup() { + nfc.setReadMsgCallback(messageReceived); +} +``` + +### Method: `isReaderDetected` + +Returns `true` if a reader is detected, otherwise returns `false`. + +```cpp +bool isReaderDetected(); +``` + +#### Example + +```cpp +if (nfc.isReaderDetected()) { + Serial.println("Reader detected!"); + // Do something +} +``` + +### Method: `closeCommunication` + +Send a command to the reader to close the communication. + +```cpp +void closeCommunication(); +``` + +#### Example + +```cpp +if (nfc.isReaderDetected()) { + Serial.println("Reader detected!"); + nfc.handleCardEmulation(); + nfc.closeCommunication(); +} +``` + +### Method: `sendMessage` + +Send an NDEF message to the reader. + +```cpp +void sendMessage(); +``` + +#### Example + +```cpp +if (nfc.isReaderDetected()) { + Serial.println("Reader detected!"); + Serial.println("Sending NDEF message..."); + nfc.sendMessage(); +} +``` + +## Class Interface + +### Constant `UNDETERMINED` + +Constant for the undetermined interface. + +```cpp +UNDETERMINED = 0x0 +``` + +#### Example + +```cpp +nfc.interface.UNDETERMINED; +``` + +### Constant `FRAME` + +Constant for the frame interface. + +```cpp +FRAME = 0x1 +``` + +#### Example + +```cpp +nfc.interface.FRAME; +``` + +### Constant `ISODEP` + +Constant for the ISO-DEP interface. + +```cpp +ISODEP = 0x2 +``` + +#### Example + +```cpp +nfc.interface.ISODEP; +``` + +### Constant `NFCDEP` + +Constant for the NFC-DEP interface. + +```cpp +NFCDEP = 0x3 +``` + +#### Example + +```cpp +nfc.interface.NFCDEP; +``` + +### Constant `TAGCMD` + +Constant for the tag interface. + +```cpp +TAGCMD = 0x80 +``` + +#### Example + +```cpp +nfc.interface.TAGCMD; +``` + +## Class Mode + +### Constant `READER_WRITER` + +Constant for the Reader/Writer mode. + +```cpp +READER_WRITER = 1 +``` + +#### Example + +```cpp +nfc.mode.READER_WRITER; +``` + +### Constant `EMULATION` + +Constant for the Card Emulation mode. + +```cpp +EMULATION = 2 +``` + +#### Example + +```cpp +nfc.mode.EMULATION; +``` + +### Constant `P2P` + +Constant for the Peer to Peer mode. + +```cpp +P2P = 3 +``` + +#### Example + +```cpp +nfc.mode.P2P; +``` + +## Class ModeTech + +### Constant `POLL` + +Constant for the Poll mode. + +```cpp +POLL = 0x00 +``` + +#### Example + +```cpp +nfc.modeTech.POLL; +``` + +### Constant `LISTEN` + +Constant for the Listen mode. + +```cpp +LISTEN = 0x80 +``` + +#### Example + +```cpp +nfc.modeTech.LISTEN; +``` + +### Constant `MASK` + +Constant for the Mask mode. + +```cpp +MASK = 0xF0 +``` + +#### Example + +```cpp +nfc.modeTech.MASK; +``` + +## Class Protocol + +### Constant `UNDETERMINED` + +Constant for the undetermined protocol. + +```cpp +UNDETERMINED = 0x0 +``` + +#### Example + +```cpp +nfc.protocol.UNDETERMINED; +``` + +### Constant `T1T` + +Constant for the T1T protocol. + +```cpp +T1T = 0x1 +``` + +#### Example + +```cpp +nfc.protocol.T1T; +``` + +### Constant `T2T` + +Constant for the T2T protocol. + +```cpp +T2T = 0x2 +``` + +#### Example + +```cpp +nfc.protocol.T2T; +``` + +### Constant `T3T` + +Constant for the T3T protocol. + +```cpp +T3T = 0x3 +``` + +#### Example + +```cpp +nfc.protocol.T3T; +``` + +### Constant `ISODEP` + +Constant for the ISO-DEP protocol. + +```cpp +ISODEP = 0x4 +``` + +#### Example + +```cpp +nfc.protocol.ISODEP; +``` + +### Constant `NFCDEP` + +Constant for the NFC-DEP protocol. + +```cpp +NFCDEP = 0x5 +``` + +#### Example + +```cpp +nfc.protocol.NFCDEP; +``` + +### Constant `ISO15693` + +Constant for the ISO15693 protocol. + +```cpp +ISO15693 = 0x6 +``` + +#### Example + +```cpp +nfc.protocol.ISO15693; +``` + +### Constant `MIFARE` + +Constant for the MIFARE protocol. + +```cpp +MIFARE = 0x80 +``` + +#### Example + +```cpp +nfc.protocol.MIFARE; +``` + +## Class Tech + +### Constant `PASSIVE_NFCA` + +Constant for the Passive NFC-A technology. + +```cpp +PASSIVE_NFCA = 0 +``` + +#### Example + +```cpp +nfc.tech.PASSIVE_NFCA; +``` + +### Constant `PASSIVE_NFCB` + +Constant for the Passive NFC-B technology. + +```cpp +PASSIVE_NFCB = 1 +``` + +#### Example + +```cpp +nfc.tech.PASSIVE_NFCB; +``` + +### Constant `PASSIVE_NFCF` + +Constant for the Passive NFC-F technology. + +```cpp +PASSIVE_NFCF = 2 +``` + +#### Example + +```cpp +nfc.tech.PASSIVE_NFCF; +``` + +### Constant `ACTIVE_NFCA` + +Constant for the Active NFC-A technology. + +```cpp +ACTIVE_NFCA = 3 +``` + +#### Example + +```cpp +nfc.tech.ACTIVE_NFCA; +``` + +### Constant `ACTIVE_NFCF` + +Constant for the Active NFC-F technology. + +```cpp +ACTIVE_NFCF = 5 +``` + +#### Example + +```cpp +nfc.tech.ACTIVE_NFCF; +``` + +### Constant `PASSIVE_15693` + +Constant for the Passive 15693 technology. + +```cpp +PASSIVE_15693 = 6 +``` + +#### Example + +```cpp +nfc.tech.PASSIVE_15693; +``` + +### Constant `PASSIVE_NFCV` + +Constant for the Passive NFC-V technology (ISO 15693). + +```cpp +PASSIVE_NFCV = 6 +``` + +#### Example + +```cpp +nfc.tech.PASSIVE_NFCV; +``` + +## Class RemoteDevice + +A `RemoteDevice` object represents a remote NFC device such as a tag or a reader. + +### Getters for device properties + +### Method: `getInterface` + +Get the interface of the device. + +```cpp +unsigned char getInterface() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("Interface: "); + switch (nfc.remoteDevice.getInterface()) { + case nfc.interface.ISODEP: + Serial.println("ISO-DEP"); + break; + case nfc.interface.NFCDEP: + Serial.println("NFC-DEP"); + break; + case nfc.interface.TAGCMD: + Serial.println("TAG"); + break; + case nfc.interface.FRAME: + Serial.println("FRAME"); + break; + case nfc.interface.UNDETERMINED: + Serial.println("UNDETERMINED"); + break; + default: + Serial.println("UNKNOWN"); + break; + } +} +``` + +### Method: `getProtocol` + +Get the protocol of the device. + +```cpp +unsigned char getProtocol() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("Protocol: "); + switch (nfc.remoteDevice.getProtocol()) { + case nfc.protocol.T1T: + Serial.println("T1T"); + break; + case nfc.protocol.T2T: + Serial.println("T2T"); + break; + case nfc.protocol.T3T: + Serial.println("T3T"); + break; + case nfc.protocol.ISODEP: + Serial.println("ISO-DEP"); + break; + case nfc.protocol.NFCDEP: + Serial.println("NFC-DEP"); + break; + case nfc.protocol.ISO15693: + Serial.println("ISO15693"); + break; + case nfc.protocol.MIFARE: + Serial.println("MIFARE"); + break; + case nfc.protocol.UNDETERMINED: + Serial.println("UNDETERMINED"); + break; + default: + Serial.println("UNKNOWN"); + break; + } +} +``` + +### Method: `getModeTech` + +Get the mode tech of the device. + +```cpp +unsigned char getModeTech() const; ``` -### Method: `ConfigMode` +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("Technology: "); + switch (nfc.remoteDevice.getModeTech()) { + case nfc.tech.PASSIVE_NFCA: + Serial.println("PASSIVE NFC A"); + break; + case nfc.tech.PASSIVE_NFCB: + Serial.println("PASSIVE NFC B"); + break; + case nfc.tech.PASSIVE_NFCF: + Serial.println("PASSIVE NFC F"); + break; + case nfc.tech.PASSIVE_15693: + Serial.println("PASSIVE 15693"); + break; + } +} +``` -Configure Mode for device +### Method: `hasMoreTags` -- `1`: RW Mode. -- `2`: Emulation mode. +Returns `true` if there are more tags to be discovered, otherwise returns `false`. -```c -uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) +```cpp +bool hasMoreTags() const; ``` -### Method: `StartDiscovery` +#### Example -Returns . +```cpp +if (nfc.isTagDetected()) { + if (nfc.remoteDevice.hasMoreTags()) { + Serial.println("Multiple cards are detected!"); + nfc.activateNextTagDiscovery(); + } +} +``` -```c -uint8_t StartDiscovery(mode); +### Getters for device information properties + +### Method: `getSensResLen` + +Get the SENS RES length of the device. + +```cpp +unsigned char getSensResLen() const; ``` -### Method: `CardModeReceive` +### Method: `getSensRes` -Returns . +Get the SENS RES (ATQA) of the device. Only available for tags that use passive communication and the NFC-A, NFC-B or NFC-F technologies, otherwise returns `NULL`. -```c -bool CardModeReceive (unsigned char *pData unsigned char *pDataSize); +```cpp +const unsigned char* getSensRes() const; ``` -### Method: `CardModeSend` +#### Example -Return. +```cpp +if (nfc.isTagDetected()) { + Serial.print("ATQA: "); + for (int i = 0; i < nfc.remoteDevice.getSensResLen(); i++) { + Serial.print(nfc.remoteDevice.getSensRes()[i], HEX); + Serial.print(" "); + } + Serial.println(); +} +``` -```c -bool CardModeSend (unsigned char *pData, unsigned char DataSize); +### Method: `getSelResLen` + +Get the SEL RES length of the device. + +```cpp +unsigned char getSelResLen() const; ``` -### Method: `ReaderActivateNext` +### Method: `getSelRes` -Return. +Get the SEL RES (SAK) of the device. Only available for tags that use passive communication and the NFC-A technology, otherwise returns `NULL`. -```c -bool ReaderActivateNext(RfIntf_t *pRfIntf); +```cpp +const unsigned char* getSelRes() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("SAK: "); + for (int i = 0; i < nfc.remoteDevice.getSelResLen(); i++) { + Serial.print(nfc.remoteDevice.getSelRes()[i], HEX); + Serial.print(" "); + } + Serial.println(); +} ``` -### Method: `WaitForDiscoveryNotification` +### Method: `getNFCIDLen` -Return. +Get the NFCID length of the device. -```c -bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf); +```cpp +unsigned char getNFCIDLen() const; ``` +### Method: `getNFCID` + +Get the NFCID of the device. Only available for tags that use passive communication and the NFC-A technology, otherwise returns `NULL`. -### Method: `ProcessReaderMode` +```cpp +const unsigned char* getNFCID() const; +``` -Return. +#### Example -```c -void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); +```cpp +if (nfc.isTagDetected()) { + Serial.print("NFCID: "); + for (int i = 0; i < nfc.remoteDevice.getNFCIDLen(); i++) { + Serial.print(nfc.remoteDevice.getNFCID()[i], HEX); + Serial.print(" "); + } + Serial.println(); +} ``` -### Methods: `StopDiscovery` +### Method: `getRatsLen` -Return. +Get the RATS length of the device. -```c -bool StopDiscovery(void); +```cpp +unsigned char getRatsLen() const; +``` + +### Method: `getRats` + +Get the RATS of the device. Only available for tags that use passive communication and the NFC-A technology, otherwise returns `NULL`. + +```cpp +const unsigned char* getRats() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("RATS: "); + for (int i = 0; i < nfc.remoteDevice.getRatsLen(); i++) { + Serial.print(nfc.remoteDevice.getRats()[i], HEX); + Serial.print(" "); + } + Serial.println(); +} +``` + +### Method: `getAttribResLen` + +Get the ATTRIB RES length of the device. + +```cpp +unsigned char getAttribResLen() const; +``` + +### Method: `getAttribRes` + +Get the ATTRIB RES of the device. Only available for tags that use passive communication and the NFC-B technology, otherwise returns `NULL`. + +```cpp +const unsigned char* getAttribRes() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("ATTRIB RES: "); + for (int i = 0; i < nfc.remoteDevice.getAttribResLen(); i++) { + Serial.print(nfc.remoteDevice.getAttribRes()[i], HEX); + Serial.print(" "); + } + Serial.println(); +} +``` + +### Method: `getBitRate` + +Get the bit rate of the device. Only available for tags that use passive communication and the NFC-F technology, otherwise returns `NULL`. + +```cpp +unsigned char getBitRate() const; +``` + +#### Example + +```cpp +Serial.print("Bitrate = "); +Serial.println((nfc.remoteDevice.getBitRate() == 1) ? "212" : "424"); +``` + +### Method: `getAFI` + +Get the AFI of the device. Only available for tags that use passive communication and the NFC-V technology, otherwise returns `NULL`. + +```cpp +unsigned char getAFI() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("AFI = "); + Serial.println(nfc.remoteDevice.getAFI()); +} +``` + +### Method: `getDSFID` + +Get the DSF ID of the device. Only available for tags that use passive communication and the NFC-V technology, otherwise returns `NULL`. + +```cpp +unsigned char getDSFID() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("DSF ID = "); + Serial.println(nfc.remoteDevice.getDSFID(), HEX); +} +``` + +### Method: `getID` + +Get the ID of the device. Only available for tags that use passive communication and the NFC-V technology, otherwise returns `NULL`. + +```cpp +const unsigned char* getID() const; +``` + +#### Example + +```cpp +if (nfc.isTagDetected()) { + Serial.print("ID: "); + for (int i = 0; i < sizeof(nfc.remoteDevice.getID()); i++) { + Serial.print(nfc.remoteDevice.getID()[i], HEX); + Serial.print(" "); + } + Serial.println(); +} +``` + +## Class NdefMessage + +A `NdefMessage` object represents an NDEF message. An NDEF message is a container for one or more NDEF records. + +### Method: `begin` + +Registers a callback function to be called when an NDEF message is received wich updates the message content. + +```cpp +void begin(); +``` + +#### Example + +```cpp +NdefMessage message; + +void setup() { + message.begin(); +} +``` + +### Method: `getContentSize` + +Get the content size of the message. + +```cpp +static unsigned short getContentSize(); +``` + +### Method: `getContent` + +Get the content of the message. + +```cpp +static unsigned char *getContent(); +``` + +### Method: `setContent` + +Set the content of the message. + +```cpp +static void setContent(unsigned char *content, unsigned short contentSize); +``` + +### Method: `getRecord` + +Get the record of the message. + +```cpp +NdefRecord_t getRecord(); +``` + +The record is a structure that contains the following properties: + +```cpp +typedef struct { + NdefRecordType_e recordType; + unsigned char *recordPayload; + unsigned int recordPayloadSize; +} NdefRecord_t; +``` + +#### Example + +```cpp +message.getRecord(); +``` + +### Method: `isEmpty` + +Returns `true` if the message is empty, otherwise returns `false`. + +```cpp +bool isEmpty(); +``` + +#### Example + +```cpp +if (message.isEmpty()) { + Serial.println("The message is empty!"); +} +``` + +### Method: `isNotEmpty` + +Returns `true` if the message is not empty, otherwise returns `false`. + +```cpp +bool isNotEmpty(); +``` + +#### Example + +```cpp +if (message.isNotEmpty()) { + Serial.println("The message is not empty!"); +} +``` + +### Method: `hasRecord` + +Returns `true` if the message has a record, otherwise returns `false`. + +```cpp +bool hasRecord(); +``` + +#### Example + +```cpp +if (message.hasRecord()) { + Serial.println("The message has a record!"); +} +``` + +## Class NdefRecord + +A `NdefRecord` object represents an NDEF record. An NDEF record is a data structure that contains data that is stored or transported in an NDEF message. + +#### Example + +```cpp +NdefRecord record; +``` + +### Method: `create` + +Creates a new NDEF record. + +```cpp +void create(NdefRecord_t record); +``` + +#### Example + +```cpp +record.create(message.getRecord()); +``` + +### Method: `isEmpty` + +Returns `true` if the record is empty, otherwise returns `false`. + +```cpp +bool isEmpty(); +``` + +#### Example + +```cpp +if (record.isEmpty()) { + Serial.println("The record is empty!"); +} +``` + +### Method: `isNotEmpty` + +Returns `true` if the record is not empty, otherwise returns `false`. + +```cpp +bool isNotEmpty(); +``` + +#### Example + +```cpp +if (record.isNotEmpty()) { + Serial.println("The record is not empty!"); +} +``` + +### Method: `getType` + +Get the type of the record. + +```cpp +NdefRecordType_e getType(); +``` + +The type is a structure that contains the following properties: + +```cpp +typedef enum { + WELL_KNOWN_SIMPLE_TEXT, + WELL_KNOWN_SIMPLE_URI, + WELL_KNOWN_SMART_POSTER, + WELL_KNOWN_HANDOVER_SELECT, + WELL_KNOWN_HANDOVER_REQUEST, + WELL_KNOWN_ALTERNATIVE_CARRIER, + WELL_KNOWN_COLLISION_RESOLUTION, + MEDIA_VCARD, + MEDIA_HANDOVER_WIFI, + MEDIA_HANDOVER_BT, + MEDIA_HANDOVER_BLE, + MEDIA_HANDOVER_BLE_SECURE, + ABSOLUTE_URI, + UNSUPPORTED_NDEF_RECORD = 0xFF +} NdefRecordType_e; +``` + +### Method: `getPayloadSize` + +Get the payload size of the record. + +```cpp +unsigned short getPayloadSize(); +``` + +### Method: `getPayload` + +Get the payload of the record. + +```cpp +unsigned char *getPayload(); +``` + +#### Example + +```cpp +Serial.print("Payload: "); +for (int i = 0; i < record.getPayloadSize(); i++) { + Serial.print(record.getPayload()[i], HEX); + Serial.print(" "); +} +Serial.println(); +``` + +### Method: `getText` + +Get the text of the record if the type is `WELL_KNOWN_SIMPLE_TEXT`, otherwise returns `"null"`. + +```cpp +String getText(); +``` + +#### Example + +```cpp +Serial.print("Text: "); +Serial.println(record.getText()); +``` + +### Method: `getBluetoothName` + +Get the Bluetooth name of the record if the type is `MEDIA_HANDOVER_BT`, otherwise returns `"null"`. + +```cpp +String getBluetoothName(); +``` + +#### Example + +```cpp +Serial.print("Bluetooth name: "); +Serial.println(record.getBluetoothName()); +``` + +### Method: `getBluetoothAddress` + +Get the Bluetooth address of the record if the type is `MEDIA_HANDOVER_BT`, otherwise returns `"null"`. + +```cpp +String getBluetoothAddress(); +``` + +#### Example + +```cpp +Serial.print("Bluetooth address: "); +Serial.println(record.getBluetoothAddress()); +``` + +### Method: `getWifiSSID` + +Get the WiFi SSID of the record if the type is `MEDIA_HANDOVER_WIFI`, otherwise returns `"null"`. + +```cpp +String getWifiSSID(); +``` + +#### Example + +```cpp +Serial.print("WiFi SSID: "); +Serial.println(record.getWifiSSID()); +``` + +### Method: `getWifiPassword` + +Get the WiFi password of the record if the type is `MEDIA_HANDOVER_WIFI`, otherwise returns `"null"`. + +```cpp +String getWiFiPassword(); +``` + +#### Example + +```cpp +Serial.print("WiFi password: "); +Serial.println(record.getWiFiPassword()); +``` + +### Method: `getWiFiAuthenticationType` + +Get the WiFi authentication type of the record if the type is `MEDIA_HANDOVER_WIFI`, otherwise returns `"null"`. + +```cpp +String getWiFiAuthenticationType(); +``` + +#### Example + +```cpp +Serial.print("WiFi authentication type: "); +Serial.println(record.getWiFiAuthenticationType()); +``` + +### Method: `getWiFiEncryptionType` + +Get the WiFi encryption type of the record if the type is `MEDIA_HANDOVER_WIFI`, otherwise returns `"null"`. + +```cpp +String getWiFiEncryptionType(); +``` + +#### Example + +```cpp +Serial.print("WiFi encryption type: "); +Serial.println(record.getWiFiEncryptionType()); +``` + +### Method: `getVCardContent` + +Get the vCard content of the record if the type is `MEDIA_VCARD`, otherwise returns `"null"`. + +```cpp +String getVCardContent(); +``` + +#### Example + +```cpp +Serial.print("vCard content: "); +Serial.println(record.getVCardContent()); +``` + +### Method: `getUri` + +Get the URI of the record if the type is `WELL_KNOWN_SIMPLE_URI`, otherwise returns `"null"`. + +```cpp +String getUri(); +``` + +#### Example + +```cpp +Serial.print("URI: "); +Serial.println(record.getUri()); ``` \ No newline at end of file diff --git a/README.md b/README.md index de93f19..dd36353 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ Optimized for fast design-in, NXP's PN7150 NFC solutions fully comply with the N PN7150 is the high-performance version of PN7120, the plug'n-play NFC solution for easy integration into any OS environment, reducing Bill of Material (BOM) size and cost. PN71xx controllers are ideal for home automation applications such as gateways and work seamlessly with NFC-connected tags. +## API Documentation + +Check the [API Documentation](/API.md) for more information about the methods available in the library. + ### Compatibility * Arduino MKR Family diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index 7b0b586..b31a617 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -155,7 +155,7 @@ void displayCardInfo() { // Funtion in charge to show the card/s in te field Serial.print("\tID = "); Serial.println(getHexRepresentation(nfc.remoteDevice.getID(), sizeof(nfc.remoteDevice.getID()))); - Serial.print("\ntAFI = "); + Serial.print("\tAFI = "); Serial.println(nfc.remoteDevice.getAFI()); Serial.print("\tDSF ID = "); diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 30239e7..05cdeb1 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -1,13 +1,13 @@ /** * Example to read NDEF messages - * Authors: + * Authors: * Salvador Mendoza - @Netxing - salmg.net * Francisco Torres - Electronic Cats - electroniccats.com - * + * * August 2023 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ @@ -18,7 +18,7 @@ #define PN7150_ADDR (0x28) // Function prototypes -void ndefCallback(); +void messageReceived(); String getHexRepresentation(const byte *data, const uint32_t dataSize); void displayDeviceInfo(); void displayRecordInfo(NdefRecord record); @@ -34,7 +34,7 @@ void setup() { Serial.println("Detect NFC tags with PN7150"); // Register a callback function to be called when an NDEF message is received - nfc.setSendMsgCallback(ndefCallback); + nfc.setReadMsgCallback(messageReceived); Serial.println("Initializing..."); @@ -84,7 +84,7 @@ void loop() { } /// @brief Callback function called when an NDEF message is received -void ndefCallback() { +void messageReceived() { NdefRecord record; Serial.println("Processing Callback..."); @@ -223,7 +223,7 @@ void displayRecordInfo(NdefRecord record) { case MEDIA_HANDOVER_WIFI: Serial.println("\tReceived WIFI credentials:"); Serial.println("\t- SSID: " + record.getWiFiSSID()); - Serial.println("\t- Network key: " + record.getWiFiNetworkKey()); + Serial.println("\t- Network key: " + record.getWiFiPassword()); Serial.println("\t- Authentication type: " + record.getWiFiAuthenticationType()); Serial.println("\t- Encryption type: " + record.getWiFiEncryptionType()); break; diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index e14c18d..6d1e2b9 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -95,6 +95,32 @@ void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) { timeOut = theTimeOut; } +uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; + uint16_t NbBytes = 0; + + // Reset RF settings restauration flag + (void)writeData(NCICoreReset, 4); + getMessage(15); + NbBytes = rxMessageLength; + if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { + return ERROR; + } + getMessage(); + NbBytes = rxMessageLength; + if (NbBytes != 0) { + // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); + // Is CORE_GENERIC_ERROR_NTF ? + if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { + /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ + // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; + } else { + return ERROR; + } + } + return SUCCESS; +} + bool Electroniccats_PN7150::getMessage(uint16_t timeout) { // check for message using timeout, 5 milisec as default setTimeOut(timeout); rxMessageLength = 0; @@ -179,6 +205,58 @@ int Electroniccats_PN7150::GetFwVersion() { return getFirmwareVersion(); } +uint8_t Electroniccats_PN7150::connectNCI() { + uint8_t i = 2; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + + // Check if begin function has been called + if (this->_hasBeenInitialized) { + return SUCCESS; + } + + // Open connection to NXPNCI + _wire->begin(); + if (_VENpin != 255) { + digitalWrite(_VENpin, HIGH); + delay(1); + digitalWrite(_VENpin, LOW); + delay(1); + digitalWrite(_VENpin, HIGH); + delay(3); + } + + // Loop until NXPNCI answers + while (wakeupNCI() != SUCCESS) { + if (i-- == 0) + return ERROR; + delay(500); + } + + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) + return ERROR; + + // Retrieve NXP-NCI NFC Controller generation + if (rxBuffer[17 + rxBuffer[8]] == 0x08) + gNfcController_generation = 1; + else if (rxBuffer[17 + rxBuffer[8]] == 0x10) + gNfcController_generation = 2; + + // Retrieve NXP-NCI NFC Controller FW version + gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V + gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO + gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO +#ifdef DEBUG + Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); + Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); + Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); + Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); +#endif + + return SUCCESS; +} + /// @brief Update the internal mode, stop discovery, and build the command to configure the PN7150 chip based on the input mode /// @param modeSE /// @return SUCCESS or ERROR @@ -1060,8 +1138,8 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(uint16_t tout) { return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfInterface, tout); } -bool Electroniccats_PN7150::isTagDetected() { - return !Electroniccats_PN7150::waitForDiscoveryNotification(500); +bool Electroniccats_PN7150::isTagDetected(uint16_t tout) { + return !Electroniccats_PN7150::waitForDiscoveryNotification(tout); } // Deprecated, use isTagDetected() instead @@ -1069,84 +1147,6 @@ bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); } -uint8_t Electroniccats_PN7150::connectNCI() { - uint8_t i = 2; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - - // Check if begin function has been called - if (this->_hasBeenInitialized) { - return SUCCESS; - } - - // Open connection to NXPNCI - _wire->begin(); - if (_VENpin != 255) { - digitalWrite(_VENpin, HIGH); - delay(1); - digitalWrite(_VENpin, LOW); - delay(1); - digitalWrite(_VENpin, HIGH); - delay(3); - } - - // Loop until NXPNCI answers - while (wakeupNCI() != SUCCESS) { - if (i-- == 0) - return ERROR; - delay(500); - } - - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - return ERROR; - - // Retrieve NXP-NCI NFC Controller generation - if (rxBuffer[17 + rxBuffer[8]] == 0x08) - gNfcController_generation = 1; - else if (rxBuffer[17 + rxBuffer[8]] == 0x10) - gNfcController_generation = 2; - - // Retrieve NXP-NCI NFC Controller FW version - gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V - gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO - gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO -#ifdef DEBUG - Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); - Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); - Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); - Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); -#endif - - return SUCCESS; -} - -uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; - uint16_t NbBytes = 0; - - // Reset RF settings restauration flag - (void)writeData(NCICoreReset, 4); - getMessage(15); - NbBytes = rxMessageLength; - if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { - return ERROR; - } - getMessage(); - NbBytes = rxMessageLength; - if (NbBytes != 0) { - // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); - // Is CORE_GENERIC_ERROR_NTF ? - if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { - /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ - // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; - } else { - return ERROR; - } - } - return SUCCESS; -} - bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char DataSize) { bool status; uint8_t Cmd[MAX_NCI_FRAME_SIZE]; @@ -1771,7 +1771,7 @@ bool Electroniccats_PN7150::setP2PMode() { return true; } -void Electroniccats_PN7150::setSendMsgCallback(CustomCallback_t function) { +void Electroniccats_PN7150::setReadMsgCallback(CustomCallback_t function) { registerNdefReceivedCallback(function); } diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index aded486..96e6dec 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -21,11 +21,11 @@ #include // Gives us access to all typical Arduino types and functions // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library #include "Mode.h" +#include "NdefMessage.h" +#include "NdefRecord.h" #include "P2P_NDEF.h" #include "RemoteDevice.h" #include "T4T_NDEF_emu.h" -#include "NdefMessage.h" -#include "NdefRecord.h" // #define DEBGU2 // #define DEBUG3 @@ -112,8 +112,9 @@ class Electroniccats_PN7150 : public Mode { uint32_t rxMessageLength; // length of the last message received. As these are not 0x00 terminated, we need to remember the length uint8_t gNfcController_generation = 0; uint8_t gNfcController_fw_version[3] = {0}; - void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command + void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command bool isTimeOut() const; + uint8_t wakeupNCI(); bool getMessage(uint16_t timeout = 5); // 5 miliseconds as default to wait for interrupt responses public: @@ -129,9 +130,13 @@ class Electroniccats_PN7150 : public Mode { uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read int getFirmwareVersion(); int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead - uint8_t configMode(uint8_t modeSE); + uint8_t connectNCI(); + uint8_t configMode(uint8_t modeSE); // Deprecated, use configMode(void) instead uint8_t configMode(void); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode(void) instead + bool setReaderWriterMode(); + bool setEmulationMode(); + bool setP2PMode(); bool configureSettings(void); bool ConfigureSettings(void); // Deprecated, use configureSettings(void) instead bool configureSettings(uint8_t *nfcuid, uint8_t uidlen); @@ -140,39 +145,37 @@ class Electroniccats_PN7150 : public Mode { uint8_t startDiscovery(void); uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery(void) instead bool stopDiscovery(); - bool StopDiscovery(); // Deprecated, use stopDiscovery() instead - bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); - bool waitForDiscoveryNotification(uint16_t tout = 0); - bool isTagDetected(); + bool StopDiscovery(); // Deprecated, use stopDiscovery() instead + bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); // Deprecated, use isTagDetected() instead + bool waitForDiscoveryNotification(uint16_t tout = 0); // Deprecated, use isTagDetected() instead + bool isTagDetected(uint16_t tout = 500); bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); // Deprecated, use isTagDetected() instead - uint8_t connectNCI(); - uint8_t wakeupNCI(); bool cardModeSend(unsigned char *pData, unsigned char DataSize); bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead - void processCardMode(RfIntf_t RfIntf); - void processCardMode(void); void handleCardEmulation(); - void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead - void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); + void processCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead + void processCardMode(void); // Deprecated, use handleCardEmulation() instead + void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead + void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use waitForTagRemoval(), readNdefMessage() or writeNdefMessage() and readNdefMessage() instead void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead - void processP2pMode(RfIntf_t RfIntf); - void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead - void presenceCheck(RfIntf_t RfIntf); - void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use waitForTagRemoval() instead + void processP2pMode(RfIntf_t RfIntf); // TODO: rename it + void ProcessP2pMode(RfIntf_t RfIntf); // Deprecated, use processP2pMode() instead + void presenceCheck(RfIntf_t RfIntf); // Deprecated, use waitForTagRemoval() instead + void PresenceCheck(RfIntf_t RfIntf); // Deprecated, use waitForTagRemoval() instead void waitForTagRemoval(); bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead - bool readerReActivate(RfIntf_t *pRfIntf); - bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead - bool readerActivateNext(RfIntf_t *pRfIntf); - bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead + bool readerReActivate(RfIntf_t *pRfIntf); // TODO: remove the pointer + bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead + bool readerActivateNext(RfIntf_t *pRfIntf); // TODO: remove it + bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead bool activateNextTagDiscovery(); - void readNdef(RfIntf_t RfIntf); + void readNdef(RfIntf_t RfIntf); // TODO: remove it void readNdefMessage(); - void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead - void writeNdef(RfIntf_t RfIntf); + void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead + void writeNdef(RfIntf_t RfIntf); // TODO: remove it void writeNdefMessage(); void WriteNdef(RfIntf_t RfIntf); // Deprecated, use writeNdefMessage() instead bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate); @@ -180,10 +183,7 @@ class Electroniccats_PN7150 : public Mode { bool nciFactoryTestRfOn(); bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead bool reset(); - bool setReaderWriterMode(); - bool setEmulationMode(); - bool setP2PMode(); - void setSendMsgCallback(CustomCallback_t function); + void setReadMsgCallback(CustomCallback_t function); bool isReaderDetected(); void closeCommunication(); void sendMessage(); diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 9db9dd6..c921cb7 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -2,11 +2,11 @@ * Library to manage the NDEF message * Authors: * Francisco Torres - Electronic Cats - electroniccats.com - * + * * August 2023 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index fe91f83..026adcc 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -2,11 +2,11 @@ * Library to manage the NDEF record * Authors: * Francisco Torres - Electronic Cats - electroniccats.com - * + * * August 2023 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ @@ -183,7 +183,7 @@ String NdefRecord::getWiFiEncryptionType() { return encryptionType; } -String NdefRecord::getWiFiNetworkKey() { +String NdefRecord::getWiFiPassword() { String networkKey = newString; unsigned char index = 0; diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 5eddb0c..0a5e777 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -2,11 +2,11 @@ * Library to manage the NDEF record * Authors: * Francisco Torres - Electronic Cats - electroniccats.com - * + * * August 2023 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ @@ -15,11 +15,12 @@ #define NdefRecord_H #include + #include "ndef_helper.h" class NdefRecord { private: - NdefRecord_t content; + NdefRecord_t content; NdefRecordType_e type; unsigned char *payload; unsigned short payloadSize; @@ -28,21 +29,21 @@ class NdefRecord { public: NdefRecord(); - void create(NdefRecord_t record); - bool isEmpty(); - bool isNotEmpty(); - NdefRecordType_e getType(); - unsigned char *getPayload(); - unsigned short getPayloadSize(); - String getText(); - String getBluetoothName(); - String getBluetoothAddress(); - String getWiFiSSID(); - String getWiFiAuthenticationType(); - String getWiFiEncryptionType(); - String getWiFiNetworkKey(); - String getVCardContent(); - String getUri(); + void create(NdefRecord_t record); + bool isEmpty(); + bool isNotEmpty(); + NdefRecordType_e getType(); + unsigned char *getPayload(); + unsigned short getPayloadSize(); + String getText(); + String getBluetoothName(); + String getBluetoothAddress(); + String getWiFiSSID(); + String getWiFiAuthenticationType(); + String getWiFiEncryptionType(); + String getWiFiPassword(); + String getVCardContent(); + String getUri(); }; #endif \ No newline at end of file diff --git a/src/RemoteDevice.cpp b/src/RemoteDevice.cpp index 29e9bcd..35ae223 100644 --- a/src/RemoteDevice.cpp +++ b/src/RemoteDevice.cpp @@ -46,9 +46,6 @@ const unsigned char* RemoteDevice::getSensRes() const { break; case (tech.PASSIVE_NFCV): - return this->remoteDeviceStruct.info.nfcVPP.id; - break; - default: return NULL; break; diff --git a/src/ndef_helper.h b/src/ndef_helper.h index 1d5a3dd..9338ccd 100644 --- a/src/ndef_helper.h +++ b/src/ndef_helper.h @@ -47,8 +47,7 @@ typedef enum { UNSUPPORTED_NDEF_RECORD = 0xFF } NdefRecordType_e; -typedef struct -{ +typedef struct { NdefRecordType_e recordType; unsigned char *recordPayload; unsigned int recordPayloadSize; From 19d9441c190431dc2738402bba0cd0cdbf76fafb Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 11:11:09 -0600 Subject: [PATCH 094/106] feat: remove duplicated start discovery --- examples/DetectTagsOld/DetectTagsOld.ino | 185 ------------------ .../DetectingReadersOld.ino | 71 ------- examples/DetectingReadersOld/Makefile | 19 -- .../ISO14443-3A_read_block.ino | 6 +- .../ISO14443-3A_write_block.ino | 6 +- .../ISO15693_read_block.ino | 6 +- .../ISO15693_write_block.ino | 6 +- .../Makefile | 0 .../MifareClassic_read_block.ino | 12 +- .../MifareClassic_write_block.ino | 6 +- examples/P2P_Discovery/P2P_Discovery.ino | 4 +- src/Electroniccats_PN7150.cpp | 13 +- src/Electroniccats_PN7150.h | 5 +- 13 files changed, 29 insertions(+), 310 deletions(-) delete mode 100644 examples/DetectTagsOld/DetectTagsOld.ino delete mode 100644 examples/DetectingReadersOld/DetectingReadersOld.ino delete mode 100644 examples/DetectingReadersOld/Makefile rename examples/{DetectTagsOld => MifareClassic_read_block}/Makefile (100%) diff --git a/examples/DetectTagsOld/DetectTagsOld.ino b/examples/DetectTagsOld/DetectTagsOld.ino deleted file mode 100644 index 66cbdbe..0000000 --- a/examples/DetectTagsOld/DetectTagsOld.ino +++ /dev/null @@ -1,185 +0,0 @@ -/** - * Example detect tags and show their unique ID - * Authors: - * Salvador Mendoza - @Netxing - salmg.net - * For Electronic Cats - electroniccats.com - * - * March 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, - * please buy us a round! - * Distributed as-is; no warranty is given. - */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (11) -#define PN7150_VEN (13) -#define PN7150_ADDR (0x28) - -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags - -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation - -void ResetMode(){ //Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.configMode(mode); - nfc.startDiscovery(mode); -} - -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format - uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { - Serial.print(F("0x")); - // Append leading 0 for small values - if (data[szPos] <= 0xF) - Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) - { - Serial.print(F(" ")); - } - } - Serial.println(); -} -void displayCardInfo(RfIntf_t RfIntf){ //Funtion in charge to show the card/s in te field - char tmp[16]; - while (1){ - switch(RfIntf.Protocol){ //Indetify card protocol - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: - Serial.print(" - POLL MODE: Remote activated tag type: "); - Serial.println(RfIntf.Protocol); - break; - case PROT_ISO15693: - Serial.println(" - POLL MODE: Remote ISO15693 card activated"); - break; - case PROT_MIFARE: - Serial.println(" - POLL MODE: Remote MIFARE card activated"); - break; - default: - Serial.println(" - POLL MODE: Undetermined target"); - return; - } - - switch(RfIntf.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfIntf.Info.NFC_APP.NfcId, RfIntf.Info.NFC_APP.NfcIdLen); - - if(RfIntf.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X",RfIntf.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); Serial.println(" "); - - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCB): - if(RfIntf.Info.NFC_BPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_BPP.SensRes,RfIntf.Info.NFC_BPP.SensResLen); - } - break; - - case (MODE_POLL | TECH_PASSIVE_NFCF): - Serial.print("\tBitrate = "); - Serial.println((RfIntf.Info.NFC_FPP.BitRate == 1) ? "212" : "424"); - - if(RfIntf.Info.NFC_FPP.SensResLen != 0) { - Serial.print("\tSENS_RES = "); - PrintBuf(RfIntf.Info.NFC_FPP.SensRes,RfIntf.Info.NFC_FPP.SensResLen); - } - break; - - case (MODE_POLL | TECH_PASSIVE_15693): - Serial.print("\tID = "); - PrintBuf(RfIntf.Info.NFC_VPP.ID,sizeof(RfIntf.Info.NFC_VPP.ID)); - - Serial.print("\ntAFI = "); - Serial.println(RfIntf.Info.NFC_VPP.AFI); - - Serial.print("\tDSFID = "); - Serial.println(RfIntf.Info.NFC_VPP.DSFID,HEX); - break; - - default: - break; - } - if(RfIntf.MoreTags) { // It will try to identify more NFC cards if they are the same technology - if(nfc.readerActivateNext(&RfIntf) == NFC_ERROR) break; - } - else break; - } -} - -void setup(){ - Serial.begin(9600); - while(!Serial); - Serial.println("Detect NFC tags with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (1); - } - - if (nfc.configureSettings()) { - Serial.println("The Configure Settings is failed!"); - while (1); - } - - if(nfc.configMode(mode)){ //Set up the configuration mode - Serial.println("The Configure Mode is failed!!"); - while (1); - } - nfc.startDiscovery(mode); //NCI Discovery mode - Serial.println("Waiting for an Card ..."); -} - -void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - displayCardInfo(RfInterface); - switch(RfInterface.Protocol) { - case PROT_T1T: - case PROT_T2T: - case PROT_T3T: - case PROT_ISODEP: - nfc.processReaderMode(RfInterface, READ_NDEF); - break; - - case PROT_ISO15693: - break; - - case PROT_MIFARE: - nfc.processReaderMode(RfInterface, READ_NDEF); - break; - - default: - break; - } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); - } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("CARD REMOVED!"); - - nfc.stopDiscovery(); - nfc.startDiscovery(mode); - } - ResetMode(); - delay(500); -} diff --git a/examples/DetectingReadersOld/DetectingReadersOld.ino b/examples/DetectingReadersOld/DetectingReadersOld.ino deleted file mode 100644 index 43c320e..0000000 --- a/examples/DetectingReadersOld/DetectingReadersOld.ino +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Example detect tags and show their unique ID - * Authors: - * Salvador Mendoza - @Netxing - salmg.net - * For Electronic Cats - electroniccats.com - * - * March 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, - * please buy us a round! - * Distributed as-is; no warranty is given. - */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (11) -#define PN7150_VEN (13) -#define PN7150_ADDR (0x28) - -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 - -unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; - -uint8_t mode = 2; // modes: 1 = Reader/ Writer, 2 = Emulation - -void setup(){ - Serial.begin(9600); - while(!Serial); - Serial.println("Detect NFC readers with PN7150"); - Serial.println("Initializing..."); - - if (nfc.connectNCI()) { //Wake up the board - Serial.println("Error while setting up the mode, check connections!"); - while (1); - } - - if (nfc.configureSettings()) { - Serial.println("The Configure Settings is failed!"); - while (1); - } - - if(nfc.configMode(mode)){ //Set up the configuration mode - Serial.println("The Configure Mode is failed!!"); - while (1); - } - nfc.startDiscovery(mode); //NCI Discovery mode - Serial.println("Waiting for an Reader Card ..."); -} - -void loop(){ - Serial.print("."); - if(nfc.cardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? - if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes - switch (Cmd[1]) { - case 0xA4: //Something tries to select a file, meaning that it is a reader - Serial.println("Reader detected!"); - break; - - case 0xB0: //SFI - break; - - case 0xD0: //... - break; - - default: - break; - } - nfc.cardModeSend(STATUSOK, sizeof(STATUSOK)); - } - } -} diff --git a/examples/DetectingReadersOld/Makefile b/examples/DetectingReadersOld/Makefile deleted file mode 100644 index 4ab2c77..0000000 --- a/examples/DetectingReadersOld/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -BOARD_TAG = electroniccats:mbed_rp2040:bombercat -MONITOR_PORT = /dev/cu.usbmodem1101 - -compile: - arduino-cli compile --fqbn $(BOARD_TAG) - -upload: - arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose - -monitor: - arduino-cli monitor -p $(MONITOR_PORT) - -clean: - arduino-cli cache clean - -wait: - sleep 2 - -all: compile upload wait monitor \ No newline at end of file diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 1a5719f..e8aeed2 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -86,7 +86,7 @@ void setup(){ Serial.println("The Configure Mode failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } @@ -131,7 +131,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index c6c3bba..e1121d8 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -102,7 +102,7 @@ void setup(){ Serial.println("The Configure Mode failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } @@ -147,7 +147,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index 051eb84..8a46a05 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -28,7 +28,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -87,7 +87,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO15693 Card ..."); } @@ -128,7 +128,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 7643f05..dc3473c 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -45,14 +45,14 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an ISO15693 Card ..."); } void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -141,7 +141,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/DetectTagsOld/Makefile b/examples/MifareClassic_read_block/Makefile similarity index 100% rename from examples/DetectTagsOld/Makefile rename to examples/MifareClassic_read_block/Makefile diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 1797e67..99bee89 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -13,14 +13,14 @@ */ #include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) +#define PN7150_IRQ (11) +#define PN7150_VEN (13) #define PN7150_ADDR (0x28) #define BLK_NB_MFC 4 // Block tat wants to be read #define KEY_MFC 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // Default Mifare Classic key -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 RfIntf_t RfInterface; //Intarface to save data for multiple tags uint8_t mode = 1; @@ -28,7 +28,7 @@ uint8_t mode = 1; void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -95,7 +95,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an Mifare Classic Card ..."); } @@ -140,7 +140,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 5a3b22e..fcc5c7d 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -31,7 +31,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format @@ -132,7 +132,7 @@ void setup(){ Serial.println("The Configure Mode is failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for an Mifare Classic Card ..."); } @@ -177,7 +177,7 @@ void loop(){ Serial.println("CARD REMOVED!"); nfc.stopDiscovery(); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } ResetMode(); delay(500); diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index f4754cf..5794f0b 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -25,7 +25,7 @@ uint8_t mode = 3; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); nfc.configMode(mode); - nfc.startDiscovery(mode); + nfc.StartDiscovery(mode); } @@ -49,7 +49,7 @@ void setup(){ Serial.println("The Configure Mode failed!!"); while (1); } - nfc.startDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); //NCI Discovery mode Serial.println("Waiting for a P2P device..."); } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 6d1e2b9..a1d1c43 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -928,7 +928,7 @@ bool Electroniccats_PN7150::ConfigureSettings(uint8_t *uidcf, uint8_t uidlen) { return Electroniccats_PN7150::configureSettings(uidcf, uidlen); } -uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { +uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { int mode = Electroniccats_PN7150::getMode(); if (mode != modeSE) { Electroniccats_PN7150::setMode(modeSE); @@ -962,12 +962,7 @@ uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { uint8_t Electroniccats_PN7150::startDiscovery() { int mode = Electroniccats_PN7150::getMode(); - return Electroniccats_PN7150::startDiscovery(mode); -} - -// Deprecated, use startDiscovery(void) instead -uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { - return Electroniccats_PN7150::startDiscovery(modeSE); + return Electroniccats_PN7150::StartDiscovery(mode); } bool Electroniccats_PN7150::stopDiscovery() { @@ -1507,8 +1502,8 @@ bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { getMessage(100); /* Then re-activate the target */ - NCIActivate[4] = pRfIntf->Protocol; - NCIActivate[5] = pRfIntf->Interface; + NCIActivate[4] = remoteDevice.getProtocol(); + NCIActivate[5] = remoteDevice.getInterface(); (void)writeData(NCIDeactivate, sizeof(NCIDeactivate)); getMessage(); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 96e6dec..2e5e55a 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -141,9 +141,8 @@ class Electroniccats_PN7150 : public Mode { bool ConfigureSettings(void); // Deprecated, use configureSettings(void) instead bool configureSettings(uint8_t *nfcuid, uint8_t uidlen); bool ConfigureSettings(uint8_t *nfcuid, uint8_t uidlen); // Deprecated, use configureSettings() instead - uint8_t startDiscovery(uint8_t modeSE); - uint8_t startDiscovery(void); - uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery(void) instead + uint8_t startDiscovery(); + uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead bool stopDiscovery(); bool StopDiscovery(); // Deprecated, use stopDiscovery() instead bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); // Deprecated, use isTagDetected() instead From fbc884c1b387f26382660ceb780e9aa9cd75bd4e Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 13:48:47 -0600 Subject: [PATCH 095/106] docs: add francisco to authors --- src/Electroniccats_PN7150.cpp | 6 +++--- src/Electroniccats_PN7150.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index a1d1c43..35c83ba 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -3,6 +3,7 @@ * Porting authors: * Salvador Mendoza - @Netxing - salmg.net * Andres Sabas - Electronic Cats - electroniccats.com + * Francisco Torres - Electronic Cats - electroniccats.com * * August 2023 * @@ -11,10 +12,9 @@ * please buy us a round! * Distributed as-is; no warranty is given. * - * Some methods and ideas were extract from https://github.com/Strooom/PN7150 - * - * + * Some methods and ideas were extracted from https://github.com/Strooom/PN7150 */ + #include "Electroniccats_PN7150.h" uint8_t gNextTag_Protocol = PROT_UNDETERMINED; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 2e5e55a..37b350d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -1,10 +1,9 @@ -#ifndef Electroniccats_PN7150_H -#define Electroniccats_PN7150_H /** * NXP PN7150 Driver * Porting authors: * Salvador Mendoza - @Netxing - salmg.net * Andres Sabas - Electronic Cats - electroniccats.com + * Francisco Torres - Electronic Cats - electroniccats.com * * August 2023 * @@ -13,11 +12,12 @@ * please buy us a round! * Distributed as-is; no warranty is given. * - * A few methods and ideas were extract from - * https://github.com/Strooom/PN7150 - * + * Some methods and ideas were extracted from https://github.com/Strooom/PN7150 */ +#ifndef Electroniccats_PN7150_H +#define Electroniccats_PN7150_H + #include // Gives us access to all typical Arduino types and functions // The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library #include "Mode.h" From bdf460a52bc6734bd0904cce7a27c863a7f732f7 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 14:20:09 -0600 Subject: [PATCH 096/106] feat: remove duplicated wait for discovery notification --- .../ISO14443-3A_read_block.ino | 172 ++++++++-------- .../ISO14443-3A_write_block.ino | 6 +- .../ISO15693_read_block.ino | 6 +- .../ISO15693_write_block.ino | 6 +- .../MifareClassic_read_block.ino | 193 +++++++++--------- .../MifareClassic_write_block.ino | 6 +- examples/P2P_Discovery/P2P_Discovery.ino | 6 +- src/Electroniccats_PN7150.cpp | 22 +- src/Electroniccats_PN7150.h | 7 +- 9 files changed, 209 insertions(+), 215 deletions(-) diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index e8aeed2..3af2926 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -1,135 +1,141 @@ /** - * Example to read a ISO14443-3A(Tag Type 2 - T2T) block 5 and show its information - * Authors: + * Example to read a ISO14443-3A(Tag Type 2 - T2T) block 5 and show its information + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * November 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) -#define BLK_NB_ISO14443_3A (5) //Block to be read it +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (15) +#define PN7150_VEN (14) +#define PN7150_ADDR (0x28) +#define BLK_NB_ISO14443_3A (5) // Block to be read it -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Intarface to save data for multiple tags -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation +uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation -void ResetMode(){ //Reset the configuration mode after each reading +void ResetMode() { // Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++){ + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); + Serial.print(data[szPos] & 0xff, HEX); if ((numBytes > 1) && (szPos != numBytes - 1)) Serial.print(F(" ")); } Serial.println(); } -void PCD_ISO14443_3A_scenario (void){ - bool status; - unsigned char Resp[256]; - unsigned char RespSize; - /* Read block */ - unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A}; - - status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ - Serial.print("Error reading block: "); - Serial.print(ReadBlock[1],HEX); - Serial.print(" with error: "); - Serial.print(Resp[RespSize-1],HEX); - return; - } - Serial.print("------------------------Block "); - Serial.print(BLK_NB_ISO14443_3A, HEX); - Serial.println("-------------------------"); - PrintBuf(Resp, 4); +void PCD_ISO14443_3A_scenario(void) { + bool status; + unsigned char Resp[256]; + unsigned char RespSize; + /* Read block */ + unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A}; + + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0x00)) { + Serial.print("Error reading block: "); + Serial.print(ReadBlock[1], HEX); + Serial.print(" with error: "); + Serial.print(Resp[RespSize - 1], HEX); + return; + } + Serial.print("------------------------Block "); + Serial.print(BLK_NB_ISO14443_3A, HEX); + Serial.println("-------------------------"); + PrintBuf(Resp, 4); } -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); + while (!Serial) + ; Serial.println("Read ISO14443-3A(T2T) data block 5 with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings failed!"); - while (1); + while (1) + ; } - - if(nfc.configMode(mode)){ //Set up the configuration mode + + if (nfc.ConfigMode(mode)) { // Set up the configuration mode Serial.println("The Configure Mode failed!!"); - while (1); + while (1) + ; } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); // NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } -void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - switch(RfInterface.Protocol) { +void loop() { + if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards + switch (RfInterface.Protocol) { case PROT_T2T: Serial.println(" - Found ISO14443-3A(T2T) card"); - switch(RfInterface.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): - char tmp[16]; - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); - - if(RfInterface.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); Serial.println(" "); - } + switch (RfInterface.ModeTech) { // Indetify card technology + case (MODE_POLL | TECH_PASSIVE_NFCA): + char tmp[16]; + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[0]); + Serial.print(tmp); + Serial.print(" "); + sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[1]); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); + + if (RfInterface.Info.NFC_APP.SelResLen != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SelRes[0]); + Serial.print(tmp); + Serial.println(" "); + } break; - } - PCD_ISO14443_3A_scenario(); - break; - + } + PCD_ISO14443_3A_scenario(); + break; + default: - Serial.println(" - Found a card, but it is not ISO14443-3A(T2T)!"); - break; + Serial.println(" - Found a card, but it is not ISO14443-3A(T2T)!"); + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + + //* It can detect multiple cards at the same time if they use the same protocol + if (RfInterface.MoreTags) { + nfc.readerActivateNext(&RfInterface); } - //* Wait for card removal + //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - + nfc.stopDiscovery(); nfc.StartDiscovery(mode); } diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index e1121d8..191e7e6 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } @@ -98,7 +98,7 @@ void setup(){ while (1); } - if(nfc.configMode(mode)){ //Set up the configuration mode + if(nfc.ConfigMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode failed!!"); while (1); } @@ -107,7 +107,7 @@ void setup(){ } void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_T2T: Serial.println(" - Found ISO14443-3A(T2T) card"); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index 8a46a05..baf1d80 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } @@ -83,7 +83,7 @@ void setup(){ while (1); } - if(nfc.configMode(mode)){ //Set up the configuration mode + if(nfc.ConfigMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } @@ -92,7 +92,7 @@ void setup(){ } void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_ISO15693: Serial.println(" - Found ISO15693 card"); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index dc3473c..187b26a 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -41,7 +41,7 @@ void setup(){ while (1); } - if(nfc.configMode(mode)){ //Set up the configuration mode + if(nfc.ConfigMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } @@ -51,7 +51,7 @@ void setup(){ void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } @@ -105,7 +105,7 @@ void PCD_ISO15693_scenario (void){ } void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_ISO15693: Serial.println(" - Found ISO15693 card"); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 99bee89..55f6c91 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -1,144 +1,149 @@ /** - * Example to read a Mifare Classic block 4 and show its information - * Authors: + * Example to read a Mifare Classic block 4 and show its information + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * March 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (11) -#define PN7150_VEN (13) -#define PN7150_ADDR (0x28) -#define BLK_NB_MFC 4 // Block tat wants to be read -#define KEY_MFC 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // Default Mifare Classic key +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +#define BLK_NB_MFC 4 // Block tat wants to be read +#define KEY_MFC 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // Default Mifare Classic key -uint8_t mode = 1; - // modes: 1 = Reader/ Writer, 2 = Emulation -void ResetMode(){ //Reset the configuration mode after each reading +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 +RfIntf_t RfInterface; // Intarface to save data for multiple tags + +uint8_t mode = 1; +// modes: 1 = Reader/ Writer, 2 = Emulation +void ResetMode() { // Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) - { + Serial.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { Serial.print(F(" ")); } } Serial.println(); } -void PCD_MIFARE_scenario (void){ - Serial.println("Start reading process..."); - bool status; - unsigned char Resp[256]; - unsigned char RespSize; - /* Authenticate sector 1 with generic keys */ - unsigned char Auth[] = {0x40, BLK_NB_MFC/4, 0x10, KEY_MFC}; - /* Read block 4 */ - unsigned char Read[] = {0x10, 0x30, BLK_NB_MFC}; - - /* Authenticate */ - status = nfc.readerTagCmd(Auth, sizeof(Auth), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) - Serial.println("Auth error!"); - - /* Read block */ - status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) - Serial.print("Error reading sector!"); - - Serial.print("------------------------Sector "); - Serial.print(BLK_NB_MFC/4, DEC); - Serial.println("-------------------------"); - - PrintBuf(Resp+1, RespSize-2); +void PCD_MIFARE_scenario(void) { + Serial.println("Start reading process..."); + bool status; + unsigned char Resp[256]; + unsigned char RespSize; + /* Authenticate sector 1 with generic keys */ + unsigned char Auth[] = {0x40, BLK_NB_MFC / 4, 0x10, KEY_MFC}; + /* Read block 4 */ + unsigned char Read[] = {0x10, 0x30, BLK_NB_MFC}; + + /* Authenticate */ + status = nfc.readerTagCmd(Auth, sizeof(Auth), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) + Serial.println("Auth error!"); + + /* Read block */ + status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) + Serial.print("Error reading sector!"); + + Serial.print("------------------------Sector "); + Serial.print(BLK_NB_MFC / 4, DEC); + Serial.println("-------------------------"); + + PrintBuf(Resp + 1, RespSize - 2); } -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); + while (!Serial) + ; Serial.println("Read mifare classic data block 4 with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); - while (1); + while (1) + ; } - - if(nfc.configMode(mode)){ //Set up the configuration mode + + if (nfc.ConfigMode(mode)) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); - while (1); + while (1) + ; } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.StartDiscovery(mode); // NCI Discovery mode Serial.println("Waiting for an Mifare Classic Card ..."); } -void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - switch(RfInterface.Protocol) { +void loop() { + if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards + switch (RfInterface.Protocol) { case PROT_MIFARE: Serial.println(" - Found MIFARE card"); - switch(RfInterface.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): - char tmp[16]; - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); - - if(RfInterface.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); Serial.println(" "); - } + switch (RfInterface.ModeTech) { // Indetify card technology + case (MODE_POLL | TECH_PASSIVE_NFCA): + char tmp[16]; + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[0]); + Serial.print(tmp); + Serial.print(" "); + sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[1]); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); + + if (RfInterface.Info.NFC_APP.SelResLen != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SelRes[0]); + Serial.print(tmp); + Serial.println(" "); + } break; - } - PCD_MIFARE_scenario(); - break; - + } + PCD_MIFARE_scenario(); + break; + default: - Serial.println(" - Found a card, but it is not Mifare"); - break; + Serial.println(" - Found a card, but it is not Mifare"); + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + + //* It can detect multiple cards at the same time if they use the same protocol + if (RfInterface.MoreTags) { + nfc.readerActivateNext(&RfInterface); } - //* Wait for card removal + //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); Serial.println("CARD REMOVED!"); - + nfc.stopDiscovery(); nfc.StartDiscovery(mode); } diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index fcc5c7d..b1e4c02 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -30,7 +30,7 @@ uint8_t mode = 1; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } @@ -128,7 +128,7 @@ void setup(){ while (1); } - if(nfc.configMode(mode)){ //Set up the configuration mode + if(nfc.ConfigMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1); } @@ -137,7 +137,7 @@ void setup(){ } void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards + if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards switch(RfInterface.Protocol) { case PROT_MIFARE: Serial.println(" - Found MIFARE card"); diff --git a/examples/P2P_Discovery/P2P_Discovery.ino b/examples/P2P_Discovery/P2P_Discovery.ino index 5794f0b..1473e14 100644 --- a/examples/P2P_Discovery/P2P_Discovery.ino +++ b/examples/P2P_Discovery/P2P_Discovery.ino @@ -24,7 +24,7 @@ uint8_t mode = 3; // modes: 1 = void ResetMode(){ //Reset the configuration mode after each reading Serial.println("Re-initializing..."); - nfc.configMode(mode); + nfc.ConfigMode(mode); nfc.StartDiscovery(mode); } @@ -45,7 +45,7 @@ void setup(){ while (1); } - if(nfc.configMode(mode)){ //Set up the configuration mode + if(nfc.ConfigMode(mode)){ //Set up the configuration mode Serial.println("The Configure Mode failed!!"); while (1); } @@ -54,7 +54,7 @@ void setup(){ } void loop(){ - if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect + if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect if (RfInterface.Interface == INTF_NFCDEP) { if ((RfInterface.ModeTech & MODE_LISTEN) == MODE_LISTEN) Serial.println(" - P2P TARGET MODE: Activated from remote Initiator"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 35c83ba..6182390 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -260,7 +260,7 @@ uint8_t Electroniccats_PN7150::connectNCI() { /// @brief Update the internal mode, stop discovery, and build the command to configure the PN7150 chip based on the input mode /// @param modeSE /// @return SUCCESS or ERROR -uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { +uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { unsigned mode = (modeSE == 1 ? MODE_RW : modeSE == 2 ? MODE_CARDEMU : MODE_P2P); @@ -369,12 +369,7 @@ uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { uint8_t Electroniccats_PN7150::configMode() { int mode = Electroniccats_PN7150::getMode(); - return Electroniccats_PN7150::configMode(mode); -} - -// Deprecated, use configMode(void) instead -uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { - return Electroniccats_PN7150::configMode(modeSE); + return Electroniccats_PN7150::ConfigMode(mode); } bool Electroniccats_PN7150::configureSettings(void) { @@ -980,7 +975,7 @@ bool Electroniccats_PN7150::StopDiscovery() { return Electroniccats_PN7150::stopDiscovery(); } -bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout) { +bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout) { uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x01, protocol.ISODEP, interface.ISODEP}; // P2P Support @@ -1129,17 +1124,8 @@ bool Electroniccats_PN7150::waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint return SUCCESS; } -bool Electroniccats_PN7150::waitForDiscoveryNotification(uint16_t tout) { - return Electroniccats_PN7150::waitForDiscoveryNotification(&this->dummyRfInterface, tout); -} - bool Electroniccats_PN7150::isTagDetected(uint16_t tout) { - return !Electroniccats_PN7150::waitForDiscoveryNotification(tout); -} - -// Deprecated, use isTagDetected() instead -bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout) { - return Electroniccats_PN7150::waitForDiscoveryNotification(pRfIntf, tout); + return Electroniccats_PN7150::WaitForDiscoveryNotification(&this->dummyRfInterface, tout); } bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char DataSize) { diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 37b350d..9f41573 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -131,9 +131,8 @@ class Electroniccats_PN7150 : public Mode { int getFirmwareVersion(); int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t connectNCI(); - uint8_t configMode(uint8_t modeSE); // Deprecated, use configMode(void) instead - uint8_t configMode(void); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode(void) instead + uint8_t configMode(void); bool setReaderWriterMode(); bool setEmulationMode(); bool setP2PMode(); @@ -145,10 +144,8 @@ class Electroniccats_PN7150 : public Mode { uint8_t StartDiscovery(uint8_t modeSE); // Deprecated, use startDiscovery() instead bool stopDiscovery(); bool StopDiscovery(); // Deprecated, use stopDiscovery() instead - bool waitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); // Deprecated, use isTagDetected() instead - bool waitForDiscoveryNotification(uint16_t tout = 0); // Deprecated, use isTagDetected() instead - bool isTagDetected(uint16_t tout = 500); bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint16_t tout = 0); // Deprecated, use isTagDetected() instead + bool isTagDetected(uint16_t tout = 500); bool cardModeSend(unsigned char *pData, unsigned char DataSize); bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); From 0002f923b54e56415f6aeba27ff56eb0826a131d Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 14:46:39 -0600 Subject: [PATCH 097/106] feat: remove duplicated process card mode --- src/Electroniccats_PN7150.cpp | 13 ++----------- src/Electroniccats_PN7150.h | 4 +--- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 6182390..dec3bf4 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1181,7 +1181,7 @@ bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char return Electroniccats_PN7150::cardModeReceive(pData, pDataSize); } -void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { +void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { uint8_t Answer[MAX_NCI_FRAME_SIZE]; uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x00}; @@ -1229,17 +1229,8 @@ void Electroniccats_PN7150::processCardMode(RfIntf_t RfIntf) { } } -void Electroniccats_PN7150::processCardMode(void) { - Electroniccats_PN7150::processCardMode(this->dummyRfInterface); -} - void Electroniccats_PN7150::handleCardEmulation() { - Electroniccats_PN7150::processCardMode(); -} - -// Deprecated, use handleCardEmulation() instead -void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { - Electroniccats_PN7150::processCardMode(RfIntf); + Electroniccats_PN7150::ProcessCardMode(this->dummyRfInterface); } void Electroniccats_PN7150::processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation) { diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 9f41573..076a692 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -151,9 +151,7 @@ class Electroniccats_PN7150 : public Mode { bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead void handleCardEmulation(); - void processCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead - void processCardMode(void); // Deprecated, use handleCardEmulation() instead - void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead + void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use waitForTagRemoval(), readNdefMessage() or writeNdefMessage() and readNdefMessage() instead void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead void processP2pMode(RfIntf_t RfIntf); // TODO: rename it From dee0949fa5b65e40783487a45a193131cbe9ca79 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 15:11:43 -0600 Subject: [PATCH 098/106] fix: return value for is tag detected --- src/Electroniccats_PN7150.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index dec3bf4..cf0ea46 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1125,7 +1125,7 @@ bool Electroniccats_PN7150::WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint } bool Electroniccats_PN7150::isTagDetected(uint16_t tout) { - return Electroniccats_PN7150::WaitForDiscoveryNotification(&this->dummyRfInterface, tout); + return !Electroniccats_PN7150::WaitForDiscoveryNotification(&this->dummyRfInterface, tout); } bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char DataSize) { From 44ff34d099c881658d0864547d73210c60d0f3c6 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 16:10:51 -0600 Subject: [PATCH 099/106] feat: remove the pointer argument from reader re activate --- src/Electroniccats_PN7150.cpp | 4 ++-- src/Electroniccats_PN7150.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index cf0ea46..3091b7a 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1469,7 +1469,7 @@ bool Electroniccats_PN7150::ReaderTagCmd(unsigned char *pCommand, unsigned char return Electroniccats_PN7150::readerTagCmd(pCommand, CommandSize, pAnswer, pAnswerSize); } -bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { +bool Electroniccats_PN7150::readerReActivate() { uint8_t NCIDeactivate[] = {0x21, 0x06, 0x01, 0x01}; uint8_t NCIActivate[] = {0x21, 0x04, 0x03, 0x01, 0x00, 0x00}; @@ -1493,7 +1493,7 @@ bool Electroniccats_PN7150::readerReActivate(RfIntf_t *pRfIntf) { // Deprecated, use readerReActivate() instead bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { - return Electroniccats_PN7150::readerReActivate(pRfIntf); + return Electroniccats_PN7150::readerReActivate(); } bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 076a692..4f73d6a 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -151,7 +151,7 @@ class Electroniccats_PN7150 : public Mode { bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead void handleCardEmulation(); - void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead + void ProcessCardMode(RfIntf_t RfIntf); // Deprecated, use handleCardEmulation() instead void processReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use waitForTagRemoval(), readNdefMessage() or writeNdefMessage() and readNdefMessage() instead void ProcessReaderMode(RfIntf_t RfIntf, RW_Operation_t Operation); // Deprecated, use processReaderMode() instead void processP2pMode(RfIntf_t RfIntf); // TODO: rename it @@ -161,7 +161,7 @@ class Electroniccats_PN7150 : public Mode { void waitForTagRemoval(); bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead - bool readerReActivate(RfIntf_t *pRfIntf); // TODO: remove the pointer + bool readerReActivate(); bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool readerActivateNext(RfIntf_t *pRfIntf); // TODO: remove it bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead From d70c524dab52924d0be585b1c467b9622eefbe4c Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 16:28:55 -0600 Subject: [PATCH 100/106] feat: remove duplicated reader activate next --- .../ISO14443-3A_read_block/ISO14443-3A_read_block.ino | 2 +- .../ISO14443-3A_write_block.ino | 2 +- examples/ISO15693_read_block/ISO15693_read_block.ino | 2 +- examples/ISO15693_write_block/ISO15693_write_block.ino | 2 +- .../MifareClassic_read_block.ino | 2 +- .../MifareClassic_write_block.ino | 2 +- src/Electroniccats_PN7150.cpp | 10 +++++----- src/Electroniccats_PN7150.h | 6 +++--- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 3af2926..6a5500f 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -130,7 +130,7 @@ void loop() { //* It can detect multiple cards at the same time if they use the same protocol if (RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + nfc.activateNextTagDiscovery(); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 191e7e6..bc596ae 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -140,7 +140,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + nfc.activateNextTagDiscovery(); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index baf1d80..b297307 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -121,7 +121,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + nfc.activateNextTagDiscovery(); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 187b26a..9b365ce 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -134,7 +134,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + nfc.activateNextTagDiscovery(); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 55f6c91..78c16bb 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -138,7 +138,7 @@ void loop() { //* It can detect multiple cards at the same time if they use the same protocol if (RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + nfc.activateNextTagDiscovery(); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index b1e4c02..919b280 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -170,7 +170,7 @@ void loop(){ //* It can detect multiple cards at the same time if they use the same protocol if(RfInterface.MoreTags) { - nfc.readerActivateNext(&RfInterface); + nfc.activateNextTagDiscovery(); } //* Wait for card removal nfc.processReaderMode(RfInterface, PRESENCE_CHECK); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 3091b7a..39a6bbb 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1496,7 +1496,7 @@ bool Electroniccats_PN7150::ReaderReActivate(RfIntf_t *pRfIntf) { return Electroniccats_PN7150::readerReActivate(); } -bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { +bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { uint8_t NCIStopDiscovery[] = {0x21, 0x06, 0x01, 0x01}; uint8_t NCIRfDiscoverSelect[] = {0x21, 0x04, 0x03, 0x02, PROT_ISODEP, INTF_ISODEP}; @@ -1555,13 +1555,13 @@ bool Electroniccats_PN7150::readerActivateNext(RfIntf_t *pRfIntf) { } bool Electroniccats_PN7150::activateNextTagDiscovery() { - return !Electroniccats_PN7150::readerActivateNext(&this->dummyRfInterface); + return !Electroniccats_PN7150::ReaderActivateNext(&this->dummyRfInterface); } // Deprecated, use readerActivateNext() instead -bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { - return Electroniccats_PN7150::readerActivateNext(pRfIntf); -} +// bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { +// return Electroniccats_PN7150::activateNextTagDiscovery(pRfIntf); +// } void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 4f73d6a..4c682b5 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -162,10 +162,10 @@ class Electroniccats_PN7150 : public Mode { bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize); // Deprecated, use readerTagCmd() instead bool readerReActivate(); - bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead - bool readerActivateNext(RfIntf_t *pRfIntf); // TODO: remove it - bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead + bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool activateNextTagDiscovery(); + bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead + // bool activateNextTagDiscovery(); void readNdef(RfIntf_t RfIntf); // TODO: remove it void readNdefMessage(); void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead From ab374d8dfb0cc44f22f5c465a4d19f3aaf9c4e62 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 16:47:21 -0600 Subject: [PATCH 101/106] refactor: iso 14434-3a read block --- examples/DetectTags/DetectTags.ino | 1 + .../ISO14443-3A_read_block.ino | 48 ++++++++----------- examples/ISO14443-3A_read_block/Makefile | 20 ++++++++ src/Electroniccats_PN7150.cpp | 5 -- src/Electroniccats_PN7150.h | 1 - 5 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 examples/ISO14443-3A_read_block/Makefile diff --git a/examples/DetectTags/DetectTags.ino b/examples/DetectTags/DetectTags.ino index b31a617..5315597 100644 --- a/examples/DetectTags/DetectTags.ino +++ b/examples/DetectTags/DetectTags.ino @@ -63,6 +63,7 @@ void loop() { nfc.activateNextTagDiscovery(); Serial.println("Multiple cards are detected!"); } + Serial.println("Remove the Card"); nfc.waitForTagRemoval(); Serial.println("Card removed!"); diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 6a5500f..0c888e1 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -13,22 +13,13 @@ */ #include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) +#define PN7150_IRQ (11) +#define PN7150_VEN (13) #define PN7150_ADDR (0x28) #define BLK_NB_ISO14443_3A (5) // Block to be read it Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Intarface to save data for multiple tags - -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation - -void ResetMode() { // Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); -} void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; @@ -84,37 +75,37 @@ void setup() { ; } - if (nfc.ConfigMode(mode)) { // Set up the configuration mode + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode failed!!"); while (1) ; } - nfc.StartDiscovery(mode); // NCI Discovery mode + nfc.startDiscovery(); // NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } void loop() { - if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards - switch (RfInterface.Protocol) { + if (nfc.isTagDetected()) { + switch (nfc.remoteDevice.getProtocol()) { case PROT_T2T: Serial.println(" - Found ISO14443-3A(T2T) card"); - switch (RfInterface.ModeTech) { // Indetify card technology + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology case (MODE_POLL | TECH_PASSIVE_NFCA): char tmp[16]; Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[0]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[0]); Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[1]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[1]); Serial.print(tmp); Serial.println(" "); Serial.print("\tNFCID = "); - PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); + PrintBuf(nfc.remoteDevice.getNFCID(), nfc.remoteDevice.getNFCIDLen()); - if (RfInterface.Info.NFC_APP.SelResLen != 0) { + if (nfc.remoteDevice.getSelResLen() != 0) { Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SelRes[0]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSelRes()[0]); Serial.print(tmp); Serial.println(" "); } @@ -129,16 +120,17 @@ void loop() { } //* It can detect multiple cards at the same time if they use the same protocol - if (RfInterface.MoreTags) { + if (nfc.remoteDevice.hasMoreTags()) { nfc.activateNextTagDiscovery(); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("CARD REMOVED!"); - nfc.stopDiscovery(); - nfc.StartDiscovery(mode); + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); + Serial.println("CARD REMOVED!"); } - ResetMode(); + + Serial.println("Restarting..."); + nfc.reset(); + Serial.println("Waiting for a Card..."); delay(500); } diff --git a/examples/ISO14443-3A_read_block/Makefile b/examples/ISO14443-3A_read_block/Makefile new file mode 100644 index 0000000..67609a6 --- /dev/null +++ b/examples/ISO14443-3A_read_block/Makefile @@ -0,0 +1,20 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +# BOARD_TAG = rp2040:rp2040:generic +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) --warnings all + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 39a6bbb..f3979d9 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1558,11 +1558,6 @@ bool Electroniccats_PN7150::activateNextTagDiscovery() { return !Electroniccats_PN7150::ReaderActivateNext(&this->dummyRfInterface); } -// Deprecated, use readerActivateNext() instead -// bool Electroniccats_PN7150::ReaderActivateNext(RfIntf_t *pRfIntf) { -// return Electroniccats_PN7150::activateNextTagDiscovery(pRfIntf); -// } - void Electroniccats_PN7150::readNdef(RfIntf_t RfIntf) { uint8_t Cmd[MAX_NCI_FRAME_SIZE]; uint16_t CmdSize = 0; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 4c682b5..04781fe 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -165,7 +165,6 @@ class Electroniccats_PN7150 : public Mode { bool ReaderReActivate(RfIntf_t *pRfIntf); // Deprecated, use readerReActivate() instead bool activateNextTagDiscovery(); bool ReaderActivateNext(RfIntf_t *pRfIntf); // Deprecated, use activateNextTagDiscovery() instead - // bool activateNextTagDiscovery(); void readNdef(RfIntf_t RfIntf); // TODO: remove it void readNdefMessage(); void ReadNdef(RfIntf_t RfIntf); // Deprecated, use readNdefMessage() instead From f7dfb2d44430e2c8d01b027122cb74f9f1724915 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 16:59:57 -0600 Subject: [PATCH 102/106] refactor: iso 14434-3a write block --- .../ISO14443-3A_write_block.ino | 214 +++++++++--------- examples/ISO14443-3A_write_block/Makefile | 20 ++ 2 files changed, 126 insertions(+), 108 deletions(-) create mode 100644 examples/ISO14443-3A_write_block/Makefile diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index bc596ae..48183fa 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -1,154 +1,152 @@ /** - * Example to write a ISO14443-3A(Tag Type 2 - T2T) block 5 and show its information - * Authors: + * Example to write a ISO14443-3A(Tag Type 2 - T2T) block 5 and show its information + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * November 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) -#define BLK_NB_ISO14443_3A (5) //Block to be read it -#define DATA_WRITE_ISO14443_3A 0x11, 0x22, 0x33, 0x44 //Data to write +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +#define BLK_NB_ISO14443_3A (5) // Block to be read it +#define DATA_WRITE_ISO14443_3A 0x11, 0x22, 0x33, 0x44 // Data to write -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -void ResetMode(){ //Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); -} - -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++){ + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); + Serial.print(data[szPos] & 0xff, HEX); if ((numBytes > 1) && (szPos != numBytes - 1)) Serial.print(F(" ")); } Serial.println(); } -void PCD_ISO14443_3A_scenario (void){ - bool status; - unsigned char Resp[256]; - unsigned char RespSize; - /* Read block */ - unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A}; - /* Write block */ - unsigned char WriteBlock[] = {0xA2, BLK_NB_ISO14443_3A, DATA_WRITE_ISO14443_3A}; - - // Write - status = nfc.readerTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) - { - Serial.print("Error writing block: "); - Serial.print(ReadBlock[1],HEX); - Serial.print(" with error: "); - Serial.print(Resp[RespSize-1],HEX); - return; - } - Serial.print("Wrote: "); - Serial.println(WriteBlock[1]); - - //Read block - status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ - Serial.print("Error reading block: "); - Serial.print(ReadBlock[1],HEX); - Serial.print(" with error: "); - Serial.print(Resp[RespSize-1],HEX); - return; - } - Serial.print("------------------------Block "); - Serial.print(BLK_NB_ISO14443_3A, HEX); - Serial.println("-------------------------"); - PrintBuf(Resp, 4); +void PCD_ISO14443_3A_scenario(void) { + bool status; + unsigned char Resp[256]; + unsigned char RespSize; + /* Read block */ + unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A}; + /* Write block */ + unsigned char WriteBlock[] = {0xA2, BLK_NB_ISO14443_3A, DATA_WRITE_ISO14443_3A}; + + // Write + status = nfc.readerTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.print("Error writing block: "); + Serial.print(ReadBlock[1], HEX); + Serial.print(" with error: "); + Serial.print(Resp[RespSize - 1], HEX); + return; + } + Serial.print("Wrote: "); + Serial.println(WriteBlock[1]); + + // Read block + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0x00)) { + Serial.print("Error reading block: "); + Serial.print(ReadBlock[1], HEX); + Serial.print(" with error: "); + Serial.print(Resp[RespSize - 1], HEX); + return; + } + Serial.print("------------------------Block "); + Serial.print(BLK_NB_ISO14443_3A, HEX); + Serial.println("-------------------------"); + PrintBuf(Resp, 4); } -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); + while (!Serial) + ; Serial.println("Write ISO14443-3A(T2T) data block 5 with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings failed!"); - while (1); + while (1) + ; } - - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode failed!!"); - while (1); + while (1) + ; } - nfc.StartDiscovery(mode); //NCI Discovery mode + nfc.startDiscovery(); // NCI Discovery mode Serial.println("Waiting for an ISO14443-3A Card ..."); } -void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - switch(RfInterface.Protocol) { +void loop() { + if (nfc.isTagDetected()) { + switch (nfc.remoteDevice.getProtocol()) { case PROT_T2T: Serial.println(" - Found ISO14443-3A(T2T) card"); - switch(RfInterface.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): - char tmp[16]; - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); - - if(RfInterface.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); Serial.println(" "); - } + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology + case (MODE_POLL | TECH_PASSIVE_NFCA): + char tmp[16]; + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[0]); + Serial.print(tmp); + Serial.print(" "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[1]); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(nfc.remoteDevice.getNFCID(), nfc.remoteDevice.getNFCIDLen()); + + if (nfc.remoteDevice.getSelResLen() != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSelRes()[0]); + Serial.print(tmp); + Serial.println(" "); + } break; - } - PCD_ISO14443_3A_scenario(); - break; - + } + PCD_ISO14443_3A_scenario(); + break; + default: - Serial.println(" - Found a card, but it is not ISO14443-3A(T2T)!"); - break; + Serial.println(" - Found a card, but it is not ISO14443-3A(T2T)!"); + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.activateNextTagDiscovery(); + + //* It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + nfc.activateNextTagDiscovery(); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); + + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); Serial.println("CARD REMOVED!"); - - nfc.stopDiscovery(); - nfc.StartDiscovery(mode); } - ResetMode(); + + Serial.println("Restarting..."); + nfc.reset(); + Serial.println("Waiting for a Card..."); delay(500); } diff --git a/examples/ISO14443-3A_write_block/Makefile b/examples/ISO14443-3A_write_block/Makefile new file mode 100644 index 0000000..67609a6 --- /dev/null +++ b/examples/ISO14443-3A_write_block/Makefile @@ -0,0 +1,20 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +# BOARD_TAG = rp2040:rp2040:generic +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) --warnings all + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file From ead008840d07c762e6f370a515a72c091bc4e055 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 17:14:52 -0600 Subject: [PATCH 103/106] refactor: iso 15693 read block --- .../ISO14443-3A_read_block.ino | 2 +- .../ISO14443-3A_write_block.ino | 2 +- .../ISO15693_read_block.ino | 177 +++++++++--------- examples/ISO15693_read_block/Makefile | 19 ++ 4 files changed, 107 insertions(+), 93 deletions(-) create mode 100644 examples/ISO15693_read_block/Makefile diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index 0c888e1..d9b1c16 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -131,6 +131,6 @@ void loop() { Serial.println("Restarting..."); nfc.reset(); - Serial.println("Waiting for a Card..."); + Serial.println("Waiting for an ISO14443-3A Card ..."); delay(500); } diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 48183fa..4dece9c 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -147,6 +147,6 @@ void loop() { Serial.println("Restarting..."); nfc.reset(); - Serial.println("Waiting for a Card..."); + Serial.println("Waiting for an ISO14443-3A Card ..."); delay(500); } diff --git a/examples/ISO15693_read_block/ISO15693_read_block.ino b/examples/ISO15693_read_block/ISO15693_read_block.ino index b297307..62376f3 100644 --- a/examples/ISO15693_read_block/ISO15693_read_block.ino +++ b/examples/ISO15693_read_block/ISO15693_read_block.ino @@ -1,135 +1,130 @@ /** - * Example to read a ISO15693 block 8 and show its information - * Authors: + * Example to read a ISO15693 block 8 and show its information + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * November 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) -#define BLK_NB_ISO15693 (8) //Block to be read it +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) +#define BLK_NB_ISO15693 (8) // Block to be read it -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation - -void ResetMode(){ //Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); -} - -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++){ + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); + Serial.print(data[szPos] & 0xff, HEX); if ((numBytes > 1) && (szPos != numBytes - 1)) Serial.print(F(" ")); } Serial.println(); } -void PCD_ISO15693_scenario (void){ - #define BLK_NB_ISO15693 8 - - bool status; - unsigned char Resp[256]; - unsigned char RespSize; - unsigned char ReadBlock[] = {0x02, 0x20, BLK_NB_ISO15693}; - - status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ - Serial.print("Error reading block: "); - Serial.print(ReadBlock[2],HEX); - Serial.print(" with error: "); - Serial.print(Resp[RespSize-1],HEX); - return; - } - Serial.print("------------------------Block "); - Serial.print(BLK_NB_ISO15693, HEX); - Serial.println("-------------------------"); - PrintBuf(Resp+1, RespSize-2); +void PCD_ISO15693_scenario(void) { +#define BLK_NB_ISO15693 8 + + bool status; + unsigned char Resp[256]; + unsigned char RespSize; + unsigned char ReadBlock[] = {0x02, 0x20, BLK_NB_ISO15693}; + + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0x00)) { + Serial.print("Error reading block: "); + Serial.print(ReadBlock[2], HEX); + Serial.print(" with error: "); + Serial.print(Resp[RespSize - 1], HEX); + return; + } + Serial.print("------------------------Block "); + Serial.print(BLK_NB_ISO15693, HEX); + Serial.println("-------------------------"); + PrintBuf(Resp + 1, RespSize - 2); } -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); + while (!Serial) + ; Serial.println("Read ISO15693 data block 8 with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); - while (1); + while (1) + ; } - - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); - while (1); + while (1) + ; } - nfc.StartDiscovery(mode); //NCI Discovery mode - Serial.println("Waiting for an ISO15693 Card ..."); + nfc.startDiscovery(); // NCI Discovery mode + Serial.println("Waiting for an ISO15693 Card..."); } -void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - switch(RfInterface.Protocol) { - case PROT_ISO15693: +void loop() { + if (nfc.isTagDetected()) { + switch (nfc.remoteDevice.getProtocol()) { + case nfc.protocol.ISO15693: Serial.println(" - Found ISO15693 card"); - switch(RfInterface.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_15693): - char tmp[16]; - Serial.print("\tID = "); - PrintBuf(RfInterface.Info.NFC_VPP.ID, sizeof(RfInterface.Info.NFC_VPP.ID)); - - Serial.print("\tAFI = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_VPP.AFI); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tDSFID = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_VPP.DSFID); - Serial.print(tmp); Serial.println(" "); + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology + case (nfc.tech.PASSIVE_15693): + char tmp[16]; + Serial.print("\tID = "); + PrintBuf(nfc.remoteDevice.getID(), sizeof(nfc.remoteDevice.getID())); + + Serial.print("\tAFI = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getAFI()); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tDSFID = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getDSFID()); + Serial.print(tmp); + Serial.println(" "); break; - } - PCD_ISO15693_scenario(); - break; - + } + PCD_ISO15693_scenario(); + break; + default: - Serial.println(" - Found a card, but it is not ISO15693!"); - break; + Serial.println(" - Found a card, but it is not ISO15693!"); + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.activateNextTagDiscovery(); + + //* It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + nfc.activateNextTagDiscovery(); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("CARD REMOVED!"); - nfc.stopDiscovery(); - nfc.StartDiscovery(mode); + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); + Serial.println("CARD REMOVED!");; } - ResetMode(); + nfc.reset(); + Serial.println("Waiting for an ISO15693 Card..."); delay(500); } diff --git a/examples/ISO15693_read_block/Makefile b/examples/ISO15693_read_block/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/ISO15693_read_block/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file From 646f3d549e43405418b637fee7c77f39b80fe81e Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 17:21:52 -0600 Subject: [PATCH 104/106] refactor: iso 15693 write block --- .../ISO15693_write_block.ino | 204 +++++++++--------- examples/ISO15693_write_block/Makefile | 19 ++ 2 files changed, 119 insertions(+), 104 deletions(-) create mode 100644 examples/ISO15693_write_block/Makefile diff --git a/examples/ISO15693_write_block/ISO15693_write_block.ino b/examples/ISO15693_write_block/ISO15693_write_block.ino index 9b365ce..b314b0c 100644 --- a/examples/ISO15693_write_block/ISO15693_write_block.ino +++ b/examples/ISO15693_write_block/ISO15693_write_block.ino @@ -1,148 +1,144 @@ /** - * Example to write a ISO15693 block 8 and show its information - * Authors: + * Example to write a ISO15693 block 8 and show its information + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * November 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) -#define BLK_NB_ISO15693 (8) //Block to write -#define DATA_WRITE_ISO15693 0x11, 0x22, 0x33, 0x44 //Data to write +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +#define BLK_NB_ISO15693 (8) // Block to write +#define DATA_WRITE_ISO15693 0x11, 0x22, 0x33, 0x44 // Data to write -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); + while (!Serial) + ; Serial.println("Write ISO15693 data block 8 with PN7150"); - - Serial.println("Initializing..."); - if (nfc.connectNCI()) { //Wake up the board + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); - while (1); + while (1) + ; } - - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); - while (1); + while (1) + ; } - nfc.StartDiscovery(mode); //NCI Discovery mode - Serial.println("Waiting for an ISO15693 Card ..."); -} - -void ResetMode(){ //Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); + nfc.startDiscovery(); // NCI Discovery mode + Serial.println("Waiting for an ISO15693 Card..."); } -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++){ + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); + Serial.print(data[szPos] & 0xff, HEX); if ((numBytes > 1) && (szPos != numBytes - 1)) Serial.print(F(" ")); } Serial.println(); } -void PCD_ISO15693_scenario (void){ - bool status; - unsigned char Resp[256]; - unsigned char RespSize; - unsigned char ReadBlock[] = {0x02, 0x20, BLK_NB_ISO15693}; - unsigned char WriteBlock[] = {0x02, 0x21, BLK_NB_ISO15693, DATA_WRITE_ISO15693}; - - // Write - status = nfc.readerTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) - { - Serial.print("Error writing block: "); - Serial.print(ReadBlock[2],HEX); - Serial.print(" with error: "); - Serial.print(Resp[RespSize-1],HEX); - return; - } - Serial.print("Wrote: "); - Serial.println(WriteBlock[2]); - - // Read - status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){ - Serial.print("Error reading block: "); - Serial.print(ReadBlock[2],HEX); - Serial.print(" with error: "); - Serial.print(Resp[RespSize-1],HEX); - return; - } - Serial.print("------------------------Block "); - Serial.print(BLK_NB_ISO15693, HEX); - Serial.println("-------------------------"); - PrintBuf(Resp+1, RespSize-2); +void PCD_ISO15693_scenario(void) { + bool status; + unsigned char Resp[256]; + unsigned char RespSize; + unsigned char ReadBlock[] = {0x02, 0x20, BLK_NB_ISO15693}; + unsigned char WriteBlock[] = {0x02, 0x21, BLK_NB_ISO15693, DATA_WRITE_ISO15693}; + + // Write + status = nfc.readerTagCmd(WriteBlock, sizeof(WriteBlock), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.print("Error writing block: "); + Serial.print(ReadBlock[2], HEX); + Serial.print(" with error: "); + Serial.print(Resp[RespSize - 1], HEX); + return; + } + Serial.print("Wrote: "); + Serial.println(WriteBlock[2]); + + // Read + status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0x00)) { + Serial.print("Error reading block: "); + Serial.print(ReadBlock[2], HEX); + Serial.print(" with error: "); + Serial.print(Resp[RespSize - 1], HEX); + return; + } + Serial.print("------------------------Block "); + Serial.print(BLK_NB_ISO15693, HEX); + Serial.println("-------------------------"); + PrintBuf(Resp + 1, RespSize - 2); } -void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - switch(RfInterface.Protocol) { - case PROT_ISO15693: +void loop() { + if (nfc.isTagDetected()) { + switch (nfc.remoteDevice.getProtocol()) { + case nfc.protocol.ISO15693: Serial.println(" - Found ISO15693 card"); - switch(RfInterface.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_15693): - char tmp[16]; - Serial.print("\tID = "); - PrintBuf(RfInterface.Info.NFC_VPP.ID, sizeof(RfInterface.Info.NFC_VPP.ID)); - - Serial.print("\tAFI = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_VPP.AFI); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tDSFID = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_VPP.DSFID); - Serial.print(tmp); Serial.println(" "); + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology + case (nfc.tech.PASSIVE_15693): + char tmp[16]; + Serial.print("\tID = "); + PrintBuf(nfc.remoteDevice.getID(), sizeof(nfc.remoteDevice.getID())); + + Serial.print("\tAFI = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getAFI()); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tDSFID = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getDSFID()); + Serial.print(tmp); + Serial.println(" "); break; - } - PCD_ISO15693_scenario(); - break; - + } + PCD_ISO15693_scenario(); + break; + default: - Serial.println(" - Found a card, but it is not ISO15693!"); - break; + Serial.println(" - Found a card, but it is not ISO15693!"); + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.activateNextTagDiscovery(); + + //* It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + nfc.activateNextTagDiscovery(); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); + + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); Serial.println("CARD REMOVED!"); - - nfc.stopDiscovery(); - nfc.StartDiscovery(mode); + ; } - ResetMode(); + nfc.reset(); + Serial.println("Waiting for an ISO15693 Card..."); delay(500); } diff --git a/examples/ISO15693_write_block/Makefile b/examples/ISO15693_write_block/Makefile new file mode 100644 index 0000000..4ab2c77 --- /dev/null +++ b/examples/ISO15693_write_block/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem1101 + +compile: + arduino-cli compile --fqbn $(BOARD_TAG) + +upload: + arduino-cli upload -p $(MONITOR_PORT) --fqbn $(BOARD_TAG) --verbose + +monitor: + arduino-cli monitor -p $(MONITOR_PORT) + +clean: + arduino-cli cache clean + +wait: + sleep 2 + +all: compile upload wait monitor \ No newline at end of file From ea5b4ca00e097bd77711e22e4fbbc50e292d5763 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 17:32:18 -0600 Subject: [PATCH 105/106] refactor: mifare classic read block --- .../ISO14443-3A_read_block.ino | 4 +- .../ISO14443-3A_write_block.ino | 4 +- .../MifareClassic_read_block.ino | 50 ++++++++----------- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino index d9b1c16..3444f98 100644 --- a/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino +++ b/examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino @@ -81,7 +81,7 @@ void setup() { ; } nfc.startDiscovery(); // NCI Discovery mode - Serial.println("Waiting for an ISO14443-3A Card ..."); + Serial.println("Waiting for an ISO14443-3A Card..."); } void loop() { @@ -131,6 +131,6 @@ void loop() { Serial.println("Restarting..."); nfc.reset(); - Serial.println("Waiting for an ISO14443-3A Card ..."); + Serial.println("Waiting for an ISO14443-3A Card..."); delay(500); } diff --git a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino index 4dece9c..8af33b8 100644 --- a/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino +++ b/examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino @@ -97,7 +97,7 @@ void setup() { ; } nfc.startDiscovery(); // NCI Discovery mode - Serial.println("Waiting for an ISO14443-3A Card ..."); + Serial.println("Waiting for an ISO14443-3A Card..."); } void loop() { @@ -147,6 +147,6 @@ void loop() { Serial.println("Restarting..."); nfc.reset(); - Serial.println("Waiting for an ISO14443-3A Card ..."); + Serial.println("Waiting for an ISO14443-3A Card..."); delay(500); } diff --git a/examples/MifareClassic_read_block/MifareClassic_read_block.ino b/examples/MifareClassic_read_block/MifareClassic_read_block.ino index 78c16bb..6c9e0d8 100644 --- a/examples/MifareClassic_read_block/MifareClassic_read_block.ino +++ b/examples/MifareClassic_read_block/MifareClassic_read_block.ino @@ -21,15 +21,6 @@ #define KEY_MFC 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // Default Mifare Classic key Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 11 (IRQ) and 13 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; // Intarface to save data for multiple tags - -uint8_t mode = 1; -// modes: 1 = Reader/ Writer, 2 = Emulation -void ResetMode() { // Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); -} void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; @@ -92,37 +83,37 @@ void setup() { ; } - if (nfc.ConfigMode(mode)) { // Set up the configuration mode + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); while (1) ; } - nfc.StartDiscovery(mode); // NCI Discovery mode - Serial.println("Waiting for an Mifare Classic Card ..."); + nfc.startDiscovery(); // NCI Discovery mode + Serial.println("Waiting for an Mifare Classic Card..."); } void loop() { - if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards - switch (RfInterface.Protocol) { - case PROT_MIFARE: + if (nfc.isTagDetected()) { + switch (nfc.remoteDevice.getProtocol()) { + case nfc.protocol.MIFARE: Serial.println(" - Found MIFARE card"); - switch (RfInterface.ModeTech) { // Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology + case (nfc.tech.PASSIVE_NFCA): char tmp[16]; Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[0]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[0]); Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[1]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[1]); Serial.print(tmp); Serial.println(" "); Serial.print("\tNFCID = "); - PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); + PrintBuf(nfc.remoteDevice.getNFCID(), nfc.remoteDevice.getNFCIDLen()); - if (RfInterface.Info.NFC_APP.SelResLen != 0) { + if (nfc.remoteDevice.getSelResLen() != 0) { Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SelRes[0]); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSelRes()[0]); Serial.print(tmp); Serial.println(" "); } @@ -137,16 +128,17 @@ void loop() { } //* It can detect multiple cards at the same time if they use the same protocol - if (RfInterface.MoreTags) { + if (nfc.remoteDevice.hasMoreTags()) { nfc.activateNextTagDiscovery(); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); - Serial.println("CARD REMOVED!"); - nfc.stopDiscovery(); - nfc.StartDiscovery(mode); + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); + Serial.println("CARD REMOVED!"); } - ResetMode(); + + Serial.println("Restarting..."); + nfc.reset(); + Serial.println("Waiting for an Mifare Classic Card..."); delay(500); } From 2a1a873ee07303429c78bee742e5ea0bfbc92865 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 31 Aug 2023 17:36:48 -0600 Subject: [PATCH 106/106] refactor: mifare classic write block --- .../MifareClassic_write_block.ino | 277 +++++++++--------- 1 file changed, 137 insertions(+), 140 deletions(-) diff --git a/examples/MifareClassic_write_block/MifareClassic_write_block.ino b/examples/MifareClassic_write_block/MifareClassic_write_block.ino index 919b280..7e0cb0a 100644 --- a/examples/MifareClassic_write_block/MifareClassic_write_block.ino +++ b/examples/MifareClassic_write_block/MifareClassic_write_block.ino @@ -1,184 +1,181 @@ /** - * Example to write a Mifare Classic block 4 and show its information - * Authors: + * Example to write a Mifare Classic block 4 and show its information + * Authors: * Salvador Mendoza - @Netxing - salmg.net * For Electronic Cats - electroniccats.com - * + * * March 2020 - * - * This code is beerware; if you see me (or any other collaborator - * member) at the local, and you've found our code helpful, + * + * This code is beerware; if you see me (or any other collaborator + * member) at the local, and you've found our code helpful, * please buy us a round! * Distributed as-is; no warranty is given. */ - -#include "Electroniccats_PN7150.h" -#define PN7150_IRQ (15) -#define PN7150_VEN (14) -#define PN7150_ADDR (0x28) -#define BLK_NB_MFC 4 // Block that wants to be read -#define KEY_MFC 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // Default Mifare Classic key +#include "Electroniccats_PN7150.h" +#define PN7150_IRQ (11) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) -// Data to be written in the Mifare Classic block -#define DATA_WRITE_MFC 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff - -Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -RfIntf_t RfInterface; //Intarface to save data for multiple tags +#define BLK_NB_MFC 4 // Block that wants to be read +#define KEY_MFC 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // Default Mifare Classic key -uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation +// Data to be written in the Mifare Classic block +#define DATA_WRITE_MFC 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff -void ResetMode(){ //Reset the configuration mode after each reading - Serial.println("Re-initializing..."); - nfc.ConfigMode(mode); - nfc.StartDiscovery(mode); -} +Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28 -void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format +void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values if (data[szPos] <= 0xF) Serial.print(F("0")); - Serial.print(data[szPos]&0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) - { + Serial.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { Serial.print(F(" ")); } } Serial.println(); } -uint8_t PCD_MIFARE_scenario (void){ - Serial.println("Start reading process..."); - bool status; - unsigned char Resp[256]; - unsigned char RespSize; - /* Authenticate sector 1 with generic keys */ - unsigned char Auth[] = {0x40, BLK_NB_MFC/4, 0x10, KEY_MFC}; - /* Read block 4 */ - unsigned char Read[] = {0x10, 0x30, BLK_NB_MFC}; - /* Write block 4 */ - unsigned char WritePart1[] = {0x10, 0xA0, BLK_NB_MFC}; - unsigned char WritePart2[] = {0x10, DATA_WRITE_MFC}; - - /* Authenticate */ - status = nfc.readerTagCmd(Auth, sizeof(Auth), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ - Serial.println("Auth error!"); - return 1; - } - - /* Read block */ - status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ - Serial.print("Error reading block!"); - return 2; - } - Serial.print("------------------------Sector "); - Serial.print(BLK_NB_MFC/4, DEC); - Serial.println("-------------------------"); - Serial.print("-------------------------Block "); - Serial.print(BLK_NB_MFC, DEC); - Serial.println("-------------------------"); - PrintBuf(Resp+1, RespSize-2); - - /* Write block */ - status = nfc.readerTagCmd(WritePart1, sizeof(WritePart1), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ - Serial.print("Error writing block!"); - return 3; - } - status = nfc.readerTagCmd(WritePart2, sizeof(WritePart2), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)){ - Serial.print("Error writing block!"); - return 4; - } - /* Read block again to see te changes*/ - status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); - if((status == NFC_ERROR) || (Resp[RespSize-1] != 0)) - { - Serial.print("Error reading block!"); - return 5; - } - Serial.print("------------------------Sector "); - Serial.print(BLK_NB_MFC/4, DEC); - Serial.println("-------------------------"); - Serial.print("----------------- New Data in Block "); - Serial.print(BLK_NB_MFC, DEC); - Serial.println("-----------------"); - PrintBuf(Resp+1, RespSize-2); - return 0; +uint8_t PCD_MIFARE_scenario(void) { + Serial.println("Start reading process..."); + bool status; + unsigned char Resp[256]; + unsigned char RespSize; + /* Authenticate sector 1 with generic keys */ + unsigned char Auth[] = {0x40, BLK_NB_MFC / 4, 0x10, KEY_MFC}; + /* Read block 4 */ + unsigned char Read[] = {0x10, 0x30, BLK_NB_MFC}; + /* Write block 4 */ + unsigned char WritePart1[] = {0x10, 0xA0, BLK_NB_MFC}; + unsigned char WritePart2[] = {0x10, DATA_WRITE_MFC}; + + /* Authenticate */ + status = nfc.readerTagCmd(Auth, sizeof(Auth), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.println("Auth error!"); + return 1; + } + + /* Read block */ + status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.print("Error reading block!"); + return 2; + } + Serial.print("------------------------Sector "); + Serial.print(BLK_NB_MFC / 4, DEC); + Serial.println("-------------------------"); + Serial.print("-------------------------Block "); + Serial.print(BLK_NB_MFC, DEC); + Serial.println("-------------------------"); + PrintBuf(Resp + 1, RespSize - 2); + + /* Write block */ + status = nfc.readerTagCmd(WritePart1, sizeof(WritePart1), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.print("Error writing block!"); + return 3; + } + status = nfc.readerTagCmd(WritePart2, sizeof(WritePart2), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.print("Error writing block!"); + return 4; + } + /* Read block again to see te changes*/ + status = nfc.readerTagCmd(Read, sizeof(Read), Resp, &RespSize); + if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0)) { + Serial.print("Error reading block!"); + return 5; + } + Serial.print("------------------------Sector "); + Serial.print(BLK_NB_MFC / 4, DEC); + Serial.println("-------------------------"); + Serial.print("----------------- New Data in Block "); + Serial.print(BLK_NB_MFC, DEC); + Serial.println("-----------------"); + PrintBuf(Resp + 1, RespSize - 2); + return 0; } -void setup(){ +void setup() { Serial.begin(9600); - while(!Serial); - Serial.println("Write mifare classic data block 4 with PN7150"); - - if (nfc.connectNCI()) { //Wake up the board + while (!Serial) + ; + Serial.println("Read mifare classic data block 4 with PN7150"); + + Serial.println("Initializing..."); + if (nfc.connectNCI()) { // Wake up the board Serial.println("Error while setting up the mode, check connections!"); - while (1); + while (1) + ; } - + if (nfc.configureSettings()) { Serial.println("The Configure Settings is failed!"); - while (1); + while (1) + ; } - - if(nfc.ConfigMode(mode)){ //Set up the configuration mode + + if (nfc.configMode()) { // Set up the configuration mode Serial.println("The Configure Mode is failed!!"); - while (1); + while (1) + ; } - nfc.StartDiscovery(mode); //NCI Discovery mode - Serial.println("Waiting for an Mifare Classic Card ..."); + nfc.startDiscovery(); // NCI Discovery mode + Serial.println("Waiting for an Mifare Classic Card..."); } -void loop(){ - if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards - switch(RfInterface.Protocol) { - case PROT_MIFARE: +void loop() { + if (nfc.isTagDetected()) { + switch (nfc.remoteDevice.getProtocol()) { + case nfc.protocol.MIFARE: Serial.println(" - Found MIFARE card"); - switch(RfInterface.ModeTech) { //Indetify card technology - case (MODE_POLL | TECH_PASSIVE_NFCA): - char tmp[16]; - Serial.print("\tSENS_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[0]); - Serial.print(tmp); Serial.print(" "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[1]); - Serial.print(tmp); Serial.println(" "); - - Serial.print("\tNFCID = "); - PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen); - - if(RfInterface.Info.NFC_APP.SelResLen != 0) { - Serial.print("\tSEL_RES = "); - sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SelRes[0]); - Serial.print(tmp); Serial.println(" "); - } + switch (nfc.remoteDevice.getModeTech()) { // Indetify card technology + case (nfc.tech.PASSIVE_NFCA): + char tmp[16]; + Serial.print("\tSENS_RES = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[0]); + Serial.print(tmp); + Serial.print(" "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSensRes()[1]); + Serial.print(tmp); + Serial.println(" "); + + Serial.print("\tNFCID = "); + PrintBuf(nfc.remoteDevice.getNFCID(), nfc.remoteDevice.getNFCIDLen()); + + if (nfc.remoteDevice.getSelResLen() != 0) { + Serial.print("\tSEL_RES = "); + sprintf(tmp, "0x%.2X", nfc.remoteDevice.getSelRes()[0]); + Serial.print(tmp); + Serial.println(" "); + } break; - } - PCD_MIFARE_scenario(); - break; - + } + PCD_MIFARE_scenario(); + break; + default: - Serial.println(" - Found a card, but it is not Mifare"); - break; + Serial.println(" - Found a card, but it is not Mifare"); + break; } - - //* It can detect multiple cards at the same time if they use the same protocol - if(RfInterface.MoreTags) { - nfc.activateNextTagDiscovery(); + + //* It can detect multiple cards at the same time if they use the same protocol + if (nfc.remoteDevice.hasMoreTags()) { + nfc.activateNextTagDiscovery(); } - //* Wait for card removal - nfc.processReaderMode(RfInterface, PRESENCE_CHECK); + + Serial.println("Remove the Card"); + nfc.waitForTagRemoval(); Serial.println("CARD REMOVED!"); - - nfc.stopDiscovery(); - nfc.StartDiscovery(mode); } - ResetMode(); + + Serial.println("Restarting..."); + nfc.reset(); + Serial.println("Waiting for an Mifare Classic Card..."); delay(500); }