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