fixed more bugs and basic sending now works again

master
Ondřej Hruška 7 years ago
parent 79f6f64514
commit 92eab30c77
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      CMakeLists.txt
  2. 2
      gex/TF_Integration.c
  3. 13
      gex/gex.h
  4. 15
      gex/gex_client.c
  5. 12
      gex/gex_client.h
  6. 6
      gex/gex_defines.h
  7. 13
      gex/gex_helpers.c
  8. 4
      gex/gex_helpers.h
  9. 4
      gex/gex_message_types.h
  10. 19
      gex/gex_unit.c
  11. 16
      gex/gex_unit.h
  12. 16
      main.c

@ -24,7 +24,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_defines.h gex/gex_unit.c gex/gex_unit.h gex/gex.h)
include_directories( include_directories(
gex gex

@ -5,6 +5,8 @@
#include <errno.h> #include <errno.h>
#include "TinyFrame.h" #include "TinyFrame.h"
#define GEX_H // to allow including other headers
#include "gex_internal.h" #include "gex_internal.h"
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, size_t len) void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, size_t len)

@ -0,0 +1,13 @@
//
// Created by MightyPork on 2017/12/19.
//
#ifndef GEX_H
#define GEX_H
#include "gex_defines.h"
#include "gex_client.h"
#include "gex_unit.h"
#include "gex_message_types.h"
#endif //GEX_H

@ -10,6 +10,7 @@
#include <errno.h> #include <errno.h>
#include "TinyFrame.h" #include "TinyFrame.h"
#define GEX_H // to allow including other headers
#include "gex_client.h" #include "gex_client.h"
#include "serial.h" #include "serial.h"
#include "gex_internal.h" #include "gex_internal.h"
@ -53,7 +54,7 @@ static TF_Result unit_report_lst(TinyFrame *tf, TF_Msg *msg)
.payload = (uint8_t *) (msg->data + 2), .payload = (uint8_t *) (msg->data + 2),
.len = (uint32_t) (msg->len - 2), .len = (uint32_t) (msg->len - 2),
.type = rpt_type, .type = rpt_type,
.unit = (lu == NULL) ? fbu : lu, .unit = (lu == NULL) ? &fbu : lu,
}; };
if (lu && lu->report_handler) { if (lu && lu->report_handler) {
@ -85,9 +86,10 @@ static TF_Result list_units_lst(TinyFrame *tf, TF_Msg *msg)
// append // append
struct gex_unit *lu = malloc(sizeof(struct gex_unit)); struct gex_unit *lu = malloc(sizeof(struct gex_unit));
lu->next = NULL; lu->next = NULL;
lu->type = "UNKNOWN"; lu->type = strdup("UNKNOWN"); // TODO
lu->name = strdup(buf); lu->name = strdup(buf);
lu->callsign = callsign; lu->callsign = callsign;
lu->gex = gex;
lu->report_handler = NULL; lu->report_handler = NULL;
if (tail == NULL) { if (tail == NULL) {
gex->ulu_head = lu; gex->ulu_head = lu;
@ -111,6 +113,12 @@ void GEX_OnReport(GexClient *gex, GexUnit *unit, GexEventListener lst)
} }
} }
/** Find a unit */
GexUnit *GEX_Unit(GexClient *gex, const char *name)
{
return gex_find_unit_by_name(gex, name);
}
/** Create a instance and connect */ /** Create a instance and connect */
GexClient *GEX_Init(const char *device, int timeout_ms) GexClient *GEX_Init(const char *device, int timeout_ms)
{ {
@ -125,6 +133,7 @@ GexClient *GEX_Init(const char *device, int timeout_ms)
gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100); gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100);
if (gex->acm_fd == -1) { if (gex->acm_fd == -1) {
free(gex); free(gex);
fprintf(stderr, "FAILED TO CONNECT TO %s!\n", device);
return NULL; return NULL;
} }
@ -172,7 +181,7 @@ void GEX_Poll(GexClient *gex)
void GEX_DeInit(GexClient *gex) void GEX_DeInit(GexClient *gex)
{ {
if (gex == NULL) return; if (gex == NULL) return;
close(gex->acm_fd); fsync(gex->acm_fd);
gex_destroy_unit_lookup(gex); gex_destroy_unit_lookup(gex);
TF_DeInit(gex->tf); TF_DeInit(gex->tf);
free(gex); free(gex);

@ -5,6 +5,10 @@
#ifndef GEX_CLIENT_GEX_CLIENT_H #ifndef GEX_CLIENT_GEX_CLIENT_H
#define GEX_CLIENT_GEX_CLIENT_H #define GEX_CLIENT_GEX_CLIENT_H
#ifndef GEX_H
#error "Include gex.h instead!"
#endif
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -22,6 +26,14 @@
*/ */
void GEX_OnReport(GexClient *gex, GexUnit *unit, GexEventListener lst); void GEX_OnReport(GexClient *gex, GexUnit *unit, GexEventListener lst);
/**
* Get a unit, or NULL
*
* @param gex - gex
* @param name - unit name
*/
GexUnit *GEX_Unit(GexClient *gex, const char *name);
/** /**
* Initialize the GEX client * Initialize the GEX client
* *

@ -5,9 +5,15 @@
#ifndef GEX_CLIENT_GEX_DEFINES_H #ifndef GEX_CLIENT_GEX_DEFINES_H
#define GEX_CLIENT_GEX_DEFINES_H #define GEX_CLIENT_GEX_DEFINES_H
#ifndef GEX_H
#error "Include gex.h instead!"
#endif
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "TinyFrame.h"
typedef TF_ID GexSession; typedef TF_ID GexSession;
typedef struct gex_client GexClient; typedef struct gex_client GexClient;

@ -3,17 +3,23 @@
// //
#include <malloc.h> #include <malloc.h>
#include <assert.h>
#define GEX_H // to allow including other headers
#include "gex_defines.h" #include "gex_defines.h"
#include "gex_helpers.h" #include "gex_helpers.h"
/** Delete recursively all GEX callsign look-up table entries */ /** Delete recursively all GEX callsign look-up table entries */
void gex_destroy_unit_lookup(GexClient *gex) void gex_destroy_unit_lookup(GexClient *gex)
{ {
assert(gex != NULL);
struct gex_unit *next = gex->ulu_head; struct gex_unit *next = gex->ulu_head;
while (next != NULL) { while (next != NULL) {
struct gex_unit *cur = next; struct gex_unit *cur = next;
next = next->next; next = next->next;
free(cur->name);
free(cur->type);
free(cur); free(cur);
} }
gex->ulu_head = NULL; gex->ulu_head = NULL;
@ -22,6 +28,8 @@ void gex_destroy_unit_lookup(GexClient *gex)
/** Get lookup entry for unit name */ /** Get lookup entry for unit name */
struct gex_unit *gex_find_unit_by_callsign(GexClient *gex, uint8_t callsign) struct gex_unit *gex_find_unit_by_callsign(GexClient *gex, uint8_t callsign)
{ {
assert(gex != NULL);
struct gex_unit *next = gex->ulu_head; struct gex_unit *next = gex->ulu_head;
while (next != NULL) { while (next != NULL) {
if (next->callsign == callsign) { if (next->callsign == callsign) {
@ -35,6 +43,9 @@ struct gex_unit *gex_find_unit_by_callsign(GexClient *gex, uint8_t callsign)
/** Get lookup entry for unit name */ /** Get lookup entry for unit name */
struct gex_unit *gex_find_unit_by_name(GexClient *gex, const char *name) struct gex_unit *gex_find_unit_by_name(GexClient *gex, const char *name)
{ {
assert(gex != NULL);
assert(name != NULL);
struct gex_unit *next = gex->ulu_head; struct gex_unit *next = gex->ulu_head;
while (next != NULL) { while (next != NULL) {
if (strcmp(next->name, name) == 0) { if (strcmp(next->name, name) == 0) {
@ -48,6 +59,8 @@ struct gex_unit *gex_find_unit_by_name(GexClient *gex, const char *name)
/** Get callsign for unit name */ /** Get callsign for unit name */
uint8_t gex_find_callsign_by_name(GexClient *gex, const char *name) uint8_t gex_find_callsign_by_name(GexClient *gex, const char *name)
{ {
assert(gex != NULL);
struct gex_unit *lu = gex_find_unit_by_name(gex, name); struct gex_unit *lu = gex_find_unit_by_name(gex, name);
return (uint8_t) ((lu == NULL) ? 0 : lu->callsign); return (uint8_t) ((lu == NULL) ? 0 : lu->callsign);
} }

@ -5,6 +5,10 @@
#ifndef GEX_CLIENT_GEX_HELPERS_H #ifndef GEX_CLIENT_GEX_HELPERS_H
#define GEX_CLIENT_GEX_HELPERS_H #define GEX_CLIENT_GEX_HELPERS_H
#ifndef GEX_H
#error "Include gex.h instead!"
#endif
#include "gex_internal.h" #include "gex_internal.h"
#include "gex_defines.h" #include "gex_defines.h"

@ -5,6 +5,10 @@
#ifndef GEX_CLIENT_GEX_MESSAGE_TYPES_H #ifndef GEX_CLIENT_GEX_MESSAGE_TYPES_H
#define GEX_CLIENT_GEX_MESSAGE_TYPES_H #define GEX_CLIENT_GEX_MESSAGE_TYPES_H
#ifndef GEX_H
#error "Include gex.h instead!"
#endif
/** /**
* Supported message types (TF_TYPE) * Supported message types (TF_TYPE)
*/ */

@ -4,6 +4,8 @@
#include <malloc.h> #include <malloc.h>
#include <assert.h> #include <assert.h>
#define GEX_H // to allow including other headers
#include "gex_defines.h" #include "gex_defines.h"
#include "gex_helpers.h" #include "gex_helpers.h"
#include "gex_message_types.h" #include "gex_message_types.h"
@ -20,9 +22,12 @@
*/ */
static void GEX_LL_Query(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len, TF_Listener lst, void *userdata2) static void GEX_LL_Query(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len, TF_Listener lst, void *userdata2)
{ {
assert(unit != NULL);
assert(unit->gex != NULL);
GexClient *gex = unit->gex; GexClient *gex = unit->gex;
uint8_t callsign = gex_find_callsign_by_name(gex, unit); uint8_t callsign = unit->callsign;
assert(callsign != 0); assert(callsign != 0);
uint8_t *pld = malloc(len + 2); uint8_t *pld = malloc(len + 2);
assert(pld != NULL); assert(pld != NULL);
@ -48,6 +53,9 @@ static void GEX_LL_Query(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t
/** Send with no listener, don't wait for response */ /** Send with no listener, don't wait for response */
void GEX_Send(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len) void GEX_Send(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len)
{ {
assert(unit != NULL);
assert(unit->gex != NULL);
GEX_LL_Query(unit, cmd, payload, len, NULL, NULL); GEX_LL_Query(unit, cmd, payload, len, NULL, NULL);
} }
@ -55,6 +63,8 @@ void GEX_Send(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len)
static TF_Result sync_query_lst(TinyFrame *tf, TF_Msg *msg) static TF_Result sync_query_lst(TinyFrame *tf, TF_Msg *msg)
{ {
GexClient *gex = tf->userdata; GexClient *gex = tf->userdata;
assert(gex != NULL);
// clone the message // clone the message
gex->sync_query_response.len = msg->len; gex->sync_query_response.len = msg->len;
gex->sync_query_response.session = msg->frame_id; gex->sync_query_response.session = msg->frame_id;
@ -70,7 +80,11 @@ static TF_Result sync_query_lst(TinyFrame *tf, TF_Msg *msg)
/** Static query */ /** Static query */
GexMsg GEX_Query(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len) GexMsg GEX_Query(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len)
{ {
assert(unit != NULL);
assert(unit->gex != NULL);
GexClient *gex = unit->gex; GexClient *gex = unit->gex;
gex->sync_query_ok = false; gex->sync_query_ok = false;
// Default response that will be used if nothing is received // Default response that will be used if nothing is received
@ -113,6 +127,9 @@ static TF_Result async_query_lst(TinyFrame *tf, TF_Msg *msg)
/** Sync query, without poll */ /** Sync query, without poll */
void GEX_QueryAsync(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len, GexEventListener lst) void GEX_QueryAsync(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len, GexEventListener lst)
{ {
assert(unit != NULL);
assert(unit->gex != NULL);
GexClient *gex = unit->gex; GexClient *gex = unit->gex;
gex->sync_query_ok = false; gex->sync_query_ok = false;
memset(&gex->sync_query_response, 0, sizeof(GexMsg)); memset(&gex->sync_query_response, 0, sizeof(GexMsg));

@ -5,6 +5,22 @@
#ifndef GEX_CLIENT_GEX_UNIT_H #ifndef GEX_CLIENT_GEX_UNIT_H
#define GEX_CLIENT_GEX_UNIT_H #define GEX_CLIENT_GEX_UNIT_H
#ifndef GEX_H
#error "Include gex.h instead!"
#endif
#include "gex_defines.h"
#include <stdint.h>
#include <stdbool.h>
/** Send with no listener, don't wait for response */
void GEX_Send(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len);
/** Static query */
GexMsg GEX_Query(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len);
/** Sync query, without poll */
void GEX_QueryAsync(GexUnit *unit, uint8_t cmd, uint8_t *payload, uint32_t len, GexEventListener lst);
#endif //GEX_CLIENT_GEX_UNIT_H #endif //GEX_CLIENT_GEX_UNIT_H

@ -1,7 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "gex_client.h"
#include <signal.h> #include <signal.h>
#include <assert.h>
#include "gex.h"
static GexClient *gex; static GexClient *gex;
@ -14,18 +16,20 @@ static void sigintHandler(int sig)
exit(0); exit(0);
} }
#define LED_CMD_TOGGLE 0x02
int main() int main()
{ {
// Bind ^C handler for safe shutdown // Bind ^C handler for safe shutdown
signal(SIGINT, sigintHandler); signal(SIGINT, sigintHandler);
gex = GEX_Init("/dev/ttyACM0", 200); gex = GEX_Init("/dev/ttyACM0", 200);
if (!gex) { if (!gex) exit(1);
fprintf(stderr, "FAILED TO CONNECT, ABORTING!\n");
exit(1); GexUnit *led = GEX_Unit(gex, "LED");
} assert(led != NULL);
printf("Hello, World!\n"); GEX_Send(led, LED_CMD_TOGGLE, NULL, 0);
GEX_DeInit(gex); GEX_DeInit(gex);
return 0; return 0;

Loading…
Cancel
Save