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