2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018-2020 Andreas Sandberg <andreas@sandberg.pp.se>
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.
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.
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/>.
23 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 /* Read/write timeouts, poll request intervals. */
32 #define PROBE_TO_MS 1000
34 #define POLL_PERIOD_MS 100
36 /* Expected receive data size for poll responses. */
37 #define POLL_RECV_LEN 130
39 /* Command code to request another poll response. */
40 #define UM_CMD_POLL 0xf0
42 static const struct rdtech_um_channel_desc default_channels[] = {
43 { "V", { 2, BVT_BE_UINT16, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
44 { "I", { 4, BVT_BE_UINT16, 1, }, { 1, 1e3, }, 3, SR_MQ_CURRENT, SR_UNIT_AMPERE },
45 { "D+", { 96, BVT_BE_UINT16, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
46 { "D-", { 98, BVT_BE_UINT16, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
47 { "T", { 10, BVT_BE_UINT16, 1, }, { 1, 1, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
48 /* Threshold-based recording (mWh) */
49 { "E", { 106, BVT_BE_UINT32, 1, }, { 1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
52 static const struct rdtech_um_channel_desc um25c_channels[] = {
53 { "V", { 2, BVT_BE_UINT16, 1, }, { 1, 1e3, }, 3, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
54 { "I", { 4, BVT_BE_UINT16, 1, }, { 100, 1e6, }, 4, SR_MQ_CURRENT, SR_UNIT_AMPERE },
55 { "D+", { 96, BVT_BE_UINT16, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
56 { "D-", { 98, BVT_BE_UINT16, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
57 { "T", { 10, BVT_BE_UINT16, 1, }, { 1, 1, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
58 /* Threshold-based recording (mWh) */
59 { "E", { 106, BVT_BE_UINT32, 1, }, { 1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
62 static gboolean csum_ok_fff1(const uint8_t *buf, size_t len)
66 if (len != POLL_RECV_LEN)
69 csum_recv = read_u16be(&buf[len - sizeof(uint16_t)]);
70 if (csum_recv != 0xfff1)
76 static gboolean csum_ok_um34c(const uint8_t *buf, size_t len)
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,
85 uint8_t csum_calc, csum_recv;
87 if (len != POLL_RECV_LEN)
91 for (i = 0; i < ARRAY_SIZE(positions); i++)
92 csum_calc ^= buf[positions[i]];
93 csum_recv = read_u8(&buf[len - sizeof(uint8_t)]);
94 if (csum_recv != csum_calc)
100 static const struct rdtech_um_profile um_profiles[] = {
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, },
106 static const struct rdtech_um_profile *find_profile(uint16_t id)
109 const struct rdtech_um_profile *profile;
111 for (i = 0; i < ARRAY_SIZE(um_profiles); i++) {
112 profile = &um_profiles[i];
113 if (profile->model_id == id)
119 SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial)
121 const struct rdtech_um_profile *p;
124 uint8_t buf[RDTECH_UM_BUFSIZE];
129 ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS);
131 sr_err("Failed to send probe request.");
135 rcvd = serial_read_blocking(serial, buf, POLL_RECV_LEN, PROBE_TO_MS);
136 if (rcvd != POLL_RECV_LEN) {
137 sr_err("Failed to read probe response.");
141 model_id = read_u16be(&buf[0]);
142 p = find_profile(model_id);
144 sr_err("Unrecognized UM device (0x%.4" PRIx16 ").", model_id);
148 if (!p->csum_ok(buf, rcvd)) {
149 sr_err("Probe response fails checksum verification.");
156 SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force)
158 struct dev_context *devc;
159 int64_t now, elapsed;
160 struct sr_serial_dev_inst *serial;
164 /* Don't send request when receive data is being accumulated. */
166 if (!force && devc->buflen)
169 /* Check for expired intervals or forced requests. */
170 now = g_get_monotonic_time() / 1000;
171 elapsed = now - devc->cmd_sent_at;
172 if (!force && elapsed < POLL_PERIOD_MS)
175 /* Send another poll request. Update interval only on success. */
178 ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS);
180 sr_err("Unable to send poll request.");
183 devc->cmd_sent_at = now;
188 static int process_data(struct sr_dev_inst *sdi,
189 const uint8_t *data, size_t dlen)
191 struct dev_context *devc;
192 const struct rdtech_um_profile *p;
200 sr_spew("Received poll packet (len: %zu).", dlen);
201 if (dlen < POLL_RECV_LEN) {
202 sr_err("Insufficient response data length: %zu", dlen);
206 if (!p->csum_ok(data, POLL_RECV_LEN)) {
207 sr_err("Packet checksum verification failed.");
211 for (ch_idx = 0; ch_idx < p->channel_count; ch_idx++) {
212 ret = bv_get_value(&v, &p->channels[ch_idx].spec, data, dlen);
215 ret = feed_queue_analog_submit(devc->feeds[ch_idx], v, 1);
220 sr_sw_limits_update_samples_read(&devc->limits, 1);
221 if (sr_sw_limits_check(&devc->limits))
222 sr_dev_acquisition_stop(sdi);
227 static int accum_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial)
229 struct dev_context *devc;
230 const struct rdtech_um_profile *p;
232 size_t space, rcvd, rdlen;
234 gboolean do_sync_check;
235 size_t sync_len, sync_idx;
238 * Receive data became available. Drain the serial transport.
239 * Grab incoming data in as large a chunk as possible. Also
240 * copes with zero receive data length, as some transports may
241 * trigger periodically without data really being available.
245 rdptr = &devc->buf[devc->buflen];
246 space = sizeof(devc->buf) - devc->buflen;
247 do_sync_check = FALSE;
248 sync_len = sizeof(uint16_t);
250 ret = serial_read_nonblocking(serial, rdptr, space);
258 if (devc->buflen < sync_len)
259 do_sync_check = TRUE;
260 devc->buflen += rcvd;
261 if (devc->buflen < sync_len)
262 do_sync_check = FALSE;
268 * Synchronize to the packetized input stream. Check the model
269 * ID at the start of receive data. Which is a weak condition,
270 * but going out of sync should be rare, and repeated attempts
271 * to synchronize should eventually succeed. Try to rate limit
272 * the emission of diagnostics messages. (Re-)run this logic
273 * at the first reception which makes enough data available,
274 * but not during subsequent accumulation of more data.
276 * Reducing redundancy in the implementation at the same time as
277 * increasing robustness would involve the creation of a checker
278 * routine, which just gets called for every byte position until
279 * it succeeds. Similar to what a previous implementation of the
280 * read loop did, which was expensive on the serial transport.
283 if (do_sync_check && read_u16be(&devc->buf[sync_idx]) != p->model_id)
284 sr_warn("Unexpected response data, trying to synchronize.");
285 while (do_sync_check) {
286 if (sync_idx + sync_len >= devc->buflen)
288 if (read_u16be(&devc->buf[sync_idx]) == p->model_id)
292 if (do_sync_check && sync_idx) {
293 sr_dbg("Skipping %zu bytes in attempt to sync.", sync_idx);
294 sync_len = devc->buflen - sync_idx;
296 memmove(&devc->buf[0], &devc->buf[sync_idx], sync_len);
297 devc->buflen -= sync_idx;
301 * Process packets as their reception completes. Periodically
302 * re-transmit poll requests. Discard consumed data after all
303 * processing has completed.
306 rdlen = devc->buflen;
308 while (ret == SR_OK && rdlen >= POLL_RECV_LEN) {
309 ret = process_data(sdi, rdptr, rdlen);
311 sr_err("Processing response packet failed.");
314 rdptr += POLL_RECV_LEN;
315 rdlen -= POLL_RECV_LEN;
317 if (0 && !sr_sw_limits_check(&devc->limits))
318 (void)rdtech_um_poll(sdi, FALSE);
320 rcvd = rdptr - devc->buf;
321 devc->buflen -= rcvd;
323 memmove(&devc->buf[0], rdptr, devc->buflen);
328 SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data)
330 struct sr_dev_inst *sdi;
331 struct dev_context *devc;
332 struct sr_serial_dev_inst *serial;
337 if (!(sdi = cb_data))
339 if (!(devc = sdi->priv))
343 * Drain and process receive data as it becomes available.
344 * Terminate acquisition upon receive or processing error.
347 if (revents == G_IO_IN) {
348 ret = accum_data(sdi, serial);
350 sr_dev_acquisition_stop(sdi);
355 /* Check configured acquisition limits. */
356 if (sr_sw_limits_check(&devc->limits)) {
357 sr_dev_acquisition_stop(sdi);
361 /* Periodically retransmit measurement requests. */
362 (void)rdtech_um_poll(sdi, FALSE);