]> sigrok.org Git - libsigrok.git/blob - src/hardware/serial-dmm/protocol.c
serial-dmm: print data bytes according to specific meter's packet length
[libsigrok.git] / src / hardware / serial-dmm / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 <config.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 static void log_dmm_packet(const uint8_t *buf, size_t len)
31 {
32         GString *text;
33
34         text = sr_hexdump_new(buf, len);
35         sr_dbg("DMM packet: %s", text->str);
36         sr_hexdump_free(text);
37 }
38
39 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
40                           void *info)
41 {
42         struct dmm_info *dmm;
43         float floatval;
44         struct sr_datafeed_packet packet;
45         struct sr_datafeed_analog analog;
46         struct sr_analog_encoding encoding;
47         struct sr_analog_meaning meaning;
48         struct sr_analog_spec spec;
49         struct dev_context *devc;
50         gboolean sent_sample;
51         struct sr_channel *channel;
52         size_t ch_idx;
53
54         dmm = (struct dmm_info *)sdi->driver;
55
56         log_dmm_packet(buf, dmm->packet_size);
57         devc = sdi->priv;
58
59         sent_sample = FALSE;
60         memset(info, 0, dmm->info_size);
61         for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) {
62                 /* Note: digits/spec_digits will be overridden by the DMM parsers. */
63                 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
64
65                 channel = g_slist_nth_data(sdi->channels, ch_idx);
66                 analog.meaning->channels = g_slist_append(NULL, channel);
67                 analog.num_samples = 1;
68                 analog.meaning->mq = 0;
69
70                 dmm->packet_parse(buf, &floatval, &analog, info);
71                 analog.data = &floatval;
72
73                 /* If this DMM needs additional handling, call the resp. function. */
74                 if (dmm->dmm_details)
75                         dmm->dmm_details(&analog, info);
76
77                 if (analog.meaning->mq != 0 && channel->enabled) {
78                         /* Got a measurement. */
79                         packet.type = SR_DF_ANALOG;
80                         packet.payload = &analog;
81                         sr_session_send(sdi, &packet);
82                         sent_sample = TRUE;
83                 }
84         }
85
86         if (sent_sample) {
87                 sr_sw_limits_update_samples_read(&devc->limits, 1);
88         }
89 }
90
91 /** Request packet, if required. */
92 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
93 {
94         struct dmm_info *dmm;
95         struct dev_context *devc;
96         struct sr_serial_dev_inst *serial;
97         int ret;
98
99         dmm = (struct dmm_info *)sdi->driver;
100
101         if (!dmm->packet_request)
102                 return SR_OK;
103
104         devc = sdi->priv;
105         serial = sdi->conn;
106
107         if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
108                 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
109                         ((devc->req_next_at - g_get_monotonic_time()) / 1000));
110                 return SR_OK;
111         }
112
113         ret = dmm->packet_request(serial);
114         if (ret < 0) {
115                 sr_err("Failed to request packet: %d.", ret);
116                 return ret;
117         }
118
119         if (dmm->req_timeout_ms)
120                 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
121
122         return SR_OK;
123 }
124
125 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
126 {
127         struct dmm_info *dmm;
128         struct dev_context *devc;
129         int len, offset;
130         struct sr_serial_dev_inst *serial;
131
132         dmm = (struct dmm_info *)sdi->driver;
133
134         devc = sdi->priv;
135         serial = sdi->conn;
136
137         /* Try to get as much data as the buffer can hold. */
138         len = DMM_BUFSIZE - devc->buflen;
139         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
140         if (len == 0)
141                 return; /* No new bytes, nothing to do. */
142         if (len < 0) {
143                 sr_err("Serial port read error: %d.", len);
144                 return;
145         }
146         devc->buflen += len;
147
148         /* Now look for packets in that data. */
149         offset = 0;
150         while ((devc->buflen - offset) >= dmm->packet_size) {
151                 if (dmm->packet_valid(devc->buf + offset)) {
152                         handle_packet(devc->buf + offset, sdi, info);
153                         offset += dmm->packet_size;
154
155                         /* Request next packet, if required. */
156                         if (!dmm->packet_request)
157                                 break;
158                         if (dmm->req_timeout_ms || dmm->req_delay_ms)
159                                 devc->req_next_at = g_get_monotonic_time() +
160                                         dmm->req_delay_ms * 1000;
161                         req_packet(sdi);
162                 } else {
163                         offset++;
164                 }
165         }
166
167         /* If we have any data left, move it to the beginning of our buffer. */
168         if (devc->buflen > offset)
169                 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
170         devc->buflen -= offset;
171 }
172
173 int receive_data(int fd, int revents, void *cb_data)
174 {
175         struct sr_dev_inst *sdi;
176         struct dev_context *devc;
177         struct dmm_info *dmm;
178         void *info;
179
180         (void)fd;
181
182         if (!(sdi = cb_data))
183                 return TRUE;
184
185         if (!(devc = sdi->priv))
186                 return TRUE;
187
188         dmm = (struct dmm_info *)sdi->driver;
189
190         if (revents == G_IO_IN) {
191                 /* Serial data arrived. */
192                 info = g_malloc(dmm->info_size);
193                 handle_new_data(sdi, info);
194                 g_free(info);
195         } else {
196                 /* Timeout; send another packet request if DMM needs it. */
197                 if (dmm->packet_request && (req_packet(sdi) < 0))
198                         return FALSE;
199         }
200
201         if (sr_sw_limits_check(&devc->limits))
202                 sr_dev_acquisition_stop(sdi);
203
204         return TRUE;
205 }