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