|
|
|
@ -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,24 +13,11 @@ |
|
|
|
|
#include "gex_client.h" |
|
|
|
|
#include "serial.h" |
|
|
|
|
|
|
|
|
|
int gex_serial_fd = -1; |
|
|
|
|
|
|
|
|
|
/** ^C handler to close it gracefully */ |
|
|
|
|
static void sigintHandler(int sig) |
|
|
|
|
{ |
|
|
|
|
if (gex_serial_fd != -1) { |
|
|
|
|
close(gex_serial_fd); |
|
|
|
|
} |
|
|
|
|
exit(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TF_Result connectivityCheckCb(TF_Msg *msg) |
|
|
|
|
TF_Result connectivityCheckCb(TinyFrame *tf, TF_Msg *msg) |
|
|
|
|
{ |
|
|
|
|
GexClient *gc = msg->userdata; |
|
|
|
|
gc->connected = true; |
|
|
|
|
GexClient *gex = tf->userdata; |
|
|
|
|
gex->connected = true; |
|
|
|
|
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data); |
|
|
|
|
|
|
|
|
|
msg->userdata = NULL; |
|
|
|
|
return TF_CLOSE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -39,54 +25,51 @@ GexClient *GEX_Init(const char *device, int timeout_ms) |
|
|
|
|
{ |
|
|
|
|
assert(device != NULL); |
|
|
|
|
|
|
|
|
|
GexClient *gc = calloc(1, sizeof(GexClient)); |
|
|
|
|
assert(gc != NULL); |
|
|
|
|
|
|
|
|
|
// Bind ^C handler for safe shutdown
|
|
|
|
|
signal(SIGINT, sigintHandler); |
|
|
|
|
GexClient *gex = calloc(1, sizeof(GexClient)); |
|
|
|
|
assert(gex != 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); |
|
|
|
|
gex->acm_device = device; |
|
|
|
|
gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100); |
|
|
|
|
if (gex->acm_fd == -1) { |
|
|
|
|
free(gex); |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gex_serial_fd = gc->acm_fd; |
|
|
|
|
gex->tf = TF_Init(TF_MASTER); |
|
|
|
|
gex->tf->userdata = 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); |
|
|
|
|
TF_Query(gex->tf, &msg, connectivityCheckCb, 0); |
|
|
|
|
GEX_Poll(gex); |
|
|
|
|
|
|
|
|
|
if (!gc->connected) { |
|
|
|
|
if (!gex->connected) { |
|
|
|
|
fprintf(stderr, "GEX doesn't respond to ping!\n"); |
|
|
|
|
GEX_DeInit(gc); |
|
|
|
|
GEX_DeInit(gex); |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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); |
|
|
|
|
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(pollbuffer, (size_t) len); |
|
|
|
|
TF_Accept(gex->tf, pollbuffer, (size_t) len); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -94,8 +77,7 @@ void GEX_Poll(GexClient *gc) |
|
|
|
|
void GEX_DeInit(GexClient *gc) |
|
|
|
|
{ |
|
|
|
|
if (gc == NULL) return; |
|
|
|
|
|
|
|
|
|
close(gc->acm_fd); |
|
|
|
|
gex_serial_fd = -1; |
|
|
|
|
TF_DeInit(gc->tf); |
|
|
|
|
free(gc); |
|
|
|
|
} |
|
|
|
|