]> sigrok.org Git - libsigrok.git/blame - hardware/common/serial.c
doxygen: Updated Doxyfile to doxygen 1.8.5.
[libsigrok.git] / hardware / common / serial.c
CommitLineData
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>
0d4405ce 25#include <libserialport.h>
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
a1bb33af 28
29a27196
UH
29/* Message logging helpers with subsystem-specific prefix string. */
30#define LOG_PREFIX "serial: "
31#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
32#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
33#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
34#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
35#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
36#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
b19f4622 37
b19f4622
UH
38/**
39 * Open the specified serial port.
40 *
299bdb24 41 * @param serial Previously initialized serial port structure.
a54dd31e
UH
42 * @param flags Flags to use when opening the serial port. Possible flags
43 * include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK.
b19f4622 44 *
299bdb24
BV
45 * If the serial structure contains a serialcomm string, it will be
46 * passed to serial_set_paramstr() after the port is opened.
47 *
48 * @return SR_OK on success, SR_ERR on failure.
b19f4622 49 */
299bdb24 50SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
d02a535e 51{
a9bce5a5
ML
52 int ret;
53 char *error;
13cd8197 54 int sp_flags = 0;
b19f4622 55
299bdb24
BV
56 if (!serial) {
57 sr_dbg("Invalid serial port.");
58 return SR_ERR;
59 }
60
61 sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
b19f4622 62
c9bc57b6
ML
63 sp_get_port_by_name(serial->port, &serial->data);
64
13cd8197
ML
65 if (flags & SERIAL_RDWR)
66 sp_flags = (SP_MODE_READ | SP_MODE_WRITE);
67 else if (flags & SERIAL_RDONLY)
68 sp_flags = SP_MODE_READ;
9647ce69
ML
69
70 serial->nonblocking = (flags & SERIAL_NONBLOCK) ? 1 : 0;
13cd8197
ML
71
72 ret = sp_open(serial->data, sp_flags);
a9bce5a5 73
a0dfaa6c
UH
74 switch (ret) {
75 case SP_ERR_ARG:
76 sr_err("Attempt to open serial port with invalid parameters.");
77 return SR_ERR_ARG;
78 case SP_ERR_FAIL:
79 error = sp_last_error_message();
80 sr_err("Error opening port: %s.", error);
81 sp_free_error_message(error);
82 return SR_ERR;
a54dd31e
UH
83 }
84
299bdb24
BV
85 if (serial->serialcomm)
86 return serial_set_paramstr(serial, serial->serialcomm);
87 else
88 return SR_OK;
d02a535e
BV
89}
90
b19f4622
UH
91/**
92 * Close the specified serial port.
93 *
299bdb24 94 * @param serial Previously initialized serial port structure.
b19f4622 95 *
299bdb24 96 * @return SR_OK on success, SR_ERR on failure.
2119ab03 97 */
299bdb24 98SR_PRIV int serial_close(struct sr_serial_dev_inst *serial)
d02a535e 99{
299bdb24 100 int ret;
a9bce5a5 101 char *error;
299bdb24
BV
102
103 if (!serial) {
104 sr_dbg("Invalid serial port.");
105 return SR_ERR;
106 }
107
64ecf7ee
ML
108 if (!serial->data) {
109 sr_dbg("Cannot close unopened serial port %s.", serial->port);
299bdb24
BV
110 return SR_ERR;
111 }
112
64ecf7ee 113 sr_spew("Closing serial port %s.", serial->port);
b19f4622 114
c9bc57b6 115 ret = sp_close(serial->data);
a9bce5a5 116
a0dfaa6c
UH
117 switch (ret) {
118 case SP_ERR_ARG:
119 sr_err("Attempt to close an invalid serial port.");
120 return SR_ERR_ARG;
121 case SP_ERR_FAIL:
122 error = sp_last_error_message();
123 sr_err("Error closing port: %s.", error);
124 sp_free_error_message(error);
125 return SR_ERR;
b19f4622 126 }
299bdb24 127
64ecf7ee
ML
128 sp_free_port(serial->data);
129 serial->data = NULL;
130
6a76efeb 131 return SR_OK;
d02a535e
BV
132}
133
b19f4622 134/**
299bdb24 135 * Flush serial port buffers.
b19f4622 136 *
299bdb24 137 * @param serial Previously initialized serial port structure.
b19f4622 138 *
299bdb24 139 * @return SR_OK on success, SR_ERR on failure.
1fdb75e1 140 */
299bdb24 141SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
06d64eb8 142{
299bdb24 143 int ret;
a9bce5a5 144 char *error;
299bdb24
BV
145
146 if (!serial) {
147 sr_dbg("Invalid serial port.");
148 return SR_ERR;
149 }
150
64ecf7ee
ML
151 if (!serial->data) {
152 sr_dbg("Cannot flush unopened serial port %s.", serial->port);
299bdb24
BV
153 return SR_ERR;
154 }
155
64ecf7ee 156 sr_spew("Flushing serial port %s.", serial->port);
b19f4622 157
13cd8197 158 ret = sp_flush(serial->data, SP_BUF_BOTH);
a9bce5a5 159
a0dfaa6c
UH
160 switch (ret) {
161 case SP_ERR_ARG:
162 sr_err("Attempt to flush an invalid serial port.");
163 return SR_ERR_ARG;
164 case SP_ERR_FAIL:
165 error = sp_last_error_message();
166 sr_err("Error flushing port: %s.", error);
167 sp_free_error_message(error);
168 return SR_ERR;
299bdb24 169 }
b19f4622 170
6a76efeb 171 return SR_OK;
06d64eb8
BV
172}
173
b19f4622 174/**
2119ab03 175 * Write a number of bytes to the specified serial port.
b19f4622 176 *
299bdb24 177 * @param serial Previously initialized serial port structure.
b19f4622
UH
178 * @param buf Buffer containing the bytes to write.
179 * @param count Number of bytes to write.
180 *
6a76efeb 181 * @return The number of bytes written, or a negative error code upon failure.
2119ab03 182 */
299bdb24
BV
183SR_PRIV int serial_write(struct sr_serial_dev_inst *serial,
184 const void *buf, size_t count)
2119ab03 185{
299bdb24 186 ssize_t ret;
a9bce5a5 187 char *error;
299bdb24
BV
188
189 if (!serial) {
190 sr_dbg("Invalid serial port.");
6a76efeb 191 return SR_ERR;
299bdb24
BV
192 }
193
64ecf7ee
ML
194 if (!serial->data) {
195 sr_dbg("Cannot use unopened serial port %s.", serial->port);
6a76efeb 196 return SR_ERR;
299bdb24
BV
197 }
198
9647ce69
ML
199 if (serial->nonblocking)
200 ret = sp_nonblocking_write(serial->data, buf, count);
201 else
202 ret = sp_blocking_write(serial->data, buf, count, 0);
a9bce5a5 203
a0dfaa6c
UH
204 switch (ret) {
205 case SP_ERR_ARG:
206 sr_err("Attempted serial port write with invalid arguments.");
207 return SR_ERR_ARG;
208 case SP_ERR_FAIL:
209 error = sp_last_error_message();
210 sr_err("Write error: %s.", error);
211 sp_free_error_message(error);
212 return SR_ERR;
a9bce5a5
ML
213 }
214
64ecf7ee 215 sr_spew("Wrote %d/%d bytes.", ret, count);
b19f4622
UH
216
217 return ret;
2119ab03
UH
218}
219
b19f4622 220/**
2119ab03 221 * Read a number of bytes from the specified serial port.
b19f4622 222 *
299bdb24 223 * @param serial Previously initialized serial port structure.
b19f4622
UH
224 * @param buf Buffer where to store the bytes that are read.
225 * @param count The number of bytes to read.
226 *
6a76efeb 227 * @return The number of bytes read, or a negative error code upon failure.
2119ab03 228 */
299bdb24
BV
229SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf,
230 size_t count)
2119ab03 231{
299bdb24 232 ssize_t ret;
c4d85a40 233 char *error;
299bdb24
BV
234
235 if (!serial) {
236 sr_dbg("Invalid serial port.");
6a76efeb 237 return SR_ERR;
299bdb24
BV
238 }
239
64ecf7ee
ML
240 if (!serial->data) {
241 sr_dbg("Cannot use unopened serial port %s.", serial->port);
6a76efeb 242 return SR_ERR;
299bdb24
BV
243 }
244
9647ce69
ML
245 if (serial->nonblocking)
246 ret = sp_nonblocking_read(serial->data, buf, count);
247 else
248 ret = sp_blocking_read(serial->data, buf, count, 0);
2119ab03 249
a0dfaa6c
UH
250 switch (ret) {
251 case SP_ERR_ARG:
252 sr_err("Attempted serial port read with invalid arguments.");
253 return SR_ERR_ARG;
c4d85a40
UH
254 case SP_ERR_FAIL:
255 error = sp_last_error_message();
256 sr_err("Read error: %s.", error);
257 sp_free_error_message(error);
258 return SR_ERR;
a9bce5a5
ML
259 }
260
c4d85a40 261 if (ret > 0)
64ecf7ee 262 sr_spew("Read %d/%d bytes.", ret, count);
b19f4622
UH
263
264 return ret;
2119ab03
UH
265}
266
b19f4622
UH
267/**
268 * Set serial parameters for the specified serial port.
269 *
299bdb24 270 * @param serial Previously initialized serial port structure.
b19f4622
UH
271 * @param baudrate The baudrate to set.
272 * @param bits The number of data bits to use.
273 * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
274 * @param stopbits The number of stop bits to use (1 or 2).
275 * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
276 * 2 = XON/XOFF).
2119ab03 277 *
b19f4622 278 * @return SR_OK upon success, SR_ERR upon failure.
1ff7712c 279 */
299bdb24 280SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
39e5d798
UH
281 int bits, int parity, int stopbits,
282 int flowcontrol, int rts, int dtr)
d02a535e 283{
a9bce5a5
ML
284 int ret;
285 char *error;
25b66c3c 286 struct sp_port_config *config;
a9bce5a5 287
299bdb24
BV
288 if (!serial) {
289 sr_dbg("Invalid serial port.");
290 return SR_ERR;
291 }
292
64ecf7ee
ML
293 if (!serial->data) {
294 sr_dbg("Cannot configure unopened serial port %s.", serial->port);
299bdb24
BV
295 return SR_ERR;
296 }
297
64ecf7ee 298 sr_spew("Setting serial parameters on port %s.", serial->port);
b19f4622 299
25b66c3c
ML
300 sp_new_config(&config);
301 sp_set_config_baudrate(config, baudrate);
302 sp_set_config_bits(config, bits);
13cd8197
ML
303 switch (parity) {
304 case 0:
25b66c3c 305 sp_set_config_parity(config, SP_PARITY_NONE);
13cd8197
ML
306 break;
307 case 1:
25b66c3c 308 sp_set_config_parity(config, SP_PARITY_EVEN);
13cd8197
ML
309 break;
310 case 2:
25b66c3c 311 sp_set_config_parity(config, SP_PARITY_ODD);
13cd8197
ML
312 break;
313 default:
314 return SR_ERR_ARG;
315 }
25b66c3c
ML
316 sp_set_config_stopbits(config, stopbits);
317 sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts);
318 sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE);
319 sp_set_config_dtr(config, dtr);
320 sp_set_config_dsr(config, SP_DSR_IGNORE);
321 sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED);
322
323 ret = sp_set_config(serial->data, config);
324 sp_free_config(config);
a9bce5a5 325
a0dfaa6c
UH
326 switch (ret) {
327 case SP_ERR_ARG:
328 sr_err("Invalid arguments for setting serial port parameters.");
329 return SR_ERR_ARG;
330 case SP_ERR_FAIL:
331 error = sp_last_error_message();
332 sr_err("Error setting serial port parameters: %s.", error);
333 sp_free_error_message(error);
334 return SR_ERR;
700dcd5c
UH
335 }
336
e46b8fb1 337 return SR_OK;
d02a535e 338}
792fc686 339
299bdb24
BV
340/**
341 * Set serial parameters for the specified serial port.
342 *
343 * @param serial Previously initialized serial port structure.
344 * @param paramstr A serial communication parameters string, in the form
26ddb5ba 345 * of <speed>/<data bits><parity><stopbits><flow>, for example "9600/8n1" or
346 * "600/7o2" or "460800/8n1/flow=2" where flow is 0 for none, 1 for rts/cts and 2 for xon/xoff.
299bdb24
BV
347 *
348 * @return SR_OK upon success, SR_ERR upon failure.
349 */
a970522b 350#define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$"
299bdb24
BV
351SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
352 const char *paramstr)
792fc686
BV
353{
354 GRegex *reg;
355 GMatchInfo *match;
26ddb5ba 356 int speed, databits, parity, stopbits, flow, rts, dtr, i;
71caaad4 357 char *mstr, **opts, **kv;
792fc686 358
26ddb5ba 359 speed = databits = parity = stopbits = flow = 0;
71caaad4 360 rts = dtr = -1;
ea088bb6 361 sr_spew("Parsing parameters from \"%s\".", paramstr);
792fc686
BV
362 reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
363 if (g_regex_match(reg, paramstr, 0, &match)) {
364 if ((mstr = g_match_info_fetch(match, 1)))
365 speed = strtoul(mstr, NULL, 10);
366 g_free(mstr);
367 if ((mstr = g_match_info_fetch(match, 2)))
368 databits = strtoul(mstr, NULL, 10);
369 g_free(mstr);
370 if ((mstr = g_match_info_fetch(match, 3))) {
371 switch (mstr[0]) {
372 case 'n':
373 parity = SERIAL_PARITY_NONE;
374 break;
375 case 'e':
376 parity = SERIAL_PARITY_EVEN;
377 break;
378 case 'o':
379 parity = SERIAL_PARITY_ODD;
380 break;
381 }
382 }
383 g_free(mstr);
384 if ((mstr = g_match_info_fetch(match, 4)))
385 stopbits = strtoul(mstr, NULL, 10);
386 g_free(mstr);
71caaad4
BV
387 if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') {
388 if (mstr[0] != '/') {
389 sr_dbg("missing separator before extra options");
390 speed = 0;
391 } else {
392 /* A set of "key=value" options separated by / */
393 opts = g_strsplit(mstr + 1, "/", 0);
394 for (i = 0; opts[i]; i++) {
395 kv = g_strsplit(opts[i], "=", 2);
396 if (!strncmp(kv[0], "rts", 3)) {
397 if (kv[1][0] == '1')
398 rts = 1;
399 else if (kv[1][0] == '0')
400 rts = 0;
401 else {
402 sr_dbg("invalid value for rts: %c", kv[1][0]);
403 speed = 0;
404 }
405 } else if (!strncmp(kv[0], "dtr", 3)) {
406 if (kv[1][0] == '1')
407 dtr = 1;
408 else if (kv[1][0] == '0')
409 dtr = 0;
410 else {
411 sr_dbg("invalid value for dtr: %c", kv[1][0]);
412 speed = 0;
413 }
26ddb5ba 414 } else if (!strncmp(kv[0], "flow", 4)) {
415 if (kv[1][0] == '0')
416 flow = 0;
417 else if (kv[1][0] == '1')
418 flow = 1;
419 else if (kv[1][0] == '2')
420 flow = 2;
421 else {
422 sr_dbg("invalid value for flow: %c", kv[1][0]);
423 speed = 0;
424 }
71caaad4
BV
425 }
426 g_strfreev(kv);
427 }
428 g_strfreev(opts);
429 }
430 }
431 g_free(mstr);
792fc686
BV
432 }
433 g_match_info_unref(match);
434 g_regex_unref(reg);
435
ea088bb6
AG
436 if (speed) {
437 return serial_set_params(serial, speed, databits, parity,
26ddb5ba 438 stopbits, flow, rts, dtr);
ea088bb6
AG
439 } else {
440 sr_dbg("Could not infer speed from parameter string.");
792fc686 441 return SR_ERR_ARG;
ea088bb6 442 }
792fc686
BV
443}
444
299bdb24
BV
445/**
446 * Read a line from the specified serial port.
447 *
448 * @param serial Previously initialized serial port structure.
449 * @param buf Buffer where to store the bytes that are read.
450 * @param buflen Size of the buffer.
451 * @param timeout_ms How long to wait for a line to come in.
452 *
453 * Reading stops when CR of LR is found, which is stripped from the buffer.
454 *
455 * @return SR_OK on success, SR_ERR on failure.
456 */
457SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
458 int *buflen, gint64 timeout_ms)
6f22a8ef 459{
b87f8504 460 gint64 start;
6f22a8ef
UH
461 int maxlen, len;
462
64ecf7ee 463 if (!serial) {
299bdb24
BV
464 sr_dbg("Invalid serial port.");
465 return SR_ERR;
466 }
467
64ecf7ee
ML
468 if (!serial->data) {
469 sr_dbg("Cannot use unopened serial port %s.", serial->port);
299bdb24
BV
470 return -1;
471 }
472
6f22a8ef
UH
473 timeout_ms *= 1000;
474 start = g_get_monotonic_time();
475
476 maxlen = *buflen;
477 *buflen = len = 0;
478 while(1) {
479 len = maxlen - *buflen - 1;
480 if (len < 1)
481 break;
299bdb24 482 len = serial_read(serial, *buf + *buflen, 1);
6f22a8ef
UH
483 if (len > 0) {
484 *buflen += len;
485 *(*buf + *buflen) = '\0';
318dd53c
BV
486 if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
487 || *(*buf + *buflen - 1) == '\n')) {
488 /* Strip CR/LF and terminate. */
6f22a8ef
UH
489 *(*buf + --*buflen) = '\0';
490 break;
491 }
492 }
493 if (g_get_monotonic_time() - start > timeout_ms)
494 /* Timeout */
495 break;
5715e84f
DT
496 if (len < 1)
497 g_usleep(2000);
6f22a8ef 498 }
318dd53c
BV
499 if (*buflen)
500 sr_dbg("Received %d: '%s'.", *buflen, *buf);
6f22a8ef
UH
501
502 return SR_OK;
503}
766456be
UH
504
505/**
506 * Try to find a valid packet in a serial data stream.
507 *
508 * @param serial Previously initialized serial port structure.
509 * @param buf Buffer containing the bytes to write.
510 * @param count Size of the buffer.
511 * @param packet_size Size, in bytes, of a valid packet.
512 * @param is_valid Callback that assesses whether the packet is valid or not.
513 * @param timeout_ms The timeout after which, if no packet is detected, to
514 * abort scanning.
515 * @param baudrate The baudrate of the serial port. This parameter is not
516 * critical, but it helps fine tune the serial port polling
517 * delay.
518 *
519 * @return SR_OK if a valid packet is found within the given timeout,
520 * SR_ERR upon failure.
521 */
522SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
523 uint8_t *buf, size_t *buflen,
524 size_t packet_size, packet_valid_t is_valid,
525 uint64_t timeout_ms, int baudrate)
526{
527 uint64_t start, time, byte_delay_us;
528 size_t ibuf, i, maxlen;
529 int len;
530
531 maxlen = *buflen;
532
64ecf7ee
ML
533 sr_dbg("Detecting packets on %s (timeout = %" PRIu64
534 "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
766456be
UH
535
536 if (maxlen < (packet_size / 2) ) {
537 sr_err("Buffer size must be at least twice the packet size.");
538 return SR_ERR;
539 }
540
766456be
UH
541 /* Assume 8n1 transmission. That is 10 bits for every byte. */
542 byte_delay_us = 10 * (1000000 / baudrate);
543 start = g_get_monotonic_time();
544
545 i = ibuf = len = 0;
546 while (ibuf < maxlen) {
547 len = serial_read(serial, &buf[ibuf], 1);
548 if (len > 0) {
549 ibuf += len;
550 } else if (len == 0) {
006dbe55 551 /* No logging, already done in serial_read(). */
766456be
UH
552 } else {
553 /* Error reading byte, but continuing anyway. */
554 }
551c3d8c
AG
555
556 time = g_get_monotonic_time() - start;
557 time /= 1000;
558
766456be
UH
559 if ((ibuf - i) >= packet_size) {
560 /* We have at least a packet's worth of data. */
561 if (is_valid(&buf[i])) {
766456be
UH
562 sr_spew("Found valid %d-byte packet after "
563 "%" PRIu64 "ms.", (ibuf - i), time);
564 *buflen = ibuf;
565 return SR_OK;
566 } else {
567 sr_spew("Got %d bytes, but not a valid "
568 "packet.", (ibuf - i));
569 }
570 /* Not a valid packet. Continue searching. */
571 i++;
572 }
551c3d8c 573 if (time >= timeout_ms) {
766456be 574 /* Timeout */
551c3d8c 575 sr_dbg("Detection timed out after %dms.", time);
766456be
UH
576 break;
577 }
5715e84f
DT
578 if (len < 1)
579 g_usleep(byte_delay_us);
766456be
UH
580 }
581
582 *buflen = ibuf;
583
584 sr_err("Didn't find a valid packet (read %d bytes).", *buflen);
585
586 return SR_ERR;
587}
1bd9e678
DJ
588
589/**
590 * Extract the serial device and options from the options linked list.
591 *
592 * @param options List of options passed from the command line.
593 * @param serial_device Pointer where to store the exctracted serial device.
594 * @param serial_options Pointer where to store the optional extracted serial
595 * options.
596 *
597 * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The
598 * returned string should not be freed by the caller.
599 */
600SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
601 const char **serial_options)
602{
603 GSList *l;
604 struct sr_config *src;
605
606 *serial_device = NULL;
607
608 for (l = options; l; l = l->next) {
609 src = l->data;
610 switch (src->key) {
611 case SR_CONF_CONN:
612 *serial_device = g_variant_get_string(src->data, NULL);
613 sr_dbg("Parsed serial device: %s", *serial_device);
614 break;
615
616 case SR_CONF_SERIALCOMM:
617 *serial_options = g_variant_get_string(src->data, NULL);
618 sr_dbg("Parsed serial options: %s", *serial_options);
619 break;
620 }
621 }
622
623 if (!*serial_device) {
624 sr_dbg("No serial device specified");
625 return SR_ERR;
626 }
627
628 return SR_OK;
629}
abc4b335
ML
630
631SR_PRIV int serial_source_add(struct sr_serial_dev_inst *serial, int events,
632 int timeout, sr_receive_data_callback_t cb, void *cb_data)
633{
a0a4c0fb
ML
634#ifdef _WIN32
635 return SR_ERR;
636#else
637 int fd;
638 sp_get_port_handle(serial->data, &fd);
639 return sr_source_add(fd, events, timeout, cb, cb_data);
640#endif
abc4b335 641}
7faa3e88
ML
642
643SR_PRIV int serial_source_remove(struct sr_serial_dev_inst *serial)
644{
a0a4c0fb
ML
645#ifdef _WIN32
646 return SR_ERR;
647#else
648 int fd;
649 sp_get_port_handle(serial->data, &fd);
650 return sr_source_remove(fd);
651#endif
7faa3e88 652}