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