diff --git a/application.fam b/application.fam index 60438e5..497ea91 100644 --- a/application.fam +++ b/application.fam @@ -3,9 +3,9 @@ App( name="NFC Comparator", apptype=FlipperAppType.EXTERNAL, entry_point="nfc_comparator_main", - stack_size=4 * 1024, + stack_size=2 * 1024, fap_category="NFC", - fap_version="1.0", + fap_version="1.1", fap_icon="assets/Comparator_10px.png", fap_description="A tool to check if a physical NFC card is the same as a saved one", fap_author="acegoal07", diff --git a/lib/reader_worker/nfc_comparator_reader_worker.c b/lib/reader_worker/nfc_comparator_reader_worker.c index 655c870..c3c0eba 100644 --- a/lib/reader_worker/nfc_comparator_reader_worker.c +++ b/lib/reader_worker/nfc_comparator_reader_worker.c @@ -4,11 +4,9 @@ NfcComparatorReaderWorker* nfc_comparator_reader_worker_alloc() { NfcComparatorReaderWorker* nfc_comparator_reader_worker = malloc(sizeof(NfcComparatorReaderWorker)); nfc_comparator_reader_worker->nfc = nfc_alloc(); - nfc_comparator_reader_worker->nfc_scanner = - nfc_scanner_alloc(nfc_comparator_reader_worker->nfc); nfc_comparator_reader_worker->thread = furi_thread_alloc_ex( "NfcComparatorReaderWorker", - 4096, + 1024, nfc_comparator_reader_worker_task, nfc_comparator_reader_worker); nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; @@ -19,11 +17,29 @@ void nfc_comparator_reader_worker_free(void* context) { NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; furi_assert(nfc_comparator_reader_worker); nfc_free(nfc_comparator_reader_worker->nfc); - nfc_scanner_free(nfc_comparator_reader_worker->nfc_scanner); furi_thread_free(nfc_comparator_reader_worker->thread); + nfc_device_free(nfc_comparator_reader_worker->loaded_nfc_card); free(nfc_comparator_reader_worker); } +void nfc_comparator_reader_worker_stop(void* context) { + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; + furi_assert(nfc_comparator_reader_worker); + if(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) { + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; + furi_thread_join(nfc_comparator_reader_worker->thread); + } +} + +void nfc_comparator_reader_worker_start(void* context) { + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; + furi_assert(nfc_comparator_reader_worker); + if(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Stopped) { + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning; + furi_thread_start(nfc_comparator_reader_worker->thread); + } +} + void nfc_comparator_reader_worker_scanner_callback(NfcScannerEvent event, void* context) { NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; furi_assert(nfc_comparator_reader_worker); @@ -48,80 +64,90 @@ NfcCommand nfc_comparator_reader_worker_poller_callback(NfcGenericEvent event, v int32_t nfc_comparator_reader_worker_task(void* context) { NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; - nfc_comparator_reader_worker->nfc_scanner = - nfc_scanner_alloc(nfc_comparator_reader_worker->nfc); - - nfc_scanner_start( - nfc_comparator_reader_worker->nfc_scanner, - nfc_comparator_reader_worker_scanner_callback, - nfc_comparator_reader_worker); - - nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning; - while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Scanning) { - furi_delay_ms(100); - } - nfc_scanner_stop(nfc_comparator_reader_worker->nfc_scanner); - - NfcPoller* nfc_poller = nfc_poller_alloc( - nfc_comparator_reader_worker->nfc, nfc_comparator_reader_worker->protocol[0]); - - nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Polling; - nfc_poller_start( - nfc_poller, nfc_comparator_reader_worker_poller_callback, nfc_comparator_reader_worker); - - while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Polling) { - furi_delay_ms(100); - } - - nfc_poller_stop(nfc_poller); - - NfcDevice* data = nfc_device_alloc(); - nfc_device_set_data( - data, - nfc_comparator_reader_worker->protocol[0], - (NfcDeviceData*)nfc_poller_get_data(nfc_poller)); - - nfc_comparator_reader_worker->compare_checks.protocol = - nfc_device_get_protocol_name(nfc_device_get_protocol(data)) == + while(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) { + switch(nfc_comparator_reader_worker->state) { + case NfcComparatorReaderWorkerState_Scanning: { + NfcScanner* nfc_scanner = nfc_scanner_alloc(nfc_comparator_reader_worker->nfc); + nfc_scanner_start( + nfc_scanner, + nfc_comparator_reader_worker_scanner_callback, + nfc_comparator_reader_worker); + while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Scanning) { + furi_delay_ms(100); + } + nfc_scanner_stop(nfc_scanner); + break; + } + case NfcComparatorReaderWorkerState_Polling: { + NfcPoller* nfc_poller = nfc_poller_alloc( + nfc_comparator_reader_worker->nfc, nfc_comparator_reader_worker->protocol[0]); + nfc_poller_start( + nfc_poller, + nfc_comparator_reader_worker_poller_callback, + nfc_comparator_reader_worker); + while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Polling) { + furi_delay_ms(100); + } + nfc_poller_stop(nfc_poller); + nfc_comparator_reader_worker->scanned_nfc_card = nfc_device_alloc(); + nfc_device_set_data( + nfc_comparator_reader_worker->scanned_nfc_card, + nfc_comparator_reader_worker->protocol[0], + (NfcDeviceData*)nfc_poller_get_data(nfc_poller)); + nfc_poller_free(nfc_poller); + break; + } + case NfcComparatorReaderWorkerState_Comparing: { + nfc_comparator_reader_worker->compare_checks.protocol = nfc_device_get_protocol_name( - nfc_device_get_protocol(nfc_comparator_reader_worker->loaded_nfc_card)) ? - true : - false; - - nfc_poller_free(nfc_poller); - - size_t poller_uid_len = 0; - const uint8_t* poller_uid = nfc_device_get_uid(data, &poller_uid_len); - FuriString* poller_uid_str = furi_string_alloc(); - for(size_t i = 0; i < poller_uid_len; i++) { - furi_string_utf8_push(poller_uid_str, poller_uid[i]); + nfc_device_get_protocol(nfc_comparator_reader_worker->scanned_nfc_card)) == + nfc_device_get_protocol_name( + nfc_device_get_protocol(nfc_comparator_reader_worker->loaded_nfc_card)) ? + true : + false; + + size_t poller_uid_len = 0; + const uint8_t* poller_uid = + nfc_device_get_uid(nfc_comparator_reader_worker->scanned_nfc_card, &poller_uid_len); + FuriString* poller_uid_str = furi_string_alloc(); + for(size_t i = 0; i < poller_uid_len; i++) { + furi_string_utf8_push(poller_uid_str, poller_uid[i]); + } + + nfc_device_free(nfc_comparator_reader_worker->scanned_nfc_card); + + size_t loaded_uid_len = 0; + const uint8_t* loaded_uid = + nfc_device_get_uid(nfc_comparator_reader_worker->loaded_nfc_card, &loaded_uid_len); + FuriString* loaded_uid_str = furi_string_alloc(); + for(size_t i = 0; i < loaded_uid_len; i++) { + furi_string_utf8_push(loaded_uid_str, loaded_uid[i]); + } + + nfc_comparator_reader_worker->compare_checks.uid = + furi_string_cmpi(loaded_uid_str, poller_uid_str) == 0 ? true : false; + + nfc_comparator_reader_worker->compare_checks.uid_length = + poller_uid_len == loaded_uid_len ? true : false; + + furi_string_free(poller_uid_str); + furi_string_free(loaded_uid_str); + + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; + break; + } + default: + break; + } } - - nfc_device_free(data); - - size_t loaded_uid_len = 0; - const uint8_t* loaded_uid = - nfc_device_get_uid(nfc_comparator_reader_worker->loaded_nfc_card, &loaded_uid_len); - FuriString* loaded_uid_str = furi_string_alloc(); - for(size_t i = 0; i < loaded_uid_len; i++) { - furi_string_utf8_push(loaded_uid_str, loaded_uid[i]); - } - - nfc_comparator_reader_worker->compare_checks.uid = - furi_string_cmpi(loaded_uid_str, poller_uid_str) == 0 ? true : false; - - furi_string_free(poller_uid_str); - furi_string_free(loaded_uid_str); - - nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; - return 0; } -void nfc_comparator_reader_worker_set_compare_nfc_device(void* context, NfcDevice* nfc_device) { +void nfc_comparator_reader_worker_set_loaded_nfc_card(void* context, const char* path_to_nfc_card) { NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; furi_assert(nfc_comparator_reader_worker); - nfc_comparator_reader_worker->loaded_nfc_card = nfc_device; + nfc_comparator_reader_worker->loaded_nfc_card = nfc_device_alloc(); + nfc_device_load(nfc_comparator_reader_worker->loaded_nfc_card, path_to_nfc_card); } bool nfc_comparator_reader_worker_is_running(void* context) { @@ -135,24 +161,6 @@ NfcComparatorReaderWorkerState nfc_comparator_reader_worker_get_state(void* cont return nfc_comparator_reader_worker->state; } -void nfc_comparator_reader_worker_stop(void* context) { - NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; - furi_assert(nfc_comparator_reader_worker); - if(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) { - nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; - } - furi_thread_join(nfc_comparator_reader_worker->thread); -} - -void nfc_comparator_reader_worker_start(void* context) { - NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; - furi_assert(nfc_comparator_reader_worker); - if(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Stopped) { - nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning; - furi_thread_start(nfc_comparator_reader_worker->thread); - } -} - NfcComparatorReaderWorkerCompareChecks nfc_comparator_reader_worker_get_compare_checks(void* context) { NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; diff --git a/lib/reader_worker/nfc_comparator_reader_worker.h b/lib/reader_worker/nfc_comparator_reader_worker.h index 29ddecf..bb8d197 100644 --- a/lib/reader_worker/nfc_comparator_reader_worker.h +++ b/lib/reader_worker/nfc_comparator_reader_worker.h @@ -1,6 +1,7 @@ #pragma once #include #include + #include #include #include @@ -15,6 +16,7 @@ typedef enum { typedef struct { bool uid; + bool uid_length; bool protocol; } NfcComparatorReaderWorkerCompareChecks; @@ -24,22 +26,23 @@ typedef struct { NfcProtocol* protocol; NfcComparatorReaderWorkerState state; NfcDevice* loaded_nfc_card; + NfcDevice* scanned_nfc_card; NfcPoller* nfc_poller; - NfcScanner* nfc_scanner; NfcComparatorReaderWorkerCompareChecks compare_checks; } NfcComparatorReaderWorker; NfcComparatorReaderWorker* nfc_comparator_reader_worker_alloc(); void nfc_comparator_reader_worker_free(void* context); +void nfc_comparator_reader_worker_stop(void* context); +void nfc_comparator_reader_worker_start(void* context); void nfc_comparator_reader_worker_scanner_callback(NfcScannerEvent event, void* context); NfcCommand nfc_comparator_reader_worker_poller_callback(NfcGenericEvent event, void* context); + int32_t nfc_comparator_reader_worker_task(void* context); -void nfc_comparator_reader_worker_set_compare_nfc_device(void* context, NfcDevice* nfc_device); +void nfc_comparator_reader_worker_set_loaded_nfc_card(void* context, const char* path_to_nfc_card); bool nfc_comparator_reader_worker_is_running(void* context); NfcComparatorReaderWorkerState nfc_comparator_reader_worker_get_state(void* context); -void nfc_comparator_reader_worker_stop(void* context); -void nfc_comparator_reader_worker_start(void* context); NfcComparatorReaderWorkerCompareChecks nfc_comparator_reader_worker_get_compare_checks(void* context); diff --git a/nfc_comparator.c b/nfc_comparator.c index cbfe6d6..de88312 100644 --- a/nfc_comparator.c +++ b/nfc_comparator.c @@ -61,9 +61,6 @@ static void nfc_comparator_free(NfcComparator* nfc_comparator) { submenu_free(nfc_comparator->submenu); file_browser_free(nfc_comparator->file_browser); furi_string_free(nfc_comparator->file_browser_output); - if(nfc_comparator->loaded_nfc_card) { - nfc_device_free(nfc_comparator->loaded_nfc_card); - } popup_free(nfc_comparator->popup); free(nfc_comparator); diff --git a/nfc_comparator.h b/nfc_comparator.h index 42cbb82..56ba044 100644 --- a/nfc_comparator.h +++ b/nfc_comparator.h @@ -2,8 +2,6 @@ #include #include -#include - #include "nfc_comparator_icons.h" #include @@ -40,7 +38,7 @@ typedef struct { FileBrowser* file_browser; FuriString* file_browser_output; Popup* popup; - NfcDevice* loaded_nfc_card; + NfcComparatorReaderWorker* worker; } NfcComparator; #define NFC_ITEM_LOCATION "/ext/nfc/" diff --git a/scenes/nfc_comparator_scene_comparator.c b/scenes/nfc_comparator_scene_comparator.c index fb7545b..b15a85a 100644 --- a/scenes/nfc_comparator_scene_comparator.c +++ b/scenes/nfc_comparator_scene_comparator.c @@ -1,11 +1,6 @@ #include "../nfc_comparator.h" -void nfc_comparator_comparator_menu_callback(void* context, uint32_t index) { - NfcComparator* nfc_comparator = context; - scene_manager_handle_custom_event(nfc_comparator->scene_manager, index); -} - void nfc_comparator_comparator_scene_on_enter(void* context) { NfcComparator* nfc_comparator = context; furi_assert(nfc_comparator); @@ -14,7 +9,8 @@ void nfc_comparator_comparator_scene_on_enter(void* context) { view_dispatcher_switch_to_view(nfc_comparator->view_dispatcher, NfcComparatorView_Popup); NfcComparatorReaderWorker* worker = nfc_comparator_reader_worker_alloc(); - nfc_comparator_reader_worker_set_compare_nfc_device(worker, nfc_comparator->loaded_nfc_card); + nfc_comparator_reader_worker_set_loaded_nfc_card( + worker, furi_string_get_cstr(nfc_comparator->file_browser_output)); nfc_comparator_reader_worker_start(worker); while(nfc_comparator_reader_worker_is_running(worker)) { @@ -33,6 +29,7 @@ void nfc_comparator_comparator_scene_on_enter(void* context) { } furi_delay_ms(100); } + nfc_comparator_reader_worker_stop(worker); popup_set_header(nfc_comparator->popup, "Compare Results", 64, 5, AlignCenter, AlignTop); @@ -44,8 +41,9 @@ void nfc_comparator_comparator_scene_on_enter(void* context) { FuriString* comparator = furi_string_alloc(); furi_string_printf( comparator, - "UID: %s\nProtocol: %s", + "UID: %s\nUID length: %s\nProtocol: %s", checks.uid ? "Match" : "Mismatch", + checks.uid_length ? "Match" : "Mismatch", checks.protocol ? "Match" : "Mismatch"); char result_buffer[158]; @@ -53,14 +51,13 @@ void nfc_comparator_comparator_scene_on_enter(void* context) { furi_string_free(comparator); - popup_set_text(nfc_comparator->popup, result_buffer, 64, 30, AlignCenter, AlignCenter); + popup_set_text(nfc_comparator->popup, result_buffer, 64, 35, AlignCenter, AlignCenter); } bool nfc_comparator_comparator_scene_on_event(void* context, SceneManagerEvent event) { - UNUSED(context); UNUSED(event); - bool consumed = false; - return consumed; + UNUSED(context); + return false; } void nfc_comparator_comparator_scene_on_exit(void* context) { diff --git a/scenes/nfc_comparator_scene_main_menu.c b/scenes/nfc_comparator_scene_main_menu.c index 59eb809..72e3297 100644 --- a/scenes/nfc_comparator_scene_main_menu.c +++ b/scenes/nfc_comparator_scene_main_menu.c @@ -24,7 +24,7 @@ void nfc_comparator_main_menu_scene_on_enter(void* context) { NfcComparatorMainMenu_StartComparator, nfc_comparator_main_menu_menu_callback, nfc_comparator, - !nfc_comparator->loaded_nfc_card, + furi_string_empty(nfc_comparator->file_browser_output), "No NFC\ncard selected"); submenu_add_item( diff --git a/scenes/nfc_comparator_scene_select_nfc_card.c b/scenes/nfc_comparator_scene_select_nfc_card.c index 6a7eee4..d661f74 100644 --- a/scenes/nfc_comparator_scene_select_nfc_card.c +++ b/scenes/nfc_comparator_scene_select_nfc_card.c @@ -2,10 +2,6 @@ void nfc_comparator_select_nfc_card_menu_callback(void* context) { NfcComparator* nfc_comparator = context; - nfc_comparator->loaded_nfc_card = nfc_device_alloc(); - nfc_device_load( - nfc_comparator->loaded_nfc_card, furi_string_get_cstr(nfc_comparator->file_browser_output)); - furi_string_reset(nfc_comparator->file_browser_output); scene_manager_previous_scene(nfc_comparator->scene_manager); }