Skip to content

Commit

Permalink
SubGhz: loading/saving files from any folder below the parent (#1142)
Browse files Browse the repository at this point in the history
Co-authored-by: あく <[email protected]>
  • Loading branch information
Skorpionm and skotopes authored Apr 21, 2022
1 parent 7c692a9 commit 0ff9f43
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 116 deletions.
2 changes: 1 addition & 1 deletion applications/subghz/scenes/subghz_scene_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool subghz_scene_delete_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubGhzCustomEventSceneDelete) {
strncpy(subghz->file_name_tmp, subghz->file_name, SUBGHZ_MAX_LEN_NAME);
strncpy(subghz->file_path_tmp, subghz->file_path, SUBGHZ_MAX_LEN_NAME);
if(subghz_delete_file(subghz)) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneDeleteSuccess);
} else {
Expand Down
11 changes: 8 additions & 3 deletions applications/subghz/scenes/subghz_scene_delete_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ void subghz_scene_delete_raw_on_enter(void* context) {
string_init(frequency_str);
string_init(modulation_str);

char delete_str[256];
snprintf(delete_str, sizeof(delete_str), "\e#Delete %s?\e#", subghz->file_name);
char delete_str[SUBGHZ_MAX_LEN_NAME + 16];
string_t file_name;
string_init(file_name);
path_extract_filename_no_ext(subghz->file_path, file_name);
snprintf(delete_str, sizeof(delete_str), "\e#Delete %s?\e#", string_get_cstr(file_name));
string_clear(file_name);

widget_add_text_box_element(
subghz->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, delete_str, false);

Expand Down Expand Up @@ -56,7 +61,7 @@ bool subghz_scene_delete_raw_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubGhzCustomEventSceneDeleteRAW) {
strncpy(subghz->file_name_tmp, subghz->file_name, SUBGHZ_MAX_LEN_NAME);
strncpy(subghz->file_path_tmp, subghz->file_path, SUBGHZ_MAX_LEN_NAME);
if(subghz_delete_file(subghz)) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneDeleteSuccess);
} else {
Expand Down
2 changes: 1 addition & 1 deletion applications/subghz/scenes/subghz_scene_more_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool subghz_scene_more_raw_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneDeleteRAW);
return true;
} else if(event.event == SubmenuIndexEdit) {
memset(subghz->file_name_tmp, 0, sizeof(subghz->file_name_tmp));
memset(subghz->file_path_tmp, 0, sizeof(subghz->file_path_tmp));
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneMoreRAW, SubmenuIndexEdit);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
Expand Down
28 changes: 18 additions & 10 deletions applications/subghz/scenes/subghz_scene_read_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ bool subghz_scene_read_raw_update_filename(SubGhz* subghz) {
break;
}

path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
strncpy(subghz->file_name, string_get_cstr(temp_str), SUBGHZ_MAX_LEN_NAME);
strncpy(subghz->file_path, string_get_cstr(temp_str), SUBGHZ_MAX_LEN_NAME);

ret = true;
} while(false);
Expand Down Expand Up @@ -66,34 +65,38 @@ void subghz_scene_read_raw_callback_end_tx(void* context) {

void subghz_scene_read_raw_on_enter(void* context) {
SubGhz* subghz = context;
string_t file_name;
string_init(file_name);

switch(subghz->txrx->rx_key_state) {
case SubGhzRxKeyStateBack:
subghz_read_raw_set_status(subghz->subghz_read_raw, SubGhzReadRAWStatusIDLE, "");
break;
case SubGhzRxKeyStateRAWLoad:
path_extract_filename_no_ext(subghz->file_path, file_name);
subghz_read_raw_set_status(
subghz->subghz_read_raw, SubGhzReadRAWStatusLoadKeyTX, subghz->file_name);
subghz->subghz_read_raw, SubGhzReadRAWStatusLoadKeyTX, string_get_cstr(file_name));
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
break;
case SubGhzRxKeyStateRAWSave:
path_extract_filename_no_ext(subghz->file_path, file_name);
subghz_read_raw_set_status(
subghz->subghz_read_raw, SubGhzReadRAWStatusSaveKey, subghz->file_name);
subghz->subghz_read_raw, SubGhzReadRAWStatusSaveKey, string_get_cstr(file_name));
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
break;
default:
subghz_read_raw_set_status(subghz->subghz_read_raw, SubGhzReadRAWStatusStart, "");
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
break;
}

string_clear(file_name);
subghz_scene_read_raw_update_statusbar(subghz);

//set callback view raw
subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);

subghz->txrx->decoder_result =
subghz_receiver_search_decoder_base_by_name(subghz->txrx->receiver, "RAW");
subghz->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver, SUBGHZ_PROTOCOL_RAW_NAME);
furi_assert(subghz->txrx->decoder_result);

//set filter RAW feed
Expand Down Expand Up @@ -230,10 +233,15 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
};
subghz_protocol_raw_save_to_file_stop(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
subghz_protocol_raw_gen_fff_data(
subghz->txrx->fff_data, SUBGHZ_APP_FOLDER, RAW_FILE_NAME);
subghz->state_notifications = SubGhzNotificationStateIDLE;

string_t temp_str;
string_init(temp_str);
string_printf(
temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, RAW_FILE_NAME, SUBGHZ_APP_EXTENSION);
subghz_protocol_raw_gen_fff_data(subghz->txrx->fff_data, string_get_cstr(temp_str));
string_clear(temp_str);

subghz->state_notifications = SubGhzNotificationStateIDLE;
subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;

return true;
Expand Down
50 changes: 33 additions & 17 deletions applications/subghz/scenes/subghz_scene_save_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <lib/subghz/protocols/raw.h>
#include <gui/modules/validators.h>

#define MAX_TEXT_INPUT_LEN 22

void subghz_scene_save_name_text_input_callback(void* context) {
furi_assert(context);
SubGhz* subghz = context;
Expand All @@ -17,58 +19,73 @@ void subghz_scene_save_name_on_enter(void* context) {
TextInput* text_input = subghz->text_input;
bool dev_name_empty = false;

if(!strcmp(subghz->file_name, "")) {
set_random_name(subghz->file_name, sizeof(subghz->file_name));
string_t file_name;
string_init(file_name);

if(!strcmp(subghz->file_path, "")) {
char file_name_buf[SUBGHZ_MAX_LEN_NAME] = {0};
set_random_name(file_name_buf, SUBGHZ_MAX_LEN_NAME);
string_set(file_name, file_name_buf);
strncpy(subghz->file_dir, SUBGHZ_APP_FOLDER, SUBGHZ_MAX_LEN_NAME);
//highlighting the entire filename by default
dev_name_empty = true;
} else {
strncpy(subghz->file_name_tmp, subghz->file_name, SUBGHZ_MAX_LEN_NAME);
strncpy(subghz->file_path_tmp, subghz->file_path, SUBGHZ_MAX_LEN_NAME);
path_extract_dirname(subghz->file_path, file_name);
strncpy(subghz->file_dir, string_get_cstr(file_name), SUBGHZ_MAX_LEN_NAME);
path_extract_filename_no_ext(subghz->file_path, file_name);
if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerNoSet) {
subghz_get_next_name_file(subghz, SUBGHZ_MAX_LEN_NAME);
path_extract_filename_no_ext(subghz->file_path, file_name);
if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) ==
SubGhzCustomEventManagerSetRAW) {
dev_name_empty = true;
}
}
}

strncpy(subghz->file_path, string_get_cstr(file_name), SUBGHZ_MAX_LEN_NAME);
text_input_set_header_text(text_input, "Name signal");
text_input_set_result_callback(
text_input,
subghz_scene_save_name_text_input_callback,
subghz,
subghz->file_name,
SUBGHZ_MAX_LEN_NAME + 1, // buffer size
subghz->file_path,
MAX_TEXT_INPUT_LEN, // buffer size
dev_name_empty);

ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
SUBGHZ_APP_FOLDER,
SUBGHZ_APP_EXTENSION,
(dev_name_empty) ? (NULL) : (subghz->file_name_tmp));
ValidatorIsFile* validator_is_file =
validator_is_file_alloc_init(subghz->file_dir, SUBGHZ_APP_EXTENSION, NULL);
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);

string_clear(file_name);

view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdTextInput);
}

bool subghz_scene_save_name_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeBack) {
strncpy(subghz->file_name, subghz->file_name_tmp, SUBGHZ_MAX_LEN_NAME);
strncpy(subghz->file_path, subghz->file_path_tmp, SUBGHZ_MAX_LEN_NAME);
scene_manager_previous_scene(subghz->scene_manager);
return true;
} else if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubGhzCustomEventSceneSaveName) {
if(strcmp(subghz->file_name, "")) {
if(strcmp(subghz->file_name_tmp, "")) {
if(strcmp(subghz->file_path, "")) {
string_t temp_str;
string_init_printf(
temp_str, "%s/%s%s", subghz->file_dir, subghz->file_path, SUBGHZ_APP_EXTENSION);
strncpy(subghz->file_path, string_get_cstr(temp_str), SUBGHZ_MAX_LEN_NAME);
string_clear(temp_str);
if(strcmp(subghz->file_path_tmp, "")) {
if(!subghz_rename_file(subghz)) {
return false;
}
} else {
if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneSetType) !=
SubGhzCustomEventManagerNoSet) {
subghz_save_protocol_to_file(
subghz, subghz->txrx->fff_data, subghz->file_name);
subghz, subghz->txrx->fff_data, subghz->file_path);
scene_manager_set_scene_state(
subghz->scene_manager,
SubGhzSceneSetType,
Expand All @@ -78,14 +95,13 @@ bool subghz_scene_save_name_on_event(void* context, SceneManagerEvent event) {
subghz,
subghz_history_get_raw_data(
subghz->txrx->history, subghz->txrx->idx_menu_chosen),
subghz->file_name);
subghz->file_path);
}
}

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerNoSet) {
subghz_protocol_raw_gen_fff_data(
subghz->txrx->fff_data, SUBGHZ_APP_FOLDER, subghz->file_name);
subghz_protocol_raw_gen_fff_data(subghz->txrx->fff_data, subghz->file_path);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerNoSet);
} else {
Expand Down
10 changes: 3 additions & 7 deletions applications/subghz/subghz.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ void subghz_free(SubGhz* subghz) {
subghz->notifications = NULL;

// About birds
furi_assert(subghz->file_name[SUBGHZ_MAX_LEN_NAME] == 0);
furi_assert(subghz->file_name_tmp[SUBGHZ_MAX_LEN_NAME] == 0);
furi_assert(subghz->file_path[SUBGHZ_MAX_LEN_NAME] == 0);
furi_assert(subghz->file_path_tmp[SUBGHZ_MAX_LEN_NAME] == 0);

// The rest
free(subghz);
Expand All @@ -260,12 +260,8 @@ int32_t subghz_app(void* p) {
// Check argument and run corresponding scene
if(p) {
if(subghz_key_load(subghz, p)) {
string_t filename;
string_init(filename);
strncpy(subghz->file_path, p, SUBGHZ_MAX_LEN_NAME);

path_extract_filename_no_ext(p, filename);
strncpy(subghz->file_name, string_get_cstr(filename), SUBGHZ_MAX_LEN_NAME);
string_clear(filename);
if((!strcmp(subghz->txrx->decoder_result->protocol->name, "RAW"))) {
//Load Raw TX
subghz->txrx->rx_key_state = SubGhzRxKeyStateRAWLoad;
Expand Down
Loading

0 comments on commit 0ff9f43

Please sign in to comment.