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