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

File format overhaul, config backend

- Explicitly list each track in file format, rather than intuiting based on start/end sentinels of string
- Bump file format version
- Temp. add manually edits for testing with new file format
- Emulate config backend

Next steps: have refactored mag_helper emulation funcs use config values rather than hardcoded ones
Zachary Weiss 3 лет назад
Родитель
Сommit
d0faf3519e
9 измененных файлов с 145 добавлено и 59 удалено
  1. 17 0
      helpers/mag_types.h
  2. 29 0
      mag.c
  3. 29 10
      mag_device.c
  4. 14 5
      mag_device.h
  5. 12 4
      mag_i.h
  6. 3 2
      scenes/mag_scene_emulate.c
  7. 38 35
      scenes/mag_scene_emulate_config.c
  8. 2 2
      scenes/mag_scene_input_value.c
  9. 1 1
      scenes/mag_scene_saved_info.c

+ 17 - 0
helpers/mag_types.h

@@ -0,0 +1,17 @@
+#pragma once
+
+typedef enum {
+    MagReverseStateOn,
+    MagReverseStateOff,
+} MagReverseState;
+
+typedef enum {
+    MagTrackStateAll,
+    MagTrackStateOne,
+    MagTrackStateTwo,
+} MagTrackState;
+
+typedef enum {
+    MagTxStateRFID,
+    MagTxStateGPIOA6A7,
+} MagTxState;

+ 29 - 0
mag.c

@@ -2,6 +2,12 @@
 
 #define TAG "Mag"
 
+#define SETTING_DEFAULT_REVERSE MagReverseStateOff
+#define SETTING_DEFAULT_TRACK MagTrackStateAll
+#define SETTING_DEFAULT_TX_RFID MagTxStateRFID
+#define SETTING_DEFAULT_US_CLOCK 240
+#define SETTING_DEFAULT_US_INTERPACKET 10
+
 static bool mag_debug_custom_event_callback(void* context, uint32_t event) {
     furi_assert(context);
     Mag* mag = context;
@@ -14,6 +20,18 @@ static bool mag_debug_back_event_callback(void* context) {
     return scene_manager_handle_back_event(mag->scene_manager);
 }
 
+static MagSetting* mag_setting_alloc() {
+    // temp hardcoded defaults
+    MagSetting* setting = malloc(sizeof(MagSetting));
+    setting->reverse = SETTING_DEFAULT_REVERSE;
+    setting->track = SETTING_DEFAULT_TRACK;
+    setting->tx = SETTING_DEFAULT_TX_RFID;
+    setting->us_clock = SETTING_DEFAULT_US_CLOCK;
+    setting->us_interpacket = SETTING_DEFAULT_US_INTERPACKET;
+
+    return setting;
+}
+
 static Mag* mag_alloc() {
     Mag* mag = malloc(sizeof(Mag));
 
@@ -33,6 +51,7 @@ static Mag* mag_alloc() {
         mag->view_dispatcher, mag_debug_back_event_callback);
 
     mag->mag_dev = mag_device_alloc();
+    mag->setting = mag_setting_alloc();
 
     // Open GUI record
     mag->gui = furi_record_open(RECORD_GUI);
@@ -81,6 +100,12 @@ static Mag* mag_alloc() {
     return mag;
 }
 
+static void mag_setting_free(MagSetting* setting) {
+    furi_assert(setting);
+
+    free(setting);
+}
+
 static void mag_free(Mag* mag) {
     furi_assert(mag);
 
@@ -91,6 +116,10 @@ static void mag_free(Mag* mag) {
     mag_device_free(mag->mag_dev);
     mag->mag_dev = NULL;
 
+    // Mag setting
+    mag_setting_free(mag->setting);
+    mag->setting = NULL;
+
     // Submenu
     view_dispatcher_remove_view(mag->view_dispatcher, MagViewSubmenu);
     submenu_free(mag->submenu);

+ 29 - 10
mag_device.c

@@ -6,25 +6,29 @@
 #define TAG "MagDevice"
 
 static const char* mag_file_header = "Flipper Mag device";
-static const uint32_t mag_file_version = 0;
+static const uint32_t mag_file_version = 1;
 
 MagDevice* mag_device_alloc() {
     MagDevice* mag_dev = malloc(sizeof(MagDevice));
-    mag_dev->dev_data = furi_string_alloc();
+    mag_dev->dev_data.track[0].str = furi_string_alloc();
+    mag_dev->dev_data.track[1].str = furi_string_alloc();
+    mag_dev->dev_data.track[2].str = furi_string_alloc();
     mag_dev->storage = furi_record_open(RECORD_STORAGE);
     mag_dev->dialogs = furi_record_open(RECORD_DIALOGS);
     mag_dev->load_path = furi_string_alloc();
     return mag_dev;
 }
 
-void mag_device_data_clear(FuriString* dev_data) {
-    furi_string_reset(dev_data);
+void mag_device_data_clear(MagDeviceData* dev_data) {
+    furi_string_reset(dev_data->track[0].str);
+    furi_string_reset(dev_data->track[1].str);
+    furi_string_reset(dev_data->track[2].str);
 }
 
 void mag_device_clear(MagDevice* mag_dev) {
     furi_assert(mag_dev);
 
-    mag_device_data_clear(mag_dev->dev_data);
+    mag_device_data_clear(&mag_dev->dev_data);
     memset(&mag_dev->dev_data, 0, sizeof(mag_dev->dev_data));
     furi_string_reset(mag_dev->load_path);
 }
@@ -36,6 +40,11 @@ void mag_device_free(MagDevice* mag_dev) {
     furi_record_close(RECORD_STORAGE);
     furi_record_close(RECORD_DIALOGS);
     furi_string_free(mag_dev->load_path);
+
+    //furi_string_free(mag_dev->dev_data.track[0].str);
+    //furi_string_free(mag_dev->dev_data.track[1].str);
+    //furi_string_free(mag_dev->dev_data.track[2].str);
+
     free(mag_dev);
 }
 
@@ -82,8 +91,14 @@ static bool mag_device_save_file(
         if(!flipper_format_write_comment_cstr(file, "Mag device track data")) break;
 
         // Write data
-        if(!flipper_format_write_string_cstr(file, "Data", furi_string_get_cstr(mag_dev->dev_data)))
-            break;
+        for(uint8_t i = 0; i < MAG_DEV_TRACKS; i++) {
+            furi_string_printf(temp_str, "Track %d", i + 1);
+            if(!flipper_format_write_string_cstr(
+                   file,
+                   furi_string_get_cstr(temp_str),
+                   furi_string_get_cstr(mag_dev->dev_data.track[i].str)))
+                break;
+        }
 
         saved = true;
     } while(0);
@@ -128,9 +143,13 @@ static bool mag_device_load_data(MagDevice* mag_dev, FuriString* path, bool show
         }
 
         // Parse data
-        if(!flipper_format_read_string(file, "Data", mag_dev->dev_data)) {
-            data_read = false;
-            break;
+        for(uint8_t i = 0; i < MAG_DEV_TRACKS; i++) {
+            furi_string_printf(temp_str, "Track %d", i + 1);
+            if(!flipper_format_read_string(
+                   file, furi_string_get_cstr(temp_str), mag_dev->dev_data.track[i].str)) {
+                data_read = false;
+                break;
+            }
         }
 
         parsed = true;

+ 14 - 5
mag_device.h

@@ -8,23 +8,32 @@
 #include "mag_icons.h"
 
 #define MAG_DEV_NAME_MAX_LEN 22
+#define MAG_DEV_TRACKS 3
 
 #define MAG_APP_FOLDER ANY_PATH("mag")
 #define MAG_APP_EXTENSION ".mag"
 
 typedef void (*MagLoadingCallback)(void* context, bool state);
 
-typedef struct MagDevice MagDevice;
+//typedef struct MagDevice MagDevice;
 
-struct MagDevice {
+typedef struct {
+    FuriString* str;
+} MagTrack;
+
+typedef struct {
+    MagTrack track[MAG_DEV_TRACKS];
+} MagDeviceData;
+
+typedef struct {
     Storage* storage;
     DialogsApp* dialogs;
-    FuriString* dev_data;
+    MagDeviceData dev_data;
     char dev_name[MAG_DEV_NAME_MAX_LEN + 1];
     FuriString* load_path;
     MagLoadingCallback loading_cb;
     void* loading_cb_ctx;
-};
+} MagDevice;
 
 MagDevice* mag_device_alloc();
 
@@ -36,7 +45,7 @@ bool mag_device_save(MagDevice* mag_dev, const char* dev_name);
 
 bool mag_file_select(MagDevice* mag_dev);
 
-void mag_device_data_clear(FuriString* dev_data);
+void mag_device_data_clear(MagDeviceData* dev_data);
 
 void mag_device_clear(MagDevice* mag_dev);
 

+ 12 - 4
mag_i.h

@@ -3,6 +3,7 @@
 #include "mag_device.h"
 #include "helpers/mag_helpers.h"
 #include "helpers/mag_text_input.h"
+#include "helpers/mag_types.h"
 
 #include <furi.h>
 #include <furi_hal.h>
@@ -13,7 +14,6 @@
 #include <gui/scene_manager.h>
 #include <notification/notification_messages.h>
 
-
 #include <gui/modules/submenu.h>
 #include <gui/modules/dialog_ex.h>
 #include <gui/modules/popup.h>
@@ -40,9 +40,15 @@ enum MagCustomEvent {
     MagEventPopupClosed,
 };
 
-typedef struct Mag Mag;
+typedef struct {
+    MagTxState tx;
+    MagTrackState track;
+    MagReverseState reverse;
+    uint32_t us_clock;
+    uint32_t us_interpacket;
+} MagSetting;
 
-struct Mag {
+typedef struct {
     ViewDispatcher* view_dispatcher;
     Gui* gui;
     NotificationApp* notifications;
@@ -55,6 +61,8 @@ struct Mag {
     FuriString* file_path;
     FuriString* file_name;
 
+    MagSetting* setting;
+
     // Common views
     Submenu* submenu;
     DialogEx* dialog_ex;
@@ -66,7 +74,7 @@ struct Mag {
 
     // Custom views
     Mag_TextInput* mag_text_input;
-};
+} Mag;
 
 typedef enum {
     MagViewSubmenu,

+ 3 - 2
scenes/mag_scene_emulate.c

@@ -15,7 +15,7 @@ void mag_scene_emulate_on_enter(void* context) {
         widget, 13, 2, AlignLeft, AlignTop, FontPrimary, furi_string_get_cstr(tmp_str));
     furi_string_reset(tmp_str);
 
-    furi_string_printf(tmp_str, furi_string_get_cstr(mag->mag_dev->dev_data));
+    furi_string_printf(tmp_str, furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str));
     widget_add_string_multiline_element(
         widget, 0, 15, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(tmp_str));
 
@@ -40,7 +40,8 @@ bool mag_scene_emulate_on_event(void* context, SceneManagerEvent event) {
             consumed = true;
 
             FuriString* tmp_str;
-            tmp_str = furi_string_alloc_set_str(furi_string_get_cstr(mag->mag_dev->dev_data));
+            tmp_str = furi_string_alloc_set_str(
+                furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str));
 
             // Assumes track 2 for temporary testing.
             // Will overhaul alongside file format and config system

+ 38 - 35
scenes/mag_scene_emulate_config.c

@@ -15,10 +15,10 @@ const char* const tx_text[TX_COUNT] = {
     "RFID",
     "A6/A7",
 };
-const bool tx_value[TX_COUNT] = {
-    true,
-    false,
-}; // placeholder; there is certainly going to be a smarter way to do this...
+const uint32_t tx_value[TX_COUNT] = {
+    MagTxStateRFID,
+    MagTxStateGPIOA6A7,
+};
 
 #define TRACK_COUNT 3
 const char* const track_text[TRACK_COUNT] = {
@@ -27,19 +27,19 @@ const char* const track_text[TRACK_COUNT] = {
     "2",
 };
 const uint32_t track_value[TRACK_COUNT] = {
-    0,
-    1,
-    2,
-}; // placeholder; will want a better way to designate both tracks. create a file akin to subghz_types.h?
+    MagTrackStateAll,
+    MagTrackStateOne,
+    MagTrackStateTwo,
+};
 
 #define REVERSE_COUNT 2
 const char* const reverse_text[REVERSE_COUNT] = {
-    "ON",
     "OFF",
+    "ON",
 };
-const bool reverse_value[REVERSE_COUNT] = {
-    true,
-    false,
+const uint32_t reverse_value[REVERSE_COUNT] = {
+    MagReverseStateOff,
+    MagReverseStateOn,
 };
 
 #define CLOCK_COUNT 15
@@ -116,31 +116,37 @@ static void mag_scene_emulate_config_set_tx(VariableItem* item) {
 
     variable_item_set_current_value_text(item, tx_text[index]);
 
-    UNUSED(mag);
-
-    // TODO: set code
+    mag->setting->tx = tx_value[index];
 };
 
 static void mag_scene_emulate_config_set_track(VariableItem* item) {
     Mag* mag = variable_item_get_context(item);
     uint8_t index = variable_item_get_current_value_index(item);
 
-    variable_item_set_current_value_text(item, track_text[index]);
+    if(mag->setting->reverse == MagReverseStateOff) {
+        variable_item_set_current_value_text(item, track_text[index]);
+        mag->setting->track = track_value[index];
+    } else if(mag->setting->reverse == MagReverseStateOn) {
+        variable_item_set_current_value_index(
+            item, value_index_uint32(MagTrackStateAll, track_value, TRACK_COUNT));
+    }
 
-    UNUSED(mag);
-
-    // TODO: set code
+    // TODO: Check there is data in selected track?
+    //       Only display track options with data?
 };
 
 static void mag_scene_emulate_config_set_reverse(VariableItem* item) {
     Mag* mag = variable_item_get_context(item);
     uint8_t index = variable_item_get_current_value_index(item);
 
-    variable_item_set_current_value_text(item, reverse_text[index]);
-
-    UNUSED(mag);
-
-    // TODO: set code
+    if(mag->setting->track == MagTrackStateAll) {
+        // only allow reverse track to be set when playing all
+        variable_item_set_current_value_text(item, reverse_text[index]);
+        mag->setting->reverse = reverse_value[index];
+    } else {
+        variable_item_set_current_value_index(
+            item, value_index_uint32(MagReverseStateOff, reverse_value, REVERSE_COUNT));
+    }
 };
 
 static void mag_scene_emulate_config_set_clock(VariableItem* item) {
@@ -149,9 +155,7 @@ static void mag_scene_emulate_config_set_clock(VariableItem* item) {
 
     variable_item_set_current_value_text(item, clock_text[index]);
 
-    UNUSED(mag);
-
-    // TODO: set code
+    mag->setting->us_clock = clock_value[index];
 };
 
 static void mag_scene_emulate_config_set_interpacket(VariableItem* item) {
@@ -160,9 +164,7 @@ static void mag_scene_emulate_config_set_interpacket(VariableItem* item) {
 
     variable_item_set_current_value_text(item, interpacket_text[index]);
 
-    UNUSED(mag);
-
-    // TODO: set code
+    mag->setting->us_interpacket = interpacket_value[index];
 };
 
 void mag_scene_emulate_config_on_enter(void* context) {
@@ -175,7 +177,7 @@ void mag_scene_emulate_config_on_enter(void* context) {
     // TX
     item = variable_item_list_add(
         mag->variable_item_list, "TX via:", TX_COUNT, mag_scene_emulate_config_set_tx, mag);
-    value_index = value_index_bool(true, tx_value, TX_COUNT);
+    value_index = value_index_uint32(mag->setting->tx, tx_value, TX_COUNT);
     scene_manager_set_scene_state(mag->scene_manager, MagSceneEmulateConfig, (uint32_t)item);
     variable_item_set_current_value_index(item, value_index);
     variable_item_set_current_value_text(item, tx_text[value_index]);
@@ -183,7 +185,7 @@ void mag_scene_emulate_config_on_enter(void* context) {
     // Track
     item = variable_item_list_add(
         mag->variable_item_list, "Track:", TRACK_COUNT, mag_scene_emulate_config_set_track, mag);
-    value_index = value_index_uint32(1, track_value, TRACK_COUNT);
+    value_index = value_index_uint32(mag->setting->track, track_value, TRACK_COUNT);
     scene_manager_set_scene_state(mag->scene_manager, MagSceneEmulateConfig, (uint32_t)item);
     variable_item_set_current_value_index(item, value_index);
     variable_item_set_current_value_text(item, track_text[value_index]);
@@ -195,7 +197,7 @@ void mag_scene_emulate_config_on_enter(void* context) {
         REVERSE_COUNT,
         mag_scene_emulate_config_set_reverse,
         mag);
-    value_index = value_index_bool(true, reverse_value, REVERSE_COUNT);
+    value_index = value_index_uint32(mag->setting->reverse, reverse_value, REVERSE_COUNT);
     scene_manager_set_scene_state(mag->scene_manager, MagSceneEmulateConfig, (uint32_t)item);
     variable_item_set_current_value_index(item, value_index);
     variable_item_set_current_value_text(item, reverse_text[value_index]);
@@ -203,7 +205,7 @@ void mag_scene_emulate_config_on_enter(void* context) {
     // Clock
     item = variable_item_list_add(
         mag->variable_item_list, "Clock:", CLOCK_COUNT, mag_scene_emulate_config_set_clock, mag);
-    value_index = value_index_uint32(240, clock_value, CLOCK_COUNT);
+    value_index = value_index_uint32(mag->setting->us_clock, clock_value, CLOCK_COUNT);
     scene_manager_set_scene_state(mag->scene_manager, MagSceneEmulateConfig, (uint32_t)item);
     variable_item_set_current_value_index(item, value_index);
     variable_item_set_current_value_text(item, clock_text[value_index]);
@@ -215,7 +217,8 @@ void mag_scene_emulate_config_on_enter(void* context) {
         INTERPACKET_COUNT,
         mag_scene_emulate_config_set_interpacket,
         mag);
-    value_index = value_index_uint32(10, interpacket_value, INTERPACKET_COUNT);
+    value_index =
+        value_index_uint32(mag->setting->us_interpacket, interpacket_value, INTERPACKET_COUNT);
     scene_manager_set_scene_state(mag->scene_manager, MagSceneEmulateConfig, (uint32_t)item);
     variable_item_set_current_value_index(item, value_index);
     variable_item_set_current_value_text(item, interpacket_text[value_index]);

+ 2 - 2
scenes/mag_scene_input_value.c

@@ -5,7 +5,7 @@ void mag_scene_input_value_on_enter(void* context) {
     Mag_TextInput* mag_text_input = mag->mag_text_input;
 
     // TODO: retrieve stored/existing data if editing rather than adding anew?
-    mag_text_store_set(mag, furi_string_get_cstr(mag->mag_dev->dev_data));
+    mag_text_store_set(mag, furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str));
 
     mag_text_input_set_header_text(mag_text_input, "Enter track data (WIP)");
     mag_text_input_set_result_callback(
@@ -23,7 +23,7 @@ bool mag_scene_input_value_on_event(void* context, SceneManagerEvent event) {
         if(event.event == MagEventNext) {
             consumed = true;
 
-            furi_string_set(mag->mag_dev->dev_data, mag->text_store);
+            furi_string_set(mag->mag_dev->dev_data.track[1].str, mag->text_store);
             scene_manager_next_scene(scene_manager, MagSceneInputName);
         }
     }

+ 1 - 1
scenes/mag_scene_saved_info.c

@@ -15,7 +15,7 @@ void mag_scene_saved_info_on_enter(void* context) {
         widget, 13, 2, AlignLeft, AlignTop, FontPrimary, furi_string_get_cstr(tmp_str));
     furi_string_reset(tmp_str);
 
-    furi_string_printf(tmp_str, furi_string_get_cstr(mag->mag_dev->dev_data));
+    furi_string_printf(tmp_str, furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str));
     widget_add_string_multiline_element(
         widget, 0, 15, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(tmp_str));