From 56ba12c8fc3a9d558bd6aa30f177344aadbf5984 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 1 Dec 2023 17:38:03 -0600 Subject: [PATCH 01/30] refactor: record type --- examples/NDEFRead/Makefile | 2 +- examples/NDEFRead/NDEFRead.ino | 20 ++++++++++---------- src/NdefRecord.cpp | 6 +++--- src/NdefRecord.h | 3 ++- src/ndef_helper.h | 20 ++++++++++++++++++++ 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/examples/NDEFRead/Makefile b/examples/NDEFRead/Makefile index 67609a6..5872a38 100644 --- a/examples/NDEFRead/Makefile +++ b/examples/NDEFRead/Makefile @@ -1,6 +1,6 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat # BOARD_TAG = rp2040:rp2040:generic -MONITOR_PORT = /dev/cu.usbmodem1101 +MONITOR_PORT = /dev/cu.usbmodem11401 compile: arduino-cli compile --fqbn $(BOARD_TAG) --warnings all diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 05cdeb1..3ee6f6d 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -95,7 +95,7 @@ void messageReceived() { return; } - // Show NDEF message details + // Show NDEF message details, it is composed of records do { record.create(message.getRecord()); // Get a new record every time we call getRecord() displayRecordInfo(record); @@ -204,23 +204,23 @@ void displayRecordInfo(NdefRecord record) { Serial.println("--- NDEF record received:"); switch (record.getType()) { - case MEDIA_VCARD: + case record.type.MEDIA_VCARD: Serial.println("vCard:"); Serial.println(record.getVCardContent()); break; - case WELL_KNOWN_SIMPLE_TEXT: + case record.type.WELL_KNOWN_SIMPLE_TEXT: Serial.println("\tWell known simple text"); Serial.println("\t- Text record: " + record.getText()); break; - case WELL_KNOWN_SIMPLE_URI: + case record.type.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 record.type.MEDIA_HANDOVER_WIFI: Serial.println("\tReceived WIFI credentials:"); Serial.println("\t- SSID: " + record.getWiFiSSID()); Serial.println("\t- Network key: " + record.getWiFiPassword()); @@ -228,34 +228,34 @@ void displayRecordInfo(NdefRecord record) { Serial.println("\t- Encryption type: " + record.getWiFiEncryptionType()); break; - case WELL_KNOWN_HANDOVER_SELECT: + case record.type.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: + case record.type.WELL_KNOWN_HANDOVER_REQUEST: Serial.print("\tHandover request version: "); Serial.print(*payload >> 4); Serial.print("."); Serial.println(*payload & 0xF); break; - case MEDIA_HANDOVER_BT: + case record.type.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: + case record.type.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: + case record.type.MEDIA_HANDOVER_BLE_SECURE: Serial.print("\tBLE secure Handover"); Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); Serial.print("\t- Payload = "); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 026adcc..071e53b 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -14,14 +14,14 @@ #include "NdefRecord.h" NdefRecord::NdefRecord() { - this->type = UNSUPPORTED_NDEF_RECORD; + this->_type = UNSUPPORTED_NDEF_RECORD; this->payload = NULL; this->payloadSize = 0; this->newString = "null"; } void NdefRecord::create(NdefRecord_t record) { - this->type = record.recordType; + this->_type = record.recordType; this->payload = record.recordPayload; this->payloadSize = record.recordPayloadSize; } @@ -55,7 +55,7 @@ bool NdefRecord::isNotEmpty() { } NdefRecordType_e NdefRecord::getType() { - return this->type; + return this->_type; } unsigned char *NdefRecord::getPayload() { diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 0a5e777..b5bd7ef 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -21,7 +21,7 @@ class NdefRecord { private: NdefRecord_t content; - NdefRecordType_e type; + NdefRecordType_e _type; unsigned char *payload; unsigned short payloadSize; String getHexRepresentation(const byte *data, const uint32_t dataSize); @@ -29,6 +29,7 @@ class NdefRecord { public: NdefRecord(); + RecordType type; void create(NdefRecord_t record); bool isEmpty(); bool isNotEmpty(); diff --git a/src/ndef_helper.h b/src/ndef_helper.h index 9338ccd..9342d1f 100644 --- a/src/ndef_helper.h +++ b/src/ndef_helper.h @@ -59,4 +59,24 @@ const char *ndef_helper_UriHead(unsigned char head); NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord); unsigned char *GetNextRecord(unsigned char *pNdefRecord); +class RecordType { + public: + enum Value { + WELL_KNOWN_SIMPLE_TEXT = 0, + 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 + }; +}; + #endif \ No newline at end of file From b0a642e47cf0216614c88519a4bf86d93140ff46 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 4 Dec 2023 17:38:18 -0600 Subject: [PATCH 02/30] test: add two text record --- examples/NDEFSend/Makefile | 2 +- examples/NDEFSend/NDEFSend.ino | 43 ++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/examples/NDEFSend/Makefile b/examples/NDEFSend/Makefile index 4ab2c77..aa11614 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.usbmodem11401 compile: arduino-cli compile --fqbn $(BOARD_TAG) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 15b18e0..a34c950 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -1,13 +1,13 @@ /** * Example to send 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. */ @@ -23,13 +23,32 @@ 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 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 +// One record, "Hello" +// D1 01 08 54 02 65 6E 48 65 6C 6C 6F +// 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 + +// Two records, "Hello" and "world" +// 91 01 08 54 02 65 6E 48 65 6C 6C 6F 51 01 08 54 02 65 6E 77 6F 72 6C 64 +const char ndefMessage[] = {0x91, // 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 + 0x51, // 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 + 'w', 'o', 'r', 'l', 'd'}; // Message Payload void setup() { Serial.begin(9600); From 61cd3730ffbd04bf6ef2e2c90b90140d0cd739ea Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 4 Dec 2023 18:54:24 -0600 Subject: [PATCH 03/30] test: add uri record --- examples/NDEFSend/NDEFSend.ino | 27 +++++++++++++++++++++++++-- src/T4T_NDEF_emu.cpp | 7 +++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index a34c950..00c8664 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -35,6 +35,23 @@ Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a gl // Two records, "Hello" and "world" // 91 01 08 54 02 65 6E 48 65 6C 6C 6F 51 01 08 54 02 65 6E 77 6F 72 6C 64 +// const char ndefMessage[] = {0x91, // 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 +// 0x51, // 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 +// 'w', 'o', 'r', 'l', 'd'}; // Message Payload + +// Three records, "Hello", "world" and Uri "https://www.electroniccats.com" +// 91 01 08 54 02 65 6E 48 65 6C 6C 6F 11 01 08 54 02 65 6E 77 6F 72 6C 64 51 01 13 55 02 65 6C 65 63 74 72 6F 6E 69 63 63 61 74 73 2E 63 6F 72 const char ndefMessage[] = {0x91, // MB/ME/CF/1/IL/TNF 0x01, // Type length (1 byte) 0x08, // Payload length @@ -42,13 +59,19 @@ const char ndefMessage[] = {0x91, // MB/ME/CF/1/IL/TNF 0x02, // Status 'e', 'n', // Language 'H', 'e', 'l', 'l', 'o', // Message Payload - 0x51, // MB/ME/CF/1/IL/TNF + 0x11, // 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 - 'w', 'o', 'r', 'l', 'd'}; // Message Payload + 'w', 'o', 'r', 'l', 'd', // Message Payload + 0x51, // MB/ME/CF/1/IL/TNF + 0x01, // Type length (1 byte) + 0x13, // Payload length + 'U', // Type -> 'T' for text, 'U' for URI + 0x02, // Status + 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload void setup() { Serial.begin(9600); diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index 12fffff..2082f88 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -66,6 +66,13 @@ static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, uns if (pT4T_NDEF_EMU_PushCb != NULL) pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_size); } + + Serial.println("pRsp:"); + for (int i = 0; i < length; i++) { + Serial.print(pRsp[i], HEX); + Serial.print(" "); + } + Serial.println(); } bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) { From f39234a1084297ddfbb693ffc5aad6e935b556c5 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 4 Dec 2023 21:54:08 -0600 Subject: [PATCH 04/30] refactor: set message content --- examples/NDEFRead/NDEFRead.ino | 6 +++--- examples/NDEFSend/NDEFSend.ino | 18 +++++++++++------- src/NdefMessage.cpp | 17 +++++++++++------ src/NdefMessage.h | 8 +++++++- src/T4T_NDEF_emu.cpp | 12 +++++------- src/T4T_NDEF_emu.h | 2 ++ 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFRead/NDEFRead.ino index 3ee6f6d..9c3d22f 100644 --- a/examples/NDEFRead/NDEFRead.ino +++ b/examples/NDEFRead/NDEFRead.ino @@ -18,7 +18,7 @@ #define PN7150_ADDR (0x28) // Function prototypes -void messageReceived(); +void messageReceivedCallback(); 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.setReadMsgCallback(messageReceived); + nfc.setReadMsgCallback(messageReceivedCallback); Serial.println("Initializing..."); @@ -84,7 +84,7 @@ void loop() { } /// @brief Callback function called when an NDEF message is received -void messageReceived() { +void messageReceivedCallback() { NdefRecord record; Serial.println("Processing Callback..."); diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 00c8664..6aced70 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -19,9 +19,10 @@ #define PN7150_ADDR (0x28) // Function prototypes -void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); +void messageSentCallback(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 +NdefMessage message; // One record, "Hello" // D1 01 08 54 02 65 6E 48 65 6C 6C 6F @@ -79,11 +80,13 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - if (T4T_NDEF_EMU_SetMessage((unsigned char *)ndefMessage, sizeof(ndefMessage), (void *)*sendMessageCallback)) { - Serial.println("Set message success!"); - } else { - Serial.println("Set message failed!"); - } + message.setContent(ndefMessage, sizeof(ndefMessage)); + + // if (T4T_NDEF_EMU_SetMessage((unsigned char *)ndefMessage, sizeof(ndefMessage), (void *)*messageSentCallback)) { + // Serial.println("Set message success!"); + // } else { + // Serial.println("Set message failed!"); + // } Serial.println("Initializing..."); @@ -109,6 +112,7 @@ void loop() { } } -void sendMessageCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { +void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { Serial.println("NDEF Record sent!"); + // Do something... } diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index c7961ad..88f916a 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -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. */ @@ -15,10 +15,12 @@ unsigned char *NdefMessage::content; unsigned short NdefMessage::contentSize; +uint8_t NdefMessage::recordCounter; NdefMessage::NdefMessage() { content = NULL; contentSize = 0; + recordCounter = 0; } void NdefMessage::begin() { @@ -42,9 +44,12 @@ unsigned short NdefMessage::getContentSize() { return contentSize; } -void NdefMessage::setContent(unsigned char *content, unsigned short contentSize) { - NdefMessage::content = content; +void NdefMessage::setContent(const char *content, unsigned short contentSize) { + NdefMessage::content = (unsigned char *)content; NdefMessage::contentSize = contentSize; + NdefMessage::recordCounter = MAX_NDEF_RECORDS; + + T4T_NDEF_EMU_SetMsg(content, contentSize); } NdefRecord_t NdefMessage::getRecord() { diff --git a/src/NdefMessage.h b/src/NdefMessage.h index c921cb7..c73e1ef 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -18,9 +18,15 @@ #include "RW_NDEF.h" #include "ndef_helper.h" +#include "NdefRecord.h" +#include "T4T_NDEF_emu.h" + +#define MAX_NDEF_RECORDS 4 class NdefMessage { private: + NdefRecord records[MAX_NDEF_RECORDS]; + static uint8_t recordCounter; static unsigned char *content; static unsigned short contentSize; static void update(unsigned char *message, unsigned short messageSize); @@ -31,7 +37,7 @@ class NdefMessage { void begin(); static unsigned char *getContent(); static unsigned short getContentSize(); - static void setContent(unsigned char *content, unsigned short contentSize); + static void setContent(const char *content, unsigned short contentSize); NdefRecord_t getRecord(); bool isEmpty(); bool isNotEmpty(); diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index 2082f88..d7cebcb 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -66,13 +66,6 @@ static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, uns if (pT4T_NDEF_EMU_PushCb != NULL) pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_size); } - - Serial.println("pRsp:"); - for (int i = 0; i < length; i++) { - Serial.print(pRsp[i], HEX); - Serial.print(" "); - } - Serial.println(); } bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) { @@ -83,6 +76,11 @@ bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_siz return true; } +void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short Message_size) { + pT4T_NdefMessage = (unsigned char *)pMessage; + T4T_NdefMessage_size = Message_size; +} + void T4T_NDEF_EMU_Reset(void) { eT4T_NDEF_EMU_State = Ready; } diff --git a/src/T4T_NDEF_emu.h b/src/T4T_NDEF_emu.h index 2506a90..163e172 100644 --- a/src/T4T_NDEF_emu.h +++ b/src/T4T_NDEF_emu.h @@ -16,4 +16,6 @@ void T4T_NDEF_EMU_Reset(void); bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); +void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short Message_size); +void T4T_NDEF_EMU_SetCallback(void *pCb); void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); From 340924e5a115e3e85d5a25594b6abb4d1ad7dbc9 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 5 Dec 2023 09:15:48 -0600 Subject: [PATCH 05/30] refactor: set custom callback --- examples/NDEFSend/NDEFSend.ino | 13 ++++++++++--- src/Electroniccats_PN7150.cpp | 4 ++++ src/Electroniccats_PN7150.h | 1 + src/T4T_NDEF_emu.cpp | 9 +++++++++ src/T4T_NDEF_emu.h | 4 +++- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index 6aced70..0526caf 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -19,7 +19,8 @@ #define PN7150_ADDR (0x28) // Function prototypes -void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); +// void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); +void messageSentCallback(); 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 NdefMessage message; @@ -81,6 +82,7 @@ void setup() { Serial.println("Send NDEF Message with PN7150"); message.setContent(ndefMessage, sizeof(ndefMessage)); + nfc.setSendMsgCallback(messageSentCallback); // if (T4T_NDEF_EMU_SetMessage((unsigned char *)ndefMessage, sizeof(ndefMessage), (void *)*messageSentCallback)) { // Serial.println("Set message success!"); @@ -112,7 +114,12 @@ void loop() { } } -void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { - Serial.println("NDEF Record sent!"); +// void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { +// Serial.println("NDEF Record sent!"); +// // Do something... +// } + +void messageSentCallback() { + Serial.println("NDEF message sent!"); // Do something... } diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index d44c1a4..806c432 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -1738,6 +1738,10 @@ void Electroniccats_PN7150::setReadMsgCallback(CustomCallback_t function) { registerNdefReceivedCallback(function); } +void Electroniccats_PN7150::setSendMsgCallback(CustomCallback_t function) { + T4T_NDEF_EMU_SetCallback(function); +} + bool Electroniccats_PN7150::isReaderDetected() { static unsigned char STATUSOK[] = {0x90, 0x00}, Cmd[256], CmdSize; bool status = false; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 902b8ba..af41623 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -180,6 +180,7 @@ class Electroniccats_PN7150 : public Mode { bool NxpNci_FactoryTest_RfOn(); // Deprecated, use nciFactoryTestRfOn() instead bool reset(); void setReadMsgCallback(CustomCallback_t function); + void setSendMsgCallback(CustomCallback_t function); bool isReaderDetected(); void closeCommunication(); void sendMessage(); diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index d7cebcb..0ad59c6 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -45,6 +45,7 @@ typedef void T4T_NDEF_EMU_Callback_t(unsigned char *, unsigned short); static T4T_NDEF_EMU_state_t eT4T_NDEF_EMU_State = Ready; static T4T_NDEF_EMU_Callback_t *pT4T_NDEF_EMU_PushCb = NULL; +CustomCallback_t *ndefSendCallback; static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, unsigned char length) { if (offset == 0) { @@ -65,6 +66,10 @@ static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, uns /* Notify application of the NDEF send */ if (pT4T_NDEF_EMU_PushCb != NULL) pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_size); + + // Notify custom callback + if (ndefSendCallback != NULL) + ndefSendCallback(); } } @@ -81,6 +86,10 @@ void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short Message_size) { T4T_NdefMessage_size = Message_size; } +void T4T_NDEF_EMU_SetCallback(CustomCallback_t function) { + ndefSendCallback = function; +} + void T4T_NDEF_EMU_Reset(void) { eT4T_NDEF_EMU_State = Ready; } diff --git a/src/T4T_NDEF_emu.h b/src/T4T_NDEF_emu.h index 163e172..d8a8b96 100644 --- a/src/T4T_NDEF_emu.h +++ b/src/T4T_NDEF_emu.h @@ -14,8 +14,10 @@ #include +typedef void CustomCallback_t(void); + void T4T_NDEF_EMU_Reset(void); bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short Message_size); -void T4T_NDEF_EMU_SetCallback(void *pCb); +void T4T_NDEF_EMU_SetCallback(CustomCallback_t function); void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); From ca0f085c28f939b621c72498183ddeea8875078a Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 5 Dec 2023 10:01:47 -0600 Subject: [PATCH 06/30] feat: add send raw message --- .../{NDEFRead => NDEFReadMessage}/Makefile | 0 .../NDEFReadMessage.ino} | 0 .../{NDEFSend => NDEFSendMessage}/Makefile | 0 .../NDEFSendMessage.ino} | 12 --- examples/NDEFSendRawMessage/Makefile | 19 ++++ .../NDEFSendRawMessage/NDEFSendRawMessage.ino | 88 +++++++++++++++++++ 6 files changed, 107 insertions(+), 12 deletions(-) rename examples/{NDEFRead => NDEFReadMessage}/Makefile (100%) rename examples/{NDEFRead/NDEFRead.ino => NDEFReadMessage/NDEFReadMessage.ino} (100%) rename examples/{NDEFSend => NDEFSendMessage}/Makefile (100%) rename examples/{NDEFSend/NDEFSend.ino => NDEFSendMessage/NDEFSendMessage.ino} (91%) create mode 100644 examples/NDEFSendRawMessage/Makefile create mode 100644 examples/NDEFSendRawMessage/NDEFSendRawMessage.ino diff --git a/examples/NDEFRead/Makefile b/examples/NDEFReadMessage/Makefile similarity index 100% rename from examples/NDEFRead/Makefile rename to examples/NDEFReadMessage/Makefile diff --git a/examples/NDEFRead/NDEFRead.ino b/examples/NDEFReadMessage/NDEFReadMessage.ino similarity index 100% rename from examples/NDEFRead/NDEFRead.ino rename to examples/NDEFReadMessage/NDEFReadMessage.ino diff --git a/examples/NDEFSend/Makefile b/examples/NDEFSendMessage/Makefile similarity index 100% rename from examples/NDEFSend/Makefile rename to examples/NDEFSendMessage/Makefile diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino similarity index 91% rename from examples/NDEFSend/NDEFSend.ino rename to examples/NDEFSendMessage/NDEFSendMessage.ino index 0526caf..475a22c 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -19,7 +19,6 @@ #define PN7150_ADDR (0x28) // Function prototypes -// void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize); void messageSentCallback(); 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 @@ -84,12 +83,6 @@ void setup() { message.setContent(ndefMessage, sizeof(ndefMessage)); nfc.setSendMsgCallback(messageSentCallback); - // if (T4T_NDEF_EMU_SetMessage((unsigned char *)ndefMessage, sizeof(ndefMessage), (void *)*messageSentCallback)) { - // Serial.println("Set message success!"); - // } else { - // Serial.println("Set message failed!"); - // } - Serial.println("Initializing..."); if (nfc.begin()) { @@ -114,11 +107,6 @@ void loop() { } } -// void messageSentCallback(unsigned char *pNdefRecord, unsigned short NdefRecordSize) { -// Serial.println("NDEF Record sent!"); -// // Do something... -// } - void messageSentCallback() { Serial.println("NDEF message sent!"); // Do something... diff --git a/examples/NDEFSendRawMessage/Makefile b/examples/NDEFSendRawMessage/Makefile new file mode 100644 index 0000000..aa11614 --- /dev/null +++ b/examples/NDEFSendRawMessage/Makefile @@ -0,0 +1,19 @@ +BOARD_TAG = electroniccats:mbed_rp2040:bombercat +MONITOR_PORT = /dev/cu.usbmodem11401 + +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/NDEFSendRawMessage/NDEFSendRawMessage.ino b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino new file mode 100644 index 0000000..878eba6 --- /dev/null +++ b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino @@ -0,0 +1,88 @@ +/** + * Example to create a custom NDEF message and send it using the PN7150. + * + * Note: If you know how to create custom NDEF messages, you can use this example, otherwise, use the NDEFSend example. + * + * 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) +#define PN7150_VEN (13) +#define PN7150_ADDR (0x28) + +// Function prototypes +void messageSentCallback(); + +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 +NdefMessage message; + +// Three records, "Hello", "world" and Uri "https://www.electroniccats.com" +const char ndefMessage[] = {0x91, // 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 + 0x11, // 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 + 'w', 'o', 'r', 'l', 'd', // Message Payload + 0x51, // MB/ME/CF/1/IL/TNF + 0x01, // Type length (1 byte) + 0x13, // Payload length + 'U', // Type -> 'T' for text, 'U' for URI + 0x02, // Status + 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload + +void setup() { + Serial.begin(9600); + while (!Serial) + ; + Serial.println("Send NDEF Message with PN7150"); + + message.setContent(ndefMessage, sizeof(ndefMessage)); + nfc.setSendMsgCallback(messageSentCallback); + + Serial.println("Initializing..."); + + if (nfc.begin()) { + Serial.println("Error initializing PN7150"); + while (true) + ; + } + + // Needed to detect readers + nfc.setEmulationMode(); + Serial.print("Waiting for an NDEF device"); +} + +void loop() { + Serial.print("."); + + if (nfc.isReaderDetected()) { + Serial.println("\nReader detected!"); + Serial.println("Sending NDEF message..."); + nfc.sendMessage(); + Serial.print("\nWaiting for an NDEF device"); + } +} + +void messageSentCallback() { + Serial.println("NDEF message sent!"); + // Do something... +} From 2d82427f53a2f4aafa21fd5354554fb2669b494a Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 6 Dec 2023 13:58:48 -0600 Subject: [PATCH 07/30] feat: show ndef content --- examples/NDEFReadMessage/NDEFReadMessage.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/NDEFReadMessage/NDEFReadMessage.ino b/examples/NDEFReadMessage/NDEFReadMessage.ino index 9c3d22f..927f2cc 100644 --- a/examples/NDEFReadMessage/NDEFReadMessage.ino +++ b/examples/NDEFReadMessage/NDEFReadMessage.ino @@ -95,6 +95,9 @@ void messageReceivedCallback() { return; } + Serial.print("NDEF message: "); + Serial.println(getHexRepresentation(message.getContent(), message.getContentSize())); + // Show NDEF message details, it is composed of records do { record.create(message.getRecord()); // Get a new record every time we call getRecord() From 5d3f15623fc7ac690b5b69c3db89b4968ff236c0 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 7 Dec 2023 19:01:19 -0600 Subject: [PATCH 08/30] feat: add text record method --- examples/NDEFSendMessage/NDEFSendMessage.ino | 54 ++++++++++------- src/NdefMessage.cpp | 53 +++++++++++++++- src/NdefMessage.h | 8 ++- src/NdefRecord.cpp | 64 ++++++++++++++++++++ src/NdefRecord.h | 17 +++++- 5 files changed, 171 insertions(+), 25 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 475a22c..4582eb7 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -53,26 +53,36 @@ NdefMessage message; // Three records, "Hello", "world" and Uri "https://www.electroniccats.com" // 91 01 08 54 02 65 6E 48 65 6C 6C 6F 11 01 08 54 02 65 6E 77 6F 72 6C 64 51 01 13 55 02 65 6C 65 63 74 72 6F 6E 69 63 63 61 74 73 2E 63 6F 72 -const char ndefMessage[] = {0x91, // 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 - 0x11, // 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 - 'w', 'o', 'r', 'l', 'd', // Message Payload - 0x51, // MB/ME/CF/1/IL/TNF - 0x01, // Type length (1 byte) - 0x13, // Payload length - 'U', // Type -> 'T' for text, 'U' for URI - 0x02, // Status - 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload +// const char ndefMessage[] = {0x91, // 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 +// 0x11, // 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 +// 'w', 'o', 'r', 'l', 'd', // Message Payload +// 0x51, // MB/ME/CF/1/IL/TNF +// 0x01, // Type length (1 byte) +// 0x13, // Payload length +// 'U', // Type -> 'T' for text, 'U' for URI +// 0x02, // Status +// 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload + +uint8_t headerContent = 0xD1; +uint8_t typeLength = 0x01; +uint8_t payloadLength = 0x08; +uint8_t type = 'T'; +uint8_t status = 0x02; +uint8_t language[2] = {'e', 'n'}; +uint8_t payload[5] = {'H', 'e', 'l', 'l', 'o'}; + +const char ndefMessage[] = {headerContent, typeLength, payloadLength, type, status, language[0], language[1], payload[0], payload[1], payload[2], payload[3], payload[4]}; void setup() { Serial.begin(9600); @@ -80,7 +90,9 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - message.setContent(ndefMessage, sizeof(ndefMessage)); + // message.setContent(ndefMessage, sizeof(ndefMessage)); + // message.addTextRecord("Hello"); + message.addTextRecord("world"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 88f916a..49b23fb 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -16,17 +16,39 @@ unsigned char *NdefMessage::content; unsigned short NdefMessage::contentSize; uint8_t NdefMessage::recordCounter; +String NdefMessage::newString; NdefMessage::NdefMessage() { content = NULL; contentSize = 0; recordCounter = 0; + newString = "null"; } void NdefMessage::begin() { registerUpdateNdefMessageCallback(NdefMessage::update); } +String NdefMessage::getHexRepresentation(const byte *data, const uint32_t dataSize) { + String hexString; + + if (dataSize == 0) { + hexString = newString; + } + + 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 NdefMessage::update(unsigned char *message, unsigned short messageSize) { if (content != NULL) { free(content); @@ -47,8 +69,8 @@ unsigned short NdefMessage::getContentSize() { void NdefMessage::setContent(const char *content, unsigned short contentSize) { NdefMessage::content = (unsigned char *)content; NdefMessage::contentSize = contentSize; - NdefMessage::recordCounter = MAX_NDEF_RECORDS; + Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); T4T_NDEF_EMU_SetMsg(content, contentSize); } @@ -78,3 +100,32 @@ bool NdefMessage::isNotEmpty() { bool NdefMessage::hasRecord() { return NdefMessage::isNotEmpty(); } + +bool NdefMessage::addRecord(NdefRecord record) { + if (recordCounter < MAX_NDEF_RECORDS) { + records[recordCounter] = record; + recordCounter++; + + setContent(record.getContent(), record.getContentSize()); + + return true; + } + return false; +} + +bool NdefMessage::addTextRecord(String text) { + NdefRecord record; + record.setHeaderFlags(0xD1); + record.setTypeLength(0x01); + record.setPayloadSize(text.length() + 3); + record.setRecordType(0x54); + record.setStatus(0x02); + record.setLanguageCode((unsigned char *)"en"); + record.setPayload((unsigned char *)text.c_str()); + + Serial.println("Payload size: " + String(record.getPayloadSize())); + unsigned char *payload = record.getPayload(); + Serial.println("Payload: " + String((char *)payload)); + + return addRecord(record); +} diff --git a/src/NdefMessage.h b/src/NdefMessage.h index c73e1ef..bc8fd73 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -23,7 +23,7 @@ #define MAX_NDEF_RECORDS 4 -class NdefMessage { +class NdefMessage : NdefRecord { private: NdefRecord records[MAX_NDEF_RECORDS]; static uint8_t recordCounter; @@ -31,6 +31,8 @@ class NdefMessage { static unsigned short contentSize; static void update(unsigned char *message, unsigned short messageSize); void getNextRecord(); + static String getHexRepresentation(const byte *data, const uint32_t dataSize); + static String newString; public: NdefMessage(); @@ -42,6 +44,10 @@ class NdefMessage { bool isEmpty(); bool isNotEmpty(); bool hasRecord(); + bool addRecord(NdefRecord record); + bool addTextRecord(String text); + bool addTextRecord(String text, String languageCode); + bool addUriRecord(String uri); }; #endif \ No newline at end of file diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 071e53b..2bfdae2 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -15,8 +15,13 @@ NdefRecord::NdefRecord() { this->_type = UNSUPPORTED_NDEF_RECORD; + this->headerFlags = 0; this->payload = NULL; this->payloadSize = 0; + this->typeLength = 0; + this->recordType = 0; + this->status = 0; + this->languageCode = 0; this->newString = "null"; } @@ -233,3 +238,62 @@ String NdefRecord::getUri() { return uri; } + +void NdefRecord::setPayload(unsigned char *payload) { + this->payload = payload; +} + +void NdefRecord::setHeaderFlags(uint8_t headerFlags) { + this->headerFlags = headerFlags; +} + +void NdefRecord::setTypeLength(uint8_t typeLength) { + this->typeLength = typeLength; +} + +void NdefRecord::setRecordType(uint8_t recordType) { + this->recordType = recordType; +} + +void NdefRecord::setStatus(uint8_t status) { + this->status = status; +} + +void NdefRecord::setLanguageCode(unsigned char *languageCode) { + this->languageCode = languageCode; + Serial.println("Language code: " + String((char *)this->languageCode)); + Serial.println("Language code: " + getHexRepresentation(this->languageCode, 2)); // Language code: 65:6E +} + +void NdefRecord::setPayloadSize(uint8_t payloadSize) { + this->payloadSize = payloadSize; +} + +const char *NdefRecord::getContent() { + char *recordContent = new char[getPayloadSize()]; + Serial.println("Language code: " + getHexRepresentation(this->languageCode, 2)); // Language code: 010:00 + + recordContent[0] = headerFlags; + recordContent[1] = typeLength; + recordContent[2] = payloadSize; + recordContent[3] = recordType; + recordContent[4] = status; + recordContent[5] = languageCode[0]; + recordContent[6] = languageCode[1]; + + Serial.println("Header flags: " + String(recordContent[0], HEX)); + Serial.print("Language code: "); + Serial.print(languageCode[0], HEX); + Serial.println(languageCode[1], HEX); + Serial.println("Language code: " + String(recordContent[5]) + String(recordContent[6])); + + for (int i = 0; i < getPayloadSize(); i++) { + recordContent[i + 7] = payload[i]; + } + + return recordContent; +} + +unsigned short NdefRecord::getContentSize() { + return getPayloadSize() + 4; // 5 bytes for header, type length, payload length, record type, status, and 2 bytes for language code +} diff --git a/src/NdefRecord.h b/src/NdefRecord.h index b5bd7ef..42142cc 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -20,10 +20,14 @@ class NdefRecord { private: - NdefRecord_t content; NdefRecordType_e _type; - unsigned char *payload; + uint8_t headerFlags; + uint8_t typeLength; unsigned short payloadSize; + uint8_t recordType; + uint8_t status; + unsigned char *languageCode; + unsigned char *payload; String getHexRepresentation(const byte *data, const uint32_t dataSize); String newString; @@ -45,6 +49,15 @@ class NdefRecord { String getWiFiPassword(); String getVCardContent(); String getUri(); + void setPayload(unsigned char *payload); + void setHeaderFlags(uint8_t headerFlags); + void setTypeLength(uint8_t typeLength); + void setRecordType(uint8_t recordType); + void setStatus(uint8_t status); + void setLanguageCode(unsigned char *languageCode); + void setPayloadSize(uint8_t payloadSize); + const char *getContent(); + unsigned short getContentSize(); }; #endif \ No newline at end of file From 889032b4a1b606f7ebecccae48673e34ddf108d6 Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 12 Dec 2023 14:49:38 -0600 Subject: [PATCH 09/30] feat: load two text records --- examples/NDEFSendMessage/NDEFSendMessage.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 4582eb7..6cbcf4c 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -91,8 +91,10 @@ void setup() { Serial.println("Send NDEF Message with PN7150"); // message.setContent(ndefMessage, sizeof(ndefMessage)); - // message.addTextRecord("Hello"); + message.addTextRecord("Hello"); message.addTextRecord("world"); + // message.addTextRecord("adios"); + // message.addTextRecord("Hola mundo!"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); From 8c79ff4e38881871b083325355cbe92152be4dbd Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 12 Dec 2023 15:55:22 -0600 Subject: [PATCH 10/30] feat: load three text records --- .gitignore | 5 +- examples/NDEFSendMessage/NDEFSendMessage.ino | 2 +- src/NdefMessage.cpp | 56 +++++++++++++++----- src/NdefMessage.h | 13 ++++- src/NdefRecord.cpp | 24 ++++----- src/NdefRecord.h | 4 +- 6 files changed, 72 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 5956980..948a515 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ src/sync.ffs_db .DS_Store -.vscode/ \ No newline at end of file +.vscode/ + +# Firmware binaries +/examples/**/build/ \ No newline at end of file diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 6cbcf4c..eef2125 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -93,7 +93,7 @@ void setup() { // message.setContent(ndefMessage, sizeof(ndefMessage)); message.addTextRecord("Hello"); message.addTextRecord("world"); - // message.addTextRecord("adios"); + message.addTextRecord("adios"); // message.addTextRecord("Hola mundo!"); nfc.setSendMsgCallback(messageSentCallback); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 49b23fb..8d999ff 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -15,6 +15,8 @@ unsigned char *NdefMessage::content; unsigned short NdefMessage::contentSize; +unsigned char *NdefMessage::newContent; +unsigned short NdefMessage::newContentSize; uint8_t NdefMessage::recordCounter; String NdefMessage::newString; @@ -74,6 +76,15 @@ void NdefMessage::setContent(const char *content, unsigned short contentSize) { T4T_NDEF_EMU_SetMsg(content, contentSize); } +void NdefMessage::addContent(const char *record, unsigned short recordSize) { + unsigned short newContentSize = contentSize + recordSize; + unsigned char *newContent = (unsigned char *)malloc(newContentSize); + memcpy(newContent, content, contentSize); + memcpy(newContent + contentSize, record, recordSize); + setContent((const char *)newContent, newContentSize); + free(newContent); +} + NdefRecord_t NdefMessage::getRecord() { NdefRecord_t ndefRecord = DetectNdefRecordType(content); @@ -102,26 +113,45 @@ bool NdefMessage::hasRecord() { } bool NdefMessage::addRecord(NdefRecord record) { - if (recordCounter < MAX_NDEF_RECORDS) { - records[recordCounter] = record; - recordCounter++; - - setContent(record.getContent(), record.getContentSize()); + if (recordCounter > MAX_NDEF_RECORDS) { + return false; + } - return true; + // records[recordCounter] = record; + + // Update header flags + if (recordCounter == 1) { + // NdefMessage::content[0] = NDEF_HEADER_FLAGS_FIRST_RECORD; + // records[recordCounter].setHeaderFlags(NDEF_HEADER_FLAGS_NEXT_RECORD); + NdefMessage::content[0] = NDEF_HEADER_FLAGS_FIRST_RECORD; + record.setHeaderFlags(NDEF_HEADER_FLAGS_NEXT_RECORD); + } else if (recordCounter == 2) { + NdefMessage::content[contentSize - record.getContentSize()] = 0x11; + record.setHeaderFlags(NDEF_HEADER_FLAGS_NEXT_RECORD); } - return false; + + NdefMessage::newContent = new unsigned char[contentSize + record.getContentSize()]; + memcpy(newContent, content, contentSize); + memcpy(newContent + contentSize, record.getContent(), record.getContentSize()); + setContent((const char *)newContent, contentSize + record.getContentSize()); + + recordCounter++; + + // addContent(record.getContent(), record.getContentSize()); // Doesn't work + // setContent(record.getContent(), record.getContentSize()); // Works + + return true; } bool NdefMessage::addTextRecord(String text) { NdefRecord record; - record.setHeaderFlags(0xD1); - record.setTypeLength(0x01); + record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); + record.setTypeLength(NDEF_TYPE_LENGTH); record.setPayloadSize(text.length() + 3); - record.setRecordType(0x54); - record.setStatus(0x02); - record.setLanguageCode((unsigned char *)"en"); - record.setPayload((unsigned char *)text.c_str()); + record.setRecordType(NDEF_TEXT_RECORD_TYPE); + record.setStatus(NDEF_STATUS); + record.setLanguageCode(NDEF_DEFAULT_LANGUAGE_CODE); + record.setPayload(text); Serial.println("Payload size: " + String(record.getPayloadSize())); unsigned char *payload = record.getPayload(); diff --git a/src/NdefMessage.h b/src/NdefMessage.h index bc8fd73..788a5e1 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -22,6 +22,14 @@ #include "T4T_NDEF_emu.h" #define MAX_NDEF_RECORDS 4 +#define NDEF_HEADER_FLAGS_SINGLE_RECORD 0xD1 +#define NDEF_HEADER_FLAGS_FIRST_RECORD 0x91 +#define NDEF_HEADER_FLAGS_NEXT_RECORD 0x51 +#define NDEF_TYPE_LENGTH 0x01 +#define NDEF_TEXT_RECORD_TYPE 'T' +#define NDEF_URI_RECORD_TYPE 'U' +#define NDEF_STATUS 0x02 +#define NDEF_DEFAULT_LANGUAGE_CODE "en" class NdefMessage : NdefRecord { private: @@ -29,10 +37,14 @@ class NdefMessage : NdefRecord { static uint8_t recordCounter; static unsigned char *content; static unsigned short contentSize; + static unsigned char *newContent; + static unsigned short newContentSize; static void update(unsigned char *message, unsigned short messageSize); void getNextRecord(); static String getHexRepresentation(const byte *data, const uint32_t dataSize); static String newString; + static void addContent(const char *record, unsigned short recordSize); + bool addRecord(NdefRecord record); public: NdefMessage(); @@ -44,7 +56,6 @@ class NdefMessage : NdefRecord { bool isEmpty(); bool isNotEmpty(); bool hasRecord(); - bool addRecord(NdefRecord record); bool addTextRecord(String text); bool addTextRecord(String text, String languageCode); bool addUriRecord(String uri); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 2bfdae2..d6b609d 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -239,8 +239,9 @@ String NdefRecord::getUri() { return uri; } -void NdefRecord::setPayload(unsigned char *payload) { - this->payload = payload; +void NdefRecord::setPayload(String payload) { + this->payload = new unsigned char[payload.length()]; + strcpy((char *)this->payload, payload.c_str()); } void NdefRecord::setHeaderFlags(uint8_t headerFlags) { @@ -259,10 +260,12 @@ void NdefRecord::setStatus(uint8_t status) { this->status = status; } -void NdefRecord::setLanguageCode(unsigned char *languageCode) { - this->languageCode = languageCode; - Serial.println("Language code: " + String((char *)this->languageCode)); - Serial.println("Language code: " + getHexRepresentation(this->languageCode, 2)); // Language code: 65:6E +void NdefRecord::setLanguageCode(String languageCode) { + this->languageCode = new unsigned char[languageCode.length()]; + strcpy((char *)this->languageCode, languageCode.c_str()); +#ifdef DEBUG3 + Serial.println("Language code: " + languageCode); +#endif } void NdefRecord::setPayloadSize(uint8_t payloadSize) { @@ -271,7 +274,6 @@ void NdefRecord::setPayloadSize(uint8_t payloadSize) { const char *NdefRecord::getContent() { char *recordContent = new char[getPayloadSize()]; - Serial.println("Language code: " + getHexRepresentation(this->languageCode, 2)); // Language code: 010:00 recordContent[0] = headerFlags; recordContent[1] = typeLength; @@ -281,12 +283,6 @@ const char *NdefRecord::getContent() { recordContent[5] = languageCode[0]; recordContent[6] = languageCode[1]; - Serial.println("Header flags: " + String(recordContent[0], HEX)); - Serial.print("Language code: "); - Serial.print(languageCode[0], HEX); - Serial.println(languageCode[1], HEX); - Serial.println("Language code: " + String(recordContent[5]) + String(recordContent[6])); - for (int i = 0; i < getPayloadSize(); i++) { recordContent[i + 7] = payload[i]; } @@ -295,5 +291,5 @@ const char *NdefRecord::getContent() { } unsigned short NdefRecord::getContentSize() { - return getPayloadSize() + 4; // 5 bytes for header, type length, payload length, record type, status, and 2 bytes for language code + return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 42142cc..9d06571 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -49,12 +49,12 @@ class NdefRecord { String getWiFiPassword(); String getVCardContent(); String getUri(); - void setPayload(unsigned char *payload); + void setPayload(String payload); void setHeaderFlags(uint8_t headerFlags); void setTypeLength(uint8_t typeLength); void setRecordType(uint8_t recordType); void setStatus(uint8_t status); - void setLanguageCode(unsigned char *languageCode); + void setLanguageCode(String languageCode); void setPayloadSize(uint8_t payloadSize); const char *getContent(); unsigned short getContentSize(); From 420d2c3ac1911ab3ea890d77cf748912e7034246 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 13 Dec 2023 11:19:01 -0600 Subject: [PATCH 11/30] feat: add multiple text records --- examples/NDEFSendMessage/NDEFSendMessage.ino | 5 +- src/NdefMessage.cpp | 61 ++++++++++---------- src/NdefMessage.h | 16 ++--- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index eef2125..187754b 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -93,8 +93,9 @@ void setup() { // message.setContent(ndefMessage, sizeof(ndefMessage)); message.addTextRecord("Hello"); message.addTextRecord("world"); - message.addTextRecord("adios"); - // message.addTextRecord("Hola mundo!"); + message.addTextRecord("Hola mundo!"); + message.addTextRecord("Hola"); + message.addTextRecord("mundo :D"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 8d999ff..a3cd41b 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -72,17 +72,36 @@ void NdefMessage::setContent(const char *content, unsigned short contentSize) { NdefMessage::content = (unsigned char *)content; NdefMessage::contentSize = contentSize; + Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); + NdefMessage::updateHeaderFlags(); Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); T4T_NDEF_EMU_SetMsg(content, contentSize); } -void NdefMessage::addContent(const char *record, unsigned short recordSize) { - unsigned short newContentSize = contentSize + recordSize; - unsigned char *newContent = (unsigned char *)malloc(newContentSize); - memcpy(newContent, content, contentSize); - memcpy(newContent + contentSize, record, recordSize); - setContent((const char *)newContent, newContentSize); - free(newContent); +void NdefMessage::updateHeaderFlags() { + uint8_t headersPositions[recordCounter]; + uint8_t recordCounterAux = 0; + Serial.print("Header positions:"); + for (uint8_t i = 0; i < contentSize; i++) { + if (content[i] == NDEF_TYPE_LENGTH) { // New record found + Serial.print(" " + String(i - 1)); + headersPositions[recordCounterAux] = i - 1; + recordCounterAux++; + } + } + Serial.println(); + + for (uint8_t i = 0; i < recordCounter; i++) { + if (i == 0) { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_SINGLE_RECORD; + } else if (i == 1) { + content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_FIRST_RECORD; + content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; + } else { + content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_NEXT_RECORD; + content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; + } + } } NdefRecord_t NdefMessage::getRecord() { @@ -112,38 +131,16 @@ bool NdefMessage::hasRecord() { return NdefMessage::isNotEmpty(); } -bool NdefMessage::addRecord(NdefRecord record) { - if (recordCounter > MAX_NDEF_RECORDS) { - return false; - } - - // records[recordCounter] = record; - - // Update header flags - if (recordCounter == 1) { - // NdefMessage::content[0] = NDEF_HEADER_FLAGS_FIRST_RECORD; - // records[recordCounter].setHeaderFlags(NDEF_HEADER_FLAGS_NEXT_RECORD); - NdefMessage::content[0] = NDEF_HEADER_FLAGS_FIRST_RECORD; - record.setHeaderFlags(NDEF_HEADER_FLAGS_NEXT_RECORD); - } else if (recordCounter == 2) { - NdefMessage::content[contentSize - record.getContentSize()] = 0x11; - record.setHeaderFlags(NDEF_HEADER_FLAGS_NEXT_RECORD); - } +void NdefMessage::addRecord(NdefRecord record) { + recordCounter++; NdefMessage::newContent = new unsigned char[contentSize + record.getContentSize()]; memcpy(newContent, content, contentSize); memcpy(newContent + contentSize, record.getContent(), record.getContentSize()); setContent((const char *)newContent, contentSize + record.getContentSize()); - - recordCounter++; - - // addContent(record.getContent(), record.getContentSize()); // Doesn't work - // setContent(record.getContent(), record.getContentSize()); // Works - - return true; } -bool NdefMessage::addTextRecord(String text) { +void NdefMessage::addTextRecord(String text) { NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); record.setTypeLength(NDEF_TYPE_LENGTH); diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 788a5e1..91124a7 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -16,15 +16,16 @@ #include -#include "RW_NDEF.h" -#include "ndef_helper.h" #include "NdefRecord.h" +#include "RW_NDEF.h" #include "T4T_NDEF_emu.h" +#include "ndef_helper.h" #define MAX_NDEF_RECORDS 4 #define NDEF_HEADER_FLAGS_SINGLE_RECORD 0xD1 #define NDEF_HEADER_FLAGS_FIRST_RECORD 0x91 -#define NDEF_HEADER_FLAGS_NEXT_RECORD 0x51 +#define NDEF_HEADER_FLAGS_NEXT_RECORD 0x11 +#define NDEF_HEADER_FLAGS_LAST_RECORD 0x51 #define NDEF_TYPE_LENGTH 0x01 #define NDEF_TEXT_RECORD_TYPE 'T' #define NDEF_URI_RECORD_TYPE 'U' @@ -44,7 +45,8 @@ class NdefMessage : NdefRecord { static String getHexRepresentation(const byte *data, const uint32_t dataSize); static String newString; static void addContent(const char *record, unsigned short recordSize); - bool addRecord(NdefRecord record); + void addRecord(NdefRecord record); + static void updateHeaderFlags(); public: NdefMessage(); @@ -56,9 +58,9 @@ class NdefMessage : NdefRecord { bool isEmpty(); bool isNotEmpty(); bool hasRecord(); - bool addTextRecord(String text); - bool addTextRecord(String text, String languageCode); - bool addUriRecord(String uri); + void addTextRecord(String text); + void addTextRecord(String text, String languageCode); + void addUriRecord(String uri); }; #endif \ No newline at end of file From fef6b1f23b0eac95c57075b0d591bbb404c4ae30 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 13 Dec 2023 19:34:00 -0600 Subject: [PATCH 12/30] refactor: debug 3 --- examples/NDEFSendMessage/NDEFSendMessage.ino | 61 -------------------- src/Electroniccats_PN7150.h | 6 -- src/NdefMessage.cpp | 17 +++++- src/NdefRecord.cpp | 3 - src/NdefRecord.h | 7 +++ 5 files changed, 21 insertions(+), 73 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 187754b..73cfe65 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -24,73 +24,12 @@ void messageSentCallback(); 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 NdefMessage message; -// One record, "Hello" -// D1 01 08 54 02 65 6E 48 65 6C 6C 6F -// 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 - -// Two records, "Hello" and "world" -// 91 01 08 54 02 65 6E 48 65 6C 6C 6F 51 01 08 54 02 65 6E 77 6F 72 6C 64 -// const char ndefMessage[] = {0x91, // 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 -// 0x51, // 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 -// 'w', 'o', 'r', 'l', 'd'}; // Message Payload - -// Three records, "Hello", "world" and Uri "https://www.electroniccats.com" -// 91 01 08 54 02 65 6E 48 65 6C 6C 6F 11 01 08 54 02 65 6E 77 6F 72 6C 64 51 01 13 55 02 65 6C 65 63 74 72 6F 6E 69 63 63 61 74 73 2E 63 6F 72 -// const char ndefMessage[] = {0x91, // 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 -// 0x11, // 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 -// 'w', 'o', 'r', 'l', 'd', // Message Payload -// 0x51, // MB/ME/CF/1/IL/TNF -// 0x01, // Type length (1 byte) -// 0x13, // Payload length -// 'U', // Type -> 'T' for text, 'U' for URI -// 0x02, // Status -// 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload - -uint8_t headerContent = 0xD1; -uint8_t typeLength = 0x01; -uint8_t payloadLength = 0x08; -uint8_t type = 'T'; -uint8_t status = 0x02; -uint8_t language[2] = {'e', 'n'}; -uint8_t payload[5] = {'H', 'e', 'l', 'l', 'o'}; - -const char ndefMessage[] = {headerContent, typeLength, payloadLength, type, status, language[0], language[1], payload[0], payload[1], payload[2], payload[3], payload[4]}; - void setup() { Serial.begin(9600); while (!Serial) ; Serial.println("Send NDEF Message with PN7150"); - // message.setContent(ndefMessage, sizeof(ndefMessage)); message.addTextRecord("Hello"); message.addTextRecord("world"); message.addTextRecord("Hola mundo!"); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index af41623..381260d 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -27,12 +27,6 @@ #include "RemoteDevice.h" #include "T4T_NDEF_emu.h" -/* - * Active DEBUG Serial -*/ -//#define DEBUG -//#define DEBUG2 - #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 diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index a3cd41b..1fb9d3e 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -72,24 +72,34 @@ void NdefMessage::setContent(const char *content, unsigned short contentSize) { NdefMessage::content = (unsigned char *)content; NdefMessage::contentSize = contentSize; +#ifdef DEBUG3 Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); +#endif NdefMessage::updateHeaderFlags(); +#ifdef DEBUG3 Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); +#endif T4T_NDEF_EMU_SetMsg(content, contentSize); } void NdefMessage::updateHeaderFlags() { uint8_t headersPositions[recordCounter]; uint8_t recordCounterAux = 0; +#ifdef DEBUG3 Serial.print("Header positions:"); +#endif for (uint8_t i = 0; i < contentSize; i++) { if (content[i] == NDEF_TYPE_LENGTH) { // New record found +#ifdef DEBUG3 Serial.print(" " + String(i - 1)); +#endif headersPositions[recordCounterAux] = i - 1; recordCounterAux++; } } +#ifdef DEBUG3 Serial.println(); +#endif for (uint8_t i = 0; i < recordCounter; i++) { if (i == 0) { @@ -150,9 +160,10 @@ void NdefMessage::addTextRecord(String text) { record.setLanguageCode(NDEF_DEFAULT_LANGUAGE_CODE); record.setPayload(text); +#ifdef DEBUG3 Serial.println("Payload size: " + String(record.getPayloadSize())); - unsigned char *payload = record.getPayload(); - Serial.println("Payload: " + String((char *)payload)); + Serial.println("Payload: " + String((char *)record.getPayload())); +#endif - return addRecord(record); + addRecord(record); } diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index d6b609d..fd17ba6 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -263,9 +263,6 @@ void NdefRecord::setStatus(uint8_t status) { void NdefRecord::setLanguageCode(String languageCode) { this->languageCode = new unsigned char[languageCode.length()]; strcpy((char *)this->languageCode, languageCode.c_str()); -#ifdef DEBUG3 - Serial.println("Language code: " + languageCode); -#endif } void NdefRecord::setPayloadSize(uint8_t payloadSize) { diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 9d06571..0e9236f 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -18,6 +18,13 @@ #include "ndef_helper.h" +/* + * Active DEBUG Serial + */ +// #define DEBUG +// #define DEBUG2 +#define DEBUG3 + class NdefRecord { private: NdefRecordType_e _type; From 7c1138c12ae5b593a41aacc9eed3ce8daa5b62a8 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 13 Dec 2023 20:34:56 -0600 Subject: [PATCH 13/30] feat: add optional language code --- examples/NDEFSendMessage/NDEFSendMessage.ino | 9 ++++----- src/NdefMessage.cpp | 8 ++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 73cfe65..b375692 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -30,11 +30,10 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - message.addTextRecord("Hello"); - message.addTextRecord("world"); - message.addTextRecord("Hola mundo!"); - message.addTextRecord("Hola"); - message.addTextRecord("mundo :D"); + message.addTextRecord("Hello"); // English by default + message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 1fb9d3e..b2fd2ad 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -150,14 +150,14 @@ void NdefMessage::addRecord(NdefRecord record) { setContent((const char *)newContent, contentSize + record.getContentSize()); } -void NdefMessage::addTextRecord(String text) { +void NdefMessage::addTextRecord(String text, String languageCode) { NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); record.setTypeLength(NDEF_TYPE_LENGTH); record.setPayloadSize(text.length() + 3); record.setRecordType(NDEF_TEXT_RECORD_TYPE); record.setStatus(NDEF_STATUS); - record.setLanguageCode(NDEF_DEFAULT_LANGUAGE_CODE); + record.setLanguageCode(languageCode); record.setPayload(text); #ifdef DEBUG3 @@ -167,3 +167,7 @@ void NdefMessage::addTextRecord(String text) { addRecord(record); } + +void NdefMessage::addTextRecord(String text) { + addTextRecord(text, NDEF_DEFAULT_LANGUAGE_CODE); +} From 8a29c4f28c9ee94e6e696f828c3eef633c2e95e1 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 14 Dec 2023 10:59:16 -0600 Subject: [PATCH 14/30] feat: add uri record method --- examples/NDEFSendMessage/NDEFSendMessage.ino | 16 ++- .../NDEFSendRawMessage/NDEFSendRawMessage.ino | 56 ++++---- src/NdefMessage.cpp | 127 +++++++++++++++++- src/NdefMessage.h | 44 +++++- src/NdefRecord.cpp | 28 +++- src/NdefRecord.h | 6 +- 6 files changed, 229 insertions(+), 48 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index b375692..3300d81 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -1,10 +1,13 @@ /** * Example to send NDEF messages + * + * Note: If you know how to create custom NDEF messages, you can use the NDEFSendRawMessage example, otherwise, use this example. + * * Authors: * Salvador Mendoza - @Netxing - salmg.net * Francisco Torres - Electronic Cats - electroniccats.com * - * August 2023 + * December 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, @@ -30,10 +33,13 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - message.addTextRecord("Hello"); // English by default - message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now - message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php - message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly + message.addTextRecord("Hello"); // English by default + message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly + message.addUriRecord("google.com"); // No prefix explicitly + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addUriRecord("rtsp://test"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino index 878eba6..88d5456 100644 --- a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino +++ b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino @@ -1,13 +1,13 @@ /** * Example to create a custom NDEF message and send it using the PN7150. - * - * Note: If you know how to create custom NDEF messages, you can use this example, otherwise, use the NDEFSend example. - * + * + * Note: If you know how to create custom NDEF messages, you can use this example, otherwise, use the NDEFSendMessage example. + * * Authors: * Salvador Mendoza - @Netxing - salmg.net * Francisco Torres - Electronic Cats - electroniccats.com * - * August 2023 + * December 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, @@ -28,26 +28,34 @@ Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a gl NdefMessage message; // Three records, "Hello", "world" and Uri "https://www.electroniccats.com" -const char ndefMessage[] = {0x91, // 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 - 0x11, // 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 - 'w', 'o', 'r', 'l', 'd', // Message Payload - 0x51, // MB/ME/CF/1/IL/TNF - 0x01, // Type length (1 byte) - 0x13, // Payload length - 'U', // Type -> 'T' for text, 'U' for URI - 0x02, // Status - 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload +// const char ndefMessage[] = {0x91, // 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 +// 0x11, // 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 +// 'w', 'o', 'r', 'l', 'd', // Message Payload +// 0x51, // MB/ME/CF/1/IL/TNF +// 0x01, // Type length (1 byte) +// 0x13, // Payload length +// 'U', // Type -> 'T' for text, 'U' for URI +// 0x02, // Status +// 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload + +// One uri record "https://www.electroniccats.com" +const char ndefMessage[] = {0xD1, + 0x01, + 0x13, + 'U', + 0x02, + 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; void setup() { Serial.begin(9600); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index b2fd2ad..37a1ec3 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -3,7 +3,7 @@ * Authors: * Francisco Torres - Electronic Cats - electroniccats.com * - * August 2023 + * December 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, @@ -160,14 +160,129 @@ void NdefMessage::addTextRecord(String text, String languageCode) { record.setLanguageCode(languageCode); record.setPayload(text); -#ifdef DEBUG3 - Serial.println("Payload size: " + String(record.getPayloadSize())); - Serial.println("Payload: " + String((char *)record.getPayload())); -#endif - addRecord(record); } void NdefMessage::addTextRecord(String text) { addTextRecord(text, NDEF_DEFAULT_LANGUAGE_CODE); } + +void NdefMessage::addUriRecord(String uri) { + NdefRecord record; + record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); + record.setTypeLength(NDEF_TYPE_LENGTH); + record.setPayloadSize(uri.length() + 3); + record.setRecordType(NDEF_URI_RECORD_TYPE); + + if (uri.startsWith("http://www.")) { + record.setStatus(NDEF_URI_HTTP_WWWDOT); + record.setPayload(uri.substring(11).c_str()); + } else if (uri.startsWith("https://www.")) { + record.setStatus(NDEF_URI_HTTPS_WWWDOT); + record.setPayload(uri.substring(12).c_str()); + } else if (uri.startsWith("http://")) { + record.setStatus(NDEF_URI_HTTP); + record.setPayload(uri.substring(7).c_str()); + } else if (uri.startsWith("https://")) { + record.setStatus(NDEF_URI_HTTPS); + record.setPayload(uri.substring(8).c_str()); + } else if (uri.startsWith("tel:")) { + record.setStatus(NDEF_URI_TEL); + record.setPayload(uri.substring(4).c_str()); + } else if (uri.startsWith("mailto:")) { + record.setStatus(NDEF_URI_MAILTO); + record.setPayload(uri.substring(7).c_str()); + } else if (uri.startsWith("ftp://anonymous:anonymous@")) { + record.setStatus(NDEF_URI_FTP_ANONIMOUS); + record.setPayload(uri.substring(26).c_str()); + } else if (uri.startsWith("ftp://ftp.")) { + record.setStatus(NDEF_URI_FTP_FTPDOT); + record.setPayload(uri.substring(9).c_str()); + } else if (uri.startsWith("ftps://")) { + record.setStatus(NDEF_URI_FTPS); + record.setPayload(uri.substring(7).c_str()); + } else if (uri.startsWith("sftp://")) { + record.setStatus(NDEF_URI_SFTP); + record.setPayload(uri.substring(7).c_str()); + } else if (uri.startsWith("smb://")) { + record.setStatus(NDEF_URI_SMB); + record.setPayload(uri.substring(6).c_str()); + } else if (uri.startsWith("nfs://")) { + record.setStatus(NDEF_URI_NFS); + record.setPayload(uri.substring(6).c_str()); + } else if (uri.startsWith("ftp://")) { + record.setStatus(NDEF_URI_FTP); + record.setPayload(uri.substring(6).c_str()); + } else if (uri.startsWith("dav://")) { + record.setStatus(NDEF_URI_DAV); + record.setPayload(uri.substring(6).c_str()); + } else if (uri.startsWith("news:")) { + record.setStatus(NDEF_URI_NEWS); + record.setPayload(uri.substring(5).c_str()); + } else if (uri.startsWith("telnet://")) { + record.setStatus(NDEF_URI_TELNET); + record.setPayload(uri.substring(9).c_str()); + } else if (uri.startsWith("imap:")) { + record.setStatus(NDEF_URI_IMAP); + record.setPayload(uri.substring(5).c_str()); + } else if (uri.startsWith("rtsp://")) { + record.setStatus(NDEF_URI_RTSP); + record.setPayload(uri.substring(7).c_str()); + } else if (uri.startsWith("urn:")) { + record.setStatus(NDEF_URI_URN); + record.setPayload(uri.substring(4).c_str()); + } else if (uri.startsWith("pop:")) { + record.setStatus(NDEF_URI_POP); + record.setPayload(uri.substring(4).c_str()); + } else if (uri.startsWith("sip:")) { + record.setStatus(NDEF_URI_SIP); + record.setPayload(uri.substring(4).c_str()); + } else if (uri.startsWith("sips:")) { + record.setStatus(NDEF_URI_SIPS); + record.setPayload(uri.substring(5).c_str()); + } else if (uri.startsWith("tftp:")) { + record.setStatus(NDEF_URI_TFTP); + record.setPayload(uri.substring(5).c_str()); + } else if (uri.startsWith("btspp://")) { + record.setStatus(NDEF_URI_BTSPP); + record.setPayload(uri.substring(8).c_str()); + } else if (uri.startsWith("btl2cap://")) { + record.setStatus(NDEF_URI_BTL2CAP); + record.setPayload(uri.substring(10).c_str()); + } else if (uri.startsWith("btgoep://")) { + record.setStatus(NDEF_URI_BTGOEP); + record.setPayload(uri.substring(9).c_str()); + } else if (uri.startsWith("tcpobex://")) { + record.setStatus(NDEF_URI_TCPOBEX); + record.setPayload(uri.substring(10).c_str()); + } else if (uri.startsWith("irdaobex://")) { + record.setStatus(NDEF_URI_IRDAOBEX); + record.setPayload(uri.substring(11).c_str()); + } else if (uri.startsWith("file://")) { + record.setStatus(NDEF_URI_FILE); + record.setPayload(uri.substring(7).c_str()); + } else if (uri.startsWith("urn:epc:id:")) { + record.setStatus(NDEF_URI_URN_EPC_ID); + record.setPayload(uri.substring(11).c_str()); + } else if (uri.startsWith("urn:epc:tag:")) { + record.setStatus(NDEF_URI_URN_EPC_TAG); + record.setPayload(uri.substring(12).c_str()); + } else if (uri.startsWith("urn:epc:pat:")) { + record.setStatus(NDEF_URI_URN_EPC_PAT); + record.setPayload(uri.substring(12).c_str()); + } else if (uri.startsWith("urn:epc:raw:")) { + record.setStatus(NDEF_URI_URN_EPC_RAW); + record.setPayload(uri.substring(12).c_str()); + } else if (uri.startsWith("urn:epc:")) { + record.setStatus(NDEF_URI_URN_EPC); + record.setPayload(uri.substring(8).c_str()); + } else if (uri.startsWith("urn:nfc:")) { + record.setStatus(NDEF_URI_URN_NFC); + record.setPayload(uri.substring(8).c_str()); + } else { + record.setStatus(NDEF_URI_NO_PREFIX); + record.setPayload(uri); + } + + addRecord(record); +} diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 91124a7..116f300 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -3,7 +3,7 @@ * Authors: * Francisco Torres - Electronic Cats - electroniccats.com * - * August 2023 + * December 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, @@ -21,7 +21,6 @@ #include "T4T_NDEF_emu.h" #include "ndef_helper.h" -#define MAX_NDEF_RECORDS 4 #define NDEF_HEADER_FLAGS_SINGLE_RECORD 0xD1 #define NDEF_HEADER_FLAGS_FIRST_RECORD 0x91 #define NDEF_HEADER_FLAGS_NEXT_RECORD 0x11 @@ -32,9 +31,45 @@ #define NDEF_STATUS 0x02 #define NDEF_DEFAULT_LANGUAGE_CODE "en" -class NdefMessage : NdefRecord { +#define NDEF_URI_NO_PREFIX 0x00 +#define NDEF_URI_HTTP_WWWDOT 0x01 +#define NDEF_URI_HTTPS_WWWDOT 0x02 +#define NDEF_URI_HTTP 0x03 +#define NDEF_URI_HTTPS 0x04 +#define NDEF_URI_TEL 0x05 +#define NDEF_URI_MAILTO 0x06 +#define NDEF_URI_FTP_ANONIMOUS 0x07 +#define NDEF_URI_FTP_FTPDOT 0x08 +#define NDEF_URI_FTPS 0x09 +#define NDEF_URI_SFTP 0x0A +#define NDEF_URI_SMB 0x0B +#define NDEF_URI_NFS 0x0C +#define NDEF_URI_FTP 0x0D +#define NDEF_URI_DAV 0x0E +#define NDEF_URI_NEWS 0x0F +#define NDEF_URI_TELNET 0x10 +#define NDEF_URI_IMAP 0x11 +#define NDEF_URI_RTSP 0x12 +#define NDEF_URI_URN 0x13 +#define NDEF_URI_POP 0x14 +#define NDEF_URI_SIP 0x15 +#define NDEF_URI_SIPS 0x16 +#define NDEF_URI_TFTP 0x17 +#define NDEF_URI_BTSPP 0x18 +#define NDEF_URI_BTL2CAP 0x19 +#define NDEF_URI_BTGOEP 0x1A +#define NDEF_URI_TCPOBEX 0x1B +#define NDEF_URI_IRDAOBEX 0x1C +#define NDEF_URI_FILE 0x1D +#define NDEF_URI_URN_EPC_ID 0x1E +#define NDEF_URI_URN_EPC_TAG 0x1F +#define NDEF_URI_URN_EPC_PAT 0x20 +#define NDEF_URI_URN_EPC_RAW 0x21 +#define NDEF_URI_URN_EPC 0x22 +#define NDEF_URI_URN_NFC 0x23 + +class NdefMessage { private: - NdefRecord records[MAX_NDEF_RECORDS]; static uint8_t recordCounter; static unsigned char *content; static unsigned short contentSize; @@ -44,7 +79,6 @@ class NdefMessage : NdefRecord { void getNextRecord(); static String getHexRepresentation(const byte *data, const uint32_t dataSize); static String newString; - static void addContent(const char *record, unsigned short recordSize); void addRecord(NdefRecord record); static void updateHeaderFlags(); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index fd17ba6..ffbe993 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -3,7 +3,7 @@ * Authors: * Francisco Torres - Electronic Cats - electroniccats.com * - * August 2023 + * December 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, @@ -23,6 +23,11 @@ NdefRecord::NdefRecord() { this->status = 0; this->languageCode = 0; this->newString = "null"; + this->textRecord = false; +} + +bool NdefRecord::isTextRecord() { + return this->textRecord; } void NdefRecord::create(NdefRecord_t record) { @@ -240,6 +245,9 @@ String NdefRecord::getUri() { } void NdefRecord::setPayload(String payload) { +#ifdef DEBUG3 + Serial.println("Payload: " + payload); +#endif this->payload = new unsigned char[payload.length()]; strcpy((char *)this->payload, payload.c_str()); } @@ -261,6 +269,7 @@ void NdefRecord::setStatus(uint8_t status) { } void NdefRecord::setLanguageCode(String languageCode) { + this->textRecord = true; this->languageCode = new unsigned char[languageCode.length()]; strcpy((char *)this->languageCode, languageCode.c_str()); } @@ -277,16 +286,23 @@ const char *NdefRecord::getContent() { recordContent[2] = payloadSize; recordContent[3] = recordType; recordContent[4] = status; - recordContent[5] = languageCode[0]; - recordContent[6] = languageCode[1]; - for (int i = 0; i < getPayloadSize(); i++) { - recordContent[i + 7] = payload[i]; + if (isTextRecord()) { + recordContent[5] = languageCode[0]; + recordContent[6] = languageCode[1]; + + for (int i = 0; i < getPayloadSize(); i++) { + recordContent[i + 7] = payload[i]; + } + } else { + for (int i = 0; i < getPayloadSize(); i++) { + recordContent[i + 5] = payload[i]; + } } return recordContent; } unsigned short NdefRecord::getContentSize() { - return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type + return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 0e9236f..8c660d9 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -3,7 +3,7 @@ * Authors: * Francisco Torres - Electronic Cats - electroniccats.com * - * August 2023 + * December 2023 * * This code is beerware; if you see me (or any other collaborator * member) at the local, and you've found our code helpful, @@ -35,8 +35,10 @@ class NdefRecord { uint8_t status; unsigned char *languageCode; unsigned char *payload; - String getHexRepresentation(const byte *data, const uint32_t dataSize); String newString; + bool textRecord; + String getHexRepresentation(const byte *data, const uint32_t dataSize); + bool isTextRecord(); public: NdefRecord(); From f5c7d9fb8e78cc8d64ddca275cc597ba7912e6e1 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 14 Dec 2023 14:09:54 -0600 Subject: [PATCH 15/30] fix: uri payload length --- src/NdefMessage.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++-- src/NdefRecord.cpp | 22 +++++++++++++++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 37a1ec3..180bdae 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -73,6 +73,7 @@ void NdefMessage::setContent(const char *content, unsigned short contentSize) { NdefMessage::contentSize = contentSize; #ifdef DEBUG3 + Serial.println("Content size: " + String(contentSize)); Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); #endif NdefMessage::updateHeaderFlags(); @@ -144,6 +145,10 @@ bool NdefMessage::hasRecord() { void NdefMessage::addRecord(NdefRecord record) { recordCounter++; +#ifdef DEBUG3 + Serial.println("Record size: " + String(record.getContentSize())); +#endif + NdefMessage::newContent = new unsigned char[contentSize + record.getContentSize()]; memcpy(newContent, content, contentSize); memcpy(newContent + contentSize, record.getContent(), record.getContentSize()); @@ -154,7 +159,7 @@ void NdefMessage::addTextRecord(String text, String languageCode) { NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); record.setTypeLength(NDEF_TYPE_LENGTH); - record.setPayloadSize(text.length() + 3); + record.setPayloadSize(text.length() + 3); // 3 = status + language code length record.setRecordType(NDEF_TEXT_RECORD_TYPE); record.setStatus(NDEF_STATUS); record.setLanguageCode(languageCode); @@ -168,120 +173,157 @@ void NdefMessage::addTextRecord(String text) { } void NdefMessage::addUriRecord(String uri) { + uint8_t statusLength = 1; NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); record.setTypeLength(NDEF_TYPE_LENGTH); - record.setPayloadSize(uri.length() + 3); + // record.setPayloadSize(uri.length() + 1); record.setRecordType(NDEF_URI_RECORD_TYPE); if (uri.startsWith("http://www.")) { record.setStatus(NDEF_URI_HTTP_WWWDOT); record.setPayload(uri.substring(11).c_str()); + record.setPayloadSize(uri.length() - 11 + statusLength); } else if (uri.startsWith("https://www.")) { record.setStatus(NDEF_URI_HTTPS_WWWDOT); record.setPayload(uri.substring(12).c_str()); + record.setPayloadSize(uri.length() - 12 + statusLength); } else if (uri.startsWith("http://")) { record.setStatus(NDEF_URI_HTTP); record.setPayload(uri.substring(7).c_str()); + record.setPayloadSize(uri.length() - 7 + statusLength); } else if (uri.startsWith("https://")) { record.setStatus(NDEF_URI_HTTPS); record.setPayload(uri.substring(8).c_str()); + record.setPayloadSize(uri.length() - 8 + statusLength); } else if (uri.startsWith("tel:")) { record.setStatus(NDEF_URI_TEL); record.setPayload(uri.substring(4).c_str()); + record.setPayloadSize(uri.length() - 4 + statusLength); } else if (uri.startsWith("mailto:")) { record.setStatus(NDEF_URI_MAILTO); record.setPayload(uri.substring(7).c_str()); + record.setPayloadSize(uri.length() - 7 + statusLength); } else if (uri.startsWith("ftp://anonymous:anonymous@")) { record.setStatus(NDEF_URI_FTP_ANONIMOUS); record.setPayload(uri.substring(26).c_str()); + record.setPayloadSize(uri.length() - 26 + statusLength); } else if (uri.startsWith("ftp://ftp.")) { record.setStatus(NDEF_URI_FTP_FTPDOT); record.setPayload(uri.substring(9).c_str()); + record.setPayloadSize(uri.length() - 9 + statusLength); } else if (uri.startsWith("ftps://")) { record.setStatus(NDEF_URI_FTPS); record.setPayload(uri.substring(7).c_str()); + record.setPayloadSize(uri.length() - 7 + statusLength); } else if (uri.startsWith("sftp://")) { record.setStatus(NDEF_URI_SFTP); record.setPayload(uri.substring(7).c_str()); + record.setPayloadSize(uri.length() - 7 + statusLength); } else if (uri.startsWith("smb://")) { record.setStatus(NDEF_URI_SMB); record.setPayload(uri.substring(6).c_str()); + record.setPayloadSize(uri.length() - 6 + statusLength); } else if (uri.startsWith("nfs://")) { record.setStatus(NDEF_URI_NFS); record.setPayload(uri.substring(6).c_str()); + record.setPayloadSize(uri.length() - 6 + statusLength); } else if (uri.startsWith("ftp://")) { record.setStatus(NDEF_URI_FTP); record.setPayload(uri.substring(6).c_str()); + record.setPayloadSize(uri.length() - 6 + statusLength); } else if (uri.startsWith("dav://")) { record.setStatus(NDEF_URI_DAV); record.setPayload(uri.substring(6).c_str()); + record.setPayloadSize(uri.length() - 6 + statusLength); } else if (uri.startsWith("news:")) { record.setStatus(NDEF_URI_NEWS); record.setPayload(uri.substring(5).c_str()); + record.setPayloadSize(uri.length() - 5 + statusLength); } else if (uri.startsWith("telnet://")) { record.setStatus(NDEF_URI_TELNET); record.setPayload(uri.substring(9).c_str()); + record.setPayloadSize(uri.length() - 9 + statusLength); } else if (uri.startsWith("imap:")) { record.setStatus(NDEF_URI_IMAP); record.setPayload(uri.substring(5).c_str()); + record.setPayloadSize(uri.length() - 5 + statusLength); } else if (uri.startsWith("rtsp://")) { record.setStatus(NDEF_URI_RTSP); record.setPayload(uri.substring(7).c_str()); + record.setPayloadSize(uri.length() - 7 + statusLength); } else if (uri.startsWith("urn:")) { record.setStatus(NDEF_URI_URN); record.setPayload(uri.substring(4).c_str()); + record.setPayloadSize(uri.length() - 4 + statusLength); } else if (uri.startsWith("pop:")) { record.setStatus(NDEF_URI_POP); record.setPayload(uri.substring(4).c_str()); + record.setPayloadSize(uri.length() - 4 + statusLength); } else if (uri.startsWith("sip:")) { record.setStatus(NDEF_URI_SIP); record.setPayload(uri.substring(4).c_str()); + record.setPayloadSize(uri.length() - 4 + statusLength); } else if (uri.startsWith("sips:")) { record.setStatus(NDEF_URI_SIPS); record.setPayload(uri.substring(5).c_str()); + record.setPayloadSize(uri.length() - 5 + statusLength); } else if (uri.startsWith("tftp:")) { record.setStatus(NDEF_URI_TFTP); record.setPayload(uri.substring(5).c_str()); + record.setPayloadSize(uri.length() - 5 + statusLength); } else if (uri.startsWith("btspp://")) { record.setStatus(NDEF_URI_BTSPP); record.setPayload(uri.substring(8).c_str()); + record.setPayloadSize(uri.length() - 8 + statusLength); } else if (uri.startsWith("btl2cap://")) { record.setStatus(NDEF_URI_BTL2CAP); record.setPayload(uri.substring(10).c_str()); + record.setPayloadSize(uri.length() - 10 + statusLength); } else if (uri.startsWith("btgoep://")) { record.setStatus(NDEF_URI_BTGOEP); record.setPayload(uri.substring(9).c_str()); + record.setPayloadSize(uri.length() - 9 + statusLength); } else if (uri.startsWith("tcpobex://")) { record.setStatus(NDEF_URI_TCPOBEX); record.setPayload(uri.substring(10).c_str()); + record.setPayloadSize(uri.length() - 10 + statusLength); } else if (uri.startsWith("irdaobex://")) { record.setStatus(NDEF_URI_IRDAOBEX); record.setPayload(uri.substring(11).c_str()); + record.setPayloadSize(uri.length() - 11 + statusLength); } else if (uri.startsWith("file://")) { record.setStatus(NDEF_URI_FILE); record.setPayload(uri.substring(7).c_str()); + record.setPayloadSize(uri.length() - 7 + statusLength); } else if (uri.startsWith("urn:epc:id:")) { record.setStatus(NDEF_URI_URN_EPC_ID); record.setPayload(uri.substring(11).c_str()); + record.setPayloadSize(uri.length() - 11 + statusLength); } else if (uri.startsWith("urn:epc:tag:")) { record.setStatus(NDEF_URI_URN_EPC_TAG); record.setPayload(uri.substring(12).c_str()); + record.setPayloadSize(uri.length() - 12 + statusLength); } else if (uri.startsWith("urn:epc:pat:")) { record.setStatus(NDEF_URI_URN_EPC_PAT); record.setPayload(uri.substring(12).c_str()); + record.setPayloadSize(uri.length() - 12 + statusLength); } else if (uri.startsWith("urn:epc:raw:")) { record.setStatus(NDEF_URI_URN_EPC_RAW); record.setPayload(uri.substring(12).c_str()); + record.setPayloadSize(uri.length() - 12 + statusLength); } else if (uri.startsWith("urn:epc:")) { record.setStatus(NDEF_URI_URN_EPC); record.setPayload(uri.substring(8).c_str()); + record.setPayloadSize(uri.length() - 8 + statusLength); } else if (uri.startsWith("urn:nfc:")) { record.setStatus(NDEF_URI_URN_NFC); record.setPayload(uri.substring(8).c_str()); + record.setPayloadSize(uri.length() - 8 + statusLength); } else { record.setStatus(NDEF_URI_NO_PREFIX); record.setPayload(uri); + record.setPayloadSize(uri.length() + statusLength); } addRecord(record); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index ffbe993..1de174d 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -73,7 +73,11 @@ unsigned char *NdefRecord::getPayload() { } unsigned short NdefRecord::getPayloadSize() { - return this->payloadSize; + if (isTextRecord()) { + return this->payloadSize; + } else { + return this->payloadSize + 2; + } } String NdefRecord::getText() { @@ -245,11 +249,15 @@ String NdefRecord::getUri() { } void NdefRecord::setPayload(String payload) { + int length = payload.length(); #ifdef DEBUG3 - Serial.println("Payload: " + payload); + Serial.println("Payload: '" + payload + "'"); #endif this->payload = new unsigned char[payload.length()]; strcpy((char *)this->payload, payload.c_str()); +#ifdef DEBUG3 + // Serial.println("Payload: '" + getHexRepresentation(this->payload, length) + "'"); +#endif } void NdefRecord::setHeaderFlags(uint8_t headerFlags) { @@ -300,9 +308,17 @@ const char *NdefRecord::getContent() { } } +#ifdef DEBUG3 + Serial.println("Payload size: " + String(getPayloadSize())); +#endif + return recordContent; } unsigned short NdefRecord::getContentSize() { - return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type + if (isTextRecord()) { + return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type + } else { + return getPayloadSize() + 2; // 2 bytes for header and payload length + } } From 75ac4735442f8705db9da2d69f3fa7e8e5f18b06 Mon Sep 17 00:00:00 2001 From: deimos Date: Sun, 17 Dec 2023 16:43:44 -0600 Subject: [PATCH 16/30] refactor: is header byte --- examples/NDEFSendMessage/NDEFSendMessage.ino | 14 +++++++++++++- src/NdefMessage.cpp | 13 ++++++++----- src/NdefMessage.h | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 3300d81..894cdf5 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -39,7 +39,19 @@ void setup() { message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly message.addUriRecord("google.com"); // No prefix explicitly message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addUriRecord("rtsp://test"); + // message.addTextRecord("Hello, lorem:50 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + message.addTextRecord("Hello"); // English by default + message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + // message.addUriRecord("http://test2.com"); + // message.addUriRecord("https://test3.com"); + // message.addUriRecord("tel:test4"); + // message.addUriRecord("http://www.test2.com"); + // message.addUriRecord("https://www.test2.com"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 180bdae..c6076e6 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -87,14 +87,14 @@ void NdefMessage::updateHeaderFlags() { uint8_t headersPositions[recordCounter]; uint8_t recordCounterAux = 0; #ifdef DEBUG3 - Serial.print("Header positions:"); + Serial.println("Header positions:"); #endif for (uint8_t i = 0; i < contentSize; i++) { - if (content[i] == NDEF_TYPE_LENGTH) { // New record found + if (isHeaderByte(content[i])) { // New record found #ifdef DEBUG3 - Serial.print(" " + String(i - 1)); + Serial.println("\t" + String(i) + ": " + String(content[i], HEX) + ","); #endif - headersPositions[recordCounterAux] = i - 1; + headersPositions[recordCounterAux] = i; recordCounterAux++; } } @@ -115,6 +115,10 @@ void NdefMessage::updateHeaderFlags() { } } +bool NdefMessage::isHeaderByte(unsigned char byte) { + return (byte == NDEF_HEADER_FLAGS_SINGLE_RECORD) || (byte == NDEF_HEADER_FLAGS_FIRST_RECORD) || (byte == NDEF_HEADER_FLAGS_NEXT_RECORD) || (byte == NDEF_HEADER_FLAGS_LAST_RECORD); +} + NdefRecord_t NdefMessage::getRecord() { NdefRecord_t ndefRecord = DetectNdefRecordType(content); @@ -177,7 +181,6 @@ void NdefMessage::addUriRecord(String uri) { NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); record.setTypeLength(NDEF_TYPE_LENGTH); - // record.setPayloadSize(uri.length() + 1); record.setRecordType(NDEF_URI_RECORD_TYPE); if (uri.startsWith("http://www.")) { diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 116f300..4830398 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -81,6 +81,7 @@ class NdefMessage { static String newString; void addRecord(NdefRecord record); static void updateHeaderFlags(); + static bool isHeaderByte(unsigned char byte); public: NdefMessage(); From 98064a9cd3e3d51108189601a7b5d2e9834ec84d Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 18 Dec 2023 14:04:26 -0600 Subject: [PATCH 17/30] fix: max message length --- examples/NDEFSendMessage/NDEFSendMessage.ino | 14 ++++++++- src/NdefMessage.cpp | 33 ++++++++++++++++---- src/NdefRecord.h | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 894cdf5..dd5d385 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -39,7 +39,11 @@ void setup() { message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly message.addUriRecord("google.com"); // No prefix explicitly message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - // message.addTextRecord("Hello, lorem:50 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + // message.addTextRecord("Hello, lorem:50 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.", "en"); + // message.addTextRecord("lorem:60 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.", "en"); + + // message.addTextRecord("lorem:70 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Donec et mollis dolor.", "en"); + message.addTextRecord("Hello"); // English by default message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php @@ -47,6 +51,14 @@ void setup() { message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addUriRecord("h.com"); + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addUriRecord("h.co"); + // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + // message.addTextRecord("Hello world, this is a test, this is a test"); // English by default + // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + // message.addTextRecord("Hello"); // English by default + // message.addUriRecord("http://test2.com"); // message.addUriRecord("https://test3.com"); // message.addUriRecord("tel:test4"); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index c6076e6..581d663 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -79,28 +79,32 @@ void NdefMessage::setContent(const char *content, unsigned short contentSize) { NdefMessage::updateHeaderFlags(); #ifdef DEBUG3 Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); + Serial.println(); #endif T4T_NDEF_EMU_SetMsg(content, contentSize); } void NdefMessage::updateHeaderFlags() { uint8_t headersPositions[recordCounter]; + uint8_t previousHeaders[recordCounter]; uint8_t recordCounterAux = 0; #ifdef DEBUG3 Serial.println("Header positions:"); #endif for (uint8_t i = 0; i < contentSize; i++) { + if (recordCounterAux == recordCounter) { + break; + } + if (isHeaderByte(content[i])) { // New record found #ifdef DEBUG3 Serial.println("\t" + String(i) + ": " + String(content[i], HEX) + ","); #endif + previousHeaders[recordCounterAux] = content[i]; headersPositions[recordCounterAux] = i; recordCounterAux++; } } -#ifdef DEBUG3 - Serial.println(); -#endif for (uint8_t i = 0; i < recordCounter; i++) { if (i == 0) { @@ -113,6 +117,13 @@ void NdefMessage::updateHeaderFlags() { content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; } } + +#ifdef DEBUG3 + for (uint8_t i = 0; i < recordCounter; i++) { + Serial.print("Header: " + String(previousHeaders[i], HEX) + " -> "); + Serial.println(String(content[headersPositions[i]], HEX)); + } +#endif } bool NdefMessage::isHeaderByte(unsigned char byte) { @@ -147,13 +158,23 @@ bool NdefMessage::hasRecord() { } void NdefMessage::addRecord(NdefRecord record) { - recordCounter++; - #ifdef DEBUG3 Serial.println("Record size: " + String(record.getContentSize())); #endif - NdefMessage::newContent = new unsigned char[contentSize + record.getContentSize()]; + uint16_t newSize = contentSize + record.getContentSize(); + + if (newSize >= 249) { +#ifdef DEBUG3 + Serial.println("NDEF message is full"); +#endif + updateHeaderFlags(); + return; + } + + recordCounter++; + + NdefMessage::newContent = new unsigned char[newSize]; memcpy(newContent, content, contentSize); memcpy(newContent + contentSize, record.getContent(), record.getContentSize()); setContent((const char *)newContent, contentSize + record.getContentSize()); diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 8c660d9..21bf544 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -23,7 +23,7 @@ */ // #define DEBUG // #define DEBUG2 -#define DEBUG3 +// #define DEBUG3 class NdefRecord { private: From a9b25108a2704d1b6ac5896cc4e71d1a83d92458 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 22 Dec 2023 14:06:44 -0600 Subject: [PATCH 18/30] fix: reorder urn condition --- examples/NDEFSendMessage/Makefile | 2 +- examples/NDEFSendMessage/NDEFSendMessage.ino | 69 ++++++++++++-------- src/NdefMessage.cpp | 8 +-- src/NdefRecord.h | 2 +- 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/examples/NDEFSendMessage/Makefile b/examples/NDEFSendMessage/Makefile index aa11614..54bf2b7 100644 --- a/examples/NDEFSendMessage/Makefile +++ b/examples/NDEFSendMessage/Makefile @@ -1,5 +1,5 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat -MONITOR_PORT = /dev/cu.usbmodem11401 +MONITOR_PORT = /dev/cu.usbmodem11101 compile: arduino-cli compile --fqbn $(BOARD_TAG) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index dd5d385..404561f 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -33,37 +33,50 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - message.addTextRecord("Hello"); // English by default - message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now - message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php - message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly - message.addUriRecord("google.com"); // No prefix explicitly - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - // message.addTextRecord("Hello, lorem:50 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.", "en"); - // message.addTextRecord("lorem:60 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.", "en"); + // message.addTextRecord("Hello"); // English by default + // message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + // message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + // message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly + // message.addUriRecord("google.com"); // No prefix explicitly + // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - // message.addTextRecord("lorem:70 ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Donec et mollis dolor.", "en"); + message.addUriRecord("google.com"); + message.addUriRecord("http://www.test1.com"); + message.addUriRecord("https://www.test2.com"); + message.addUriRecord("tel:123456789"); + message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem + message.addUriRecord("ftp://anonymous:anonymous@test5"); + message.addUriRecord("ftp://ftp.test6"); + // message.addUriRecord("ftps://test7"); + // message.addUriRecord("sftp://test8"); + // message.addUriRecord("smb://test9"); + // message.addUriRecord("nfs://test10"); + // message.addUriRecord("ftp://test11"); + // message.addUriRecord("dav://test12"); + // message.addUriRecord("news:test13"); + // message.addUriRecord("telnet://test14"); + // message.addUriRecord("imap://test15"); + // message.addUriRecord("rtsp://test16"); + message.addUriRecord("urn:test17"); + // message.addUriRecord("pop:test18"); + // message.addUriRecord("sip:test19"); + // message.addUriRecord("sips:test20"); + // message.addUriRecord("tftp://test21"); + // message.addUriRecord("btspp://test22"); + // message.addUriRecord("btl2cap://test23"); + // message.addUriRecord("btgoep://test24"); + // message.addUriRecord("tcpobex://test25"); + // message.addUriRecord("irdaobex://test26"); + // message.addUriRecord("file://test27"); - message.addTextRecord("Hello"); // English by default - message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now - message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php - message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addUriRecord("h.com"); - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addUriRecord("h.co"); - // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - // message.addTextRecord("Hello world, this is a test, this is a test"); // English by default - // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - // message.addTextRecord("Hello"); // English by default + // message.addUriRecord("urn:epc:id:sgtin:0614141.107346.1"); - // message.addUriRecord("http://test2.com"); - // message.addUriRecord("https://test3.com"); - // message.addUriRecord("tel:test4"); - // message.addUriRecord("http://www.test2.com"); - // message.addUriRecord("https://www.test2.com"); + message.addUriRecord("urn:epc:id:test28"); + message.addUriRecord("urn:epc:tag:test29"); + message.addUriRecord("urn:epc:pat:sgtin96:3.0614141.812345.6789"); + message.addUriRecord("urn:epc:raw:test30"); + message.addUriRecord("urn:epc:test31"); + message.addUriRecord("urn:nfc:test32"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 581d663..2367137 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -276,10 +276,6 @@ void NdefMessage::addUriRecord(String uri) { record.setStatus(NDEF_URI_RTSP); record.setPayload(uri.substring(7).c_str()); record.setPayloadSize(uri.length() - 7 + statusLength); - } else if (uri.startsWith("urn:")) { - record.setStatus(NDEF_URI_URN); - record.setPayload(uri.substring(4).c_str()); - record.setPayloadSize(uri.length() - 4 + statusLength); } else if (uri.startsWith("pop:")) { record.setStatus(NDEF_URI_POP); record.setPayload(uri.substring(4).c_str()); @@ -344,6 +340,10 @@ void NdefMessage::addUriRecord(String uri) { record.setStatus(NDEF_URI_URN_NFC); record.setPayload(uri.substring(8).c_str()); record.setPayloadSize(uri.length() - 8 + statusLength); + } else if (uri.startsWith("urn:")) { + record.setStatus(NDEF_URI_URN); + record.setPayload(uri.substring(4).c_str()); + record.setPayloadSize(uri.length() - 4 + statusLength); } else { record.setStatus(NDEF_URI_NO_PREFIX); record.setPayload(uri); diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 21bf544..8c660d9 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -23,7 +23,7 @@ */ // #define DEBUG // #define DEBUG2 -// #define DEBUG3 +#define DEBUG3 class NdefRecord { private: From 24c1a4c5f1c9898285e1bfef51acd241e2f937eb Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 26 Dec 2023 13:48:29 -0600 Subject: [PATCH 19/30] test: wifi media record --- examples/NDEFSendMessage/NDEFSendMessage.ino | 52 ++------ examples/NDEFSendRawMessage/Makefile | 2 +- .../NDEFSendRawMessage/NDEFSendRawMessage.ino | 120 +++++++++++++++++- src/NdefMessage.h | 1 + 4 files changed, 124 insertions(+), 51 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 404561f..6585699 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -4,7 +4,6 @@ * Note: If you know how to create custom NDEF messages, you can use the NDEFSendRawMessage example, otherwise, use this example. * * Authors: - * Salvador Mendoza - @Netxing - salmg.net * Francisco Torres - Electronic Cats - electroniccats.com * * December 2023 @@ -33,50 +32,15 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - // message.addTextRecord("Hello"); // English by default - // message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now - // message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php - // message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly - // message.addUriRecord("google.com"); // No prefix explicitly - // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - - message.addUriRecord("google.com"); - message.addUriRecord("http://www.test1.com"); - message.addUriRecord("https://www.test2.com"); - message.addUriRecord("tel:123456789"); + message.addTextRecord("Hello"); // English by default + message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly + message.addUriRecord("google.com"); // No prefix explicitly + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addMimeMediaRecord("application/vnd.wfa.wsc", "Wi-Fi Easy Connect"); + message.addUriRecord("tel:1234567890"); message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem - message.addUriRecord("ftp://anonymous:anonymous@test5"); - message.addUriRecord("ftp://ftp.test6"); - // message.addUriRecord("ftps://test7"); - // message.addUriRecord("sftp://test8"); - // message.addUriRecord("smb://test9"); - // message.addUriRecord("nfs://test10"); - // message.addUriRecord("ftp://test11"); - // message.addUriRecord("dav://test12"); - // message.addUriRecord("news:test13"); - // message.addUriRecord("telnet://test14"); - // message.addUriRecord("imap://test15"); - // message.addUriRecord("rtsp://test16"); - message.addUriRecord("urn:test17"); - // message.addUriRecord("pop:test18"); - // message.addUriRecord("sip:test19"); - // message.addUriRecord("sips:test20"); - // message.addUriRecord("tftp://test21"); - // message.addUriRecord("btspp://test22"); - // message.addUriRecord("btl2cap://test23"); - // message.addUriRecord("btgoep://test24"); - // message.addUriRecord("tcpobex://test25"); - // message.addUriRecord("irdaobex://test26"); - // message.addUriRecord("file://test27"); - - // message.addUriRecord("urn:epc:id:sgtin:0614141.107346.1"); - - message.addUriRecord("urn:epc:id:test28"); - message.addUriRecord("urn:epc:tag:test29"); - message.addUriRecord("urn:epc:pat:sgtin96:3.0614141.812345.6789"); - message.addUriRecord("urn:epc:raw:test30"); - message.addUriRecord("urn:epc:test31"); - message.addUriRecord("urn:nfc:test32"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/examples/NDEFSendRawMessage/Makefile b/examples/NDEFSendRawMessage/Makefile index aa11614..54bf2b7 100644 --- a/examples/NDEFSendRawMessage/Makefile +++ b/examples/NDEFSendRawMessage/Makefile @@ -1,5 +1,5 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat -MONITOR_PORT = /dev/cu.usbmodem11401 +MONITOR_PORT = /dev/cu.usbmodem11101 compile: arduino-cli compile --fqbn $(BOARD_TAG) diff --git a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino index 88d5456..16f39be 100644 --- a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino +++ b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino @@ -50,12 +50,120 @@ NdefMessage message; // 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload // One uri record "https://www.electroniccats.com" -const char ndefMessage[] = {0xD1, - 0x01, - 0x13, - 'U', - 0x02, - 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; +// const char ndefMessage[] = {0xD1, +// 0x01, +// 0x13, +// 'U', +// 0x02, +// 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; + +// const char ndefMessage[] = {0xD1, 0x02, 0x1A, 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.', 'w', 'f', 'a', '.', 'w', 's', 'c', 0x01, 0x00, 0x00, 0x00, 0x04, 't', 'e', 's', 't', 0x00, 0x00, 0x00, 0x01, 0x00, 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; +// One media record of type "application/vnd.wfa.wsc", with network name = test, Authentication type = OPEN, Encription type = WEP, Password = password +// D2 17 29 61 70 70 6C 69 63 61 74 69 6F 6E 2F 76 6E 64 2E 77 66 61 2E 77 73 63 10 0E 00 36 10 26 00 01 01 10 45 00 04 74 65 73 74 10 03 00 02 00 01 10 0F 00 02 00 01 10 27 00 08 70 61 73 73 77 6F 72 64 +// const char ndefMessage[] = {0xD2, +// 0x17, +// 0x29, +// 0x61, +// 0x70, +// 0x70, +// 0x6C, +// 0x69, +// 0x63, +// 0x61, +// 0x74, +// 0x69, +// 0x6F, +// 0x6E, +// 0x2F, +// 0x76, +// 0x6E, +// 0x64, +// 0x2E, +// 0x77, +// 0x66, +// 0x61, +// 0x2E, +// 0x77, +// 0x73, +// 0x63, +// 0x10, +// 0x0E, +// 0x00, +// 0x36, +// 0x10, +// 0x26, +// 0x00, +// 0x01, +// 0x01, +// 0x10, +// 0x45, +// 0x00, +// 0x04, +// 0x74, +// 0x65, +// 0x73, +// 0x74, +// 0x10, +// 0x03, +// 0x00, +// 0x02, +// 0x00, +// 0x01, +// 0x10, +// 0x0F, +// 0x00, +// 0x02, +// 0x00, +// 0x01, +// 0x10, +// 0x27, +// 0x00, +// 0x08, +// 0x70, +// 0x61, +// 0x73, +// 0x73, +// 0x77, +// 0x6F, +// 0x72, +// 0x64}; +// D2:17:29:61:61:70:70:6C:69:63:61:74:69:6F:6E:2F:76:6E:64:2E:77:66:61:2E:77:73:63:10:0E:00:36:10:26:00:01:01:10:45:00:04:74:65:73:74:10:03:00:02:00:01:10:0F:00:02:00:01:10:27:00:08:70:61:73:73:77:6F:72:64 +const char ndefMessage[] = { + 0xD2, + 0X17, + 0x29, + 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.', 'w', 'f', 'a', '.', 'w', 's', 'c', + 0x10, + 0x0E, + 0x00, + 0x36, + 0x10, + 0x26, + 0x00, + 0x01, + 0x01, + 0x10, + 0x45, + 0x00, + 0x04, + 't', 'e', 's', 't', + 0x10, + 0x03, + 0x00, + 0x02, + 0x00, + 0x01, + 0x10, + 0x0F, + 0x00, + 0x02, + 0x00, + 0x01, + 0x10, + 0x27, + 0x00, + 0x08, + 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; void setup() { Serial.begin(9600); diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 4830398..38419bb 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -96,6 +96,7 @@ class NdefMessage { void addTextRecord(String text); void addTextRecord(String text, String languageCode); void addUriRecord(String uri); + void addMimeMediaRecord(String mimeType, String payload); }; #endif \ No newline at end of file From 04888c9f749659b4b4d3d2d1fad4b3e86eaa347f Mon Sep 17 00:00:00 2001 From: deimos Date: Tue, 26 Dec 2023 14:03:09 -0600 Subject: [PATCH 20/30] fix: addMimeMediaRecord is missing --- examples/NDEFSendMessage/NDEFSendMessage.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 6585699..76efd54 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -38,7 +38,7 @@ void setup() { message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly message.addUriRecord("google.com"); // No prefix explicitly message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addMimeMediaRecord("application/vnd.wfa.wsc", "Wi-Fi Easy Connect"); + // message.addMimeMediaRecord("application/vnd.wfa.wsc", "Wi-Fi Easy Connect"); message.addUriRecord("tel:1234567890"); message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem nfc.setSendMsgCallback(messageSentCallback); From 1d6d478391c9c219608918bc74afb5f8aa2da000 Mon Sep 17 00:00:00 2001 From: deimos Date: Wed, 27 Dec 2023 17:40:28 -0600 Subject: [PATCH 21/30] test: vnd wfa wsc application --- .../NDEFSendRawMessage/NDEFSendRawMessage.ino | 97 +++---------------- src/NdefMessage.cpp | 2 + src/NdefMessage.h | 31 +++++- 3 files changed, 43 insertions(+), 87 deletions(-) diff --git a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino index 16f39be..5e3b9f9 100644 --- a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino +++ b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino @@ -57,82 +57,12 @@ NdefMessage message; // 0x02, // 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; -// const char ndefMessage[] = {0xD1, 0x02, 0x1A, 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.', 'w', 'f', 'a', '.', 'w', 's', 'c', 0x01, 0x00, 0x00, 0x00, 0x04, 't', 'e', 's', 't', 0x00, 0x00, 0x00, 0x01, 0x00, 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; -// One media record of type "application/vnd.wfa.wsc", with network name = test, Authentication type = OPEN, Encription type = WEP, Password = password -// D2 17 29 61 70 70 6C 69 63 61 74 69 6F 6E 2F 76 6E 64 2E 77 66 61 2E 77 73 63 10 0E 00 36 10 26 00 01 01 10 45 00 04 74 65 73 74 10 03 00 02 00 01 10 0F 00 02 00 01 10 27 00 08 70 61 73 73 77 6F 72 64 -// const char ndefMessage[] = {0xD2, -// 0x17, -// 0x29, -// 0x61, -// 0x70, -// 0x70, -// 0x6C, -// 0x69, -// 0x63, -// 0x61, -// 0x74, -// 0x69, -// 0x6F, -// 0x6E, -// 0x2F, -// 0x76, -// 0x6E, -// 0x64, -// 0x2E, -// 0x77, -// 0x66, -// 0x61, -// 0x2E, -// 0x77, -// 0x73, -// 0x63, -// 0x10, -// 0x0E, -// 0x00, -// 0x36, -// 0x10, -// 0x26, -// 0x00, -// 0x01, -// 0x01, -// 0x10, -// 0x45, -// 0x00, -// 0x04, -// 0x74, -// 0x65, -// 0x73, -// 0x74, -// 0x10, -// 0x03, -// 0x00, -// 0x02, -// 0x00, -// 0x01, -// 0x10, -// 0x0F, -// 0x00, -// 0x02, -// 0x00, -// 0x01, -// 0x10, -// 0x27, -// 0x00, -// 0x08, -// 0x70, -// 0x61, -// 0x73, -// 0x73, -// 0x77, -// 0x6F, -// 0x72, -// 0x64}; -// D2:17:29:61:61:70:70:6C:69:63:61:74:69:6F:6E:2F:76:6E:64:2E:77:66:61:2E:77:73:63:10:0E:00:36:10:26:00:01:01:10:45:00:04:74:65:73:74:10:03:00:02:00:01:10:0F:00:02:00:01:10:27:00:08:70:61:73:73:77:6F:72:64 +// One media record of type "application/vnd.wfa.wsc", with network name = test, Authentication type = OPEN, Encription type = AES, Password = password const char ndefMessage[] = { - 0xD2, - 0X17, - 0x29, - 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.', 'w', 'f', 'a', '.', 'w', 's', 'c', + 0xD2, // MB/ME/CF/1/IL/TNF + 0X17, // Type length + 0x2A, // Payload length + 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.', 'w', 'f', 'a', '.', 'w', 's', 'c', // Type 0x10, 0x0E, 0x00, @@ -145,25 +75,26 @@ const char ndefMessage[] = { 0x10, 0x45, 0x00, - 0x04, - 't', 'e', 's', 't', + 0x09, // Length of the Network Name attribute + // 't', 'e', 's', 't', 's', + 'B', 'o', 'm', 'b', 'e', 'r', 'c', 'a', 't', 0x10, 0x03, 0x00, - 0x02, + 0x02, // Length of the Authentication Type attribute 0x00, - 0x01, + 0x01, // Value for Authentication type 0x10, 0x0F, 0x00, - 0x02, + 0x02, // Length of the Encryption Type attribute 0x00, - 0x01, + 0x01, // Value for Encryption type 0x10, 0x27, 0x00, - 0x08, - 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + 0x04, // Length of the Network Key attribute + 'p', 'a', 's', 's'}; void setup() { Serial.begin(9600); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 2367137..1b7bc96 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -352,3 +352,5 @@ void NdefMessage::addUriRecord(String uri) { addRecord(record); } + + diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 38419bb..692ab4e 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -21,10 +21,19 @@ #include "T4T_NDEF_emu.h" #include "ndef_helper.h" -#define NDEF_HEADER_FLAGS_SINGLE_RECORD 0xD1 -#define NDEF_HEADER_FLAGS_FIRST_RECORD 0x91 -#define NDEF_HEADER_FLAGS_NEXT_RECORD 0x11 -#define NDEF_HEADER_FLAGS_LAST_RECORD 0x51 +/* + * MB = Message Begin + * ME = Message End + * CF = Chunk Flag + * SR = Short Record + * IL = ID Length present + * TNF = Type Name Format + */ +#define NDEF_HEADER_FLAGS_SINGLE_RECORD 0xD1 // 1101 0001 -> MB = 1, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 001 +#define NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD 0xD2 // 1101 0010 -> MB = 1, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 010 +#define NDEF_HEADER_FLAGS_FIRST_RECORD 0x91 // 1001 0001 -> MB = 1, ME = 0, CF = 0, SR = 1, IL = 0, TNF = 001 +#define NDEF_HEADER_FLAGS_NEXT_RECORD 0x11 // 0001 0001 -> MB = 0, ME = 0, CF = 0, SR = 1, IL = 0, TNF = 001 +#define NDEF_HEADER_FLAGS_LAST_RECORD 0x51 // 0101 0001 -> MB = 0, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 001 #define NDEF_TYPE_LENGTH 0x01 #define NDEF_TEXT_RECORD_TYPE 'T' #define NDEF_URI_RECORD_TYPE 'U' @@ -68,6 +77,20 @@ #define NDEF_URI_URN_EPC 0x22 #define NDEF_URI_URN_NFC 0x23 +// WiFi authentication types +#define WIFI_AUTH_OPEN 0x01 +#define WIFI_AUTH_WPA_PERSONAL 0x02 +#define WIFI_AUTH_SHARED 0x04 +#define WIFI_AUTH_WPA_ENTERPRISE 0x08 +#define WIFI_AUTH_WPA2_ENTERPRISE 0x10 +#define WIFI_AUTH_WPA2_PERSONAL 0x20 + +// WiFi encryption types +#define WIFI_ENCRYPT_NONE 0x01 +#define WIFI_ENCRYPT_WEP 0x02 +#define WIFI_ENCRYPT_TKIP 0x04 +#define WIFI_ENCRYPT_AES 0x08 + class NdefMessage { private: static uint8_t recordCounter; From 884548acfbfa29e6356b0259285f5fb8ac588a4e Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 28 Dec 2023 16:53:05 -0600 Subject: [PATCH 22/30] feat: add mime media record --- examples/NDEFSendMessage/NDEFSendMessage.ino | 52 ++++++++++++--- src/NdefMessage.cpp | 16 +++++ src/NdefMessage.h | 2 +- src/NdefRecord.cpp | 66 ++++++++++++++++++-- src/NdefRecord.h | 9 ++- 5 files changed, 127 insertions(+), 18 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 76efd54..38bc5a6 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -26,21 +26,55 @@ void messageSentCallback(); 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 NdefMessage message; +const char payload[] = { + 0x10, + 0x0E, + 0x00, + 0x36, + 0x10, + 0x26, + 0x00, + 0x01, + 0x01, + 0x10, + 0x45, + 0x00, + 0x09, // Length of the Network Name attribute + // 't', 'e', 's', 't', 's', + 'B', 'o', 'm', 'b', 'e', 'r', 'c', 'a', 't', + 0x10, + 0x03, + 0x00, + 0x02, // Length of the Authentication Type attribute + 0x00, + 0x01, // Value for Authentication type + 0x10, + 0x0F, + 0x00, + 0x02, // Length of the Encryption Type attribute + 0x00, + 0x01, // Value for Encryption type + 0x10, + 0x27, + 0x00, + 0x04, // Length of the Network Key attribute + 'p', 'a', 's', 's'}; + void setup() { Serial.begin(9600); while (!Serial) ; Serial.println("Send NDEF Message with PN7150"); - message.addTextRecord("Hello"); // English by default - message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now - message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php - message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly - message.addUriRecord("google.com"); // No prefix explicitly - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - // message.addMimeMediaRecord("application/vnd.wfa.wsc", "Wi-Fi Easy Connect"); - message.addUriRecord("tel:1234567890"); - message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem + // message.addTextRecord("Hello"); // English by default + // message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + // message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + // message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly + // message.addUriRecord("google.com"); // No prefix explicitly + // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addMimeMediaRecord("application/vnd.wfa.wsc", payload, sizeof(payload)); + // message.addUriRecord("tel:1234567890"); + // message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 1b7bc96..ae51b71 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -172,6 +172,13 @@ void NdefMessage::addRecord(NdefRecord record) { return; } + if (record.getContent() == NULL) { +#ifdef DEBUG3 + Serial.println("Record content is NULL"); +#endif + return; + } + recordCounter++; NdefMessage::newContent = new unsigned char[newSize]; @@ -353,4 +360,13 @@ void NdefMessage::addUriRecord(String uri) { addRecord(record); } +void NdefMessage::addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadSize) { + NdefRecord record; + record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD); + record.setTypeLength(mimeType.length()); + record.setPayloadSize(payloadSize); + record.setRecordType(mimeType); + record.setPayload(payload, payloadSize); + addRecord(record); +} diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 692ab4e..0fe0d1b 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -119,7 +119,7 @@ class NdefMessage { void addTextRecord(String text); void addTextRecord(String text, String languageCode); void addUriRecord(String uri); - void addMimeMediaRecord(String mimeType, String payload); + void addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadSize); }; #endif \ No newline at end of file diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 1de174d..13ca64b 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -19,7 +19,7 @@ NdefRecord::NdefRecord() { this->payload = NULL; this->payloadSize = 0; this->typeLength = 0; - this->recordType = 0; + this->wellKnownType = 0; this->status = 0; this->languageCode = 0; this->newString = "null"; @@ -73,6 +73,10 @@ unsigned char *NdefRecord::getPayload() { } unsigned short NdefRecord::getPayloadSize() { + if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { + return this->payloadSize; + } + if (isTextRecord()) { return this->payloadSize; } else { @@ -253,13 +257,22 @@ void NdefRecord::setPayload(String payload) { #ifdef DEBUG3 Serial.println("Payload: '" + payload + "'"); #endif + this->payload = new unsigned char[payload.length()]; strcpy((char *)this->payload, payload.c_str()); + #ifdef DEBUG3 // Serial.println("Payload: '" + getHexRepresentation(this->payload, length) + "'"); #endif } +void NdefRecord::setPayload(const char *payload, unsigned short payloadLength) { + this->payload = (unsigned char *)payload; +#ifdef DEBUG3 + Serial.println("Payload: '" + getHexRepresentation(this->payload, payloadSize) + "'"); +#endif +} + void NdefRecord::setHeaderFlags(uint8_t headerFlags) { this->headerFlags = headerFlags; } @@ -268,8 +281,13 @@ void NdefRecord::setTypeLength(uint8_t typeLength) { this->typeLength = typeLength; } -void NdefRecord::setRecordType(uint8_t recordType) { - this->recordType = recordType; +void NdefRecord::setRecordType(uint8_t wellKnownType) { + this->wellKnownType = wellKnownType; +} + +void NdefRecord::setRecordType(String type) { + this->mimeMediaType = new unsigned char[type.length()]; + strcpy((char *)this->mimeMediaType, type.c_str()); } void NdefRecord::setStatus(uint8_t status) { @@ -286,13 +304,13 @@ void NdefRecord::setPayloadSize(uint8_t payloadSize) { this->payloadSize = payloadSize; } -const char *NdefRecord::getContent() { +const char *NdefRecord::getWellKnownContent() { char *recordContent = new char[getPayloadSize()]; recordContent[0] = headerFlags; recordContent[1] = typeLength; recordContent[2] = payloadSize; - recordContent[3] = recordType; + recordContent[3] = wellKnownType; recordContent[4] = status; if (isTextRecord()) { @@ -308,14 +326,50 @@ const char *NdefRecord::getContent() { } } + return recordContent; +} + +const char *NdefRecord::getMimeMediaContent() { + char *recordContent = new char[getPayloadSize()]; + + recordContent[0] = headerFlags; + recordContent[1] = typeLength; + recordContent[2] = payloadSize; + + for (int i = 0; i < typeLength; i++) { + recordContent[i + 3] = mimeMediaType[i]; + } + + for (int i = 0; i < getPayloadSize(); i++) { + recordContent[i + 3 + typeLength] = payload[i]; + } + + return recordContent; +} + +const char *NdefRecord::getContent() { #ifdef DEBUG3 Serial.println("Payload size: " + String(getPayloadSize())); #endif - return recordContent; + // Search in the last 3 bits of headerFlags + if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + Serial.println("Well known record"); + return getWellKnownContent(); + } else if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { + Serial.println("Media record"); + return getMimeMediaContent(); + } else { + Serial.println("Unknown record"); + return NULL; + } } unsigned short NdefRecord::getContentSize() { + if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { + return getPayloadSize() + typeLength + 3; // 3 bytes for header, type length and payload length + } + if (isTextRecord()) { return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type } else { diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 8c660d9..6b70514 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -31,7 +31,8 @@ class NdefRecord { uint8_t headerFlags; uint8_t typeLength; unsigned short payloadSize; - uint8_t recordType; + uint8_t wellKnownType; + unsigned char *mimeMediaType; uint8_t status; unsigned char *languageCode; unsigned char *payload; @@ -39,6 +40,8 @@ class NdefRecord { bool textRecord; String getHexRepresentation(const byte *data, const uint32_t dataSize); bool isTextRecord(); + const char *getWellKnownContent(); + const char *getMimeMediaContent(); public: NdefRecord(); @@ -59,9 +62,11 @@ class NdefRecord { String getVCardContent(); String getUri(); void setPayload(String payload); + void setPayload(const char *payload, unsigned short payloadLength); void setHeaderFlags(uint8_t headerFlags); void setTypeLength(uint8_t typeLength); - void setRecordType(uint8_t recordType); + void setRecordType(uint8_t wellKnownType); + void setRecordType(String type); void setStatus(uint8_t status); void setLanguageCode(String languageCode); void setPayloadSize(uint8_t payloadSize); From 9b49bd0295d72a5c2b33af7e5056a59749b59fe3 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 28 Dec 2023 18:28:28 -0600 Subject: [PATCH 23/30] fix: header media positions --- examples/NDEFReadMessage/Makefile | 2 +- examples/NDEFSendMessage/NDEFSendMessage.ino | 16 +++++----- src/NdefMessage.cpp | 33 ++++++++++++++++---- src/NdefMessage.h | 3 ++ src/NdefRecord.cpp | 6 ++++ src/NdefRecord.h | 2 +- 6 files changed, 46 insertions(+), 16 deletions(-) diff --git a/examples/NDEFReadMessage/Makefile b/examples/NDEFReadMessage/Makefile index 5872a38..0bd1828 100644 --- a/examples/NDEFReadMessage/Makefile +++ b/examples/NDEFReadMessage/Makefile @@ -1,6 +1,6 @@ BOARD_TAG = electroniccats:mbed_rp2040:bombercat # BOARD_TAG = rp2040:rp2040:generic -MONITOR_PORT = /dev/cu.usbmodem11401 +MONITOR_PORT = /dev/cu.usbmodem11101 compile: arduino-cli compile --fqbn $(BOARD_TAG) --warnings all diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 38bc5a6..b8fa3ff 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -45,7 +45,7 @@ const char payload[] = { 0x10, 0x03, 0x00, - 0x02, // Length of the Authentication Type attribute + 0x02, 0x00, 0x01, // Value for Authentication type 0x10, @@ -66,14 +66,14 @@ void setup() { ; Serial.println("Send NDEF Message with PN7150"); - // message.addTextRecord("Hello"); // English by default - // message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now - // message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php - // message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly - // message.addUriRecord("google.com"); // No prefix explicitly - // message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addTextRecord("Hello"); // English by default + message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now + message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php + message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly message.addMimeMediaRecord("application/vnd.wfa.wsc", payload, sizeof(payload)); - // message.addUriRecord("tel:1234567890"); + message.addUriRecord("google.com"); // No prefix explicitly + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table + message.addUriRecord("tel:1234567890"); // message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem nfc.setSendMsgCallback(messageSentCallback); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index ae51b71..84dfae2 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -108,13 +108,34 @@ void NdefMessage::updateHeaderFlags() { for (uint8_t i = 0; i < recordCounter; i++) { if (i == 0) { - content[headersPositions[i]] = NDEF_HEADER_FLAGS_SINGLE_RECORD; + if ((content[headersPositions[i]] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_SINGLE_RECORD; + } else { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD; + } } else if (i == 1) { - content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_FIRST_RECORD; - content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; + if ((content[headersPositions[i - 1]] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_FIRST_RECORD; + } else { + content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_FIRST_MEDIA_RECORD; + } + if ((content[headersPositions[i]] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; + } else { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_MEDIA_RECORD; + } } else { - content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_NEXT_RECORD; - content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; + if ((content[headersPositions[i - 1]] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_NEXT_RECORD; + } else { + content[headersPositions[i - 1]] = NDEF_HEADER_FLAGS_NEXT_MEDIA_RECORD; + } + + if ((content[headersPositions[i]] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_RECORD; + } else { + content[headersPositions[i]] = NDEF_HEADER_FLAGS_LAST_MEDIA_RECORD; + } } } @@ -127,7 +148,7 @@ void NdefMessage::updateHeaderFlags() { } bool NdefMessage::isHeaderByte(unsigned char byte) { - return (byte == NDEF_HEADER_FLAGS_SINGLE_RECORD) || (byte == NDEF_HEADER_FLAGS_FIRST_RECORD) || (byte == NDEF_HEADER_FLAGS_NEXT_RECORD) || (byte == NDEF_HEADER_FLAGS_LAST_RECORD); + return (byte == NDEF_HEADER_FLAGS_SINGLE_RECORD) || (byte == NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD) || (byte == NDEF_HEADER_FLAGS_FIRST_RECORD) || (byte == NDEF_HEADER_FLAGS_FIRST_MEDIA_RECORD) || (byte == NDEF_HEADER_FLAGS_NEXT_RECORD) || (byte == NDEF_HEADER_FLAGS_NEXT_MEDIA_RECORD) || (byte == NDEF_HEADER_FLAGS_LAST_RECORD) || (byte == NDEF_HEADER_FLAGS_LAST_MEDIA_RECORD); } NdefRecord_t NdefMessage::getRecord() { diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 0fe0d1b..f6cbffe 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -32,8 +32,11 @@ #define NDEF_HEADER_FLAGS_SINGLE_RECORD 0xD1 // 1101 0001 -> MB = 1, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 001 #define NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD 0xD2 // 1101 0010 -> MB = 1, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 010 #define NDEF_HEADER_FLAGS_FIRST_RECORD 0x91 // 1001 0001 -> MB = 1, ME = 0, CF = 0, SR = 1, IL = 0, TNF = 001 +#define NDEF_HEADER_FLAGS_FIRST_MEDIA_RECORD 0x92 // 1001 0010 -> MB = 1, ME = 0, CF = 0, SR = 1, IL = 0, TNF = 010 #define NDEF_HEADER_FLAGS_NEXT_RECORD 0x11 // 0001 0001 -> MB = 0, ME = 0, CF = 0, SR = 1, IL = 0, TNF = 001 +#define NDEF_HEADER_FLAGS_NEXT_MEDIA_RECORD 0x12 // 0001 0010 -> MB = 0, ME = 0, CF = 0, SR = 1, IL = 0, TNF = 010 #define NDEF_HEADER_FLAGS_LAST_RECORD 0x51 // 0101 0001 -> MB = 0, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 001 +#define NDEF_HEADER_FLAGS_LAST_MEDIA_RECORD 0x52 // 0101 0010 -> MB = 0, ME = 1, CF = 0, SR = 1, IL = 0, TNF = 010 #define NDEF_TYPE_LENGTH 0x01 #define NDEF_TEXT_RECORD_TYPE 'T' #define NDEF_URI_RECORD_TYPE 'U' diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 13ca64b..59f4f8a 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -354,13 +354,19 @@ const char *NdefRecord::getContent() { // Search in the last 3 bits of headerFlags if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { +#ifdef DEBUG3 Serial.println("Well known record"); +#endif return getWellKnownContent(); } else if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { +#ifdef DEBUG3 Serial.println("Media record"); +#endif return getMimeMediaContent(); } else { +#ifdef DEBUG3 Serial.println("Unknown record"); +#endif return NULL; } } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 6b70514..cd70610 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -23,7 +23,7 @@ */ // #define DEBUG // #define DEBUG2 -#define DEBUG3 +// #define DEBUG3 class NdefRecord { private: From 9b05cb147ffd9b8f12c1d00a52a1d66c16515506 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 28 Dec 2023 20:37:44 -0600 Subject: [PATCH 24/30] feat: add wifi record method --- examples/NDEFSendMessage/NDEFSendMessage.ino | 40 ++------- src/NdefMessage.cpp | 91 ++++++++++++++++++++ src/NdefMessage.h | 3 + src/NdefRecord.h | 2 +- 4 files changed, 100 insertions(+), 36 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index b8fa3ff..8ab3b9a 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -26,40 +26,6 @@ void messageSentCallback(); 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 NdefMessage message; -const char payload[] = { - 0x10, - 0x0E, - 0x00, - 0x36, - 0x10, - 0x26, - 0x00, - 0x01, - 0x01, - 0x10, - 0x45, - 0x00, - 0x09, // Length of the Network Name attribute - // 't', 'e', 's', 't', 's', - 'B', 'o', 'm', 'b', 'e', 'r', 'c', 'a', 't', - 0x10, - 0x03, - 0x00, - 0x02, - 0x00, - 0x01, // Value for Authentication type - 0x10, - 0x0F, - 0x00, - 0x02, // Length of the Encryption Type attribute - 0x00, - 0x01, // Value for Encryption type - 0x10, - 0x27, - 0x00, - 0x04, // Length of the Network Key attribute - 'p', 'a', 's', 's'}; - void setup() { Serial.begin(9600); while (!Serial) @@ -70,10 +36,14 @@ void setup() { message.addTextRecord("world", "en"); // English explicitly, the library only supports two letter language codes (ISO 639-1) by now message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly - message.addMimeMediaRecord("application/vnd.wfa.wsc", payload, sizeof(payload)); message.addUriRecord("google.com"); // No prefix explicitly message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table message.addUriRecord("tel:1234567890"); + String ssid = "Bomber Cat"; + String authentificationType = "WPA2 PERSONAL"; + String encryptionType = "AES"; + String password = "Password"; + message.addWiFiRecord(ssid, authentificationType, encryptionType, password); // message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem nfc.setSendMsgCallback(messageSentCallback); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 84dfae2..5267327 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -106,6 +106,7 @@ void NdefMessage::updateHeaderFlags() { } } + // TODO: refactor this to make it more generic for (uint8_t i = 0; i < recordCounter; i++) { if (i == 0) { if ((content[headersPositions[i]] & NDEF_RECORD_TNF_MASK) == NDEF_WELL_KNOWN) { @@ -391,3 +392,93 @@ void NdefMessage::addMimeMediaRecord(String mimeType, const char *payload, unsig addRecord(record); } + +void NdefMessage::addWiFiRecord(String ssid, String authenticationType, String encryptionType, String password) { + NdefRecord record; + String mimeType = "application/vnd.wfa.wsc"; + unsigned char *payload = new unsigned char[ssid.length() + password.length() + 29]; // TODO: 29 must be calculated + + payload[0] = 0x10; + payload[1] = 0x0E; + payload[2] = 0x00; + payload[3] = 0x36; + payload[4] = 0x10; + payload[5] = 0x26; + payload[6] = 0x00; + payload[7] = 0x01; + payload[8] = 0x01; + payload[9] = 0x10; + payload[10] = 0x45; + payload[11] = 0x00; + payload[12] = ssid.length(); + for (int i = 0; i < ssid.length(); i++) { + payload[13 + i] = ssid[i]; + } + payload[13 + ssid.length()] = 0x10; + payload[14 + ssid.length()] = 0x03; + payload[15 + ssid.length()] = 0x00; + payload[16 + ssid.length()] = 0x02; + payload[17 + ssid.length()] = 0x00; + payload[18 + ssid.length()] = getWiFiAuthenticationType(authenticationType); + payload[19 + ssid.length()] = 0x10; + payload[20 + ssid.length()] = 0x0F; + payload[21 + ssid.length()] = 0x00; + payload[22 + ssid.length()] = 0x02; + payload[23 + ssid.length()] = 0x00; + payload[24 + ssid.length()] = getWiFiEncryptionType(encryptionType); + payload[25 + ssid.length()] = 0x10; + payload[26 + ssid.length()] = 0x27; + payload[27 + ssid.length()] = 0x00; + payload[28 + ssid.length()] = password.length(); + for (int i = 0; i < password.length(); i++) { + payload[29 + ssid.length() + i] = password[i]; + } + + record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD); + record.setTypeLength(mimeType.length()); + record.setPayloadSize(ssid.length() + password.length() + 29); // TODO: 29 must be calculated + record.setRecordType(mimeType); + record.setPayload((const char *)payload, sizeof(payload)); + + addRecord(record); +} + +uint8_t NdefMessage::getWiFiAuthenticationType(String authenticationType) { + uint8_t authenticationTypeValue = 0x00; + authenticationType.trim(); + authenticationType.toUpperCase(); + + if (authenticationType == "OPEN") { + authenticationTypeValue = WIFI_AUTH_OPEN; + } else if (authenticationType == "WPA PERSONAL") { + authenticationTypeValue = WIFI_AUTH_WPA_PERSONAL; + } else if (authenticationType == "SHARED") { + authenticationTypeValue = WIFI_AUTH_SHARED; + } else if (authenticationType == "WPA ENTERPRISE") { + authenticationTypeValue = WIFI_AUTH_WPA_ENTERPRISE; + } else if (authenticationType == "WPA2 ENTERPRISE") { + authenticationTypeValue = WIFI_AUTH_WPA2_ENTERPRISE; + } else if (authenticationType == "WPA2 PERSONAL") { + authenticationTypeValue = WIFI_AUTH_WPA2_PERSONAL; + } + + return authenticationTypeValue; +} + +uint8_t NdefMessage::getWiFiEncryptionType(String encryptionType) { + uint8_t encryptionTypeValue = 0x00; + encryptionType.trim(); + encryptionType.toUpperCase(); + + if (encryptionType == "NONE") { + encryptionTypeValue = WIFI_ENCRYPT_NONE; + } else if (encryptionType == "WEP") { + encryptionTypeValue = WIFI_ENCRYPT_WEP; + } else if (encryptionType == "TKIP") { + encryptionTypeValue = WIFI_ENCRYPT_TKIP; + } else if (encryptionType == "AES") { + encryptionTypeValue = WIFI_ENCRYPT_AES; + } + + return encryptionTypeValue; +} diff --git a/src/NdefMessage.h b/src/NdefMessage.h index f6cbffe..86ea6e3 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -108,6 +108,8 @@ class NdefMessage { void addRecord(NdefRecord record); static void updateHeaderFlags(); static bool isHeaderByte(unsigned char byte); + uint8_t getWiFiAuthenticationType(String authenticationType); + uint8_t getWiFiEncryptionType(String encryptionType); public: NdefMessage(); @@ -123,6 +125,7 @@ class NdefMessage { void addTextRecord(String text, String languageCode); void addUriRecord(String uri); void addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadSize); + void addWiFiRecord(String ssid, String authenticationType, String encryptionType, String password); }; #endif \ No newline at end of file diff --git a/src/NdefRecord.h b/src/NdefRecord.h index cd70610..6b70514 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -23,7 +23,7 @@ */ // #define DEBUG // #define DEBUG2 -// #define DEBUG3 +#define DEBUG3 class NdefRecord { private: From f5a21050aa00c760f20bd3bdc6a4a4a256775c4f Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 28 Dec 2023 21:09:17 -0600 Subject: [PATCH 25/30] fix: add support for symbols --- examples/NDEFSendMessage/NDEFSendMessage.ino | 2 +- src/NdefRecord.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 8ab3b9a..91f9466 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -44,7 +44,7 @@ void setup() { String encryptionType = "AES"; String password = "Password"; message.addWiFiRecord(ssid, authentificationType, encryptionType, password); - // message.addUriRecord("mailto:deimoshallgmail.com"); // TODO: check @ problem + message.addUriRecord("mailto:deimoshall@gmail.com"); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index 59f4f8a..cac3994 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -259,10 +259,11 @@ void NdefRecord::setPayload(String payload) { #endif this->payload = new unsigned char[payload.length()]; - strcpy((char *)this->payload, payload.c_str()); + strncpy((char *)this->payload, payload.c_str(), payload.length()); #ifdef DEBUG3 - // Serial.println("Payload: '" + getHexRepresentation(this->payload, length) + "'"); + Serial.println("Payload length: " + String(length)); + Serial.println("Payload: '" + getHexRepresentation(this->payload, length) + "'"); #endif } From 4e8b0dc465e6bccf242217221ce855fa01963017 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 28 Dec 2023 21:49:59 -0600 Subject: [PATCH 26/30] refactor: size by length in some methods --- examples/NDEFReadMessage/NDEFReadMessage.ino | 10 +- .../NDEFSendRawMessage/NDEFSendRawMessage.ino | 87 +++-------- src/NdefMessage.cpp | 136 +++++++++--------- src/NdefMessage.h | 14 +- src/NdefRecord.cpp | 72 +++++----- src/NdefRecord.h | 10 +- src/T4T_NDEF_emu.cpp | 24 ++-- src/T4T_NDEF_emu.h | 4 +- src/ndef_helper.cpp | 6 +- src/ndef_helper.h | 2 +- 10 files changed, 159 insertions(+), 206 deletions(-) diff --git a/examples/NDEFReadMessage/NDEFReadMessage.ino b/examples/NDEFReadMessage/NDEFReadMessage.ino index 927f2cc..b30bd8b 100644 --- a/examples/NDEFReadMessage/NDEFReadMessage.ino +++ b/examples/NDEFReadMessage/NDEFReadMessage.ino @@ -96,7 +96,7 @@ void messageReceivedCallback() { } Serial.print("NDEF message: "); - Serial.println(getHexRepresentation(message.getContent(), message.getContentSize())); + Serial.println(getHexRepresentation(message.getContent(), message.getContentLength())); // Show NDEF message details, it is composed of records do { @@ -253,16 +253,16 @@ void displayRecordInfo(NdefRecord record) { case record.type.MEDIA_HANDOVER_BLE: Serial.print("\tBLE Handover"); - Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); + Serial.println("\t- Payload size: " + String(record.getPayloadLength()) + " bytes"); Serial.print("\t- Payload = "); - Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); + Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadLength())); break; case record.type.MEDIA_HANDOVER_BLE_SECURE: Serial.print("\tBLE secure Handover"); - Serial.println("\t- Payload size: " + String(record.getPayloadSize()) + " bytes"); + Serial.println("\t- Payload size: " + String(record.getPayloadLength()) + " bytes"); Serial.print("\t- Payload = "); - Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadSize())); + Serial.println(getHexRepresentation(record.getPayload(), record.getPayloadLength())); break; default: diff --git a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino index 5e3b9f9..377c048 100644 --- a/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino +++ b/examples/NDEFSendRawMessage/NDEFSendRawMessage.ino @@ -28,73 +28,26 @@ Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a gl NdefMessage message; // Three records, "Hello", "world" and Uri "https://www.electroniccats.com" -// const char ndefMessage[] = {0x91, // 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 -// 0x11, // 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 -// 'w', 'o', 'r', 'l', 'd', // Message Payload -// 0x51, // MB/ME/CF/1/IL/TNF -// 0x01, // Type length (1 byte) -// 0x13, // Payload length -// 'U', // Type -> 'T' for text, 'U' for URI -// 0x02, // Status -// 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload - -// One uri record "https://www.electroniccats.com" -// const char ndefMessage[] = {0xD1, -// 0x01, -// 0x13, -// 'U', -// 0x02, -// 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; - -// One media record of type "application/vnd.wfa.wsc", with network name = test, Authentication type = OPEN, Encription type = AES, Password = password -const char ndefMessage[] = { - 0xD2, // MB/ME/CF/1/IL/TNF - 0X17, // Type length - 0x2A, // Payload length - 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.', 'w', 'f', 'a', '.', 'w', 's', 'c', // Type - 0x10, - 0x0E, - 0x00, - 0x36, - 0x10, - 0x26, - 0x00, - 0x01, - 0x01, - 0x10, - 0x45, - 0x00, - 0x09, // Length of the Network Name attribute - // 't', 'e', 's', 't', 's', - 'B', 'o', 'm', 'b', 'e', 'r', 'c', 'a', 't', - 0x10, - 0x03, - 0x00, - 0x02, // Length of the Authentication Type attribute - 0x00, - 0x01, // Value for Authentication type - 0x10, - 0x0F, - 0x00, - 0x02, // Length of the Encryption Type attribute - 0x00, - 0x01, // Value for Encryption type - 0x10, - 0x27, - 0x00, - 0x04, // Length of the Network Key attribute - 'p', 'a', 's', 's'}; +const char ndefMessage[] = {0x91, // 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 + 0x11, // 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 + 'w', 'o', 'r', 'l', 'd', // Message Payload + 0x51, // MB/ME/CF/1/IL/TNF + 0x01, // Type length (1 byte) + 0x13, // Payload length + 'U', // Type -> 'T' for text, 'U' for URI + 0x02, // Status + 'e', 'l', 'e', 'c', 't', 'r', 'o', 'n', 'i', 'c', 'c', 'a', 't', 's', '.', 'c', 'o', 'm'}; // Message Payload void setup() { Serial.begin(9600); diff --git a/src/NdefMessage.cpp b/src/NdefMessage.cpp index 5267327..c2c39cc 100644 --- a/src/NdefMessage.cpp +++ b/src/NdefMessage.cpp @@ -14,15 +14,15 @@ #include "NdefMessage.h" unsigned char *NdefMessage::content; -unsigned short NdefMessage::contentSize; +unsigned short NdefMessage::contentLength; unsigned char *NdefMessage::newContent; -unsigned short NdefMessage::newContentSize; +unsigned short NdefMessage::newContentLength; uint8_t NdefMessage::recordCounter; String NdefMessage::newString; NdefMessage::NdefMessage() { content = NULL; - contentSize = 0; + contentLength = 0; recordCounter = 0; newString = "null"; } @@ -31,57 +31,57 @@ void NdefMessage::begin() { registerUpdateNdefMessageCallback(NdefMessage::update); } -String NdefMessage::getHexRepresentation(const byte *data, const uint32_t dataSize) { +String NdefMessage::getHexRepresentation(const byte *data, const uint32_t dataLength) { String hexString; - if (dataSize == 0) { + if (dataLength == 0) { hexString = newString; } - for (uint32_t index = 0; index < dataSize; index++) { + for (uint32_t index = 0; index < dataLength; index++) { if (data[index] <= 0xF) hexString += "0"; String hexValue = String(data[index] & 0xFF, HEX); hexValue.toUpperCase(); hexString += hexValue; - if ((dataSize > 1) && (index != dataSize - 1)) { + if ((dataLength > 1) && (index != dataLength - 1)) { hexString += ":"; } } return hexString; } -void NdefMessage::update(unsigned char *message, unsigned short messageSize) { +void NdefMessage::update(unsigned char *message, unsigned short messageLength) { if (content != NULL) { free(content); } - content = (unsigned char *)malloc(messageSize); - memcpy(content, message, messageSize); - contentSize = messageSize; + content = (unsigned char *)malloc(messageLength); + memcpy(content, message, messageLength); + contentLength = messageLength; } unsigned char *NdefMessage::getContent() { return content; } -unsigned short NdefMessage::getContentSize() { - return contentSize; +unsigned short NdefMessage::getContentLength() { + return contentLength; } -void NdefMessage::setContent(const char *content, unsigned short contentSize) { +void NdefMessage::setContent(const char *content, unsigned short contentLength) { NdefMessage::content = (unsigned char *)content; - NdefMessage::contentSize = contentSize; + NdefMessage::contentLength = contentLength; #ifdef DEBUG3 - Serial.println("Content size: " + String(contentSize)); - Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); + Serial.println("Content length: " + String(contentLength)); + Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentLength)); #endif NdefMessage::updateHeaderFlags(); #ifdef DEBUG3 - Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentSize)); + Serial.println(NdefMessage::getHexRepresentation((byte *)content, (uint32_t)contentLength)); Serial.println(); #endif - T4T_NDEF_EMU_SetMsg(content, contentSize); + T4T_NDEF_EMU_SetMsg(content, contentLength); } void NdefMessage::updateHeaderFlags() { @@ -91,7 +91,7 @@ void NdefMessage::updateHeaderFlags() { #ifdef DEBUG3 Serial.println("Header positions:"); #endif - for (uint8_t i = 0; i < contentSize; i++) { + for (uint8_t i = 0; i < contentLength; i++) { if (recordCounterAux == recordCounter) { break; } @@ -181,12 +181,12 @@ bool NdefMessage::hasRecord() { void NdefMessage::addRecord(NdefRecord record) { #ifdef DEBUG3 - Serial.println("Record size: " + String(record.getContentSize())); + Serial.println("Record length: " + String(record.getContentLength())); #endif - uint16_t newSize = contentSize + record.getContentSize(); + uint16_t newLength = contentLength + record.getContentLength(); - if (newSize >= 249) { + if (newLength >= 249) { #ifdef DEBUG3 Serial.println("NDEF message is full"); #endif @@ -203,17 +203,17 @@ void NdefMessage::addRecord(NdefRecord record) { recordCounter++; - NdefMessage::newContent = new unsigned char[newSize]; - memcpy(newContent, content, contentSize); - memcpy(newContent + contentSize, record.getContent(), record.getContentSize()); - setContent((const char *)newContent, contentSize + record.getContentSize()); + NdefMessage::newContent = new unsigned char[newLength]; + memcpy(newContent, content, contentLength); + memcpy(newContent + contentLength, record.getContent(), record.getContentLength()); + setContent((const char *)newContent, contentLength + record.getContentLength()); } void NdefMessage::addTextRecord(String text, String languageCode) { NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_RECORD); record.setTypeLength(NDEF_TYPE_LENGTH); - record.setPayloadSize(text.length() + 3); // 3 = status + language code length + record.setPayloadLength(text.length() + 3); // 3 = status + language code length record.setRecordType(NDEF_TEXT_RECORD_TYPE); record.setStatus(NDEF_STATUS); record.setLanguageCode(languageCode); @@ -236,159 +236,159 @@ void NdefMessage::addUriRecord(String uri) { if (uri.startsWith("http://www.")) { record.setStatus(NDEF_URI_HTTP_WWWDOT); record.setPayload(uri.substring(11).c_str()); - record.setPayloadSize(uri.length() - 11 + statusLength); + record.setPayloadLength(uri.length() - 11 + statusLength); } else if (uri.startsWith("https://www.")) { record.setStatus(NDEF_URI_HTTPS_WWWDOT); record.setPayload(uri.substring(12).c_str()); - record.setPayloadSize(uri.length() - 12 + statusLength); + record.setPayloadLength(uri.length() - 12 + statusLength); } else if (uri.startsWith("http://")) { record.setStatus(NDEF_URI_HTTP); record.setPayload(uri.substring(7).c_str()); - record.setPayloadSize(uri.length() - 7 + statusLength); + record.setPayloadLength(uri.length() - 7 + statusLength); } else if (uri.startsWith("https://")) { record.setStatus(NDEF_URI_HTTPS); record.setPayload(uri.substring(8).c_str()); - record.setPayloadSize(uri.length() - 8 + statusLength); + record.setPayloadLength(uri.length() - 8 + statusLength); } else if (uri.startsWith("tel:")) { record.setStatus(NDEF_URI_TEL); record.setPayload(uri.substring(4).c_str()); - record.setPayloadSize(uri.length() - 4 + statusLength); + record.setPayloadLength(uri.length() - 4 + statusLength); } else if (uri.startsWith("mailto:")) { record.setStatus(NDEF_URI_MAILTO); record.setPayload(uri.substring(7).c_str()); - record.setPayloadSize(uri.length() - 7 + statusLength); + record.setPayloadLength(uri.length() - 7 + statusLength); } else if (uri.startsWith("ftp://anonymous:anonymous@")) { record.setStatus(NDEF_URI_FTP_ANONIMOUS); record.setPayload(uri.substring(26).c_str()); - record.setPayloadSize(uri.length() - 26 + statusLength); + record.setPayloadLength(uri.length() - 26 + statusLength); } else if (uri.startsWith("ftp://ftp.")) { record.setStatus(NDEF_URI_FTP_FTPDOT); record.setPayload(uri.substring(9).c_str()); - record.setPayloadSize(uri.length() - 9 + statusLength); + record.setPayloadLength(uri.length() - 9 + statusLength); } else if (uri.startsWith("ftps://")) { record.setStatus(NDEF_URI_FTPS); record.setPayload(uri.substring(7).c_str()); - record.setPayloadSize(uri.length() - 7 + statusLength); + record.setPayloadLength(uri.length() - 7 + statusLength); } else if (uri.startsWith("sftp://")) { record.setStatus(NDEF_URI_SFTP); record.setPayload(uri.substring(7).c_str()); - record.setPayloadSize(uri.length() - 7 + statusLength); + record.setPayloadLength(uri.length() - 7 + statusLength); } else if (uri.startsWith("smb://")) { record.setStatus(NDEF_URI_SMB); record.setPayload(uri.substring(6).c_str()); - record.setPayloadSize(uri.length() - 6 + statusLength); + record.setPayloadLength(uri.length() - 6 + statusLength); } else if (uri.startsWith("nfs://")) { record.setStatus(NDEF_URI_NFS); record.setPayload(uri.substring(6).c_str()); - record.setPayloadSize(uri.length() - 6 + statusLength); + record.setPayloadLength(uri.length() - 6 + statusLength); } else if (uri.startsWith("ftp://")) { record.setStatus(NDEF_URI_FTP); record.setPayload(uri.substring(6).c_str()); - record.setPayloadSize(uri.length() - 6 + statusLength); + record.setPayloadLength(uri.length() - 6 + statusLength); } else if (uri.startsWith("dav://")) { record.setStatus(NDEF_URI_DAV); record.setPayload(uri.substring(6).c_str()); - record.setPayloadSize(uri.length() - 6 + statusLength); + record.setPayloadLength(uri.length() - 6 + statusLength); } else if (uri.startsWith("news:")) { record.setStatus(NDEF_URI_NEWS); record.setPayload(uri.substring(5).c_str()); - record.setPayloadSize(uri.length() - 5 + statusLength); + record.setPayloadLength(uri.length() - 5 + statusLength); } else if (uri.startsWith("telnet://")) { record.setStatus(NDEF_URI_TELNET); record.setPayload(uri.substring(9).c_str()); - record.setPayloadSize(uri.length() - 9 + statusLength); + record.setPayloadLength(uri.length() - 9 + statusLength); } else if (uri.startsWith("imap:")) { record.setStatus(NDEF_URI_IMAP); record.setPayload(uri.substring(5).c_str()); - record.setPayloadSize(uri.length() - 5 + statusLength); + record.setPayloadLength(uri.length() - 5 + statusLength); } else if (uri.startsWith("rtsp://")) { record.setStatus(NDEF_URI_RTSP); record.setPayload(uri.substring(7).c_str()); - record.setPayloadSize(uri.length() - 7 + statusLength); + record.setPayloadLength(uri.length() - 7 + statusLength); } else if (uri.startsWith("pop:")) { record.setStatus(NDEF_URI_POP); record.setPayload(uri.substring(4).c_str()); - record.setPayloadSize(uri.length() - 4 + statusLength); + record.setPayloadLength(uri.length() - 4 + statusLength); } else if (uri.startsWith("sip:")) { record.setStatus(NDEF_URI_SIP); record.setPayload(uri.substring(4).c_str()); - record.setPayloadSize(uri.length() - 4 + statusLength); + record.setPayloadLength(uri.length() - 4 + statusLength); } else if (uri.startsWith("sips:")) { record.setStatus(NDEF_URI_SIPS); record.setPayload(uri.substring(5).c_str()); - record.setPayloadSize(uri.length() - 5 + statusLength); + record.setPayloadLength(uri.length() - 5 + statusLength); } else if (uri.startsWith("tftp:")) { record.setStatus(NDEF_URI_TFTP); record.setPayload(uri.substring(5).c_str()); - record.setPayloadSize(uri.length() - 5 + statusLength); + record.setPayloadLength(uri.length() - 5 + statusLength); } else if (uri.startsWith("btspp://")) { record.setStatus(NDEF_URI_BTSPP); record.setPayload(uri.substring(8).c_str()); - record.setPayloadSize(uri.length() - 8 + statusLength); + record.setPayloadLength(uri.length() - 8 + statusLength); } else if (uri.startsWith("btl2cap://")) { record.setStatus(NDEF_URI_BTL2CAP); record.setPayload(uri.substring(10).c_str()); - record.setPayloadSize(uri.length() - 10 + statusLength); + record.setPayloadLength(uri.length() - 10 + statusLength); } else if (uri.startsWith("btgoep://")) { record.setStatus(NDEF_URI_BTGOEP); record.setPayload(uri.substring(9).c_str()); - record.setPayloadSize(uri.length() - 9 + statusLength); + record.setPayloadLength(uri.length() - 9 + statusLength); } else if (uri.startsWith("tcpobex://")) { record.setStatus(NDEF_URI_TCPOBEX); record.setPayload(uri.substring(10).c_str()); - record.setPayloadSize(uri.length() - 10 + statusLength); + record.setPayloadLength(uri.length() - 10 + statusLength); } else if (uri.startsWith("irdaobex://")) { record.setStatus(NDEF_URI_IRDAOBEX); record.setPayload(uri.substring(11).c_str()); - record.setPayloadSize(uri.length() - 11 + statusLength); + record.setPayloadLength(uri.length() - 11 + statusLength); } else if (uri.startsWith("file://")) { record.setStatus(NDEF_URI_FILE); record.setPayload(uri.substring(7).c_str()); - record.setPayloadSize(uri.length() - 7 + statusLength); + record.setPayloadLength(uri.length() - 7 + statusLength); } else if (uri.startsWith("urn:epc:id:")) { record.setStatus(NDEF_URI_URN_EPC_ID); record.setPayload(uri.substring(11).c_str()); - record.setPayloadSize(uri.length() - 11 + statusLength); + record.setPayloadLength(uri.length() - 11 + statusLength); } else if (uri.startsWith("urn:epc:tag:")) { record.setStatus(NDEF_URI_URN_EPC_TAG); record.setPayload(uri.substring(12).c_str()); - record.setPayloadSize(uri.length() - 12 + statusLength); + record.setPayloadLength(uri.length() - 12 + statusLength); } else if (uri.startsWith("urn:epc:pat:")) { record.setStatus(NDEF_URI_URN_EPC_PAT); record.setPayload(uri.substring(12).c_str()); - record.setPayloadSize(uri.length() - 12 + statusLength); + record.setPayloadLength(uri.length() - 12 + statusLength); } else if (uri.startsWith("urn:epc:raw:")) { record.setStatus(NDEF_URI_URN_EPC_RAW); record.setPayload(uri.substring(12).c_str()); - record.setPayloadSize(uri.length() - 12 + statusLength); + record.setPayloadLength(uri.length() - 12 + statusLength); } else if (uri.startsWith("urn:epc:")) { record.setStatus(NDEF_URI_URN_EPC); record.setPayload(uri.substring(8).c_str()); - record.setPayloadSize(uri.length() - 8 + statusLength); + record.setPayloadLength(uri.length() - 8 + statusLength); } else if (uri.startsWith("urn:nfc:")) { record.setStatus(NDEF_URI_URN_NFC); record.setPayload(uri.substring(8).c_str()); - record.setPayloadSize(uri.length() - 8 + statusLength); + record.setPayloadLength(uri.length() - 8 + statusLength); } else if (uri.startsWith("urn:")) { record.setStatus(NDEF_URI_URN); record.setPayload(uri.substring(4).c_str()); - record.setPayloadSize(uri.length() - 4 + statusLength); + record.setPayloadLength(uri.length() - 4 + statusLength); } else { record.setStatus(NDEF_URI_NO_PREFIX); record.setPayload(uri); - record.setPayloadSize(uri.length() + statusLength); + record.setPayloadLength(uri.length() + statusLength); } addRecord(record); } -void NdefMessage::addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadSize) { +void NdefMessage::addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadLength) { NdefRecord record; record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD); record.setTypeLength(mimeType.length()); - record.setPayloadSize(payloadSize); + record.setPayloadLength(payloadLength); record.setRecordType(mimeType); - record.setPayload(payload, payloadSize); + record.setPayload(payload, payloadLength); addRecord(record); } @@ -436,7 +436,7 @@ void NdefMessage::addWiFiRecord(String ssid, String authenticationType, String e record.setHeaderFlags(NDEF_HEADER_FLAGS_SINGLE_MEDIA_RECORD); record.setTypeLength(mimeType.length()); - record.setPayloadSize(ssid.length() + password.length() + 29); // TODO: 29 must be calculated + record.setPayloadLength(ssid.length() + password.length() + 29); // TODO: 29 must be calculated record.setRecordType(mimeType); record.setPayload((const char *)payload, sizeof(payload)); diff --git a/src/NdefMessage.h b/src/NdefMessage.h index 86ea6e3..6415cf4 100644 --- a/src/NdefMessage.h +++ b/src/NdefMessage.h @@ -98,12 +98,12 @@ class NdefMessage { private: static uint8_t recordCounter; static unsigned char *content; - static unsigned short contentSize; + static unsigned short contentLength; static unsigned char *newContent; - static unsigned short newContentSize; - static void update(unsigned char *message, unsigned short messageSize); + static unsigned short newContentLength; + static void update(unsigned char *message, unsigned short messageLength); void getNextRecord(); - static String getHexRepresentation(const byte *data, const uint32_t dataSize); + static String getHexRepresentation(const byte *data, const uint32_t dataLength); static String newString; void addRecord(NdefRecord record); static void updateHeaderFlags(); @@ -115,8 +115,8 @@ class NdefMessage { NdefMessage(); void begin(); static unsigned char *getContent(); - static unsigned short getContentSize(); - static void setContent(const char *content, unsigned short contentSize); + static unsigned short getContentLength(); + static void setContent(const char *content, unsigned short contentLength); NdefRecord_t getRecord(); bool isEmpty(); bool isNotEmpty(); @@ -124,7 +124,7 @@ class NdefMessage { void addTextRecord(String text); void addTextRecord(String text, String languageCode); void addUriRecord(String uri); - void addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadSize); + void addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadLength); void addWiFiRecord(String ssid, String authenticationType, String encryptionType, String password); }; diff --git a/src/NdefRecord.cpp b/src/NdefRecord.cpp index cac3994..7af6994 100644 --- a/src/NdefRecord.cpp +++ b/src/NdefRecord.cpp @@ -17,7 +17,7 @@ NdefRecord::NdefRecord() { this->_type = UNSUPPORTED_NDEF_RECORD; this->headerFlags = 0; this->payload = NULL; - this->payloadSize = 0; + this->payloadLength = 0; this->typeLength = 0; this->wellKnownType = 0; this->status = 0; @@ -33,23 +33,23 @@ bool NdefRecord::isTextRecord() { void NdefRecord::create(NdefRecord_t record) { this->_type = record.recordType; this->payload = record.recordPayload; - this->payloadSize = record.recordPayloadSize; + this->payloadLength = record.recordPayloadLength; } -String NdefRecord::getHexRepresentation(const byte *data, const uint32_t dataSize) { +String NdefRecord::getHexRepresentation(const byte *data, const uint32_t dataLength) { String hexString; - if (dataSize == 0) { + if (dataLength == 0) { hexString = newString; } - for (uint32_t index = 0; index < dataSize; index++) { + for (uint32_t index = 0; index < dataLength; index++) { if (data[index] <= 0xF) hexString += "0"; String hexValue = String(data[index] & 0xFF, HEX); hexValue.toUpperCase(); hexString += hexValue; - if ((dataSize > 1) && (index != dataSize - 1)) { + if ((dataLength > 1) && (index != dataLength - 1)) { hexString += ":"; } } @@ -72,28 +72,28 @@ unsigned char *NdefRecord::getPayload() { return this->payload; } -unsigned short NdefRecord::getPayloadSize() { +unsigned short NdefRecord::getPayloadLength() { if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { - return this->payloadSize; + return this->payloadLength; } if (isTextRecord()) { - return this->payloadSize; + return this->payloadLength; } else { - return this->payloadSize + 2; + return this->payloadLength + 2; } } String NdefRecord::getText() { - unsigned char save = payload[payloadSize]; - payload[payloadSize] = '\0'; + unsigned char save = payload[payloadLength]; + payload[payloadLength] = '\0'; String text = newString; if (getType() == WELL_KNOWN_SIMPLE_TEXT) { text = reinterpret_cast(&payload[payload[0] + 1]); } - payload[payloadSize] = save; + payload[payloadLength] = save; return text; } @@ -107,7 +107,7 @@ String NdefRecord::getBluetoothName() { bluetoothName = ""; } - for (unsigned int i = 10; i < payloadSize; i++) { + for (unsigned int i = 10; i < payloadLength; i++) { if (payload[i] == 0x04) { break; } @@ -143,7 +143,7 @@ String NdefRecord::getWiFiSSID() { return ssid; } - for (unsigned int i = 0; i < payloadSize; i++) { + for (unsigned int i = 0; i < payloadLength; i++) { if (payload[i] == 0x45) { ssid = reinterpret_cast(&payload[i + 3]); break; @@ -165,7 +165,7 @@ String NdefRecord::getWiFiAuthenticationType() { index += 4; } - while (index < getPayloadSize()) { + while (index < getPayloadLength()) { if (getPayload()[index] == 0x10) { if (getPayload()[index + 1] == 0x03) { authenticationType = ndef_helper_WifiAuth(getPayload()[index + 5]); @@ -189,7 +189,7 @@ String NdefRecord::getWiFiEncryptionType() { index += 4; } - while (index < getPayloadSize()) { + while (index < getPayloadLength()) { if (getPayload()[index] == 0x10) { if (getPayload()[index + 1] == 0x0f) { encryptionType = ndef_helper_WifiEnc(getPayload()[index + 5]); @@ -213,7 +213,7 @@ String NdefRecord::getWiFiPassword() { index += 4; } - while (index < getPayloadSize()) { + while (index < getPayloadLength()) { if (getPayload()[index] == 0x10) { if (getPayload()[index + 1] == 0x27) { networkKey = reinterpret_cast(&getPayload()[index + 4]); @@ -244,10 +244,10 @@ String NdefRecord::getUri() { return uri; } - unsigned char save = payload[payloadSize]; - payload[payloadSize] = '\0'; + unsigned char save = payload[payloadLength]; + payload[payloadLength] = '\0'; uri = reinterpret_cast(ndef_helper_UriHead(payload[0]), &payload[1]); - payload[payloadSize] = save; + payload[payloadLength] = save; return uri; } @@ -270,7 +270,7 @@ void NdefRecord::setPayload(String payload) { void NdefRecord::setPayload(const char *payload, unsigned short payloadLength) { this->payload = (unsigned char *)payload; #ifdef DEBUG3 - Serial.println("Payload: '" + getHexRepresentation(this->payload, payloadSize) + "'"); + Serial.println("Payload: '" + getHexRepresentation(this->payload, payloadLength) + "'"); #endif } @@ -301,16 +301,16 @@ void NdefRecord::setLanguageCode(String languageCode) { strcpy((char *)this->languageCode, languageCode.c_str()); } -void NdefRecord::setPayloadSize(uint8_t payloadSize) { - this->payloadSize = payloadSize; +void NdefRecord::setPayloadLength(uint8_t payloadLength) { + this->payloadLength = payloadLength; } const char *NdefRecord::getWellKnownContent() { - char *recordContent = new char[getPayloadSize()]; + char *recordContent = new char[getPayloadLength()]; recordContent[0] = headerFlags; recordContent[1] = typeLength; - recordContent[2] = payloadSize; + recordContent[2] = payloadLength; recordContent[3] = wellKnownType; recordContent[4] = status; @@ -318,11 +318,11 @@ const char *NdefRecord::getWellKnownContent() { recordContent[5] = languageCode[0]; recordContent[6] = languageCode[1]; - for (int i = 0; i < getPayloadSize(); i++) { + for (int i = 0; i < getPayloadLength(); i++) { recordContent[i + 7] = payload[i]; } } else { - for (int i = 0; i < getPayloadSize(); i++) { + for (int i = 0; i < getPayloadLength(); i++) { recordContent[i + 5] = payload[i]; } } @@ -331,17 +331,17 @@ const char *NdefRecord::getWellKnownContent() { } const char *NdefRecord::getMimeMediaContent() { - char *recordContent = new char[getPayloadSize()]; + char *recordContent = new char[getPayloadLength()]; recordContent[0] = headerFlags; recordContent[1] = typeLength; - recordContent[2] = payloadSize; + recordContent[2] = payloadLength; for (int i = 0; i < typeLength; i++) { recordContent[i + 3] = mimeMediaType[i]; } - for (int i = 0; i < getPayloadSize(); i++) { + for (int i = 0; i < getPayloadLength(); i++) { recordContent[i + 3 + typeLength] = payload[i]; } @@ -350,7 +350,7 @@ const char *NdefRecord::getMimeMediaContent() { const char *NdefRecord::getContent() { #ifdef DEBUG3 - Serial.println("Payload size: " + String(getPayloadSize())); + Serial.println("Payload length: " + String(getPayloadLength())); #endif // Search in the last 3 bits of headerFlags @@ -372,14 +372,14 @@ const char *NdefRecord::getContent() { } } -unsigned short NdefRecord::getContentSize() { +unsigned short NdefRecord::getContentLength() { if ((headerFlags & NDEF_RECORD_TNF_MASK) == NDEF_MEDIA) { - return getPayloadSize() + typeLength + 3; // 3 bytes for header, type length and payload length + return getPayloadLength() + typeLength + 3; // 3 bytes for header, type length and payload length } if (isTextRecord()) { - return getPayloadSize() + 4; // 4 bytes for header, type length, payload length and record type + return getPayloadLength() + 4; // 4 bytes for header, type length, payload length and record type } else { - return getPayloadSize() + 2; // 2 bytes for header and payload length + return getPayloadLength() + 2; // 2 bytes for header and payload length } } diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 6b70514..41ebddb 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -30,7 +30,7 @@ class NdefRecord { NdefRecordType_e _type; uint8_t headerFlags; uint8_t typeLength; - unsigned short payloadSize; + unsigned short payloadLength; uint8_t wellKnownType; unsigned char *mimeMediaType; uint8_t status; @@ -38,7 +38,7 @@ class NdefRecord { unsigned char *payload; String newString; bool textRecord; - String getHexRepresentation(const byte *data, const uint32_t dataSize); + String getHexRepresentation(const byte *data, const uint32_t dataLength); bool isTextRecord(); const char *getWellKnownContent(); const char *getMimeMediaContent(); @@ -51,7 +51,7 @@ class NdefRecord { bool isNotEmpty(); NdefRecordType_e getType(); unsigned char *getPayload(); - unsigned short getPayloadSize(); + unsigned short getPayloadLength(); String getText(); String getBluetoothName(); String getBluetoothAddress(); @@ -69,9 +69,9 @@ class NdefRecord { void setRecordType(String type); void setStatus(uint8_t status); void setLanguageCode(String languageCode); - void setPayloadSize(uint8_t payloadSize); + void setPayloadLength(uint8_t payloadLength); const char *getContent(); - unsigned short getContentSize(); + unsigned short getContentLength(); }; #endif \ No newline at end of file diff --git a/src/T4T_NDEF_emu.cpp b/src/T4T_NDEF_emu.cpp index 0ad59c6..b0d25e3 100644 --- a/src/T4T_NDEF_emu.cpp +++ b/src/T4T_NDEF_emu.cpp @@ -28,7 +28,7 @@ const unsigned char T4T_NDEF_EMU_OK[] = {0x90, 0x00}; const unsigned char T4T_NDEF_EMU_NOK[] = {0x6A, 0x82}; unsigned char *pT4T_NdefMessage; -unsigned short T4T_NdefMessage_size = 0; +unsigned short T4T_NdefMessage_length = 0; unsigned char T4T_NdefMessageWritten[256]; @@ -49,12 +49,12 @@ CustomCallback_t *ndefSendCallback; 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); + pRsp[0] = (T4T_NdefMessage_length & 0xFF00) >> 8; + pRsp[1] = (T4T_NdefMessage_length & 0x00FF); if (length > 2) memcpy(&pRsp[2], &pT4T_NdefMessage[0], length - 2); } else if (offset == 1) { - pRsp[0] = (T4T_NdefMessage_size & 0x00FF); + pRsp[0] = (T4T_NdefMessage_length & 0x00FF); if (length > 1) memcpy(&pRsp[1], &pT4T_NdefMessage[0], length - 1); } else { @@ -62,10 +62,10 @@ static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, uns } /* Did we reached the end of NDEF message ?*/ - if ((offset + length) >= (T4T_NdefMessage_size + 2)) { + if ((offset + length) >= (T4T_NdefMessage_length + 2)) { /* Notify application of the NDEF send */ if (pT4T_NDEF_EMU_PushCb != NULL) - pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_size); + pT4T_NDEF_EMU_PushCb(pT4T_NdefMessage, T4T_NdefMessage_length); // Notify custom callback if (ndefSendCallback != NULL) @@ -73,17 +73,17 @@ static void T4T_NDEF_EMU_FillRsp(unsigned char *pRsp, unsigned short offset, uns } } -bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb) { +bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short messageLength, void *pCb) { pT4T_NdefMessage = pMessage; - T4T_NdefMessage_size = Message_size; + T4T_NdefMessage_length = messageLength; pT4T_NDEF_EMU_PushCb = (T4T_NDEF_EMU_Callback_t *)pCb; return true; } -void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short Message_size) { +void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short messageLength) { pT4T_NdefMessage = (unsigned char *)pMessage; - T4T_NdefMessage_size = Message_size; + T4T_NdefMessage_length = messageLength; } void T4T_NDEF_EMU_SetCallback(CustomCallback_t function) { @@ -125,7 +125,7 @@ void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned ch unsigned short offset = (pCmd[2] << 8) + pCmd[3]; unsigned char length = pCmd[4]; - if (length <= (T4T_NdefMessage_size + offset + 2)) { + if (length <= (T4T_NdefMessage_length + offset + 2)) { T4T_NDEF_EMU_FillRsp(pRsp, offset, length); *pRsp_size = length; eStatus = true; @@ -138,7 +138,7 @@ void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned ch 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]; + T4T_NdefMessage_length = (pCmd[5] << 8) + pCmd[6]; *pRsp_size = 0; eStatus = true; } diff --git a/src/T4T_NDEF_emu.h b/src/T4T_NDEF_emu.h index d8a8b96..e8c614a 100644 --- a/src/T4T_NDEF_emu.h +++ b/src/T4T_NDEF_emu.h @@ -17,7 +17,7 @@ typedef void CustomCallback_t(void); void T4T_NDEF_EMU_Reset(void); -bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short Message_size, void *pCb); -void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short Message_size); +bool T4T_NDEF_EMU_SetMessage(unsigned char *pMessage, unsigned short messageLength, void *pCb); +void T4T_NDEF_EMU_SetMsg(const char *pMessage, unsigned short messageLength); void T4T_NDEF_EMU_SetCallback(CustomCallback_t function); void T4T_NDEF_EMU_Next(unsigned char *pCmd, unsigned short Cmd_size, unsigned char *Rsp, unsigned short *pRsp_size); diff --git a/src/ndef_helper.cpp b/src/ndef_helper.cpp index 2c55d53..8b1951a 100644 --- a/src/ndef_helper.cpp +++ b/src/ndef_helper.cpp @@ -81,7 +81,7 @@ NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord) { if (pNdefRecord == NULL) { record.recordType = UNSUPPORTED_NDEF_RECORD; record.recordPayload = NULL; - record.recordPayloadSize = 0; + record.recordPayloadLength = 0; return record; } @@ -89,10 +89,10 @@ NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord) { /* Short or normal record ?*/ if (pNdefRecord[0] & NDEF_RECORD_SR_MASK) { - record.recordPayloadSize = pNdefRecord[2]; + record.recordPayloadLength = pNdefRecord[2]; typeField = 3; } else { - record.recordPayloadSize = (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5]; + record.recordPayloadLength = (pNdefRecord[2] << 24) + (pNdefRecord[3] << 16) + (pNdefRecord[4] << 8) + pNdefRecord[5]; typeField = 6; } diff --git a/src/ndef_helper.h b/src/ndef_helper.h index 9342d1f..06e9377 100644 --- a/src/ndef_helper.h +++ b/src/ndef_helper.h @@ -50,7 +50,7 @@ typedef enum { typedef struct { NdefRecordType_e recordType; unsigned char *recordPayload; - unsigned int recordPayloadSize; + unsigned int recordPayloadLength; } NdefRecord_t; const char *ndef_helper_WifiAuth(unsigned char auth); From 4385413182e20db1dba10a78b047eb1b9dcc5f74 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 29 Dec 2023 10:52:13 -0600 Subject: [PATCH 27/30] feat: update keywords and library version --- keywords.txt | 24 ++++++++++++++++++++---- library.properties | 2 +- src/NdefRecord.h | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/keywords.txt b/keywords.txt index 15fce2c..2169532 100644 --- a/keywords.txt +++ b/keywords.txt @@ -92,22 +92,29 @@ getMode KEYWORD2 ## NdefMessage.h ####################################### +begin KEYWORD2 getContent KEYWORD2 -getContentSize KEYWORD2 +getContentLength KEYWORD2 setContent KEYWORD2 getRecord KEYWORD2 isEmpty KEYWORD2 isNotEmpty KEYWORD2 hasRecord KEYWORD2 +addTextRecord KEYWORD2 +addUriRecord KEYWORD2 +addMimeMediaRecord KEYWORD2 +addWiFiRecord KEYWORD2 ####################################### ## NdefRecord.h ####################################### create KEYWORD2 +isEmpty KEYWORD2 +isNotEmpty KEYWORD2 getType KEYWORD2 getPayload KEYWORD2 -getPayloadSize KEYWORD2 +getPayloadLength KEYWORD2 getText KEYWORD2 getBluetoothName KEYWORD2 getBluetoothAddress KEYWORD2 @@ -117,9 +124,18 @@ getWiFiEncryptionType KEYWORD2 getWiFiNetworkKey KEYWORD2 getVCardContent KEYWORD2 getUri KEYWORD2 +setPayload KEYWORD2 +setPayloadLength KEYWORD2 +setHeaderFlags KEYWORD2 +setTypeLength KEYWORD2 +setRecordType KEYWORD2 +setStatus KEYWORD2 +setLanguageCode KEYWORD2 +getContent KEYWORD2 +getContentLength KEYWORD2 ####################################### -## NdefRecord.h +## RemoteDevice.h ####################################### getInterface KEYWORD2 @@ -245,4 +261,4 @@ UNSUPPORTED_NDEF_RECORD LITERAL1 recordType LITERAL1 recordPayload LITERAL1 -recordPayloadSize LITERAL1 \ No newline at end of file +recordPayloadLength LITERAL1 \ No newline at end of file diff --git a/library.properties b/library.properties index 2d4e0d2..4a0aa78 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Electronic Cats PN7150 -version=2.0.1 +version=2.1.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/NdefRecord.h b/src/NdefRecord.h index 41ebddb..31b28dd 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -63,13 +63,13 @@ class NdefRecord { String getUri(); void setPayload(String payload); void setPayload(const char *payload, unsigned short payloadLength); + void setPayloadLength(uint8_t payloadLength); void setHeaderFlags(uint8_t headerFlags); void setTypeLength(uint8_t typeLength); void setRecordType(uint8_t wellKnownType); void setRecordType(String type); void setStatus(uint8_t status); void setLanguageCode(String languageCode); - void setPayloadLength(uint8_t payloadLength); const char *getContent(); unsigned short getContentLength(); }; From 211f66304a495342e6ecd723c00d0a541f18e7a9 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 29 Dec 2023 11:51:49 -0600 Subject: [PATCH 28/30] docs(api): update ndef message methods --- API.md | 135 +++++++++++++++++-- examples/NDEFSendMessage/NDEFSendMessage.ino | 5 +- 2 files changed, 127 insertions(+), 13 deletions(-) diff --git a/API.md b/API.md index 7d86586..42db521 100644 --- a/API.md +++ b/API.md @@ -10,7 +10,6 @@ Include and instantiate the Electroniccats_PN7150 class. Creates a global NFC de #include Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); - ``` - `uint8_t PN7150_IRQ`: IRQ pin for data interrupt. @@ -1255,12 +1254,12 @@ void setup() { } ``` -### Method: `getContentSize` +### Method: `getContentLength` -Get the content size of the message. +Get the content length of the message. ```cpp -static unsigned short getContentSize(); +static unsigned short getContentLength(); ``` ### Method: `getContent` @@ -1276,7 +1275,7 @@ static unsigned char *getContent(); Set the content of the message. ```cpp -static void setContent(unsigned char *content, unsigned short contentSize); +static void setContent(unsigned char *content, unsigned short contentLength); ``` ### Method: `getRecord` @@ -1293,7 +1292,7 @@ The record is a structure that contains the following properties: typedef struct { NdefRecordType_e recordType; unsigned char *recordPayload; - unsigned int recordPayloadSize; + unsigned int recordPayloadLength; } NdefRecord_t; ``` @@ -1351,6 +1350,75 @@ if (message.hasRecord()) { } ``` +### Method: `addTextRecord` + +Adds a text record to the message. + +```cpp +void addTextRecord(String text); +``` + +#### Example 1 + +```cpp +message.addTextRecord("Hello"); // English by default +``` + +#### Example 2 + +```cpp +// English explicitly, the library only supports two letter language codes (ISO 639-1) by now +message.addTextRecord("world", "en"); +``` + +### Method: `addUriRecord` + +Adds a URI record to the message. + +```cpp +void addUriRecord(String uri); +``` + +#### Example + +```cpp +message.addUriRecord("https://www.electroniccats.com/"); +``` + +See [URI prefixes](#uri-prefixes) for a list of all the prefixes that can be used. + +### Method: `addMimeMediaRecord` + +Adds a MIME media record to the message. + +```cpp +void addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadLength); +``` + +#### Example + +```cpp +message.addMimeMediaRecord("text/plain", "Hello world!", 12); +``` + +### Method: `addWiFiRecord` + +Adds a WiFi record to the message. + +```cpp +void addWiFiRecord(String ssid, String authenticationType, String encryptionType, String password); +``` + +#### Example + +```cpp +String ssid = "Bomber Cat"; +String authentificationType = "WPA2 PERSONAL"; +String encryptionType = "AES"; +String password = "Password"; +message.addWiFiRecord(ssid, authentificationType, encryptionType, password); +``` + ## 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. @@ -1436,12 +1504,12 @@ typedef enum { } NdefRecordType_e; ``` -### Method: `getPayloadSize` +### Method: `getPayloadLength` -Get the payload size of the record. +Get the payload length of the record. ```cpp -unsigned short getPayloadSize(); +unsigned short getPayloadLength(); ``` ### Method: `getPayload` @@ -1456,7 +1524,7 @@ unsigned char *getPayload(); ```cpp Serial.print("Payload: "); -for (int i = 0; i < record.getPayloadSize(); i++) { +for (int i = 0; i < record.getPayloadLength(); i++) { Serial.print(record.getPayload()[i], HEX); Serial.print(" "); } @@ -1596,4 +1664,49 @@ String getUri(); ```cpp Serial.print("URI: "); Serial.println(record.getUri()); -``` \ No newline at end of file +``` + +## Appendix + +### URI prefixes + +Here is a list of all the prefixes that can be used with the [`addUriRecord`](#method-addurirecord) method. + +| Prefix | Meaning | +| --- | --- | +| `0x00` | No prepending is done, the URI is encoded in its entirety in the record payload | +| `0x01` | `http://www.` | +| `0x02` | `https://www.` | +| `0x03` | `http://` | +| `0x04` | `https://` | +| `0x05` | `tel:` | +| `0x06` | `mailto:` | +| `0x07` | `ftp://anonymous:anonymous@` | +| `0x08` | `ftp://ftp.` | +| `0x09` | `ftps://` | +| `0x0A` | `sftp://` | +| `0x0B` | `smb://` | +| `0x0C` | `nfs://` | +| `0x0D` | `ftp://` | +| `0x0E` | `dav://` | +| `0x0F` | `news:` | +| `0x10` | `telnet://` | +| `0x11` | `imap:` | +| `0x12` | `rtsp:` | +| `0x13` | `urn:` | +| `0x14` | `pop:` | +| `0x15` | `sip:` | +| `0x16` | `sips:` | +| `0x17` | `tftp:` | +| `0x18` | `btspp://` | +| `0x19` | `btl2cap://` | +| `0x1A` | `btgoep://` | +| `0x1B` | `tcpobex://` | +| `0x1C` | `irdaobex://` | +| `0x1D` | `file://` | +| `0x1E` | `urn:epc:id:` | +| `0x1F` | `urn:epc:tag:` | +| `0x20` | `urn:epc:pat:` | +| `0x21` | `urn:epc:raw:` | +| `0x22` | `urn:epc:` | +| `0x23` | `urn:nfc:` | diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 91f9466..0e42d82 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -37,14 +37,15 @@ void setup() { message.addTextRecord("Hola mundo!", "es"); // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly message.addUriRecord("google.com"); // No prefix explicitly - message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly, the library can handle all the prefixes listed at TODO: add link to prefixes table - message.addUriRecord("tel:1234567890"); + message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly + message.addUriRecord("tel:1234567890"); // The library can handle all the prefixes listed at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/refactor/API.md##uri-prefixes String ssid = "Bomber Cat"; String authentificationType = "WPA2 PERSONAL"; String encryptionType = "AES"; String password = "Password"; message.addWiFiRecord(ssid, authentificationType, encryptionType, password); message.addUriRecord("mailto:deimoshall@gmail.com"); + message.addMimeMediaRecord("text/plain", "Hello world!", 12); nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); From 54d1fafeba6391295ba6e7888c84685059e66b10 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 29 Dec 2023 13:25:53 -0600 Subject: [PATCH 29/30] docs(api): update ndef record methods --- API.md | 179 ++++++++++++++++++- examples/NDEFSendMessage/NDEFSendMessage.ino | 12 +- src/NdefRecord.h | 2 +- 3 files changed, 185 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index 42db521..b43c930 100644 --- a/API.md +++ b/API.md @@ -1356,6 +1356,7 @@ Adds a text record to the message. ```cpp void addTextRecord(String text); +void addTextRecord(String text, String languageCode); ``` #### Example 1 @@ -1398,7 +1399,7 @@ void addMimeMediaRecord(String mimeType, const char *payload, unsigned short pay #### Example ```cpp -message.addMimeMediaRecord("text/plain", "Hello world!", 12); +message.addMimeMediaRecord("text/plain", "Hello world!", 12); // 12 is the payload length ``` ### Method: `addWiFiRecord` @@ -1419,6 +1420,8 @@ String password = "Password"; message.addWiFiRecord(ssid, authentificationType, encryptionType, password); ``` +See [WiFi authentication types](#wifi-authentication-types) and [WiFi encryption types](#wifi-encryption-types) for a list of all the types that can be used. + ## 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. @@ -1666,6 +1669,152 @@ Serial.print("URI: "); Serial.println(record.getUri()); ``` +### Method: `setPayload` + +Set the payload of the record. + +```cpp +void setPayload(String payload); +void setPayload(const char *payload, unsigned short payloadLength); +``` + +#### Example 1 + +```cpp +record.setPayload("Hello world!"); +``` + +#### Example 2 + +```cpp +record.setPayload("Hello world!", 12); +``` + +### Method: `setPayloadLength` + +Set the payload length of the record. + +```cpp +void setPayloadLength(uint8_t payloadLength); +``` + +#### Example + +```cpp +record.setPayloadLength(12); +``` + +### Method: `setHeaderFlags` + +Set the header flags of the record. + +```cpp +void setHeaderFlags(uint8_t headerFlags); +``` + +#### Example + +```cpp +record.setHeaderFlags(0x00); +``` + +### Method: `setTypeLength` + +Set the type length of the record. + +```cpp +void setTypeLength(uint8_t typeLength); +``` + +#### Example + +```cpp +record.setTypeLength(0x00); +``` + +### Method: `setRecordType` + +Set the record type of the record. + +```cpp +void setRecordType(uint8_t wellKnownType); +void setRecordType(String type); +``` + +#### Example 1 + +```cpp +record.setRecordType(NDEF_TEXT_RECORD_TYPE); +``` + +#### Example 2 + +```cpp +record.setRecordType("application/vnd.wfa.wsc"); +``` + +### Method: `setStatus` + +Set the status of the record. + +```cpp +void setStatus(uint8_t status); +``` + +#### Example + +```cpp +record.setStatus(NDEF_STATUS); +``` + +### Method: `setLanguageCode` + +Set the language code of the record. + +```cpp +void setLanguageCode(String languageCode); +``` + +#### Example + +```cpp +record.setLanguageCode("en"); +``` + +### Method: `getContentLength` + +Get the content length of the record. + +```cpp +unsigned short getContentLength(); +``` + +#### Example + +```cpp +Serial.print("Content length: "); +Serial.println(record.getContentLength()); +``` + +### Method: `getContent` + +Get the content of the record. + +```cpp +const char *getContent(); +``` + +#### Example + +```cpp +Serial.print("Content: "); +for (int i = 0; i < record.getContentLength(); i++) { + Serial.print(record.getContent()[i], HEX); + Serial.print(" "); +} +Serial.println(); +``` + ## Appendix ### URI prefixes @@ -1710,3 +1859,31 @@ Here is a list of all the prefixes that can be used with the [`addUriRecord`](#m | `0x21` | `urn:epc:raw:` | | `0x22` | `urn:epc:` | | `0x23` | `urn:nfc:` | + +### WiFi authentication types + +Here is a list of all the authentication types that can be used with the [`addWiFiRecord`](#method-addwifirecord) method. + +| Authentication type | Meaning | +| --- | --- | +| `"OPEN"` | Open | +| `"WPA PERSONAL"` | WPA Personal | +| `"SHARED"` | Shared | +| `"WPA ENTERPRISE"` | WPA Enterprise | +| `"WPA2 ENTERPRISE"` | WPA2 Enterprise | +| `"WPA2 PERSONAL"` | WPA2 Personal | + +Any other value will be interpreted as `UNKNOWN`. + +### WiFi encryption types + +Here is a list of all the encryption types that can be used with the [`addWiFiRecord`](#method-addwifirecord) method. + +| Encryption type | Meaning | +| --- | --- | +| `"NONE"` | None | +| `"WEP"` | WEP | +| `"TKIP"` | TKIP | +| `"AES"` | AES | + +Any other value will be interpreted as `UNKNOWN`. diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index 0e42d82..b69f470 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -38,14 +38,14 @@ void setup() { message.addTextRecord("Bonjour le monde!", "fr"); // French explicitly message.addUriRecord("google.com"); // No prefix explicitly message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly - message.addUriRecord("tel:1234567890"); // The library can handle all the prefixes listed at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/refactor/API.md##uri-prefixes - String ssid = "Bomber Cat"; - String authentificationType = "WPA2 PERSONAL"; - String encryptionType = "AES"; - String password = "Password"; + message.addUriRecord("tel:1234567890"); // The library can handle all the prefixes listed at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#uri-prefixes + String ssid = "Bomber Cat"; // SSID of the WiFi network + String authentificationType = "WPA2 PERSONAL"; // Supported authentification types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-authentication-types + String encryptionType = "AES"; // Supported encryption types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-encryption-types + String password = "Password"; // Password of the WiFi network message.addWiFiRecord(ssid, authentificationType, encryptionType, password); message.addUriRecord("mailto:deimoshall@gmail.com"); - message.addMimeMediaRecord("text/plain", "Hello world!", 12); + message.addMimeMediaRecord("text/plain", "Hello world!", 12); // Media-type as defined in RFC 2046 nfc.setSendMsgCallback(messageSentCallback); Serial.println("Initializing..."); diff --git a/src/NdefRecord.h b/src/NdefRecord.h index 31b28dd..79de02f 100644 --- a/src/NdefRecord.h +++ b/src/NdefRecord.h @@ -23,7 +23,7 @@ */ // #define DEBUG // #define DEBUG2 -#define DEBUG3 +// #define DEBUG3 class NdefRecord { private: From 1df0e56ffb22b1074e32ea3194ca5e3666aeccd6 Mon Sep 17 00:00:00 2001 From: deimos Date: Fri, 29 Dec 2023 14:20:51 -0600 Subject: [PATCH 30/30] fix: typo --- examples/NDEFSendMessage/NDEFSendMessage.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/NDEFSendMessage/NDEFSendMessage.ino b/examples/NDEFSendMessage/NDEFSendMessage.ino index b69f470..55ddba5 100644 --- a/examples/NDEFSendMessage/NDEFSendMessage.ino +++ b/examples/NDEFSendMessage/NDEFSendMessage.ino @@ -40,10 +40,10 @@ void setup() { message.addUriRecord("https://www.electroniccats.com"); // https://www. prefix explicitly message.addUriRecord("tel:1234567890"); // The library can handle all the prefixes listed at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#uri-prefixes String ssid = "Bomber Cat"; // SSID of the WiFi network - String authentificationType = "WPA2 PERSONAL"; // Supported authentification types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-authentication-types + String authenticationType = "WPA2 PERSONAL"; // Supported authentication types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-authentication-types String encryptionType = "AES"; // Supported encryption types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-encryption-types String password = "Password"; // Password of the WiFi network - message.addWiFiRecord(ssid, authentificationType, encryptionType, password); + message.addWiFiRecord(ssid, authenticationType, encryptionType, password); message.addUriRecord("mailto:deimoshall@gmail.com"); message.addMimeMediaRecord("text/plain", "Hello world!", 12); // Media-type as defined in RFC 2046 nfc.setSendMsgCallback(messageSentCallback);