diff --git a/moxanix.cfg b/moxanix.cfg index 173657a..f6a0595 100644 --- a/moxanix.cfg +++ b/moxanix.cfg @@ -10,7 +10,7 @@ [4001] -dev = /dev/ttyUSB0 +dev = /dev/ttyS1 speed = 115200 [4002] diff --git a/moxerver.c b/moxerver.c index 996101f..77f68d4 100644 --- a/moxerver.c +++ b/moxerver.c @@ -53,10 +53,10 @@ int time2string(time_t time, char* timestamp) { /* Parse handler function, used to configure serial port */ int parse_handler(void *user, const char *section, const char *name, const char *value) { - printf("[%s] section = %s, name = %s, value = %s\n", __func__, section, name, value); + //printf("[%s] section = %s, name = %s, value = %s\n", __func__, section, name, value); if (!strcmp(name, "speed") && (unsigned int)atoi(section) == server.port) { - printf("[%s] setting %s speed for port %s\n", __func__, value, section); + fprintf(stderr, "[%s] setting %s speed for port %s\n", __func__, value, section); if (cfsetispeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0 || cfsetospeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0) { @@ -170,7 +170,12 @@ int main(int argc, char *argv[]) { fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path); /* start thread that handles tty device */ - ret = pthread_create(&tty_thread, NULL, tty_thread_func, &tty_dev); //TODO check return value? + ret = pthread_create(&tty_thread, NULL, tty_thread_func, &tty_dev); + if (ret) { + fprintf(stderr, "[%s] error starting serial monitor thread" + ", pthread_create returned %d\n", NAME, ret); + return -1; + } /* loop with timeouts waiting for client connection and data */ while (1) { diff --git a/moxerver.h b/moxerver.h index 618b7e6..ccf5055 100644 --- a/moxerver.h +++ b/moxerver.h @@ -35,6 +35,7 @@ struct client_t { struct tty_t { int fd; /* tty file descriptor */ + struct termios ttysetdef; /* default tty termios settings */ struct termios ttyset; /* tty termios settings */ char path[DEV_PATH]; /* tty device path */ char data[DATABUF_LEN]; /* buffer for data received from tty */ diff --git a/tty.c b/tty.c index 3eda2f8..9e22d8a 100644 --- a/tty.c +++ b/tty.c @@ -22,6 +22,11 @@ int tty_open(struct tty_t *tty_dev) { else tty_dev->fd = fd; + /* store default termios setitngs */ + if (tcgetattr(tty_dev->fd, &(tty_dev->ttysetdef))) + fprintf(stderr, "[%s] error reading device default config\n" + "\t\t-> default config will not be restored upon exit", __func__); + tty_dev->ttyset.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON); tty_dev->ttyset.c_oflag &= ~(OCRNL | ONLCR | ONLRET | @@ -35,17 +40,17 @@ int tty_open(struct tty_t *tty_dev) { /* if speed is set to B0 (e.g. cfg file is not provided), default values are used */ if (cfgetispeed(&(tty_dev->ttyset)) == baud_to_speed(0) && cfsetispeed(&(tty_dev->ttyset), TTY_DEF_BAUD_RATE) < 0) { - fprintf(stderr, "[%s] error configuring tty device speed\n", NAME); + fprintf(stderr, "[%s] error configuring tty device speed\n", __func__); return -errno; } if (cfgetospeed(&(tty_dev->ttyset)) == baud_to_speed(0) && cfsetospeed(&(tty_dev->ttyset), TTY_DEF_BAUD_RATE) < 0) { - fprintf(stderr, "[%s] error configuring tty device speed\n", NAME); + fprintf(stderr, "[%s] error configuring tty device speed\n", __func__); return -errno; } - if(tcsetattr(tty_dev->fd, TCSAFLUSH, &(tty_dev->ttyset)) < 0) { - fprintf(stderr, "[%s] error configuring tty device\n", NAME); + if (tcsetattr(tty_dev->fd, TCSANOW, &(tty_dev->ttyset)) < 0) { + fprintf(stderr, "[%s] error configuring tty device\n", __func__); return -errno; } @@ -55,7 +60,20 @@ int tty_open(struct tty_t *tty_dev) { /* Closes the tty device. */ int tty_close(struct tty_t *tty_dev) { - // close and set "tty_dev.fd = -1" + + int fd = tty_dev->fd; + tty_dev->fd = -1; + + fprintf(stderr, "[%s] closing tty device \n", __func__); + + if (tcsetattr(tty_dev->fd, TCSANOW, &(tty_dev->ttysetdef)) < 0) { + fprintf(stderr, "[%s] error restorting tty device default config\n", __func__); + return -errno; + } + + if (close(fd) < 0) + return -errno; + return 0; }