From 09527c3ab6dfdecf33c99e2bbe6eddaa64c30aa3 Mon Sep 17 00:00:00 2001 From: gid9798 <30450294+gid9798@users.noreply.github.com> Date: Sat, 20 May 2023 01:15:36 +0300 Subject: [PATCH] keeloq --- .../main/subghz/helpers/subghz_txrx.c | 6 + .../main/subghz/helpers/subghz_txrx.h | 2 + .../scenes/subghz_scene_receiver_info.c | 7 +- .../main/subghz/scenes/subghz_scene_rpc.c | 7 +- .../subghz/scenes/subghz_scene_transmitter.c | 8 +- .../main/subghz_remote/subghz_remote_app_i.c | 5 +- firmware/targets/f7/api_symbols.csv | 6 +- lib/subghz/environment.c | 30 +++++ lib/subghz/environment.h | 12 ++ lib/subghz/protocols/keeloq.c | 120 ++++++++++-------- lib/subghz/protocols/keeloq.h | 4 - lib/subghz/protocols/star_line.c | 77 ++++++----- 12 files changed, 176 insertions(+), 108 deletions(-) diff --git a/applications/main/subghz/helpers/subghz_txrx.c b/applications/main/subghz/helpers/subghz_txrx.c index c1f519ba0..c2d7e1bf5 100644 --- a/applications/main/subghz/helpers/subghz_txrx.c +++ b/applications/main/subghz/helpers/subghz_txrx.c @@ -557,6 +557,12 @@ bool subghz_txrx_get_debug_pin_state(SubGhzTxRx* instance) { return instance->debug_pin_state; } +void subghz_txrx_reset_dynamic(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..6c28cc5f0 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(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..d9ba5bd84 100644 --- a/applications/main/subghz/scenes/subghz_scene_receiver_info.c +++ b/applications/main/subghz/scenes/subghz_scene_receiver_info.c @@ -191,10 +191,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(subghz->txrx); } diff --git a/applications/main/subghz/scenes/subghz_scene_rpc.c b/applications/main/subghz/scenes/subghz_scene_rpc.c index ef718c4e1..edadc89bb 100644 --- a/applications/main/subghz/scenes/subghz_scene_rpc.c +++ b/applications/main/subghz/scenes/subghz_scene_rpc.c @@ -110,10 +110,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(subghz->txrx); } diff --git a/applications/main/subghz/scenes/subghz_scene_transmitter.c b/applications/main/subghz/scenes/subghz_scene_transmitter.c index 15a53603d..933f619f0 100644 --- a/applications/main/subghz/scenes/subghz_scene_transmitter.c +++ b/applications/main/subghz/scenes/subghz_scene_transmitter.c @@ -107,10 +107,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(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 349fb87cd..6086b4c25 100644 --- a/applications/main/subghz_remote/subghz_remote_app_i.c +++ b/applications/main/subghz_remote/subghz_remote_app_i.c @@ -256,12 +256,9 @@ bool subrem_tx_stop_sub(SubGhzRemoteApp* app, bool forced) { subrem_save_protocol_to_file( sub_preset->fff_data, furi_string_get_cstr(sub_preset->file_path)); - keeloq_reset_mfname(); - keeloq_reset_kl_type(); + subghz_environment_reset_keeloq(app->environment); keeloq_reset_original_btn(); subghz_custom_btns_reset(); - star_line_reset_mfname(); - star_line_reset_kl_type(); } app->tx_running = false; diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 84d0beda8..ddda9dcba 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -1,5 +1,5 @@ entry,status,name,type,params -Version,+,26.3,, +Version,+,26.5,, Header,+,applications/services/bt/bt_service/bt.h,, Header,+,applications/services/cli/cli.h,, Header,+,applications/services/cli/cli_vcp.h,, @@ -2731,7 +2731,11 @@ Function,+,subghz_environment_get_keystore,SubGhzKeystore*,SubGhzEnvironment* Function,+,subghz_environment_get_nice_flor_s_rainbow_table_file_name,const char*,SubGhzEnvironment* Function,+,subghz_environment_get_protocol_name_registry,const char*,"SubGhzEnvironment*, size_t" Function,+,subghz_environment_get_protocol_registry,void*,SubGhzEnvironment* +Function,-,subghz_environment_keeloq_get_kl_type,uint8_t,SubGhzEnvironment* +Function,-,subghz_environment_keeloq_get_mf,const char*,SubGhzEnvironment* +Function,-,subghz_environment_keeloq_set_mf,void,"SubGhzEnvironment*, const char*, uint8_t" 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*" diff --git a/lib/subghz/environment.c b/lib/subghz/environment.c index 5ded243c4..55caedb6d 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,30 @@ const char* } else { return NULL; } +} + +void subghz_environment_reset_keeloq(SubGhzEnvironment* instance) { + furi_assert(instance); + + instance->mfname = ""; + instance->kl_type = 0; +} + +const char* subghz_environment_keeloq_get_mf(SubGhzEnvironment* instance) { + furi_assert(instance); + + return instance->mfname; +} + +uint8_t subghz_environment_keeloq_get_kl_type(SubGhzEnvironment* instance) { + furi_assert(instance); + + return instance->kl_type; +} + +void subghz_environment_keeloq_set_mf(SubGhzEnvironment* instance, const char* mf, uint8_t kl_type) { + furi_assert(instance); + + instance->mfname = mf; + instance->kl_type = kl_type; } \ No newline at end of file diff --git a/lib/subghz/environment.h b/lib/subghz/environment.h index 7bd38ba2f..a23341e2f 100644 --- a/lib/subghz/environment.h +++ b/lib/subghz/environment.h @@ -110,6 +110,18 @@ 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); + +// Private + +const char* subghz_environment_keeloq_get_mf(SubGhzEnvironment* instance); +uint8_t subghz_environment_keeloq_get_kl_type(SubGhzEnvironment* instance); +void subghz_environment_keeloq_set_mf(SubGhzEnvironment* instance, const char* mf, uint8_t kl_type); + #ifdef __cplusplus } #endif diff --git a/lib/subghz/protocols/keeloq.c b/lib/subghz/protocols/keeloq.c index 26b5a20a9..0d151e68f 100644 --- a/lib/subghz/protocols/keeloq.c +++ b/lib/subghz/protocols/keeloq.c @@ -89,26 +89,20 @@ const SubGhzProtocol subghz_protocol_keeloq = { .decoder = &subghz_protocol_keeloq_decoder, .encoder = &subghz_protocol_keeloq_encoder, }; - -static const char* mfname; -static uint8_t kl_type; +// TODO: +// static const char* mfname; +// static uint8_t kl_type; static uint8_t klq_prog_mode; static uint16_t temp_counter; +static SubGhzEnvironment* kl_environment; + 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 @@ -127,6 +121,8 @@ void* subghz_protocol_encoder_keeloq_alloc(SubGhzEnvironment* environment) { instance->generic.protocol_name = instance->base.protocol->name; instance->keystore = subghz_environment_get_keystore(environment); + kl_environment = environment; + instance->encoder.repeat = 100; instance->encoder.size_upload = 256; instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration)); @@ -252,7 +248,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 = subghz_environment_keeloq_get_kl_type(kl_environment); for M_EACH( manufacture_code, @@ -292,21 +288,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); @@ -507,7 +503,15 @@ 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); + subghz_environment_keeloq_set_mf( + kl_environment, + furi_string_get_cstr(instance->manufacture_from_file), + subghz_environment_keeloq_get_kl_type(kl_environment)); + // TODO: + FURI_LOG_W( + "MF encoder", + "Alloc enviro 0x%X", + (uintptr_t)subghz_environment_keeloq_get_mf(kl_environment)); } else { FURI_LOG_D(TAG, "ENCODER: Missing Manufacture"); } @@ -579,6 +583,7 @@ void* subghz_protocol_decoder_keeloq_alloc(SubGhzEnvironment* environment) { instance->keystore = subghz_environment_get_keystore(environment); instance->manufacture_from_file = furi_string_alloc(); + kl_environment = environment; klq_prog_mode = KEELOQ_PROG_MODE_OFF; return instance; @@ -596,8 +601,7 @@ void subghz_protocol_decoder_keeloq_reset(void* context) { furi_assert(context); SubGhzProtocolDecoderKeeloq* instance = context; instance->decoder.parser_step = KeeloqDecoderStepReset; - mfname = ""; - kl_type = 0; + subghz_environment_reset_keeloq(kl_environment); } void subghz_protocol_decoder_keeloq_feed(void* context, bool level, uint32_t duration) { @@ -740,9 +744,15 @@ 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 = subghz_environment_keeloq_get_mf(kl_environment); + uint8_t kl_type_en = subghz_environment_keeloq_get_kl_type(kl_environment); + // TODO: + FURI_LOG_W("RMcontroll", "MF NAME: %s", mfname); if(strcmp(mfname, "Unknown") == 0) { return 1; @@ -758,7 +768,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -770,7 +781,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -780,7 +792,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -790,7 +803,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -800,7 +814,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -810,7 +825,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -820,7 +836,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -829,8 +846,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; - kl_type = 1; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 1); return 1; } @@ -845,8 +861,7 @@ 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; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 1); return 1; } @@ -858,8 +873,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; - kl_type = 2; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 2); return 1; } @@ -868,8 +882,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; - kl_type = 2; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 2); return 1; } @@ -879,8 +892,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; - kl_type = 3; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 3); return 1; } @@ -890,8 +902,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; - kl_type = 3; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 3); return 1; } @@ -901,8 +912,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; - kl_type = 4; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 4); return 1; } @@ -911,8 +921,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; - kl_type = 4; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 4); return 1; } @@ -922,7 +931,7 @@ static uint8_t subghz_protocol_keeloq_check_remote_controller_selector( } *manufacture_name = "Unknown"; - mfname = "Unknown"; + subghz_environment_keeloq_set_mf(kl_environment, "Unknown", kl_type_en); instance->cnt = 0; return 0; @@ -935,6 +944,7 @@ 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; + uint8_t kl_type_en = subghz_environment_keeloq_get_kl_type(kl_environment); // 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) { @@ -942,11 +952,11 @@ static void subghz_protocol_keeloq_check_remote_controller( 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; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, kl_type_en); instance->cnt = key_hop >> 16; } else if((key_hop & 0xFFF) == (0x000) && (key_fix >> 28) == ((key_hop >> 12) & 0x0f)) { *manufacture_name = "HCS101"; - mfname = *manufacture_name; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, kl_type_en); instance->cnt = key_hop >> 16; } else { subghz_protocol_keeloq_check_remote_controller_selector( @@ -956,11 +966,11 @@ static void subghz_protocol_keeloq_check_remote_controller( } else if(klq_prog_mode == KEELOQ_PROG_MODE_BFT) { *manufacture_name = "BFT"; - mfname = *manufacture_name; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, kl_type_en); instance->cnt = temp_counter; } else if(klq_prog_mode == KEELOQ_PROG_MODE_APRIMATIC) { *manufacture_name = "Aprimatic"; - mfname = *manufacture_name; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, kl_type_en); instance->cnt = temp_counter; } @@ -1052,7 +1062,15 @@ 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); + subghz_environment_keeloq_set_mf( + kl_environment, + furi_string_get_cstr(instance->manufacture_from_file), + subghz_environment_keeloq_get_kl_type(kl_environment)); + // TODO: + FURI_LOG_W( + "MF decoder", + "Alloc enviro 0x%X", + (uintptr_t)subghz_environment_keeloq_get_mf(kl_environment)); } else { FURI_LOG_D(TAG, "DECODER: Missing Manufacture"); } diff --git a/lib/subghz/protocols/keeloq.h b/lib/subghz/protocols/keeloq.h index 9862e0e92..26f569e8c 100644 --- a/lib/subghz/protocols/keeloq.h +++ b/lib/subghz/protocols/keeloq.h @@ -13,10 +13,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(); /** diff --git a/lib/subghz/protocols/star_line.c b/lib/subghz/protocols/star_line.c index 362e34f3c..64d9bef80 100644 --- a/lib/subghz/protocols/star_line.c +++ b/lib/subghz/protocols/star_line.c @@ -82,18 +82,12 @@ const SubGhzProtocol subghz_protocol_star_line = { .decoder = &subghz_protocol_star_line_decoder, .encoder = &subghz_protocol_star_line_encoder, }; +// TODO: +// static const char* mfname; -static const char* mfname; +// static int kl_type; -static int kl_type; - -void star_line_reset_mfname() { - mfname = ""; -} - -void star_line_reset_kl_type() { - kl_type = 0; -} +static SubGhzEnvironment* kl_environment; /** * Analysis of received data @@ -113,6 +107,8 @@ void* subghz_protocol_encoder_star_line_alloc(SubGhzEnvironment* environment) { instance->generic.protocol_name = instance->base.protocol->name; instance->keystore = subghz_environment_get_keystore(environment); + kl_environment = environment; + instance->manufacture_from_file = furi_string_alloc(); instance->encoder.repeat = 10; @@ -163,6 +159,7 @@ static bool instance->generic.data, instance->generic.data_count_bit); hop = code_found_reverse & 0x00000000ffffffff; } else { + uint8_t kl_type_en = subghz_environment_keeloq_get_kl_type(kl_environment); for M_EACH( manufacture_code, @@ -184,11 +181,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 +297,15 @@ 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); + subghz_environment_keeloq_set_mf( + kl_environment, + furi_string_get_cstr(instance->manufacture_from_file), + subghz_environment_keeloq_get_kl_type(kl_environment)); + // TODO: + FURI_LOG_W( + "MF encoder", + "Alloc enviro 0x%X", + (uintptr_t)subghz_environment_keeloq_get_mf(kl_environment)); } else { FURI_LOG_D(TAG, "ENCODER: Missing Manufacture"); } @@ -366,6 +371,7 @@ void* subghz_protocol_decoder_star_line_alloc(SubGhzEnvironment* environment) { instance->manufacture_from_file = furi_string_alloc(); instance->keystore = subghz_environment_get_keystore(environment); + kl_environment = environment; return instance; } @@ -382,8 +388,7 @@ void subghz_protocol_decoder_star_line_reset(void* context) { furi_assert(context); SubGhzProtocolDecoderStarLine* instance = context; instance->decoder.parser_step = StarLineDecoderStepReset; - mfname = ""; - kl_type = 0; + subghz_environment_reset_keeloq(kl_environment); } void subghz_protocol_decoder_star_line_feed(void* context, bool level, uint32_t duration) { @@ -523,9 +528,15 @@ 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 = subghz_environment_keeloq_get_mf(kl_environment); + uint8_t kl_type_en = subghz_environment_keeloq_get_kl_type(kl_environment); + // TODO: + FURI_LOG_W("RMcontroll", "MF NAME: %s", mfname); if(strcmp(mfname, "Unknown") == 0) { return 1; @@ -542,7 +553,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -555,7 +567,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; + subghz_environment_keeloq_set_mf( + kl_environment, *manufacture_name, kl_type_en); return 1; } break; @@ -565,8 +578,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; - kl_type = 1; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 1); return 1; } // Check for mirrored man @@ -580,8 +592,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; - kl_type = 1; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 1); return 1; } //########################### @@ -593,8 +604,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; - kl_type = 2; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 2); return 1; } // Check for mirrored man @@ -604,8 +614,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; - kl_type = 2; + subghz_environment_keeloq_set_mf(kl_environment, *manufacture_name, 2); return 1; } break; @@ -614,7 +623,7 @@ static uint8_t subghz_protocol_star_line_check_remote_controller_selector( } *manufacture_name = "Unknown"; - mfname = "Unknown"; + subghz_environment_keeloq_set_mf(kl_environment, "Unknown", kl_type_en); instance->cnt = 0; return 0; @@ -690,7 +699,15 @@ 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); + subghz_environment_keeloq_set_mf( + kl_environment, + furi_string_get_cstr(instance->manufacture_from_file), + subghz_environment_keeloq_get_kl_type(kl_environment)); + // TODO: + FURI_LOG_W( + "MF decoder", + "Alloc enviro 0x%X", + (uintptr_t)subghz_environment_keeloq_get_mf(kl_environment)); } else { FURI_LOG_D(TAG, "DECODER: Missing Manufacture"); }