]> sigrok.org Git - libsigrok.git/blame - src/device.c
Remove unnecessary call to libusb_get_device_descriptor().
[libsigrok.git] / src / device.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
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
6ec6c43b 20#include <config.h>
a1bb33af
UH
21#include <stdio.h>
22#include <glib.h>
c1aae900 23#include <libsigrok/libsigrok.h>
45c59c8b 24#include "libsigrok-internal.h"
a1bb33af 25
2ad1deb8 26/** @cond PRIVATE */
3544f848 27#define LOG_PREFIX "device"
2ad1deb8 28/** @endcond */
a885ce3e 29
393fb9cb
UH
30/**
31 * @file
32 *
33 * Device handling in libsigrok.
34 */
35
7b870c38
UH
36/**
37 * @defgroup grp_devices Devices
38 *
39 * Device handling in libsigrok.
40 *
41 * @{
42 */
43
04cb9157 44/** @private
5e23fcab
ML
45 * Allocate and initialize new struct sr_channel and add to sdi.
46 * @param[in] sdi The device instance the channel is connected to.
91aea754
UH
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
04cb9157 51 *
c368e6f3 52 * @return A new struct sr_channel*.
04cb9157 53 */
5e23fcab
ML
54SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
55 int index, int type, gboolean enabled, const char *name)
48a486cd 56{
ba7dd8bb 57 struct sr_channel *ch;
48a486cd 58
c368e6f3 59 ch = g_malloc0(sizeof(struct sr_channel));
837b0866 60 ch->sdi = sdi;
ba7dd8bb
UH
61 ch->index = index;
62 ch->type = type;
63 ch->enabled = enabled;
48a486cd 64 if (name)
ba7dd8bb 65 ch->name = g_strdup(name);
48a486cd 66
5e23fcab
ML
67 sdi->channels = g_slist_append(sdi->channels, ch);
68
ba7dd8bb 69 return ch;
48a486cd
BV
70}
71
94799bc4 72/**
6f1346fb 73 * Set the name of the specified channel.
94799bc4 74 *
ba7dd8bb 75 * If the channel already has a different name assigned to it, it will be
94799bc4
UH
76 * removed, and the new name will be saved instead.
77 *
6f1346fb
ML
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.
0e3b1439 81 *
37e8b4c4 82 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
9fb5f2df 83 *
47117241 84 * @since 0.3.0
94799bc4 85 */
6f1346fb
ML
86SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
87 const char *name)
a1bb33af 88{
6f1346fb
ML
89 if (!channel) {
90 sr_err("%s: channel was NULL", __func__);
0e3b1439 91 return SR_ERR_ARG;
94799bc4
UH
92 }
93
6f1346fb
ML
94 g_free(channel->name);
95 channel->name = g_strdup(name);
96 return SR_OK;
a1bb33af
UH
97}
98
be5bf44d 99/**
6f1346fb 100 * Enable or disable a channel.
be5bf44d 101 *
6f1346fb
ML
102 * @param[in] channel The channel to enable or disable.
103 * @param[in] state TRUE to enable the channel, FALSE to disable.
be5bf44d 104 *
2a854d71 105 * @return SR_OK on success or SR_ERR on failure. In case of invalid
ba7dd8bb 106 * arguments, SR_ERR_ARG is returned and the channel enabled state
2a854d71 107 * remains unchanged.
9fb5f2df 108 *
47117241 109 * @since 0.3.0
be5bf44d 110 */
6f1346fb 111SR_API int sr_dev_channel_enable(struct sr_channel *channel,
be5bf44d
BV
112 gboolean state)
113{
be5bf44d 114 int ret;
2a854d71 115 gboolean was_enabled;
6f1346fb 116 struct sr_dev_inst *sdi;
be5bf44d 117
6f1346fb 118 if (!channel)
be5bf44d
BV
119 return SR_ERR_ARG;
120
6f1346fb
ML
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;
be5bf44d
BV
131 }
132
6f1346fb 133 return SR_OK;
be5bf44d
BV
134}
135
9c24d16a
BV
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
94799bc4 155/**
9c5332d2
UH
156 * Determine whether the specified device instance has the specified
157 * capability.
94799bc4 158 *
9c5332d2 159 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
8ec95d22
UH
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).
04cb9157 163 * @param[in] key The option that should be checked for is supported by the
4d15e5c9 164 * specified device.
94799bc4 165 *
04cb9157
MH
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.
9fb5f2df 169 *
53f05fa8 170 * @since 0.2.0
94799bc4 171 */
4d15e5c9 172SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
7d658874 173{
003595ac 174 GVariant *gvar;
4d15e5c9 175 const int *devopts;
003595ac
BV
176 gsize num_opts, i;
177 int ret;
7d658874 178
003595ac 179 if (!sdi || !sdi->driver || !sdi->driver->config_list)
8ec95d22 180 return FALSE;
94799bc4 181
8f996b89 182 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
92b68bb5 183 &gvar, sdi, NULL) != SR_OK)
8ec95d22 184 return FALSE;
94799bc4 185
003595ac
BV
186 ret = FALSE;
187 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
188 for (i = 0; i < num_opts; i++) {
d099d880 189 if ((devopts[i] & SR_CONF_MASK) == key) {
003595ac
BV
190 ret = TRUE;
191 break;
192 }
94799bc4 193 }
003595ac 194 g_variant_unref(gvar);
218557b8 195
003595ac 196 return ret;
a1bb33af 197}
fd9836bf 198
e705ce3b
UH
199/**
200 * Allocate and init a new user-generated device instance.
aac29cc1
UH
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().
e705ce3b
UH
208 */
209SR_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
aac29cc1
UH
214 sdi = g_malloc0(sizeof(struct sr_dev_inst));
215
0af636be
UH
216 sdi->vendor = g_strdup(vendor);
217 sdi->model = g_strdup(model);
218 sdi->version = g_strdup(version);
e705ce3b
UH
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 */
227SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
228{
e705ce3b
UH
229 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
230 return SR_ERR_ARG;
231
5e23fcab 232 sr_channel_new(sdi, index, type, TRUE, name);
e705ce3b
UH
233
234 return SR_OK;
235}
236
04cb9157
MH
237/** @private
238 * Free device instance struct created by sr_dev_inst().
7aebe22d 239 * @param sdi device instance to free.
04cb9157 240 */
48a486cd
BV
241SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
242{
ba7dd8bb 243 struct sr_channel *ch;
b1b1944e 244 struct sr_channel_group *cg;
d3cff734
BV
245 GSList *l;
246
ba7dd8bb
UH
247 for (l = sdi->channels; l; l = l->next) {
248 ch = l->data;
249 g_free(ch->name);
379d2609 250 g_free(ch->priv);
ba7dd8bb 251 g_free(ch);
d3cff734 252 }
ba7dd8bb 253 g_slist_free(sdi->channels);
d3cff734 254
b1b1944e
BV
255 for (l = sdi->channel_groups; l; l = l->next) {
256 cg = l->data;
7aebe22d
BV
257 g_free(cg->name);
258 g_slist_free(cg->channels);
b1b1944e 259 g_free(cg->priv);
7aebe22d 260 g_free(cg);
b1b1944e
BV
261 }
262 g_slist_free(sdi->channel_groups);
90c7f4e9 263
48a486cd
BV
264 g_free(sdi->vendor);
265 g_free(sdi->model);
266 g_free(sdi->version);
2fe6210a
SA
267 g_free(sdi->serial_num);
268 g_free(sdi->connection_id);
48a486cd
BV
269 g_free(sdi);
270}
271
272#ifdef HAVE_LIBUSB_1_0
273
04cb9157
MH
274/** @private
275 * Allocate and init struct for USB device instance.
2eb84c98
UH
276 * @param[in] bus @copydoc sr_usb_dev_inst::bus
277 * @param[in] address @copydoc sr_usb_dev_inst::address
278 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
04cb9157 279 *
2eb84c98 280 * @retval other struct sr_usb_dev_inst * for USB device instance.
04cb9157 281 */
48a486cd
BV
282SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
283 uint8_t address, struct libusb_device_handle *hdl)
284{
285 struct sr_usb_dev_inst *udi;
286
91219afc 287 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
48a486cd
BV
288 udi->bus = bus;
289 udi->address = address;
290 udi->devhdl = hdl;
291
292 return udi;
293}
294
04cb9157
MH
295/** @private
296 * Free struct * allocated by sr_usb_dev_inst().
2eb84c98 297 * @param usb struct* to free. Must not be NULL.
04cb9157 298 */
48a486cd
BV
299SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
300{
a006798b 301 g_free(usb);
48a486cd
BV
302}
303
304#endif
305
c4f2dfd0
UH
306#ifdef HAVE_LIBSERIALPORT
307
9fb5f2df
UH
308/**
309 * @private
299bdb24
BV
310 *
311 * Both parameters are copied to newly allocated strings, and freed
312 * automatically by sr_serial_dev_inst_free().
9fb5f2df 313 *
04cb9157 314 * @param[in] port OS-specific serial port specification. Examples:
9fb5f2df 315 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
91219afc 316 * Must not be NULL.
04cb9157
MH
317 * @param[in] serialcomm A serial communication parameters string, in the form
318 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
319 * "9600/8n1" or "600/7o2". This is an optional parameter;
91219afc 320 * it may be filled in later. Can be NULL.
9fb5f2df
UH
321 *
322 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
323 * or NULL on error.
299bdb24 324 */
48a486cd 325SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
299bdb24 326 const char *serialcomm)
48a486cd
BV
327{
328 struct sr_serial_dev_inst *serial;
329
91219afc 330 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
48a486cd 331 serial->port = g_strdup(port);
299bdb24
BV
332 if (serialcomm)
333 serial->serialcomm = g_strdup(serialcomm);
48a486cd
BV
334
335 return serial;
336}
337
04cb9157
MH
338/** @private
339 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
2eb84c98 340 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
04cb9157 341 */
48a486cd
BV
342SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
343{
344 g_free(serial->port);
299bdb24 345 g_free(serial->serialcomm);
acac8fc3 346 g_free(serial);
48a486cd 347}
c4f2dfd0
UH
348#endif
349
df823ac4 350/** @private */
ae67644f
ML
351SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
352{
353 struct sr_usbtmc_dev_inst *usbtmc;
354
91219afc 355 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
ae67644f
ML
356 usbtmc->device = g_strdup(device);
357 usbtmc->fd = -1;
358
359 return usbtmc;
360}
361
df823ac4 362/** @private */
ae67644f
ML
363SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
364{
365 g_free(usbtmc->device);
366 g_free(usbtmc);
367}
368
576ff5b0
UH
369/**
370 * Get the list of devices/instances of the specified driver.
371 *
372 * @param driver The driver to use. Must not be NULL.
373 *
374 * @return The list of devices/instances of this driver, or NULL upon errors
375 * or if the list is empty.
376 *
53f05fa8 377 * @since 0.2.0
576ff5b0 378 */
f99e32af 379SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
811deee4 380{
811deee4 381 if (driver && driver->dev_list)
4f840ce9 382 return driver->dev_list(driver);
811deee4
BV
383 else
384 return NULL;
385}
386
576ff5b0 387/**
8102cee4 388 * Clear the list of device instances a driver knows about.
576ff5b0 389 *
8102cee4
BV
390 * @param driver The driver to use. This must be a pointer to one of
391 * the entries returned by sr_driver_list(). Must not be NULL.
576ff5b0 392 *
8102cee4
BV
393 * @retval SR_OK Success
394 * @retval SR_ERR_ARG Invalid driver
576ff5b0
UH
395 *
396 * @since 0.2.0
397 */
f99e32af 398SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
811deee4 399{
8102cee4
BV
400 int ret;
401
402 if (!driver) {
403 sr_err("Invalid driver.");
404 return SR_ERR_ARG;
405 }
406
407 if (driver->dev_clear)
4f840ce9 408 ret = driver->dev_clear(driver);
811deee4 409 else
8102cee4
BV
410 ret = std_dev_clear(driver, NULL);
411
412 return ret;
811deee4
BV
413}
414
576ff5b0
UH
415/**
416 * Open the specified device.
417 *
418 * @param sdi Device instance to use. Must not be NULL.
419 *
420 * @return SR_OK upon success, a negative error code upon errors.
421 *
422 * @since 0.2.0
423 */
efdecf4c
BV
424SR_API int sr_dev_open(struct sr_dev_inst *sdi)
425{
426 int ret;
427
428 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
429 return SR_ERR;
430
431 ret = sdi->driver->dev_open(sdi);
432
433 return ret;
434}
435
576ff5b0
UH
436/**
437 * Close the specified device.
438 *
439 * @param sdi Device instance to use. Must not be NULL.
440 *
441 * @return SR_OK upon success, a negative error code upon errors.
442 *
443 * @since 0.2.0
444 */
efdecf4c
BV
445SR_API int sr_dev_close(struct sr_dev_inst *sdi)
446{
447 int ret;
448
449 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
450 return SR_ERR;
451
452 ret = sdi->driver->dev_close(sdi);
453
454 return ret;
455}
456
0157fdce
SA
457/**
458 * Queries a device instances' driver.
459 *
460 * @param sdi Device instance to use. Must not be NULL.
461 *
462 * @return The driver instance or NULL on error.
463 */
2f5f9705 464SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
0157fdce
SA
465{
466 if (!sdi || !sdi->driver)
467 return NULL;
468
469 return sdi->driver;
470}
471
472/**
473 * Queries a device instances' vendor.
474 *
475 * @param sdi Device instance to use. Must not be NULL.
476 *
477 * @return The vendor string or NULL.
478 */
2f5f9705 479SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
0157fdce
SA
480{
481 if (!sdi)
482 return NULL;
483
484 return sdi->vendor;
485}
486
487/**
488 * Queries a device instances' model.
489 *
490 * @param sdi Device instance to use. Must not be NULL.
491 *
492 * @return The model string or NULL.
493 */
2f5f9705 494SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
0157fdce
SA
495{
496 if (!sdi)
497 return NULL;
498
499 return sdi->model;
500}
501
502/**
503 * Queries a device instances' version.
504 *
505 * @param sdi Device instance to use. Must not be NULL.
506 *
507 * @return The version string or NULL.
508 */
2f5f9705 509SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
0157fdce
SA
510{
511 if (!sdi)
512 return NULL;
513
514 return sdi->version;
515}
516
517/**
518 * Queries a device instances' serial number.
519 *
520 * @param sdi Device instance to use. Must not be NULL.
521 *
522 * @return The serial number string or NULL.
523 */
2f5f9705 524SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
0157fdce
SA
525{
526 if (!sdi)
527 return NULL;
528
529 return sdi->serial_num;
530}
531
532/**
533 * Queries a device instances' connection identifier.
534 *
535 * @param sdi Device instance to use. Must not be NULL.
536 *
537 * @return A copy of the connection id string or NULL. The caller is responsible
538 * for g_free()ing the string when it is no longer needed.
539 */
2f5f9705 540SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
0157fdce 541{
468665df 542#ifdef HAVE_LIBUSB_1_0
0157fdce 543 struct drv_context *drvc;
8de8551b 544 int cnt, i, a, b;
9c6a2913 545 char connection_id[64];
0157fdce
SA
546 struct sr_usb_dev_inst *usb;
547 struct libusb_device **devlist;
9c6a2913 548#endif
0157fdce
SA
549
550 if (!sdi)
551 return NULL;
552
9c6a2913 553#ifdef HAVE_LIBSERIALPORT
e8cbb223
SA
554 struct sr_serial_dev_inst *serial;
555
556 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
557 /* connection_id isn't populated, let's do that here. */
558
559 serial = sdi->conn;
2f5f9705 560 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
e8cbb223 561 }
9c6a2913 562#endif
e8cbb223 563
9c6a2913 564#ifdef HAVE_LIBUSB_1_0
0157fdce
SA
565 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
566 /* connection_id isn't populated, let's do that here. */
567
41812aca 568 drvc = sdi->driver->context;
0157fdce
SA
569 usb = sdi->conn;
570
571 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
572 sr_err("Failed to retrieve device list: %s.",
573 libusb_error_name(cnt));
574 return NULL;
575 }
576
577 for (i = 0; i < cnt; i++) {
0157fdce
SA
578 /* Find the USB device by the logical address we know. */
579 b = libusb_get_bus_number(devlist[i]);
580 a = libusb_get_device_address(devlist[i]);
581 if (b != usb->bus || a != usb->address)
582 continue;
583
584 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
2f5f9705 585 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
0157fdce
SA
586 break;
587 }
588
589 libusb_free_device_list(devlist, 1);
590 }
9c6a2913 591#endif
0157fdce
SA
592
593 return sdi->connection_id;
594}
595
e437da2b
UH
596/**
597 * Queries a device instances' channel list.
598 *
599 * @param sdi Device instance to use. Must not be NULL.
600 *
601 * @return The GSList of channels or NULL.
602 */
2f5f9705 603SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
e437da2b
UH
604{
605 if (!sdi)
606 return NULL;
607
608 return sdi->channels;
609}
610
611/**
612 * Queries a device instances' channel groups list.
613 *
614 * @param sdi Device instance to use. Must not be NULL.
615 *
616 * @return The GSList of channel groups or NULL.
617 */
2f5f9705 618SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
e437da2b
UH
619{
620 if (!sdi)
621 return NULL;
622
623 return sdi->channel_groups;
624}
625
7b870c38 626/** @} */