diff --git a/TinyFrame/TF_Config.h b/TinyFrame/TF_Config.h index 22a3869..992e9b8 100644 --- a/TinyFrame/TF_Config.h +++ b/TinyFrame/TF_Config.h @@ -60,7 +60,7 @@ typedef uint8_t TF_COUNT; // Frame ID listeners (wait for response / multi-part message) #define TF_MAX_ID_LST 4 // Frame Type listeners (wait for frame with a specific first payload byte) -#define TF_MAX_TYPE_LST 5 +#define TF_MAX_TYPE_LST 6 // Generic listeners (fallback if no other listener catches it) #define TF_MAX_GEN_LST 1 diff --git a/comm/messages.c b/comm/messages.c index 5e488bf..0c248e7 100644 --- a/comm/messages.c +++ b/comm/messages.c @@ -3,6 +3,7 @@ // #include +#include #include "platform.h" #include "TinyFrame.h" #include "framework/unit_registry.h" @@ -59,6 +60,7 @@ static void settings_bulkread_cb(BulkRead *bulk, uint32_t chunk, uint8_t *buffer // clean-up request if (buffer == NULL) { free(bulk); + dbg("INI read complete."); return; } @@ -66,20 +68,78 @@ static void settings_bulkread_cb(BulkRead *bulk, uint32_t chunk, uint8_t *buffer settings_build_ini(&iw); } +/** + * Listener: Export INI file via TF + */ static TF_Result lst_ini_export(TinyFrame *tf, TF_Msg *msg) { + dbg("Bulk read INI file"); + BulkRead *bulk = malloc(sizeof(BulkRead)); + assert_param(bulk); + bulk->frame_id = msg->frame_id; bulk->len = settings_get_ini_len(); bulk->read = settings_bulkread_cb; + bulk->userdata = NULL; + bulkread_start(tf, bulk); return TF_STAY; } +// --------------------------------------------------------------------------- + +/** Callback for receiving a key-value pair from the INi parser when importing a INI file */ +static void iniparser_cb(const char *section, const char *key, const char *value, void *userData) +{ + settings_load_ini_key(section, key, value); +} + +/** Callback for importing INI file */ +static void settings_bulkwrite_cb(BulkWrite *bulk, const uint8_t *chunk, uint32_t len) +{ + // clean-up request + if (chunk == NULL) { + ini_parse_end(); + settings_load_ini_end(); + dbg("INI write complete."); + + free(bulk); + return; + } + + ini_parse((const char *) chunk, len); +} + +/** + * Listener: Import INI file (write from PC via TF) + */ static TF_Result lst_ini_import(TinyFrame *tf, TF_Msg *msg) { - // + dbg("Bulk write INI file"); + + BulkWrite *bulk = malloc(sizeof(BulkWrite)); + assert_param(bulk); + + bulk->frame_id = msg->frame_id; + bulk->len = settings_get_ini_len(); + bulk->write = settings_bulkwrite_cb; + + settings_load_ini_begin(); + ini_parse_begin(iniparser_cb, NULL); + + bulkwrite_start(tf, bulk); + + return TF_STAY; +} + +// --------------------------------------------------------------------------- + +/** Listener: Save settings to Flash */ +static TF_Result lst_persist_cfg(TinyFrame *tf, TF_Msg *msg) +{ + settings_save(); return TF_STAY; } @@ -96,6 +156,7 @@ void comm_init(void) suc &= TF_AddTypeListener(comm, MSG_LIST_UNITS, lst_list_units); suc &= TF_AddTypeListener(comm, MSG_INI_READ, lst_ini_export); suc &= TF_AddTypeListener(comm, MSG_INI_WRITE, lst_ini_import); + suc &= TF_AddTypeListener(comm, MSG_PERSIST_CFG, lst_persist_cfg); // fall-through suc &= TF_AddGenericListener(comm, lst_default); diff --git a/comm/messages.h b/comm/messages.h index 1ae6f8f..8d113c2 100644 --- a/comm/messages.h +++ b/comm/messages.h @@ -36,6 +36,7 @@ enum Message_Types_ { MSG_LIST_UNITS = 0x20, //!< Get all active unit call-signs, types and names MSG_INI_READ = 0x21, //!< Read the ini file via bulk MSG_INI_WRITE = 0x22, //!< Write the ini file via bulk + MSG_PERSIST_CFG = 0x23, //!< Write current settings to Flash (the equivalent of replacing the lock jumper) }; extern TinyFrame *comm;