]> sigrok.org Git - libsigrok.git/blob - hardware/uni-t-dmm/api.c
usb: sr_usb_find() uses standardized connection string to find a USB device
[libsigrok.git] / hardware / uni-t-dmm / api.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26
27 static const int hwcaps[] = {
28         SR_HWCAP_MULTIMETER,
29         SR_HWCAP_LIMIT_SAMPLES,
30         SR_HWCAP_LIMIT_MSEC,
31         SR_HWCAP_CONTINUOUS,
32         0,
33 };
34
35 static const char *probe_names[] = {
36         "Probe",
37         NULL,
38 };
39
40 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
41 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
42
43 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
44 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
45
46 /* After hw_init() this will point to a device-specific entry (see above). */
47 static struct sr_dev_driver *di = NULL;
48
49 static int clear_instances(void)
50 {
51         /* TODO: Use common code later. */
52
53         return SR_OK;
54 }
55
56 static int hw_init(struct sr_context *sr_ctx, int dmm)
57 {
58         struct drv_context *drvc;
59
60         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
61                 sr_err("Driver context malloc failed.");
62                 return SR_ERR_MALLOC;
63         }
64
65         if (dmm == UNI_T_UT61D)
66                 di = di_ut61d;
67         else if (dmm == VOLTCRAFT_VC820)
68                 di = di_vc820;
69         sr_dbg("Selected '%s' subdriver.", di->name);
70
71         drvc->sr_ctx = sr_ctx;
72         di->priv = drvc;
73
74         return SR_OK;
75 }
76
77 static int hw_init_ut61d(struct sr_context *sr_ctx)
78 {
79         return hw_init(sr_ctx, UNI_T_UT61D);
80 }
81
82 static int hw_init_vc820(struct sr_context *sr_ctx)
83 {
84         return hw_init(sr_ctx, VOLTCRAFT_VC820);
85 }
86
87 static GSList *hw_scan(GSList *options)
88 {
89         GSList *devices, *l;
90         struct sr_dev_inst *sdi;
91         struct dev_context *devc;
92         struct drv_context *drvc;
93         struct sr_probe *probe;
94         struct libusb_device *ldev;
95         int i, devcnt;
96
97         (void)options;
98
99         drvc = di->priv;
100
101         devices = NULL;
102
103         if (!(l = sr_usb_find(drvc->sr_ctx->libusb_ctx, "1a86.e008")))
104                 return NULL;
105
106         for (i = 0; i < (int)g_slist_length(l); i++) {
107                 ldev = (struct libusb_device *)l->data;
108
109                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
110                         sr_err("Device context malloc failed.");
111                         return NULL;
112                 }
113
114                 devcnt = g_slist_length(drvc->instances);
115                 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
116                                 di->longname, NULL, NULL))) {
117                         sr_err("sr_dev_inst_new returned NULL.");
118                         return NULL;
119                 }
120                 sdi->priv = devc;
121                 sdi->driver = di;
122                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
123                         return NULL;
124                 sdi->probes = g_slist_append(sdi->probes, probe);
125                 devc->usb = sr_usb_dev_inst_new(
126                                 libusb_get_bus_number(ldev),
127                                 libusb_get_device_address(ldev), NULL);
128                 drvc->instances = g_slist_append(drvc->instances, l->data);
129                 devices = g_slist_append(devices, sdi);
130         }
131
132         return devices;
133 }
134
135 static GSList *hw_dev_list(void)
136 {
137         struct drv_context *drvc;
138
139         drvc = di->priv;
140
141         return drvc->instances;
142 }
143
144 static int hw_dev_open(struct sr_dev_inst *sdi)
145 {
146         struct dev_context *devc;
147
148         devc = sdi->priv;
149
150         return sr_usb_open(NULL, devc->usb);
151 }
152
153 static int hw_dev_close(struct sr_dev_inst *sdi)
154 {
155         (void)sdi;
156
157         /* TODO */
158
159         return SR_OK;
160 }
161
162 static int hw_cleanup(void)
163 {
164         clear_instances();
165
166         return SR_OK;
167 }
168
169 static int hw_info_get(int info_id, const void **data,
170                        const struct sr_dev_inst *sdi)
171 {
172         (void)sdi;
173
174         sr_spew("Backend requested info_id %d.", info_id);
175
176         switch (info_id) {
177         case SR_DI_HWCAPS:
178                 *data = hwcaps;
179                 sr_spew("%s: Returning hwcaps.", __func__);
180                 break;
181         case SR_DI_NUM_PROBES:
182                 *data = GINT_TO_POINTER(1);
183                 sr_spew("%s: Returning number of probes.", __func__);
184                 break;
185         case SR_DI_PROBE_NAMES:
186                 *data = probe_names;
187                 sr_spew("%s: Returning probe names.", __func__);
188                 break;
189         case SR_DI_SAMPLERATES:
190                 /* TODO: Get rid of this. */
191                 *data = NULL;
192                 sr_spew("%s: Returning samplerates.", __func__);
193                 return SR_ERR_ARG;
194                 break;
195         case SR_DI_CUR_SAMPLERATE:
196                 /* TODO: Get rid of this. */
197                 *data = NULL;
198                 sr_spew("%s: Returning current samplerate.", __func__);
199                 return SR_ERR_ARG;
200                 break;
201         default:
202                 sr_err("%s: Unknown info_id %d.", __func__, info_id);
203                 return SR_ERR_ARG;
204                 break;
205         }
206
207         return SR_OK;
208 }
209
210 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
211                              const void *value)
212 {
213         struct dev_context *devc;
214
215         devc = sdi->priv;
216
217         switch (hwcap) {
218         case SR_HWCAP_LIMIT_MSEC:
219                 /* TODO: Not yet implemented. */
220                 if (*(const uint64_t *)value == 0) {
221                         sr_err("Time limit cannot be 0.");
222                         return SR_ERR;
223                 }
224                 devc->limit_msec = *(const uint64_t *)value;
225                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
226                        devc->limit_msec);
227                 break;
228         case SR_HWCAP_LIMIT_SAMPLES:
229                 if (*(const uint64_t *)value == 0) {
230                         sr_err("Sample limit cannot be 0.");
231                         return SR_ERR;
232                 }
233                 devc->limit_samples = *(const uint64_t *)value;
234                 sr_dbg("Setting sample limit to %" PRIu64 ".",
235                        devc->limit_samples);
236                 break;
237         default:
238                 sr_err("Unknown capability: %d.", hwcap);
239                 return SR_ERR;
240                 break;
241         }
242
243         return SR_OK;
244 }
245
246 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
247                                     void *cb_data)
248 {
249         struct sr_datafeed_packet packet;
250         struct sr_datafeed_header header;
251         struct sr_datafeed_meta_analog meta;
252         struct dev_context *devc;
253
254         devc = sdi->priv;
255
256         sr_dbg("Starting acquisition.");
257
258         devc->cb_data = cb_data;
259
260         /* Send header packet to the session bus. */
261         sr_dbg("Sending SR_DF_HEADER.");
262         packet.type = SR_DF_HEADER;
263         packet.payload = (uint8_t *)&header;
264         header.feed_version = 1;
265         gettimeofday(&header.starttime, NULL);
266         sr_session_send(devc->cb_data, &packet);
267
268         /* Send metadata about the SR_DF_ANALOG packets to come. */
269         sr_dbg("Sending SR_DF_META_ANALOG.");
270         packet.type = SR_DF_META_ANALOG;
271         packet.payload = &meta;
272         meta.num_probes = 1;
273         sr_session_send(devc->cb_data, &packet);
274
275         if (!strcmp(di->name, "uni-t-ut61d")) {
276                 sr_source_add(0, 0, 10 /* poll_timeout */,
277                               uni_t_ut61d_receive_data, (void *)sdi);
278         } else if (!strcmp(di->name, "voltcraft-vc820")) {
279                 sr_source_add(0, 0, 10 /* poll_timeout */,
280                               voltcraft_vc820_receive_data, (void *)sdi);
281         }
282
283         return SR_OK;
284 }
285
286 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
287 {
288         struct sr_datafeed_packet packet;
289
290         (void)sdi;
291
292         sr_dbg("Stopping acquisition.");
293
294         /* Send end packet to the session bus. */
295         sr_dbg("Sending SR_DF_END.");
296         packet.type = SR_DF_END;
297         sr_session_send(cb_data, &packet);
298
299         /* TODO? */
300         sr_source_remove(0);
301
302         return SR_OK;
303 }
304
305 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
306         .name = "uni-t-ut61d",
307         .longname = "UNI-T UT61D",
308         .api_version = 1,
309         .init = hw_init_ut61d,
310         .cleanup = hw_cleanup,
311         .scan = hw_scan,
312         .dev_list = hw_dev_list,
313         .dev_clear = clear_instances,
314         .dev_open = hw_dev_open,
315         .dev_close = hw_dev_close,
316         .info_get = hw_info_get,
317         .dev_config_set = hw_dev_config_set,
318         .dev_acquisition_start = hw_dev_acquisition_start,
319         .dev_acquisition_stop = hw_dev_acquisition_stop,
320         .priv = NULL,
321 };
322
323 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
324         .name = "voltcraft-vc820",
325         .longname = "Voltcraft VC-820",
326         .api_version = 1,
327         .init = hw_init_vc820,
328         .cleanup = hw_cleanup,
329         .scan = hw_scan,
330         .dev_list = hw_dev_list,
331         .dev_clear = clear_instances,
332         .dev_open = hw_dev_open,
333         .dev_close = hw_dev_close,
334         .info_get = hw_info_get,
335         .dev_config_set = hw_dev_config_set,
336         .dev_acquisition_start = hw_dev_acquisition_start,
337         .dev_acquisition_stop = hw_dev_acquisition_stop,
338         .priv = NULL,
339 };