unleashed-firmware/applications/main/subghz_remote/subghz_remote_app.c

1070 lines
36 KiB
C
Raw Normal View History

2022-06-01 21:40:20 +03:00
#include <furi.h>
2022-08-01 23:20:04 +03:00
2022-06-01 21:40:20 +03:00
#include <gui/gui.h>
#include <input/input.h>
#include <dialogs/dialogs.h>
2022-08-01 23:20:04 +03:00
#include <storage/storage.h>
#include <notification/notification.h>
2022-06-01 21:40:20 +03:00
#include <notification/notification_messages.h>
#include <assets_icons.h>
2022-08-01 23:20:04 +03:00
#include <flipper_format/flipper_format_i.h>
#include <lib/toolbox/path.h>
2022-09-14 21:04:04 +03:00
#include <applications/main/subghz/subghz_i.h>
2022-08-01 23:20:04 +03:00
#include <lib/subghz/protocols/raw.h>
#include <lib/subghz/protocols/protocol_items.h>
#include <lib/subghz/types.h>
#include <lib/subghz/protocols/keeloq.h>
#include <lib/subghz/protocols/star_line.h>
#include <lib/subghz/blocks/custom_btn.h>
#define SUBREMOTEMAP_FOLDER "/ext/subghz_remote"
#define SUBREMOTEMAP_EXTENSION ".txt"
2022-06-01 21:40:20 +03:00
#define TAG "SubGHzRemote"
typedef struct {
uint32_t frequency;
2022-10-05 21:27:13 +03:00
FuriString* name;
2022-10-05 21:27:13 +03:00
FuriString* protocol;
uint32_t repeat;
uint8_t* data;
size_t data_size;
SubGhzProtocolDecoderBase* decoder;
} SubRemotePreset;
2022-06-01 21:40:20 +03:00
typedef struct {
2022-07-20 19:01:38 +03:00
FuriMutex* model_mutex;
2022-06-01 21:40:20 +03:00
2022-07-20 19:01:38 +03:00
FuriMessageQueue* input_queue;
2022-06-01 21:40:20 +03:00
ViewPort* view_port;
Gui* gui;
SubGhzSetting* setting;
SubGhzEnvironment* environment;
SubGhzReceiver* subghz_receiver;
NotificationApp* notification;
SubRemotePreset* txpreset;
2022-10-05 21:27:13 +03:00
FuriString* up_file;
FuriString* down_file;
FuriString* left_file;
FuriString* right_file;
FuriString* ok_file;
2022-06-01 21:40:20 +03:00
2022-10-05 21:27:13 +03:00
FuriString* file_path;
2022-08-01 23:20:04 +03:00
2022-06-01 21:40:20 +03:00
char* up_label;
char* down_label;
char* left_label;
char* right_label;
char* ok_label;
int up_enabled;
int down_enabled;
int left_enabled;
int right_enabled;
int ok_enabled;
char* send_status;
int send_status_c;
int processing;
SubGhzTransmitter* tx_transmitter;
FlipperFormat* tx_fff_data;
const char* tx_file_path;
2022-06-01 21:40:20 +03:00
int button;
int file_result;
bool tx_not_allowed;
2022-06-01 21:40:20 +03:00
2022-10-05 21:27:13 +03:00
FuriString* signal;
} SubGHzRemote;
2022-06-01 21:40:20 +03:00
SubRemotePreset* subghz_remote_preset_alloc(void) {
SubRemotePreset* preset = malloc(sizeof(SubRemotePreset));
2022-10-05 21:27:13 +03:00
preset->name = furi_string_alloc();
preset->protocol = furi_string_alloc();
preset->repeat = 200;
return preset;
}
void subghz_remote_preset_free(SubRemotePreset* preset) {
2022-10-05 21:27:13 +03:00
furi_string_free(preset->name);
furi_string_free(preset->protocol);
free(preset);
}
2022-06-01 21:40:20 +03:00
static char* char_to_str(char* str, int i) {
char* converted = malloc(sizeof(char) * i + 1);
memcpy(converted, str, i);
converted[i] = '\0';
return converted;
}
2022-11-10 12:15:10 +03:00
2022-06-01 21:40:20 +03:00
//get filename without path
static char* extract_filename(const char* name, int len) {
2022-10-05 21:27:13 +03:00
FuriString* tmp;
tmp = furi_string_alloc();
2022-06-01 21:40:20 +03:00
//remove path
path_extract_filename_no_ext(name, tmp);
2022-10-05 21:27:13 +03:00
return char_to_str((char*)furi_string_get_cstr(tmp), len);
2022-06-01 21:40:20 +03:00
}
2022-11-10 12:15:10 +03:00
static void cfg_read_file_path(
FlipperFormat* fff_file,
FuriString* text_file_path,
char** text_file_label,
const char* read_key,
int* is_enabled) {
if(!flipper_format_read_string(fff_file, read_key, text_file_path)) {
FURI_LOG_W(TAG, "Could not read %s string", read_key);
*text_file_label = "N/A";
*is_enabled = 0;
} else {
*text_file_label = extract_filename(furi_string_get_cstr(text_file_path), 16);
FURI_LOG_D(TAG, "%s file: %s", read_key, furi_string_get_cstr(text_file_path));
*is_enabled = 1;
}
flipper_format_rewind(fff_file);
}
static void cfg_read_file_label(
FlipperFormat* fff_file,
char** text_file_label,
const char* read_key,
bool is_enabled) {
FuriString* temp_label = furi_string_alloc();
if(!flipper_format_read_string(fff_file, read_key, temp_label)) {
FURI_LOG_W(TAG, "Could not read %s string", read_key);
} else {
if(is_enabled == 1) {
*text_file_label = char_to_str((char*)furi_string_get_cstr(temp_label), 16);
}
FURI_LOG_D(TAG, "%s label: %s", read_key, *text_file_label);
}
flipper_format_rewind(fff_file);
furi_string_free(temp_label);
}
2022-06-01 21:40:20 +03:00
/*
* check that map file exists
* assign variables to values within map file
* set missing filenames to N/A
* set filename as label if label definitions are missing
* set error flag if all buttons are N/A
* set error flag if missing map file
*/
2022-06-01 21:40:20 +03:00
void subghz_remote_cfg_set_check(SubGHzRemote* app, FuriString* file_name) {
Storage* storage = furi_record_open(RECORD_STORAGE);
2022-06-01 21:40:20 +03:00
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
app->file_result = 1;
2022-06-01 21:40:20 +03:00
app->up_enabled = 0;
app->down_enabled = 0;
app->left_enabled = 0;
app->right_enabled = 0;
app->ok_enabled = 0;
2022-06-01 21:40:20 +03:00
//check that map file exists
2022-10-05 21:27:13 +03:00
if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_E(TAG, "Could not open MAP file %s", furi_string_get_cstr(file_name));
} else {
//Filename Assignment/Check Start
2022-06-01 21:40:20 +03:00
//assign variables to values within map file
//set missing filenames to N/A
2022-11-10 12:15:10 +03:00
cfg_read_file_path(fff_data_file, app->up_file, &app->up_label, "UP", &app->up_enabled);
2022-06-01 21:40:20 +03:00
2022-11-10 12:15:10 +03:00
cfg_read_file_path(
fff_data_file, app->down_file, &app->down_label, "DOWN", &app->down_enabled);
2022-06-01 21:40:20 +03:00
2022-11-10 12:15:10 +03:00
cfg_read_file_path(
fff_data_file, app->left_file, &app->left_label, "LEFT", &app->left_enabled);
2022-06-01 21:40:20 +03:00
2022-11-10 12:15:10 +03:00
cfg_read_file_path(
fff_data_file, app->right_file, &app->right_label, "RIGHT", &app->right_enabled);
2022-06-01 21:40:20 +03:00
2022-11-10 12:15:10 +03:00
cfg_read_file_path(fff_data_file, app->ok_file, &app->ok_label, "OK", &app->ok_enabled);
2022-06-01 21:40:20 +03:00
2022-06-12 05:42:37 +03:00
//File definitions are done.
//File checks will follow after label assignment in order to close the universal_rf_map file without the need to reopen it again.
2022-06-01 21:40:20 +03:00
//Label Assignment/Check Start
2022-11-10 12:15:10 +03:00
cfg_read_file_label(fff_data_file, &app->up_label, "ULABEL", app->up_enabled);
cfg_read_file_label(fff_data_file, &app->down_label, "DLABEL", app->down_enabled);
cfg_read_file_label(fff_data_file, &app->left_label, "LLABEL", app->left_enabled);
cfg_read_file_label(fff_data_file, &app->right_label, "RLABEL", app->right_enabled);
cfg_read_file_label(fff_data_file, &app->ok_label, "OKLABEL", app->ok_enabled);
2022-06-01 21:40:20 +03:00
}
flipper_format_file_close(fff_data_file);
2022-06-01 21:40:20 +03:00
flipper_format_free(fff_data_file);
//File Existence Check
//Check each file definition if not already set to "N/A"
//determine if files exist.
//determine whether or not to continue to launch app with missing variables
//if 5 files are missing, throw error
//if button is still enabled, check that file exists
if(app->up_enabled == 1) {
furi_string_set(file_name, app->up_file);
fff_data_file = flipper_format_file_alloc(storage);
if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_W(TAG, "Could not open UP file %s", furi_string_get_cstr(file_name));
//disable button, and set label to "N/A"
app->up_enabled = 0;
app->up_label = "N/A";
}
//close the file
flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
}
if(app->down_enabled == 1) {
furi_string_set(file_name, app->down_file);
fff_data_file = flipper_format_file_alloc(storage);
if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_W(TAG, "Could not open DOWN file %s", furi_string_get_cstr(file_name));
app->down_enabled = 0;
app->down_label = "N/A";
}
flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
}
if(app->left_enabled == 1) {
furi_string_set(file_name, app->left_file);
fff_data_file = flipper_format_file_alloc(storage);
if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_W(TAG, "Could not open LEFT file %s", furi_string_get_cstr(file_name));
app->left_enabled = 0;
app->left_label = "N/A";
}
flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
}
if(app->right_enabled == 1) {
furi_string_set(file_name, app->right_file);
fff_data_file = flipper_format_file_alloc(storage);
if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_W(TAG, "Could not open RIGHT file %s", furi_string_get_cstr(file_name));
app->right_enabled = 0;
app->right_label = "N/A";
}
flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
}
if(app->ok_enabled == 1) {
furi_string_set(file_name, app->ok_file);
fff_data_file = flipper_format_file_alloc(storage);
if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_W(TAG, "Could not open OK file %s", furi_string_get_cstr(file_name));
app->ok_enabled = 0;
app->ok_label = "N/A";
}
flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
}
furi_record_close(RECORD_STORAGE);
if(app->up_enabled == 0 && app->down_enabled == 0 && app->left_enabled == 0 &&
app->right_enabled == 0 && app->ok_enabled == 0) {
app->file_result = 1;
} else {
app->file_result = 2;
}
2022-06-01 21:40:20 +03:00
}
static void subghz_remote_end_send(SubGHzRemote* app) {
2022-06-01 21:40:20 +03:00
app->processing = 0;
}
bool subghz_remote_set_preset(SubRemotePreset* p, const char* preset) {
if(!strcmp(preset, "FuriHalSubGhzPresetOok270Async")) {
2022-10-05 21:27:13 +03:00
furi_string_set(p->name, "AM270");
} else if(!strcmp(preset, "FuriHalSubGhzPresetOok650Async")) {
2022-10-05 21:27:13 +03:00
furi_string_set(p->name, "AM650");
} else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev238Async")) {
2022-10-05 21:27:13 +03:00
furi_string_set(p->name, "FM238");
} else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev476Async")) {
2022-10-05 21:27:13 +03:00
furi_string_set(p->name, "FM476");
} else if(!strcmp(preset, "FuriHalSubGhzPresetCustom")) {
FURI_LOG_E(TAG, "Custom preset unsupported now");
return false;
2022-10-05 21:27:13 +03:00
// furi_string_set(p->name, "CUSTOM");
} else {
FURI_LOG_E(TAG, "Unsupported preset");
return false;
}
return true;
}
bool subghz_remote_key_load(
SubRemotePreset* preset,
FlipperFormat* fff_file,
FlipperFormat* fff_data,
SubGhzSetting* setting,
SubGhzReceiver* receiver,
const char* path) {
//
if(!flipper_format_rewind(fff_file)) {
FURI_LOG_E(TAG, "Rewind error");
return false;
}
2022-10-05 21:27:13 +03:00
FuriString* temp_str;
temp_str = furi_string_alloc();
bool res = false;
do {
// load frequency from file
if(!flipper_format_read_uint32(fff_file, "Frequency", &preset->frequency, 1)) {
FURI_LOG_W(TAG, "Cannot read frequency. Defaulting to 433.92 MHz");
preset->frequency = 433920000;
}
// load preset from file
if(!flipper_format_read_string(fff_file, "Preset", temp_str)) {
FURI_LOG_W(TAG, "Could not read Preset. Defaulting to Ook650Async");
2022-10-05 21:27:13 +03:00
furi_string_set(temp_str, "FuriHalSubGhzPresetOok650Async");
}
if(!subghz_remote_set_preset(preset, furi_string_get_cstr(temp_str))) {
FURI_LOG_E(TAG, "Could not set preset");
break;
}
2022-10-05 21:27:13 +03:00
if(!strcmp(furi_string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
// TODO: check if preset is custom
FURI_LOG_E(TAG, "Could not use custom preset");
break;
}
size_t preset_index =
2022-10-05 21:27:13 +03:00
subghz_setting_get_inx_preset_by_name(setting, furi_string_get_cstr(preset->name));
preset->data = subghz_setting_get_preset_data(setting, preset_index);
preset->data_size = subghz_setting_get_preset_data_size(setting, preset_index);
// load protocol from file
if(!flipper_format_read_string(fff_file, "Protocol", preset->protocol)) {
FURI_LOG_E(TAG, "Could not read Protocol.");
break;
}
2022-10-05 21:27:13 +03:00
if(!furi_string_cmp_str(preset->protocol, "RAW")) {
subghz_protocol_raw_gen_fff_data(fff_data, path);
} else {
stream_copy_full(
flipper_format_get_raw_stream(fff_file), flipper_format_get_raw_stream(fff_data));
}
2022-08-19 04:38:35 +03:00
// repeat
if(!flipper_format_insert_or_update_uint32(fff_file, "Repeat", &preset->repeat, 1)) {
FURI_LOG_E(TAG, "Unable to insert or update Repeat");
break;
}
preset->decoder = subghz_receiver_search_decoder_base_by_name(
2022-10-05 21:27:13 +03:00
receiver, furi_string_get_cstr(preset->protocol));
2022-08-19 04:38:35 +03:00
if(preset->decoder) {
SubGhzProtocolStatus status =
subghz_protocol_decoder_base_deserialize(preset->decoder, fff_data);
if(status != SubGhzProtocolStatusOk) {
FURI_LOG_E(TAG, "Protocol deserialize failed, status = %d", status);
break;
}
} else {
2022-10-05 21:27:13 +03:00
FURI_LOG_E(TAG, "Protocol %s not found", furi_string_get_cstr(temp_str));
}
res = true;
} while(0);
2022-10-05 21:27:13 +03:00
furi_string_free(temp_str);
return res;
}
// method modified from subghz_i.c
// https://github.com/flipperdevices/flipperzero-firmware/blob/b0daa601ad5b87427a45f9089c8b403a01f72c2a/applications/subghz/subghz_i.c#L417-L456
bool subghz_remote_save_protocol_to_file(FlipperFormat* fff_file, const char* dev_file_name) {
furi_assert(fff_file);
furi_assert(dev_file_name);
Storage* storage = furi_record_open(RECORD_STORAGE);
Stream* flipper_format_stream = flipper_format_get_raw_stream(fff_file);
bool saved = false;
2022-10-05 21:27:13 +03:00
FuriString* file_dir;
file_dir = furi_string_alloc();
path_extract_dirname(dev_file_name, file_dir);
do {
flipper_format_delete_key(fff_file, "Repeat");
//flipper_format_delete_key(fff_file, "Manufacture");
2022-10-05 21:27:13 +03:00
if(!storage_simply_mkdir(storage, furi_string_get_cstr(file_dir))) {
2022-08-15 00:52:05 +03:00
FURI_LOG_E(TAG, "(save) Cannot mkdir");
break;
}
if(!storage_simply_remove(storage, dev_file_name)) {
2022-08-15 00:52:05 +03:00
FURI_LOG_E(TAG, "(save) Cannot remove");
break;
}
stream_seek(flipper_format_stream, 0, StreamOffsetFromStart);
stream_save_to_file(flipper_format_stream, storage, dev_file_name, FSOM_CREATE_ALWAYS);
saved = true;
FURI_LOG_D(TAG, "(save) OK Save");
} while(0);
2022-10-05 21:27:13 +03:00
furi_string_free(file_dir);
furi_record_close(RECORD_STORAGE);
return saved;
}
void subghz_remote_tx_stop(SubGHzRemote* app) {
if(app->processing == 0) {
return;
}
2022-10-05 21:27:13 +03:00
if(!furi_string_cmp_str(app->txpreset->protocol, "RAW")) {
while(!furi_hal_subghz_is_async_tx_complete()) {
furi_delay_ms(15);
}
}
//Stop TX
furi_hal_subghz_stop_async_tx();
2022-11-10 12:15:10 +03:00
//FURI_LOG_I(TAG, "TX Done!");
subghz_transmitter_stop(app->tx_transmitter);
FURI_LOG_D(TAG, "Checking if protocol is dynamic");
const SubGhzProtocolRegistry* protocol_registry_items =
subghz_environment_get_protocol_registry(app->environment);
const SubGhzProtocol* proto = subghz_protocol_registry_get_by_name(
protocol_registry_items, furi_string_get_cstr(app->txpreset->protocol));
FURI_LOG_D(TAG, "Protocol-TYPE %d", proto->type);
if(proto && proto->type == SubGhzProtocolTypeDynamic) {
FURI_LOG_D(TAG, "Protocol is dynamic. Saving key");
subghz_remote_save_protocol_to_file(app->tx_fff_data, app->tx_file_path);
keeloq_reset_mfname();
keeloq_reset_kl_type();
keeloq_reset_original_btn();
subghz_custom_btns_reset();
star_line_reset_mfname();
star_line_reset_kl_type();
}
subghz_transmitter_free(app->tx_transmitter);
furi_hal_subghz_idle();
notification_message(app->notification, &sequence_blink_stop);
subghz_remote_preset_free(app->txpreset);
flipper_format_free(app->tx_fff_data);
subghz_remote_end_send(app);
}
static bool subghz_remote_send_sub(SubGHzRemote* app, FlipperFormat* fff_data) {
//
bool res = false;
do {
if(!furi_hal_subghz_is_tx_allowed(app->txpreset->frequency)) {
printf(
"In your settings, only reception on this frequency (%lu) is allowed,\r\n"
"the actual operation of the subghz remote app is not possible\r\n ",
app->txpreset->frequency);
app->tx_not_allowed = true;
subghz_remote_end_send(app);
break;
} else {
app->tx_not_allowed = false;
}
2022-06-01 21:40:20 +03:00
app->tx_transmitter = subghz_transmitter_alloc_init(
2022-10-05 21:27:13 +03:00
app->environment, furi_string_get_cstr(app->txpreset->protocol));
if(!app->tx_transmitter) {
break;
}
2022-06-01 21:40:20 +03:00
subghz_transmitter_deserialize(app->tx_transmitter, fff_data);
2022-06-01 21:40:20 +03:00
furi_hal_subghz_reset();
furi_hal_subghz_idle();
furi_hal_subghz_load_custom_preset(app->txpreset->data);
2023-02-02 22:47:50 +03:00
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeInput, GpioPullNo, GpioSpeedLow);
furi_hal_subghz_idle();
2022-10-26 22:47:32 +03:00
furi_hal_subghz_set_frequency_and_path(app->txpreset->frequency);
2023-02-02 22:47:50 +03:00
furi_hal_gpio_write(furi_hal_subghz.cc1101_g0_pin, false);
furi_hal_gpio_init(
furi_hal_subghz.cc1101_g0_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
2022-06-01 21:40:20 +03:00
if(!furi_hal_subghz_tx()) {
FURI_LOG_E(TAG, "Sending not allowed");
break;
}
2022-06-01 21:40:20 +03:00
2022-11-10 12:15:10 +03:00
//FURI_LOG_I(TAG, "Sending...");
notification_message(app->notification, &sequence_blink_start_magenta);
2022-08-19 04:38:35 +03:00
furi_hal_subghz_start_async_tx(subghz_transmitter_yield, app->tx_transmitter);
2022-08-15 00:52:05 +03:00
res = true;
} while(0);
2022-08-15 00:52:05 +03:00
return res;
}
2022-06-01 21:40:20 +03:00
static void subghz_remote_send_signal(SubGHzRemote* app, Storage* storage, const char* path) {
2022-11-10 12:15:10 +03:00
FURI_LOG_D(TAG, "Sending: %s", path);
2022-06-01 21:40:20 +03:00
app->tx_file_path = path;
2022-06-01 21:40:20 +03:00
app->tx_fff_data = flipper_format_string_alloc();
2022-06-01 21:40:20 +03:00
app->txpreset = subghz_remote_preset_alloc();
2022-06-01 21:40:20 +03:00
// load settings/stream from .sub file
FlipperFormat* fff_file = flipper_format_file_alloc(storage);
bool open_ok = false;
do {
if(!flipper_format_file_open_existing(fff_file, path)) {
FURI_LOG_E(TAG, "Could not open file %s", path);
break;
}
if(!subghz_remote_key_load(
app->txpreset,
fff_file,
app->tx_fff_data,
app->setting,
app->subghz_receiver,
path)) {
FURI_LOG_E(TAG, "Could not load key");
break;
}
open_ok = true;
} while(0);
flipper_format_free(fff_file);
if(!open_ok) {
FURI_LOG_E(TAG, "Could not load file!");
return;
}
2022-06-01 21:40:20 +03:00
subghz_remote_send_sub(app, app->tx_fff_data);
}
2022-06-01 21:40:20 +03:00
static void subghz_remote_process_signal(SubGHzRemote* app, FuriString* signal) {
view_port_update(app->view_port);
2022-06-01 21:40:20 +03:00
2022-11-10 12:15:10 +03:00
FURI_LOG_D(TAG, "signal = %s", furi_string_get_cstr(signal));
2022-10-05 21:27:13 +03:00
if(strlen(furi_string_get_cstr(signal)) > 12) {
Storage* storage = furi_record_open(RECORD_STORAGE);
subghz_remote_send_signal(app, storage, furi_string_get_cstr(signal));
furi_record_close(RECORD_STORAGE);
2022-10-05 21:27:13 +03:00
} else if(strlen(furi_string_get_cstr(signal)) < 10) {
subghz_remote_end_send(app);
2022-06-01 21:40:20 +03:00
}
}
static void render_callback(Canvas* canvas, void* ctx) {
SubGHzRemote* app = ctx;
2022-07-20 19:01:38 +03:00
furi_check(furi_mutex_acquire(app->model_mutex, FuriWaitForever) == FuriStatusOk);
2022-06-01 21:40:20 +03:00
//setup different canvas settings
if(app->file_result == 1) {
//if map has no valid filenames defined
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
2022-08-01 23:20:04 +03:00
canvas_draw_str_aligned(canvas, 62, 5, AlignCenter, AlignTop, "Config is incorrect.");
2022-06-01 21:40:20 +03:00
canvas_set_font(canvas, FontSecondary);
2022-08-01 23:20:04 +03:00
canvas_draw_str_aligned(canvas, 62, 30, AlignCenter, AlignTop, "Please configure map.");
canvas_draw_str_aligned(canvas, 62, 60, AlignCenter, AlignBottom, "Press Back to Exit.");
} else if(app->file_result == 3) {
//if map has no valid filenames defined
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 62, 5, AlignCenter, AlignTop, "Checking config.");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 62, 30, AlignCenter, AlignTop, "If app is stuck...");
canvas_draw_str_aligned(canvas, 62, 60, AlignCenter, AlignBottom, "Press Back to Exit.");
} else if(app->tx_not_allowed) {
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 62, 5, AlignCenter, AlignTop, "Transmission is blocked.");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 62, 15, AlignCenter, AlignTop, "Frequency is outside of");
canvas_draw_str_aligned(canvas, 62, 25, AlignCenter, AlignTop, "default range.");
canvas_draw_str_aligned(canvas, 62, 35, AlignCenter, AlignTop, "Check docs.");
canvas_draw_str_aligned(canvas, 62, 60, AlignCenter, AlignBottom, "Press Back to Exit.");
2022-06-01 21:40:20 +03:00
} else {
//map found, draw all the things
canvas_clear(canvas);
//canvas_set_font(canvas, FontPrimary);
//canvas_draw_str(canvas, 0, 10, "U: ");
//canvas_draw_str(canvas, 0, 20, "L: ");
//canvas_draw_str(canvas, 0, 30, "R: ");
//canvas_draw_str(canvas, 0, 40, "D: ");
//canvas_draw_str(canvas, 0, 50, "Ok: ");
//PNGs are located in assets/icons/SubGHzRemote before compilation
2022-06-01 21:40:20 +03:00
//Icons for Labels
//canvas_draw_icon(canvas, 0, 0, &I_SubGHzRemote_LeftAlignedButtons_9x64);
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 1, 5, &I_ButtonUp_7x4);
canvas_draw_icon(canvas, 1, 15, &I_ButtonDown_7x4);
canvas_draw_icon(canvas, 2, 23, &I_ButtonLeft_4x7);
canvas_draw_icon(canvas, 2, 33, &I_ButtonRight_4x7);
canvas_draw_icon(canvas, 0, 42, &I_Ok_btn_9x9);
canvas_draw_icon(canvas, 0, 53, &I_back_10px);
2022-06-01 21:40:20 +03:00
//Labels
canvas_set_font(canvas, FontSecondary);
2022-08-01 23:20:04 +03:00
canvas_draw_str(canvas, 10, 10, app->up_label);
canvas_draw_str(canvas, 10, 20, app->down_label);
canvas_draw_str(canvas, 10, 30, app->left_label);
canvas_draw_str(canvas, 10, 40, app->right_label);
canvas_draw_str(canvas, 10, 50, app->ok_label);
2022-06-01 21:40:20 +03:00
canvas_draw_str_aligned(canvas, 11, 62, AlignLeft, AlignBottom, "Press=Exit.");
2022-06-01 21:40:20 +03:00
//Status text and indicator
2022-08-01 23:20:04 +03:00
canvas_draw_str_aligned(canvas, 126, 10, AlignRight, AlignBottom, app->send_status);
2022-06-01 21:40:20 +03:00
switch(app->send_status_c) {
case 0:
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
2022-06-01 21:40:20 +03:00
break;
case 1:
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
canvas_draw_icon(canvas, 116, 17, &I_Pin_arrow_up_7x9);
2022-06-01 21:40:20 +03:00
break;
case 2:
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
canvas_draw_icon(canvas, 116, 17, &I_Pin_arrow_down_7x9);
2022-06-01 21:40:20 +03:00
break;
case 3:
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
canvas_draw_icon(canvas, 115, 18, &I_Pin_arrow_right_9x7);
2022-06-01 21:40:20 +03:00
break;
case 4:
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
canvas_draw_icon(canvas, 115, 18, &I_Pin_arrow_left_9x7);
2022-06-01 21:40:20 +03:00
break;
case 5:
2022-08-01 23:20:04 +03:00
canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
canvas_draw_icon(canvas, 116, 18, &I_Pin_star_7x7);
2022-06-01 21:40:20 +03:00
break;
}
//Repeat indicator
//canvas_draw_str_aligned(canvas, 125, 40, AlignRight, AlignBottom, "Repeat:");
//canvas_draw_icon(canvas, 115, 39, &I_SubGHzRemote_Repeat_12x14);
//canvas_draw_str_aligned(canvas, 125, 62, AlignRight, AlignBottom, int_to_char(app->repeat));
2022-06-01 21:40:20 +03:00
}
2022-07-20 19:01:38 +03:00
furi_mutex_release(app->model_mutex);
2022-06-01 21:40:20 +03:00
}
static void input_callback(InputEvent* input_event, void* ctx) {
SubGHzRemote* app = ctx;
2022-08-01 23:20:04 +03:00
furi_message_queue_put(app->input_queue, input_event, 0);
2022-06-01 21:40:20 +03:00
}
void subghz_remote_subghz_alloc(SubGHzRemote* app) {
// load subghz presets
app->setting = subghz_setting_alloc();
subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
// load mfcodes
app->environment = subghz_environment_alloc();
subghz_environment_load_keystore(app->environment, EXT_PATH("subghz/assets/keeloq_mfcodes"));
subghz_environment_load_keystore(
app->environment, EXT_PATH("subghz/assets/keeloq_mfcodes_user"));
subghz_environment_set_came_atomo_rainbow_table_file_name(
app->environment, EXT_PATH("subghz/assets/came_atomo"));
subghz_environment_set_nice_flor_s_rainbow_table_file_name(
app->environment, EXT_PATH("subghz/assets/nice_flor_s"));
subghz_environment_set_protocol_registry(app->environment, (void*)&subghz_protocol_registry);
app->subghz_receiver = subghz_receiver_alloc_init(app->environment);
}
SubGHzRemote* subghz_remote_alloc(void) {
SubGHzRemote* app = malloc(sizeof(SubGHzRemote));
// Enable power for External CC1101 if it is connected
furi_hal_subghz_enable_ext_power();
// Auto switch to internal radio if external radio is not available
furi_delay_ms(15);
if(!furi_hal_subghz_check_radio()) {
furi_hal_subghz_set_radio_type(SubGhzRadioInternal);
}
2023-03-06 14:23:59 +03:00
furi_hal_power_suppress_charge_enter();
app->model_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
app->input_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
app->view_port = view_port_alloc();
view_port_draw_callback_set(app->view_port, render_callback, app);
view_port_input_callback_set(app->view_port, input_callback, app);
// Open GUI and register view_port
app->gui = furi_record_open(RECORD_GUI);
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
app->notification = furi_record_open(RECORD_NOTIFICATION);
2022-06-01 21:40:20 +03:00
return app;
}
void subghz_remote_free(SubGHzRemote* app, bool with_subghz) {
furi_hal_power_suppress_charge_exit();
// Disable power for External CC1101 if it was enabled and module is connected
furi_hal_subghz_disable_ext_power();
2022-10-05 21:27:13 +03:00
furi_string_free(app->up_file);
furi_string_free(app->down_file);
furi_string_free(app->left_file);
furi_string_free(app->right_file);
furi_string_free(app->ok_file);
2022-10-05 21:27:13 +03:00
furi_string_free(app->file_path);
furi_string_free(app->signal);
2022-08-01 23:20:04 +03:00
2022-06-01 21:40:20 +03:00
gui_remove_view_port(app->gui, app->view_port);
2022-08-01 23:20:04 +03:00
furi_record_close(RECORD_GUI);
2022-06-01 21:40:20 +03:00
view_port_free(app->view_port);
2022-09-17 17:53:44 +03:00
app->gui = NULL;
2022-06-01 21:40:20 +03:00
2022-07-20 19:01:38 +03:00
furi_message_queue_free(app->input_queue);
2022-06-01 21:40:20 +03:00
2022-07-20 19:01:38 +03:00
furi_mutex_free(app->model_mutex);
2022-06-01 21:40:20 +03:00
if(with_subghz) {
furi_hal_subghz_sleep();
subghz_setting_free(app->setting);
subghz_receiver_free(app->subghz_receiver);
subghz_environment_free(app->environment);
}
furi_record_close(RECORD_NOTIFICATION);
2022-09-17 17:53:44 +03:00
app->notification = NULL;
2022-06-01 21:40:20 +03:00
free(app);
}
int32_t subghz_remote_app(void* p) {
2022-06-01 21:40:20 +03:00
UNUSED(p);
SubGHzRemote* app = subghz_remote_alloc();
2022-06-01 21:40:20 +03:00
2022-10-05 21:27:13 +03:00
app->file_path = furi_string_alloc();
2022-10-06 02:30:40 +03:00
app->signal = furi_string_alloc();
2022-08-01 23:20:04 +03:00
2022-06-01 21:40:20 +03:00
//setup variables before population
2022-10-05 21:27:13 +03:00
app->up_file = furi_string_alloc();
app->down_file = furi_string_alloc();
app->left_file = furi_string_alloc();
app->right_file = furi_string_alloc();
app->ok_file = furi_string_alloc();
app->file_result = 3;
Storage* storage = furi_record_open(RECORD_STORAGE);
storage_common_migrate(storage, EXT_PATH("unirf"), SUBREMOTEMAP_FOLDER);
if(!storage_simply_mkdir(storage, SUBREMOTEMAP_FOLDER)) {
FURI_LOG_E(TAG, "Could not create folder %s", SUBREMOTEMAP_FOLDER);
2022-08-01 23:20:04 +03:00
}
furi_record_close(RECORD_STORAGE);
2022-08-01 23:20:04 +03:00
furi_string_set(app->file_path, SUBREMOTEMAP_FOLDER);
2022-08-01 23:20:04 +03:00
DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
2022-09-14 21:04:04 +03:00
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, SUBREMOTEMAP_EXTENSION, &I_sub1_10px);
browser_options.base_path = SUBREMOTEMAP_FOLDER;
2022-09-14 21:04:04 +03:00
bool res = dialog_file_browser_show(dialogs, app->file_path, app->file_path, &browser_options);
2022-08-01 23:20:04 +03:00
furi_record_close(RECORD_DIALOGS);
if(!res) {
FURI_LOG_E(TAG, "No file selected");
subghz_remote_free(app, false);
return 255;
2022-08-01 23:20:04 +03:00
} else {
//check map and population variables
subghz_remote_cfg_set_check(app, app->file_path);
2022-08-01 23:20:04 +03:00
}
2022-06-01 21:40:20 +03:00
// init subghz stuff
subghz_remote_subghz_alloc(app);
2022-06-01 21:40:20 +03:00
bool exit_loop = false;
2022-08-01 23:20:04 +03:00
if(app->file_result == 2) {
FURI_LOG_D(
2022-06-01 21:40:20 +03:00
TAG,
"U: %s - D: %s - L: %s - R: %s - O: %s ",
2022-10-05 21:27:13 +03:00
furi_string_get_cstr(app->up_file),
furi_string_get_cstr(app->down_file),
furi_string_get_cstr(app->left_file),
furi_string_get_cstr(app->right_file),
furi_string_get_cstr(app->ok_file));
2022-06-01 21:40:20 +03:00
//variables to control multiple button presses and status updates
app->send_status = "Idle";
app->send_status_c = 0;
app->processing = 0;
//app->repeat = 1;
2022-06-01 21:40:20 +03:00
app->button = 0;
2022-06-12 05:42:37 +03:00
//refresh screen to update variables before processing main screen or error screens
2022-07-20 19:01:38 +03:00
furi_mutex_release(app->model_mutex);
2022-06-12 05:42:37 +03:00
view_port_update(app->view_port);
2022-06-01 21:40:20 +03:00
//input detect loop start
InputEvent input;
while(1) {
2022-07-26 21:52:34 +03:00
furi_check(
furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk);
FURI_LOG_D(
2022-06-01 21:40:20 +03:00
TAG,
"key: %s type: %s",
input_get_key_name(input.key),
input_get_type_name(input.type));
switch(input.key) {
case InputKeyUp:
if(input.type == InputTypePress) {
2022-06-01 21:40:20 +03:00
if(app->up_enabled) {
if(app->processing == 0) {
2022-10-06 02:30:40 +03:00
furi_string_reset(app->signal);
furi_string_set(app->signal, app->up_file);
2022-08-01 23:20:04 +03:00
app->button = 1;
2022-06-01 21:40:20 +03:00
app->processing = 1;
}
}
}
if(input.type == InputTypeRelease) {
if(app->up_enabled) {
subghz_remote_tx_stop(app);
}
}
2022-06-01 21:40:20 +03:00
break;
case InputKeyDown:
if(input.type == InputTypePress) {
2022-06-01 21:40:20 +03:00
if(app->down_enabled) {
if(app->processing == 0) {
2022-10-06 02:30:40 +03:00
furi_string_reset(app->signal);
furi_string_set(app->signal, app->down_file);
2022-08-01 23:20:04 +03:00
app->button = 2;
2022-06-01 21:40:20 +03:00
app->processing = 1;
}
}
}
if(input.type == InputTypeRelease) {
if(app->down_enabled) {
subghz_remote_tx_stop(app);
}
}
2022-06-01 21:40:20 +03:00
break;
case InputKeyRight:
if(input.type == InputTypePress) {
2022-06-01 21:40:20 +03:00
if(app->right_enabled) {
if(app->processing == 0) {
2022-10-06 02:30:40 +03:00
furi_string_reset(app->signal);
furi_string_set(app->signal, app->right_file);
2022-08-01 23:20:04 +03:00
app->button = 3;
2022-06-01 21:40:20 +03:00
app->processing = 1;
}
}
}
if(input.type == InputTypeRelease) {
if(app->right_enabled) {
subghz_remote_tx_stop(app);
}
}
2022-06-01 21:40:20 +03:00
break;
case InputKeyLeft:
if(input.type == InputTypePress) {
2022-06-01 21:40:20 +03:00
if(app->left_enabled) {
if(app->processing == 0) {
2022-10-06 02:30:40 +03:00
furi_string_reset(app->signal);
furi_string_set(app->signal, app->left_file);
2022-08-01 23:20:04 +03:00
app->button = 4;
2022-06-01 21:40:20 +03:00
app->processing = 1;
}
}
}
if(input.type == InputTypeRelease) {
if(app->left_enabled) {
subghz_remote_tx_stop(app);
}
}
2022-06-01 21:40:20 +03:00
break;
case InputKeyOk:
if(input.type == InputTypePress) {
2022-06-01 21:40:20 +03:00
if(app->ok_enabled) {
if(app->processing == 0) {
2022-10-06 02:30:40 +03:00
furi_string_reset(app->signal);
furi_string_set(app->signal, app->ok_file);
2022-06-01 21:40:20 +03:00
app->button = 5;
app->processing = 1;
}
}
}
if(input.type == InputTypeRelease) {
if(app->ok_enabled) {
subghz_remote_tx_stop(app);
}
}
2022-06-01 21:40:20 +03:00
break;
case InputKeyBack:
subghz_remote_tx_stop(app);
exit_loop = true;
2022-06-01 21:40:20 +03:00
break;
2022-11-02 23:07:00 +03:00
default:
break;
2022-06-01 21:40:20 +03:00
}
if(app->processing == 0) {
FURI_LOG_D(TAG, "processing 0");
2022-06-01 21:40:20 +03:00
app->send_status = "Idle";
app->send_status_c = 0;
app->button = 0;
} else if(app->processing == 1) {
FURI_LOG_D(TAG, "processing 1");
2022-06-01 21:40:20 +03:00
app->send_status = "Send";
2022-06-01 21:40:20 +03:00
switch(app->button) {
case 1:
app->send_status_c = 1;
break;
case 2:
app->send_status_c = 2;
break;
case 3:
app->send_status_c = 3;
break;
case 4:
app->send_status_c = 4;
break;
case 5:
app->send_status_c = 5;
break;
}
app->processing = 2;
subghz_remote_process_signal(app, app->signal);
2022-06-01 21:40:20 +03:00
}
if(exit_loop == true) {
2022-07-20 19:01:38 +03:00
furi_mutex_release(app->model_mutex);
2022-06-01 21:40:20 +03:00
break;
}
2022-07-20 19:01:38 +03:00
furi_mutex_release(app->model_mutex);
2022-06-01 21:40:20 +03:00
view_port_update(app->view_port);
}
} else if(app->file_result == 1 || app->file_result == 3) {
2022-06-12 05:42:37 +03:00
//refresh screen to update variables before processing main screen or error screens
view_port_update(app->view_port);
2022-06-01 21:40:20 +03:00
InputEvent input;
while(1) {
2022-07-26 21:52:34 +03:00
furi_check(
furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk);
FURI_LOG_D(
2022-06-01 21:40:20 +03:00
TAG,
"key: %s type: %s",
input_get_key_name(input.key),
input_get_type_name(input.type));
switch(input.key) {
case InputKeyRight:
break;
case InputKeyLeft:
break;
case InputKeyUp:
break;
case InputKeyDown:
break;
case InputKeyOk:
break;
case InputKeyBack:
exit_loop = true;
2022-06-01 21:40:20 +03:00
break;
2022-11-02 23:07:00 +03:00
default:
break;
2022-06-01 21:40:20 +03:00
}
if(exit_loop == true) {
2022-07-20 19:01:38 +03:00
furi_mutex_release(app->model_mutex);
2022-06-01 21:40:20 +03:00
break;
}
2022-07-20 19:01:38 +03:00
furi_mutex_release(app->model_mutex);
2022-06-01 21:40:20 +03:00
view_port_update(app->view_port);
}
2022-08-01 23:20:04 +03:00
} else {
furi_mutex_release(app->model_mutex);
2022-06-01 21:40:20 +03:00
}
// remove & free all stuff created by app
subghz_remote_free(app, true);
2022-06-01 21:40:20 +03:00
return 0;
2023-02-20 18:55:53 +03:00
}