blob: ced46fa120fc665d58e672be109227c8b6c02ca0 [file] [log] [blame]
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02001/*
Uwe Hermann50985c22013-04-23 22:24:30 +02002 * This file is part of the libsigrok project.
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02003 *
Bert Vermeulen13d8e032013-03-24 11:21:00 +01004 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02005 *
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>
Uwe Hermann545f9782012-10-24 00:41:21 +020022#include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
Bert Vermeulen45c59c82012-07-05 00:55:07 +020023#include "libsigrok.h"
24#include "libsigrok-internal.h"
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020025
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020026/** @cond PRIVATE */
Martin Ling3544f842013-12-23 03:38:35 +000027#define LOG_PREFIX "device"
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020028/** @endcond */
Uwe Hermanna885ce32012-11-11 12:44:16 +010029
Uwe Hermann7b870c32012-10-21 16:13:36 +020030/**
Uwe Hermann393fb9c2012-10-22 00:30:12 +020031 * @file
32 *
33 * Device handling in libsigrok.
34 */
35
36/**
Uwe Hermann7b870c32012-10-21 16:13:36 +020037 * @defgroup grp_devices Devices
38 *
39 * Device handling in libsigrok.
40 *
41 * @{
42 */
43
Matthias Heidbrink04cb9152013-11-22 20:40:52 +010044/** @private
Uwe Hermann91aea752014-03-20 21:58:57 +010045 * 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
Matthias Heidbrink04cb9152013-11-22 20:40:52 +010050 *
Uwe Hermann91aea752014-03-20 21:58:57 +010051 * @return NULL (failure) or new struct sr_channel*.
Matthias Heidbrink04cb9152013-11-22 20:40:52 +010052 */
Uwe Hermann56d0d242014-03-24 21:36:04 +010053SR_PRIV struct sr_channel *sr_channel_new(int index, int type,
Bert Vermeulen48a486c2012-07-25 00:20:06 +020054 gboolean enabled, const char *name)
55{
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010056 struct sr_channel *ch;
Bert Vermeulen48a486c2012-07-25 00:20:06 +020057
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010058 if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) {
59 sr_err("Channel malloc failed.");
Bert Vermeulen48a486c2012-07-25 00:20:06 +020060 return NULL;
61 }
62
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010063 ch->index = index;
64 ch->type = type;
65 ch->enabled = enabled;
Bert Vermeulen48a486c2012-07-25 00:20:06 +020066 if (name)
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010067 ch->name = g_strdup(name);
Bert Vermeulen48a486c2012-07-25 00:20:06 +020068
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010069 return ch;
Bert Vermeulen48a486c2012-07-25 00:20:06 +020070}
71
Uwe Hermann94799bc2011-12-28 16:19:15 +010072/**
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010073 * Set the name of the specified channel in the specified device.
Uwe Hermann94799bc2011-12-28 16:19:15 +010074 *
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010075 * If the channel already has a different name assigned to it, it will be
Uwe Hermann94799bc2011-12-28 16:19:15 +010076 * removed, and the new name will be saved instead.
77 *
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010078 * @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
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +020082 * of the string is made.
Uwe Hermann0e3b1432011-12-28 17:16:33 +010083 *
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +020084 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +020085 *
Uwe Hermann47117242014-05-04 20:51:05 +020086 * @since 0.3.0
Uwe Hermann94799bc2011-12-28 16:19:15 +010087 */
Uwe Hermannf3ca73e2014-03-24 16:05:42 +010088SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010089 int channelnum, const char *name)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020090{
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +020091 GSList *l;
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010092 struct sr_channel *ch;
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +020093 int ret;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020094
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +020095 if (!sdi) {
96 sr_err("%s: sdi was NULL", __func__);
Uwe Hermann0e3b1432011-12-28 17:16:33 +010097 return SR_ERR_ARG;
Uwe Hermann94799bc2011-12-28 16:19:15 +010098 }
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020099
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +0200100 ret = SR_ERR_ARG;
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100101 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);
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +0200106 ret = SR_OK;
107 break;
108 }
Uwe Hermann94799bc2011-12-28 16:19:15 +0100109 }
110
Bert Vermeulen37e8b4c2012-07-22 20:05:36 +0200111 return ret;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200112}
113
Uwe Hermann94799bc2011-12-28 16:19:15 +0100114/**
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100115 * Enable or disable a channel on the specified device.
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200116 *
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100117 * @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.
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200120 *
Daniel Elstner2a854d72014-01-19 20:39:11 +0100121 * @return SR_OK on success or SR_ERR on failure. In case of invalid
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100122 * arguments, SR_ERR_ARG is returned and the channel enabled state
Daniel Elstner2a854d72014-01-19 20:39:11 +0100123 * remains unchanged.
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200124 *
Uwe Hermann47117242014-05-04 20:51:05 +0200125 * @since 0.3.0
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200126 */
Uwe Hermannf3ca73e2014-03-24 16:05:42 +0100127SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200128 gboolean state)
129{
130 GSList *l;
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100131 struct sr_channel *ch;
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200132 int ret;
Daniel Elstner2a854d72014-01-19 20:39:11 +0100133 gboolean was_enabled;
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200134
135 if (!sdi)
136 return SR_ERR_ARG;
137
138 ret = SR_ERR_ARG;
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100139 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;
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200144 ret = SR_OK;
Daniel Elstner2a854d72014-01-19 20:39:11 +0100145 if (!state != !was_enabled && sdi->driver
Uwe Hermannf3ca73e2014-03-24 16:05:42 +0100146 && sdi->driver->config_channel_set) {
147 ret = sdi->driver->config_channel_set(
Uwe Hermann3f239f02014-03-24 22:39:42 +0100148 sdi, ch, SR_CHANNEL_SET_ENABLED);
Daniel Elstner2a854d72014-01-19 20:39:11 +0100149 /* Roll back change if it wasn't applicable. */
150 if (ret == SR_ERR_ARG)
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100151 ch->enabled = was_enabled;
Daniel Elstner2a854d72014-01-19 20:39:11 +0100152 }
Bert Vermeulenbe5bf442012-07-22 12:23:59 +0200153 break;
154 }
155 }
156
157 return ret;
158}
159
160/**
Uwe Hermann9c5332d2012-10-21 16:52:56 +0200161 * Determine whether the specified device instance has the specified
162 * capability.
Uwe Hermann94799bc2011-12-28 16:19:15 +0100163 *
Uwe Hermann9c5332d2012-10-21 16:52:56 +0200164 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
Uwe Hermann8ec95d22012-03-21 19:28:43 +0100165 * 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).
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100168 * @param[in] key The option that should be checked for is supported by the
Bert Vermeulen4d15e5c2013-01-25 16:20:40 +0100169 * specified device.
Uwe Hermann94799bc2011-12-28 16:19:15 +0100170 *
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100171 * @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.
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200174 *
Uwe Hermann53f05fa2013-11-03 16:08:38 +0100175 * @since 0.2.0
Uwe Hermann94799bc2011-12-28 16:19:15 +0100176 */
Bert Vermeulen4d15e5c2013-01-25 16:20:40 +0100177SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
Bert Vermeulen7d658872011-01-31 22:34:14 +0100178{
Bert Vermeulen003595a2013-03-25 20:21:10 +0100179 GVariant *gvar;
Bert Vermeulen4d15e5c2013-01-25 16:20:40 +0100180 const int *devopts;
Bert Vermeulen003595a2013-03-25 20:21:10 +0100181 gsize num_opts, i;
182 int ret;
Bert Vermeulen7d658872011-01-31 22:34:14 +0100183
Bert Vermeulen003595a2013-03-25 20:21:10 +0100184 if (!sdi || !sdi->driver || !sdi->driver->config_list)
Uwe Hermann8ec95d22012-03-21 19:28:43 +0100185 return FALSE;
Uwe Hermann218557b2011-02-13 11:27:52 +0100186
Martin Ling8f996b82013-04-20 01:00:49 +0100187 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
poljar (Damir Jelić)92b68bb2014-01-16 02:53:41 +0100188 &gvar, sdi, NULL) != SR_OK)
Uwe Hermann8ec95d22012-03-21 19:28:43 +0100189 return FALSE;
Uwe Hermann94799bc2011-12-28 16:19:15 +0100190
Bert Vermeulen003595a2013-03-25 20:21:10 +0100191 ret = FALSE;
192 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
193 for (i = 0; i < num_opts; i++) {
Bert Vermeulend099d882014-10-09 23:42:24 +0200194 if ((devopts[i] & SR_CONF_MASK) == key) {
Bert Vermeulen003595a2013-03-25 20:21:10 +0100195 ret = TRUE;
196 break;
197 }
Uwe Hermann94799bc2011-12-28 16:19:15 +0100198 }
Bert Vermeulen003595a2013-03-25 20:21:10 +0100199 g_variant_unref(gvar);
Uwe Hermann94799bc2011-12-28 16:19:15 +0100200
Bert Vermeulen003595a2013-03-25 20:21:10 +0100201 return ret;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200202}
Anatoly Sokolovfd9836b2012-01-29 16:56:06 +0400203
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100204/** @private
205 * Allocate and init new device instance struct.
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100206 * @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
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100211 *
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100212 * @retval NULL Error
213 * @retval struct sr_dev_inst *. Dynamically allocated, free using
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100214 * sr_dev_inst_free().
215 */
Soeren Apel1b9e5672014-09-27 22:20:51 +0200216SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int status,
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200217 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)))) {
Uwe Hermannc4227fc2013-01-29 12:55:00 +0100222 sr_err("Device instance malloc failed.");
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200223 return NULL;
224 }
225
Bert Vermeulene8d3d6c2012-07-29 03:01:57 +0200226 sdi->driver = NULL;
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200227 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;
Soeren Apel2fe62102014-09-23 17:43:27 +0200232 sdi->serial_num = NULL;
233 sdi->connection_id = NULL;
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100234 sdi->channels = NULL;
Uwe Hermann660e3982014-03-14 21:09:37 +0100235 sdi->channel_groups = NULL;
Martin Ling0812c402014-05-04 23:07:00 +0100236 sdi->session = NULL;
Bert Vermeulen9e2e9862012-10-01 03:03:24 +0200237 sdi->conn = NULL;
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200238 sdi->priv = NULL;
239
240 return sdi;
241}
242
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100243/** @private
244 * Free device instance struct created by sr_dev_inst().
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100245 * @param sdi struct* to free.
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100246 */
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200247SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
248{
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100249 struct sr_channel *ch;
Bert Vermeulend3cff732012-08-04 12:03:05 +0200250 GSList *l;
251
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100252 for (l = sdi->channels; l; l = l->next) {
253 ch = l->data;
254 g_free(ch->name);
Bert Vermeulen379d2602014-09-08 03:25:41 +0200255 g_free(ch->priv);
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100256 g_free(ch);
Bert Vermeulend3cff732012-08-04 12:03:05 +0200257 }
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100258 g_slist_free(sdi->channels);
Bert Vermeulend3cff732012-08-04 12:03:05 +0200259
Uwe Hermann660e3982014-03-14 21:09:37 +0100260 if (sdi->channel_groups)
261 g_slist_free(sdi->channel_groups);
poljar (Damir Jelić)90c7f4e2013-11-19 01:08:18 +0100262
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200263 g_free(sdi->vendor);
264 g_free(sdi->model);
265 g_free(sdi->version);
Soeren Apel2fe62102014-09-23 17:43:27 +0200266 g_free(sdi->serial_num);
267 g_free(sdi->connection_id);
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200268 g_free(sdi);
269}
270
271#ifdef HAVE_LIBUSB_1_0
272
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100273/** @private
274 * Allocate and init struct for USB device instance.
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100275 * @param[in] bus @copydoc sr_usb_dev_inst::bus
276 * @param[in] address @copydoc sr_usb_dev_inst::address
277 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100278 *
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100279 * @retval NULL Error
280 * @retval other struct sr_usb_dev_inst * for USB device instance.
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100281 */
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200282SR_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
287 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
Uwe Hermannc4227fc2013-01-29 12:55:00 +0100288 sr_err("USB device instance malloc failed.");
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200289 return NULL;
290 }
291
292 udi->bus = bus;
293 udi->address = address;
294 udi->devhdl = hdl;
295
296 return udi;
297}
298
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100299/** @private
300 * Free struct * allocated by sr_usb_dev_inst().
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100301 * @param usb struct* to free. Must not be NULL.
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100302 */
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200303SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
304{
Bert Vermeulena0067982013-05-06 00:40:17 +0200305 g_free(usb);
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200306}
307
308#endif
309
Uwe Hermannc4f2dfd2013-11-13 19:56:13 +0100310#ifdef HAVE_LIBSERIALPORT
311
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200312/**
313 * @private
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100314 *
315 * Both parameters are copied to newly allocated strings, and freed
316 * automatically by sr_serial_dev_inst_free().
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200317 *
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100318 * @param[in] port OS-specific serial port specification. Examples:
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200319 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100320 * @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.
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200324 *
325 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
326 * or NULL on error.
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100327 */
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200328SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100329 const char *serialcomm)
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200330{
331 struct sr_serial_dev_inst *serial;
332
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100333 if (!port) {
Uwe Hermannc4227fc2013-01-29 12:55:00 +0100334 sr_err("Serial port required.");
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100335 return NULL;
336 }
337
338 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
Uwe Hermannc4227fc2013-01-29 12:55:00 +0100339 sr_err("Serial device instance malloc failed.");
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200340 return NULL;
341 }
342
343 serial->port = g_strdup(port);
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100344 if (serialcomm)
345 serial->serialcomm = g_strdup(serialcomm);
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200346
347 return serial;
348}
349
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100350/** @private
351 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
Uwe Hermann2eb84c92013-12-16 10:24:32 +0100352 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100353 */
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200354SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
355{
356 g_free(serial->port);
Bert Vermeulen299bdb22012-11-11 20:38:47 +0100357 g_free(serial->serialcomm);
Alexandru Gagniucacac8fc2012-12-19 21:39:38 -0600358 g_free(serial);
Bert Vermeulen48a486c2012-07-25 00:20:06 +0200359}
Uwe Hermannc4f2dfd2013-11-13 19:56:13 +0100360#endif
361
Uwe Hermanndf823ac2013-12-27 16:18:28 +0100362/** @private */
Martin Lingae676442013-11-29 00:48:42 +0000363SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
364{
365 struct sr_usbtmc_dev_inst *usbtmc;
366
367 if (!device) {
368 sr_err("Device name required.");
369 return NULL;
370 }
371
372 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
373 sr_err("USBTMC device instance malloc failed.");
374 return NULL;
375 }
376
377 usbtmc->device = g_strdup(device);
378 usbtmc->fd = -1;
379
380 return usbtmc;
381}
382
Uwe Hermanndf823ac2013-12-27 16:18:28 +0100383/** @private */
Martin Lingae676442013-11-29 00:48:42 +0000384SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
385{
386 g_free(usbtmc->device);
387 g_free(usbtmc);
388}
389
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200390/**
391 * Get the list of devices/instances of the specified driver.
392 *
393 * @param driver The driver to use. Must not be NULL.
394 *
395 * @return The list of devices/instances of this driver, or NULL upon errors
396 * or if the list is empty.
397 *
Uwe Hermann53f05fa2013-11-03 16:08:38 +0100398 * @since 0.2.0
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200399 */
Bert Vermeulenf99e32a2013-04-22 15:55:06 +0200400SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
Bert Vermeulen811deee2012-08-06 00:59:25 +0200401{
Bert Vermeulen811deee2012-08-06 00:59:25 +0200402 if (driver && driver->dev_list)
403 return driver->dev_list();
404 else
405 return NULL;
406}
407
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200408/**
Bert Vermeulen8102cee2014-02-26 21:25:07 +0100409 * Clear the list of device instances a driver knows about.
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200410 *
Bert Vermeulen8102cee2014-02-26 21:25:07 +0100411 * @param driver The driver to use. This must be a pointer to one of
412 * the entries returned by sr_driver_list(). Must not be NULL.
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200413 *
Bert Vermeulen8102cee2014-02-26 21:25:07 +0100414 * @retval SR_OK Success
415 * @retval SR_ERR_ARG Invalid driver
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200416 *
417 * @since 0.2.0
418 */
Bert Vermeulenf99e32a2013-04-22 15:55:06 +0200419SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
Bert Vermeulen811deee2012-08-06 00:59:25 +0200420{
Bert Vermeulen8102cee2014-02-26 21:25:07 +0100421 int ret;
422
423 if (!driver) {
424 sr_err("Invalid driver.");
425 return SR_ERR_ARG;
426 }
427
428 if (driver->dev_clear)
429 ret = driver->dev_clear();
Bert Vermeulen811deee2012-08-06 00:59:25 +0200430 else
Bert Vermeulen8102cee2014-02-26 21:25:07 +0100431 ret = std_dev_clear(driver, NULL);
432
433 return ret;
Bert Vermeulen811deee2012-08-06 00:59:25 +0200434}
435
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200436/**
437 * Open 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 */
Bert Vermeulenefdecf42013-04-23 00:21:57 +0200445SR_API int sr_dev_open(struct sr_dev_inst *sdi)
446{
447 int ret;
448
449 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
450 return SR_ERR;
451
452 ret = sdi->driver->dev_open(sdi);
453
454 return ret;
455}
456
Uwe Hermann576ff5b2013-05-05 17:14:20 +0200457/**
458 * Close the specified device.
459 *
460 * @param sdi Device instance to use. Must not be NULL.
461 *
462 * @return SR_OK upon success, a negative error code upon errors.
463 *
464 * @since 0.2.0
465 */
Bert Vermeulenefdecf42013-04-23 00:21:57 +0200466SR_API int sr_dev_close(struct sr_dev_inst *sdi)
467{
468 int ret;
469
470 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
471 return SR_ERR;
472
473 ret = sdi->driver->dev_close(sdi);
474
475 return ret;
476}
477
Soeren Apel0157fdc2014-10-16 20:17:04 +0200478/**
479 * Queries a device instances' driver.
480 *
481 * @param sdi Device instance to use. Must not be NULL.
482 *
483 * @return The driver instance or NULL on error.
484 */
485SR_API struct sr_dev_driver *sr_dev_inst_driver_get(struct sr_dev_inst *sdi)
486{
487 if (!sdi || !sdi->driver)
488 return NULL;
489
490 return sdi->driver;
491}
492
493/**
494 * Queries a device instances' vendor.
495 *
496 * @param sdi Device instance to use. Must not be NULL.
497 *
498 * @return The vendor string or NULL.
499 */
500SR_API const char *sr_dev_inst_vendor_get(struct sr_dev_inst *sdi)
501{
502 if (!sdi)
503 return NULL;
504
505 return sdi->vendor;
506}
507
508/**
509 * Queries a device instances' model.
510 *
511 * @param sdi Device instance to use. Must not be NULL.
512 *
513 * @return The model string or NULL.
514 */
515SR_API const char *sr_dev_inst_model_get(struct sr_dev_inst *sdi)
516{
517 if (!sdi)
518 return NULL;
519
520 return sdi->model;
521}
522
523/**
524 * Queries a device instances' version.
525 *
526 * @param sdi Device instance to use. Must not be NULL.
527 *
528 * @return The version string or NULL.
529 */
530SR_API const char *sr_dev_inst_version_get(struct sr_dev_inst *sdi)
531{
532 if (!sdi)
533 return NULL;
534
535 return sdi->version;
536}
537
538/**
539 * Queries a device instances' serial number.
540 *
541 * @param sdi Device instance to use. Must not be NULL.
542 *
543 * @return The serial number string or NULL.
544 */
545SR_API const char *sr_dev_inst_sernum_get(struct sr_dev_inst *sdi)
546{
547 if (!sdi)
548 return NULL;
549
550 return sdi->serial_num;
551}
552
553/**
554 * Queries a device instances' connection identifier.
555 *
556 * @param sdi Device instance to use. Must not be NULL.
557 *
558 * @return A copy of the connection id string or NULL. The caller is responsible
559 * for g_free()ing the string when it is no longer needed.
560 */
561SR_API const char *sr_dev_inst_connid_get(struct sr_dev_inst *sdi)
562{
563 struct drv_context *drvc;
564 struct sr_usb_dev_inst *usb;
565 struct libusb_device **devlist;
566 struct libusb_device_descriptor des;
567 int r, cnt, i, a, b;
568 char connection_id[64];
569
570 if (!sdi)
571 return NULL;
572
573 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
574 /* connection_id isn't populated, let's do that here. */
575
576 drvc = sdi->driver->priv;
577 usb = sdi->conn;
578
579 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
580 sr_err("Failed to retrieve device list: %s.",
581 libusb_error_name(cnt));
582 return NULL;
583 }
584
585 for (i = 0; i < cnt; i++) {
586 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
587 sr_err("Failed to get device descriptor: %s.",
588 libusb_error_name(r));
589 continue;
590 }
591
592 /* Find the USB device by the logical address we know. */
593 b = libusb_get_bus_number(devlist[i]);
594 a = libusb_get_device_address(devlist[i]);
595 if (b != usb->bus || a != usb->address)
596 continue;
597
598 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
599 sdi->connection_id = g_strdup(connection_id);
600 break;
601 }
602
603 libusb_free_device_list(devlist, 1);
604 }
605
606 return sdi->connection_id;
607}
608
Uwe Hermann7b870c32012-10-21 16:13:36 +0200609/** @} */