]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
7 | * Copyright (C) 2014 Uffe Jakobsen <uffe@uffe.org> | |
8 | * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net> | |
9 | * | |
10 | * This program is free software: you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation, either version 3 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
23 | ||
24 | #include <config.h> | |
25 | #include <string.h> | |
26 | #include <stdlib.h> | |
27 | #include <glib.h> | |
28 | #include <glib/gstdio.h> | |
29 | #ifdef HAVE_LIBSERIALPORT | |
30 | #include <libserialport.h> | |
31 | #endif | |
32 | #include <libsigrok/libsigrok.h> | |
33 | #include "libsigrok-internal.h" | |
34 | #ifdef _WIN32 | |
35 | #include <windows.h> /* for HANDLE */ | |
36 | #endif | |
37 | ||
38 | /** @cond PRIVATE */ | |
39 | #define LOG_PREFIX "serial" | |
40 | /** @endcond */ | |
41 | ||
42 | /** | |
43 | * @file | |
44 | * | |
45 | * Serial port handling. | |
46 | */ | |
47 | ||
48 | /** | |
49 | * @defgroup grp_serial Serial port handling | |
50 | * | |
51 | * Serial port handling functions. | |
52 | * | |
53 | * @{ | |
54 | */ | |
55 | ||
56 | #ifdef HAVE_SERIAL_COMM | |
57 | ||
58 | /* See if an (assumed opened) serial port is of any supported type. */ | |
59 | static int dev_is_supported(struct sr_serial_dev_inst *serial) | |
60 | { | |
61 | if (!serial || !serial->lib_funcs) | |
62 | return 0; | |
63 | ||
64 | return 1; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Open the specified serial port. | |
69 | * | |
70 | * @param serial Previously initialized serial port structure. | |
71 | * @param[in] flags Flags to use when opening the serial port. Possible flags | |
72 | * include SERIAL_RDWR, SERIAL_RDONLY. | |
73 | * | |
74 | * If the serial structure contains a serialcomm string, it will be | |
75 | * passed to serial_set_paramstr() after the port is opened. | |
76 | * | |
77 | * @retval SR_OK Success. | |
78 | * @retval SR_ERR Failure. | |
79 | * | |
80 | * @private | |
81 | */ | |
82 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) | |
83 | { | |
84 | int ret; | |
85 | ||
86 | if (!serial) { | |
87 | sr_dbg("Invalid serial port."); | |
88 | return SR_ERR; | |
89 | } | |
90 | ||
91 | sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags); | |
92 | ||
93 | /* | |
94 | * Determine which serial transport library to use. Derive the | |
95 | * variant from the serial port's name. Default to libserialport | |
96 | * for backwards compatibility. | |
97 | */ | |
98 | if (ser_name_is_hid(serial)) | |
99 | serial->lib_funcs = ser_lib_funcs_hid; | |
100 | else if (ser_name_is_bt(serial)) | |
101 | serial->lib_funcs = ser_lib_funcs_bt; | |
102 | else | |
103 | serial->lib_funcs = ser_lib_funcs_libsp; | |
104 | if (!serial->lib_funcs) | |
105 | return SR_ERR_NA; | |
106 | ||
107 | /* | |
108 | * Note that use of the 'rcv_buffer' is optional, and the buffer's | |
109 | * size heavily depends on the specific transport. That's why the | |
110 | * buffer's content gets accessed and the buffer is released here in | |
111 | * common code, but the buffer gets allocated in libraries' open() | |
112 | * routines. | |
113 | */ | |
114 | ||
115 | /* | |
116 | * Run the transport's open routine. Setup the bitrate and the | |
117 | * UART frame format. | |
118 | */ | |
119 | if (!serial->lib_funcs->open) | |
120 | return SR_ERR_NA; | |
121 | ret = serial->lib_funcs->open(serial, flags); | |
122 | if (ret != SR_OK) | |
123 | return ret; | |
124 | ||
125 | if (serial->serialcomm) { | |
126 | ret = serial_set_paramstr(serial, serial->serialcomm); | |
127 | if (ret != SR_OK) | |
128 | return ret; | |
129 | } | |
130 | ||
131 | /* | |
132 | * Flush potentially dangling RX data. Availability of the | |
133 | * flush primitive depends on the transport/cable, absense | |
134 | * is non-fatal. | |
135 | */ | |
136 | ret = serial_flush(serial); | |
137 | if (ret == SR_ERR_NA) | |
138 | ret = SR_OK; | |
139 | if (ret != SR_OK) | |
140 | return ret; | |
141 | ||
142 | return SR_OK; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Close the specified serial port. | |
147 | * | |
148 | * @param serial Previously initialized serial port structure. | |
149 | * | |
150 | * @retval SR_OK Success. | |
151 | * @retval SR_ERR Failure. | |
152 | * | |
153 | * @private | |
154 | */ | |
155 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) | |
156 | { | |
157 | int rc; | |
158 | ||
159 | if (!serial) { | |
160 | sr_dbg("Invalid serial port."); | |
161 | return SR_ERR; | |
162 | } | |
163 | ||
164 | sr_spew("Closing serial port %s.", serial->port); | |
165 | ||
166 | if (!serial->lib_funcs || !serial->lib_funcs->close) | |
167 | return SR_ERR_NA; | |
168 | ||
169 | rc = serial->lib_funcs->close(serial); | |
170 | if (rc == SR_OK && serial->rcv_buffer) { | |
171 | g_string_free(serial->rcv_buffer, TRUE); | |
172 | serial->rcv_buffer = NULL; | |
173 | } | |
174 | ||
175 | return rc; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Flush serial port buffers. Empty buffers, discard pending RX and TX data. | |
180 | * | |
181 | * @param serial Previously initialized serial port structure. | |
182 | * | |
183 | * @retval SR_OK Success. | |
184 | * @retval SR_ERR Failure. | |
185 | * | |
186 | * @private | |
187 | */ | |
188 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) | |
189 | { | |
190 | if (!serial) { | |
191 | sr_dbg("Invalid serial port."); | |
192 | return SR_ERR; | |
193 | } | |
194 | ||
195 | sr_spew("Flushing serial port %s.", serial->port); | |
196 | ||
197 | sr_ser_discard_queued_data(serial); | |
198 | ||
199 | if (!serial->lib_funcs || !serial->lib_funcs->flush) | |
200 | return SR_ERR_NA; | |
201 | ||
202 | return serial->lib_funcs->flush(serial); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Drain serial port buffers. Wait for pending TX data to be sent. | |
207 | * | |
208 | * @param serial Previously initialized serial port structure. | |
209 | * | |
210 | * @retval SR_OK Success. | |
211 | * @retval SR_ERR Failure. | |
212 | * | |
213 | * @private | |
214 | */ | |
215 | SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial) | |
216 | { | |
217 | if (!serial) { | |
218 | sr_dbg("Invalid serial port."); | |
219 | return SR_ERR; | |
220 | } | |
221 | ||
222 | sr_spew("Draining serial port %s.", serial->port); | |
223 | ||
224 | if (!serial->lib_funcs || !serial->lib_funcs->drain) | |
225 | return SR_ERR_NA; | |
226 | ||
227 | return serial->lib_funcs->drain(serial); | |
228 | } | |
229 | ||
230 | /* | |
231 | * Provide an internal RX data buffer for the serial port. This is not | |
232 | * supposed to be used directly by applications. Instead optional and | |
233 | * alternative transports for serial communication can use this buffer | |
234 | * if their progress is driven from background activity, and is not | |
235 | * (directly) driven by external API calls. | |
236 | * | |
237 | * BEWARE! This implementation assumes that data which gets communicated | |
238 | * via UART can get stored in a GString (which is a char array). Since | |
239 | * the API hides this detail, we can address this issue later when needed. | |
240 | * Callers use the API which communicates bytes. | |
241 | * | |
242 | * Applications optionally can register a "per RX chunk" callback, when | |
243 | * they depend on the frame boundaries of the respective physical layer. | |
244 | * Most callers just want the stream of RX data, and can use the buffer. | |
245 | * | |
246 | * The availability of RX chunks to callbacks, as well as the capability | |
247 | * to pass on exact frames as chunks or potential re-assembly of chunks | |
248 | * to a single data block, depend on each transport's implementation. | |
249 | */ | |
250 | ||
251 | /** | |
252 | * Register application callback for RX data chunks. | |
253 | * | |
254 | * @param[in] serial Previously initialized serial port instance. | |
255 | * @param[in] cb Routine to call as RX data becomes available. | |
256 | * @param[in] cb_data User data to pass to the callback in addition to RX data. | |
257 | * | |
258 | * @retval SR_ERR_ARG Invalid parameters. | |
259 | * @retval SR_OK Successful registration. | |
260 | * | |
261 | * Callbacks get unregistered by specifying NULL for the 'cb' parameter. | |
262 | * | |
263 | * @private | |
264 | */ | |
265 | SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial, | |
266 | serial_rx_chunk_callback cb, void *cb_data) | |
267 | { | |
268 | if (!serial) | |
269 | return SR_ERR_ARG; | |
270 | ||
271 | serial->rx_chunk_cb_func = cb; | |
272 | serial->rx_chunk_cb_data = cb_data; | |
273 | ||
274 | return SR_OK; | |
275 | } | |
276 | ||
277 | /** | |
278 | * Discard previously queued RX data. Internal to the serial subsystem, | |
279 | * coordination between common and transport specific support code. | |
280 | * | |
281 | * @param[in] serial Previously opened serial port instance. | |
282 | * | |
283 | * @private | |
284 | */ | |
285 | SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial) | |
286 | { | |
287 | if (!serial || !serial->rcv_buffer) | |
288 | return; | |
289 | ||
290 | g_string_truncate(serial->rcv_buffer, 0); | |
291 | } | |
292 | ||
293 | /** | |
294 | * Get amount of queued RX data. Internal to the serial subsystem, | |
295 | * coordination between common and transport specific support code. | |
296 | * | |
297 | * @param[in] serial Previously opened serial port instance. | |
298 | * | |
299 | * @private | |
300 | */ | |
301 | SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial) | |
302 | { | |
303 | if (!serial || !serial->rcv_buffer) | |
304 | return 0; | |
305 | ||
306 | return serial->rcv_buffer->len; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Queue received data. Internal to the serial subsystem, coordination | |
311 | * between common and transport specific support code. | |
312 | * | |
313 | * @param[in] serial Previously opened serial port instance. | |
314 | * @param[in] data Pointer to data bytes to queue. | |
315 | * @param[in] len Number of data bytes to queue. | |
316 | * | |
317 | * @private | |
318 | */ | |
319 | SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial, | |
320 | const uint8_t *data, size_t len) | |
321 | { | |
322 | if (!serial || !data || !len) | |
323 | return; | |
324 | ||
325 | if (serial->rx_chunk_cb_func) | |
326 | serial->rx_chunk_cb_func(serial, serial->rx_chunk_cb_data, data, len); | |
327 | else if (serial->rcv_buffer) | |
328 | g_string_append_len(serial->rcv_buffer, (const gchar *)data, len); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Retrieve previously queued RX data. Internal to the serial subsystem, | |
333 | * coordination between common and transport specific support code. | |
334 | * | |
335 | * @param[in] serial Previously opened serial port instance. | |
336 | * @param[out] data Pointer to store retrieved data bytes into. | |
337 | * @param[in] len Number of data bytes to retrieve. | |
338 | * | |
339 | * @private | |
340 | */ | |
341 | SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial, | |
342 | uint8_t *data, size_t len) | |
343 | { | |
344 | size_t qlen; | |
345 | GString *buf; | |
346 | ||
347 | if (!serial || !data || !len) | |
348 | return 0; | |
349 | ||
350 | qlen = sr_ser_has_queued_data(serial); | |
351 | if (!qlen) | |
352 | return 0; | |
353 | ||
354 | buf = serial->rcv_buffer; | |
355 | if (len > buf->len) | |
356 | len = buf->len; | |
357 | if (len) { | |
358 | memcpy(data, buf->str, len); | |
359 | g_string_erase(buf, 0, len); | |
360 | } | |
361 | ||
362 | return len; | |
363 | } | |
364 | ||
365 | /** | |
366 | * Check for available receive data. | |
367 | * | |
368 | * @param[in] serial Previously opened serial port instance. | |
369 | * | |
370 | * @returns The number of (known) available RX data bytes. | |
371 | * | |
372 | * Returns 0 if no receive data is available, or if the amount of | |
373 | * available receive data cannot get determined. | |
374 | * | |
375 | * @private | |
376 | */ | |
377 | SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial) | |
378 | { | |
379 | size_t lib_count, buf_count; | |
380 | ||
381 | if (!serial) | |
382 | return 0; | |
383 | ||
384 | lib_count = 0; | |
385 | if (serial->lib_funcs && serial->lib_funcs->get_rx_avail) | |
386 | lib_count = serial->lib_funcs->get_rx_avail(serial); | |
387 | ||
388 | buf_count = sr_ser_has_queued_data(serial); | |
389 | ||
390 | return lib_count + buf_count; | |
391 | } | |
392 | ||
393 | static int _serial_write(struct sr_serial_dev_inst *serial, | |
394 | const void *buf, size_t count, | |
395 | int nonblocking, unsigned int timeout_ms) | |
396 | { | |
397 | ssize_t ret; | |
398 | ||
399 | if (!serial) { | |
400 | sr_dbg("Invalid serial port."); | |
401 | return SR_ERR; | |
402 | } | |
403 | ||
404 | if (!serial->lib_funcs || !serial->lib_funcs->write) | |
405 | return SR_ERR_NA; | |
406 | ret = serial->lib_funcs->write(serial, buf, count, | |
407 | nonblocking, timeout_ms); | |
408 | sr_spew("Wrote %zd/%zu bytes.", ret, count); | |
409 | ||
410 | return ret; | |
411 | } | |
412 | ||
413 | /** | |
414 | * Write a number of bytes to the specified serial port, blocking until finished. | |
415 | * | |
416 | * @param serial Previously initialized serial port structure. | |
417 | * @param[in] buf Buffer containing the bytes to write. | |
418 | * @param[in] count Number of bytes to write. | |
419 | * @param[in] timeout_ms Timeout in ms, or 0 for no timeout. | |
420 | * | |
421 | * @retval SR_ERR_ARG Invalid argument. | |
422 | * @retval SR_ERR Other error. | |
423 | * @retval other The number of bytes written. If this is less than the number | |
424 | * specified in the call, the timeout was reached. | |
425 | * | |
426 | * @private | |
427 | */ | |
428 | SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial, | |
429 | const void *buf, size_t count, unsigned int timeout_ms) | |
430 | { | |
431 | return _serial_write(serial, buf, count, 0, timeout_ms); | |
432 | } | |
433 | ||
434 | /** | |
435 | * Write a number of bytes to the specified serial port, return immediately. | |
436 | * | |
437 | * @param serial Previously initialized serial port structure. | |
438 | * @param[in] buf Buffer containing the bytes to write. | |
439 | * @param[in] count Number of bytes to write. | |
440 | * | |
441 | * @retval SR_ERR_ARG Invalid argument. | |
442 | * @retval SR_ERR Other error. | |
443 | * @retval other The number of bytes written. | |
444 | * | |
445 | * @private | |
446 | */ | |
447 | SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial, | |
448 | const void *buf, size_t count) | |
449 | { | |
450 | return _serial_write(serial, buf, count, 1, 0); | |
451 | } | |
452 | ||
453 | static int _serial_read(struct sr_serial_dev_inst *serial, | |
454 | void *buf, size_t count, int nonblocking, unsigned int timeout_ms) | |
455 | { | |
456 | ssize_t ret; | |
457 | ||
458 | if (!serial) { | |
459 | sr_dbg("Invalid serial port."); | |
460 | return SR_ERR; | |
461 | } | |
462 | ||
463 | if (!serial->lib_funcs || !serial->lib_funcs->read) | |
464 | return SR_ERR_NA; | |
465 | ret = serial->lib_funcs->read(serial, buf, count, | |
466 | nonblocking, timeout_ms); | |
467 | if (ret > 0) | |
468 | sr_spew("Read %zd/%zu bytes.", ret, count); | |
469 | ||
470 | return ret; | |
471 | } | |
472 | ||
473 | /** | |
474 | * Read a number of bytes from the specified serial port, block until finished. | |
475 | * | |
476 | * @param serial Previously initialized serial port structure. | |
477 | * @param buf Buffer where to store the bytes that are read. | |
478 | * @param[in] count The number of bytes to read. | |
479 | * @param[in] timeout_ms Timeout in ms, or 0 for no timeout. | |
480 | * | |
481 | * @retval SR_ERR_ARG Invalid argument. | |
482 | * @retval SR_ERR Other error. | |
483 | * @retval other The number of bytes read. If this is less than the number | |
484 | * requested, the timeout was reached. | |
485 | * | |
486 | * @private | |
487 | */ | |
488 | SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, | |
489 | void *buf, size_t count, unsigned int timeout_ms) | |
490 | { | |
491 | return _serial_read(serial, buf, count, 0, timeout_ms); | |
492 | } | |
493 | ||
494 | /** | |
495 | * Try to read up to @a count bytes from the specified serial port, return | |
496 | * immediately with what's available. | |
497 | * | |
498 | * @param serial Previously initialized serial port structure. | |
499 | * @param buf Buffer where to store the bytes that are read. | |
500 | * @param[in] count The number of bytes to read. | |
501 | * | |
502 | * @retval SR_ERR_ARG Invalid argument. | |
503 | * @retval SR_ERR Other error. | |
504 | * @retval other The number of bytes read. | |
505 | * | |
506 | * @private | |
507 | */ | |
508 | SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, | |
509 | void *buf, size_t count) | |
510 | { | |
511 | return _serial_read(serial, buf, count, 1, 0); | |
512 | } | |
513 | ||
514 | /** | |
515 | * Set serial parameters for the specified serial port. | |
516 | * | |
517 | * @param serial Previously initialized serial port structure. | |
518 | * @param[in] baudrate The baudrate to set. | |
519 | * @param[in] bits The number of data bits to use (5, 6, 7 or 8). | |
520 | * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
521 | * @param[in] stopbits The number of stop bits to use (1 or 2). | |
522 | * @param[in] flowcontrol The flow control settings to use (0 = none, | |
523 | * 1 = RTS/CTS, 2 = XON/XOFF). | |
524 | * @param[in] rts Status of RTS line (0 or 1; required by some interfaces). | |
525 | * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces). | |
526 | * | |
527 | * @retval SR_OK Success. | |
528 | * @retval SR_ERR Failure. | |
529 | * | |
530 | * @private | |
531 | */ | |
532 | SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, | |
533 | int baudrate, int bits, int parity, int stopbits, | |
534 | int flowcontrol, int rts, int dtr) | |
535 | { | |
536 | int ret; | |
537 | ||
538 | if (!serial) { | |
539 | sr_dbg("Invalid serial port."); | |
540 | return SR_ERR; | |
541 | } | |
542 | ||
543 | sr_spew("Setting serial parameters on port %s.", serial->port); | |
544 | ||
545 | if (!serial->lib_funcs || !serial->lib_funcs->set_params) | |
546 | return SR_ERR_NA; | |
547 | ret = serial->lib_funcs->set_params(serial, | |
548 | baudrate, bits, parity, stopbits, | |
549 | flowcontrol, rts, dtr); | |
550 | if (ret == SR_OK) { | |
551 | serial->comm_params.bit_rate = baudrate; | |
552 | serial->comm_params.data_bits = bits; | |
553 | serial->comm_params.parity_bits = parity ? 1 : 0; | |
554 | serial->comm_params.stop_bits = stopbits; | |
555 | sr_dbg("DBG: %s() rate %d, %d%s%d", __func__, | |
556 | baudrate, bits, | |
557 | (parity == 0) ? "n" : "x", | |
558 | stopbits); | |
559 | } | |
560 | ||
561 | return ret; | |
562 | } | |
563 | ||
564 | /** | |
565 | * Set serial parameters for the specified serial port from parameter string. | |
566 | * | |
567 | * @param serial Previously initialized serial port structure. | |
568 | * @param[in] paramstr A serial communication parameters string of the form | |
569 | * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n | |
570 | * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n | |
571 | * \<baudrate\>=integer Baud rate.\n | |
572 | * \<bits\>=5|6|7|8 Number of data bits.\n | |
573 | * \<parity\>=n|e|o None, even, odd.\n | |
574 | * \<stopbits\>=1|2 One or two stop bits.\n | |
575 | * Options:\n | |
576 | * dtr=0|1 Set DTR off resp. on.\n | |
577 | * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n | |
578 | * rts=0|1 Set RTS off resp. on.\n | |
579 | * Please note that values and combinations of these parameters must be | |
580 | * supported by the concrete serial interface hardware and the drivers for it. | |
581 | * | |
582 | * @retval SR_OK Success. | |
583 | * @retval SR_ERR Failure. | |
584 | * | |
585 | * @private | |
586 | */ | |
587 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, | |
588 | const char *paramstr) | |
589 | { | |
590 | /** @cond PRIVATE */ | |
591 | #define SERIAL_COMM_SPEC "^(\\d+)(/([5678])([neo])([12]))?(.*)$" | |
592 | /** @endcond */ | |
593 | ||
594 | GRegex *reg; | |
595 | GMatchInfo *match; | |
596 | int speed, databits, parity, stopbits, flow, rts, dtr, i; | |
597 | char *mstr, **opts, **kv; | |
598 | ||
599 | speed = flow = 0; | |
600 | databits = 8; | |
601 | parity = SP_PARITY_NONE; | |
602 | stopbits = 1; | |
603 | rts = dtr = -1; | |
604 | sr_spew("Parsing parameters from \"%s\".", paramstr); | |
605 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); | |
606 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
607 | if ((mstr = g_match_info_fetch(match, 1))) | |
608 | speed = strtoul(mstr, NULL, 10); | |
609 | g_free(mstr); | |
610 | if ((mstr = g_match_info_fetch(match, 3)) && mstr[0]) | |
611 | databits = strtoul(mstr, NULL, 10); | |
612 | g_free(mstr); | |
613 | if ((mstr = g_match_info_fetch(match, 4)) && mstr[0]) { | |
614 | switch (mstr[0]) { | |
615 | case 'n': | |
616 | parity = SP_PARITY_NONE; | |
617 | break; | |
618 | case 'e': | |
619 | parity = SP_PARITY_EVEN; | |
620 | break; | |
621 | case 'o': | |
622 | parity = SP_PARITY_ODD; | |
623 | break; | |
624 | } | |
625 | } | |
626 | g_free(mstr); | |
627 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0]) | |
628 | stopbits = strtoul(mstr, NULL, 10); | |
629 | g_free(mstr); | |
630 | if ((mstr = g_match_info_fetch(match, 6)) && mstr[0] != '\0') { | |
631 | if (mstr[0] != '/') { | |
632 | sr_dbg("missing separator before extra options"); | |
633 | speed = 0; | |
634 | } else { | |
635 | /* A set of "key=value" options separated by / */ | |
636 | opts = g_strsplit(mstr + 1, "/", 0); | |
637 | for (i = 0; opts[i]; i++) { | |
638 | kv = g_strsplit(opts[i], "=", 2); | |
639 | if (!strncmp(kv[0], "rts", 3)) { | |
640 | if (kv[1][0] == '1') | |
641 | rts = 1; | |
642 | else if (kv[1][0] == '0') | |
643 | rts = 0; | |
644 | else { | |
645 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
646 | speed = 0; | |
647 | } | |
648 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
649 | if (kv[1][0] == '1') | |
650 | dtr = 1; | |
651 | else if (kv[1][0] == '0') | |
652 | dtr = 0; | |
653 | else { | |
654 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
655 | speed = 0; | |
656 | } | |
657 | } else if (!strncmp(kv[0], "flow", 4)) { | |
658 | if (kv[1][0] == '0') | |
659 | flow = 0; | |
660 | else if (kv[1][0] == '1') | |
661 | flow = 1; | |
662 | else if (kv[1][0] == '2') | |
663 | flow = 2; | |
664 | else { | |
665 | sr_dbg("invalid value for flow: %c", kv[1][0]); | |
666 | speed = 0; | |
667 | } | |
668 | } | |
669 | g_strfreev(kv); | |
670 | } | |
671 | g_strfreev(opts); | |
672 | } | |
673 | } | |
674 | g_free(mstr); | |
675 | } | |
676 | g_match_info_unref(match); | |
677 | g_regex_unref(reg); | |
678 | sr_spew("Got params: rate %d, frame %d/%d/%d, flow %d, rts %d, dtr %d.", | |
679 | speed, databits, parity, stopbits, flow, rts, dtr); | |
680 | ||
681 | if (speed) { | |
682 | return serial_set_params(serial, speed, databits, parity, | |
683 | stopbits, flow, rts, dtr); | |
684 | } else { | |
685 | sr_dbg("Could not infer speed from parameter string."); | |
686 | return SR_ERR_ARG; | |
687 | } | |
688 | } | |
689 | ||
690 | /** | |
691 | * Read a line from the specified serial port. | |
692 | * | |
693 | * @param[in] serial Previously initialized serial port structure. | |
694 | * @param[out] buf Buffer where to store the bytes that are read. | |
695 | * @param[in] buflen Size of the buffer. | |
696 | * @param[in] timeout_ms How long to wait for a line to come in. | |
697 | * | |
698 | * Reading stops when CR or LF is found, which is stripped from the buffer. | |
699 | * | |
700 | * @retval SR_OK Success. | |
701 | * @retval SR_ERR Failure. | |
702 | * | |
703 | * @private | |
704 | */ | |
705 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, | |
706 | char **buf, int *buflen, gint64 timeout_ms) | |
707 | { | |
708 | gint64 start, remaining; | |
709 | int maxlen, len; | |
710 | ||
711 | if (!serial) { | |
712 | sr_dbg("Invalid serial port."); | |
713 | return SR_ERR; | |
714 | } | |
715 | ||
716 | if (!dev_is_supported(serial)) { | |
717 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
718 | return -1; | |
719 | } | |
720 | ||
721 | start = g_get_monotonic_time(); | |
722 | remaining = timeout_ms; | |
723 | ||
724 | maxlen = *buflen; | |
725 | *buflen = len = 0; | |
726 | while (1) { | |
727 | len = maxlen - *buflen - 1; | |
728 | if (len < 1) | |
729 | break; | |
730 | len = serial_read_blocking(serial, *buf + *buflen, 1, remaining); | |
731 | if (len > 0) { | |
732 | *buflen += len; | |
733 | *(*buf + *buflen) = '\0'; | |
734 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' | |
735 | || *(*buf + *buflen - 1) == '\n')) { | |
736 | /* Strip CR/LF and terminate. */ | |
737 | *(*buf + --*buflen) = '\0'; | |
738 | break; | |
739 | } | |
740 | } | |
741 | /* Reduce timeout by time elapsed. */ | |
742 | remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000); | |
743 | if (remaining <= 0) | |
744 | /* Timeout */ | |
745 | break; | |
746 | if (len < 1) | |
747 | g_usleep(2000); | |
748 | } | |
749 | if (*buflen) | |
750 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
751 | ||
752 | return SR_OK; | |
753 | } | |
754 | ||
755 | /** | |
756 | * Try to find a valid packet in a serial data stream. | |
757 | * | |
758 | * @param serial Previously initialized serial port structure. | |
759 | * @param buf Buffer containing the bytes to write. | |
760 | * @param buflen Size of the buffer. | |
761 | * @param[in] packet_size Size, in bytes, of a valid packet. | |
762 | * @param is_valid Callback that assesses whether the packet is valid or not. | |
763 | * @param[in] timeout_ms The timeout after which, if no packet is detected, to | |
764 | * abort scanning. | |
765 | * | |
766 | * @retval SR_OK Valid packet was found within the given timeout. | |
767 | * @retval SR_ERR Failure. | |
768 | * | |
769 | * @private | |
770 | */ | |
771 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
772 | uint8_t *buf, size_t *buflen, | |
773 | size_t packet_size, | |
774 | packet_valid_callback is_valid, | |
775 | uint64_t timeout_ms) | |
776 | { | |
777 | uint64_t start, time, byte_delay_us; | |
778 | size_t ibuf, i, maxlen; | |
779 | ssize_t len; | |
780 | ||
781 | maxlen = *buflen; | |
782 | ||
783 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 "ms).", | |
784 | serial->port, timeout_ms); | |
785 | ||
786 | if (maxlen < (packet_size * 2) ) { | |
787 | sr_err("Buffer size must be at least twice the packet size."); | |
788 | return SR_ERR; | |
789 | } | |
790 | ||
791 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ | |
792 | byte_delay_us = serial_timeout(serial, 1) * 1000; | |
793 | start = g_get_monotonic_time(); | |
794 | ||
795 | i = ibuf = len = 0; | |
796 | while (ibuf < maxlen) { | |
797 | len = serial_read_nonblocking(serial, &buf[ibuf], 1); | |
798 | if (len > 0) { | |
799 | ibuf += len; | |
800 | } else if (len == 0) { | |
801 | /* No logging, already done in serial_read(). */ | |
802 | } else { | |
803 | /* Error reading byte, but continuing anyway. */ | |
804 | } | |
805 | ||
806 | time = g_get_monotonic_time() - start; | |
807 | time /= 1000; | |
808 | ||
809 | if ((ibuf - i) >= packet_size) { | |
810 | GString *text; | |
811 | /* We have at least a packet's worth of data. */ | |
812 | text = sr_hexdump_new(&buf[i], packet_size); | |
813 | sr_spew("Trying packet: %s", text->str); | |
814 | sr_hexdump_free(text); | |
815 | if (is_valid(&buf[i])) { | |
816 | sr_spew("Found valid %zu-byte packet after " | |
817 | "%" PRIu64 "ms.", (ibuf - i), time); | |
818 | *buflen = ibuf; | |
819 | return SR_OK; | |
820 | } else { | |
821 | sr_spew("Got %zu bytes, but not a valid " | |
822 | "packet.", (ibuf - i)); | |
823 | } | |
824 | /* Not a valid packet. Continue searching. */ | |
825 | i++; | |
826 | } | |
827 | if (time >= timeout_ms) { | |
828 | /* Timeout */ | |
829 | sr_dbg("Detection timed out after %" PRIu64 "ms.", time); | |
830 | break; | |
831 | } | |
832 | if (len < 1) | |
833 | g_usleep(byte_delay_us); | |
834 | } | |
835 | ||
836 | *buflen = ibuf; | |
837 | ||
838 | sr_err("Didn't find a valid packet (read %zu bytes).", *buflen); | |
839 | ||
840 | return SR_ERR; | |
841 | } | |
842 | ||
843 | /** | |
844 | * Extract the serial device and options from the options linked list. | |
845 | * | |
846 | * @param options List of options passed from the command line. | |
847 | * @param serial_device Pointer where to store the extracted serial device. | |
848 | * @param serial_options Pointer where to store the optional extracted serial | |
849 | * options. | |
850 | * | |
851 | * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The | |
852 | * returned string should not be freed by the caller. | |
853 | * | |
854 | * @private | |
855 | */ | |
856 | SR_PRIV int sr_serial_extract_options(GSList *options, | |
857 | const char **serial_device, const char **serial_options) | |
858 | { | |
859 | GSList *l; | |
860 | struct sr_config *src; | |
861 | ||
862 | *serial_device = NULL; | |
863 | ||
864 | for (l = options; l; l = l->next) { | |
865 | src = l->data; | |
866 | switch (src->key) { | |
867 | case SR_CONF_CONN: | |
868 | *serial_device = g_variant_get_string(src->data, NULL); | |
869 | sr_dbg("Parsed serial device: %s.", *serial_device); | |
870 | break; | |
871 | case SR_CONF_SERIALCOMM: | |
872 | *serial_options = g_variant_get_string(src->data, NULL); | |
873 | sr_dbg("Parsed serial options: %s.", *serial_options); | |
874 | break; | |
875 | } | |
876 | } | |
877 | ||
878 | if (!*serial_device) { | |
879 | sr_dbg("No serial device specified."); | |
880 | return SR_ERR; | |
881 | } | |
882 | ||
883 | return SR_OK; | |
884 | } | |
885 | ||
886 | /** @private */ | |
887 | SR_PRIV int serial_source_add(struct sr_session *session, | |
888 | struct sr_serial_dev_inst *serial, int events, int timeout, | |
889 | sr_receive_data_callback cb, void *cb_data) | |
890 | { | |
891 | if ((events & (G_IO_IN | G_IO_ERR)) && (events & G_IO_OUT)) { | |
892 | sr_err("Cannot poll input/error and output simultaneously."); | |
893 | return SR_ERR_ARG; | |
894 | } | |
895 | ||
896 | if (!dev_is_supported(serial)) { | |
897 | sr_err("Invalid serial port."); | |
898 | return SR_ERR_ARG; | |
899 | } | |
900 | ||
901 | if (!serial->lib_funcs || !serial->lib_funcs->setup_source_add) | |
902 | return SR_ERR_NA; | |
903 | ||
904 | return serial->lib_funcs->setup_source_add(session, serial, | |
905 | events, timeout, cb, cb_data); | |
906 | } | |
907 | ||
908 | /** @private */ | |
909 | SR_PRIV int serial_source_remove(struct sr_session *session, | |
910 | struct sr_serial_dev_inst *serial) | |
911 | { | |
912 | if (!dev_is_supported(serial)) { | |
913 | sr_err("Invalid serial port."); | |
914 | return SR_ERR_ARG; | |
915 | } | |
916 | ||
917 | if (!serial->lib_funcs || !serial->lib_funcs->setup_source_remove) | |
918 | return SR_ERR_NA; | |
919 | ||
920 | return serial->lib_funcs->setup_source_remove(session, serial); | |
921 | } | |
922 | ||
923 | /** | |
924 | * Create/allocate a new sr_serial_port structure. | |
925 | * | |
926 | * @param name The OS dependent name of the serial port. Must not be NULL. | |
927 | * @param description An end user friendly description for the serial port. | |
928 | * Can be NULL (in that case the empty string is used | |
929 | * as description). | |
930 | * | |
931 | * @return The newly allocated sr_serial_port struct. | |
932 | */ | |
933 | static struct sr_serial_port *sr_serial_new(const char *name, | |
934 | const char *description) | |
935 | { | |
936 | struct sr_serial_port *serial; | |
937 | ||
938 | if (!name) | |
939 | return NULL; | |
940 | ||
941 | serial = g_malloc0(sizeof(*serial)); | |
942 | serial->name = g_strdup(name); | |
943 | serial->description = g_strdup(description ? description : ""); | |
944 | ||
945 | return serial; | |
946 | } | |
947 | ||
948 | /** | |
949 | * Free a previously allocated sr_serial_port structure. | |
950 | * | |
951 | * @param serial The sr_serial_port struct to free. Must not be NULL. | |
952 | */ | |
953 | SR_API void sr_serial_free(struct sr_serial_port *serial) | |
954 | { | |
955 | if (!serial) | |
956 | return; | |
957 | g_free(serial->name); | |
958 | g_free(serial->description); | |
959 | g_free(serial); | |
960 | } | |
961 | ||
962 | static GSList *append_port_list(GSList *devs, const char *name, const char *desc) | |
963 | { | |
964 | return g_slist_append(devs, sr_serial_new(name, desc)); | |
965 | } | |
966 | ||
967 | /** | |
968 | * List available serial devices. | |
969 | * | |
970 | * @return A GSList of strings containing the path of the serial devices or | |
971 | * NULL if no serial device is found. The returned list must be freed | |
972 | * by the caller. | |
973 | */ | |
974 | SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver) | |
975 | { | |
976 | GSList *tty_devs; | |
977 | GSList *(*list_func)(GSList *list, sr_ser_list_append_t append); | |
978 | ||
979 | /* Currently unused, but will be used by some drivers later on. */ | |
980 | (void)driver; | |
981 | ||
982 | tty_devs = NULL; | |
983 | if (ser_lib_funcs_libsp && ser_lib_funcs_libsp->list) { | |
984 | list_func = ser_lib_funcs_libsp->list; | |
985 | tty_devs = list_func(tty_devs, append_port_list); | |
986 | } | |
987 | if (ser_lib_funcs_hid && ser_lib_funcs_hid->list) { | |
988 | list_func = ser_lib_funcs_hid->list; | |
989 | tty_devs = list_func(tty_devs, append_port_list); | |
990 | } | |
991 | if (ser_lib_funcs_bt && ser_lib_funcs_bt->list) { | |
992 | list_func = ser_lib_funcs_bt->list; | |
993 | tty_devs = list_func(tty_devs, append_port_list); | |
994 | } | |
995 | ||
996 | return tty_devs; | |
997 | } | |
998 | ||
999 | static GSList *append_port_find(GSList *devs, const char *name) | |
1000 | { | |
1001 | if (!name || !*name) | |
1002 | return devs; | |
1003 | ||
1004 | return g_slist_append(devs, g_strdup(name)); | |
1005 | } | |
1006 | ||
1007 | /** | |
1008 | * Find USB serial devices via the USB vendor ID and product ID. | |
1009 | * | |
1010 | * @param[in] vendor_id Vendor ID of the USB device. | |
1011 | * @param[in] product_id Product ID of the USB device. | |
1012 | * | |
1013 | * @return A GSList of strings containing the path of the serial device or | |
1014 | * NULL if no serial device is found. The returned list must be freed | |
1015 | * by the caller. | |
1016 | * | |
1017 | * @private | |
1018 | */ | |
1019 | SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id) | |
1020 | { | |
1021 | GSList *tty_devs; | |
1022 | GSList *(*find_func)(GSList *list, sr_ser_find_append_t append, | |
1023 | uint16_t vid, uint16_t pid); | |
1024 | ||
1025 | tty_devs = NULL; | |
1026 | if (ser_lib_funcs_libsp && ser_lib_funcs_libsp->find_usb) { | |
1027 | find_func = ser_lib_funcs_libsp->find_usb; | |
1028 | tty_devs = find_func(tty_devs, append_port_find, | |
1029 | vendor_id, product_id); | |
1030 | } | |
1031 | if (ser_lib_funcs_hid && ser_lib_funcs_hid->find_usb) { | |
1032 | find_func = ser_lib_funcs_hid->find_usb; | |
1033 | tty_devs = find_func(tty_devs, append_port_find, | |
1034 | vendor_id, product_id); | |
1035 | } | |
1036 | ||
1037 | return tty_devs; | |
1038 | } | |
1039 | ||
1040 | /** @private */ | |
1041 | SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes) | |
1042 | { | |
1043 | int bits, baud, ret, timeout_ms; | |
1044 | ||
1045 | /* Get the bitrate and frame length. */ | |
1046 | bits = baud = 0; | |
1047 | if (port->lib_funcs && port->lib_funcs->get_frame_format) { | |
1048 | ret = port->lib_funcs->get_frame_format(port, &baud, &bits); | |
1049 | if (ret != SR_OK) | |
1050 | bits = baud = 0; | |
1051 | } else { | |
1052 | baud = port->comm_params.bit_rate; | |
1053 | bits = 1 + port->comm_params.data_bits + | |
1054 | port->comm_params.parity_bits + | |
1055 | port->comm_params.stop_bits; | |
1056 | } | |
1057 | ||
1058 | /* Derive the timeout. Default to 1s. */ | |
1059 | timeout_ms = 1000; | |
1060 | if (bits && baud) { | |
1061 | /* Throw in 10ms for misc OS overhead. */ | |
1062 | timeout_ms = 10; | |
1063 | timeout_ms += ((1000.0 / baud) * bits) * num_bytes; | |
1064 | } | |
1065 | ||
1066 | return timeout_ms; | |
1067 | } | |
1068 | ||
1069 | #else | |
1070 | ||
1071 | /* TODO Put fallback.c content here? */ | |
1072 | ||
1073 | #endif | |
1074 | ||
1075 | /** @} */ |