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. 33
      main.c

@ -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

@ -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"

@ -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) {

@ -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

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

Loading…
Cancel
Save