Skip to content

Commit

Permalink
Add -d option to specify serial device to use
Browse files Browse the repository at this point in the history
  • Loading branch information
banksy-git committed Feb 22, 2021
1 parent fa520f9 commit 3c65f79
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions gateway/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <string.h>
#include <termios.h>

#define SERIAL_PORT "/dev/ttyS1"
#define DEFAULT_SERIAL_PORT "/dev/ttyS1"
#define BUF_SIZE 1024

static fd_set _master_read_set;
Expand Down Expand Up @@ -50,11 +50,11 @@ static void _error_exit(const char* msg)
exit(EXIT_FAILURE);
}

static int _open_serial_port(bool is_hw_flow_control)
static int _open_serial_port(const char* serial_port, bool is_hw_flow_control)
{
int fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
int fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd<0) {
_error_exit("Could not open serial port " SERIAL_PORT);
_error_exit("Could not open serial port");
}
fcntl(fd, F_SETFL, 0);

Expand Down Expand Up @@ -112,27 +112,36 @@ int main(int argc, char** argv)

bool is_hardware_flow_control = true;
uint16_t port = 8888;
char* serial_port = NULL;
opterr = 0;

int c;
while ((c = getopt (argc, argv, "fp:")) != -1) {
while ((c = getopt (argc, argv, "fp:d:")) != -1) {
switch(c) {
case 'f':
is_hardware_flow_control = false;
break;
case 'p':
port = atoi(optarg);
break;
case 'd':
free(serial_port);
serial_port = strdup(optarg);
break;
case '?':
default:
_error_exit("Unknown args");
}
}

fprintf(stderr, "serialgateway: port %d, flow=%s\n",
port, (is_hardware_flow_control)?"HW":"sw");
if (serial_port==NULL) {
serial_port = DEFAULT_SERIAL_PORT;
}

fprintf(stderr, "serialgateway: serial=%s port %d, flow=%s\n",
serial_port, port, (is_hardware_flow_control)?"HW":"sw");

_serial_fd = _open_serial_port(is_hardware_flow_control);
_serial_fd = _open_serial_port(serial_port, is_hardware_flow_control);

// Create listening socket
int listen_sock;
Expand Down

0 comments on commit 3c65f79

Please sign in to comment.