2022-09-27 22:01:09 +00:00
|
|
|
#include "subghz_last_settings.h"
|
2022-10-01 04:39:51 +00:00
|
|
|
#include "subghz_i.h"
|
2022-09-27 22:01:09 +00:00
|
|
|
|
|
|
|
#define TAG "SubGhzLastSettings"
|
|
|
|
|
2022-09-28 01:49:06 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FILE_TYPE "Flipper SubGhz Last Setting File"
|
2024-04-05 03:36:35 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FILE_VERSION 3
|
2022-09-28 03:46:45 +00:00
|
|
|
#define SUBGHZ_LAST_SETTINGS_PATH EXT_PATH("subghz/assets/last_subghz.settings")
|
2022-10-01 04:39:51 +00:00
|
|
|
|
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY "Frequency"
|
2023-08-08 04:58:20 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_PRESET "Preset" // AKA Modulation
|
2022-10-11 14:45:46 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_FEEDBACK_LEVEL "FeedbackLevel"
|
2022-11-16 21:37:35 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER "FATrigger"
|
2024-04-05 03:36:35 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_PROTOCOL_FILE_NAMES "ProtocolNames"
|
2023-08-04 14:52:16 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_HOPPING_ENABLE "Hopping"
|
2023-08-08 04:58:20 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_IGNORE_FILTER "IgnoreFilter"
|
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FILTER "Filter"
|
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_RSSI_THRESHOLD "RSSI"
|
2023-12-24 00:08:24 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_DELETE_OLD "DelOldSignals"
|
2022-09-27 22:01:09 +00:00
|
|
|
|
2022-09-28 01:49:06 +00:00
|
|
|
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);
|
|
|
|
|
2024-04-05 03:36:35 +00:00
|
|
|
// Default values (all others set to 0, if read from file fails these are used)
|
|
|
|
instance->frequency = SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY;
|
|
|
|
instance->preset_index = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
|
|
|
|
instance->frequency_analyzer_feedback_level =
|
|
|
|
SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_FEEDBACK_LEVEL;
|
|
|
|
instance->frequency_analyzer_trigger = SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_TRIGGER;
|
|
|
|
// See bin_raw_value in scenes/subghz_scene_receiver_config.c
|
|
|
|
instance->filter = SubGhzProtocolFlag_Decodable;
|
|
|
|
instance->rssi = SUBGHZ_RAW_THRESHOLD_MIN;
|
|
|
|
|
2022-09-27 22:01:09 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-09-28 01:49:06 +00:00
|
|
|
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
|
|
|
|
|
2024-04-05 01:02:33 +00:00
|
|
|
FuriString* temp_str = furi_string_alloc();
|
|
|
|
uint32_t config_version = 0;
|
|
|
|
|
2024-04-05 03:36:35 +00:00
|
|
|
if(FSE_OK == storage_sd_status(storage) &&
|
2022-09-28 01:49:06 +00:00
|
|
|
flipper_format_file_open_existing(fff_data_file, SUBGHZ_LAST_SETTINGS_PATH)) {
|
2024-04-05 01:02:33 +00:00
|
|
|
do {
|
|
|
|
if(!flipper_format_read_header(fff_data_file, temp_str, &config_version)) break;
|
|
|
|
if((strcmp(furi_string_get_cstr(temp_str), SUBGHZ_LAST_SETTING_FILE_TYPE) != 0) ||
|
|
|
|
(config_version != SUBGHZ_LAST_SETTING_FILE_VERSION)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_FREQUENCY, &instance->frequency, 1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_PRESET, &instance->preset_index, 1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_FEEDBACK_LEVEL,
|
|
|
|
&instance->frequency_analyzer_feedback_level,
|
|
|
|
1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_float(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER,
|
|
|
|
&instance->frequency_analyzer_trigger,
|
|
|
|
1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_bool(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_PROTOCOL_FILE_NAMES,
|
|
|
|
&instance->protocol_file_names,
|
|
|
|
1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_bool(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_HOPPING_ENABLE,
|
|
|
|
&instance->enable_hopping,
|
|
|
|
1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_IGNORE_FILTER,
|
|
|
|
&instance->ignore_filter,
|
|
|
|
1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_FILTER, &instance->filter, 1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_float(
|
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_RSSI_THRESHOLD, &instance->rssi, 1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_bool(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_DELETE_OLD,
|
|
|
|
&instance->delete_old_signals,
|
|
|
|
1)) {
|
|
|
|
flipper_format_rewind(fff_data_file);
|
|
|
|
}
|
|
|
|
|
2024-04-05 01:02:33 +00:00
|
|
|
} while(0);
|
2022-09-28 01:49:06 +00:00
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Error open file %s", SUBGHZ_LAST_SETTINGS_PATH);
|
|
|
|
}
|
|
|
|
|
2024-04-05 01:02:33 +00:00
|
|
|
furi_string_free(temp_str);
|
|
|
|
|
2024-04-05 03:36:35 +00:00
|
|
|
flipper_format_file_close(fff_data_file);
|
|
|
|
flipper_format_free(fff_data_file);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
2023-08-08 04:58:20 +00:00
|
|
|
|
2024-04-05 03:36:35 +00:00
|
|
|
if(instance->frequency == 0 || !furi_hal_subghz_is_tx_allowed(instance->frequency)) {
|
2022-09-27 22:01:09 +00:00
|
|
|
instance->frequency = SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY;
|
|
|
|
}
|
2022-09-28 01:49:06 +00:00
|
|
|
|
2024-04-05 03:36:35 +00:00
|
|
|
if(instance->preset_index > (uint32_t)preset_count - 1) {
|
|
|
|
instance->preset_index = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
|
|
|
|
}
|
2022-09-28 01:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_last_settings_save(SubGhzLastSettings* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
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;
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_uint32(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_FREQUENCY, &instance->frequency, 1)) {
|
2022-10-01 04:39:51 +00:00
|
|
|
break;
|
2023-08-08 04:58:20 +00:00
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_uint32(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_PRESET, &instance->preset_index, 1)) {
|
2022-09-28 01:49:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-12 02:15:45 +00:00
|
|
|
file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_FEEDBACK_LEVEL,
|
|
|
|
&instance->frequency_analyzer_feedback_level,
|
|
|
|
1)) {
|
2022-10-11 14:45:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_float(
|
2022-11-16 21:37:35 +00:00
|
|
|
file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER,
|
|
|
|
&instance->frequency_analyzer_trigger,
|
|
|
|
1)) {
|
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_bool(
|
2023-07-27 15:25:32 +00:00
|
|
|
file,
|
2024-04-05 03:36:35 +00:00
|
|
|
SUBGHZ_LAST_SETTING_FIELD_PROTOCOL_FILE_NAMES,
|
|
|
|
&instance->protocol_file_names,
|
2023-07-27 15:25:32 +00:00
|
|
|
1)) {
|
|
|
|
break;
|
2023-03-16 13:03:22 +00:00
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_bool(
|
2023-08-04 14:52:16 +00:00
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_HOPPING_ENABLE, &instance->enable_hopping, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_uint32(
|
2023-08-08 04:58:20 +00:00
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_IGNORE_FILTER, &instance->ignore_filter, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_uint32(
|
2023-08-08 04:58:20 +00:00
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_FILTER, &instance->filter, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
if(!flipper_format_write_float(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_RSSI_THRESHOLD, &instance->rssi, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_write_bool(
|
2023-12-24 00:08:24 +00:00
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_DELETE_OLD, &instance->delete_old_signals, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
2024-04-05 03:36:35 +00:00
|
|
|
|
2022-09-28 01:49:06 +00:00
|
|
|
saved = true;
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
if(!saved) {
|
|
|
|
FURI_LOG_E(TAG, "Error save file %s", SUBGHZ_LAST_SETTINGS_PATH);
|
|
|
|
}
|
|
|
|
|
2022-09-28 03:51:13 +00:00
|
|
|
flipper_format_file_close(file);
|
2022-09-28 01:49:06 +00:00
|
|
|
flipper_format_free(file);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
|
|
|
|
return saved;
|
2022-10-01 04:39:51 +00:00
|
|
|
}
|