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