From 9b05cb147ffd9b8f12c1d00a52a1d66c16515506 Mon Sep 17 00:00:00 2001 From: deimos Date: Thu, 28 Dec 2023 20:37:44 -0600 Subject: [PATCH] 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: