diff --git a/applications/main/subghz/helpers/subghz_txrx.c b/applications/main/subghz/helpers/subghz_txrx.c index c1f519ba0..3275b7288 100644 --- a/applications/main/subghz/helpers/subghz_txrx.c +++ b/applications/main/subghz/helpers/subghz_txrx.c @@ -1,5 +1,6 @@ #include "subghz_txrx_i.h" #include +#include #define TAG "SubGhz" @@ -557,6 +558,13 @@ bool subghz_txrx_get_debug_pin_state(SubGhzTxRx* instance) { return instance->debug_pin_state; } +void subghz_txrx_reset_dynamic_and_custom_btns(SubGhzTxRx* instance) { + furi_assert(instance); + subghz_environment_reset_keeloq(instance->environment); + + subghz_custom_btns_reset(); +} + SubGhzReceiver* subghz_txrx_get_receiver(SubGhzTxRx* instance) { furi_assert(instance); return instance->receiver; diff --git a/applications/main/subghz/helpers/subghz_txrx.h b/applications/main/subghz/helpers/subghz_txrx.h index fd7c024b8..6ad5d97bd 100644 --- a/applications/main/subghz/helpers/subghz_txrx.h +++ b/applications/main/subghz/helpers/subghz_txrx.h @@ -293,4 +293,6 @@ void subghz_txrx_set_raw_file_encoder_worker_callback_end( void subghz_txrx_set_debug_pin_state(SubGhzTxRx* instance, bool state); bool subghz_txrx_get_debug_pin_state(SubGhzTxRx* instance); +void subghz_txrx_reset_dynamic_and_custom_btns(SubGhzTxRx* instance); + SubGhzReceiver* subghz_txrx_get_receiver(SubGhzTxRx* instance); // TODO use only in DecodeRaw diff --git a/applications/main/subghz/scenes/subghz_scene_receiver_info.c b/applications/main/subghz/scenes/subghz_scene_receiver_info.c index 2d89de731..e3ff485d2 100644 --- a/applications/main/subghz/scenes/subghz_scene_receiver_info.c +++ b/applications/main/subghz/scenes/subghz_scene_receiver_info.c @@ -1,7 +1,5 @@ #include "../subghz_i.h" #include "../helpers/subghz_custom_event.h" -#include -#include #include @@ -108,7 +106,6 @@ void subghz_scene_receiver_info_draw_widget(SubGhz* subghz) { void subghz_scene_receiver_info_on_enter(void* context) { SubGhz* subghz = context; - keeloq_reset_original_btn(); subghz_custom_btns_reset(); subghz_scene_receiver_info_draw_widget(subghz); @@ -191,10 +188,5 @@ void subghz_scene_receiver_info_on_exit(void* context) { SubGhz* subghz = context; widget_reset(subghz->widget); - 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_txrx_reset_dynamic_and_custom_btns(subghz->txrx); } diff --git a/applications/main/subghz/scenes/subghz_scene_rpc.c b/applications/main/subghz/scenes/subghz_scene_rpc.c index ef718c4e1..7b3c940a5 100644 --- a/applications/main/subghz/scenes/subghz_scene_rpc.c +++ b/applications/main/subghz/scenes/subghz_scene_rpc.c @@ -1,6 +1,4 @@ #include "../subghz_i.h" -#include -#include #include @@ -110,10 +108,5 @@ void subghz_scene_rpc_on_exit(void* context) { popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop); popup_set_icon(popup, 0, 0, NULL); - 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_txrx_reset_dynamic_and_custom_btns(subghz->txrx); } diff --git a/applications/main/subghz/scenes/subghz_scene_transmitter.c b/applications/main/subghz/scenes/subghz_scene_transmitter.c index 15a53603d..ab5605f95 100644 --- a/applications/main/subghz/scenes/subghz_scene_transmitter.c +++ b/applications/main/subghz/scenes/subghz_scene_transmitter.c @@ -1,8 +1,6 @@ #include "../subghz_i.h" #include "../views/transmitter.h" #include -#include -#include #include @@ -46,7 +44,6 @@ bool subghz_scene_transmitter_update_data_show(void* context) { void subghz_scene_transmitter_on_enter(void* context) { SubGhz* subghz = context; - keeloq_reset_original_btn(); subghz_custom_btns_reset(); if(!subghz_scene_transmitter_update_data_show(subghz)) { @@ -107,10 +104,6 @@ bool subghz_scene_transmitter_on_event(void* context, SceneManagerEvent event) { void subghz_scene_transmitter_on_exit(void* context) { SubGhz* subghz = context; subghz->state_notifications = SubGhzNotificationStateIDLE; - 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_txrx_reset_dynamic_and_custom_btns(subghz->txrx); } diff --git a/applications/main/subghz_remote/subghz_remote_app_i.c b/applications/main/subghz_remote/subghz_remote_app_i.c index 121fbf787..26659ccb1 100644 --- a/applications/main/subghz_remote/subghz_remote_app_i.c +++ b/applications/main/subghz_remote/subghz_remote_app_i.c @@ -229,8 +229,6 @@ bool subrem_tx_start_sub(SubGhzRemoteApp* app, SubRemSubFilePreset* sub_preset) 0); #ifdef APP_SUBGHZREMOTE - subghz_custom_btn_set(SUBGHZ_CUSTOM_BTN_OK); - keeloq_reset_original_btn(); subghz_custom_btns_reset(); #endif @@ -250,12 +248,8 @@ bool subrem_tx_stop_sub(SubGhzRemoteApp* app, bool forced) { subghz_txrx_stop(app->txrx); #ifdef APP_SUBGHZREMOTE if(sub_preset->type == SubGhzProtocolTypeDynamic) { - keeloq_reset_mfname(); - keeloq_reset_kl_type(); - star_line_reset_mfname(); - star_line_reset_kl_type(); + subghz_txrx_reset_dynamic_and_custom_btns(app->txrx); } - keeloq_reset_original_btn(); subghz_custom_btns_reset(); #endif return true; diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index ee5ce31b3..2eb217165 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -1817,9 +1817,6 @@ Function,-,j1f,float,float Function,-,jn,double,"int, double" Function,-,jnf,float,"int, float" Function,-,jrand48,long,unsigned short[3] -Function,-,keeloq_reset_kl_type,void, -Function,-,keeloq_reset_mfname,void, -Function,-,keeloq_reset_original_btn,void, Function,-,l64a,char*,long Function,-,labs,long,long Function,-,lcong48,void,unsigned short[7] @@ -2563,8 +2560,6 @@ Function,+,srand,void,unsigned Function,-,srand48,void,long Function,-,srandom,void,unsigned Function,+,sscanf,int,"const char*, const char*, ..." -Function,-,star_line_reset_kl_type,void, -Function,-,star_line_reset_mfname,void, Function,+,storage_common_copy,FS_Error,"Storage*, const char*, const char*" Function,+,storage_common_exists,_Bool,"Storage*, const char*" Function,+,storage_common_fs_info,FS_Error,"Storage*, const char*, uint64_t*, uint64_t*" @@ -2711,13 +2706,6 @@ Function,+,subghz_block_generic_deserialize,SubGhzProtocolStatus,"SubGhzBlockGen Function,+,subghz_block_generic_deserialize_check_count_bit,SubGhzProtocolStatus,"SubGhzBlockGeneric*, FlipperFormat*, uint16_t" Function,+,subghz_block_generic_get_preset_name,void,"const char*, FuriString*" Function,+,subghz_block_generic_serialize,SubGhzProtocolStatus,"SubGhzBlockGeneric*, FlipperFormat*, SubGhzRadioPreset*" -Function,-,subghz_custom_btn_get,uint8_t, -Function,-,subghz_custom_btn_get_original,uint8_t, -Function,-,subghz_custom_btn_is_allowed,_Bool, -Function,-,subghz_custom_btn_set,_Bool,uint8_t -Function,-,subghz_custom_btn_set_max,void,uint8_t -Function,-,subghz_custom_btn_set_original,void,uint8_t -Function,-,subghz_custom_btns_reset,void, Function,+,subghz_environment_alloc,SubGhzEnvironment*, Function,+,subghz_environment_free,void,SubGhzEnvironment* Function,+,subghz_environment_get_alutech_at_4n_rainbow_table_file_name,const char*,SubGhzEnvironment* @@ -2727,6 +2715,7 @@ Function,+,subghz_environment_get_nice_flor_s_rainbow_table_file_name,const char Function,+,subghz_environment_get_protocol_name_registry,const char*,"SubGhzEnvironment*, size_t" Function,+,subghz_environment_get_protocol_registry,void*,SubGhzEnvironment* Function,+,subghz_environment_load_keystore,_Bool,"SubGhzEnvironment*, const char*" +Function,+,subghz_environment_reset_keeloq,void,SubGhzEnvironment* Function,+,subghz_environment_set_alutech_at_4n_rainbow_table_file_name,void,"SubGhzEnvironment*, const char*" Function,+,subghz_environment_set_came_atomo_rainbow_table_file_name,void,"SubGhzEnvironment*, const char*" Function,+,subghz_environment_set_nice_flor_s_rainbow_table_file_name,void,"SubGhzEnvironment*, const char*" @@ -2737,6 +2726,7 @@ Function,-,subghz_keystore_get_data,SubGhzKeyArray_t*,SubGhzKeystore* Function,-,subghz_keystore_load,_Bool,"SubGhzKeystore*, const char*" Function,-,subghz_keystore_raw_encrypted_save,_Bool,"const char*, const char*, uint8_t*" Function,-,subghz_keystore_raw_get_data,_Bool,"const char*, size_t, uint8_t*, size_t" +Function,-,subghz_keystore_reset_kl,void,SubGhzKeystore* Function,-,subghz_keystore_save,_Bool,"SubGhzKeystore*, const char*, uint8_t*" Function,-,subghz_protocol_alutech_at_4n_create_data,_Bool,"void*, FlipperFormat*, uint32_t, uint8_t, uint16_t, SubGhzRadioPreset*" Function,+,subghz_protocol_blocks_add_bit,void,"SubGhzBlockDecoder*, uint8_t" diff --git a/lib/subghz/blocks/custom_btn.c b/lib/subghz/blocks/custom_btn.c index 275a4c930..1919fc5e4 100644 --- a/lib/subghz/blocks/custom_btn.c +++ b/lib/subghz/blocks/custom_btn.c @@ -1,8 +1,9 @@ -#include "custom_btn.h" +#include "custom_btn_i.h" static uint8_t custom_btn_id = SUBGHZ_CUSTOM_BTN_OK; static uint8_t custom_btn_original = 0; static uint8_t custom_btn_max_btns = 0; +static uint8_t controller_programming_mode = PROG_MODE_OFF; bool subghz_custom_btn_set(uint8_t btn_id) { if(btn_id > custom_btn_max_btns) { @@ -33,8 +34,18 @@ void subghz_custom_btn_set_max(uint8_t b) { void subghz_custom_btns_reset() { custom_btn_original = 0; custom_btn_max_btns = 0; + controller_programming_mode = PROG_MODE_OFF; + custom_btn_id = SUBGHZ_CUSTOM_BTN_OK; } bool subghz_custom_btn_is_allowed() { return custom_btn_max_btns != 0; +} + +void subghz_custom_btn_set_prog_mode(ProgMode prog_mode) { + controller_programming_mode = prog_mode; +} + +ProgMode subghz_custom_btn_get_prog_mode() { + return controller_programming_mode; } \ No newline at end of file diff --git a/lib/subghz/blocks/custom_btn.h b/lib/subghz/blocks/custom_btn.h index c13842fa8..e06457ccd 100644 --- a/lib/subghz/blocks/custom_btn.h +++ b/lib/subghz/blocks/custom_btn.h @@ -15,12 +15,8 @@ bool subghz_custom_btn_set(uint8_t btn_id); uint8_t subghz_custom_btn_get(); -void subghz_custom_btn_set_original(uint8_t btn_code); - uint8_t subghz_custom_btn_get_original(); -void subghz_custom_btn_set_max(uint8_t b); - void subghz_custom_btns_reset(); bool subghz_custom_btn_is_allowed(); \ No newline at end of file diff --git a/lib/subghz/blocks/custom_btn_i.h b/lib/subghz/blocks/custom_btn_i.h new file mode 100644 index 000000000..f75ba4068 --- /dev/null +++ b/lib/subghz/blocks/custom_btn_i.h @@ -0,0 +1,17 @@ +#pragma once + +#include "custom_btn.h" + +#define PROG_MODE_OFF (0U) +#define PROG_MODE_KEELOQ_BFT (1U) +#define PROG_MODE_KEELOQ_APRIMATIC (2U) + +typedef uint8_t ProgMode; + +void subghz_custom_btn_set_original(uint8_t btn_code); + +void subghz_custom_btn_set_max(uint8_t b); + +void subghz_custom_btn_set_prog_mode(ProgMode prog_mode); + +ProgMode subghz_custom_btn_get_prog_mode(); \ No newline at end of file diff --git a/lib/subghz/environment.c b/lib/subghz/environment.c index 5ded243c4..bcada8752 100644 --- a/lib/subghz/environment.c +++ b/lib/subghz/environment.c @@ -7,6 +7,8 @@ struct SubGhzEnvironment { const char* came_atomo_rainbow_table_file_name; const char* nice_flor_s_rainbow_table_file_name; const char* alutech_at_4n_rainbow_table_file_name; + const char* mfname; + uint8_t kl_type; }; SubGhzEnvironment* subghz_environment_alloc() { @@ -17,6 +19,8 @@ SubGhzEnvironment* subghz_environment_alloc() { instance->came_atomo_rainbow_table_file_name = NULL; instance->nice_flor_s_rainbow_table_file_name = NULL; instance->alutech_at_4n_rainbow_table_file_name = NULL; + instance->mfname = ""; + instance->kl_type = 0; return instance; } @@ -115,4 +119,10 @@ const char* } else { return NULL; } -} \ No newline at end of file +} + +void subghz_environment_reset_keeloq(SubGhzEnvironment* instance) { + furi_assert(instance); + + subghz_keystore_reset_kl(instance->keystore); +} diff --git a/lib/subghz/environment.h b/lib/subghz/environment.h index 7bd38ba2f..67de48df9 100644 --- a/lib/subghz/environment.h +++ b/lib/subghz/environment.h @@ -110,6 +110,12 @@ void* subghz_environment_get_protocol_registry(SubGhzEnvironment* instance); */ const char* subghz_environment_get_protocol_name_registry(SubGhzEnvironment* instance, size_t idx); +/** + * Resetting the parameters used in the keeloq protocol. + * @param instance Pointer to a SubGhzEnvironment instance + */ +void subghz_environment_reset_keeloq(SubGhzEnvironment* instance); + #ifdef __cplusplus } #endif diff --git a/lib/subghz/protocols/alutech_at_4n.c b/lib/subghz/protocols/alutech_at_4n.c index 281e820f0..1a35550c6 100644 --- a/lib/subghz/protocols/alutech_at_4n.c +++ b/lib/subghz/protocols/alutech_at_4n.c @@ -5,7 +5,7 @@ #include "../blocks/generic.h" #include "../blocks/math.h" -#include "../blocks/custom_btn.h" +#include "../blocks/custom_btn_i.h" #define TAG "SubGhzProtocoAlutech_at_4n" diff --git a/lib/subghz/protocols/came_atomo.c b/lib/subghz/protocols/came_atomo.c index 4723ec99f..cea7ebf6f 100644 --- a/lib/subghz/protocols/came_atomo.c +++ b/lib/subghz/protocols/came_atomo.c @@ -7,7 +7,7 @@ #include "../blocks/generic.h" #include "../blocks/math.h" -#include "../blocks/custom_btn.h" +#include "../blocks/custom_btn_i.h" #define TAG "SubGhzProtocoCameAtomo" diff --git a/lib/subghz/protocols/keeloq.c b/lib/subghz/protocols/keeloq.c index 26b5a20a9..2163c0500 100644 --- a/lib/subghz/protocols/keeloq.c +++ b/lib/subghz/protocols/keeloq.c @@ -10,14 +10,11 @@ #include "../blocks/generic.h" #include "../blocks/math.h" -#include "../blocks/custom_btn.h" +#include "../blocks/custom_btn_i.h" +#include "../subghz_keystore_i.h" #define TAG "SubGhzProtocolKeeloq" -#define KEELOQ_PROG_MODE_OFF (0U) -#define KEELOQ_PROG_MODE_BFT (1U) -#define KEELOQ_PROG_MODE_APRIMATIC (2U) - static const SubGhzBlockConst subghz_protocol_keeloq_const = { .te_short = 400, .te_long = 800, @@ -90,25 +87,6 @@ const SubGhzProtocol subghz_protocol_keeloq = { .encoder = &subghz_protocol_keeloq_encoder, }; -static const char* mfname; -static uint8_t kl_type; -static uint8_t klq_prog_mode; -static uint16_t temp_counter; - -void keeloq_reset_original_btn() { - subghz_custom_btns_reset(); - temp_counter = 0; - klq_prog_mode = KEELOQ_PROG_MODE_OFF; -} - -void keeloq_reset_mfname() { - mfname = ""; -} - -void keeloq_reset_kl_type() { - kl_type = 0; -} - /** * Analysis of received data * @param instance Pointer to a SubGhzBlockGeneric* instance @@ -165,29 +143,32 @@ static bool subghz_protocol_keeloq_gen_data( } // programming mode on / off conditions + ProgMode prog_mode = subghz_custom_btn_get_prog_mode(); if(strcmp(instance->manufacture_name, "BFT") == 0) { // BFT programming mode on / off conditions if(btn == 0xF) { - klq_prog_mode = KEELOQ_PROG_MODE_BFT; - } else if(klq_prog_mode == KEELOQ_PROG_MODE_BFT) { - klq_prog_mode = KEELOQ_PROG_MODE_OFF; + prog_mode = PROG_MODE_KEELOQ_BFT; + } else if(prog_mode == PROG_MODE_KEELOQ_BFT) { + prog_mode = PROG_MODE_OFF; } } else if(strcmp(instance->manufacture_name, "Aprimatic") == 0) { // Aprimatic programming mode on / off conditions if(btn == 0xF) { - klq_prog_mode = KEELOQ_PROG_MODE_APRIMATIC; - } else if(klq_prog_mode == KEELOQ_PROG_MODE_APRIMATIC) { - klq_prog_mode = KEELOQ_PROG_MODE_OFF; + prog_mode = PROG_MODE_KEELOQ_APRIMATIC; + } else if(prog_mode == PROG_MODE_KEELOQ_APRIMATIC) { + prog_mode = PROG_MODE_OFF; } } + subghz_custom_btn_set_prog_mode(prog_mode); + // If we using BFT programming mode we will trasmit its seed in hop part like original remote - if(klq_prog_mode == KEELOQ_PROG_MODE_BFT) { + if(prog_mode == PROG_MODE_KEELOQ_BFT) { hop = instance->generic.seed; - } else if(klq_prog_mode == KEELOQ_PROG_MODE_APRIMATIC) { + } else if(prog_mode == PROG_MODE_KEELOQ_APRIMATIC) { // If we using Aprimatic programming mode we will trasmit some strange looking hop value, why? cuz manufacturer did it this way :) hop = 0x1A2B3C4D; } - if(counter_up && klq_prog_mode == KEELOQ_PROG_MODE_OFF) { + if(counter_up && prog_mode == PROG_MODE_OFF) { if(instance->generic.cnt < 0xFFFF) { if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) >= 0xFFFF) { instance->generic.cnt = 0; @@ -198,7 +179,7 @@ static bool subghz_protocol_keeloq_gen_data( instance->generic.cnt = 0; } } - if(klq_prog_mode == KEELOQ_PROG_MODE_OFF) { + if(prog_mode == PROG_MODE_OFF) { // Protocols that do not use encryption if(strcmp(instance->manufacture_name, "Unknown") == 0) { code_found_reverse = subghz_protocol_blocks_reverse_key( @@ -252,7 +233,7 @@ static bool subghz_protocol_keeloq_gen_data( decrypt = btn << 28 | (0x000) << 16 | instance->generic.cnt; // Beninca / Allmatic -> no serial - simple XOR } - + uint8_t kl_type_en = instance->keystore->kl_type; for M_EACH( manufacture_code, @@ -292,21 +273,21 @@ static bool subghz_protocol_keeloq_gen_data( hop = subghz_protocol_keeloq_common_encrypt(decrypt, man); break; case KEELOQ_LEARNING_UNKNOWN: - if(kl_type == 1) { + if(kl_type_en == 1) { hop = subghz_protocol_keeloq_common_encrypt( decrypt, manufacture_code->key); } - if(kl_type == 2) { + if(kl_type_en == 2) { man = subghz_protocol_keeloq_common_normal_learning( fix, manufacture_code->key); hop = subghz_protocol_keeloq_common_encrypt(decrypt, man); } - if(kl_type == 3) { + if(kl_type_en == 3) { man = subghz_protocol_keeloq_common_secure_learning( fix, instance->generic.seed, manufacture_code->key); hop = subghz_protocol_keeloq_common_encrypt(decrypt, man); } - if(kl_type == 4) { + if(kl_type_en == 4) { man = subghz_protocol_keeloq_common_magic_xor_type1_learning( instance->generic.serial, manufacture_code->key); hop = subghz_protocol_keeloq_common_encrypt(decrypt, man); @@ -399,9 +380,10 @@ static bool if(instance->manufacture_name == 0x0) { instance->manufacture_name = ""; } - if(klq_prog_mode == KEELOQ_PROG_MODE_BFT) { + ProgMode prog_mode = subghz_custom_btn_get_prog_mode(); + if(prog_mode == PROG_MODE_KEELOQ_BFT) { instance->manufacture_name = "BFT"; - } else if(klq_prog_mode == KEELOQ_PROG_MODE_APRIMATIC) { + } else if(prog_mode == PROG_MODE_KEELOQ_APRIMATIC) { instance->manufacture_name = "Aprimatic"; } uint8_t klq_last_custom_btn = 0xA; @@ -507,7 +489,7 @@ SubGhzProtocolStatus if(flipper_format_read_string( flipper_format, "Manufacture", instance->manufacture_from_file)) { instance->manufacture_name = furi_string_get_cstr(instance->manufacture_from_file); - mfname = furi_string_get_cstr(instance->manufacture_from_file); + instance->keystore->mfname = instance->manufacture_name; } else { FURI_LOG_D(TAG, "ENCODER: Missing Manufacture"); } @@ -579,7 +561,7 @@ void* subghz_protocol_decoder_keeloq_alloc(SubGhzEnvironment* environment) { instance->keystore = subghz_environment_get_keystore(environment); instance->manufacture_from_file = furi_string_alloc(); - klq_prog_mode = KEELOQ_PROG_MODE_OFF; + subghz_custom_btn_set_prog_mode(PROG_MODE_OFF); return instance; } @@ -596,8 +578,9 @@ void subghz_protocol_decoder_keeloq_reset(void* context) { furi_assert(context); SubGhzProtocolDecoderKeeloq* instance = context; instance->decoder.parser_step = KeeloqDecoderStepReset; - mfname = ""; - kl_type = 0; + // TODO + instance->keystore->mfname = ""; + instance->keystore->kl_type = 0; } void subghz_protocol_decoder_keeloq_feed(void* context, bool level, uint32_t duration) { @@ -740,9 +723,12 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( uint32_t decrypt = 0; uint64_t man; bool mf_not_set = false; - if(mfname == 0x0) { - mfname = ""; - } + // TODO: + // if(mfname == 0x0) { + // mfname = ""; + // } + + const char* mfname = keystore->mfname; if(strcmp(mfname, "Unknown") == 0) { return 1; @@ -758,7 +744,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -770,7 +756,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -780,7 +766,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -790,7 +776,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -800,7 +786,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -810,7 +796,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -820,7 +806,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -829,8 +815,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 1; + keystore->mfname = *manufacture_name; + keystore->kl_type = 1; return 1; } @@ -845,8 +831,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_rev); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 1; + keystore->mfname = *manufacture_name; + keystore->kl_type = 1; return 1; } @@ -858,8 +844,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 2; + keystore->mfname = *manufacture_name; + keystore->kl_type = 2; return 1; } @@ -868,8 +854,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 2; + keystore->mfname = *manufacture_name; + keystore->kl_type = 2; return 1; } @@ -879,8 +865,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 3; + keystore->mfname = *manufacture_name; + keystore->kl_type = 3; return 1; } @@ -890,8 +876,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 3; + keystore->mfname = *manufacture_name; + keystore->kl_type = 3; return 1; } @@ -901,8 +887,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 4; + keystore->mfname = *manufacture_name; + keystore->kl_type = 4; return 1; } @@ -911,8 +897,8 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( decrypt = subghz_protocol_keeloq_common_decrypt(hop, man); if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 4; + keystore->mfname = *manufacture_name; + keystore->kl_type = 4; return 1; } @@ -922,7 +908,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( } *manufacture_name = "Unknown"; - mfname = "Unknown"; + keystore->mfname = "Unknown"; instance->cnt = 0; return 0; @@ -935,18 +921,20 @@ static void subghz_protocol_keeloq_check_remote_controller( uint64_t key = subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit); uint32_t key_fix = key >> 32; uint32_t key_hop = key & 0x00000000ffffffff; + static uint16_t temp_counter = 0; // Be careful with prog_mode // If we are in BFT / Aprimatic programming mode we will set previous remembered counter and skip mf keys check - if(klq_prog_mode == KEELOQ_PROG_MODE_OFF) { + ProgMode prog_mode = subghz_custom_btn_get_prog_mode(); + if(prog_mode == PROG_MODE_OFF) { // Check key AN-Motors if((key_hop >> 24) == ((key_hop >> 16) & 0x00ff) && (key_fix >> 28) == ((key_hop >> 12) & 0x0f) && (key_hop & 0xFFF) == 0x404) { *manufacture_name = "AN-Motors"; - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; instance->cnt = key_hop >> 16; } else if((key_hop & 0xFFF) == (0x000) && (key_fix >> 28) == ((key_hop >> 12) & 0x0f)) { *manufacture_name = "HCS101"; - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; instance->cnt = key_hop >> 16; } else { subghz_protocol_keeloq_check_remote_controller_selector( @@ -954,14 +942,17 @@ static void subghz_protocol_keeloq_check_remote_controller( } temp_counter = instance->cnt; - } else if(klq_prog_mode == KEELOQ_PROG_MODE_BFT) { + } else if(prog_mode == PROG_MODE_KEELOQ_BFT) { *manufacture_name = "BFT"; - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; instance->cnt = temp_counter; - } else if(klq_prog_mode == KEELOQ_PROG_MODE_APRIMATIC) { + } else if(prog_mode == PROG_MODE_KEELOQ_APRIMATIC) { *manufacture_name = "Aprimatic"; - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; instance->cnt = temp_counter; + } else { + // Counter protection + furi_crash("Unsuported Prog Mode"); } instance->serial = key_fix & 0x0FFFFFFF; @@ -1052,7 +1043,7 @@ SubGhzProtocolStatus if(flipper_format_read_string( flipper_format, "Manufacture", instance->manufacture_from_file)) { instance->manufacture_name = furi_string_get_cstr(instance->manufacture_from_file); - mfname = furi_string_get_cstr(instance->manufacture_from_file); + instance->keystore->mfname = instance->manufacture_name; } else { FURI_LOG_D(TAG, "DECODER: Missing Manufacture"); } diff --git a/lib/subghz/protocols/keeloq.h b/lib/subghz/protocols/keeloq.h index 9862e0e92..a757e82d5 100644 --- a/lib/subghz/protocols/keeloq.h +++ b/lib/subghz/protocols/keeloq.h @@ -2,8 +2,6 @@ #include "base.h" -#include "../blocks/custom_btn.h" - #define SUBGHZ_PROTOCOL_KEELOQ_NAME "KeeLoq" typedef struct SubGhzProtocolDecoderKeeloq SubGhzProtocolDecoderKeeloq; @@ -13,12 +11,6 @@ extern const SubGhzProtocolDecoder subghz_protocol_keeloq_decoder; extern const SubGhzProtocolEncoder subghz_protocol_keeloq_encoder; extern const SubGhzProtocol subghz_protocol_keeloq; -void keeloq_reset_mfname(); - -void keeloq_reset_kl_type(); - -void keeloq_reset_original_btn(); - /** * Allocate SubGhzProtocolEncoderKeeloq. * @param environment Pointer to a SubGhzEnvironment instance diff --git a/lib/subghz/protocols/nice_flor_s.c b/lib/subghz/protocols/nice_flor_s.c index 5eaa21274..eca9c4e95 100644 --- a/lib/subghz/protocols/nice_flor_s.c +++ b/lib/subghz/protocols/nice_flor_s.c @@ -6,7 +6,7 @@ #include "../blocks/generic.h" #include "../blocks/math.h" -#include "../blocks/custom_btn.h" +#include "../blocks/custom_btn_i.h" /* * https://phreakerclub.com/1615 diff --git a/lib/subghz/protocols/secplus_v2.c b/lib/subghz/protocols/secplus_v2.c index 7acfb74ac..bf2246292 100644 --- a/lib/subghz/protocols/secplus_v2.c +++ b/lib/subghz/protocols/secplus_v2.c @@ -7,7 +7,7 @@ #include "../blocks/generic.h" #include "../blocks/math.h" -#include "../blocks/custom_btn.h" +#include "../blocks/custom_btn_i.h" /* * Help diff --git a/lib/subghz/protocols/somfy_telis.c b/lib/subghz/protocols/somfy_telis.c index 6a159a7ea..5fbd90275 100644 --- a/lib/subghz/protocols/somfy_telis.c +++ b/lib/subghz/protocols/somfy_telis.c @@ -7,7 +7,7 @@ #include "../blocks/generic.h" #include "../blocks/math.h" -#include "../blocks/custom_btn.h" +#include "../blocks/custom_btn_i.h" #define TAG "SubGhzProtocolSomfyTelis" diff --git a/lib/subghz/protocols/star_line.c b/lib/subghz/protocols/star_line.c index 362e34f3c..05afd80a6 100644 --- a/lib/subghz/protocols/star_line.c +++ b/lib/subghz/protocols/star_line.c @@ -10,6 +10,8 @@ #include "../blocks/generic.h" #include "../blocks/math.h" +#include "../subghz_keystore_i.h" + #define TAG "SubGhzProtocolStarLine" static const SubGhzBlockConst subghz_protocol_star_line_const = { @@ -83,18 +85,6 @@ const SubGhzProtocol subghz_protocol_star_line = { .encoder = &subghz_protocol_star_line_encoder, }; -static const char* mfname; - -static int kl_type; - -void star_line_reset_mfname() { - mfname = ""; -} - -void star_line_reset_kl_type() { - kl_type = 0; -} - /** * Analysis of received data * @param instance Pointer to a SubGhzBlockGeneric* instance @@ -163,6 +153,7 @@ static bool instance->generic.data, instance->generic.data_count_bit); hop = code_found_reverse & 0x00000000ffffffff; } else { + uint8_t kl_type_en = instance->keystore->kl_type; for M_EACH( manufacture_code, @@ -184,11 +175,11 @@ static bool hop = subghz_protocol_keeloq_common_encrypt(decrypt, man); break; case KEELOQ_LEARNING_UNKNOWN: - if(kl_type == 1) { + if(kl_type_en == 1) { hop = subghz_protocol_keeloq_common_encrypt( decrypt, manufacture_code->key); } - if(kl_type == 2) { + if(kl_type_en == 2) { man = subghz_protocol_keeloq_common_normal_learning( fix, manufacture_code->key); hop = subghz_protocol_keeloq_common_encrypt(decrypt, man); @@ -300,7 +291,7 @@ SubGhzProtocolStatus if(flipper_format_read_string( flipper_format, "Manufacture", instance->manufacture_from_file)) { instance->manufacture_name = furi_string_get_cstr(instance->manufacture_from_file); - mfname = furi_string_get_cstr(instance->manufacture_from_file); + instance->keystore->mfname = instance->manufacture_name; } else { FURI_LOG_D(TAG, "ENCODER: Missing Manufacture"); } @@ -382,8 +373,9 @@ void subghz_protocol_decoder_star_line_reset(void* context) { furi_assert(context); SubGhzProtocolDecoderStarLine* instance = context; instance->decoder.parser_step = StarLineDecoderStepReset; - mfname = ""; - kl_type = 0; + // TODO + instance->keystore->mfname = ""; + instance->keystore->kl_type = 0; } void subghz_protocol_decoder_star_line_feed(void* context, bool level, uint32_t duration) { @@ -523,9 +515,12 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( uint32_t decrypt = 0; uint64_t man_normal_learning; bool mf_not_set = false; - if(mfname == 0x0) { - mfname = ""; - } + // TODO: + // if(mfname == 0x0) { + // mfname = ""; + // } + + const char* mfname = keystore->mfname; if(strcmp(mfname, "Unknown") == 0) { return 1; @@ -542,7 +537,7 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( if(subghz_protocol_star_line_check_decrypt( instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -555,7 +550,7 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( if(subghz_protocol_star_line_check_decrypt( instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; + keystore->mfname = *manufacture_name; return 1; } break; @@ -565,8 +560,8 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( if(subghz_protocol_star_line_check_decrypt( instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 1; + keystore->mfname = *manufacture_name; + keystore->kl_type = 1; return 1; } // Check for mirrored man @@ -580,8 +575,8 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( if(subghz_protocol_star_line_check_decrypt( instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 1; + keystore->mfname = *manufacture_name; + keystore->kl_type = 1; return 1; } //########################### @@ -593,8 +588,8 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( if(subghz_protocol_star_line_check_decrypt( instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 2; + keystore->mfname = *manufacture_name; + keystore->kl_type = 2; return 1; } // Check for mirrored man @@ -604,8 +599,8 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( if(subghz_protocol_star_line_check_decrypt( instance, decrypt, btn, end_serial)) { *manufacture_name = furi_string_get_cstr(manufacture_code->name); - mfname = *manufacture_name; - kl_type = 2; + keystore->mfname = *manufacture_name; + keystore->kl_type = 2; return 1; } break; @@ -614,7 +609,7 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( } *manufacture_name = "Unknown"; - mfname = "Unknown"; + keystore->mfname = "Unknown"; instance->cnt = 0; return 0; @@ -690,7 +685,7 @@ SubGhzProtocolStatus if(flipper_format_read_string( flipper_format, "Manufacture", instance->manufacture_from_file)) { instance->manufacture_name = furi_string_get_cstr(instance->manufacture_from_file); - mfname = furi_string_get_cstr(instance->manufacture_from_file); + instance->keystore->mfname = instance->manufacture_name; } else { FURI_LOG_D(TAG, "DECODER: Missing Manufacture"); } diff --git a/lib/subghz/protocols/star_line.h b/lib/subghz/protocols/star_line.h index 901b82f7c..851819ec7 100644 --- a/lib/subghz/protocols/star_line.h +++ b/lib/subghz/protocols/star_line.h @@ -11,10 +11,6 @@ extern const SubGhzProtocolDecoder subghz_protocol_star_line_decoder; extern const SubGhzProtocolEncoder subghz_protocol_star_line_encoder; extern const SubGhzProtocol subghz_protocol_star_line; -void star_line_reset_mfname(); - -void star_line_reset_kl_type(); - /** * Allocate SubGhzProtocolEncoderStarLine. * @param environment Pointer to a SubGhzEnvironment instance diff --git a/lib/subghz/subghz_keystore.c b/lib/subghz/subghz_keystore.c index e0b1cf6ca..54ed15a99 100644 --- a/lib/subghz/subghz_keystore.c +++ b/lib/subghz/subghz_keystore.c @@ -1,4 +1,5 @@ #include "subghz_keystore.h" +#include "subghz_keystore_i.h" #include #include @@ -26,18 +27,23 @@ typedef enum { SubGhzKeystoreEncryptionAES256, } SubGhzKeystoreEncryption; -struct SubGhzKeystore { - SubGhzKeyArray_t data; -}; - SubGhzKeystore* subghz_keystore_alloc() { SubGhzKeystore* instance = malloc(sizeof(SubGhzKeystore)); SubGhzKeyArray_init(instance->data); + subghz_keystore_reset_kl(instance); + return instance; } +void subghz_keystore_reset_kl(SubGhzKeystore* instance) { + furi_assert(instance); + + instance->mfname = ""; + instance->kl_type = 0; +} + void subghz_keystore_free(SubGhzKeystore* instance) { furi_assert(instance); diff --git a/lib/subghz/subghz_keystore.h b/lib/subghz/subghz_keystore.h index 06ae8adae..dfb5c7373 100644 --- a/lib/subghz/subghz_keystore.h +++ b/lib/subghz/subghz_keystore.h @@ -75,6 +75,8 @@ bool subghz_keystore_raw_encrypted_save( */ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len); +void subghz_keystore_reset_kl(SubGhzKeystore* instance); + #ifdef __cplusplus } #endif diff --git a/lib/subghz/subghz_keystore_i.h b/lib/subghz/subghz_keystore_i.h new file mode 100644 index 000000000..8bf2f1e51 --- /dev/null +++ b/lib/subghz/subghz_keystore_i.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +struct SubGhzKeystore { + SubGhzKeyArray_t data; + const char* mfname; + uint8_t kl_type; +};