]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <glib.h> | |
22 | #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */ | |
23 | #include "libsigrok.h" | |
24 | #include "libsigrok-internal.h" | |
25 | ||
26 | /** @cond PRIVATE */ | |
27 | #define LOG_PREFIX "device" | |
28 | /** @endcond */ | |
29 | ||
30 | /** | |
31 | * @file | |
32 | * | |
33 | * Device handling in libsigrok. | |
34 | */ | |
35 | ||
36 | /** | |
37 | * @defgroup grp_devices Devices | |
38 | * | |
39 | * Device handling in libsigrok. | |
40 | * | |
41 | * @{ | |
42 | */ | |
43 | ||
44 | /** @private | |
45 | * Allocate and initialize new struct sr_channel | |
46 | * @param[in] index @copydoc sr_channel::index | |
47 | * @param[in] type @copydoc sr_channel::type | |
48 | * @param[in] enabled @copydoc sr_channel::enabled | |
49 | * @param[in] name @copydoc sr_channel::name | |
50 | * | |
51 | * @return A new struct sr_channel*. | |
52 | */ | |
53 | SR_PRIV struct sr_channel *sr_channel_new(int index, int type, | |
54 | gboolean enabled, const char *name) | |
55 | { | |
56 | struct sr_channel *ch; | |
57 | ||
58 | ch = g_malloc0(sizeof(struct sr_channel)); | |
59 | ch->index = index; | |
60 | ch->type = type; | |
61 | ch->enabled = enabled; | |
62 | if (name) | |
63 | ch->name = g_strdup(name); | |
64 | ||
65 | return ch; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Set the name of the specified channel in the specified device. | |
70 | * | |
71 | * If the channel already has a different name assigned to it, it will be | |
72 | * removed, and the new name will be saved instead. | |
73 | * | |
74 | * @param sdi The device instance the channel is connected to. | |
75 | * @param[in] channelnum The number of the channel whose name to set. | |
76 | * Note that the channel numbers start at 0. | |
77 | * @param[in] name The new name that the specified channel should get. A copy | |
78 | * of the string is made. | |
79 | * | |
80 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
81 | * | |
82 | * @since 0.3.0 | |
83 | */ | |
84 | SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi, | |
85 | int channelnum, const char *name) | |
86 | { | |
87 | GSList *l; | |
88 | struct sr_channel *ch; | |
89 | int ret; | |
90 | ||
91 | if (!sdi) { | |
92 | sr_err("%s: sdi was NULL", __func__); | |
93 | return SR_ERR_ARG; | |
94 | } | |
95 | ||
96 | ret = SR_ERR_ARG; | |
97 | for (l = sdi->channels; l; l = l->next) { | |
98 | ch = l->data; | |
99 | if (ch->index == channelnum) { | |
100 | g_free(ch->name); | |
101 | ch->name = g_strdup(name); | |
102 | ret = SR_OK; | |
103 | break; | |
104 | } | |
105 | } | |
106 | ||
107 | return ret; | |
108 | } | |
109 | ||
110 | /** | |
111 | * Enable or disable a channel on the specified device. | |
112 | * | |
113 | * @param sdi The device instance the channel is connected to. | |
114 | * @param channelnum The channel number, starting from 0. | |
115 | * @param state TRUE to enable the channel, FALSE to disable. | |
116 | * | |
117 | * @return SR_OK on success or SR_ERR on failure. In case of invalid | |
118 | * arguments, SR_ERR_ARG is returned and the channel enabled state | |
119 | * remains unchanged. | |
120 | * | |
121 | * @since 0.3.0 | |
122 | */ | |
123 | SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum, | |
124 | gboolean state) | |
125 | { | |
126 | GSList *l; | |
127 | struct sr_channel *ch; | |
128 | int ret; | |
129 | gboolean was_enabled; | |
130 | ||
131 | if (!sdi) | |
132 | return SR_ERR_ARG; | |
133 | ||
134 | ret = SR_ERR_ARG; | |
135 | for (l = sdi->channels; l; l = l->next) { | |
136 | ch = l->data; | |
137 | if (ch->index == channelnum) { | |
138 | was_enabled = ch->enabled; | |
139 | ch->enabled = state; | |
140 | ret = SR_OK; | |
141 | if (!state != !was_enabled && sdi->driver | |
142 | && sdi->driver->config_channel_set) { | |
143 | ret = sdi->driver->config_channel_set( | |
144 | sdi, ch, SR_CHANNEL_SET_ENABLED); | |
145 | /* Roll back change if it wasn't applicable. */ | |
146 | if (ret == SR_ERR_ARG) | |
147 | ch->enabled = was_enabled; | |
148 | } | |
149 | break; | |
150 | } | |
151 | } | |
152 | ||
153 | return ret; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Determine whether the specified device instance has the specified | |
158 | * capability. | |
159 | * | |
160 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
161 | * If the device's 'driver' field is NULL (virtual device), this | |
162 | * function will always return FALSE (virtual devices don't have | |
163 | * a hardware capabilities list). | |
164 | * @param[in] key The option that should be checked for is supported by the | |
165 | * specified device. | |
166 | * | |
167 | * @retval TRUE Device has the specified option | |
168 | * @retval FALSE Device does not have the specified option, invalid input | |
169 | * parameters or other error conditions. | |
170 | * | |
171 | * @since 0.2.0 | |
172 | */ | |
173 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) | |
174 | { | |
175 | GVariant *gvar; | |
176 | const int *devopts; | |
177 | gsize num_opts, i; | |
178 | int ret; | |
179 | ||
180 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
181 | return FALSE; | |
182 | ||
183 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
184 | &gvar, sdi, NULL) != SR_OK) | |
185 | return FALSE; | |
186 | ||
187 | ret = FALSE; | |
188 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
189 | for (i = 0; i < num_opts; i++) { | |
190 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
191 | ret = TRUE; | |
192 | break; | |
193 | } | |
194 | } | |
195 | g_variant_unref(gvar); | |
196 | ||
197 | return ret; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Allocate and init a new user-generated device instance. | |
202 | * | |
203 | * @param vendor Device vendor | |
204 | * @param model Device model | |
205 | * @param version Device version | |
206 | * | |
207 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
208 | * sr_dev_inst_free(). | |
209 | */ | |
210 | SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor, | |
211 | const char *model, const char *version) | |
212 | { | |
213 | struct sr_dev_inst *sdi; | |
214 | ||
215 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
216 | ||
217 | sdi->vendor = g_strdup(vendor); | |
218 | sdi->model = g_strdup(model); | |
219 | sdi->version = g_strdup(version); | |
220 | sdi->inst_type = SR_INST_USER; | |
221 | ||
222 | return sdi; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Add a new channel to the specified device instance. | |
227 | */ | |
228 | SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name) | |
229 | { | |
230 | struct sr_channel *ch; | |
231 | ||
232 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) | |
233 | return SR_ERR_ARG; | |
234 | ||
235 | ch = sr_channel_new(index, type, TRUE, name); | |
236 | sdi->channels = g_slist_append(sdi->channels, ch); | |
237 | ||
238 | return SR_OK; | |
239 | } | |
240 | ||
241 | /** @private | |
242 | * Free device instance struct created by sr_dev_inst(). | |
243 | * @param sdi device instance to free. | |
244 | */ | |
245 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) | |
246 | { | |
247 | struct sr_channel *ch; | |
248 | struct sr_channel_group *cg; | |
249 | GSList *l; | |
250 | ||
251 | for (l = sdi->channels; l; l = l->next) { | |
252 | ch = l->data; | |
253 | g_free(ch->name); | |
254 | g_free(ch->priv); | |
255 | g_free(ch); | |
256 | } | |
257 | g_slist_free(sdi->channels); | |
258 | ||
259 | for (l = sdi->channel_groups; l; l = l->next) { | |
260 | cg = l->data; | |
261 | g_free(cg->name); | |
262 | g_slist_free(cg->channels); | |
263 | g_free(cg->priv); | |
264 | g_free(cg); | |
265 | } | |
266 | g_slist_free(sdi->channel_groups); | |
267 | ||
268 | g_free(sdi->vendor); | |
269 | g_free(sdi->model); | |
270 | g_free(sdi->version); | |
271 | g_free(sdi->serial_num); | |
272 | g_free(sdi->connection_id); | |
273 | g_free(sdi); | |
274 | } | |
275 | ||
276 | #ifdef HAVE_LIBUSB_1_0 | |
277 | ||
278 | /** @private | |
279 | * Allocate and init struct for USB device instance. | |
280 | * @param[in] bus @copydoc sr_usb_dev_inst::bus | |
281 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
282 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
283 | * | |
284 | * @retval other struct sr_usb_dev_inst * for USB device instance. | |
285 | */ | |
286 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, | |
287 | uint8_t address, struct libusb_device_handle *hdl) | |
288 | { | |
289 | struct sr_usb_dev_inst *udi; | |
290 | ||
291 | udi = g_malloc0(sizeof(struct sr_usb_dev_inst)); | |
292 | udi->bus = bus; | |
293 | udi->address = address; | |
294 | udi->devhdl = hdl; | |
295 | ||
296 | return udi; | |
297 | } | |
298 | ||
299 | /** @private | |
300 | * Free struct * allocated by sr_usb_dev_inst(). | |
301 | * @param usb struct* to free. Must not be NULL. | |
302 | */ | |
303 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) | |
304 | { | |
305 | g_free(usb); | |
306 | } | |
307 | ||
308 | #endif | |
309 | ||
310 | #ifdef HAVE_LIBSERIALPORT | |
311 | ||
312 | /** | |
313 | * @private | |
314 | * | |
315 | * Both parameters are copied to newly allocated strings, and freed | |
316 | * automatically by sr_serial_dev_inst_free(). | |
317 | * | |
318 | * @param[in] port OS-specific serial port specification. Examples: | |
319 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". | |
320 | * Must not be NULL. | |
321 | * @param[in] serialcomm A serial communication parameters string, in the form | |
322 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
323 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
324 | * it may be filled in later. Can be NULL. | |
325 | * | |
326 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
327 | * or NULL on error. | |
328 | */ | |
329 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, | |
330 | const char *serialcomm) | |
331 | { | |
332 | struct sr_serial_dev_inst *serial; | |
333 | ||
334 | serial = g_malloc0(sizeof(struct sr_serial_dev_inst)); | |
335 | serial->port = g_strdup(port); | |
336 | if (serialcomm) | |
337 | serial->serialcomm = g_strdup(serialcomm); | |
338 | ||
339 | return serial; | |
340 | } | |
341 | ||
342 | /** @private | |
343 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
344 | * @param serial struct sr_serial_dev_inst * to free. Must not be NULL. | |
345 | */ | |
346 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) | |
347 | { | |
348 | g_free(serial->port); | |
349 | g_free(serial->serialcomm); | |
350 | g_free(serial); | |
351 | } | |
352 | #endif | |
353 | ||
354 | /** @private */ | |
355 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) | |
356 | { | |
357 | struct sr_usbtmc_dev_inst *usbtmc; | |
358 | ||
359 | usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst)); | |
360 | usbtmc->device = g_strdup(device); | |
361 | usbtmc->fd = -1; | |
362 | ||
363 | return usbtmc; | |
364 | } | |
365 | ||
366 | /** @private */ | |
367 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) | |
368 | { | |
369 | g_free(usbtmc->device); | |
370 | g_free(usbtmc); | |
371 | } | |
372 | ||
373 | /** | |
374 | * Get the list of devices/instances of the specified driver. | |
375 | * | |
376 | * @param driver The driver to use. Must not be NULL. | |
377 | * | |
378 | * @return The list of devices/instances of this driver, or NULL upon errors | |
379 | * or if the list is empty. | |
380 | * | |
381 | * @since 0.2.0 | |
382 | */ | |
383 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) | |
384 | { | |
385 | if (driver && driver->dev_list) | |
386 | return driver->dev_list(); | |
387 | else | |
388 | return NULL; | |
389 | } | |
390 | ||
391 | /** | |
392 | * Clear the list of device instances a driver knows about. | |
393 | * | |
394 | * @param driver The driver to use. This must be a pointer to one of | |
395 | * the entries returned by sr_driver_list(). Must not be NULL. | |
396 | * | |
397 | * @retval SR_OK Success | |
398 | * @retval SR_ERR_ARG Invalid driver | |
399 | * | |
400 | * @since 0.2.0 | |
401 | */ | |
402 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) | |
403 | { | |
404 | int ret; | |
405 | ||
406 | if (!driver) { | |
407 | sr_err("Invalid driver."); | |
408 | return SR_ERR_ARG; | |
409 | } | |
410 | ||
411 | if (driver->dev_clear) | |
412 | ret = driver->dev_clear(); | |
413 | else | |
414 | ret = std_dev_clear(driver, NULL); | |
415 | ||
416 | return ret; | |
417 | } | |
418 | ||
419 | /** | |
420 | * Open the specified device. | |
421 | * | |
422 | * @param sdi Device instance to use. Must not be NULL. | |
423 | * | |
424 | * @return SR_OK upon success, a negative error code upon errors. | |
425 | * | |
426 | * @since 0.2.0 | |
427 | */ | |
428 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) | |
429 | { | |
430 | int ret; | |
431 | ||
432 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
433 | return SR_ERR; | |
434 | ||
435 | ret = sdi->driver->dev_open(sdi); | |
436 | ||
437 | return ret; | |
438 | } | |
439 | ||
440 | /** | |
441 | * Close the specified device. | |
442 | * | |
443 | * @param sdi Device instance to use. Must not be NULL. | |
444 | * | |
445 | * @return SR_OK upon success, a negative error code upon errors. | |
446 | * | |
447 | * @since 0.2.0 | |
448 | */ | |
449 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) | |
450 | { | |
451 | int ret; | |
452 | ||
453 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
454 | return SR_ERR; | |
455 | ||
456 | ret = sdi->driver->dev_close(sdi); | |
457 | ||
458 | return ret; | |
459 | } | |
460 | ||
461 | /** | |
462 | * Queries a device instances' driver. | |
463 | * | |
464 | * @param sdi Device instance to use. Must not be NULL. | |
465 | * | |
466 | * @return The driver instance or NULL on error. | |
467 | */ | |
468 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) | |
469 | { | |
470 | if (!sdi || !sdi->driver) | |
471 | return NULL; | |
472 | ||
473 | return sdi->driver; | |
474 | } | |
475 | ||
476 | /** | |
477 | * Queries a device instances' vendor. | |
478 | * | |
479 | * @param sdi Device instance to use. Must not be NULL. | |
480 | * | |
481 | * @return The vendor string or NULL. | |
482 | */ | |
483 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) | |
484 | { | |
485 | if (!sdi) | |
486 | return NULL; | |
487 | ||
488 | return sdi->vendor; | |
489 | } | |
490 | ||
491 | /** | |
492 | * Queries a device instances' model. | |
493 | * | |
494 | * @param sdi Device instance to use. Must not be NULL. | |
495 | * | |
496 | * @return The model string or NULL. | |
497 | */ | |
498 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) | |
499 | { | |
500 | if (!sdi) | |
501 | return NULL; | |
502 | ||
503 | return sdi->model; | |
504 | } | |
505 | ||
506 | /** | |
507 | * Queries a device instances' version. | |
508 | * | |
509 | * @param sdi Device instance to use. Must not be NULL. | |
510 | * | |
511 | * @return The version string or NULL. | |
512 | */ | |
513 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) | |
514 | { | |
515 | if (!sdi) | |
516 | return NULL; | |
517 | ||
518 | return sdi->version; | |
519 | } | |
520 | ||
521 | /** | |
522 | * Queries a device instances' serial number. | |
523 | * | |
524 | * @param sdi Device instance to use. Must not be NULL. | |
525 | * | |
526 | * @return The serial number string or NULL. | |
527 | */ | |
528 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) | |
529 | { | |
530 | if (!sdi) | |
531 | return NULL; | |
532 | ||
533 | return sdi->serial_num; | |
534 | } | |
535 | ||
536 | /** | |
537 | * Queries a device instances' connection identifier. | |
538 | * | |
539 | * @param sdi Device instance to use. Must not be NULL. | |
540 | * | |
541 | * @return A copy of the connection id string or NULL. The caller is responsible | |
542 | * for g_free()ing the string when it is no longer needed. | |
543 | */ | |
544 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) | |
545 | { | |
546 | struct drv_context *drvc; | |
547 | int r, cnt, i, a, b; | |
548 | char connection_id[64]; | |
549 | ||
550 | #ifdef HAVE_LIBUSB_1_0 | |
551 | struct sr_usb_dev_inst *usb; | |
552 | struct libusb_device **devlist; | |
553 | struct libusb_device_descriptor des; | |
554 | #endif | |
555 | ||
556 | if (!sdi) | |
557 | return NULL; | |
558 | ||
559 | #ifdef HAVE_LIBSERIALPORT | |
560 | struct sr_serial_dev_inst *serial; | |
561 | ||
562 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
563 | /* connection_id isn't populated, let's do that here. */ | |
564 | ||
565 | serial = sdi->conn; | |
566 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); | |
567 | } | |
568 | #endif | |
569 | ||
570 | ||
571 | #ifdef HAVE_LIBUSB_1_0 | |
572 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { | |
573 | /* connection_id isn't populated, let's do that here. */ | |
574 | ||
575 | drvc = sdi->driver->priv; | |
576 | usb = sdi->conn; | |
577 | ||
578 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
579 | sr_err("Failed to retrieve device list: %s.", | |
580 | libusb_error_name(cnt)); | |
581 | return NULL; | |
582 | } | |
583 | ||
584 | for (i = 0; i < cnt; i++) { | |
585 | if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { | |
586 | sr_err("Failed to get device descriptor: %s.", | |
587 | libusb_error_name(r)); | |
588 | continue; | |
589 | } | |
590 | ||
591 | /* Find the USB device by the logical address we know. */ | |
592 | b = libusb_get_bus_number(devlist[i]); | |
593 | a = libusb_get_device_address(devlist[i]); | |
594 | if (b != usb->bus || a != usb->address) | |
595 | continue; | |
596 | ||
597 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
598 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); | |
599 | break; | |
600 | } | |
601 | ||
602 | libusb_free_device_list(devlist, 1); | |
603 | } | |
604 | #endif | |
605 | ||
606 | return sdi->connection_id; | |
607 | } | |
608 | ||
609 | /** | |
610 | * Queries a device instances' channel list. | |
611 | * | |
612 | * @param sdi Device instance to use. Must not be NULL. | |
613 | * | |
614 | * @return The GSList of channels or NULL. | |
615 | */ | |
616 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) | |
617 | { | |
618 | if (!sdi) | |
619 | return NULL; | |
620 | ||
621 | return sdi->channels; | |
622 | } | |
623 | ||
624 | /** | |
625 | * Queries a device instances' channel groups list. | |
626 | * | |
627 | * @param sdi Device instance to use. Must not be NULL. | |
628 | * | |
629 | * @return The GSList of channel groups or NULL. | |
630 | */ | |
631 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) | |
632 | { | |
633 | if (!sdi) | |
634 | return NULL; | |
635 | ||
636 | return sdi->channel_groups; | |
637 | } | |
638 | ||
639 | /** @} */ |