Skip to content

Commit

Permalink
feat: add wifi record method
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Dec 29, 2023
1 parent 9b49bd0 commit 9b05cb1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 36 deletions.
40 changes: 5 additions & 35 deletions examples/NDEFSendMessage/NDEFSendMessage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down
91 changes: 91 additions & 0 deletions src/NdefMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/NdefMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/NdefRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
// #define DEBUG
// #define DEBUG2
// #define DEBUG3
#define DEBUG3

class NdefRecord {
private:
Expand Down

0 comments on commit 9b05cb1

Please sign in to comment.