diff --git a/applications/main/nfc/helpers/nfc_detected_protocols.c b/applications/main/nfc/helpers/nfc_detected_protocols.c new file mode 100644 index 000000000..339d4ddc2 --- /dev/null +++ b/applications/main/nfc/helpers/nfc_detected_protocols.c @@ -0,0 +1,85 @@ +#include "nfc_detected_protocols.h" + +#include + +struct NfcDetectedProtocols { + uint32_t protocols_detected_num; + NfcProtocol protocols_detected[NfcProtocolNum]; + uint32_t selected_idx; +}; + +NfcDetectedProtocols* nfc_detected_protocols_alloc(void) { + NfcDetectedProtocols* instance = malloc(sizeof(NfcDetectedProtocols)); + + instance->protocols_detected_num = 0; + instance->selected_idx = 0; + + return instance; +} + +void nfc_detected_protocols_free(NfcDetectedProtocols* instance) { + furi_assert(instance); + + free(instance); +} + +void nfc_detected_protocols_reset(NfcDetectedProtocols* instance) { + furi_assert(instance); + + instance->protocols_detected_num = 0; + memset(instance->protocols_detected, 0, sizeof(instance->protocols_detected)); + instance->selected_idx = 0; +} + +void nfc_detected_protocols_select(NfcDetectedProtocols* instance, uint32_t idx) { + furi_assert(instance); + + instance->selected_idx = idx; +} + +void nfc_detected_protocols_set( + NfcDetectedProtocols* instance, + const NfcProtocol* types, + uint32_t count) { + furi_assert(instance); + furi_assert(types); + furi_assert(count < NfcProtocolNum); + + memcpy(instance->protocols_detected, types, count); + instance->protocols_detected_num = count; + instance->selected_idx = 0; +} + +uint32_t nfc_detected_protocols_get_num(NfcDetectedProtocols* instance) { + furi_assert(instance); + + return instance->protocols_detected_num; +} + +NfcProtocol nfc_detected_protocols_get_protocol(NfcDetectedProtocols* instance, uint32_t idx) { + furi_assert(instance); + furi_assert(idx < instance->protocols_detected_num); + + return instance->protocols_detected[idx]; +} + +void nfc_detected_protocols_fill_all_protocols(NfcDetectedProtocols* instance) { + furi_assert(instance); + + instance->protocols_detected_num = NfcProtocolNum; + for(uint32_t i = 0; i < NfcProtocolNum; i++) { + instance->protocols_detected[i] = i; + } +} + +NfcProtocol nfc_detected_protocols_get_selected(NfcDetectedProtocols* instance) { + furi_assert(instance); + + return instance->protocols_detected[instance->selected_idx]; +} + +uint32_t nfc_detected_protocols_get_selected_idx(NfcDetectedProtocols* instance) { + furi_assert(instance); + + return instance->selected_idx; +} diff --git a/applications/main/nfc/helpers/nfc_detected_protocols.h b/applications/main/nfc/helpers/nfc_detected_protocols.h new file mode 100644 index 000000000..2ab46add3 --- /dev/null +++ b/applications/main/nfc/helpers/nfc_detected_protocols.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +typedef struct NfcDetectedProtocols NfcDetectedProtocols; + +NfcDetectedProtocols* nfc_detected_protocols_alloc(void); + +void nfc_detected_protocols_free(NfcDetectedProtocols* instance); + +void nfc_detected_protocols_reset(NfcDetectedProtocols* instance); + +void nfc_detected_protocols_select(NfcDetectedProtocols* instance, uint32_t idx); + +void nfc_detected_protocols_set( + NfcDetectedProtocols* instance, + const NfcProtocol* types, + uint32_t count); + +uint32_t nfc_detected_protocols_get_num(NfcDetectedProtocols* instance); + +NfcProtocol nfc_detected_protocols_get_protocol(NfcDetectedProtocols* instance, uint32_t idx); + +void nfc_detected_protocols_fill_all_protocols(NfcDetectedProtocols* instance); + +NfcProtocol nfc_detected_protocols_get_selected(NfcDetectedProtocols* instance); + +uint32_t nfc_detected_protocols_get_selected_idx(NfcDetectedProtocols* instance); diff --git a/applications/main/nfc/helpers/protocol_support/nfc_protocol_support.c b/applications/main/nfc/helpers/protocol_support/nfc_protocol_support.c index 2ba5c65ba..630b3beef 100644 --- a/applications/main/nfc/helpers/protocol_support/nfc_protocol_support.c +++ b/applications/main/nfc/helpers/protocol_support/nfc_protocol_support.c @@ -150,8 +150,7 @@ static void nfc_protocol_support_scene_read_on_enter(NfcApp* instance) { view_dispatcher_switch_to_view(instance->view_dispatcher, NfcViewPopup); - const NfcProtocol protocol = - instance->protocols_detected[instance->protocols_detected_selected_idx]; + const NfcProtocol protocol = nfc_detected_protocols_get_selected(instance->detected_protocols); instance->poller = nfc_poller_alloc(instance->nfc, protocol); view_dispatcher_switch_to_view(instance->view_dispatcher, NfcViewPopup); @@ -186,7 +185,7 @@ static bool nfc_protocol_support_scene_read_on_event(NfcApp* instance, SceneMana consumed = true; } else { const NfcProtocol protocol = - instance->protocols_detected[instance->protocols_detected_selected_idx]; + nfc_detected_protocols_get_selected(instance->detected_protocols); consumed = nfc_protocol_support[protocol]->scene_read.on_event(instance, event); } } else if(event.event == NfcCustomEventPollerFailure) { @@ -199,7 +198,7 @@ static bool nfc_protocol_support_scene_read_on_event(NfcApp* instance, SceneMana consumed = true; } else if(event.event == NfcCustomEventCardDetected) { const NfcProtocol protocol = - instance->protocols_detected[instance->protocols_detected_selected_idx]; + nfc_detected_protocols_get_selected(instance->detected_protocols); consumed = nfc_protocol_support[protocol]->scene_read.on_event(instance, event); } } else if(event.type == SceneManagerEventTypeBack) { diff --git a/applications/main/nfc/nfc_app.c b/applications/main/nfc/nfc_app.c index 62a1206f1..f3aa5e724 100644 --- a/applications/main/nfc/nfc_app.c +++ b/applications/main/nfc/nfc_app.c @@ -50,6 +50,7 @@ NfcApp* nfc_app_alloc(void) { instance->nfc = nfc_alloc(); + instance->detected_protocols = nfc_detected_protocols_alloc(); instance->felica_auth = felica_auth_alloc(); instance->mf_ul_auth = mf_ultralight_auth_alloc(); instance->slix_unlock = slix_unlock_alloc(); @@ -142,6 +143,7 @@ void nfc_app_free(NfcApp* instance) { nfc_free(instance->nfc); + nfc_detected_protocols_free(instance->detected_protocols); felica_auth_free(instance->felica_auth); mf_ultralight_auth_free(instance->mf_ul_auth); slix_unlock_free(instance->slix_unlock); @@ -433,23 +435,6 @@ void nfc_show_loading_popup(void* context, bool show) { } } -void nfc_app_set_detected_protocols(NfcApp* instance, const NfcProtocol* types, uint32_t count) { - furi_assert(instance); - furi_assert(types); - furi_assert(count < NfcProtocolNum); - - memcpy(instance->protocols_detected, types, count); - instance->protocols_detected_num = count; - instance->protocols_detected_selected_idx = 0; -} - -void nfc_app_reset_detected_protocols(NfcApp* instance) { - furi_assert(instance); - - instance->protocols_detected_selected_idx = 0; - instance->protocols_detected_num = 0; -} - void nfc_append_filename_string_when_present(NfcApp* instance, FuriString* string) { furi_assert(instance); furi_assert(string); diff --git a/applications/main/nfc/nfc_app_i.h b/applications/main/nfc/nfc_app_i.h index c91ef33df..0a09fa924 100644 --- a/applications/main/nfc/nfc_app_i.h +++ b/applications/main/nfc/nfc_app_i.h @@ -26,6 +26,7 @@ #include "views/dict_attack.h" #include +#include "helpers/nfc_detected_protocols.h" #include "helpers/nfc_custom_event.h" #include "helpers/mf_ultralight_auth.h" #include "helpers/mf_user_dict.h" @@ -106,9 +107,7 @@ struct NfcApp { FuriString* text_box_store; uint8_t byte_input_store[NFC_BYTE_INPUT_STORE_SIZE]; - uint32_t protocols_detected_num; - NfcProtocol protocols_detected[NfcProtocolNum]; - uint32_t protocols_detected_selected_idx; + NfcDetectedProtocols* detected_protocols; RpcAppSystem* rpc_ctx; NfcRpcState rpc_state; @@ -193,8 +192,4 @@ bool nfc_save_file(NfcApp* instance, FuriString* path); void nfc_make_app_folder(NfcApp* instance); -void nfc_app_set_detected_protocols(NfcApp* instance, const NfcProtocol* types, uint32_t count); - -void nfc_app_reset_detected_protocols(NfcApp* instance); - void nfc_append_filename_string_when_present(NfcApp* instance, FuriString* string); diff --git a/applications/main/nfc/scenes/nfc_scene_detect.c b/applications/main/nfc/scenes/nfc_scene_detect.c index 3ef153657..7ef3f9d87 100644 --- a/applications/main/nfc/scenes/nfc_scene_detect.c +++ b/applications/main/nfc/scenes/nfc_scene_detect.c @@ -7,7 +7,8 @@ void nfc_scene_detect_scan_callback(NfcScannerEvent event, void* context) { NfcApp* instance = context; if(event.type == NfcScannerEventTypeDetected) { - nfc_app_set_detected_protocols(instance, event.data.protocols, event.data.protocol_num); + nfc_detected_protocols_set( + instance->detected_protocols, event.data.protocols, event.data.protocol_num); view_dispatcher_send_custom_event(instance->view_dispatcher, NfcCustomEventWorkerExit); } } @@ -23,7 +24,7 @@ void nfc_scene_detect_on_enter(void* context) { popup_set_icon(instance->popup, 0, 8, &I_NFC_manual_60x50); view_dispatcher_switch_to_view(instance->view_dispatcher, NfcViewPopup); - nfc_app_reset_detected_protocols(instance); + nfc_detected_protocols_reset(instance->detected_protocols); instance->scanner = nfc_scanner_alloc(instance->nfc); nfc_scanner_start(instance->scanner, nfc_scene_detect_scan_callback, instance); @@ -37,7 +38,7 @@ bool nfc_scene_detect_on_event(void* context, SceneManagerEvent event) { if(event.type == SceneManagerEventTypeCustom) { if(event.event == NfcCustomEventWorkerExit) { - if(instance->protocols_detected_num > 1) { + if(nfc_detected_protocols_get_num(instance->detected_protocols) > 1) { notification_message(instance->notifications, &sequence_single_vibro); scene_manager_next_scene(instance->scene_manager, NfcSceneSelectProtocol); } else { diff --git a/applications/main/nfc/scenes/nfc_scene_mf_ultralight_unlock_warn.c b/applications/main/nfc/scenes/nfc_scene_mf_ultralight_unlock_warn.c index 4df8a6289..a0b6986d1 100644 --- a/applications/main/nfc/scenes/nfc_scene_mf_ultralight_unlock_warn.c +++ b/applications/main/nfc/scenes/nfc_scene_mf_ultralight_unlock_warn.c @@ -57,7 +57,8 @@ bool nfc_scene_mf_ultralight_unlock_warn_on_event(void* context, SceneManagerEve if(event.type == SceneManagerEventTypeCustom) { if(event.event == DialogExResultRight) { const NfcProtocol mfu_protocol[] = {NfcProtocolMfUltralight}; - nfc_app_set_detected_protocols(nfc, mfu_protocol, COUNT_OF(mfu_protocol)); + nfc_detected_protocols_set( + nfc->detected_protocols, mfu_protocol, COUNT_OF(mfu_protocol)); scene_manager_next_scene(nfc->scene_manager, NfcSceneRead); dolphin_deed(DolphinDeedNfcRead); consumed = true; @@ -77,7 +78,8 @@ bool nfc_scene_mf_ultralight_unlock_warn_on_event(void* context, SceneManagerEve if(event.type == SceneManagerEventTypeCustom) { if(event.event == DialogExResultCenter) { const NfcProtocol mfu_protocol[] = {NfcProtocolMfUltralight}; - nfc_app_set_detected_protocols(nfc, mfu_protocol, COUNT_OF(mfu_protocol)); + nfc_detected_protocols_set( + nfc->detected_protocols, mfu_protocol, COUNT_OF(mfu_protocol)); scene_manager_next_scene(nfc->scene_manager, NfcSceneRead); dolphin_deed(DolphinDeedNfcRead); consumed = true; diff --git a/applications/main/nfc/scenes/nfc_scene_select_protocol.c b/applications/main/nfc/scenes/nfc_scene_select_protocol.c index af644035e..f2c92b631 100644 --- a/applications/main/nfc/scenes/nfc_scene_select_protocol.c +++ b/applications/main/nfc/scenes/nfc_scene_select_protocol.c @@ -14,21 +14,19 @@ void nfc_scene_select_protocol_on_enter(void* context) { const char* prefix; if(scene_manager_has_previous_scene(instance->scene_manager, NfcSceneExtraActions)) { prefix = "Read"; - instance->protocols_detected_num = NfcProtocolNum; - for(uint32_t i = 0; i < NfcProtocolNum; i++) { - instance->protocols_detected[i] = i; - } + nfc_detected_protocols_fill_all_protocols(instance->detected_protocols); } else { prefix = "Read as"; submenu_set_header(submenu, "Multi-protocol card"); } - for(uint32_t i = 0; i < instance->protocols_detected_num; i++) { + for(uint32_t i = 0; i < nfc_detected_protocols_get_num(instance->detected_protocols); i++) { furi_string_printf( temp_str, "%s %s", prefix, - nfc_device_get_protocol_name(instance->protocols_detected[i])); + nfc_device_get_protocol_name( + nfc_detected_protocols_get_protocol(instance->detected_protocols, i))); furi_string_replace_str(temp_str, "Mifare", "MIFARE"); submenu_add_item( @@ -40,9 +38,8 @@ void nfc_scene_select_protocol_on_enter(void* context) { } furi_string_free(temp_str); - const uint32_t state = - scene_manager_get_scene_state(instance->scene_manager, NfcSceneSelectProtocol); - submenu_set_selected_item(submenu, state); + submenu_set_selected_item( + submenu, nfc_detected_protocols_get_selected_idx(instance->detected_protocols)); view_dispatcher_switch_to_view(instance->view_dispatcher, NfcViewMenu); } @@ -52,10 +49,8 @@ bool nfc_scene_select_protocol_on_event(void* context, SceneManagerEvent event) bool consumed = false; if(event.type == SceneManagerEventTypeCustom) { - instance->protocols_detected_selected_idx = event.event; + nfc_detected_protocols_select(instance->detected_protocols, event.event); scene_manager_next_scene(instance->scene_manager, NfcSceneRead); - scene_manager_set_scene_state( - instance->scene_manager, NfcSceneSelectProtocol, event.event); consumed = true; } else if(event.type == SceneManagerEventTypeBack) { if(scene_manager_has_previous_scene(instance->scene_manager, NfcSceneDetect)) { diff --git a/applications/main/nfc/scenes/nfc_scene_start.c b/applications/main/nfc/scenes/nfc_scene_start.c index e8774b4aa..53857b849 100644 --- a/applications/main/nfc/scenes/nfc_scene_start.c +++ b/applications/main/nfc/scenes/nfc_scene_start.c @@ -25,7 +25,7 @@ void nfc_scene_start_on_enter(void* context) { nfc_device_clear(nfc->nfc_device); iso14443_3a_reset(nfc->iso14443_3a_edit_data); // Reset detected protocols list - nfc_app_reset_detected_protocols(nfc); + nfc_detected_protocols_reset(nfc->detected_protocols); submenu_add_item(submenu, "Read", SubmenuIndexRead, nfc_scene_start_submenu_callback, nfc); submenu_add_item(