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