make it work with new tinyframe
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ set(SOURCE_FILES
|
||||
gex/gex_client.h
|
||||
gex/utils/hexdump.c
|
||||
gex/utils/hexdump.h
|
||||
gex/protocol/TF_Integration.c
|
||||
gex/TF_Integration.c
|
||||
gex/utils/payload_builder.c
|
||||
gex/utils/payload_builder.h
|
||||
gex/utils/payload_parser.c
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
|
||||
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, size_t len)
|
||||
{
|
||||
assert(gex_serial_fd != 0); // TODO update after TF has instances
|
||||
GexClient *gc = tf->userdata;
|
||||
assert(gc->acm_fd != 0);
|
||||
|
||||
ssize_t rv = write(gex_serial_fd, buff, len);
|
||||
ssize_t rv = write(gc->acm_fd, buff, len);
|
||||
if (rv != len) {
|
||||
fprintf(stderr, "ERROR %d in TF write: %s\n", errno, strerror(errno));
|
||||
}
|
||||
+44
-62
@@ -5,7 +5,6 @@
|
||||
#include <malloc.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
@@ -14,88 +13,71 @@
|
||||
#include "gex_client.h"
|
||||
#include "serial.h"
|
||||
|
||||
int gex_serial_fd = -1;
|
||||
|
||||
/** ^C handler to close it gracefully */
|
||||
static void sigintHandler(int sig)
|
||||
TF_Result connectivityCheckCb(TinyFrame *tf, TF_Msg *msg)
|
||||
{
|
||||
if (gex_serial_fd != -1) {
|
||||
close(gex_serial_fd);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
TF_Result connectivityCheckCb(TF_Msg *msg)
|
||||
{
|
||||
GexClient *gc = msg->userdata;
|
||||
gc->connected = true;
|
||||
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data);
|
||||
|
||||
msg->userdata = NULL;
|
||||
return TF_CLOSE;
|
||||
GexClient *gex = tf->userdata;
|
||||
gex->connected = true;
|
||||
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data);
|
||||
return TF_CLOSE;
|
||||
}
|
||||
|
||||
GexClient *GEX_Init(const char *device, int timeout_ms)
|
||||
{
|
||||
assert(device != NULL);
|
||||
assert(device != NULL);
|
||||
|
||||
GexClient *gc = calloc(1, sizeof(GexClient));
|
||||
assert(gc != NULL);
|
||||
GexClient *gex = calloc(1, sizeof(GexClient));
|
||||
assert(gex != NULL);
|
||||
|
||||
// Bind ^C handler for safe shutdown
|
||||
signal(SIGINT, sigintHandler);
|
||||
// Open the device
|
||||
gex->acm_device = device;
|
||||
gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100);
|
||||
if (gex->acm_fd == -1) {
|
||||
free(gex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Open the device
|
||||
gc->acm_device = device;
|
||||
gc->acm_fd = serial_open(device, false, (timeout_ms+50)/100);
|
||||
if (gc->acm_fd == -1) {
|
||||
free(gc);
|
||||
return NULL;
|
||||
}
|
||||
gex->tf = TF_Init(TF_MASTER);
|
||||
gex->tf->userdata = gex;
|
||||
|
||||
gex_serial_fd = gc->acm_fd;
|
||||
// Test connectivity
|
||||
TF_Msg msg;
|
||||
TF_ClearMsg(&msg);
|
||||
msg.type = 0x01; // TODO use constant
|
||||
TF_Query(gex->tf, &msg, connectivityCheckCb, 0);
|
||||
GEX_Poll(gex);
|
||||
|
||||
// Test connectivity
|
||||
TF_Msg msg;
|
||||
TF_ClearMsg(&msg);
|
||||
msg.type = 0x01; // TODO use constant
|
||||
msg.userdata = gc;
|
||||
TF_Query(&msg, connectivityCheckCb, 0);
|
||||
GEX_Poll(gc);
|
||||
if (!gex->connected) {
|
||||
fprintf(stderr, "GEX doesn't respond to ping!\n");
|
||||
GEX_DeInit(gex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!gc->connected) {
|
||||
fprintf(stderr, "GEX doesn't respond to ping!\n");
|
||||
GEX_DeInit(gc);
|
||||
return NULL;
|
||||
}
|
||||
// TODO load and store unit callsigns + names
|
||||
|
||||
// TODO load and store unit callsigns + names
|
||||
|
||||
return gc;
|
||||
return gex;
|
||||
}
|
||||
|
||||
|
||||
void GEX_Poll(GexClient *gc)
|
||||
void GEX_Poll(GexClient *gex)
|
||||
{
|
||||
static uint8_t pollbuffer[4096];
|
||||
uint8_t pollbuffer[1024];
|
||||
|
||||
assert(gc != NULL);
|
||||
assert(gex != NULL);
|
||||
|
||||
ssize_t len = read(gc->acm_fd, pollbuffer, 4096);
|
||||
if (len < 0) {
|
||||
fprintf(stderr, "ERROR %d in GEX Poll: %s\n", errno, strerror(errno));
|
||||
} else {
|
||||
// hexDump("Received", pollbuffer, (uint32_t) len);
|
||||
TF_Accept(pollbuffer, (size_t) len);
|
||||
}
|
||||
ssize_t len = read(gex->acm_fd, pollbuffer, 1024);
|
||||
if (len < 0) {
|
||||
fprintf(stderr, "ERROR %d in GEX Poll: %s\n", errno, strerror(errno));
|
||||
} else {
|
||||
//hexDump("Received", pollbuffer, (uint32_t) len);
|
||||
TF_Accept(gex->tf, pollbuffer, (size_t) len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GEX_DeInit(GexClient *gc)
|
||||
{
|
||||
if (gc == NULL) return;
|
||||
|
||||
close(gc->acm_fd);
|
||||
gex_serial_fd = -1;
|
||||
free(gc);
|
||||
if (gc == NULL) return;
|
||||
close(gc->acm_fd);
|
||||
TF_DeInit(gc->tf);
|
||||
free(gc);
|
||||
}
|
||||
|
||||
+7
-5
@@ -8,11 +8,13 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "TinyFrame.h"
|
||||
|
||||
struct gex_client_ {
|
||||
const char *acm_device;
|
||||
int acm_fd;
|
||||
bool connected;
|
||||
TinyFrame *tf;
|
||||
const char *acm_device;
|
||||
int acm_fd;
|
||||
bool connected;
|
||||
};
|
||||
|
||||
typedef struct gex_client_ GexClient;
|
||||
@@ -28,9 +30,9 @@ GexClient *GEX_Init(const char *device, int timeout_ms);
|
||||
|
||||
/**
|
||||
* Poll for new messages
|
||||
* @param gc - client
|
||||
* @param gex - client
|
||||
*/
|
||||
void GEX_Poll(GexClient *gc);
|
||||
void GEX_Poll(GexClient *gex);
|
||||
|
||||
/**
|
||||
* Safely release all resources used up by GEX_Init()
|
||||
|
||||
@@ -169,6 +169,13 @@ TinyFrame * _TF_FN TF_Init(TF_Peer peer_bit)
|
||||
return tf;
|
||||
}
|
||||
|
||||
/** Release the struct */
|
||||
void TF_DeInit(TinyFrame *tf)
|
||||
{
|
||||
if (tf == NULL) return;
|
||||
free(tf);
|
||||
}
|
||||
|
||||
//region Listeners
|
||||
|
||||
/** Reset ID listener's timeout to the original value */
|
||||
@@ -696,7 +703,7 @@ static size_t _TF_FN TF_ComposeBody(uint8_t *outbuff,
|
||||
* @param cksum - checksum variable used for the body
|
||||
* @return nr of bytes in outbuff used
|
||||
*/
|
||||
static size_t _TF_FN TF_ComposeTail(uint8_t *outbuff, const TF_CKSUM *cksum)
|
||||
static size_t _TF_FN TF_ComposeTail(uint8_t *outbuff, TF_CKSUM *cksum)
|
||||
{
|
||||
int8_t si = 0; // signed small int
|
||||
uint8_t b = 0;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
* Upstream URL: https://github.com/MightyPork/TinyFrame
|
||||
*/
|
||||
|
||||
#define TF_VERSION "2.0.0"
|
||||
#define TF_VERSION "2.0.1"
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
#include <stdint.h> // for uint8_t etc
|
||||
@@ -235,6 +235,13 @@ TinyFrame *TF_Init(TF_Peer peer_bit);
|
||||
*/
|
||||
void TF_InitStatic(TinyFrame *tf, TF_Peer peer_bit);
|
||||
|
||||
/**
|
||||
* De-init the dynamically allocated TF instance
|
||||
*
|
||||
* @param tf
|
||||
*/
|
||||
void TF_DeInit(TinyFrame *tf);
|
||||
|
||||
/**
|
||||
* Reset the frame parser state machine.
|
||||
* This does not affect registered listeners.
|
||||
|
||||
@@ -1,10 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "gex_client.h"
|
||||
#include <signal.h>
|
||||
|
||||
static GexClient *gex;
|
||||
|
||||
/** ^C handler to close it gracefully */
|
||||
static void sigintHandler(int sig)
|
||||
{
|
||||
if (gex != NULL) {
|
||||
GEX_DeInit(gex);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
GexClient *gex = GEX_Init("/dev/ttyACM0", 200);
|
||||
// Bind ^C handler for safe shutdown
|
||||
signal(SIGINT, sigintHandler);
|
||||
|
||||
gex = GEX_Init("/dev/ttyACM0", 200);
|
||||
if (!gex) {
|
||||
fprintf(stderr, "FAILED TO CONNECT, ABORTING!\n");
|
||||
exit(1);
|
||||
|
||||
Reference in New Issue
Block a user