Adding termios configuration and reading from tty device with some other code refactoring. The character read from serial device are currently only printf-ed to console.
This commit is contained in:
parent
e6775c120d
commit
c33bd6ca4e
2 changed files with 46 additions and 27 deletions
30
moxerver.c
30
moxerver.c
|
@ -12,6 +12,7 @@
|
|||
/* 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;
|
||||
|
||||
/* Prints help message. */
|
||||
static void usage() {
|
||||
|
@ -44,8 +45,6 @@ int main(int argc, char *argv[]) {
|
|||
int ret;
|
||||
|
||||
unsigned int tcp_port = -1;
|
||||
char tty_path[DEV_PATH] = {'\0'};
|
||||
struct tty_t tty_dev;
|
||||
|
||||
fd_set read_fds;
|
||||
int fdmax;
|
||||
|
@ -53,7 +52,6 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
pthread_t tty_thread;
|
||||
|
||||
|
||||
/* catch and handle some quit signals, SIGKILL can't be caught */
|
||||
signal(SIGTERM, quit_handler);
|
||||
signal(SIGQUIT, quit_handler);
|
||||
|
@ -73,7 +71,15 @@ int main(int argc, char *argv[]) {
|
|||
break;
|
||||
/* get tty device path */
|
||||
case 't':
|
||||
sprintf(tty_path, optarg);
|
||||
if ((strnlen(optarg, DEV_PATH) == 0) ||
|
||||
(strnlen(optarg, DEV_PATH) > (DEV_PATH - 1))) {
|
||||
fprintf(stderr, "[%s] error: tty path was not specified\n\n", NAME);
|
||||
usage();
|
||||
return -1;
|
||||
} else {
|
||||
/* set tty device path to in tty_dev struct */
|
||||
strcpy(tty_dev.path, optarg);
|
||||
}
|
||||
break;
|
||||
/* print help and exit */
|
||||
case 'h':
|
||||
|
@ -92,34 +98,26 @@ int main(int argc, char *argv[]) {
|
|||
usage();
|
||||
return -1;
|
||||
}
|
||||
if (strlen(tty_path) == 0) {
|
||||
fprintf(stderr, "[%s] error: tty path was not specified\n\n", NAME);
|
||||
usage();
|
||||
return -1;
|
||||
} else {
|
||||
/* set tty device path to in tty_dev struct */
|
||||
strcpy(tty_dev.path, tty_path);
|
||||
}
|
||||
|
||||
/* introduction message */
|
||||
fprintf(stderr, "[%s] === MoxaNix ===\n", NAME);
|
||||
|
||||
//TODO remove the following line after development phase
|
||||
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_path);
|
||||
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path);
|
||||
|
||||
/* initialize */
|
||||
server_setup(&server, tcp_port);
|
||||
client.socket = -1;
|
||||
|
||||
|
||||
//TODO this is a good place to create and start the TTY thread, use "tty_path" when opening device
|
||||
//TODO this is a good place to create and start the TTY thread, use "tty_dev.path" when opening device
|
||||
if (tty_open(&tty_dev) < 0) {
|
||||
fprintf(stderr, "[%s] error: opening of tty device at %s failed\n"
|
||||
"\t\t-> continuing in echo mode\n", NAME, tty_path);
|
||||
"\t\t-> continuing in echo mode\n", NAME, tty_dev.path);
|
||||
//return -1;
|
||||
}
|
||||
|
||||
ret = pthread_create(&tty_thread, NULL, tty_thread_func, "starting tty thread...");
|
||||
ret = pthread_create(&tty_thread, NULL, tty_thread_func, &tty_dev);
|
||||
|
||||
/* loop with timeouts waiting for client connection and data*/
|
||||
while (1) {
|
||||
|
|
43
tty.c
43
tty.c
|
@ -15,7 +15,27 @@ int tty_open(struct tty_t *tty_dev) {
|
|||
return -errno;
|
||||
else
|
||||
tty_dev->fd = fd;
|
||||
|
||||
|
||||
tty_dev->ttyset.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR |
|
||||
PARMRK | INPCK | ISTRIP | IXON);
|
||||
tty_dev->ttyset.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
|
||||
ONOCR | OFILL | OLCUC | OPOST);
|
||||
tty_dev->ttyset.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
|
||||
tty_dev->ttyset.c_cflag &= ~(CSIZE | PARENB);
|
||||
tty_dev->ttyset.c_cflag |= CS8;
|
||||
tty_dev->ttyset.c_cc[VMIN] = 1;
|
||||
tty_dev->ttyset.c_cc[VTIME] = 0;
|
||||
|
||||
if(cfsetispeed(&(tty_dev->ttyset), B115200) < 0 || cfsetospeed(&(tty_dev->ttyset), B115200) < 0) {
|
||||
fprintf(stderr, "[%s] error configuring tty device speed\n", NAME);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
if(tcsetattr(tty_dev->fd, TCSAFLUSH, &(tty_dev->ttyset)) < 0) {
|
||||
fprintf(stderr, "[%s] error configuring tty device\n", NAME);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
@ -46,23 +66,24 @@ int tty_write(struct tty_t *tty_dev, char *databuf, int datalen) {
|
|||
}
|
||||
|
||||
void *tty_thread_func(void *arg) {
|
||||
int i = 0;
|
||||
char *str;
|
||||
str = (char*)arg;
|
||||
//int i = 0;
|
||||
char c;
|
||||
struct tty_t *tty_dev = (struct tty_t*)arg;
|
||||
|
||||
fprintf(stderr, "[%s] tty thread started with passed argument: %s\n", NAME, str);
|
||||
fprintf(stderr, "[%s] tty thread started with passed argument: %s\n", NAME, tty_dev->path);
|
||||
|
||||
//while ((i * 10) < TTY_THREAD_TIMEOUT_SEC) {
|
||||
while (1) {
|
||||
sleep(10);
|
||||
fprintf(stderr, "[%s] tty thread reporting ...\n", NAME);
|
||||
i++;
|
||||
//sleep(10);
|
||||
if (read(tty_dev->fd, &c, 1) > 0)
|
||||
printf("%c", c);
|
||||
|
||||
//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;
|
||||
return (void *)tty_dev;;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue