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