Просмотр исходного кода

Merge branch 'master' into feat/more-repeats

DerSkythe 3 лет назад
Родитель
Сommit
0688a783c0

+ 0 - 1
scenes/subbrute_scene_setup_attack.c

@@ -71,7 +71,6 @@ bool subbrute_scene_setup_attack_on_event(void* context, SceneManagerEvent event
 
     if(event.type == SceneManagerEventTypeCustom) {
         if(event.event == SubBruteCustomEventTypeTransmitStarted) {
-            subbrute_attack_view_set_worker_type(view, false);
             scene_manager_next_scene(instance->scene_manager, SubBruteSceneRunAttack);
         } else if(event.event == SubBruteCustomEventTypeSaveFile) {
             subbrute_attack_view_init_values(

+ 57 - 47
views/subbrute_attack_view.c

@@ -14,10 +14,14 @@ struct SubBruteAttackView {
     View* view;
     SubBruteAttackViewCallback callback;
     void* context;
+    SubBruteAttacks attack_type;
+    uint64_t max_value;
+    uint64_t current_step;
+    bool is_attacking;
 };
 
 typedef struct {
-    SubBruteAttacks index;
+    SubBruteAttacks attack_type;
     uint64_t max_value;
     uint64_t current_step;
     uint8_t extra_repeats;
@@ -46,76 +50,81 @@ bool subbrute_attack_view_input(InputEvent* event, void* context) {
     SubBruteAttackView* instance = context;
 
     if(event->key == InputKeyBack && event->type == InputTypeShort) {
-        instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
+        instance->is_attacking = false;
         with_view_model(
             instance->view,
             SubBruteAttackViewModel * model,
-            {
-                model->is_attacking = false;
-                model->is_continuous_worker = false;
-            },
+            { model->is_attacking = false; },
             true);
+
+        instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
         return true;
     }
 
-    bool is_attacking = false;
+    bool update = false;
 
-    with_view_model(
-        instance->view,
-        SubBruteAttackViewModel * model,
-        { is_attacking = model->is_attacking; },
-        false);
-
-    if(!is_attacking) {
+    if(!instance->is_attacking) {
         if(event->type == InputTypeShort && event->key == InputKeyOk) {
 #ifdef FURI_DEBUG
             FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
 #endif
-            with_view_model(
-                instance->view,
-                SubBruteAttackViewModel * model,
-                {
-                    model->is_attacking = true;
-                    model->is_continuous_worker = false;
-                    icon_animation_stop(model->icon);
-                    icon_animation_start(model->icon);
-                },
-                true);
+            instance->is_attacking = true;
             instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
+            update = true;
         } else if(event->key == InputKeyUp) {
             instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
+            update = true;
         } else if(event->key == InputKeyDown) {
             instance->callback(SubBruteCustomEventTypeTransmitCustom, instance->context);
+            update = true;
         } else if(event->type == InputTypeShort) {
             if(event->key == InputKeyLeft) {
                 instance->callback(SubBruteCustomEventTypeChangeStepDown, instance->context);
             } else if(event->key == InputKeyRight) {
                 instance->callback(SubBruteCustomEventTypeChangeStepUp, instance->context);
             }
+            update = true;
         } else if(event->type == InputTypeRepeat) {
             if(event->key == InputKeyLeft) {
                 instance->callback(SubBruteCustomEventTypeChangeStepDownMore, instance->context);
             } else if(event->key == InputKeyRight) {
                 instance->callback(SubBruteCustomEventTypeChangeStepUpMore, instance->context);
             }
+            update = true;
         }
     } else {
+        // ATTACK Mode!
         if((event->type == InputTypeShort || event->type == InputTypeRepeat) &&
            (event->key == InputKeyOk || event->key == InputKeyBack)) {
-            with_view_model(
-                instance->view,
-                SubBruteAttackViewModel * model,
-                {
-                    model->is_attacking = false;
-                    model->is_continuous_worker = false;
-                    icon_animation_stop(model->icon);
-                    icon_animation_start(model->icon);
-                },
-                true);
+            instance->is_attacking = false;
             instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
+
+            update = true;
         }
     }
 
+    if(update) {
+        with_view_model(
+            instance->view,
+            SubBruteAttackViewModel * model,
+            {
+                if(model->is_attacking != instance->is_attacking) {
+                    if(instance->is_attacking) {
+                        icon_animation_stop(model->icon);
+                        icon_animation_start(model->icon);
+                    } else {
+                        icon_animation_stop(model->icon);
+                    }
+                }
+
+                model->attack_type = instance->attack_type;
+                model->max_value = instance->max_value;
+                model->current_step = instance->current_step;
+                model->is_attacking = instance->is_attacking;
+            },
+            true);
+    }
+
     return true;
 }
 
@@ -133,13 +142,18 @@ SubBruteAttackView* subbrute_attack_view_alloc() {
             model->icon = icon_animation_alloc(&A_Sub1ghz_14);
             view_tie_icon_animation(instance->view, model->icon);
         },
-        false);
+        true);
 
     view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_attack_view_draw);
     view_set_input_callback(instance->view, subbrute_attack_view_input);
     view_set_enter_callback(instance->view, subbrute_attack_view_enter);
     view_set_exit_callback(instance->view, subbrute_attack_view_exit);
 
+    instance->attack_type = SubBruteAttackTotalCount;
+    instance->max_value = 0x00;
+    instance->current_step = 0;
+    instance->is_attacking = false;
+
     return instance;
 }
 
@@ -178,6 +192,7 @@ void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_
 #ifdef FURI_DEBUG
     //FURI_LOG_D(TAG, "Set step: %d", current_step);
 #endif
+    instance->current_step = current_step;
     with_view_model(
         instance->view,
         SubBruteAttackViewModel * model,
@@ -185,15 +200,6 @@ void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_
         true);
 }
 
-void subbrute_attack_view_set_worker_type(SubBruteAttackView* instance, bool is_continuous_worker) {
-    furi_assert(instance);
-    with_view_model(
-        instance->view,
-        SubBruteAttackViewModel * model,
-        { model->is_continuous_worker = is_continuous_worker; },
-        true);
-}
-
 // We need to call init every time, because not every time we calls enter
 // normally, call enter only once
 void subbrute_attack_view_init_values(
@@ -206,17 +212,21 @@ void subbrute_attack_view_init_values(
 #ifdef FURI_DEBUG
     FURI_LOG_D(
         TAG,
-        "init, index: %d, max_value: %lld, current_step: %lld",
+        "init, attack_type: %d, max_value: %lld, current_step: %lld",
         index,
         max_value,
         current_step);
 #endif
+    instance->attack_type = index;
+    instance->max_value = max_value;
+    instance->current_step = current_step;
+    instance->is_attacking = is_attacking;
     with_view_model(
         instance->view,
         SubBruteAttackViewModel * model,
         {
             model->max_value = max_value;
-            model->index = index;
+            model->attack_type = index;
             model->current_step = current_step;
             model->is_attacking = is_attacking;
             model->extra_repeats = extra_repeats;
@@ -316,7 +326,7 @@ void subbrute_attack_view_draw(Canvas* canvas, void* context) {
     char buffer[26];
 
     const char* attack_name = NULL;
-    attack_name = subbrute_protocol_name(model->index);
+    attack_name = subbrute_protocol_name(model->attack_type);
     // Title
     if(model->is_attacking) {
         canvas_set_color(canvas, ColorBlack);

+ 0 - 1
views/subbrute_attack_view.h

@@ -16,7 +16,6 @@ SubBruteAttackView* subbrute_attack_view_alloc();
 void subbrute_attack_view_free(SubBruteAttackView* instance);
 View* subbrute_attack_view_get_view(SubBruteAttackView* instance);
 void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step);
-void subbrute_attack_view_set_worker_type(SubBruteAttackView* instance, bool is_continuous_worker);
 void subbrute_attack_view_init_values(
     SubBruteAttackView* instance,
     uint8_t index,

+ 108 - 121
views/subbrute_main_view.c

@@ -10,10 +10,17 @@
 #define STATUS_BAR_Y_SHIFT 14
 #define TAG "SubBruteMainView"
 
+#define ITEMS_ON_SCREEN 3
+
 struct SubBruteMainView {
     View* view;
     SubBruteMainViewCallback callback;
     void* context;
+    uint8_t index;
+    bool is_select_byte;
+    const char* key_field;
+    uint8_t repeats_count;
+    uint8_t window_position;
 };
 
 typedef struct {
@@ -22,6 +29,7 @@ typedef struct {
     uint8_t window_position;
     bool is_select_byte;
     const char* key_field;
+    uint8_t repeats_count;
 } SubBruteMainViewModel;
 
 void subbrute_main_view_set_callback(
@@ -81,6 +89,8 @@ FuriString* center_displayed_key(const char* key_cstr, uint8_t index) {
 }
 
 void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
+    SubBruteMainViewModel* m = model;
+
     // Title
     canvas_set_font(canvas, FontPrimary);
     canvas_draw_box(canvas, 0, 0, canvas_width(canvas), STATUS_BAR_Y_SHIFT);
@@ -88,17 +98,17 @@ void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
     canvas_draw_str_aligned(canvas, 64, 3, AlignCenter, AlignTop, "Sub-GHz BruteForcer 3.1");
     canvas_invert_color(canvas);
 
-    if(model->is_select_byte) {
+    if(m->is_select_byte) {
 #ifdef FURI_DEBUG
         //FURI_LOG_D(TAG, "key_field: %s", model->key_field);
 #endif
         char msg_index[18];
-        snprintf(msg_index, sizeof(msg_index), "Field index : %d", model->index);
+        snprintf(msg_index, sizeof(msg_index), "Field index : %d", m->index);
         canvas_draw_str_aligned(canvas, 64, 26, AlignCenter, AlignTop, msg_index);
 
         FuriString* menu_items;
 
-        menu_items = center_displayed_key(model->key_field, model->index);
+        menu_items = center_displayed_key(m->key_field, m->index);
         canvas_set_font(canvas, FontSecondary);
         canvas_draw_str_aligned(
             canvas, 64, 40, AlignCenter, AlignTop, furi_string_get_cstr(menu_items));
@@ -113,7 +123,6 @@ void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
         // Menu
         canvas_set_color(canvas, ColorBlack);
         canvas_set_font(canvas, FontSecondary);
-        uint8_t items_on_screen = 3;
         const uint8_t item_height = 16;
 
 #ifdef FURI_DEBUG
@@ -123,7 +132,7 @@ void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
             uint8_t item_position = position - model->window_position;
 
             if(item_position < items_on_screen) {
-                if(model->index == position) {
+                if(m->index == position) {
                     canvas_draw_str_aligned(
                         canvas,
                         4,
@@ -151,7 +160,7 @@ void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
             canvas_width(canvas),
             STATUS_BAR_Y_SHIFT + 2,
             canvas_height(canvas) - STATUS_BAR_Y_SHIFT,
-            model->index,
+            m->index,
             SubBruteAttackTotalCount);
     }
 }
@@ -171,107 +180,83 @@ bool subbrute_main_view_input(InputEvent* event, void* context) {
     const uint8_t min_value = 0;
     const uint8_t correct_total = SubBruteAttackTotalCount - 1;
     uint8_t index = 0;
-    bool is_select_byte = false;
-    with_view_model(
-        instance->view,
-        SubBruteMainViewModel * model,
-        { is_select_byte = model->is_select_byte; },
-        false);
 
+    bool updated = false;
     bool consumed = false;
-    if(!is_select_byte) {
-        if((event->type == InputTypeShort) || (event->type == InputTypeRepeat)) {
-            bool ret = false;
-            with_view_model(
-                instance->view,
-                SubBruteMainViewModel * model,
-                {
-                    uint8_t items_on_screen = 3;
-                    if(event->key == InputKeyUp) {
-                        if(model->index == min_value) {
-                            model->index = correct_total;
-                        } else {
-                            model->index = CLAMP(model->index - 1, correct_total, min_value);
-                        }
-                        ret = true;
-                        consumed = true;
-                    } else if(event->key == InputKeyDown) {
-                        if(model->index == correct_total) {
-                            model->index = min_value;
-                        } else {
-                            model->index = CLAMP(model->index + 1, correct_total, min_value);
-                        }
-                        ret = true;
-                        consumed = true;
-                    }
-                    if(ret) {
-                        model->window_position = model->index;
-                        if(model->window_position > 0) {
-                            model->window_position -= 1;
-                        }
-
-                        if(SubBruteAttackTotalCount <= items_on_screen) {
-                            model->window_position = 0;
-                        } else {
-                            if(model->window_position >=
-                               (SubBruteAttackTotalCount - items_on_screen)) {
-                                model->window_position =
-                                    (SubBruteAttackTotalCount - items_on_screen);
-                            }
-                        }
-                    }
-                    index = model->index;
-                },
-                ret);
-        }
-
-#ifdef FURI_DEBUG
-        with_view_model(
-            instance->view, SubBruteMainViewModel * model, { index = model->index; }, false);
-        FURI_LOG_I(TAG, "Index: %d", index);
-#endif
+    bool is_short = (event->type == InputTypeShort) || (event->type == InputTypeRepeat);
 
-        if(event->key == InputKeyOk && event->type == InputTypeShort) {
+    if(!instance->is_select_byte) {
+        if(event->key == InputKeyUp && is_short) {
+            if(instance->index == min_value) {
+                instance->index = correct_total;
+            } else {
+                instance->index = CLAMP(instance->index - 1, correct_total, min_value);
+            }
+            updated = true;
+            consumed = true;
+        } else if(event->key == InputKeyDown && is_short) {
+            if(instance->index == correct_total) {
+                instance->index = min_value;
+            } else {
+                instance->index = CLAMP(instance->index + 1, correct_total, min_value);
+            }
+            updated = true;
+            consumed = true;
+        } else if(event->key == InputKeyOk && is_short) {
             if(index == SubBruteAttackLoadFile) {
                 instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
             } else {
                 instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
             }
             consumed = true;
+            updated = true;
         }
-    } else {
-        if((event->type == InputTypeShort) || (event->type == InputTypeRepeat)) {
-            with_view_model(
-                instance->view,
-                SubBruteMainViewModel * model,
-                {
-                    if(event->key == InputKeyLeft) {
-                        if(model->index > 0) {
-                            model->index--;
-                        }
-                    } else if(event->key == InputKeyRight) {
-                        if(model->index < 7) {
-                            model->index++;
-                        }
-                    }
-
-                    index = model->index;
-                },
-                true);
-        }
-
-#ifdef FURI_DEBUG
-        with_view_model(
-            instance->view, SubBruteMainViewModel * model, { index = model->index; }, false);
-        FURI_LOG_I(TAG, "Index: %d", index);
-#endif
+        if(updated) {
+            instance->window_position = instance->index;
+            if(instance->window_position > 0) {
+                instance->window_position -= 1;
+            }
 
-        if(event->key == InputKeyOk && event->type == InputTypeShort) {
+            if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
+                instance->window_position = 0;
+            } else {
+                if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
+                    instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
+                }
+            }
+        }
+    } else {
+        if(event->key == InputKeyLeft && is_short) {
+            if(instance->index > 0) {
+                instance->index--;
+            }
+            updated = true;
+        } else if(event->key == InputKeyRight && is_short) {
+            if(instance->index < 7) {
+                instance->index++;
+            }
+            updated = true;
+        } else if(event->key == InputKeyOk && is_short) {
             instance->callback(SubBruteCustomEventTypeIndexSelected, instance->context);
             consumed = true;
+            updated = true;
         }
     }
 
+    if(updated) {
+        with_view_model(
+            instance->view,
+            SubBruteMainViewModel * model,
+            {
+                model->index = instance->index;
+                model->window_position = instance->window_position;
+                model->key_field = instance->key_field;
+                model->is_select_byte = instance->is_select_byte;
+                model->repeats_count = instance->repeats_count;
+            },
+            true);
+    }
+
     return consumed;
 }
 
@@ -320,9 +305,16 @@ SubBruteMainView* subbrute_main_view_alloc() {
             model->extra_repeats = 0;
             model->key_field = NULL;
             model->is_select_byte = false;
+            model->repeats_count = 0;
         },
         true);
 
+    instance->index = 0;
+    instance->window_position = 0;
+    instance->key_field = NULL;
+    instance->is_select_byte = false;
+    instance->repeats_count = 0;
+
     return instance;
 }
 
@@ -348,46 +340,41 @@ void subbrute_main_view_set_index(
 #ifdef FURI_DEBUG
     FURI_LOG_I(TAG, "Set index: %d", idx);
 #endif
+    instance->is_select_byte = is_select_byte;
+    instance->key_field = key_field;
+    instance->index = idx;
+    instance->window_position = idx;
+
+    if(!is_select_byte) {
+        if(instance->window_position > 0) {
+            instance->window_position -= 1;
+        }
+
+        if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
+            instance->window_position = 0;
+        } else {
+            if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
+                instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
+            }
+        }
+    }
+
     with_view_model(
         instance->view,
         SubBruteMainViewModel * model,
         {
-            model->is_select_byte = is_select_byte;
-            model->key_field = key_field;
-            model->index = idx;
-            model->window_position = idx;
-
-            if(!is_select_byte) {
-                uint8_t items_on_screen = 3;
-
-                if(model->window_position > 0) {
-                    model->window_position -= 1;
-                }
-
-                if(SubBruteAttackTotalCount <= items_on_screen) {
-                    model->window_position = 0;
-                } else {
-                    if(model->window_position >= (SubBruteAttackTotalCount - items_on_screen)) {
-                        model->window_position = (SubBruteAttackTotalCount - items_on_screen);
-                    }
-                }
-            }
+            model->index = instance->index;
+            model->window_position = instance->window_position;
+            model->key_field = instance->key_field;
+            model->is_select_byte = instance->is_select_byte;
+            model->repeats_count = instance->repeats_count;
         },
         true);
 }
 
 SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
     furi_assert(instance);
-
-    uint8_t idx = 0;
-    with_view_model(
-        instance->view, SubBruteMainViewModel * model, { idx = model->index; }, false);
-
-#ifdef FURI_DEBUG
-    FURI_LOG_D(TAG, "Get index: %d", idx);
-#endif
-
-    return idx;
+    return instance->index;
 }
 
 uint8_t subbrute_main_view_get_extra_repeats(SubBruteMainView* instance) {