Fixed all bugs with saving directly to file, also fixed misspeled if/ifdef in all app

This commit is contained in:
derskythe 2022-10-02 03:18:30 +04:00
parent 230f09dddd
commit bbd3f9cf71
8 changed files with 143 additions and 119 deletions

View file

@ -86,3 +86,5 @@ typedef enum {
SubGhzViewReceiverModeLive, SubGhzViewReceiverModeLive,
SubGhzViewReceiverModeFile, SubGhzViewReceiverModeFile,
} SubGhzViewReceiverMode; } SubGhzViewReceiverMode;
#define SUBGHZ_HISTORY_REMOVE_SAVED_ITEMS 1

View file

@ -31,7 +31,7 @@ bool subghz_scene_frequency_analyzer_on_event(void* context, SceneManagerEvent e
return true; return true;
} else if(event.event == SubGhzCustomEventViewReceiverUnlock) { } else if(event.event == SubGhzCustomEventViewReceiverUnlock) {
// Don't need to save, we already saved on short event // Don't need to save, we already saved on short event
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_W(TAG, "Goto next scene!"); FURI_LOG_W(TAG, "Goto next scene!");
#endif #endif
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiver); scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiver);

View file

@ -185,6 +185,7 @@ bool subghz_scene_receiver_on_event(void* context, SceneManagerEvent event) {
consumed = true; consumed = true;
break; break;
case SubGhzCustomEventViewReceiverOK: case SubGhzCustomEventViewReceiverOK:
// Show file info, scene: receiver_info
subghz->txrx->idx_menu_chosen = subghz->txrx->idx_menu_chosen =
subghz_view_receiver_get_idx_menu(subghz->subghz_receiver); subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverInfo); scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverInfo);

View file

@ -222,10 +222,10 @@ void subghz_scene_receiver_config_on_enter(void* context) {
VariableItem* item; VariableItem* item;
uint8_t value_index; uint8_t value_index;
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_D( FURI_LOG_D(
TAG, TAG,
"last frequency: %d, preset: %d", "Last frequency: %d, Preset: %d",
subghz->last_settings->frequency, subghz->last_settings->frequency,
subghz->last_settings->preset); subghz->last_settings->preset);
#endif #endif

View file

@ -180,10 +180,10 @@ SubGhz* subghz_alloc() {
subghz->last_settings = subghz_last_settings_alloc(); subghz->last_settings = subghz_last_settings_alloc();
subghz_last_settings_load( subghz_last_settings_load(
subghz->last_settings, subghz_setting_get_preset_count(subghz->setting)); subghz->last_settings, subghz_setting_get_preset_count(subghz->setting));
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_D( FURI_LOG_D(
TAG, TAG,
"last frequency: %d, preset: %d, detect_raw: %d", "Last frequency: %d, Preset: %d, Detect_RAW: %d",
subghz->last_settings->frequency, subghz->last_settings->frequency,
subghz->last_settings->preset, subghz->last_settings->preset,
subghz->last_settings->detect_raw); subghz->last_settings->detect_raw);

View file

@ -3,9 +3,15 @@
#include <flipper_format/flipper_format_i.h> #include <flipper_format/flipper_format_i.h>
#define SUBGHZ_HISTORY_MAX 65 #define SUBGHZ_HISTORY_MAX 65
/**
* @brief Settings for temporary files
*
*/
#define SUBGHZ_HISTORY_TMP_DIR EXT_PATH("subghz/tmp_history") #define SUBGHZ_HISTORY_TMP_DIR EXT_PATH("subghz/tmp_history")
#define SUBGHZ_HISTORY_TMP_EXTENSION ".tmp" #define SUBGHZ_HISTORY_TMP_EXTENSION ".tmp"
#define SUBGHZ_HISTORY_TMP_FILE_KEY "Filename" #define SUBGHZ_HISTORY_TMP_FILE_KEY "Filename"
#define TAG "SubGhzHistory" #define TAG "SubGhzHistory"
typedef struct { typedef struct {
@ -35,14 +41,14 @@ struct SubGhzHistory {
SubGhzHistoryStruct* history; SubGhzHistoryStruct* history;
}; };
#if FURI_DEBUG #ifdef FURI_DEBUG
#define LOG_DELAY 1 #define LOG_DELAY 0
#endif #endif
/** /**
* @brief Generate filename like 000.tmp * @brief Generate filename like 000.tmp
* *
* @param filename * @param filename - input parameter
* @param index - index of file, timestamp doesn't accepted! * @param index - index of file, timestamp doesn't accepted!
*/ */
void subghz_history_generate_temp_filename(string_t filename, uint32_t index) { void subghz_history_generate_temp_filename(string_t filename, uint32_t index) {
@ -52,56 +58,98 @@ void subghz_history_generate_temp_filename(string_t filename, uint32_t index) {
} }
/** /**
* @brief Check SD card, recursive delete dir and files and create new dir * @brief Check if directory for temporary files is exists
* *
* @param instance * @param instance SubGhzHistory*
* @param only_remove_dir * @return true
* @return false
*/
bool subghz_history_is_tmp_dir_exists(SubGhzHistory* instance) {
FileInfo file_info;
storage_common_stat(instance->storage, SUBGHZ_HISTORY_TMP_DIR, &file_info);
if(storage_common_stat(instance->storage, SUBGHZ_HISTORY_TMP_DIR, &file_info) == FSE_OK) {
if(file_info.flags & FSF_DIRECTORY) {
return true;
}
}
return false;
}
/**
* @brief Check SD card and create temporary dir if not exists,
* Result write_tmp_files without this unstable work is GUARANTEED
*
* @param instance - SubGhzHistory*
* @return - true all ok
* @return - false we have a problems
*/
bool subghz_history_check_sdcard(SubGhzHistory* instance) {
#ifdef FURI_DEBUG
FURI_LOG_I(TAG, "check_sdcard");
uint32_t start_time = furi_get_tick();
#endif
bool result = false;
// Stage 0 - check SD Card
FS_Error status = storage_sd_status(instance->storage);
if(status == FSE_OK) {
result = subghz_history_is_tmp_dir_exists(instance);
if(!subghz_history_is_tmp_dir_exists(instance)) {
result = storage_simply_mkdir(instance->storage, SUBGHZ_HISTORY_TMP_DIR);
}
} else {
FURI_LOG_W(TAG, "SD storage not installed! Status: %d", status);
}
#ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Running time (check_sdcard): %d ms", furi_get_tick() - start_time);
#endif
return result;
}
/**
* @brief Recursive delete dir and files and create new temp dir
*
* @param instance - SubGhzHistory*
* @return true - if all ok * @return true - if all ok
* @return false - if something failed * @return false - if something failed
*/ */
bool subghz_history_clear_dir_or_create(SubGhzHistory* instance, bool only_remove_dir) { void subghz_history_clear_tmp_dir(SubGhzHistory* instance) {
#if FURI_DEBUG furi_assert(instance);
FURI_LOG_D(TAG, "subghz_history_clear_dir_or_create: %s", only_remove_dir ? "true" : "false"); #ifdef FURI_DEBUG
furi_delay_ms(LOG_DELAY); FURI_LOG_I(TAG, "clear_tmp_dir");
#endif #endif
// Stage 0 - SD installed? if(!instance->write_tmp_files) {
FS_Error status = storage_sd_status(instance->storage); // Nothing to do here!
if(status != FSE_OK) { return;
FURI_LOG_W(TAG, "SD storage not installed! Status: %d", status); }
return false; uint32_t start_time = furi_get_tick();
// Stage 0 - Dir exists?
bool res = subghz_history_is_tmp_dir_exists(instance);
if(res) {
// Stage 1 - delete all content if exists
FileInfo fileinfo;
storage_common_stat(instance->storage, SUBGHZ_HISTORY_TMP_DIR, &fileinfo);
if(fileinfo.flags & FSF_DIRECTORY) {
res = storage_simply_remove_recursive(instance->storage, SUBGHZ_HISTORY_TMP_DIR);
} else {
res = (storage_common_remove(instance->storage, SUBGHZ_HISTORY_TMP_DIR) == FSE_OK);
}
} }
// Stage 1 - delete all content if exists // Stage 2 - create dir if necessary
FileInfo fileinfo; res = !storage_simply_mkdir(instance->storage, SUBGHZ_HISTORY_TMP_DIR);
storage_common_stat(instance->storage, SUBGHZ_HISTORY_TMP_DIR, &fileinfo); if(!res) {
FURI_LOG_E(TAG, "Cannot process temp dir!");
// This is temp
bool res = false; //instance->write_tmp_files = true;
// Uncomment it
if(fileinfo.flags & FSF_DIRECTORY) {
res = storage_simply_remove_recursive(instance->storage, SUBGHZ_HISTORY_TMP_DIR);
} else {
res = (storage_common_remove(instance->storage, SUBGHZ_HISTORY_TMP_DIR) == FSE_OK);
} }
#if FURI_DEBUG uint32_t stop_time = furi_get_tick() - start_time;
FURI_LOG_D(TAG, "storage_common_remove done: %s", res ? "true" : "false"); FURI_LOG_I(TAG, "Running time (clear_tmp_dir): %d ms", stop_time);
furi_delay_ms(LOG_DELAY);
#endif
// Uncomment it
// Stage 2 - create dir
if(!only_remove_dir && res) {
res = storage_simply_mkdir(instance->storage, SUBGHZ_HISTORY_TMP_DIR);
#if FURI_DEBUG
FURI_LOG_D(TAG, "storage_simply_mkdir done: %s", res ? "true" : "false");
furi_delay_ms(LOG_DELAY);
#endif
}
return res;
} }
SubGhzHistory* subghz_history_alloc(void) { SubGhzHistory* subghz_history_alloc(void) {
@ -110,18 +158,35 @@ SubGhzHistory* subghz_history_alloc(void) {
instance->history = malloc(sizeof(SubGhzHistoryStruct)); instance->history = malloc(sizeof(SubGhzHistoryStruct));
SubGhzHistoryItemArray_init(instance->history->data); SubGhzHistoryItemArray_init(instance->history->data);
instance->storage = furi_record_open(RECORD_STORAGE); instance->storage = furi_record_open(RECORD_STORAGE);
instance->write_tmp_files = false; instance->write_tmp_files = subghz_history_check_sdcard(instance);
#if FURI_DEBUG if(!instance->write_tmp_files) {
FURI_LOG_D(TAG, "BEFORE subghz_history_clear_dir_or_create"); FURI_LOG_E(TAG, "Unstable work! Cannot use SD Card!");
furi_delay_ms(LOG_DELAY); }
#endif
// Check if we can write files on SD
instance->write_tmp_files = subghz_history_clear_dir_or_create(instance, false);
return instance; return instance;
} }
/**
* @brief Free item and free all resources
*
* @param item - SubGhzHistoryItem*
*/
void subghz_history_item_free(SubGhzHistoryItem* item) {
furi_assert(item);
string_clear(item->item_str);
string_clear(item->preset->name);
string_clear(item->protocol_name);
free(item->preset);
item->type = 0;
item->is_file = false;
if(item->flipper_string != NULL) {
flipper_format_free(item->flipper_string);
}
}
/** /**
* @brief free all items in array * @brief free all items in array
* *
@ -130,17 +195,7 @@ SubGhzHistory* subghz_history_alloc(void) {
void subghz_history_clean_item_array(SubGhzHistory* instance) { void subghz_history_clean_item_array(SubGhzHistory* instance) {
for for
M_EACH(item, instance->history->data, SubGhzHistoryItemArray_t) { M_EACH(item, instance->history->data, SubGhzHistoryItemArray_t) {
string_clear(item->item_str); subghz_history_item_free(item);
string_clear(item->preset->name);
string_clear(item->protocol_name);
free(item->preset);
item->type = 0;
item->is_file = false;
if(item->flipper_string != NULL) {
flipper_format_free(item->flipper_string);
}
} }
} }
@ -152,9 +207,8 @@ void subghz_history_free(SubGhzHistory* instance) {
SubGhzHistoryItemArray_clear(instance->history->data); SubGhzHistoryItemArray_clear(instance->history->data);
free(instance->history); free(instance->history);
if(instance->write_tmp_files) { // Delete all temporary file, on exit it's ok
instance->write_tmp_files = subghz_history_clear_dir_or_create(instance, true); subghz_history_clear_tmp_dir(instance);
}
furi_record_close(RECORD_STORAGE); furi_record_close(RECORD_STORAGE);
@ -188,10 +242,6 @@ void subghz_history_reset(SubGhzHistory* instance) {
SubGhzHistoryItemArray_reset(instance->history->data); SubGhzHistoryItemArray_reset(instance->history->data);
instance->last_index_write = 0; instance->last_index_write = 0;
instance->code_last_hash_data = 0; instance->code_last_hash_data = 0;
if(instance->write_tmp_files) {
instance->write_tmp_files = subghz_history_clear_dir_or_create(instance, false);
}
} }
uint16_t subghz_history_get_item(SubGhzHistory* instance) { uint16_t subghz_history_get_item(SubGhzHistory* instance) {
@ -221,22 +271,16 @@ FlipperFormat* subghz_history_get_raw_data(SubGhzHistory* instance, uint16_t idx
bool result_ok = false; bool result_ok = false;
if(instance->write_tmp_files && item->is_file) { if(instance->write_tmp_files && item->is_file) {
// We have files! // We have files!
#if FURI_DEBUG
FURI_LOG_D(TAG, "We have files!");
furi_delay_ms(LOG_DELAY);
#endif
string_t filename; string_t filename;
string_t dir_path; string_t dir_path;
string_init(filename); string_init(filename);
string_init(dir_path); string_init(dir_path);
subghz_history_generate_temp_filename(filename, idx); subghz_history_generate_temp_filename(filename, idx);
string_cat_printf( string_init_printf(
dir_path, "%s/%s", SUBGHZ_HISTORY_TMP_DIR, string_get_cstr(filename)); dir_path, "%s/%s", SUBGHZ_HISTORY_TMP_DIR, string_get_cstr(filename));
// string_init_printf(
// dir_path, "%s/%s", SUBGHZ_HISTORY_TMP_DIR, string_get_cstr(filename));
if(storage_file_exists(instance->storage, string_get_cstr(dir_path))) { if(storage_file_exists(instance->storage, string_get_cstr(dir_path))) {
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Exist: %s", dir_path); FURI_LOG_D(TAG, "Exist: %s", dir_path);
furi_delay_ms(LOG_DELAY); furi_delay_ms(LOG_DELAY);
#endif #endif
@ -248,7 +292,7 @@ FlipperFormat* subghz_history_get_raw_data(SubGhzHistory* instance, uint16_t idx
size_t size = stream_load_from_file( size_t size = stream_load_from_file(
dst_stream, instance->storage, string_get_cstr(dir_path)); dst_stream, instance->storage, string_get_cstr(dir_path));
if(size > 0) { if(size > 0) {
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Save ok!"); FURI_LOG_I(TAG, "Save ok!");
furi_delay_ms(LOG_DELAY); furi_delay_ms(LOG_DELAY);
#endif #endif
@ -267,7 +311,7 @@ FlipperFormat* subghz_history_get_raw_data(SubGhzHistory* instance, uint16_t idx
string_clear(filename); string_clear(filename);
string_clear(dir_path); string_clear(dir_path);
} else { } else {
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_W(TAG, "Write TMP files failed!"); FURI_LOG_W(TAG, "Write TMP files failed!");
furi_delay_ms(LOG_DELAY); furi_delay_ms(LOG_DELAY);
#endif #endif
@ -314,7 +358,6 @@ bool subghz_history_add_to_history(
instance->code_last_hash_data = subghz_protocol_decoder_base_get_hash_data(decoder_base); instance->code_last_hash_data = subghz_protocol_decoder_base_get_hash_data(decoder_base);
instance->last_update_timestamp = furi_get_tick(); instance->last_update_timestamp = furi_get_tick();
string_t text; string_t text;
string_init(text); string_init(text);
SubGhzHistoryItem* item = SubGhzHistoryItemArray_push_raw(instance->history->data); SubGhzHistoryItem* item = SubGhzHistoryItemArray_push_raw(instance->history->data);
@ -343,9 +386,6 @@ bool subghz_history_add_to_history(
break; break;
} else { } else {
string_init_printf(item->protocol_name, "%s", string_get_cstr(instance->tmp_string)); string_init_printf(item->protocol_name, "%s", string_get_cstr(instance->tmp_string));
#if FURI_DEBUG
FURI_LOG_I(TAG, "File protocol: %s", string_get_cstr(item->protocol_name));
#endif
} }
if(!strcmp(string_get_cstr(instance->tmp_string), "RAW")) { if(!strcmp(string_get_cstr(instance->tmp_string), "RAW")) {
string_printf( string_printf(
@ -403,17 +443,7 @@ bool subghz_history_add_to_history(
} }
} while(false); } while(false);
// Copy streams
// Thinking that some data may be saved
// Stream* src = flipper_format_get_raw_stream(flipper_string);
// stream_seek(src, 0, StreamOffsetFromStart);
// Stream* dst = string_stream_alloc();
// stream_clean(dst);
// stream_copy_full(src, dst);
// If we can write to files // If we can write to files
//bool no_close = false;
if(instance->write_tmp_files) { if(instance->write_tmp_files) {
string_t filename; string_t filename;
string_t dir_path; string_t dir_path;
@ -422,21 +452,16 @@ bool subghz_history_add_to_history(
subghz_history_generate_temp_filename(filename, instance->last_index_write); subghz_history_generate_temp_filename(filename, instance->last_index_write);
string_cat_printf(dir_path, "%s/%s", SUBGHZ_HISTORY_TMP_DIR, string_get_cstr(filename)); string_cat_printf(dir_path, "%s/%s", SUBGHZ_HISTORY_TMP_DIR, string_get_cstr(filename));
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Let's do some hack. Create file %s", string_get_cstr(dir_path)); FURI_LOG_I(TAG, "Save temp file: %s", string_get_cstr(dir_path));
#endif #endif
Stream* dst = flipper_format_get_raw_stream(item->flipper_string); Stream* dst = flipper_format_get_raw_stream(item->flipper_string);
stream_rewind(dst); stream_rewind(dst);
// stream_seek(dst, 0, StreamOffsetFromStart);
if(stream_save_to_file( if(stream_save_to_file(
dst, instance->storage, string_get_cstr(dir_path), FSOM_CREATE_ALWAYS) > 0) { dst, instance->storage, string_get_cstr(dir_path), FSOM_CREATE_ALWAYS) > 0) {
// Free flipper_format
//flipper_format_free(flipper_string);
//flipper_string = NULL;
//stream_free(dst);
flipper_format_free(item->flipper_string); flipper_format_free(item->flipper_string);
item->flipper_string = NULL; item->flipper_string = NULL;
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Save done!"); FURI_LOG_I(TAG, "Save done!");
#endif #endif
// This item contains fake data to load from SD // This item contains fake data to load from SD
@ -447,18 +472,14 @@ bool subghz_history_add_to_history(
string_clear(filename); string_clear(filename);
string_clear(dir_path); string_clear(dir_path);
/* }*/
} else { } else {
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Old fashion way"); FURI_LOG_W(TAG, "Old fashion way");
#endif #endif
// Old fashion way
//item->flipper_string = dst;
} }
//flipper_format_free(flipper_string);
string_clear(text); string_clear(text);
instance->last_index_write++; instance->last_index_write++;
return true; return true;
} }

View file

@ -29,7 +29,7 @@ void subghz_last_settings_free(SubGhzLastSettings* instance) {
void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count) { void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count) {
furi_assert(instance); furi_assert(instance);
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "subghz_last_settings_load"); FURI_LOG_I(TAG, "subghz_last_settings_load");
#endif #endif
@ -76,8 +76,8 @@ void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count
bool subghz_last_settings_save(SubGhzLastSettings* instance) { bool subghz_last_settings_save(SubGhzLastSettings* instance) {
furi_assert(instance); furi_assert(instance);
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "subghz_last_settings_save"); FURI_LOG_I(TAG, "last_settings_save");
#endif #endif
bool saved = false; bool saved = false;

View file

@ -194,7 +194,7 @@ uint32_t subghz_frequency_find_correct(uint32_t input) {
uint32_t prev_freq = 0; uint32_t prev_freq = 0;
uint32_t current = 0; uint32_t current = 0;
uint32_t result = 0; uint32_t result = 0;
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_D(TAG, "input: %d", input); FURI_LOG_D(TAG, "input: %d", input);
#endif #endif
for(size_t i = 0; i < sizeof(subghz_frequency_list); i++) { for(size_t i = 0; i < sizeof(subghz_frequency_list); i++) {
@ -274,7 +274,7 @@ bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
frequency_candidate = subghz_frequency_find_correct(frequency_candidate); frequency_candidate = subghz_frequency_find_correct(frequency_candidate);
} }
if(frequency_candidate > 0 && frequency_candidate != model->frequency_to_save) { if(frequency_candidate > 0 && frequency_candidate != model->frequency_to_save) {
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_D( FURI_LOG_D(
TAG, TAG,
"frequency_to_save: %d, candidate: %d", "frequency_to_save: %d, candidate: %d",
@ -289,7 +289,7 @@ bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
return true; return true;
}); });
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I( FURI_LOG_I(
TAG, TAG,
"updated: %d, long: %d, type: %d", "updated: %d, long: %d, type: %d",
@ -304,7 +304,7 @@ bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
// First device receive short, then when user release button we get long // First device receive short, then when user release button we get long
if(event->type == InputTypeLong) { if(event->type == InputTypeLong) {
#if FURI_DEBUG #ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Longpress!"); FURI_LOG_I(TAG, "Longpress!");
#endif #endif
// Stop blinking // Stop blinking