forked from hzeller/rpt2pnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine-connection.cc
152 lines (136 loc) · 4.28 KB
/
machine-connection.cc
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
* (c) [email protected]. Free Software. GNU Public License v3.0 and above
*/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <string>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
static bool SetTTYParams(int fd, const char *params) {
speed_t speed = B115200;
if (params[0] == 'b' || params[0] == 'B')
params = params + 1;
if (*params) {
int speed_number = atoi(params);
switch (speed_number) {
case 9600: speed = B9600; break;
case 19200: speed = B19200; break;
case 38400: speed = B38400; break;
case 57600: speed = B57600; break;
case 115200: speed = B115200; break;
case 230400: speed = B230400; break;
case 460800: speed = B460800; break;
default:
fprintf(stderr, "Invalid speed '%s'; valid speeds are "
"[9600, 19200, 38400, 57600, 115200, 230400, 460800]\n",
params);
return false;
break;
}
}
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
perror("tcgetattr() failed. Is this a tty ?");
return false;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
tty.c_cflag |= (CLOCAL | CREAD); // no modem controls
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8
tty.c_cflag &= ~PARENB; // N
tty.c_cflag &= ~CSTOPB; // 1
tty.c_cflag &= ~CRTSCTS; // No hardware flow-control
// terminal magic. Non-canonical mode
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return false;
}
return true;
}
// Wait for input to become ready for read or timeout reached.
// If the file-descriptor becomes readable, returns number of milli-seconds
// left.
// Returns 0 on timeout (i.e. no millis left and nothing to be read).
// Returns -1 on error.
static int AwaitReadReady(int fd, int timeout_millis) {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = timeout_millis * 1000;
FD_SET(fd, &read_fds);
int s = select(fd + 1, &read_fds, NULL, NULL, &tv);
if (s < 0)
return -1;
return tv.tv_usec / 1000;
}
static int ReadLine(int fd, char *result, int len, bool do_echo) {
int bytes_read = 0;
char c = 0;
while (c != '\n' && c != '\r' && bytes_read < len) {
if (read(fd, &c, 1) < 0)
return -1;
++bytes_read;
*result++ = c;
if (do_echo) write(STDERR_FILENO, &c, 1); // echo back.
}
*result = '\0';
return bytes_read;
}
/*
*
* Public interface functions
*
*/
int OpenMachineConnection(const char *descriptor) {
if (descriptor == nullptr) return -1;
const char *comma = strchrnul(descriptor, ',');
const std::string path(descriptor, comma);
int fd = open(path.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
fprintf(stderr, "Opening %s: %s\n", path.c_str(), strerror(errno));
return -1;
}
if (!SetTTYParams(fd, *comma ? comma+1 : "")) {
return -1;
}
return fd;
}
int DiscardPendingInput(int fd, int timeout_ms) {
if (fd < 0) return 0;
int total_bytes = 0;
char buf[128];
while (AwaitReadReady(fd, timeout_ms) > 0) {
int r = read(fd, buf, sizeof(buf));
if (r < 0) {
perror("reading trouble");
return -1;
}
total_bytes += r;
if (r > 0) write(STDERR_FILENO, buf, r); // echo back.
}
return total_bytes;
}
// 'ok' comes on a single line, maybe followed by something.
void WaitForOkAck(int fd) {
char buffer[512];
for (;;) {
if (ReadLine(fd, buffer, sizeof(buffer), false) < 0)
break;
if (strncasecmp(buffer, "ok", 2) == 0)
break;
// If we didn't get 'ok', it might be an important error message. Print.
fprintf(stderr, "%s", buffer);
}
}