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