added pre-built functions for reading and writing INI

master
Ondřej Hruška 7 years ago
parent 9e183920fc
commit 6efe32654a
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      CMakeLists.txt
  2. 1
      gex/gex.h
  3. 5
      gex/gex_client.c
  4. 16
      gex/gex_client.h
  5. 33
      gex/gex_settings.c
  6. 21
      gex/gex_settings.h
  7. 23
      main.c

@ -26,7 +26,7 @@ set(SOURCE_FILES
gex/protocol/TinyFrame.c gex/protocol/TinyFrame.c
gex/protocol/TinyFrame.h gex/protocol/TinyFrame.h
gex/utils/type_coerce.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( include_directories(
gex gex

@ -7,6 +7,7 @@
#include "gex_defines.h" #include "gex_defines.h"
#include "gex_client.h" #include "gex_client.h"
#include "gex_settings.h"
#include "gex_unit.h" #include "gex_unit.h"
#include "gex_message_types.h" #include "gex_message_types.h"

@ -19,7 +19,8 @@
#include "gex_helpers.h" #include "gex_helpers.h"
#include "utils/payload_parser.h" #include "utils/payload_parser.h"
GexUnit *GEX_SystemUnit(GexClient *gex) /** Get the system unit */
GexUnit *GEX_SysUnit(GexClient *gex)
{ {
return &gex->system_unit; return &gex->system_unit;
} }
@ -119,7 +120,7 @@ TinyFrame *GEX_GetTF(GexClient *gex)
} }
/** Find a unit */ /** 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); GexUnit *u = gex_find_unit_by_name(gex, name);
if (u == NULL) { if (u == NULL) {

@ -33,9 +33,21 @@ void GEX_SetUnitReportListener(GexUnit *unit, GexEventListener lst);
*/ */
void GEX_SetDefaultReportListener(GexClient *gex, 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); 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 * Get a unit, or NULL
@ -43,7 +55,7 @@ GexUnit *GEX_SystemUnit(GexClient *gex);
* @param gex - gex * @param gex - gex
* @param name - unit name * @param name - unit name
*/ */
GexUnit *GEX_Unit(GexClient *gex, const char *name); GexUnit *GEX_GetUnit(GexClient *gex, const char *name);
/** /**
* Initialize the GEX client * Initialize the GEX client

@ -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);
}

@ -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 <stdint.h>
#include <stdbool.h>
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

@ -80,12 +80,13 @@ int main(void)
gex = GEX_Init("/dev/ttyACM0", 200); gex = GEX_Init("/dev/ttyACM0", 200);
if (!gex) exit(1); if (!gex) exit(1);
// Fallback TF listener for reporting unhandled frames
TF_AddGenericListener(GEX_GetTF(gex), hdl_default); TF_AddGenericListener(GEX_GetTF(gex), hdl_default);
//GexUnit *led = GEX_Unit(gex, "LED"); //GexUnit *led = GEX_Unit(gex, "LED");
//GEX_Send(led, LED_CMD_TOGGLE, NULL, 0); //GEX_Send(led, LED_CMD_TOGGLE, NULL, 0);
GexUnit *test = GEX_Unit(gex, "TEST"); GexUnit *test = GEX_GetUnit(gex, "TEST");
assert(test != NULL); assert(test != NULL);
// the "PING" command // the "PING" command
@ -98,7 +99,7 @@ int main(void)
fprintf(stderr, "Cmd \"PING\" OK\n"); fprintf(stderr, "Cmd \"PING\" OK\n");
#endif #endif
#if 1 #if 0
// Load settings to a buffer as INI // Load settings to a buffer as INI
uint8_t inifile[10000]; uint8_t inifile[10000];
br = (GexBulk){ br = (GexBulk){
@ -108,14 +109,28 @@ int main(void)
.req_data = NULL, .req_data = NULL,
.req_len = 0, .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, "Read %d bytes of INI:\n", actuallyRead);
fprintf(stderr, "%.*s", actuallyRead, inifile); fprintf(stderr, "%.*s", actuallyRead, inifile);
// And send it back... // And send it back...
br.len = actuallyRead; br.len = actuallyRead;
br.req_cmd = MSG_INI_WRITE; 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 #endif
#if 0 #if 0

Loading…
Cancel
Save