]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <string.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <unistd.h> | |
25 | #ifdef _WIN32 | |
26 | #include <windows.h> | |
27 | #else | |
28 | #include <glob.h> | |
29 | #include <termios.h> | |
30 | #endif | |
31 | #include <stdlib.h> | |
32 | #include <glib.h> | |
33 | #include "libsigrok.h" | |
34 | #include "libsigrok-internal.h" | |
35 | ||
36 | // FIXME: Must be moved, or rather passed as function argument. | |
37 | #ifdef _WIN32 | |
38 | static HANDLE hdl; | |
39 | #endif | |
40 | ||
41 | static const char *serial_port_glob[] = { | |
42 | /* Linux */ | |
43 | "/dev/ttyS*", | |
44 | "/dev/ttyUSB*", | |
45 | "/dev/ttyACM*", | |
46 | /* MacOS X */ | |
47 | "/dev/ttys*", | |
48 | "/dev/tty.USB-*", | |
49 | "/dev/tty.Modem-*", | |
50 | NULL, | |
51 | }; | |
52 | ||
53 | SR_PRIV GSList *list_serial_ports(void) | |
54 | { | |
55 | GSList *ports; | |
56 | ||
57 | #ifdef _WIN32 | |
58 | /* TODO */ | |
59 | ports = NULL; | |
60 | ports = g_slist_append(ports, g_strdup("COM1")); | |
61 | #else | |
62 | glob_t g; | |
63 | unsigned int i, j; | |
64 | ||
65 | ports = NULL; | |
66 | for (i = 0; serial_port_glob[i]; i++) { | |
67 | if (glob(serial_port_glob[i], 0, NULL, &g)) | |
68 | continue; | |
69 | for (j = 0; j < g.gl_pathc; j++) | |
70 | ports = g_slist_append(ports, g_strdup(g.gl_pathv[j])); | |
71 | globfree(&g); | |
72 | } | |
73 | #endif | |
74 | ||
75 | return ports; | |
76 | } | |
77 | ||
78 | SR_PRIV int serial_open(const char *pathname, int flags) | |
79 | { | |
80 | #ifdef _WIN32 | |
81 | /* FIXME: Don't hardcode COM1. */ | |
82 | hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, | |
83 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
84 | if (hdl == INVALID_HANDLE_VALUE) { | |
85 | /* TODO: Error handling. */ | |
86 | } | |
87 | return 0; | |
88 | #else | |
89 | return open(pathname, flags); | |
90 | #endif | |
91 | } | |
92 | ||
93 | /* | |
94 | * Close the serial port. | |
95 | * Returns 0 upon success, -1 upon failure. | |
96 | */ | |
97 | SR_PRIV int serial_close(int fd) | |
98 | { | |
99 | #ifdef _WIN32 | |
100 | /* Returns non-zero upon success, 0 upon failure. */ | |
101 | return (CloseHandle(hdl) == 0) ? -1 : 0; | |
102 | #else | |
103 | /* Returns 0 upon success, -1 upon failure. */ | |
104 | return close(fd); | |
105 | #endif | |
106 | } | |
107 | ||
108 | /* | |
109 | * Flush serial port buffers (if any). | |
110 | * Returns 0 upon success, -1 upon failure. | |
111 | */ | |
112 | SR_PRIV int serial_flush(int fd) | |
113 | { | |
114 | #ifdef _WIN32 | |
115 | /* Returns non-zero upon success, 0 upon failure. */ | |
116 | return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0; | |
117 | #else | |
118 | /* Returns 0 upon success, -1 upon failure. */ | |
119 | return tcflush(fd, TCIOFLUSH); | |
120 | #endif | |
121 | } | |
122 | ||
123 | /* | |
124 | * Write a number of bytes to the specified serial port. | |
125 | * Returns the number of bytes written, or -1 upon failure. | |
126 | */ | |
127 | SR_PRIV int serial_write(int fd, const void *buf, size_t count) | |
128 | { | |
129 | #ifdef _WIN32 | |
130 | DWORD tmp = 0; | |
131 | ||
132 | /* FIXME */ | |
133 | /* Returns non-zero upon success, 0 upon failure. */ | |
134 | WriteFile(hdl, buf, count, &tmp, NULL); | |
135 | #else | |
136 | /* Returns the number of bytes written, or -1 upon failure. */ | |
137 | return write(fd, buf, count); | |
138 | #endif | |
139 | } | |
140 | ||
141 | /* | |
142 | * Read a number of bytes from the specified serial port. | |
143 | * Returns the number of bytes read, or -1 upon failure. | |
144 | */ | |
145 | SR_PRIV int serial_read(int fd, void *buf, size_t count) | |
146 | { | |
147 | #ifdef _WIN32 | |
148 | DWORD tmp = 0; | |
149 | ||
150 | /* FIXME */ | |
151 | /* Returns non-zero upon success, 0 upon failure. */ | |
152 | return ReadFile(hdl, buf, count, &tmp, NULL); | |
153 | #else | |
154 | /* Returns the number of bytes read, or -1 upon failure. */ | |
155 | return read(fd, buf, count); | |
156 | #endif | |
157 | } | |
158 | ||
159 | SR_PRIV void *serial_backup_params(int fd) | |
160 | { | |
161 | #ifdef _WIN32 | |
162 | /* TODO */ | |
163 | #else | |
164 | struct termios *term; | |
165 | ||
166 | /* TODO: 'term' is never g_free()'d? */ | |
167 | if (!(term = g_try_malloc(sizeof(struct termios)))) { | |
168 | sr_err("serial: %s: term malloc failed", __func__); | |
169 | return NULL; | |
170 | } | |
171 | ||
172 | tcgetattr(fd, term); | |
173 | ||
174 | return term; | |
175 | #endif | |
176 | } | |
177 | ||
178 | SR_PRIV void serial_restore_params(int fd, void *backup) | |
179 | { | |
180 | #ifdef _WIN32 | |
181 | /* TODO */ | |
182 | #else | |
183 | tcsetattr(fd, TCSADRAIN, (struct termios *)backup); | |
184 | #endif | |
185 | } | |
186 | ||
187 | /* | |
188 | * Set serial parameters. | |
189 | * | |
190 | * flowcontrol: 1 = rts/cts, 2 = xon/xoff | |
191 | * parity: 0 = none, 1 = even, 2 = odd | |
192 | */ | |
193 | SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, | |
194 | int stopbits, int flowcontrol) | |
195 | { | |
196 | #ifdef _WIN32 | |
197 | DCB dcb; | |
198 | ||
199 | if (!GetCommState(hdl, &dcb)) { | |
200 | /* TODO: Error handling. */ | |
201 | return SR_ERR; | |
202 | } | |
203 | ||
204 | switch (baudrate) { | |
205 | /* TODO: Support for higher baud rates. */ | |
206 | case 115200: | |
207 | dcb.BaudRate = CBR_115200; | |
208 | break; | |
209 | case 57600: | |
210 | dcb.BaudRate = CBR_57600; | |
211 | break; | |
212 | case 38400: | |
213 | dcb.BaudRate = CBR_38400; | |
214 | break; | |
215 | case 19200: | |
216 | dcb.BaudRate = CBR_19200; | |
217 | break; | |
218 | case 9600: | |
219 | dcb.BaudRate = CBR_9600; | |
220 | break; | |
221 | case 4800: | |
222 | dcb.BaudRate = CBR_4800; | |
223 | break; | |
224 | default: | |
225 | /* TODO: Error handling. */ | |
226 | break; | |
227 | } | |
228 | dcb.ByteSize = bits; | |
229 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
230 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
231 | ||
232 | if (!SetCommState(hdl, &dcb)) { | |
233 | /* TODO: Error handling. */ | |
234 | return SR_ERR; | |
235 | } | |
236 | #else | |
237 | struct termios term; | |
238 | speed_t baud; | |
239 | ||
240 | if (tcgetattr(fd, &term) < 0) | |
241 | return SR_ERR; | |
242 | ||
243 | switch (baudrate) { | |
244 | case 4800: | |
245 | baud = B4800; | |
246 | break; | |
247 | case 9600: | |
248 | baud = B9600; | |
249 | break; | |
250 | case 38400: | |
251 | baud = B38400; | |
252 | break; | |
253 | case 57600: | |
254 | baud = B57600; | |
255 | break; | |
256 | case 115200: | |
257 | baud = B115200; | |
258 | break; | |
259 | #ifndef __APPLE__ | |
260 | case 460800: | |
261 | baud = B460800; | |
262 | break; | |
263 | #endif | |
264 | default: | |
265 | return SR_ERR; | |
266 | } | |
267 | if (cfsetospeed(&term, baud) < 0) | |
268 | return SR_ERR; | |
269 | if (cfsetispeed(&term, baud) < 0) | |
270 | return SR_ERR; | |
271 | ||
272 | term.c_cflag &= ~CSIZE; | |
273 | switch (bits) { | |
274 | case 8: | |
275 | term.c_cflag |= CS8; | |
276 | break; | |
277 | case 7: | |
278 | term.c_cflag |= CS7; | |
279 | break; | |
280 | default: | |
281 | return SR_ERR; | |
282 | } | |
283 | ||
284 | term.c_cflag &= ~CSTOPB; | |
285 | switch (stopbits) { | |
286 | case 1: | |
287 | break; | |
288 | case 2: | |
289 | term.c_cflag |= CSTOPB; | |
290 | default: | |
291 | return SR_ERR; | |
292 | } | |
293 | ||
294 | term.c_iflag &= ~(IXON | IXOFF); | |
295 | term.c_cflag &= ~CRTSCTS; | |
296 | switch (flowcontrol) { | |
297 | case 0: | |
298 | /* No flow control. */ | |
299 | break; | |
300 | case 1: | |
301 | term.c_cflag |= CRTSCTS; | |
302 | case 2: | |
303 | term.c_iflag |= IXON | IXOFF; | |
304 | break; | |
305 | default: | |
306 | return SR_ERR; | |
307 | } | |
308 | ||
309 | term.c_iflag &= ~IGNPAR; | |
310 | term.c_cflag &= ~(PARODD | PARENB); | |
311 | switch (parity) { | |
312 | case SERIAL_PARITY_NONE: | |
313 | term.c_iflag |= IGNPAR; | |
314 | break; | |
315 | case SERIAL_PARITY_EVEN: | |
316 | term.c_cflag |= PARENB; | |
317 | break; | |
318 | case SERIAL_PARITY_ODD: | |
319 | term.c_cflag |= PARENB | PARODD; | |
320 | break; | |
321 | default: | |
322 | return SR_ERR; | |
323 | } | |
324 | ||
325 | /* Some default parameters */ | |
326 | term.c_iflag &= ~(ICRNL); | |
327 | term.c_lflag &= ~(ICANON | ECHO); | |
328 | ||
329 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) | |
330 | return SR_ERR; | |
331 | #endif | |
332 | ||
333 | return SR_OK; | |
334 | } | |
335 | ||
336 | #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$" | |
337 | SR_PRIV int serial_set_paramstr(int fd, const char *paramstr) | |
338 | { | |
339 | GRegex *reg; | |
340 | GMatchInfo *match; | |
341 | int speed, databits, parity, stopbits; | |
342 | char *mstr; | |
343 | ||
344 | speed = databits = parity = stopbits = 0; | |
345 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); | |
346 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
347 | if ((mstr = g_match_info_fetch(match, 1))) | |
348 | speed = strtoul(mstr, NULL, 10); | |
349 | g_free(mstr); | |
350 | if ((mstr = g_match_info_fetch(match, 2))) | |
351 | databits = strtoul(mstr, NULL, 10); | |
352 | g_free(mstr); | |
353 | if ((mstr = g_match_info_fetch(match, 3))) { | |
354 | switch (mstr[0]) { | |
355 | case 'n': | |
356 | parity = SERIAL_PARITY_NONE; | |
357 | break; | |
358 | case 'e': | |
359 | parity = SERIAL_PARITY_EVEN; | |
360 | break; | |
361 | case 'o': | |
362 | parity = SERIAL_PARITY_ODD; | |
363 | break; | |
364 | } | |
365 | } | |
366 | g_free(mstr); | |
367 | if ((mstr = g_match_info_fetch(match, 4))) | |
368 | stopbits = strtoul(mstr, NULL, 10); | |
369 | g_free(mstr); | |
370 | } | |
371 | g_match_info_unref(match); | |
372 | g_regex_unref(reg); | |
373 | ||
374 | if (speed) | |
375 | return serial_set_params(fd, speed, databits, parity, stopbits, 0); | |
376 | else | |
377 | return SR_ERR_ARG; | |
378 | } | |
379 |