]> sigrok.org Git - libsigrok.git/blame_incremental - device.c
manson-hcs-3xxx: Use maximum voltage and current read from device.
[libsigrok.git] / device.c
... / ...
CommitLineData
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 */
53SR_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 */
88SR_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 */
127SR_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 * Determine whether the specified device instance has the specified
162 * capability.
163 *
164 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
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).
168 * @param[in] key The option that should be checked for is supported by the
169 * specified device.
170 *
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.
174 *
175 * @since 0.2.0
176 */
177SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
178{
179 GVariant *gvar;
180 const int *devopts;
181 gsize num_opts, i;
182 int ret;
183
184 if (!sdi || !sdi->driver || !sdi->driver->config_list)
185 return FALSE;
186
187 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
188 &gvar, sdi, NULL) != SR_OK)
189 return FALSE;
190
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 }
198 }
199 g_variant_unref(gvar);
200
201 return ret;
202}
203
204/** @private
205 * Allocate and init new device instance struct.
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
211 *
212 * @retval NULL Error
213 * @retval struct sr_dev_inst *. Dynamically allocated, free using
214 * sr_dev_inst_free().
215 */
216SR_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)))) {
222 sr_err("Device instance malloc failed.");
223 return NULL;
224 }
225
226 sdi->driver = NULL;
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;
233 sdi->channels = NULL;
234 sdi->channel_groups = NULL;
235 sdi->conn = NULL;
236 sdi->priv = NULL;
237
238 return sdi;
239}
240
241/** @private
242 * Free device instance struct created by sr_dev_inst().
243 * @param sdi struct* to free.
244 */
245SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
246{
247 struct sr_channel *ch;
248 GSList *l;
249
250 for (l = sdi->channels; l; l = l->next) {
251 ch = l->data;
252 g_free(ch->name);
253 g_free(ch);
254 }
255 g_slist_free(sdi->channels);
256
257 if (sdi->channel_groups)
258 g_slist_free(sdi->channel_groups);
259
260 g_free(sdi->vendor);
261 g_free(sdi->model);
262 g_free(sdi->version);
263 g_free(sdi);
264}
265
266#ifdef HAVE_LIBUSB_1_0
267
268/** @private
269 * Allocate and init struct for USB device instance.
270 * @param[in] bus @copydoc sr_usb_dev_inst::bus
271 * @param[in] address @copydoc sr_usb_dev_inst::address
272 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
273 *
274 * @retval NULL Error
275 * @retval other struct sr_usb_dev_inst * for USB device instance.
276 */
277SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
278 uint8_t address, struct libusb_device_handle *hdl)
279{
280 struct sr_usb_dev_inst *udi;
281
282 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
283 sr_err("USB device instance malloc failed.");
284 return NULL;
285 }
286
287 udi->bus = bus;
288 udi->address = address;
289 udi->devhdl = hdl;
290
291 return udi;
292}
293
294/** @private
295 * Free struct * allocated by sr_usb_dev_inst().
296 * @param usb struct* to free. Must not be NULL.
297 */
298SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
299{
300 g_free(usb);
301}
302
303#endif
304
305#ifdef HAVE_LIBSERIALPORT
306
307/**
308 * @private
309 *
310 * Both parameters are copied to newly allocated strings, and freed
311 * automatically by sr_serial_dev_inst_free().
312 *
313 * @param[in] port OS-specific serial port specification. Examples:
314 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
315 * @param[in] serialcomm A serial communication parameters string, in the form
316 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
317 * "9600/8n1" or "600/7o2". This is an optional parameter;
318 * it may be filled in later.
319 *
320 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
321 * or NULL on error.
322 */
323SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
324 const char *serialcomm)
325{
326 struct sr_serial_dev_inst *serial;
327
328 if (!port) {
329 sr_err("Serial port required.");
330 return NULL;
331 }
332
333 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
334 sr_err("Serial device instance malloc failed.");
335 return NULL;
336 }
337
338 serial->port = g_strdup(port);
339 if (serialcomm)
340 serial->serialcomm = g_strdup(serialcomm);
341
342 return serial;
343}
344
345/** @private
346 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
347 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
348 */
349SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
350{
351 g_free(serial->port);
352 g_free(serial->serialcomm);
353 g_free(serial);
354}
355#endif
356
357/** @private */
358SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
359{
360 struct sr_usbtmc_dev_inst *usbtmc;
361
362 if (!device) {
363 sr_err("Device name required.");
364 return NULL;
365 }
366
367 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
368 sr_err("USBTMC device instance malloc failed.");
369 return NULL;
370 }
371
372 usbtmc->device = g_strdup(device);
373 usbtmc->fd = -1;
374
375 return usbtmc;
376}
377
378/** @private */
379SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
380{
381 g_free(usbtmc->device);
382 g_free(usbtmc);
383}
384
385/**
386 * Get the list of devices/instances of the specified driver.
387 *
388 * @param driver The driver to use. Must not be NULL.
389 *
390 * @return The list of devices/instances of this driver, or NULL upon errors
391 * or if the list is empty.
392 *
393 * @since 0.2.0
394 */
395SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
396{
397 if (driver && driver->dev_list)
398 return driver->dev_list();
399 else
400 return NULL;
401}
402
403/**
404 * Clear the list of device instances a driver knows about.
405 *
406 * @param driver The driver to use. This must be a pointer to one of
407 * the entries returned by sr_driver_list(). Must not be NULL.
408 *
409 * @retval SR_OK Success
410 * @retval SR_ERR_ARG Invalid driver
411 *
412 * @since 0.2.0
413 */
414SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
415{
416 int ret;
417
418 if (!driver) {
419 sr_err("Invalid driver.");
420 return SR_ERR_ARG;
421 }
422
423 if (driver->dev_clear)
424 ret = driver->dev_clear();
425 else
426 ret = std_dev_clear(driver, NULL);
427
428 return ret;
429}
430
431/**
432 * Open the specified device.
433 *
434 * @param sdi Device instance to use. Must not be NULL.
435 *
436 * @return SR_OK upon success, a negative error code upon errors.
437 *
438 * @since 0.2.0
439 */
440SR_API int sr_dev_open(struct sr_dev_inst *sdi)
441{
442 int ret;
443
444 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
445 return SR_ERR;
446
447 ret = sdi->driver->dev_open(sdi);
448
449 return ret;
450}
451
452/**
453 * Close the specified device.
454 *
455 * @param sdi Device instance to use. Must not be NULL.
456 *
457 * @return SR_OK upon success, a negative error code upon errors.
458 *
459 * @since 0.2.0
460 */
461SR_API int sr_dev_close(struct sr_dev_inst *sdi)
462{
463 int ret;
464
465 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
466 return SR_ERR;
467
468 ret = sdi->driver->dev_close(sdi);
469
470 return ret;
471}
472
473/** @} */