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