Skip to content

Commit

Permalink
feat: add mime media record
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Dec 28, 2023
1 parent 1d6d478 commit 884548a
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 18 deletions.
52 changes: 43 additions & 9 deletions examples/NDEFSendMessage/NDEFSendMessage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand Down
16 changes: 16 additions & 0 deletions src/NdefMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/NdefMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 60 additions & 6 deletions src/NdefRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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()) {
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions src/NdefRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ 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;
String newString;
bool textRecord;
String getHexRepresentation(const byte *data, const uint32_t dataSize);
bool isTextRecord();
const char *getWellKnownContent();
const char *getMimeMediaContent();

public:
NdefRecord();
Expand All @@ -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);
Expand Down

0 comments on commit 884548a

Please sign in to comment.