]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012-2013 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 | ||
29 | static const int32_t hwopts[] = { | |
30 | SR_CONF_CONN, | |
31 | }; | |
32 | ||
33 | static const int32_t hwcaps[] = { | |
34 | SR_CONF_MULTIMETER, | |
35 | SR_CONF_LIMIT_SAMPLES, | |
36 | SR_CONF_LIMIT_MSEC, | |
37 | SR_CONF_CONTINUOUS, | |
38 | }; | |
39 | ||
40 | SR_PRIV struct sr_dev_driver tecpel_dmm_8060_driver_info; | |
41 | SR_PRIV struct sr_dev_driver tecpel_dmm_8061_driver_info; | |
42 | SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info; | |
43 | SR_PRIV struct sr_dev_driver uni_t_ut61e_driver_info; | |
44 | SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info; | |
45 | SR_PRIV struct sr_dev_driver voltcraft_vc840_driver_info; | |
46 | ||
47 | SR_PRIV struct dmm_info udmms[] = { | |
48 | { | |
49 | "Tecpel", "DMM-8060", 2400, | |
50 | FS9721_PACKET_SIZE, NULL, | |
51 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
52 | NULL, | |
53 | &tecpel_dmm_8060_driver_info, receive_data_TECPEL_DMM_8060, | |
54 | }, | |
55 | { | |
56 | "Tecpel", "DMM-8061", 2400, | |
57 | FS9721_PACKET_SIZE, NULL, | |
58 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
59 | sr_fs9721_00_temp_c, | |
60 | &tecpel_dmm_8061_driver_info, receive_data_TECPEL_DMM_8061, | |
61 | }, | |
62 | { | |
63 | "UNI-T", "UT61D", 2400, | |
64 | FS9922_PACKET_SIZE, NULL, | |
65 | sr_fs9922_packet_valid, sr_fs9922_parse, | |
66 | NULL, | |
67 | &uni_t_ut61d_driver_info, receive_data_UNI_T_UT61D, | |
68 | }, | |
69 | { | |
70 | "UNI-T", "UT61E", 19230, | |
71 | ES51922_PACKET_SIZE, NULL, | |
72 | sr_es51922_packet_valid, sr_es51922_parse, | |
73 | NULL, | |
74 | &uni_t_ut61e_driver_info, receive_data_UNI_T_UT61E, | |
75 | }, | |
76 | { | |
77 | "Voltcraft", "VC-820", 2400, | |
78 | FS9721_PACKET_SIZE, NULL, | |
79 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
80 | NULL, | |
81 | &voltcraft_vc820_driver_info, receive_data_VOLTCRAFT_VC820, | |
82 | }, | |
83 | { | |
84 | "Voltcraft", "VC-840", 2400, | |
85 | FS9721_PACKET_SIZE, NULL, | |
86 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
87 | sr_fs9721_00_temp_c, | |
88 | &voltcraft_vc840_driver_info, receive_data_VOLTCRAFT_VC840, | |
89 | }, | |
90 | }; | |
91 | ||
92 | static int dev_clear(int dmm) | |
93 | { | |
94 | return std_dev_clear(udmms[dmm].di, NULL); | |
95 | } | |
96 | ||
97 | static int init(struct sr_context *sr_ctx, int dmm) | |
98 | { | |
99 | sr_dbg("Selected '%s' subdriver.", udmms[dmm].di->name); | |
100 | ||
101 | return std_init(sr_ctx, udmms[dmm].di, LOG_PREFIX); | |
102 | } | |
103 | ||
104 | static GSList *scan(GSList *options, int dmm) | |
105 | { | |
106 | GSList *usb_devices, *devices, *l; | |
107 | struct sr_dev_inst *sdi; | |
108 | struct dev_context *devc; | |
109 | struct drv_context *drvc; | |
110 | struct sr_usb_dev_inst *usb; | |
111 | struct sr_config *src; | |
112 | struct sr_probe *probe; | |
113 | const char *conn; | |
114 | ||
115 | drvc = udmms[dmm].di->priv; | |
116 | ||
117 | conn = NULL; | |
118 | for (l = options; l; l = l->next) { | |
119 | src = l->data; | |
120 | switch (src->key) { | |
121 | case SR_CONF_CONN: | |
122 | conn = g_variant_get_string(src->data, NULL); | |
123 | break; | |
124 | } | |
125 | } | |
126 | if (!conn) | |
127 | conn = UNI_T_UT_D04_NEW; | |
128 | ||
129 | devices = NULL; | |
130 | if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { | |
131 | g_slist_free_full(usb_devices, g_free); | |
132 | return NULL; | |
133 | } | |
134 | ||
135 | for (l = usb_devices; l; l = l->next) { | |
136 | usb = l->data; | |
137 | ||
138 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
139 | sr_err("Device context malloc failed."); | |
140 | return NULL; | |
141 | } | |
142 | ||
143 | devc->first_run = TRUE; | |
144 | ||
145 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, | |
146 | udmms[dmm].vendor, udmms[dmm].device, NULL))) { | |
147 | sr_err("sr_dev_inst_new returned NULL."); | |
148 | return NULL; | |
149 | } | |
150 | sdi->priv = devc; | |
151 | sdi->driver = udmms[dmm].di; | |
152 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
153 | return NULL; | |
154 | sdi->probes = g_slist_append(sdi->probes, probe); | |
155 | ||
156 | sdi->inst_type = SR_INST_USB; | |
157 | sdi->conn = usb; | |
158 | ||
159 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
160 | devices = g_slist_append(devices, sdi); | |
161 | } | |
162 | ||
163 | return devices; | |
164 | } | |
165 | ||
166 | static GSList *dev_list(int dmm) | |
167 | { | |
168 | return ((struct drv_context *)(udmms[dmm].di->priv))->instances; | |
169 | } | |
170 | ||
171 | static int dev_open(struct sr_dev_inst *sdi, int dmm) | |
172 | { | |
173 | struct drv_context *drvc; | |
174 | struct sr_usb_dev_inst *usb; | |
175 | int ret; | |
176 | ||
177 | drvc = udmms[dmm].di->priv; | |
178 | usb = sdi->conn; | |
179 | ||
180 | if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) | |
181 | sdi->status = SR_ST_ACTIVE; | |
182 | ||
183 | return ret; | |
184 | } | |
185 | ||
186 | static int dev_close(struct sr_dev_inst *sdi) | |
187 | { | |
188 | (void)sdi; | |
189 | ||
190 | /* TODO */ | |
191 | ||
192 | sdi->status = SR_ST_INACTIVE; | |
193 | ||
194 | return SR_OK; | |
195 | } | |
196 | ||
197 | static int cleanup(int dmm) | |
198 | { | |
199 | return dev_clear(dmm); | |
200 | } | |
201 | ||
202 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) | |
203 | { | |
204 | struct dev_context *devc; | |
205 | ||
206 | devc = sdi->priv; | |
207 | ||
208 | switch (id) { | |
209 | case SR_CONF_LIMIT_MSEC: | |
210 | if (g_variant_get_uint64(data) == 0) { | |
211 | sr_err("Time limit cannot be 0."); | |
212 | return SR_ERR; | |
213 | } | |
214 | devc->limit_msec = g_variant_get_uint64(data); | |
215 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
216 | devc->limit_msec); | |
217 | break; | |
218 | case SR_CONF_LIMIT_SAMPLES: | |
219 | if (g_variant_get_uint64(data) == 0) { | |
220 | sr_err("Sample limit cannot be 0."); | |
221 | return SR_ERR; | |
222 | } | |
223 | devc->limit_samples = g_variant_get_uint64(data); | |
224 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
225 | devc->limit_samples); | |
226 | break; | |
227 | default: | |
228 | return SR_ERR_NA; | |
229 | } | |
230 | ||
231 | return SR_OK; | |
232 | } | |
233 | ||
234 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
235 | { | |
236 | (void)sdi; | |
237 | ||
238 | switch (key) { | |
239 | case SR_CONF_SCAN_OPTIONS: | |
240 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
241 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
242 | break; | |
243 | case SR_CONF_DEVICE_OPTIONS: | |
244 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
245 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
246 | break; | |
247 | default: | |
248 | return SR_ERR_NA; | |
249 | } | |
250 | ||
251 | return SR_OK; | |
252 | } | |
253 | ||
254 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
255 | void *cb_data, int dmm) | |
256 | { | |
257 | struct dev_context *devc; | |
258 | ||
259 | devc = sdi->priv; | |
260 | ||
261 | devc->cb_data = cb_data; | |
262 | ||
263 | devc->starttime = g_get_monotonic_time(); | |
264 | ||
265 | /* Send header packet to the session bus. */ | |
266 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
267 | ||
268 | sr_source_add(0, 0, 10 /* poll_timeout */, | |
269 | udmms[dmm].receive_data, (void *)sdi); | |
270 | ||
271 | return SR_OK; | |
272 | } | |
273 | ||
274 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
275 | { | |
276 | struct sr_datafeed_packet packet; | |
277 | ||
278 | (void)sdi; | |
279 | ||
280 | sr_dbg("Stopping acquisition."); | |
281 | ||
282 | /* Send end packet to the session bus. */ | |
283 | sr_dbg("Sending SR_DF_END."); | |
284 | packet.type = SR_DF_END; | |
285 | sr_session_send(cb_data, &packet); | |
286 | ||
287 | /* TODO? */ | |
288 | sr_source_remove(0); | |
289 | ||
290 | return SR_OK; | |
291 | } | |
292 | ||
293 | /* Driver-specific API function wrappers */ | |
294 | #define HW_INIT(X) \ | |
295 | static int init_##X(struct sr_context *sr_ctx) { return init(sr_ctx, X); } | |
296 | #define HW_CLEANUP(X) \ | |
297 | static int cleanup_##X(void) { return cleanup(X); } | |
298 | #define HW_SCAN(X) \ | |
299 | static GSList *scan_##X(GSList *options) { return scan(options, X); } | |
300 | #define HW_DEV_LIST(X) \ | |
301 | static GSList *dev_list_##X(void) { return dev_list(X); } | |
302 | #define HW_DEV_CLEAR(X) \ | |
303 | static int dev_clear_##X(void) { return dev_clear(X); } | |
304 | #define HW_DEV_ACQUISITION_START(X) \ | |
305 | static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \ | |
306 | void *cb_data) { return dev_acquisition_start(sdi, cb_data, X); } | |
307 | #define HW_DEV_OPEN(X) \ | |
308 | static int dev_open_##X(struct sr_dev_inst *sdi) { return dev_open(sdi, X); } | |
309 | ||
310 | /* Driver structs and API function wrappers */ | |
311 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
312 | HW_INIT(ID_UPPER) \ | |
313 | HW_CLEANUP(ID_UPPER) \ | |
314 | HW_SCAN(ID_UPPER) \ | |
315 | HW_DEV_LIST(ID_UPPER) \ | |
316 | HW_DEV_CLEAR(ID_UPPER) \ | |
317 | HW_DEV_ACQUISITION_START(ID_UPPER) \ | |
318 | HW_DEV_OPEN(ID_UPPER) \ | |
319 | SR_PRIV struct sr_dev_driver ID##_driver_info = { \ | |
320 | .name = NAME, \ | |
321 | .longname = LONGNAME, \ | |
322 | .api_version = 1, \ | |
323 | .init = init_##ID_UPPER, \ | |
324 | .cleanup = cleanup_##ID_UPPER, \ | |
325 | .scan = scan_##ID_UPPER, \ | |
326 | .dev_list = dev_list_##ID_UPPER, \ | |
327 | .dev_clear = dev_clear_##ID_UPPER, \ | |
328 | .config_get = NULL, \ | |
329 | .config_set = config_set, \ | |
330 | .config_list = config_list, \ | |
331 | .dev_open = dev_open_##ID_UPPER, \ | |
332 | .dev_close = dev_close, \ | |
333 | .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \ | |
334 | .dev_acquisition_stop = dev_acquisition_stop, \ | |
335 | .priv = NULL, \ | |
336 | }; | |
337 | ||
338 | DRV(tecpel_dmm_8060, TECPEL_DMM_8060, "tecpel-dmm-8060", "Tecpel DMM-8060") | |
339 | DRV(tecpel_dmm_8061, TECPEL_DMM_8061, "tecpel-dmm-8061", "Tecpel DMM-8061") | |
340 | DRV(uni_t_ut61d, UNI_T_UT61D, "uni-t-ut61d", "UNI-T UT61D") | |
341 | DRV(uni_t_ut61e, UNI_T_UT61E, "uni-t-ut61e", "UNI-T UT61E") | |
342 | DRV(voltcraft_vc820, VOLTCRAFT_VC820, "voltcraft-vc820", "Voltcraft VC-820") | |
343 | DRV(voltcraft_vc840, VOLTCRAFT_VC840, "voltcraft-vc840", "Voltcraft VC-840") |