From e17877b6a1d8f88ac508bf27839d8f866c1b282e Mon Sep 17 00:00:00 2001 From: assasinfil Date: Thu, 9 Feb 2023 12:57:22 +0300 Subject: [PATCH 1/8] Kinggates stylo 4k tx realization Co-authored-by: MX <10697207+xMasterX@users.noreply.github.com> --- firmware/targets/f7/api_symbols.csv | 7 +- lib/subghz/protocols/kinggates_stylo_4k.c | 241 ++++++++++++++++++++-- lib/subghz/protocols/kinggates_stylo_4k.h | 36 ++++ 3 files changed, 265 insertions(+), 19 deletions(-) diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 0a90daf55..df703a4bd 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,+,13.1,, +Version,+,13.2,, Header,+,applications/services/bt/bt_service/bt.h,, Header,+,applications/services/cli/cli.h,, Header,+,applications/services/cli/cli_vcp.h,, @@ -3058,6 +3058,11 @@ Function,-,subghz_protocol_encoder_keeloq_deserialize,_Bool,"void*, FlipperForma Function,-,subghz_protocol_encoder_keeloq_free,void,void* Function,-,subghz_protocol_encoder_keeloq_stop,void,void* Function,-,subghz_protocol_encoder_keeloq_yield,LevelDuration,void* +Function,-,subghz_protocol_encoder_kinggates_stylo_4k_alloc,void*,SubGhzEnvironment* +Function,-,subghz_protocol_encoder_kinggates_stylo_4k_deserialize,_Bool,"void*, FlipperFormat*" +Function,-,subghz_protocol_encoder_kinggates_stylo_4k_free,void,void* +Function,-,subghz_protocol_encoder_kinggates_stylo_4k_stop,void,void* +Function,-,subghz_protocol_encoder_kinggates_stylo_4k_yield,LevelDuration,void* Function,-,subghz_protocol_encoder_linear_alloc,void*,SubGhzEnvironment* Function,-,subghz_protocol_encoder_linear_delta3_alloc,void*,SubGhzEnvironment* Function,-,subghz_protocol_encoder_linear_delta3_deserialize,_Bool,"void*, FlipperFormat*" diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index 2c8de0d2d..a92f646b8 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -23,7 +23,6 @@ struct SubGhzProtocolDecoderKingGates_stylo_4k { SubGhzBlockDecoder decoder; SubGhzBlockGeneric generic; - uint64_t data; uint16_t header_count; SubGhzKeystore* keystore; }; @@ -33,6 +32,7 @@ struct SubGhzProtocolEncoderKingGates_stylo_4k { SubGhzProtocolBlockEncoder encoder; SubGhzBlockGeneric generic; + SubGhzKeystore* keystore; }; typedef enum { @@ -57,23 +57,227 @@ const SubGhzProtocolDecoder subghz_protocol_kinggates_stylo_4k_decoder = { }; const SubGhzProtocolEncoder subghz_protocol_kinggates_stylo_4k_encoder = { - .alloc = NULL, - .free = NULL, + .alloc = subghz_protocol_encoder_kinggates_stylo_4k_alloc, + .free = subghz_protocol_encoder_kinggates_stylo_4k_free, - .deserialize = NULL, - .stop = NULL, - .yield = NULL, + .deserialize = subghz_protocol_encoder_kinggates_stylo_4k_deserialize, + .stop = subghz_protocol_encoder_kinggates_stylo_4k_stop, + .yield = subghz_protocol_encoder_kinggates_stylo_4k_yield, }; const SubGhzProtocol subghz_protocol_kinggates_stylo_4k = { .name = SUBGHZ_PROTOCOL_KINGGATES_STYLO_4K_NAME, .type = SubGhzProtocolTypeDynamic, - .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable, + .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | + SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send, .decoder = &subghz_protocol_kinggates_stylo_4k_decoder, .encoder = &subghz_protocol_kinggates_stylo_4k_encoder, }; +// +// Encoder +// + +// Pre define function +static void subghz_protocol_kinggates_stylo_4k_remote_controller( + SubGhzBlockGeneric* instance, + uint64_t data, + SubGhzKeystore* keystore); + +void* subghz_protocol_encoder_kinggates_stylo_4k_alloc(SubGhzEnvironment* environment) { + SubGhzProtocolEncoderKingGates_stylo_4k* instance = + malloc(sizeof(SubGhzProtocolEncoderKingGates_stylo_4k)); + + instance->base.protocol = &subghz_protocol_kinggates_stylo_4k; + instance->generic.protocol_name = instance->base.protocol->name; + instance->keystore = subghz_environment_get_keystore(environment); + + instance->encoder.repeat = 10; + instance->encoder.size_upload = 512; + instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration)); + instance->encoder.is_running = false; + + return instance; +} + +void subghz_protocol_encoder_kinggates_stylo_4k_free(void* context) { + furi_assert(context); + SubGhzProtocolEncoderKingGates_stylo_4k* instance = context; + free(instance->encoder.upload); + free(instance); +} + +void subghz_protocol_encoder_kinggates_stylo_4k_stop(void* context) { + SubGhzProtocolEncoderKingGates_stylo_4k* instance = context; + instance->encoder.is_running = false; +} + +LevelDuration subghz_protocol_encoder_kinggates_stylo_4k_yield(void* context) { + SubGhzProtocolEncoderKingGates_stylo_4k* instance = context; + + if(instance->encoder.repeat == 0 || !instance->encoder.is_running) { + instance->encoder.is_running = false; + return level_duration_reset(); + } + + LevelDuration ret = instance->encoder.upload[instance->encoder.front]; + + if(++instance->encoder.front == instance->encoder.size_upload) { + instance->encoder.repeat--; + instance->encoder.front = 0; + } + + return ret; +} + +/** + * Key generation from simple data + * @param instance Pointer to a SubGhzProtocolEncoderKingGates_stylo_4k* instance + * @param btn Button number, 4 bit + */ +static bool subghz_protocol_kinggates_stylo_4k_gen_data( + SubGhzProtocolEncoderKingGates_stylo_4k* instance, + uint8_t btn) { + if(instance->generic.cnt < 0xFFFF) { + instance->generic.cnt++; + } else if(instance->generic.cnt >= 0xFFFF) { + instance->generic.cnt = 0; + } + //uint64_t fix = instance->generic.data; + uint32_t decrypt = btn << 28 | 0x0C << 24 | (instance->generic.serial & 0xFF) << 16 | + instance->generic.cnt; + uint32_t hop = 0; + int res = 0; + + for + M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) { + res = strcmp(furi_string_get_cstr(manufacture_code->name), "Kingates_Stylo4k"); + if(res == 0) { + //Simple Learning + hop = subghz_protocol_keeloq_common_encrypt(decrypt, manufacture_code->key); + break; + } + } + + if(hop) { + instance->generic.data_2 = subghz_protocol_blocks_reverse_key(hop, 32); + return true; + } + return false; +} + +/** + * Generating an upload from data. + * @param instance Pointer to a SubGhzProtocolEncoderKingGates_stylo_4k instance + * @return true On success + */ +static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( + SubGhzProtocolEncoderKingGates_stylo_4k* instance, + uint8_t btn) { + furi_assert(instance); + + // Gen new key + if(subghz_protocol_kinggates_stylo_4k_gen_data(instance, btn)) { + //ToDo if you need to add a callback to automatically update the data on the display + } else { + return false; + } + + size_t index = 0; + /*size_t size_upload = (instance->generic.data_count_bit * 2); + if(size_upload > instance->encoder.size_upload) { + FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer."); + return false; + } else { + instance->encoder.size_upload = size_upload; + }*/ + + // Start + instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)9500); + + // Send header + for(uint8_t i = 12; i > 0; i--) { + instance->encoder.upload[index++] = + level_duration_make(true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); + instance->encoder.upload[index++] = level_duration_make( + false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); + } + + // After header + instance->encoder.upload[index - 1].duration += (uint32_t)2200; + instance->encoder.upload[index++] = + level_duration_make(true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short * 2); + + // Send key data + for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) { + if(bit_read(instance->generic.data, i - 1)) { + //send bit 1 + instance->encoder.upload[index++] = level_duration_make( + false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); + instance->encoder.upload[index++] = level_duration_make( + true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_long); + } else { + //send bit 0 + instance->encoder.upload[index++] = level_duration_make( + false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_long); + instance->encoder.upload[index++] = level_duration_make( + true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); + } + } + + // Set upload size after generating upload, fix it later + + instance->encoder.size_upload = index; + + return true; +} + +bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( + void* context, + FlipperFormat* flipper_format) { + furi_assert(context); + SubGhzProtocolEncoderKingGates_stylo_4k* instance = context; + bool res = false; + do { + if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) { + FURI_LOG_E(TAG, "Deserialize error"); + break; + } + + subghz_protocol_kinggates_stylo_4k_remote_controller( + &instance->generic, instance->generic.data_2, instance->keystore); + + //optional parameter parameter + flipper_format_read_uint32( + flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1); + + subghz_protocol_encoder_kinggates_stylo_4k_get_upload(instance, instance->generic.btn); + + if(!flipper_format_rewind(flipper_format)) { + FURI_LOG_E(TAG, "Rewind error"); + break; + } + uint8_t key_data[sizeof(uint64_t)] = {0}; + for(size_t i = 0; i < sizeof(uint64_t); i++) { + key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF; + } + if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) { + FURI_LOG_E(TAG, "Unable to add Key"); + break; + } + + instance->encoder.is_running = true; + + res = true; + } while(false); + + return res; +} + +// +// Decoder +// void* subghz_protocol_decoder_kinggates_stylo_4k_alloc(SubGhzEnvironment* environment) { SubGhzProtocolDecoderKingGates_stylo_4k* instance = malloc(sizeof(SubGhzProtocolDecoderKingGates_stylo_4k)); @@ -130,7 +334,7 @@ void subghz_protocol_decoder_kinggates_stylo_4k_feed(void* context, bool level, subghz_protocol_kinggates_stylo_4k_const.te_delta * 2) { instance->decoder.parser_step = KingGates_stylo_4kDecoderStepSaveDuration; instance->decoder.decode_data = 0; - instance->data = 0; + instance->generic.data_2 = 0; instance->decoder.decode_count_bit = 0; instance->header_count = 0; } @@ -140,8 +344,8 @@ void subghz_protocol_decoder_kinggates_stylo_4k_feed(void* context, bool level, if(duration >= ((uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_long * 3)) { if(instance->decoder.decode_count_bit == subghz_protocol_kinggates_stylo_4k_const.min_count_bit_for_found) { - instance->generic.data = instance->data; - instance->data = instance->decoder.decode_data; + instance->generic.data = instance->generic.data_2; + instance->generic.data_2 = instance->decoder.decode_data; instance->generic.data_count_bit = instance->decoder.decode_count_bit; if(instance->base.callback) @@ -150,7 +354,7 @@ void subghz_protocol_decoder_kinggates_stylo_4k_feed(void* context, bool level, instance->decoder.parser_step = KingGates_stylo_4kDecoderStepReset; instance->decoder.decode_data = 0; - instance->data = 0; + instance->generic.data_2 = 0; instance->decoder.decode_count_bit = 0; instance->header_count = 0; break; @@ -185,7 +389,7 @@ void subghz_protocol_decoder_kinggates_stylo_4k_feed(void* context, bool level, instance->header_count = 0; } if(instance->decoder.decode_count_bit == 53) { - instance->data = instance->decoder.decode_data; + instance->generic.data_2 = instance->decoder.decode_data; instance->decoder.decode_data = 0; } } else { @@ -199,7 +403,8 @@ void subghz_protocol_decoder_kinggates_stylo_4k_feed(void* context, bool level, /** * Analysis of received data * @param instance Pointer to a SubGhzBlockGeneric* instance - * @param file_name Full path to rainbow table the file + * @param data Input encrypted data + * @param keystore Pointer to a SubGhzKeystore* instance */ static void subghz_protocol_kinggates_stylo_4k_remote_controller( SubGhzBlockGeneric* instance, @@ -270,7 +475,7 @@ bool subghz_protocol_decoder_kinggates_stylo_4k_serialize( uint8_t key_data[sizeof(uint64_t)] = {0}; for(size_t i = 0; i < sizeof(uint64_t); i++) { - key_data[sizeof(uint64_t) - i - 1] = (instance->data >> (i * 8)) & 0xFF; + key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> (i * 8)) & 0xFF; } if(res && !flipper_format_write_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) { @@ -307,7 +512,7 @@ bool subghz_protocol_decoder_kinggates_stylo_4k_deserialize( break; } for(uint8_t i = 0; i < sizeof(uint64_t); i++) { - instance->data = instance->data << 8 | key_data[i]; + instance->generic.data_2 = instance->generic.data_2 << 8 | key_data[i]; } ret = true; } while(false); @@ -318,7 +523,7 @@ void subghz_protocol_decoder_kinggates_stylo_4k_get_string(void* context, FuriSt furi_assert(context); SubGhzProtocolDecoderKingGates_stylo_4k* instance = context; subghz_protocol_kinggates_stylo_4k_remote_controller( - &instance->generic, instance->data, instance->keystore); + &instance->generic, instance->generic.data_2, instance->keystore); furi_string_cat_printf( output, @@ -328,9 +533,9 @@ void subghz_protocol_decoder_kinggates_stylo_4k_get_string(void* context, FuriSt "Cnt:0x%04lX\r\n", instance->generic.protocol_name, instance->generic.data, - instance->data, + instance->generic.data_2, instance->generic.data_count_bit, instance->generic.serial, instance->generic.btn, instance->generic.cnt); -} +} \ No newline at end of file diff --git a/lib/subghz/protocols/kinggates_stylo_4k.h b/lib/subghz/protocols/kinggates_stylo_4k.h index c9f1cf380..9717f6715 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.h +++ b/lib/subghz/protocols/kinggates_stylo_4k.h @@ -10,6 +10,42 @@ extern const SubGhzProtocolDecoder subghz_protocol_kinggates_stylo_4k_decoder; extern const SubGhzProtocolEncoder subghz_protocol_kinggates_stylo_4k_encoder; extern const SubGhzProtocol subghz_protocol_kinggates_stylo_4k; +/** + * Allocate SubGhzProtocolEncoderKingGates_stylo_4k. + * @param environment Pointer to a SubGhzEnvironment instance + * @return SubGhzProtocolEncoderKingGates_stylo_4k* pointer to a SubGhzProtocolEncoderKingGates_stylo_4k instance + */ +void* subghz_protocol_encoder_kinggates_stylo_4k_alloc(SubGhzEnvironment* environment); + +/** + * Free SubGhzProtocolEncoderKingGates_stylo_4k. + * @param context Pointer to a SubGhzProtocolEncoderKingGates_stylo_4k instance + */ +void subghz_protocol_encoder_kinggates_stylo_4k_free(void* context); + +/** + * Deserialize and generating an upload to send. + * @param context Pointer to a SubGhzProtocolEncoderKingGates_stylo_4k instance + * @param flipper_format Pointer to a FlipperFormat instance + * @return true On success + */ +bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( + void* context, + FlipperFormat* flipper_format); + +/** + * Forced transmission stop. + * @param context Pointer to a SubGhzProtocolEncoderKingGates_stylo_4k instance + */ +void subghz_protocol_encoder_kinggates_stylo_4k_stop(void* context); + +/** + * Getting the level and duration of the upload to be loaded into DMA. + * @param context Pointer to a SubGhzProtocolEncoderKingGates_stylo_4k instance + * @return LevelDuration + */ +LevelDuration subghz_protocol_encoder_kinggates_stylo_4k_yield(void* context); + /** * Allocate SubGhzProtocolDecoderKingGates_stylo_4k. * @param environment Pointer to a SubGhzEnvironment instance From 5d6ce87259ee8989360ebc64be8cdfe14c43c873 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Thu, 9 Feb 2023 19:06:41 +0300 Subject: [PATCH 2/8] Encoder fix --- lib/subghz/protocols/kinggates_stylo_4k.c | 56 +++++++++++++++-------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index a92f646b8..effa199bf 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -82,7 +82,6 @@ const SubGhzProtocol subghz_protocol_kinggates_stylo_4k = { // Pre define function static void subghz_protocol_kinggates_stylo_4k_remote_controller( SubGhzBlockGeneric* instance, - uint64_t data, SubGhzKeystore* keystore); void* subghz_protocol_encoder_kinggates_stylo_4k_alloc(SubGhzEnvironment* environment) { @@ -139,29 +138,47 @@ LevelDuration subghz_protocol_encoder_kinggates_stylo_4k_yield(void* context) { static bool subghz_protocol_kinggates_stylo_4k_gen_data( SubGhzProtocolEncoderKingGates_stylo_4k* instance, uint8_t btn) { - if(instance->generic.cnt < 0xFFFF) { - instance->generic.cnt++; - } else if(instance->generic.cnt >= 0xFFFF) { - instance->generic.cnt = 0; - } - //uint64_t fix = instance->generic.data; - uint32_t decrypt = btn << 28 | 0x0C << 24 | (instance->generic.serial & 0xFF) << 16 | - instance->generic.cnt; - uint32_t hop = 0; + uint32_t hop = subghz_protocol_blocks_reverse_key(instance->generic.data_2 >> 4, 32); + uint64_t fix = subghz_protocol_blocks_reverse_key(instance->generic.data, 53); int res = 0; + uint32_t decrypt = 0; for M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) { res = strcmp(furi_string_get_cstr(manufacture_code->name), "Kingates_Stylo4k"); if(res == 0) { //Simple Learning - hop = subghz_protocol_keeloq_common_encrypt(decrypt, manufacture_code->key); + decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key); + break; + } + } + instance->generic.cnt = decrypt & 0xFFFF; + + if(instance->generic.cnt < 0xFFFF) { + instance->generic.cnt++; + } else if(instance->generic.cnt >= 0xFFFF) { + instance->generic.cnt = 0; + } + + instance->generic.btn = (fix >> 17) & 0x0F; + instance->generic.serial = ((fix >> 5) & 0xFFFF0000) | (fix & 0xFFFF); + //uint64_t fix = instance->generic.data; + uint32_t data = btn << 28 | 0x0C << 24 | (instance->generic.serial & 0xFF) << 16 | + instance->generic.cnt; + + uint32_t encrypt = 0; + for + M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) { + res = strcmp(furi_string_get_cstr(manufacture_code->name), "Kingates_Stylo4k"); + if(res == 0) { + //Simple Learning + encrypt = subghz_protocol_keeloq_common_encrypt(data, manufacture_code->key); break; } } - if(hop) { - instance->generic.data_2 = subghz_protocol_blocks_reverse_key(hop, 32); + if(encrypt) { + instance->generic.data_2 = subghz_protocol_blocks_reverse_key(encrypt, 32); return true; } return false; @@ -246,7 +263,7 @@ bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( } subghz_protocol_kinggates_stylo_4k_remote_controller( - &instance->generic, instance->generic.data_2, instance->keystore); + &instance->generic, instance->keystore); //optional parameter parameter flipper_format_read_uint32( @@ -260,9 +277,9 @@ bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( } uint8_t key_data[sizeof(uint64_t)] = {0}; for(size_t i = 0; i < sizeof(uint64_t); i++) { - key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF; + key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> i * 8) & 0xFF; } - if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) { + if(!flipper_format_update_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) { FURI_LOG_E(TAG, "Unable to add Key"); break; } @@ -408,7 +425,6 @@ void subghz_protocol_decoder_kinggates_stylo_4k_feed(void* context, bool level, */ static void subghz_protocol_kinggates_stylo_4k_remote_controller( SubGhzBlockGeneric* instance, - uint64_t data, SubGhzKeystore* keystore) { /** * 9500us 12*(400/400) 2200/800|1-bit|0-bit| @@ -431,7 +447,7 @@ static void subghz_protocol_kinggates_stylo_4k_remote_controller( * */ - uint32_t hop = subghz_protocol_blocks_reverse_key(data >> 4, 32); + uint32_t hop = subghz_protocol_blocks_reverse_key(instance->data_2 >> 4, 32); uint64_t fix = subghz_protocol_blocks_reverse_key(instance->data, 53); bool ret = false; uint32_t decrypt = 0; @@ -511,6 +527,7 @@ bool subghz_protocol_decoder_kinggates_stylo_4k_deserialize( FURI_LOG_E(TAG, "Missing Data"); break; } + for(uint8_t i = 0; i < sizeof(uint64_t); i++) { instance->generic.data_2 = instance->generic.data_2 << 8 | key_data[i]; } @@ -522,8 +539,7 @@ bool subghz_protocol_decoder_kinggates_stylo_4k_deserialize( void subghz_protocol_decoder_kinggates_stylo_4k_get_string(void* context, FuriString* output) { furi_assert(context); SubGhzProtocolDecoderKingGates_stylo_4k* instance = context; - subghz_protocol_kinggates_stylo_4k_remote_controller( - &instance->generic, instance->generic.data_2, instance->keystore); + subghz_protocol_kinggates_stylo_4k_remote_controller(&instance->generic, instance->keystore); furi_string_cat_printf( output, From 502e8d1d8d88c9c2b7c7f15c0a3da4f41679b9f3 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Thu, 9 Feb 2023 21:17:16 +0300 Subject: [PATCH 3/8] Small fix --- lib/subghz/protocols/kinggates_stylo_4k.c | 63 +++++++++++++++++------ 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index effa199bf..f851efa9b 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -138,6 +138,7 @@ LevelDuration subghz_protocol_encoder_kinggates_stylo_4k_yield(void* context) { static bool subghz_protocol_kinggates_stylo_4k_gen_data( SubGhzProtocolEncoderKingGates_stylo_4k* instance, uint8_t btn) { + UNUSED(btn); uint32_t hop = subghz_protocol_blocks_reverse_key(instance->generic.data_2 >> 4, 32); uint64_t fix = subghz_protocol_blocks_reverse_key(instance->generic.data, 53); int res = 0; @@ -154,33 +155,32 @@ static bool subghz_protocol_kinggates_stylo_4k_gen_data( } instance->generic.cnt = decrypt & 0xFFFF; - if(instance->generic.cnt < 0xFFFF) { - instance->generic.cnt++; - } else if(instance->generic.cnt >= 0xFFFF) { - instance->generic.cnt = 0; - } + // if(instance->generic.cnt < 0xFFFF) { + // instance->generic.cnt++; + // } else if(instance->generic.cnt >= 0xFFFF) { + // instance->generic.cnt = 0; + // } instance->generic.btn = (fix >> 17) & 0x0F; instance->generic.serial = ((fix >> 5) & 0xFFFF0000) | (fix & 0xFFFF); //uint64_t fix = instance->generic.data; - uint32_t data = btn << 28 | 0x0C << 24 | (instance->generic.serial & 0xFF) << 16 | - instance->generic.cnt; + uint32_t data = decrypt >> 16; + data <<= 16; + data |= instance->generic.cnt; - uint32_t encrypt = 0; + uint64_t encrypt = 0; for M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) { res = strcmp(furi_string_get_cstr(manufacture_code->name), "Kingates_Stylo4k"); if(res == 0) { //Simple Learning encrypt = subghz_protocol_keeloq_common_encrypt(data, manufacture_code->key); - break; + // encrypt = subghz_protocol_blocks_reverse_key(encrypt, 32); + instance->generic.data_2 = encrypt << 4; + return true; } } - if(encrypt) { - instance->generic.data_2 = subghz_protocol_blocks_reverse_key(encrypt, 32); - return true; - } return false; } @@ -227,8 +227,23 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( level_duration_make(true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short * 2); // Send key data - for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) { - if(bit_read(instance->generic.data, i - 1)) { + for(uint8_t i = instance->generic.data_count_bit; i > 64; i--) { + if(bit_read(instance->generic.data, i - 1 - 64)) { + //send bit 1 + instance->encoder.upload[index++] = level_duration_make( + false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); + instance->encoder.upload[index++] = level_duration_make( + true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_long); + } else { + //send bit 0 + instance->encoder.upload[index++] = level_duration_make( + false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_long); + instance->encoder.upload[index++] = level_duration_make( + true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); + } + } + for(uint8_t i = 64; i > 0; i--) { + if(bit_read(instance->generic.data_2, i - 1)) { //send bit 1 instance->encoder.upload[index++] = level_duration_make( false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); @@ -269,13 +284,29 @@ bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( flipper_format_read_uint32( flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1); + if(!flipper_format_rewind(flipper_format)) { + FURI_LOG_E(TAG, "Rewind error"); + break; + } + + uint8_t key_data[sizeof(uint64_t)] = {0}; + if(!flipper_format_read_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) { + FURI_LOG_E(TAG, "Missing Data"); + break; + } + + for(uint8_t i = 0; i < sizeof(uint64_t); i++) { + instance->generic.data_2 = instance->generic.data_2 << 8 | key_data[i]; + } + subghz_protocol_encoder_kinggates_stylo_4k_get_upload(instance, instance->generic.btn); if(!flipper_format_rewind(flipper_format)) { FURI_LOG_E(TAG, "Rewind error"); break; } - uint8_t key_data[sizeof(uint64_t)] = {0}; + + // uint8_t key_data[sizeof(uint64_t)] = {0}; for(size_t i = 0; i < sizeof(uint64_t); i++) { key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> i * 8) & 0xFF; } From b47174d2067ee76fad1fc955e833a30a4c164b65 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Thu, 9 Feb 2023 21:26:10 +0300 Subject: [PATCH 4/8] Completed encoder --- lib/subghz/protocols/kinggates_stylo_4k.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index f851efa9b..a6f71342c 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -155,15 +155,15 @@ static bool subghz_protocol_kinggates_stylo_4k_gen_data( } instance->generic.cnt = decrypt & 0xFFFF; - // if(instance->generic.cnt < 0xFFFF) { - // instance->generic.cnt++; - // } else if(instance->generic.cnt >= 0xFFFF) { - // instance->generic.cnt = 0; - // } + if(instance->generic.cnt < 0xFFFF) { + instance->generic.cnt++; + } else if(instance->generic.cnt >= 0xFFFF) { + instance->generic.cnt = 0; + } instance->generic.btn = (fix >> 17) & 0x0F; instance->generic.serial = ((fix >> 5) & 0xFFFF0000) | (fix & 0xFFFF); - //uint64_t fix = instance->generic.data; + uint32_t data = decrypt >> 16; data <<= 16; data |= instance->generic.cnt; @@ -175,7 +175,7 @@ static bool subghz_protocol_kinggates_stylo_4k_gen_data( if(res == 0) { //Simple Learning encrypt = subghz_protocol_keeloq_common_encrypt(data, manufacture_code->key); - // encrypt = subghz_protocol_blocks_reverse_key(encrypt, 32); + encrypt = subghz_protocol_blocks_reverse_key(encrypt, 32); instance->generic.data_2 = encrypt << 4; return true; } @@ -306,7 +306,7 @@ bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( break; } - // uint8_t key_data[sizeof(uint64_t)] = {0}; + // uint8_t key_data2[sizeof(uint64_t)] = {0}; for(size_t i = 0; i < sizeof(uint64_t); i++) { key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> i * 8) & 0xFF; } From 05be2007624787db28e0f100b7a73cf30e1a5ba0 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Thu, 9 Feb 2023 21:37:54 +0300 Subject: [PATCH 5/8] Hop len fix --- lib/subghz/protocols/kinggates_stylo_4k.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index a6f71342c..d86c5cd29 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -242,7 +242,8 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); } } - for(uint8_t i = 64; i > 0; i--) { + + for(uint8_t i = 36; i > 0; i--) { if(bit_read(instance->generic.data_2, i - 1)) { //send bit 1 instance->encoder.upload[index++] = level_duration_make( From a551317cc2803321a5b6e8f6bdbb15c0362a33db Mon Sep 17 00:00:00 2001 From: assasinfil Date: Fri, 10 Feb 2023 15:55:31 +0300 Subject: [PATCH 6/8] Encoder fix --- lib/subghz/protocols/kinggates_stylo_4k.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index d86c5cd29..1249af5b2 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -176,6 +176,8 @@ static bool subghz_protocol_kinggates_stylo_4k_gen_data( //Simple Learning encrypt = subghz_protocol_keeloq_common_encrypt(data, manufacture_code->key); encrypt = subghz_protocol_blocks_reverse_key(encrypt, 32); + // instance->generic.data = + // subghz_protocol_blocks_reverse_key(instance->generic.data, 64); instance->generic.data_2 = encrypt << 4; return true; } @@ -222,13 +224,14 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( } // After header - instance->encoder.upload[index - 1].duration += (uint32_t)2200; + instance->encoder.upload[index - 1].duration = + (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_long * 2; instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short * 2); // Send key data - for(uint8_t i = instance->generic.data_count_bit; i > 64; i--) { - if(bit_read(instance->generic.data, i - 1 - 64)) { + for(uint8_t i = 53; i > 0; i--) { + if(bit_read(instance->generic.data, i - 1)) { //send bit 1 instance->encoder.upload[index++] = level_duration_make( false, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short); @@ -243,7 +246,7 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( } } - for(uint8_t i = 36; i > 0; i--) { + for(uint8_t i = 36; i > 4; i--) { if(bit_read(instance->generic.data_2, i - 1)) { //send bit 1 instance->encoder.upload[index++] = level_duration_make( From d9f6b02da5005ae071220e64880f2d260153bbf7 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Fri, 10 Feb 2023 16:17:56 +0300 Subject: [PATCH 7/8] Hop fix --- lib/subghz/protocols/kinggates_stylo_4k.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index 1249af5b2..f4b8c8712 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -246,7 +246,7 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( } } - for(uint8_t i = 36; i > 4; i--) { + for(uint8_t i = 36; i > 0; i--) { if(bit_read(instance->generic.data_2, i - 1)) { //send bit 1 instance->encoder.upload[index++] = level_duration_make( From 5cf4f5fab4fba9192009bf57a5dfe8a6700a3156 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Fri, 10 Feb 2023 16:32:30 +0300 Subject: [PATCH 8/8] Cleanup --- lib/subghz/protocols/kinggates_stylo_4k.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/subghz/protocols/kinggates_stylo_4k.c b/lib/subghz/protocols/kinggates_stylo_4k.c index f4b8c8712..5f2a83d77 100644 --- a/lib/subghz/protocols/kinggates_stylo_4k.c +++ b/lib/subghz/protocols/kinggates_stylo_4k.c @@ -164,9 +164,7 @@ static bool subghz_protocol_kinggates_stylo_4k_gen_data( instance->generic.btn = (fix >> 17) & 0x0F; instance->generic.serial = ((fix >> 5) & 0xFFFF0000) | (fix & 0xFFFF); - uint32_t data = decrypt >> 16; - data <<= 16; - data |= instance->generic.cnt; + uint32_t data = (decrypt & 0xFFFF0000) | instance->generic.cnt; uint64_t encrypt = 0; for @@ -176,8 +174,6 @@ static bool subghz_protocol_kinggates_stylo_4k_gen_data( //Simple Learning encrypt = subghz_protocol_keeloq_common_encrypt(data, manufacture_code->key); encrypt = subghz_protocol_blocks_reverse_key(encrypt, 32); - // instance->generic.data = - // subghz_protocol_blocks_reverse_key(instance->generic.data, 64); instance->generic.data_2 = encrypt << 4; return true; } @@ -204,13 +200,6 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( } size_t index = 0; - /*size_t size_upload = (instance->generic.data_count_bit * 2); - if(size_upload > instance->encoder.size_upload) { - FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer."); - return false; - } else { - instance->encoder.size_upload = size_upload; - }*/ // Start instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)9500); @@ -229,7 +218,7 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_kinggates_stylo_4k_const.te_short * 2); - // Send key data + // Send key fix for(uint8_t i = 53; i > 0; i--) { if(bit_read(instance->generic.data, i - 1)) { //send bit 1 @@ -246,6 +235,7 @@ static bool subghz_protocol_encoder_kinggates_stylo_4k_get_upload( } } + // Send key hop for(uint8_t i = 36; i > 0; i--) { if(bit_read(instance->generic.data_2, i - 1)) { //send bit 1 @@ -310,7 +300,6 @@ bool subghz_protocol_encoder_kinggates_stylo_4k_deserialize( break; } - // uint8_t key_data2[sizeof(uint64_t)] = {0}; for(size_t i = 0; i < sizeof(uint64_t); i++) { key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> i * 8) & 0xFF; }