Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
File format overhaul, config backend
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
zacharyweiss committed Jan 21, 2023
1 parent 9d20ab6 commit d0faf35
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 59 deletions.
17 changes: 17 additions & 0 deletions helpers/mag_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

typedef enum {
MagReverseStateOn,
MagReverseStateOff,
} MagReverseState;

typedef enum {
MagTrackStateAll,
MagTrackStateOne,
MagTrackStateTwo,
} MagTrackState;

typedef enum {
MagTxStateRFID,
MagTxStateGPIOA6A7,
} MagTxState;
29 changes: 29 additions & 0 deletions mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
39 changes: 29 additions & 10 deletions mag_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 14 additions & 5 deletions mag_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand Down
16 changes: 12 additions & 4 deletions mag_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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;
Expand All @@ -55,6 +61,8 @@ struct Mag {
FuriString* file_path;
FuriString* file_name;

MagSetting* setting;

// Common views
Submenu* submenu;
DialogEx* dialog_ex;
Expand All @@ -66,7 +74,7 @@ struct Mag {

// Custom views
Mag_TextInput* mag_text_input;
};
} Mag;

typedef enum {
MagViewSubmenu,
Expand Down
5 changes: 3 additions & 2 deletions scenes/mag_scene_emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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
Expand Down
Loading

0 comments on commit d0faf35

Please sign in to comment.