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