diff --git a/Makefile b/Makefile index 8f1e249..fa9aaef 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ TARGET = moxerver # special include directories INCDIRS = -I. # special library directories -LIBDIRS = -L +LIBDIRS = -L. # used libraries #LIBS = -lm LIBS = -lpthread @@ -20,11 +20,11 @@ HEADERS = $(wildcard *.h) # all objects are built from their .c files and all headers in the directory %.o: %.c $(HEADERS) - $(CC) $(CFLAGS) $(LIBS) -c $< -o $@ + $(CC) $(CFLAGS) -c $< -o $@ # target is built from all object files $(TARGET): $(OBJECTS) - $(CC) $(CFLAGS) $(LIBS) $(OBJECTS) -o $@ + $(CC) $(OBJECTS) $(CFLAGS) -o $@ # support for default, clean and all options diff --git a/client.c b/client.c index 4a6ff98..57b5bbc 100644 --- a/client.c +++ b/client.c @@ -52,7 +52,7 @@ int client_write(struct client_t *client, char *databuf, int datalen) { int len; /* handle special telnet characters to display them correctly on client */ - telnet_handle_client_write(databuf, &datalen); + //telnet_handle_client_write(databuf, &datalen); //TODO let's print received bytes during development phase... { diff --git a/moxerver.c b/moxerver.c index 39160b6..1b20afb 100644 --- a/moxerver.c +++ b/moxerver.c @@ -10,9 +10,9 @@ /* Global variables used throughout the application */ -struct server_t server; -struct client_t client; //TODO working with only 1 client, this can be expanded into a list -struct tty_t tty_dev; +//struct server_t server; +//struct client_t client; //TODO working with only 1 client, this can be expanded into a list +//struct tty_t tty_dev; /* Prints help message. */ static void usage() { @@ -176,9 +176,10 @@ int main(int argc, char *argv[]) { continue; } /* echo back to client */ - client_write(&client, client.data, ret); + //client_write(&client, client.data, ret); //TODO we should send this data to TTY device here + tty_write(&tty_dev, client.data, ret); } } if (ret == 0) { diff --git a/moxerver.h b/moxerver.h index ef0c36a..b541a53 100644 --- a/moxerver.h +++ b/moxerver.h @@ -34,6 +34,9 @@ struct tty_t { char data[DATA_BUFLEN]; /* buffer for data received from tty */ }; +struct server_t server; +struct client_t client; //TODO working with only 1 client, this can be expanded into a list +struct tty_t tty_dev; /* Sets up the server on specific port, binds to a socket and listens for client connections. */ int server_setup(struct server_t *server, unsigned int port); diff --git a/tty.c b/tty.c index 8a6695d..d77adca 100644 --- a/tty.c +++ b/tty.c @@ -1,6 +1,7 @@ #include "moxerver.h" #include #define TTY_THREAD_TIMEOUT_SEC 30 +#define TTY_WAIT_TIMEOUT 5 /* seconds for select() timeout in server loop */ #define NAME "tty" /* Opens the tty device and configures it. */ @@ -61,22 +62,41 @@ int tty_read(struct tty_t *tty_dev) { /* Sends data from a buffer to tty device. */ int tty_write(struct tty_t *tty_dev, char *databuf, int datalen) { + write(tty_dev->fd, databuf, datalen); // databuf should point to client data buffer return 0; } +/* Main thread for reading and writing to tty device */ void *tty_thread_func(void *arg) { - //int i = 0; - char c; + //char c; struct tty_t *tty_dev = (struct tty_t*)arg; + struct timeval tv; + ssize_t br = 0; + int ret; + fd_set read_fds; fprintf(stderr, "[%s] tty thread started with passed argument: %s\n", NAME, tty_dev->path); - //while ((i * 10) < TTY_THREAD_TIMEOUT_SEC) { while (1) { + /* setup parameters for select() */ + tv.tv_sec = TTY_WAIT_TIMEOUT; + tv.tv_usec = 0; + FD_ZERO(&read_fds); + FD_SET(tty_dev->fd, &read_fds); + + /* wait with select() */ + ret = select(tty_dev->fd + 1, &read_fds, NULL, NULL, &tv); + + if (ret > 0 && FD_ISSET(tty_dev->fd, &read_fds)) { + br = read(tty_dev->fd, tty_dev->data, DATA_BUFLEN); + client_write(&client, tty_dev->data, br); + } + else { + } //sleep(10); - if (read(tty_dev->fd, &c, 1) > 0) - printf("%c", c); + //if (read(tty_dev->fd, &c, 1) > 0) + // printf("%c", c); //fprintf(stderr, "[%s] tty thread reporting ...\n", NAME); //i++;