]>
Commit | Line | Data |
---|---|---|
79081ec8 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
79081ec8 | 3 | * |
bc653a56 | 4 | * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> |
79081ec8 UH |
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 | 29 | static const int32_t hwopts[] = { |
431ec7ca | 30 | SR_CONF_CONN, |
73365eae UH |
31 | }; |
32 | ||
7d93a62e | 33 | static 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 | 40 | SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info; |
fdbcb86d | 41 | SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info; |
6ac5f892 | 42 | |
c8852687 UH |
43 | SR_PRIV struct dmm_info udmms[] = { |
44 | { | |
bc653a56 | 45 | "UNI-T", "UT61D", 19230, |
c8852687 UH |
46 | FS9922_PACKET_SIZE, NULL, |
47 | sr_fs9922_packet_valid, sr_fs9922_parse, | |
48 | NULL, | |
49 | &uni_t_ut61d_driver_info, receive_data_UNI_T_UT61D, | |
50 | }, | |
51 | { | |
bc653a56 | 52 | "Voltcraft", "VC-820", 2400, |
c8852687 UH |
53 | FS9721_PACKET_SIZE, NULL, |
54 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
55 | NULL, | |
56 | &voltcraft_vc820_driver_info, receive_data_VOLTCRAFT_VC820, | |
57 | }, | |
58 | }; | |
6ac5f892 | 59 | |
c8852687 | 60 | static int clear_instances(int dmm) |
79081ec8 | 61 | { |
c8852687 UH |
62 | (void)dmm; |
63 | ||
79081ec8 UH |
64 | /* TODO: Use common code later. */ |
65 | ||
66 | return SR_OK; | |
67 | } | |
68 | ||
34f06b90 | 69 | static int hw_init(struct sr_context *sr_ctx, int dmm) |
79081ec8 | 70 | { |
c8852687 | 71 | sr_dbg("Selected '%s' subdriver.", udmms[dmm].di->name); |
6ac5f892 | 72 | |
bc653a56 | 73 | return std_hw_init(sr_ctx, udmms[dmm].di, DRIVER_LOG_DOMAIN); |
fdbcb86d UH |
74 | } |
75 | ||
c8852687 | 76 | static GSList *hw_scan(GSList *options, int dmm) |
79081ec8 | 77 | { |
388f9d3e | 78 | GSList *usb_devices, *devices, *l; |
79081ec8 | 79 | struct sr_dev_inst *sdi; |
41d9427f | 80 | struct dev_context *devc; |
79081ec8 | 81 | struct drv_context *drvc; |
388f9d3e | 82 | struct sr_usb_dev_inst *usb; |
431ec7ca | 83 | struct sr_config *src; |
41d9427f | 84 | struct sr_probe *probe; |
388f9d3e | 85 | const char *conn; |
79081ec8 | 86 | |
c8852687 | 87 | drvc = udmms[dmm].di->priv; |
79081ec8 | 88 | |
388f9d3e UH |
89 | conn = NULL; |
90 | for (l = options; l; l = l->next) { | |
431ec7ca BV |
91 | src = l->data; |
92 | switch (src->key) { | |
93 | case SR_CONF_CONN: | |
7d93a62e | 94 | conn = g_variant_get_string(src->data, NULL); |
388f9d3e UH |
95 | break; |
96 | } | |
97 | } | |
98 | if (!conn) | |
99 | conn = UNI_T_UT_D04_NEW; | |
41d9427f | 100 | |
388f9d3e UH |
101 | devices = NULL; |
102 | if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { | |
103 | g_slist_free_full(usb_devices, g_free); | |
79081ec8 | 104 | return NULL; |
388f9d3e | 105 | } |
79081ec8 | 106 | |
388f9d3e UH |
107 | for (l = usb_devices; l; l = l->next) { |
108 | usb = l->data; | |
41d9427f UH |
109 | |
110 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
111 | sr_err("Device context malloc failed."); | |
112 | return NULL; | |
113 | } | |
114 | ||
bc653a56 UH |
115 | devc->first_run = TRUE; |
116 | ||
388f9d3e | 117 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, |
c8852687 | 118 | udmms[dmm].vendor, udmms[dmm].device, NULL))) { |
41d9427f UH |
119 | sr_err("sr_dev_inst_new returned NULL."); |
120 | return NULL; | |
121 | } | |
122 | sdi->priv = devc; | |
c8852687 | 123 | sdi->driver = udmms[dmm].di; |
41d9427f UH |
124 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) |
125 | return NULL; | |
126 | sdi->probes = g_slist_append(sdi->probes, probe); | |
388f9d3e UH |
127 | |
128 | devc->usb = usb; | |
129 | ||
130 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
41d9427f | 131 | devices = g_slist_append(devices, sdi); |
79081ec8 UH |
132 | } |
133 | ||
134 | return devices; | |
135 | } | |
136 | ||
c8852687 | 137 | static GSList *hw_dev_list(int dmm) |
79081ec8 | 138 | { |
bc653a56 | 139 | return ((struct drv_context *)(udmms[dmm].di->priv))->instances; |
79081ec8 UH |
140 | } |
141 | ||
c8852687 | 142 | static int hw_dev_open(struct sr_dev_inst *sdi, int dmm) |
79081ec8 | 143 | { |
388f9d3e | 144 | struct drv_context *drvc; |
41d9427f | 145 | struct dev_context *devc; |
bc653a56 | 146 | int ret; |
41d9427f | 147 | |
c8852687 | 148 | drvc = udmms[dmm].di->priv; |
41d9427f UH |
149 | devc = sdi->priv; |
150 | ||
bc653a56 UH |
151 | if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb)) == SR_OK) |
152 | sdi->status = SR_ST_ACTIVE; | |
e73ffd42 | 153 | |
bc653a56 | 154 | return ret; |
79081ec8 UH |
155 | } |
156 | ||
157 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
158 | { | |
159 | (void)sdi; | |
160 | ||
161 | /* TODO */ | |
162 | ||
bc653a56 | 163 | sdi->status = SR_ST_INACTIVE; |
e73ffd42 | 164 | |
79081ec8 UH |
165 | return SR_OK; |
166 | } | |
167 | ||
c8852687 | 168 | static int hw_cleanup(int dmm) |
79081ec8 | 169 | { |
c8852687 | 170 | clear_instances(dmm); |
79081ec8 | 171 | |
79081ec8 UH |
172 | return SR_OK; |
173 | } | |
174 | ||
7d93a62e | 175 | static int config_set(int id, GVariant *data, 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 | 183 | /* TODO: Not yet implemented. */ |
7d93a62e | 184 | if (g_variant_get_uint64(data) == 0) { |
79081ec8 UH |
185 | sr_err("Time limit cannot be 0."); |
186 | return SR_ERR; | |
187 | } | |
7d93a62e | 188 | devc->limit_msec = g_variant_get_uint64(data); |
79081ec8 UH |
189 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
190 | devc->limit_msec); | |
191 | break; | |
1953564a | 192 | case SR_CONF_LIMIT_SAMPLES: |
7d93a62e | 193 | if (g_variant_get_uint64(data) == 0) { |
79081ec8 UH |
194 | sr_err("Sample limit cannot be 0."); |
195 | return SR_ERR; | |
196 | } | |
7d93a62e | 197 | devc->limit_samples = g_variant_get_uint64(data); |
79081ec8 UH |
198 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
199 | devc->limit_samples); | |
200 | break; | |
201 | default: | |
bd6fbf62 | 202 | return SR_ERR_NA; |
79081ec8 UH |
203 | } |
204 | ||
205 | return SR_OK; | |
206 | } | |
207 | ||
7d93a62e | 208 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc | 209 | { |
a1c743fc BV |
210 | (void)sdi; |
211 | ||
212 | switch (key) { | |
0d485e30 | 213 | case SR_CONF_SCAN_OPTIONS: |
7d93a62e BV |
214 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
215 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 216 | break; |
9a6517d1 | 217 | case SR_CONF_DEVICE_OPTIONS: |
7d93a62e BV |
218 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
219 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 220 | break; |
a1c743fc | 221 | default: |
bd6fbf62 | 222 | return SR_ERR_NA; |
a1c743fc BV |
223 | } |
224 | ||
225 | return SR_OK; | |
226 | } | |
227 | ||
79081ec8 | 228 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
c8852687 | 229 | void *cb_data, int dmm) |
79081ec8 | 230 | { |
79081ec8 UH |
231 | struct dev_context *devc; |
232 | ||
233 | devc = sdi->priv; | |
234 | ||
79081ec8 UH |
235 | devc->cb_data = cb_data; |
236 | ||
237 | /* Send header packet to the session bus. */ | |
4afdfd46 | 238 | std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); |
79081ec8 | 239 | |
c8852687 UH |
240 | sr_source_add(0, 0, 10 /* poll_timeout */, |
241 | udmms[dmm].receive_data, (void *)sdi); | |
79081ec8 UH |
242 | |
243 | return SR_OK; | |
244 | } | |
245 | ||
69b07d14 | 246 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
79081ec8 UH |
247 | { |
248 | struct sr_datafeed_packet packet; | |
249 | ||
250 | (void)sdi; | |
251 | ||
252 | sr_dbg("Stopping acquisition."); | |
253 | ||
254 | /* Send end packet to the session bus. */ | |
255 | sr_dbg("Sending SR_DF_END."); | |
256 | packet.type = SR_DF_END; | |
257 | sr_session_send(cb_data, &packet); | |
258 | ||
259 | /* TODO? */ | |
260 | sr_source_remove(0); | |
261 | ||
262 | return SR_OK; | |
263 | } | |
264 | ||
c8852687 UH |
265 | /* Driver-specific API function wrappers */ |
266 | #define HW_INIT(X) \ | |
267 | static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); } | |
268 | #define HW_CLEANUP(X) \ | |
269 | static int hw_cleanup_##X(void) { return hw_cleanup(X); } | |
270 | #define HW_SCAN(X) \ | |
271 | static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); } | |
272 | #define HW_DEV_LIST(X) \ | |
273 | static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); } | |
274 | #define CLEAR_INSTANCES(X) \ | |
275 | static int clear_instances_##X(void) { return clear_instances(X); } | |
276 | #define HW_DEV_ACQUISITION_START(X) \ | |
277 | static int hw_dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \ | |
278 | void *cb_data) { return hw_dev_acquisition_start(sdi, cb_data, X); } | |
279 | #define HW_DEV_OPEN(X) \ | |
280 | static int hw_dev_open_##X(struct sr_dev_inst *sdi) { return hw_dev_open(sdi, X); } | |
281 | ||
282 | /* Driver structs and API function wrappers */ | |
283 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
284 | HW_INIT(ID_UPPER) \ | |
285 | HW_CLEANUP(ID_UPPER) \ | |
286 | HW_SCAN(ID_UPPER) \ | |
287 | HW_DEV_LIST(ID_UPPER) \ | |
288 | CLEAR_INSTANCES(ID_UPPER) \ | |
289 | HW_DEV_ACQUISITION_START(ID_UPPER) \ | |
290 | HW_DEV_OPEN(ID_UPPER) \ | |
291 | SR_PRIV struct sr_dev_driver ID##_driver_info = { \ | |
292 | .name = NAME, \ | |
293 | .longname = LONGNAME, \ | |
294 | .api_version = 1, \ | |
295 | .init = hw_init_##ID_UPPER, \ | |
296 | .cleanup = hw_cleanup_##ID_UPPER, \ | |
297 | .scan = hw_scan_##ID_UPPER, \ | |
298 | .dev_list = hw_dev_list_##ID_UPPER, \ | |
299 | .dev_clear = clear_instances_##ID_UPPER, \ | |
300 | .config_get = NULL, \ | |
301 | .config_set = config_set, \ | |
302 | .config_list = config_list, \ | |
303 | .dev_open = hw_dev_open_##ID_UPPER, \ | |
304 | .dev_close = hw_dev_close, \ | |
305 | .dev_acquisition_start = hw_dev_acquisition_start_##ID_UPPER, \ | |
306 | .dev_acquisition_stop = hw_dev_acquisition_stop, \ | |
307 | .priv = NULL, \ | |
fdbcb86d UH |
308 | }; |
309 | ||
c8852687 UH |
310 | DRV(uni_t_ut61d, UNI_T_UT61D, "uni-t-ut61d", "UNI-T UT61D") |
311 | DRV(voltcraft_vc820, VOLTCRAFT_VC820, "voltcraft-vc820", "Voltcraft VC-820") |