unleashed-firmware/applications/main/subghz/scenes/subghz_scene_decode_raw.c

280 lines
11 KiB
C
Raw Normal View History

2022-08-28 04:46:59 +03:00
#include "../subghz_i.h"
#include "../views/receiver.h"
2022-09-15 06:45:19 +03:00
#include <lib/subghz/protocols/raw.h>
2022-08-28 04:46:59 +03:00
#include <lib/subghz/subghz_file_encoder_worker.h>
#define TAG "SubGhzDecodeRaw"
#define SAMPLES_TO_READ_PER_TICK 400
// TODO:
// [X] Remember RAW file after decoding
// [X] Decode in tick events instead of on_enter
// [X] Make "Config" label optional in subghz_view_receiver_draw (../views/receiver.c)
// [X] Make "Scanning..." label optional in subghz_view_receiver_draw (../views/receiver.c)
// [X] Add Decoding logo
// [ ] Design nicer Decoding logo
// [X] Check progress in stream_buffer, instead of raw stream
// [X] Blink led while decoding
// [X] Stop rx blink (blue, fast) on history item view
// [X] Don't reparse file on back
// [X] Fix: RX animation+LED returning from decoded detail view
// [X] Find good value for SAMPLES_TO_READ_PER_TICK
// [X] Fix: read errors (slow flash) after aborting decode read
static void subghz_scene_receiver_update_statusbar(void* context) {
SubGhz* subghz = context;
2023-05-05 03:51:16 +03:00
FuriString* history_stat_str = furi_string_alloc();
2023-05-09 15:11:54 +03:00
if(!subghz_history_get_text_space_left(subghz->history, history_stat_str)) {
2023-05-05 03:51:16 +03:00
FuriString* frequency_str = furi_string_alloc();
FuriString* modulation_str = furi_string_alloc();
2022-08-28 04:46:59 +03:00
subghz_txrx_get_frequency_and_modulation(
subghz->txrx, frequency_str, modulation_str, false);
2022-08-28 04:46:59 +03:00
subghz_view_receiver_add_data_statusbar(
subghz->subghz_receiver,
2022-10-05 21:27:13 +03:00
furi_string_get_cstr(frequency_str),
furi_string_get_cstr(modulation_str),
furi_string_get_cstr(history_stat_str));
2022-08-28 04:46:59 +03:00
2022-10-05 21:27:13 +03:00
furi_string_free(frequency_str);
furi_string_free(modulation_str);
2022-08-28 04:46:59 +03:00
} else {
subghz_view_receiver_add_data_statusbar(
2022-10-05 21:27:13 +03:00
subghz->subghz_receiver, furi_string_get_cstr(history_stat_str), "", "");
2022-08-28 04:46:59 +03:00
}
2022-10-05 21:27:13 +03:00
furi_string_free(history_stat_str);
2022-08-28 04:46:59 +03:00
}
void subghz_scene_decode_raw_callback(SubGhzCustomEvent event, void* context) {
furi_assert(context);
SubGhz* subghz = context;
view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
}
static void subghz_scene_add_to_history_callback(
SubGhzReceiver* receiver,
SubGhzProtocolDecoderBase* decoder_base,
void* context) {
furi_assert(context);
SubGhz* subghz = context;
2023-05-05 03:51:16 +03:00
FuriString* item_name = furi_string_alloc();
FuriString* item_time = furi_string_alloc();
2023-05-09 15:11:54 +03:00
uint16_t idx = subghz_history_get_item(subghz->history);
2023-05-09 19:10:56 +03:00
SubGhzRadioPreset preset = subghz_txrx_get_preset(subghz->txrx);
2022-08-28 04:46:59 +03:00
2023-05-09 15:11:54 +03:00
if(subghz_history_add_to_history(subghz->history, decoder_base, &preset)) {
2023-04-21 03:08:38 +03:00
furi_string_reset(item_name);
furi_string_reset(item_time);
2022-08-28 04:46:59 +03:00
subghz->state_notifications = SubGhzNotificationStateRxDone;
2023-05-09 15:11:54 +03:00
subghz_history_get_text_item_menu(subghz->history, item_name, idx);
subghz_history_get_time_item_menu(subghz->history, item_time, idx);
2022-08-28 04:46:59 +03:00
subghz_view_receiver_add_item_to_menu(
subghz->subghz_receiver,
2023-04-21 03:08:38 +03:00
furi_string_get_cstr(item_name),
furi_string_get_cstr(item_time),
2023-05-09 15:11:54 +03:00
subghz_history_get_type_protocol(subghz->history, idx));
2022-08-28 04:46:59 +03:00
subghz_scene_receiver_update_statusbar(subghz);
}
subghz_receiver_reset(receiver);
2023-04-21 03:08:38 +03:00
furi_string_free(item_name);
furi_string_free(item_time);
2022-08-28 04:46:59 +03:00
}
bool subghz_scene_decode_raw_start(SubGhz* subghz) {
2023-05-05 03:51:16 +03:00
FuriString* file_name = furi_string_alloc();
2022-08-28 04:46:59 +03:00
bool success = false;
do {
2023-05-09 21:54:56 +03:00
if(!flipper_format_rewind(subghz_txrx_get_fff_data(subghz->txrx))) {
2022-08-28 04:46:59 +03:00
FURI_LOG_E(TAG, "Rewind error");
break;
}
if(!flipper_format_read_string(
2023-05-09 21:54:56 +03:00
subghz_txrx_get_fff_data(subghz->txrx), "File_name", file_name)) {
2022-08-28 04:46:59 +03:00
FURI_LOG_E(TAG, "Missing File_name");
break;
}
success = true;
} while(false);
if(success) {
2022-10-05 21:27:13 +03:00
//FURI_LOG_I(TAG, "Listening at \033[0;33m%s\033[0m.", furi_string_get_cstr(file_name));
2022-08-28 04:46:59 +03:00
subghz->decode_raw_file_worker_encoder = subghz_file_encoder_worker_alloc();
if(subghz_file_encoder_worker_start(
subghz->decode_raw_file_worker_encoder, furi_string_get_cstr(file_name))) {
2022-08-28 04:46:59 +03:00
//the worker needs a file in order to open and read part of the file
furi_delay_ms(100);
} else {
success = false;
}
if(!success) {
subghz_file_encoder_worker_free(subghz->decode_raw_file_worker_encoder);
2022-08-28 04:46:59 +03:00
}
}
2022-10-05 21:27:13 +03:00
furi_string_free(file_name);
2022-08-28 04:46:59 +03:00
return success;
}
bool subghz_scene_decode_raw_next(SubGhz* subghz) {
LevelDuration level_duration;
2023-05-09 18:24:25 +03:00
SubGhzReceiver* receiver = subghz_txrx_get_receiver(subghz->txrx);
2022-08-28 04:46:59 +03:00
for(uint32_t read = SAMPLES_TO_READ_PER_TICK; read > 0; --read) {
level_duration =
subghz_file_encoder_worker_get_level_duration(subghz->decode_raw_file_worker_encoder);
2022-08-28 04:46:59 +03:00
if(!level_duration_is_reset(level_duration)) {
bool level = level_duration_get_level(level_duration);
uint32_t duration = level_duration_get_duration(level_duration);
2023-05-09 18:24:25 +03:00
subghz_receiver_decode(receiver, level, duration);
2022-08-28 04:46:59 +03:00
} else {
subghz->decode_raw_state = SubGhzDecodeRawStateLoaded;
2022-08-28 04:46:59 +03:00
subghz->state_notifications = SubGhzNotificationStateIDLE;
2022-11-30 20:03:55 +03:00
subghz_view_receiver_add_data_progress(subghz->subghz_receiver, "Done!");
2022-08-28 04:46:59 +03:00
return false; // No more samples available
}
}
// Update progress info
2023-05-05 03:51:16 +03:00
FuriString* progress_str = furi_string_alloc();
subghz_file_encoder_worker_get_text_progress(
subghz->decode_raw_file_worker_encoder, progress_str);
2022-08-28 04:46:59 +03:00
2022-10-05 21:27:13 +03:00
subghz_view_receiver_add_data_progress(
subghz->subghz_receiver, furi_string_get_cstr(progress_str));
2022-08-28 04:46:59 +03:00
2022-10-05 21:27:13 +03:00
furi_string_free(progress_str);
2022-08-28 04:46:59 +03:00
return true; // More samples available
}
void subghz_scene_decode_raw_on_enter(void* context) {
SubGhz* subghz = context;
2023-05-05 03:51:16 +03:00
FuriString* item_name = furi_string_alloc();
FuriString* item_time = furi_string_alloc();
2022-08-28 04:46:59 +03:00
subghz_view_receiver_set_lock(
subghz->subghz_receiver, subghz_is_locked(subghz)); //TODO Doesn't matter in DecodeRAW
2022-08-28 04:46:59 +03:00
subghz_view_receiver_set_mode(subghz->subghz_receiver, SubGhzViewReceiverModeFile);
subghz_view_receiver_set_callback(
subghz->subghz_receiver, subghz_scene_decode_raw_callback, subghz);
2023-05-09 18:24:25 +03:00
subghz_txrx_set_rx_calback(subghz->txrx, subghz_scene_add_to_history_callback, subghz);
2022-08-28 04:46:59 +03:00
2023-05-09 15:58:56 +03:00
subghz_txrx_receiver_set_filter(subghz->txrx, SubGhzProtocolFlag_Decodable);
2022-09-18 01:48:10 +03:00
if(subghz->decode_raw_state == SubGhzDecodeRawStateStart) {
2022-08-28 04:46:59 +03:00
//Decode RAW to history
2023-05-09 15:11:54 +03:00
subghz_history_reset(subghz->history);
2022-08-28 04:46:59 +03:00
if(subghz_scene_decode_raw_start(subghz)) {
subghz->decode_raw_state = SubGhzDecodeRawStateLoading;
2022-08-28 04:46:59 +03:00
subghz->state_notifications = SubGhzNotificationStateRx;
}
} else {
//Load history to receiver
subghz_view_receiver_exit(subghz->subghz_receiver);
2023-05-09 15:11:54 +03:00
for(uint8_t i = 0; i < subghz_history_get_item(subghz->history); i++) {
2023-04-21 03:08:38 +03:00
furi_string_reset(item_name);
furi_string_reset(item_time);
2023-05-09 15:11:54 +03:00
subghz_history_get_text_item_menu(subghz->history, item_name, i);
subghz_history_get_time_item_menu(subghz->history, item_time, i);
2022-08-28 04:46:59 +03:00
subghz_view_receiver_add_item_to_menu(
subghz->subghz_receiver,
2023-04-21 03:08:38 +03:00
furi_string_get_cstr(item_name),
furi_string_get_cstr(item_time),
2023-05-09 15:11:54 +03:00
subghz_history_get_type_protocol(subghz->history, i));
2022-08-28 04:46:59 +03:00
}
2023-04-21 03:08:38 +03:00
furi_string_free(item_name);
furi_string_free(item_time);
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, subghz->idx_menu_chosen);
2022-08-28 04:46:59 +03:00
}
subghz_scene_receiver_update_statusbar(subghz);
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdReceiver);
}
bool subghz_scene_decode_raw_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case SubGhzCustomEventViewReceiverBack:
subghz->decode_raw_state = SubGhzDecodeRawStateStart;
subghz->idx_menu_chosen = 0;
subghz->in_decoder_scene = false;
subghz->in_decoder_scene_skip = false;
2023-05-09 18:24:25 +03:00
subghz_txrx_set_rx_calback(subghz->txrx, NULL, subghz);
2022-08-28 04:46:59 +03:00
if(subghz_file_encoder_worker_is_running(subghz->decode_raw_file_worker_encoder)) {
subghz_file_encoder_worker_stop(subghz->decode_raw_file_worker_encoder);
2022-08-28 04:46:59 +03:00
}
subghz_file_encoder_worker_free(subghz->decode_raw_file_worker_encoder);
2022-08-28 04:46:59 +03:00
subghz->state_notifications = SubGhzNotificationStateIDLE;
2022-09-18 01:48:10 +03:00
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerNoSet);
2022-08-28 04:46:59 +03:00
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneMoreRAW);
consumed = true;
break;
case SubGhzCustomEventViewReceiverOK:
subghz->idx_menu_chosen = subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
2022-08-28 04:46:59 +03:00
subghz->state_notifications = SubGhzNotificationStateIDLE;
subghz->in_decoder_scene = true;
2022-08-28 04:46:59 +03:00
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverInfo);
consumed = true;
break;
case SubGhzCustomEventViewReceiverConfig:
2022-09-16 21:37:01 +03:00
FURI_LOG_W(TAG, "No config options");
2022-08-28 04:46:59 +03:00
consumed = true;
break;
case SubGhzCustomEventViewReceiverOffDisplay:
notification_message(subghz->notifications, &sequence_display_backlight_off);
consumed = true;
break;
case SubGhzCustomEventViewReceiverUnlock:
subghz_unlock(subghz); //TODO There is no such event in DecodeRAW
2022-08-28 04:46:59 +03:00
consumed = true;
break;
default:
break;
}
} else if(event.type == SceneManagerEventTypeTick) {
switch(subghz->state_notifications) {
case SubGhzNotificationStateRx:
notification_message(subghz->notifications, &sequence_blink_cyan_10);
break;
case SubGhzNotificationStateRxDone:
notification_message(subghz->notifications, &subghz_sequence_rx);
2022-08-28 04:46:59 +03:00
subghz->state_notifications = SubGhzNotificationStateRx;
break;
default:
break;
}
switch(subghz->decode_raw_state) {
2022-08-28 04:46:59 +03:00
case SubGhzDecodeRawStateLoading:
subghz_scene_decode_raw_next(subghz);
break;
default:
break;
}
}
return consumed;
}
void subghz_scene_decode_raw_on_exit(void* context) {
UNUSED(context);
}