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