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