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