]>
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 | /* 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 | ||
35 | /** | |
36 | * @file | |
37 | * | |
38 | * Device handling in libsigrok. | |
39 | */ | |
40 | ||
41 | /** | |
42 | * @defgroup grp_devices Devices | |
43 | * | |
44 | * Device handling in libsigrok. | |
45 | * | |
46 | * @{ | |
47 | */ | |
48 | ||
49 | /** @private */ | |
50 | SR_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)))) { | |
56 | sr_err("Probe malloc failed."); | |
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 | ||
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 | * | |
75 | * @param sdi The device instance the probe is connected to. | |
76 | * @param probenum The number of the probe whose name to set. | |
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. | |
80 | * | |
81 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
82 | */ | |
83 | SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi, | |
84 | int probenum, const char *name) | |
85 | { | |
86 | GSList *l; | |
87 | struct sr_probe *probe; | |
88 | int ret; | |
89 | ||
90 | if (!sdi) { | |
91 | sr_err("%s: sdi was NULL", __func__); | |
92 | return SR_ERR_ARG; | |
93 | } | |
94 | ||
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 | } | |
104 | } | |
105 | ||
106 | return ret; | |
107 | } | |
108 | ||
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 | */ | |
118 | SR_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 | ||
141 | /** | |
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. | |
146 | * | |
147 | * @param sdi Must not be NULL. | |
148 | * @param probenum The probe number, starting from 0. | |
149 | * @param trigger Trigger string, in the format used by sigrok-cli | |
150 | * | |
151 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
152 | */ | |
153 | SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum, | |
154 | const char *trigger) | |
155 | { | |
156 | GSList *l; | |
157 | struct sr_probe *probe; | |
158 | int ret; | |
159 | ||
160 | if (!sdi) | |
161 | return SR_ERR_ARG; | |
162 | ||
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 | } | |
173 | } | |
174 | ||
175 | return ret; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Determine whether the specified device instance has the specified | |
180 | * capability. | |
181 | * | |
182 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
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). | |
186 | * @param hwcap The capability that should be checked (whether it's supported | |
187 | * by the specified device). | |
188 | * | |
189 | * @return TRUE if the device has the specified capability, FALSE otherwise. | |
190 | * FALSE is also returned upon invalid input parameters or other | |
191 | * error conditions. | |
192 | */ | |
193 | SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap) | |
194 | { | |
195 | const int *hwcaps; | |
196 | int i; | |
197 | ||
198 | if (!sdi || !sdi->driver) | |
199 | return FALSE; | |
200 | ||
201 | if (sdi->driver->info_get(SR_DI_HWCAPS, | |
202 | (const void **)&hwcaps, NULL) != SR_OK) | |
203 | return FALSE; | |
204 | ||
205 | for (i = 0; hwcaps[i]; i++) { | |
206 | if (hwcaps[i] == hwcap) | |
207 | return TRUE; | |
208 | } | |
209 | ||
210 | return FALSE; | |
211 | } | |
212 | ||
213 | /** @private */ | |
214 | SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status, | |
215 | const char *vendor, const char *model, const char *version) | |
216 | { | |
217 | struct sr_dev_inst *sdi; | |
218 | ||
219 | if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) { | |
220 | sr_err("%s: sdi malloc failed", __func__); | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | sdi->driver = NULL; | |
225 | sdi->index = index; | |
226 | sdi->status = status; | |
227 | sdi->inst_type = -1; | |
228 | sdi->vendor = vendor ? g_strdup(vendor) : NULL; | |
229 | sdi->model = model ? g_strdup(model) : NULL; | |
230 | sdi->version = version ? g_strdup(version) : NULL; | |
231 | sdi->probes = NULL; | |
232 | sdi->priv = NULL; | |
233 | ||
234 | return sdi; | |
235 | } | |
236 | ||
237 | /** @private */ | |
238 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) | |
239 | { | |
240 | struct sr_probe *probe; | |
241 | GSList *l; | |
242 | ||
243 | for (l = sdi->probes; l; l = l->next) { | |
244 | probe = l->data; | |
245 | g_free(probe->name); | |
246 | g_free(probe); | |
247 | } | |
248 | ||
249 | g_free(sdi->priv); | |
250 | g_free(sdi->vendor); | |
251 | g_free(sdi->model); | |
252 | g_free(sdi->version); | |
253 | g_free(sdi); | |
254 | ||
255 | } | |
256 | ||
257 | #ifdef HAVE_LIBUSB_1_0 | |
258 | ||
259 | /** @private */ | |
260 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, | |
261 | uint8_t address, struct libusb_device_handle *hdl) | |
262 | { | |
263 | struct sr_usb_dev_inst *udi; | |
264 | ||
265 | if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { | |
266 | sr_err("%s: udi malloc failed", __func__); | |
267 | return NULL; | |
268 | } | |
269 | ||
270 | udi->bus = bus; | |
271 | udi->address = address; | |
272 | udi->devhdl = hdl; | |
273 | ||
274 | return udi; | |
275 | } | |
276 | ||
277 | /** @private */ | |
278 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) | |
279 | { | |
280 | (void)usb; | |
281 | ||
282 | /* Nothing to do for this device instance type. */ | |
283 | } | |
284 | ||
285 | #endif | |
286 | ||
287 | /** @private | |
288 | * @param pathname OS-specific serial port specification. Examples: | |
289 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". | |
290 | * @param serialcomm A serial communication parameters string, in the form | |
291 | * of <speed>/<data bits><parity><stopbits>, for example "9600/8n1" or | |
292 | * "600/7o2". This is an optional parameter; it may be filled in later. | |
293 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
294 | * or NULL on error. | |
295 | * | |
296 | * Both parameters are copied to newly allocated strings, and freed | |
297 | * automatically by sr_serial_dev_inst_free(). | |
298 | */ | |
299 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, | |
300 | const char *serialcomm) | |
301 | { | |
302 | struct sr_serial_dev_inst *serial; | |
303 | ||
304 | if (!port) { | |
305 | sr_err("hwdriver: serial port required"); | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) { | |
310 | sr_err("hwdriver: serial malloc failed"); | |
311 | return NULL; | |
312 | } | |
313 | ||
314 | serial->port = g_strdup(port); | |
315 | if (serialcomm) | |
316 | serial->serialcomm = g_strdup(serialcomm); | |
317 | serial->fd = -1; | |
318 | ||
319 | return serial; | |
320 | } | |
321 | ||
322 | /** @private */ | |
323 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) | |
324 | { | |
325 | ||
326 | g_free(serial->port); | |
327 | g_free(serial->serialcomm); | |
328 | ||
329 | } | |
330 | ||
331 | SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
332 | const void *value) | |
333 | { | |
334 | int ret; | |
335 | ||
336 | if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) { | |
337 | sr_err("Unable to set config option."); | |
338 | return SR_ERR; | |
339 | } | |
340 | ||
341 | ret = sdi->driver->dev_config_set(sdi, hwcap, value); | |
342 | ||
343 | return ret; | |
344 | } | |
345 | ||
346 | SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver) | |
347 | { | |
348 | ||
349 | if (driver && driver->dev_list) | |
350 | return driver->dev_list(); | |
351 | else | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver) | |
356 | { | |
357 | ||
358 | if (driver && driver->dev_clear) | |
359 | return driver->dev_clear(); | |
360 | else | |
361 | return SR_OK; | |
362 | } | |
363 | ||
364 | /** @} */ |