From d5161f080617acc4ba2a0d4406e340d57693f921 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Thu, 6 Mar 2025 01:40:46 +0000 Subject: [PATCH] Fix reading empty NDEF message --- .../protocol_support/type_4_tag/type_4_tag.c | 16 ++++++++++------ .../type_4_tag/type_4_tag_render.c | 19 ++++++++++++------- .../type_4_tag/type_4_tag_poller_i.c | 8 +++++++- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag.c b/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag.c index aefd095c1..97a4872c7 100644 --- a/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag.c +++ b/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag.c @@ -44,12 +44,16 @@ static void nfc_scene_more_info_on_enter_type_4_tag(NfcApp* instance) { scene_manager_get_scene_state(instance->scene_manager, NfcSceneMoreInfo); if(scene_state == NfcSceneMoreInfoStateASCII) { - pretty_format_bytes_hex_canonical( - instance->text_box_store, - TYPE_4_TAG_RENDER_BYTES_PER_LINE, - PRETTY_FORMAT_FONT_MONOSPACE, - simple_array_cget_data(data->ndef_data), - simple_array_get_count(data->ndef_data)); + if(simple_array_get_count(data->ndef_data) == 0) { + furi_string_cat_str(instance->text_box_store, "No NDEF data to show"); + } else { + pretty_format_bytes_hex_canonical( + instance->text_box_store, + TYPE_4_TAG_RENDER_BYTES_PER_LINE, + PRETTY_FORMAT_FONT_MONOSPACE, + simple_array_cget_data(data->ndef_data), + simple_array_get_count(data->ndef_data)); + } widget_add_text_scroll_element( instance->widget, 0, 0, 128, 48, furi_string_get_cstr(instance->text_box_store)); diff --git a/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag_render.c b/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag_render.c index bf85feb78..8fdeecb14 100644 --- a/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag_render.c +++ b/applications/main/nfc/helpers/protocol_support/type_4_tag/type_4_tag_render.c @@ -10,7 +10,11 @@ void nfc_render_type_4_tag_info( FuriString* str) { nfc_render_iso14443_4a_brief(type_4_tag_get_base_data(data), str); - furi_string_cat(str, "\n::::::::::::::::::[Tag Specs]::::::::::::::::::\n"); + furi_string_cat(str, "\n:::::::::::::::[Stored NDEF]:::::::::::::::\n"); + furi_string_cat_printf( + str, "Current NDEF Size: %lu\n", simple_array_get_count(data->ndef_data)); + + furi_string_cat(str, "::::::::::::::::::[Tag Specs]::::::::::::::::::\n"); furi_string_cat_printf( str, "T4T Mapping Version: %u.%u\n", data->t4t_version.major, data->t4t_version.minor); furi_string_cat_printf(str, "NDEF File ID: 0x%04X\n", data->ndef_file_id); @@ -24,13 +28,10 @@ void nfc_render_type_4_tag_info( data->ndef_read_lock == 0 ? " (unlocked)" : ""); furi_string_cat_printf( str, - "Write Lock: 0x%02X%s\n", + "Write Lock: 0x%02X%s", data->ndef_write_lock, data->ndef_write_lock == 0 ? " (unlocked)" : ""); - furi_string_cat(str, ":::::::::::::::[Stored NDEF]:::::::::::::::\n"); - furi_string_cat_printf(str, "Current NDEF Size: %lu", simple_array_get_count(data->ndef_data)); - if(format_type != NfcProtocolFormatTypeFull) return; furi_string_cat(str, "\n\e#ISO14443-4 data"); @@ -38,9 +39,13 @@ void nfc_render_type_4_tag_info( } void nfc_render_type_4_tag_dump(const Type4TagData* data, FuriString* str) { - furi_string_cat_printf(str, "\e*"); - const uint8_t* ndef_data = simple_array_cget_data(data->ndef_data); size_t ndef_len = simple_array_get_count(data->ndef_data); + if(ndef_len == 0) { + furi_string_cat_str(str, "No NDEF data to show"); + return; + } + const uint8_t* ndef_data = simple_array_cget_data(data->ndef_data); + furi_string_cat_printf(str, "\e*"); for(size_t i = 0; i < ndef_len; i += TYPE_4_TAG_RENDER_BYTES_PER_LINE) { const uint8_t* line_data = &ndef_data[i]; for(size_t j = 0; j < TYPE_4_TAG_RENDER_BYTES_PER_LINE; j += 2) { diff --git a/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c b/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c index 5d8c56d40..ab2f28285 100644 --- a/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c +++ b/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c @@ -208,14 +208,20 @@ Type4TagError type_4_tag_poller_read_ndef(Type4TagPoller* instance) { ndef_len = bit_lib_bytes_to_num_be(bit_buffer_get_data(instance->rx_buffer), sizeof(ndef_len)); - simple_array_init(instance->data->ndef_data, ndef_len); + if(ndef_len == 0) { + FURI_LOG_D(TAG, "NDEF file is empty"); + break; + } if(ndef_len > 510) { // Both size and offset for READ BINARY are 1 byte uint // So the furthest we can read is 255 bytes at offset 255 // AKA 2 * 255 byte chunks = 510 bytes // TODO: Surely there has to be another way? FURI_LOG_E(TAG, "NDEF file too long: %zu bytes", ndef_len); + error = Type4TagErrorProtocol; + break; } + simple_array_init(instance->data->ndef_data, ndef_len); FURI_LOG_D(TAG, "Read NDEF"); const uint8_t type_4_tag_read_ndef_apdu_1[] = {