]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
7 | * Copyright (C) 2014 Uffe Jakobsen <uffe@uffe.org> | |
8 | * | |
9 | * This program is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | #include <config.h> | |
24 | #include <glib.h> | |
25 | #include <libsigrok/libsigrok.h> | |
26 | #include "libsigrok-internal.h" | |
27 | #ifdef HAVE_LIBSERIALPORT | |
28 | #include <libserialport.h> | |
29 | #endif | |
30 | #ifdef G_OS_WIN32 | |
31 | #include <windows.h> /* for HANDLE */ | |
32 | #endif | |
33 | ||
34 | #define LOG_PREFIX "serial-libsp" | |
35 | ||
36 | /** | |
37 | * @file | |
38 | * | |
39 | * Serial port handling, wraps the external libserialport dependency. | |
40 | */ | |
41 | ||
42 | #ifdef HAVE_LIBSERIALPORT | |
43 | ||
44 | /** | |
45 | * @defgroup grp_serial_libsp Serial port handling, libserialport group | |
46 | * | |
47 | * Serial port handling functions, based on libserialport. | |
48 | * | |
49 | * @{ | |
50 | */ | |
51 | ||
52 | static int sr_ser_libsp_open(struct sr_serial_dev_inst *serial, int flags) | |
53 | { | |
54 | int ret; | |
55 | char *error; | |
56 | int sp_flags; | |
57 | ||
58 | sp_get_port_by_name(serial->port, &serial->sp_data); | |
59 | ||
60 | sp_flags = 0; | |
61 | if (flags & SERIAL_RDWR) | |
62 | sp_flags = (SP_MODE_READ | SP_MODE_WRITE); | |
63 | else if (flags & SERIAL_RDONLY) | |
64 | sp_flags = SP_MODE_READ; | |
65 | ||
66 | ret = sp_open(serial->sp_data, sp_flags); | |
67 | ||
68 | switch (ret) { | |
69 | case SP_ERR_ARG: | |
70 | sr_err("Attempt to open serial port with invalid parameters."); | |
71 | return SR_ERR_ARG; | |
72 | case SP_ERR_FAIL: | |
73 | error = sp_last_error_message(); | |
74 | sr_err("Error opening port (%d): %s.", | |
75 | sp_last_error_code(), error); | |
76 | sp_free_error_message(error); | |
77 | return SR_ERR; | |
78 | } | |
79 | ||
80 | return SR_OK; | |
81 | } | |
82 | ||
83 | static int sr_ser_libsp_close(struct sr_serial_dev_inst *serial) | |
84 | { | |
85 | int ret; | |
86 | char *error; | |
87 | ||
88 | if (!serial->sp_data) { | |
89 | sr_dbg("Cannot close unopened serial port %s.", serial->port); | |
90 | return SR_ERR; | |
91 | } | |
92 | ||
93 | ret = sp_close(serial->sp_data); | |
94 | ||
95 | switch (ret) { | |
96 | case SP_ERR_ARG: | |
97 | sr_err("Attempt to close an invalid serial port."); | |
98 | return SR_ERR_ARG; | |
99 | case SP_ERR_FAIL: | |
100 | error = sp_last_error_message(); | |
101 | sr_err("Error closing port (%d): %s.", | |
102 | sp_last_error_code(), error); | |
103 | sp_free_error_message(error); | |
104 | return SR_ERR; | |
105 | } | |
106 | ||
107 | sp_free_port(serial->sp_data); | |
108 | serial->sp_data = NULL; | |
109 | ||
110 | return SR_OK; | |
111 | } | |
112 | ||
113 | static int sr_ser_libsp_flush(struct sr_serial_dev_inst *serial) | |
114 | { | |
115 | int ret; | |
116 | char *error; | |
117 | ||
118 | if (!serial->sp_data) { | |
119 | sr_dbg("Cannot flush unopened serial port %s.", serial->port); | |
120 | return SR_ERR; | |
121 | } | |
122 | ||
123 | ret = sp_flush(serial->sp_data, SP_BUF_BOTH); | |
124 | ||
125 | switch (ret) { | |
126 | case SP_ERR_ARG: | |
127 | sr_err("Attempt to flush an invalid serial port."); | |
128 | return SR_ERR_ARG; | |
129 | case SP_ERR_FAIL: | |
130 | error = sp_last_error_message(); | |
131 | sr_err("Error flushing port (%d): %s.", | |
132 | sp_last_error_code(), error); | |
133 | sp_free_error_message(error); | |
134 | return SR_ERR; | |
135 | } | |
136 | ||
137 | return SR_OK; | |
138 | } | |
139 | ||
140 | static int sr_ser_libsp_drain(struct sr_serial_dev_inst *serial) | |
141 | { | |
142 | int ret; | |
143 | char *error; | |
144 | ||
145 | if (!serial->sp_data) { | |
146 | sr_dbg("Cannot drain unopened serial port %s.", serial->port); | |
147 | return SR_ERR; | |
148 | } | |
149 | ||
150 | ret = sp_drain(serial->sp_data); | |
151 | ||
152 | if (ret == SP_ERR_FAIL) { | |
153 | error = sp_last_error_message(); | |
154 | sr_err("Error draining port (%d): %s.", | |
155 | sp_last_error_code(), error); | |
156 | sp_free_error_message(error); | |
157 | return SR_ERR; | |
158 | } | |
159 | ||
160 | return SR_OK; | |
161 | } | |
162 | ||
163 | static int sr_ser_libsp_write(struct sr_serial_dev_inst *serial, | |
164 | const void *buf, size_t count, | |
165 | int nonblocking, unsigned int timeout_ms) | |
166 | { | |
167 | ssize_t ret; | |
168 | char *error; | |
169 | ||
170 | if (!serial->sp_data) { | |
171 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
172 | return SR_ERR; | |
173 | } | |
174 | ||
175 | if (nonblocking) | |
176 | ret = sp_nonblocking_write(serial->sp_data, buf, count); | |
177 | else | |
178 | ret = sp_blocking_write(serial->sp_data, buf, count, timeout_ms); | |
179 | ||
180 | switch (ret) { | |
181 | case SP_ERR_ARG: | |
182 | sr_err("Attempted serial port write with invalid arguments."); | |
183 | return SR_ERR_ARG; | |
184 | case SP_ERR_FAIL: | |
185 | error = sp_last_error_message(); | |
186 | sr_err("Write error (%d): %s.", sp_last_error_code(), error); | |
187 | sp_free_error_message(error); | |
188 | return SR_ERR; | |
189 | } | |
190 | ||
191 | return ret; | |
192 | } | |
193 | ||
194 | static int sr_ser_libsp_read(struct sr_serial_dev_inst *serial, | |
195 | void *buf, size_t count, | |
196 | int nonblocking, unsigned int timeout_ms) | |
197 | { | |
198 | ssize_t ret; | |
199 | char *error; | |
200 | ||
201 | if (!serial->sp_data) { | |
202 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
203 | return SR_ERR; | |
204 | } | |
205 | ||
206 | if (nonblocking) | |
207 | ret = sp_nonblocking_read(serial->sp_data, buf, count); | |
208 | else | |
209 | ret = sp_blocking_read(serial->sp_data, buf, count, timeout_ms); | |
210 | ||
211 | switch (ret) { | |
212 | case SP_ERR_ARG: | |
213 | sr_err("Attempted serial port read with invalid arguments."); | |
214 | return SR_ERR_ARG; | |
215 | case SP_ERR_FAIL: | |
216 | error = sp_last_error_message(); | |
217 | sr_err("Read error (%d): %s.", sp_last_error_code(), error); | |
218 | sp_free_error_message(error); | |
219 | return SR_ERR; | |
220 | } | |
221 | ||
222 | return ret; | |
223 | } | |
224 | ||
225 | static int sr_ser_libsp_set_params(struct sr_serial_dev_inst *serial, | |
226 | int baudrate, int bits, int parity, int stopbits, | |
227 | int flowcontrol, int rts, int dtr) | |
228 | { | |
229 | int ret; | |
230 | char *error; | |
231 | struct sp_port_config *config; | |
232 | int cts, xonoff; | |
233 | ||
234 | if (!serial->sp_data) { | |
235 | sr_dbg("Cannot configure unopened serial port %s.", serial->port); | |
236 | return SR_ERR; | |
237 | } | |
238 | ||
239 | sp_new_config(&config); | |
240 | sp_set_config_baudrate(config, baudrate); | |
241 | sp_set_config_bits(config, bits); | |
242 | switch (parity) { | |
243 | case 0: | |
244 | sp_set_config_parity(config, SP_PARITY_NONE); | |
245 | break; | |
246 | case 1: | |
247 | sp_set_config_parity(config, SP_PARITY_EVEN); | |
248 | break; | |
249 | case 2: | |
250 | sp_set_config_parity(config, SP_PARITY_ODD); | |
251 | break; | |
252 | default: | |
253 | return SR_ERR_ARG; | |
254 | } | |
255 | sp_set_config_stopbits(config, stopbits); | |
256 | rts = flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts; | |
257 | sp_set_config_rts(config, rts); | |
258 | cts = flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; | |
259 | sp_set_config_cts(config, cts); | |
260 | sp_set_config_dtr(config, dtr); | |
261 | sp_set_config_dsr(config, SP_DSR_IGNORE); | |
262 | xonoff = flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED; | |
263 | sp_set_config_xon_xoff(config, xonoff); | |
264 | ||
265 | ret = sp_set_config(serial->sp_data, config); | |
266 | sp_free_config(config); | |
267 | ||
268 | switch (ret) { | |
269 | case SP_ERR_ARG: | |
270 | sr_err("Invalid arguments for setting serial port parameters."); | |
271 | return SR_ERR_ARG; | |
272 | case SP_ERR_FAIL: | |
273 | error = sp_last_error_message(); | |
274 | sr_err("Error setting serial port parameters (%d): %s.", | |
275 | sp_last_error_code(), error); | |
276 | sp_free_error_message(error); | |
277 | return SR_ERR; | |
278 | } | |
279 | ||
280 | return SR_OK; | |
281 | } | |
282 | ||
283 | #ifdef G_OS_WIN32 | |
284 | typedef HANDLE event_handle; | |
285 | #else | |
286 | typedef int event_handle; | |
287 | #endif | |
288 | ||
289 | static int sr_ser_libsp_source_add_int(struct sr_serial_dev_inst *serial, | |
290 | int events, | |
291 | void **keyptr, gintptr *fdptr, unsigned int *pollevtptr) | |
292 | { | |
293 | struct sp_event_set *event_set; | |
294 | gintptr poll_fd; | |
295 | unsigned int poll_events; | |
296 | enum sp_event mask; | |
297 | ||
298 | if ((events & (G_IO_IN | G_IO_ERR)) && (events & G_IO_OUT)) { | |
299 | sr_err("Cannot poll input/error and output simultaneously."); | |
300 | return SR_ERR_ARG; | |
301 | } | |
302 | if (!serial->sp_data) { | |
303 | sr_err("Invalid serial port."); | |
304 | return SR_ERR_ARG; | |
305 | } | |
306 | ||
307 | if (sp_new_event_set(&event_set) != SP_OK) | |
308 | return SR_ERR; | |
309 | ||
310 | mask = 0; | |
311 | if (events & G_IO_IN) | |
312 | mask |= SP_EVENT_RX_READY; | |
313 | if (events & G_IO_OUT) | |
314 | mask |= SP_EVENT_TX_READY; | |
315 | if (events & G_IO_ERR) | |
316 | mask |= SP_EVENT_ERROR; | |
317 | ||
318 | if (sp_add_port_events(event_set, serial->sp_data, mask) != SP_OK) { | |
319 | sp_free_event_set(event_set); | |
320 | return SR_ERR; | |
321 | } | |
322 | if (event_set->count != 1) { | |
323 | sr_err("Unexpected number (%u) of event handles to poll.", | |
324 | event_set->count); | |
325 | sp_free_event_set(event_set); | |
326 | return SR_ERR; | |
327 | } | |
328 | ||
329 | poll_fd = (gintptr) ((event_handle *)event_set->handles)[0]; | |
330 | mask = event_set->masks[0]; | |
331 | ||
332 | sp_free_event_set(event_set); | |
333 | ||
334 | poll_events = 0; | |
335 | if (mask & SP_EVENT_RX_READY) | |
336 | poll_events |= G_IO_IN; | |
337 | if (mask & SP_EVENT_TX_READY) | |
338 | poll_events |= G_IO_OUT; | |
339 | if (mask & SP_EVENT_ERROR) | |
340 | poll_events |= G_IO_ERR; | |
341 | ||
342 | /* | |
343 | * Using serial->sp_data as the key for the event source is not quite | |
344 | * proper, as it makes it impossible to create another event source | |
345 | * for the same serial port. However, these fixed keys will soon be | |
346 | * removed from the API anyway, so this is OK for now. | |
347 | */ | |
348 | *keyptr = serial->sp_data; | |
349 | *fdptr = poll_fd; | |
350 | *pollevtptr = poll_events; | |
351 | ||
352 | return SR_OK; | |
353 | } | |
354 | ||
355 | static int sr_ser_libsp_source_add(struct sr_session *session, | |
356 | struct sr_serial_dev_inst *serial, int events, int timeout, | |
357 | sr_receive_data_callback cb, void *cb_data) | |
358 | { | |
359 | int ret; | |
360 | void *key; | |
361 | gintptr poll_fd; | |
362 | unsigned int poll_events; | |
363 | ||
364 | ret = sr_ser_libsp_source_add_int(serial, events, | |
365 | &key, &poll_fd, &poll_events); | |
366 | if (ret != SR_OK) | |
367 | return ret; | |
368 | ||
369 | return sr_session_fd_source_add(session, | |
370 | key, poll_fd, poll_events, | |
371 | timeout, cb, cb_data); | |
372 | } | |
373 | ||
374 | static int sr_ser_libsp_source_remove(struct sr_session *session, | |
375 | struct sr_serial_dev_inst *serial) | |
376 | { | |
377 | void *key; | |
378 | ||
379 | key = serial->sp_data; | |
380 | return sr_session_source_remove_internal(session, key); | |
381 | } | |
382 | ||
383 | static GSList *sr_ser_libsp_list(GSList *list, sr_ser_list_append_t append) | |
384 | { | |
385 | struct sp_port **ports; | |
386 | size_t i; | |
387 | const char *name; | |
388 | const char *desc; | |
389 | ||
390 | if (sp_list_ports(&ports) != SP_OK) | |
391 | return list; | |
392 | ||
393 | for (i = 0; ports[i]; i++) { | |
394 | name = sp_get_port_name(ports[i]); | |
395 | desc = sp_get_port_description(ports[i]); | |
396 | list = append(list, name, desc); | |
397 | } | |
398 | ||
399 | sp_free_port_list(ports); | |
400 | ||
401 | return list; | |
402 | } | |
403 | ||
404 | static GSList *sr_ser_libsp_find_usb(GSList *list, sr_ser_find_append_t append, | |
405 | uint16_t vendor_id, uint16_t product_id) | |
406 | { | |
407 | struct sp_port **ports; | |
408 | int i, vid, pid; | |
409 | ||
410 | if (sp_list_ports(&ports) != SP_OK) | |
411 | return list; | |
412 | ||
413 | for (i = 0; ports[i]; i++) { | |
414 | if (sp_get_port_transport(ports[i]) != SP_TRANSPORT_USB) | |
415 | continue; | |
416 | if (sp_get_port_usb_vid_pid(ports[i], &vid, &pid) != SP_OK) | |
417 | continue; | |
418 | if (vendor_id && vid != vendor_id) | |
419 | continue; | |
420 | if (product_id && pid != product_id) | |
421 | continue; | |
422 | list = append(list, sp_get_port_name(ports[i])); | |
423 | } | |
424 | ||
425 | sp_free_port_list(ports); | |
426 | ||
427 | return list; | |
428 | } | |
429 | ||
430 | static int sr_ser_libsp_get_frame_format(struct sr_serial_dev_inst *serial, | |
431 | int *baud, int *bits) | |
432 | { | |
433 | struct sp_port_config *config; | |
434 | int ret, tmp; | |
435 | enum sp_parity parity; | |
436 | ||
437 | if (sp_new_config(&config) < 0) | |
438 | return SR_ERR_MALLOC; | |
439 | *baud = *bits = 0; | |
440 | ret = SR_OK; | |
441 | do { | |
442 | if (sp_get_config(serial->sp_data, config) < 0) { | |
443 | ret = SR_ERR_NA; | |
444 | break; | |
445 | } | |
446 | ||
447 | if (sp_get_config_baudrate(config, &tmp) < 0) { | |
448 | ret = SR_ERR_NA; | |
449 | break; | |
450 | } | |
451 | *baud = tmp; | |
452 | ||
453 | *bits += 1; /* Start bit. */ | |
454 | if (sp_get_config_bits(config, &tmp) < 0) { | |
455 | ret = SR_ERR_NA; | |
456 | break; | |
457 | } | |
458 | *bits += tmp; | |
459 | if (sp_get_config_parity(config, &parity) < 0) { | |
460 | ret = SR_ERR_NA; | |
461 | break; | |
462 | } | |
463 | *bits += (parity != SP_PARITY_NONE) ? 1 : 0; | |
464 | if (sp_get_config_stopbits(config, &tmp) < 0) { | |
465 | ret = SR_ERR_NA; | |
466 | break; | |
467 | } | |
468 | *bits += tmp; | |
469 | } while (FALSE); | |
470 | sp_free_config(config); | |
471 | ||
472 | return ret; | |
473 | } | |
474 | ||
475 | static size_t sr_ser_libsp_get_rx_avail(struct sr_serial_dev_inst *serial) | |
476 | { | |
477 | int rc; | |
478 | ||
479 | if (!serial) | |
480 | return 0; | |
481 | ||
482 | rc = sp_input_waiting(serial->sp_data); | |
483 | if (rc < 0) | |
484 | return 0; | |
485 | ||
486 | return rc; | |
487 | } | |
488 | ||
489 | static struct ser_lib_functions serlib_sp = { | |
490 | .open = sr_ser_libsp_open, | |
491 | .close = sr_ser_libsp_close, | |
492 | .flush = sr_ser_libsp_flush, | |
493 | .drain = sr_ser_libsp_drain, | |
494 | .write = sr_ser_libsp_write, | |
495 | .read = sr_ser_libsp_read, | |
496 | .set_params = sr_ser_libsp_set_params, | |
497 | .setup_source_add = sr_ser_libsp_source_add, | |
498 | .setup_source_remove = sr_ser_libsp_source_remove, | |
499 | .list = sr_ser_libsp_list, | |
500 | .find_usb = sr_ser_libsp_find_usb, | |
501 | .get_frame_format = sr_ser_libsp_get_frame_format, | |
502 | .get_rx_avail = sr_ser_libsp_get_rx_avail, | |
503 | }; | |
504 | SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp = &serlib_sp; | |
505 | ||
506 | #else | |
507 | ||
508 | SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp = NULL; | |
509 | ||
510 | #endif |