Adding tty thread primitive which only reports alive status every 10 seconds.

This commit is contained in:
Luka Miljak 2014-03-14 17:28:29 +01:00
parent be121ac22f
commit 3926b7738d
4 changed files with 37 additions and 7 deletions

View file

@ -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))

View file

@ -1,5 +1,6 @@
#include "moxerver.h"
#include <signal.h> /* handling quit signals */
#include <pthread.h>
#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;
}
}

View file

@ -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);

28
tty.c
View file

@ -1,5 +1,7 @@
#include "moxerver.h"
#include <string.h>
#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;
}
}
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;
}