]> sigrok.org Git - libsigrok.git/blame - src/device.c
Build: Set local include directories in Makefile.am
[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
20#include <stdio.h>
21#include <glib.h>
545f9786 22#include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
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
94799bc4 136/**
9c5332d2
UH
137 * Determine whether the specified device instance has the specified
138 * capability.
94799bc4 139 *
9c5332d2 140 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
8ec95d22
UH
141 * If the device's 'driver' field is NULL (virtual device), this
142 * function will always return FALSE (virtual devices don't have
143 * a hardware capabilities list).
04cb9157 144 * @param[in] key The option that should be checked for is supported by the
4d15e5c9 145 * specified device.
94799bc4 146 *
04cb9157
MH
147 * @retval TRUE Device has the specified option
148 * @retval FALSE Device does not have the specified option, invalid input
149 * parameters or other error conditions.
9fb5f2df 150 *
53f05fa8 151 * @since 0.2.0
94799bc4 152 */
4d15e5c9 153SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
7d658874 154{
003595ac 155 GVariant *gvar;
4d15e5c9 156 const int *devopts;
003595ac
BV
157 gsize num_opts, i;
158 int ret;
7d658874 159
003595ac 160 if (!sdi || !sdi->driver || !sdi->driver->config_list)
8ec95d22 161 return FALSE;
94799bc4 162
8f996b89 163 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
92b68bb5 164 &gvar, sdi, NULL) != SR_OK)
8ec95d22 165 return FALSE;
94799bc4 166
003595ac
BV
167 ret = FALSE;
168 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
169 for (i = 0; i < num_opts; i++) {
d099d880 170 if ((devopts[i] & SR_CONF_MASK) == key) {
003595ac
BV
171 ret = TRUE;
172 break;
173 }
94799bc4 174 }
003595ac 175 g_variant_unref(gvar);
218557b8 176
003595ac 177 return ret;
a1bb33af 178}
fd9836bf 179
e705ce3b
UH
180/**
181 * Allocate and init a new user-generated device instance.
aac29cc1
UH
182 *
183 * @param vendor Device vendor
184 * @param model Device model
185 * @param version Device version
186 *
187 * @retval struct sr_dev_inst *. Dynamically allocated, free using
188 * sr_dev_inst_free().
e705ce3b
UH
189 */
190SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
191 const char *model, const char *version)
192{
193 struct sr_dev_inst *sdi;
194
aac29cc1
UH
195 sdi = g_malloc0(sizeof(struct sr_dev_inst));
196
0af636be
UH
197 sdi->vendor = g_strdup(vendor);
198 sdi->model = g_strdup(model);
199 sdi->version = g_strdup(version);
e705ce3b
UH
200 sdi->inst_type = SR_INST_USER;
201
202 return sdi;
203}
204
205/**
206 * Add a new channel to the specified device instance.
207 */
208SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
209{
e705ce3b
UH
210 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
211 return SR_ERR_ARG;
212
5e23fcab 213 sr_channel_new(sdi, index, type, TRUE, name);
e705ce3b
UH
214
215 return SR_OK;
216}
217
04cb9157
MH
218/** @private
219 * Free device instance struct created by sr_dev_inst().
7aebe22d 220 * @param sdi device instance to free.
04cb9157 221 */
48a486cd
BV
222SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
223{
ba7dd8bb 224 struct sr_channel *ch;
b1b1944e 225 struct sr_channel_group *cg;
d3cff734
BV
226 GSList *l;
227
ba7dd8bb
UH
228 for (l = sdi->channels; l; l = l->next) {
229 ch = l->data;
230 g_free(ch->name);
379d2609 231 g_free(ch->priv);
ba7dd8bb 232 g_free(ch);
d3cff734 233 }
ba7dd8bb 234 g_slist_free(sdi->channels);
d3cff734 235
b1b1944e
BV
236 for (l = sdi->channel_groups; l; l = l->next) {
237 cg = l->data;
7aebe22d
BV
238 g_free(cg->name);
239 g_slist_free(cg->channels);
b1b1944e 240 g_free(cg->priv);
7aebe22d 241 g_free(cg);
b1b1944e
BV
242 }
243 g_slist_free(sdi->channel_groups);
90c7f4e9 244
48a486cd
BV
245 g_free(sdi->vendor);
246 g_free(sdi->model);
247 g_free(sdi->version);
2fe6210a
SA
248 g_free(sdi->serial_num);
249 g_free(sdi->connection_id);
48a486cd
BV
250 g_free(sdi);
251}
252
253#ifdef HAVE_LIBUSB_1_0
254
04cb9157
MH
255/** @private
256 * Allocate and init struct for USB device instance.
2eb84c98
UH
257 * @param[in] bus @copydoc sr_usb_dev_inst::bus
258 * @param[in] address @copydoc sr_usb_dev_inst::address
259 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
04cb9157 260 *
2eb84c98 261 * @retval other struct sr_usb_dev_inst * for USB device instance.
04cb9157 262 */
48a486cd
BV
263SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
264 uint8_t address, struct libusb_device_handle *hdl)
265{
266 struct sr_usb_dev_inst *udi;
267
91219afc 268 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
48a486cd
BV
269 udi->bus = bus;
270 udi->address = address;
271 udi->devhdl = hdl;
272
273 return udi;
274}
275
04cb9157
MH
276/** @private
277 * Free struct * allocated by sr_usb_dev_inst().
2eb84c98 278 * @param usb struct* to free. Must not be NULL.
04cb9157 279 */
48a486cd
BV
280SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
281{
a006798b 282 g_free(usb);
48a486cd
BV
283}
284
285#endif
286
c4f2dfd0
UH
287#ifdef HAVE_LIBSERIALPORT
288
9fb5f2df
UH
289/**
290 * @private
299bdb24
BV
291 *
292 * Both parameters are copied to newly allocated strings, and freed
293 * automatically by sr_serial_dev_inst_free().
9fb5f2df 294 *
04cb9157 295 * @param[in] port OS-specific serial port specification. Examples:
9fb5f2df 296 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
91219afc 297 * Must not be NULL.
04cb9157
MH
298 * @param[in] serialcomm A serial communication parameters string, in the form
299 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
300 * "9600/8n1" or "600/7o2". This is an optional parameter;
91219afc 301 * it may be filled in later. Can be NULL.
9fb5f2df
UH
302 *
303 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
304 * or NULL on error.
299bdb24 305 */
48a486cd 306SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
299bdb24 307 const char *serialcomm)
48a486cd
BV
308{
309 struct sr_serial_dev_inst *serial;
310
91219afc 311 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
48a486cd 312 serial->port = g_strdup(port);
299bdb24
BV
313 if (serialcomm)
314 serial->serialcomm = g_strdup(serialcomm);
48a486cd
BV
315
316 return serial;
317}
318
04cb9157
MH
319/** @private
320 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
2eb84c98 321 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
04cb9157 322 */
48a486cd
BV
323SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
324{
325 g_free(serial->port);
299bdb24 326 g_free(serial->serialcomm);
acac8fc3 327 g_free(serial);
48a486cd 328}
c4f2dfd0
UH
329#endif
330
df823ac4 331/** @private */
ae67644f
ML
332SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
333{
334 struct sr_usbtmc_dev_inst *usbtmc;
335
91219afc 336 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
ae67644f
ML
337 usbtmc->device = g_strdup(device);
338 usbtmc->fd = -1;
339
340 return usbtmc;
341}
342
df823ac4 343/** @private */
ae67644f
ML
344SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
345{
346 g_free(usbtmc->device);
347 g_free(usbtmc);
348}
349
576ff5b0
UH
350/**
351 * Get the list of devices/instances of the specified driver.
352 *
353 * @param driver The driver to use. Must not be NULL.
354 *
355 * @return The list of devices/instances of this driver, or NULL upon errors
356 * or if the list is empty.
357 *
53f05fa8 358 * @since 0.2.0
576ff5b0 359 */
f99e32af 360SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
811deee4 361{
811deee4 362 if (driver && driver->dev_list)
4f840ce9 363 return driver->dev_list(driver);
811deee4
BV
364 else
365 return NULL;
366}
367
576ff5b0 368/**
8102cee4 369 * Clear the list of device instances a driver knows about.
576ff5b0 370 *
8102cee4
BV
371 * @param driver The driver to use. This must be a pointer to one of
372 * the entries returned by sr_driver_list(). Must not be NULL.
576ff5b0 373 *
8102cee4
BV
374 * @retval SR_OK Success
375 * @retval SR_ERR_ARG Invalid driver
576ff5b0
UH
376 *
377 * @since 0.2.0
378 */
f99e32af 379SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
811deee4 380{
8102cee4
BV
381 int ret;
382
383 if (!driver) {
384 sr_err("Invalid driver.");
385 return SR_ERR_ARG;
386 }
387
388 if (driver->dev_clear)
4f840ce9 389 ret = driver->dev_clear(driver);
811deee4 390 else
8102cee4
BV
391 ret = std_dev_clear(driver, NULL);
392
393 return ret;
811deee4
BV
394}
395
576ff5b0
UH
396/**
397 * Open the specified device.
398 *
399 * @param sdi Device instance to use. Must not be NULL.
400 *
401 * @return SR_OK upon success, a negative error code upon errors.
402 *
403 * @since 0.2.0
404 */
efdecf4c
BV
405SR_API int sr_dev_open(struct sr_dev_inst *sdi)
406{
407 int ret;
408
409 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
410 return SR_ERR;
411
412 ret = sdi->driver->dev_open(sdi);
413
414 return ret;
415}
416
576ff5b0
UH
417/**
418 * Close the specified device.
419 *
420 * @param sdi Device instance to use. Must not be NULL.
421 *
422 * @return SR_OK upon success, a negative error code upon errors.
423 *
424 * @since 0.2.0
425 */
efdecf4c
BV
426SR_API int sr_dev_close(struct sr_dev_inst *sdi)
427{
428 int ret;
429
430 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
431 return SR_ERR;
432
433 ret = sdi->driver->dev_close(sdi);
434
435 return ret;
436}
437
0157fdce
SA
438/**
439 * Queries a device instances' driver.
440 *
441 * @param sdi Device instance to use. Must not be NULL.
442 *
443 * @return The driver instance or NULL on error.
444 */
2f5f9705 445SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
0157fdce
SA
446{
447 if (!sdi || !sdi->driver)
448 return NULL;
449
450 return sdi->driver;
451}
452
453/**
454 * Queries a device instances' vendor.
455 *
456 * @param sdi Device instance to use. Must not be NULL.
457 *
458 * @return The vendor string or NULL.
459 */
2f5f9705 460SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
0157fdce
SA
461{
462 if (!sdi)
463 return NULL;
464
465 return sdi->vendor;
466}
467
468/**
469 * Queries a device instances' model.
470 *
471 * @param sdi Device instance to use. Must not be NULL.
472 *
473 * @return The model string or NULL.
474 */
2f5f9705 475SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
0157fdce
SA
476{
477 if (!sdi)
478 return NULL;
479
480 return sdi->model;
481}
482
483/**
484 * Queries a device instances' version.
485 *
486 * @param sdi Device instance to use. Must not be NULL.
487 *
488 * @return The version string or NULL.
489 */
2f5f9705 490SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
0157fdce
SA
491{
492 if (!sdi)
493 return NULL;
494
495 return sdi->version;
496}
497
498/**
499 * Queries a device instances' serial number.
500 *
501 * @param sdi Device instance to use. Must not be NULL.
502 *
503 * @return The serial number string or NULL.
504 */
2f5f9705 505SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
0157fdce
SA
506{
507 if (!sdi)
508 return NULL;
509
510 return sdi->serial_num;
511}
512
513/**
514 * Queries a device instances' connection identifier.
515 *
516 * @param sdi Device instance to use. Must not be NULL.
517 *
518 * @return A copy of the connection id string or NULL. The caller is responsible
519 * for g_free()ing the string when it is no longer needed.
520 */
2f5f9705 521SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
0157fdce 522{
468665df 523#ifdef HAVE_LIBUSB_1_0
0157fdce 524 struct drv_context *drvc;
9c6a2913
SA
525 int r, cnt, i, a, b;
526 char connection_id[64];
0157fdce
SA
527 struct sr_usb_dev_inst *usb;
528 struct libusb_device **devlist;
529 struct libusb_device_descriptor des;
9c6a2913 530#endif
0157fdce
SA
531
532 if (!sdi)
533 return NULL;
534
9c6a2913 535#ifdef HAVE_LIBSERIALPORT
e8cbb223
SA
536 struct sr_serial_dev_inst *serial;
537
538 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
539 /* connection_id isn't populated, let's do that here. */
540
541 serial = sdi->conn;
2f5f9705 542 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
e8cbb223 543 }
9c6a2913 544#endif
e8cbb223 545
9c6a2913 546#ifdef HAVE_LIBUSB_1_0
0157fdce
SA
547 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
548 /* connection_id isn't populated, let's do that here. */
549
41812aca 550 drvc = sdi->driver->context;
0157fdce
SA
551 usb = sdi->conn;
552
553 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
554 sr_err("Failed to retrieve device list: %s.",
555 libusb_error_name(cnt));
556 return NULL;
557 }
558
559 for (i = 0; i < cnt; i++) {
560 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
561 sr_err("Failed to get device descriptor: %s.",
562 libusb_error_name(r));
563 continue;
564 }
565
566 /* Find the USB device by the logical address we know. */
567 b = libusb_get_bus_number(devlist[i]);
568 a = libusb_get_device_address(devlist[i]);
569 if (b != usb->bus || a != usb->address)
570 continue;
571
572 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
2f5f9705 573 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
0157fdce
SA
574 break;
575 }
576
577 libusb_free_device_list(devlist, 1);
578 }
9c6a2913 579#endif
0157fdce
SA
580
581 return sdi->connection_id;
582}
583
e437da2b
UH
584/**
585 * Queries a device instances' channel list.
586 *
587 * @param sdi Device instance to use. Must not be NULL.
588 *
589 * @return The GSList of channels or NULL.
590 */
2f5f9705 591SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
e437da2b
UH
592{
593 if (!sdi)
594 return NULL;
595
596 return sdi->channels;
597}
598
599/**
600 * Queries a device instances' channel groups list.
601 *
602 * @param sdi Device instance to use. Must not be NULL.
603 *
604 * @return The GSList of channel groups or NULL.
605 */
2f5f9705 606SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
e437da2b
UH
607{
608 if (!sdi)
609 return NULL;
610
611 return sdi->channel_groups;
612}
613
7b870c38 614/** @} */