]>
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; |
ce3777ad | 52 | SR_PRIV struct sr_dev_driver metex_me31_driver_info; |
f086b830 | 53 | |
ce3777ad UH |
54 | static struct sr_dev_driver *di_dt4000zc = &digitek_dt4000zc_driver_info; |
55 | static struct sr_dev_driver *di_tp4000zc = &tekpower_tp4000zc_driver_info; | |
56 | static struct sr_dev_driver *di_me31 = &metex_me31_driver_info; | |
729b01f9 UH |
57 | |
58 | /* After hw_init() this will point to a device-specific entry (see above). */ | |
59 | static struct sr_dev_driver *di = NULL; | |
60 | ||
61 | SR_PRIV struct dmm_info dmms[] = { | |
f086b830 | 62 | { |
ce3777ad UH |
63 | "Digitek", "DT4000ZC", "2400/8n1", 2400, |
64 | FS9721_PACKET_SIZE, NULL, | |
65 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
f086b830 UH |
66 | dmm_details_dt4000zc, |
67 | }, | |
729b01f9 | 68 | { |
ce3777ad UH |
69 | "TekPower", "TP4000ZC", "2400/8n1", 2400, |
70 | FS9721_PACKET_SIZE, NULL, | |
71 | sr_fs9721_packet_valid, sr_fs9721_parse, | |
729b01f9 UH |
72 | dmm_details_tp4000zc, |
73 | }, | |
ce3777ad UH |
74 | { |
75 | "Metex", "ME-31", "600/7n2/rts=0/dtr=1", 600, | |
76 | METEX14_PACKET_SIZE, sr_metex14_packet_request, | |
77 | sr_metex14_packet_valid, sr_metex14_parse, | |
78 | NULL, | |
79 | }, | |
729b01f9 | 80 | }; |
7dc55d93 AG |
81 | |
82 | /* Properly close and free all devices. */ | |
83 | static int clear_instances(void) | |
84 | { | |
85 | struct sr_dev_inst *sdi; | |
86 | struct drv_context *drvc; | |
87 | struct dev_context *devc; | |
88 | GSList *l; | |
89 | ||
90 | if (!(drvc = di->priv)) | |
91 | return SR_OK; | |
92 | ||
93 | drvc = di->priv; | |
94 | for (l = drvc->instances; l; l = l->next) { | |
95 | if (!(sdi = l->data)) | |
96 | continue; | |
97 | if (!(devc = sdi->priv)) | |
98 | continue; | |
99 | sr_serial_dev_inst_free(devc->serial); | |
100 | sr_dev_inst_free(sdi); | |
101 | } | |
102 | g_slist_free(drvc->instances); | |
103 | drvc->instances = NULL; | |
104 | ||
105 | return SR_OK; | |
106 | } | |
107 | ||
729b01f9 | 108 | static int hw_init(int dmm) |
7dc55d93 AG |
109 | { |
110 | struct drv_context *drvc; | |
111 | ||
112 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
bbabddbd UH |
113 | sr_err("Driver context malloc failed."); |
114 | return SR_ERR_MALLOC; | |
7dc55d93 AG |
115 | } |
116 | ||
f086b830 | 117 | if (dmm == DIGITEK_DT4000ZC) |
ce3777ad | 118 | di = di_dt4000zc; |
729b01f9 | 119 | if (dmm == TEKPOWER_TP4000ZC) |
ce3777ad UH |
120 | di = di_tp4000zc; |
121 | if (dmm == METEX_ME31) | |
122 | di = di_me31; | |
729b01f9 UH |
123 | sr_dbg("Selected '%s' subdriver.", di->name); |
124 | ||
7dc55d93 AG |
125 | di->priv = drvc; |
126 | ||
127 | return SR_OK; | |
128 | } | |
129 | ||
f086b830 UH |
130 | static int hw_init_digitek_dt4000zc(void) |
131 | { | |
132 | return hw_init(DIGITEK_DT4000ZC); | |
133 | } | |
134 | ||
729b01f9 UH |
135 | static int hw_init_tekpower_tp4000zc(void) |
136 | { | |
137 | return hw_init(TEKPOWER_TP4000ZC); | |
138 | } | |
139 | ||
ce3777ad UH |
140 | static int hw_init_metex_me31(void) |
141 | { | |
142 | return hw_init(METEX_ME31); | |
143 | } | |
144 | ||
729b01f9 | 145 | static GSList *scan(const char *conn, const char *serialcomm, int dmm) |
7dc55d93 AG |
146 | { |
147 | struct sr_dev_inst *sdi; | |
148 | struct drv_context *drvc; | |
149 | struct dev_context *devc; | |
150 | struct sr_probe *probe; | |
d35afa87 | 151 | struct sr_serial_dev_inst *serial; |
7dc55d93 | 152 | GSList *devices; |
2546b05c AG |
153 | int dropped, ret; |
154 | size_t len; | |
155 | uint8_t buf[128]; | |
7dc55d93 | 156 | |
d35afa87 | 157 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
7dc55d93 | 158 | return NULL; |
d35afa87 | 159 | |
ce3777ad | 160 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
7dc55d93 | 161 | return NULL; |
7dc55d93 | 162 | |
ce3777ad | 163 | sr_info("Probing port %s.", conn); |
7dc55d93 AG |
164 | |
165 | drvc = di->priv; | |
7dc55d93 | 166 | devices = NULL; |
d35afa87 | 167 | serial_flush(serial); |
bbabddbd UH |
168 | |
169 | /* | |
170 | * There's no way to get an ID from the multimeter. It just sends data | |
171 | * periodically, so the best we can do is check if the packets match | |
172 | * the expected format. | |
173 | */ | |
7dc55d93 | 174 | |
2546b05c AG |
175 | /* Let's get a bit of data and see if we can find a packet. */ |
176 | len = sizeof(buf); | |
7dc55d93 | 177 | |
ce3777ad UH |
178 | /* Request a packet if the DMM requires this. */ |
179 | if (dmms[dmm].packet_request) { | |
180 | if ((ret = dmms[dmm].packet_request(serial)) < 0) { | |
181 | sr_err("Failed to request packet: %d.", ret); | |
182 | return FALSE; | |
183 | } | |
184 | } | |
185 | ||
729b01f9 UH |
186 | ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size, |
187 | dmms[dmm].packet_valid, 1000, | |
188 | dmms[dmm].baudrate); | |
2546b05c AG |
189 | if (ret != SR_OK) |
190 | goto scan_cleanup; | |
7dc55d93 | 191 | |
2546b05c AG |
192 | /* |
193 | * If we dropped more than two packets worth of data, something is | |
194 | * wrong. We shouldn't quit however, since the dropped bytes might be | |
195 | * just zeroes at the beginning of the stream. Those can occur as a | |
196 | * combination of the nonstandard cable that ships with this device and | |
197 | * the serial port or USB to serial adapter. | |
198 | */ | |
729b01f9 UH |
199 | dropped = len - dmms[dmm].packet_size; |
200 | if (dropped > 2 * dmms[dmm].packet_size) | |
6bef68a7 | 201 | sr_warn("Had to drop too much data."); |
7dc55d93 | 202 | |
2546b05c | 203 | sr_info("Found device on port %s.", conn); |
7dc55d93 | 204 | |
729b01f9 UH |
205 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor, |
206 | dmms[dmm].device, ""))) | |
2546b05c | 207 | goto scan_cleanup; |
6bef68a7 | 208 | |
2546b05c AG |
209 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
210 | sr_err("Device context malloc failed."); | |
211 | goto scan_cleanup; | |
7dc55d93 AG |
212 | } |
213 | ||
2546b05c AG |
214 | devc->serial = serial; |
215 | ||
216 | sdi->priv = devc; | |
217 | sdi->driver = di; | |
218 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
219 | goto scan_cleanup; | |
220 | sdi->probes = g_slist_append(sdi->probes, probe); | |
221 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
222 | devices = g_slist_append(devices, sdi); | |
223 | ||
224 | scan_cleanup: | |
d35afa87 | 225 | serial_close(serial); |
2546b05c | 226 | |
7dc55d93 AG |
227 | return devices; |
228 | } | |
229 | ||
230 | static GSList *hw_scan(GSList *options) | |
231 | { | |
232 | struct sr_hwopt *opt; | |
233 | GSList *l, *devices; | |
234 | const char *conn, *serialcomm; | |
729b01f9 | 235 | int dmm; |
7dc55d93 AG |
236 | |
237 | conn = serialcomm = NULL; | |
238 | for (l = options; l; l = l->next) { | |
239 | opt = l->data; | |
240 | switch (opt->hwopt) { | |
241 | case SR_HWOPT_CONN: | |
242 | conn = opt->value; | |
243 | break; | |
244 | case SR_HWOPT_SERIALCOMM: | |
245 | serialcomm = opt->value; | |
246 | break; | |
247 | } | |
248 | } | |
249 | if (!conn) | |
250 | return NULL; | |
251 | ||
f086b830 | 252 | if (!strcmp(di->name, "digitek-dt4000zc")) |
729b01f9 | 253 | dmm = 0; |
f086b830 UH |
254 | if (!strcmp(di->name, "tekpower-tp4000zc")) |
255 | dmm = 1; | |
ce3777ad UH |
256 | if (!strcmp(di->name, "metex-me31")) |
257 | dmm = 2; | |
729b01f9 | 258 | |
7dc55d93 AG |
259 | if (serialcomm) { |
260 | /* Use the provided comm specs. */ | |
729b01f9 | 261 | devices = scan(conn, serialcomm, dmm); |
7dc55d93 | 262 | } else { |
d35afa87 | 263 | /* Try the default. */ |
729b01f9 | 264 | devices = scan(conn, dmms[dmm].conn, dmm); |
7dc55d93 AG |
265 | } |
266 | ||
267 | return devices; | |
268 | } | |
269 | ||
270 | static GSList *hw_dev_list(void) | |
271 | { | |
272 | struct drv_context *drvc; | |
273 | ||
274 | drvc = di->priv; | |
275 | ||
276 | return drvc->instances; | |
277 | } | |
278 | ||
279 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
280 | { | |
281 | struct dev_context *devc; | |
282 | ||
283 | if (!(devc = sdi->priv)) { | |
284 | sr_err("sdi->priv was NULL."); | |
285 | return SR_ERR_BUG; | |
286 | } | |
287 | ||
ce3777ad | 288 | if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
7dc55d93 | 289 | return SR_ERR; |
bbabddbd | 290 | |
7dc55d93 AG |
291 | sdi->status = SR_ST_ACTIVE; |
292 | ||
293 | return SR_OK; | |
294 | } | |
295 | ||
296 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
297 | { | |
298 | struct dev_context *devc; | |
299 | ||
300 | if (!(devc = sdi->priv)) { | |
301 | sr_err("sdi->priv was NULL."); | |
302 | return SR_ERR_BUG; | |
303 | } | |
304 | ||
305 | if (devc->serial && devc->serial->fd != -1) { | |
d35afa87 | 306 | serial_close(devc->serial); |
7dc55d93 AG |
307 | sdi->status = SR_ST_INACTIVE; |
308 | } | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
313 | static int hw_cleanup(void) | |
314 | { | |
315 | clear_instances(); | |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | static int hw_info_get(int info_id, const void **data, | |
bbabddbd | 321 | const struct sr_dev_inst *sdi) |
7dc55d93 | 322 | { |
bbabddbd | 323 | (void)sdi; |
7dc55d93 AG |
324 | |
325 | switch (info_id) { | |
326 | case SR_DI_HWOPTS: | |
327 | *data = hwopts; | |
328 | break; | |
329 | case SR_DI_HWCAPS: | |
330 | *data = hwcaps; | |
331 | break; | |
332 | case SR_DI_NUM_PROBES: | |
333 | *data = GINT_TO_POINTER(1); | |
334 | break; | |
335 | case SR_DI_PROBE_NAMES: | |
336 | *data = probe_names; | |
337 | break; | |
338 | default: | |
339 | return SR_ERR_ARG; | |
340 | } | |
341 | ||
342 | return SR_OK; | |
343 | } | |
344 | ||
345 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
bbabddbd | 346 | const void *value) |
7dc55d93 AG |
347 | { |
348 | struct dev_context *devc; | |
349 | ||
350 | if (sdi->status != SR_ST_ACTIVE) | |
351 | return SR_ERR; | |
352 | ||
353 | if (!(devc = sdi->priv)) { | |
354 | sr_err("sdi->priv was NULL."); | |
355 | return SR_ERR_BUG; | |
356 | } | |
357 | ||
358 | switch (hwcap) { | |
359 | case SR_HWCAP_LIMIT_SAMPLES: | |
360 | devc->limit_samples = *(const uint64_t *)value; | |
361 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
362 | devc->limit_samples); | |
363 | break; | |
364 | default: | |
365 | sr_err("Unknown capability: %d.", hwcap); | |
366 | return SR_ERR; | |
367 | break; | |
368 | } | |
369 | ||
370 | return SR_OK; | |
371 | } | |
372 | ||
373 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
bbabddbd | 374 | void *cb_data) |
7dc55d93 AG |
375 | { |
376 | struct sr_datafeed_packet packet; | |
377 | struct sr_datafeed_header header; | |
378 | struct sr_datafeed_meta_analog meta; | |
379 | struct dev_context *devc; | |
729b01f9 | 380 | int (*receive_data)(int, int, void *) = NULL; |
7dc55d93 AG |
381 | |
382 | if (!(devc = sdi->priv)) { | |
383 | sr_err("sdi->priv was NULL."); | |
384 | return SR_ERR_BUG; | |
385 | } | |
386 | ||
387 | sr_dbg("Starting acquisition."); | |
388 | ||
389 | devc->cb_data = cb_data; | |
390 | ||
bbabddbd UH |
391 | /* |
392 | * Reset the number of samples to take. If we've already collected our | |
7dc55d93 | 393 | * quota, but we start a new session, and don't reset this, we'll just |
bbabddbd UH |
394 | * quit without acquiring any new samples. |
395 | */ | |
7dc55d93 AG |
396 | devc->num_samples = 0; |
397 | ||
398 | /* Send header packet to the session bus. */ | |
399 | sr_dbg("Sending SR_DF_HEADER."); | |
400 | packet.type = SR_DF_HEADER; | |
401 | packet.payload = (uint8_t *)&header; | |
402 | header.feed_version = 1; | |
403 | gettimeofday(&header.starttime, NULL); | |
404 | sr_session_send(devc->cb_data, &packet); | |
405 | ||
406 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
407 | sr_dbg("Sending SR_DF_META_ANALOG."); | |
408 | packet.type = SR_DF_META_ANALOG; | |
409 | packet.payload = &meta; | |
410 | meta.num_probes = 1; | |
411 | sr_session_send(devc->cb_data, &packet); | |
412 | ||
f086b830 UH |
413 | if (!strcmp(di->name, "digitek-dt4000zc")) |
414 | receive_data = digitek_dt4000zc_receive_data; | |
729b01f9 UH |
415 | if (!strcmp(di->name, "tekpower-tp4000zc")) |
416 | receive_data = tekpower_tp4000zc_receive_data; | |
ce3777ad UH |
417 | if (!strcmp(di->name, "metex-me31")) |
418 | receive_data = metex_me31_receive_data; | |
729b01f9 | 419 | |
bbabddbd | 420 | /* Poll every 50ms, or whenever some data comes in. */ |
7dc55d93 | 421 | sr_source_add(devc->serial->fd, G_IO_IN, 50, |
729b01f9 | 422 | receive_data, (void *)sdi); |
7dc55d93 AG |
423 | |
424 | return SR_OK; | |
425 | } | |
426 | ||
69b07d14 | 427 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
7dc55d93 AG |
428 | { |
429 | struct sr_datafeed_packet packet; | |
430 | struct dev_context *devc; | |
431 | ||
432 | if (sdi->status != SR_ST_ACTIVE) | |
433 | return SR_ERR; | |
434 | ||
435 | if (!(devc = sdi->priv)) { | |
436 | sr_err("sdi->priv was NULL."); | |
437 | return SR_ERR_BUG; | |
438 | } | |
439 | ||
440 | sr_dbg("Stopping acquisition."); | |
441 | ||
442 | sr_source_remove(devc->serial->fd); | |
443 | hw_dev_close((struct sr_dev_inst *)sdi); | |
444 | ||
445 | /* Send end packet to the session bus. */ | |
446 | sr_dbg("Sending SR_DF_END."); | |
447 | packet.type = SR_DF_END; | |
448 | sr_session_send(cb_data, &packet); | |
449 | ||
450 | return SR_OK; | |
451 | } | |
452 | ||
f086b830 UH |
453 | SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info = { |
454 | .name = "digitek-dt4000zc", | |
455 | .longname = "Digitek DT4000ZC", | |
456 | .api_version = 1, | |
457 | .init = hw_init_digitek_dt4000zc, | |
458 | .cleanup = hw_cleanup, | |
459 | .scan = hw_scan, | |
460 | .dev_list = hw_dev_list, | |
461 | .dev_clear = clear_instances, | |
462 | .dev_open = hw_dev_open, | |
463 | .dev_close = hw_dev_close, | |
464 | .info_get = hw_info_get, | |
465 | .dev_config_set = hw_dev_config_set, | |
466 | .dev_acquisition_start = hw_dev_acquisition_start, | |
467 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
468 | .priv = NULL, | |
469 | }; | |
470 | ||
729b01f9 UH |
471 | SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = { |
472 | .name = "tekpower-tp4000zc", | |
473 | .longname = "TekPower TP4000ZC", | |
7dc55d93 | 474 | .api_version = 1, |
729b01f9 | 475 | .init = hw_init_tekpower_tp4000zc, |
7dc55d93 AG |
476 | .cleanup = hw_cleanup, |
477 | .scan = hw_scan, | |
478 | .dev_list = hw_dev_list, | |
479 | .dev_clear = clear_instances, | |
480 | .dev_open = hw_dev_open, | |
481 | .dev_close = hw_dev_close, | |
482 | .info_get = hw_info_get, | |
483 | .dev_config_set = hw_dev_config_set, | |
484 | .dev_acquisition_start = hw_dev_acquisition_start, | |
485 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
486 | .priv = NULL, | |
487 | }; | |
ce3777ad UH |
488 | |
489 | SR_PRIV struct sr_dev_driver metex_me31_driver_info = { | |
490 | .name = "metex-me31", | |
491 | .longname = "Metex ME-31", | |
492 | .api_version = 1, | |
493 | .init = hw_init_metex_me31, | |
494 | .cleanup = hw_cleanup, | |
495 | .scan = hw_scan, | |
496 | .dev_list = hw_dev_list, | |
497 | .dev_clear = clear_instances, | |
498 | .dev_open = hw_dev_open, | |
499 | .dev_close = hw_dev_close, | |
500 | .info_get = hw_info_get, | |
501 | .dev_config_set = hw_dev_config_set, | |
502 | .dev_acquisition_start = hw_dev_acquisition_start, | |
503 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
504 | .priv = NULL, | |
505 | }; |