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