]> sigrok.org Git - libsigrok.git/blob - hardware/tekpower-dmm/api.c
tekpower-dmm: Cosmetics, coding-style, consistency fixes.
[libsigrok.git] / hardware / tekpower-dmm / api.c
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>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 static const int hwopts[] = {
32         SR_HWOPT_CONN,
33         SR_HWOPT_SERIALCOMM,
34         0,
35 };
36
37 static const int hwcaps[] = {
38         SR_HWCAP_MULTIMETER,
39         SR_HWCAP_LIMIT_SAMPLES,
40         SR_HWCAP_CONTINUOUS,
41         0,
42 };
43
44 static const char *probe_names[] = {
45         "Probe",
46         NULL,
47 };
48
49 SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info;
50 static struct sr_dev_driver *di = &tekpower_dmm_driver_info;
51
52 /* Properly close and free all devices. */
53 static int clear_instances(void)
54 {
55         struct sr_dev_inst *sdi;
56         struct drv_context *drvc;
57         struct dev_context *devc;
58         GSList *l;
59
60         if (!(drvc = di->priv))
61                 return SR_OK;
62
63         drvc = di->priv;
64         for (l = drvc->instances; l; l = l->next) {
65                 if (!(sdi = l->data))
66                         continue;
67                 if (!(devc = sdi->priv))
68                         continue;
69                 sr_serial_dev_inst_free(devc->serial);
70                 sr_dev_inst_free(sdi);
71         }
72         g_slist_free(drvc->instances);
73         drvc->instances = NULL;
74
75         return SR_OK;
76 }
77
78 static int hw_init(void)
79 {
80         struct drv_context *drvc;
81
82         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
83                 sr_err("Driver context malloc failed.");
84                 return SR_ERR_MALLOC;
85         }
86
87         di->priv = drvc;
88
89         return SR_OK;
90 }
91
92 static int serial_readline(int fd, char **buf, int *buflen,
93                            uint64_t timeout_ms)
94 {
95         uint64_t start;
96         int maxlen, len;
97
98         timeout_ms *= 1000;
99         start = g_get_monotonic_time();
100
101         maxlen = *buflen;
102         *buflen = len = 0;
103         while (1) {
104                 len = maxlen - *buflen - 1;
105                 if (len < 1)
106                         break;
107                 len = serial_read(fd, *buf + *buflen, 1);
108                 if (len > 0) {
109                         *buflen += len;
110                         *(*buf + *buflen) = '\0';
111                         if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
112                                 /* Strip LF and terminate. */
113                                 *(*buf + --*buflen) = '\0';
114                                 break;
115                         }
116                 }
117                 if (g_get_monotonic_time() - start > timeout_ms)
118                         /* Timeout */
119                         break;
120                 g_usleep(2000);
121         }
122
123         return SR_OK;
124 }
125
126 static GSList *lcd14_scan(const char *conn, const char *serialcomm)
127 {
128         struct sr_dev_inst *sdi;
129         struct drv_context *drvc;
130         struct dev_context *devc;
131         struct sr_probe *probe;
132         struct lcd14_packet *packet;
133         GSList *devices;
134         int i, len, fd, retry, good_packets = 0, dropped, ret;
135         char buf[128], *b;
136
137         if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) == -1) {
138                 sr_err("Unable to open %s: %s.", conn, strerror(errno));
139                 return NULL;
140         }
141         if ((ret = serial_set_paramstr(fd, serialcomm)) != SR_OK) {
142                 sr_err("Unable to set serial parameters: %d", ret);
143                 return NULL;
144         }
145
146         sr_info("Probing port %s readonly.", conn);
147
148         drvc = di->priv;
149         b = buf;
150         retry = 0;
151         devices = NULL;
152         serial_flush(fd);
153
154         /*
155          * There's no way to get an ID from the multimeter. It just sends data
156          * periodically, so the best we can do is check if the packets match
157          * the expected format.
158          */
159         while (!devices && retry < 3) {
160                 retry++;
161
162                 /* Let's get a bit of data and see if we can find a packet. */
163                 len = sizeof(buf);
164                 serial_readline(fd, &b, &len, 500);
165                 if ((len == 0) || (len < LCD14_PACKET_SIZE)) {
166                         /* Not enough data received, is the DMM connected? */
167                         continue;
168                 }
169
170                 /* Let's treat our buffer like a stream, and find any
171                  * valid packets */
172                 for (i = 0; i < len - LCD14_PACKET_SIZE + 1;) {
173                         packet = (void *)(&buf[i]);
174                         if (!lcd14_is_packet_valid(packet, NULL)) {
175                                 i++;
176                                 continue;
177                         }
178                         good_packets++;
179                         i += LCD14_PACKET_SIZE;
180                 }
181
182                 /*
183                  * If we dropped more than two packets worth of data,
184                  * something is wrong.
185                  */
186                 dropped = len - (good_packets * LCD14_PACKET_SIZE);
187                 if (dropped > 2 * LCD14_PACKET_SIZE)
188                         continue;
189
190                 /* Let's see if we have anything good. */
191                 if (good_packets == 0)
192                         continue;
193
194                 sr_info("Found device on port %s.", conn);
195
196                 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
197                                             "TP4000ZC", "")))
198                         return NULL;
199                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
200                         sr_err("Device context malloc failed.");
201                         return NULL;
202                 }
203
204                 devc->serial = sr_serial_dev_inst_new(conn, -1);
205                 devc->serialcomm = g_strdup(serialcomm);
206
207                 sdi->priv = devc;
208                 sdi->driver = di;
209                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
210                         return NULL;
211                 sdi->probes = g_slist_append(sdi->probes, probe);
212                 drvc->instances = g_slist_append(drvc->instances, sdi);
213                 devices = g_slist_append(devices, sdi);
214                 break;
215         }
216
217         serial_close(fd);
218         return devices;
219 }
220
221 static GSList *hw_scan(GSList *options)
222 {
223         struct sr_hwopt *opt;
224         GSList *l, *devices;
225         const char *conn, *serialcomm;
226
227         conn = serialcomm = NULL;
228         for (l = options; l; l = l->next) {
229                 opt = l->data;
230                 switch (opt->hwopt) {
231                 case SR_HWOPT_CONN:
232                         conn = opt->value;
233                         break;
234                 case SR_HWOPT_SERIALCOMM:
235                         serialcomm = opt->value;
236                         break;
237                 }
238         }
239         if (!conn)
240                 return NULL;
241
242         if (serialcomm) {
243                 /* Use the provided comm specs. */
244                 devices = lcd14_scan(conn, serialcomm);
245         } else {
246                 /* Try the default 2400/8n1. */
247                 devices = lcd14_scan(conn, "2400/8n1");
248         }
249
250         return devices;
251 }
252
253 static GSList *hw_dev_list(void)
254 {
255         struct drv_context *drvc;
256
257         drvc = di->priv;
258
259         return drvc->instances;
260 }
261
262 static int hw_dev_open(struct sr_dev_inst *sdi)
263 {
264         int ret;
265         struct dev_context *devc;
266
267         if (!(devc = sdi->priv)) {
268                 sr_err("sdi->priv was NULL.");
269                 return SR_ERR_BUG;
270         }
271
272         devc->serial->fd = serial_open(devc->serial->port, O_RDONLY);
273         if (devc->serial->fd == -1) {
274                 sr_err("Couldn't open serial port '%s'.", devc->serial->port);
275                 return SR_ERR;
276         }
277
278         ret = serial_set_paramstr(devc->serial->fd, devc->serialcomm);
279         if (ret != SR_OK) {
280                 sr_err("Unable to set serial parameters: %d.", ret);
281                 return SR_ERR;
282         }
283         sdi->status = SR_ST_ACTIVE;
284
285         return SR_OK;
286 }
287
288 static int hw_dev_close(struct sr_dev_inst *sdi)
289 {
290         struct dev_context *devc;
291
292         if (!(devc = sdi->priv)) {
293                 sr_err("sdi->priv was NULL.");
294                 return SR_ERR_BUG;
295         }
296
297         if (devc->serial && devc->serial->fd != -1) {
298                 serial_close(devc->serial->fd);
299                 devc->serial->fd = -1;
300                 sdi->status = SR_ST_INACTIVE;
301         }
302
303         return SR_OK;
304 }
305
306 static int hw_cleanup(void)
307 {
308         clear_instances();
309
310         return SR_OK;
311 }
312
313 static int hw_info_get(int info_id, const void **data,
314                        const struct sr_dev_inst *sdi)
315 {
316         (void)sdi;
317
318         switch (info_id) {
319         case SR_DI_HWOPTS:
320                 *data = hwopts;
321                 break;
322         case SR_DI_HWCAPS:
323                 *data = hwcaps;
324                 break;
325         case SR_DI_NUM_PROBES:
326                 *data = GINT_TO_POINTER(1);
327                 break;
328         case SR_DI_PROBE_NAMES:
329                 *data = probe_names;
330                 break;
331         default:
332                 return SR_ERR_ARG;
333         }
334
335         return SR_OK;
336 }
337
338 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
339                              const void *value)
340 {
341         struct dev_context *devc;
342
343         if (sdi->status != SR_ST_ACTIVE)
344                 return SR_ERR;
345
346         if (!(devc = sdi->priv)) {
347                 sr_err("sdi->priv was NULL.");
348                 return SR_ERR_BUG;
349         }
350
351         switch (hwcap) {
352         case SR_HWCAP_LIMIT_SAMPLES:
353                 devc->limit_samples = *(const uint64_t *)value;
354                 sr_dbg("Setting sample limit to %" PRIu64 ".",
355                        devc->limit_samples);
356                 break;
357         default:
358                 sr_err("Unknown capability: %d.", hwcap);
359                 return SR_ERR;
360                 break;
361         }
362
363         return SR_OK;
364 }
365
366 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
367                                     void *cb_data)
368 {
369         struct sr_datafeed_packet packet;
370         struct sr_datafeed_header header;
371         struct sr_datafeed_meta_analog meta;
372         struct dev_context *devc;
373
374         if (!(devc = sdi->priv)) {
375                 sr_err("sdi->priv was NULL.");
376                 return SR_ERR_BUG;
377         }
378
379         sr_dbg("Starting acquisition.");
380
381         devc->cb_data = cb_data;
382
383         /*
384          * Reset the number of samples to take. If we've already collected our
385          * quota, but we start a new session, and don't reset this, we'll just
386          * quit without acquiring any new samples.
387          */
388         devc->num_samples = 0;
389
390         /* Send header packet to the session bus. */
391         sr_dbg("Sending SR_DF_HEADER.");
392         packet.type = SR_DF_HEADER;
393         packet.payload = (uint8_t *)&header;
394         header.feed_version = 1;
395         gettimeofday(&header.starttime, NULL);
396         sr_session_send(devc->cb_data, &packet);
397
398         /* Send metadata about the SR_DF_ANALOG packets to come. */
399         sr_dbg("Sending SR_DF_META_ANALOG.");
400         packet.type = SR_DF_META_ANALOG;
401         packet.payload = &meta;
402         meta.num_probes = 1;
403         sr_session_send(devc->cb_data, &packet);
404
405         /* Poll every 50ms, or whenever some data comes in. */
406         sr_source_add(devc->serial->fd, G_IO_IN, 50,
407                       tekpower_dmm_receive_data, (void *)sdi);
408
409         return SR_OK;
410 }
411
412 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
413                                    void *cb_data)
414 {
415         struct sr_datafeed_packet packet;
416         struct dev_context *devc;
417
418         if (sdi->status != SR_ST_ACTIVE)
419                 return SR_ERR;
420
421         if (!(devc = sdi->priv)) {
422                 sr_err("sdi->priv was NULL.");
423                 return SR_ERR_BUG;
424         }
425
426         sr_dbg("Stopping acquisition.");
427
428         sr_source_remove(devc->serial->fd);
429         hw_dev_close((struct sr_dev_inst *)sdi);
430
431         /* Send end packet to the session bus. */
432         sr_dbg("Sending SR_DF_END.");
433         packet.type = SR_DF_END;
434         sr_session_send(cb_data, &packet);
435
436         return SR_OK;
437 }
438
439 SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info = {
440         .name = "tekpower-dmm",
441         .longname = "TekPower/Digitek TP4000ZC/DT4000ZC DMM",
442         .api_version = 1,
443         .init = hw_init,
444         .cleanup = hw_cleanup,
445         .scan = hw_scan,
446         .dev_list = hw_dev_list,
447         .dev_clear = clear_instances,
448         .dev_open = hw_dev_open,
449         .dev_close = hw_dev_close,
450         .info_get = hw_info_get,
451         .dev_config_set = hw_dev_config_set,
452         .dev_acquisition_start = hw_dev_acquisition_start,
453         .dev_acquisition_stop = hw_dev_acquisition_stop,
454         .priv = NULL,
455 };