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