]>
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: | |
301 | /* TODO */ | |
302 | sr_usb_dev_inst_free(devc->usb); | |
303 | break; | |
304 | case DMM_TRANSPORT_SERIAL: | |
305 | if (devc->serial && devc->serial->fd != -1) | |
306 | serial_close(devc->serial->fd); | |
307 | sr_serial_dev_inst_free(devc->serial); | |
308 | break; | |
309 | } | |
310 | } | |
311 | ||
312 | sr_dev_inst_free(sdi); | |
313 | } | |
314 | ||
315 | g_slist_free(drvc->instances); | |
316 | drvc->instances = NULL; | |
317 | ||
318 | return SR_OK; | |
319 | } | |
320 | ||
40dda2c3 | 321 | static int hw_init(void) |
ca3d84cc | 322 | { |
301a5e4c BV |
323 | struct drv_context *drvc; |
324 | ||
325 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
326 | sr_err("genericdmm: driver context malloc failed."); | |
327 | return SR_ERR; | |
328 | } | |
ca3d84cc | 329 | |
ca3d84cc BV |
330 | if (libusb_init(&genericdmm_usb_context) != 0) { |
331 | sr_err("genericdmm: Failed to initialize USB."); | |
61136ea6 | 332 | return SR_ERR; |
ca3d84cc BV |
333 | } |
334 | ||
301a5e4c | 335 | gdi->priv = drvc; |
61136ea6 BV |
336 | |
337 | return SR_OK; | |
338 | } | |
339 | ||
3a0fe402 | 340 | static GSList *hw_scan(GSList *options) |
61136ea6 | 341 | { |
3a0fe402 BV |
342 | GSList *l, *ldef, *defopts, *newopts, *devices; |
343 | struct sr_hwopt *opt, *defopt; | |
344 | struct dev_profile *pr, *profile; | |
d6db79a4 | 345 | struct sr_dev_inst *sdi; |
301a5e4c | 346 | struct drv_context *drvc; |
3c6ce226 | 347 | struct dev_context *devc; |
3a0fe402 BV |
348 | const char *model; |
349 | ||
301a5e4c BV |
350 | drvc = gdi->priv; |
351 | ||
3a0fe402 BV |
352 | /* Separate model from the options list. */ |
353 | model = NULL; | |
354 | newopts = NULL; | |
355 | for (l = options; l; l = l->next) { | |
356 | opt = l->data; | |
357 | if (opt->hwopt == SR_HWOPT_MODEL) | |
358 | model = opt->value; | |
359 | else | |
360 | /* New list with references to the original data. */ | |
361 | newopts = g_slist_append(newopts, opt); | |
362 | } | |
363 | if (!model) { | |
d6db79a4 | 364 | /* This driver only works when a model is specified. */ |
3a0fe402 BV |
365 | return NULL; |
366 | } | |
61136ea6 | 367 | |
3a0fe402 BV |
368 | /* Find a profile with this model name. */ |
369 | profile = NULL; | |
370 | for (pr = dev_profiles; pr->modelid; pr++) { | |
371 | if (!strcmp(pr->modelid, model)) { | |
372 | profile = pr; | |
373 | break; | |
374 | } | |
ca3d84cc | 375 | } |
3a0fe402 BV |
376 | if (!profile) { |
377 | sr_err("Unknown model %s.", model); | |
378 | return NULL; | |
379 | } | |
380 | ||
381 | /* Initialize the DMM chip driver. */ | |
382 | if (profile->chip->init) | |
383 | profile->chip->init(); | |
ca3d84cc | 384 | |
3a0fe402 BV |
385 | /* Convert the profile's default options list to a GSList. */ |
386 | defopts = NULL; | |
387 | for (opt = profile->defaults_opts; opt->hwopt; opt++) { | |
388 | /* New list with references to const data in the profile. */ | |
389 | defopts = g_slist_append(defopts, opt); | |
ca3d84cc | 390 | } |
ca3d84cc | 391 | |
3a0fe402 BV |
392 | /* Options given as argument to this function override the |
393 | * profile's default options. | |
394 | */ | |
395 | for (ldef = defopts; ldef; ldef = ldef->next) { | |
396 | defopt = ldef->data; | |
397 | for (l = newopts; l; l = l->next) { | |
398 | opt = l->data; | |
399 | if (opt->hwopt == defopt->hwopt) { | |
400 | /* Override the default, and drop it from the | |
401 | * options list. | |
402 | */ | |
403 | ldef->data = l->data; | |
404 | newopts = g_slist_remove(newopts, opt); | |
405 | break; | |
406 | } | |
407 | } | |
408 | } | |
409 | /* Whatever is left in newopts wasn't in the default options. */ | |
410 | defopts = g_slist_concat(defopts, newopts); | |
411 | g_slist_free(newopts); | |
412 | ||
413 | if (profile->chip->scan) | |
414 | /* The DMM chip driver wants to do its own scanning. */ | |
415 | devices = profile->chip->scan(defopts); | |
416 | else | |
417 | devices = default_scan(defopts); | |
418 | g_slist_free(defopts); | |
419 | ||
420 | if (devices) { | |
421 | /* TODO: need to fix up sdi->index fields */ | |
d6db79a4 BV |
422 | for (l = devices; l; l = l->next) { |
423 | /* The default connection-based scanner doesn't really | |
424 | * know about profiles, so it never filled in the vendor | |
425 | * or model. Do that now. | |
426 | */ | |
427 | sdi = l->data; | |
3c6ce226 BV |
428 | devc = sdi->priv; |
429 | devc->profile = profile; | |
d6db79a4 BV |
430 | sdi->driver = gdi; |
431 | if (!sdi->vendor) | |
432 | sdi->vendor = g_strdup(profile->vendor); | |
433 | if (!sdi->model) | |
434 | sdi->model = g_strdup(profile->model); | |
435 | /* Add a copy of these new devices to the driver instances. */ | |
301a5e4c | 436 | drvc->instances = g_slist_append(drvc->instances, l->data); |
d6db79a4 | 437 | } |
3a0fe402 BV |
438 | } |
439 | ||
440 | return devices; | |
ca3d84cc BV |
441 | } |
442 | ||
811deee4 BV |
443 | static GSList *hw_dev_list(void) |
444 | { | |
445 | struct drv_context *drvc; | |
446 | ||
447 | drvc = gdi->priv; | |
448 | ||
449 | return drvc->instances; | |
450 | } | |
451 | ||
25a0f108 | 452 | static int hw_dev_open(struct sr_dev_inst *sdi) |
ca3d84cc | 453 | { |
301a5e4c | 454 | struct dev_context *devc; |
3c6ce226 | 455 | int ret; |
ca3d84cc | 456 | |
301a5e4c | 457 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
458 | sr_err("genericdmm: sdi->priv was NULL."); |
459 | return SR_ERR_BUG; | |
460 | } | |
461 | ||
3c6ce226 | 462 | ret = SR_OK; |
301a5e4c | 463 | switch (devc->profile->transport) { |
ca3d84cc | 464 | case DMM_TRANSPORT_USBHID: |
3c6ce226 | 465 | ret = open_usb(sdi); |
ca3d84cc BV |
466 | break; |
467 | case DMM_TRANSPORT_SERIAL: | |
468 | /* TODO: O_NONBLOCK? */ | |
811deee4 | 469 | sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port); |
301a5e4c BV |
470 | devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); |
471 | if (devc->serial->fd == -1) { | |
ca3d84cc | 472 | sr_err("genericdmm: Couldn't open serial port '%s'.", |
301a5e4c | 473 | devc->serial->port); |
3c6ce226 | 474 | ret = SR_ERR; |
ca3d84cc | 475 | } |
301a5e4c | 476 | // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2); |
ca3d84cc BV |
477 | break; |
478 | default: | |
479 | sr_err("No transport set."); | |
3c6ce226 | 480 | ret = SR_ERR; |
ca3d84cc BV |
481 | } |
482 | ||
3c6ce226 | 483 | return ret; |
ca3d84cc BV |
484 | } |
485 | ||
25a0f108 | 486 | static int hw_dev_close(struct sr_dev_inst *sdi) |
ca3d84cc | 487 | { |
301a5e4c | 488 | struct dev_context *devc; |
ca3d84cc | 489 | |
301a5e4c | 490 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
491 | sr_err("genericdmm: %s: sdi->priv was NULL.", __func__); |
492 | return SR_ERR_BUG; | |
493 | } | |
494 | ||
301a5e4c | 495 | switch (devc->profile->transport) { |
ca3d84cc BV |
496 | case DMM_TRANSPORT_USBHID: |
497 | /* TODO */ | |
498 | break; | |
499 | case DMM_TRANSPORT_SERIAL: | |
301a5e4c BV |
500 | if (devc->serial && devc->serial->fd != -1) { |
501 | serial_close(devc->serial->fd); | |
502 | devc->serial->fd = -1; | |
ca3d84cc BV |
503 | sdi->status = SR_ST_INACTIVE; |
504 | } | |
505 | break; | |
506 | } | |
507 | ||
508 | return SR_OK; | |
509 | } | |
510 | ||
511 | static int hw_cleanup(void) | |
512 | { | |
ca3d84cc | 513 | |
811deee4 | 514 | clear_instances(); |
ca3d84cc BV |
515 | |
516 | if (genericdmm_usb_context) | |
517 | libusb_exit(genericdmm_usb_context); | |
518 | ||
519 | return SR_OK; | |
520 | } | |
521 | ||
c4a1de59 | 522 | static int hw_info_get(int info_id, const void **data, |
6910bf6b | 523 | const struct sr_dev_inst *sdi) |
ca3d84cc | 524 | { |
301a5e4c | 525 | struct dev_context *devc; |
ca3d84cc | 526 | |
6f57fd96 | 527 | (void)sdi; |
301a5e4c | 528 | (void)devc; |
6f57fd96 | 529 | |
c4a1de59 | 530 | switch (info_id) { |
f1a14ea7 BV |
531 | case SR_DI_HWOPTS: |
532 | *data = hwopts; | |
533 | break; | |
534 | case SR_DI_HWCAPS: | |
535 | *data = hwcaps; | |
536 | break; | |
ca3d84cc | 537 | case SR_DI_NUM_PROBES: |
6910bf6b | 538 | *data = GINT_TO_POINTER(1); |
ca3d84cc BV |
539 | break; |
540 | case SR_DI_PROBE_NAMES: | |
6910bf6b | 541 | *data = probe_names; |
ca3d84cc BV |
542 | break; |
543 | case SR_DI_CUR_SAMPLERATE: | |
544 | /* TODO get rid of this */ | |
6910bf6b | 545 | *data = NULL; |
3c6ce226 | 546 | return SR_ERR_ARG; |
ca3d84cc BV |
547 | break; |
548 | default: | |
549 | /* Unknown device info ID. */ | |
6910bf6b | 550 | return SR_ERR_ARG; |
ca3d84cc BV |
551 | } |
552 | ||
6910bf6b | 553 | return SR_OK; |
ca3d84cc BV |
554 | } |
555 | ||
6f4b1868 BV |
556 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, |
557 | const void *value) | |
ca3d84cc | 558 | { |
301a5e4c | 559 | struct dev_context *devc; |
ca3d84cc | 560 | |
301a5e4c | 561 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
562 | sr_err("genericdmm: sdi->priv was NULL."); |
563 | return SR_ERR_BUG; | |
564 | } | |
565 | ||
ca3d84cc BV |
566 | switch (hwcap) { |
567 | case SR_HWCAP_LIMIT_MSEC: | |
3c6ce226 | 568 | /* TODO: not yet implemented */ |
ca3d84cc BV |
569 | if (*(const uint64_t *)value == 0) { |
570 | sr_err("genericdmm: LIMIT_MSEC can't be 0."); | |
571 | return SR_ERR; | |
572 | } | |
301a5e4c | 573 | devc->limit_msec = *(const uint64_t *)value; |
3c6ce226 | 574 | sr_dbg("genericdmm: Setting time limit to %" PRIu64 "ms.", |
301a5e4c | 575 | devc->limit_msec); |
ca3d84cc BV |
576 | break; |
577 | case SR_HWCAP_LIMIT_SAMPLES: | |
301a5e4c | 578 | devc->limit_samples = *(const uint64_t *)value; |
3c6ce226 | 579 | sr_dbg("genericdmm: Setting sample limit to %" PRIu64 ".", |
301a5e4c | 580 | devc->limit_samples); |
ca3d84cc | 581 | break; |
ca3d84cc BV |
582 | default: |
583 | sr_err("genericdmm: Unknown capability: %d.", hwcap); | |
584 | return SR_ERR; | |
585 | break; | |
586 | } | |
587 | ||
588 | return SR_OK; | |
589 | } | |
590 | ||
591 | static int receive_data(int fd, int revents, void *cb_data) | |
592 | { | |
593 | struct sr_dev_inst *sdi; | |
301a5e4c | 594 | struct dev_context *devc; |
ca3d84cc | 595 | |
3c6ce226 BV |
596 | (void)revents; |
597 | ||
ca3d84cc | 598 | if (!(sdi = cb_data)) |
3c6ce226 | 599 | return TRUE; |
ca3d84cc | 600 | |
301a5e4c | 601 | if (!(devc = sdi->priv)) |
3c6ce226 | 602 | return TRUE; |
ca3d84cc | 603 | |
301a5e4c | 604 | switch (devc->profile->transport) { |
ca3d84cc | 605 | case DMM_TRANSPORT_USBHID: |
3c6ce226 BV |
606 | if (devc->profile->chip->data) |
607 | devc->profile->chip->data(sdi); | |
ca3d84cc BV |
608 | break; |
609 | case DMM_TRANSPORT_SERIAL: | |
610 | /* TODO */ | |
3c6ce226 | 611 | fd = fd; |
ca3d84cc BV |
612 | break; |
613 | } | |
614 | ||
3c6ce226 BV |
615 | if (devc->num_samples >= devc->limit_samples) |
616 | hw_dev_acquisition_stop(sdi, cb_data); | |
617 | ||
ca3d84cc BV |
618 | return TRUE; |
619 | } | |
620 | ||
3ffb6964 BV |
621 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
622 | void *cb_data) | |
ca3d84cc BV |
623 | { |
624 | struct sr_datafeed_packet packet; | |
625 | struct sr_datafeed_header header; | |
626 | struct sr_datafeed_meta_analog meta; | |
301a5e4c | 627 | struct dev_context *devc; |
ca3d84cc | 628 | |
301a5e4c | 629 | if (!(devc = sdi->priv)) { |
ca3d84cc BV |
630 | sr_err("genericdmm: sdi->priv was NULL."); |
631 | return SR_ERR_BUG; | |
632 | } | |
633 | ||
634 | sr_dbg("genericdmm: Starting acquisition."); | |
635 | ||
301a5e4c | 636 | devc->cb_data = cb_data; |
ca3d84cc BV |
637 | |
638 | /* Send header packet to the session bus. */ | |
639 | sr_dbg("genericdmm: Sending SR_DF_HEADER."); | |
640 | packet.type = SR_DF_HEADER; | |
641 | packet.payload = (uint8_t *)&header; | |
642 | header.feed_version = 1; | |
643 | gettimeofday(&header.starttime, NULL); | |
301a5e4c | 644 | sr_session_send(devc->cb_data, &packet); |
ca3d84cc BV |
645 | |
646 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
647 | sr_dbg("genericdmm: Sending SR_DF_META_ANALOG."); | |
648 | packet.type = SR_DF_META_ANALOG; | |
649 | packet.payload = &meta; | |
650 | meta.num_probes = 1; | |
301a5e4c | 651 | sr_session_send(devc->cb_data, &packet); |
ca3d84cc BV |
652 | |
653 | /* Hook up a proxy handler to receive data from the device. */ | |
301a5e4c | 654 | switch (devc->profile->transport) { |
ca3d84cc | 655 | case DMM_TRANSPORT_USBHID: |
3c6ce226 BV |
656 | /* Callously using stdin here. This works because no G_IO_* flags |
657 | * are set, but will certainly break when any other driver does | |
658 | * this, and runs at the same time as genericdmm. | |
659 | * We'll need a timeout-only source when revamping the whole | |
660 | * driver source system. | |
661 | */ | |
662 | sr_source_add(0, 0, devc->profile->poll_timeout, | |
663 | receive_data, (void *)sdi); | |
ca3d84cc BV |
664 | break; |
665 | case DMM_TRANSPORT_SERIAL: | |
666 | /* TODO serial FD setup */ | |
301a5e4c | 667 | // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi); |
ca3d84cc BV |
668 | break; |
669 | } | |
670 | ||
671 | return SR_OK; | |
672 | } | |
673 | ||
3ffb6964 BV |
674 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, |
675 | void *cb_data) | |
ca3d84cc BV |
676 | { |
677 | struct sr_datafeed_packet packet; | |
678 | ||
679 | /* Avoid compiler warnings. */ | |
3ffb6964 | 680 | (void)sdi; |
ca3d84cc BV |
681 | |
682 | sr_dbg("genericdmm: Stopping acquisition."); | |
683 | ||
684 | /* Send end packet to the session bus. */ | |
685 | sr_dbg("genericdmm: Sending SR_DF_END."); | |
686 | packet.type = SR_DF_END; | |
687 | sr_session_send(cb_data, &packet); | |
688 | ||
3c6ce226 BV |
689 | sr_source_remove(0); |
690 | ||
ca3d84cc BV |
691 | return SR_OK; |
692 | } | |
693 | ||
694 | SR_PRIV struct sr_dev_driver genericdmm_driver_info = { | |
695 | .name = "genericdmm", | |
696 | .longname = "Generic DMM", | |
697 | .api_version = 1, | |
698 | .init = hw_init, | |
699 | .cleanup = hw_cleanup, | |
61136ea6 | 700 | .scan = hw_scan, |
811deee4 BV |
701 | .dev_list = hw_dev_list, |
702 | .dev_clear = clear_instances, | |
ca3d84cc BV |
703 | .dev_open = hw_dev_open, |
704 | .dev_close = hw_dev_close, | |
6910bf6b | 705 | .info_get = hw_info_get, |
ca3d84cc BV |
706 | .dev_config_set = hw_dev_config_set, |
707 | .dev_acquisition_start = hw_dev_acquisition_start, | |
708 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
301a5e4c | 709 | .priv = NULL, |
ca3d84cc | 710 | }; |