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