]> sigrok.org Git - libsigrok.git/blob - src/device.c
sr_dev_inst_connid_get() requires libusb.
[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 <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 NULL (failure) or 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         if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) {
59                 sr_err("Channel malloc failed.");
60                 return NULL;
61         }
62
63         ch->index = index;
64         ch->type = type;
65         ch->enabled = enabled;
66         if (name)
67                 ch->name = g_strdup(name);
68
69         return ch;
70 }
71
72 /**
73  * Set the name of the specified channel in the specified device.
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 sdi The device instance the channel is connected to.
79  * @param[in] channelnum The number of the channel whose name to set.
80  *                 Note that the channel numbers start at 0.
81  * @param[in] name The new name that the specified channel should get. A copy
82  *             of the string is made.
83  *
84  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
85  *
86  * @since 0.3.0
87  */
88 SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
89                 int channelnum, const char *name)
90 {
91         GSList *l;
92         struct sr_channel *ch;
93         int ret;
94
95         if (!sdi) {
96                 sr_err("%s: sdi was NULL", __func__);
97                 return SR_ERR_ARG;
98         }
99
100         ret = SR_ERR_ARG;
101         for (l = sdi->channels; l; l = l->next) {
102                 ch = l->data;
103                 if (ch->index == channelnum) {
104                         g_free(ch->name);
105                         ch->name = g_strdup(name);
106                         ret = SR_OK;
107                         break;
108                 }
109         }
110
111         return ret;
112 }
113
114 /**
115  * Enable or disable a channel on the specified device.
116  *
117  * @param sdi The device instance the channel is connected to.
118  * @param channelnum The channel number, starting from 0.
119  * @param state TRUE to enable the channel, FALSE to disable.
120  *
121  * @return SR_OK on success or SR_ERR on failure.  In case of invalid
122  *         arguments, SR_ERR_ARG is returned and the channel enabled state
123  *         remains unchanged.
124  *
125  * @since 0.3.0
126  */
127 SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
128                 gboolean state)
129 {
130         GSList *l;
131         struct sr_channel *ch;
132         int ret;
133         gboolean was_enabled;
134
135         if (!sdi)
136                 return SR_ERR_ARG;
137
138         ret = SR_ERR_ARG;
139         for (l = sdi->channels; l; l = l->next) {
140                 ch = l->data;
141                 if (ch->index == channelnum) {
142                         was_enabled = ch->enabled;
143                         ch->enabled = state;
144                         ret = SR_OK;
145                         if (!state != !was_enabled && sdi->driver
146                                         && sdi->driver->config_channel_set) {
147                                 ret = sdi->driver->config_channel_set(
148                                         sdi, ch, SR_CHANNEL_SET_ENABLED);
149                                 /* Roll back change if it wasn't applicable. */
150                                 if (ret == SR_ERR_ARG)
151                                         ch->enabled = was_enabled;
152                         }
153                         break;
154                 }
155         }
156
157         return ret;
158 }
159
160 /**
161  * Determine whether the specified device instance has the specified
162  * capability.
163  *
164  * @param sdi Pointer to the device instance to be checked. Must not be NULL.
165  *            If the device's 'driver' field is NULL (virtual device), this
166  *            function will always return FALSE (virtual devices don't have
167  *            a hardware capabilities list).
168  * @param[in] key The option that should be checked for is supported by the
169  *            specified device.
170  *
171  * @retval TRUE Device has the specified option
172  * @retval FALSE Device does not have the specified option, invalid input
173  *         parameters or other error conditions.
174  *
175  * @since 0.2.0
176  */
177 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
178 {
179         GVariant *gvar;
180         const int *devopts;
181         gsize num_opts, i;
182         int ret;
183
184         if (!sdi || !sdi->driver || !sdi->driver->config_list)
185                 return FALSE;
186
187         if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
188                                 &gvar, sdi, NULL) != SR_OK)
189                 return FALSE;
190
191         ret = FALSE;
192         devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
193         for (i = 0; i < num_opts; i++) {
194                 if ((devopts[i] & SR_CONF_MASK) == key) {
195                         ret = TRUE;
196                         break;
197                 }
198         }
199         g_variant_unref(gvar);
200
201         return ret;
202 }
203
204 /** @private
205  *  Allocate and init new device instance struct.
206  *  @param[in]  index   @copydoc sr_dev_inst::index
207  *  @param[in]  status  @copydoc sr_dev_inst::status
208  *  @param[in]  vendor  @copydoc sr_dev_inst::vendor
209  *  @param[in]  model   @copydoc sr_dev_inst::model
210  *  @param[in]  version @copydoc sr_dev_inst::version
211  *
212  *  @retval NULL Error
213  *  @retval struct sr_dev_inst *. Dynamically allocated, free using
214  *              sr_dev_inst_free().
215  */
216 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int status,
217                 const char *vendor, const char *model, const char *version)
218 {
219         struct sr_dev_inst *sdi;
220
221         if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
222                 sr_err("Device instance malloc failed.");
223                 return NULL;
224         }
225
226         sdi->driver = NULL;
227         sdi->status = status;
228         sdi->inst_type = -1;
229         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
230         sdi->model = model ? g_strdup(model) : NULL;
231         sdi->version = version ? g_strdup(version) : NULL;
232         sdi->serial_num = NULL;
233         sdi->connection_id = NULL;
234         sdi->channels = NULL;
235         sdi->channel_groups = NULL;
236         sdi->session = NULL;
237         sdi->conn = NULL;
238         sdi->priv = NULL;
239
240         return sdi;
241 }
242
243 /** @private
244  *  Free device instance struct created by sr_dev_inst().
245  *  @param sdi  struct* to free.
246  */
247 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
248 {
249         struct sr_channel *ch;
250         struct sr_channel_group *cg;
251         GSList *l;
252
253         for (l = sdi->channels; l; l = l->next) {
254                 ch = l->data;
255                 g_free(ch->name);
256                 g_free(ch->priv);
257                 g_free(ch);
258         }
259         g_slist_free(sdi->channels);
260
261         for (l = sdi->channel_groups; l; l = l->next) {
262                 cg = l->data;
263                 g_free(cg->priv);
264         }
265         g_slist_free(sdi->channel_groups);
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 NULL Error
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         if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
292                 sr_err("USB device instance malloc failed.");
293                 return NULL;
294         }
295
296         udi->bus = bus;
297         udi->address = address;
298         udi->devhdl = hdl;
299
300         return udi;
301 }
302
303 /** @private
304  *  Free struct * allocated by sr_usb_dev_inst().
305  *  @param usb  struct* to free. Must not be NULL.
306  */
307 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
308 {
309         g_free(usb);
310 }
311
312 #endif
313
314 #ifdef HAVE_LIBSERIALPORT
315
316 /**
317  * @private
318  *
319  * Both parameters are copied to newly allocated strings, and freed
320  * automatically by sr_serial_dev_inst_free().
321  *
322  * @param[in] port OS-specific serial port specification. Examples:
323  *                 "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
324  * @param[in] serialcomm A serial communication parameters string, in the form
325  *              of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
326  *              "9600/8n1" or "600/7o2". This is an optional parameter;
327  *              it may be filled in later.
328  *
329  * @return A pointer to a newly initialized struct sr_serial_dev_inst,
330  *         or NULL on error.
331  */
332 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
333                 const char *serialcomm)
334 {
335         struct sr_serial_dev_inst *serial;
336
337         if (!port) {
338                 sr_err("Serial port required.");
339                 return NULL;
340         }
341
342         if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
343                 sr_err("Serial device instance malloc failed.");
344                 return NULL;
345         }
346
347         serial->port = g_strdup(port);
348         if (serialcomm)
349                 serial->serialcomm = g_strdup(serialcomm);
350
351         return serial;
352 }
353
354 /** @private
355  *  Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
356  *  @param serial   struct sr_serial_dev_inst * to free. Must not be NULL.
357  */
358 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
359 {
360         g_free(serial->port);
361         g_free(serial->serialcomm);
362         g_free(serial);
363 }
364 #endif
365
366 /** @private */
367 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
368 {
369         struct sr_usbtmc_dev_inst *usbtmc;
370
371         if (!device) {
372                 sr_err("Device name required.");
373                 return NULL;
374         }
375
376         if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
377                 sr_err("USBTMC device instance malloc failed.");
378                 return NULL;
379         }
380
381         usbtmc->device = g_strdup(device);
382         usbtmc->fd = -1;
383
384         return usbtmc;
385 }
386
387 /** @private */
388 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
389 {
390         g_free(usbtmc->device);
391         g_free(usbtmc);
392 }
393
394 /**
395  * Get the list of devices/instances of the specified driver.
396  *
397  * @param driver The driver to use. Must not be NULL.
398  *
399  * @return The list of devices/instances of this driver, or NULL upon errors
400  *         or if the list is empty.
401  *
402  * @since 0.2.0
403  */
404 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
405 {
406         if (driver && driver->dev_list)
407                 return driver->dev_list();
408         else
409                 return NULL;
410 }
411
412 /**
413  * Clear the list of device instances a driver knows about.
414  *
415  * @param driver The driver to use. This must be a pointer to one of
416  *               the entries returned by sr_driver_list(). Must not be NULL.
417  *
418  * @retval SR_OK Success
419  * @retval SR_ERR_ARG Invalid driver
420  *
421  * @since 0.2.0
422  */
423 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
424 {
425         int ret;
426
427         if (!driver) {
428                 sr_err("Invalid driver.");
429                 return SR_ERR_ARG;
430         }
431
432         if (driver->dev_clear)
433                 ret = driver->dev_clear();
434         else
435                 ret = std_dev_clear(driver, NULL);
436
437         return ret;
438 }
439
440 /**
441  * Open 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_open(struct sr_dev_inst *sdi)
450 {
451         int ret;
452
453         if (!sdi || !sdi->driver || !sdi->driver->dev_open)
454                 return SR_ERR;
455
456         ret = sdi->driver->dev_open(sdi);
457
458         return ret;
459 }
460
461 /**
462  * Close the specified device.
463  *
464  * @param sdi Device instance to use. Must not be NULL.
465  *
466  * @return SR_OK upon success, a negative error code upon errors.
467  *
468  * @since 0.2.0
469  */
470 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
471 {
472         int ret;
473
474         if (!sdi || !sdi->driver || !sdi->driver->dev_close)
475                 return SR_ERR;
476
477         ret = sdi->driver->dev_close(sdi);
478
479         return ret;
480 }
481
482 /**
483  * Queries a device instances' driver.
484  *
485  * @param sdi Device instance to use. Must not be NULL.
486  *
487  * @return The driver instance or NULL on error.
488  */
489 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(struct sr_dev_inst *sdi)
490 {
491         if (!sdi || !sdi->driver)
492                 return NULL;
493
494         return sdi->driver;
495 }
496
497 /**
498  * Queries a device instances' vendor.
499  *
500  * @param sdi Device instance to use. Must not be NULL.
501  *
502  * @return The vendor string or NULL.
503  */
504 SR_API const char *sr_dev_inst_vendor_get(struct sr_dev_inst *sdi)
505 {
506         if (!sdi)
507                 return NULL;
508
509         return sdi->vendor;
510 }
511
512 /**
513  * Queries a device instances' model.
514  *
515  * @param sdi Device instance to use. Must not be NULL.
516  *
517  * @return The model string or NULL.
518  */
519 SR_API const char *sr_dev_inst_model_get(struct sr_dev_inst *sdi)
520 {
521         if (!sdi)
522                 return NULL;
523
524         return sdi->model;
525 }
526
527 /**
528  * Queries a device instances' version.
529  *
530  * @param sdi Device instance to use. Must not be NULL.
531  *
532  * @return The version string or NULL.
533  */
534 SR_API const char *sr_dev_inst_version_get(struct sr_dev_inst *sdi)
535 {
536         if (!sdi)
537                 return NULL;
538
539         return sdi->version;
540 }
541
542 /**
543  * Queries a device instances' serial number.
544  *
545  * @param sdi Device instance to use. Must not be NULL.
546  *
547  * @return The serial number string or NULL.
548  */
549 SR_API const char *sr_dev_inst_sernum_get(struct sr_dev_inst *sdi)
550 {
551         if (!sdi)
552                 return NULL;
553
554         return sdi->serial_num;
555 }
556
557 #ifdef HAVE_LIBUSB_1_0
558 /**
559  * Queries a device instances' connection identifier.
560  *
561  * @param sdi Device instance to use. Must not be NULL.
562  *
563  * @return A copy of the connection id string or NULL. The caller is responsible
564  *         for g_free()ing the string when it is no longer needed.
565  */
566 SR_API const char *sr_dev_inst_connid_get(struct sr_dev_inst *sdi)
567 {
568         struct drv_context *drvc;
569         struct sr_usb_dev_inst *usb;
570         struct libusb_device **devlist;
571         struct libusb_device_descriptor des;
572         int r, cnt, i, a, b;
573         char connection_id[64];
574
575         if (!sdi)
576                 return NULL;
577
578         #ifdef HAVE_LIBSERIALPORT
579         struct sr_serial_dev_inst *serial;
580
581         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
582                 /* connection_id isn't populated, let's do that here. */
583
584                 serial = sdi->conn;
585                 sdi->connection_id = g_strdup(serial->port);
586         }
587         #endif
588
589
590         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
591                 /* connection_id isn't populated, let's do that here. */
592
593                 drvc = sdi->driver->priv;
594                 usb = sdi->conn;
595
596                 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
597                         sr_err("Failed to retrieve device list: %s.",
598                                libusb_error_name(cnt));
599                         return NULL;
600                 }
601
602                 for (i = 0; i < cnt; i++) {
603                         if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
604                                 sr_err("Failed to get device descriptor: %s.",
605                                        libusb_error_name(r));
606                                 continue;
607                         }
608
609                         /* Find the USB device by the logical address we know. */
610                         b = libusb_get_bus_number(devlist[i]);
611                         a = libusb_get_device_address(devlist[i]);
612                         if (b != usb->bus || a != usb->address)
613                                 continue;
614
615                         usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
616                         sdi->connection_id = g_strdup(connection_id);
617                         break;
618                 }
619
620                 libusb_free_device_list(devlist, 1);
621         }
622
623         return sdi->connection_id;
624 }
625 #endif
626
627 /** @} */