diff --git a/Makefile b/Makefile index 0a1fb8b..61022dc 100644 --- a/Makefile +++ b/Makefile @@ -7,11 +7,11 @@ INCDIRS = -I. LIBDIRS = -L # used libraries #LIBS = -lm -LIBS = -l +LIBS = -lpthread # compiler and flags CC = gcc -CFLAGS = -Wall $(INCDIRS) $(LIBDIRS) +CFLAGS = -Wall $(INCDIRS) $(LIBDIRS) $(LIBS) # objects are .o files created from all .c files in the directory (same name) OBJECTS = $(patsubst %.c,%.o,$(wildcard *.c)) diff --git a/moxerver.c b/moxerver.c index b17ba09..a306aca 100644 --- a/moxerver.c +++ b/moxerver.c @@ -1,5 +1,6 @@ #include "moxerver.h" #include /* handling quit signals */ +#include #define NAME "moxerver" @@ -49,6 +50,8 @@ int main(int argc, char *argv[]) { fd_set read_fds; int fdmax; struct timeval tv; + + pthread_t tty_thread; /* catch and handle some quit signals, SIGKILL can't be caught */ @@ -107,7 +110,7 @@ int main(int argc, char *argv[]) { //TODO this is a good place to create and start the TTY thread, use "tty_path" when opening device - + ret = pthread_create(&tty_thread, NULL, tty_thread_func, "starting tty thread..."); /* loop with timeouts waiting for client connection and data*/ while (1) { @@ -177,10 +180,12 @@ int main(int argc, char *argv[]) { } /* END while() loop */ + pthread_join(tty_thread, NULL); + /* unexpected break from while() loop */ fprintf(stderr, "[%s] unexpected condition\n", NAME); /* cleanup and exit with -1 */ cleanup(-1); return -1; -} \ No newline at end of file +} diff --git a/moxerver.h b/moxerver.h index 7275df2..5130333 100644 --- a/moxerver.h +++ b/moxerver.h @@ -71,6 +71,7 @@ int tty_reconfigure(struct tty_t *tty_dev, struct termios newttyset); 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); - +/* Main tty thread function */ +void *tty_thread_func(void *arg); diff --git a/tty.c b/tty.c index c909190..64966a7 100644 --- a/tty.c +++ b/tty.c @@ -1,5 +1,7 @@ #include "moxerver.h" - +#include +#define TTY_THREAD_TIMEOUT_SEC 30 +#define NAME "tty" /* Opens the tty device and configures it. */ int tty_open(struct tty_t *tty_dev, char* path) { @@ -34,4 +36,26 @@ int tty_read(struct tty_t *tty_dev) { int tty_write(struct tty_t *tty_dev, char *databuf, int datalen) { // databuf should point to client data buffer return 0; -} \ No newline at end of file +} + +void *tty_thread_func(void *arg) { + int i = 0; + char *str; + str = (char*)arg; + + fprintf(stderr, "[%s] tty thread started with passed argument: %s\n", NAME, str); + + //while ((i * 10) < TTY_THREAD_TIMEOUT_SEC) { + while (1) { + sleep(10); + fprintf(stderr, "[%s] tty thread reporting ...\n", NAME); + i++; + } + + fprintf(stderr, "[%s] tty thread stoped\n", NAME); + + strncpy(str, "bye", strlen(str)); + + return (void *)str; +} +