unleashed-firmware/applications/subghz/subghz.c
2022-07-26 21:09:31 +03:00

407 lines
14 KiB
C

/* Abandon hope, all ye who enter here. */
#include "m-string.h"
#include "subghz/types.h"
#include "subghz_i.h"
#include <lib/toolbox/path.h>
static const NotificationSequence sequence_blink_start_magenta = {
&message_blink_start_10,
&message_blink_set_color_magenta,
&message_do_not_reset,
NULL,
};
static const NotificationSequence sequence_blink_stop = {
&message_blink_stop,
NULL,
};
bool subghz_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
SubGhz* subghz = context;
return scene_manager_handle_custom_event(subghz->scene_manager, event);
}
bool subghz_back_event_callback(void* context) {
furi_assert(context);
SubGhz* subghz = context;
return scene_manager_handle_back_event(subghz->scene_manager);
}
void subghz_tick_event_callback(void* context) {
furi_assert(context);
SubGhz* subghz = context;
scene_manager_handle_tick_event(subghz->scene_manager);
}
static bool subghz_rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
furi_assert(context);
SubGhz* subghz = context;
if(!subghz->rpc_ctx) {
return false;
}
bool result = false;
if(event == RpcAppEventSessionClose) {
rpc_system_app_set_callback(subghz->rpc_ctx, NULL, NULL);
subghz->rpc_ctx = NULL;
notification_message(subghz->notifications, &sequence_blink_stop);
view_dispatcher_send_custom_event(subghz->view_dispatcher, SubGhzCustomEventSceneExit);
if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
subghz_tx_stop(subghz);
subghz_sleep(subghz);
}
result = true;
} else if(event == RpcAppEventAppExit) {
view_dispatcher_send_custom_event(subghz->view_dispatcher, SubGhzCustomEventSceneExit);
if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
subghz_tx_stop(subghz);
subghz_sleep(subghz);
}
result = true;
} else if(event == RpcAppEventLoadFile) {
if(arg) {
if(subghz_key_load(subghz, arg, false)) {
string_set_str(subghz->file_path, arg);
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubGhzCustomEventSceneRpcLoad);
result = true;
}
}
} else if(event == RpcAppEventButtonPress) {
if(subghz->txrx->txrx_state == SubGhzTxRxStateSleep) {
notification_message(subghz->notifications, &sequence_blink_start_magenta);
result = subghz_tx_start(subghz, subghz->txrx->fff_data);
}
} else if(event == RpcAppEventButtonRelease) {
if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
notification_message(subghz->notifications, &sequence_blink_stop);
subghz_tx_stop(subghz);
subghz_sleep(subghz);
result = true;
}
}
return result;
}
SubGhz* subghz_alloc() {
SubGhz* subghz = malloc(sizeof(SubGhz));
string_init(subghz->file_path);
string_init(subghz->file_path_tmp);
// GUI
subghz->gui = furi_record_open(RECORD_GUI);
// View Dispatcher
subghz->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(subghz->view_dispatcher);
subghz->scene_manager = scene_manager_alloc(&subghz_scene_handlers, subghz);
view_dispatcher_set_event_callback_context(subghz->view_dispatcher, subghz);
view_dispatcher_set_custom_event_callback(
subghz->view_dispatcher, subghz_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
subghz->view_dispatcher, subghz_back_event_callback);
view_dispatcher_set_tick_event_callback(
subghz->view_dispatcher, subghz_tick_event_callback, 100);
// Open Notification record
subghz->notifications = furi_record_open(RECORD_NOTIFICATION);
// SubMenu
subghz->submenu = submenu_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher, SubGhzViewIdMenu, submenu_get_view(subghz->submenu));
// Receiver
subghz->subghz_receiver = subghz_view_receiver_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdReceiver,
subghz_view_receiver_get_view(subghz->subghz_receiver));
// Popup
subghz->popup = popup_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher, SubGhzViewIdPopup, popup_get_view(subghz->popup));
// Text Input
subghz->text_input = text_input_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher, SubGhzViewIdTextInput, text_input_get_view(subghz->text_input));
// Byte Input
subghz->byte_input = byte_input_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher, SubGhzViewIdByteInput, byte_input_get_view(subghz->byte_input));
// Custom Widget
subghz->widget = widget_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher, SubGhzViewIdWidget, widget_get_view(subghz->widget));
//Dialog
subghz->dialogs = furi_record_open(RECORD_DIALOGS);
// Transmitter
subghz->subghz_transmitter = subghz_view_transmitter_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdTransmitter,
subghz_view_transmitter_get_view(subghz->subghz_transmitter));
// Variable Item List
subghz->variable_item_list = variable_item_list_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdVariableItemList,
variable_item_list_get_view(subghz->variable_item_list));
// Frequency Analyzer
subghz->subghz_frequency_analyzer = subghz_frequency_analyzer_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdFrequencyAnalyzer,
subghz_frequency_analyzer_get_view(subghz->subghz_frequency_analyzer));
// Read RAW
subghz->subghz_read_raw = subghz_read_raw_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdReadRAW,
subghz_read_raw_get_view(subghz->subghz_read_raw));
// Carrier Test Module
subghz->subghz_test_carrier = subghz_test_carrier_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdTestCarrier,
subghz_test_carrier_get_view(subghz->subghz_test_carrier));
// Packet Test
subghz->subghz_test_packet = subghz_test_packet_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdTestPacket,
subghz_test_packet_get_view(subghz->subghz_test_packet));
// Static send
subghz->subghz_test_static = subghz_test_static_alloc();
view_dispatcher_add_view(
subghz->view_dispatcher,
SubGhzViewIdStatic,
subghz_test_static_get_view(subghz->subghz_test_static));
//init setting
subghz->setting = subghz_setting_alloc();
subghz_setting_load(subghz->setting, EXT_PATH("subghz/assets/setting_user"));
//init Worker & Protocol & History & KeyBoard
subghz->lock = SubGhzLockOff;
subghz->txrx = malloc(sizeof(SubGhzTxRx));
subghz->txrx->preset = malloc(sizeof(SubGhzPesetDefinition));
string_init(subghz->txrx->preset->name);
subghz_preset_init(
subghz, "AM650", subghz_setting_get_default_frequency(subghz->setting), NULL, 0);
subghz->txrx->txrx_state = SubGhzTxRxStateSleep;
subghz->txrx->hopper_state = SubGhzHopperStateOFF;
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
subghz->txrx->history = subghz_history_alloc();
subghz->txrx->worker = subghz_worker_alloc();
subghz->txrx->fff_data = flipper_format_string_alloc();
subghz->txrx->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_nice_flor_s_rainbow_table_file_name(
subghz->txrx->environment, EXT_PATH("subghz/assets/nice_flor_s"));
subghz->txrx->receiver = subghz_receiver_alloc_init(subghz->txrx->environment);
subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_Decodable);
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);
//Init Error_str
string_init(subghz->error_str);
return subghz;
}
void subghz_free(SubGhz* subghz) {
furi_assert(subghz);
if(subghz->rpc_ctx) {
rpc_system_app_set_callback(subghz->rpc_ctx, NULL, NULL);
rpc_system_app_send_exited(subghz->rpc_ctx);
notification_message(subghz->notifications, &sequence_blink_stop);
subghz->rpc_ctx = NULL;
}
// Packet Test
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdTestPacket);
subghz_test_packet_free(subghz->subghz_test_packet);
// Carrier Test
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdTestCarrier);
subghz_test_carrier_free(subghz->subghz_test_carrier);
// Static
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdStatic);
subghz_test_static_free(subghz->subghz_test_static);
// Receiver
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdReceiver);
subghz_view_receiver_free(subghz->subghz_receiver);
// TextInput
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdTextInput);
text_input_free(subghz->text_input);
// ByteInput
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdByteInput);
byte_input_free(subghz->byte_input);
// Custom Widget
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdWidget);
widget_free(subghz->widget);
//Dialog
furi_record_close(RECORD_DIALOGS);
// Transmitter
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdTransmitter);
subghz_view_transmitter_free(subghz->subghz_transmitter);
// Variable Item List
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdVariableItemList);
variable_item_list_free(subghz->variable_item_list);
// Frequency Analyzer
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdFrequencyAnalyzer);
subghz_frequency_analyzer_free(subghz->subghz_frequency_analyzer);
// Read RAW
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdReadRAW);
subghz_read_raw_free(subghz->subghz_read_raw);
// Submenu
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdMenu);
submenu_free(subghz->submenu);
// Popup
view_dispatcher_remove_view(subghz->view_dispatcher, SubGhzViewIdPopup);
popup_free(subghz->popup);
// Scene manager
scene_manager_free(subghz->scene_manager);
// View Dispatcher
view_dispatcher_free(subghz->view_dispatcher);
// GUI
furi_record_close(RECORD_GUI);
subghz->gui = NULL;
//setting
subghz_setting_free(subghz->setting);
//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);
subghz_history_free(subghz->txrx->history);
string_clear(subghz->txrx->preset->name);
free(subghz->txrx->preset);
free(subghz->txrx->secure_data);
free(subghz->txrx);
//Error string
string_clear(subghz->error_str);
// Notifications
furi_record_close(RECORD_NOTIFICATION);
subghz->notifications = NULL;
// Path strings
string_clear(subghz->file_path);
string_clear(subghz->file_path_tmp);
// The rest
free(subghz);
}
int32_t subghz_app(void* p) {
SubGhz* subghz = subghz_alloc();
//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"));
// Check argument and run corresponding scene
if(p) {
uint32_t rpc_ctx = 0;
if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
subghz->rpc_ctx = (void*)rpc_ctx;
rpc_system_app_set_callback(subghz->rpc_ctx, subghz_rpc_command_callback, subghz);
rpc_system_app_send_started(subghz->rpc_ctx);
view_dispatcher_attach_to_gui(
subghz->view_dispatcher, subghz->gui, ViewDispatcherTypeDesktop);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneRpc);
} else {
view_dispatcher_attach_to_gui(
subghz->view_dispatcher, subghz->gui, ViewDispatcherTypeFullscreen);
if(subghz_key_load(subghz, p, true)) {
string_set_str(subghz->file_path, p);
if((!strcmp(subghz->txrx->decoder_result->protocol->name, "RAW"))) {
//Load Raw TX
subghz->txrx->rx_key_state = SubGhzRxKeyStateRAWLoad;
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReadRAW);
} else {
//Load transmitter TX
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneTransmitter);
}
} else {
//exit app
scene_manager_stop(subghz->scene_manager);
view_dispatcher_stop(subghz->view_dispatcher);
}
}
} else {
view_dispatcher_attach_to_gui(
subghz->view_dispatcher, subghz->gui, ViewDispatcherTypeFullscreen);
string_set_str(subghz->file_path, SUBGHZ_APP_FOLDER);
if(load_database) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneStart);
} else {
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneShowError, SubGhzCustomEventManagerSet);
string_set_str(
subghz->error_str,
"No SD card or\ndatabase found.\nSome app function\nmay be reduced.");
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
}
}
furi_hal_power_suppress_charge_enter();
view_dispatcher_run(subghz->view_dispatcher);
furi_hal_power_suppress_charge_exit();
subghz_free(subghz);
return 0;
}