SubGhz: refactoring

This commit is contained in:
gid9798 2023-05-09 13:34:54 +03:00
parent b99d309feb
commit f71900694b
11 changed files with 113 additions and 93 deletions

View file

@ -29,7 +29,7 @@ static void subghz_scene_receiver_update_statusbar(void* context) {
FuriString* frequency_str = furi_string_alloc();
FuriString* modulation_str = furi_string_alloc();
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
subghz_view_receiver_add_data_statusbar(
subghz->subghz_receiver,

View file

@ -15,7 +15,7 @@ void subghz_scene_delete_on_enter(void* context) {
FuriString* modulation_str = furi_string_alloc();
FuriString* text = furi_string_alloc();
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
widget_add_string_element(
subghz->widget,
78,
@ -33,7 +33,7 @@ void subghz_scene_delete_on_enter(void* context) {
AlignTop,
FontSecondary,
furi_string_get_cstr(modulation_str));
subghz_protocol_decoder_base_get_string(subghz->txrx->decoder_result, text);
subghz_protocol_decoder_base_get_string(subghz_txrx_get_decoder(subghz->txrx), text);
widget_add_string_multiline_element(
subghz->widget, 0, 0, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(text));

View file

@ -30,7 +30,7 @@ void subghz_scene_delete_raw_on_enter(void* context) {
widget_add_string_element(
subghz->widget, 38, 25, AlignLeft, AlignTop, FontSecondary, "RAW signal");
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
widget_add_string_element(
subghz->widget,
35,

View file

@ -41,10 +41,10 @@ static void subghz_scene_read_raw_update_statusbar(void* context) {
FuriString* modulation_str = furi_string_alloc();
#ifdef SUBGHZ_EXT_PRESET_NAME
subghz_get_frequency_modulation(subghz, frequency_str, NULL);
furi_string_printf(modulation_str, "%s", furi_string_get_cstr(subghz->txrx->preset->name));
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, true);
//furi_string_printf(modulation_str, "%s", furi_string_get_cstr(subghz->txrx->preset->name));
#else
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
#endif
subghz_read_raw_add_data_statusbar(
subghz->subghz_read_raw,
@ -113,9 +113,7 @@ void subghz_scene_read_raw_on_enter(void* context) {
//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, SUBGHZ_PROTOCOL_RAW_NAME);
furi_assert(subghz->txrx->decoder_result);
furi_check(subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, SUBGHZ_PROTOCOL_RAW_NAME));
//set filter RAW feed
subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_RAW);
@ -131,7 +129,7 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
subghz_txrx_stop(subghz->txrx);
//Stop save file
subghz_protocol_raw_save_to_file_stop(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
(SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx));
subghz->state_notifications = SubGhzNotificationStateIDLE;
//needed save?
if((subghz_rx_key_state_get(subghz) == SubGhzRxKeyStateAddKey) ||
@ -255,10 +253,10 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
case SubGhzCustomEventViewReadRAWIDLE:
subghz_txrx_stop(subghz->txrx);
size_t spl_count = subghz_protocol_raw_get_sample_write(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
(SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx));
subghz_protocol_raw_save_to_file_stop(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
(SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx));
FuriString* temp_str = furi_string_alloc();
furi_string_printf(
@ -284,7 +282,7 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
} else {
if(subghz_protocol_raw_save_to_file_init(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
(SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx),
RAW_FILE_NAME,
subghz->txrx->preset)) {
DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
@ -333,14 +331,15 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
subghz_read_raw_update_sample_write(
subghz->subghz_read_raw,
subghz_protocol_raw_get_sample_write(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result));
(SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx)));
SubGhzThresholdRssiData ret_rssi =
subghz_threshold_get_rssi_data(subghz->threshold_rssi);
subghz_read_raw_add_data_rssi(
subghz->subghz_read_raw, ret_rssi.rssi, ret_rssi.is_above);
subghz_protocol_raw_save_to_file_pause(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, !ret_rssi.is_above);
(SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx),
!ret_rssi.is_above);
break;
case SubGhzNotificationStateTx:

View file

@ -46,17 +46,20 @@ static void subghz_scene_receiver_update_statusbar(void* context) {
#ifdef SUBGHZ_EXT_PRESET_NAME
if(subghz_history_get_last_index(subghz->txrx->history) > 0) {
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
} else {
subghz_get_frequency_modulation(subghz, frequency_str, NULL);
FuriString* temp_str = furi_string_alloc();
subghz_get_frequency_modulation(subghz->txrx, frequency_str, temp_str, true);
furi_string_printf(
modulation_str,
"%s Mod: %s",
furi_hal_subghz_get_radio_type() ? "Ext" : "Int",
furi_string_get_cstr(subghz->txrx->preset->name));
furi_string_get_cstr(temp_str));
furi_string_free(temp_str);
}
#else
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
#endif
subghz_view_receiver_add_data_statusbar(
@ -155,37 +158,26 @@ void subghz_scene_receiver_on_enter(void* context) {
// TODO: Replace with proper solution based on protocol flags, remove kostily and velosipedy from here
// Needs to be done after subghz refactoring merge!!!
if(subghz->ignore_starline == true) {
SubGhzProtocolDecoderBase* protocoldecoderbase = NULL;
protocoldecoderbase =
subghz_receiver_search_decoder_base_by_name(subghz->txrx->receiver, "Star Line");
if(protocoldecoderbase) {
if(subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, "Star Line")) {
subghz_protocol_decoder_base_set_decoder_callback(
protocoldecoderbase, NULL, subghz->txrx->receiver);
subghz_txrx_get_decoder(subghz->txrx), NULL, subghz->txrx->receiver);
}
}
if(subghz->ignore_auto_alarms == true) {
SubGhzProtocolDecoderBase* protocoldecoderbase = NULL;
protocoldecoderbase =
subghz_receiver_search_decoder_base_by_name(subghz->txrx->receiver, "KIA Seed");
if(protocoldecoderbase) {
if(subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, "KIA Seed")) {
subghz_protocol_decoder_base_set_decoder_callback(
protocoldecoderbase, NULL, subghz->txrx->receiver);
subghz_txrx_get_decoder(subghz->txrx), NULL, subghz->txrx->receiver);
}
protocoldecoderbase = NULL;
protocoldecoderbase =
subghz_receiver_search_decoder_base_by_name(subghz->txrx->receiver, "Scher-Khan");
if(protocoldecoderbase) {
if(subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, "Scher-Khan")) {
subghz_protocol_decoder_base_set_decoder_callback(
protocoldecoderbase, NULL, subghz->txrx->receiver);
subghz_txrx_get_decoder(subghz->txrx), NULL, subghz->txrx->receiver);
}
}
if(subghz->ignore_magellan == true) {
SubGhzProtocolDecoderBase* protocoldecoderbase = NULL;
protocoldecoderbase =
subghz_receiver_search_decoder_base_by_name(subghz->txrx->receiver, "Magellan");
if(protocoldecoderbase) {
if(subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, "Magellan")) {
subghz_protocol_decoder_base_set_decoder_callback(
protocoldecoderbase, NULL, subghz->txrx->receiver);
subghz_txrx_get_decoder(subghz->txrx), NULL, subghz->txrx->receiver);
}
}
@ -199,9 +191,8 @@ void subghz_scene_receiver_on_enter(void* context) {
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, subghz->idx_menu_chosen);
//to use a universal decoder, we are looking for a link to it
subghz->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver, SUBGHZ_PROTOCOL_BIN_RAW_NAME);
furi_assert(subghz->txrx->decoder_result);
furi_check(
subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, SUBGHZ_PROTOCOL_BIN_RAW_NAME));
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdReceiver);
}
@ -277,7 +268,7 @@ bool subghz_scene_receiver_on_event(void* context, SceneManagerEvent event) {
subghz_receiver_rssi(subghz->subghz_receiver, ret_rssi.rssi);
subghz_protocol_decoder_bin_raw_data_input_rssi(
(SubGhzProtocolDecoderBinRAW*)subghz->txrx->decoder_result, ret_rssi.rssi);
(SubGhzProtocolDecoderBinRAW*)subghz_txrx_get_decoder(subghz->txrx), ret_rssi.rssi);
switch(subghz->state_notifications) {
case SubGhzNotificationStateRx:

View file

@ -23,14 +23,13 @@ void subghz_scene_receiver_info_callback(GuiButtonType result, InputType type, v
static bool subghz_scene_receiver_info_update_parser(void* context) {
SubGhz* subghz = context;
subghz->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver,
subghz_history_get_protocol_name(subghz->txrx->history, subghz->idx_menu_chosen));
if(subghz->txrx->decoder_result) {
if(subghz_txrx_load_decoder_by_name_protocol(
subghz->txrx,
subghz_history_get_protocol_name(subghz->txrx->history, subghz->idx_menu_chosen))) {
//todo we are trying to deserialize without checking for errors, since it is assumed that we just received this chignal
subghz_protocol_decoder_base_deserialize(
subghz->txrx->decoder_result,
subghz_txrx_get_decoder(subghz->txrx),
subghz_history_get_raw_data(subghz->txrx->history, subghz->idx_menu_chosen));
SubGhzRadioPreset* preset =
@ -53,7 +52,7 @@ void subghz_scene_receiver_info_draw_widget(SubGhz* subghz) {
FuriString* modulation_str = furi_string_alloc();
FuriString* text = furi_string_alloc();
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
widget_add_string_element(
subghz->widget,
78,
@ -71,7 +70,7 @@ void subghz_scene_receiver_info_draw_widget(SubGhz* subghz) {
AlignTop,
FontSecondary,
furi_string_get_cstr(modulation_str));
subghz_protocol_decoder_base_get_string(subghz->txrx->decoder_result, text);
subghz_protocol_decoder_base_get_string(subghz_txrx_get_decoder(subghz->txrx), text);
widget_add_string_multiline_element(
subghz->widget, 0, 0, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(text));

View file

@ -15,7 +15,7 @@ void subghz_scene_transmitter_callback(SubGhzCustomEvent event, void* context) {
bool subghz_scene_transmitter_update_data_show(void* context) {
SubGhz* subghz = context;
bool ret = false;
if(subghz->txrx->decoder_result) {
if(subghz_txrx_get_decoder(subghz->txrx)) {
FuriString* key_str = furi_string_alloc();
FuriString* frequency_str = furi_string_alloc();
FuriString* modulation_str = furi_string_alloc();
@ -23,16 +23,17 @@ bool subghz_scene_transmitter_update_data_show(void* context) {
bool show_button = false;
if(subghz_protocol_decoder_base_deserialize(
subghz->txrx->decoder_result, subghz_txtx_get_fff_data(subghz->txrx)) ==
subghz_txrx_get_decoder(subghz->txrx), subghz_txtx_get_fff_data(subghz->txrx)) ==
SubGhzProtocolStatusOk) {
subghz_protocol_decoder_base_get_string(subghz->txrx->decoder_result, key_str);
subghz_protocol_decoder_base_get_string(
subghz_txrx_get_decoder(subghz->txrx), key_str);
if((subghz->txrx->decoder_result->protocol->flag & SubGhzProtocolFlag_Send) ==
SubGhzProtocolFlag_Send) {
show_button = true;
}
subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
subghz_get_frequency_modulation(subghz->txrx, frequency_str, modulation_str, false);
subghz_view_transmitter_add_data_to_show(
subghz->subghz_transmitter,
furi_string_get_cstr(key_str),

View file

@ -18,20 +18,6 @@
#define TAG "SubGhz"
void subghz_get_frequency_modulation(SubGhz* subghz, FuriString* frequency, FuriString* modulation) {
furi_assert(subghz);
if(frequency != NULL) {
furi_string_printf(
frequency,
"%03ld.%02ld",
subghz->txrx->preset->frequency / 1000000 % 1000,
subghz->txrx->preset->frequency / 10000 % 100);
}
if(modulation != NULL) {
furi_string_printf(modulation, "%.2s", furi_string_get_cstr(subghz->txrx->preset->name));
}
}
void subghz_dialog_message_show_only_rx(SubGhz* subghz) {
DialogsApp* dialogs = subghz->dialogs;
DialogMessage* message = dialog_message_alloc();
@ -97,7 +83,6 @@ bool subghz_key_load(SubGhz* subghz, const char* file_path, bool show_dialog) {
load_key_state = SubGhzLoadKeyStateOnlyRx;
break;
}
//subghz->txrx->preset->frequency = temp_data32;
//Load preset
if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
@ -105,31 +90,31 @@ bool subghz_key_load(SubGhz* subghz, const char* file_path, bool show_dialog) {
break;
}
if(!subghz_set_preset(subghz->txrx, furi_string_get_cstr(temp_str))) {
furi_string_set_str(
temp_str, subghz_set_preset(subghz->txrx, furi_string_get_cstr(temp_str)));
if(temp_str == NULL) {
break;
}
if(!strcmp(furi_string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
if(!strcmp(furi_string_get_cstr(temp_str), "CUSTOM")) {
//Todo add Custom_preset_module
//delete preset if it already exists
subghz_setting_delete_custom_preset(
subghz_txrx_get_setting(subghz->txrx),
furi_string_get_cstr(subghz->txrx->preset->name));
subghz_txrx_get_setting(subghz->txrx), furi_string_get_cstr(temp_str));
//load custom preset from file
if(!subghz_setting_load_custom_preset(
subghz_txrx_get_setting(subghz->txrx),
furi_string_get_cstr(subghz->txrx->preset->name),
furi_string_get_cstr(temp_str),
fff_data_file)) {
FURI_LOG_E(TAG, "Missing Custom preset");
break;
}
}
size_t preset_index = subghz_setting_get_inx_preset_by_name(
subghz_txrx_get_setting(subghz->txrx),
furi_string_get_cstr(subghz->txrx->preset->name));
subghz_txrx_get_setting(subghz->txrx), furi_string_get_cstr(temp_str));
subghz_preset_init(
subghz->txrx,
furi_string_get_cstr(subghz->txrx->preset->name),
furi_string_get_cstr(temp_str),
temp_data32,
subghz_setting_get_preset_data(subghz_txrx_get_setting(subghz->txrx), preset_index),
subghz_setting_get_preset_data_size(
@ -151,11 +136,10 @@ bool subghz_key_load(SubGhz* subghz, const char* file_path, bool show_dialog) {
flipper_format_get_raw_stream(subghz_txtx_get_fff_data(subghz->txrx)));
}
subghz->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver, furi_string_get_cstr(temp_str));
if(subghz->txrx->decoder_result) {
if(subghz_txrx_load_decoder_by_name_protocol(
subghz->txrx, furi_string_get_cstr(temp_str))) {
SubGhzProtocolStatus status = subghz_protocol_decoder_base_deserialize(
subghz->txrx->decoder_result, subghz_txtx_get_fff_data(subghz->txrx));
subghz_txrx_get_decoder(subghz->txrx), subghz_txtx_get_fff_data(subghz->txrx));
if(status != SubGhzProtocolStatusOk) {
load_key_state = SubGhzLoadKeyStateProtocolDescriptionErr;
break;

View file

@ -118,8 +118,6 @@ struct SubGhz {
void* rpc_ctx;
};
void subghz_get_frequency_modulation(SubGhz* subghz, FuriString* frequency, FuriString* modulation);
void subghz_blink_start(SubGhz* instance);
void subghz_blink_stop(SubGhz* instance);

View file

@ -15,22 +15,45 @@ void subghz_preset_init(
txrx->preset->data_size = preset_data_size;
}
bool subghz_set_preset(SubGhzTxRx* txrx, const char* preset) {
void subghz_get_frequency_modulation(
SubGhzTxRx* txrx,
FuriString* frequency,
FuriString* modulation,
bool long_name) {
furi_assert(txrx);
if(frequency != NULL) {
furi_string_printf(
frequency,
"%03ld.%02ld",
txrx->preset->frequency / 1000000 % 1000,
txrx->preset->frequency / 10000 % 100);
}
if(modulation != NULL) {
if(long_name) {
furi_string_printf(modulation, "%s", furi_string_get_cstr(txrx->preset->name));
} else {
furi_string_printf(modulation, "%.2s", furi_string_get_cstr(txrx->preset->name));
}
}
}
const char* subghz_set_preset(SubGhzTxRx* txrx, const char* preset) {
UNUSED(txrx);
const char* preset_name = NULL;
if(!strcmp(preset, "FuriHalSubGhzPresetOok270Async")) {
furi_string_set(txrx->preset->name, "AM270");
preset_name = "AM270";
} else if(!strcmp(preset, "FuriHalSubGhzPresetOok650Async")) {
furi_string_set(txrx->preset->name, "AM650");
preset_name = "AM650";
} else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev238Async")) {
furi_string_set(txrx->preset->name, "FM238");
preset_name = "FM238";
} else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev476Async")) {
furi_string_set(txrx->preset->name, "FM476");
preset_name = "FM476";
} else if(!strcmp(preset, "FuriHalSubGhzPresetCustom")) {
furi_string_set(txrx->preset->name, "CUSTOM");
preset_name = "CUSTOM";
} else {
FURI_LOG_E(TAG, "Unknown preset");
return false;
}
return true;
return preset_name;
}
void subghz_begin(SubGhzTxRx* txrx, uint8_t* preset_data) {
@ -385,6 +408,24 @@ SubGhzSpeakerState subghz_speaker_get_state(SubGhzTxRx* txrx) {
return txrx->speaker_state;
}
bool subghz_txrx_load_decoder_by_name_protocol(SubGhzTxRx* txrx, const char* name_protocol) {
furi_assert(txrx);
furi_assert(name_protocol);
bool res = false;
txrx->decoder_result = NULL;
txrx->decoder_result =
subghz_receiver_search_decoder_base_by_name(txrx->receiver, name_protocol);
if(txrx->decoder_result) {
res = true;
}
return res;
}
SubGhzProtocolDecoderBase* subghz_txrx_get_decoder(SubGhzTxRx* txrx) {
furi_assert(txrx);
return txrx->decoder_result;
}
//#############Create new Key##############
#include <lib/subghz/protocols/protocol_items.h>
#include <lib/subghz/protocols/keeloq.h>

View file

@ -47,7 +47,12 @@ void subghz_preset_init(
uint8_t* preset_data,
size_t preset_data_size);
bool subghz_set_preset(SubGhzTxRx* txrx, const char* preset);
const char* subghz_set_preset(SubGhzTxRx* txrx, const char* preset);
void subghz_get_frequency_modulation(
SubGhzTxRx* txrx,
FuriString* frequency,
FuriString* modulation,
bool long_name);
void subghz_begin(SubGhzTxRx* txrx, uint8_t* preset_data);
uint32_t subghz_rx(SubGhzTxRx* txrx, uint32_t frequency);
bool subghz_tx_start(SubGhzTxRx* txrx, FlipperFormat* flipper_format);
@ -69,6 +74,8 @@ void subghz_speaker_mute(SubGhzTxRx* txrx);
void subghz_speaker_unmute(SubGhzTxRx* txrx);
void subghz_speaker_set_state(SubGhzTxRx* txrx, SubGhzSpeakerState state);
SubGhzSpeakerState subghz_speaker_get_state(SubGhzTxRx* txrx);
bool subghz_txrx_load_decoder_by_name_protocol(SubGhzTxRx* txrx, const char* name_protocol);
SubGhzProtocolDecoderBase* subghz_txrx_get_decoder(SubGhzTxRx* txrx);
void subghz_txrx_need_save_callback_set(
SubGhzTxRx* txrx,