]> sigrok.org Git - libsigrok.git/blob - device.c
Add/update Doxygen @since tags.
[libsigrok.git] / device.c
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 <stdio.h>
21 #include <glib.h>
22 #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
23 #include "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 /** @private
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
50  *
51  *  @return NULL (failure) or new struct sr_channel*.
52  */
53 SR_PRIV struct sr_channel *sr_channel_new(int index, int type,
54                 gboolean enabled, const char *name)
55 {
56         struct sr_channel *ch;
57
58         if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) {
59                 sr_err("Channel malloc failed.");
60                 return NULL;
61         }
62
63         ch->index = index;
64         ch->type = type;
65         ch->enabled = enabled;
66         if (name)
67                 ch->name = g_strdup(name);
68
69         return ch;
70 }
71
72 /**
73  * Set the name of the specified channel in the specified device.
74  *
75  * If the channel already has a different name assigned to it, it will be
76  * removed, and the new name will be saved instead.
77  *
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
82  *             of the string is made.
83  *
84  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
85  *
86  * @since 0.3.0
87  */
88 SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
89                 int channelnum, const char *name)
90 {
91         GSList *l;
92         struct sr_channel *ch;
93         int ret;
94
95         if (!sdi) {
96                 sr_err("%s: sdi was NULL", __func__);
97                 return SR_ERR_ARG;
98         }
99
100         ret = SR_ERR_ARG;
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);
106                         ret = SR_OK;
107                         break;
108                 }
109         }
110
111         return ret;
112 }
113
114 /**
115  * Enable or disable a channel on the specified device.
116  *
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.
120  *
121  * @return SR_OK on success or SR_ERR on failure.  In case of invalid
122  *         arguments, SR_ERR_ARG is returned and the channel enabled state
123  *         remains unchanged.
124  *
125  * @since 0.3.0
126  */
127 SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
128                 gboolean state)
129 {
130         GSList *l;
131         struct sr_channel *ch;
132         int ret;
133         gboolean was_enabled;
134
135         if (!sdi)
136                 return SR_ERR_ARG;
137
138         ret = SR_ERR_ARG;
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;
144                         ret = SR_OK;
145                         if (!state != !was_enabled && sdi->driver
146                                         && sdi->driver->config_channel_set) {
147                                 ret = sdi->driver->config_channel_set(
148                                         sdi, ch, SR_CHANNEL_SET_ENABLED);
149                                 /* Roll back change if it wasn't applicable. */
150                                 if (ret == SR_ERR_ARG)
151                                         ch->enabled = was_enabled;
152                         }
153                         break;
154                 }
155         }
156
157         return ret;
158 }
159
160 /**
161  * Add a trigger to the specified device (and the specified channel).
162  *
163  * If the specified channel of this device already has a trigger, it will
164  * be silently replaced.
165  *
166  * @param[in,out] sdi Pointer to the device instance; must not be NULL.
167  * @param[in] channelnum Number of channel, starting at 0.
168  * @param[in] trigger Trigger string, in the format used by sigrok-cli
169  *
170  * @return SR_OK on success or SR_ERR on failure.  In case of invalid
171  *         arguments, SR_ERR_ARG is returned and the trigger settings
172  *         remain unchanged.
173  *
174  * @since 0.2.0
175  */
176 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int channelnum,
177                 const char *trigger)
178 {
179         GSList *l;
180         struct sr_channel *ch;
181         char *old_trigger;
182         int ret;
183
184         if (!sdi)
185                 return SR_ERR_ARG;
186
187         ret = SR_ERR_ARG;
188         for (l = sdi->channels; l; l = l->next) {
189                 ch = l->data;
190                 if (ch->index == channelnum) {
191                         old_trigger = ch->trigger;
192                         ret = SR_OK;
193                         if (g_strcmp0(trigger, old_trigger) == 0)
194                                 break;
195                         /* Set new trigger if it has changed. */
196                         ch->trigger = g_strdup(trigger);
197
198                         if (sdi->driver && sdi->driver->config_channel_set) {
199                                 ret = sdi->driver->config_channel_set(
200                                         sdi, ch, SR_CHANNEL_SET_TRIGGER);
201                                 /* Roll back change if it wasn't applicable. */
202                                 if (ret == SR_ERR_ARG) {
203                                         g_free(ch->trigger);
204                                         ch->trigger = old_trigger;
205                                         break;
206                                 }
207                         }
208                         g_free(old_trigger);
209                         break;
210                 }
211         }
212
213         return ret;
214 }
215
216 /**
217  * Determine whether the specified device instance has the specified
218  * capability.
219  *
220  * @param sdi Pointer to the device instance to be checked. Must not be NULL.
221  *            If the device's 'driver' field is NULL (virtual device), this
222  *            function will always return FALSE (virtual devices don't have
223  *            a hardware capabilities list).
224  * @param[in] key The option that should be checked for is supported by the
225  *            specified device.
226  *
227  * @retval TRUE Device has the specified option
228  * @retval FALSE Device does not have the specified option, invalid input
229  *         parameters or other error conditions.
230  *
231  * @since 0.2.0
232  */
233 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
234 {
235         GVariant *gvar;
236         const int *devopts;
237         gsize num_opts, i;
238         int ret;
239
240         if (!sdi || !sdi->driver || !sdi->driver->config_list)
241                 return FALSE;
242
243         if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
244                                 &gvar, sdi, NULL) != SR_OK)
245                 return FALSE;
246
247         ret = FALSE;
248         devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
249         for (i = 0; i < num_opts; i++) {
250                 if (devopts[i] == key) {
251                         ret = TRUE;
252                         break;
253                 }
254         }
255         g_variant_unref(gvar);
256
257         return ret;
258 }
259
260 /** @private
261  *  Allocate and init new device instance struct.
262  *  @param[in]  index   @copydoc sr_dev_inst::index
263  *  @param[in]  status  @copydoc sr_dev_inst::status
264  *  @param[in]  vendor  @copydoc sr_dev_inst::vendor
265  *  @param[in]  model   @copydoc sr_dev_inst::model
266  *  @param[in]  version @copydoc sr_dev_inst::version
267  *
268  *  @retval NULL Error
269  *  @retval struct sr_dev_inst *. Dynamically allocated, free using
270  *              sr_dev_inst_free().
271  */
272 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
273                 const char *vendor, const char *model, const char *version)
274 {
275         struct sr_dev_inst *sdi;
276
277         if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
278                 sr_err("Device instance malloc failed.");
279                 return NULL;
280         }
281
282         sdi->driver = NULL;
283         sdi->index = index;
284         sdi->status = status;
285         sdi->inst_type = -1;
286         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
287         sdi->model = model ? g_strdup(model) : NULL;
288         sdi->version = version ? g_strdup(version) : NULL;
289         sdi->channels = NULL;
290         sdi->channel_groups = NULL;
291         sdi->conn = NULL;
292         sdi->priv = NULL;
293
294         return sdi;
295 }
296
297 /** @private
298  *  Free device instance struct created by sr_dev_inst().
299  *  @param sdi  struct* to free.
300  */
301 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
302 {
303         struct sr_channel *ch;
304         GSList *l;
305
306         for (l = sdi->channels; l; l = l->next) {
307                 ch = l->data;
308                 g_free(ch->name);
309                 g_free(ch->trigger);
310                 g_free(ch);
311         }
312         g_slist_free(sdi->channels);
313
314         if (sdi->channel_groups)
315                 g_slist_free(sdi->channel_groups);
316
317         g_free(sdi->vendor);
318         g_free(sdi->model);
319         g_free(sdi->version);
320         g_free(sdi);
321 }
322
323 #ifdef HAVE_LIBUSB_1_0
324
325 /** @private
326  *  Allocate and init struct for USB device instance.
327  *  @param[in]  bus @copydoc sr_usb_dev_inst::bus
328  *  @param[in]  address @copydoc sr_usb_dev_inst::address
329  *  @param[in]  hdl @copydoc sr_usb_dev_inst::devhdl
330  *
331  *  @retval NULL Error
332  *  @retval other struct sr_usb_dev_inst * for USB device instance.
333  */
334 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
335                         uint8_t address, struct libusb_device_handle *hdl)
336 {
337         struct sr_usb_dev_inst *udi;
338
339         if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
340                 sr_err("USB device instance malloc failed.");
341                 return NULL;
342         }
343
344         udi->bus = bus;
345         udi->address = address;
346         udi->devhdl = hdl;
347
348         return udi;
349 }
350
351 /** @private
352  *  Free struct * allocated by sr_usb_dev_inst().
353  *  @param usb  struct* to free. Must not be NULL.
354  */
355 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
356 {
357         g_free(usb);
358 }
359
360 #endif
361
362 #ifdef HAVE_LIBSERIALPORT
363
364 /**
365  * @private
366  *
367  * Both parameters are copied to newly allocated strings, and freed
368  * automatically by sr_serial_dev_inst_free().
369  *
370  * @param[in] port OS-specific serial port specification. Examples:
371  *                 "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
372  * @param[in] serialcomm A serial communication parameters string, in the form
373  *              of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
374  *              "9600/8n1" or "600/7o2". This is an optional parameter;
375  *              it may be filled in later.
376  *
377  * @return A pointer to a newly initialized struct sr_serial_dev_inst,
378  *         or NULL on error.
379  */
380 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
381                 const char *serialcomm)
382 {
383         struct sr_serial_dev_inst *serial;
384
385         if (!port) {
386                 sr_err("Serial port required.");
387                 return NULL;
388         }
389
390         if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
391                 sr_err("Serial device instance malloc failed.");
392                 return NULL;
393         }
394
395         serial->port = g_strdup(port);
396         if (serialcomm)
397                 serial->serialcomm = g_strdup(serialcomm);
398
399         return serial;
400 }
401
402 /** @private
403  *  Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
404  *  @param serial   struct sr_serial_dev_inst * to free. Must not be NULL.
405  */
406 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
407 {
408         g_free(serial->port);
409         g_free(serial->serialcomm);
410         g_free(serial);
411 }
412 #endif
413
414 /** @private */
415 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
416 {
417         struct sr_usbtmc_dev_inst *usbtmc;
418
419         if (!device) {
420                 sr_err("Device name required.");
421                 return NULL;
422         }
423
424         if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
425                 sr_err("USBTMC device instance malloc failed.");
426                 return NULL;
427         }
428
429         usbtmc->device = g_strdup(device);
430         usbtmc->fd = -1;
431
432         return usbtmc;
433 }
434
435 /** @private */
436 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
437 {
438         g_free(usbtmc->device);
439         g_free(usbtmc);
440 }
441
442 /**
443  * Get the list of devices/instances of the specified driver.
444  *
445  * @param driver The driver to use. Must not be NULL.
446  *
447  * @return The list of devices/instances of this driver, or NULL upon errors
448  *         or if the list is empty.
449  *
450  * @since 0.2.0
451  */
452 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
453 {
454         if (driver && driver->dev_list)
455                 return driver->dev_list();
456         else
457                 return NULL;
458 }
459
460 /**
461  * Clear the list of device instances a driver knows about.
462  *
463  * @param driver The driver to use. This must be a pointer to one of
464  *               the entries returned by sr_driver_list(). Must not be NULL.
465  *
466  * @retval SR_OK Success
467  * @retval SR_ERR_ARG Invalid driver
468  *
469  * @since 0.2.0
470  */
471 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
472 {
473         int ret;
474
475         if (!driver) {
476                 sr_err("Invalid driver.");
477                 return SR_ERR_ARG;
478         }
479
480         if (driver->dev_clear)
481                 ret = driver->dev_clear();
482         else
483                 ret = std_dev_clear(driver, NULL);
484
485         return ret;
486 }
487
488 /**
489  * Open the specified device.
490  *
491  * @param sdi Device instance to use. Must not be NULL.
492  *
493  * @return SR_OK upon success, a negative error code upon errors.
494  *
495  * @since 0.2.0
496  */
497 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
498 {
499         int ret;
500
501         if (!sdi || !sdi->driver || !sdi->driver->dev_open)
502                 return SR_ERR;
503
504         ret = sdi->driver->dev_open(sdi);
505
506         return ret;
507 }
508
509 /**
510  * Close the specified device.
511  *
512  * @param sdi Device instance to use. Must not be NULL.
513  *
514  * @return SR_OK upon success, a negative error code upon errors.
515  *
516  * @since 0.2.0
517  */
518 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
519 {
520         int ret;
521
522         if (!sdi || !sdi->driver || !sdi->driver->dev_close)
523                 return SR_ERR;
524
525         ret = sdi->driver->dev_close(sdi);
526
527         return ret;
528 }
529
530 /** @} */