]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
b19f4622 | 5 | * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de> |
766456be | 6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
a1bb33af UH |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
54b38f64 | 22 | #include <string.h> |
d02a535e | 23 | #include <stdlib.h> |
a1bb33af | 24 | #include <glib.h> |
b541f837 | 25 | #include <glib/gstdio.h> |
0d4405ce | 26 | #include <libserialport.h> |
45c59c8b BV |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
a1bb33af | 29 | |
3544f848 | 30 | #define LOG_PREFIX "serial" |
b19f4622 | 31 | |
b19f4622 UH |
32 | /** |
33 | * Open the specified serial port. | |
34 | * | |
299bdb24 | 35 | * @param serial Previously initialized serial port structure. |
a9cf2035 | 36 | * @param[in] flags Flags to use when opening the serial port. Possible flags |
98edee79 | 37 | * include SERIAL_RDWR, SERIAL_RDONLY. |
b19f4622 | 38 | * |
299bdb24 BV |
39 | * If the serial structure contains a serialcomm string, it will be |
40 | * passed to serial_set_paramstr() after the port is opened. | |
41 | * | |
d0a92abd MH |
42 | * @retval SR_OK Success. |
43 | * @retval SR_ERR Failure. | |
b19f4622 | 44 | */ |
299bdb24 | 45 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) |
d02a535e | 46 | { |
a9bce5a5 ML |
47 | int ret; |
48 | char *error; | |
13cd8197 | 49 | int sp_flags = 0; |
b19f4622 | 50 | |
299bdb24 BV |
51 | if (!serial) { |
52 | sr_dbg("Invalid serial port."); | |
53 | return SR_ERR; | |
54 | } | |
55 | ||
56 | sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags); | |
b19f4622 | 57 | |
c9bc57b6 ML |
58 | sp_get_port_by_name(serial->port, &serial->data); |
59 | ||
13cd8197 ML |
60 | if (flags & SERIAL_RDWR) |
61 | sp_flags = (SP_MODE_READ | SP_MODE_WRITE); | |
62 | else if (flags & SERIAL_RDONLY) | |
63 | sp_flags = SP_MODE_READ; | |
9647ce69 | 64 | |
13cd8197 | 65 | ret = sp_open(serial->data, sp_flags); |
a9bce5a5 | 66 | |
a0dfaa6c UH |
67 | switch (ret) { |
68 | case SP_ERR_ARG: | |
69 | sr_err("Attempt to open serial port with invalid parameters."); | |
70 | return SR_ERR_ARG; | |
71 | case SP_ERR_FAIL: | |
72 | error = sp_last_error_message(); | |
cb7b165b UH |
73 | sr_err("Error opening port (%d): %s.", |
74 | sp_last_error_code(), error); | |
a0dfaa6c UH |
75 | sp_free_error_message(error); |
76 | return SR_ERR; | |
a54dd31e UH |
77 | } |
78 | ||
299bdb24 BV |
79 | if (serial->serialcomm) |
80 | return serial_set_paramstr(serial, serial->serialcomm); | |
81 | else | |
82 | return SR_OK; | |
d02a535e BV |
83 | } |
84 | ||
b19f4622 UH |
85 | /** |
86 | * Close the specified serial port. | |
87 | * | |
299bdb24 | 88 | * @param serial Previously initialized serial port structure. |
b19f4622 | 89 | * |
d0a92abd MH |
90 | * @retval SR_OK Success. |
91 | * @retval SR_ERR Failure. | |
2119ab03 | 92 | */ |
299bdb24 | 93 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) |
d02a535e | 94 | { |
299bdb24 | 95 | int ret; |
a9bce5a5 | 96 | char *error; |
299bdb24 BV |
97 | |
98 | if (!serial) { | |
99 | sr_dbg("Invalid serial port."); | |
100 | return SR_ERR; | |
101 | } | |
102 | ||
64ecf7ee ML |
103 | if (!serial->data) { |
104 | sr_dbg("Cannot close unopened serial port %s.", serial->port); | |
299bdb24 BV |
105 | return SR_ERR; |
106 | } | |
107 | ||
64ecf7ee | 108 | sr_spew("Closing serial port %s.", serial->port); |
b19f4622 | 109 | |
c9bc57b6 | 110 | ret = sp_close(serial->data); |
a9bce5a5 | 111 | |
a0dfaa6c UH |
112 | switch (ret) { |
113 | case SP_ERR_ARG: | |
114 | sr_err("Attempt to close an invalid serial port."); | |
115 | return SR_ERR_ARG; | |
116 | case SP_ERR_FAIL: | |
117 | error = sp_last_error_message(); | |
cb7b165b UH |
118 | sr_err("Error closing port (%d): %s.", |
119 | sp_last_error_code(), error); | |
a0dfaa6c UH |
120 | sp_free_error_message(error); |
121 | return SR_ERR; | |
b19f4622 | 122 | } |
299bdb24 | 123 | |
64ecf7ee ML |
124 | sp_free_port(serial->data); |
125 | serial->data = NULL; | |
126 | ||
6a76efeb | 127 | return SR_OK; |
d02a535e BV |
128 | } |
129 | ||
b19f4622 | 130 | /** |
299bdb24 | 131 | * Flush serial port buffers. |
b19f4622 | 132 | * |
299bdb24 | 133 | * @param serial Previously initialized serial port structure. |
b19f4622 | 134 | * |
d0a92abd MH |
135 | * @retval SR_OK Success. |
136 | * @retval SR_ERR Failure. | |
1fdb75e1 | 137 | */ |
299bdb24 | 138 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) |
06d64eb8 | 139 | { |
299bdb24 | 140 | int ret; |
a9bce5a5 | 141 | char *error; |
299bdb24 BV |
142 | |
143 | if (!serial) { | |
144 | sr_dbg("Invalid serial port."); | |
145 | return SR_ERR; | |
146 | } | |
147 | ||
64ecf7ee ML |
148 | if (!serial->data) { |
149 | sr_dbg("Cannot flush unopened serial port %s.", serial->port); | |
299bdb24 BV |
150 | return SR_ERR; |
151 | } | |
152 | ||
64ecf7ee | 153 | sr_spew("Flushing serial port %s.", serial->port); |
b19f4622 | 154 | |
13cd8197 | 155 | ret = sp_flush(serial->data, SP_BUF_BOTH); |
a9bce5a5 | 156 | |
a0dfaa6c UH |
157 | switch (ret) { |
158 | case SP_ERR_ARG: | |
159 | sr_err("Attempt to flush an invalid serial port."); | |
160 | return SR_ERR_ARG; | |
161 | case SP_ERR_FAIL: | |
162 | error = sp_last_error_message(); | |
cb7b165b UH |
163 | sr_err("Error flushing port (%d): %s.", |
164 | sp_last_error_code(), error); | |
a0dfaa6c UH |
165 | sp_free_error_message(error); |
166 | return SR_ERR; | |
299bdb24 | 167 | } |
b19f4622 | 168 | |
6a76efeb | 169 | return SR_OK; |
06d64eb8 BV |
170 | } |
171 | ||
9a474211 ML |
172 | static int _serial_write(struct sr_serial_dev_inst *serial, |
173 | const void *buf, size_t count, int nonblocking) | |
2119ab03 | 174 | { |
299bdb24 | 175 | ssize_t ret; |
a9bce5a5 | 176 | char *error; |
299bdb24 BV |
177 | |
178 | if (!serial) { | |
179 | sr_dbg("Invalid serial port."); | |
6a76efeb | 180 | return SR_ERR; |
299bdb24 BV |
181 | } |
182 | ||
64ecf7ee ML |
183 | if (!serial->data) { |
184 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
6a76efeb | 185 | return SR_ERR; |
299bdb24 BV |
186 | } |
187 | ||
9a474211 | 188 | if (nonblocking) |
9647ce69 ML |
189 | ret = sp_nonblocking_write(serial->data, buf, count); |
190 | else | |
191 | ret = sp_blocking_write(serial->data, buf, count, 0); | |
a9bce5a5 | 192 | |
a0dfaa6c UH |
193 | switch (ret) { |
194 | case SP_ERR_ARG: | |
195 | sr_err("Attempted serial port write with invalid arguments."); | |
196 | return SR_ERR_ARG; | |
197 | case SP_ERR_FAIL: | |
198 | error = sp_last_error_message(); | |
cb7b165b | 199 | sr_err("Write error (%d): %s.", sp_last_error_code(), error); |
a0dfaa6c UH |
200 | sp_free_error_message(error); |
201 | return SR_ERR; | |
a9bce5a5 ML |
202 | } |
203 | ||
64ecf7ee | 204 | sr_spew("Wrote %d/%d bytes.", ret, count); |
b19f4622 UH |
205 | |
206 | return ret; | |
2119ab03 UH |
207 | } |
208 | ||
b19f4622 | 209 | /** |
98edee79 | 210 | * Write a number of bytes to the specified serial port, blocking until finished. |
b19f4622 | 211 | * |
299bdb24 | 212 | * @param serial Previously initialized serial port structure. |
a9cf2035 MH |
213 | * @param[in] buf Buffer containing the bytes to write. |
214 | * @param[in] count Number of bytes to write. | |
b19f4622 | 215 | * |
a9cf2035 MH |
216 | * @retval SR_ERR_ARG Invalid argument. |
217 | * @retval SR_ERR Other error. | |
218 | * @retval other The number of bytes written. | |
2119ab03 | 219 | */ |
9a474211 ML |
220 | SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial, |
221 | const void *buf, size_t count) | |
222 | { | |
223 | return _serial_write(serial, buf, count, 0); | |
224 | } | |
225 | ||
a9cf2035 MH |
226 | /** |
227 | * Write a number of bytes to the specified serial port, return immediately. | |
98edee79 ML |
228 | * |
229 | * @param serial Previously initialized serial port structure. | |
230 | * @param[in] buf Buffer containing the bytes to write. | |
231 | * @param[in] count Number of bytes to write. | |
232 | * | |
233 | * @retval SR_ERR_ARG Invalid argument. | |
234 | * @retval SR_ERR Other error. | |
235 | * @retval other The number of bytes written. | |
a9cf2035 | 236 | */ |
9a474211 ML |
237 | SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial, |
238 | const void *buf, size_t count) | |
239 | { | |
240 | return _serial_write(serial, buf, count, 1); | |
241 | } | |
242 | ||
243 | static int _serial_read(struct sr_serial_dev_inst *serial, void *buf, | |
244 | size_t count, int nonblocking) | |
2119ab03 | 245 | { |
299bdb24 | 246 | ssize_t ret; |
c4d85a40 | 247 | char *error; |
299bdb24 BV |
248 | |
249 | if (!serial) { | |
250 | sr_dbg("Invalid serial port."); | |
6a76efeb | 251 | return SR_ERR; |
299bdb24 BV |
252 | } |
253 | ||
64ecf7ee ML |
254 | if (!serial->data) { |
255 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
6a76efeb | 256 | return SR_ERR; |
299bdb24 BV |
257 | } |
258 | ||
9a474211 | 259 | if (nonblocking) |
9647ce69 ML |
260 | ret = sp_nonblocking_read(serial->data, buf, count); |
261 | else | |
262 | ret = sp_blocking_read(serial->data, buf, count, 0); | |
2119ab03 | 263 | |
a0dfaa6c UH |
264 | switch (ret) { |
265 | case SP_ERR_ARG: | |
266 | sr_err("Attempted serial port read with invalid arguments."); | |
267 | return SR_ERR_ARG; | |
c4d85a40 UH |
268 | case SP_ERR_FAIL: |
269 | error = sp_last_error_message(); | |
cb7b165b | 270 | sr_err("Read error (%d): %s.", sp_last_error_code(), error); |
c4d85a40 UH |
271 | sp_free_error_message(error); |
272 | return SR_ERR; | |
a9bce5a5 ML |
273 | } |
274 | ||
c4d85a40 | 275 | if (ret > 0) |
64ecf7ee | 276 | sr_spew("Read %d/%d bytes.", ret, count); |
b19f4622 UH |
277 | |
278 | return ret; | |
2119ab03 UH |
279 | } |
280 | ||
9a474211 | 281 | /** |
98edee79 | 282 | * Read a number of bytes from the specified serial port, block until finished. |
9a474211 ML |
283 | * |
284 | * @param serial Previously initialized serial port structure. | |
285 | * @param buf Buffer where to store the bytes that are read. | |
a9cf2035 | 286 | * @param[in] count The number of bytes to read. |
9a474211 | 287 | * |
a9cf2035 MH |
288 | * @retval SR_ERR_ARG Invalid argument. |
289 | * @retval SR_ERR Other error. | |
290 | * @retval other The number of bytes read. | |
9a474211 | 291 | */ |
9a474211 ML |
292 | SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf, |
293 | size_t count) | |
294 | { | |
295 | return _serial_read(serial, buf, count, 0); | |
296 | } | |
297 | ||
a9cf2035 MH |
298 | /** |
299 | * Try to read up to @a count bytes from the specified serial port, return | |
300 | * immediately with what's available. | |
98edee79 ML |
301 | * |
302 | * @param serial Previously initialized serial port structure. | |
303 | * @param buf Buffer where to store the bytes that are read. | |
304 | * @param[in] count The number of bytes to read. | |
305 | * | |
306 | * @retval SR_ERR_ARG Invalid argument. | |
307 | * @retval SR_ERR Other error. | |
308 | * @retval other The number of bytes read. | |
a9cf2035 | 309 | */ |
9a474211 ML |
310 | SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf, |
311 | size_t count) | |
312 | { | |
313 | return _serial_read(serial, buf, count, 1); | |
314 | } | |
315 | ||
b19f4622 UH |
316 | /** |
317 | * Set serial parameters for the specified serial port. | |
318 | * | |
299bdb24 | 319 | * @param serial Previously initialized serial port structure. |
04cb9157 MH |
320 | * @param[in] baudrate The baudrate to set. |
321 | * @param[in] bits The number of data bits to use (5, 6, 7 or 8). | |
322 | * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
323 | * @param[in] stopbits The number of stop bits to use (1 or 2). | |
324 | * @param[in] flowcontrol The flow control settings to use (0 = none, | |
325 | * 1 = RTS/CTS, 2 = XON/XOFF). | |
326 | * @param[in] rts Status of RTS line (0 or 1; required by some interfaces). | |
327 | * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces). | |
2119ab03 | 328 | * |
d0a92abd | 329 | * @retval SR_OK Success. |
04cb9157 | 330 | * @retval SR_ERR Failure. |
1ff7712c | 331 | */ |
299bdb24 | 332 | SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, |
39e5d798 UH |
333 | int bits, int parity, int stopbits, |
334 | int flowcontrol, int rts, int dtr) | |
d02a535e | 335 | { |
a9bce5a5 ML |
336 | int ret; |
337 | char *error; | |
25b66c3c | 338 | struct sp_port_config *config; |
a9bce5a5 | 339 | |
299bdb24 BV |
340 | if (!serial) { |
341 | sr_dbg("Invalid serial port."); | |
342 | return SR_ERR; | |
343 | } | |
344 | ||
64ecf7ee ML |
345 | if (!serial->data) { |
346 | sr_dbg("Cannot configure unopened serial port %s.", serial->port); | |
299bdb24 BV |
347 | return SR_ERR; |
348 | } | |
349 | ||
64ecf7ee | 350 | sr_spew("Setting serial parameters on port %s.", serial->port); |
b19f4622 | 351 | |
25b66c3c ML |
352 | sp_new_config(&config); |
353 | sp_set_config_baudrate(config, baudrate); | |
354 | sp_set_config_bits(config, bits); | |
13cd8197 ML |
355 | switch (parity) { |
356 | case 0: | |
25b66c3c | 357 | sp_set_config_parity(config, SP_PARITY_NONE); |
13cd8197 ML |
358 | break; |
359 | case 1: | |
25b66c3c | 360 | sp_set_config_parity(config, SP_PARITY_EVEN); |
13cd8197 ML |
361 | break; |
362 | case 2: | |
25b66c3c | 363 | sp_set_config_parity(config, SP_PARITY_ODD); |
13cd8197 ML |
364 | break; |
365 | default: | |
366 | return SR_ERR_ARG; | |
367 | } | |
25b66c3c ML |
368 | sp_set_config_stopbits(config, stopbits); |
369 | sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts); | |
370 | sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE); | |
371 | sp_set_config_dtr(config, dtr); | |
372 | sp_set_config_dsr(config, SP_DSR_IGNORE); | |
373 | sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED); | |
374 | ||
375 | ret = sp_set_config(serial->data, config); | |
376 | sp_free_config(config); | |
a9bce5a5 | 377 | |
a0dfaa6c UH |
378 | switch (ret) { |
379 | case SP_ERR_ARG: | |
380 | sr_err("Invalid arguments for setting serial port parameters."); | |
381 | return SR_ERR_ARG; | |
382 | case SP_ERR_FAIL: | |
383 | error = sp_last_error_message(); | |
cb7b165b UH |
384 | sr_err("Error setting serial port parameters (%d): %s.", |
385 | sp_last_error_code(), error); | |
a0dfaa6c UH |
386 | sp_free_error_message(error); |
387 | return SR_ERR; | |
700dcd5c UH |
388 | } |
389 | ||
e46b8fb1 | 390 | return SR_OK; |
d02a535e | 391 | } |
792fc686 | 392 | |
299bdb24 | 393 | /** |
48d3238e | 394 | * Set serial parameters for the specified serial port from parameter string. |
299bdb24 BV |
395 | * |
396 | * @param serial Previously initialized serial port structure. | |
48d3238e MH |
397 | * @param[in] paramstr A serial communication parameters string of the form |
398 | * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n | |
399 | * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n | |
400 | * \<baudrate\>=integer Baud rate.\n | |
401 | * \<bits\>=5|6|7|8 Number of data bits.\n | |
402 | * \<parity\>=n|e|o None, even, odd.\n | |
403 | * \<stopbits\>=1|2 One or two stop bits.\n | |
404 | * Options:\n | |
405 | * dtr=0|1 Set DTR off resp. on.\n | |
406 | * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n | |
407 | * rts=0|1 Set RTS off resp. on.\n | |
408 | * Please note that values and combinations of these parameters must be | |
409 | * supported by the concrete serial interface hardware and the drivers for it. | |
04cb9157 MH |
410 | * @retval SR_OK Success. |
411 | * @retval SR_ERR Failure. | |
299bdb24 | 412 | */ |
299bdb24 BV |
413 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, |
414 | const char *paramstr) | |
792fc686 | 415 | { |
48d3238e MH |
416 | #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$" |
417 | ||
792fc686 BV |
418 | GRegex *reg; |
419 | GMatchInfo *match; | |
26ddb5ba | 420 | int speed, databits, parity, stopbits, flow, rts, dtr, i; |
71caaad4 | 421 | char *mstr, **opts, **kv; |
792fc686 | 422 | |
26ddb5ba | 423 | speed = databits = parity = stopbits = flow = 0; |
71caaad4 | 424 | rts = dtr = -1; |
ea088bb6 | 425 | sr_spew("Parsing parameters from \"%s\".", paramstr); |
792fc686 BV |
426 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); |
427 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
428 | if ((mstr = g_match_info_fetch(match, 1))) | |
429 | speed = strtoul(mstr, NULL, 10); | |
430 | g_free(mstr); | |
431 | if ((mstr = g_match_info_fetch(match, 2))) | |
432 | databits = strtoul(mstr, NULL, 10); | |
433 | g_free(mstr); | |
434 | if ((mstr = g_match_info_fetch(match, 3))) { | |
435 | switch (mstr[0]) { | |
436 | case 'n': | |
437 | parity = SERIAL_PARITY_NONE; | |
438 | break; | |
439 | case 'e': | |
440 | parity = SERIAL_PARITY_EVEN; | |
441 | break; | |
442 | case 'o': | |
443 | parity = SERIAL_PARITY_ODD; | |
444 | break; | |
445 | } | |
446 | } | |
447 | g_free(mstr); | |
448 | if ((mstr = g_match_info_fetch(match, 4))) | |
449 | stopbits = strtoul(mstr, NULL, 10); | |
450 | g_free(mstr); | |
71caaad4 BV |
451 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { |
452 | if (mstr[0] != '/') { | |
453 | sr_dbg("missing separator before extra options"); | |
454 | speed = 0; | |
455 | } else { | |
456 | /* A set of "key=value" options separated by / */ | |
457 | opts = g_strsplit(mstr + 1, "/", 0); | |
458 | for (i = 0; opts[i]; i++) { | |
459 | kv = g_strsplit(opts[i], "=", 2); | |
460 | if (!strncmp(kv[0], "rts", 3)) { | |
461 | if (kv[1][0] == '1') | |
462 | rts = 1; | |
463 | else if (kv[1][0] == '0') | |
464 | rts = 0; | |
465 | else { | |
466 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
467 | speed = 0; | |
468 | } | |
469 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
470 | if (kv[1][0] == '1') | |
471 | dtr = 1; | |
472 | else if (kv[1][0] == '0') | |
473 | dtr = 0; | |
474 | else { | |
475 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
476 | speed = 0; | |
477 | } | |
26ddb5ba | 478 | } else if (!strncmp(kv[0], "flow", 4)) { |
479 | if (kv[1][0] == '0') | |
480 | flow = 0; | |
481 | else if (kv[1][0] == '1') | |
482 | flow = 1; | |
483 | else if (kv[1][0] == '2') | |
484 | flow = 2; | |
485 | else { | |
486 | sr_dbg("invalid value for flow: %c", kv[1][0]); | |
487 | speed = 0; | |
488 | } | |
71caaad4 BV |
489 | } |
490 | g_strfreev(kv); | |
491 | } | |
492 | g_strfreev(opts); | |
493 | } | |
494 | } | |
495 | g_free(mstr); | |
792fc686 BV |
496 | } |
497 | g_match_info_unref(match); | |
498 | g_regex_unref(reg); | |
499 | ||
ea088bb6 AG |
500 | if (speed) { |
501 | return serial_set_params(serial, speed, databits, parity, | |
26ddb5ba | 502 | stopbits, flow, rts, dtr); |
ea088bb6 AG |
503 | } else { |
504 | sr_dbg("Could not infer speed from parameter string."); | |
792fc686 | 505 | return SR_ERR_ARG; |
ea088bb6 | 506 | } |
792fc686 BV |
507 | } |
508 | ||
299bdb24 BV |
509 | /** |
510 | * Read a line from the specified serial port. | |
511 | * | |
512 | * @param serial Previously initialized serial port structure. | |
513 | * @param buf Buffer where to store the bytes that are read. | |
514 | * @param buflen Size of the buffer. | |
04cb9157 | 515 | * @param[in] timeout_ms How long to wait for a line to come in. |
299bdb24 BV |
516 | * |
517 | * Reading stops when CR of LR is found, which is stripped from the buffer. | |
518 | * | |
04cb9157 MH |
519 | * @retval SR_OK Success. |
520 | * @retval SR_ERR Failure. | |
299bdb24 BV |
521 | */ |
522 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, | |
523 | int *buflen, gint64 timeout_ms) | |
6f22a8ef | 524 | { |
bf505eed | 525 | gint64 start, remaining; |
6f22a8ef UH |
526 | int maxlen, len; |
527 | ||
64ecf7ee | 528 | if (!serial) { |
299bdb24 BV |
529 | sr_dbg("Invalid serial port."); |
530 | return SR_ERR; | |
531 | } | |
532 | ||
64ecf7ee ML |
533 | if (!serial->data) { |
534 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
299bdb24 BV |
535 | return -1; |
536 | } | |
537 | ||
6f22a8ef | 538 | start = g_get_monotonic_time(); |
bf505eed | 539 | remaining = timeout_ms; |
6f22a8ef UH |
540 | |
541 | maxlen = *buflen; | |
542 | *buflen = len = 0; | |
543 | while(1) { | |
544 | len = maxlen - *buflen - 1; | |
545 | if (len < 1) | |
546 | break; | |
bf505eed | 547 | len = sp_blocking_read(serial->data, *buf + *buflen, 1, remaining); |
6f22a8ef UH |
548 | if (len > 0) { |
549 | *buflen += len; | |
550 | *(*buf + *buflen) = '\0'; | |
318dd53c BV |
551 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' |
552 | || *(*buf + *buflen - 1) == '\n')) { | |
553 | /* Strip CR/LF and terminate. */ | |
6f22a8ef UH |
554 | *(*buf + --*buflen) = '\0'; |
555 | break; | |
556 | } | |
557 | } | |
bf505eed ML |
558 | /* Reduce timeout by time elapsed. */ |
559 | remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000); | |
560 | if (remaining <= 0) | |
6f22a8ef UH |
561 | /* Timeout */ |
562 | break; | |
5715e84f DT |
563 | if (len < 1) |
564 | g_usleep(2000); | |
6f22a8ef | 565 | } |
318dd53c BV |
566 | if (*buflen) |
567 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
6f22a8ef UH |
568 | |
569 | return SR_OK; | |
570 | } | |
766456be UH |
571 | |
572 | /** | |
573 | * Try to find a valid packet in a serial data stream. | |
574 | * | |
575 | * @param serial Previously initialized serial port structure. | |
576 | * @param buf Buffer containing the bytes to write. | |
04cb9157 MH |
577 | * @param buflen Size of the buffer. |
578 | * @param[in] packet_size Size, in bytes, of a valid packet. | |
766456be | 579 | * @param is_valid Callback that assesses whether the packet is valid or not. |
04cb9157 | 580 | * @param[in] timeout_ms The timeout after which, if no packet is detected, to |
766456be | 581 | * abort scanning. |
04cb9157 | 582 | * @param[in] baudrate The baudrate of the serial port. This parameter is not |
766456be UH |
583 | * critical, but it helps fine tune the serial port polling |
584 | * delay. | |
585 | * | |
d0a92abd | 586 | * @retval SR_OK Valid packet was found within the given timeout. |
04cb9157 | 587 | * @retval SR_ERR Failure. |
766456be UH |
588 | */ |
589 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
590 | uint8_t *buf, size_t *buflen, | |
144f6660 UH |
591 | size_t packet_size, |
592 | packet_valid_callback is_valid, | |
766456be UH |
593 | uint64_t timeout_ms, int baudrate) |
594 | { | |
595 | uint64_t start, time, byte_delay_us; | |
596 | size_t ibuf, i, maxlen; | |
597 | int len; | |
598 | ||
599 | maxlen = *buflen; | |
600 | ||
64ecf7ee ML |
601 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 |
602 | "ms, baudrate = %d).", serial->port, timeout_ms, baudrate); | |
766456be UH |
603 | |
604 | if (maxlen < (packet_size / 2) ) { | |
605 | sr_err("Buffer size must be at least twice the packet size."); | |
606 | return SR_ERR; | |
607 | } | |
608 | ||
766456be UH |
609 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ |
610 | byte_delay_us = 10 * (1000000 / baudrate); | |
611 | start = g_get_monotonic_time(); | |
612 | ||
613 | i = ibuf = len = 0; | |
614 | while (ibuf < maxlen) { | |
18e4d5bc | 615 | len = serial_read_nonblocking(serial, &buf[ibuf], 1); |
766456be UH |
616 | if (len > 0) { |
617 | ibuf += len; | |
618 | } else if (len == 0) { | |
006dbe55 | 619 | /* No logging, already done in serial_read(). */ |
766456be UH |
620 | } else { |
621 | /* Error reading byte, but continuing anyway. */ | |
622 | } | |
551c3d8c AG |
623 | |
624 | time = g_get_monotonic_time() - start; | |
625 | time /= 1000; | |
626 | ||
766456be UH |
627 | if ((ibuf - i) >= packet_size) { |
628 | /* We have at least a packet's worth of data. */ | |
629 | if (is_valid(&buf[i])) { | |
766456be UH |
630 | sr_spew("Found valid %d-byte packet after " |
631 | "%" PRIu64 "ms.", (ibuf - i), time); | |
632 | *buflen = ibuf; | |
633 | return SR_OK; | |
634 | } else { | |
635 | sr_spew("Got %d bytes, but not a valid " | |
636 | "packet.", (ibuf - i)); | |
637 | } | |
638 | /* Not a valid packet. Continue searching. */ | |
639 | i++; | |
640 | } | |
551c3d8c | 641 | if (time >= timeout_ms) { |
766456be | 642 | /* Timeout */ |
551c3d8c | 643 | sr_dbg("Detection timed out after %dms.", time); |
766456be UH |
644 | break; |
645 | } | |
5715e84f DT |
646 | if (len < 1) |
647 | g_usleep(byte_delay_us); | |
766456be UH |
648 | } |
649 | ||
650 | *buflen = ibuf; | |
651 | ||
652 | sr_err("Didn't find a valid packet (read %d bytes).", *buflen); | |
653 | ||
654 | return SR_ERR; | |
655 | } | |
1bd9e678 DJ |
656 | |
657 | /** | |
658 | * Extract the serial device and options from the options linked list. | |
659 | * | |
660 | * @param options List of options passed from the command line. | |
661 | * @param serial_device Pointer where to store the exctracted serial device. | |
662 | * @param serial_options Pointer where to store the optional extracted serial | |
663 | * options. | |
664 | * | |
665 | * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The | |
666 | * returned string should not be freed by the caller. | |
667 | */ | |
668 | SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device, | |
669 | const char **serial_options) | |
670 | { | |
671 | GSList *l; | |
672 | struct sr_config *src; | |
673 | ||
674 | *serial_device = NULL; | |
675 | ||
676 | for (l = options; l; l = l->next) { | |
677 | src = l->data; | |
678 | switch (src->key) { | |
679 | case SR_CONF_CONN: | |
680 | *serial_device = g_variant_get_string(src->data, NULL); | |
681 | sr_dbg("Parsed serial device: %s", *serial_device); | |
682 | break; | |
683 | ||
684 | case SR_CONF_SERIALCOMM: | |
685 | *serial_options = g_variant_get_string(src->data, NULL); | |
686 | sr_dbg("Parsed serial options: %s", *serial_options); | |
687 | break; | |
688 | } | |
689 | } | |
690 | ||
691 | if (!*serial_device) { | |
692 | sr_dbg("No serial device specified"); | |
693 | return SR_ERR; | |
694 | } | |
695 | ||
696 | return SR_OK; | |
697 | } | |
abc4b335 | 698 | |
a0a4c0fb | 699 | #ifdef _WIN32 |
ba1949f5 | 700 | typedef HANDLE event_handle; |
a0a4c0fb | 701 | #else |
ba1949f5 | 702 | typedef int event_handle; |
a0a4c0fb | 703 | #endif |
ba1949f5 | 704 | |
102f1239 BV |
705 | SR_PRIV int serial_source_add(struct sr_session *session, |
706 | struct sr_serial_dev_inst *serial, int events, int timeout, | |
707 | sr_receive_data_callback cb, void *cb_data) | |
ba1949f5 ML |
708 | { |
709 | enum sp_event mask = 0; | |
710 | unsigned int i; | |
711 | ||
712 | if (sp_new_event_set(&serial->event_set) != SP_OK) | |
713 | return SR_ERR; | |
714 | ||
715 | if (events & G_IO_IN) | |
716 | mask |= SP_EVENT_RX_READY; | |
717 | if (events & G_IO_OUT) | |
718 | mask |= SP_EVENT_TX_READY; | |
719 | if (events & G_IO_ERR) | |
720 | mask |= SP_EVENT_ERROR; | |
721 | ||
722 | if (sp_add_port_events(serial->event_set, serial->data, mask) != SP_OK) { | |
723 | sp_free_event_set(serial->event_set); | |
724 | return SR_ERR; | |
725 | } | |
726 | ||
727 | serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count); | |
728 | ||
729 | for (i = 0; i < serial->event_set->count; i++) { | |
730 | ||
731 | serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i]; | |
732 | ||
733 | mask = serial->event_set->masks[i]; | |
734 | ||
735 | if (mask & SP_EVENT_RX_READY) | |
736 | serial->pollfds[i].events |= G_IO_IN; | |
737 | if (mask & SP_EVENT_TX_READY) | |
738 | serial->pollfds[i].events |= G_IO_OUT; | |
739 | if (mask & SP_EVENT_ERROR) | |
740 | serial->pollfds[i].events |= G_IO_ERR; | |
741 | ||
102f1239 BV |
742 | if (sr_session_source_add_pollfd(session, &serial->pollfds[i], |
743 | timeout, cb, cb_data) != SR_OK) | |
ba1949f5 ML |
744 | return SR_ERR; |
745 | } | |
746 | ||
747 | return SR_OK; | |
abc4b335 | 748 | } |
7faa3e88 | 749 | |
102f1239 BV |
750 | SR_PRIV int serial_source_remove(struct sr_session *session, |
751 | struct sr_serial_dev_inst *serial) | |
7faa3e88 | 752 | { |
ba1949f5 ML |
753 | unsigned int i; |
754 | ||
755 | for (i = 0; i < serial->event_set->count; i++) | |
102f1239 | 756 | if (sr_session_source_remove_pollfd(session, &serial->pollfds[i]) != SR_OK) |
ba1949f5 ML |
757 | return SR_ERR; |
758 | ||
759 | g_free(serial->pollfds); | |
760 | sp_free_event_set(serial->event_set); | |
761 | ||
762 | serial->pollfds = NULL; | |
763 | serial->event_set = NULL; | |
764 | ||
765 | return SR_OK; | |
7faa3e88 | 766 | } |
b541f837 AJ |
767 | |
768 | /** | |
769 | * Find USB serial devices via the USB vendor ID and product ID. | |
770 | * | |
d0a92abd MH |
771 | * @param[in] vendor_id Vendor ID of the USB device. |
772 | * @param[in] product_id Product ID of the USB device. | |
b541f837 AJ |
773 | * |
774 | * @return A GSList of strings containing the path of the serial device or | |
775 | * NULL if no serial device is found. The returned list must be freed | |
776 | * by the caller. | |
777 | */ | |
778 | SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id) | |
779 | { | |
01f6e330 AJ |
780 | GSList *tty_devs = NULL; |
781 | struct sp_port **ports; | |
782 | int i, vid, pid; | |
b541f837 | 783 | |
01f6e330 AJ |
784 | if (sp_list_ports(&ports) != SP_OK) |
785 | return NULL; | |
b541f837 | 786 | |
01f6e330 AJ |
787 | for (i=0; ports[i]; i++) |
788 | if (sp_get_port_transport(ports[i]) == SP_TRANSPORT_USB && | |
789 | sp_get_port_usb_vid_pid(ports[i], &vid, &pid) == SP_OK && | |
790 | vid == vendor_id && pid == product_id) | |
791 | tty_devs = g_slist_prepend(tty_devs, | |
792 | g_strdup(sp_get_port_name(ports[i]))); | |
b541f837 | 793 | |
01f6e330 | 794 | sp_free_port_list(ports); |
b541f837 | 795 | return tty_devs; |
b541f837 | 796 | } |