]> sigrok.org Git - libsigrok.git/blob - src/device.c
0eb86f4dee75ad66b05f31ba78b32ffcc39d1cba
[libsigrok.git] / src / device.c
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  */
54 SR_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  */
86 SR_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  */
111 SR_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. */
137 SR_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  */
172 SR_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  * Allocate and init a new user-generated device instance.
201  *
202  * @param vendor Device vendor
203  * @param model Device model
204  * @param version Device version
205  *
206  * @retval struct sr_dev_inst *. Dynamically allocated, free using
207  *         sr_dev_inst_free().
208  */
209 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
210                 const char *model, const char *version)
211 {
212         struct sr_dev_inst *sdi;
213
214         sdi = g_malloc0(sizeof(struct sr_dev_inst));
215
216         sdi->vendor = g_strdup(vendor);
217         sdi->model = g_strdup(model);
218         sdi->version = g_strdup(version);
219         sdi->inst_type = SR_INST_USER;
220
221         return sdi;
222 }
223
224 /**
225  * Add a new channel to the specified device instance.
226  */
227 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
228 {
229         if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
230                 return SR_ERR_ARG;
231
232         sr_channel_new(sdi, index, type, TRUE, name);
233
234         return SR_OK;
235 }
236
237 /** @private
238  *  Free device instance struct created by sr_dev_inst().
239  *  @param sdi device instance to free.
240  */
241 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
242 {
243         struct sr_channel *ch;
244         struct sr_channel_group *cg;
245         GSList *l;
246
247         for (l = sdi->channels; l; l = l->next) {
248                 ch = l->data;
249                 g_free(ch->name);
250                 g_free(ch->priv);
251                 g_free(ch);
252         }
253         g_slist_free(sdi->channels);
254
255         for (l = sdi->channel_groups; l; l = l->next) {
256                 cg = l->data;
257                 g_free(cg->name);
258                 g_slist_free(cg->channels);
259                 g_free(cg->priv);
260                 g_free(cg);
261         }
262         g_slist_free(sdi->channel_groups);
263
264         if (sdi->session)
265                 sr_session_dev_remove(sdi->session, sdi);
266
267         g_free(sdi->vendor);
268         g_free(sdi->model);
269         g_free(sdi->version);
270         g_free(sdi->serial_num);
271         g_free(sdi->connection_id);
272         g_free(sdi);
273 }
274
275 #ifdef HAVE_LIBUSB_1_0
276
277 /** @private
278  *  Allocate and init struct for USB device instance.
279  *  @param[in]  bus @copydoc sr_usb_dev_inst::bus
280  *  @param[in]  address @copydoc sr_usb_dev_inst::address
281  *  @param[in]  hdl @copydoc sr_usb_dev_inst::devhdl
282  *
283  *  @retval other struct sr_usb_dev_inst * for USB device instance.
284  */
285 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
286                         uint8_t address, struct libusb_device_handle *hdl)
287 {
288         struct sr_usb_dev_inst *udi;
289
290         udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
291         udi->bus = bus;
292         udi->address = address;
293         udi->devhdl = hdl;
294
295         return udi;
296 }
297
298 /** @private
299  *  Free struct * allocated by sr_usb_dev_inst().
300  *  @param usb  struct* to free. Must not be NULL.
301  */
302 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
303 {
304         g_free(usb);
305 }
306
307 #endif
308
309 #ifdef HAVE_LIBSERIALPORT
310
311 /**
312  * @private
313  *
314  * Both parameters are copied to newly allocated strings, and freed
315  * automatically by sr_serial_dev_inst_free().
316  *
317  * @param[in] port OS-specific serial port specification. Examples:
318  *                 "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
319  *                 Must not be NULL.
320  * @param[in] serialcomm A serial communication parameters string, in the form
321  *              of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
322  *              "9600/8n1" or "600/7o2". This is an optional parameter;
323  *              it may be filled in later. Can be NULL.
324  *
325  * @return A pointer to a newly initialized struct sr_serial_dev_inst,
326  *         or NULL on error.
327  */
328 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
329                 const char *serialcomm)
330 {
331         struct sr_serial_dev_inst *serial;
332
333         serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
334         serial->port = g_strdup(port);
335         if (serialcomm)
336                 serial->serialcomm = g_strdup(serialcomm);
337
338         return serial;
339 }
340
341 /** @private
342  *  Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
343  *  @param serial   struct sr_serial_dev_inst * to free. Must not be NULL.
344  */
345 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
346 {
347         g_free(serial->port);
348         g_free(serial->serialcomm);
349         g_free(serial);
350 }
351 #endif
352
353 /** @private */
354 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
355 {
356         struct sr_usbtmc_dev_inst *usbtmc;
357
358         usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
359         usbtmc->device = g_strdup(device);
360         usbtmc->fd = -1;
361
362         return usbtmc;
363 }
364
365 /** @private */
366 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
367 {
368         g_free(usbtmc->device);
369         g_free(usbtmc);
370 }
371
372 /**
373  * Get the list of devices/instances of the specified driver.
374  *
375  * @param driver The driver to use. Must not be NULL.
376  *
377  * @return The list of devices/instances of this driver, or NULL upon errors
378  *         or if the list is empty.
379  *
380  * @since 0.2.0
381  */
382 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
383 {
384         if (driver && driver->dev_list)
385                 return driver->dev_list(driver);
386         else
387                 return NULL;
388 }
389
390 /**
391  * Clear the list of device instances a driver knows about.
392  *
393  * @param driver The driver to use. This must be a pointer to one of
394  *               the entries returned by sr_driver_list(). Must not be NULL.
395  *
396  * @retval SR_OK Success
397  * @retval SR_ERR_ARG Invalid driver
398  *
399  * @since 0.2.0
400  */
401 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
402 {
403         int ret;
404
405         if (!driver) {
406                 sr_err("Invalid driver.");
407                 return SR_ERR_ARG;
408         }
409
410         if (driver->dev_clear)
411                 ret = driver->dev_clear(driver);
412         else
413                 ret = std_dev_clear(driver, NULL);
414
415         return ret;
416 }
417
418 /**
419  * Open the specified device.
420  *
421  * @param sdi Device instance to use. Must not be NULL.
422  *
423  * @return SR_OK upon success, a negative error code upon errors.
424  *
425  * @since 0.2.0
426  */
427 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
428 {
429         int ret;
430
431         if (!sdi || !sdi->driver || !sdi->driver->dev_open)
432                 return SR_ERR;
433
434         ret = sdi->driver->dev_open(sdi);
435
436         return ret;
437 }
438
439 /**
440  * Close the specified device.
441  *
442  * @param sdi Device instance to use. Must not be NULL.
443  *
444  * @return SR_OK upon success, a negative error code upon errors.
445  *
446  * @since 0.2.0
447  */
448 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
449 {
450         int ret;
451
452         if (!sdi || !sdi->driver || !sdi->driver->dev_close)
453                 return SR_ERR;
454
455         ret = sdi->driver->dev_close(sdi);
456
457         return ret;
458 }
459
460 /**
461  * Queries a device instances' driver.
462  *
463  * @param sdi Device instance to use. Must not be NULL.
464  *
465  * @return The driver instance or NULL on error.
466  */
467 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
468 {
469         if (!sdi || !sdi->driver)
470                 return NULL;
471
472         return sdi->driver;
473 }
474
475 /**
476  * Queries a device instances' vendor.
477  *
478  * @param sdi Device instance to use. Must not be NULL.
479  *
480  * @return The vendor string or NULL.
481  */
482 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
483 {
484         if (!sdi)
485                 return NULL;
486
487         return sdi->vendor;
488 }
489
490 /**
491  * Queries a device instances' model.
492  *
493  * @param sdi Device instance to use. Must not be NULL.
494  *
495  * @return The model string or NULL.
496  */
497 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
498 {
499         if (!sdi)
500                 return NULL;
501
502         return sdi->model;
503 }
504
505 /**
506  * Queries a device instances' version.
507  *
508  * @param sdi Device instance to use. Must not be NULL.
509  *
510  * @return The version string or NULL.
511  */
512 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
513 {
514         if (!sdi)
515                 return NULL;
516
517         return sdi->version;
518 }
519
520 /**
521  * Queries a device instances' serial number.
522  *
523  * @param sdi Device instance to use. Must not be NULL.
524  *
525  * @return The serial number string or NULL.
526  */
527 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
528 {
529         if (!sdi)
530                 return NULL;
531
532         return sdi->serial_num;
533 }
534
535 /**
536  * Queries a device instances' connection identifier.
537  *
538  * @param sdi Device instance to use. Must not be NULL.
539  *
540  * @return A copy of the connection id string or NULL. The caller is responsible
541  *         for g_free()ing the string when it is no longer needed.
542  */
543 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
544 {
545 #ifdef HAVE_LIBUSB_1_0
546         struct drv_context *drvc;
547         int cnt, i, a, b;
548         char connection_id[64];
549         struct sr_usb_dev_inst *usb;
550         struct libusb_device **devlist;
551 #endif
552
553         if (!sdi)
554                 return NULL;
555
556 #ifdef HAVE_LIBSERIALPORT
557         struct sr_serial_dev_inst *serial;
558
559         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
560                 /* connection_id isn't populated, let's do that here. */
561
562                 serial = sdi->conn;
563                 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
564         }
565 #endif
566
567 #ifdef HAVE_LIBUSB_1_0
568         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
569                 /* connection_id isn't populated, let's do that here. */
570
571                 drvc = sdi->driver->context;
572                 usb = sdi->conn;
573
574                 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
575                         sr_err("Failed to retrieve device list: %s.",
576                                libusb_error_name(cnt));
577                         return NULL;
578                 }
579
580                 for (i = 0; i < cnt; i++) {
581                         /* Find the USB device by the logical address we know. */
582                         b = libusb_get_bus_number(devlist[i]);
583                         a = libusb_get_device_address(devlist[i]);
584                         if (b != usb->bus || a != usb->address)
585                                 continue;
586
587                         usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
588                         ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
589                         break;
590                 }
591
592                 libusb_free_device_list(devlist, 1);
593         }
594 #endif
595
596         return sdi->connection_id;
597 }
598
599 /**
600  * Queries a device instances' channel list.
601  *
602  * @param sdi Device instance to use. Must not be NULL.
603  *
604  * @return The GSList of channels or NULL.
605  */
606 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
607 {
608         if (!sdi)
609                 return NULL;
610
611         return sdi->channels;
612 }
613
614 /**
615  * Queries a device instances' channel groups list.
616  *
617  * @param sdi Device instance to use. Must not be NULL.
618  *
619  * @return The GSList of channel groups or NULL.
620  */
621 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
622 {
623         if (!sdi)
624                 return NULL;
625
626         return sdi->channel_groups;
627 }
628
629 /** @} */