]> sigrok.org Git - libsigrok.git/blame - src/hardware/rdtech-um/protocol.c
rdtech-um: adjust read/write timeout style for text line length
[libsigrok.git] / src / hardware / rdtech-um / protocol.c
CommitLineData
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
42static const struct binary_analog_channel rdtech_default_channels[] = {
323b32f4
GS
43 { "V", { 2, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
44 { "I", { 4, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_CURRENT, SR_UNIT_AMPERE },
8b607a24
AS
45 { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
46 { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
323b32f4 47 { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
8b607a24
AS
48 /* Threshold-based recording (mWh) */
49 { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
0988d4ae 50 ALL_ZERO,
8b607a24
AS
51};
52
53static const struct binary_analog_channel rdtech_um25c_channels[] = {
238a6ef2
GS
54 { "V", { 2, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
55 { "I", { 4, BVT_BE_UINT16, 0.0001, }, 4, SR_MQ_CURRENT, SR_UNIT_AMPERE },
8b607a24
AS
56 { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
57 { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
58 { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
59 /* Threshold-based recording (mWh) */
60 { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
0988d4ae 61 ALL_ZERO,
8b607a24
AS
62};
63
85accbc2 64static gboolean csum_ok_fff1(const uint8_t *buf, size_t len)
8b607a24 65{
85accbc2
GS
66 uint16_t csum_recv;
67
ae33433b 68 if (len != POLL_RECV_LEN)
85accbc2
GS
69 return FALSE;
70
71 csum_recv = read_u16be(&buf[len - sizeof(uint16_t)]);
72 if (csum_recv != 0xfff1)
73 return FALSE;
74
75 return TRUE;
8b607a24
AS
76}
77
85accbc2 78static gboolean csum_ok_um34c(const uint8_t *buf, size_t len)
8b607a24
AS
79{
80 static const int positions[] = {
81 1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53,
82 55, 57, 59, 63, 67, 69, 73, 79, 83, 89, 97, 99, 109,
83 111, 113, 119, 121, 127,
84 };
fda34e5a 85
85accbc2
GS
86 size_t i;
87 uint8_t csum_calc, csum_recv;
8b607a24 88
ae33433b 89 if (len != POLL_RECV_LEN)
85accbc2 90 return FALSE;
8b607a24 91
85accbc2 92 csum_calc = 0;
8b607a24 93 for (i = 0; i < ARRAY_SIZE(positions); i++)
85accbc2
GS
94 csum_calc ^= buf[positions[i]];
95 csum_recv = read_u8(&buf[len - sizeof(uint8_t)]);
96 if (csum_recv != csum_calc)
97 return FALSE;
8b607a24 98
85accbc2 99 return TRUE;
8b607a24
AS
100}
101
102static const struct rdtech_um_profile um_profiles[] = {
85accbc2
GS
103 { "UM24C", RDTECH_UM24C, rdtech_default_channels, csum_ok_fff1, },
104 { "UM25C", RDTECH_UM25C, rdtech_um25c_channels, csum_ok_fff1, },
105 { "UM34C", RDTECH_UM34C, rdtech_default_channels, csum_ok_um34c, },
8b607a24
AS
106};
107
108static const struct rdtech_um_profile *find_profile(uint16_t id)
109{
4441e573
GS
110 size_t i;
111 const struct rdtech_um_profile *profile;
fda34e5a 112
8b607a24 113 for (i = 0; i < ARRAY_SIZE(um_profiles); i++) {
4441e573
GS
114 profile = &um_profiles[i];
115 if (profile->model_id == id)
116 return profile;
8b607a24
AS
117 }
118 return NULL;
119}
120
121SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial)
122{
123 const struct rdtech_um_profile *p;
ae33433b
GS
124 uint8_t req;
125 int ret;
85accbc2 126 uint8_t buf[RDTECH_UM_BUFSIZE];
4441e573 127 int rcvd;
bcbf2b59 128 uint16_t model_id;
8b607a24 129
ae33433b
GS
130 req = UM_CMD_POLL;
131 ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS);
132 if (ret < 0) {
133 sr_err("Failed to send probe request.");
8b607a24
AS
134 return NULL;
135 }
136
ae33433b
GS
137 rcvd = serial_read_blocking(serial, buf, POLL_RECV_LEN, PROBE_TO_MS);
138 if (rcvd != POLL_RECV_LEN) {
8b607a24
AS
139 sr_err("Failed to read probe response.");
140 return NULL;
141 }
142
bcbf2b59
GS
143 model_id = read_u16be(&buf[0]);
144 p = find_profile(model_id);
8b607a24 145 if (!p) {
bcbf2b59 146 sr_err("Unrecognized UM device (0x%.4" PRIx16 ").", model_id);
8b607a24
AS
147 return NULL;
148 }
149
4441e573 150 if (!p->csum_ok(buf, rcvd)) {
bcbf2b59 151 sr_err("Probe response fails checksum verification.");
8b607a24
AS
152 return NULL;
153 }
154
155 return p;
156}
157
c1f4f589 158SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force)
8b607a24 159{
fda34e5a 160 struct dev_context *devc;
c1f4f589 161 int64_t now, elapsed;
fda34e5a 162 struct sr_serial_dev_inst *serial;
ae33433b
GS
163 uint8_t req;
164 int ret;
8b607a24 165
c1f4f589
GS
166 /* Check for expired intervals or forced requests. */
167 devc = sdi->priv;
168 now = g_get_monotonic_time() / 1000;
169 elapsed = now - devc->cmd_sent_at;
ae33433b 170 if (!force && elapsed < POLL_PERIOD_MS)
c1f4f589
GS
171 return SR_OK;
172
173 /* Send another poll request. Update interval only on success. */
fda34e5a 174 serial = sdi->conn;
ae33433b
GS
175 req = UM_CMD_POLL;
176 ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS);
177 if (ret < 0) {
8b607a24
AS
178 sr_err("Unable to send poll request.");
179 return SR_ERR;
180 }
c1f4f589 181 devc->cmd_sent_at = now;
8b607a24
AS
182
183 return SR_OK;
184}
185
186static void handle_poll_data(const struct sr_dev_inst *sdi)
187{
fda34e5a 188 struct dev_context *devc;
4441e573 189 size_t ch_idx;
8b607a24
AS
190 GSList *ch;
191
fda34e5a 192 devc = sdi->priv;
85accbc2 193 sr_spew("Received poll packet (len: %zu).", devc->buflen);
ae33433b 194 if (devc->buflen != POLL_RECV_LEN) {
85accbc2 195 sr_err("Unexpected poll packet length: %zu", devc->buflen);
8b607a24
AS
196 return;
197 }
198
4441e573
GS
199 ch_idx = 0;
200 for (ch = sdi->channels; ch; ch = g_slist_next(ch)) {
8b607a24 201 bv_send_analog_channel(sdi, ch->data,
4441e573 202 &devc->profile->channels[ch_idx],
323b32f4 203 devc->buf, devc->buflen);
4441e573 204 ch_idx++;
323b32f4 205 }
8b607a24
AS
206
207 sr_sw_limits_update_samples_read(&devc->limits, 1);
208}
209
210static void recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial)
211{
fda34e5a
GS
212 struct dev_context *devc;
213 const struct rdtech_um_profile *p;
8b607a24
AS
214 int len;
215
216 /* Serial data arrived. */
fda34e5a
GS
217 devc = sdi->priv;
218 p = devc->profile;
ae33433b 219 while (devc->buflen < POLL_RECV_LEN) {
8b607a24
AS
220 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
221 if (len < 1)
222 return;
bcbf2b59 223 devc->buflen += len;
8b607a24
AS
224
225 /* Check if the poll model ID matches the profile. */
226 if (devc->buflen == 2 && RB16(devc->buf) != p->model_id) {
227 sr_warn("Illegal model ID in poll response (0x%.4" PRIx16 "),"
228 " skipping 1 byte.",
229 RB16(devc->buf));
230 devc->buflen--;
231 memmove(devc->buf, devc->buf + 1, devc->buflen);
232 }
233 }
234
ae33433b 235 if (devc->buflen != POLL_RECV_LEN)
bcbf2b59
GS
236 sr_warn("Skipping packet, unexpected receive length.");
237 else if (!p->csum_ok(devc->buf, devc->buflen))
238 sr_warn("Skipping packet, checksum verification failed.");
8b607a24 239 else
bcbf2b59 240 handle_poll_data(sdi);
8b607a24
AS
241 devc->buflen = 0;
242}
243
244SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data)
245{
246 struct sr_dev_inst *sdi;
247 struct dev_context *devc;
248 struct sr_serial_dev_inst *serial;
8b607a24
AS
249
250 (void)fd;
251
252 if (!(sdi = cb_data))
253 return TRUE;
8b607a24
AS
254 if (!(devc = sdi->priv))
255 return TRUE;
256
ae33433b 257 /* Drain and process receive data as it becomes available. */
8b607a24
AS
258 serial = sdi->conn;
259 if (revents == G_IO_IN)
260 recv_poll_data(sdi, serial);
261
ae33433b 262 /* Check configured acquisition limits. */
8b607a24
AS
263 if (sr_sw_limits_check(&devc->limits)) {
264 sr_dev_acquisition_stop(sdi);
265 return TRUE;
266 }
267
ae33433b 268 /* Periodically emit measurement requests. */
c1f4f589 269 (void)rdtech_um_poll(sdi, FALSE);
8b607a24
AS
270
271 return TRUE;
272}