]> sigrok.org Git - libsigrok.git/blame_incremental - device.c
libsigrok-internal.h: Fix libserialport.h name.
[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/* Message logging helpers with subsystem-specific prefix string. */
27#define LOG_PREFIX "device: "
28#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
29#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
30#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
31#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
32#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
33#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
34
35/**
36 * @file
37 *
38 * Device handling in libsigrok.
39 */
40
41/**
42 * @defgroup grp_devices Devices
43 *
44 * Device handling in libsigrok.
45 *
46 * @{
47 */
48
49/** @private */
50SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
51 gboolean enabled, const char *name)
52{
53 struct sr_probe *probe;
54
55 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
56 sr_err("Probe malloc failed.");
57 return NULL;
58 }
59
60 probe->index = index;
61 probe->type = type;
62 probe->enabled = enabled;
63 if (name)
64 probe->name = g_strdup(name);
65
66 return probe;
67}
68
69/**
70 * Set the name of the specified probe in the specified device.
71 *
72 * If the probe already has a different name assigned to it, it will be
73 * removed, and the new name will be saved instead.
74 *
75 * @param sdi The device instance the probe is connected to.
76 * @param probenum The number of the probe whose name to set.
77 * Note that the probe numbers start at 0.
78 * @param name The new name that the specified probe should get. A copy
79 * of the string is made.
80 *
81 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
82 *
83 * @since 0.2.0
84 */
85SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
86 int probenum, const char *name)
87{
88 GSList *l;
89 struct sr_probe *probe;
90 int ret;
91
92 if (!sdi) {
93 sr_err("%s: sdi was NULL", __func__);
94 return SR_ERR_ARG;
95 }
96
97 ret = SR_ERR_ARG;
98 for (l = sdi->probes; l; l = l->next) {
99 probe = l->data;
100 if (probe->index == probenum) {
101 g_free(probe->name);
102 probe->name = g_strdup(name);
103 ret = SR_OK;
104 break;
105 }
106 }
107
108 return ret;
109}
110
111/**
112 * Enable or disable a probe on the specified device.
113 *
114 * @param sdi The device instance the probe is connected to.
115 * @param probenum The probe number, starting from 0.
116 * @param state TRUE to enable the probe, FALSE to disable.
117 *
118 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
119 *
120 * @since 0.2.0
121 */
122SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
123 gboolean state)
124{
125 GSList *l;
126 struct sr_probe *probe;
127 int ret;
128
129 if (!sdi)
130 return SR_ERR_ARG;
131
132 ret = SR_ERR_ARG;
133 for (l = sdi->probes; l; l = l->next) {
134 probe = l->data;
135 if (probe->index == probenum) {
136 probe->enabled = state;
137 ret = SR_OK;
138 break;
139 }
140 }
141
142 return ret;
143}
144
145/**
146 * Add a trigger to the specified device (and the specified probe).
147 *
148 * If the specified probe of this device already has a trigger, it will
149 * be silently replaced.
150 *
151 * @param sdi Must not be NULL.
152 * @param probenum The probe number, starting from 0.
153 * @param trigger Trigger string, in the format used by sigrok-cli
154 *
155 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
156 *
157 * @since 0.2.0
158 */
159SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
160 const char *trigger)
161{
162 GSList *l;
163 struct sr_probe *probe;
164 int ret;
165
166 if (!sdi)
167 return SR_ERR_ARG;
168
169 ret = SR_ERR_ARG;
170 for (l = sdi->probes; l; l = l->next) {
171 probe = l->data;
172 if (probe->index == probenum) {
173 /* If the probe already has a trigger, kill it first. */
174 g_free(probe->trigger);
175 probe->trigger = g_strdup(trigger);
176 ret = SR_OK;
177 break;
178 }
179 }
180
181 return ret;
182}
183
184/**
185 * Determine whether the specified device instance has the specified
186 * capability.
187 *
188 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
189 * If the device's 'driver' field is NULL (virtual device), this
190 * function will always return FALSE (virtual devices don't have
191 * a hardware capabilities list).
192 * @param key The option that should be checked for support on the
193 * specified device.
194 *
195 * @return TRUE if the device has the specified option, FALSE otherwise.
196 * FALSE is also returned on invalid input parameters or other
197 * error conditions.
198 *
199 * @since 0.2.0
200 */
201SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
202{
203 GVariant *gvar;
204 const int *devopts;
205 gsize num_opts, i;
206 int ret;
207
208 if (!sdi || !sdi->driver || !sdi->driver->config_list)
209 return FALSE;
210
211 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
212 &gvar, NULL, NULL) != SR_OK)
213 return FALSE;
214
215 ret = FALSE;
216 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
217 for (i = 0; i < num_opts; i++) {
218 if (devopts[i] == key) {
219 ret = TRUE;
220 break;
221 }
222 }
223 g_variant_unref(gvar);
224
225 return ret;
226}
227
228/** @private */
229SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
230 const char *vendor, const char *model, const char *version)
231{
232 struct sr_dev_inst *sdi;
233
234 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
235 sr_err("Device instance malloc failed.");
236 return NULL;
237 }
238
239 sdi->driver = NULL;
240 sdi->index = index;
241 sdi->status = status;
242 sdi->inst_type = -1;
243 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
244 sdi->model = model ? g_strdup(model) : NULL;
245 sdi->version = version ? g_strdup(version) : NULL;
246 sdi->probes = NULL;
247 sdi->probe_groups = NULL;
248 sdi->conn = NULL;
249 sdi->priv = NULL;
250
251 return sdi;
252}
253
254/** @private */
255SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
256{
257 struct sr_probe *probe;
258 GSList *l;
259
260 for (l = sdi->probes; l; l = l->next) {
261 probe = l->data;
262 g_free(probe->name);
263 g_free(probe->trigger);
264 g_free(probe);
265 }
266 g_slist_free(sdi->probes);
267
268 g_free(sdi->vendor);
269 g_free(sdi->model);
270 g_free(sdi->version);
271 g_free(sdi);
272}
273
274#ifdef HAVE_LIBUSB_1_0
275
276/** @private */
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 */
295SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
296{
297 g_free(usb);
298}
299
300#endif
301
302#ifdef HAVE_LIBSERIALPORT
303
304/**
305 * @private
306 *
307 * Both parameters are copied to newly allocated strings, and freed
308 * automatically by sr_serial_dev_inst_free().
309 *
310 * @param pathname OS-specific serial port specification. Examples:
311 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
312 * @param serialcomm A serial communication parameters string, in the form
313 * of <speed>/<data bits><parity><stopbits>, for example
314 * "9600/8n1" or "600/7o2". This is an optional parameter;
315 * it may be filled in later.
316 *
317 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
318 * or NULL on error.
319 */
320SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
321 const char *serialcomm)
322{
323 struct sr_serial_dev_inst *serial;
324
325 if (!port) {
326 sr_err("Serial port required.");
327 return NULL;
328 }
329
330 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
331 sr_err("Serial device instance malloc failed.");
332 return NULL;
333 }
334
335 serial->port = g_strdup(port);
336 if (serialcomm)
337 serial->serialcomm = g_strdup(serialcomm);
338 serial->fd = -1;
339
340 return serial;
341}
342
343/** @private */
344SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
345{
346 g_free(serial->port);
347 g_free(serial->serialcomm);
348 g_free(serial);
349}
350
351#endif
352
353/**
354 * Get the list of devices/instances of the specified driver.
355 *
356 * @param driver The driver to use. Must not be NULL.
357 *
358 * @return The list of devices/instances of this driver, or NULL upon errors
359 * or if the list is empty.
360 *
361 * @since 0.2.0
362 */
363SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
364{
365 if (driver && driver->dev_list)
366 return driver->dev_list();
367 else
368 return NULL;
369}
370
371/**
372 * Clear all devices/instances of the specified driver.
373 *
374 * @param driver The driver to use. Must not be NULL.
375 *
376 * @return SR_OK upon success, a negative error code upon errors.
377 *
378 * @since 0.2.0
379 */
380SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
381{
382 if (driver && driver->dev_clear)
383 return driver->dev_clear();
384 else
385 return SR_OK;
386}
387
388/**
389 * Open the specified device.
390 *
391 * @param sdi Device instance to use. Must not be NULL.
392 *
393 * @return SR_OK upon success, a negative error code upon errors.
394 *
395 * @since 0.2.0
396 */
397SR_API int sr_dev_open(struct sr_dev_inst *sdi)
398{
399 int ret;
400
401 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
402 return SR_ERR;
403
404 ret = sdi->driver->dev_open(sdi);
405
406 return ret;
407}
408
409/**
410 * Close the specified device.
411 *
412 * @param sdi Device instance to use. Must not be NULL.
413 *
414 * @return SR_OK upon success, a negative error code upon errors.
415 *
416 * @since 0.2.0
417 */
418SR_API int sr_dev_close(struct sr_dev_inst *sdi)
419{
420 int ret;
421
422 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
423 return SR_ERR;
424
425 ret = sdi->driver->dev_close(sdi);
426
427 return ret;
428}
429
430/** @} */