]> sigrok.org Git - libsigrok.git/blame - device.c
Doxygen: Explain init/shutdown, add small example.
[libsigrok.git] / device.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 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>
45c59c8b
BV
22#include "libsigrok.h"
23#include "libsigrok-internal.h"
a1bb33af 24
393fb9cb
UH
25/**
26 * @file
27 *
28 * Device handling in libsigrok.
29 */
30
7b870c38
UH
31/**
32 * @defgroup grp_devices Devices
33 *
34 * Device handling in libsigrok.
35 *
36 * @{
37 */
38
b4bd7088 39/** @private */
48a486cd
BV
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
94799bc4
UH
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 *
37e8b4c4 65 * @param sdi The device instance the probe is connected to.
94799bc4 66 * @param probenum The number of the probe whose name to set.
37e8b4c4
BV
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.
0e3b1439 70 *
37e8b4c4 71 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
94799bc4 72 */
37e8b4c4
BV
73SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
74 int probenum, const char *name)
a1bb33af 75{
37e8b4c4
BV
76 GSList *l;
77 struct sr_probe *probe;
78 int ret;
a1bb33af 79
37e8b4c4
BV
80 if (!sdi) {
81 sr_err("%s: sdi was NULL", __func__);
0e3b1439 82 return SR_ERR_ARG;
94799bc4
UH
83 }
84
37e8b4c4
BV
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 }
94799bc4
UH
94 }
95
37e8b4c4 96 return ret;
a1bb33af
UH
97}
98
be5bf44d
BV
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
94799bc4 131/**
01c3e9db
UH
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.
94799bc4 136 *
c7ee3ddb 137 * @param sdi Must not be NULL.
a5f2e707
BV
138 * @param probenum The probe number, starting from 0.
139 * @param trigger Trigger string, in the format used by sigrok-cli
0e3b1439 140 *
a5f2e707 141 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
94799bc4 142 */
58453e58
BV
143SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
144 const char *trigger)
a1bb33af 145{
58453e58
BV
146 GSList *l;
147 struct sr_probe *probe;
148 int ret;
a1bb33af 149
58453e58 150 if (!sdi)
0e3b1439 151 return SR_ERR_ARG;
94799bc4 152
58453e58
BV
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 }
94799bc4 163 }
a1bb33af 164
58453e58 165 return ret;
7d658874
BV
166}
167
94799bc4 168/**
9c5332d2
UH
169 * Determine whether the specified device instance has the specified
170 * capability.
94799bc4 171 *
9c5332d2 172 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
8ec95d22
UH
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).
94799bc4
UH
176 * @param hwcap The capability that should be checked (whether it's supported
177 * by the specified device).
178 *
a5f2e707 179 * @return TRUE if the device has the specified capability, FALSE otherwise.
94799bc4
UH
180 * FALSE is also returned upon invalid input parameters or other
181 * error conditions.
182 */
a5b35a16 183SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
7d658874 184{
915f7cc8
JH
185 const int *hwcaps;
186 int i;
7d658874 187
a5b35a16 188 if (!sdi || !sdi->driver)
8ec95d22 189 return FALSE;
94799bc4 190
a5b35a16
BV
191 if (sdi->driver->info_get(SR_DI_HWCAPS,
192 (const void **)&hwcaps, NULL) != SR_OK)
8ec95d22 193 return FALSE;
94799bc4 194
ffedd0bf 195 for (i = 0; hwcaps[i]; i++) {
a5b35a16
BV
196 if (hwcaps[i] == hwcap)
197 return TRUE;
94799bc4 198 }
218557b8 199
7d658874 200 return FALSE;
a1bb33af 201}
fd9836bf 202
b4bd7088 203/** @private */
48a486cd
BV
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
e8d3d6c8 214 sdi->driver = NULL;
48a486cd
BV
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
b4bd7088 227/** @private */
48a486cd
BV
228SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
229{
d3cff734
BV
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
48a486cd
BV
239 g_free(sdi->priv);
240 g_free(sdi->vendor);
241 g_free(sdi->model);
242 g_free(sdi->version);
243 g_free(sdi);
d3cff734 244
48a486cd
BV
245}
246
247#ifdef HAVE_LIBUSB_1_0
248
b4bd7088 249/** @private */
48a486cd
BV
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
b4bd7088 267/** @private */
48a486cd
BV
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
b4bd7088 278/** @private */
48a486cd
BV
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
b4bd7088 295/** @private */
48a486cd
BV
296SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
297{
298 g_free(serial->port);
299}
300
a56f1480
BV
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}
811deee4
BV
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
7b870c38 334/** @} */