]> sigrok.org Git - libsigrok.git/blame_incremental - device.c
Doxygen: Add @file items for the relevant files.
[libsigrok.git] / device.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 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 "libsigrok.h"
23#include "libsigrok-internal.h"
24
25/**
26 * @file
27 *
28 * Device handling in libsigrok.
29 */
30
31/**
32 * @defgroup grp_devices Devices
33 *
34 * Device handling in libsigrok.
35 *
36 * @{
37 */
38
39/** @private */
40SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
41 gboolean enabled, const char *name)
42{
43 struct sr_probe *probe;
44
45 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
46 sr_err("hwdriver: probe malloc failed");
47 return NULL;
48 }
49
50 probe->index = index;
51 probe->type = type;
52 probe->enabled = enabled;
53 if (name)
54 probe->name = g_strdup(name);
55
56 return probe;
57}
58
59/**
60 * Set the name of the specified probe in the specified device.
61 *
62 * If the probe already has a different name assigned to it, it will be
63 * removed, and the new name will be saved instead.
64 *
65 * @param sdi The device instance the probe is connected to.
66 * @param probenum The number of the probe whose name to set.
67 * Note that the probe numbers start at 0.
68 * @param name The new name that the specified probe should get. A copy
69 * of the string is made.
70 *
71 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
72 */
73SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
74 int probenum, const char *name)
75{
76 GSList *l;
77 struct sr_probe *probe;
78 int ret;
79
80 if (!sdi) {
81 sr_err("%s: sdi was NULL", __func__);
82 return SR_ERR_ARG;
83 }
84
85 ret = SR_ERR_ARG;
86 for (l = sdi->probes; l; l = l->next) {
87 probe = l->data;
88 if (probe->index == probenum) {
89 g_free(probe->name);
90 probe->name = g_strdup(name);
91 ret = SR_OK;
92 break;
93 }
94 }
95
96 return ret;
97}
98
99/**
100 * Enable or disable a probe on the specified device.
101 *
102 * @param sdi The device instance the probe is connected to.
103 * @param probenum The probe number, starting from 0.
104 * @param state TRUE to enable the probe, FALSE to disable.
105 *
106 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
107 */
108SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
109 gboolean state)
110{
111 GSList *l;
112 struct sr_probe *probe;
113 int ret;
114
115 if (!sdi)
116 return SR_ERR_ARG;
117
118 ret = SR_ERR_ARG;
119 for (l = sdi->probes; l; l = l->next) {
120 probe = l->data;
121 if (probe->index == probenum) {
122 probe->enabled = state;
123 ret = SR_OK;
124 break;
125 }
126 }
127
128 return ret;
129}
130
131/**
132 * Add a trigger to the specified device (and the specified probe).
133 *
134 * If the specified probe of this device already has a trigger, it will
135 * be silently replaced.
136 *
137 * @param sdi Must not be NULL.
138 * @param probenum The probe number, starting from 0.
139 * @param trigger Trigger string, in the format used by sigrok-cli
140 *
141 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
142 */
143SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
144 const char *trigger)
145{
146 GSList *l;
147 struct sr_probe *probe;
148 int ret;
149
150 if (!sdi)
151 return SR_ERR_ARG;
152
153 ret = SR_ERR_ARG;
154 for (l = sdi->probes; l; l = l->next) {
155 probe = l->data;
156 if (probe->index == probenum) {
157 /* If the probe already has a trigger, kill it first. */
158 g_free(probe->trigger);
159 probe->trigger = g_strdup(trigger);
160 ret = SR_OK;
161 break;
162 }
163 }
164
165 return ret;
166}
167
168/**
169 * Determine whether the specified device instance has the specified
170 * capability.
171 *
172 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
173 * If the device's 'driver' field is NULL (virtual device), this
174 * function will always return FALSE (virtual devices don't have
175 * a hardware capabilities list).
176 * @param hwcap The capability that should be checked (whether it's supported
177 * by the specified device).
178 *
179 * @return TRUE if the device has the specified capability, FALSE otherwise.
180 * FALSE is also returned upon invalid input parameters or other
181 * error conditions.
182 */
183SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
184{
185 const int *hwcaps;
186 int i;
187
188 if (!sdi || !sdi->driver)
189 return FALSE;
190
191 if (sdi->driver->info_get(SR_DI_HWCAPS,
192 (const void **)&hwcaps, NULL) != SR_OK)
193 return FALSE;
194
195 for (i = 0; hwcaps[i]; i++) {
196 if (hwcaps[i] == hwcap)
197 return TRUE;
198 }
199
200 return FALSE;
201}
202
203/** @private */
204SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
205 const char *vendor, const char *model, const char *version)
206{
207 struct sr_dev_inst *sdi;
208
209 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
210 sr_err("hwdriver: %s: sdi malloc failed", __func__);
211 return NULL;
212 }
213
214 sdi->driver = NULL;
215 sdi->index = index;
216 sdi->status = status;
217 sdi->inst_type = -1;
218 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
219 sdi->model = model ? g_strdup(model) : NULL;
220 sdi->version = version ? g_strdup(version) : NULL;
221 sdi->probes = NULL;
222 sdi->priv = NULL;
223
224 return sdi;
225}
226
227/** @private */
228SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
229{
230 struct sr_probe *probe;
231 GSList *l;
232
233 for (l = sdi->probes; l; l = l->next) {
234 probe = l->data;
235 g_free(probe->name);
236 g_free(probe);
237 }
238
239 g_free(sdi->priv);
240 g_free(sdi->vendor);
241 g_free(sdi->model);
242 g_free(sdi->version);
243 g_free(sdi);
244
245}
246
247#ifdef HAVE_LIBUSB_1_0
248
249/** @private */
250SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
251 uint8_t address, struct libusb_device_handle *hdl)
252{
253 struct sr_usb_dev_inst *udi;
254
255 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
256 sr_err("hwdriver: %s: udi malloc failed", __func__);
257 return NULL;
258 }
259
260 udi->bus = bus;
261 udi->address = address;
262 udi->devhdl = hdl;
263
264 return udi;
265}
266
267/** @private */
268SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
269{
270 /* Avoid compiler warnings. */
271 (void)usb;
272
273 /* Nothing to do for this device instance type. */
274}
275
276#endif
277
278/** @private */
279SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
280 int fd)
281{
282 struct sr_serial_dev_inst *serial;
283
284 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
285 sr_err("hwdriver: %s: serial malloc failed", __func__);
286 return NULL;
287 }
288
289 serial->port = g_strdup(port);
290 serial->fd = fd;
291
292 return serial;
293}
294
295/** @private */
296SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
297{
298 g_free(serial->port);
299}
300
301SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
302 const void *value)
303{
304 int ret;
305
306 if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) {
307 sr_err("hwdriver: unable to set config option");
308 return SR_ERR;
309 }
310
311 ret = sdi->driver->dev_config_set(sdi, hwcap, value);
312
313 return ret;
314}
315
316SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver)
317{
318
319 if (driver && driver->dev_list)
320 return driver->dev_list();
321 else
322 return NULL;
323}
324
325SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver)
326{
327
328 if (driver && driver->dev_clear)
329 return driver->dev_clear();
330 else
331 return SR_OK;
332}
333
334/** @} */