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