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