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

Merge pull request #16 from bettse/plugin

Wiegand parsing using a plugin
Eric Betts 1 год назад
Родитель
Сommit
123141a58f
9 измененных файлов с 127 добавлено и 0 удалено
  1. 2 0
      .github/workflows/main.yml
  2. 3 0
      .gitmodules
  3. 9 0
      application.fam
  4. 1 0
      plugin
  5. 19 0
      scenes/seader_scene_card_menu.c
  6. 1 0
      scenes/seader_scene_config.h
  7. 47 0
      scenes/seader_scene_formats.c
  8. 34 0
      seader.c
  9. 11 0
      seader_i.h

+ 2 - 0
.github/workflows/main.yml

@@ -7,6 +7,8 @@ jobs:
     steps:
       - name: Checkout
         uses: actions/checkout@v3
+        with:
+          submodules: 'true'
       - name: Build with ufbt
         uses: flipperdevices/flipperzero-ufbt-action@v0.1.2
         id: build-app

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "plugin"]
+	path = plugin
+	url = https://gitlab.com/bettse/flipper-wiegand-plugin.git

+ 9 - 0
application.fam

@@ -14,6 +14,7 @@ App(
     sources=[
       "*.c",
       "aeabi_uldivmod.sx",
+      "!plugin/*.c",
     ],
     fap_icon="icons/logo.png",
     fap_category="NFC",
@@ -39,3 +40,11 @@ App(
     fap_weburl="https://seader.ericbetts.dev",
     fap_icon_assets="icons",
 )
+
+App(
+    appid="plugin_wiegand",
+    apptype=FlipperAppType.PLUGIN,
+    entry_point="plugin_wiegand_ep",
+    requires=["seader"],
+    sources=["plugin/wiegand.c"],
+)

+ 1 - 0
plugin

@@ -0,0 +1 @@
+Subproject commit 4353973354f4e616b7e4b2ca4bc8d4d4eb0769f3

+ 19 - 0
scenes/seader_scene_card_menu.c

@@ -1,6 +1,7 @@
 #include "../seader_i.h"
 
 enum SubmenuIndex {
+    SubmenuIndexParse,
     SubmenuIndexSave,
     SubmenuIndexSavePicopass,
     SubmenuIndexSaveRFID,
@@ -17,8 +18,21 @@ void seader_scene_card_menu_submenu_callback(void* context, uint32_t index) {
 void seader_scene_card_menu_on_enter(void* context) {
     Seader* seader = context;
     SeaderCredential* credential = seader->credential;
+    PluginWiegand* plugin = seader->plugin_wiegand;
     Submenu* submenu = seader->submenu;
 
+    if(plugin) {
+        size_t format_count = plugin->count(credential->bit_length, credential->credential);
+        if(format_count > 0) {
+            submenu_add_item(
+                submenu,
+                "Parse",
+                SubmenuIndexParse,
+                seader_scene_card_menu_submenu_callback,
+                seader);
+        }
+    }
+
     submenu_add_item(
         submenu, "Save", SubmenuIndexSave, seader_scene_card_menu_submenu_callback, seader);
     submenu_add_item(
@@ -85,6 +99,11 @@ bool seader_scene_card_menu_on_event(void* context, SceneManagerEvent event) {
             seader->credential->save_format = SeaderCredentialSaveFormatMFC;
             scene_manager_next_scene(seader->scene_manager, SeaderSceneSaveName);
             consumed = true;
+        } else if(event.event == SubmenuIndexParse) {
+            scene_manager_set_scene_state(
+                seader->scene_manager, SeaderSceneCardMenu, SubmenuIndexParse);
+            scene_manager_next_scene(seader->scene_manager, SeaderSceneFormats);
+            consumed = true;
         }
     } else if(event.type == SceneManagerEventTypeBack) {
         consumed = scene_manager_search_and_switch_to_previous_scene(

+ 1 - 0
scenes/seader_scene_config.h

@@ -15,3 +15,4 @@ ADD_SCENE(seader, delete_success, DeleteSuccess)
 ADD_SCENE(seader, credential_info, CredentialInfo)
 ADD_SCENE(seader, sam_info, SamInfo)
 ADD_SCENE(seader, virtual_credential, VirtualCredential)
+ADD_SCENE(seader, formats, Formats)

+ 47 - 0
scenes/seader_scene_formats.c

@@ -0,0 +1,47 @@
+#include "../seader_i.h"
+#include <dolphin/dolphin.h>
+
+void seader_scene_formats_on_enter(void* context) {
+    Seader* seader = context;
+    PluginWiegand* plugin = seader->plugin_wiegand;
+    SeaderCredential* credential = seader->credential;
+
+    FuriString* str = seader->text_box_store;
+    furi_string_reset(str);
+
+    if(plugin) {
+        FuriString* description = furi_string_alloc();
+        size_t format_count = plugin->count(credential->bit_length, credential->credential);
+        for(size_t i = 0; i < format_count; i++) {
+            plugin->description(credential->bit_length, credential->credential, i, description);
+
+            furi_string_cat_printf(str, "%s\n", furi_string_get_cstr(description));
+        }
+        furi_string_free(description);
+    }
+
+    text_box_set_font(seader->text_box, TextBoxFontHex);
+    text_box_set_text(seader->text_box, furi_string_get_cstr(seader->text_box_store));
+    view_dispatcher_switch_to_view(seader->view_dispatcher, SeaderViewTextBox);
+}
+
+bool seader_scene_formats_on_event(void* context, SceneManagerEvent event) {
+    Seader* seader = context;
+    bool consumed = false;
+
+    if(event.type == SceneManagerEventTypeCustom) {
+        if(event.event == GuiButtonTypeLeft) {
+            consumed = scene_manager_previous_scene(seader->scene_manager);
+        }
+    } else if(event.type == SceneManagerEventTypeBack) {
+        consumed = scene_manager_previous_scene(seader->scene_manager);
+    }
+    return consumed;
+}
+
+void seader_scene_formats_on_exit(void* context) {
+    Seader* seader = context;
+
+    // Clear views
+    text_box_reset(seader->text_box);
+}

+ 34 - 0
seader.c

@@ -80,11 +80,38 @@ Seader* seader_alloc() {
     view_dispatcher_add_view(
         seader->view_dispatcher, SeaderViewTextInput, text_input_get_view(seader->text_input));
 
+    // TextBox
+    seader->text_box = text_box_alloc();
+    view_dispatcher_add_view(
+        seader->view_dispatcher, SeaderViewTextBox, text_box_get_view(seader->text_box));
+    seader->text_box_store = furi_string_alloc();
+
     // Custom Widget
     seader->widget = widget_alloc();
     view_dispatcher_add_view(
         seader->view_dispatcher, SeaderViewWidget, widget_get_view(seader->widget));
 
+    seader->plugin_manager =
+        plugin_manager_alloc(PLUGIN_APP_ID, PLUGIN_API_VERSION, firmware_api_interface);
+
+    seader->plugin_wiegand = NULL;
+    if(plugin_manager_load_all(seader->plugin_manager, APP_DATA_PATH("plugins")) !=
+       PluginManagerErrorNone) {
+        FURI_LOG_E(TAG, "Failed to load all libs");
+    } else {
+        uint32_t plugin_count = plugin_manager_get_count(seader->plugin_manager);
+        FURI_LOG_I(TAG, "Loaded %lu plugin(s)", plugin_count);
+
+        for(uint32_t i = 0; i < plugin_count; i++) {
+            const PluginWiegand* plugin = plugin_manager_get_ep(seader->plugin_manager, i);
+            FURI_LOG_I(TAG, "plugin name: %s", plugin->name);
+            if(strcmp(plugin->name, "Plugin Wiegand") == 0) {
+                // Have to cast to drop "const" qualifier
+                seader->plugin_wiegand = (PluginWiegand*)plugin;
+            }
+        }
+    }
+
     return seader;
 }
 
@@ -122,6 +149,11 @@ void seader_free(Seader* seader) {
     view_dispatcher_remove_view(seader->view_dispatcher, SeaderViewTextInput);
     text_input_free(seader->text_input);
 
+    // TextBox
+    view_dispatcher_remove_view(seader->view_dispatcher, SeaderViewTextBox);
+    text_box_free(seader->text_box);
+    furi_string_free(seader->text_box_store);
+
     // Custom Widget
     view_dispatcher_remove_view(seader->view_dispatcher, SeaderViewWidget);
     widget_free(seader->widget);
@@ -144,6 +176,8 @@ void seader_free(Seader* seader) {
     furi_record_close(RECORD_NOTIFICATION);
     seader->notifications = NULL;
 
+    plugin_manager_free(seader->plugin_manager);
+
     free(seader);
 }
 

+ 11 - 0
seader_i.h

@@ -18,6 +18,7 @@
 #include <gui/modules/popup.h>
 #include <gui/modules/loading.h>
 #include <gui/modules/text_input.h>
+#include <gui/modules/text_box.h>
 #include <gui/modules/widget.h>
 
 #include <input/input.h>
@@ -40,6 +41,11 @@
 #include <Payload.h>
 #include <FrameProtocol.h>
 
+#include "plugin/interface.h"
+#include <flipper_application/flipper_application.h>
+#include <flipper_application/plugins/plugin_manager.h>
+#include <loader/firmware_api/firmware_api.h>
+
 #include "protocol/picopass_poller.h"
 #include "scenes/seader_scene.h"
 
@@ -103,6 +109,7 @@ struct Seader {
     Popup* popup;
     Loading* loading;
     TextInput* text_input;
+    TextBox* text_box;
     Widget* widget;
 
     Nfc* nfc;
@@ -110,6 +117,9 @@ struct Seader {
     PicopassPoller* picopass_poller;
 
     NfcDevice* nfc_device;
+
+    PluginManager* plugin_manager;
+    PluginWiegand* plugin_wiegand;
 };
 
 struct SeaderPollerContainer {
@@ -122,6 +132,7 @@ typedef enum {
     SeaderViewPopup,
     SeaderViewLoading,
     SeaderViewTextInput,
+    SeaderViewTextBox,
     SeaderViewWidget,
     SeaderViewUart,
 } SeaderView;