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