From a260465721171f5c4ed0e97276fbbbbde58289e9 Mon Sep 17 00:00:00 2001 From: gid9798 <30450294+gid9798@users.noreply.github.com> Date: Tue, 30 May 2023 22:18:54 +0300 Subject: [PATCH] Prog_mode to custom_btn --- .../main/subghz/helpers/subghz_txrx.c | 2 +- .../scenes/subghz_scene_receiver_info.c | 1 - .../subghz/scenes/subghz_scene_transmitter.c | 1 - .../main/subghz_remote/subghz_remote_app_i.c | 3 - firmware/targets/f7/api_symbols.csv | 8 --- lib/subghz/blocks/custom_btn.c | 13 ++++- lib/subghz/blocks/custom_btn.h | 4 -- lib/subghz/blocks/custom_btn_i.h | 17 ++++++ lib/subghz/protocols/alutech_at_4n.c | 2 +- lib/subghz/protocols/came_atomo.c | 2 +- lib/subghz/protocols/keeloq.c | 56 +++++++++---------- lib/subghz/protocols/keeloq.h | 4 -- lib/subghz/protocols/nice_flor_s.c | 2 +- lib/subghz/protocols/secplus_v2.c | 2 +- lib/subghz/protocols/somfy_telis.c | 2 +- 15 files changed, 61 insertions(+), 58 deletions(-) create mode 100644 lib/subghz/blocks/custom_btn_i.h diff --git a/applications/main/subghz/helpers/subghz_txrx.c b/applications/main/subghz/helpers/subghz_txrx.c index 9d957cd29..79d05b5be 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" @@ -561,7 +562,6 @@ void subghz_txrx_reset_dynamic(SubGhzTxRx* instance) { furi_assert(instance); subghz_environment_reset_keeloq(instance->environment); - keeloq_reset_original_btn(); subghz_custom_btns_reset(); } diff --git a/applications/main/subghz/scenes/subghz_scene_receiver_info.c b/applications/main/subghz/scenes/subghz_scene_receiver_info.c index d9ba5bd84..b0c538a58 100644 --- a/applications/main/subghz/scenes/subghz_scene_receiver_info.c +++ b/applications/main/subghz/scenes/subghz_scene_receiver_info.c @@ -108,7 +108,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); diff --git a/applications/main/subghz/scenes/subghz_scene_transmitter.c b/applications/main/subghz/scenes/subghz_scene_transmitter.c index 933f619f0..dba8d13c7 100644 --- a/applications/main/subghz/scenes/subghz_scene_transmitter.c +++ b/applications/main/subghz/scenes/subghz_scene_transmitter.c @@ -46,7 +46,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)) { diff --git a/applications/main/subghz_remote/subghz_remote_app_i.c b/applications/main/subghz_remote/subghz_remote_app_i.c index 6086b4c25..6ed8978de 100644 --- a/applications/main/subghz_remote/subghz_remote_app_i.c +++ b/applications/main/subghz_remote/subghz_remote_app_i.c @@ -179,8 +179,6 @@ bool subrem_tx_start_sub( FURI_LOG_I(TAG, "Send %s", furi_string_get_cstr(sub_preset->label)); - subghz_custom_btn_set(SUBGHZ_CUSTOM_BTN_OK); - keeloq_reset_original_btn(); subghz_custom_btns_reset(); do { @@ -257,7 +255,6 @@ bool subrem_tx_stop_sub(SubGhzRemoteApp* app, bool forced) { sub_preset->fff_data, furi_string_get_cstr(sub_preset->file_path)); subghz_environment_reset_keeloq(app->environment); - keeloq_reset_original_btn(); subghz_custom_btns_reset(); } diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 1e00e181c..a7470a0b9 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -1825,7 +1825,6 @@ Function,-,j1f,float,float Function,-,jn,double,"int, double" Function,-,jnf,float,"int, float" Function,-,jrand48,long,unsigned short[3] -Function,-,keeloq_reset_original_btn,void, Function,-,l64a,char*,long Function,-,labs,long,long Function,-,lcong48,void,unsigned short[7] @@ -2712,13 +2711,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* 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/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 df7ae748d..2163c0500 100644 --- a/lib/subghz/protocols/keeloq.c +++ b/lib/subghz/protocols/keeloq.c @@ -10,15 +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, @@ -91,15 +87,6 @@ const SubGhzProtocol subghz_protocol_keeloq = { .encoder = &subghz_protocol_keeloq_encoder, }; -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; -} - /** * Analysis of received data * @param instance Pointer to a SubGhzBlockGeneric* instance @@ -156,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; @@ -189,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( @@ -390,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; @@ -570,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; } @@ -930,9 +921,11 @@ 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) { @@ -949,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"; 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"; keystore->mfname = *manufacture_name; instance->cnt = temp_counter; + } else { + // Counter protection + furi_crash("Unsuported Prog Mode"); } instance->serial = key_fix & 0x0FFFFFFF; diff --git a/lib/subghz/protocols/keeloq.h b/lib/subghz/protocols/keeloq.h index 26f569e8c..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,8 +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_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 d39f794ab..607c929ea 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"