]>
Commit | Line | Data |
---|---|---|
ca3d84cc BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <fcntl.h> | |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
ca3d84cc BV |
27 | #include "genericdmm.h" |
28 | ||
b84c13d7 | 29 | extern SR_PRIV struct dmmchip dmmchip_victor70c; |
ca3d84cc | 30 | |
7fc754a0 BV |
31 | static struct sr_hwopt victor_70c_vidpid[] = { |
32 | { SR_HWOPT_CONN, "1244.d237" }, | |
33 | { 0, NULL } | |
34 | }; | |
ca3d84cc | 35 | static struct dev_profile dev_profiles[] = { |
b84c13d7 BV |
36 | { "victor-70c", "Victor", "70C", &dmmchip_victor70c, |
37 | DMM_TRANSPORT_USBHID, 1000, victor_70c_vidpid | |
7fc754a0 | 38 | }, |
3c6ce226 | 39 | { NULL, NULL, NULL, NULL, 0, 0, NULL } |
ca3d84cc BV |
40 | }; |
41 | ||
f1a14ea7 BV |
42 | static const int hwopts[] = { |
43 | SR_HWOPT_MODEL, | |
44 | SR_HWOPT_CONN, | |
45 | SR_HWOPT_SERIALCOMM, | |
46 | 0, | |
47 | }; | |
48 | ||
ca3d84cc BV |
49 | static const int hwcaps[] = { |
50 | SR_HWCAP_MULTIMETER, | |
51 | SR_HWCAP_LIMIT_SAMPLES, | |
52 | SR_HWCAP_LIMIT_MSEC, | |
53 | SR_HWCAP_CONTINUOUS, | |
ca3d84cc BV |
54 | 0, |
55 | }; | |
56 | ||
57 | static const char *probe_names[] = { | |
58 | "Probe", | |
59 | NULL, | |
60 | }; | |
61 | ||
75337758 BV |
62 | SR_PRIV struct sr_dev_driver genericdmm_driver_info; |
63 | static struct sr_dev_driver *gdi = &genericdmm_driver_info; | |
64 | /* TODO need a way to keep this local to the static library */ | |
3c6ce226 BV |
65 | static libusb_context *genericdmm_usb_context = NULL; |
66 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, | |
67 | void *cb_data); | |
ca3d84cc BV |
68 | |
69 | ||
bbb40871 BV |
70 | static GSList *connect_usb(const char *conn) |
71 | { | |
72 | struct sr_dev_inst *sdi; | |
301a5e4c BV |
73 | struct drv_context *drvc; |
74 | struct dev_context *devc; | |
3c6ce226 | 75 | struct sr_probe *probe; |
bbb40871 BV |
76 | libusb_device **devlist; |
77 | struct libusb_device_descriptor des; | |
78 | GSList *devices; | |
79 | GRegex *reg; | |
80 | GMatchInfo *match; | |
81 | int vid, pid, bus, addr, devcnt, err, i; | |
82 | char *mstr; | |
83 | ||
301a5e4c BV |
84 | drvc = gdi->priv; |
85 | ||
bbb40871 BV |
86 | vid = pid = bus = addr = 0; |
87 | reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL); | |
88 | if (g_regex_match(reg, conn, 0, &match)) { | |
89 | /* Extract VID. */ | |
90 | if ((mstr = g_match_info_fetch(match, 1))) | |
91 | vid = strtoul(mstr, NULL, 16); | |
92 | g_free(mstr); | |
93 | ||
94 | /* Extract PID. */ | |
95 | if ((mstr = g_match_info_fetch(match, 2))) | |
96 | pid = strtoul(mstr, NULL, 16); | |
97 | g_free(mstr); | |
98 | } else { | |
99 | g_match_info_unref(match); | |
100 | g_regex_unref(reg); | |
101 | reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL); | |
102 | if (g_regex_match(reg, conn, 0, &match)) { | |
103 | /* Extract bus. */ | |
104 | if ((mstr = g_match_info_fetch(match, 0))) | |
105 | bus = strtoul(mstr, NULL, 16); | |
106 | g_free(mstr); | |
107 | ||
108 | /* Extract address. */ | |
109 | if ((mstr = g_match_info_fetch(match, 0))) | |
110 | addr = strtoul(mstr, NULL, 16); | |
111 | g_free(mstr); | |
112 | } | |
113 | } | |
114 | g_match_info_unref(match); | |
115 | g_regex_unref(reg); | |
116 | ||
117 | if (vid + pid + bus + addr == 0) | |
118 | return NULL; | |
119 | ||
120 | if (bus > 64) { | |
2980cc24 | 121 | sr_err("Invalid bus."); |
bbb40871 BV |
122 | return NULL; |
123 | } | |
124 | ||
125 | if (addr > 127) { | |
2980cc24 | 126 | sr_err("Invalid address."); |
bbb40871 BV |
127 | return NULL; |
128 | } | |
129 | ||
130 | /* Looks like a valid USB device specification, but is it connected? */ | |
131 | devices = NULL; | |
132 | libusb_get_device_list(genericdmm_usb_context, &devlist); | |
133 | for (i = 0; devlist[i]; i++) { | |
134 | if ((err = libusb_get_device_descriptor(devlist[i], &des))) { | |
2980cc24 | 135 | sr_err("Failed to get device descriptor: %d.", err); |
bbb40871 BV |
136 | continue; |
137 | } | |
138 | ||
139 | if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) | |
140 | /* VID/PID specified, but no match. */ | |
141 | continue; | |
142 | ||
143 | if (bus + addr && ( | |
144 | libusb_get_bus_number(devlist[i]) != bus | |
145 | || libusb_get_device_address(devlist[i]) != addr)) | |
146 | /* Bus/address specified, but no match. */ | |
147 | continue; | |
148 | ||
149 | /* Found one. */ | |
301a5e4c | 150 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
2980cc24 | 151 | sr_err("Device context malloc failed."); |
886a52b6 | 152 | return NULL; |
bbb40871 BV |
153 | } |
154 | ||
301a5e4c | 155 | devcnt = g_slist_length(drvc->instances); |
3c6ce226 | 156 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, |
d6db79a4 | 157 | NULL, NULL, NULL))) { |
2980cc24 | 158 | sr_err("sr_dev_inst_new returned NULL."); |
bbb40871 BV |
159 | return NULL; |
160 | } | |
301a5e4c | 161 | sdi->priv = devc; |
3c6ce226 BV |
162 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) |
163 | return NULL; | |
164 | sdi->probes = g_slist_append(sdi->probes, probe); | |
301a5e4c | 165 | devc->usb = sr_usb_dev_inst_new( |
bbb40871 BV |
166 | libusb_get_bus_number(devlist[i]), |
167 | libusb_get_device_address(devlist[i]), NULL); | |
168 | devices = g_slist_append(devices, sdi); | |
169 | } | |
170 | libusb_free_device_list(devlist, 1); | |
171 | ||
172 | return devices; | |
173 | } | |
174 | ||
175 | static GSList *connect_serial(const char *conn, const char *serialcomm) | |
176 | { | |
177 | GSList *devices; | |
178 | ||
179 | devices = NULL; | |
180 | ||
181 | /* TODO */ | |
2980cc24 | 182 | sr_dbg("Not yet implemented."); |
bbb40871 BV |
183 | |
184 | return devices; | |
185 | } | |
186 | ||
187 | GSList *genericdmm_connect(const char *conn, const char *serialcomm) | |
188 | { | |
189 | GSList *devices; | |
190 | ||
191 | if (serialcomm) | |
192 | /* Must be a serial port. */ | |
193 | return connect_serial(conn, serialcomm); | |
194 | ||
195 | if ((devices = connect_usb(conn))) | |
196 | return devices; | |
197 | ||
198 | return NULL; | |
199 | } | |
200 | ||
3a0fe402 BV |
201 | static GSList *default_scan(GSList *options) |
202 | { | |
203 | GSList *l, *devices; | |
204 | struct sr_hwopt *opt; | |
205 | const char *conn, *serialcomm; | |
206 | ||
207 | devices = NULL; | |
208 | conn = serialcomm = NULL; | |
209 | for (l = options; l; l = l->next) { | |
210 | opt = l->data; | |
211 | switch (opt->hwopt) { | |
212 | case SR_HWOPT_CONN: | |
213 | conn = opt->value; | |
214 | break; | |
215 | case SR_HWOPT_SERIALCOMM: | |
216 | serialcomm = opt->value; | |
217 | break; | |
218 | } | |
219 | } | |
220 | if (conn) | |
221 | devices = genericdmm_connect(conn, serialcomm); | |
222 | ||
223 | return devices; | |
224 | } | |
225 | ||
3c6ce226 BV |
226 | static int open_usb(struct sr_dev_inst *sdi) |
227 | { | |
228 | libusb_device **devlist; | |
229 | struct libusb_device_descriptor des; | |
230 | struct dev_context *devc; | |
231 | int ret, tmp, cnt, i; | |
232 | ||
233 | devc = sdi->priv; | |
234 | ||
235 | if (sdi->status == SR_ST_ACTIVE) | |
236 | /* already in use */ | |
237 | return SR_ERR; | |
238 | ||
239 | cnt = libusb_get_device_list(genericdmm_usb_context, &devlist); | |
240 | if (cnt < 0) { | |
2980cc24 | 241 | sr_err("Failed to retrieve device list (%d).", cnt); |
3c6ce226 BV |
242 | return SR_ERR; |
243 | } | |
244 | ||
245 | ret = SR_ERR; | |
246 | for (i = 0; i < cnt; i++) { | |
247 | if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) { | |
2980cc24 | 248 | sr_err("Failed to get device descriptor: %d.", tmp); |
3c6ce226 BV |
249 | continue; |
250 | } | |
251 | ||
252 | if (libusb_get_bus_number(devlist[i]) != devc->usb->bus | |
253 | || libusb_get_device_address(devlist[i]) != devc->usb->address) | |
254 | /* this is not the one */ | |
255 | continue; | |
256 | ||
257 | if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) { | |
2980cc24 | 258 | sr_err("Failed to open device: %d.", tmp); |
3c6ce226 BV |
259 | break; |
260 | } | |
261 | ||
2980cc24 UH |
262 | sr_info("Opened device %s on %d.%d.", devc->profile->modelid, |
263 | devc->usb->bus, devc->usb->address); | |
3c6ce226 BV |
264 | ret = SR_OK; |
265 | break; | |
266 | } | |
267 | libusb_free_device_list(devlist, 1); | |
268 | ||
269 | return ret; | |
270 | } | |
271 | ||
811deee4 BV |
272 | static int clear_instances(void) |
273 | { | |
274 | GSList *l; | |
275 | struct sr_dev_inst *sdi; | |
276 | struct dev_context *devc; | |
277 | struct drv_context *drvc; | |
278 | ||
279 | if (!(drvc = gdi->priv)) | |
280 | return SR_OK; | |
281 | ||
282 | /* Properly close and free all devices. */ | |
283 | for (l = drvc->instances; l; l = l->next) { | |
284 | if (!(sdi = l->data)) { | |
285 | /* Log error, but continue cleaning up the rest. */ | |
2980cc24 | 286 | sr_err("sdi was NULL, continuing."); |
811deee4 BV |
287 | continue; |
288 | } | |
289 | if (!(devc = sdi->priv)) { | |
290 | /* Log error, but continue cleaning up the rest. */ | |
2980cc24 | 291 | sr_err("sdi->priv was NULL, continuing."); |
811deee4 BV |
292 | continue; |
293 | } | |
294 | ||
295 | if (devc->profile) { | |
296 | switch (devc->profile->transport) { | |
297 | case DMM_TRANSPORT_USBHID: | |
811deee4 BV |
298 | sr_usb_dev_inst_free(devc->usb); |
299 | break; | |
300 | case DMM_TRANSPORT_SERIAL: | |
301 | if (devc->serial && devc->serial->fd != -1) | |
302 | serial_close(devc->serial->fd); | |
303 | sr_serial_dev_inst_free(devc->serial); | |
304 | break; | |
305 | } | |
306 | } | |
307 | ||
308 | sr_dev_inst_free(sdi); | |
309 | } | |
310 | ||
311 | g_slist_free(drvc->instances); | |
312 | drvc->instances = NULL; | |
313 | ||
314 | return SR_OK; | |
315 | } | |
316 | ||
40dda2c3 | 317 | static int hw_init(void) |
ca3d84cc | 318 | { |
301a5e4c BV |
319 | struct drv_context *drvc; |
320 | ||
321 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
2980cc24 | 322 | sr_err("Driver context malloc failed."); |
886a52b6 | 323 | return SR_ERR_MALLOC; |
301a5e4c | 324 | } |
ca3d84cc | 325 | |
ca3d84cc | 326 | if (libusb_init(&genericdmm_usb_context) != 0) { |
2980cc24 | 327 | sr_err("Failed to initialize USB."); |
61136ea6 | 328 | return SR_ERR; |
ca3d84cc BV |
329 | } |
330 | ||
301a5e4c | 331 | gdi->priv = drvc; |
61136ea6 BV |
332 | |
333 | return SR_OK; | |
334 | } | |
335 | ||
3a0fe402 | 336 | static GSList *hw_scan(GSList *options) |
61136ea6 | 337 | { |
3a0fe402 BV |
338 | GSList *l, *ldef, *defopts, *newopts, *devices; |
339 | struct sr_hwopt *opt, *defopt; | |
340 | struct dev_profile *pr, *profile; | |
d6db79a4 | 341 | struct sr_dev_inst *sdi; |
301a5e4c | 342 | struct drv_context *drvc; |
3c6ce226 | 343 | struct dev_context *devc; |
3a0fe402 BV |
344 | const char *model; |
345 | ||
301a5e4c BV |
346 | drvc = gdi->priv; |
347 | ||
3a0fe402 BV |
348 | /* Separate model from the options list. */ |
349 | model = NULL; | |
350 | newopts = NULL; | |
351 | for (l = options; l; l = l->next) { | |
352 | opt = l->data; | |
353 | if (opt->hwopt == SR_HWOPT_MODEL) | |
354 | model = opt->value; | |
355 | else | |
356 | /* New list with references to the original data. */ | |
357 | newopts = g_slist_append(newopts, opt); | |
358 | } | |
359 | if (!model) { | |
d6db79a4 | 360 | /* This driver only works when a model is specified. */ |
3a0fe402 BV |
361 | return NULL; |
362 | } | |
61136ea6 | 363 | |
3a0fe402 BV |
364 | /* Find a profile with this model name. */ |
365 | profile = NULL; | |
366 | for (pr = dev_profiles; pr->modelid; pr++) { | |
367 | if (!strcmp(pr->modelid, model)) { | |
368 | profile = pr; | |
369 | break; | |
370 | } | |
ca3d84cc | 371 | } |
3a0fe402 BV |
372 | if (!profile) { |
373 | sr_err("Unknown model %s.", model); | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | /* Initialize the DMM chip driver. */ | |
378 | if (profile->chip->init) | |
379 | profile->chip->init(); | |
ca3d84cc | 380 | |
3a0fe402 BV |
381 | /* Convert the profile's default options list to a GSList. */ |
382 | defopts = NULL; | |
383 | for (opt = profile->defaults_opts; opt->hwopt; opt++) { | |
384 | /* New list with references to const data in the profile. */ | |
385 | defopts = g_slist_append(defopts, opt); | |
ca3d84cc | 386 | } |
ca3d84cc | 387 | |
3a0fe402 BV |
388 | /* Options given as argument to this function override the |
389 | * profile's default options. | |
390 | */ | |
391 | for (ldef = defopts; ldef; ldef = ldef->next) { | |
392 | defopt = ldef->data; | |
393 | for (l = newopts; l; l = l->next) { | |
394 | opt = l->data; | |
395 | if (opt->hwopt == defopt->hwopt) { | |
396 | /* Override the default, and drop it from the | |
397 | * options list. | |
398 | */ | |
399 | ldef->data = l->data; | |
400 | newopts = g_slist_remove(newopts, opt); | |
401 | break; | |
402 | } | |
403 | } | |
404 | } | |
405 | /* Whatever is left in newopts wasn't in the default options. */ | |
406 | defopts = g_slist_concat(defopts, newopts); | |
407 | g_slist_free(newopts); | |
408 | ||
409 | if (profile->chip->scan) | |
410 | /* The DMM chip driver wants to do its own scanning. */ | |
411 | devices = profile->chip->scan(defopts); | |
412 | else | |
413 | devices = default_scan(defopts); | |
414 | g_slist_free(defopts); | |
415 | ||
416 | if (devices) { | |
417 | /* TODO: need to fix up sdi->index fields */ | |
d6db79a4 BV |
418 | for (l = devices; l; l = l->next) { |
419 | /* The default connection-based scanner doesn't really | |
420 | * know about profiles, so it never filled in the vendor | |
421 | * or model. Do that now. | |
422 | */ | |
423 | sdi = l->data; | |
3c6ce226 BV |
424 | devc = sdi->priv; |
425 | devc->profile = profile; | |
d6db79a4 BV |
426 | sdi->driver = gdi; |
427 | if (!sdi->vendor) | |
428 | sdi->vendor = g_strdup(profile->vendor); | |
429 | if (!sdi->model) | |
430 | sdi->model = g_strdup(profile->model); | |
431 | /* Add a copy of these new devices to the driver instances. */ | |
301a5e4c | 432 | drvc->instances = g_slist_append(drvc->instances, l->data); |
d6db79a4 | 433 | } |
3a0fe402 BV |
434 | } |
435 | ||
436 | return devices; | |
ca3d84cc BV |
437 | } |
438 | ||
811deee4 BV |
439 | static GSList *hw_dev_list(void) |
440 | { | |
441 | struct drv_context *drvc; | |
442 | ||
443 | drvc = gdi->priv; | |
444 | ||
445 | return drvc->instances; | |
446 | } | |
447 | ||
25a0f108 | 448 | static int hw_dev_open(struct sr_dev_inst *sdi) |
ca3d84cc | 449 | { |
301a5e4c | 450 | struct dev_context *devc; |
3c6ce226 | 451 | int ret; |
ca3d84cc | 452 | |
301a5e4c | 453 | if (!(devc = sdi->priv)) { |
2980cc24 | 454 | sr_err("sdi->priv was NULL."); |
ca3d84cc BV |
455 | return SR_ERR_BUG; |
456 | } | |
457 | ||
3c6ce226 | 458 | ret = SR_OK; |
301a5e4c | 459 | switch (devc->profile->transport) { |
ca3d84cc | 460 | case DMM_TRANSPORT_USBHID: |
3c6ce226 | 461 | ret = open_usb(sdi); |
ca3d84cc BV |
462 | break; |
463 | case DMM_TRANSPORT_SERIAL: | |
2980cc24 | 464 | sr_dbg("Opening serial port '%s'.", devc->serial->port); |
301a5e4c BV |
465 | devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); |
466 | if (devc->serial->fd == -1) { | |
2980cc24 | 467 | sr_err("Couldn't open serial port '%s'.", |
301a5e4c | 468 | devc->serial->port); |
3c6ce226 | 469 | ret = SR_ERR; |
ca3d84cc | 470 | } |
301a5e4c | 471 | // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2); |
ca3d84cc BV |
472 | break; |
473 | default: | |
474 | sr_err("No transport set."); | |
3c6ce226 | 475 | ret = SR_ERR; |
ca3d84cc BV |
476 | } |
477 | ||
3c6ce226 | 478 | return ret; |
ca3d84cc BV |
479 | } |
480 | ||
25a0f108 | 481 | static int hw_dev_close(struct sr_dev_inst *sdi) |
ca3d84cc | 482 | { |
301a5e4c | 483 | struct dev_context *devc; |
ca3d84cc | 484 | |
301a5e4c | 485 | if (!(devc = sdi->priv)) { |
2980cc24 | 486 | sr_err("%s: sdi->priv was NULL.", __func__); |
ca3d84cc BV |
487 | return SR_ERR_BUG; |
488 | } | |
489 | ||
301a5e4c | 490 | switch (devc->profile->transport) { |
ca3d84cc BV |
491 | case DMM_TRANSPORT_USBHID: |
492 | /* TODO */ | |
493 | break; | |
494 | case DMM_TRANSPORT_SERIAL: | |
301a5e4c BV |
495 | if (devc->serial && devc->serial->fd != -1) { |
496 | serial_close(devc->serial->fd); | |
497 | devc->serial->fd = -1; | |
ca3d84cc BV |
498 | sdi->status = SR_ST_INACTIVE; |
499 | } | |
500 | break; | |
501 | } | |
502 | ||
503 | return SR_OK; | |
504 | } | |
505 | ||
506 | static int hw_cleanup(void) | |
507 | { | |
ca3d84cc | 508 | |
811deee4 | 509 | clear_instances(); |
ca3d84cc BV |
510 | |
511 | if (genericdmm_usb_context) | |
512 | libusb_exit(genericdmm_usb_context); | |
513 | ||
514 | return SR_OK; | |
515 | } | |
516 | ||
c4a1de59 | 517 | static int hw_info_get(int info_id, const void **data, |
6910bf6b | 518 | const struct sr_dev_inst *sdi) |
ca3d84cc | 519 | { |
301a5e4c | 520 | struct dev_context *devc; |
ca3d84cc | 521 | |
6f57fd96 | 522 | (void)sdi; |
301a5e4c | 523 | (void)devc; |
6f57fd96 | 524 | |
c4a1de59 | 525 | switch (info_id) { |
f1a14ea7 BV |
526 | case SR_DI_HWOPTS: |
527 | *data = hwopts; | |
528 | break; | |
529 | case SR_DI_HWCAPS: | |
530 | *data = hwcaps; | |
531 | break; | |
ca3d84cc | 532 | case SR_DI_NUM_PROBES: |
6910bf6b | 533 | *data = GINT_TO_POINTER(1); |
ca3d84cc BV |
534 | break; |
535 | case SR_DI_PROBE_NAMES: | |
6910bf6b | 536 | *data = probe_names; |
ca3d84cc BV |
537 | break; |
538 | case SR_DI_CUR_SAMPLERATE: | |
539 | /* TODO get rid of this */ | |
6910bf6b | 540 | *data = NULL; |
3c6ce226 | 541 | return SR_ERR_ARG; |
ca3d84cc BV |
542 | break; |
543 | default: | |
544 | /* Unknown device info ID. */ | |
6910bf6b | 545 | return SR_ERR_ARG; |
ca3d84cc BV |
546 | } |
547 | ||
6910bf6b | 548 | return SR_OK; |
ca3d84cc BV |
549 | } |
550 | ||
6f4b1868 BV |
551 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, |
552 | const void *value) | |
ca3d84cc | 553 | { |
301a5e4c | 554 | struct dev_context *devc; |
ca3d84cc | 555 | |
301a5e4c | 556 | if (!(devc = sdi->priv)) { |
2980cc24 | 557 | sr_err("sdi->priv was NULL."); |
ca3d84cc BV |
558 | return SR_ERR_BUG; |
559 | } | |
560 | ||
ca3d84cc BV |
561 | switch (hwcap) { |
562 | case SR_HWCAP_LIMIT_MSEC: | |
3c6ce226 | 563 | /* TODO: not yet implemented */ |
ca3d84cc | 564 | if (*(const uint64_t *)value == 0) { |
2980cc24 | 565 | sr_err("LIMIT_MSEC can't be 0."); |
ca3d84cc BV |
566 | return SR_ERR; |
567 | } | |
301a5e4c | 568 | devc->limit_msec = *(const uint64_t *)value; |
2980cc24 | 569 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
301a5e4c | 570 | devc->limit_msec); |
ca3d84cc BV |
571 | break; |
572 | case SR_HWCAP_LIMIT_SAMPLES: | |
301a5e4c | 573 | devc->limit_samples = *(const uint64_t *)value; |
2980cc24 | 574 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
301a5e4c | 575 | devc->limit_samples); |
ca3d84cc | 576 | break; |
ca3d84cc | 577 | default: |
2980cc24 | 578 | sr_err("Unknown capability: %d.", hwcap); |
ca3d84cc BV |
579 | return SR_ERR; |
580 | break; | |
581 | } | |
582 | ||
583 | return SR_OK; | |
584 | } | |
585 | ||
586 | static int receive_data(int fd, int revents, void *cb_data) | |
587 | { | |
588 | struct sr_dev_inst *sdi; | |
301a5e4c | 589 | struct dev_context *devc; |
ca3d84cc | 590 | |
3c6ce226 BV |
591 | (void)revents; |
592 | ||
ca3d84cc | 593 | if (!(sdi = cb_data)) |
3c6ce226 | 594 | return TRUE; |
ca3d84cc | 595 | |
301a5e4c | 596 | if (!(devc = sdi->priv)) |
3c6ce226 | 597 | return TRUE; |
ca3d84cc | 598 | |
301a5e4c | 599 | switch (devc->profile->transport) { |
ca3d84cc | 600 | case DMM_TRANSPORT_USBHID: |
3c6ce226 BV |
601 | if (devc->profile->chip->data) |
602 | devc->profile->chip->data(sdi); | |
ca3d84cc BV |
603 | break; |
604 | case DMM_TRANSPORT_SERIAL: | |
605 | /* TODO */ | |
3c6ce226 | 606 | fd = fd; |
ca3d84cc BV |
607 | break; |
608 | } | |
609 | ||
3c6ce226 BV |
610 | if (devc->num_samples >= devc->limit_samples) |
611 | hw_dev_acquisition_stop(sdi, cb_data); | |
612 | ||
ca3d84cc BV |
613 | return TRUE; |
614 | } | |
615 | ||
3ffb6964 BV |
616 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
617 | void *cb_data) | |
ca3d84cc BV |
618 | { |
619 | struct sr_datafeed_packet packet; | |
620 | struct sr_datafeed_header header; | |
621 | struct sr_datafeed_meta_analog meta; | |
301a5e4c | 622 | struct dev_context *devc; |
ca3d84cc | 623 | |
301a5e4c | 624 | if (!(devc = sdi->priv)) { |
2980cc24 | 625 | sr_err("sdi->priv was NULL."); |
ca3d84cc BV |
626 | return SR_ERR_BUG; |
627 | } | |
628 | ||
2980cc24 | 629 | sr_dbg("Starting acquisition."); |
ca3d84cc | 630 | |
301a5e4c | 631 | devc->cb_data = cb_data; |
ca3d84cc BV |
632 | |
633 | /* Send header packet to the session bus. */ | |
2980cc24 | 634 | sr_dbg("Sending SR_DF_HEADER."); |
ca3d84cc BV |
635 | packet.type = SR_DF_HEADER; |
636 | packet.payload = (uint8_t *)&header; | |
637 | header.feed_version = 1; | |
638 | gettimeofday(&header.starttime, NULL); | |
301a5e4c | 639 | sr_session_send(devc->cb_data, &packet); |
ca3d84cc BV |
640 | |
641 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
2980cc24 | 642 | sr_dbg("Sending SR_DF_META_ANALOG."); |
ca3d84cc BV |
643 | packet.type = SR_DF_META_ANALOG; |
644 | packet.payload = &meta; | |
645 | meta.num_probes = 1; | |
301a5e4c | 646 | sr_session_send(devc->cb_data, &packet); |
ca3d84cc BV |
647 | |
648 | /* Hook up a proxy handler to receive data from the device. */ | |
301a5e4c | 649 | switch (devc->profile->transport) { |
ca3d84cc | 650 | case DMM_TRANSPORT_USBHID: |
3c6ce226 BV |
651 | /* Callously using stdin here. This works because no G_IO_* flags |
652 | * are set, but will certainly break when any other driver does | |
653 | * this, and runs at the same time as genericdmm. | |
654 | * We'll need a timeout-only source when revamping the whole | |
655 | * driver source system. | |
656 | */ | |
657 | sr_source_add(0, 0, devc->profile->poll_timeout, | |
658 | receive_data, (void *)sdi); | |
ca3d84cc BV |
659 | break; |
660 | case DMM_TRANSPORT_SERIAL: | |
661 | /* TODO serial FD setup */ | |
301a5e4c | 662 | // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi); |
ca3d84cc BV |
663 | break; |
664 | } | |
665 | ||
666 | return SR_OK; | |
667 | } | |
668 | ||
3ffb6964 BV |
669 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, |
670 | void *cb_data) | |
ca3d84cc BV |
671 | { |
672 | struct sr_datafeed_packet packet; | |
673 | ||
674 | /* Avoid compiler warnings. */ | |
3ffb6964 | 675 | (void)sdi; |
ca3d84cc | 676 | |
2980cc24 | 677 | sr_dbg("Stopping acquisition."); |
ca3d84cc BV |
678 | |
679 | /* Send end packet to the session bus. */ | |
2980cc24 | 680 | sr_dbg("Sending SR_DF_END."); |
ca3d84cc BV |
681 | packet.type = SR_DF_END; |
682 | sr_session_send(cb_data, &packet); | |
683 | ||
3c6ce226 BV |
684 | sr_source_remove(0); |
685 | ||
ca3d84cc BV |
686 | return SR_OK; |
687 | } | |
688 | ||
689 | SR_PRIV struct sr_dev_driver genericdmm_driver_info = { | |
690 | .name = "genericdmm", | |
691 | .longname = "Generic DMM", | |
692 | .api_version = 1, | |
693 | .init = hw_init, | |
694 | .cleanup = hw_cleanup, | |
61136ea6 | 695 | .scan = hw_scan, |
811deee4 BV |
696 | .dev_list = hw_dev_list, |
697 | .dev_clear = clear_instances, | |
ca3d84cc BV |
698 | .dev_open = hw_dev_open, |
699 | .dev_close = hw_dev_close, | |
6910bf6b | 700 | .info_get = hw_info_get, |
ca3d84cc BV |
701 | .dev_config_set = hw_dev_config_set, |
702 | .dev_acquisition_start = hw_dev_acquisition_start, | |
703 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
301a5e4c | 704 | .priv = NULL, |
ca3d84cc | 705 | }; |