-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodem-chatter.c
114 lines (96 loc) · 2.65 KB
/
modem-chatter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <sys/select.h>
#include <readline/readline.h>
#include <readline/history.h>
// We begin with \001 and end with \002 to avoid confusing readline.
// If we don't use the \001 and \002, line wrapping won't work:
// http://stackoverflow.com/questions/9468435
#define KRED "\001\x1B[31m\002"
#define KGRN "\001\x1B[32m\002"
#define KNRM "\001\x1B[0m\002"
#define INPUTPROMPT KRED "->|"
#define OUTPUTPROMPT KGRN "<-|"
void process_input(int serialfd, int timeout);
int
main(int argc, char *argv[])
{
struct termios term;
int i;
int serialfd;
char c;
char *device = argv[1];
serialfd = open (device, O_RDWR | O_NOCTTY);
if (serialfd < 0){
error (1, errno, "open");
}
memset (&term, 0, sizeof (term));
term.c_cflag = CREAD | CS8;
term.c_cc[VMIN] = 1;
cfsetispeed (&term, B115200);
cfsetospeed (&term, B115200);
tcsetattr (serialfd, TCSANOW, &term);
/* Flush input buffer. */
int oldfl = fcntl (serialfd, F_GETFL);
fcntl (serialfd, F_SETFL, oldfl | O_NONBLOCK);
while (read (serialfd, &c, 1) != -1);
fcntl (serialfd, F_SETFL, oldfl);
while(1){
char *line = readline(OUTPUTPROMPT);
if(!line){
puts(KNRM);
break;
}
else {
dprintf(serialfd,"%s", line);
add_history(line);
free(line);
}
process_input(serialfd,1);
}
close (serialfd);
return EXIT_SUCCESS;
}
void
process_input(int serialfd, int timeout)
{
char c;
int newline = 1;
//// Initialize file descriptor sets
while(1) {
fd_set read_fds, write_fds, except_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(serialfd, &read_fds);
// Set timeout to 1.0 seconds
struct timeval tv;
tv.tv_sec = timeout; tv.tv_usec = 0;
// Wait for input to become ready or until the time out; the first parameter is
// 1 more than the largest file descriptor in any of the sets
if (select(serialfd + 1, &read_fds, &write_fds, &except_fds, &tv) == 1){
read (serialfd, &c, 1);
if (newline){
newline = 0;
printf(KRED "->|");
}
putchar (c);
if(c == '\n'){
newline = 1;
}
}
else { //we ran into timeout
if (!newline){
putchar('\n');
}
break;
}
}
}