]> sigrok.org Git - libsigrok.git/blame - hardware/tekpower-dmm/api.c
tekpower-dmm: Improve serial detection.
[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/**
97 * Try to find a valid packet in a serial data stream
98 *
99 * @param fd File descriptor of the serial port.
100 * @param buf Buffer containing the bytes to write.
101 * @param count Size of the buffer.
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 abort
105 * scanning.
106 * @param baudrate the baudrate of the serial port. This parameter is not
107 * critical, but it helps fine tune the serial port polling
108 * delay
109 *
110 * @return SR_OK if a valid packet is found within he given timeout,
111 * SR_ERR upon failure.
112 */
113static int serial_stream_detect(struct sr_serial_dev_inst *serial,
114 uint8_t *buf, size_t *buflen,
115 const size_t packet_size,
116 packet_valid_t is_valid,
117 uint64_t timeout_ms, int baudrate)
118{
119 uint64_t start;
120 uint64_t time;
121 uint64_t byte_delay_us;
122 size_t ibuf, i;
123 int len;
124 const size_t maxlen = *buflen;
125
126 if(maxlen < (packet_size << 1) ) {
127 sr_err("Buffer size must be at least twice the packet size");
128 return SR_ERR;
129 }
130
131 timeout_ms *= 1000;
132 /* Assume 8n1 transmission. That is 10 bits for every byte */
133 byte_delay_us = 10000000 / baudrate;
134 start = g_get_monotonic_time();
135
136 i = ibuf = len = 0;
137 while (ibuf < maxlen) {
138 len = serial_read(serial, &buf[ibuf], 1);
139 if (len > 0)
140 ibuf+= len;
141 if ((ibuf - i) >= packet_size) {
142 /* We have at least a packet's worth of data */
143 if (is_valid(&buf[i])) {
144 time = g_get_monotonic_time()-start;
145 time /= 1000;
146 sr_spew("Serial detection took %li ms", time);
147 *buflen = ibuf;
148 return SR_OK;
149 }
150 /* Not a valid packet; continue searching */
151 i++;
152 }
153 if (g_get_monotonic_time() - start > timeout_ms) {
154 /* Timeout */
155 sr_warn("Serial detection timeout");
156 break;
157 }
158 g_usleep(byte_delay_us);
159 }
160
161 *buflen = ibuf;
162 return SR_ERR;
163
164}
165
7dc55d93
AG
166static GSList *lcd14_scan(const char *conn, const char *serialcomm)
167{
168 struct sr_dev_inst *sdi;
169 struct drv_context *drvc;
170 struct dev_context *devc;
171 struct sr_probe *probe;
d35afa87 172 struct sr_serial_dev_inst *serial;
7dc55d93 173 GSList *devices;
2546b05c
AG
174 int dropped, ret;
175 size_t len;
176 uint8_t buf[128];
7dc55d93 177
d35afa87 178 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
7dc55d93 179 return NULL;
d35afa87
BV
180
181 if (serial_open(serial, O_RDONLY|O_NONBLOCK) != SR_OK)
7dc55d93 182 return NULL;
7dc55d93 183
bbabddbd 184 sr_info("Probing port %s readonly.", conn);
7dc55d93
AG
185
186 drvc = di->priv;
7dc55d93 187 devices = NULL;
d35afa87 188 serial_flush(serial);
bbabddbd
UH
189
190 /*
191 * There's no way to get an ID from the multimeter. It just sends data
192 * periodically, so the best we can do is check if the packets match
193 * the expected format.
194 */
7dc55d93 195
2546b05c
AG
196 /* Let's get a bit of data and see if we can find a packet. */
197 len = sizeof(buf);
7dc55d93 198
2546b05c
AG
199 ret = serial_stream_detect(serial, buf, &len, FS9721_PACKET_SIZE,
200 sr_fs9721_packet_valid, 1000, 2400);
201 if (ret != SR_OK)
202 goto scan_cleanup;
7dc55d93 203
2546b05c
AG
204 /*
205 * If we dropped more than two packets worth of data, something is
206 * wrong. We shouldn't quit however, since the dropped bytes might be
207 * just zeroes at the beginning of the stream. Those can occur as a
208 * combination of the nonstandard cable that ships with this device and
209 * the serial port or USB to serial adapter.
210 */
211 dropped = len - FS9721_PACKET_SIZE;
212 if (dropped > 2 * FS9721_PACKET_SIZE) {
7dc55d93 213
2546b05c
AG
214 sr_warn("Had to drop too much data");
215 }
7dc55d93 216
2546b05c 217 sr_info("Found device on port %s.", conn);
7dc55d93 218
2546b05c
AG
219 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
220 "TP4000ZC", "")))
221 goto scan_cleanup;
222 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
223 sr_err("Device context malloc failed.");
224 goto scan_cleanup;
7dc55d93
AG
225 }
226
2546b05c
AG
227 devc->serial = serial;
228
229 sdi->priv = devc;
230 sdi->driver = di;
231 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
232 goto scan_cleanup;
233 sdi->probes = g_slist_append(sdi->probes, probe);
234 drvc->instances = g_slist_append(drvc->instances, sdi);
235 devices = g_slist_append(devices, sdi);
236
237scan_cleanup:
d35afa87 238 serial_close(serial);
2546b05c 239
7dc55d93
AG
240 return devices;
241}
242
243static GSList *hw_scan(GSList *options)
244{
245 struct sr_hwopt *opt;
246 GSList *l, *devices;
247 const char *conn, *serialcomm;
248
249 conn = serialcomm = NULL;
250 for (l = options; l; l = l->next) {
251 opt = l->data;
252 switch (opt->hwopt) {
253 case SR_HWOPT_CONN:
254 conn = opt->value;
255 break;
256 case SR_HWOPT_SERIALCOMM:
257 serialcomm = opt->value;
258 break;
259 }
260 }
261 if (!conn)
262 return NULL;
263
264 if (serialcomm) {
265 /* Use the provided comm specs. */
266 devices = lcd14_scan(conn, serialcomm);
267 } else {
d35afa87
BV
268 /* Try the default. */
269 devices = lcd14_scan(conn, SERIALCOMM);
7dc55d93
AG
270 }
271
272 return devices;
273}
274
275static GSList *hw_dev_list(void)
276{
277 struct drv_context *drvc;
278
279 drvc = di->priv;
280
281 return drvc->instances;
282}
283
284static int hw_dev_open(struct sr_dev_inst *sdi)
285{
286 struct dev_context *devc;
287
288 if (!(devc = sdi->priv)) {
289 sr_err("sdi->priv was NULL.");
290 return SR_ERR_BUG;
291 }
292
d35afa87 293 if (serial_open(devc->serial, O_RDONLY) != SR_OK)
7dc55d93 294 return SR_ERR;
bbabddbd 295
7dc55d93
AG
296 sdi->status = SR_ST_ACTIVE;
297
298 return SR_OK;
299}
300
301static int hw_dev_close(struct sr_dev_inst *sdi)
302{
303 struct dev_context *devc;
304
305 if (!(devc = sdi->priv)) {
306 sr_err("sdi->priv was NULL.");
307 return SR_ERR_BUG;
308 }
309
310 if (devc->serial && devc->serial->fd != -1) {
d35afa87 311 serial_close(devc->serial);
7dc55d93
AG
312 sdi->status = SR_ST_INACTIVE;
313 }
314
315 return SR_OK;
316}
317
318static int hw_cleanup(void)
319{
320 clear_instances();
321
322 return SR_OK;
323}
324
325static int hw_info_get(int info_id, const void **data,
bbabddbd 326 const struct sr_dev_inst *sdi)
7dc55d93 327{
bbabddbd 328 (void)sdi;
7dc55d93
AG
329
330 switch (info_id) {
331 case SR_DI_HWOPTS:
332 *data = hwopts;
333 break;
334 case SR_DI_HWCAPS:
335 *data = hwcaps;
336 break;
337 case SR_DI_NUM_PROBES:
338 *data = GINT_TO_POINTER(1);
339 break;
340 case SR_DI_PROBE_NAMES:
341 *data = probe_names;
342 break;
343 default:
344 return SR_ERR_ARG;
345 }
346
347 return SR_OK;
348}
349
350static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
bbabddbd 351 const void *value)
7dc55d93
AG
352{
353 struct dev_context *devc;
354
355 if (sdi->status != SR_ST_ACTIVE)
356 return SR_ERR;
357
358 if (!(devc = sdi->priv)) {
359 sr_err("sdi->priv was NULL.");
360 return SR_ERR_BUG;
361 }
362
363 switch (hwcap) {
364 case SR_HWCAP_LIMIT_SAMPLES:
365 devc->limit_samples = *(const uint64_t *)value;
366 sr_dbg("Setting sample limit to %" PRIu64 ".",
367 devc->limit_samples);
368 break;
369 default:
370 sr_err("Unknown capability: %d.", hwcap);
371 return SR_ERR;
372 break;
373 }
374
375 return SR_OK;
376}
377
378static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
bbabddbd 379 void *cb_data)
7dc55d93
AG
380{
381 struct sr_datafeed_packet packet;
382 struct sr_datafeed_header header;
383 struct sr_datafeed_meta_analog meta;
384 struct dev_context *devc;
385
386 if (!(devc = sdi->priv)) {
387 sr_err("sdi->priv was NULL.");
388 return SR_ERR_BUG;
389 }
390
391 sr_dbg("Starting acquisition.");
392
393 devc->cb_data = cb_data;
394
bbabddbd
UH
395 /*
396 * Reset the number of samples to take. If we've already collected our
7dc55d93 397 * quota, but we start a new session, and don't reset this, we'll just
bbabddbd
UH
398 * quit without acquiring any new samples.
399 */
7dc55d93
AG
400 devc->num_samples = 0;
401
402 /* Send header packet to the session bus. */
403 sr_dbg("Sending SR_DF_HEADER.");
404 packet.type = SR_DF_HEADER;
405 packet.payload = (uint8_t *)&header;
406 header.feed_version = 1;
407 gettimeofday(&header.starttime, NULL);
408 sr_session_send(devc->cb_data, &packet);
409
410 /* Send metadata about the SR_DF_ANALOG packets to come. */
411 sr_dbg("Sending SR_DF_META_ANALOG.");
412 packet.type = SR_DF_META_ANALOG;
413 packet.payload = &meta;
414 meta.num_probes = 1;
415 sr_session_send(devc->cb_data, &packet);
416
bbabddbd 417 /* Poll every 50ms, or whenever some data comes in. */
7dc55d93 418 sr_source_add(devc->serial->fd, G_IO_IN, 50,
bbabddbd 419 tekpower_dmm_receive_data, (void *)sdi);
7dc55d93
AG
420
421 return SR_OK;
422}
423
69b07d14 424static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
7dc55d93
AG
425{
426 struct sr_datafeed_packet packet;
427 struct dev_context *devc;
428
429 if (sdi->status != SR_ST_ACTIVE)
430 return SR_ERR;
431
432 if (!(devc = sdi->priv)) {
433 sr_err("sdi->priv was NULL.");
434 return SR_ERR_BUG;
435 }
436
437 sr_dbg("Stopping acquisition.");
438
439 sr_source_remove(devc->serial->fd);
440 hw_dev_close((struct sr_dev_inst *)sdi);
441
442 /* Send end packet to the session bus. */
443 sr_dbg("Sending SR_DF_END.");
444 packet.type = SR_DF_END;
445 sr_session_send(cb_data, &packet);
446
447 return SR_OK;
448}
449
bbabddbd 450SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info = {
7dc55d93 451 .name = "tekpower-dmm",
bbabddbd 452 .longname = "TekPower/Digitek TP4000ZC/DT4000ZC DMM",
7dc55d93
AG
453 .api_version = 1,
454 .init = hw_init,
455 .cleanup = hw_cleanup,
456 .scan = hw_scan,
457 .dev_list = hw_dev_list,
458 .dev_clear = clear_instances,
459 .dev_open = hw_dev_open,
460 .dev_close = hw_dev_close,
461 .info_get = hw_info_get,
462 .dev_config_set = hw_dev_config_set,
463 .dev_acquisition_start = hw_dev_acquisition_start,
464 .dev_acquisition_stop = hw_dev_acquisition_stop,
465 .priv = NULL,
466};