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