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