From 32b40d268a5dd7bfd7583c89b73070342975c2a6 Mon Sep 17 00:00:00 2001 From: Derek Jamison Date: Fri, 20 Oct 2023 16:43:38 -0500 Subject: [PATCH] Fix off-by-one bug. Stop clicking when all found. --- subghz/apps/genie-recorder/application.fam | 2 +- subghz/apps/genie-recorder/genie.c | 16 ++++++++------ subghz/apps/genie-recorder/genie_about.h | 2 +- subghz/apps/genie-recorder/genie_file.c | 25 ++++++++++++++++------ subghz/apps/genie-recorder/genie_file.h | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/subghz/apps/genie-recorder/application.fam b/subghz/apps/genie-recorder/application.fam index e774c82..db68073 100644 --- a/subghz/apps/genie-recorder/application.fam +++ b/subghz/apps/genie-recorder/application.fam @@ -5,7 +5,7 @@ App( entry_point="genie_record_app", requires=["gui", "subghz"], stack_size=4 * 1024, - fap_version=(2, 4), + fap_version=(2, 5), fap_icon="genie.png", fap_category="Sub-GHz", fap_icon_assets="assets", diff --git a/subghz/apps/genie-recorder/genie.c b/subghz/apps/genie-recorder/genie.c index 3847475..8be62a2 100644 --- a/subghz/apps/genie-recorder/genie.c +++ b/subghz/apps/genie-recorder/genie.c @@ -42,7 +42,7 @@ typedef struct { bool pressed; uint32_t click_counter; uint32_t rx_counter; - uint16_t genie_save_counter; + uint32_t genie_save_counter; FuriString* key; } GenieApp; @@ -116,8 +116,8 @@ static void genie_view_draw_callback(Canvas* canvas, void* model) { "Got %ld", app->genie_save_counter > 0 ? app->genie_save_counter : app->rx_counter); canvas_draw_str(canvas, 75, 45, buffer); - if(app->genie_save_counter != 0xFFFF) { - snprintf(buffer, COUNT_OF(buffer), "Remaining codes %d", 65536 - app->genie_save_counter); + if(app->genie_save_counter < 0x10000) { + snprintf(buffer, COUNT_OF(buffer), "Remaining codes %ld", 65536 - app->genie_save_counter); canvas_draw_str(canvas, 1, 32, buffer); } else { canvas_draw_str(canvas, 1, 30, "Found all codes!"); @@ -136,7 +136,7 @@ static uint32_t last_count() { return count; } -static uint16_t save_count(uint32_t count, FuriString* key, bool is_genie) { +static uint32_t save_count(uint32_t count, FuriString* key, bool is_genie) { FURI_LOG_D(TAG, "%ld,%s", count, furi_string_get_cstr(key)); genie_save(count, key); if(is_genie) { @@ -169,7 +169,7 @@ static void genie_packet(FuriString* buffer, void* context) { GenieApp* app = (GenieApp*)context; app->processing = true; -/* + /* if(furi_string_search_str(buffer, "KeeLoq 64bit") < furi_string_size(buffer)) { release_button(app); FURI_LOG_D(TAG, "KeeLoq 64bit packet"); @@ -180,7 +180,7 @@ static void genie_packet(FuriString* buffer, void* context) { save_count(app->click_counter, app->key, false); } } else -*/ +*/ if(furi_string_search_str(buffer, "Genie 64bit") < furi_string_size(buffer)) { release_button(app); FURI_LOG_D(TAG, "Genie 64bit packet"); @@ -198,7 +198,9 @@ static void genie_packet(FuriString* buffer, void* context) { static void genie_tick(void* context) { GenieApp* app = (GenieApp*)context; if(!app->processing) { - if(app->pressed) { + if(app->genie_save_counter > 0xFFFF) { + release_button(app); + } else if(app->pressed) { release_button(app); } else { app->click_counter++; diff --git a/subghz/apps/genie-recorder/genie_about.h b/subghz/apps/genie-recorder/genie_about.h index 77f514c..dc0bb13 100644 --- a/subghz/apps/genie-recorder/genie_about.h +++ b/subghz/apps/genie-recorder/genie_about.h @@ -2,7 +2,7 @@ #define GENIE_ABOUT_TEXT \ "Genie garage door recorder.\n" \ - "Version 2.4\n---\n" \ + "Version 2.5\n---\n" \ "Custom genie firmware is\n" \ "required for this app.\n" \ "---\n" \ diff --git a/subghz/apps/genie-recorder/genie_file.c b/subghz/apps/genie-recorder/genie_file.c index 80d1e04..6dd264f 100644 --- a/subghz/apps/genie-recorder/genie_file.c +++ b/subghz/apps/genie-recorder/genie_file.c @@ -10,7 +10,7 @@ // Should match application.fam, fap_version. #define GENIE_MAJOR_VERSION 2 -#define GENIE_MINOR_VERSION 4 +#define GENIE_MINOR_VERSION 5 #define GENIE_MAGIC_CODE 0x472A @@ -120,8 +120,8 @@ static uint32_t hex_to_i32(const char* data) { return value; } -uint16_t genie_save_bin(const char* key) { - uint16_t result = 0; +uint32_t genie_save_bin(const char* key) { + uint32_t result = 0; uint32_t key_high = 0; uint32_t key_low = 0; key_high = hex_to_i32(key); @@ -189,9 +189,17 @@ uint16_t genie_save_bin(const char* key) { uint32_t existing = storage_file_read32(file); if(existing != 0 && existing != key_high) { - FURI_LOG_E( - TAG, "Key mismatch at %04X. Old: %08lX, New: %08lX", count, existing, key_high); - result = count; + if(count != 0xFFFF) { + FURI_LOG_E( + TAG, + "Key mismatch at %04X. Old: %08lX, New: %08lX", + count, + existing, + key_high); + result = count; + } else { + result = 0x10000; + } break; } @@ -213,13 +221,16 @@ uint16_t genie_save_bin(const char* key) { if(count != 0xFFFF) { count++; + result = count; + if(!storage_file_write16(file, count)) { FURI_LOG_E(TAG, "Failed to write count to file: %04X", count); break; } + } else { + result = 0x10000; } - result = count; } else { FURI_LOG_E(TAG, "Failed to open file"); break; diff --git a/subghz/apps/genie-recorder/genie_file.h b/subghz/apps/genie-recorder/genie_file.h index 97fbc1a..77d3531 100644 --- a/subghz/apps/genie-recorder/genie_file.h +++ b/subghz/apps/genie-recorder/genie_file.h @@ -3,6 +3,6 @@ #include bool genie_save(uint32_t count, FuriString* key); -uint16_t genie_save_bin(const char* key); +uint32_t genie_save_bin(const char* key); uint32_t genie_load(); void genie_file_init(); \ No newline at end of file