2015-05-16 08:50:58 +03:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2014-06-08 10:13:29 +04:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2013-2014 Kevin Lange
|
2012-11-29 11:05:19 +04:00
|
|
|
*
|
2015-05-16 08:50:58 +03:00
|
|
|
* serial console
|
|
|
|
*
|
|
|
|
* Runs a dumb console on a serial port or something similar.
|
|
|
|
*
|
2012-11-29 11:05:19 +04:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2015-08-08 05:07:46 +03:00
|
|
|
#include <stdint.h>
|
2012-11-29 11:05:19 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syscall.h>
|
2013-05-06 02:00:24 +04:00
|
|
|
#include <signal.h>
|
2013-11-23 09:54:47 +04:00
|
|
|
#include <termios.h>
|
2014-05-12 04:40:16 +04:00
|
|
|
#include <fcntl.h>
|
2014-04-27 12:37:33 +04:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "lib/pthread.h"
|
2012-11-29 11:05:19 +04:00
|
|
|
|
|
|
|
int fd = 0;
|
|
|
|
|
|
|
|
int child_pid = 0;
|
|
|
|
|
2015-08-21 03:32:42 +03:00
|
|
|
int keep_echo = 0;
|
|
|
|
int dos_lines = 0;
|
|
|
|
int keep_canon = 0;
|
|
|
|
|
2012-11-29 11:05:19 +04:00
|
|
|
void *print_serial_stuff(void * garbage) {
|
|
|
|
child_pid = gettid();
|
2015-08-08 05:07:46 +03:00
|
|
|
|
2012-11-29 11:05:19 +04:00
|
|
|
while (1) {
|
2015-08-08 05:07:46 +03:00
|
|
|
char buf[1024];
|
|
|
|
size_t r = read(fd, buf, 1024);
|
|
|
|
fwrite(buf, 1, r, stdout);
|
2013-04-15 12:26:03 +04:00
|
|
|
fflush(stdout);
|
2012-11-29 11:05:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_exit(garbage);
|
|
|
|
}
|
|
|
|
|
2013-11-23 09:54:47 +04:00
|
|
|
struct termios old;
|
|
|
|
|
|
|
|
void set_unbuffered() {
|
|
|
|
tcgetattr(fileno(stdin), &old);
|
|
|
|
struct termios new = old;
|
2015-08-21 03:32:42 +03:00
|
|
|
if (!keep_canon) {
|
|
|
|
new.c_lflag &= (~ICANON);
|
|
|
|
}
|
|
|
|
if (!keep_echo) {
|
|
|
|
new.c_lflag &= (~ECHO);
|
|
|
|
}
|
2013-11-23 09:54:47 +04:00
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_buffered() {
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
|
|
|
|
}
|
|
|
|
|
2015-08-21 03:32:42 +03:00
|
|
|
int show_usage(int argc, char * argv[]) {
|
|
|
|
printf(
|
|
|
|
"Serial client.\n"
|
|
|
|
"\n"
|
|
|
|
"usage: %s [-e] [-r] [-c] [device path]\n"
|
|
|
|
"\n"
|
|
|
|
" -e \033[3mkeep echo enabled\033[0m\n"
|
|
|
|
" -c \033[3mkeep canon enabled\033[0m\n"
|
|
|
|
" -r \033[3mtransform line feeds to \\r\\n\033[0m\n"
|
|
|
|
" -? \033[3mshow this help text\033[0m\n"
|
|
|
|
"\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-29 11:05:19 +04:00
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
pthread_t receive_thread;
|
|
|
|
pthread_t flush_thread;
|
|
|
|
|
2015-08-21 03:32:42 +03:00
|
|
|
int arg = 1;
|
|
|
|
char * device;
|
|
|
|
|
|
|
|
while (arg < argc) {
|
|
|
|
if (argv[arg][0] != '-') break;
|
|
|
|
if (!strcmp(argv[arg], "-e")) {
|
|
|
|
keep_echo = 1;
|
|
|
|
} else if (!strcmp(argv[arg], "-r")) {
|
|
|
|
dos_lines = 1;
|
|
|
|
} else if (!strcmp(argv[arg], "-c")) {
|
|
|
|
keep_canon = 1;
|
|
|
|
} else if (!strcmp(argv[arg], "-?")) {
|
|
|
|
return show_usage(argc, argv);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: Unrecognized option: %s\n", argv[0], argv[arg]);
|
|
|
|
}
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg == argc) {
|
2013-03-15 11:20:55 +04:00
|
|
|
device = "/dev/ttyS0";
|
2015-08-21 03:32:42 +03:00
|
|
|
} else {
|
|
|
|
device = argv[arg];
|
2012-11-29 11:05:19 +04:00
|
|
|
}
|
|
|
|
|
2013-11-23 09:54:47 +04:00
|
|
|
set_unbuffered();
|
2012-11-29 11:05:19 +04:00
|
|
|
|
2014-05-12 04:40:16 +04:00
|
|
|
fd = open(device, 0, 0);
|
2013-03-15 11:20:55 +04:00
|
|
|
|
2012-11-29 11:05:19 +04:00
|
|
|
pthread_create(&receive_thread, NULL, print_serial_stuff, NULL);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
char c = fgetc(stdin);
|
2015-08-21 03:32:42 +03:00
|
|
|
if (c == 0x1D) { /* ^] */
|
|
|
|
while (1) {
|
|
|
|
printf("serial-console> ");
|
|
|
|
set_buffered();
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
char line[1024];
|
|
|
|
fgets(line, 1024, stdin);
|
|
|
|
|
|
|
|
if (feof(stdin)) {
|
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
printf("Waiting for threads to shut down...\n");
|
|
|
|
while (wait(NULL) != -1);
|
|
|
|
printf("Exiting.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-29 11:05:19 +04:00
|
|
|
|
2015-08-21 03:32:42 +03:00
|
|
|
int i = strlen(line);
|
|
|
|
line[i-1] = '\0';
|
|
|
|
|
|
|
|
if (!strcmp(line, "quit")) {
|
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
printf("Waiting for threads to shut down...\n");
|
|
|
|
while (wait(NULL) != -1);
|
|
|
|
printf("Exiting.\n");
|
|
|
|
return 0;
|
|
|
|
} else if (!strcmp(line, "continue")) {
|
|
|
|
set_unbuffered();
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
2012-11-29 11:05:19 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 03:32:42 +03:00
|
|
|
} else {
|
|
|
|
if (dos_lines && c == '\n') {
|
|
|
|
char buf[1] = {'\r'};
|
|
|
|
write(fd, buf, 1);
|
|
|
|
}
|
|
|
|
char buf[1] = {c};
|
|
|
|
write(fd, buf, 1);
|
2012-11-29 11:05:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2013-11-23 09:54:47 +04:00
|
|
|
set_buffered();
|
2012-11-29 11:05:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|