-
Notifications
You must be signed in to change notification settings - Fork 19
/
os_serial_linux.cpp
executable file
·237 lines (208 loc) · 4.91 KB
/
os_serial_linux.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* os_serial_linux.cpp
*
* (C) Copyright 2014 Ulrich Hecht
*
* This file is part of CASCADE. CASCADE is almost free software; you can
* redistribute it and/or modify it under the terms of the Cascade Public
* License 1.0. Read the file "LICENSE" for details.
*/
#include "os.h"
#include "debug.h"
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/serial.h>
#include <dirent.h>
//#define DRYDOCK
int os_serial_open(const char *tty, bool nonblock)
{
#ifdef DRYDOCK
return 2; /* stderr */
#else
int fd;
if (nonblock)
fd = open(tty, O_RDWR|O_NOCTTY|O_NDELAY);
else
fd = open(tty, O_RDWR|O_NOCTTY);
if (fd < 0) {
ERROR("OS failed to open serial device %s: %s\n", tty, strerror(errno));
return -1;
}
/* put TTY in raw mode */
struct termios tio;
tcgetattr(fd, &tio);
cfmakeraw(&tio);
tcsetattr(fd, TCSANOW, &tio);
DEBUG(OS, "OS serial flush after open\n");
tcflush(fd, TCIOFLUSH); /* flush stale serial buffers */
return fd;
#endif
}
int os_serial_close(int handle)
{
return close(handle);
}
int os_serial_send(int handle, const char *msg)
{
DEBUG(OS, "OS serial send %s\n", msg);
return write(handle, msg, strlen(msg));
}
int os_serial_send_buf(int handle, const unsigned char *buf, int len)
{
DEBUG(OS, "OS serial send buffer, length %d\n", len);
return write(handle, buf, len);
}
int os_serial_send_byte(int handle, char byte)
{
DEBUG(OS, "OS serial send byte %02X\n", byte);
return write(handle, &byte, 1);
}
int os_serial_bytes_received(int fd)
{
int bytes = 0;
ioctl(fd, FIONREAD, &bytes);
return bytes;
}
int os_serial_read_to_buffer(int fd, void *buf, int count)
{
return read(fd, buf, count);
}
int os_serial_flush(int fd)
{
DEBUG(OS, "OS serial flush\n");
return tcflush(fd, TCIOFLUSH);
}
int os_serial_set_baudrate(int fd, int baudrate)
{
struct termios tios;
if (tcgetattr(fd, &tios) < 0) {
ERROR("tcgetattr failed\n");
return -1;
}
struct serial_struct ser;
if (ioctl(fd, TIOCGSERIAL, &ser)) {
ERROR("TIOCGSERIAL failed\n");
return -1;
}
ser.custom_divisor = ser.baud_base / baudrate;
ser.flags &= ~ASYNC_SPD_MASK;
ser.flags |= ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
tios.c_cflag &= ~CBAUD;
tios.c_cflag |= B38400;
if (ioctl(fd, TIOCSSERIAL, &ser)) {
ERROR("TIOCSSERIAL failed\n");
return -1;
}
if (ioctl(fd, TIOCGSERIAL, &ser)) {
ERROR("TIOCGSERIAL failed\n");
return -1;
}
if (ser.custom_divisor != ser.baud_base / baudrate) {
ERROR("failed to set baudrate divisor, is %d, should be %d\n", ser.custom_divisor, ser.baud_base / baudrate);
}
if (tcsetattr(fd, TCSANOW, &tios) < 0) {
ERROR("tcsetattr failed\n");
return -1;
}
return 0;
}
int os_serial_clear_break(int fd)
{
if (ioctl(fd, TIOCCBRK) < 0) {
ERROR("TIOCCBRK failed\n");
return -1;
}
return 0;
}
int os_serial_set_break(int fd)
{
if (ioctl(fd, TIOCSBRK) < 0) {
ERROR("TIOCSBRK failed\n");
return -1;
}
return 0;
}
int os_serial_set_rts(int fd)
{
int flags;
if (ioctl(fd, TIOCMGET, &flags)) {
ERROR("CMBIC: %s\n", strerror(errno));
return -1;
}
flags |= TIOCM_RTS;
return ioctl(fd, TIOCMSET, &flags);
}
int os_serial_clear_rts(int fd)
{
int flags;
if (ioctl(fd, TIOCMGET, &flags)) {
ERROR("CMBIC: %s\n", strerror(errno));
return -1;
}
flags &= ~TIOCM_RTS;
return ioctl(fd, TIOCMSET, &flags);
}
int os_serial_set_dtr(int fd)
{
int flags;
if (ioctl(fd, TIOCMGET, &flags)) {
ERROR("CMBIC: %s\n", strerror(errno));
return -1;
}
flags |= TIOCM_DTR;
return ioctl(fd, TIOCMSET, &flags);
}
int os_serial_clear_dtr(int fd)
{
int flags;
if (ioctl(fd, TIOCMGET, &flags)) {
ERROR("CMBIC: %s\n", strerror(errno));
return -1;
}
flags &= ~TIOCM_DTR;
return ioctl(fd, TIOCMSET, &flags);
}
const char *os_serial_get_error(void)
{
return strerror(errno);
}
#define SYS_DRIVER_PATH "/sys/bus/usb-serial/drivers/"
int os_serial_find_port(const char *driver, char **tty_found)
{
int sh;
char *sysfs_dir = new char[sizeof(SYS_DRIVER_PATH) + strlen(driver)];
sprintf(sysfs_dir, SYS_DRIVER_PATH "%s", driver);
DEBUG(IFACE, "checking %s for tty\n", sysfs_dir);
for (;;) {
DIR *dir = opendir(sysfs_dir);
if (!dir) {
os_msleep(300);
continue;
}
struct dirent *ent;
while ((ent = readdir(dir))) {
if (!strncmp("ttyUSB", ent->d_name, 6)) {
char *tty = new char[sizeof("/dev/") + strlen(ent->d_name)];
sprintf(tty, "/dev/%s", ent->d_name);
DEBUG(IFACE, "trying to open %s\n", tty);
sh = os_serial_open(tty, false);
if (sh >= 0 && tty_found)
*tty_found = strdup(tty);
delete tty;
if (sh >= 0) {
closedir(dir);
delete sysfs_dir;
return sh;
}
}
}
closedir(dir);
os_msleep(300);
}
}