]>
Commit | Line | Data |
---|---|---|
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 | ||
2ecc745c UH |
44 | /** |
45 | * Allocate and initialize a new struct sr_channel and add it to sdi. | |
46 | * | |
47 | * @param[in] sdi The device instance the channel is connected to. | |
48 | * Must not be NULL. | |
49 | * @param[in] index @copydoc sr_channel::index | |
50 | * @param[in] type @copydoc sr_channel::type | |
51 | * @param[in] enabled @copydoc sr_channel::enabled | |
52 | * @param[in] name @copydoc sr_channel::name | |
53 | * | |
54 | * @return A new struct sr_channel*. | |
55 | * | |
56 | * @private | |
04cb9157 | 57 | */ |
5e23fcab ML |
58 | SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi, |
59 | int index, int type, gboolean enabled, const char *name) | |
48a486cd | 60 | { |
ba7dd8bb | 61 | struct sr_channel *ch; |
48a486cd | 62 | |
c368e6f3 | 63 | ch = g_malloc0(sizeof(struct sr_channel)); |
837b0866 | 64 | ch->sdi = sdi; |
ba7dd8bb UH |
65 | ch->index = index; |
66 | ch->type = type; | |
67 | ch->enabled = enabled; | |
48a486cd | 68 | if (name) |
ba7dd8bb | 69 | ch->name = g_strdup(name); |
48a486cd | 70 | |
5e23fcab ML |
71 | sdi->channels = g_slist_append(sdi->channels, ch); |
72 | ||
ba7dd8bb | 73 | return ch; |
48a486cd BV |
74 | } |
75 | ||
94799bc4 | 76 | /** |
6f1346fb | 77 | * Set the name of the specified channel. |
94799bc4 | 78 | * |
ba7dd8bb | 79 | * If the channel already has a different name assigned to it, it will be |
94799bc4 UH |
80 | * removed, and the new name will be saved instead. |
81 | * | |
2ecc745c UH |
82 | * @param[in] channel The channel whose name to set. Must not be NULL. |
83 | * @param[in] name The new name that the specified channel should get. | |
84 | * A copy of the string is made. | |
0e3b1439 | 85 | * |
37e8b4c4 | 86 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. |
9fb5f2df | 87 | * |
47117241 | 88 | * @since 0.3.0 |
94799bc4 | 89 | */ |
6f1346fb ML |
90 | SR_API int sr_dev_channel_name_set(struct sr_channel *channel, |
91 | const char *name) | |
a1bb33af | 92 | { |
2ecc745c | 93 | if (!channel) |
0e3b1439 | 94 | return SR_ERR_ARG; |
94799bc4 | 95 | |
6f1346fb ML |
96 | g_free(channel->name); |
97 | channel->name = g_strdup(name); | |
2ecc745c | 98 | |
6f1346fb | 99 | return SR_OK; |
a1bb33af UH |
100 | } |
101 | ||
be5bf44d | 102 | /** |
6f1346fb | 103 | * Enable or disable a channel. |
be5bf44d | 104 | * |
2ecc745c UH |
105 | * @param[in] channel The channel to enable or disable. Must not be NULL. |
106 | * @param[in] state TRUE to enable the channel, FALSE to disable. | |
be5bf44d | 107 | * |
2ecc745c | 108 | * @return SR_OK on success or SR_ERR on failure. In case of invalid |
ba7dd8bb | 109 | * arguments, SR_ERR_ARG is returned and the channel enabled state |
2a854d71 | 110 | * remains unchanged. |
9fb5f2df | 111 | * |
47117241 | 112 | * @since 0.3.0 |
be5bf44d | 113 | */ |
2ecc745c | 114 | SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state) |
be5bf44d | 115 | { |
be5bf44d | 116 | int ret; |
2a854d71 | 117 | gboolean was_enabled; |
6f1346fb | 118 | struct sr_dev_inst *sdi; |
be5bf44d | 119 | |
6f1346fb | 120 | if (!channel) |
be5bf44d BV |
121 | return SR_ERR_ARG; |
122 | ||
6f1346fb ML |
123 | sdi = channel->sdi; |
124 | was_enabled = channel->enabled; | |
125 | channel->enabled = state; | |
126 | if (!state != !was_enabled && sdi->driver | |
127 | && sdi->driver->config_channel_set) { | |
128 | ret = sdi->driver->config_channel_set( | |
129 | sdi, channel, SR_CHANNEL_SET_ENABLED); | |
130 | /* Roll back change if it wasn't applicable. */ | |
131 | if (ret != SR_OK) | |
132 | return ret; | |
be5bf44d BV |
133 | } |
134 | ||
6f1346fb | 135 | return SR_OK; |
be5bf44d BV |
136 | } |
137 | ||
9c24d16a BV |
138 | /* Returns the next enabled channel, wrapping around if necessary. */ |
139 | SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi, | |
140 | struct sr_channel *cur_channel) | |
141 | { | |
142 | struct sr_channel *next_channel; | |
143 | GSList *l; | |
144 | ||
145 | next_channel = cur_channel; | |
146 | do { | |
147 | l = g_slist_find(sdi->channels, next_channel); | |
148 | if (l && l->next) | |
149 | next_channel = l->next->data; | |
150 | else | |
151 | next_channel = sdi->channels->data; | |
152 | } while (!next_channel->enabled); | |
153 | ||
154 | return next_channel; | |
155 | } | |
156 | ||
94799bc4 | 157 | /** |
9c5332d2 UH |
158 | * Determine whether the specified device instance has the specified |
159 | * capability. | |
94799bc4 | 160 | * |
9c5332d2 | 161 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. |
8ec95d22 UH |
162 | * If the device's 'driver' field is NULL (virtual device), this |
163 | * function will always return FALSE (virtual devices don't have | |
164 | * a hardware capabilities list). | |
04cb9157 | 165 | * @param[in] key The option that should be checked for is supported by the |
4d15e5c9 | 166 | * specified device. |
94799bc4 | 167 | * |
2ecc745c | 168 | * @retval TRUE Device has the specified option. |
04cb9157 MH |
169 | * @retval FALSE Device does not have the specified option, invalid input |
170 | * parameters or other error conditions. | |
9fb5f2df | 171 | * |
53f05fa8 | 172 | * @since 0.2.0 |
94799bc4 | 173 | */ |
4d15e5c9 | 174 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) |
7d658874 | 175 | { |
003595ac | 176 | GVariant *gvar; |
4d15e5c9 | 177 | const int *devopts; |
003595ac BV |
178 | gsize num_opts, i; |
179 | int ret; | |
7d658874 | 180 | |
003595ac | 181 | if (!sdi || !sdi->driver || !sdi->driver->config_list) |
8ec95d22 | 182 | return FALSE; |
94799bc4 | 183 | |
8f996b89 | 184 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, |
92b68bb5 | 185 | &gvar, sdi, NULL) != SR_OK) |
8ec95d22 | 186 | return FALSE; |
94799bc4 | 187 | |
003595ac BV |
188 | ret = FALSE; |
189 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
190 | for (i = 0; i < num_opts; i++) { | |
d099d880 | 191 | if ((devopts[i] & SR_CONF_MASK) == key) { |
003595ac BV |
192 | ret = TRUE; |
193 | break; | |
194 | } | |
94799bc4 | 195 | } |
003595ac | 196 | g_variant_unref(gvar); |
218557b8 | 197 | |
003595ac | 198 | return ret; |
a1bb33af | 199 | } |
fd9836bf | 200 | |
e7136c62 ML |
201 | /** |
202 | * Enumerate the configuration options of the specified item. | |
203 | * | |
204 | * @param driver Pointer to the driver to be checked. Must not be NULL. | |
205 | * @param sdi Pointer to the device instance to be checked. May be NULL to | |
206 | * check driver options. | |
2ecc745c UH |
207 | * @param cg Pointer to a channel group, if a specific channel group is to |
208 | * be checked. Must be NULL to check device-wide options. | |
209 | * | |
e7136c62 ML |
210 | * @return A GArray * of enum sr_configkey values, or NULL on invalid |
211 | * arguments. The array must be freed by the caller using | |
212 | * g_array_free(). | |
213 | * | |
214 | * @since 0.4.0 | |
215 | */ | |
2ecc745c UH |
216 | SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver, |
217 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
e7136c62 ML |
218 | { |
219 | GVariant *gvar; | |
220 | const uint32_t *opts; | |
221 | uint32_t opt; | |
222 | gsize num_opts, i; | |
223 | GArray *result; | |
224 | ||
225 | if (!driver || !driver->config_list) | |
226 | return NULL; | |
227 | ||
228 | if (sdi && sdi->driver != driver) | |
229 | return NULL; | |
230 | ||
231 | if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK) | |
232 | return NULL; | |
233 | ||
234 | opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t)); | |
235 | ||
236 | result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts); | |
237 | ||
238 | for (i = 0; i < num_opts; i++) { | |
239 | opt = opts[i] & SR_CONF_MASK; | |
240 | g_array_insert_val(result, i, opt); | |
241 | } | |
242 | ||
243 | g_variant_unref(gvar); | |
244 | ||
245 | return result; | |
246 | } | |
247 | ||
71e9c54d ML |
248 | /** |
249 | * Enumerate the configuration capabilities supported by a device instance | |
250 | * for a given configuration key. | |
251 | * | |
252 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
253 | * If the device's 'driver' field is NULL (virtual device), this | |
254 | * function will always return FALSE (virtual devices don't have | |
255 | * a hardware capabilities list). | |
2ecc745c UH |
256 | * @param cg Pointer to a channel group, if a specific channel group is to |
257 | * be checked. Must be NULL to check device-wide options. | |
71e9c54d ML |
258 | * @param[in] key The option that should be checked for is supported by the |
259 | * specified device. | |
260 | * | |
261 | * @retval A bitmask of enum sr_configcap values, which will be zero for | |
262 | * invalid inputs or if the key is unsupported. | |
263 | * | |
264 | * @since 0.4.0 | |
265 | */ | |
0c697a4b | 266 | SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi, |
71e9c54d ML |
267 | const struct sr_channel_group *cg, const int key) |
268 | { | |
269 | GVariant *gvar; | |
270 | const int *devopts; | |
271 | gsize num_opts, i; | |
272 | int ret; | |
273 | ||
274 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
275 | return 0; | |
276 | ||
277 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
278 | &gvar, sdi, cg) != SR_OK) | |
279 | return 0; | |
280 | ||
281 | ret = 0; | |
282 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
283 | for (i = 0; i < num_opts; i++) { | |
284 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
285 | ret = devopts[i] & ~SR_CONF_MASK; | |
286 | break; | |
287 | } | |
288 | } | |
289 | g_variant_unref(gvar); | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
e705ce3b UH |
294 | /** |
295 | * Allocate and init a new user-generated device instance. | |
aac29cc1 | 296 | * |
2ecc745c UH |
297 | * @param vendor Device vendor. |
298 | * @param model Device model. | |
299 | * @param version Device version. | |
aac29cc1 UH |
300 | * |
301 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
302 | * sr_dev_inst_free(). | |
e705ce3b UH |
303 | */ |
304 | SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor, | |
305 | const char *model, const char *version) | |
306 | { | |
307 | struct sr_dev_inst *sdi; | |
308 | ||
aac29cc1 UH |
309 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
310 | ||
0af636be UH |
311 | sdi->vendor = g_strdup(vendor); |
312 | sdi->model = g_strdup(model); | |
313 | sdi->version = g_strdup(version); | |
e705ce3b UH |
314 | sdi->inst_type = SR_INST_USER; |
315 | ||
316 | return sdi; | |
317 | } | |
318 | ||
319 | /** | |
320 | * Add a new channel to the specified device instance. | |
2ecc745c UH |
321 | * |
322 | * @param[in] index @copydoc sr_channel::index | |
323 | * @param[in] type @copydoc sr_channel::type | |
324 | * @param[in] name @copydoc sr_channel::name | |
325 | * | |
326 | * @return SR_OK Success. | |
327 | * @return SR_OK Invalid argument. | |
e705ce3b UH |
328 | */ |
329 | SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name) | |
330 | { | |
e705ce3b UH |
331 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) |
332 | return SR_ERR_ARG; | |
333 | ||
5e23fcab | 334 | sr_channel_new(sdi, index, type, TRUE, name); |
e705ce3b UH |
335 | |
336 | return SR_OK; | |
337 | } | |
338 | ||
2ecc745c UH |
339 | /** |
340 | * Free device instance struct created by sr_dev_inst(). | |
341 | * | |
4bf93988 | 342 | * @param sdi Device instance to free. If NULL, the function will do nothing. |
2ecc745c UH |
343 | * |
344 | * @private | |
04cb9157 | 345 | */ |
48a486cd BV |
346 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) |
347 | { | |
ba7dd8bb | 348 | struct sr_channel *ch; |
b1b1944e | 349 | struct sr_channel_group *cg; |
d3cff734 BV |
350 | GSList *l; |
351 | ||
4bf93988 UH |
352 | if (!sdi) |
353 | return; | |
354 | ||
ba7dd8bb UH |
355 | for (l = sdi->channels; l; l = l->next) { |
356 | ch = l->data; | |
357 | g_free(ch->name); | |
379d2609 | 358 | g_free(ch->priv); |
ba7dd8bb | 359 | g_free(ch); |
d3cff734 | 360 | } |
ba7dd8bb | 361 | g_slist_free(sdi->channels); |
d3cff734 | 362 | |
b1b1944e BV |
363 | for (l = sdi->channel_groups; l; l = l->next) { |
364 | cg = l->data; | |
7aebe22d BV |
365 | g_free(cg->name); |
366 | g_slist_free(cg->channels); | |
b1b1944e | 367 | g_free(cg->priv); |
7aebe22d | 368 | g_free(cg); |
b1b1944e BV |
369 | } |
370 | g_slist_free(sdi->channel_groups); | |
90c7f4e9 | 371 | |
fe7b8efc SB |
372 | if (sdi->session) |
373 | sr_session_dev_remove(sdi->session, sdi); | |
374 | ||
48a486cd BV |
375 | g_free(sdi->vendor); |
376 | g_free(sdi->model); | |
377 | g_free(sdi->version); | |
2fe6210a SA |
378 | g_free(sdi->serial_num); |
379 | g_free(sdi->connection_id); | |
48a486cd BV |
380 | g_free(sdi); |
381 | } | |
382 | ||
383 | #ifdef HAVE_LIBUSB_1_0 | |
384 | ||
2ecc745c UH |
385 | /** |
386 | * Allocate and init a struct for a USB device instance. | |
387 | * | |
388 | * @param[in] bus @copydoc sr_usb_dev_inst::bus | |
389 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
390 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
04cb9157 | 391 | * |
2ecc745c UH |
392 | * @return The struct sr_usb_dev_inst * for USB device instance. |
393 | * | |
394 | * @private | |
04cb9157 | 395 | */ |
48a486cd BV |
396 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, |
397 | uint8_t address, struct libusb_device_handle *hdl) | |
398 | { | |
399 | struct sr_usb_dev_inst *udi; | |
400 | ||
91219afc | 401 | udi = g_malloc0(sizeof(struct sr_usb_dev_inst)); |
48a486cd BV |
402 | udi->bus = bus; |
403 | udi->address = address; | |
404 | udi->devhdl = hdl; | |
405 | ||
406 | return udi; | |
407 | } | |
408 | ||
2ecc745c UH |
409 | /** |
410 | * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst(). | |
411 | * | |
5801ce78 UH |
412 | * @param usb The struct sr_usb_dev_inst * to free. If NULL, this |
413 | * function does nothing. | |
2ecc745c UH |
414 | * |
415 | * @private | |
04cb9157 | 416 | */ |
48a486cd BV |
417 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) |
418 | { | |
a006798b | 419 | g_free(usb); |
48a486cd BV |
420 | } |
421 | ||
422 | #endif | |
423 | ||
c4f2dfd0 UH |
424 | #ifdef HAVE_LIBSERIALPORT |
425 | ||
9fb5f2df | 426 | /** |
2ecc745c | 427 | * Allocate and init a struct for a serial device instance. |
299bdb24 BV |
428 | * |
429 | * Both parameters are copied to newly allocated strings, and freed | |
430 | * automatically by sr_serial_dev_inst_free(). | |
9fb5f2df | 431 | * |
04cb9157 | 432 | * @param[in] port OS-specific serial port specification. Examples: |
9fb5f2df | 433 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". |
91219afc | 434 | * Must not be NULL. |
04cb9157 MH |
435 | * @param[in] serialcomm A serial communication parameters string, in the form |
436 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
437 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
91219afc | 438 | * it may be filled in later. Can be NULL. |
9fb5f2df UH |
439 | * |
440 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
441 | * or NULL on error. | |
2ecc745c UH |
442 | * |
443 | * @private | |
299bdb24 | 444 | */ |
48a486cd | 445 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, |
299bdb24 | 446 | const char *serialcomm) |
48a486cd BV |
447 | { |
448 | struct sr_serial_dev_inst *serial; | |
449 | ||
91219afc | 450 | serial = g_malloc0(sizeof(struct sr_serial_dev_inst)); |
48a486cd | 451 | serial->port = g_strdup(port); |
299bdb24 BV |
452 | if (serialcomm) |
453 | serial->serialcomm = g_strdup(serialcomm); | |
48a486cd BV |
454 | |
455 | return serial; | |
456 | } | |
457 | ||
2ecc745c UH |
458 | /** |
459 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
460 | * | |
04891a99 UH |
461 | * @param serial The struct sr_serial_dev_inst * to free. If NULL, this |
462 | * function will do nothing. | |
2ecc745c UH |
463 | * |
464 | * @private | |
04cb9157 | 465 | */ |
48a486cd BV |
466 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) |
467 | { | |
04891a99 UH |
468 | if (!serial) |
469 | return; | |
470 | ||
48a486cd | 471 | g_free(serial->port); |
299bdb24 | 472 | g_free(serial->serialcomm); |
acac8fc3 | 473 | g_free(serial); |
48a486cd | 474 | } |
c4f2dfd0 UH |
475 | #endif |
476 | ||
df823ac4 | 477 | /** @private */ |
ae67644f ML |
478 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) |
479 | { | |
480 | struct sr_usbtmc_dev_inst *usbtmc; | |
481 | ||
91219afc | 482 | usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst)); |
ae67644f ML |
483 | usbtmc->device = g_strdup(device); |
484 | usbtmc->fd = -1; | |
485 | ||
486 | return usbtmc; | |
487 | } | |
488 | ||
df823ac4 | 489 | /** @private */ |
ae67644f ML |
490 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) |
491 | { | |
492 | g_free(usbtmc->device); | |
493 | g_free(usbtmc); | |
494 | } | |
495 | ||
576ff5b0 UH |
496 | /** |
497 | * Get the list of devices/instances of the specified driver. | |
498 | * | |
499 | * @param driver The driver to use. Must not be NULL. | |
500 | * | |
501 | * @return The list of devices/instances of this driver, or NULL upon errors | |
502 | * or if the list is empty. | |
503 | * | |
53f05fa8 | 504 | * @since 0.2.0 |
576ff5b0 | 505 | */ |
f99e32af | 506 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) |
811deee4 | 507 | { |
811deee4 | 508 | if (driver && driver->dev_list) |
4f840ce9 | 509 | return driver->dev_list(driver); |
811deee4 BV |
510 | else |
511 | return NULL; | |
512 | } | |
513 | ||
576ff5b0 | 514 | /** |
8102cee4 | 515 | * Clear the list of device instances a driver knows about. |
576ff5b0 | 516 | * |
8102cee4 BV |
517 | * @param driver The driver to use. This must be a pointer to one of |
518 | * the entries returned by sr_driver_list(). Must not be NULL. | |
576ff5b0 | 519 | * |
2ecc745c UH |
520 | * @retval SR_OK Success. |
521 | * @retval SR_ERR_ARG Invalid driver. | |
576ff5b0 UH |
522 | * |
523 | * @since 0.2.0 | |
524 | */ | |
f99e32af | 525 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) |
811deee4 | 526 | { |
8102cee4 BV |
527 | int ret; |
528 | ||
529 | if (!driver) { | |
530 | sr_err("Invalid driver."); | |
531 | return SR_ERR_ARG; | |
532 | } | |
533 | ||
534 | if (driver->dev_clear) | |
4f840ce9 | 535 | ret = driver->dev_clear(driver); |
811deee4 | 536 | else |
8102cee4 BV |
537 | ret = std_dev_clear(driver, NULL); |
538 | ||
539 | return ret; | |
811deee4 BV |
540 | } |
541 | ||
576ff5b0 UH |
542 | /** |
543 | * Open the specified device. | |
544 | * | |
545 | * @param sdi Device instance to use. Must not be NULL. | |
546 | * | |
547 | * @return SR_OK upon success, a negative error code upon errors. | |
548 | * | |
549 | * @since 0.2.0 | |
550 | */ | |
efdecf4c BV |
551 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) |
552 | { | |
553 | int ret; | |
554 | ||
555 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
556 | return SR_ERR; | |
557 | ||
558 | ret = sdi->driver->dev_open(sdi); | |
559 | ||
560 | return ret; | |
561 | } | |
562 | ||
576ff5b0 UH |
563 | /** |
564 | * Close the specified device. | |
565 | * | |
566 | * @param sdi Device instance to use. Must not be NULL. | |
567 | * | |
568 | * @return SR_OK upon success, a negative error code upon errors. | |
569 | * | |
570 | * @since 0.2.0 | |
571 | */ | |
efdecf4c BV |
572 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) |
573 | { | |
574 | int ret; | |
575 | ||
576 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
577 | return SR_ERR; | |
578 | ||
579 | ret = sdi->driver->dev_close(sdi); | |
580 | ||
581 | return ret; | |
582 | } | |
583 | ||
0157fdce SA |
584 | /** |
585 | * Queries a device instances' driver. | |
586 | * | |
587 | * @param sdi Device instance to use. Must not be NULL. | |
588 | * | |
589 | * @return The driver instance or NULL on error. | |
590 | */ | |
2f5f9705 | 591 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
592 | { |
593 | if (!sdi || !sdi->driver) | |
594 | return NULL; | |
595 | ||
596 | return sdi->driver; | |
597 | } | |
598 | ||
599 | /** | |
600 | * Queries a device instances' vendor. | |
601 | * | |
602 | * @param sdi Device instance to use. Must not be NULL. | |
603 | * | |
604 | * @return The vendor string or NULL. | |
605 | */ | |
2f5f9705 | 606 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
607 | { |
608 | if (!sdi) | |
609 | return NULL; | |
610 | ||
611 | return sdi->vendor; | |
612 | } | |
613 | ||
614 | /** | |
615 | * Queries a device instances' model. | |
616 | * | |
617 | * @param sdi Device instance to use. Must not be NULL. | |
618 | * | |
619 | * @return The model string or NULL. | |
620 | */ | |
2f5f9705 | 621 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
622 | { |
623 | if (!sdi) | |
624 | return NULL; | |
625 | ||
626 | return sdi->model; | |
627 | } | |
628 | ||
629 | /** | |
630 | * Queries a device instances' version. | |
631 | * | |
632 | * @param sdi Device instance to use. Must not be NULL. | |
633 | * | |
634 | * @return The version string or NULL. | |
635 | */ | |
2f5f9705 | 636 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
637 | { |
638 | if (!sdi) | |
639 | return NULL; | |
640 | ||
641 | return sdi->version; | |
642 | } | |
643 | ||
644 | /** | |
645 | * Queries a device instances' serial number. | |
646 | * | |
647 | * @param sdi Device instance to use. Must not be NULL. | |
648 | * | |
649 | * @return The serial number string or NULL. | |
650 | */ | |
2f5f9705 | 651 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
652 | { |
653 | if (!sdi) | |
654 | return NULL; | |
655 | ||
656 | return sdi->serial_num; | |
657 | } | |
658 | ||
659 | /** | |
660 | * Queries a device instances' connection identifier. | |
661 | * | |
662 | * @param sdi Device instance to use. Must not be NULL. | |
663 | * | |
2ecc745c | 664 | * @return A copy of the connection ID string or NULL. The caller is responsible |
0157fdce SA |
665 | * for g_free()ing the string when it is no longer needed. |
666 | */ | |
2f5f9705 | 667 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) |
0157fdce | 668 | { |
468665df | 669 | #ifdef HAVE_LIBUSB_1_0 |
0157fdce | 670 | struct drv_context *drvc; |
8de8551b | 671 | int cnt, i, a, b; |
9c6a2913 | 672 | char connection_id[64]; |
0157fdce SA |
673 | struct sr_usb_dev_inst *usb; |
674 | struct libusb_device **devlist; | |
9c6a2913 | 675 | #endif |
0157fdce SA |
676 | |
677 | if (!sdi) | |
678 | return NULL; | |
679 | ||
9c6a2913 | 680 | #ifdef HAVE_LIBSERIALPORT |
e8cbb223 SA |
681 | struct sr_serial_dev_inst *serial; |
682 | ||
683 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
684 | /* connection_id isn't populated, let's do that here. */ | |
685 | ||
686 | serial = sdi->conn; | |
2f5f9705 | 687 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); |
e8cbb223 | 688 | } |
9c6a2913 | 689 | #endif |
e8cbb223 | 690 | |
9c6a2913 | 691 | #ifdef HAVE_LIBUSB_1_0 |
0157fdce SA |
692 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { |
693 | /* connection_id isn't populated, let's do that here. */ | |
694 | ||
41812aca | 695 | drvc = sdi->driver->context; |
0157fdce SA |
696 | usb = sdi->conn; |
697 | ||
698 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
699 | sr_err("Failed to retrieve device list: %s.", | |
700 | libusb_error_name(cnt)); | |
701 | return NULL; | |
702 | } | |
703 | ||
704 | for (i = 0; i < cnt; i++) { | |
0157fdce SA |
705 | /* Find the USB device by the logical address we know. */ |
706 | b = libusb_get_bus_number(devlist[i]); | |
707 | a = libusb_get_device_address(devlist[i]); | |
708 | if (b != usb->bus || a != usb->address) | |
709 | continue; | |
710 | ||
711 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
2f5f9705 | 712 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); |
0157fdce SA |
713 | break; |
714 | } | |
715 | ||
716 | libusb_free_device_list(devlist, 1); | |
717 | } | |
9c6a2913 | 718 | #endif |
0157fdce SA |
719 | |
720 | return sdi->connection_id; | |
721 | } | |
722 | ||
e437da2b UH |
723 | /** |
724 | * Queries a device instances' channel list. | |
725 | * | |
726 | * @param sdi Device instance to use. Must not be NULL. | |
727 | * | |
728 | * @return The GSList of channels or NULL. | |
729 | */ | |
2f5f9705 | 730 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) |
e437da2b UH |
731 | { |
732 | if (!sdi) | |
733 | return NULL; | |
734 | ||
735 | return sdi->channels; | |
736 | } | |
737 | ||
738 | /** | |
739 | * Queries a device instances' channel groups list. | |
740 | * | |
741 | * @param sdi Device instance to use. Must not be NULL. | |
742 | * | |
743 | * @return The GSList of channel groups or NULL. | |
744 | */ | |
2f5f9705 | 745 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) |
e437da2b UH |
746 | { |
747 | if (!sdi) | |
748 | return NULL; | |
749 | ||
750 | return sdi->channel_groups; | |
751 | } | |
752 | ||
7b870c38 | 753 | /** @} */ |