Skip to content

Commit

Permalink
fix: max message length
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Dec 18, 2023
1 parent 75ac473 commit 98064a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
14 changes: 13 additions & 1 deletion examples/NDEFSendMessage/NDEFSendMessage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,26 @@ 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
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("http://test2.com");
// message.addUriRecord("https://test3.com");
// message.addUriRecord("tel:test4");
Expand Down
33 changes: 27 additions & 6 deletions src/NdefMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down
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 98064a9

Please sign in to comment.