]> sigrok.org Git - libsigrok.git/blame - hardware/tekpower-dmm/api.c
Drop obsolete genericdmm driver.
[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
7dc55d93
AG
94static GSList *lcd14_scan(const char *conn, const char *serialcomm)
95{
96 struct sr_dev_inst *sdi;
97 struct drv_context *drvc;
98 struct dev_context *devc;
99 struct sr_probe *probe;
d35afa87 100 struct sr_serial_dev_inst *serial;
7dc55d93 101 GSList *devices;
d35afa87 102 int i, len, retry, good_packets = 0, dropped;
8c1adf37 103 uint8_t buf[128], *b;
7dc55d93 104
d35afa87 105 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
7dc55d93 106 return NULL;
d35afa87
BV
107
108 if (serial_open(serial, O_RDONLY|O_NONBLOCK) != SR_OK)
7dc55d93 109 return NULL;
7dc55d93 110
bbabddbd 111 sr_info("Probing port %s readonly.", conn);
7dc55d93
AG
112
113 drvc = di->priv;
114 b = buf;
115 retry = 0;
116 devices = NULL;
d35afa87 117 serial_flush(serial);
bbabddbd
UH
118
119 /*
120 * There's no way to get an ID from the multimeter. It just sends data
121 * periodically, so the best we can do is check if the packets match
122 * the expected format.
123 */
124 while (!devices && retry < 3) {
7dc55d93
AG
125 retry++;
126
bbabddbd 127 /* Let's get a bit of data and see if we can find a packet. */
7dc55d93 128 len = sizeof(buf);
d35afa87 129 serial_readline(serial, (char **)&b, &len, 500);
be5c1d3b 130 if ((len == 0) || (len < FS9721_PACKET_SIZE)) {
bbabddbd 131 /* Not enough data received, is the DMM connected? */
7dc55d93
AG
132 continue;
133 }
134
135 /* Let's treat our buffer like a stream, and find any
136 * valid packets */
be5c1d3b 137 for (i = 0; i < len - FS9721_PACKET_SIZE + 1;) {
8c1adf37 138 if (!sr_fs9721_packet_valid(&buf[i])) {
7dc55d93
AG
139 i++;
140 continue;
141 }
142 good_packets++;
be5c1d3b 143 i += FS9721_PACKET_SIZE;
7dc55d93
AG
144 }
145
bbabddbd
UH
146 /*
147 * If we dropped more than two packets worth of data,
148 * something is wrong.
149 */
be5c1d3b
AG
150 dropped = len - (good_packets * FS9721_PACKET_SIZE);
151 if (dropped > 2 * FS9721_PACKET_SIZE)
7dc55d93
AG
152 continue;
153
bbabddbd 154 /* Let's see if we have anything good. */
7dc55d93
AG
155 if (good_packets == 0)
156 continue;
157
bbabddbd 158 sr_info("Found device on port %s.", conn);
7dc55d93
AG
159
160 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
161 "TP4000ZC", "")))
162 return NULL;
163 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
bbabddbd 164 sr_err("Device context malloc failed.");
7dc55d93
AG
165 return NULL;
166 }
167
d35afa87 168 devc->serial = serial;
7dc55d93
AG
169
170 sdi->priv = devc;
171 sdi->driver = di;
172 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
173 return NULL;
174 sdi->probes = g_slist_append(sdi->probes, probe);
175 drvc->instances = g_slist_append(drvc->instances, sdi);
176 devices = g_slist_append(devices, sdi);
177 break;
178 }
179
d35afa87 180 serial_close(serial);
7dc55d93
AG
181 return devices;
182}
183
184static GSList *hw_scan(GSList *options)
185{
186 struct sr_hwopt *opt;
187 GSList *l, *devices;
188 const char *conn, *serialcomm;
189
190 conn = serialcomm = NULL;
191 for (l = options; l; l = l->next) {
192 opt = l->data;
193 switch (opt->hwopt) {
194 case SR_HWOPT_CONN:
195 conn = opt->value;
196 break;
197 case SR_HWOPT_SERIALCOMM:
198 serialcomm = opt->value;
199 break;
200 }
201 }
202 if (!conn)
203 return NULL;
204
205 if (serialcomm) {
206 /* Use the provided comm specs. */
207 devices = lcd14_scan(conn, serialcomm);
208 } else {
d35afa87
BV
209 /* Try the default. */
210 devices = lcd14_scan(conn, SERIALCOMM);
7dc55d93
AG
211 }
212
213 return devices;
214}
215
216static GSList *hw_dev_list(void)
217{
218 struct drv_context *drvc;
219
220 drvc = di->priv;
221
222 return drvc->instances;
223}
224
225static int hw_dev_open(struct sr_dev_inst *sdi)
226{
227 struct dev_context *devc;
228
229 if (!(devc = sdi->priv)) {
230 sr_err("sdi->priv was NULL.");
231 return SR_ERR_BUG;
232 }
233
d35afa87 234 if (serial_open(devc->serial, O_RDONLY) != SR_OK)
7dc55d93 235 return SR_ERR;
bbabddbd 236
7dc55d93
AG
237 sdi->status = SR_ST_ACTIVE;
238
239 return SR_OK;
240}
241
242static int hw_dev_close(struct sr_dev_inst *sdi)
243{
244 struct dev_context *devc;
245
246 if (!(devc = sdi->priv)) {
247 sr_err("sdi->priv was NULL.");
248 return SR_ERR_BUG;
249 }
250
251 if (devc->serial && devc->serial->fd != -1) {
d35afa87 252 serial_close(devc->serial);
7dc55d93
AG
253 sdi->status = SR_ST_INACTIVE;
254 }
255
256 return SR_OK;
257}
258
259static int hw_cleanup(void)
260{
261 clear_instances();
262
263 return SR_OK;
264}
265
266static int hw_info_get(int info_id, const void **data,
bbabddbd 267 const struct sr_dev_inst *sdi)
7dc55d93 268{
bbabddbd 269 (void)sdi;
7dc55d93
AG
270
271 switch (info_id) {
272 case SR_DI_HWOPTS:
273 *data = hwopts;
274 break;
275 case SR_DI_HWCAPS:
276 *data = hwcaps;
277 break;
278 case SR_DI_NUM_PROBES:
279 *data = GINT_TO_POINTER(1);
280 break;
281 case SR_DI_PROBE_NAMES:
282 *data = probe_names;
283 break;
284 default:
285 return SR_ERR_ARG;
286 }
287
288 return SR_OK;
289}
290
291static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
bbabddbd 292 const void *value)
7dc55d93
AG
293{
294 struct dev_context *devc;
295
296 if (sdi->status != SR_ST_ACTIVE)
297 return SR_ERR;
298
299 if (!(devc = sdi->priv)) {
300 sr_err("sdi->priv was NULL.");
301 return SR_ERR_BUG;
302 }
303
304 switch (hwcap) {
305 case SR_HWCAP_LIMIT_SAMPLES:
306 devc->limit_samples = *(const uint64_t *)value;
307 sr_dbg("Setting sample limit to %" PRIu64 ".",
308 devc->limit_samples);
309 break;
310 default:
311 sr_err("Unknown capability: %d.", hwcap);
312 return SR_ERR;
313 break;
314 }
315
316 return SR_OK;
317}
318
319static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
bbabddbd 320 void *cb_data)
7dc55d93
AG
321{
322 struct sr_datafeed_packet packet;
323 struct sr_datafeed_header header;
324 struct sr_datafeed_meta_analog meta;
325 struct dev_context *devc;
326
327 if (!(devc = sdi->priv)) {
328 sr_err("sdi->priv was NULL.");
329 return SR_ERR_BUG;
330 }
331
332 sr_dbg("Starting acquisition.");
333
334 devc->cb_data = cb_data;
335
bbabddbd
UH
336 /*
337 * Reset the number of samples to take. If we've already collected our
7dc55d93 338 * quota, but we start a new session, and don't reset this, we'll just
bbabddbd
UH
339 * quit without acquiring any new samples.
340 */
7dc55d93
AG
341 devc->num_samples = 0;
342
343 /* Send header packet to the session bus. */
344 sr_dbg("Sending SR_DF_HEADER.");
345 packet.type = SR_DF_HEADER;
346 packet.payload = (uint8_t *)&header;
347 header.feed_version = 1;
348 gettimeofday(&header.starttime, NULL);
349 sr_session_send(devc->cb_data, &packet);
350
351 /* Send metadata about the SR_DF_ANALOG packets to come. */
352 sr_dbg("Sending SR_DF_META_ANALOG.");
353 packet.type = SR_DF_META_ANALOG;
354 packet.payload = &meta;
355 meta.num_probes = 1;
356 sr_session_send(devc->cb_data, &packet);
357
bbabddbd 358 /* Poll every 50ms, or whenever some data comes in. */
7dc55d93 359 sr_source_add(devc->serial->fd, G_IO_IN, 50,
bbabddbd 360 tekpower_dmm_receive_data, (void *)sdi);
7dc55d93
AG
361
362 return SR_OK;
363}
364
69b07d14 365static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
7dc55d93
AG
366{
367 struct sr_datafeed_packet packet;
368 struct dev_context *devc;
369
370 if (sdi->status != SR_ST_ACTIVE)
371 return SR_ERR;
372
373 if (!(devc = sdi->priv)) {
374 sr_err("sdi->priv was NULL.");
375 return SR_ERR_BUG;
376 }
377
378 sr_dbg("Stopping acquisition.");
379
380 sr_source_remove(devc->serial->fd);
381 hw_dev_close((struct sr_dev_inst *)sdi);
382
383 /* Send end packet to the session bus. */
384 sr_dbg("Sending SR_DF_END.");
385 packet.type = SR_DF_END;
386 sr_session_send(cb_data, &packet);
387
388 return SR_OK;
389}
390
bbabddbd 391SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info = {
7dc55d93 392 .name = "tekpower-dmm",
bbabddbd 393 .longname = "TekPower/Digitek TP4000ZC/DT4000ZC DMM",
7dc55d93
AG
394 .api_version = 1,
395 .init = hw_init,
396 .cleanup = hw_cleanup,
397 .scan = hw_scan,
398 .dev_list = hw_dev_list,
399 .dev_clear = clear_instances,
400 .dev_open = hw_dev_open,
401 .dev_close = hw_dev_close,
402 .info_get = hw_info_get,
403 .dev_config_set = hw_dev_config_set,
404 .dev_acquisition_start = hw_dev_acquisition_start,
405 .dev_acquisition_stop = hw_dev_acquisition_stop,
406 .priv = NULL,
407};