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