]>
Commit | Line | Data |
---|---|---|
8b607a24 AS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2018-2020 Andreas Sandberg <andreas@sandberg.pp.se> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
dca972f8 | 21 | |
8b607a24 AS |
22 | #include <glib.h> |
23 | #include <libsigrok/libsigrok.h> | |
dca972f8 GS |
24 | #include <math.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
8b607a24 AS |
28 | #include "libsigrok-internal.h" |
29 | #include "protocol.h" | |
30 | ||
ae33433b GS |
31 | /* Read/write timeouts, poll request intervals. */ |
32 | #define PROBE_TO_MS 1000 | |
33 | #define WRITE_TO_MS 1 | |
34 | #define POLL_PERIOD_MS 100 | |
8b607a24 | 35 | |
ae33433b GS |
36 | /* Expected receive data size for poll responses. */ |
37 | #define POLL_RECV_LEN 130 | |
8b607a24 | 38 | |
ae33433b | 39 | /* Command code to request another poll response. */ |
8b607a24 AS |
40 | #define UM_CMD_POLL 0xf0 |
41 | ||
1b43eb29 | 42 | static const struct rdtech_um_channel_desc default_channels[] = { |
d555959d GS |
43 | { "V", { 2, BVT_BE_UINT16, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, |
44 | { "I", { 4, BVT_BE_UINT16, }, { 1, 1e3, }, 3, SR_MQ_CURRENT, SR_UNIT_AMPERE }, | |
45 | { "D+", { 96, BVT_BE_UINT16, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, | |
46 | { "D-", { 98, BVT_BE_UINT16, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, | |
47 | { "T", { 10, BVT_BE_UINT16, }, { 1, 1, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS }, | |
8b607a24 | 48 | /* Threshold-based recording (mWh) */ |
d555959d | 49 | { "E", { 106, BVT_BE_UINT32, }, { 1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR }, |
8b607a24 AS |
50 | }; |
51 | ||
1b43eb29 | 52 | static const struct rdtech_um_channel_desc um25c_channels[] = { |
d555959d GS |
53 | { "V", { 2, BVT_BE_UINT16, }, { 1, 1e3, }, 3, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, |
54 | { "I", { 4, BVT_BE_UINT16, }, { 100, 1e6, }, 4, SR_MQ_CURRENT, SR_UNIT_AMPERE }, | |
55 | { "D+", { 96, BVT_BE_UINT16, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, | |
56 | { "D-", { 98, BVT_BE_UINT16, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, | |
57 | { "T", { 10, BVT_BE_UINT16, }, { 1, 1, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS }, | |
8b607a24 | 58 | /* Threshold-based recording (mWh) */ |
d555959d | 59 | { "E", { 106, BVT_BE_UINT32, }, { 1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR }, |
8b607a24 AS |
60 | }; |
61 | ||
85accbc2 | 62 | static gboolean csum_ok_fff1(const uint8_t *buf, size_t len) |
8b607a24 | 63 | { |
85accbc2 GS |
64 | uint16_t csum_recv; |
65 | ||
ae33433b | 66 | if (len != POLL_RECV_LEN) |
85accbc2 GS |
67 | return FALSE; |
68 | ||
69 | csum_recv = read_u16be(&buf[len - sizeof(uint16_t)]); | |
70 | if (csum_recv != 0xfff1) | |
71 | return FALSE; | |
72 | ||
73 | return TRUE; | |
8b607a24 AS |
74 | } |
75 | ||
85accbc2 | 76 | static gboolean csum_ok_um34c(const uint8_t *buf, size_t len) |
8b607a24 AS |
77 | { |
78 | static const int positions[] = { | |
79 | 1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53, | |
80 | 55, 57, 59, 63, 67, 69, 73, 79, 83, 89, 97, 99, 109, | |
81 | 111, 113, 119, 121, 127, | |
82 | }; | |
fda34e5a | 83 | |
85accbc2 GS |
84 | size_t i; |
85 | uint8_t csum_calc, csum_recv; | |
8b607a24 | 86 | |
ae33433b | 87 | if (len != POLL_RECV_LEN) |
85accbc2 | 88 | return FALSE; |
8b607a24 | 89 | |
85accbc2 | 90 | csum_calc = 0; |
8b607a24 | 91 | for (i = 0; i < ARRAY_SIZE(positions); i++) |
85accbc2 GS |
92 | csum_calc ^= buf[positions[i]]; |
93 | csum_recv = read_u8(&buf[len - sizeof(uint8_t)]); | |
94 | if (csum_recv != csum_calc) | |
95 | return FALSE; | |
8b607a24 | 96 | |
85accbc2 | 97 | return TRUE; |
8b607a24 AS |
98 | } |
99 | ||
100 | static const struct rdtech_um_profile um_profiles[] = { | |
7cfa93b0 GS |
101 | { "UM24C", RDTECH_UM24C, ARRAY_AND_SIZE(default_channels), csum_ok_fff1, }, |
102 | { "UM25C", RDTECH_UM25C, ARRAY_AND_SIZE(um25c_channels), csum_ok_fff1, }, | |
103 | { "UM34C", RDTECH_UM34C, ARRAY_AND_SIZE(default_channels), csum_ok_um34c, }, | |
8b607a24 AS |
104 | }; |
105 | ||
106 | static const struct rdtech_um_profile *find_profile(uint16_t id) | |
107 | { | |
4441e573 GS |
108 | size_t i; |
109 | const struct rdtech_um_profile *profile; | |
fda34e5a | 110 | |
8b607a24 | 111 | for (i = 0; i < ARRAY_SIZE(um_profiles); i++) { |
4441e573 GS |
112 | profile = &um_profiles[i]; |
113 | if (profile->model_id == id) | |
114 | return profile; | |
8b607a24 AS |
115 | } |
116 | return NULL; | |
117 | } | |
118 | ||
119 | SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial) | |
120 | { | |
121 | const struct rdtech_um_profile *p; | |
ae33433b GS |
122 | uint8_t req; |
123 | int ret; | |
85accbc2 | 124 | uint8_t buf[RDTECH_UM_BUFSIZE]; |
4441e573 | 125 | int rcvd; |
bcbf2b59 | 126 | uint16_t model_id; |
8b607a24 | 127 | |
ae33433b GS |
128 | req = UM_CMD_POLL; |
129 | ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS); | |
130 | if (ret < 0) { | |
131 | sr_err("Failed to send probe request."); | |
8b607a24 AS |
132 | return NULL; |
133 | } | |
134 | ||
ae33433b GS |
135 | rcvd = serial_read_blocking(serial, buf, POLL_RECV_LEN, PROBE_TO_MS); |
136 | if (rcvd != POLL_RECV_LEN) { | |
8b607a24 AS |
137 | sr_err("Failed to read probe response."); |
138 | return NULL; | |
139 | } | |
140 | ||
bcbf2b59 GS |
141 | model_id = read_u16be(&buf[0]); |
142 | p = find_profile(model_id); | |
8b607a24 | 143 | if (!p) { |
bcbf2b59 | 144 | sr_err("Unrecognized UM device (0x%.4" PRIx16 ").", model_id); |
8b607a24 AS |
145 | return NULL; |
146 | } | |
147 | ||
4441e573 | 148 | if (!p->csum_ok(buf, rcvd)) { |
bcbf2b59 | 149 | sr_err("Probe response fails checksum verification."); |
8b607a24 AS |
150 | return NULL; |
151 | } | |
152 | ||
153 | return p; | |
154 | } | |
155 | ||
c1f4f589 | 156 | SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force) |
8b607a24 | 157 | { |
fda34e5a | 158 | struct dev_context *devc; |
c1f4f589 | 159 | int64_t now, elapsed; |
fda34e5a | 160 | struct sr_serial_dev_inst *serial; |
ae33433b GS |
161 | uint8_t req; |
162 | int ret; | |
8b607a24 | 163 | |
481f894f | 164 | /* Don't send request when receive data is being accumulated. */ |
c1f4f589 | 165 | devc = sdi->priv; |
481f894f GS |
166 | if (!force && devc->buflen) |
167 | return SR_OK; | |
168 | ||
169 | /* Check for expired intervals or forced requests. */ | |
c1f4f589 GS |
170 | now = g_get_monotonic_time() / 1000; |
171 | elapsed = now - devc->cmd_sent_at; | |
ae33433b | 172 | if (!force && elapsed < POLL_PERIOD_MS) |
c1f4f589 GS |
173 | return SR_OK; |
174 | ||
175 | /* Send another poll request. Update interval only on success. */ | |
fda34e5a | 176 | serial = sdi->conn; |
ae33433b GS |
177 | req = UM_CMD_POLL; |
178 | ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS); | |
179 | if (ret < 0) { | |
8b607a24 AS |
180 | sr_err("Unable to send poll request."); |
181 | return SR_ERR; | |
182 | } | |
c1f4f589 | 183 | devc->cmd_sent_at = now; |
8b607a24 AS |
184 | |
185 | return SR_OK; | |
186 | } | |
187 | ||
481f894f GS |
188 | static int process_data(struct sr_dev_inst *sdi, |
189 | const uint8_t *data, size_t dlen) | |
8b607a24 | 190 | { |
fda34e5a | 191 | struct dev_context *devc; |
481f894f | 192 | const struct rdtech_um_profile *p; |
4441e573 | 193 | size_t ch_idx; |
1b43eb29 | 194 | float v; |
481f894f | 195 | int ret; |
8b607a24 | 196 | |
fda34e5a | 197 | devc = sdi->priv; |
481f894f GS |
198 | p = devc->profile; |
199 | ||
200 | sr_spew("Received poll packet (len: %zu).", dlen); | |
201 | if (dlen < POLL_RECV_LEN) { | |
202 | sr_err("Insufficient response data length: %zu", dlen); | |
203 | return SR_ERR_DATA; | |
204 | } | |
205 | ||
206 | if (!p->csum_ok(data, POLL_RECV_LEN)) { | |
207 | sr_err("Packet checksum verification failed."); | |
208 | return SR_ERR_DATA; | |
8b607a24 AS |
209 | } |
210 | ||
d6ca362d GS |
211 | ret = SR_OK; |
212 | std_session_send_df_frame_begin(sdi); | |
1b43eb29 | 213 | for (ch_idx = 0; ch_idx < p->channel_count; ch_idx++) { |
48b7c901 | 214 | ret = bv_get_value_len(&v, &p->channels[ch_idx].spec, data, dlen); |
1b43eb29 | 215 | if (ret != SR_OK) |
d6ca362d | 216 | break; |
f40d8479 | 217 | ret = feed_queue_analog_submit_one(devc->feeds[ch_idx], v, 1); |
481f894f | 218 | if (ret != SR_OK) |
d6ca362d | 219 | break; |
323b32f4 | 220 | } |
d6ca362d | 221 | std_session_send_df_frame_end(sdi); |
8b607a24 | 222 | |
d6ca362d | 223 | sr_sw_limits_update_frames_read(&devc->limits, 1); |
481f894f GS |
224 | if (sr_sw_limits_check(&devc->limits)) |
225 | sr_dev_acquisition_stop(sdi); | |
226 | ||
d6ca362d | 227 | return ret; |
8b607a24 AS |
228 | } |
229 | ||
481f894f | 230 | static int accum_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial) |
8b607a24 | 231 | { |
fda34e5a GS |
232 | struct dev_context *devc; |
233 | const struct rdtech_um_profile *p; | |
481f894f GS |
234 | uint8_t *rdptr; |
235 | size_t space, rcvd, rdlen; | |
236 | int ret; | |
237 | gboolean do_sync_check; | |
238 | size_t sync_len, sync_idx; | |
239 | ||
240 | /* | |
241 | * Receive data became available. Drain the serial transport. | |
242 | * Grab incoming data in as large a chunk as possible. Also | |
243 | * copes with zero receive data length, as some transports may | |
244 | * trigger periodically without data really being available. | |
245 | */ | |
fda34e5a GS |
246 | devc = sdi->priv; |
247 | p = devc->profile; | |
481f894f GS |
248 | rdptr = &devc->buf[devc->buflen]; |
249 | space = sizeof(devc->buf) - devc->buflen; | |
250 | do_sync_check = FALSE; | |
251 | sync_len = sizeof(uint16_t); | |
252 | while (space) { | |
253 | ret = serial_read_nonblocking(serial, rdptr, space); | |
254 | if (ret < 0) | |
255 | return SR_ERR_IO; | |
256 | rcvd = (size_t)ret; | |
257 | if (rcvd == 0) | |
258 | break; | |
259 | if (rcvd > space) | |
260 | return SR_ERR_BUG; | |
261 | if (devc->buflen < sync_len) | |
262 | do_sync_check = TRUE; | |
263 | devc->buflen += rcvd; | |
264 | if (devc->buflen < sync_len) | |
265 | do_sync_check = FALSE; | |
266 | space -= rcvd; | |
267 | rdptr += rcvd; | |
268 | } | |
269 | ||
270 | /* | |
271 | * Synchronize to the packetized input stream. Check the model | |
272 | * ID at the start of receive data. Which is a weak condition, | |
273 | * but going out of sync should be rare, and repeated attempts | |
274 | * to synchronize should eventually succeed. Try to rate limit | |
275 | * the emission of diagnostics messages. (Re-)run this logic | |
276 | * at the first reception which makes enough data available, | |
277 | * but not during subsequent accumulation of more data. | |
278 | * | |
279 | * Reducing redundancy in the implementation at the same time as | |
280 | * increasing robustness would involve the creation of a checker | |
281 | * routine, which just gets called for every byte position until | |
282 | * it succeeds. Similar to what a previous implementation of the | |
283 | * read loop did, which was expensive on the serial transport. | |
284 | */ | |
285 | sync_idx = 0; | |
286 | if (do_sync_check && read_u16be(&devc->buf[sync_idx]) != p->model_id) | |
287 | sr_warn("Unexpected response data, trying to synchronize."); | |
288 | while (do_sync_check) { | |
289 | if (sync_idx + sync_len >= devc->buflen) | |
290 | break; | |
291 | if (read_u16be(&devc->buf[sync_idx]) == p->model_id) | |
292 | break; | |
293 | sync_idx++; | |
294 | } | |
295 | if (do_sync_check && sync_idx) { | |
296 | sr_dbg("Skipping %zu bytes in attempt to sync.", sync_idx); | |
297 | sync_len = devc->buflen - sync_idx; | |
298 | if (sync_len) | |
299 | memmove(&devc->buf[0], &devc->buf[sync_idx], sync_len); | |
300 | devc->buflen -= sync_idx; | |
301 | } | |
302 | ||
303 | /* | |
304 | * Process packets as their reception completes. Periodically | |
305 | * re-transmit poll requests. Discard consumed data after all | |
306 | * processing has completed. | |
307 | */ | |
308 | rdptr = devc->buf; | |
309 | rdlen = devc->buflen; | |
310 | ret = SR_OK; | |
311 | while (ret == SR_OK && rdlen >= POLL_RECV_LEN) { | |
312 | ret = process_data(sdi, rdptr, rdlen); | |
313 | if (ret != SR_OK) { | |
314 | sr_err("Processing response packet failed."); | |
315 | break; | |
8b607a24 | 316 | } |
481f894f GS |
317 | rdptr += POLL_RECV_LEN; |
318 | rdlen -= POLL_RECV_LEN; | |
319 | ||
320 | if (0 && !sr_sw_limits_check(&devc->limits)) | |
321 | (void)rdtech_um_poll(sdi, FALSE); | |
8b607a24 | 322 | } |
481f894f GS |
323 | rcvd = rdptr - devc->buf; |
324 | devc->buflen -= rcvd; | |
325 | if (devc->buflen) | |
326 | memmove(&devc->buf[0], rdptr, devc->buflen); | |
8b607a24 | 327 | |
481f894f | 328 | return ret; |
8b607a24 AS |
329 | } |
330 | ||
331 | SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data) | |
332 | { | |
333 | struct sr_dev_inst *sdi; | |
334 | struct dev_context *devc; | |
335 | struct sr_serial_dev_inst *serial; | |
481f894f | 336 | int ret; |
8b607a24 AS |
337 | |
338 | (void)fd; | |
339 | ||
340 | if (!(sdi = cb_data)) | |
341 | return TRUE; | |
8b607a24 AS |
342 | if (!(devc = sdi->priv)) |
343 | return TRUE; | |
344 | ||
481f894f GS |
345 | /* |
346 | * Drain and process receive data as it becomes available. | |
347 | * Terminate acquisition upon receive or processing error. | |
348 | */ | |
8b607a24 | 349 | serial = sdi->conn; |
481f894f GS |
350 | if (revents == G_IO_IN) { |
351 | ret = accum_data(sdi, serial); | |
352 | if (ret != SR_OK) { | |
353 | sr_dev_acquisition_stop(sdi); | |
354 | return TRUE; | |
355 | } | |
356 | } | |
8b607a24 | 357 | |
ae33433b | 358 | /* Check configured acquisition limits. */ |
8b607a24 AS |
359 | if (sr_sw_limits_check(&devc->limits)) { |
360 | sr_dev_acquisition_stop(sdi); | |
361 | return TRUE; | |
362 | } | |
363 | ||
481f894f | 364 | /* Periodically retransmit measurement requests. */ |
c1f4f589 | 365 | (void)rdtech_um_poll(sdi, FALSE); |
8b607a24 AS |
366 | |
367 | return TRUE; | |
368 | } |