From 519ebe648ccec7b4625172e2fa2ab87edbbdf7ee Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Mon, 17 Dec 2012 22:43:21 -0300 Subject: [PATCH] swuart/: convert "hammering the peer" demo into a primitive interactive chat Also rename directory from swuart/ to swuart-chat/, and application from "swuart" to "chat". --- {swuart => swuart-chat}/Makefile | 4 +- swuart-chat/chat.c | 105 +++++++++++++++++++++++++++++++ swuart/test.c | 56 ----------------- 3 files changed, 107 insertions(+), 58 deletions(-) rename {swuart => swuart-chat}/Makefile (89%) create mode 100644 swuart-chat/chat.c delete mode 100644 swuart/test.c diff --git a/swuart/Makefile b/swuart-chat/Makefile similarity index 89% rename from swuart/Makefile rename to swuart-chat/Makefile index cf8a0d2..d0b40fa 100644 --- a/swuart/Makefile +++ b/swuart-chat/Makefile @@ -3,8 +3,8 @@ CFLAGS = -g -Wall -O9 -I../libubb/include LDFLAGS = -static LDLIBS = -L../libubb -lubb -MAIN = swuart -OBJS = test.o +MAIN = chat +OBJS = chat.o .PHONY: all clean spotless diff --git a/swuart-chat/chat.c b/swuart-chat/chat.c new file mode 100644 index 0000000..17908f3 --- /dev/null +++ b/swuart-chat/chat.c @@ -0,0 +1,105 @@ + +/* + * swuart-chat/chat.c - Simple two-way chat using swuart + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +#define RX UBB_DAT0 +#define TX UBB_DAT1 + + +/* ----- TTY raw mode ------------------------------------------------------ */ + + +static struct termios old_term; + +static void raw(void) +{ + struct termios term; + + if (tcgetattr(0, &old_term) < 0) { + perror("tcgetattr"); + exit(1); + } + term = old_term; + cfmakeraw(&term); + if (tcsetattr(0, TCSAFLUSH, &term) < 0) { + perror("tcsetattr"); + exit(1); + } + if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) { + perror("fcntl"); + exit(1); + } +} + + +static void restore_term(void) +{ + if (tcsetattr(0, TCSAFLUSH, &old_term) < 0) + perror("tcsetattr"); +} + + +static void at_exit(void) +{ + restore_term(); + swuart_close(); +} + + +int main(int argc, char **argv) +{ + uint8_t local[10], remote[100]; + int got, i; + + raw(); + atexit(at_exit); + swuart_open(TX, RX, 38400); + + while (1) { + got = read(0, local, sizeof(local)); + if (got < 0 && errno == EAGAIN) + got = 0; + if (got < 0) { + perror("read"); + exit(1); + } + if (memchr(local, 3, got)) + break; + got = swuart_trx(local, got, remote, sizeof(remote), + 1000, 100); + for (i = 0; i != got; i++) + if (remote[i] >= ' ' && remote[i] <= '~') + printf("%c", remote[i]); + else if (remote[i] == 13) + printf("\r\n"); + else if (remote[i] == 8 || remote[i] == 127) + printf("\b \b"); + else + printf("\\%02o", remote[i]); + fflush(stdout); + } + printf("\r\n"); + return 0; +} diff --git a/swuart/test.c b/swuart/test.c deleted file mode 100644 index 777bfc6..0000000 --- a/swuart/test.c +++ /dev/null @@ -1,56 +0,0 @@ - -/* - * swuart/test.c - Software-implemented UART for UBB - * - * Written 2012 by Werner Almesberger - * Copyright 2012 Werner Almesberger - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - */ - - -#include -#include -#include -#include - -#include -#include - - -#define RX UBB_DAT0 -#define TX UBB_DAT1 - - -static void at_exit(void) -{ - swuart_close(); -} - - -int main(int argc, char **argv) -{ - uint8_t buf[40]; - struct swuart_err err; - int got, i; - - swuart_open(TX, RX, atoi(argv[1])); - atexit(at_exit); - while (1) { - got = swuart_trx(argv[2], strlen(argv[2]), buf, sizeof(buf), - 10000, 100); - swuart_get_errors(&err); - printf("%d (%d %d %d): ", got, - err.glitch, err.framing, err.overflow); - for (i = 0; i != got; i++) - if (buf[i] >= ' ' && buf[i] <= '~') - printf("%c", buf[i]); - else - printf("\\%02o", buf[i]); - printf("\n"); - usleep(100); - } -}