]> sigrok.org Git - libsigrok.git/blob - hardware/uni-t-dmm/api.c
Get rid of obsolete SR_DI_NUM_PROBES and SR_DI_PROBE_NAMES.
[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 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
36 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
37
38 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
39 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
40
41 /* After hw_init() this will point to a device-specific entry (see above). */
42 static struct sr_dev_driver *di = NULL;
43
44 static int clear_instances(void)
45 {
46         /* TODO: Use common code later. */
47
48         return SR_OK;
49 }
50
51 static int hw_init(struct sr_context *sr_ctx, int dmm)
52 {
53         struct drv_context *drvc;
54
55         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
56                 sr_err("Driver context malloc failed.");
57                 return SR_ERR_MALLOC;
58         }
59
60         if (dmm == UNI_T_UT61D)
61                 di = di_ut61d;
62         else if (dmm == VOLTCRAFT_VC820)
63                 di = di_vc820;
64         sr_dbg("Selected '%s' subdriver.", di->name);
65
66         drvc->sr_ctx = sr_ctx;
67         di->priv = drvc;
68
69         return SR_OK;
70 }
71
72 static int hw_init_ut61d(struct sr_context *sr_ctx)
73 {
74         return hw_init(sr_ctx, UNI_T_UT61D);
75 }
76
77 static int hw_init_vc820(struct sr_context *sr_ctx)
78 {
79         return hw_init(sr_ctx, VOLTCRAFT_VC820);
80 }
81
82 static GSList *hw_scan(GSList *options)
83 {
84         GSList *devices, *l;
85         struct sr_dev_inst *sdi;
86         struct dev_context *devc;
87         struct drv_context *drvc;
88         struct sr_probe *probe;
89         struct libusb_device *ldev;
90         int i, devcnt;
91
92         (void)options;
93
94         drvc = di->priv;
95
96         devices = NULL;
97
98         if (!(l = sr_usb_find(drvc->sr_ctx->libusb_ctx, "1a86.e008")))
99                 return NULL;
100
101         for (i = 0; i < (int)g_slist_length(l); i++) {
102                 ldev = (struct libusb_device *)l->data;
103
104                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
105                         sr_err("Device context malloc failed.");
106                         return NULL;
107                 }
108
109                 devcnt = g_slist_length(drvc->instances);
110                 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
111                                 di->longname, NULL, NULL))) {
112                         sr_err("sr_dev_inst_new returned NULL.");
113                         return NULL;
114                 }
115                 sdi->priv = devc;
116                 sdi->driver = di;
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                 drvc->instances = g_slist_append(drvc->instances, l->data);
124                 devices = g_slist_append(devices, sdi);
125         }
126
127         return devices;
128 }
129
130 static GSList *hw_dev_list(void)
131 {
132         struct drv_context *drvc;
133
134         drvc = di->priv;
135
136         return drvc->instances;
137 }
138
139 static int hw_dev_open(struct sr_dev_inst *sdi)
140 {
141         struct dev_context *devc;
142
143         devc = sdi->priv;
144
145         return sr_usb_open(NULL, devc->usb);
146 }
147
148 static int hw_dev_close(struct sr_dev_inst *sdi)
149 {
150         (void)sdi;
151
152         /* TODO */
153
154         return SR_OK;
155 }
156
157 static int hw_cleanup(void)
158 {
159         clear_instances();
160
161         return SR_OK;
162 }
163
164 static int hw_info_get(int info_id, const void **data,
165                        const struct sr_dev_inst *sdi)
166 {
167         (void)sdi;
168
169         sr_spew("Backend requested info_id %d.", info_id);
170
171         switch (info_id) {
172         case SR_DI_HWCAPS:
173                 *data = hwcaps;
174                 sr_spew("%s: Returning hwcaps.", __func__);
175                 break;
176         case SR_DI_SAMPLERATES:
177                 /* TODO: Get rid of this. */
178                 *data = NULL;
179                 sr_spew("%s: Returning samplerates.", __func__);
180                 return SR_ERR_ARG;
181                 break;
182         case SR_DI_CUR_SAMPLERATE:
183                 /* TODO: Get rid of this. */
184                 *data = NULL;
185                 sr_spew("%s: Returning current samplerate.", __func__);
186                 return SR_ERR_ARG;
187                 break;
188         default:
189                 sr_err("%s: Unknown info_id %d.", __func__, info_id);
190                 return SR_ERR_ARG;
191                 break;
192         }
193
194         return SR_OK;
195 }
196
197 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
198                              const void *value)
199 {
200         struct dev_context *devc;
201
202         devc = sdi->priv;
203
204         switch (hwcap) {
205         case SR_HWCAP_LIMIT_MSEC:
206                 /* TODO: Not yet implemented. */
207                 if (*(const uint64_t *)value == 0) {
208                         sr_err("Time limit cannot be 0.");
209                         return SR_ERR;
210                 }
211                 devc->limit_msec = *(const uint64_t *)value;
212                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
213                        devc->limit_msec);
214                 break;
215         case SR_HWCAP_LIMIT_SAMPLES:
216                 if (*(const uint64_t *)value == 0) {
217                         sr_err("Sample limit cannot be 0.");
218                         return SR_ERR;
219                 }
220                 devc->limit_samples = *(const uint64_t *)value;
221                 sr_dbg("Setting sample limit to %" PRIu64 ".",
222                        devc->limit_samples);
223                 break;
224         default:
225                 sr_err("Unknown capability: %d.", hwcap);
226                 return SR_ERR;
227                 break;
228         }
229
230         return SR_OK;
231 }
232
233 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
234                                     void *cb_data)
235 {
236         struct sr_datafeed_packet packet;
237         struct sr_datafeed_header header;
238         struct sr_datafeed_meta_analog meta;
239         struct dev_context *devc;
240
241         devc = sdi->priv;
242
243         sr_dbg("Starting acquisition.");
244
245         devc->cb_data = cb_data;
246
247         /* Send header packet to the session bus. */
248         sr_dbg("Sending SR_DF_HEADER.");
249         packet.type = SR_DF_HEADER;
250         packet.payload = (uint8_t *)&header;
251         header.feed_version = 1;
252         gettimeofday(&header.starttime, NULL);
253         sr_session_send(devc->cb_data, &packet);
254
255         /* Send metadata about the SR_DF_ANALOG packets to come. */
256         sr_dbg("Sending SR_DF_META_ANALOG.");
257         packet.type = SR_DF_META_ANALOG;
258         packet.payload = &meta;
259         meta.num_probes = 1;
260         sr_session_send(devc->cb_data, &packet);
261
262         if (!strcmp(di->name, "uni-t-ut61d")) {
263                 sr_source_add(0, 0, 10 /* poll_timeout */,
264                               uni_t_ut61d_receive_data, (void *)sdi);
265         } else if (!strcmp(di->name, "voltcraft-vc820")) {
266                 sr_source_add(0, 0, 10 /* poll_timeout */,
267                               voltcraft_vc820_receive_data, (void *)sdi);
268         }
269
270         return SR_OK;
271 }
272
273 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
274 {
275         struct sr_datafeed_packet packet;
276
277         (void)sdi;
278
279         sr_dbg("Stopping acquisition.");
280
281         /* Send end packet to the session bus. */
282         sr_dbg("Sending SR_DF_END.");
283         packet.type = SR_DF_END;
284         sr_session_send(cb_data, &packet);
285
286         /* TODO? */
287         sr_source_remove(0);
288
289         return SR_OK;
290 }
291
292 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
293         .name = "uni-t-ut61d",
294         .longname = "UNI-T UT61D",
295         .api_version = 1,
296         .init = hw_init_ut61d,
297         .cleanup = hw_cleanup,
298         .scan = hw_scan,
299         .dev_list = hw_dev_list,
300         .dev_clear = clear_instances,
301         .dev_open = hw_dev_open,
302         .dev_close = hw_dev_close,
303         .info_get = hw_info_get,
304         .dev_config_set = hw_dev_config_set,
305         .dev_acquisition_start = hw_dev_acquisition_start,
306         .dev_acquisition_stop = hw_dev_acquisition_stop,
307         .priv = NULL,
308 };
309
310 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
311         .name = "voltcraft-vc820",
312         .longname = "Voltcraft VC-820",
313         .api_version = 1,
314         .init = hw_init_vc820,
315         .cleanup = hw_cleanup,
316         .scan = hw_scan,
317         .dev_list = hw_dev_list,
318         .dev_clear = clear_instances,
319         .dev_open = hw_dev_open,
320         .dev_close = hw_dev_close,
321         .info_get = hw_info_get,
322         .dev_config_set = hw_dev_config_set,
323         .dev_acquisition_start = hw_dev_acquisition_start,
324         .dev_acquisition_stop = hw_dev_acquisition_stop,
325         .priv = NULL,
326 };