diff --git a/applications/main/subghz/scenes/subghz_scene_receiver_config.c b/applications/main/subghz/scenes/subghz_scene_receiver_config.c index 5fc019f71..affb01a0b 100644 --- a/applications/main/subghz/scenes/subghz_scene_receiver_config.c +++ b/applications/main/subghz/scenes/subghz_scene_receiver_config.c @@ -352,7 +352,7 @@ bool subghz_scene_receiver_config_on_event(void* context, SceneManagerEvent even void subghz_scene_receiver_config_on_exit(void* context) { SubGhz* subghz = context; variable_item_list_reset(subghz->variable_item_list); - SAVE_SUBGHZ_LAST_SETTINGS(&subghz->last_settings); + subghz_last_settings_save(subghz->last_settings); scene_manager_set_scene_state( subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerNoSet); } diff --git a/applications/main/subghz/subghz.c b/applications/main/subghz/subghz.c index 39196fbfc..8b9dd399f 100644 --- a/applications/main/subghz/subghz.c +++ b/applications/main/subghz/subghz.c @@ -177,8 +177,9 @@ SubGhz* subghz_alloc() { subghz_setting_load(subghz->setting, EXT_PATH("subghz/assets/setting_user")); // Load last used values for Read, Read RAW, etc. or default - subghz_last_settings_check_struct(); - LOAD_SUBGHZ_LAST_SETTINGS(&subghz->last_settings); + subghz->last_settings = subghz_last_settings_alloc(); + subghz_last_settings_load( + subghz->last_settings, subghz_setting_get_preset_count(subghz->setting)); #if FURI_DEBUG FURI_LOG_D( TAG, @@ -187,7 +188,6 @@ SubGhz* subghz_alloc() { subghz->last_settings->preset); #endif subghz_setting_set_default_frequency(subghz->setting, subghz->last_settings->frequency); - SAVE_SUBGHZ_LAST_SETTINGS(&subghz->last_settings); //init Worker & Protocol & History & KeyBoard subghz->lock = SubGhzLockOff; @@ -306,6 +306,7 @@ void subghz_free(SubGhz* subghz) { //setting subghz_setting_free(subghz->setting); + subghz_last_settings_free(subghz->last_settings); //Worker & Protocol & History subghz_receiver_free(subghz->txrx->receiver); diff --git a/applications/main/subghz/subghz_last_settings.c b/applications/main/subghz/subghz_last_settings.c index cfc7b4e2e..764c78fea 100644 --- a/applications/main/subghz/subghz_last_settings.c +++ b/applications/main/subghz/subghz_last_settings.c @@ -1,19 +1,105 @@ #include "subghz_last_settings.h" +#include +#include +#include #define TAG "SubGhzLastSettings" +#define SUBGHZ_LAST_SETTING_FILE_TYPE "Flipper SubGhz Last Setting File" +#define SUBGHZ_LAST_SETTING_FILE_VERSION 1 +#define SUBGHZ_LAST_SETTINGS_PATH EXT_PATH(".subghz.settings") // 1 = "AM650" // "AM270", "AM650", "FM238", "FM476", #define SUBGHZ_LAST_SETTING_DEFAULT_PRESET 1 #define SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY 433920000 -void subghz_last_settings_check_struct(void) { +SubGhzLastSettings* subghz_last_settings_alloc(void) { + SubGhzLastSettings* instance = malloc(sizeof(SubGhzLastSettings)); + return instance; +} + +void subghz_last_settings_free(SubGhzLastSettings* instance) { + furi_assert(instance); + free(instance); +} + +void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count) { + furi_assert(instance); +#if FURI_DEBUG + FURI_LOG_I(TAG, "subghz_last_settings_load"); +#endif + Storage* storage = furi_record_open(RECORD_STORAGE); - if(!storage_file_exists(storage, SUBGHZ_LAST_SETTINGS_PATH)) { - SubGhzLastSettings* instance = malloc(sizeof(SubGhzLastSettings)); + FlipperFormat* fff_data_file = flipper_format_file_alloc(storage); + + uint32_t temp_frequency = 0; + int32_t temp_preset = 0; + + if(FSE_OK == storage_sd_status(storage) && SUBGHZ_LAST_SETTINGS_PATH && + flipper_format_file_open_existing(fff_data_file, SUBGHZ_LAST_SETTINGS_PATH)) { + flipper_format_read_int32(fff_data_file, "Preset", (int32_t*)&temp_preset, 1); + flipper_format_read_uint32(fff_data_file, "Frequency", (uint32_t*)&temp_frequency, 1); + } else { + FURI_LOG_E(TAG, "Error open file %s", SUBGHZ_LAST_SETTINGS_PATH); + } + + if(temp_frequency == 0 || !furi_hal_subghz_is_tx_allowed(temp_frequency)) { + FURI_LOG_W(TAG, "Last used frequency not found or can't be used!"); instance->frequency = SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY; instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET; - SAVE_SUBGHZ_LAST_SETTINGS(&instance); + } else { + instance->frequency = temp_frequency; + + if(temp_preset > (int32_t)preset_count - 1 || temp_preset < 0) { + FURI_LOG_W(TAG, "Last used preset no found"); + instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET; + } else { + instance->preset = temp_preset; + } } + + flipper_format_free(fff_data_file); furi_record_close(RECORD_STORAGE); +} + +bool subghz_last_settings_save(SubGhzLastSettings* instance) { + furi_assert(instance); +#if FURI_DEBUG + FURI_LOG_I(TAG, "subghz_last_settings_save"); +#endif + + bool saved = false; + Storage* storage = furi_record_open(RECORD_STORAGE); + FlipperFormat* file = flipper_format_file_alloc(storage); + + do { + if(FSE_OK != storage_sd_status(storage)) { + break; + } + + // Open file + if(!flipper_format_file_open_always(file, SUBGHZ_LAST_SETTINGS_PATH)) break; + + // Write header + if(!flipper_format_write_header_cstr( + file, SUBGHZ_LAST_SETTING_FILE_TYPE, SUBGHZ_LAST_SETTING_FILE_VERSION)) + break; + + if(!flipper_format_insert_or_update_int32(file, "Preset", &instance->preset, 1)) { + break; + } + if(!flipper_format_insert_or_update_uint32(file, "Frequency", &instance->frequency, 1)) { + break; + } + saved = true; + } while(0); + + if(!saved) { + FURI_LOG_E(TAG, "Error save file %s", SUBGHZ_LAST_SETTINGS_PATH); + } + + flipper_format_free(file); + furi_record_close(RECORD_STORAGE); + + return saved; } \ No newline at end of file diff --git a/applications/main/subghz/subghz_last_settings.h b/applications/main/subghz/subghz_last_settings.h index 27e67e252..8067bb66a 100644 --- a/applications/main/subghz/subghz_last_settings.h +++ b/applications/main/subghz/subghz_last_settings.h @@ -1,36 +1,19 @@ #pragma once -#include "subghz_last_settings_filename.h" - #include #include #include -#include #include -#define SUBGHZ_LAST_SETTINGS_VER (1) -#define SUBGHZ_LAST_SETTINGS_PATH EXT_PATH(SUBGHZ_LAST_SETTINGS_FILE_NAME) -#define SUBGHZ_LAST_SETTINGS_MAGIC (0xCC) - -#define SAVE_SUBGHZ_LAST_SETTINGS(x) \ - saved_struct_save( \ - SUBGHZ_LAST_SETTINGS_PATH, \ - (x), \ - sizeof(SubGhzLastSettings), \ - SUBGHZ_LAST_SETTINGS_MAGIC, \ - SUBGHZ_LAST_SETTINGS_VER) - -#define LOAD_SUBGHZ_LAST_SETTINGS(x) \ - saved_struct_load( \ - SUBGHZ_LAST_SETTINGS_PATH, \ - (x), \ - sizeof(SubGhzLastSettings), \ - SUBGHZ_LAST_SETTINGS_MAGIC, \ - SUBGHZ_LAST_SETTINGS_VER) - typedef struct { uint32_t frequency; - uint8_t preset; + int32_t preset; } SubGhzLastSettings; -void subghz_last_settings_check_struct(void); \ No newline at end of file +SubGhzLastSettings* subghz_last_settings_alloc(void); + +void subghz_last_settings_free(SubGhzLastSettings* instance); + +void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count); + +bool subghz_last_settings_save(SubGhzLastSettings* instance); \ No newline at end of file diff --git a/applications/main/subghz/subghz_last_settings_filename.h b/applications/main/subghz/subghz_last_settings_filename.h deleted file mode 100644 index 3f34262c3..000000000 --- a/applications/main/subghz/subghz_last_settings_filename.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#define SUBGHZ_LAST_SETTINGS_FILE_NAME ".subghz.settings" \ No newline at end of file