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