]>
Commit | Line | Data |
---|---|---|
7dc55d93 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
7dc55d93 AG |
3 | * |
4 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
729b01f9 | 5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> |
7dc55d93 AG |
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 | ||
6ec6c43b | 21 | #include <config.h> |
7dc55d93 AG |
22 | #include <stdlib.h> |
23 | #include <math.h> | |
24 | #include <string.h> | |
22f54192 | 25 | #include <glib.h> |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
bbabddbd UH |
27 | #include "libsigrok-internal.h" |
28 | #include "protocol.h" | |
7dc55d93 | 29 | |
75aaf967 | 30 | static void log_dmm_packet(const uint8_t *buf, size_t len) |
6bef68a7 | 31 | { |
75aaf967 GS |
32 | GString *text; |
33 | ||
abcb1385 GS |
34 | if (sr_log_loglevel_get() < SR_LOG_DBG) |
35 | return; | |
36 | ||
75aaf967 GS |
37 | text = sr_hexdump_new(buf, len); |
38 | sr_dbg("DMM packet: %s", text->str); | |
39 | sr_hexdump_free(text); | |
6bef68a7 UH |
40 | } |
41 | ||
070668a0 GS |
42 | static void handle_packet(struct sr_dev_inst *sdi, |
43 | const uint8_t *buf, size_t len, void *info) | |
7dc55d93 | 44 | { |
1a863916 | 45 | struct dmm_info *dmm; |
070668a0 | 46 | struct dev_context *devc; |
8c1adf37 | 47 | float floatval; |
070668a0 | 48 | double doubleval; |
7dc55d93 | 49 | struct sr_datafeed_packet packet; |
ffa2b6f9 UH |
50 | struct sr_datafeed_analog analog; |
51 | struct sr_analog_encoding encoding; | |
52 | struct sr_analog_meaning meaning; | |
53 | struct sr_analog_spec spec; | |
556a926d | 54 | gboolean sent_sample; |
e91c9f6e | 55 | struct sr_channel *channel; |
556a926d | 56 | size_t ch_idx; |
7dc55d93 | 57 | |
1a863916 UH |
58 | dmm = (struct dmm_info *)sdi->driver; |
59 | ||
070668a0 | 60 | log_dmm_packet(buf, len); |
69e19dd7 | 61 | devc = sdi->priv; |
6bef68a7 | 62 | |
556a926d GS |
63 | sent_sample = FALSE; |
64 | memset(info, 0, dmm->info_size); | |
65 | for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) { | |
66 | /* Note: digits/spec_digits will be overridden by the DMM parsers. */ | |
67 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); | |
68 | ||
e91c9f6e GS |
69 | channel = g_slist_nth_data(sdi->channels, ch_idx); |
70 | analog.meaning->channels = g_slist_append(NULL, channel); | |
556a926d GS |
71 | analog.num_samples = 1; |
72 | analog.meaning->mq = 0; | |
73 | ||
070668a0 GS |
74 | if (dmm->packet_parse) { |
75 | dmm->packet_parse(buf, &floatval, &analog, info); | |
76 | analog.data = &floatval; | |
77 | analog.encoding->unitsize = sizeof(floatval); | |
78 | } else if (dmm->packet_parse_len) { | |
79 | dmm->packet_parse_len(dmm->dmm_state, buf, len, | |
80 | &doubleval, &analog, info); | |
81 | analog.data = &doubleval; | |
82 | analog.encoding->unitsize = sizeof(doubleval); | |
83 | } | |
556a926d GS |
84 | |
85 | /* If this DMM needs additional handling, call the resp. function. */ | |
86 | if (dmm->dmm_details) | |
87 | dmm->dmm_details(&analog, info); | |
88 | ||
e91c9f6e | 89 | if (analog.meaning->mq != 0 && channel->enabled) { |
556a926d GS |
90 | /* Got a measurement. */ |
91 | packet.type = SR_DF_ANALOG; | |
92 | packet.payload = &analog; | |
93 | sr_session_send(sdi, &packet); | |
94 | sent_sample = TRUE; | |
95 | } | |
96 | } | |
7dc55d93 | 97 | |
556a926d | 98 | if (sent_sample) { |
9f51c463 | 99 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
7dc55d93 | 100 | } |
7dc55d93 AG |
101 | } |
102 | ||
2baac44d | 103 | /** Request packet, if required. */ |
bcbef5ed | 104 | SR_PRIV int req_packet(struct sr_dev_inst *sdi) |
2baac44d | 105 | { |
1a863916 | 106 | struct dmm_info *dmm; |
2baac44d MH |
107 | struct dev_context *devc; |
108 | struct sr_serial_dev_inst *serial; | |
070668a0 | 109 | uint64_t now, left, next; |
2baac44d MH |
110 | int ret; |
111 | ||
1a863916 | 112 | dmm = (struct dmm_info *)sdi->driver; |
bcbef5ed | 113 | if (!dmm->packet_request) |
2baac44d MH |
114 | return SR_OK; |
115 | ||
116 | devc = sdi->priv; | |
117 | serial = sdi->conn; | |
118 | ||
070668a0 GS |
119 | now = g_get_monotonic_time(); |
120 | if (devc->req_next_at && now < devc->req_next_at) { | |
121 | left = (devc->req_next_at - now) / 1000; | |
122 | sr_spew("Not re-requesting yet, %" PRIu64 "ms left.", left); | |
2baac44d MH |
123 | return SR_OK; |
124 | } | |
125 | ||
070668a0 | 126 | sr_spew("Requesting next packet."); |
bcbef5ed | 127 | ret = dmm->packet_request(serial); |
2baac44d MH |
128 | if (ret < 0) { |
129 | sr_err("Failed to request packet: %d.", ret); | |
130 | return ret; | |
131 | } | |
132 | ||
070668a0 GS |
133 | if (dmm->req_timeout_ms) { |
134 | next = now + dmm->req_timeout_ms * 1000; | |
135 | devc->req_next_at = next; | |
136 | } | |
2baac44d MH |
137 | |
138 | return SR_OK; | |
139 | } | |
140 | ||
bcbef5ed | 141 | static void handle_new_data(struct sr_dev_inst *sdi, void *info) |
7dc55d93 | 142 | { |
1a863916 | 143 | struct dmm_info *dmm; |
69e19dd7 | 144 | struct dev_context *devc; |
1e1bfcd0 | 145 | struct sr_serial_dev_inst *serial; |
070668a0 GS |
146 | int ret; |
147 | size_t read_len, check_pos, check_len, pkt_size, copy_len; | |
148 | uint8_t *check_ptr; | |
149 | uint64_t deadline; | |
bbabddbd | 150 | |
1a863916 UH |
151 | dmm = (struct dmm_info *)sdi->driver; |
152 | ||
69e19dd7 | 153 | devc = sdi->priv; |
1e1bfcd0 | 154 | serial = sdi->conn; |
69e19dd7 | 155 | |
070668a0 GS |
156 | /* Add the maximum available RX data we can get to the local buffer. */ |
157 | read_len = DMM_BUFSIZE - devc->buflen; | |
158 | ret = serial_read_nonblocking(serial, &devc->buf[devc->buflen], read_len); | |
159 | if (ret == 0) | |
66a43576 | 160 | return; /* No new bytes, nothing to do. */ |
070668a0 GS |
161 | if (ret < 0) { |
162 | sr_err("Serial port read error: %d.", ret); | |
7dc55d93 AG |
163 | return; |
164 | } | |
070668a0 GS |
165 | devc->buflen += ret; |
166 | ||
167 | /* | |
168 | * Process packets when their reception has completed, or keep | |
169 | * trying to synchronize to the stream of input data. | |
170 | */ | |
171 | check_pos = 0; | |
172 | while (check_pos < devc->buflen) { | |
173 | /* Got the (minimum) amount of receive data for a packet? */ | |
174 | check_len = devc->buflen - check_pos; | |
175 | if (check_len < dmm->packet_size) | |
176 | break; | |
177 | sr_dbg("Checking: pos %zu, len %zu.", check_pos, check_len); | |
178 | ||
179 | /* Is it a valid packet? */ | |
180 | check_ptr = &devc->buf[check_pos]; | |
181 | if (dmm->packet_valid_len) { | |
182 | ret = dmm->packet_valid_len(dmm->dmm_state, | |
183 | check_ptr, check_len, &pkt_size); | |
184 | if (ret == SR_PACKET_NEED_RX) { | |
185 | sr_dbg("Need more RX data."); | |
2baac44d | 186 | break; |
070668a0 GS |
187 | } |
188 | if (ret == SR_PACKET_INVALID) { | |
189 | sr_dbg("Not a valid packet, searching."); | |
190 | check_pos++; | |
191 | continue; | |
192 | } | |
193 | } else if (dmm->packet_valid) { | |
194 | if (!dmm->packet_valid(check_ptr)) { | |
195 | sr_dbg("Not a valid packet, searching."); | |
196 | check_pos++; | |
197 | continue; | |
198 | } | |
199 | pkt_size = dmm->packet_size; | |
200 | } | |
201 | ||
202 | /* Process the package. */ | |
203 | sr_dbg("Valid packet, size %zu, processing", pkt_size); | |
204 | handle_packet(sdi, check_ptr, pkt_size, info); | |
205 | check_pos += pkt_size; | |
206 | ||
207 | /* Arrange for the next packet request if needed. */ | |
208 | if (!dmm->packet_request) | |
209 | continue; | |
210 | if (dmm->req_timeout_ms || dmm->req_delay_ms) { | |
211 | deadline = g_get_monotonic_time(); | |
212 | deadline += dmm->req_delay_ms * 1000; | |
213 | devc->req_next_at = deadline; | |
7dc55d93 | 214 | } |
070668a0 GS |
215 | req_packet(sdi); |
216 | continue; | |
7dc55d93 AG |
217 | } |
218 | ||
bbabddbd | 219 | /* If we have any data left, move it to the beginning of our buffer. */ |
070668a0 GS |
220 | if (devc->buflen > check_pos) { |
221 | copy_len = devc->buflen - check_pos; | |
222 | memmove(&devc->buf[0], &devc->buf[check_pos], copy_len); | |
223 | } | |
224 | devc->buflen -= check_pos; | |
225 | ||
226 | /* | |
227 | * If the complete buffer filled up and none of it got processed, | |
228 | * discard the unprocessed buffer, re-sync to the stream in later | |
229 | * calls again. | |
230 | */ | |
231 | if (devc->buflen == sizeof(devc->buf)) { | |
232 | sr_info("Drop unprocessed RX data, try to re-sync to stream."); | |
233 | devc->buflen = 0; | |
234 | } | |
7dc55d93 AG |
235 | } |
236 | ||
bcbef5ed | 237 | int receive_data(int fd, int revents, void *cb_data) |
7dc55d93 | 238 | { |
642e9d62 | 239 | struct sr_dev_inst *sdi; |
7dc55d93 | 240 | struct dev_context *devc; |
bcbef5ed | 241 | struct dmm_info *dmm; |
bcbef5ed | 242 | void *info; |
7dc55d93 | 243 | |
d35afa87 BV |
244 | (void)fd; |
245 | ||
7dc55d93 AG |
246 | if (!(sdi = cb_data)) |
247 | return TRUE; | |
248 | ||
249 | if (!(devc = sdi->priv)) | |
250 | return TRUE; | |
251 | ||
1a863916 | 252 | dmm = (struct dmm_info *)sdi->driver; |
bcbef5ed | 253 | |
bbabddbd | 254 | if (revents == G_IO_IN) { |
7dc55d93 | 255 | /* Serial data arrived. */ |
145d794f | 256 | info = g_malloc(dmm->info_size); |
bcbef5ed | 257 | handle_new_data(sdi, info); |
145d794f | 258 | g_free(info); |
ce3777ad | 259 | } else { |
2baac44d | 260 | /* Timeout; send another packet request if DMM needs it. */ |
bcbef5ed | 261 | if (dmm->packet_request && (req_packet(sdi) < 0)) |
2baac44d | 262 | return FALSE; |
7dc55d93 AG |
263 | } |
264 | ||
9f51c463 | 265 | if (sr_sw_limits_check(&devc->limits)) |
d2f7c417 | 266 | sr_dev_acquisition_stop(sdi); |
f9b9bd63 | 267 | |
7dc55d93 AG |
268 | return TRUE; |
269 | } |