]>
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; | |
b84c13d7 | 31 | extern SR_PRIV struct dmmchip dmmchip_victor70c; |
ca3d84cc | 32 | |
7fc754a0 BV |
33 | static struct sr_hwopt victor_70c_vidpid[] = { |
34 | { SR_HWOPT_CONN, "1244.d237" }, | |
35 | { 0, NULL } | |
36 | }; | |
ca3d84cc | 37 | static struct dev_profile dev_profiles[] = { |
b84c13d7 BV |
38 | { "victor-70c", "Victor", "70C", &dmmchip_victor70c, |
39 | DMM_TRANSPORT_USBHID, 1000, victor_70c_vidpid | |
7fc754a0 | 40 | }, |
3c6ce226 BV |
41 | { "mastech-va18b", "Mastech", "VA18B", NULL, DMM_TRANSPORT_SERIAL, 0, NULL}, |
42 | { NULL, NULL, NULL, NULL, 0, 0, NULL } | |
ca3d84cc BV |
43 | }; |
44 | ||
f1a14ea7 BV |
45 | static const int hwopts[] = { |
46 | SR_HWOPT_MODEL, | |
47 | SR_HWOPT_CONN, | |
48 | SR_HWOPT_SERIALCOMM, | |
49 | 0, | |
50 | }; | |
51 | ||
ca3d84cc BV |
52 | static const int hwcaps[] = { |
53 | SR_HWCAP_MULTIMETER, | |
54 | SR_HWCAP_LIMIT_SAMPLES, | |
55 | SR_HWCAP_LIMIT_MSEC, | |
56 | SR_HWCAP_CONTINUOUS, | |
ca3d84cc BV |
57 | 0, |
58 | }; | |
59 | ||
60 | static const char *probe_names[] = { | |
61 | "Probe", | |
62 | NULL, | |
63 | }; | |
64 | ||
75337758 BV |
65 | SR_PRIV struct sr_dev_driver genericdmm_driver_info; |
66 | static struct sr_dev_driver *gdi = &genericdmm_driver_info; | |
67 | /* TODO need a way to keep this local to the static library */ | |
3c6ce226 BV |
68 | static libusb_context *genericdmm_usb_context = NULL; |
69 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, | |
70 | void *cb_data); | |
ca3d84cc BV |
71 | |
72 | ||
bbb40871 BV |
73 | static GSList *connect_usb(const char *conn) |
74 | { | |
75 | struct sr_dev_inst *sdi; | |
301a5e4c BV |
76 | struct drv_context *drvc; |
77 | struct dev_context *devc; | |
3c6ce226 | 78 | struct sr_probe *probe; |
bbb40871 BV |
79 | libusb_device **devlist; |
80 | struct libusb_device_descriptor des; | |
81 | GSList *devices; | |
82 | GRegex *reg; | |
83 | GMatchInfo *match; | |
84 | int vid, pid, bus, addr, devcnt, err, i; | |
85 | char *mstr; | |
86 | ||
301a5e4c BV |
87 | drvc = gdi->priv; |
88 | ||
bbb40871 BV |
89 | vid = pid = bus = addr = 0; |
90 | reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL); | |
91 | if (g_regex_match(reg, conn, 0, &match)) { | |
92 | /* Extract VID. */ | |
93 | if ((mstr = g_match_info_fetch(match, 1))) | |
94 | vid = strtoul(mstr, NULL, 16); | |
95 | g_free(mstr); | |
96 | ||
97 | /* Extract PID. */ | |
98 | if ((mstr = g_match_info_fetch(match, 2))) | |
99 | pid = strtoul(mstr, NULL, 16); | |
100 | g_free(mstr); | |
101 | } else { | |
102 | g_match_info_unref(match); | |
103 | g_regex_unref(reg); | |
104 | reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL); | |
105 | if (g_regex_match(reg, conn, 0, &match)) { | |
106 | /* Extract bus. */ | |
107 | if ((mstr = g_match_info_fetch(match, 0))) | |
108 | bus = strtoul(mstr, NULL, 16); | |
109 | g_free(mstr); | |
110 | ||
111 | /* Extract address. */ | |
112 | if ((mstr = g_match_info_fetch(match, 0))) | |
113 | addr = strtoul(mstr, NULL, 16); | |
114 | g_free(mstr); | |
115 | } | |
116 | } | |
117 | g_match_info_unref(match); | |
118 | g_regex_unref(reg); | |
119 | ||
120 | if (vid + pid + bus + addr == 0) | |
121 | return NULL; | |
122 | ||
123 | if (bus > 64) { | |
124 | sr_err("invalid bus"); | |
125 | return NULL; | |
126 | } | |
127 | ||
128 | if (addr > 127) { | |
129 | sr_err("invalid address"); | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | /* Looks like a valid USB device specification, but is it connected? */ | |
134 | devices = NULL; | |
135 | libusb_get_device_list(genericdmm_usb_context, &devlist); | |
136 | for (i = 0; devlist[i]; i++) { | |
137 | if ((err = libusb_get_device_descriptor(devlist[i], &des))) { | |
138 | sr_err("genericdmm: failed to get device descriptor: %d", err); | |
139 | continue; | |
140 | } | |
141 | ||
142 | if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) | |
143 | /* VID/PID specified, but no match. */ | |
144 | continue; | |
145 | ||
146 | if (bus + addr && ( | |
147 | libusb_get_bus_number(devlist[i]) != bus | |
148 | || libusb_get_device_address(devlist[i]) != addr)) | |
149 | /* Bus/address specified, but no match. */ | |
150 | continue; | |
151 | ||
152 | /* Found one. */ | |
301a5e4c BV |
153 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
154 | sr_err("genericdmm: devc malloc failed."); | |
bbb40871 BV |
155 | return 0; |
156 | } | |
157 | ||
301a5e4c | 158 | devcnt = g_slist_length(drvc->instances); |
3c6ce226 | 159 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, |
d6db79a4 | 160 | NULL, NULL, NULL))) { |
bbb40871 BV |
161 | sr_err("genericdmm: sr_dev_inst_new returned NULL."); |
162 | return NULL; | |
163 | } | |
301a5e4c | 164 | sdi->priv = devc; |
3c6ce226 BV |
165 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) |
166 | return NULL; | |
167 | sdi->probes = g_slist_append(sdi->probes, probe); | |
301a5e4c | 168 | devc->usb = sr_usb_dev_inst_new( |
bbb40871 BV |
169 | libusb_get_bus_number(devlist[i]), |
170 | libusb_get_device_address(devlist[i]), NULL); | |
171 | devices = g_slist_append(devices, sdi); | |
172 | } | |
173 | libusb_free_device_list(devlist, 1); | |
174 | ||
175 | return devices; | |
176 | } | |
177 | ||
178 | static GSList *connect_serial(const char *conn, const char *serialcomm) | |
179 | { | |
180 | GSList *devices; | |
181 | ||
182 | devices = NULL; | |
183 | ||
184 | /* TODO */ | |
185 | sr_dbg("not yet implemented"); | |
186 | ||
187 | return devices; | |
188 | } | |
189 | ||
190 | GSList *genericdmm_connect(const char *conn, const char *serialcomm) | |
191 | { | |
192 | GSList *devices; | |
193 | ||
194 | if (serialcomm) | |
195 | /* Must be a serial port. */ | |
196 | return connect_serial(conn, serialcomm); | |
197 | ||
198 | if ((devices = connect_usb(conn))) | |
199 | return devices; | |
200 | ||
201 | return NULL; | |
202 | } | |
203 | ||
3a0fe402 BV |
204 | static GSList *default_scan(GSList *options) |
205 | { | |
206 | GSList *l, *devices; | |
207 | struct sr_hwopt *opt; | |
208 | const char *conn, *serialcomm; | |
209 | ||
210 | devices = NULL; | |
211 | conn = serialcomm = NULL; | |
212 | for (l = options; l; l = l->next) { | |
213 | opt = l->data; | |
214 | switch (opt->hwopt) { | |
215 | case SR_HWOPT_CONN: | |
216 | conn = opt->value; | |
217 | break; | |
218 | case SR_HWOPT_SERIALCOMM: | |
219 | serialcomm = opt->value; | |
220 | break; | |
221 | } | |
222 | } | |
223 | if (conn) | |
224 | devices = genericdmm_connect(conn, serialcomm); | |
225 | ||
226 | return devices; | |
227 | } | |
228 | ||
3c6ce226 BV |
229 | static int open_usb(struct sr_dev_inst *sdi) |
230 | { | |
231 | libusb_device **devlist; | |
232 | struct libusb_device_descriptor des; | |
233 | struct dev_context *devc; | |
234 | int ret, tmp, cnt, i; | |
235 | ||
236 | devc = sdi->priv; | |
237 | ||
238 | if (sdi->status == SR_ST_ACTIVE) | |
239 | /* already in use */ | |
240 | return SR_ERR; | |
241 | ||
242 | cnt = libusb_get_device_list(genericdmm_usb_context, &devlist); | |
243 | if (cnt < 0) { | |
244 | sr_err("genericdmm: Failed to retrieve device list (%d)", cnt); | |
245 | return SR_ERR; | |
246 | } | |
247 | ||
248 | ret = SR_ERR; | |
249 | for (i = 0; i < cnt; i++) { | |
250 | if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) { | |
251 | sr_err("genericdmm: Failed to get device descriptor: %d.", tmp); | |
252 | continue; | |
253 | } | |
254 | ||
255 | if (libusb_get_bus_number(devlist[i]) != devc->usb->bus | |
256 | || libusb_get_device_address(devlist[i]) != devc->usb->address) | |
257 | /* this is not the one */ | |
258 | continue; | |
259 | ||
260 | if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) { | |
261 | sr_err("genericdmm: Failed to open device: %d.", tmp); | |
262 | break; | |
263 | } | |
264 | ||
265 | sr_info("genericdmm: Opened device %s on %d.%d ", devc->profile->modelid, | |
266 | devc->usb->bus, devc->usb->address); | |
267 | ret = SR_OK; | |
268 | break; | |
269 | } | |
270 | libusb_free_device_list(devlist, 1); | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
811deee4 BV |
275 | static int clear_instances(void) |
276 | { | |
277 | GSList *l; | |
278 | struct sr_dev_inst *sdi; | |
279 | struct dev_context *devc; | |
280 | struct drv_context *drvc; | |
281 | ||
282 | if (!(drvc = gdi->priv)) | |
283 | return SR_OK; | |
284 | ||
285 | /* Properly close and free all devices. */ | |
286 | for (l = drvc->instances; l; l = l->next) { | |
287 | if (!(sdi = l->data)) { | |
288 | /* Log error, but continue cleaning up the rest. */ | |
289 | sr_err("genericdmm: sdi was NULL, continuing."); | |
290 | continue; | |
291 | } | |
292 | if (!(devc = sdi->priv)) { | |
293 | /* Log error, but continue cleaning up the rest. */ | |
294 | sr_err("genericdmm: sdi->priv was NULL, continuing."); | |
295 | continue; | |
296 | } | |
297 | ||
298 | if (devc->profile) { | |
299 | switch (devc->profile->transport) { | |
300 | case DMM_TRANSPORT_USBHID: | |
811deee4 BV |
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: | |
811deee4 | 467 | sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port); |
301a5e4c BV |
468 | devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); |
469 | if (devc->serial->fd == -1) { | |
ca3d84cc | 470 | sr_err("genericdmm: Couldn't open serial port '%s'.", |
301a5e4c | 471 | devc->serial->port); |
3c6ce226 | 472 | ret = SR_ERR; |
ca3d84cc | 473 | } |
301a5e4c | 474 | // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2); |
ca3d84cc BV |
475 | break; |
476 | default: | |
477 | sr_err("No transport set."); | |
3c6ce226 | 478 | ret = SR_ERR; |
ca3d84cc BV |
479 | } |
480 | ||
3c6ce226 | 481 | return ret; |
ca3d84cc BV |
482 | } |
483 | ||
25a0f108 | 484 | static int hw_dev_close(struct sr_dev_inst *sdi) |
ca3d84cc | 485 | { |
301a5e4c | 486 | struct dev_context *devc; |
ca3d84cc | 487 | |
301a5e4c | 488 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
489 | sr_err("genericdmm: %s: sdi->priv was NULL.", __func__); |
490 | return SR_ERR_BUG; | |
491 | } | |
492 | ||
301a5e4c | 493 | switch (devc->profile->transport) { |
ca3d84cc BV |
494 | case DMM_TRANSPORT_USBHID: |
495 | /* TODO */ | |
496 | break; | |
497 | case DMM_TRANSPORT_SERIAL: | |
301a5e4c BV |
498 | if (devc->serial && devc->serial->fd != -1) { |
499 | serial_close(devc->serial->fd); | |
500 | devc->serial->fd = -1; | |
ca3d84cc BV |
501 | sdi->status = SR_ST_INACTIVE; |
502 | } | |
503 | break; | |
504 | } | |
505 | ||
506 | return SR_OK; | |
507 | } | |
508 | ||
509 | static int hw_cleanup(void) | |
510 | { | |
ca3d84cc | 511 | |
811deee4 | 512 | clear_instances(); |
ca3d84cc BV |
513 | |
514 | if (genericdmm_usb_context) | |
515 | libusb_exit(genericdmm_usb_context); | |
516 | ||
517 | return SR_OK; | |
518 | } | |
519 | ||
c4a1de59 | 520 | static int hw_info_get(int info_id, const void **data, |
6910bf6b | 521 | const struct sr_dev_inst *sdi) |
ca3d84cc | 522 | { |
301a5e4c | 523 | struct dev_context *devc; |
ca3d84cc | 524 | |
6f57fd96 | 525 | (void)sdi; |
301a5e4c | 526 | (void)devc; |
6f57fd96 | 527 | |
c4a1de59 | 528 | switch (info_id) { |
f1a14ea7 BV |
529 | case SR_DI_HWOPTS: |
530 | *data = hwopts; | |
531 | break; | |
532 | case SR_DI_HWCAPS: | |
533 | *data = hwcaps; | |
534 | break; | |
ca3d84cc | 535 | case SR_DI_NUM_PROBES: |
6910bf6b | 536 | *data = GINT_TO_POINTER(1); |
ca3d84cc BV |
537 | break; |
538 | case SR_DI_PROBE_NAMES: | |
6910bf6b | 539 | *data = probe_names; |
ca3d84cc BV |
540 | break; |
541 | case SR_DI_CUR_SAMPLERATE: | |
542 | /* TODO get rid of this */ | |
6910bf6b | 543 | *data = NULL; |
3c6ce226 | 544 | return SR_ERR_ARG; |
ca3d84cc BV |
545 | break; |
546 | default: | |
547 | /* Unknown device info ID. */ | |
6910bf6b | 548 | return SR_ERR_ARG; |
ca3d84cc BV |
549 | } |
550 | ||
6910bf6b | 551 | return SR_OK; |
ca3d84cc BV |
552 | } |
553 | ||
6f4b1868 BV |
554 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, |
555 | const void *value) | |
ca3d84cc | 556 | { |
301a5e4c | 557 | struct dev_context *devc; |
ca3d84cc | 558 | |
301a5e4c | 559 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
560 | sr_err("genericdmm: sdi->priv was NULL."); |
561 | return SR_ERR_BUG; | |
562 | } | |
563 | ||
ca3d84cc BV |
564 | switch (hwcap) { |
565 | case SR_HWCAP_LIMIT_MSEC: | |
3c6ce226 | 566 | /* TODO: not yet implemented */ |
ca3d84cc BV |
567 | if (*(const uint64_t *)value == 0) { |
568 | sr_err("genericdmm: LIMIT_MSEC can't be 0."); | |
569 | return SR_ERR; | |
570 | } | |
301a5e4c | 571 | devc->limit_msec = *(const uint64_t *)value; |
3c6ce226 | 572 | sr_dbg("genericdmm: Setting time limit to %" PRIu64 "ms.", |
301a5e4c | 573 | devc->limit_msec); |
ca3d84cc BV |
574 | break; |
575 | case SR_HWCAP_LIMIT_SAMPLES: | |
301a5e4c | 576 | devc->limit_samples = *(const uint64_t *)value; |
3c6ce226 | 577 | sr_dbg("genericdmm: Setting sample limit to %" PRIu64 ".", |
301a5e4c | 578 | devc->limit_samples); |
ca3d84cc | 579 | break; |
ca3d84cc BV |
580 | default: |
581 | sr_err("genericdmm: Unknown capability: %d.", hwcap); | |
582 | return SR_ERR; | |
583 | break; | |
584 | } | |
585 | ||
586 | return SR_OK; | |
587 | } | |
588 | ||
589 | static int receive_data(int fd, int revents, void *cb_data) | |
590 | { | |
591 | struct sr_dev_inst *sdi; | |
301a5e4c | 592 | struct dev_context *devc; |
ca3d84cc | 593 | |
3c6ce226 BV |
594 | (void)revents; |
595 | ||
ca3d84cc | 596 | if (!(sdi = cb_data)) |
3c6ce226 | 597 | return TRUE; |
ca3d84cc | 598 | |
301a5e4c | 599 | if (!(devc = sdi->priv)) |
3c6ce226 | 600 | return TRUE; |
ca3d84cc | 601 | |
301a5e4c | 602 | switch (devc->profile->transport) { |
ca3d84cc | 603 | case DMM_TRANSPORT_USBHID: |
3c6ce226 BV |
604 | if (devc->profile->chip->data) |
605 | devc->profile->chip->data(sdi); | |
ca3d84cc BV |
606 | break; |
607 | case DMM_TRANSPORT_SERIAL: | |
608 | /* TODO */ | |
3c6ce226 | 609 | fd = fd; |
ca3d84cc BV |
610 | break; |
611 | } | |
612 | ||
3c6ce226 BV |
613 | if (devc->num_samples >= devc->limit_samples) |
614 | hw_dev_acquisition_stop(sdi, cb_data); | |
615 | ||
ca3d84cc BV |
616 | return TRUE; |
617 | } | |
618 | ||
3ffb6964 BV |
619 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
620 | void *cb_data) | |
ca3d84cc BV |
621 | { |
622 | struct sr_datafeed_packet packet; | |
623 | struct sr_datafeed_header header; | |
624 | struct sr_datafeed_meta_analog meta; | |
301a5e4c | 625 | struct dev_context *devc; |
ca3d84cc | 626 | |
301a5e4c | 627 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
628 | sr_err("genericdmm: sdi->priv was NULL."); |
629 | return SR_ERR_BUG; | |
630 | } | |
631 | ||
632 | sr_dbg("genericdmm: Starting acquisition."); | |
633 | ||
301a5e4c | 634 | devc->cb_data = cb_data; |
ca3d84cc BV |
635 | |
636 | /* Send header packet to the session bus. */ | |
637 | sr_dbg("genericdmm: Sending SR_DF_HEADER."); | |
638 | packet.type = SR_DF_HEADER; | |
639 | packet.payload = (uint8_t *)&header; | |
640 | header.feed_version = 1; | |
641 | gettimeofday(&header.starttime, NULL); | |
301a5e4c | 642 | sr_session_send(devc->cb_data, &packet); |
ca3d84cc BV |
643 | |
644 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
645 | sr_dbg("genericdmm: Sending SR_DF_META_ANALOG."); | |
646 | packet.type = SR_DF_META_ANALOG; | |
647 | packet.payload = &meta; | |
648 | meta.num_probes = 1; | |
301a5e4c | 649 | sr_session_send(devc->cb_data, &packet); |
ca3d84cc BV |
650 | |
651 | /* Hook up a proxy handler to receive data from the device. */ | |
301a5e4c | 652 | switch (devc->profile->transport) { |
ca3d84cc | 653 | case DMM_TRANSPORT_USBHID: |
3c6ce226 BV |
654 | /* Callously using stdin here. This works because no G_IO_* flags |
655 | * are set, but will certainly break when any other driver does | |
656 | * this, and runs at the same time as genericdmm. | |
657 | * We'll need a timeout-only source when revamping the whole | |
658 | * driver source system. | |
659 | */ | |
660 | sr_source_add(0, 0, devc->profile->poll_timeout, | |
661 | receive_data, (void *)sdi); | |
ca3d84cc BV |
662 | break; |
663 | case DMM_TRANSPORT_SERIAL: | |
664 | /* TODO serial FD setup */ | |
301a5e4c | 665 | // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi); |
ca3d84cc BV |
666 | break; |
667 | } | |
668 | ||
669 | return SR_OK; | |
670 | } | |
671 | ||
3ffb6964 BV |
672 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, |
673 | void *cb_data) | |
ca3d84cc BV |
674 | { |
675 | struct sr_datafeed_packet packet; | |
676 | ||
677 | /* Avoid compiler warnings. */ | |
3ffb6964 | 678 | (void)sdi; |
ca3d84cc BV |
679 | |
680 | sr_dbg("genericdmm: Stopping acquisition."); | |
681 | ||
682 | /* Send end packet to the session bus. */ | |
683 | sr_dbg("genericdmm: Sending SR_DF_END."); | |
684 | packet.type = SR_DF_END; | |
685 | sr_session_send(cb_data, &packet); | |
686 | ||
3c6ce226 BV |
687 | sr_source_remove(0); |
688 | ||
ca3d84cc BV |
689 | return SR_OK; |
690 | } | |
691 | ||
692 | SR_PRIV struct sr_dev_driver genericdmm_driver_info = { | |
693 | .name = "genericdmm", | |
694 | .longname = "Generic DMM", | |
695 | .api_version = 1, | |
696 | .init = hw_init, | |
697 | .cleanup = hw_cleanup, | |
61136ea6 | 698 | .scan = hw_scan, |
811deee4 BV |
699 | .dev_list = hw_dev_list, |
700 | .dev_clear = clear_instances, | |
ca3d84cc BV |
701 | .dev_open = hw_dev_open, |
702 | .dev_close = hw_dev_close, | |
6910bf6b | 703 | .info_get = hw_info_get, |
ca3d84cc BV |
704 | .dev_config_set = hw_dev_config_set, |
705 | .dev_acquisition_start = hw_dev_acquisition_start, | |
706 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
301a5e4c | 707 | .priv = NULL, |
ca3d84cc | 708 | }; |