]> sigrok.org Git - libsigrok.git/blame - hardware/uni-t-dmm/api.c
common/dmm: Drop obsolete *is_packet_start() functions.
[libsigrok.git] / hardware / uni-t-dmm / api.c
CommitLineData
79081ec8 1/*
50985c20 2 * This file is part of the libsigrok project.
79081ec8
UH
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
388f9d3e
UH
27#define UNI_T_UT_D04_NEW "1a86.e008"
28
7d93a62e 29static const int32_t hwopts[] = {
431ec7ca 30 SR_CONF_CONN,
73365eae
UH
31};
32
7d93a62e 33static const int32_t hwcaps[] = {
1953564a
BV
34 SR_CONF_MULTIMETER,
35 SR_CONF_LIMIT_SAMPLES,
36 SR_CONF_LIMIT_MSEC,
37 SR_CONF_CONTINUOUS,
79081ec8
UH
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
34f06b90 56static int hw_init(struct sr_context *sr_ctx, int dmm)
79081ec8 57{
fdbcb86d 58 if (dmm == UNI_T_UT61D)
6ac5f892 59 di = di_ut61d;
fdbcb86d 60 else if (dmm == VOLTCRAFT_VC820)
6ac5f892 61 di = di_vc820;
8c1adf37 62 sr_dbg("Selected '%s' subdriver.", di->name);
6ac5f892 63
063e7aef 64 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
79081ec8
UH
65}
66
34f06b90 67static int hw_init_ut61d(struct sr_context *sr_ctx)
fdbcb86d 68{
34f06b90 69 return hw_init(sr_ctx, UNI_T_UT61D);
fdbcb86d
UH
70}
71
34f06b90 72static int hw_init_vc820(struct sr_context *sr_ctx)
fdbcb86d 73{
34f06b90 74 return hw_init(sr_ctx, VOLTCRAFT_VC820);
fdbcb86d
UH
75}
76
6ac5f892 77static GSList *hw_scan(GSList *options)
79081ec8 78{
388f9d3e 79 GSList *usb_devices, *devices, *l;
79081ec8 80 struct sr_dev_inst *sdi;
41d9427f 81 struct dev_context *devc;
79081ec8 82 struct drv_context *drvc;
388f9d3e 83 struct sr_usb_dev_inst *usb;
431ec7ca 84 struct sr_config *src;
41d9427f 85 struct sr_probe *probe;
388f9d3e 86 const char *conn;
79081ec8
UH
87
88 (void)options;
89
6ac5f892 90 drvc = di->priv;
79081ec8 91
388f9d3e
UH
92 /* USB scan is always authoritative. */
93 clear_instances();
94
95 conn = NULL;
96 for (l = options; l; l = l->next) {
431ec7ca
BV
97 src = l->data;
98 switch (src->key) {
99 case SR_CONF_CONN:
7d93a62e 100 conn = g_variant_get_string(src->data, NULL);
388f9d3e
UH
101 break;
102 }
103 }
104 if (!conn)
105 conn = UNI_T_UT_D04_NEW;
41d9427f 106
388f9d3e
UH
107 devices = NULL;
108 if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
109 g_slist_free_full(usb_devices, g_free);
79081ec8 110 return NULL;
388f9d3e 111 }
79081ec8 112
388f9d3e
UH
113 for (l = usb_devices; l; l = l->next) {
114 usb = l->data;
41d9427f
UH
115
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
117 sr_err("Device context malloc failed.");
118 return NULL;
119 }
120
388f9d3e 121 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
41d9427f
UH
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);
388f9d3e
UH
131
132 devc->usb = usb;
133
134 drvc->instances = g_slist_append(drvc->instances, sdi);
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 142{
0e94d524 143 return ((struct drv_context *)(di->priv))->instances;
79081ec8
UH
144}
145
146static int hw_dev_open(struct sr_dev_inst *sdi)
147{
388f9d3e 148 struct drv_context *drvc;
41d9427f 149 struct dev_context *devc;
e73ffd42 150 int ret;
41d9427f 151
388f9d3e 152 drvc = di->priv;
41d9427f
UH
153 devc = sdi->priv;
154
e73ffd42
BV
155 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb)) == SR_OK)
156 sdi->status = SR_ST_ACTIVE;
157
158 return ret;
79081ec8
UH
159}
160
161static int hw_dev_close(struct sr_dev_inst *sdi)
162{
163 (void)sdi;
164
165 /* TODO */
166
e73ffd42
BV
167 sdi->status = SR_ST_INACTIVE;
168
79081ec8
UH
169 return SR_OK;
170}
171
172static int hw_cleanup(void)
173{
174 clear_instances();
175
79081ec8
UH
176 return SR_OK;
177}
178
7d93a62e 179static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
79081ec8
UH
180{
181 struct dev_context *devc;
182
183 devc = sdi->priv;
184
035a1078 185 switch (id) {
1953564a 186 case SR_CONF_LIMIT_MSEC:
79081ec8 187 /* TODO: Not yet implemented. */
7d93a62e 188 if (g_variant_get_uint64(data) == 0) {
79081ec8
UH
189 sr_err("Time limit cannot be 0.");
190 return SR_ERR;
191 }
7d93a62e 192 devc->limit_msec = g_variant_get_uint64(data);
79081ec8
UH
193 sr_dbg("Setting time limit to %" PRIu64 "ms.",
194 devc->limit_msec);
195 break;
1953564a 196 case SR_CONF_LIMIT_SAMPLES:
7d93a62e 197 if (g_variant_get_uint64(data) == 0) {
79081ec8
UH
198 sr_err("Sample limit cannot be 0.");
199 return SR_ERR;
200 }
7d93a62e 201 devc->limit_samples = g_variant_get_uint64(data);
79081ec8
UH
202 sr_dbg("Setting sample limit to %" PRIu64 ".",
203 devc->limit_samples);
204 break;
205 default:
bd6fbf62 206 return SR_ERR_NA;
79081ec8
UH
207 }
208
209 return SR_OK;
210}
211
7d93a62e 212static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
a1c743fc
BV
213{
214
215 (void)sdi;
216
217 switch (key) {
0d485e30 218 case SR_CONF_SCAN_OPTIONS:
7d93a62e
BV
219 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
220 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
0d485e30 221 break;
9a6517d1 222 case SR_CONF_DEVICE_OPTIONS:
7d93a62e
BV
223 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
224 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
9a6517d1 225 break;
a1c743fc 226 default:
bd6fbf62 227 return SR_ERR_NA;
a1c743fc
BV
228 }
229
230 return SR_OK;
231}
232
79081ec8 233static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
6ac5f892 234 void *cb_data)
79081ec8 235{
79081ec8
UH
236 struct dev_context *devc;
237
238 devc = sdi->priv;
239
79081ec8
UH
240 devc->cb_data = cb_data;
241
242 /* Send header packet to the session bus. */
4afdfd46 243 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
79081ec8 244
6ac5f892 245 if (!strcmp(di->name, "uni-t-ut61d")) {
fdbcb86d
UH
246 sr_source_add(0, 0, 10 /* poll_timeout */,
247 uni_t_ut61d_receive_data, (void *)sdi);
6ac5f892 248 } else if (!strcmp(di->name, "voltcraft-vc820")) {
fdbcb86d
UH
249 sr_source_add(0, 0, 10 /* poll_timeout */,
250 voltcraft_vc820_receive_data, (void *)sdi);
251 }
79081ec8
UH
252
253 return SR_OK;
254}
255
69b07d14 256static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
79081ec8
UH
257{
258 struct sr_datafeed_packet packet;
259
260 (void)sdi;
261
262 sr_dbg("Stopping acquisition.");
263
264 /* Send end packet to the session bus. */
265 sr_dbg("Sending SR_DF_END.");
266 packet.type = SR_DF_END;
267 sr_session_send(cb_data, &packet);
268
269 /* TODO? */
270 sr_source_remove(0);
271
272 return SR_OK;
273}
274
fdbcb86d
UH
275SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
276 .name = "uni-t-ut61d",
277 .longname = "UNI-T UT61D",
278 .api_version = 1,
279 .init = hw_init_ut61d,
280 .cleanup = hw_cleanup,
6ac5f892
UH
281 .scan = hw_scan,
282 .dev_list = hw_dev_list,
fdbcb86d 283 .dev_clear = clear_instances,
6fab7b8f 284 .config_get = NULL,
035a1078 285 .config_set = config_set,
a1c743fc 286 .config_list = config_list,
fdbcb86d
UH
287 .dev_open = hw_dev_open,
288 .dev_close = hw_dev_close,
6ac5f892 289 .dev_acquisition_start = hw_dev_acquisition_start,
fdbcb86d
UH
290 .dev_acquisition_stop = hw_dev_acquisition_stop,
291 .priv = NULL,
292};
293
294SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
295 .name = "voltcraft-vc820",
296 .longname = "Voltcraft VC-820",
79081ec8 297 .api_version = 1,
fdbcb86d 298 .init = hw_init_vc820,
79081ec8 299 .cleanup = hw_cleanup,
6ac5f892
UH
300 .scan = hw_scan,
301 .dev_list = hw_dev_list,
79081ec8 302 .dev_clear = clear_instances,
6fab7b8f 303 .config_get = NULL,
035a1078 304 .config_set = config_set,
a1c743fc 305 .config_list = config_list,
79081ec8
UH
306 .dev_open = hw_dev_open,
307 .dev_close = hw_dev_close,
6ac5f892 308 .dev_acquisition_start = hw_dev_acquisition_start,
79081ec8
UH
309 .dev_acquisition_stop = hw_dev_acquisition_stop,
310 .priv = NULL,
311};