]> sigrok.org Git - libsigrok.git/blob - hardware/radioshack-dmm/protocol.c
radioshack-dmm: Separate protocol parser from driver
[libsigrok.git] / hardware / radioshack-dmm / protocol.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 <stdlib.h>
22 #include <math.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <glib.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30
31 static void handle_packet(const uint8_t *rs_packet,
32                           struct dev_context *devc)
33 {
34         float rawval;
35         struct sr_datafeed_packet packet;
36         struct sr_datafeed_analog *analog;
37
38         /* TODO: Check malloc return value. */
39         analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
40         /* TODO: Check malloc return value. */
41
42         analog->num_samples = 1;
43         analog->mq = -1;
44
45         sr_rs9lcd_parse(rs_packet, &rawval, analog, NULL);
46         analog->data = &rawval;
47
48         if (analog->mq != -1) {
49                 /* Got a measurement. */
50                 sr_spew("Value: %f.", rawval);
51                 packet.type = SR_DF_ANALOG;
52                 packet.payload = analog;
53                 sr_session_send(devc->cb_data, &packet);
54                 devc->num_samples++;
55         }
56         g_free(analog);
57 }
58
59 static void handle_new_data(struct dev_context *devc)
60 {
61         int len;
62         size_t i, offset = 0;
63         uint8_t *rs_packet;
64
65         /* Try to get as much data as the buffer can hold. */
66         len = RS_DMM_BUFSIZE - devc->buflen;
67         len = serial_read(devc->serial, devc->buf + devc->buflen, len);
68         if (len < 1) {
69                 sr_err("Serial port read error.");
70                 return;
71         }
72         devc->buflen += len;
73
74         /* Now look for packets in that data. */
75         while ((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) {
76                 rs_packet = (void *)(devc->buf + offset);
77                 if (sr_rs9lcd_packet_valid(rs_packet)) {
78                         handle_packet(rs_packet, devc);
79                         offset += RS_22_812_PACKET_SIZE;
80                 } else {
81                         offset++;
82                 }
83         }
84
85         /* If we have any data left, move it to the beginning of our buffer. */
86         for (i = 0; i < devc->buflen - offset; i++)
87                 devc->buf[i] = devc->buf[offset + i];
88         devc->buflen -= offset;
89 }
90
91 SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data)
92 {
93         struct sr_dev_inst *sdi;
94         struct dev_context *devc;
95
96         (void)fd;
97
98         if (!(sdi = cb_data))
99                 return TRUE;
100
101         if (!(devc = sdi->priv))
102                 return TRUE;
103
104         if (revents == G_IO_IN) {
105                 /* Serial data arrived. */
106                 handle_new_data(devc);
107         }
108
109         if (devc->num_samples >= devc->limit_samples) {
110                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
111                 return TRUE;
112         }
113
114         return TRUE;
115 }