]> sigrok.org Git - libsigrok.git/blob - hardware/genericdmm/api.c
genericdmm: use new scan API
[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
32 static struct sr_hwopt victor_70c_vidpid[] = {
33         { SR_HWOPT_CONN, "1244.d237" },
34         { 0, NULL }
35 };
36 static struct dev_profile dev_profiles[] = {
37         { "victor-70c", "Victor", "70C", &dmmchip_fs9922,
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 }
42 };
43
44 static const int hwcaps[] = {
45         SR_HWCAP_MULTIMETER,
46         SR_HWCAP_LIMIT_SAMPLES,
47         SR_HWCAP_LIMIT_MSEC,
48         SR_HWCAP_CONTINUOUS,
49         SR_HWOPT_MODEL,
50         SR_HWOPT_CONN,
51         SR_HWOPT_SERIALCOMM,
52         0,
53 };
54
55 static const char *probe_names[] = {
56         "Probe",
57         NULL,
58 };
59
60 SR_PRIV struct sr_dev_driver genericdmm_driver_info;
61 static struct sr_dev_driver *gdi = &genericdmm_driver_info;
62 /* TODO need a way to keep this local to the static library */
63 SR_PRIV libusb_context *genericdmm_usb_context = NULL;
64
65
66 static 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
164 static 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
176 GSList *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
190 static 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
215 static int hw_init(void)
216 {
217
218         if (libusb_init(&genericdmm_usb_context) != 0) {
219                 sr_err("genericdmm: Failed to initialize USB.");
220                 return SR_ERR;
221         }
222
223
224         return SR_OK;
225 }
226
227 static GSList *hw_scan(GSList *options)
228 {
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         }
249
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                 }
257         }
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();
266
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);
272         }
273
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;
310 }
311
312 static int hw_dev_open(int dev_index)
313 {
314         struct sr_dev_inst *sdi;
315         struct context *ctx;
316
317         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
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
350 static int hw_dev_close(int dev_index)
351 {
352         struct sr_dev_inst *sdi;
353         struct context *ctx;
354
355         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
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
383 static 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. */
390         for (l = gdi->instances; l; l = l->next) {
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
418         g_slist_free(gdi->instances);
419         gdi->instances = NULL;
420
421         if (genericdmm_usb_context)
422                 libusb_exit(genericdmm_usb_context);
423
424         return SR_OK;
425 }
426
427 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
428 {
429         struct sr_dev_inst *sdi;
430         struct context *ctx;
431         const void *info;
432
433         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
434                 sr_err("genericdmm: sdi was NULL.");
435                 return NULL;
436         }
437
438         if (!(ctx = sdi->priv)) {
439                 sr_err("genericdmm: sdi->priv was NULL.");
440                 return NULL;
441         }
442
443                 sr_spew("genericdmm: dev_index %d, dev_info_id %d.",
444                                 dev_index, dev_info_id);
445
446         switch (dev_info_id) {
447         case SR_DI_INST:
448                 info = sdi;
449                 sr_spew("genericdmm: Returning sdi.");
450                 break;
451         case SR_DI_NUM_PROBES:
452                 info = GINT_TO_POINTER(1);
453                 sr_spew("genericdmm: Returning number of probes: 1.");
454                 break;
455         case SR_DI_PROBE_NAMES:
456                 info = probe_names;
457                 sr_spew("genericdmm: Returning probenames.");
458                 break;
459         case SR_DI_CUR_SAMPLERATE:
460                 /* TODO get rid of this */
461                 info = NULL;
462                 sr_spew("genericdmm: Returning samplerate: 0.");
463                 break;
464         default:
465                 /* Unknown device info ID. */
466                 sr_err("genericdmm: Unknown device info ID: %d.", dev_info_id);
467                 info = NULL;
468                 break;
469         }
470
471         return info;
472 }
473
474 static int hw_dev_status_get(int dev_index)
475 {
476         struct sr_dev_inst *sdi;
477
478         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
479                 sr_err("genericdmm: sdi was NULL, device not found.");
480                 return SR_ST_NOT_FOUND;
481         }
482
483         sr_dbg("genericdmm: Returning status: %d.", sdi->status);
484
485         return sdi->status;
486 }
487
488 static const int *hw_hwcap_get_all(void)
489 {
490         sr_spew("genericdmm: Returning list of device capabilities.");
491
492         return hwcaps;
493 }
494
495 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
496 {
497         struct sr_dev_inst *sdi;
498         struct context *ctx;
499
500         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
501                 sr_err("genericdmm: sdi was NULL.");
502                 return SR_ERR_BUG;
503         }
504
505         if (!(ctx = sdi->priv)) {
506                 sr_err("genericdmm: sdi->priv was NULL.");
507                 return SR_ERR_BUG;
508         }
509
510         sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
511
512         switch (hwcap) {
513         case SR_HWCAP_LIMIT_MSEC:
514                 if (*(const uint64_t *)value == 0) {
515                         sr_err("genericdmm: LIMIT_MSEC can't be 0.");
516                         return SR_ERR;
517                 }
518                 ctx->limit_msec = *(const uint64_t *)value;
519                 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
520                        ctx->limit_msec);
521                 break;
522         case SR_HWCAP_LIMIT_SAMPLES:
523                 ctx->limit_samples = *(const uint64_t *)value;
524                 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
525                        ctx->limit_samples);
526                 break;
527         default:
528                 sr_err("genericdmm: Unknown capability: %d.", hwcap);
529                 return SR_ERR;
530                 break;
531         }
532
533         return SR_OK;
534 }
535
536 static int receive_data(int fd, int revents, void *cb_data)
537 {
538         struct sr_dev_inst *sdi;
539         struct context *ctx;
540
541         if (!(sdi = cb_data))
542                 return FALSE;
543
544         if (!(ctx = sdi->priv))
545                 return FALSE;
546
547         if (revents != G_IO_IN) {
548                 sr_err("genericdmm: No data?");
549                 return FALSE;
550         }
551
552         switch (ctx->profile->transport) {
553         case DMM_TRANSPORT_USBHID:
554                 /* TODO */
555                 break;
556         case DMM_TRANSPORT_SERIAL:
557                 /* TODO */
558                 break;
559         }
560
561         return TRUE;
562 }
563
564 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
565 {
566         struct sr_datafeed_packet packet;
567         struct sr_datafeed_header header;
568         struct sr_datafeed_meta_analog meta;
569         struct sr_dev_inst *sdi;
570         struct context *ctx;
571
572         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
573                 sr_err("genericdmm: sdi was NULL.");
574                 return SR_ERR_BUG;
575         }
576
577         if (!(ctx = sdi->priv)) {
578                 sr_err("genericdmm: sdi->priv was NULL.");
579                 return SR_ERR_BUG;
580         }
581
582         sr_dbg("genericdmm: Starting acquisition.");
583
584         ctx->cb_data = cb_data;
585
586         /* Send header packet to the session bus. */
587         sr_dbg("genericdmm: Sending SR_DF_HEADER.");
588         packet.type = SR_DF_HEADER;
589         packet.payload = (uint8_t *)&header;
590         header.feed_version = 1;
591         gettimeofday(&header.starttime, NULL);
592         sr_session_send(ctx->cb_data, &packet);
593
594         /* Send metadata about the SR_DF_ANALOG packets to come. */
595         sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
596         packet.type = SR_DF_META_ANALOG;
597         packet.payload = &meta;
598         meta.num_probes = 1;
599         sr_session_send(ctx->cb_data, &packet);
600
601         /* Hook up a proxy handler to receive data from the device. */
602         switch (ctx->profile->transport) {
603         case DMM_TRANSPORT_USBHID:
604                 /* TODO libusb FD setup */
605                 break;
606         case DMM_TRANSPORT_SERIAL:
607                 /* TODO serial FD setup */
608                 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
609                 break;
610         }
611
612         return SR_OK;
613 }
614
615 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
616 {
617         struct sr_datafeed_packet packet;
618
619         /* Avoid compiler warnings. */
620         (void)dev_index;
621
622         sr_dbg("genericdmm: Stopping acquisition.");
623
624         /* Send end packet to the session bus. */
625         sr_dbg("genericdmm: Sending SR_DF_END.");
626         packet.type = SR_DF_END;
627         sr_session_send(cb_data, &packet);
628
629         return SR_OK;
630 }
631
632 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
633         .name = "genericdmm",
634         .longname = "Generic DMM",
635         .api_version = 1,
636         .init = hw_init,
637         .cleanup = hw_cleanup,
638         .scan = hw_scan,
639         .dev_open = hw_dev_open,
640         .dev_close = hw_dev_close,
641         .dev_info_get = hw_dev_info_get,
642         .dev_status_get = hw_dev_status_get,
643         .hwcap_get_all = hw_hwcap_get_all,
644         .dev_config_set = hw_dev_config_set,
645         .dev_acquisition_start = hw_dev_acquisition_start,
646         .dev_acquisition_stop = hw_dev_acquisition_stop,
647         .instances = NULL,
648 };