frux-c 2 лет назад
Родитель
Сommit
59392b764b
8 измененных файлов с 354 добавлено и 0 удалено
  1. 30 0
      scenes/uhf_scene.c
  2. 29 0
      scenes/uhf_scene.h
  3. 16 0
      scenes/uhf_scene_config.h
  4. 57 0
      scenes/uhf_scene_start.c
  5. 124 0
      uhf_app.c
  6. 3 0
      uhf_app.h
  7. 93 0
      uhf_app_i.h
  8. 2 0
      uhf_data_i.h

+ 30 - 0
scenes/uhf_scene.c

@@ -0,0 +1,30 @@
+#include "uhf_scene.h"
+
+// Generate scene on_enter handlers array
+#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
+void (*const uhf_on_enter_handlers[])(void*) = {
+#include "uhf_scene_config.h"
+};
+#undef ADD_SCENE
+
+// Generate scene on_event handlers array
+#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
+bool (*const uhf_on_event_handlers[])(void* context, SceneManagerEvent event) = {
+#include "uhf_scene_config.h"
+};
+#undef ADD_SCENE
+
+// Generate scene on_exit handlers array
+#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
+void (*const uhf_on_exit_handlers[])(void* context) = {
+#include "uhf_scene_config.h"
+};
+#undef ADD_SCENE
+
+// Initialize scene handlers configuration structure
+const SceneManagerHandlers uhf_scene_handlers = {
+    .on_enter_handlers = uhf_on_enter_handlers,
+    .on_event_handlers = uhf_on_event_handlers,
+    .on_exit_handlers = uhf_on_exit_handlers,
+    .scene_num = UHFSceneNum,
+};

+ 29 - 0
scenes/uhf_scene.h

@@ -0,0 +1,29 @@
+#pragma once
+
+#include <gui/scene_manager.h>
+
+// Generate scene id and total number
+#define ADD_SCENE(prefix, name, id) UHFScene##id,
+typedef enum {
+#include "uhf_scene_config.h"
+    UHFSceneNum,
+} UHFScene;
+#undef ADD_SCENE
+
+extern const SceneManagerHandlers uhf_scene_handlers;
+
+// Generate scene on_enter handlers declaration
+#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
+#include "uhf_scene_config.h"
+#undef ADD_SCENE
+
+// Generate scene on_event handlers declaration
+#define ADD_SCENE(prefix, name, id) \
+    bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
+#include "uhf_scene_config.h"
+#undef ADD_SCENE
+
+// Generate scene on_exit handlers declaration
+#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
+#include "uhf_scene_config.h"
+#undef ADD_SCENE

+ 16 - 0
scenes/uhf_scene_config.h

@@ -0,0 +1,16 @@
+ADD_SCENE(uhf, start, Start)
+// ADD_SCENE(uhf, read_card, ReadCard)
+// ADD_SCENE(uhf, read_card_success, ReadCardSuccess)
+// ADD_SCENE(uhf, card_menu, CardMenu)
+// ADD_SCENE(uhf, save_name, SaveName)
+// ADD_SCENE(uhf, save_success, SaveSuccess)
+// ADD_SCENE(uhf, saved_menu, SavedMenu)
+// ADD_SCENE(uhf, file_select, FileSelect)
+// ADD_SCENE(uhf, device_info, DeviceInfo)
+// ADD_SCENE(uhf, delete, Delete)
+// ADD_SCENE(uhf, delete_success, DeleteSuccess)
+// ADD_SCENE(uhf, write_card, WriteCard)
+// ADD_SCENE(uhf, write_card_success, WriteCardSuccess)
+// ADD_SCENE(uhf, read_factory_success, ReadFactorySuccess)
+// ADD_SCENE(uhf, write_key, WriteKey)
+// ADD_SCENE(uhf, key_menu, KeyMenu)

+ 57 - 0
scenes/uhf_scene_start.c

@@ -0,0 +1,57 @@
+#include "../uhf_app_i.h"
+
+enum SubmenuIndex { SubmenuIndexRead, SubmenuIndexSaved, SubmenuIndexSettings };
+
+void uhf_scene_start_submenu_callback(void* context, uint32_t index) {
+    UHFApp* uhf_app = context;
+    view_dispatcher_send_custom_event(uhf_app->view_dispatcher, index);
+}
+void uhf_scene_start_on_enter(void* context) {
+    UHFApp* uhf_app = context;
+
+    Submenu* submenu = uhf_app->submenu;
+    submenu_add_item(
+        submenu, "Read Tag", SubmenuIndexRead, uhf_scene_start_submenu_callback, uhf_app);
+    submenu_add_item(
+        submenu, "Saved", SubmenuIndexSaved, uhf_scene_start_submenu_callback, uhf_app);
+    submenu_add_item(
+        submenu, "Settings", SubmenuIndexSettings, uhf_scene_start_submenu_callback, uhf_app);
+
+    submenu_set_selected_item(
+        submenu, scene_manager_get_scene_state(uhf_app->scene_manager, UHFSceneStart));
+    view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewMenu);
+}
+
+bool uhf_scene_start_on_event(void* context, SceneManagerEvent event) {
+    UNUSED(context);
+    // UHFApp* uhf_app = context;
+    bool consumed = false;
+
+    if(event.type == SceneManagerEventTypeCustom) {
+        FURI_LOG_E("EVENT", "%lu", event.event);
+        // if(event.event == SubmenuIndexRead) {
+        //     scene_manager_set_scene_state(uhf_app->scene_manager, UHFSceneStart, SubmenuIndexRead);
+        //     scene_manager_next_scene(uhf_app->scene_manager, UHFSceneReadCard);
+        //     consumed = true;
+        // } else if(event.event == SubmenuIndexSaved) {
+        // Explicitly save state so that the correct item is
+        // reselected if the user cancels loading a file.
+        // scene_manager_set_scene_state(
+        //     uhf_app->scene_manager, UHFSceneStart, SubmenuIndexSaved);
+        // scene_manager_next_scene(uhf_app->scene_manager, UHFSceneFileSelect);
+        // consumed = true;
+        // } else if(event.event == SubmenuIndexEliteDictAttack) {
+        //     scene_manager_set_scene_state(
+        //         uhf_app->scene_manager, UHFSceneStart, SubmenuIndexEliteDictAttack);
+        //     scene_manager_next_scene(uhf_app->scene_manager, UHFSceneEliteDictAttack);
+        //     consumed = true;
+        // }
+        // consumed = true;
+    }
+    return consumed;
+}
+
+void uhf_scene_start_on_exit(void* context) {
+    UHFApp* uhf_app = context;
+    submenu_reset(uhf_app->submenu);
+}

+ 124 - 0
uhf_app.c

@@ -0,0 +1,124 @@
+#include "uhf_app_i.h"
+
+bool uhf_custom_event_callback(void* context, uint32_t event) {
+    furi_assert(context);
+    UHFApp* uhf_app = context;
+    return scene_manager_handle_custom_event(uhf_app->scene_manager, event);
+}
+
+bool uhf_back_event_callback(void* context) {
+    furi_assert(context);
+    UHFApp* uhf_app = context;
+    return scene_manager_handle_back_event(uhf_app->scene_manager);
+}
+
+void uhf_tick_event_callback(void* context) {
+    furi_assert(context);
+    UHFApp* uhf_app = context;
+    scene_manager_handle_tick_event(uhf_app->scene_manager);
+}
+
+UHFApp* uhf_app_alloc() {
+    UHFApp* uhf_app = (UHFApp*)malloc(sizeof(UHFApp));
+    uhf_app->worker = (UHFWorker*)uhf_worker_alloc();
+    uhf_app->view_dispatcher = view_dispatcher_alloc();
+    uhf_app->scene_manager = scene_manager_alloc(&uhf_scene_handlers, uhf_app);
+    view_dispatcher_enable_queue(uhf_app->view_dispatcher);
+    view_dispatcher_set_event_callback_context(uhf_app->view_dispatcher, uhf_app);
+    view_dispatcher_set_custom_event_callback(uhf_app->view_dispatcher, uhf_custom_event_callback);
+    view_dispatcher_set_navigation_event_callback(
+        uhf_app->view_dispatcher, uhf_back_event_callback);
+    view_dispatcher_set_tick_event_callback(
+        uhf_app->view_dispatcher, uhf_tick_event_callback, 100);
+
+    // Open GUI record
+    uhf_app->gui = furi_record_open(RECORD_GUI);
+    view_dispatcher_attach_to_gui(
+        uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
+
+    // Open Notification record
+    uhf_app->notifications = furi_record_open(RECORD_NOTIFICATION);
+
+    // Submenu
+    uhf_app->submenu = submenu_alloc();
+    view_dispatcher_add_view(
+        uhf_app->view_dispatcher, UHFViewMenu, submenu_get_view(uhf_app->submenu));
+
+    // Popup
+    uhf_app->popup = popup_alloc();
+    view_dispatcher_add_view(
+        uhf_app->view_dispatcher, UHFViewPopup, popup_get_view(uhf_app->popup));
+
+    // Loading
+    uhf_app->loading = loading_alloc();
+    view_dispatcher_add_view(
+        uhf_app->view_dispatcher, UHFViewLoading, loading_get_view(uhf_app->loading));
+
+    // Text Input
+    uhf_app->text_input = text_input_alloc();
+    view_dispatcher_add_view(
+        uhf_app->view_dispatcher, UHFViewTextInput, text_input_get_view(uhf_app->text_input));
+
+    // Custom Widget
+    uhf_app->widget = widget_alloc();
+    view_dispatcher_add_view(
+        uhf_app->view_dispatcher, UHFViewWidget, widget_get_view(uhf_app->widget));
+
+    return uhf_app;
+}
+
+void uhf_app_free(UHFApp* uhf_app) {
+    furi_assert(uhf_app);
+
+    // Submenu
+    view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
+    submenu_free(uhf_app->submenu);
+
+    // Popup
+    view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
+    popup_free(uhf_app->popup);
+
+    // Loading
+    view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
+    loading_free(uhf_app->loading);
+
+    // TextInput
+    view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
+    text_input_free(uhf_app->text_input);
+
+    // Custom Widget
+    view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
+    widget_free(uhf_app->widget);
+
+    // Worker
+    uhf_worker_stop(uhf_app->worker);
+    uhf_worker_free(uhf_app->worker);
+
+    // View Dispatcher
+    view_dispatcher_free(uhf_app->view_dispatcher);
+
+    // Scene Manager
+    scene_manager_free(uhf_app->scene_manager);
+
+    // GUI
+    furi_record_close(RECORD_GUI);
+    uhf_app->gui = NULL;
+
+    // Notifications
+    furi_record_close(RECORD_NOTIFICATION);
+    uhf_app->notifications = NULL;
+
+    free(uhf_app);
+}
+
+int32_t uhf_app_main(void* ctx) {
+    UNUSED(ctx);
+    UHFApp* uhf_app = uhf_app_alloc();
+    view_dispatcher_attach_to_gui(
+        uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
+    scene_manager_next_scene(uhf_app->scene_manager, UHFSceneStart);
+    view_dispatcher_run(uhf_app->view_dispatcher);
+
+    uhf_app_free(uhf_app);
+    return 0;
+}

+ 3 - 0
uhf_app.h

@@ -0,0 +1,3 @@
+#pragma once
+
+typedef struct UHFApp UHFApp;

+ 93 - 0
uhf_app_i.h

@@ -0,0 +1,93 @@
+#pragma once
+
+#include "uhf_app.h"
+#include "uhf_worker.h"
+
+#include <furi.h>
+#include <gui/gui.h>
+#include <gui/view_dispatcher.h>
+#include <gui/scene_manager.h>
+#include <notification/notification_messages.h>
+
+#include <gui/modules/submenu.h>
+#include <gui/modules/popup.h>
+#include <gui/modules/loading.h>
+#include <gui/modules/text_input.h>
+#include <gui/modules/widget.h>
+
+#include <input/input.h>
+
+#include "scenes/uhf_scene.h"
+
+#include <storage/storage.h>
+#include <lib/toolbox/path.h>
+// #include <uhf_icons.h>
+
+#define UHF_TEXT_STORE_SIZE 128
+
+enum UHFCustomEvent {
+    // Reserve first 100 events for button types and indexes, starting from 0
+    UHFCustomEventReserved = 100,
+
+    UHFCustomEventViewExit,
+    UHFCustomEventWorkerExit,
+    UHFCustomEventByteInputDone,
+    UHFCustomEventTextInputDone,
+    UHFCustomEventDictAttackSkip,
+};
+
+typedef enum {
+    EventTypeTick,
+    EventTypeKey,
+} EventType;
+
+struct UHFApp {
+    UHFWorker* worker;
+    ViewDispatcher* view_dispatcher;
+    Gui* gui;
+    NotificationApp* notifications;
+    SceneManager* scene_manager;
+    // UHFDevice* dev;
+
+    char text_store[UHF_TEXT_STORE_SIZE + 1];
+    FuriString* text_box_store;
+
+    // Common Views
+    Submenu* submenu;
+    Popup* popup;
+    Loading* loading;
+    TextInput* text_input;
+    Widget* widget;
+};
+
+typedef enum {
+    UHFViewMenu,
+    UHFViewPopup,
+    UHFViewLoading,
+    UHFViewTextInput,
+    UHFViewWidget,
+} UHFView;
+
+UHFApp* uhf_app_alloc();
+
+void uhf_text_store_set(UHFApp* uhf, const char* text, ...);
+
+void uhf_text_store_clear(UHFApp* uhf);
+
+void uhf_blink_start(UHFApp* uhf);
+
+void uhf_blink_stop(UHFApp* uhf);
+
+void uhf_show_loading_popup(void* context, bool show);
+
+/** Check if memory is set to pattern
+ *
+ * @warning    zero size will return false
+ *
+ * @param[in]  data     Pointer to the byte array
+ * @param[in]  pattern  The pattern
+ * @param[in]  size     The byte array size
+ *
+ * @return     True if memory is set to pattern, false otherwise
+ */
+bool uhf_is_memset(const uint8_t* data, const uint8_t pattern, size_t size);

+ 2 - 0
uhf_data_i.h

@@ -0,0 +1,2 @@
+// todo : probably will move some of the uhf_data functions to internal only
+// once i figure out how to structure the method calls