]>
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> |
bce75f94 | 7 | * Copyright (C) 2014 Uffe Jakobsen <uffe@uffe.org> |
b79c3422 | 8 | * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net> |
a1bb33af UH |
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 | ||
6ec6c43b | 24 | #include <config.h> |
54b38f64 | 25 | #include <string.h> |
d02a535e | 26 | #include <stdlib.h> |
a1bb33af | 27 | #include <glib.h> |
b541f837 | 28 | #include <glib/gstdio.h> |
1ac8c218 | 29 | #ifdef HAVE_LIBSERIALPORT |
0d4405ce | 30 | #include <libserialport.h> |
1ac8c218 | 31 | #endif |
c1aae900 | 32 | #include <libsigrok/libsigrok.h> |
45c59c8b | 33 | #include "libsigrok-internal.h" |
00f0016c | 34 | #ifdef _WIN32 |
041b579d DE |
35 | #include <windows.h> /* for HANDLE */ |
36 | #endif | |
a1bb33af | 37 | |
e00b3f58 | 38 | /** @cond PRIVATE */ |
3544f848 | 39 | #define LOG_PREFIX "serial" |
e00b3f58 UH |
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 | */ | |
b19f4622 | 55 | |
1ac8c218 GS |
56 | #ifdef HAVE_SERIAL_COMM |
57 | ||
bf6b9e7b | 58 | /* See if an (assumed opened) serial port is of any supported type. */ |
a7b8692e GS |
59 | static int dev_is_supported(struct sr_serial_dev_inst *serial) |
60 | { | |
bf6b9e7b | 61 | if (!serial || !serial->lib_funcs) |
a7b8692e GS |
62 | return 0; |
63 | ||
64 | return 1; | |
65 | } | |
66 | ||
b19f4622 UH |
67 | /** |
68 | * Open the specified serial port. | |
69 | * | |
299bdb24 | 70 | * @param serial Previously initialized serial port structure. |
a9cf2035 | 71 | * @param[in] flags Flags to use when opening the serial port. Possible flags |
d9251a2c | 72 | * include SERIAL_RDWR, SERIAL_RDONLY. |
b19f4622 | 73 | * |
299bdb24 BV |
74 | * If the serial structure contains a serialcomm string, it will be |
75 | * passed to serial_set_paramstr() after the port is opened. | |
76 | * | |
d0a92abd MH |
77 | * @retval SR_OK Success. |
78 | * @retval SR_ERR Failure. | |
e00b3f58 UH |
79 | * |
80 | * @private | |
b19f4622 | 81 | */ |
299bdb24 | 82 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) |
d02a535e | 83 | { |
a9bce5a5 | 84 | int ret; |
b19f4622 | 85 | |
299bdb24 BV |
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); | |
b19f4622 | 92 | |
4417074c GS |
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; | |
b79c3422 GS |
100 | else if (ser_name_is_bt(serial)) |
101 | serial->lib_funcs = ser_lib_funcs_bt; | |
4417074c GS |
102 | else |
103 | serial->lib_funcs = ser_lib_funcs_libsp; | |
a7b8692e GS |
104 | if (!serial->lib_funcs) |
105 | return SR_ERR_NA; | |
106 | ||
ad5aa993 GS |
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 | ||
a7b8692e GS |
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); | |
ae4c1fb6 GS |
122 | if (ret != SR_OK) |
123 | return ret; | |
a54dd31e | 124 | |
cb828f1b WS |
125 | if (serial->serialcomm) { |
126 | ret = serial_set_paramstr(serial, serial->serialcomm); | |
127 | if (ret != SR_OK) | |
128 | return ret; | |
129 | } | |
130 | ||
0dc27cd1 GS |
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; | |
d02a535e BV |
143 | } |
144 | ||
b19f4622 UH |
145 | /** |
146 | * Close the specified serial port. | |
147 | * | |
299bdb24 | 148 | * @param serial Previously initialized serial port structure. |
b19f4622 | 149 | * |
d0a92abd MH |
150 | * @retval SR_OK Success. |
151 | * @retval SR_ERR Failure. | |
e00b3f58 UH |
152 | * |
153 | * @private | |
2119ab03 | 154 | */ |
299bdb24 | 155 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) |
d02a535e | 156 | { |
ad5aa993 GS |
157 | int rc; |
158 | ||
299bdb24 BV |
159 | if (!serial) { |
160 | sr_dbg("Invalid serial port."); | |
161 | return SR_ERR; | |
162 | } | |
163 | ||
64ecf7ee | 164 | sr_spew("Closing serial port %s.", serial->port); |
b19f4622 | 165 | |
a7b8692e GS |
166 | if (!serial->lib_funcs || !serial->lib_funcs->close) |
167 | return SR_ERR_NA; | |
168 | ||
ad5aa993 GS |
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; | |
d02a535e BV |
176 | } |
177 | ||
b19f4622 | 178 | /** |
6213c38e | 179 | * Flush serial port buffers. Empty buffers, discard pending RX and TX data. |
b19f4622 | 180 | * |
299bdb24 | 181 | * @param serial Previously initialized serial port structure. |
b19f4622 | 182 | * |
d0a92abd MH |
183 | * @retval SR_OK Success. |
184 | * @retval SR_ERR Failure. | |
e00b3f58 UH |
185 | * |
186 | * @private | |
1fdb75e1 | 187 | */ |
299bdb24 | 188 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) |
06d64eb8 | 189 | { |
299bdb24 BV |
190 | if (!serial) { |
191 | sr_dbg("Invalid serial port."); | |
192 | return SR_ERR; | |
193 | } | |
194 | ||
64ecf7ee | 195 | sr_spew("Flushing serial port %s.", serial->port); |
b19f4622 | 196 | |
ad5aa993 GS |
197 | sr_ser_discard_queued_data(serial); |
198 | ||
a7b8692e GS |
199 | if (!serial->lib_funcs || !serial->lib_funcs->flush) |
200 | return SR_ERR_NA; | |
201 | ||
202 | return serial->lib_funcs->flush(serial); | |
06d64eb8 BV |
203 | } |
204 | ||
bce75f94 | 205 | /** |
6213c38e | 206 | * Drain serial port buffers. Wait for pending TX data to be sent. |
bce75f94 UJ |
207 | * |
208 | * @param serial Previously initialized serial port structure. | |
209 | * | |
210 | * @retval SR_OK Success. | |
211 | * @retval SR_ERR Failure. | |
e00b3f58 UH |
212 | * |
213 | * @private | |
bce75f94 UJ |
214 | */ |
215 | SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial) | |
216 | { | |
bce75f94 UJ |
217 | if (!serial) { |
218 | sr_dbg("Invalid serial port."); | |
219 | return SR_ERR; | |
220 | } | |
221 | ||
bce75f94 UJ |
222 | sr_spew("Draining serial port %s.", serial->port); |
223 | ||
a7b8692e GS |
224 | if (!serial->lib_funcs || !serial->lib_funcs->drain) |
225 | return SR_ERR_NA; | |
226 | ||
227 | return serial->lib_funcs->drain(serial); | |
bce75f94 UJ |
228 | } |
229 | ||
ad5aa993 GS |
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. | |
d4787248 GS |
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. | |
ad5aa993 GS |
249 | */ |
250 | ||
d4787248 GS |
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 | * | |
6762401d | 261 | * Callbacks get unregistered by specifying NULL for the 'cb' parameter. |
82b9f3d1 UH |
262 | * |
263 | * @private | |
d4787248 GS |
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 | ||
ad5aa993 GS |
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 | * | |
82b9f3d1 | 283 | * @private |
ad5aa993 GS |
284 | */ |
285 | SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial) | |
286 | { | |
bf6b9e7b | 287 | if (!serial || !serial->rcv_buffer) |
ad5aa993 GS |
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 | * | |
82b9f3d1 | 299 | * @private |
ad5aa993 GS |
300 | */ |
301 | SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial) | |
302 | { | |
bf6b9e7b | 303 | if (!serial || !serial->rcv_buffer) |
ad5aa993 GS |
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 | * | |
82b9f3d1 | 317 | * @private |
ad5aa993 GS |
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 | { | |
bf6b9e7b | 322 | if (!serial || !data || !len) |
ad5aa993 GS |
323 | return; |
324 | ||
d4787248 GS |
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) | |
ad5aa993 GS |
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 | * | |
82b9f3d1 | 339 | * @private |
ad5aa993 GS |
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 | ||
bf6b9e7b | 347 | if (!serial || !data || !len) |
ad5aa993 GS |
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 | ||
8c3df6e5 GS |
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. | |
82b9f3d1 UH |
374 | * |
375 | * @private | |
8c3df6e5 GS |
376 | */ |
377 | SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial) | |
378 | { | |
ad5aa993 | 379 | size_t lib_count, buf_count; |
a7b8692e GS |
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 | ||
ad5aa993 GS |
388 | buf_count = sr_ser_has_queued_data(serial); |
389 | ||
390 | return lib_count + buf_count; | |
8c3df6e5 GS |
391 | } |
392 | ||
9a474211 | 393 | static int _serial_write(struct sr_serial_dev_inst *serial, |
ae4c1fb6 GS |
394 | const void *buf, size_t count, |
395 | int nonblocking, unsigned int timeout_ms) | |
2119ab03 | 396 | { |
299bdb24 BV |
397 | ssize_t ret; |
398 | ||
399 | if (!serial) { | |
400 | sr_dbg("Invalid serial port."); | |
6a76efeb | 401 | return SR_ERR; |
299bdb24 BV |
402 | } |
403 | ||
a7b8692e GS |
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); | |
6433156c | 408 | sr_spew("Wrote %zd/%zu bytes.", ret, count); |
b19f4622 UH |
409 | |
410 | return ret; | |
2119ab03 UH |
411 | } |
412 | ||
b19f4622 | 413 | /** |
98edee79 | 414 | * Write a number of bytes to the specified serial port, blocking until finished. |
b19f4622 | 415 | * |
299bdb24 | 416 | * @param serial Previously initialized serial port structure. |
a9cf2035 MH |
417 | * @param[in] buf Buffer containing the bytes to write. |
418 | * @param[in] count Number of bytes to write. | |
eead2782 | 419 | * @param[in] timeout_ms Timeout in ms, or 0 for no timeout. |
b19f4622 | 420 | * |
a9cf2035 MH |
421 | * @retval SR_ERR_ARG Invalid argument. |
422 | * @retval SR_ERR Other error. | |
eead2782 ML |
423 | * @retval other The number of bytes written. If this is less than the number |
424 | * specified in the call, the timeout was reached. | |
e00b3f58 UH |
425 | * |
426 | * @private | |
2119ab03 | 427 | */ |
9a474211 | 428 | SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial, |
ae4c1fb6 | 429 | const void *buf, size_t count, unsigned int timeout_ms) |
9a474211 | 430 | { |
eead2782 | 431 | return _serial_write(serial, buf, count, 0, timeout_ms); |
9a474211 ML |
432 | } |
433 | ||
a9cf2035 MH |
434 | /** |
435 | * Write a number of bytes to the specified serial port, return immediately. | |
98edee79 ML |
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. | |
e00b3f58 UH |
444 | * |
445 | * @private | |
446 | */ | |
9a474211 | 447 | SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial, |
ae4c1fb6 | 448 | const void *buf, size_t count) |
9a474211 | 449 | { |
eead2782 | 450 | return _serial_write(serial, buf, count, 1, 0); |
9a474211 ML |
451 | } |
452 | ||
ae4c1fb6 GS |
453 | static int _serial_read(struct sr_serial_dev_inst *serial, |
454 | void *buf, size_t count, int nonblocking, unsigned int timeout_ms) | |
2119ab03 | 455 | { |
299bdb24 BV |
456 | ssize_t ret; |
457 | ||
458 | if (!serial) { | |
459 | sr_dbg("Invalid serial port."); | |
6a76efeb | 460 | return SR_ERR; |
299bdb24 BV |
461 | } |
462 | ||
a7b8692e GS |
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); | |
c4d85a40 | 467 | if (ret > 0) |
6433156c | 468 | sr_spew("Read %zd/%zu bytes.", ret, count); |
b19f4622 UH |
469 | |
470 | return ret; | |
2119ab03 UH |
471 | } |
472 | ||
9a474211 | 473 | /** |
98edee79 | 474 | * Read a number of bytes from the specified serial port, block until finished. |
9a474211 ML |
475 | * |
476 | * @param serial Previously initialized serial port structure. | |
477 | * @param buf Buffer where to store the bytes that are read. | |
a9cf2035 | 478 | * @param[in] count The number of bytes to read. |
eead2782 | 479 | * @param[in] timeout_ms Timeout in ms, or 0 for no timeout. |
9a474211 | 480 | * |
a9cf2035 | 481 | * @retval SR_ERR_ARG Invalid argument. |
d9251a2c UH |
482 | * @retval SR_ERR Other error. |
483 | * @retval other The number of bytes read. If this is less than the number | |
eead2782 | 484 | * requested, the timeout was reached. |
e00b3f58 UH |
485 | * |
486 | * @private | |
9a474211 | 487 | */ |
ae4c1fb6 GS |
488 | SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, |
489 | void *buf, size_t count, unsigned int timeout_ms) | |
9a474211 | 490 | { |
eead2782 | 491 | return _serial_read(serial, buf, count, 0, timeout_ms); |
9a474211 ML |
492 | } |
493 | ||
a9cf2035 MH |
494 | /** |
495 | * Try to read up to @a count bytes from the specified serial port, return | |
496 | * immediately with what's available. | |
98edee79 ML |
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. | |
d9251a2c UH |
503 | * @retval SR_ERR Other error. |
504 | * @retval other The number of bytes read. | |
e00b3f58 UH |
505 | * |
506 | * @private | |
a9cf2035 | 507 | */ |
ae4c1fb6 GS |
508 | SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, |
509 | void *buf, size_t count) | |
9a474211 | 510 | { |
eead2782 | 511 | return _serial_read(serial, buf, count, 1, 0); |
9a474211 ML |
512 | } |
513 | ||
b19f4622 UH |
514 | /** |
515 | * Set serial parameters for the specified serial port. | |
516 | * | |
299bdb24 | 517 | * @param serial Previously initialized serial port structure. |
04cb9157 MH |
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, | |
d9251a2c | 523 | * 1 = RTS/CTS, 2 = XON/XOFF). |
04cb9157 MH |
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). | |
2119ab03 | 526 | * |
d0a92abd | 527 | * @retval SR_OK Success. |
04cb9157 | 528 | * @retval SR_ERR Failure. |
e00b3f58 UH |
529 | * |
530 | * @private | |
1ff7712c | 531 | */ |
ae4c1fb6 GS |
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) | |
d02a535e | 535 | { |
a9bce5a5 | 536 | int ret; |
a9bce5a5 | 537 | |
299bdb24 BV |
538 | if (!serial) { |
539 | sr_dbg("Invalid serial port."); | |
540 | return SR_ERR; | |
541 | } | |
542 | ||
64ecf7ee | 543 | sr_spew("Setting serial parameters on port %s.", serial->port); |
b19f4622 | 544 | |
a7b8692e GS |
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); | |
ae4c1fb6 GS |
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); | |
13cd8197 | 559 | } |
639c6f61 | 560 | |
ae4c1fb6 | 561 | return ret; |
d02a535e | 562 | } |
792fc686 | 563 | |
3ad30b4e GS |
564 | /** |
565 | * Manipulate handshake state for the specified serial port. | |
566 | * | |
567 | * @param serial Previously initialized serial port structure. | |
568 | * @param[in] rts Status of RTS line (0 or 1; or -1 to ignore). | |
569 | * @param[in] dtr Status of DTR line (0 or 1; or -1 to ignore). | |
570 | * | |
571 | * @retval SR_OK Success. | |
572 | * @retval SR_ERR Failure. | |
573 | * | |
574 | * @private | |
575 | */ | |
576 | SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial, | |
577 | int rts, int dtr) | |
578 | { | |
579 | int ret; | |
580 | ||
581 | if (!serial) { | |
582 | sr_dbg("Invalid serial port."); | |
583 | return SR_ERR; | |
584 | } | |
585 | ||
586 | sr_spew("Modifying serial parameters on port %s.", serial->port); | |
587 | ||
588 | if (!serial->lib_funcs || !serial->lib_funcs->set_handshake) | |
589 | return SR_ERR_NA; | |
590 | ret = serial->lib_funcs->set_handshake(serial, rts, dtr); | |
591 | ||
592 | return ret; | |
593 | } | |
594 | ||
299bdb24 | 595 | /** |
48d3238e | 596 | * Set serial parameters for the specified serial port from parameter string. |
299bdb24 BV |
597 | * |
598 | * @param serial Previously initialized serial port structure. | |
48d3238e MH |
599 | * @param[in] paramstr A serial communication parameters string of the form |
600 | * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n | |
d9251a2c | 601 | * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n |
48d3238e MH |
602 | * \<baudrate\>=integer Baud rate.\n |
603 | * \<bits\>=5|6|7|8 Number of data bits.\n | |
604 | * \<parity\>=n|e|o None, even, odd.\n | |
605 | * \<stopbits\>=1|2 One or two stop bits.\n | |
606 | * Options:\n | |
607 | * dtr=0|1 Set DTR off resp. on.\n | |
608 | * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n | |
609 | * rts=0|1 Set RTS off resp. on.\n | |
610 | * Please note that values and combinations of these parameters must be | |
611 | * supported by the concrete serial interface hardware and the drivers for it. | |
e00b3f58 | 612 | * |
04cb9157 MH |
613 | * @retval SR_OK Success. |
614 | * @retval SR_ERR Failure. | |
e00b3f58 UH |
615 | * |
616 | * @private | |
299bdb24 | 617 | */ |
299bdb24 | 618 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, |
ae4c1fb6 | 619 | const char *paramstr) |
792fc686 | 620 | { |
e00b3f58 | 621 | /** @cond PRIVATE */ |
dbb3e2ad | 622 | #define SERIAL_COMM_SPEC "^(\\d+)(/([5678])([neo])([12]))?(.*)$" |
e00b3f58 | 623 | /** @endcond */ |
48d3238e | 624 | |
792fc686 BV |
625 | GRegex *reg; |
626 | GMatchInfo *match; | |
26ddb5ba | 627 | int speed, databits, parity, stopbits, flow, rts, dtr, i; |
71caaad4 | 628 | char *mstr, **opts, **kv; |
792fc686 | 629 | |
dbb3e2ad GS |
630 | speed = flow = 0; |
631 | databits = 8; | |
632 | parity = SP_PARITY_NONE; | |
633 | stopbits = 1; | |
71caaad4 | 634 | rts = dtr = -1; |
ea088bb6 | 635 | sr_spew("Parsing parameters from \"%s\".", paramstr); |
792fc686 BV |
636 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); |
637 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
638 | if ((mstr = g_match_info_fetch(match, 1))) | |
639 | speed = strtoul(mstr, NULL, 10); | |
640 | g_free(mstr); | |
dbb3e2ad | 641 | if ((mstr = g_match_info_fetch(match, 3)) && mstr[0]) |
792fc686 BV |
642 | databits = strtoul(mstr, NULL, 10); |
643 | g_free(mstr); | |
dbb3e2ad | 644 | if ((mstr = g_match_info_fetch(match, 4)) && mstr[0]) { |
792fc686 BV |
645 | switch (mstr[0]) { |
646 | case 'n': | |
4d6a5008 | 647 | parity = SP_PARITY_NONE; |
792fc686 BV |
648 | break; |
649 | case 'e': | |
4d6a5008 | 650 | parity = SP_PARITY_EVEN; |
792fc686 BV |
651 | break; |
652 | case 'o': | |
4d6a5008 | 653 | parity = SP_PARITY_ODD; |
792fc686 BV |
654 | break; |
655 | } | |
656 | } | |
657 | g_free(mstr); | |
dbb3e2ad | 658 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0]) |
792fc686 BV |
659 | stopbits = strtoul(mstr, NULL, 10); |
660 | g_free(mstr); | |
dbb3e2ad | 661 | if ((mstr = g_match_info_fetch(match, 6)) && mstr[0] != '\0') { |
71caaad4 BV |
662 | if (mstr[0] != '/') { |
663 | sr_dbg("missing separator before extra options"); | |
664 | speed = 0; | |
665 | } else { | |
666 | /* A set of "key=value" options separated by / */ | |
667 | opts = g_strsplit(mstr + 1, "/", 0); | |
668 | for (i = 0; opts[i]; i++) { | |
669 | kv = g_strsplit(opts[i], "=", 2); | |
670 | if (!strncmp(kv[0], "rts", 3)) { | |
671 | if (kv[1][0] == '1') | |
672 | rts = 1; | |
673 | else if (kv[1][0] == '0') | |
674 | rts = 0; | |
675 | else { | |
676 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
677 | speed = 0; | |
678 | } | |
679 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
680 | if (kv[1][0] == '1') | |
681 | dtr = 1; | |
682 | else if (kv[1][0] == '0') | |
683 | dtr = 0; | |
684 | else { | |
685 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
686 | speed = 0; | |
687 | } | |
26ddb5ba | 688 | } else if (!strncmp(kv[0], "flow", 4)) { |
689 | if (kv[1][0] == '0') | |
690 | flow = 0; | |
691 | else if (kv[1][0] == '1') | |
692 | flow = 1; | |
693 | else if (kv[1][0] == '2') | |
694 | flow = 2; | |
695 | else { | |
696 | sr_dbg("invalid value for flow: %c", kv[1][0]); | |
697 | speed = 0; | |
698 | } | |
71caaad4 BV |
699 | } |
700 | g_strfreev(kv); | |
701 | } | |
702 | g_strfreev(opts); | |
703 | } | |
704 | } | |
705 | g_free(mstr); | |
792fc686 BV |
706 | } |
707 | g_match_info_unref(match); | |
708 | g_regex_unref(reg); | |
dbb3e2ad GS |
709 | sr_spew("Got params: rate %d, frame %d/%d/%d, flow %d, rts %d, dtr %d.", |
710 | speed, databits, parity, stopbits, flow, rts, dtr); | |
792fc686 | 711 | |
b1184024 | 712 | if (!speed) { |
ea088bb6 | 713 | sr_dbg("Could not infer speed from parameter string."); |
792fc686 | 714 | return SR_ERR_ARG; |
ea088bb6 | 715 | } |
b1184024 GS |
716 | |
717 | return serial_set_params(serial, speed, | |
718 | databits, parity, stopbits, | |
719 | flow, rts, dtr); | |
792fc686 BV |
720 | } |
721 | ||
299bdb24 BV |
722 | /** |
723 | * Read a line from the specified serial port. | |
724 | * | |
6213c38e GS |
725 | * @param[in] serial Previously initialized serial port structure. |
726 | * @param[out] buf Buffer where to store the bytes that are read. | |
727 | * @param[in] buflen Size of the buffer. | |
04cb9157 | 728 | * @param[in] timeout_ms How long to wait for a line to come in. |
299bdb24 | 729 | * |
6213c38e | 730 | * Reading stops when CR or LF is found, which is stripped from the buffer. |
299bdb24 | 731 | * |
04cb9157 MH |
732 | * @retval SR_OK Success. |
733 | * @retval SR_ERR Failure. | |
e00b3f58 UH |
734 | * |
735 | * @private | |
299bdb24 | 736 | */ |
ae4c1fb6 GS |
737 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, |
738 | char **buf, int *buflen, gint64 timeout_ms) | |
6f22a8ef | 739 | { |
bf505eed | 740 | gint64 start, remaining; |
6f22a8ef UH |
741 | int maxlen, len; |
742 | ||
64ecf7ee | 743 | if (!serial) { |
299bdb24 BV |
744 | sr_dbg("Invalid serial port."); |
745 | return SR_ERR; | |
746 | } | |
747 | ||
a7b8692e | 748 | if (!dev_is_supported(serial)) { |
64ecf7ee | 749 | sr_dbg("Cannot use unopened serial port %s.", serial->port); |
299bdb24 BV |
750 | return -1; |
751 | } | |
752 | ||
6f22a8ef | 753 | start = g_get_monotonic_time(); |
bf505eed | 754 | remaining = timeout_ms; |
6f22a8ef UH |
755 | |
756 | maxlen = *buflen; | |
757 | *buflen = len = 0; | |
0c5f2abc | 758 | while (1) { |
6f22a8ef UH |
759 | len = maxlen - *buflen - 1; |
760 | if (len < 1) | |
761 | break; | |
ae4c1fb6 | 762 | len = serial_read_blocking(serial, *buf + *buflen, 1, remaining); |
6f22a8ef UH |
763 | if (len > 0) { |
764 | *buflen += len; | |
765 | *(*buf + *buflen) = '\0'; | |
318dd53c BV |
766 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' |
767 | || *(*buf + *buflen - 1) == '\n')) { | |
768 | /* Strip CR/LF and terminate. */ | |
6f22a8ef UH |
769 | *(*buf + --*buflen) = '\0'; |
770 | break; | |
771 | } | |
772 | } | |
bf505eed ML |
773 | /* Reduce timeout by time elapsed. */ |
774 | remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000); | |
775 | if (remaining <= 0) | |
6f22a8ef UH |
776 | /* Timeout */ |
777 | break; | |
5715e84f DT |
778 | if (len < 1) |
779 | g_usleep(2000); | |
6f22a8ef | 780 | } |
318dd53c BV |
781 | if (*buflen) |
782 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
6f22a8ef UH |
783 | |
784 | return SR_OK; | |
785 | } | |
766456be UH |
786 | |
787 | /** | |
788 | * Try to find a valid packet in a serial data stream. | |
789 | * | |
1a7adeac GS |
790 | * @param[in] serial Previously initialized serial port structure. |
791 | * @param[in] buf Buffer containing the bytes to write. | |
792 | * @param[in] buflen Size of the buffer. | |
04cb9157 | 793 | * @param[in] packet_size Size, in bytes, of a valid packet. |
1a7adeac GS |
794 | * @param[in] is_valid Callback that assesses whether the packet is valid or not. |
795 | * @param[in] is_valid_len Callback which checks a variable length packet. | |
796 | * @param[out] return_size Detected packet size in case of successful match. | |
04cb9157 | 797 | * @param[in] timeout_ms The timeout after which, if no packet is detected, to |
d9251a2c | 798 | * abort scanning. |
766456be | 799 | * |
1a7adeac GS |
800 | * Data is received from the serial port and into the caller provided |
801 | * buffer, until the buffer is exhausted, or the timeout has expired, | |
802 | * or a valid packet was found. Receive data is passed to the caller | |
803 | * provided validity check routine, assuming either fixed size packets | |
804 | * (#is_valid parameter, exact match to the #packet_size length) or | |
805 | * packets of variable length (#is_valid_len parameter, minimum length | |
806 | * #packet_size required for first invocation). | |
807 | * | |
d0a92abd | 808 | * @retval SR_OK Valid packet was found within the given timeout. |
04cb9157 | 809 | * @retval SR_ERR Failure. |
e00b3f58 UH |
810 | * |
811 | * @private | |
766456be UH |
812 | */ |
813 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
ae4c1fb6 | 814 | uint8_t *buf, size_t *buflen, |
1a7adeac GS |
815 | size_t packet_size, packet_valid_callback is_valid, |
816 | packet_valid_len_callback is_valid_len, size_t *return_size, | |
d03815a0 | 817 | uint64_t timeout_ms) |
766456be | 818 | { |
1a7adeac GS |
819 | uint64_t start_us, elapsed_ms, byte_delay_us; |
820 | size_t fill_idx, check_idx, max_fill_idx; | |
821 | ssize_t recv_len; | |
822 | const uint8_t *check_ptr; | |
823 | size_t check_len, pkt_len; | |
824 | gboolean do_dump; | |
825 | int ret; | |
766456be | 826 | |
d03815a0 GS |
827 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 "ms).", |
828 | serial->port, timeout_ms); | |
766456be | 829 | |
1a7adeac GS |
830 | max_fill_idx = *buflen; |
831 | if (max_fill_idx < 2 * packet_size) { | |
832 | /* | |
833 | * The heuristics in this check is only useful for fixed | |
834 | * packet length scenarios, but for variable length setups | |
835 | * we don't know the packets' sizes up front. | |
836 | */ | |
837 | sr_err("Small stream detect RX buffer, want 2x packet size."); | |
838 | return SR_ERR_ARG; | |
766456be UH |
839 | } |
840 | ||
d03815a0 | 841 | byte_delay_us = serial_timeout(serial, 1) * 1000; |
1a7adeac GS |
842 | start_us = g_get_monotonic_time(); |
843 | ||
844 | check_idx = fill_idx = 0; | |
845 | while (fill_idx < max_fill_idx) { | |
846 | /* | |
847 | * Read bytes individually. Lets callers continue to | |
848 | * successfully process next RX data after first match. | |
849 | * Run full loop bodies for empty or failed reception | |
850 | * in an iteration, to have timeouts checked. | |
851 | */ | |
852 | recv_len = serial_read_nonblocking(serial, &buf[fill_idx], 1); | |
853 | if (recv_len > 0) | |
854 | fill_idx += recv_len; | |
855 | ||
856 | /* Dump receive data when (a minimum) size is reached. */ | |
857 | check_ptr = &buf[check_idx]; | |
858 | check_len = fill_idx - check_idx; | |
859 | do_dump = check_len >= packet_size; | |
860 | do_dump &= sr_log_loglevel_get() >= SR_LOG_SPEW; | |
861 | if (do_dump) { | |
862 | GString *text; | |
766456be | 863 | |
1a7adeac GS |
864 | text = sr_hexdump_new(check_ptr, check_len); |
865 | sr_spew("Trying packet: len %zu, bytes %s", | |
866 | check_len, text->str); | |
867 | sr_hexdump_free(text); | |
766456be | 868 | } |
551c3d8c | 869 | |
1a7adeac GS |
870 | /* A packet's (minimum) length was received, check its data. */ |
871 | elapsed_ms = g_get_monotonic_time() - start_us; | |
872 | elapsed_ms /= 1000; | |
873 | if (is_valid_len && check_len >= packet_size) { | |
874 | pkt_len = packet_size; | |
875 | ret = is_valid_len(NULL, check_ptr, check_len, &pkt_len); | |
876 | if (ret == SR_PACKET_VALID) { | |
877 | /* Exact match. Terminate with success. */ | |
878 | sr_spew("Valid packet after %" PRIu64 "ms.", | |
879 | elapsed_ms); | |
880 | sr_spew("RX count %zu, packet len %zu.", | |
881 | fill_idx, pkt_len); | |
882 | *buflen = fill_idx; | |
883 | if (return_size) | |
884 | *return_size = pkt_len; | |
766456be | 885 | return SR_OK; |
1a7adeac GS |
886 | } |
887 | if (ret == SR_PACKET_NEED_RX) { | |
888 | /* Incomplete, keep accumulating RX data. */ | |
889 | sr_spew("Checker needs more RX data."); | |
766456be | 890 | } else { |
1a7adeac GS |
891 | /* Not a valid packet. Continue searching. */ |
892 | sr_spew("Invalid packet, advancing read pos."); | |
893 | check_idx++; | |
894 | } | |
895 | } | |
896 | if (is_valid && check_len >= packet_size) { | |
897 | if (is_valid(check_ptr)) { | |
898 | /* Exact match. Terminate with success. */ | |
899 | sr_spew("Valid packet after %" PRIu64 "ms.", | |
900 | elapsed_ms); | |
901 | sr_spew("RX count %zu, packet len %zu.", | |
902 | fill_idx, packet_size); | |
903 | *buflen = fill_idx; | |
904 | if (return_size) | |
905 | *return_size = packet_size; | |
906 | return SR_OK; | |
766456be UH |
907 | } |
908 | /* Not a valid packet. Continue searching. */ | |
1a7adeac GS |
909 | sr_spew("Invalid packet, advancing read pointer."); |
910 | check_idx++; | |
766456be | 911 | } |
1a7adeac GS |
912 | |
913 | /* Check for packet search timeout. */ | |
914 | if (elapsed_ms >= timeout_ms) { | |
915 | sr_dbg("Detection timed out after %" PRIu64 "ms.", | |
916 | elapsed_ms); | |
766456be UH |
917 | break; |
918 | } | |
1a7adeac | 919 | if (recv_len < 1) |
5715e84f | 920 | g_usleep(byte_delay_us); |
766456be | 921 | } |
1a7adeac GS |
922 | sr_info("Didn't find a valid packet (read %zu bytes).", fill_idx); |
923 | *buflen = fill_idx; | |
766456be UH |
924 | |
925 | return SR_ERR; | |
926 | } | |
1bd9e678 DJ |
927 | |
928 | /** | |
929 | * Extract the serial device and options from the options linked list. | |
930 | * | |
9d6a8960 GS |
931 | * The caller's passed in references get updated when the list of options |
932 | * contains one of the desired parameters. This lets callers pre-assign | |
933 | * default values which take effect in the absence of user specifications. | |
934 | * Either reference is optional, passing #NULL is acceptable. | |
935 | * | |
936 | * Callers must not free returned strings. These shall be considered | |
937 | * read-only handles to data that is managed elsewhere. | |
938 | * | |
939 | * @param[in] options List of options passed from the command line. | |
940 | * @param[out] serial_device Pointer where to store the extracted serial device. | |
941 | * @param[out] serial_options Pointer where to store the optional extracted serial | |
1bd9e678 DJ |
942 | * options. |
943 | * | |
9d6a8960 | 944 | * @return SR_OK if a serial_device is found, SR_ERR if no device is found. |
e00b3f58 UH |
945 | * |
946 | * @private | |
1bd9e678 | 947 | */ |
ae4c1fb6 GS |
948 | SR_PRIV int sr_serial_extract_options(GSList *options, |
949 | const char **serial_device, const char **serial_options) | |
1bd9e678 DJ |
950 | { |
951 | GSList *l; | |
952 | struct sr_config *src; | |
953 | ||
1bd9e678 DJ |
954 | for (l = options; l; l = l->next) { |
955 | src = l->data; | |
956 | switch (src->key) { | |
957 | case SR_CONF_CONN: | |
9d6a8960 GS |
958 | if (!serial_device) |
959 | break; | |
1bd9e678 | 960 | *serial_device = g_variant_get_string(src->data, NULL); |
b1a7ca3b | 961 | sr_dbg("Parsed serial device: %s.", *serial_device); |
1bd9e678 | 962 | break; |
1bd9e678 | 963 | case SR_CONF_SERIALCOMM: |
9d6a8960 GS |
964 | if (!serial_options) |
965 | break; | |
1bd9e678 | 966 | *serial_options = g_variant_get_string(src->data, NULL); |
b1a7ca3b | 967 | sr_dbg("Parsed serial options: %s.", *serial_options); |
1bd9e678 DJ |
968 | break; |
969 | } | |
970 | } | |
971 | ||
9d6a8960 | 972 | if (serial_device && !*serial_device) { |
b1a7ca3b | 973 | sr_dbg("No serial device specified."); |
1bd9e678 DJ |
974 | return SR_ERR; |
975 | } | |
976 | ||
977 | return SR_OK; | |
978 | } | |
abc4b335 | 979 | |
e00b3f58 | 980 | /** @private */ |
102f1239 | 981 | SR_PRIV int serial_source_add(struct sr_session *session, |
ae4c1fb6 GS |
982 | struct sr_serial_dev_inst *serial, int events, int timeout, |
983 | sr_receive_data_callback cb, void *cb_data) | |
ba1949f5 | 984 | { |
ae4c1fb6 | 985 | if ((events & (G_IO_IN | G_IO_ERR)) && (events & G_IO_OUT)) { |
cbc1413f DE |
986 | sr_err("Cannot poll input/error and output simultaneously."); |
987 | return SR_ERR_ARG; | |
988 | } | |
989 | ||
a7b8692e | 990 | if (!dev_is_supported(serial)) { |
ae4c1fb6 GS |
991 | sr_err("Invalid serial port."); |
992 | return SR_ERR_ARG; | |
ba1949f5 ML |
993 | } |
994 | ||
a7b8692e GS |
995 | if (!serial->lib_funcs || !serial->lib_funcs->setup_source_add) |
996 | return SR_ERR_NA; | |
997 | ||
998 | return serial->lib_funcs->setup_source_add(session, serial, | |
ae4c1fb6 | 999 | events, timeout, cb, cb_data); |
abc4b335 | 1000 | } |
7faa3e88 | 1001 | |
e00b3f58 | 1002 | /** @private */ |
102f1239 | 1003 | SR_PRIV int serial_source_remove(struct sr_session *session, |
ae4c1fb6 | 1004 | struct sr_serial_dev_inst *serial) |
7faa3e88 | 1005 | { |
a7b8692e | 1006 | if (!dev_is_supported(serial)) { |
ae4c1fb6 GS |
1007 | sr_err("Invalid serial port."); |
1008 | return SR_ERR_ARG; | |
1009 | } | |
1010 | ||
a7b8692e GS |
1011 | if (!serial->lib_funcs || !serial->lib_funcs->setup_source_remove) |
1012 | return SR_ERR_NA; | |
1013 | ||
1014 | return serial->lib_funcs->setup_source_remove(session, serial); | |
7faa3e88 | 1015 | } |
b541f837 | 1016 | |
b1a7ca3b UH |
1017 | /** |
1018 | * Create/allocate a new sr_serial_port structure. | |
1019 | * | |
1020 | * @param name The OS dependent name of the serial port. Must not be NULL. | |
1021 | * @param description An end user friendly description for the serial port. | |
1022 | * Can be NULL (in that case the empty string is used | |
1023 | * as description). | |
1024 | * | |
1025 | * @return The newly allocated sr_serial_port struct. | |
1026 | */ | |
24287ea9 | 1027 | static struct sr_serial_port *sr_serial_new(const char *name, |
ae4c1fb6 | 1028 | const char *description) |
24287ea9 AJ |
1029 | { |
1030 | struct sr_serial_port *serial; | |
1031 | ||
1032 | if (!name) | |
1033 | return NULL; | |
1034 | ||
e47a9562 | 1035 | serial = g_malloc0(sizeof(*serial)); |
24287ea9 AJ |
1036 | serial->name = g_strdup(name); |
1037 | serial->description = g_strdup(description ? description : ""); | |
b1a7ca3b | 1038 | |
24287ea9 AJ |
1039 | return serial; |
1040 | } | |
1041 | ||
b1a7ca3b UH |
1042 | /** |
1043 | * Free a previously allocated sr_serial_port structure. | |
1044 | * | |
1045 | * @param serial The sr_serial_port struct to free. Must not be NULL. | |
1046 | */ | |
24287ea9 AJ |
1047 | SR_API void sr_serial_free(struct sr_serial_port *serial) |
1048 | { | |
b1a7ca3b | 1049 | if (!serial) |
24287ea9 AJ |
1050 | return; |
1051 | g_free(serial->name); | |
1052 | g_free(serial->description); | |
1053 | g_free(serial); | |
1054 | } | |
1055 | ||
ae4c1fb6 GS |
1056 | static GSList *append_port_list(GSList *devs, const char *name, const char *desc) |
1057 | { | |
1058 | return g_slist_append(devs, sr_serial_new(name, desc)); | |
1059 | } | |
1060 | ||
24287ea9 AJ |
1061 | /** |
1062 | * List available serial devices. | |
1063 | * | |
1064 | * @return A GSList of strings containing the path of the serial devices or | |
1065 | * NULL if no serial device is found. The returned list must be freed | |
1066 | * by the caller. | |
1067 | */ | |
1068 | SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver) | |
1069 | { | |
ae4c1fb6 | 1070 | GSList *tty_devs; |
a7b8692e | 1071 | GSList *(*list_func)(GSList *list, sr_ser_list_append_t append); |
24287ea9 | 1072 | |
b1a7ca3b | 1073 | /* Currently unused, but will be used by some drivers later on. */ |
24287ea9 AJ |
1074 | (void)driver; |
1075 | ||
ae4c1fb6 | 1076 | tty_devs = NULL; |
a7b8692e GS |
1077 | if (ser_lib_funcs_libsp && ser_lib_funcs_libsp->list) { |
1078 | list_func = ser_lib_funcs_libsp->list; | |
1079 | tty_devs = list_func(tty_devs, append_port_list); | |
1080 | } | |
4417074c GS |
1081 | if (ser_lib_funcs_hid && ser_lib_funcs_hid->list) { |
1082 | list_func = ser_lib_funcs_hid->list; | |
1083 | tty_devs = list_func(tty_devs, append_port_list); | |
1084 | } | |
b79c3422 GS |
1085 | if (ser_lib_funcs_bt && ser_lib_funcs_bt->list) { |
1086 | list_func = ser_lib_funcs_bt->list; | |
1087 | tty_devs = list_func(tty_devs, append_port_list); | |
1088 | } | |
24287ea9 | 1089 | |
ae4c1fb6 GS |
1090 | return tty_devs; |
1091 | } | |
24287ea9 | 1092 | |
ae4c1fb6 GS |
1093 | static GSList *append_port_find(GSList *devs, const char *name) |
1094 | { | |
1095 | if (!name || !*name) | |
1096 | return devs; | |
b1a7ca3b | 1097 | |
ae4c1fb6 | 1098 | return g_slist_append(devs, g_strdup(name)); |
24287ea9 AJ |
1099 | } |
1100 | ||
b541f837 AJ |
1101 | /** |
1102 | * Find USB serial devices via the USB vendor ID and product ID. | |
1103 | * | |
d0a92abd MH |
1104 | * @param[in] vendor_id Vendor ID of the USB device. |
1105 | * @param[in] product_id Product ID of the USB device. | |
b541f837 AJ |
1106 | * |
1107 | * @return A GSList of strings containing the path of the serial device or | |
1108 | * NULL if no serial device is found. The returned list must be freed | |
1109 | * by the caller. | |
e00b3f58 UH |
1110 | * |
1111 | * @private | |
b541f837 AJ |
1112 | */ |
1113 | SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id) | |
1114 | { | |
ae4c1fb6 | 1115 | GSList *tty_devs; |
a7b8692e GS |
1116 | GSList *(*find_func)(GSList *list, sr_ser_find_append_t append, |
1117 | uint16_t vid, uint16_t pid); | |
b541f837 | 1118 | |
ae4c1fb6 | 1119 | tty_devs = NULL; |
a7b8692e GS |
1120 | if (ser_lib_funcs_libsp && ser_lib_funcs_libsp->find_usb) { |
1121 | find_func = ser_lib_funcs_libsp->find_usb; | |
1122 | tty_devs = find_func(tty_devs, append_port_find, | |
1123 | vendor_id, product_id); | |
1124 | } | |
4417074c GS |
1125 | if (ser_lib_funcs_hid && ser_lib_funcs_hid->find_usb) { |
1126 | find_func = ser_lib_funcs_hid->find_usb; | |
1127 | tty_devs = find_func(tty_devs, append_port_find, | |
1128 | vendor_id, product_id); | |
1129 | } | |
b1a7ca3b | 1130 | |
b541f837 | 1131 | return tty_devs; |
b541f837 | 1132 | } |
c5cfc735 | 1133 | |
e00b3f58 | 1134 | /** @private */ |
c5cfc735 BV |
1135 | SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes) |
1136 | { | |
bf6b9e7b | 1137 | int bits, baud, ret, timeout_ms; |
c5cfc735 | 1138 | |
639c6f61 | 1139 | /* Get the bitrate and frame length. */ |
c5cfc735 | 1140 | bits = baud = 0; |
a7b8692e GS |
1141 | if (port->lib_funcs && port->lib_funcs->get_frame_format) { |
1142 | ret = port->lib_funcs->get_frame_format(port, &baud, &bits); | |
1143 | if (ret != SR_OK) | |
1144 | bits = baud = 0; | |
1145 | } else { | |
639c6f61 GS |
1146 | baud = port->comm_params.bit_rate; |
1147 | bits = 1 + port->comm_params.data_bits + | |
1148 | port->comm_params.parity_bits + | |
1149 | port->comm_params.stop_bits; | |
1150 | } | |
c5cfc735 | 1151 | |
ae4c1fb6 GS |
1152 | /* Derive the timeout. Default to 1s. */ |
1153 | timeout_ms = 1000; | |
c5cfc735 BV |
1154 | if (bits && baud) { |
1155 | /* Throw in 10ms for misc OS overhead. */ | |
1156 | timeout_ms = 10; | |
1157 | timeout_ms += ((1000.0 / baud) * bits) * num_bytes; | |
1158 | } | |
1159 | ||
c5cfc735 BV |
1160 | return timeout_ms; |
1161 | } | |
e00b3f58 | 1162 | |
1ac8c218 GS |
1163 | #else |
1164 | ||
1165 | /* TODO Put fallback.c content here? */ | |
1166 | ||
1167 | #endif | |
1168 | ||
e00b3f58 | 1169 | /** @} */ |