Sfoglia il codice sorgente

Check for keys file

Eric Betts 8 mesi fa
parent
commit
a96ecf8d56

+ 42 - 0
scenes/weebo_keys_missing.c

@@ -0,0 +1,42 @@
+#include "../weebo_i.h"
+
+void weebo_scene_keys_missing_popup_callback(void* context) {
+    Weebo* weebo = context;
+    view_dispatcher_send_custom_event(weebo->view_dispatcher, WeeboCustomEventViewExit);
+}
+
+void weebo_scene_keys_missing_on_enter(void* context) {
+    Weebo* weebo = context;
+
+    Popup* popup = weebo->popup;
+
+    popup_set_header(popup, "key_retail.bin missing", 58, 28, AlignCenter, AlignCenter);
+    // popup_set_text(popup, "words", 64, 36, AlignCenter, AlignTop);
+    popup_set_context(weebo->popup, weebo);
+    popup_set_callback(popup, weebo_scene_keys_missing_popup_callback);
+    popup_set_timeout(popup, 5 * 60 * 1000);
+    popup_enable_timeout(weebo->popup);
+
+    view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewPopup);
+}
+
+bool weebo_scene_keys_missing_on_event(void* context, SceneManagerEvent event) {
+    Weebo* weebo = context;
+    bool consumed = false;
+
+    if(event.type == SceneManagerEventTypeCustom) {
+        scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneKeysMissing, event.event);
+        if(event.event == WeeboCustomEventViewExit) {
+            while(scene_manager_previous_scene(weebo->scene_manager))
+                ;
+            consumed = true;
+        }
+    }
+
+    return consumed;
+}
+
+void weebo_scene_keys_missing_on_exit(void* context) {
+    Weebo* weebo = context;
+    popup_reset(weebo->popup);
+}

+ 2 - 1
scenes/weebo_scene_config.h

@@ -1 +1,2 @@
-ADD_SCENE(weebo, start, Start)
+ADD_SCENE(weebo, main_menu, MainMenu)
+ADD_SCENE(weebo, keys_missing, KeysMissing)

+ 46 - 0
scenes/weebo_scene_main_menu.c

@@ -0,0 +1,46 @@
+#include "../weebo_i.h"
+
+#define TAG "SceneMainMenu"
+
+enum SubmenuIndex {
+    SubmenuIndexSelect = 0,
+};
+
+void weebo_scene_main_menu_submenu_callback(void* context, uint32_t index) {
+    Weebo* weebo = context;
+    view_dispatcher_send_custom_event(weebo->view_dispatcher, index);
+}
+
+void weebo_scene_main_menu_on_enter(void* context) {
+    Weebo* weebo = context;
+    Submenu* submenu = weebo->submenu;
+    submenu_reset(submenu);
+
+    submenu_add_item(
+        submenu, "Select", SubmenuIndexSelect, weebo_scene_main_menu_submenu_callback, weebo);
+
+    submenu_set_selected_item(
+        submenu, scene_manager_get_scene_state(weebo->scene_manager, WeeboSceneMainMenu));
+    view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewMenu);
+}
+
+bool weebo_scene_main_menu_on_event(void* context, SceneManagerEvent event) {
+    Weebo* weebo = context;
+    bool consumed = false;
+
+    if(event.type == SceneManagerEventTypeCustom) {
+        scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneMainMenu, event.event);
+        if(event.event == SubmenuIndexSelect) {
+            // scene_manager_next_scene(weebo->scene_manager, WeeboScenePassportNumberInput);
+            consumed = true;
+        }
+    }
+
+    return consumed;
+}
+
+void weebo_scene_main_menu_on_exit(void* context) {
+    Weebo* weebo = context;
+
+    submenu_reset(weebo->submenu);
+}

+ 0 - 58
scenes/weebo_scene_start.c

@@ -1,58 +0,0 @@
-#include "../weebo_i.h"
-enum SubmenuIndex {
-    SubmenuIndexSamPresent,
-    SubmenuIndexSamMissing,
-};
-
-static void weebo_scene_start_detect_callback(void* context) {
-    Weebo* weebo = context;
-    UNUSED(weebo);
-    // view_dispatcher_send_custom_event(weebo->view_dispatcher, WeeboWorkerEventSamMissing);
-}
-
-void weebo_scene_start_submenu_callback(void* context, uint32_t index) {
-    Weebo* weebo = context;
-    view_dispatcher_send_custom_event(weebo->view_dispatcher, index);
-}
-
-void weebo_scene_start_on_enter(void* context) {
-    Weebo* weebo = context;
-
-    Popup* popup = weebo->popup;
-
-    popup_set_context(weebo->popup, weebo);
-    popup_set_callback(weebo->popup, weebo_scene_start_detect_callback);
-    popup_set_header(popup, "Detecting SAM", 58, 48, AlignCenter, AlignCenter);
-    popup_set_timeout(weebo->popup, 2500);
-    popup_enable_timeout(weebo->popup);
-
-    view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewPopup);
-}
-
-bool weebo_scene_start_on_event(void* context, SceneManagerEvent event) {
-    Weebo* weebo = context;
-    bool consumed = false;
-
-    if(event.type == SceneManagerEventTypeCustom) {
-        scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneStart, event.event);
-        /*
-        if(event.event == WeeboWorkerEventSamPresent) {
-            scene_manager_next_scene(weebo->scene_manager, WeeboSceneSamPresent);
-            consumed = true;
-        } else if(event.event == WeeboWorkerEventSamMissing) {
-            scene_manager_next_scene(weebo->scene_manager, WeeboSceneSamMissing);
-            consumed = true;
-        } else if(event.event == WeeboWorkerEventSamWrong) {
-            scene_manager_next_scene(weebo->scene_manager, WeeboSceneSamWrong);
-            consumed = true;
-        }
-        */
-    }
-
-    return consumed;
-}
-
-void weebo_scene_start_on_exit(void* context) {
-    Weebo* weebo = context;
-    popup_reset(weebo->popup);
-}

+ 46 - 1
weebo.c

@@ -15,6 +15,44 @@ void calculate_pwd(uint8_t* uid, uint8_t* pwd) {
     pwd[3] = uid[4] ^ uid[6] ^ 0x55;
 }
 
+bool weebo_load_key_retail(Weebo* weebo) {
+    FuriString* path = furi_string_alloc();
+    bool parsed = false;
+    uint8_t buffer[160];
+    memset(buffer, 0, sizeof(buffer));
+    Storage* storage = furi_record_open(RECORD_STORAGE);
+    Stream* stream = file_stream_alloc(storage);
+
+    do {
+        furi_string_printf(
+            path, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, WEEBO_KEY_RETAIL_FILENAME, ".bin");
+
+        bool opened =
+            file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING);
+        if(!opened) {
+            FURI_LOG_E(TAG, "Failed to open file");
+            break;
+        }
+
+        size_t bytes_read = stream_read(stream, buffer, sizeof(buffer));
+        if(bytes_read != sizeof(buffer)) {
+            FURI_LOG_E(TAG, "Insufficient data");
+            break;
+        }
+
+        memcpy(&weebo->amiiboKeys, buffer, bytes_read);
+
+        // TODO: compare SHA1
+        parsed = true;
+    } while(false);
+
+    file_stream_close(stream);
+    furi_record_close(RECORD_STORAGE);
+    furi_string_free(path);
+
+    return parsed;
+}
+
 bool weebo_custom_event_callback(void* context, uint32_t event) {
     furi_assert(context);
     Weebo* weebo = context;
@@ -98,6 +136,8 @@ Weebo* weebo_alloc() {
     weebo->dialogs = furi_record_open(RECORD_DIALOGS);
     weebo->load_path = furi_string_alloc();
 
+    weebo->keys_loaded = false;
+
     return weebo;
 }
 
@@ -209,7 +249,12 @@ int32_t weebo_app(void* p) {
     UNUSED(p);
     Weebo* weebo = weebo_alloc();
 
-    scene_manager_next_scene(weebo->scene_manager, WeeboSceneStart);
+    weebo->keys_loaded = weebo_load_key_retail(weebo);
+    if(weebo->keys_loaded) {
+        scene_manager_next_scene(weebo->scene_manager, WeeboSceneMainMenu);
+    } else {
+        scene_manager_next_scene(weebo->scene_manager, WeeboSceneKeysMissing);
+    }
 
     view_dispatcher_run(weebo->view_dispatcher);
 

+ 4 - 0
weebo_i.h

@@ -24,6 +24,9 @@
 #include <nfc/nfc_poller.h>
 #include <nfc/nfc_device.h>
 
+#include <lib/toolbox/stream/stream.h>
+#include <lib/toolbox/stream/file_stream.h>
+
 /* generated by fbt from .png files in images folder */
 #include <weebo_icons.h>
 
@@ -75,6 +78,7 @@ struct Weebo {
     FuriString* load_path;
     char file_name[WEEBO_FILE_NAME_MAX_LENGTH + 1];
 
+    bool keys_loaded;
     nfc3d_amiibo_keys amiiboKeys;
 };