]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <libsigrok/libsigrok.h> | |
25 | #include "libsigrok-internal.h" | |
26 | #include "scpi.h" | |
27 | ||
28 | /** @cond PRIVATE */ | |
29 | #define LOG_PREFIX "device" | |
30 | /** @endcond */ | |
31 | ||
32 | /** | |
33 | * @file | |
34 | * | |
35 | * Device handling in libsigrok. | |
36 | */ | |
37 | ||
38 | /** | |
39 | * @defgroup grp_devices Devices | |
40 | * | |
41 | * Device handling in libsigrok. | |
42 | * | |
43 | * @{ | |
44 | */ | |
45 | ||
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 | |
59 | */ | |
60 | SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi, | |
61 | int index, int type, gboolean enabled, const char *name) | |
62 | { | |
63 | struct sr_channel *ch; | |
64 | ||
65 | ch = g_malloc0(sizeof(*ch)); | |
66 | ch->sdi = sdi; | |
67 | ch->index = index; | |
68 | ch->type = type; | |
69 | ch->enabled = enabled; | |
70 | if (name) | |
71 | ch->name = g_strdup(name); | |
72 | ||
73 | sdi->channels = g_slist_append(sdi->channels, ch); | |
74 | ||
75 | return ch; | |
76 | } | |
77 | ||
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 | * @private | |
98 | */ | |
99 | SR_PRIV void sr_channel_free_cb(void *p) | |
100 | { | |
101 | sr_channel_free(p); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Set the name of the specified channel. | |
106 | * | |
107 | * If the channel already has a different name assigned to it, it will be | |
108 | * removed, and the new name will be saved instead. | |
109 | * | |
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. | |
113 | * | |
114 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
115 | * | |
116 | * @since 0.3.0 | |
117 | */ | |
118 | SR_API int sr_dev_channel_name_set(struct sr_channel *channel, | |
119 | const char *name) | |
120 | { | |
121 | if (!channel) | |
122 | return SR_ERR_ARG; | |
123 | ||
124 | g_free(channel->name); | |
125 | channel->name = g_strdup(name); | |
126 | ||
127 | return SR_OK; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Enable or disable a channel. | |
132 | * | |
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. | |
135 | * | |
136 | * @return SR_OK on success or SR_ERR on failure. In case of invalid | |
137 | * arguments, SR_ERR_ARG is returned and the channel enabled state | |
138 | * remains unchanged. | |
139 | * | |
140 | * @since 0.3.0 | |
141 | */ | |
142 | SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state) | |
143 | { | |
144 | int ret; | |
145 | gboolean was_enabled; | |
146 | struct sr_dev_inst *sdi; | |
147 | ||
148 | if (!channel) | |
149 | return SR_ERR_ARG; | |
150 | ||
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; | |
161 | } | |
162 | ||
163 | return SR_OK; | |
164 | } | |
165 | ||
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 | */ | |
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 | ||
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 | * | |
206 | * @return #TRUE upon differences or unexpected input, #FALSE otherwise. | |
207 | * | |
208 | * @private | |
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 | * | |
232 | * @return #TRUE upon differences or unexpected input, #FALSE otherwise. | |
233 | * | |
234 | * @private | |
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 | ||
258 | /** | |
259 | * Determine whether the specified device instance has the specified | |
260 | * capability. | |
261 | * | |
262 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
263 | * If the device's 'driver' field is NULL (virtual device), this | |
264 | * function will always return FALSE (virtual devices don't have | |
265 | * a hardware capabilities list). | |
266 | * @param[in] key The option that should be checked for is supported by the | |
267 | * specified device. | |
268 | * | |
269 | * @retval TRUE Device has the specified option. | |
270 | * @retval FALSE Device does not have the specified option, invalid input | |
271 | * parameters or other error conditions. | |
272 | * | |
273 | * @since 0.2.0 | |
274 | */ | |
275 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) | |
276 | { | |
277 | GVariant *gvar; | |
278 | const int *devopts; | |
279 | gsize num_opts, i; | |
280 | int ret; | |
281 | ||
282 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
283 | return FALSE; | |
284 | ||
285 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
286 | &gvar, sdi, NULL) != SR_OK) | |
287 | return FALSE; | |
288 | ||
289 | ret = FALSE; | |
290 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
291 | for (i = 0; i < num_opts; i++) { | |
292 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
293 | ret = TRUE; | |
294 | break; | |
295 | } | |
296 | } | |
297 | g_variant_unref(gvar); | |
298 | ||
299 | return ret; | |
300 | } | |
301 | ||
302 | /** | |
303 | * Enumerate the configuration options of the specified item. | |
304 | * | |
305 | * @param driver Pointer to the driver to be checked. Must not be NULL. | |
306 | * @param sdi Pointer to the device instance to be checked. May be NULL to | |
307 | * check driver options. | |
308 | * @param cg Pointer to a channel group, if a specific channel group is to | |
309 | * be checked. Must be NULL to check device-wide options. | |
310 | * | |
311 | * @return A GArray * of enum sr_configkey values, or NULL on invalid | |
312 | * arguments. The array must be freed by the caller using | |
313 | * g_array_free(). | |
314 | * | |
315 | * @since 0.4.0 | |
316 | */ | |
317 | SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver, | |
318 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
319 | { | |
320 | GVariant *gvar; | |
321 | const uint32_t *opts; | |
322 | uint32_t opt; | |
323 | gsize num_opts, i; | |
324 | GArray *result; | |
325 | ||
326 | if (!driver || !driver->config_list) | |
327 | return NULL; | |
328 | ||
329 | if (sdi && sdi->driver != driver) | |
330 | return NULL; | |
331 | ||
332 | if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK) | |
333 | return NULL; | |
334 | ||
335 | opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t)); | |
336 | ||
337 | result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts); | |
338 | ||
339 | for (i = 0; i < num_opts; i++) { | |
340 | opt = opts[i] & SR_CONF_MASK; | |
341 | g_array_insert_val(result, i, opt); | |
342 | } | |
343 | ||
344 | g_variant_unref(gvar); | |
345 | ||
346 | return result; | |
347 | } | |
348 | ||
349 | /** | |
350 | * Enumerate the configuration capabilities supported by a device instance | |
351 | * for a given configuration key. | |
352 | * | |
353 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
354 | * If the device's 'driver' field is NULL (virtual device), this | |
355 | * function will always return FALSE (virtual devices don't have | |
356 | * a hardware capabilities list). | |
357 | * @param cg Pointer to a channel group, if a specific channel group is to | |
358 | * be checked. Must be NULL to check device-wide options. | |
359 | * @param[in] key The option that should be checked for is supported by the | |
360 | * specified device. | |
361 | * | |
362 | * @retval A bitmask of enum sr_configcap values, which will be zero for | |
363 | * invalid inputs or if the key is unsupported. | |
364 | * | |
365 | * @since 0.4.0 | |
366 | */ | |
367 | SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi, | |
368 | const struct sr_channel_group *cg, const int key) | |
369 | { | |
370 | GVariant *gvar; | |
371 | const int *devopts; | |
372 | gsize num_opts, i; | |
373 | int ret; | |
374 | ||
375 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
376 | return 0; | |
377 | ||
378 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
379 | &gvar, sdi, cg) != SR_OK) | |
380 | return 0; | |
381 | ||
382 | ret = 0; | |
383 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
384 | for (i = 0; i < num_opts; i++) { | |
385 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
386 | ret = devopts[i] & ~SR_CONF_MASK; | |
387 | break; | |
388 | } | |
389 | } | |
390 | g_variant_unref(gvar); | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
395 | /** | |
396 | * Allocate and init a new user-generated device instance. | |
397 | * | |
398 | * @param vendor Device vendor. | |
399 | * @param model Device model. | |
400 | * @param version Device version. | |
401 | * | |
402 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
403 | * sr_dev_inst_free(). | |
404 | */ | |
405 | SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor, | |
406 | const char *model, const char *version) | |
407 | { | |
408 | struct sr_dev_inst *sdi; | |
409 | ||
410 | sdi = g_malloc0(sizeof(*sdi)); | |
411 | ||
412 | sdi->vendor = g_strdup(vendor); | |
413 | sdi->model = g_strdup(model); | |
414 | sdi->version = g_strdup(version); | |
415 | sdi->inst_type = SR_INST_USER; | |
416 | ||
417 | return sdi; | |
418 | } | |
419 | ||
420 | /** | |
421 | * Add a new channel to the specified device instance. | |
422 | * | |
423 | * @param[in] index @copydoc sr_channel::index | |
424 | * @param[in] type @copydoc sr_channel::type | |
425 | * @param[in] name @copydoc sr_channel::name | |
426 | * | |
427 | * @return SR_OK Success. | |
428 | * @return SR_OK Invalid argument. | |
429 | */ | |
430 | SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name) | |
431 | { | |
432 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) | |
433 | return SR_ERR_ARG; | |
434 | ||
435 | sr_channel_new(sdi, index, type, TRUE, name); | |
436 | ||
437 | return SR_OK; | |
438 | } | |
439 | ||
440 | /** | |
441 | * Free device instance struct created by sr_dev_inst(). | |
442 | * | |
443 | * @param sdi Device instance to free. If NULL, the function will do nothing. | |
444 | * | |
445 | * @private | |
446 | */ | |
447 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) | |
448 | { | |
449 | struct sr_channel *ch; | |
450 | struct sr_channel_group *cg; | |
451 | GSList *l; | |
452 | ||
453 | if (!sdi) | |
454 | return; | |
455 | ||
456 | for (l = sdi->channels; l; l = l->next) { | |
457 | ch = l->data; | |
458 | sr_channel_free(ch); | |
459 | } | |
460 | g_slist_free(sdi->channels); | |
461 | ||
462 | for (l = sdi->channel_groups; l; l = l->next) { | |
463 | cg = l->data; | |
464 | g_free(cg->name); | |
465 | g_slist_free(cg->channels); | |
466 | g_free(cg->priv); | |
467 | g_free(cg); | |
468 | } | |
469 | g_slist_free(sdi->channel_groups); | |
470 | ||
471 | if (sdi->session) | |
472 | sr_session_dev_remove(sdi->session, sdi); | |
473 | ||
474 | g_free(sdi->vendor); | |
475 | g_free(sdi->model); | |
476 | g_free(sdi->version); | |
477 | g_free(sdi->serial_num); | |
478 | g_free(sdi->connection_id); | |
479 | g_free(sdi); | |
480 | } | |
481 | ||
482 | #ifdef HAVE_LIBUSB_1_0 | |
483 | ||
484 | /** | |
485 | * Allocate and init a struct for a USB device instance. | |
486 | * | |
487 | * @param[in] bus @copydoc sr_usb_dev_inst::bus | |
488 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
489 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
490 | * | |
491 | * @return The struct sr_usb_dev_inst * for USB device instance. | |
492 | * | |
493 | * @private | |
494 | */ | |
495 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, | |
496 | uint8_t address, struct libusb_device_handle *hdl) | |
497 | { | |
498 | struct sr_usb_dev_inst *udi; | |
499 | ||
500 | udi = g_malloc0(sizeof(*udi)); | |
501 | udi->bus = bus; | |
502 | udi->address = address; | |
503 | udi->devhdl = hdl; | |
504 | ||
505 | return udi; | |
506 | } | |
507 | ||
508 | /** | |
509 | * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst(). | |
510 | * | |
511 | * @param usb The struct sr_usb_dev_inst * to free. If NULL, this | |
512 | * function does nothing. | |
513 | * | |
514 | * @private | |
515 | */ | |
516 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) | |
517 | { | |
518 | g_free(usb); | |
519 | } | |
520 | ||
521 | #endif | |
522 | ||
523 | #ifdef HAVE_SERIAL_COMM | |
524 | ||
525 | /** | |
526 | * Allocate and init a struct for a serial device instance. | |
527 | * | |
528 | * Both parameters are copied to newly allocated strings, and freed | |
529 | * automatically by sr_serial_dev_inst_free(). | |
530 | * | |
531 | * @param[in] port OS-specific serial port specification. Examples: | |
532 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". | |
533 | * Must not be NULL. | |
534 | * @param[in] serialcomm A serial communication parameters string, in the form | |
535 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
536 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
537 | * it may be filled in later. Can be NULL. | |
538 | * | |
539 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
540 | * or NULL on error. | |
541 | * | |
542 | * @private | |
543 | */ | |
544 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, | |
545 | const char *serialcomm) | |
546 | { | |
547 | struct sr_serial_dev_inst *serial; | |
548 | ||
549 | serial = g_malloc0(sizeof(*serial)); | |
550 | serial->port = g_strdup(port); | |
551 | if (serialcomm) | |
552 | serial->serialcomm = g_strdup(serialcomm); | |
553 | ||
554 | return serial; | |
555 | } | |
556 | ||
557 | /** | |
558 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
559 | * | |
560 | * @param serial The struct sr_serial_dev_inst * to free. If NULL, this | |
561 | * function will do nothing. | |
562 | * | |
563 | * @private | |
564 | */ | |
565 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) | |
566 | { | |
567 | if (!serial) | |
568 | return; | |
569 | ||
570 | g_free(serial->port); | |
571 | g_free(serial->serialcomm); | |
572 | g_free(serial); | |
573 | } | |
574 | #endif | |
575 | ||
576 | /** @private */ | |
577 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) | |
578 | { | |
579 | struct sr_usbtmc_dev_inst *usbtmc; | |
580 | ||
581 | usbtmc = g_malloc0(sizeof(*usbtmc)); | |
582 | usbtmc->device = g_strdup(device); | |
583 | usbtmc->fd = -1; | |
584 | ||
585 | return usbtmc; | |
586 | } | |
587 | ||
588 | /** @private */ | |
589 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) | |
590 | { | |
591 | if (!usbtmc) | |
592 | return; | |
593 | ||
594 | g_free(usbtmc->device); | |
595 | g_free(usbtmc); | |
596 | } | |
597 | ||
598 | /** | |
599 | * Get the list of devices/instances of the specified driver. | |
600 | * | |
601 | * @param driver The driver to use. Must not be NULL. | |
602 | * | |
603 | * @return The list of devices/instances of this driver, or NULL upon errors | |
604 | * or if the list is empty. | |
605 | * | |
606 | * @since 0.2.0 | |
607 | */ | |
608 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) | |
609 | { | |
610 | if (driver && driver->dev_list) | |
611 | return driver->dev_list(driver); | |
612 | else | |
613 | return NULL; | |
614 | } | |
615 | ||
616 | /** | |
617 | * Clear the list of device instances a driver knows about. | |
618 | * | |
619 | * @param driver The driver to use. This must be a pointer to one of | |
620 | * the entries returned by sr_driver_list(). Must not be NULL. | |
621 | * | |
622 | * @retval SR_OK Success. | |
623 | * @retval SR_ERR_ARG Invalid driver. | |
624 | * | |
625 | * @since 0.2.0 | |
626 | */ | |
627 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) | |
628 | { | |
629 | if (!driver) { | |
630 | sr_err("Invalid driver."); | |
631 | return SR_ERR_ARG; | |
632 | } | |
633 | ||
634 | if (!driver->context) { | |
635 | /* | |
636 | * Driver was never initialized, nothing to do. | |
637 | * | |
638 | * No log message since this usually gets called for all | |
639 | * drivers, whether they were initialized or not. | |
640 | */ | |
641 | return SR_OK; | |
642 | } | |
643 | ||
644 | /* No log message here, too verbose and not very useful. */ | |
645 | ||
646 | return driver->dev_clear(driver); | |
647 | } | |
648 | ||
649 | /** | |
650 | * Open the specified device instance. | |
651 | * | |
652 | * If the device instance is already open (sdi->status == SR_ST_ACTIVE), | |
653 | * SR_ERR will be returned and no re-opening of the device will be attempted. | |
654 | * | |
655 | * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise | |
656 | * it will be left unchanged. | |
657 | * | |
658 | * @param sdi Device instance to use. Must not be NULL. | |
659 | * | |
660 | * @retval SR_OK Success. | |
661 | * @retval SR_ERR_ARG Invalid arguments. | |
662 | * @retval SR_ERR Device instance was already active, or other error. | |
663 | * | |
664 | * @since 0.2.0 | |
665 | */ | |
666 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) | |
667 | { | |
668 | int ret; | |
669 | ||
670 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
671 | return SR_ERR_ARG; | |
672 | ||
673 | if (sdi->status == SR_ST_ACTIVE) { | |
674 | sr_err("%s: Device instance already active, can't re-open.", | |
675 | sdi->driver->name); | |
676 | return SR_ERR; | |
677 | } | |
678 | ||
679 | sr_dbg("%s: Opening device instance.", sdi->driver->name); | |
680 | ||
681 | ret = sdi->driver->dev_open(sdi); | |
682 | ||
683 | if (ret == SR_OK) | |
684 | sdi->status = SR_ST_ACTIVE; | |
685 | ||
686 | return ret; | |
687 | } | |
688 | ||
689 | /** | |
690 | * Close the specified device instance. | |
691 | * | |
692 | * If the device instance is not open (sdi->status != SR_ST_ACTIVE), | |
693 | * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted. | |
694 | * | |
695 | * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether | |
696 | * there are any errors during closing of the device instance (any errors | |
697 | * will be reported via error code and log message, though). | |
698 | * | |
699 | * @param sdi Device instance to use. Must not be NULL. | |
700 | * | |
701 | * @retval SR_OK Success. | |
702 | * @retval SR_ERR_ARG Invalid arguments. | |
703 | * @retval SR_ERR_DEV_CLOSED Device instance was not active. | |
704 | * @retval SR_ERR Other error. | |
705 | * | |
706 | * @since 0.2.0 | |
707 | */ | |
708 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) | |
709 | { | |
710 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
711 | return SR_ERR_ARG; | |
712 | ||
713 | if (sdi->status != SR_ST_ACTIVE) { | |
714 | sr_err("%s: Device instance not active, can't close.", | |
715 | sdi->driver->name); | |
716 | return SR_ERR_DEV_CLOSED; | |
717 | } | |
718 | ||
719 | sdi->status = SR_ST_INACTIVE; | |
720 | ||
721 | sr_dbg("%s: Closing device instance.", sdi->driver->name); | |
722 | ||
723 | return sdi->driver->dev_close(sdi); | |
724 | } | |
725 | ||
726 | /** | |
727 | * Queries a device instances' driver. | |
728 | * | |
729 | * @param sdi Device instance to use. Must not be NULL. | |
730 | * | |
731 | * @return The driver instance or NULL on error. | |
732 | */ | |
733 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) | |
734 | { | |
735 | if (!sdi || !sdi->driver) | |
736 | return NULL; | |
737 | ||
738 | return sdi->driver; | |
739 | } | |
740 | ||
741 | /** | |
742 | * Queries a device instances' vendor. | |
743 | * | |
744 | * @param sdi Device instance to use. Must not be NULL. | |
745 | * | |
746 | * @return The vendor string or NULL. | |
747 | */ | |
748 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) | |
749 | { | |
750 | if (!sdi) | |
751 | return NULL; | |
752 | ||
753 | return sdi->vendor; | |
754 | } | |
755 | ||
756 | /** | |
757 | * Queries a device instances' model. | |
758 | * | |
759 | * @param sdi Device instance to use. Must not be NULL. | |
760 | * | |
761 | * @return The model string or NULL. | |
762 | */ | |
763 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) | |
764 | { | |
765 | if (!sdi) | |
766 | return NULL; | |
767 | ||
768 | return sdi->model; | |
769 | } | |
770 | ||
771 | /** | |
772 | * Queries a device instances' version. | |
773 | * | |
774 | * @param sdi Device instance to use. Must not be NULL. | |
775 | * | |
776 | * @return The version string or NULL. | |
777 | */ | |
778 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) | |
779 | { | |
780 | if (!sdi) | |
781 | return NULL; | |
782 | ||
783 | return sdi->version; | |
784 | } | |
785 | ||
786 | /** | |
787 | * Queries a device instances' serial number. | |
788 | * | |
789 | * @param sdi Device instance to use. Must not be NULL. | |
790 | * | |
791 | * @return The serial number string or NULL. | |
792 | */ | |
793 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) | |
794 | { | |
795 | if (!sdi) | |
796 | return NULL; | |
797 | ||
798 | return sdi->serial_num; | |
799 | } | |
800 | ||
801 | /** | |
802 | * Queries a device instances' connection identifier. | |
803 | * | |
804 | * @param sdi Device instance to use. Must not be NULL. | |
805 | * | |
806 | * @return A copy of the connection ID string or NULL. The caller is responsible | |
807 | * for g_free()ing the string when it is no longer needed. | |
808 | */ | |
809 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) | |
810 | { | |
811 | #ifdef HAVE_LIBUSB_1_0 | |
812 | struct drv_context *drvc; | |
813 | int cnt, i, a, b; | |
814 | char conn_id_usb[64]; | |
815 | struct sr_usb_dev_inst *usb; | |
816 | struct libusb_device **devlist; | |
817 | #endif | |
818 | ||
819 | #ifdef HAVE_SERIAL_COMM | |
820 | struct sr_serial_dev_inst *serial; | |
821 | #endif | |
822 | ||
823 | struct sr_scpi_dev_inst *scpi; | |
824 | char *conn_id_scpi; | |
825 | ||
826 | if (!sdi) | |
827 | return NULL; | |
828 | ||
829 | #ifdef HAVE_SERIAL_COMM | |
830 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
831 | /* connection_id isn't populated, let's do that for serial devices. */ | |
832 | ||
833 | serial = sdi->conn; | |
834 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); | |
835 | } | |
836 | #endif | |
837 | ||
838 | #ifdef HAVE_LIBUSB_1_0 | |
839 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { | |
840 | /* connection_id isn't populated, let's do that for USB devices. */ | |
841 | ||
842 | drvc = sdi->driver->context; | |
843 | usb = sdi->conn; | |
844 | ||
845 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
846 | sr_err("Failed to retrieve device list: %s.", | |
847 | libusb_error_name(cnt)); | |
848 | return NULL; | |
849 | } | |
850 | ||
851 | for (i = 0; i < cnt; i++) { | |
852 | /* Find the USB device by the logical address we know. */ | |
853 | b = libusb_get_bus_number(devlist[i]); | |
854 | a = libusb_get_device_address(devlist[i]); | |
855 | if (b != usb->bus || a != usb->address) | |
856 | continue; | |
857 | ||
858 | if (usb_get_port_path(devlist[i], conn_id_usb, sizeof(conn_id_usb)) < 0) | |
859 | continue; | |
860 | ||
861 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_usb); | |
862 | break; | |
863 | } | |
864 | ||
865 | libusb_free_device_list(devlist, 1); | |
866 | } | |
867 | #endif | |
868 | ||
869 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SCPI)) { | |
870 | /* connection_id isn't populated, let's do that for SCPI devices. */ | |
871 | ||
872 | scpi = sdi->conn; | |
873 | sr_scpi_connection_id(scpi, &conn_id_scpi); | |
874 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_scpi); | |
875 | g_free(conn_id_scpi); | |
876 | } | |
877 | ||
878 | return sdi->connection_id; | |
879 | } | |
880 | ||
881 | /** | |
882 | * Queries a device instances' channel list. | |
883 | * | |
884 | * @param sdi Device instance to use. Must not be NULL. | |
885 | * | |
886 | * @return The GSList of channels or NULL. | |
887 | */ | |
888 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) | |
889 | { | |
890 | if (!sdi) | |
891 | return NULL; | |
892 | ||
893 | return sdi->channels; | |
894 | } | |
895 | ||
896 | /** | |
897 | * Queries a device instances' channel groups list. | |
898 | * | |
899 | * @param sdi Device instance to use. Must not be NULL. | |
900 | * | |
901 | * @return The GSList of channel groups or NULL. | |
902 | */ | |
903 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) | |
904 | { | |
905 | if (!sdi) | |
906 | return NULL; | |
907 | ||
908 | return sdi->channel_groups; | |
909 | } | |
910 | ||
911 | /** @} */ |