]> sigrok.org Git - libsigrok.git/blob - hardware/genericdmm/api.c
genericdmm: more flexible device discovery
[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 int hw_init(void)
191 {
192
193         if (libusb_init(&genericdmm_usb_context) != 0) {
194                 sr_err("genericdmm: Failed to initialize USB.");
195                 return SR_ERR;
196         }
197
198
199         return SR_OK;
200 }
201
202 static int hw_scan(void)
203 {
204         struct sr_dev_inst *sdi;
205         struct context *ctx;
206         int devcnt = 0;
207
208         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
209                 sr_err("genericdmm: ctx malloc failed.");
210                 return 0;
211         }
212
213         devcnt = g_slist_length(genericdmm_dev_insts);
214         if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, "Generic DMM",
215                         NULL, NULL))) {
216                 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
217                 return 0;
218         }
219         sdi->priv = ctx;
220         genericdmm_dev_insts = g_slist_append(genericdmm_dev_insts, sdi);
221
222         /* Always initialized just one device instance. */
223         return 0;
224 }
225
226 static int hw_dev_open(int dev_index)
227 {
228         struct sr_dev_inst *sdi;
229         struct context *ctx;
230
231         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
232                 sr_err("genericdmm: sdi was NULL.");
233                 return SR_ERR_BUG;
234         }
235
236         if (!(ctx = sdi->priv)) {
237                 sr_err("genericdmm: sdi->priv was NULL.");
238                 return SR_ERR_BUG;
239         }
240
241         sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
242
243         switch (ctx->profile->transport) {
244         case DMM_TRANSPORT_USBHID:
245                 /* TODO */
246                 break;
247         case DMM_TRANSPORT_SERIAL:
248                 /* TODO: O_NONBLOCK? */
249                 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
250                 if (ctx->serial->fd == -1) {
251                         sr_err("genericdmm: Couldn't open serial port '%s'.",
252                                ctx->serial->port);
253                         return SR_ERR;
254                 }
255                 //      serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
256                 break;
257         default:
258                 sr_err("No transport set.");
259         }
260
261         return SR_OK;
262 }
263
264 static int hw_dev_close(int dev_index)
265 {
266         struct sr_dev_inst *sdi;
267         struct context *ctx;
268
269         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
270                 sr_err("genericdmm: %s: sdi was NULL.", __func__);
271                 return SR_ERR_BUG;
272         }
273
274         if (!(ctx = sdi->priv)) {
275                 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
276                 return SR_ERR_BUG;
277         }
278
279         /* TODO: Check for != NULL. */
280
281         switch (ctx->profile->transport) {
282         case DMM_TRANSPORT_USBHID:
283                 /* TODO */
284                 break;
285         case DMM_TRANSPORT_SERIAL:
286                 if (ctx->serial && ctx->serial->fd != -1) {
287                         serial_close(ctx->serial->fd);
288                         ctx->serial->fd = -1;
289                         sdi->status = SR_ST_INACTIVE;
290                 }
291                 break;
292         }
293
294         return SR_OK;
295 }
296
297 static int hw_cleanup(void)
298 {
299         GSList *l;
300         struct sr_dev_inst *sdi;
301         struct context *ctx;
302
303         /* Properly close and free all devices. */
304         for (l = gdi->instances; l; l = l->next) {
305                 if (!(sdi = l->data)) {
306                         /* Log error, but continue cleaning up the rest. */
307                         sr_err("genericdmm: sdi was NULL, continuing.");
308                         continue;
309                 }
310                 if (!(ctx = sdi->priv)) {
311                         /* Log error, but continue cleaning up the rest. */
312                         sr_err("genericdmm: sdi->priv was NULL, continuing.");
313                         continue;
314                 }
315
316                 if (ctx->profile) {
317                         switch (ctx->profile->transport) {
318                         case DMM_TRANSPORT_USBHID:
319                                 /* TODO */
320                                 break;
321                         case DMM_TRANSPORT_SERIAL:
322                                 if (ctx->serial && ctx->serial->fd != -1)
323                                         serial_close(ctx->serial->fd);
324                                 sr_serial_dev_inst_free(ctx->serial);
325                                 break;
326                         }
327                 }
328
329                 sr_dev_inst_free(sdi);
330         }
331
332         g_slist_free(gdi->instances);
333         gdi->instances = NULL;
334
335         if (genericdmm_usb_context)
336                 libusb_exit(genericdmm_usb_context);
337
338         return SR_OK;
339 }
340
341 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
342 {
343         struct sr_dev_inst *sdi;
344         struct context *ctx;
345         const void *info;
346
347         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
348                 sr_err("genericdmm: sdi was NULL.");
349                 return NULL;
350         }
351
352         if (!(ctx = sdi->priv)) {
353                 sr_err("genericdmm: sdi->priv was NULL.");
354                 return NULL;
355         }
356
357                 sr_spew("genericdmm: dev_index %d, dev_info_id %d.",
358                                 dev_index, dev_info_id);
359
360         switch (dev_info_id) {
361         case SR_DI_INST:
362                 info = sdi;
363                 sr_spew("genericdmm: Returning sdi.");
364                 break;
365         case SR_DI_NUM_PROBES:
366                 info = GINT_TO_POINTER(1);
367                 sr_spew("genericdmm: Returning number of probes: 1.");
368                 break;
369         case SR_DI_PROBE_NAMES:
370                 info = probe_names;
371                 sr_spew("genericdmm: Returning probenames.");
372                 break;
373         case SR_DI_CUR_SAMPLERATE:
374                 /* TODO get rid of this */
375                 info = NULL;
376                 sr_spew("genericdmm: Returning samplerate: 0.");
377                 break;
378         default:
379                 /* Unknown device info ID. */
380                 sr_err("genericdmm: Unknown device info ID: %d.", dev_info_id);
381                 info = NULL;
382                 break;
383         }
384
385         return info;
386 }
387
388 static int hw_dev_status_get(int dev_index)
389 {
390         struct sr_dev_inst *sdi;
391
392         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
393                 sr_err("genericdmm: sdi was NULL, device not found.");
394                 return SR_ST_NOT_FOUND;
395         }
396
397         sr_dbg("genericdmm: Returning status: %d.", sdi->status);
398
399         return sdi->status;
400 }
401
402 static const int *hw_hwcap_get_all(void)
403 {
404         sr_spew("genericdmm: Returning list of device capabilities.");
405
406         return hwcaps;
407 }
408
409 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
410 {
411         struct sr_dev_inst *sdi;
412         struct context *ctx;
413
414         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
415                 sr_err("genericdmm: sdi was NULL.");
416                 return SR_ERR_BUG;
417         }
418
419         if (!(ctx = sdi->priv)) {
420                 sr_err("genericdmm: sdi->priv was NULL.");
421                 return SR_ERR_BUG;
422         }
423
424         sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
425
426         switch (hwcap) {
427         case SR_HWCAP_LIMIT_MSEC:
428                 if (*(const uint64_t *)value == 0) {
429                         sr_err("genericdmm: LIMIT_MSEC can't be 0.");
430                         return SR_ERR;
431                 }
432                 ctx->limit_msec = *(const uint64_t *)value;
433                 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
434                        ctx->limit_msec);
435                 break;
436         case SR_HWCAP_LIMIT_SAMPLES:
437                 ctx->limit_samples = *(const uint64_t *)value;
438                 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
439                        ctx->limit_samples);
440                 break;
441         default:
442                 sr_err("genericdmm: Unknown capability: %d.", hwcap);
443                 return SR_ERR;
444                 break;
445         }
446
447         return SR_OK;
448 }
449
450 static int receive_data(int fd, int revents, void *cb_data)
451 {
452         struct sr_dev_inst *sdi;
453         struct context *ctx;
454
455         if (!(sdi = cb_data))
456                 return FALSE;
457
458         if (!(ctx = sdi->priv))
459                 return FALSE;
460
461         if (revents != G_IO_IN) {
462                 sr_err("genericdmm: No data?");
463                 return FALSE;
464         }
465
466         switch (ctx->profile->transport) {
467         case DMM_TRANSPORT_USBHID:
468                 /* TODO */
469                 break;
470         case DMM_TRANSPORT_SERIAL:
471                 /* TODO */
472                 break;
473         }
474
475         return TRUE;
476 }
477
478 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
479 {
480         struct sr_datafeed_packet packet;
481         struct sr_datafeed_header header;
482         struct sr_datafeed_meta_analog meta;
483         struct sr_dev_inst *sdi;
484         struct context *ctx;
485
486         if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
487                 sr_err("genericdmm: sdi was NULL.");
488                 return SR_ERR_BUG;
489         }
490
491         if (!(ctx = sdi->priv)) {
492                 sr_err("genericdmm: sdi->priv was NULL.");
493                 return SR_ERR_BUG;
494         }
495
496         sr_dbg("genericdmm: Starting acquisition.");
497
498         ctx->cb_data = cb_data;
499
500         /* Send header packet to the session bus. */
501         sr_dbg("genericdmm: Sending SR_DF_HEADER.");
502         packet.type = SR_DF_HEADER;
503         packet.payload = (uint8_t *)&header;
504         header.feed_version = 1;
505         gettimeofday(&header.starttime, NULL);
506         sr_session_send(ctx->cb_data, &packet);
507
508         /* Send metadata about the SR_DF_ANALOG packets to come. */
509         sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
510         packet.type = SR_DF_META_ANALOG;
511         packet.payload = &meta;
512         meta.num_probes = 1;
513         sr_session_send(ctx->cb_data, &packet);
514
515         /* Hook up a proxy handler to receive data from the device. */
516         switch (ctx->profile->transport) {
517         case DMM_TRANSPORT_USBHID:
518                 /* TODO libusb FD setup */
519                 break;
520         case DMM_TRANSPORT_SERIAL:
521                 /* TODO serial FD setup */
522                 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
523                 break;
524         }
525
526         return SR_OK;
527 }
528
529 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
530 {
531         struct sr_datafeed_packet packet;
532
533         /* Avoid compiler warnings. */
534         (void)dev_index;
535
536         sr_dbg("genericdmm: Stopping acquisition.");
537
538         /* Send end packet to the session bus. */
539         sr_dbg("genericdmm: Sending SR_DF_END.");
540         packet.type = SR_DF_END;
541         sr_session_send(cb_data, &packet);
542
543         return SR_OK;
544 }
545
546 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
547         .name = "genericdmm",
548         .longname = "Generic DMM",
549         .api_version = 1,
550         .init = hw_init,
551         .cleanup = hw_cleanup,
552         .scan = hw_scan,
553         .dev_open = hw_dev_open,
554         .dev_close = hw_dev_close,
555         .dev_info_get = hw_dev_info_get,
556         .dev_status_get = hw_dev_status_get,
557         .hwcap_get_all = hw_hwcap_get_all,
558         .dev_config_set = hw_dev_config_set,
559         .dev_acquisition_start = hw_dev_acquisition_start,
560         .dev_acquisition_stop = hw_dev_acquisition_stop,
561         .instances = NULL,
562 };