From 91e44d5b2f0e895b75eb9f4d2331a8048700de59 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Wed, 26 Jun 2024 21:11:01 +0200 Subject: [PATCH 1/2] NFC: Cache plugin name not full path, saves some RAM --- .../main/nfc/helpers/nfc_supported_cards.c | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/applications/main/nfc/helpers/nfc_supported_cards.c b/applications/main/nfc/helpers/nfc_supported_cards.c index 1e0e7ba6b..04372e2ad 100644 --- a/applications/main/nfc/helpers/nfc_supported_cards.c +++ b/applications/main/nfc/helpers/nfc_supported_cards.c @@ -24,7 +24,7 @@ typedef enum { } NfcSupportedCardsPluginFeature; typedef struct { - FuriString* path; + FuriString* name; NfcProtocol protocol; NfcSupportedCardsPluginFeature feature; } NfcSupportedCardsPluginCache; @@ -73,7 +73,7 @@ void nfc_supported_cards_free(NfcSupportedCards* instance) { !NfcSupportedCardsPluginCache_end_p(iter); NfcSupportedCardsPluginCache_next(iter)) { NfcSupportedCardsPluginCache* plugin_cache = NfcSupportedCardsPluginCache_ref(iter); - furi_string_free(plugin_cache->path); + furi_string_free(plugin_cache->name); } NfcSupportedCardsPluginCache_clear(instance->plugins_cache_arr); @@ -111,16 +111,21 @@ static void nfc_supported_cards_load_context_free(NfcSupportedCardsLoadContext* static const NfcSupportedCardsPlugin* nfc_supported_cards_get_plugin( NfcSupportedCardsLoadContext* instance, - const FuriString* path, + const char* name, const ElfApiInterface* api_interface) { furi_assert(instance); - furi_assert(path); + furi_assert(name); const NfcSupportedCardsPlugin* plugin = NULL; do { if(instance->app) flipper_application_free(instance->app); instance->app = flipper_application_alloc(instance->storage, api_interface); - if(flipper_application_preload(instance->app, furi_string_get_cstr(path)) != + + // Reconstruct path + path_concat(NFC_SUPPORTED_CARDS_PLUGINS_PATH, name, instance->file_path); + furi_string_cat(instance->file_path, NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX); + + if(flipper_application_preload(instance->app, furi_string_get_cstr(instance->file_path)) != FlipperApplicationPreloadStatusSuccess) break; if(!flipper_application_is_plugin(instance->app)) break; @@ -155,9 +160,11 @@ static const NfcSupportedCardsPlugin* nfc_supported_cards_get_next_plugin( if(!furi_string_end_with_str(instance->file_path, NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX)) continue; - path_concat(NFC_SUPPORTED_CARDS_PLUGINS_PATH, instance->file_name, instance->file_path); + size_t trim_suffix = + furi_string_size(instance->file_path) - strlen(NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX); + instance->file_name[trim_suffix] = '\0'; - plugin = nfc_supported_cards_get_plugin(instance, instance->file_path, api_interface); + plugin = nfc_supported_cards_get_plugin(instance, instance->file_name, api_interface); } while(plugin == NULL); //-V654 return plugin; @@ -181,7 +188,7 @@ void nfc_supported_cards_load_cache(NfcSupportedCards* instance) { if(plugin == NULL) break; //-V547 NfcSupportedCardsPluginCache plugin_cache = {}; //-V779 - plugin_cache.path = furi_string_alloc_set(instance->load_context->file_path); + plugin_cache.name = furi_string_alloc_set(instance->load_context->file_name); plugin_cache.protocol = plugin->protocol; if(plugin->verify) { plugin_cache.feature |= NfcSupportedCardsPluginFeatureHasVerify; @@ -233,7 +240,7 @@ bool nfc_supported_cards_read(NfcSupportedCards* instance, NfcDevice* device, Nf const ElfApiInterface* api_interface = composite_api_resolver_get(instance->api_resolver); const NfcSupportedCardsPlugin* plugin = nfc_supported_cards_get_plugin( - instance->load_context, plugin_cache->path, api_interface); + instance->load_context, furi_string_get_cstr(plugin_cache->name), api_interface); if(plugin == NULL) continue; if(plugin->verify) { @@ -281,7 +288,7 @@ bool nfc_supported_cards_parse( const ElfApiInterface* api_interface = composite_api_resolver_get(instance->api_resolver); const NfcSupportedCardsPlugin* plugin = nfc_supported_cards_get_plugin( - instance->load_context, plugin_cache->path, api_interface); + instance->load_context, furi_string_get_cstr(plugin_cache->name), api_interface); if(plugin == NULL) continue; if(plugin->parse) { From 7afe6906d0550c5ca90449ef74aec15dc33d6702 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Wed, 3 Jul 2024 23:11:33 +0200 Subject: [PATCH 2/2] Remove file_path FuriString from context Co-authored-by: gornekich --- .../main/nfc/helpers/nfc_supported_cards.c | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/applications/main/nfc/helpers/nfc_supported_cards.c b/applications/main/nfc/helpers/nfc_supported_cards.c index 04372e2ad..dc23dd510 100644 --- a/applications/main/nfc/helpers/nfc_supported_cards.c +++ b/applications/main/nfc/helpers/nfc_supported_cards.c @@ -41,7 +41,6 @@ typedef enum { typedef struct { Storage* storage; File* directory; - FuriString* file_path; char file_name[256]; FlipperApplication* app; } NfcSupportedCardsLoadContext; @@ -86,7 +85,6 @@ static NfcSupportedCardsLoadContext* nfc_supported_cards_load_context_alloc(void instance->storage = furi_record_open(RECORD_STORAGE); instance->directory = storage_file_alloc(instance->storage); - instance->file_path = furi_string_alloc(); if(!storage_dir_open(instance->directory, NFC_SUPPORTED_CARDS_PLUGINS_PATH)) { FURI_LOG_D(TAG, "Failed to open directory: %s", NFC_SUPPORTED_CARDS_PLUGINS_PATH); @@ -100,8 +98,6 @@ static void nfc_supported_cards_load_context_free(NfcSupportedCardsLoadContext* flipper_application_free(instance->app); } - furi_string_free(instance->file_path); - storage_dir_close(instance->directory); storage_file_free(instance->directory); @@ -117,15 +113,13 @@ static const NfcSupportedCardsPlugin* nfc_supported_cards_get_plugin( furi_assert(name); const NfcSupportedCardsPlugin* plugin = NULL; + FuriString* plugin_path = furi_string_alloc_printf( + "%s/%s%s", NFC_SUPPORTED_CARDS_PLUGINS_PATH, name, NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX); do { if(instance->app) flipper_application_free(instance->app); instance->app = flipper_application_alloc(instance->storage, api_interface); - // Reconstruct path - path_concat(NFC_SUPPORTED_CARDS_PLUGINS_PATH, name, instance->file_path); - furi_string_cat(instance->file_path, NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX); - - if(flipper_application_preload(instance->app, furi_string_get_cstr(instance->file_path)) != + if(flipper_application_preload(instance->app, furi_string_get_cstr(plugin_path)) != FlipperApplicationPreloadStatusSuccess) break; if(!flipper_application_is_plugin(instance->app)) break; @@ -141,6 +135,7 @@ static const NfcSupportedCardsPlugin* nfc_supported_cards_get_plugin( plugin = descriptor->entry_point; } while(false); + furi_string_free(plugin_path); return plugin; } @@ -156,13 +151,19 @@ static const NfcSupportedCardsPlugin* nfc_supported_cards_get_next_plugin( instance->directory, NULL, instance->file_name, sizeof(instance->file_name))) break; - furi_string_set(instance->file_path, instance->file_name); - if(!furi_string_end_with_str(instance->file_path, NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX)) - continue; + size_t suffix_len = strlen(NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX); + size_t file_name_len = strlen(instance->file_name); + if(file_name_len <= suffix_len) break; - size_t trim_suffix = - furi_string_size(instance->file_path) - strlen(NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX); - instance->file_name[trim_suffix] = '\0'; + size_t suffix_start_pos = file_name_len - suffix_len; + if(memcmp( + &instance->file_name[suffix_start_pos], + NFC_SUPPORTED_CARDS_PLUGIN_SUFFIX, + suffix_len) != 0) + break; + + // Trim suffix from file_name to save memory. The suffix will be concatenated on plugin load. + instance->file_name[suffix_start_pos] = '\0'; plugin = nfc_supported_cards_get_plugin(instance, instance->file_name, api_interface); } while(plugin == NULL); //-V654