]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
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> | |
22 | #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */ | |
23 | #include "libsigrok.h" | |
24 | #include "libsigrok-internal.h" | |
25 | ||
26 | /** | |
27 | * @file | |
28 | * | |
29 | * Device handling in libsigrok. | |
30 | */ | |
31 | ||
32 | /** | |
33 | * @defgroup grp_devices Devices | |
34 | * | |
35 | * Device handling in libsigrok. | |
36 | * | |
37 | * @{ | |
38 | */ | |
39 | ||
40 | /** @private */ | |
41 | SR_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 | ||
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 | * | |
66 | * @param sdi The device instance the probe is connected to. | |
67 | * @param probenum The number of the probe whose name to set. | |
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. | |
71 | * | |
72 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
73 | */ | |
74 | SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi, | |
75 | int probenum, const char *name) | |
76 | { | |
77 | GSList *l; | |
78 | struct sr_probe *probe; | |
79 | int ret; | |
80 | ||
81 | if (!sdi) { | |
82 | sr_err("%s: sdi was NULL", __func__); | |
83 | return SR_ERR_ARG; | |
84 | } | |
85 | ||
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 | } | |
95 | } | |
96 | ||
97 | return ret; | |
98 | } | |
99 | ||
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 | */ | |
109 | SR_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 | ||
132 | /** | |
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. | |
137 | * | |
138 | * @param sdi Must not be NULL. | |
139 | * @param probenum The probe number, starting from 0. | |
140 | * @param trigger Trigger string, in the format used by sigrok-cli | |
141 | * | |
142 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
143 | */ | |
144 | SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum, | |
145 | const char *trigger) | |
146 | { | |
147 | GSList *l; | |
148 | struct sr_probe *probe; | |
149 | int ret; | |
150 | ||
151 | if (!sdi) | |
152 | return SR_ERR_ARG; | |
153 | ||
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 | } | |
164 | } | |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Determine whether the specified device instance has the specified | |
171 | * capability. | |
172 | * | |
173 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
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). | |
177 | * @param hwcap The capability that should be checked (whether it's supported | |
178 | * by the specified device). | |
179 | * | |
180 | * @return TRUE if the device has the specified capability, FALSE otherwise. | |
181 | * FALSE is also returned upon invalid input parameters or other | |
182 | * error conditions. | |
183 | */ | |
184 | SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap) | |
185 | { | |
186 | const int *hwcaps; | |
187 | int i; | |
188 | ||
189 | if (!sdi || !sdi->driver) | |
190 | return FALSE; | |
191 | ||
192 | if (sdi->driver->info_get(SR_DI_HWCAPS, | |
193 | (const void **)&hwcaps, NULL) != SR_OK) | |
194 | return FALSE; | |
195 | ||
196 | for (i = 0; hwcaps[i]; i++) { | |
197 | if (hwcaps[i] == hwcap) | |
198 | return TRUE; | |
199 | } | |
200 | ||
201 | return FALSE; | |
202 | } | |
203 | ||
204 | /** @private */ | |
205 | SR_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 | ||
215 | sdi->driver = NULL; | |
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 | ||
228 | /** @private */ | |
229 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) | |
230 | { | |
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 | ||
240 | g_free(sdi->priv); | |
241 | g_free(sdi->vendor); | |
242 | g_free(sdi->model); | |
243 | g_free(sdi->version); | |
244 | g_free(sdi); | |
245 | ||
246 | } | |
247 | ||
248 | #ifdef HAVE_LIBUSB_1_0 | |
249 | ||
250 | /** @private */ | |
251 | SR_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 | ||
268 | /** @private */ | |
269 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) | |
270 | { | |
271 | /* Avoid compiler warnings. */ | |
272 | (void)usb; | |
273 | ||
274 | /* Nothing to do for this device instance type. */ | |
275 | } | |
276 | ||
277 | #endif | |
278 | ||
279 | /** @private */ | |
280 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, | |
281 | int fd) | |
282 | { | |
283 | struct sr_serial_dev_inst *serial; | |
284 | ||
285 | if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) { | |
286 | sr_err("hwdriver: %s: serial malloc failed", __func__); | |
287 | return NULL; | |
288 | } | |
289 | ||
290 | serial->port = g_strdup(port); | |
291 | serial->fd = fd; | |
292 | ||
293 | return serial; | |
294 | } | |
295 | ||
296 | /** @private */ | |
297 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) | |
298 | { | |
299 | g_free(serial->port); | |
300 | } | |
301 | ||
302 | SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
303 | const void *value) | |
304 | { | |
305 | int ret; | |
306 | ||
307 | if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) { | |
308 | sr_err("hwdriver: unable to set config option"); | |
309 | return SR_ERR; | |
310 | } | |
311 | ||
312 | ret = sdi->driver->dev_config_set(sdi, hwcap, value); | |
313 | ||
314 | return ret; | |
315 | } | |
316 | ||
317 | SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver) | |
318 | { | |
319 | ||
320 | if (driver && driver->dev_list) | |
321 | return driver->dev_list(); | |
322 | else | |
323 | return NULL; | |
324 | } | |
325 | ||
326 | SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver) | |
327 | { | |
328 | ||
329 | if (driver && driver->dev_clear) | |
330 | return driver->dev_clear(); | |
331 | else | |
332 | return SR_OK; | |
333 | } | |
334 | ||
335 | /** @} */ |