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