]>
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 | ||
20 | #include <stdio.h> | |
21 | #include <glib.h> | |
545f9786 | 22 | #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */ |
45c59c8b BV |
23 | #include "libsigrok.h" |
24 | #include "libsigrok-internal.h" | |
a1bb33af | 25 | |
2ad1deb8 | 26 | /** @cond PRIVATE */ |
3544f848 | 27 | #define LOG_PREFIX "device" |
2ad1deb8 | 28 | /** @endcond */ |
a885ce3e | 29 | |
393fb9cb UH |
30 | /** |
31 | * @file | |
32 | * | |
33 | * Device handling in libsigrok. | |
34 | */ | |
35 | ||
7b870c38 UH |
36 | /** |
37 | * @defgroup grp_devices Devices | |
38 | * | |
39 | * Device handling in libsigrok. | |
40 | * | |
41 | * @{ | |
42 | */ | |
43 | ||
04cb9157 | 44 | /** @private |
91aea754 UH |
45 | * Allocate and initialize new struct sr_channel |
46 | * @param[in] index @copydoc sr_channel::index | |
47 | * @param[in] type @copydoc sr_channel::type | |
48 | * @param[in] enabled @copydoc sr_channel::enabled | |
49 | * @param[in] name @copydoc sr_channel::name | |
04cb9157 | 50 | * |
91aea754 | 51 | * @return NULL (failure) or new struct sr_channel*. |
04cb9157 | 52 | */ |
56d0d245 | 53 | SR_PRIV struct sr_channel *sr_channel_new(int index, int type, |
48a486cd BV |
54 | gboolean enabled, const char *name) |
55 | { | |
ba7dd8bb | 56 | struct sr_channel *ch; |
48a486cd | 57 | |
ba7dd8bb UH |
58 | if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) { |
59 | sr_err("Channel malloc failed."); | |
48a486cd BV |
60 | return NULL; |
61 | } | |
62 | ||
ba7dd8bb UH |
63 | ch->index = index; |
64 | ch->type = type; | |
65 | ch->enabled = enabled; | |
48a486cd | 66 | if (name) |
ba7dd8bb | 67 | ch->name = g_strdup(name); |
48a486cd | 68 | |
ba7dd8bb | 69 | return ch; |
48a486cd BV |
70 | } |
71 | ||
94799bc4 | 72 | /** |
ba7dd8bb | 73 | * Set the name of the specified channel in the specified device. |
94799bc4 | 74 | * |
ba7dd8bb | 75 | * If the channel already has a different name assigned to it, it will be |
94799bc4 UH |
76 | * removed, and the new name will be saved instead. |
77 | * | |
ba7dd8bb UH |
78 | * @param sdi The device instance the channel is connected to. |
79 | * @param[in] channelnum The number of the channel whose name to set. | |
80 | * Note that the channel numbers start at 0. | |
81 | * @param[in] name The new name that the specified channel should get. A copy | |
37e8b4c4 | 82 | * of the string is made. |
0e3b1439 | 83 | * |
37e8b4c4 | 84 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. |
9fb5f2df | 85 | * |
47117241 | 86 | * @since 0.3.0 |
94799bc4 | 87 | */ |
f3ca73ed | 88 | SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi, |
ba7dd8bb | 89 | int channelnum, const char *name) |
a1bb33af | 90 | { |
37e8b4c4 | 91 | GSList *l; |
ba7dd8bb | 92 | struct sr_channel *ch; |
37e8b4c4 | 93 | int ret; |
a1bb33af | 94 | |
37e8b4c4 BV |
95 | if (!sdi) { |
96 | sr_err("%s: sdi was NULL", __func__); | |
0e3b1439 | 97 | return SR_ERR_ARG; |
94799bc4 UH |
98 | } |
99 | ||
37e8b4c4 | 100 | ret = SR_ERR_ARG; |
ba7dd8bb UH |
101 | for (l = sdi->channels; l; l = l->next) { |
102 | ch = l->data; | |
103 | if (ch->index == channelnum) { | |
104 | g_free(ch->name); | |
105 | ch->name = g_strdup(name); | |
37e8b4c4 BV |
106 | ret = SR_OK; |
107 | break; | |
108 | } | |
94799bc4 UH |
109 | } |
110 | ||
37e8b4c4 | 111 | return ret; |
a1bb33af UH |
112 | } |
113 | ||
be5bf44d | 114 | /** |
ba7dd8bb | 115 | * Enable or disable a channel on the specified device. |
be5bf44d | 116 | * |
ba7dd8bb UH |
117 | * @param sdi The device instance the channel is connected to. |
118 | * @param channelnum The channel number, starting from 0. | |
119 | * @param state TRUE to enable the channel, FALSE to disable. | |
be5bf44d | 120 | * |
2a854d71 | 121 | * @return SR_OK on success or SR_ERR on failure. In case of invalid |
ba7dd8bb | 122 | * arguments, SR_ERR_ARG is returned and the channel enabled state |
2a854d71 | 123 | * remains unchanged. |
9fb5f2df | 124 | * |
47117241 | 125 | * @since 0.3.0 |
be5bf44d | 126 | */ |
f3ca73ed | 127 | SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum, |
be5bf44d BV |
128 | gboolean state) |
129 | { | |
130 | GSList *l; | |
ba7dd8bb | 131 | struct sr_channel *ch; |
be5bf44d | 132 | int ret; |
2a854d71 | 133 | gboolean was_enabled; |
be5bf44d BV |
134 | |
135 | if (!sdi) | |
136 | return SR_ERR_ARG; | |
137 | ||
138 | ret = SR_ERR_ARG; | |
ba7dd8bb UH |
139 | for (l = sdi->channels; l; l = l->next) { |
140 | ch = l->data; | |
141 | if (ch->index == channelnum) { | |
142 | was_enabled = ch->enabled; | |
143 | ch->enabled = state; | |
be5bf44d | 144 | ret = SR_OK; |
2a854d71 | 145 | if (!state != !was_enabled && sdi->driver |
f3ca73ed UH |
146 | && sdi->driver->config_channel_set) { |
147 | ret = sdi->driver->config_channel_set( | |
3f239f08 | 148 | sdi, ch, SR_CHANNEL_SET_ENABLED); |
2a854d71 DE |
149 | /* Roll back change if it wasn't applicable. */ |
150 | if (ret == SR_ERR_ARG) | |
ba7dd8bb | 151 | ch->enabled = was_enabled; |
2a854d71 | 152 | } |
be5bf44d BV |
153 | break; |
154 | } | |
155 | } | |
156 | ||
157 | return ret; | |
158 | } | |
159 | ||
94799bc4 | 160 | /** |
9c5332d2 UH |
161 | * Determine whether the specified device instance has the specified |
162 | * capability. | |
94799bc4 | 163 | * |
9c5332d2 | 164 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. |
8ec95d22 UH |
165 | * If the device's 'driver' field is NULL (virtual device), this |
166 | * function will always return FALSE (virtual devices don't have | |
167 | * a hardware capabilities list). | |
04cb9157 | 168 | * @param[in] key The option that should be checked for is supported by the |
4d15e5c9 | 169 | * specified device. |
94799bc4 | 170 | * |
04cb9157 MH |
171 | * @retval TRUE Device has the specified option |
172 | * @retval FALSE Device does not have the specified option, invalid input | |
173 | * parameters or other error conditions. | |
9fb5f2df | 174 | * |
53f05fa8 | 175 | * @since 0.2.0 |
94799bc4 | 176 | */ |
4d15e5c9 | 177 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) |
7d658874 | 178 | { |
003595ac | 179 | GVariant *gvar; |
4d15e5c9 | 180 | const int *devopts; |
003595ac BV |
181 | gsize num_opts, i; |
182 | int ret; | |
7d658874 | 183 | |
003595ac | 184 | if (!sdi || !sdi->driver || !sdi->driver->config_list) |
8ec95d22 | 185 | return FALSE; |
94799bc4 | 186 | |
8f996b89 | 187 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, |
92b68bb5 | 188 | &gvar, sdi, NULL) != SR_OK) |
8ec95d22 | 189 | return FALSE; |
94799bc4 | 190 | |
003595ac BV |
191 | ret = FALSE; |
192 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
193 | for (i = 0; i < num_opts; i++) { | |
194 | if (devopts[i] == key) { | |
195 | ret = TRUE; | |
196 | break; | |
197 | } | |
94799bc4 | 198 | } |
003595ac | 199 | g_variant_unref(gvar); |
218557b8 | 200 | |
003595ac | 201 | return ret; |
a1bb33af | 202 | } |
fd9836bf | 203 | |
04cb9157 MH |
204 | /** @private |
205 | * Allocate and init new device instance struct. | |
2eb84c98 UH |
206 | * @param[in] index @copydoc sr_dev_inst::index |
207 | * @param[in] status @copydoc sr_dev_inst::status | |
208 | * @param[in] vendor @copydoc sr_dev_inst::vendor | |
209 | * @param[in] model @copydoc sr_dev_inst::model | |
210 | * @param[in] version @copydoc sr_dev_inst::version | |
04cb9157 | 211 | * |
2eb84c98 UH |
212 | * @retval NULL Error |
213 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
04cb9157 MH |
214 | * sr_dev_inst_free(). |
215 | */ | |
48a486cd BV |
216 | SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status, |
217 | const char *vendor, const char *model, const char *version) | |
218 | { | |
219 | struct sr_dev_inst *sdi; | |
220 | ||
221 | if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) { | |
c4227fc6 | 222 | sr_err("Device instance malloc failed."); |
48a486cd BV |
223 | return NULL; |
224 | } | |
225 | ||
e8d3d6c8 | 226 | sdi->driver = NULL; |
48a486cd BV |
227 | sdi->index = index; |
228 | sdi->status = status; | |
229 | sdi->inst_type = -1; | |
230 | sdi->vendor = vendor ? g_strdup(vendor) : NULL; | |
231 | sdi->model = model ? g_strdup(model) : NULL; | |
232 | sdi->version = version ? g_strdup(version) : NULL; | |
2fe6210a SA |
233 | sdi->serial_num = NULL; |
234 | sdi->connection_id = NULL; | |
ba7dd8bb | 235 | sdi->channels = NULL; |
660e398f | 236 | sdi->channel_groups = NULL; |
0812c40e | 237 | sdi->session = NULL; |
9e2e9864 | 238 | sdi->conn = NULL; |
48a486cd BV |
239 | sdi->priv = NULL; |
240 | ||
241 | return sdi; | |
242 | } | |
243 | ||
04cb9157 MH |
244 | /** @private |
245 | * Free device instance struct created by sr_dev_inst(). | |
2eb84c98 | 246 | * @param sdi struct* to free. |
04cb9157 | 247 | */ |
48a486cd BV |
248 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) |
249 | { | |
ba7dd8bb | 250 | struct sr_channel *ch; |
d3cff734 BV |
251 | GSList *l; |
252 | ||
ba7dd8bb UH |
253 | for (l = sdi->channels; l; l = l->next) { |
254 | ch = l->data; | |
255 | g_free(ch->name); | |
379d2609 | 256 | g_free(ch->priv); |
ba7dd8bb | 257 | g_free(ch); |
d3cff734 | 258 | } |
ba7dd8bb | 259 | g_slist_free(sdi->channels); |
d3cff734 | 260 | |
660e398f UH |
261 | if (sdi->channel_groups) |
262 | g_slist_free(sdi->channel_groups); | |
90c7f4e9 | 263 | |
48a486cd BV |
264 | g_free(sdi->vendor); |
265 | g_free(sdi->model); | |
266 | g_free(sdi->version); | |
2fe6210a SA |
267 | g_free(sdi->serial_num); |
268 | g_free(sdi->connection_id); | |
48a486cd BV |
269 | g_free(sdi); |
270 | } | |
271 | ||
272 | #ifdef HAVE_LIBUSB_1_0 | |
273 | ||
04cb9157 MH |
274 | /** @private |
275 | * Allocate and init struct for USB device instance. | |
2eb84c98 UH |
276 | * @param[in] bus @copydoc sr_usb_dev_inst::bus |
277 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
278 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
04cb9157 | 279 | * |
2eb84c98 UH |
280 | * @retval NULL Error |
281 | * @retval other struct sr_usb_dev_inst * for USB device instance. | |
04cb9157 | 282 | */ |
48a486cd BV |
283 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, |
284 | uint8_t address, struct libusb_device_handle *hdl) | |
285 | { | |
286 | struct sr_usb_dev_inst *udi; | |
287 | ||
288 | if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { | |
c4227fc6 | 289 | sr_err("USB device instance malloc failed."); |
48a486cd BV |
290 | return NULL; |
291 | } | |
292 | ||
293 | udi->bus = bus; | |
294 | udi->address = address; | |
295 | udi->devhdl = hdl; | |
296 | ||
297 | return udi; | |
298 | } | |
299 | ||
04cb9157 MH |
300 | /** @private |
301 | * Free struct * allocated by sr_usb_dev_inst(). | |
2eb84c98 | 302 | * @param usb struct* to free. Must not be NULL. |
04cb9157 | 303 | */ |
48a486cd BV |
304 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) |
305 | { | |
a006798b | 306 | g_free(usb); |
48a486cd BV |
307 | } |
308 | ||
309 | #endif | |
310 | ||
c4f2dfd0 UH |
311 | #ifdef HAVE_LIBSERIALPORT |
312 | ||
9fb5f2df UH |
313 | /** |
314 | * @private | |
299bdb24 BV |
315 | * |
316 | * Both parameters are copied to newly allocated strings, and freed | |
317 | * automatically by sr_serial_dev_inst_free(). | |
9fb5f2df | 318 | * |
04cb9157 | 319 | * @param[in] port OS-specific serial port specification. Examples: |
9fb5f2df | 320 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". |
04cb9157 MH |
321 | * @param[in] serialcomm A serial communication parameters string, in the form |
322 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
323 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
324 | * it may be filled in later. | |
9fb5f2df UH |
325 | * |
326 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
327 | * or NULL on error. | |
299bdb24 | 328 | */ |
48a486cd | 329 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, |
299bdb24 | 330 | const char *serialcomm) |
48a486cd BV |
331 | { |
332 | struct sr_serial_dev_inst *serial; | |
333 | ||
299bdb24 | 334 | if (!port) { |
c4227fc6 | 335 | sr_err("Serial port required."); |
299bdb24 BV |
336 | return NULL; |
337 | } | |
338 | ||
339 | if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) { | |
c4227fc6 | 340 | sr_err("Serial device instance malloc failed."); |
48a486cd BV |
341 | return NULL; |
342 | } | |
343 | ||
344 | serial->port = g_strdup(port); | |
299bdb24 BV |
345 | if (serialcomm) |
346 | serial->serialcomm = g_strdup(serialcomm); | |
48a486cd BV |
347 | |
348 | return serial; | |
349 | } | |
350 | ||
04cb9157 MH |
351 | /** @private |
352 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
2eb84c98 | 353 | * @param serial struct sr_serial_dev_inst * to free. Must not be NULL. |
04cb9157 | 354 | */ |
48a486cd BV |
355 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) |
356 | { | |
357 | g_free(serial->port); | |
299bdb24 | 358 | g_free(serial->serialcomm); |
acac8fc3 | 359 | g_free(serial); |
48a486cd | 360 | } |
c4f2dfd0 UH |
361 | #endif |
362 | ||
df823ac4 | 363 | /** @private */ |
ae67644f ML |
364 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) |
365 | { | |
366 | struct sr_usbtmc_dev_inst *usbtmc; | |
367 | ||
368 | if (!device) { | |
369 | sr_err("Device name required."); | |
370 | return NULL; | |
371 | } | |
372 | ||
373 | if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) { | |
374 | sr_err("USBTMC device instance malloc failed."); | |
375 | return NULL; | |
376 | } | |
377 | ||
378 | usbtmc->device = g_strdup(device); | |
379 | usbtmc->fd = -1; | |
380 | ||
381 | return usbtmc; | |
382 | } | |
383 | ||
df823ac4 | 384 | /** @private */ |
ae67644f ML |
385 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) |
386 | { | |
387 | g_free(usbtmc->device); | |
388 | g_free(usbtmc); | |
389 | } | |
390 | ||
576ff5b0 UH |
391 | /** |
392 | * Get the list of devices/instances of the specified driver. | |
393 | * | |
394 | * @param driver The driver to use. Must not be NULL. | |
395 | * | |
396 | * @return The list of devices/instances of this driver, or NULL upon errors | |
397 | * or if the list is empty. | |
398 | * | |
53f05fa8 | 399 | * @since 0.2.0 |
576ff5b0 | 400 | */ |
f99e32af | 401 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) |
811deee4 | 402 | { |
811deee4 BV |
403 | if (driver && driver->dev_list) |
404 | return driver->dev_list(); | |
405 | else | |
406 | return NULL; | |
407 | } | |
408 | ||
576ff5b0 | 409 | /** |
8102cee4 | 410 | * Clear the list of device instances a driver knows about. |
576ff5b0 | 411 | * |
8102cee4 BV |
412 | * @param driver The driver to use. This must be a pointer to one of |
413 | * the entries returned by sr_driver_list(). Must not be NULL. | |
576ff5b0 | 414 | * |
8102cee4 BV |
415 | * @retval SR_OK Success |
416 | * @retval SR_ERR_ARG Invalid driver | |
576ff5b0 UH |
417 | * |
418 | * @since 0.2.0 | |
419 | */ | |
f99e32af | 420 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) |
811deee4 | 421 | { |
8102cee4 BV |
422 | int ret; |
423 | ||
424 | if (!driver) { | |
425 | sr_err("Invalid driver."); | |
426 | return SR_ERR_ARG; | |
427 | } | |
428 | ||
429 | if (driver->dev_clear) | |
430 | ret = driver->dev_clear(); | |
811deee4 | 431 | else |
8102cee4 BV |
432 | ret = std_dev_clear(driver, NULL); |
433 | ||
434 | return ret; | |
811deee4 BV |
435 | } |
436 | ||
576ff5b0 UH |
437 | /** |
438 | * Open the specified device. | |
439 | * | |
440 | * @param sdi Device instance to use. Must not be NULL. | |
441 | * | |
442 | * @return SR_OK upon success, a negative error code upon errors. | |
443 | * | |
444 | * @since 0.2.0 | |
445 | */ | |
efdecf4c BV |
446 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) |
447 | { | |
448 | int ret; | |
449 | ||
450 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
451 | return SR_ERR; | |
452 | ||
453 | ret = sdi->driver->dev_open(sdi); | |
454 | ||
455 | return ret; | |
456 | } | |
457 | ||
576ff5b0 UH |
458 | /** |
459 | * Close the specified device. | |
460 | * | |
461 | * @param sdi Device instance to use. Must not be NULL. | |
462 | * | |
463 | * @return SR_OK upon success, a negative error code upon errors. | |
464 | * | |
465 | * @since 0.2.0 | |
466 | */ | |
efdecf4c BV |
467 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) |
468 | { | |
469 | int ret; | |
470 | ||
471 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
472 | return SR_ERR; | |
473 | ||
474 | ret = sdi->driver->dev_close(sdi); | |
475 | ||
476 | return ret; | |
477 | } | |
478 | ||
7b870c38 | 479 | /** @} */ |