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