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