diff --git a/applications/main/subghz/scenes/subghz_scene_ext_module_settings.c b/applications/main/subghz/scenes/subghz_scene_ext_module_settings.c index 508ca7dcc..963483db3 100644 --- a/applications/main/subghz/scenes/subghz_scene_ext_module_settings.c +++ b/applications/main/subghz/scenes/subghz_scene_ext_module_settings.c @@ -62,7 +62,7 @@ static void subghz_scene_receiver_config_set_debug_pin(VariableItem* item) { variable_item_set_current_value_text(item, debug_pin_text[index]); - subghz->txrx->debug_pin_state = index == 1; + subghz_txrx_set_debug_pin_state(subghz->txrx, index == 1); } static void subghz_scene_receiver_config_set_debug_counter(VariableItem* item) { @@ -164,7 +164,7 @@ void subghz_scene_ext_module_settings_on_enter(void* context) { DEBUG_P_COUNT, subghz_scene_receiver_config_set_debug_pin, subghz); - value_index_dpin = subghz->txrx->debug_pin_state; + value_index_dpin = subghz_txrx_get_debug_pin_state(subghz->txrx); variable_item_set_current_value_index(item, value_index_dpin); variable_item_set_current_value_text(item, debug_pin_text[value_index_dpin]); diff --git a/applications/main/subghz/subghz.c b/applications/main/subghz/subghz.c index b2fca5a9e..36a817117 100644 --- a/applications/main/subghz/subghz.c +++ b/applications/main/subghz/subghz.c @@ -180,11 +180,15 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { subghz_test_static_get_view(subghz->subghz_test_static)); #endif - subghz->txrx = malloc(sizeof(SubGhzTxRx)); - //init setting - subghz->txrx->setting = subghz_setting_alloc(); + //init threshold rssi + subghz->threshold_rssi = subghz_threshold_rssi_alloc(); - subghz_setting_load(subghz->txrx->setting, EXT_PATH("subghz/assets/setting_user")); + //init TxRx & Protocol & History & KeyBoard + subghz_unlock(subghz); + + subghz->txrx = subghz_txrx_alloc(); + + SubGhzSetting* setting = subghz_txrx_get_setting(subghz->txrx); // Custom Presets load without using config file if(!alloc_for_tx_only) { @@ -194,8 +198,7 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { (const char*)"Custom_preset_data", (const char*)"02 0D 0B 06 08 32 07 04 14 00 13 02 12 04 11 83 10 67 15 24 18 18 19 16 1D 91 1C 00 1B 07 20 FB 22 10 21 56 00 00 C0 00 00 00 00 00 00 00"); flipper_format_rewind(temp_fm_preset); - subghz_setting_load_custom_preset( - subghz->txrx->setting, (const char*)"FM95", temp_fm_preset); + subghz_setting_load_custom_preset(setting, (const char*)"FM95", temp_fm_preset); flipper_format_free(temp_fm_preset); @@ -206,8 +209,7 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { (const char*)"Custom_preset_data", (const char*)"02 0D 03 47 08 32 0B 06 15 32 14 00 13 00 12 00 11 32 10 A7 18 18 19 1D 1D 92 1C 00 1B 04 20 FB 22 17 21 B6 00 00 00 12 0E 34 60 C5 C1 C0"); flipper_format_rewind(temp_fm_preset2); - subghz_setting_load_custom_preset( - subghz->txrx->setting, (const char*)"FM15k", temp_fm_preset2); + subghz_setting_load_custom_preset(setting, (const char*)"FM15k", temp_fm_preset2); flipper_format_free(temp_fm_preset2); @@ -218,8 +220,7 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { (const char*)"Custom_preset_data", (const char*)"02 0D 07 04 08 32 0B 06 10 64 11 93 12 0C 13 02 14 00 15 15 18 18 19 16 1B 07 1C 00 1D 91 20 FB 21 56 22 10 00 00 C0 00 00 00 00 00 00 00"); flipper_format_rewind(temp_fm_preset3); - subghz_setting_load_custom_preset( - subghz->txrx->setting, (const char*)"Pagers", temp_fm_preset3); + subghz_setting_load_custom_preset(setting, (const char*)"Pagers", temp_fm_preset3); flipper_format_free(temp_fm_preset3); @@ -230,8 +231,7 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { (const char*)"Custom_preset_data", (const char*)"02 0D 0B 06 08 32 07 04 14 00 13 02 12 04 11 36 10 69 15 32 18 18 19 16 1D 91 1C 00 1B 07 20 FB 22 10 21 56 00 00 C0 00 00 00 00 00 00 00"); flipper_format_rewind(temp_fm_preset4); - subghz_setting_load_custom_preset( - subghz->txrx->setting, (const char*)"HND_1", temp_fm_preset4); + subghz_setting_load_custom_preset(setting, (const char*)"HND_1", temp_fm_preset4); flipper_format_free(temp_fm_preset4); @@ -241,8 +241,7 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { (const char*)"Custom_preset_data", (const char*)"02 0D 0B 06 08 32 07 04 14 00 13 02 12 07 11 36 10 E9 15 32 18 18 19 16 1D 92 1C 40 1B 03 20 FB 22 10 21 56 00 00 C0 00 00 00 00 00 00 00"); flipper_format_rewind(temp_fm_preset5); - subghz_setting_load_custom_preset( - subghz->txrx->setting, (const char*)"HND_2", temp_fm_preset5); + subghz_setting_load_custom_preset(setting, (const char*)"HND_2", temp_fm_preset5); flipper_format_free(temp_fm_preset5); } @@ -259,61 +258,24 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { subghz->last_settings->frequency, subghz->last_settings->preset); #endif - subghz_setting_set_default_frequency( - subghz->txrx->setting, subghz->last_settings->frequency); + subghz_setting_set_default_frequency(setting, subghz->last_settings->frequency); } - //init threshold rssi - subghz->threshold_rssi = subghz_threshold_rssi_alloc(); - - //init Worker & Protocol & History & KeyBoard - subghz_unlock(subghz); - - subghz->txrx->preset = malloc(sizeof(SubGhzRadioPreset)); - subghz->txrx->preset->name = furi_string_alloc(); if(!alloc_for_tx_only) { subghz_set_preset(subghz->txrx, "AM650", subghz->last_settings->frequency, NULL, 0); - } else { - subghz_set_preset( - subghz->txrx, - "AM650", - subghz_setting_get_default_frequency(subghz->txrx->setting), - NULL, - 0); } - subghz->txrx->txrx_state = SubGhzTxRxStateSleep; - subghz_hopper_set_state(subghz->txrx, SubGhzHopperStateOFF); - subghz_speaker_set_state(subghz->txrx, SubGhzSpeakerStateDisable); + subghz_rx_key_state_set(subghz, SubGhzRxKeyStateIDLE); - subghz->txrx->debug_pin_state = false; + if(!alloc_for_tx_only) { subghz->history = subghz_history_alloc(); } - subghz->txrx->worker = subghz_worker_alloc(); - - subghz->txrx->fff_data = flipper_format_string_alloc(); subghz->secure_data = malloc(sizeof(SecureData)); - subghz->txrx->environment = subghz_environment_alloc(); - subghz_environment_set_came_atomo_rainbow_table_file_name( - subghz->txrx->environment, EXT_PATH("subghz/assets/came_atomo")); - subghz_environment_set_alutech_at_4n_rainbow_table_file_name( - subghz->txrx->environment, EXT_PATH("subghz/assets/alutech_at_4n")); - subghz_environment_set_nice_flor_s_rainbow_table_file_name( - subghz->txrx->environment, EXT_PATH("subghz/assets/nice_flor_s")); - subghz_environment_set_protocol_registry( - subghz->txrx->environment, (void*)&subghz_protocol_registry); - subghz->txrx->receiver = subghz_receiver_alloc_init(subghz->txrx->environment); - subghz->filter = SubGhzProtocolFlag_Decodable; - subghz_txrx_receiver_set_filter(subghz->txrx, subghz->filter); - subghz_worker_set_overrun_callback( - subghz->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset); - subghz_worker_set_pair_callback( - subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode); - subghz_worker_set_context(subghz->txrx->worker, subghz->txrx->receiver); + subghz_txrx_receiver_set_filter(subghz->txrx, subghz->filter); subghz_txrx_need_save_callback_set(subghz->txrx, subghz_save_to_file, subghz); @@ -405,8 +367,6 @@ void subghz_free(SubGhz* subghz, bool alloc_for_tx_only) { furi_record_close(RECORD_GUI); subghz->gui = NULL; - // setting - subghz_setting_free(subghz->txrx->setting); if(!alloc_for_tx_only) { subghz_last_settings_free(subghz->last_settings); } @@ -414,22 +374,14 @@ void subghz_free(SubGhz* subghz, bool alloc_for_tx_only) { // threshold rssi subghz_threshold_rssi_free(subghz->threshold_rssi); - //Worker & Protocol & History - - subghz_receiver_free(subghz->txrx->receiver); - - subghz_environment_free(subghz->txrx->environment); - - subghz_worker_free(subghz->txrx->worker); - - flipper_format_free(subghz->txrx->fff_data); if(!alloc_for_tx_only) { subghz_history_free(subghz->history); } - furi_string_free(subghz->txrx->preset->name); - free(subghz->txrx->preset); + free(subghz->secure_data); - free(subghz->txrx); + + //TxRx + subghz_txrx_free(subghz->txrx); //Error string furi_string_free(subghz->error_str); @@ -462,12 +414,6 @@ int32_t subghz_app(void* p) { subghz->raw_send_only = false; } - //Load database - bool load_database = subghz_environment_load_keystore( - subghz->txrx->environment, EXT_PATH("subghz/assets/keeloq_mfcodes")); - subghz_environment_load_keystore( - subghz->txrx->environment, EXT_PATH("subghz/assets/keeloq_mfcodes_user")); - // Call enable power for external module furi_hal_subghz_enable_ext_power(); @@ -512,7 +458,7 @@ int32_t subghz_app(void* p) { view_dispatcher_attach_to_gui( subghz->view_dispatcher, subghz->gui, ViewDispatcherTypeFullscreen); furi_string_set(subghz->file_path, SUBGHZ_APP_FOLDER); - if(load_database) { + if(subghz_txrx_is_load_database(subghz->txrx)) { scene_manager_next_scene(subghz->scene_manager, SubGhzSceneStart); } else { scene_manager_set_scene_state( diff --git a/applications/main/subghz/subghz_radio.c b/applications/main/subghz/subghz_radio.c index 656306ed8..6e3ae3294 100644 --- a/applications/main/subghz/subghz_radio.c +++ b/applications/main/subghz/subghz_radio.c @@ -1,7 +1,77 @@ #include "subghz_radio.h" +#include #define TAG "SubGhz" +SubGhzTxRx* subghz_txrx_alloc() { + SubGhzTxRx* txrx = malloc(sizeof(SubGhzTxRx)); + txrx->setting = subghz_setting_alloc(); + subghz_setting_load(txrx->setting, EXT_PATH("subghz/assets/setting_user")); + + txrx->preset = malloc(sizeof(SubGhzRadioPreset)); + txrx->preset->name = furi_string_alloc(); + subghz_set_preset(txrx, "AM650", subghz_setting_get_default_frequency(txrx->setting), NULL, 0); + + txrx->txrx_state = SubGhzTxRxStateSleep; + + subghz_hopper_set_state(txrx, SubGhzHopperStateOFF); + subghz_speaker_set_state(txrx, SubGhzSpeakerStateDisable); + subghz_txrx_set_debug_pin_state(txrx, false); + + txrx->worker = subghz_worker_alloc(); + txrx->fff_data = flipper_format_string_alloc(); + + txrx->environment = subghz_environment_alloc(); + txrx->load_database = subghz_environment_load_keystore( + txrx->environment, EXT_PATH("subghz/assets/keeloq_mfcodes")); + subghz_environment_load_keystore( + txrx->environment, EXT_PATH("subghz/assets/keeloq_mfcodes_user")); + subghz_environment_set_came_atomo_rainbow_table_file_name( + txrx->environment, EXT_PATH("subghz/assets/came_atomo")); + subghz_environment_set_alutech_at_4n_rainbow_table_file_name( + txrx->environment, EXT_PATH("subghz/assets/alutech_at_4n")); + subghz_environment_set_nice_flor_s_rainbow_table_file_name( + txrx->environment, EXT_PATH("subghz/assets/nice_flor_s")); + subghz_environment_set_protocol_registry(txrx->environment, (void*)&subghz_protocol_registry); + txrx->receiver = subghz_receiver_alloc_init(txrx->environment); + + subghz_worker_set_overrun_callback( + txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset); + subghz_worker_set_pair_callback( + txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode); + subghz_worker_set_context(txrx->worker, txrx->receiver); + + return txrx; +} + +void subghz_txrx_free(SubGhzTxRx* txrx) { + furi_assert(txrx); + + subghz_worker_free(txrx->worker); + subghz_receiver_free(txrx->receiver); + subghz_environment_free(txrx->environment); + flipper_format_free(txrx->fff_data); + furi_string_free(txrx->preset->name); + subghz_setting_free(txrx->setting); + free(txrx->preset); + free(txrx); +} + +bool subghz_txrx_is_load_database(SubGhzTxRx* txrx) { + furi_assert(txrx); + return txrx->load_database; +} + +void subghz_txrx_set_debug_pin_state(SubGhzTxRx* txrx, bool state) { + furi_assert(txrx); + txrx->debug_pin_state = state; +} + +bool subghz_txrx_get_debug_pin_state(SubGhzTxRx* txrx) { + furi_assert(txrx); + return txrx->debug_pin_state; +} + void subghz_set_preset( SubGhzTxRx* txrx, const char* preset_name, diff --git a/applications/main/subghz/subghz_radio.h b/applications/main/subghz/subghz_radio.h index 59f93733c..5e5b1a82f 100644 --- a/applications/main/subghz/subghz_radio.h +++ b/applications/main/subghz/subghz_radio.h @@ -24,6 +24,7 @@ struct SubGhzTxRx { uint8_t hopper_timeout; uint8_t hopper_idx_frequency; + bool load_database; SubGhzHopperState hopper_state; SubGhzTxRxState txrx_state; @@ -38,6 +39,13 @@ struct SubGhzTxRx { typedef struct SubGhzTxRx SubGhzTxRx; +SubGhzTxRx* subghz_txrx_alloc(); +void subghz_txrx_free(SubGhzTxRx* txrx); +bool subghz_txrx_is_load_database(SubGhzTxRx* txrx); + +void subghz_txrx_set_debug_pin_state(SubGhzTxRx* txrx, bool state); +bool subghz_txrx_get_debug_pin_state(SubGhzTxRx* txrx); + void subghz_set_preset( SubGhzTxRx* txrx, const char* preset_name,