]> sigrok.org Git - libsigrok.git/blame_incremental - src/device.c
Remove unnecessary dev_clear() callbacks
[libsigrok.git] / src / device.c
... / ...
CommitLineData
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 <config.h>
21#include <stdio.h>
22#include <glib.h>
23#include <libsigrok/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 and add to sdi.
46 * @param[in] sdi The device instance the channel is connected to.
47 * @param[in] index @copydoc sr_channel::index
48 * @param[in] type @copydoc sr_channel::type
49 * @param[in] enabled @copydoc sr_channel::enabled
50 * @param[in] name @copydoc sr_channel::name
51 *
52 * @return A new struct sr_channel*.
53 */
54SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
55 int index, int type, gboolean enabled, const char *name)
56{
57 struct sr_channel *ch;
58
59 ch = g_malloc0(sizeof(struct sr_channel));
60 ch->sdi = sdi;
61 ch->index = index;
62 ch->type = type;
63 ch->enabled = enabled;
64 if (name)
65 ch->name = g_strdup(name);
66
67 sdi->channels = g_slist_append(sdi->channels, ch);
68
69 return ch;
70}
71
72/**
73 * Set the name of the specified channel.
74 *
75 * If the channel already has a different name assigned to it, it will be
76 * removed, and the new name will be saved instead.
77 *
78 * @param[in] channel The channel whose name to set.
79 * @param[in] name The new name that the specified channel should get. A
80 * copy of the string is made.
81 *
82 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
83 *
84 * @since 0.3.0
85 */
86SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
87 const char *name)
88{
89 if (!channel) {
90 sr_err("%s: channel was NULL", __func__);
91 return SR_ERR_ARG;
92 }
93
94 g_free(channel->name);
95 channel->name = g_strdup(name);
96 return SR_OK;
97}
98
99/**
100 * Enable or disable a channel.
101 *
102 * @param[in] channel The channel to enable or disable.
103 * @param[in] state TRUE to enable the channel, FALSE to disable.
104 *
105 * @return SR_OK on success or SR_ERR on failure. In case of invalid
106 * arguments, SR_ERR_ARG is returned and the channel enabled state
107 * remains unchanged.
108 *
109 * @since 0.3.0
110 */
111SR_API int sr_dev_channel_enable(struct sr_channel *channel,
112 gboolean state)
113{
114 int ret;
115 gboolean was_enabled;
116 struct sr_dev_inst *sdi;
117
118 if (!channel)
119 return SR_ERR_ARG;
120
121 sdi = channel->sdi;
122 was_enabled = channel->enabled;
123 channel->enabled = state;
124 if (!state != !was_enabled && sdi->driver
125 && sdi->driver->config_channel_set) {
126 ret = sdi->driver->config_channel_set(
127 sdi, channel, SR_CHANNEL_SET_ENABLED);
128 /* Roll back change if it wasn't applicable. */
129 if (ret != SR_OK)
130 return ret;
131 }
132
133 return SR_OK;
134}
135
136/* Returns the next enabled channel, wrapping around if necessary. */
137SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
138 struct sr_channel *cur_channel)
139{
140 struct sr_channel *next_channel;
141 GSList *l;
142
143 next_channel = cur_channel;
144 do {
145 l = g_slist_find(sdi->channels, next_channel);
146 if (l && l->next)
147 next_channel = l->next->data;
148 else
149 next_channel = sdi->channels->data;
150 } while (!next_channel->enabled);
151
152 return next_channel;
153}
154
155/**
156 * Determine whether the specified device instance has the specified
157 * capability.
158 *
159 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
160 * If the device's 'driver' field is NULL (virtual device), this
161 * function will always return FALSE (virtual devices don't have
162 * a hardware capabilities list).
163 * @param[in] key The option that should be checked for is supported by the
164 * specified device.
165 *
166 * @retval TRUE Device has the specified option
167 * @retval FALSE Device does not have the specified option, invalid input
168 * parameters or other error conditions.
169 *
170 * @since 0.2.0
171 */
172SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
173{
174 GVariant *gvar;
175 const int *devopts;
176 gsize num_opts, i;
177 int ret;
178
179 if (!sdi || !sdi->driver || !sdi->driver->config_list)
180 return FALSE;
181
182 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
183 &gvar, sdi, NULL) != SR_OK)
184 return FALSE;
185
186 ret = FALSE;
187 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
188 for (i = 0; i < num_opts; i++) {
189 if ((devopts[i] & SR_CONF_MASK) == key) {
190 ret = TRUE;
191 break;
192 }
193 }
194 g_variant_unref(gvar);
195
196 return ret;
197}
198
199/**
200 * Enumerate the configuration options of the specified item.
201 *
202 * @param driver Pointer to the driver to be checked. Must not be NULL.
203 * @param sdi Pointer to the device instance to be checked. May be NULL to
204 * check driver options.
205 * @param cg Pointer to a channel group, if a specific channel group is to
206 * be checked. Must be NULL to check device-wide options.
207 * @return A GArray * of enum sr_configkey values, or NULL on invalid
208 * arguments. The array must be freed by the caller using
209 * g_array_free().
210 *
211 * @since 0.4.0
212 */
213SR_API GArray *sr_dev_options(
214 const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi,
215 const struct sr_channel_group *cg)
216{
217 GVariant *gvar;
218 const uint32_t *opts;
219 uint32_t opt;
220 gsize num_opts, i;
221 GArray *result;
222
223 if (!driver || !driver->config_list)
224 return NULL;
225
226 if (sdi && sdi->driver != driver)
227 return NULL;
228
229 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
230 return NULL;
231
232 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
233
234 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
235
236 for (i = 0; i < num_opts; i++) {
237 opt = opts[i] & SR_CONF_MASK;
238 g_array_insert_val(result, i, opt);
239 }
240
241 g_variant_unref(gvar);
242
243 return result;
244}
245
246/**
247 * Enumerate the configuration capabilities supported by a device instance
248 * for a given configuration key.
249 *
250 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
251 * If the device's 'driver' field is NULL (virtual device), this
252 * function will always return FALSE (virtual devices don't have
253 * a hardware capabilities list).
254 * @param cg Pointer to a channel group, if a specific channel group is to
255 * be checked. Must be NULL to check device-wide options.
256 * @param[in] key The option that should be checked for is supported by the
257 * specified device.
258 *
259 * @retval A bitmask of enum sr_configcap values, which will be zero for
260 * invalid inputs or if the key is unsupported.
261 *
262 * @since 0.4.0
263 */
264SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
265 const struct sr_channel_group *cg, const int key)
266{
267 GVariant *gvar;
268 const int *devopts;
269 gsize num_opts, i;
270 int ret;
271
272 if (!sdi || !sdi->driver || !sdi->driver->config_list)
273 return 0;
274
275 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
276 &gvar, sdi, cg) != SR_OK)
277 return 0;
278
279 ret = 0;
280 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
281 for (i = 0; i < num_opts; i++) {
282 if ((devopts[i] & SR_CONF_MASK) == key) {
283 ret = devopts[i] & ~SR_CONF_MASK;
284 break;
285 }
286 }
287 g_variant_unref(gvar);
288
289 return ret;
290}
291
292/**
293 * Allocate and init a new user-generated device instance.
294 *
295 * @param vendor Device vendor
296 * @param model Device model
297 * @param version Device version
298 *
299 * @retval struct sr_dev_inst *. Dynamically allocated, free using
300 * sr_dev_inst_free().
301 */
302SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
303 const char *model, const char *version)
304{
305 struct sr_dev_inst *sdi;
306
307 sdi = g_malloc0(sizeof(struct sr_dev_inst));
308
309 sdi->vendor = g_strdup(vendor);
310 sdi->model = g_strdup(model);
311 sdi->version = g_strdup(version);
312 sdi->inst_type = SR_INST_USER;
313
314 return sdi;
315}
316
317/**
318 * Add a new channel to the specified device instance.
319 */
320SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
321{
322 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
323 return SR_ERR_ARG;
324
325 sr_channel_new(sdi, index, type, TRUE, name);
326
327 return SR_OK;
328}
329
330/** @private
331 * Free device instance struct created by sr_dev_inst().
332 * @param sdi device instance to free.
333 */
334SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
335{
336 struct sr_channel *ch;
337 struct sr_channel_group *cg;
338 GSList *l;
339
340 for (l = sdi->channels; l; l = l->next) {
341 ch = l->data;
342 g_free(ch->name);
343 g_free(ch->priv);
344 g_free(ch);
345 }
346 g_slist_free(sdi->channels);
347
348 for (l = sdi->channel_groups; l; l = l->next) {
349 cg = l->data;
350 g_free(cg->name);
351 g_slist_free(cg->channels);
352 g_free(cg->priv);
353 g_free(cg);
354 }
355 g_slist_free(sdi->channel_groups);
356
357 if (sdi->session)
358 sr_session_dev_remove(sdi->session, sdi);
359
360 g_free(sdi->vendor);
361 g_free(sdi->model);
362 g_free(sdi->version);
363 g_free(sdi->serial_num);
364 g_free(sdi->connection_id);
365 g_free(sdi);
366}
367
368#ifdef HAVE_LIBUSB_1_0
369
370/** @private
371 * Allocate and init struct for USB device instance.
372 * @param[in] bus @copydoc sr_usb_dev_inst::bus
373 * @param[in] address @copydoc sr_usb_dev_inst::address
374 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
375 *
376 * @retval other struct sr_usb_dev_inst * for USB device instance.
377 */
378SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
379 uint8_t address, struct libusb_device_handle *hdl)
380{
381 struct sr_usb_dev_inst *udi;
382
383 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
384 udi->bus = bus;
385 udi->address = address;
386 udi->devhdl = hdl;
387
388 return udi;
389}
390
391/** @private
392 * Free struct * allocated by sr_usb_dev_inst().
393 * @param usb struct* to free. Must not be NULL.
394 */
395SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
396{
397 g_free(usb);
398}
399
400#endif
401
402#ifdef HAVE_LIBSERIALPORT
403
404/**
405 * @private
406 *
407 * Both parameters are copied to newly allocated strings, and freed
408 * automatically by sr_serial_dev_inst_free().
409 *
410 * @param[in] port OS-specific serial port specification. Examples:
411 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
412 * Must not be NULL.
413 * @param[in] serialcomm A serial communication parameters string, in the form
414 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
415 * "9600/8n1" or "600/7o2". This is an optional parameter;
416 * it may be filled in later. Can be NULL.
417 *
418 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
419 * or NULL on error.
420 */
421SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
422 const char *serialcomm)
423{
424 struct sr_serial_dev_inst *serial;
425
426 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
427 serial->port = g_strdup(port);
428 if (serialcomm)
429 serial->serialcomm = g_strdup(serialcomm);
430
431 return serial;
432}
433
434/** @private
435 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
436 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
437 */
438SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
439{
440 g_free(serial->port);
441 g_free(serial->serialcomm);
442 g_free(serial);
443}
444#endif
445
446/** @private */
447SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
448{
449 struct sr_usbtmc_dev_inst *usbtmc;
450
451 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
452 usbtmc->device = g_strdup(device);
453 usbtmc->fd = -1;
454
455 return usbtmc;
456}
457
458/** @private */
459SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
460{
461 g_free(usbtmc->device);
462 g_free(usbtmc);
463}
464
465/**
466 * Get the list of devices/instances of the specified driver.
467 *
468 * @param driver The driver to use. Must not be NULL.
469 *
470 * @return The list of devices/instances of this driver, or NULL upon errors
471 * or if the list is empty.
472 *
473 * @since 0.2.0
474 */
475SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
476{
477 if (driver && driver->dev_list)
478 return driver->dev_list(driver);
479 else
480 return NULL;
481}
482
483/**
484 * Clear the list of device instances a driver knows about.
485 *
486 * @param driver The driver to use. This must be a pointer to one of
487 * the entries returned by sr_driver_list(). Must not be NULL.
488 *
489 * @retval SR_OK Success
490 * @retval SR_ERR_ARG Invalid driver
491 *
492 * @since 0.2.0
493 */
494SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
495{
496 int ret;
497
498 if (!driver) {
499 sr_err("Invalid driver.");
500 return SR_ERR_ARG;
501 }
502
503 if (driver->dev_clear)
504 ret = driver->dev_clear(driver);
505 else
506 ret = std_dev_clear(driver, NULL);
507
508 return ret;
509}
510
511/**
512 * Open the specified device.
513 *
514 * @param sdi Device instance to use. Must not be NULL.
515 *
516 * @return SR_OK upon success, a negative error code upon errors.
517 *
518 * @since 0.2.0
519 */
520SR_API int sr_dev_open(struct sr_dev_inst *sdi)
521{
522 int ret;
523
524 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
525 return SR_ERR;
526
527 ret = sdi->driver->dev_open(sdi);
528
529 return ret;
530}
531
532/**
533 * Close the specified device.
534 *
535 * @param sdi Device instance to use. Must not be NULL.
536 *
537 * @return SR_OK upon success, a negative error code upon errors.
538 *
539 * @since 0.2.0
540 */
541SR_API int sr_dev_close(struct sr_dev_inst *sdi)
542{
543 int ret;
544
545 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
546 return SR_ERR;
547
548 ret = sdi->driver->dev_close(sdi);
549
550 return ret;
551}
552
553/**
554 * Queries a device instances' driver.
555 *
556 * @param sdi Device instance to use. Must not be NULL.
557 *
558 * @return The driver instance or NULL on error.
559 */
560SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
561{
562 if (!sdi || !sdi->driver)
563 return NULL;
564
565 return sdi->driver;
566}
567
568/**
569 * Queries a device instances' vendor.
570 *
571 * @param sdi Device instance to use. Must not be NULL.
572 *
573 * @return The vendor string or NULL.
574 */
575SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
576{
577 if (!sdi)
578 return NULL;
579
580 return sdi->vendor;
581}
582
583/**
584 * Queries a device instances' model.
585 *
586 * @param sdi Device instance to use. Must not be NULL.
587 *
588 * @return The model string or NULL.
589 */
590SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
591{
592 if (!sdi)
593 return NULL;
594
595 return sdi->model;
596}
597
598/**
599 * Queries a device instances' version.
600 *
601 * @param sdi Device instance to use. Must not be NULL.
602 *
603 * @return The version string or NULL.
604 */
605SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
606{
607 if (!sdi)
608 return NULL;
609
610 return sdi->version;
611}
612
613/**
614 * Queries a device instances' serial number.
615 *
616 * @param sdi Device instance to use. Must not be NULL.
617 *
618 * @return The serial number string or NULL.
619 */
620SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
621{
622 if (!sdi)
623 return NULL;
624
625 return sdi->serial_num;
626}
627
628/**
629 * Queries a device instances' connection identifier.
630 *
631 * @param sdi Device instance to use. Must not be NULL.
632 *
633 * @return A copy of the connection id string or NULL. The caller is responsible
634 * for g_free()ing the string when it is no longer needed.
635 */
636SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
637{
638#ifdef HAVE_LIBUSB_1_0
639 struct drv_context *drvc;
640 int cnt, i, a, b;
641 char connection_id[64];
642 struct sr_usb_dev_inst *usb;
643 struct libusb_device **devlist;
644#endif
645
646 if (!sdi)
647 return NULL;
648
649#ifdef HAVE_LIBSERIALPORT
650 struct sr_serial_dev_inst *serial;
651
652 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
653 /* connection_id isn't populated, let's do that here. */
654
655 serial = sdi->conn;
656 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
657 }
658#endif
659
660#ifdef HAVE_LIBUSB_1_0
661 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
662 /* connection_id isn't populated, let's do that here. */
663
664 drvc = sdi->driver->context;
665 usb = sdi->conn;
666
667 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
668 sr_err("Failed to retrieve device list: %s.",
669 libusb_error_name(cnt));
670 return NULL;
671 }
672
673 for (i = 0; i < cnt; i++) {
674 /* Find the USB device by the logical address we know. */
675 b = libusb_get_bus_number(devlist[i]);
676 a = libusb_get_device_address(devlist[i]);
677 if (b != usb->bus || a != usb->address)
678 continue;
679
680 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
681 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
682 break;
683 }
684
685 libusb_free_device_list(devlist, 1);
686 }
687#endif
688
689 return sdi->connection_id;
690}
691
692/**
693 * Queries a device instances' channel list.
694 *
695 * @param sdi Device instance to use. Must not be NULL.
696 *
697 * @return The GSList of channels or NULL.
698 */
699SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
700{
701 if (!sdi)
702 return NULL;
703
704 return sdi->channels;
705}
706
707/**
708 * Queries a device instances' channel groups list.
709 *
710 * @param sdi Device instance to use. Must not be NULL.
711 *
712 * @return The GSList of channel groups or NULL.
713 */
714SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
715{
716 if (!sdi)
717 return NULL;
718
719 return sdi->channel_groups;
720}
721
722/** @} */