Skip to content

Commit

Permalink
feat: add record class
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Aug 23, 2023
1 parent 48525c7 commit ec99d67
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 103 deletions.
207 changes: 106 additions & 101 deletions examples/NDEFRead/NDEFRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -161,115 +161,115 @@ void displayDeviceInfo() {
}
}

void displayRecordInfo(NdefRecord_t record) {
void displayRecordInfo(NdefRecord record) {
unsigned char save;
String SSID;
String bluetoothName;
String bluetoothAddress;
Serial.println("--- NDEF record received:");

switch (record.recordType) {
case MEDIA_VCARD: {
save = record.recordPayload[record.recordPayloadSize];
record.recordPayload[record.recordPayloadSize] = '\0';
Serial.print("vCard:");
Serial.println(reinterpret_cast<const char *>(record.recordPayload));
record.recordPayload[record.recordPayloadSize] = save;
} break;
switch (record.getType()) {
// case MEDIA_VCARD: {
// save = record.recordPayload[record.recordPayloadSize];
// record.recordPayload[record.recordPayloadSize] = '\0';
// Serial.print("vCard:");
// Serial.println(reinterpret_cast<const char *>(record.recordPayload));
// record.recordPayload[record.recordPayloadSize] = save;
// } break;

case WELL_KNOWN_SIMPLE_TEXT: {
save = record.recordPayload[record.recordPayloadSize];
record.recordPayload[record.recordPayloadSize] = '\0';
Serial.print("Text record: ");
Serial.println(reinterpret_cast<const char *>(&record.recordPayload[record.recordPayload[0] + 1]));
record.recordPayload[record.recordPayloadSize] = save;
} break;

case WELL_KNOWN_SIMPLE_URI: {
save = record.recordPayload[record.recordPayloadSize];
record.recordPayload[record.recordPayloadSize] = '\0';
Serial.print("URI record: ");
Serial.println(reinterpret_cast<const char *>(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1]));
record.recordPayload[record.recordPayloadSize] = save;
// save = record.recordPayload[record.recordPayloadSize];
// record.recordPayload[record.recordPayloadSize] = '\0';
Serial.print("Text record: " + record.getText());
// Serial.println(reinterpret_cast<const char *>(&record.recordPayload[record.recordPayload[0] + 1]));
// record.recordPayload[record.recordPayloadSize] = save;
} break;

case MEDIA_HANDOVER_WIFI: {
unsigned char index = 0, i;

Serial.println("--- Received WIFI credentials:");
if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e))
index += 4;
while (index < record.recordPayloadSize) {
if (record.recordPayload[index] == 0x10) {
if (record.recordPayload[index + 1] == 0x45) {
Serial.print("- SSID = ");
Serial.println(reinterpret_cast<const char *>(&record.recordPayload[index + 4 + 0]));
Serial.println(SSID);
} else if (record.recordPayload[index + 1] == 0x03) {
Serial.print("- Authenticate Type = ");
Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5]));
} else if (record.recordPayload[index + 1] == 0x0f) {
Serial.print("- Encryption Type = ");
Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5]));
} else if (record.recordPayload[index + 1] == 0x27) {
Serial.print("- Network key = ");
Serial.println(reinterpret_cast<const char *>(&record.recordPayload[index + 4]));
Serial.print("- Network key = ");
Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3]));
}
index += 4 + record.recordPayload[index + 3];
} else
continue;
}
} break;

case WELL_KNOWN_HANDOVER_SELECT:
Serial.print("Handover select version ");
Serial.print(record.recordPayload[0] >> 4);
Serial.println(record.recordPayload[0] & 0xF);
break;

case WELL_KNOWN_HANDOVER_REQUEST:
Serial.print("Handover request version ");
Serial.print(record.recordPayload[0] >> 4);
Serial.println(record.recordPayload[0] & 0xF);
break;

case MEDIA_HANDOVER_BT:
Serial.print("- Payload size: ");
Serial.println(record.recordPayloadSize);
Serial.print("- Bluetooth Handover payload = ");
Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize));
Serial.print("- Bluetooth name: '");
bluetoothName = "";
for (unsigned int i = 10; i < record.recordPayloadSize; i++) {
if (record.recordPayload[i] == 0x04) {
break;
}
bluetoothName += (char)record.recordPayload[i];
}
Serial.println(bluetoothName + "'");

Serial.print("- Bluetooth address: '");
bluetoothAddress = "";
for (unsigned int i = 7; i >= 2; i--) {
bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1);
if (i > 2) {
bluetoothAddress += ":";
}
}
Serial.println(bluetoothAddress + "'");
break;

case MEDIA_HANDOVER_BLE:
Serial.print("- BLE Handover payload = ");
Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize));
break;

case MEDIA_HANDOVER_BLE_SECURE:
Serial.print("- BLE secure Handover payload = ");
Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize));
break;
// case WELL_KNOWN_SIMPLE_URI: {
// save = record.recordPayload[record.recordPayloadSize];
// record.recordPayload[record.recordPayloadSize] = '\0';
// Serial.print("URI record: ");
// Serial.println(reinterpret_cast<const char *>(ndef_helper_UriHead(record.recordPayload[0]), &record.recordPayload[1]));
// record.recordPayload[record.recordPayloadSize] = save;
// } break;

// case MEDIA_HANDOVER_WIFI: {
// unsigned char index = 0, i;

// Serial.println("--- Received WIFI credentials:");
// if ((record.recordPayload[index] == 0x10) && (record.recordPayload[index + 1] == 0x0e))
// index += 4;
// while (index < record.recordPayloadSize) {
// if (record.recordPayload[index] == 0x10) {
// if (record.recordPayload[index + 1] == 0x45) {
// Serial.print("- SSID = ");
// Serial.println(reinterpret_cast<const char *>(&record.recordPayload[index + 4 + 0]));
// Serial.println(SSID);
// } else if (record.recordPayload[index + 1] == 0x03) {
// Serial.print("- Authenticate Type = ");
// Serial.println(ndef_helper_WifiAuth(record.recordPayload[index + 5]));
// } else if (record.recordPayload[index + 1] == 0x0f) {
// Serial.print("- Encryption Type = ");
// Serial.println(ndef_helper_WifiEnc(record.recordPayload[index + 5]));
// } else if (record.recordPayload[index + 1] == 0x27) {
// Serial.print("- Network key = ");
// Serial.println(reinterpret_cast<const char *>(&record.recordPayload[index + 4]));
// Serial.print("- Network key = ");
// Serial.println(getHexRepresentation(&record.recordPayload[index + 4], record.recordPayload[index + 3]));
// }
// index += 4 + record.recordPayload[index + 3];
// } else
// continue;
// }
// } break;

// case WELL_KNOWN_HANDOVER_SELECT:
// Serial.print("Handover select version ");
// Serial.print(record.recordPayload[0] >> 4);
// Serial.println(record.recordPayload[0] & 0xF);
// break;

// case WELL_KNOWN_HANDOVER_REQUEST:
// Serial.print("Handover request version ");
// Serial.print(record.recordPayload[0] >> 4);
// Serial.println(record.recordPayload[0] & 0xF);
// break;

// case MEDIA_HANDOVER_BT:
// Serial.print("- Payload size: ");
// Serial.println(record.recordPayloadSize);
// Serial.print("- Bluetooth Handover payload = ");
// Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize));
// Serial.print("- Bluetooth name: '");
// bluetoothName = "";
// for (unsigned int i = 10; i < record.recordPayloadSize; i++) {
// if (record.recordPayload[i] == 0x04) {
// break;
// }
// bluetoothName += (char)record.recordPayload[i];
// }
// Serial.println(bluetoothName + "'");

// Serial.print("- Bluetooth address: '");
// bluetoothAddress = "";
// for (unsigned int i = 7; i >= 2; i--) {
// bluetoothAddress += getHexRepresentation(&record.recordPayload[i], 1);
// if (i > 2) {
// bluetoothAddress += ":";
// }
// }
// Serial.println(bluetoothAddress + "'");
// break;

// case MEDIA_HANDOVER_BLE:
// Serial.print("- BLE Handover payload = ");
// Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize));
// break;

// case MEDIA_HANDOVER_BLE_SECURE:
// Serial.print("- BLE secure Handover payload = ");
// Serial.println(getHexRepresentation(record.recordPayload, record.recordPayloadSize));
// break;

default:
Serial.println("Unsupported NDEF record, cannot parse");
Expand All @@ -279,10 +279,14 @@ void displayRecordInfo(NdefRecord_t record) {
Serial.println("");
}

/// @brief Callback function called when an NDEF message is received
void ndefCallback() {
NdefRecord_t record;
NdefRecord record2;
Serial.println("Processing Callback...");

// message is automatically updated when a new NDEF message is received
// only if we call message.begin() in setup()
if (message.isEmpty()) {
Serial.println("--- Provisioned buffer size too small or NDEF message empty");
return;
Expand All @@ -291,7 +295,8 @@ void ndefCallback() {
// TODO: replace with record.isNotEmpty() when implemented
while (message.hasRecord()) {
// Get a new record every time we call getRecord()
record = message.getRecord();
displayRecordInfo(record);
// record = message.getRecord();
record2.create(message.getRecord());
displayRecordInfo(record2);
}
}
3 changes: 1 addition & 2 deletions src/Electroniccats_PN7150.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
// The HW interface between The PN7150 and the DeviceHost is I2C, so we need the I2C library.library
#include "Mode.h"
#include "P2P_NDEF.h"
#include "RW_NDEF.h"
#include "RemoteDevice.h"
#include "T4T_NDEF_emu.h"
// #include "ndef_helper.h"
#include "NdefMessage.h"
#include "NdefRecord.h"

// #define DEBGU2
// #define DEBUG3
Expand Down
39 changes: 39 additions & 0 deletions src/NdefRecord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "NdefRecord.h"

NdefRecord::NdefRecord() {
type = UNSUPPORTED_NDEF_RECORD;
payload = NULL;
payloadSize = 0;
}

void NdefRecord::create(NdefRecord_t record) {
this->type = record.recordType;
this->payload = record.recordPayload;
this->payloadSize = record.recordPayloadSize;
}

NdefRecordType_e NdefRecord::getType() {
return this->type;
}

unsigned char NdefRecord::getPayload() {
return this->payload[getPayloadSize()];
}

unsigned short NdefRecord::getPayloadSize() {
return this->payloadSize;
}

String NdefRecord::getText() {
unsigned char save = payload[payloadSize];
payload[payloadSize] = '\0';
String text = "";

if (getType() == WELL_KNOWN_SIMPLE_TEXT) {
text = reinterpret_cast<const char *>(&payload[payload[0] + 1]);
}

payload[payloadSize] = save;

return text;
}
23 changes: 23 additions & 0 deletions src/NdefRecord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef NdefRecord_H
#define NdefRecord_H

#include <Arduino.h>

#include "ndef_helper.h"

class NdefRecord {
private:
NdefRecordType_e type;
unsigned char *payload;
unsigned short payloadSize;

public:
NdefRecord();
void create(NdefRecord_t record);
NdefRecordType_e getType();
unsigned char getPayload();
unsigned short getPayloadSize();
String getText();
};

#endif
4 changes: 4 additions & 0 deletions src/ndef_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* copyright or trademark. NXP must not be liable for any loss or damage
* arising from its use.
*/
#ifndef NdefHelper_H
#define NdefHelper_H

#define NDEF_EMPTY 0x00
#define NDEF_WELL_KNOWN 0x01
Expand Down Expand Up @@ -57,3 +59,5 @@ const char *ndef_helper_WifiEnc(unsigned char enc);
const char *ndef_helper_UriHead(unsigned char head);
NdefRecord_t DetectNdefRecordType(unsigned char *pNdefRecord);
unsigned char *GetNextRecord(unsigned char *pNdefRecord);

#endif

0 comments on commit ec99d67

Please sign in to comment.