From f55a83b5fc12286118f4fdba9fbb0f24d2656df6 Mon Sep 17 00:00:00 2001 From: Igor Socec Date: Thu, 6 Mar 2014 12:48:39 +0100 Subject: [PATCH] introducing tty device path as mandatory parameter --- moxerver.c | 34 ++++++++++++++++++++++++++-------- moxerver.h | 5 +++-- tty.c | 3 ++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/moxerver.c b/moxerver.c index d6bdf56..f544759 100644 --- a/moxerver.c +++ b/moxerver.c @@ -7,7 +7,8 @@ /* Prints help message. */ void usage() { //TODO maybe some styling should be done - fprintf(stderr, "Usage: moxerver -p port [-h]\n\n"); + fprintf(stderr, "Usage: moxerver -p tcp_port -t tty_path [-h]\n"); + fprintf(stderr, "- tcp_port must be in range 4001 to 4008\n\n"); } /* MoxaNix main program loop. */ @@ -17,7 +18,10 @@ int main(int argc, char *argv[]) { struct server_t server; struct client_t client; //TODO working with only 1 client, this can be expanded into a list - unsigned int port; + struct tty_t tty_dev; + + unsigned int tcp_port = -1; + char tty_path[DEV_PATH] = {'\0'}; fd_set read_fds; int fdmax; @@ -25,22 +29,26 @@ int main(int argc, char *argv[]) { /* grab arguments */ - if (argc == 1) { + if (argc <= 1) { fprintf(stderr, "error parsing arguments\n"); usage(); return 0; } - while ((ret = getopt(argc, argv, ":p:h")) != -1) { + while ((ret = getopt(argc, argv, ":p:t:h")) != -1) { switch (ret) { /* get server port number */ case 'p': - port = (unsigned int) atoi(optarg); + tcp_port = (unsigned int) atoi(optarg); /* check port range */ - if (port < 4001 || port > 4008) { + if (tcp_port < 4001 || tcp_port > 4008) { fprintf(stderr, "error: port number out of 4001-4008 range\n"); return -1; } break; + /* get tty device path */ + case 't': + sprintf(tty_path, optarg); + break; /* print help and exit */ case 'h': usage(); @@ -51,15 +59,25 @@ int main(int argc, char *argv[]) { return 0; } } + if (strlen(tty_path) == 0) { + fprintf(stderr, "error parsing arguments\n"); + usage(); + return 0; + } /* introduction message */ fprintf(stderr, "=== MoxaNix ===\n"); + //TODO remove the following line after development phase + fprintf(stderr, "TCP port: %d, TTY device path: %s\n", tcp_port, tty_path); + /* initialize */ - server_setup(&server, port); + server_setup(&server, tcp_port); client.socket = -1; - //TODO this is a good place to create and start the TTY thread + + //TODO this is a good place to create and start the TTY thread, use "tty_path" when opening device + /* loop with timeouts waiting for client connection */ while (1) { diff --git a/moxerver.h b/moxerver.h index e79f63d..4ebafe7 100644 --- a/moxerver.h +++ b/moxerver.h @@ -9,7 +9,7 @@ #include #define DATA_BUFLEN 128 - +#define DEV_PATH 32 struct server_t { int socket; /* server socket */ @@ -27,6 +27,7 @@ struct client_t { struct tty_t { int fd; /* tty file descriptor */ struct termios ttyset; /* tty termios settings */ + char path[DEV_PATH]; /* tty device path */ char data[DATA_BUFLEN]; /* buffer for data received from tty */ }; @@ -50,7 +51,7 @@ int client_write(struct client_t *client, char *databuf, int datalen); /* Opens the tty device and configures it. */ -int tty_open(struct tty_t *tty_dev); +int tty_open(struct tty_t *tty_dev, char* path); /* Closes the tty device. */ int tty_close(struct tty_t *tty_dev); /* Reconfigures the tty device. */ diff --git a/tty.c b/tty.c index 8b0bfc0..c909190 100644 --- a/tty.c +++ b/tty.c @@ -2,11 +2,12 @@ /* Opens the tty device and configures it. */ -int tty_open(struct tty_t *tty_dev) { +int tty_open(struct tty_t *tty_dev, char* path) { // PROPOSAL: // open tty device to get file descriptor @tty_dev.fd // setup tty device parameters @tty_dev.ttyset // apply settings by calling tcsetattr(fd, ttyset) + // on success copy path to @tty_dev.path return 0; } /* Closes the tty device. */