From 2762ff2deff461a96a4d32b4e62f077175897397 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:11:43 +0300 Subject: [PATCH] gangqi serial validator and fix add manually --- .../helpers/subghz_txrx_create_protocol_key.c | 25 +++++++++++++++++++ .../helpers/subghz_txrx_create_protocol_key.h | 7 ++++++ .../subghz/scenes/subghz_scene_set_type.c | 12 ++++----- lib/subghz/protocols/gangqi.c | 21 +++++++++++----- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.c b/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.c index a29484902..1ec4a13f0 100644 --- a/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.c +++ b/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.c @@ -382,3 +382,28 @@ bool subghz_txrx_gen_secplus_v1_protocol( } return ret; } + +void subghz_txrx_gen_serial_gangqi(uint64_t* result_key) { + uint64_t randkey = (uint64_t)rand(); + uint64_t only_required_bytes = (randkey & 0xFFFFF0000); + uint16_t sum_of_3bytes = ((only_required_bytes >> 32) & 0xFF) + + ((only_required_bytes >> 24) & 0xFF) + + ((only_required_bytes >> 16) & 0xFF); + + while(!((!(sum_of_3bytes & 0x3)) && ((0xb2 < sum_of_3bytes) && (sum_of_3bytes < 0x1ae)))) { + randkey = (uint64_t)rand(); + only_required_bytes = (randkey & 0xFFFFF0000); + sum_of_3bytes = ((only_required_bytes >> 32) & 0xFF) + + ((only_required_bytes >> 24) & 0xFF) + + ((only_required_bytes >> 16) & 0xFF); + } + + // Serial 01 button 01 + uint64_t new_key = only_required_bytes | (0b01 << 14) | (0xD << 10) | (0b01 << 8); + + uint8_t crc = -0xD7 - ((new_key >> 32) & 0xFF) - ((new_key >> 24) & 0xFF) - + ((new_key >> 16) & 0xFF) - ((new_key >> 8) & 0xFF); + + // Add crc sum to the end + *result_key = (new_key | crc); +} diff --git a/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.h b/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.h index 0b9cfefab..55932bd39 100644 --- a/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.h +++ b/applications/main/subghz/helpers/subghz_txrx_create_protocol_key.h @@ -146,3 +146,10 @@ bool subghz_txrx_gen_secplus_v1_protocol( SubGhzTxRx* instance, const char* name_preset, uint32_t frequency); + +/** + * Generate valid serial number for GangQi protocol + * + * @return uint64_t if success + */ +void subghz_txrx_gen_serial_gangqi(uint64_t* result_key); diff --git a/applications/main/subghz/scenes/subghz_scene_set_type.c b/applications/main/subghz/scenes/subghz_scene_set_type.c index 554b7cd7e..43bf05044 100644 --- a/applications/main/subghz/scenes/subghz_scene_set_type.c +++ b/applications/main/subghz/scenes/subghz_scene_set_type.c @@ -183,6 +183,10 @@ bool subghz_scene_set_type_on_event(void* context, SceneManagerEvent event) { } uint64_t key = (uint64_t)rand(); + + uint64_t gangqi_key; + subghz_txrx_gen_serial_gangqi(&gangqi_key); + GenInfo gen_info = {0}; switch(event.event) { case SetTypePricenton433: @@ -312,13 +316,7 @@ bool subghz_scene_set_type_on_event(void* context, SceneManagerEvent event) { .freq = 433920000, .data.name = SUBGHZ_PROTOCOL_GANGQI_NAME, // Add button 0xD arm and crc sum to the end - .data.key = - ((key & 0x00FF00000) | 0x3400B7500 | - ((((-0xD7 - (((key & 0x00FF00000) | 0x3400B7500) >> 32)) & 0xFF) - - ((((key & 0x00FF00000) | 0x3400B7500) >> 24) & 0xFF) - - ((((key & 0x00FF00000) | 0x3400B7500) >> 16) & 0xFF) - - ((((key & 0x00FF00000) | 0x3400B7500) >> 8) & 0xFF)) & - 0xFF)), + .data.key = gangqi_key, .data.bits = 34, .data.te = 0}; break; diff --git a/lib/subghz/protocols/gangqi.c b/lib/subghz/protocols/gangqi.c index c6d9dd0e8..00e731e21 100644 --- a/lib/subghz/protocols/gangqi.c +++ b/lib/subghz/protocols/gangqi.c @@ -233,10 +233,10 @@ static void subghz_protocol_gangqi_remote_controller(SubGhzBlockGeneric* instanc // 09.2024 - @xMasterX (MMX) // Thanks @Skorpionm for support! + //// 4D=F8=171=229 byte sum should be always the same // Button // Serial || BBBB || CRC (byte sum) with overflow and starting point 0xD7 - //34AAB75BC = 00110100101010101011 01 1101 01 101111 00 // A (0xD) - // 4D=F8=171=229 byte sum + //034AAB75BC = 00110100101010101011 01 1101 01 101111 00 // A (0xD) //034AAB79B8 = 00110100101010101011 01 1110 01 101110 00 // B (0xE) //034AAB6DC4 = 00110100101010101011 01 1011 01 110001 00 // C (0xB) //034AAB5DD4 = 00110100101010101011 01 0111 01 110101 00 // D (0x7) @@ -245,8 +245,8 @@ static void subghz_protocol_gangqi_remote_controller(SubGhzBlockGeneric* instanc //034AAB49E8 = 00110100101010101011 01 0010 01 111010 00 // C (0x2) //034AAB59D8 = 00110100101010101011 01 0110 01 110110 00 // D (0x6) //034AAB45EC = 00110100101010101011 01 0001 01 111011 00 // Settings exit (0x1) - //0348557514 = 00110100100001010101 01 1101 01 000101 00 - //03427B75F4 = 00110100001001111011 01 1101 01 111101 00 + // + // Serial 3 bytes should meet requirements see validation example at subghz_protocol_decoder_gangqi_get_string // // Code for finding start byte for crc sum // @@ -488,12 +488,17 @@ void subghz_protocol_decoder_gangqi_get_string(void* context, FuriString* output ((instance->generic.data >> 24) & 0xFF) - ((instance->generic.data >> 16) & 0xFF) - ((instance->generic.data >> 8) & 0xFF); + uint16_t sum_3bytes_serial = ((instance->generic.serial >> 16) & 0xFF) + + ((instance->generic.serial >> 8) & 0xFF) + + (instance->generic.serial & 0xFF); + furi_string_cat_printf( output, "%s %db\r\n" "Key: 0x%X%08lX\r\n" "Serial: 0x%05lX CRC: 0x%02X\r\n" - "Btn: 0x%01X - %s\r\n", + "Btn: 0x%01X - %s\r\n" + "Serial is %s\r\n", instance->generic.protocol_name, instance->generic.data_count_bit, (uint8_t)(instance->generic.data >> 32), @@ -501,5 +506,9 @@ void subghz_protocol_decoder_gangqi_get_string(void* context, FuriString* output instance->generic.serial, crc, instance->generic.btn, - subghz_protocol_gangqi_get_button_name(instance->generic.btn)); + subghz_protocol_gangqi_get_button_name(instance->generic.btn), + ((!(sum_3bytes_serial & 0x3)) && + ((0xb2 < sum_3bytes_serial) && (sum_3bytes_serial < 0x1ae))) ? + "valid" : + "invalid"); }