|
|
@ -5,7 +5,6 @@ |
|
|
|
#include <malloc.h> |
|
|
|
#include <malloc.h> |
|
|
|
#include <assert.h> |
|
|
|
#include <assert.h> |
|
|
|
#include <unistd.h> |
|
|
|
#include <unistd.h> |
|
|
|
#include <signal.h> |
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
#include <errno.h> |
|
|
|
#include <errno.h> |
|
|
@ -14,24 +13,11 @@ |
|
|
|
#include "gex_client.h" |
|
|
|
#include "gex_client.h" |
|
|
|
#include "serial.h" |
|
|
|
#include "serial.h" |
|
|
|
|
|
|
|
|
|
|
|
int gex_serial_fd = -1; |
|
|
|
TF_Result connectivityCheckCb(TinyFrame *tf, TF_Msg *msg) |
|
|
|
|
|
|
|
|
|
|
|
/** ^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) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
GexClient *gc = msg->userdata; |
|
|
|
GexClient *gex = tf->userdata; |
|
|
|
gc->connected = true; |
|
|
|
gex->connected = true; |
|
|
|
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data); |
|
|
|
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data); |
|
|
|
|
|
|
|
|
|
|
|
msg->userdata = NULL; |
|
|
|
|
|
|
|
return TF_CLOSE; |
|
|
|
return TF_CLOSE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -39,54 +25,51 @@ GexClient *GEX_Init(const char *device, int timeout_ms) |
|
|
|
{ |
|
|
|
{ |
|
|
|
assert(device != NULL); |
|
|
|
assert(device != NULL); |
|
|
|
|
|
|
|
|
|
|
|
GexClient *gc = calloc(1, sizeof(GexClient)); |
|
|
|
GexClient *gex = calloc(1, sizeof(GexClient)); |
|
|
|
assert(gc != NULL); |
|
|
|
assert(gex != NULL); |
|
|
|
|
|
|
|
|
|
|
|
// Bind ^C handler for safe shutdown
|
|
|
|
|
|
|
|
signal(SIGINT, sigintHandler); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Open the device
|
|
|
|
// Open the device
|
|
|
|
gc->acm_device = device; |
|
|
|
gex->acm_device = device; |
|
|
|
gc->acm_fd = serial_open(device, false, (timeout_ms+50)/100); |
|
|
|
gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100); |
|
|
|
if (gc->acm_fd == -1) { |
|
|
|
if (gex->acm_fd == -1) { |
|
|
|
free(gc); |
|
|
|
free(gex); |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
gex_serial_fd = gc->acm_fd; |
|
|
|
gex->tf = TF_Init(TF_MASTER); |
|
|
|
|
|
|
|
gex->tf->userdata = gex; |
|
|
|
|
|
|
|
|
|
|
|
// Test connectivity
|
|
|
|
// Test connectivity
|
|
|
|
TF_Msg msg; |
|
|
|
TF_Msg msg; |
|
|
|
TF_ClearMsg(&msg); |
|
|
|
TF_ClearMsg(&msg); |
|
|
|
msg.type = 0x01; // TODO use constant
|
|
|
|
msg.type = 0x01; // TODO use constant
|
|
|
|
msg.userdata = gc; |
|
|
|
TF_Query(gex->tf, &msg, connectivityCheckCb, 0); |
|
|
|
TF_Query(&msg, connectivityCheckCb, 0); |
|
|
|
GEX_Poll(gex); |
|
|
|
GEX_Poll(gc); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!gc->connected) { |
|
|
|
if (!gex->connected) { |
|
|
|
fprintf(stderr, "GEX doesn't respond to ping!\n"); |
|
|
|
fprintf(stderr, "GEX doesn't respond to ping!\n"); |
|
|
|
GEX_DeInit(gc); |
|
|
|
GEX_DeInit(gex); |
|
|
|
return NULL; |
|
|
|
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); |
|
|
|
ssize_t len = read(gex->acm_fd, pollbuffer, 1024); |
|
|
|
if (len < 0) { |
|
|
|
if (len < 0) { |
|
|
|
fprintf(stderr, "ERROR %d in GEX Poll: %s\n", errno, strerror(errno)); |
|
|
|
fprintf(stderr, "ERROR %d in GEX Poll: %s\n", errno, strerror(errno)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
//hexDump("Received", pollbuffer, (uint32_t) len);
|
|
|
|
//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) |
|
|
|
void GEX_DeInit(GexClient *gc) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (gc == NULL) return; |
|
|
|
if (gc == NULL) return; |
|
|
|
|
|
|
|
|
|
|
|
close(gc->acm_fd); |
|
|
|
close(gc->acm_fd); |
|
|
|
gex_serial_fd = -1; |
|
|
|
TF_DeInit(gc->tf); |
|
|
|
free(gc); |
|
|
|
free(gc); |
|
|
|
} |
|
|
|
} |
|
|
|