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