Recognize channel number at start of command line. More error checking.

This commit is contained in:
wb2osz 2018-01-01 20:08:59 -05:00
parent 9cd305950f
commit 6ffef6ed52
1 changed files with 117 additions and 78 deletions

View File

@ -34,8 +34,6 @@
* Default is to connect to localhost:8001. * Default is to connect to localhost:8001.
* See the "usage" functions at the bottom for details. * See the "usage" functions at the bottom for details.
* *
* FIXME: This is a rough prototype that needs more work.
*
*---------------------------------------------------------------*/ *---------------------------------------------------------------*/
#include "direwolf.h" // Sets _WIN32_WINNT for XP API level needed by ws2tcpip.h #include "direwolf.h" // Sets _WIN32_WINNT for XP API level needed by ws2tcpip.h
@ -49,6 +47,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#endif #endif
@ -58,17 +58,17 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h>
//#include "ax25_pad.h" #include "ax25_pad.h"
#include "textcolor.h" #include "textcolor.h"
#include "serial_port.h" #include "serial_port.h"
#include "kiss_frame.h" #include "kiss_frame.h"
#include "sock.h" #include "sock.h"
#include "dtime_now.h" #include "dtime_now.h"
#include "audio.h" // for DEFAULT_TXDELAY, etc. #include "audio.h" // for DEFAULT_TXDELAY, etc.
#include "dtime_now.h"
// TODO: define in one place, use everywhere. // TODO: define in one place, use everywhere.
@ -90,16 +90,11 @@ static THREAD_F tnc_listen_serial (void *arg);
static void send_to_kiss_tnc (int chan, int cmd, char *data, int dlen); static void send_to_kiss_tnc (int chan, int cmd, char *data, int dlen);
static void hex_dump (unsigned char *p, int len); static void hex_dump (unsigned char *p, int len);
// Formerly used write/read on Linux, for some forgotten reason,
// Why didn't I use send/recv for Linux? // but making them the same seems to make more sense.
#if __WIN32__
#define SOCK_SEND(s,data,size) send(s,data,size,0) #define SOCK_SEND(s,data,size) send(s,data,size,0)
#define SOCK_RECV(s,data,size) recv(s,data,size,0) #define SOCK_RECV(s,data,size) recv(s,data,size,0)
#else
#define SOCK_SEND(s,data,size) write(s,data,size)
#define SOCK_RECV(s,data,size) read(s,data,size)
#endif
static void usage(void); static void usage(void);
static void usage2(void); static void usage2(void);
@ -273,8 +268,20 @@ int main (int argc, char *argv[])
* If receive queue directory was specified, make sure that it exists. * If receive queue directory was specified, make sure that it exists.
*/ */
if (strlen(receive_output) > 0) { if (strlen(receive_output) > 0) {
struct stat s;
// TODO if (stat(receive_output, &s) == 0) {
if ( ! S_ISDIR(s.st_mode)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Receive queue location, %s, is not a directory.\n", receive_output);
exit (EXIT_FAILURE);
}
}
else {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Receive queue location, %s, does not exist.\n", receive_output);
exit (EXIT_FAILURE);
}
} }
/* If port begins with digit, consider it to be TCP. */ /* If port begins with digit, consider it to be TCP. */
@ -427,9 +434,34 @@ static void process_input (char *stuff)
trim (stuff); trim (stuff);
/* /*
* Optional prefix, like "[9]" to specify channel. TODO FIXME * Optional prefix, like "[9]" or "[99]" to specify channel.
*/ */
p = stuff; p = stuff;
while (isspace(*p)) p++;
if (*p == '[') {
p++;
if (p[1] == ']') {
chan = atoi(p);
p += 2;
}
else if (p[2] == ']') {
chan = atoi(p);
p += 3;
}
else {
text_color_set(DW_COLOR_ERROR);
dw_printf ("ERROR! One or two digit channel number and ] was expected after [ at beginning of line.\n");
usage2();
return;
}
if (chan < 0 || chan > 15) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("ERROR! KISS channel number must be in range of 0 thru 15.\n");
usage2();
return;
}
while (isspace(*p)) p++;
}
/* /*
* If it starts with upper case letter or digit, assume it is an AX.25 frame in monitor format. * If it starts with upper case letter or digit, assume it is an AX.25 frame in monitor format.
@ -548,15 +580,19 @@ static void send_to_kiss_tnc (int chan, int cmd, char *data, int dlen)
hex_dump (kissed, klen); hex_dump (kissed, klen);
} }
// FIXME: Should check for non -1 server_sock or serial_fd.
// Might need to delay when not using interactive input.
if (using_tcp) { if (using_tcp) {
int rc = SOCK_SEND(server_sock, (char*)kissed, klen); int rc = SOCK_SEND(server_sock, (char*)kissed, klen);
(void)rc; if (rc != klen) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("ERROR writing KISS frame to socket.\n");
}
} }
else { else {
serial_port_write (serial_fd, (char*)kissed, klen); int rc = serial_port_write (serial_fd, (char*)kissed, klen);
if (rc != klen) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("ERROR writing KISS frame to serial port.\n");
}
} }
} /* end send_to_kiss_tnc */ } /* end send_to_kiss_tnc */
@ -578,8 +614,6 @@ static void send_to_kiss_tnc (int chan, int cmd, char *data, int dlen)
* *
*--------------------------------------------------------------------*/ *--------------------------------------------------------------------*/
//#define MAX_HOSTS 30
static THREAD_F tnc_listen_net (void *arg) static THREAD_F tnc_listen_net (void *arg)
{ {
int err; int err;
@ -673,6 +707,11 @@ static THREAD_F tnc_listen_serial (void *arg)
if (serial_fd == MYFDERROR) { if (serial_fd == MYFDERROR) {
text_color_set(DW_COLOR_ERROR); text_color_set(DW_COLOR_ERROR);
dw_printf("Unable to connect to KISS TNC serial port %s.\n", port); dw_printf("Unable to connect to KISS TNC serial port %s.\n", port);
#if __WIN32__
#else
// More detail such as "permission denied" or "no such device"
dw_printf("%s\n", strerror(errno));
#endif
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
@ -902,7 +941,7 @@ static void usage2 (void)
dw_printf (" s Slot time, 10ms units s 10\n"); dw_printf (" s Slot time, 10ms units s 10\n");
dw_printf (" t txTail, 10ms units t 5\n"); dw_printf (" t txTail, 10ms units t 5\n");
dw_printf (" f Full duplex f 0\n"); dw_printf (" f Full duplex f 0\n");
dw_printf (" h set Hardware h T.B.D.\n"); dw_printf (" h set Hardware h TNC:\n");
dw_printf ("\n"); dw_printf ("\n");
dw_printf (" Lines may be preceded by the form \"[9]\" to indicate a\n"); dw_printf (" Lines may be preceded by the form \"[9]\" to indicate a\n");
dw_printf (" channel other than the default 0.\n"); dw_printf (" channel other than the default 0.\n");