]>
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 | ||
61 | static int poll_csum_fff1(char buf[], int len) | |
62 | { | |
63 | if (len != UM_POLL_LEN) | |
64 | return 0; | |
65 | else | |
66 | return RB16(&buf[len - 2]) == 0xFFF1; | |
67 | } | |
68 | ||
69 | static int poll_csum_um34c(char buf[], int len) | |
70 | { | |
71 | static const int positions[] = { | |
72 | 1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53, | |
73 | 55, 57, 59, 63, 67, 69, 73, 79, 83, 89, 97, 99, 109, | |
74 | 111, 113, 119, 121, 127, | |
75 | }; | |
fda34e5a | 76 | |
8b607a24 | 77 | unsigned int i; |
fda34e5a | 78 | uint8_t csum; |
8b607a24 AS |
79 | |
80 | if (len != UM_POLL_LEN) | |
81 | return 0; | |
82 | ||
fda34e5a | 83 | csum = 0; |
8b607a24 AS |
84 | for (i = 0; i < ARRAY_SIZE(positions); i++) |
85 | csum ^= buf[positions[i]]; | |
fda34e5a GS |
86 | if (csum != (uint8_t)buf[len - 1]) |
87 | return 0; | |
8b607a24 | 88 | |
fda34e5a | 89 | return 1; |
8b607a24 AS |
90 | } |
91 | ||
92 | static const struct rdtech_um_profile um_profiles[] = { | |
93 | { "UM24C", RDTECH_UM24C, rdtech_default_channels, &poll_csum_fff1, }, | |
94 | { "UM25C", RDTECH_UM25C, rdtech_um25c_channels, &poll_csum_fff1, }, | |
95 | { "UM34C", RDTECH_UM34C, rdtech_default_channels, &poll_csum_um34c, }, | |
96 | }; | |
97 | ||
98 | static const struct rdtech_um_profile *find_profile(uint16_t id) | |
99 | { | |
100 | unsigned int i; | |
fda34e5a | 101 | |
8b607a24 AS |
102 | for (i = 0; i < ARRAY_SIZE(um_profiles); i++) { |
103 | if (um_profiles[i].model_id == id) | |
104 | return &um_profiles[i]; | |
105 | } | |
106 | return NULL; | |
107 | } | |
108 | ||
109 | SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial) | |
110 | { | |
111 | const struct rdtech_um_profile *p; | |
fda34e5a | 112 | uint8_t request; |
8b607a24 AS |
113 | char buf[RDTECH_UM_BUFSIZE]; |
114 | int len; | |
115 | ||
fda34e5a | 116 | request = UM_CMD_POLL; |
8b607a24 | 117 | if (serial_write_blocking(serial, &request, sizeof(request), |
323b32f4 | 118 | SERIAL_WRITE_TIMEOUT_MS) < 0) { |
8b607a24 AS |
119 | sr_err("Unable to send probe request."); |
120 | return NULL; | |
121 | } | |
122 | ||
123 | len = serial_read_blocking(serial, buf, UM_POLL_LEN, UM_TIMEOUT_MS); | |
124 | if (len != UM_POLL_LEN) { | |
125 | sr_err("Failed to read probe response."); | |
126 | return NULL; | |
127 | } | |
128 | ||
129 | p = find_profile(RB16(&buf[0])); | |
130 | if (!p) { | |
131 | sr_err("Unrecognized UM device (0x%.4" PRIx16 ")!", RB16(&buf[0])); | |
132 | return NULL; | |
133 | } | |
134 | ||
135 | if (!p->poll_csum(buf, len)) { | |
136 | sr_err("Probe response contains illegal checksum or end marker.\n"); | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | return p; | |
141 | } | |
142 | ||
143 | SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi) | |
144 | { | |
fda34e5a GS |
145 | struct dev_context *devc; |
146 | struct sr_serial_dev_inst *serial; | |
147 | uint8_t request; | |
8b607a24 | 148 | |
fda34e5a GS |
149 | serial = sdi->conn; |
150 | request = UM_CMD_POLL; | |
8b607a24 | 151 | if (serial_write_blocking(serial, &request, sizeof(request), |
323b32f4 | 152 | SERIAL_WRITE_TIMEOUT_MS) < 0) { |
8b607a24 AS |
153 | sr_err("Unable to send poll request."); |
154 | return SR_ERR; | |
155 | } | |
156 | ||
fda34e5a | 157 | devc = sdi->priv; |
8b607a24 AS |
158 | devc->cmd_sent_at = g_get_monotonic_time() / 1000; |
159 | ||
160 | return SR_OK; | |
161 | } | |
162 | ||
163 | static void handle_poll_data(const struct sr_dev_inst *sdi) | |
164 | { | |
fda34e5a | 165 | struct dev_context *devc; |
8b607a24 AS |
166 | int i; |
167 | GSList *ch; | |
168 | ||
fda34e5a | 169 | devc = sdi->priv; |
8b607a24 AS |
170 | sr_spew("Received poll packet (len: %d).", devc->buflen); |
171 | if (devc->buflen != UM_POLL_LEN) { | |
172 | sr_err("Unexpected poll packet length: %i", devc->buflen); | |
173 | return; | |
174 | } | |
175 | ||
323b32f4 | 176 | for (ch = sdi->channels, i = 0; ch; ch = g_slist_next(ch), i++) { |
8b607a24 | 177 | bv_send_analog_channel(sdi, ch->data, |
323b32f4 GS |
178 | &devc->profile->channels[i], |
179 | devc->buf, devc->buflen); | |
180 | } | |
8b607a24 AS |
181 | |
182 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
183 | } | |
184 | ||
185 | static void recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial) | |
186 | { | |
fda34e5a GS |
187 | struct dev_context *devc; |
188 | const struct rdtech_um_profile *p; | |
8b607a24 AS |
189 | int len; |
190 | ||
191 | /* Serial data arrived. */ | |
fda34e5a GS |
192 | devc = sdi->priv; |
193 | p = devc->profile; | |
8b607a24 AS |
194 | while (devc->buflen < UM_POLL_LEN) { |
195 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); | |
196 | if (len < 1) | |
197 | return; | |
198 | ||
199 | devc->buflen++; | |
200 | ||
201 | /* Check if the poll model ID matches the profile. */ | |
202 | if (devc->buflen == 2 && RB16(devc->buf) != p->model_id) { | |
203 | sr_warn("Illegal model ID in poll response (0x%.4" PRIx16 ")," | |
204 | " skipping 1 byte.", | |
205 | RB16(devc->buf)); | |
206 | devc->buflen--; | |
207 | memmove(devc->buf, devc->buf + 1, devc->buflen); | |
208 | } | |
209 | } | |
210 | ||
211 | if (devc->buflen == UM_POLL_LEN && p->poll_csum(devc->buf, devc->buflen)) | |
212 | handle_poll_data(sdi); | |
213 | else | |
214 | sr_warn("Skipping packet with illegal checksum / end marker."); | |
215 | ||
216 | devc->buflen = 0; | |
217 | } | |
218 | ||
219 | SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data) | |
220 | { | |
221 | struct sr_dev_inst *sdi; | |
222 | struct dev_context *devc; | |
223 | struct sr_serial_dev_inst *serial; | |
224 | int64_t now, elapsed; | |
225 | ||
226 | (void)fd; | |
227 | ||
228 | if (!(sdi = cb_data)) | |
229 | return TRUE; | |
230 | ||
231 | if (!(devc = sdi->priv)) | |
232 | return TRUE; | |
233 | ||
234 | serial = sdi->conn; | |
235 | if (revents == G_IO_IN) | |
236 | recv_poll_data(sdi, serial); | |
237 | ||
238 | if (sr_sw_limits_check(&devc->limits)) { | |
239 | sr_dev_acquisition_stop(sdi); | |
240 | return TRUE; | |
241 | } | |
242 | ||
243 | now = g_get_monotonic_time() / 1000; | |
244 | elapsed = now - devc->cmd_sent_at; | |
245 | ||
246 | if (elapsed > UM_POLL_PERIOD_MS) | |
247 | rdtech_um_poll(sdi); | |
248 | ||
249 | return TRUE; | |
250 | } |