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