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