DerSkythe 3 лет назад
Родитель
Сommit
145b267d86

+ 63 - 13
applications/plugins/subbrute/helpers/subbrute_worker.c

@@ -12,7 +12,7 @@ SubBruteWorker* subbrute_worker_alloc() {
     SubBruteWorker* instance = malloc(sizeof(SubBruteWorker));
 
     instance->state = SubBruteWorkerStateIDLE;
-    instance->key_index = 0;
+    instance->step = 0;
     instance->worker_running = false;
     instance->initiated = false;
     instance->last_time_tx_data = 0;
@@ -31,6 +31,8 @@ SubBruteWorker* subbrute_worker_alloc() {
     instance->transmitter = NULL;
     instance->environment = subghz_environment_alloc();
 
+    instance->transmit_mode = false;
+
     return instance;
 }
 
@@ -54,7 +56,7 @@ void subbrute_worker_free(SubBruteWorker* instance) {
 }
 
 uint64_t subbrute_worker_get_step(SubBruteWorker* instance) {
-    return instance->key_index;
+    return instance->step;
 }
 
 bool subbrute_worker_set_step(SubBruteWorker* instance, uint64_t step) {
@@ -64,7 +66,7 @@ bool subbrute_worker_set_step(SubBruteWorker* instance, uint64_t step) {
         return false;
     }
 
-    instance->key_index = step;
+    instance->step = step;
 
     return true;
 }
@@ -85,13 +87,30 @@ bool subbrute_worker_init_default_attack(
     instance->frequency = protocol->frequency;
     instance->preset = protocol->preset;
     instance->file = protocol->file;
-    instance->key_index = step;
+    instance->step = step;
     instance->bits = protocol->bits;
     instance->te = protocol->te;
+    instance->repeat = protocol->repeat;
     instance->load_index = 0;
     instance->file_key = NULL;
     instance->max_value = subbrute_protocol_calc_max_value(instance->attack, instance->bits);
 
+    instance->initiated = true;
+    instance->state = SubBruteWorkerStateReady;
+    subbrute_worker_send_callback(instance);
+#ifdef FURI_DEBUG
+    FURI_LOG_I(
+        TAG,
+        "subbrute_worker_init_default_attack: %s, bits: %d, preset: %s, file: %s, te: %d, repeat: %d, max_value: %lld",
+        subbrute_protocol_name(instance->attack),
+        instance->bits,
+        subbrute_protocol_preset(instance->preset),
+        subbrute_protocol_file(instance->file),
+        instance->te,
+        instance->repeat,
+        instance->max_value);
+#endif
+
     return true;
 }
 
@@ -112,13 +131,30 @@ bool subbrute_worker_init_file_attack(
     instance->frequency = protocol->frequency;
     instance->preset = protocol->preset;
     instance->file = protocol->file;
-    instance->key_index = step;
+    instance->step = step;
     instance->bits = protocol->bits;
     instance->te = protocol->te;
     instance->load_index = load_index;
+    instance->repeat = protocol->repeat;
     instance->file_key = file_key;
     instance->max_value = subbrute_protocol_calc_max_value(instance->attack, instance->bits);
 
+    instance->initiated = true;
+    instance->state = SubBruteWorkerStateReady;
+    subbrute_worker_send_callback(instance);
+#ifdef FURI_DEBUG
+    FURI_LOG_I(
+        TAG,
+        "subbrute_worker_init_file_attack: %s, bits: %d, preset: %s, file: %s, te: %d, repeat: %d, max_value: %lld",
+        subbrute_protocol_name(instance->attack),
+        instance->bits,
+        subbrute_protocol_preset(instance->preset),
+        subbrute_protocol_file(instance->file),
+        instance->te,
+        instance->repeat,
+        instance->max_value);
+#endif
+
     return true;
 }
 
@@ -183,13 +219,13 @@ bool subbrute_worker_transmit_current_key(SubBruteWorker* instance, uint64_t ste
     }
 
     instance->last_time_tx_data = ticks;
-    instance->key_index = step;
+    instance->step = step;
 
     bool result;
     FlipperFormat* flipper_format = flipper_format_string_alloc();
     Stream* stream = flipper_format_get_raw_stream(flipper_format);
 
-    FuriString* payload = furi_string_alloc();
+    FuriString* payload = NULL;
     stream_clean(stream);
 
     if(instance->attack == SubBruteAttackLoadFile) {
@@ -252,8 +288,16 @@ void subbrute_worker_set_callback(
 }
 
 void subbrute_worker_subghz_transmit(SubBruteWorker* instance, FlipperFormat* flipper_format) {
+    while(instance->transmit_mode) {
+        furi_delay_ms(SUBBRUTE_TX_TIMEOUT);
+    }
+    instance->transmit_mode = true;
+    if(instance->transmitter != NULL) {
+        subghz_transmitter_free(instance->transmitter);
+        instance->transmitter = NULL;
+    }
     instance->transmitter = subghz_transmitter_alloc_init(
-        instance->environment, subbrute_protocol_name(instance->attack));
+        instance->environment, subbrute_protocol_file(instance->file));
     subghz_transmitter_deserialize(instance->transmitter, flipper_format);
     furi_hal_subghz_reset();
     furi_hal_subghz_load_preset(instance->preset);
@@ -269,6 +313,8 @@ void subbrute_worker_subghz_transmit(SubBruteWorker* instance, FlipperFormat* fl
     furi_hal_subghz_sleep();
     subghz_transmitter_free(instance->transmitter);
     instance->transmitter = NULL;
+
+    instance->transmit_mode = false;
 }
 
 void subbrute_worker_send_callback(SubBruteWorker* instance) {
@@ -307,12 +353,12 @@ int32_t subbrute_worker_thread(void* context) {
     Stream* stream = flipper_format_get_raw_stream(flipper_format);
 
     while(instance->worker_running) {
-        FuriString* payload = furi_string_alloc();
+        FuriString* payload = NULL;
         stream_clean(stream);
 
         if(instance->attack == SubBruteAttackLoadFile) {
             payload = subbrute_protocol_file_payload(
-                instance->key_index,
+                instance->step,
                 instance->bits,
                 instance->te,
                 instance->repeat,
@@ -320,8 +366,12 @@ int32_t subbrute_worker_thread(void* context) {
                 instance->file_key);
         } else {
             payload = subbrute_protocol_default_payload(
-                instance->key_index, instance->bits, instance->te, instance->repeat);
+                instance->step, instance->bits, instance->te, instance->repeat);
         }
+#ifdef FURI_DEBUG
+        FURI_LOG_I(TAG, "Payload: %s", furi_string_get_cstr(payload));
+        furi_delay_ms(SUBBRUTE_MANUAL_TRANSMIT_INTERVAL / 4);
+#endif
 
         size_t written = stream_write_string(stream, payload);
         if(written <= 0) {
@@ -334,7 +384,7 @@ int32_t subbrute_worker_thread(void* context) {
 
         subbrute_worker_subghz_transmit(instance, flipper_format);
 
-        if(instance->key_index + 1 > instance->max_value) {
+        if(instance->step + 1 > instance->max_value) {
 #ifdef FURI_DEBUG
             FURI_LOG_I(TAG, "Worker finished to end");
 #endif
@@ -342,7 +392,7 @@ int32_t subbrute_worker_thread(void* context) {
             furi_string_free(payload);
             break;
         }
-        instance->key_index++;
+        instance->step++;
 
         furi_string_free(payload);
         furi_delay_ms(SUBBRUTE_TX_TIMEOUT);

+ 2 - 1
applications/plugins/subbrute/helpers/subbrute_worker_private.h

@@ -10,9 +10,10 @@ struct SubBruteWorker {
     SubBruteWorkerState state;
     volatile bool worker_running;
     volatile bool initiated;
+    volatile bool transmit_mode;
 
     // Current step
-    uint64_t key_index;
+    uint64_t step;
 
     // SubGhz
     FuriThread* thread;

+ 8 - 1
applications/plugins/subbrute/scenes/subbrute_scene_run_attack.c

@@ -46,7 +46,10 @@ void subbrute_scene_run_attack_on_enter(void* context) {
         instance->worker, subbrute_scene_run_attack_device_state_changed, instance);
 
     if(!subbrute_worker_is_running(instance->worker)) {
-        subbrute_worker_start(instance->worker);
+        if(!subbrute_worker_start(instance->worker)) {
+            view_dispatcher_send_custom_event(
+                instance->view_dispatcher, SubBruteCustomEventTypeError);
+        }
     }
 }
 
@@ -72,6 +75,10 @@ bool subbrute_scene_run_attack_on_event(void* context, SceneManagerEvent event)
                 instance->scene_manager, SubBruteSceneSetupAttack);
         } else if(event.event == SubBruteCustomEventTypeError) {
             notification_message(instance->notifications, &sequence_error);
+
+            // Stop transmit
+            scene_manager_search_and_switch_to_previous_scene(
+                instance->scene_manager, SubBruteSceneSetupAttack);
         } else if(event.event == SubBruteCustomEventTypeUpdateView) {
             //subbrute_attack_view_set_current_step(view, instance->device->key_index);
         }

+ 6 - 1
applications/plugins/subbrute/scenes/subbrute_scene_start.c

@@ -41,7 +41,12 @@ bool subbrute_scene_start_on_event(void* context, SceneManagerEvent event) {
 
     if(event.type == SceneManagerEventTypeCustom) {
 #ifdef FURI_DEBUG
-        FURI_LOG_D(TAG, "Event: %ld", event.event);
+        FURI_LOG_D(
+            TAG,
+            "Event: %ld, SubBruteCustomEventTypeMenuSelected: %s, SubBruteCustomEventTypeLoadFile: %s",
+            event.event,
+            event.event == SubBruteCustomEventTypeMenuSelected ? "true" : "false",
+            event.event == SubBruteCustomEventTypeLoadFile ? "true" : "false");
 #endif
         if(event.event == SubBruteCustomEventTypeMenuSelected) {
             SubBruteAttacks attack = subbrute_main_view_get_index(instance->view_main);

+ 34 - 0
applications/plugins/subbrute/subbrute_device.c

@@ -153,6 +153,13 @@ SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBrute
     furi_hal_subghz_reset();
 
     uint8_t protocol_check_result = SubBruteFileResultProtocolNotFound;
+#ifdef FURI_DEBUG
+    uint8_t bits;
+    uint8_t te;
+    uint8_t repeat;
+    FuriHalSubGhzPreset preset;
+    SubBruteFileProtocol file;
+#endif
     if(type != SubBruteAttackLoadFile) {
         instance->decoder_result = subghz_receiver_search_decoder_base_by_name(
             instance->receiver, subbrute_protocol_file(instance->protocol_info->file));
@@ -167,6 +174,13 @@ SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBrute
             instance->max_value =
                 subbrute_protocol_calc_max_value(instance->attack, instance->protocol_info->bits);
         }
+#ifdef FURI_DEBUG
+        bits = instance->protocol_info->bits;
+        te = instance->protocol_info->te;
+        repeat = instance->protocol_info->repeat;
+        preset = instance->protocol_info->preset;
+        file = instance->protocol_info->file;
+#endif
     } else {
         // And here we need to set preset enum
         protocol_check_result = SubBruteFileResultOk;
@@ -174,6 +188,13 @@ SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBrute
         // Calc max value
         instance->max_value =
             subbrute_protocol_calc_max_value(instance->attack, instance->file_protocol_info->bits);
+#ifdef FURI_DEBUG
+        bits = instance->file_protocol_info->bits;
+        te = instance->file_protocol_info->te;
+        repeat = instance->file_protocol_info->repeat;
+        preset = instance->file_protocol_info->preset;
+        file = instance->file_protocol_info->file;
+#endif
     }
 
     subghz_receiver_free(instance->receiver);
@@ -183,6 +204,19 @@ SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBrute
         return SubBruteFileResultProtocolNotFound;
     }
 
+#ifdef FURI_DEBUG
+    FURI_LOG_I(
+        TAG,
+        "subbrute_device_attack_set: %s, bits: %d, preset: %s, file: %s, te: %d, repeat: %d, max_value: %lld",
+        subbrute_protocol_name(instance->attack),
+        bits,
+        subbrute_protocol_preset(preset),
+        subbrute_protocol_file(file),
+        te,
+        repeat,
+        instance->max_value);
+#endif
+
     return SubBruteFileResultOk;
 }
 

+ 5 - 5
applications/plugins/subbrute/subbrute_protocols.c

@@ -210,7 +210,7 @@ static const char* subbrute_key_file_start_no_tail =
     "Filetype: Flipper SubGhz Key File\nVersion: 1\nFrequency: %u\nPreset: %s\nProtocol: %s\nBit: %d\nKey: %s\nRepeat: %d\n";
 static const char* subbrute_key_file_start_with_tail =
     "Filetype: Flipper SubGhz Key File\nVersion: 1\nFrequency: %u\nPreset: %s\nProtocol: %s\nBit: %d\nKey: %s\nTE: %d\nRepeat: %d\n";
-static const char* subbrute_key_small_no_tail = "Bit: %d\nKey: %s\nRepeat: %d\nRepeat: %d\n";
+static const char* subbrute_key_small_no_tail = "Bit: %d\nKey: %s\nRepeat: %d\n";
 static const char* subbrute_key_small_with_tail = "Bit: %d\nKey: %s\nTE: %d\nRepeat: %d\n";
 
 const char* subbrute_protocol_name(SubBruteAttacks index) {
@@ -268,7 +268,7 @@ FuriString*
     furi_string_free(buffer);
 
 #ifdef FURI_DEBUG
-    FURI_LOG_D(TAG, "candidate: %s, step: %lld", furi_string_get_cstr(candidate), step);
+    //FURI_LOG_D(TAG, "candidate: %s, step: %lld", furi_string_get_cstr(candidate), step);
 #endif
     FuriString* result;
 
@@ -280,7 +280,7 @@ FuriString*
             subbrute_key_small_no_tail, bits, furi_string_get_cstr(candidate), repeat);
     }
 #ifdef FURI_DEBUG
-    FURI_LOG_D(TAG, "result: %s", furi_string_get_cstr(result));
+    //FURI_LOG_D(TAG, "result: %s", furi_string_get_cstr(result));
 #endif
 
     furi_string_free(candidate);
@@ -305,7 +305,7 @@ FuriString* subbrute_protocol_file_payload(
     furi_string_replace_at(candidate, load_index * 3, 3, subbrute_payload_byte);
 
 #ifdef FURI_DEBUG
-    FURI_LOG_D(TAG, "candidate: %s, step: %lld", furi_string_get_cstr(candidate), step);
+    //FURI_LOG_D(TAG, "candidate: %s, step: %lld", furi_string_get_cstr(candidate), step);
 #endif
     FuriString* result;
 
@@ -318,7 +318,7 @@ FuriString* subbrute_protocol_file_payload(
     }
 
 #ifdef FURI_DEBUG
-    FURI_LOG_D(TAG, "result: %s", furi_string_get_cstr(result));
+    //FURI_LOG_D(TAG, "result: %s", furi_string_get_cstr(result));
 #endif
 
     furi_string_free(candidate);