From 6efe32654a022ee4a24626ec387732dd8d9a3f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 25 Dec 2017 23:14:40 +0100 Subject: [PATCH] added pre-built functions for reading and writing INI --- CMakeLists.txt | 2 +- gex/gex.h | 1 + gex/gex_client.c | 5 +++-- gex/gex_client.h | 16 ++++++++++++++-- gex/gex_settings.c | 33 +++++++++++++++++++++++++++++++++ gex/gex_settings.h | 21 +++++++++++++++++++++ main.c | 33 ++++++++++++++++++++++++--------- 7 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 gex/gex_settings.c create mode 100644 gex/gex_settings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f7491b0..970d4cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ set(SOURCE_FILES gex/protocol/TinyFrame.c gex/protocol/TinyFrame.h gex/utils/type_coerce.h - gex/gex_defines.h gex/gex_unit.c gex/gex_unit.h gex/gex.h gex/gex_bulk.c) + gex/gex_defines.h gex/gex_unit.c gex/gex_unit.h gex/gex.h gex/gex_bulk.c gex/gex_settings.c gex/gex_settings.h) include_directories( gex diff --git a/gex/gex.h b/gex/gex.h index b76ff06..9eb0b37 100644 --- a/gex/gex.h +++ b/gex/gex.h @@ -7,6 +7,7 @@ #include "gex_defines.h" #include "gex_client.h" +#include "gex_settings.h" #include "gex_unit.h" #include "gex_message_types.h" diff --git a/gex/gex_client.c b/gex/gex_client.c index 334c6fa..018a8f8 100644 --- a/gex/gex_client.c +++ b/gex/gex_client.c @@ -19,7 +19,8 @@ #include "gex_helpers.h" #include "utils/payload_parser.h" -GexUnit *GEX_SystemUnit(GexClient *gex) +/** Get the system unit */ +GexUnit *GEX_SysUnit(GexClient *gex) { return &gex->system_unit; } @@ -119,7 +120,7 @@ TinyFrame *GEX_GetTF(GexClient *gex) } /** Find a unit */ -GexUnit *GEX_Unit(GexClient *gex, const char *name) +GexUnit *GEX_GetUnit(GexClient *gex, const char *name) { GexUnit *u = gex_find_unit_by_name(gex, name); if (u == NULL) { diff --git a/gex/gex_client.h b/gex/gex_client.h index c1b1e6a..f6901c9 100644 --- a/gex/gex_client.h +++ b/gex/gex_client.h @@ -33,9 +33,21 @@ void GEX_SetUnitReportListener(GexUnit *unit, GexEventListener lst); */ void GEX_SetDefaultReportListener(GexClient *gex, GexEventListener lst); +/** + * Get the bare TinyFrame instance + * @param gex - GEX client + * @return TinyFrame instance pointer + */ TinyFrame *GEX_GetTF(GexClient *gex); -GexUnit *GEX_SystemUnit(GexClient *gex); +/** + * Get the dummy SYSTEM unit. Can be used for bulk read/write that's + * not targeted at a particular unit. + * + * @param gex - client + * @return the SYSTEM unit + */ +GexUnit *GEX_SysUnit(GexClient *gex); /** * Get a unit, or NULL @@ -43,7 +55,7 @@ GexUnit *GEX_SystemUnit(GexClient *gex); * @param gex - gex * @param name - unit name */ -GexUnit *GEX_Unit(GexClient *gex, const char *name); +GexUnit *GEX_GetUnit(GexClient *gex, const char *name); /** * Initialize the GEX client diff --git a/gex/gex_settings.c b/gex/gex_settings.c new file mode 100644 index 0000000..e2b5559 --- /dev/null +++ b/gex/gex_settings.c @@ -0,0 +1,33 @@ +// +// Created by MightyPork on 2017/12/25. +// + +#define GEX_H // to allow including other headers +#include "gex_settings.h" +#include "gex_defines.h" +#include "gex_internal.h" +#include "gex_message_types.h" + +uint32_t GEX_SettingsIniRead(GexClient *gex, char *buffer, uint32_t maxlen) +{ + GexBulk br = (GexBulk){ + .buffer = (uint8_t *) buffer, + .capacity = maxlen, + .req_cmd = MSG_INI_READ + }; + + uint32_t actuallyRead = GEX_BulkRead(GEX_SysUnit(gex), &br); + + return actuallyRead; +} + +bool GEX_SettingsIniWrite(GexClient *gex, const char *buffer) +{ + GexBulk bw = (GexBulk){ + .buffer = (uint8_t *) buffer, + .len = (uint32_t) strlen(buffer), + .req_cmd = MSG_INI_WRITE + }; + + return GEX_BulkWrite(GEX_SysUnit(gex), &bw); +} diff --git a/gex/gex_settings.h b/gex/gex_settings.h new file mode 100644 index 0000000..b4ee34b --- /dev/null +++ b/gex/gex_settings.h @@ -0,0 +1,21 @@ +// +// Created by MightyPork on 2017/12/25. +// + +#ifndef GEX_CLIENT_GEX_SETTINGS_H +#define GEX_CLIENT_GEX_SETTINGS_H + +#ifndef GEX_H +#error "Include gex.h instead!" +#endif + +#include "gex_defines.h" +#include "gex_unit.h" +#include +#include + +uint32_t GEX_SettingsIniRead(GexClient *gex, char *buffer, uint32_t maxlen); + +bool GEX_SettingsIniWrite(GexClient *gex, const char *buffer); + +#endif //GEX_CLIENT_GEX_SETTINGS_H diff --git a/main.c b/main.c index 6e82df9..8c4e1a5 100644 --- a/main.c +++ b/main.c @@ -80,12 +80,13 @@ int main(void) gex = GEX_Init("/dev/ttyACM0", 200); if (!gex) exit(1); + // Fallback TF listener for reporting unhandled frames TF_AddGenericListener(GEX_GetTF(gex), hdl_default); //GexUnit *led = GEX_Unit(gex, "LED"); //GEX_Send(led, LED_CMD_TOGGLE, NULL, 0); - GexUnit *test = GEX_Unit(gex, "TEST"); + GexUnit *test = GEX_GetUnit(gex, "TEST"); assert(test != NULL); // the "PING" command @@ -98,24 +99,38 @@ int main(void) fprintf(stderr, "Cmd \"PING\" OK\n"); #endif -#if 1 +#if 0 // Load settings to a buffer as INI uint8_t inifile[10000]; br = (GexBulk){ - .buffer = inifile, - .capacity = 10000, - .req_cmd = MSG_INI_READ, - .req_data = NULL, - .req_len = 0, + .buffer = inifile, + .capacity = 10000, + .req_cmd = MSG_INI_READ, + .req_data = NULL, + .req_len = 0, }; - uint32_t actuallyRead = GEX_BulkRead(GEX_SystemUnit(gex), &br); + uint32_t actuallyRead = GEX_BulkRead(GEX_SysUnit(gex), &br); fprintf(stderr, "Read %d bytes of INI:\n", actuallyRead); fprintf(stderr, "%.*s", actuallyRead, inifile); // And send it back... br.len = actuallyRead; br.req_cmd = MSG_INI_WRITE; - GEX_BulkWrite(GEX_SystemUnit(gex), &br); + GEX_BulkWrite(GEX_SysUnit(gex), &br); +#endif + +#if 1 + // Load settings to a buffer as INI + char inifile[10000]; + uint32_t len = GEX_SettingsIniRead(gex, inifile, 10000); + assert(len > 0); + + fprintf(stderr, "Read %d bytes of INI:\n", len); + fprintf(stderr, "%.*s", len, inifile); + + // And send it back... + bool suc = GEX_SettingsIniWrite(gex, inifile); + assert(suc); #endif #if 0