Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFC: NDEF SmartPoster support #275

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@
- OFW: USB/BT Remote: Mouse clicker option to click as fast as possible (by @sumukhj1219)
- CLI: Print plugin name on load fail (by @Willy-JL)
- NFC:
- NDEF parser supports Mifare Classic (#265 by @luu176), rewritten to be protocol-agnostic and more improvements (#265 by @Willy-JL)
- NDEF parser supports NTAG I2C Plus 1k and 2k chips too (by @RocketGod-git)
- NDEF parser decodes URL-encoded URI characters (#267 by @jaylikesbunda)
- NDEF Parser:
- Mifare Classic support (#265 by @luu176), protocol-agnostic rewrite and more improvements (#265 by @Willy-JL)
- Decoding of URL-encoded URI characters (#267 by @jaylikesbunda)
- SmartPoster record support (#275 by @Willy-JL)
- Enable parsing NTAG I2C Plus 1k and 2k chips too (#237 by @RocketGod-git)
- Added 6 new Mifare Classic keys from Bulgaria Hotel (#216 by @z3r0l1nk)
- UL: Add iq aparts hotel key (by @xMasterX)
- OFW/UL: Rename 'Detect Reader' to 'Extract MFC Keys' (by @bettse & @xMasterX)
Expand Down
43 changes: 31 additions & 12 deletions applications/main/nfc/plugins/supported_cards/ndef.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,20 @@ static bool ndef_parse_wifi(Ndef* ndef, size_t pos, size_t len) {
return true;
}

static bool ndef_parse_payload(
// ---=== ndef layout parsing ===---

static bool
ndef_parse_message(Ndef* ndef, size_t pos, size_t len, size_t message_num, bool smart_poster);
static size_t ndef_parse_tlv(Ndef* ndef, size_t pos, size_t already_parsed);
static bool ndef_parse_record(
Ndef* ndef,
size_t pos,
size_t len,
NdefTnf tnf,
const char* type,
uint8_t type_len);

static bool ndef_parse_record(
Ndef* ndef,
size_t pos,
size_t len,
Expand All @@ -507,13 +520,16 @@ static bool ndef_parse_payload(

switch(tnf) {
case NdefTnfWellKnownType:
if(strncmp("U", type, type_len) == 0) {
if(strncmp("Sp", type, type_len) == 0) {
furi_string_cat(ndef->output, "SmartPoster\nContained records below\n\n");
return ndef_parse_message(ndef, pos, len, 0, true);
} else if(strncmp("U", type, type_len) == 0) {
return ndef_parse_uri(ndef, pos, len);
} else if(strncmp("T", type, type_len) == 0) {
return ndef_parse_text(ndef, pos, len);
}
// Dump data without parsing
furi_string_cat(ndef->output, "Unsupported\n");
furi_string_cat(ndef->output, "Unknown\n");
ndef_print(ndef, "Well-known Type", type, type_len, false);
if(!ndef_dump(ndef, "Payload", pos, len, false)) return false;
return true;
Expand All @@ -527,7 +543,7 @@ static bool ndef_parse_payload(
return ndef_parse_wifi(ndef, pos, len);
}
// Dump data without parsing
furi_string_cat(ndef->output, "Unsupported\n");
furi_string_cat(ndef->output, "Unknown\n");
ndef_print(ndef, "Media Type", type, type_len, false);
if(!ndef_dump(ndef, "Payload", pos, len, false)) return false;
return true;
Expand All @@ -548,11 +564,10 @@ static bool ndef_parse_payload(
}
}

// ---=== tlv and message parsing ===---

// NDEF message structure:
// https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/protocols/nfc/index.html#ndef_message_and_record_format
static bool ndef_parse_message(Ndef* ndef, size_t pos, size_t len, size_t message_num) {
static bool
ndef_parse_message(Ndef* ndef, size_t pos, size_t len, size_t message_num, bool smart_poster) {
size_t end = pos + len;

size_t record_num = 0;
Expand Down Expand Up @@ -599,7 +614,7 @@ static bool ndef_parse_message(Ndef* ndef, size_t pos, size_t len, size_t messag
}

// Payload Type
char type_buf[32]; // Longest type supported in ndef_parse_payload() is 32 chars excl terminator
char type_buf[32]; // Longest type supported in ndef_parse_record() is 32 chars excl terminator
char* type = type_buf;
bool type_was_allocated = false;
if(type_len) {
Expand All @@ -617,9 +632,12 @@ static bool ndef_parse_message(Ndef* ndef, size_t pos, size_t len, size_t messag
// Payload ID
pos += id_len;

furi_string_cat_printf(ndef->output, "\e*> M%dR%d: ", message_num, record_num);
if(!ndef_parse_payload(
ndef, pos, payload_len, flags_tnf.type_name_format, type, type_len)) {
if(smart_poster) {
furi_string_cat_printf(ndef->output, "\e*> SP-R%d: ", record_num);
} else {
furi_string_cat_printf(ndef->output, "\e*> M%d-R%d: ", message_num, record_num);
}
if(!ndef_parse_record(ndef, pos, payload_len, flags_tnf.type_name_format, type, type_len)) {
if(type_was_allocated) free(type);
return false;
}
Expand Down Expand Up @@ -678,7 +696,8 @@ static size_t ndef_parse_tlv(Ndef* ndef, size_t pos, size_t already_parsed) {
break;
}

if(!ndef_parse_message(ndef, pos, len, ++message_num + already_parsed)) return 0;
if(!ndef_parse_message(ndef, pos, len, ++message_num + already_parsed, false))
return 0;
pos += len;

break;
Expand Down
Loading