Skip to content

Commit

Permalink
NFC: Cache plugin name not full path, saves some RAM [ci skip]
Browse files Browse the repository at this point in the history
by Willy-JL
  • Loading branch information
xMasterX committed Jul 4, 2024
1 parent bd6b216 commit 223de97
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions applications/main/nfc/helpers/nfc_supported_cards.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef enum {
} NfcSupportedCardsPluginFeature;

typedef struct {
FuriString* path;
FuriString* name;
NfcProtocol protocol;
NfcSupportedCardsPluginFeature feature;
} NfcSupportedCardsPluginCache;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 223de97

Please sign in to comment.