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