]> sigrok.org Git - libsigrok.git/blame - device.c
Add Rigol DS1052E/1102E VID:PID
[libsigrok.git] / device.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
13d8e03c 4 * Copyright (C) 2013 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
a885ce3e
UH
26/* Message logging helpers with driver-specific prefix string. */
27#define DRIVER_LOG_DOMAIN "device: "
28#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
29#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
30#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
31#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
32#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
33#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
34
393fb9cb
UH
35/**
36 * @file
37 *
38 * Device handling in libsigrok.
39 */
40
7b870c38
UH
41/**
42 * @defgroup grp_devices Devices
43 *
44 * Device handling in libsigrok.
45 *
46 * @{
47 */
48
b4bd7088 49/** @private */
48a486cd
BV
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)))) {
a885ce3e 56 sr_err("Probe malloc failed.");
48a486cd
BV
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
94799bc4
UH
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 *
37e8b4c4 75 * @param sdi The device instance the probe is connected to.
94799bc4 76 * @param probenum The number of the probe whose name to set.
37e8b4c4
BV
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.
0e3b1439 80 *
37e8b4c4 81 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
94799bc4 82 */
37e8b4c4
BV
83SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
84 int probenum, const char *name)
a1bb33af 85{
37e8b4c4
BV
86 GSList *l;
87 struct sr_probe *probe;
88 int ret;
a1bb33af 89
37e8b4c4
BV
90 if (!sdi) {
91 sr_err("%s: sdi was NULL", __func__);
0e3b1439 92 return SR_ERR_ARG;
94799bc4
UH
93 }
94
37e8b4c4
BV
95 ret = SR_ERR_ARG;
96 for (l = sdi->probes; l; l = l->next) {
97 probe = l->data;
98 if (probe->index == probenum) {
99 g_free(probe->name);
100 probe->name = g_strdup(name);
101 ret = SR_OK;
102 break;
103 }
94799bc4
UH
104 }
105
37e8b4c4 106 return ret;
a1bb33af
UH
107}
108
be5bf44d
BV
109/**
110 * Enable or disable a probe on the specified device.
111 *
112 * @param sdi The device instance the probe is connected to.
113 * @param probenum The probe number, starting from 0.
114 * @param state TRUE to enable the probe, FALSE to disable.
115 *
116 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
117 */
118SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
119 gboolean state)
120{
121 GSList *l;
122 struct sr_probe *probe;
123 int ret;
124
125 if (!sdi)
126 return SR_ERR_ARG;
127
128 ret = SR_ERR_ARG;
129 for (l = sdi->probes; l; l = l->next) {
130 probe = l->data;
131 if (probe->index == probenum) {
132 probe->enabled = state;
133 ret = SR_OK;
134 break;
135 }
136 }
137
138 return ret;
139}
140
94799bc4 141/**
01c3e9db
UH
142 * Add a trigger to the specified device (and the specified probe).
143 *
144 * If the specified probe of this device already has a trigger, it will
145 * be silently replaced.
94799bc4 146 *
c7ee3ddb 147 * @param sdi Must not be NULL.
a5f2e707
BV
148 * @param probenum The probe number, starting from 0.
149 * @param trigger Trigger string, in the format used by sigrok-cli
0e3b1439 150 *
a5f2e707 151 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
94799bc4 152 */
58453e58
BV
153SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
154 const char *trigger)
a1bb33af 155{
58453e58
BV
156 GSList *l;
157 struct sr_probe *probe;
158 int ret;
a1bb33af 159
58453e58 160 if (!sdi)
0e3b1439 161 return SR_ERR_ARG;
94799bc4 162
58453e58
BV
163 ret = SR_ERR_ARG;
164 for (l = sdi->probes; l; l = l->next) {
165 probe = l->data;
166 if (probe->index == probenum) {
167 /* If the probe already has a trigger, kill it first. */
168 g_free(probe->trigger);
169 probe->trigger = g_strdup(trigger);
170 ret = SR_OK;
171 break;
172 }
94799bc4 173 }
a1bb33af 174
58453e58 175 return ret;
7d658874
BV
176}
177
94799bc4 178/**
9c5332d2
UH
179 * Determine whether the specified device instance has the specified
180 * capability.
94799bc4 181 *
9c5332d2 182 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
8ec95d22
UH
183 * If the device's 'driver' field is NULL (virtual device), this
184 * function will always return FALSE (virtual devices don't have
185 * a hardware capabilities list).
ca0938c5 186 * @param key The option that should be checked for support on the
4d15e5c9 187 * specified device.
94799bc4 188 *
4d15e5c9
BV
189 * @return TRUE if the device has the specified option, FALSE otherwise.
190 * FALSE is also returned on invalid input parameters or other
94799bc4
UH
191 * error conditions.
192 */
4d15e5c9 193SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
7d658874 194{
003595ac 195 GVariant *gvar;
4d15e5c9 196 const int *devopts;
003595ac
BV
197 gsize num_opts, i;
198 int ret;
7d658874 199
003595ac 200 if (!sdi || !sdi->driver || !sdi->driver->config_list)
8ec95d22 201 return FALSE;
94799bc4 202
003595ac 203 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, NULL) != SR_OK)
8ec95d22 204 return FALSE;
94799bc4 205
003595ac
BV
206 ret = FALSE;
207 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
208 for (i = 0; i < num_opts; i++) {
209 if (devopts[i] == key) {
210 ret = TRUE;
211 break;
212 }
94799bc4 213 }
003595ac 214 g_variant_unref(gvar);
218557b8 215
003595ac 216 return ret;
a1bb33af 217}
fd9836bf 218
b4bd7088 219/** @private */
48a486cd
BV
220SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
221 const char *vendor, const char *model, const char *version)
222{
223 struct sr_dev_inst *sdi;
224
225 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
c4227fc6 226 sr_err("Device instance malloc failed.");
48a486cd
BV
227 return NULL;
228 }
229
e8d3d6c8 230 sdi->driver = NULL;
48a486cd
BV
231 sdi->index = index;
232 sdi->status = status;
233 sdi->inst_type = -1;
234 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
235 sdi->model = model ? g_strdup(model) : NULL;
236 sdi->version = version ? g_strdup(version) : NULL;
237 sdi->probes = NULL;
238 sdi->priv = NULL;
239
240 return sdi;
241}
242
b4bd7088 243/** @private */
48a486cd
BV
244SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
245{
d3cff734
BV
246 struct sr_probe *probe;
247 GSList *l;
248
249 for (l = sdi->probes; l; l = l->next) {
250 probe = l->data;
251 g_free(probe->name);
252 g_free(probe);
253 }
254
48a486cd
BV
255 g_free(sdi->priv);
256 g_free(sdi->vendor);
257 g_free(sdi->model);
258 g_free(sdi->version);
259 g_free(sdi);
d3cff734 260
48a486cd
BV
261}
262
263#ifdef HAVE_LIBUSB_1_0
264
b4bd7088 265/** @private */
48a486cd
BV
266SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
267 uint8_t address, struct libusb_device_handle *hdl)
268{
269 struct sr_usb_dev_inst *udi;
270
271 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
c4227fc6 272 sr_err("USB device instance malloc failed.");
48a486cd
BV
273 return NULL;
274 }
275
276 udi->bus = bus;
277 udi->address = address;
278 udi->devhdl = hdl;
279
280 return udi;
281}
282
b4bd7088 283/** @private */
48a486cd
BV
284SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
285{
48a486cd
BV
286 (void)usb;
287
288 /* Nothing to do for this device instance type. */
289}
290
291#endif
292
299bdb24
BV
293/** @private
294 * @param pathname OS-specific serial port specification. Examples:
295 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
296 * @param serialcomm A serial communication parameters string, in the form
297 * of <speed>/<data bits><parity><stopbits>, for example "9600/8n1" or
298 * "600/7o2". This is an optional parameter; it may be filled in later.
299 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
300 * or NULL on error.
301 *
302 * Both parameters are copied to newly allocated strings, and freed
303 * automatically by sr_serial_dev_inst_free().
304 */
48a486cd 305SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
299bdb24 306 const char *serialcomm)
48a486cd
BV
307{
308 struct sr_serial_dev_inst *serial;
309
299bdb24 310 if (!port) {
c4227fc6 311 sr_err("Serial port required.");
299bdb24
BV
312 return NULL;
313 }
314
315 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
c4227fc6 316 sr_err("Serial device instance malloc failed.");
48a486cd
BV
317 return NULL;
318 }
319
320 serial->port = g_strdup(port);
299bdb24
BV
321 if (serialcomm)
322 serial->serialcomm = g_strdup(serialcomm);
323 serial->fd = -1;
48a486cd
BV
324
325 return serial;
326}
327
b4bd7088 328/** @private */
48a486cd
BV
329SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
330{
331 g_free(serial->port);
299bdb24 332 g_free(serial->serialcomm);
acac8fc3 333 g_free(serial);
48a486cd
BV
334}
335
811deee4
BV
336SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver)
337{
811deee4
BV
338 if (driver && driver->dev_list)
339 return driver->dev_list();
340 else
341 return NULL;
342}
343
344SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver)
345{
811deee4
BV
346 if (driver && driver->dev_clear)
347 return driver->dev_clear();
348 else
349 return SR_OK;
350}
351
7b870c38 352/** @} */