]>
Commit | Line | Data |
---|---|---|
7dc55d93 AG |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
729b01f9 | 6 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> |
7dc55d93 AG |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <glib.h> | |
7dc55d93 AG |
23 | #include <sys/types.h> |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <string.h> | |
27 | #include <errno.h> | |
bbabddbd UH |
28 | #include "libsigrok.h" |
29 | #include "libsigrok-internal.h" | |
30 | #include "protocol.h" | |
7dc55d93 AG |
31 | |
32 | static const int hwopts[] = { | |
33 | SR_HWOPT_CONN, | |
34 | SR_HWOPT_SERIALCOMM, | |
35 | 0, | |
36 | }; | |
37 | ||
38 | static const int hwcaps[] = { | |
39 | SR_HWCAP_MULTIMETER, | |
40 | SR_HWCAP_LIMIT_SAMPLES, | |
41 | SR_HWCAP_CONTINUOUS, | |
42 | 0, | |
43 | }; | |
44 | ||
45 | static const char *probe_names[] = { | |
46 | "Probe", | |
47 | NULL, | |
48 | }; | |
49 | ||
f086b830 | 50 | SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info; |
729b01f9 | 51 | SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info; |
f086b830 UH |
52 | |
53 | static struct sr_dev_driver *di_digitek_dt4000zc = &digitek_dt4000zc_driver_info; | |
729b01f9 UH |
54 | static struct sr_dev_driver *di_tekpower_tp4000zc = &tekpower_tp4000zc_driver_info; |
55 | ||
56 | /* After hw_init() this will point to a device-specific entry (see above). */ | |
57 | static struct sr_dev_driver *di = NULL; | |
58 | ||
59 | SR_PRIV struct dmm_info dmms[] = { | |
f086b830 UH |
60 | { |
61 | "Digitek", "DT4000ZC", | |
62 | "2400/8n1", 2400, | |
63 | FS9721_PACKET_SIZE, | |
64 | sr_fs9721_packet_valid, | |
65 | sr_fs9721_parse, | |
66 | dmm_details_dt4000zc, | |
67 | }, | |
729b01f9 UH |
68 | { |
69 | "TekPower", "TP4000ZC", | |
70 | "2400/8n1", 2400, | |
71 | FS9721_PACKET_SIZE, | |
72 | sr_fs9721_packet_valid, | |
73 | sr_fs9721_parse, | |
74 | dmm_details_tp4000zc, | |
75 | }, | |
76 | }; | |
7dc55d93 AG |
77 | |
78 | /* Properly close and free all devices. */ | |
79 | static int clear_instances(void) | |
80 | { | |
81 | struct sr_dev_inst *sdi; | |
82 | struct drv_context *drvc; | |
83 | struct dev_context *devc; | |
84 | GSList *l; | |
85 | ||
86 | if (!(drvc = di->priv)) | |
87 | return SR_OK; | |
88 | ||
89 | drvc = di->priv; | |
90 | for (l = drvc->instances; l; l = l->next) { | |
91 | if (!(sdi = l->data)) | |
92 | continue; | |
93 | if (!(devc = sdi->priv)) | |
94 | continue; | |
95 | sr_serial_dev_inst_free(devc->serial); | |
96 | sr_dev_inst_free(sdi); | |
97 | } | |
98 | g_slist_free(drvc->instances); | |
99 | drvc->instances = NULL; | |
100 | ||
101 | return SR_OK; | |
102 | } | |
103 | ||
729b01f9 | 104 | static int hw_init(int dmm) |
7dc55d93 AG |
105 | { |
106 | struct drv_context *drvc; | |
107 | ||
108 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
bbabddbd UH |
109 | sr_err("Driver context malloc failed."); |
110 | return SR_ERR_MALLOC; | |
7dc55d93 AG |
111 | } |
112 | ||
f086b830 UH |
113 | if (dmm == DIGITEK_DT4000ZC) |
114 | di = di_digitek_dt4000zc; | |
729b01f9 UH |
115 | if (dmm == TEKPOWER_TP4000ZC) |
116 | di = di_tekpower_tp4000zc; | |
117 | sr_dbg("Selected '%s' subdriver.", di->name); | |
118 | ||
7dc55d93 AG |
119 | di->priv = drvc; |
120 | ||
121 | return SR_OK; | |
122 | } | |
123 | ||
f086b830 UH |
124 | static int hw_init_digitek_dt4000zc(void) |
125 | { | |
126 | return hw_init(DIGITEK_DT4000ZC); | |
127 | } | |
128 | ||
729b01f9 UH |
129 | static int hw_init_tekpower_tp4000zc(void) |
130 | { | |
131 | return hw_init(TEKPOWER_TP4000ZC); | |
132 | } | |
133 | ||
134 | static GSList *scan(const char *conn, const char *serialcomm, int dmm) | |
7dc55d93 AG |
135 | { |
136 | struct sr_dev_inst *sdi; | |
137 | struct drv_context *drvc; | |
138 | struct dev_context *devc; | |
139 | struct sr_probe *probe; | |
d35afa87 | 140 | struct sr_serial_dev_inst *serial; |
7dc55d93 | 141 | GSList *devices; |
2546b05c AG |
142 | int dropped, ret; |
143 | size_t len; | |
144 | uint8_t buf[128]; | |
7dc55d93 | 145 | |
d35afa87 | 146 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
7dc55d93 | 147 | return NULL; |
d35afa87 | 148 | |
a54dd31e | 149 | if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK) |
7dc55d93 | 150 | return NULL; |
7dc55d93 | 151 | |
bbabddbd | 152 | sr_info("Probing port %s readonly.", conn); |
7dc55d93 AG |
153 | |
154 | drvc = di->priv; | |
7dc55d93 | 155 | devices = NULL; |
d35afa87 | 156 | serial_flush(serial); |
bbabddbd UH |
157 | |
158 | /* | |
159 | * There's no way to get an ID from the multimeter. It just sends data | |
160 | * periodically, so the best we can do is check if the packets match | |
161 | * the expected format. | |
162 | */ | |
7dc55d93 | 163 | |
2546b05c AG |
164 | /* Let's get a bit of data and see if we can find a packet. */ |
165 | len = sizeof(buf); | |
7dc55d93 | 166 | |
729b01f9 UH |
167 | ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size, |
168 | dmms[dmm].packet_valid, 1000, | |
169 | dmms[dmm].baudrate); | |
2546b05c AG |
170 | if (ret != SR_OK) |
171 | goto scan_cleanup; | |
7dc55d93 | 172 | |
2546b05c AG |
173 | /* |
174 | * If we dropped more than two packets worth of data, something is | |
175 | * wrong. We shouldn't quit however, since the dropped bytes might be | |
176 | * just zeroes at the beginning of the stream. Those can occur as a | |
177 | * combination of the nonstandard cable that ships with this device and | |
178 | * the serial port or USB to serial adapter. | |
179 | */ | |
729b01f9 UH |
180 | dropped = len - dmms[dmm].packet_size; |
181 | if (dropped > 2 * dmms[dmm].packet_size) | |
6bef68a7 | 182 | sr_warn("Had to drop too much data."); |
7dc55d93 | 183 | |
2546b05c | 184 | sr_info("Found device on port %s.", conn); |
7dc55d93 | 185 | |
729b01f9 UH |
186 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor, |
187 | dmms[dmm].device, ""))) | |
2546b05c | 188 | goto scan_cleanup; |
6bef68a7 | 189 | |
2546b05c AG |
190 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
191 | sr_err("Device context malloc failed."); | |
192 | goto scan_cleanup; | |
7dc55d93 AG |
193 | } |
194 | ||
2546b05c AG |
195 | devc->serial = serial; |
196 | ||
197 | sdi->priv = devc; | |
198 | sdi->driver = di; | |
199 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
200 | goto scan_cleanup; | |
201 | sdi->probes = g_slist_append(sdi->probes, probe); | |
202 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
203 | devices = g_slist_append(devices, sdi); | |
204 | ||
205 | scan_cleanup: | |
d35afa87 | 206 | serial_close(serial); |
2546b05c | 207 | |
7dc55d93 AG |
208 | return devices; |
209 | } | |
210 | ||
211 | static GSList *hw_scan(GSList *options) | |
212 | { | |
213 | struct sr_hwopt *opt; | |
214 | GSList *l, *devices; | |
215 | const char *conn, *serialcomm; | |
729b01f9 | 216 | int dmm; |
7dc55d93 AG |
217 | |
218 | conn = serialcomm = NULL; | |
219 | for (l = options; l; l = l->next) { | |
220 | opt = l->data; | |
221 | switch (opt->hwopt) { | |
222 | case SR_HWOPT_CONN: | |
223 | conn = opt->value; | |
224 | break; | |
225 | case SR_HWOPT_SERIALCOMM: | |
226 | serialcomm = opt->value; | |
227 | break; | |
228 | } | |
229 | } | |
230 | if (!conn) | |
231 | return NULL; | |
232 | ||
f086b830 | 233 | if (!strcmp(di->name, "digitek-dt4000zc")) |
729b01f9 | 234 | dmm = 0; |
f086b830 UH |
235 | if (!strcmp(di->name, "tekpower-tp4000zc")) |
236 | dmm = 1; | |
729b01f9 | 237 | |
7dc55d93 AG |
238 | if (serialcomm) { |
239 | /* Use the provided comm specs. */ | |
729b01f9 | 240 | devices = scan(conn, serialcomm, dmm); |
7dc55d93 | 241 | } else { |
d35afa87 | 242 | /* Try the default. */ |
729b01f9 | 243 | devices = scan(conn, dmms[dmm].conn, dmm); |
7dc55d93 AG |
244 | } |
245 | ||
246 | return devices; | |
247 | } | |
248 | ||
249 | static GSList *hw_dev_list(void) | |
250 | { | |
251 | struct drv_context *drvc; | |
252 | ||
253 | drvc = di->priv; | |
254 | ||
255 | return drvc->instances; | |
256 | } | |
257 | ||
258 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
259 | { | |
260 | struct dev_context *devc; | |
261 | ||
262 | if (!(devc = sdi->priv)) { | |
263 | sr_err("sdi->priv was NULL."); | |
264 | return SR_ERR_BUG; | |
265 | } | |
266 | ||
a54dd31e | 267 | if (serial_open(devc->serial, SERIAL_RDONLY) != SR_OK) |
7dc55d93 | 268 | return SR_ERR; |
bbabddbd | 269 | |
7dc55d93 AG |
270 | sdi->status = SR_ST_ACTIVE; |
271 | ||
272 | return SR_OK; | |
273 | } | |
274 | ||
275 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
276 | { | |
277 | struct dev_context *devc; | |
278 | ||
279 | if (!(devc = sdi->priv)) { | |
280 | sr_err("sdi->priv was NULL."); | |
281 | return SR_ERR_BUG; | |
282 | } | |
283 | ||
284 | if (devc->serial && devc->serial->fd != -1) { | |
d35afa87 | 285 | serial_close(devc->serial); |
7dc55d93 AG |
286 | sdi->status = SR_ST_INACTIVE; |
287 | } | |
288 | ||
289 | return SR_OK; | |
290 | } | |
291 | ||
292 | static int hw_cleanup(void) | |
293 | { | |
294 | clear_instances(); | |
295 | ||
296 | return SR_OK; | |
297 | } | |
298 | ||
299 | static int hw_info_get(int info_id, const void **data, | |
bbabddbd | 300 | const struct sr_dev_inst *sdi) |
7dc55d93 | 301 | { |
bbabddbd | 302 | (void)sdi; |
7dc55d93 AG |
303 | |
304 | switch (info_id) { | |
305 | case SR_DI_HWOPTS: | |
306 | *data = hwopts; | |
307 | break; | |
308 | case SR_DI_HWCAPS: | |
309 | *data = hwcaps; | |
310 | break; | |
311 | case SR_DI_NUM_PROBES: | |
312 | *data = GINT_TO_POINTER(1); | |
313 | break; | |
314 | case SR_DI_PROBE_NAMES: | |
315 | *data = probe_names; | |
316 | break; | |
317 | default: | |
318 | return SR_ERR_ARG; | |
319 | } | |
320 | ||
321 | return SR_OK; | |
322 | } | |
323 | ||
324 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
bbabddbd | 325 | const void *value) |
7dc55d93 AG |
326 | { |
327 | struct dev_context *devc; | |
328 | ||
329 | if (sdi->status != SR_ST_ACTIVE) | |
330 | return SR_ERR; | |
331 | ||
332 | if (!(devc = sdi->priv)) { | |
333 | sr_err("sdi->priv was NULL."); | |
334 | return SR_ERR_BUG; | |
335 | } | |
336 | ||
337 | switch (hwcap) { | |
338 | case SR_HWCAP_LIMIT_SAMPLES: | |
339 | devc->limit_samples = *(const uint64_t *)value; | |
340 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
341 | devc->limit_samples); | |
342 | break; | |
343 | default: | |
344 | sr_err("Unknown capability: %d.", hwcap); | |
345 | return SR_ERR; | |
346 | break; | |
347 | } | |
348 | ||
349 | return SR_OK; | |
350 | } | |
351 | ||
352 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
bbabddbd | 353 | void *cb_data) |
7dc55d93 AG |
354 | { |
355 | struct sr_datafeed_packet packet; | |
356 | struct sr_datafeed_header header; | |
357 | struct sr_datafeed_meta_analog meta; | |
358 | struct dev_context *devc; | |
729b01f9 | 359 | int (*receive_data)(int, int, void *) = NULL; |
7dc55d93 AG |
360 | |
361 | if (!(devc = sdi->priv)) { | |
362 | sr_err("sdi->priv was NULL."); | |
363 | return SR_ERR_BUG; | |
364 | } | |
365 | ||
366 | sr_dbg("Starting acquisition."); | |
367 | ||
368 | devc->cb_data = cb_data; | |
369 | ||
bbabddbd UH |
370 | /* |
371 | * Reset the number of samples to take. If we've already collected our | |
7dc55d93 | 372 | * quota, but we start a new session, and don't reset this, we'll just |
bbabddbd UH |
373 | * quit without acquiring any new samples. |
374 | */ | |
7dc55d93 AG |
375 | devc->num_samples = 0; |
376 | ||
377 | /* Send header packet to the session bus. */ | |
378 | sr_dbg("Sending SR_DF_HEADER."); | |
379 | packet.type = SR_DF_HEADER; | |
380 | packet.payload = (uint8_t *)&header; | |
381 | header.feed_version = 1; | |
382 | gettimeofday(&header.starttime, NULL); | |
383 | sr_session_send(devc->cb_data, &packet); | |
384 | ||
385 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
386 | sr_dbg("Sending SR_DF_META_ANALOG."); | |
387 | packet.type = SR_DF_META_ANALOG; | |
388 | packet.payload = &meta; | |
389 | meta.num_probes = 1; | |
390 | sr_session_send(devc->cb_data, &packet); | |
391 | ||
f086b830 UH |
392 | if (!strcmp(di->name, "digitek-dt4000zc")) |
393 | receive_data = digitek_dt4000zc_receive_data; | |
729b01f9 UH |
394 | if (!strcmp(di->name, "tekpower-tp4000zc")) |
395 | receive_data = tekpower_tp4000zc_receive_data; | |
396 | ||
bbabddbd | 397 | /* Poll every 50ms, or whenever some data comes in. */ |
7dc55d93 | 398 | sr_source_add(devc->serial->fd, G_IO_IN, 50, |
729b01f9 | 399 | receive_data, (void *)sdi); |
7dc55d93 AG |
400 | |
401 | return SR_OK; | |
402 | } | |
403 | ||
69b07d14 | 404 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
7dc55d93 AG |
405 | { |
406 | struct sr_datafeed_packet packet; | |
407 | struct dev_context *devc; | |
408 | ||
409 | if (sdi->status != SR_ST_ACTIVE) | |
410 | return SR_ERR; | |
411 | ||
412 | if (!(devc = sdi->priv)) { | |
413 | sr_err("sdi->priv was NULL."); | |
414 | return SR_ERR_BUG; | |
415 | } | |
416 | ||
417 | sr_dbg("Stopping acquisition."); | |
418 | ||
419 | sr_source_remove(devc->serial->fd); | |
420 | hw_dev_close((struct sr_dev_inst *)sdi); | |
421 | ||
422 | /* Send end packet to the session bus. */ | |
423 | sr_dbg("Sending SR_DF_END."); | |
424 | packet.type = SR_DF_END; | |
425 | sr_session_send(cb_data, &packet); | |
426 | ||
427 | return SR_OK; | |
428 | } | |
429 | ||
f086b830 UH |
430 | SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info = { |
431 | .name = "digitek-dt4000zc", | |
432 | .longname = "Digitek DT4000ZC", | |
433 | .api_version = 1, | |
434 | .init = hw_init_digitek_dt4000zc, | |
435 | .cleanup = hw_cleanup, | |
436 | .scan = hw_scan, | |
437 | .dev_list = hw_dev_list, | |
438 | .dev_clear = clear_instances, | |
439 | .dev_open = hw_dev_open, | |
440 | .dev_close = hw_dev_close, | |
441 | .info_get = hw_info_get, | |
442 | .dev_config_set = hw_dev_config_set, | |
443 | .dev_acquisition_start = hw_dev_acquisition_start, | |
444 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
445 | .priv = NULL, | |
446 | }; | |
447 | ||
729b01f9 UH |
448 | SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = { |
449 | .name = "tekpower-tp4000zc", | |
450 | .longname = "TekPower TP4000ZC", | |
7dc55d93 | 451 | .api_version = 1, |
729b01f9 | 452 | .init = hw_init_tekpower_tp4000zc, |
7dc55d93 AG |
453 | .cleanup = hw_cleanup, |
454 | .scan = hw_scan, | |
455 | .dev_list = hw_dev_list, | |
456 | .dev_clear = clear_instances, | |
457 | .dev_open = hw_dev_open, | |
458 | .dev_close = hw_dev_close, | |
459 | .info_get = hw_info_get, | |
460 | .dev_config_set = hw_dev_config_set, | |
461 | .dev_acquisition_start = hw_dev_acquisition_start, | |
462 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
463 | .priv = NULL, | |
464 | }; |