]> sigrok.org Git - libsigrok.git/blob - src/hardware/rdtech-um/protocol.c
rdtech-um: rephrase channel count in profile description
[libsigrok.git] / src / hardware / rdtech-um / protocol.c
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>
21
22 #include <glib.h>
23 #include <libsigrok/libsigrok.h>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
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
35
36 /* Expected receive data size for poll responses. */
37 #define POLL_RECV_LEN 130
38
39 /* Command code to request another poll response. */
40 #define UM_CMD_POLL 0xf0
41
42 static const struct binary_analog_channel default_channels[] = {
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 },
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 },
47         { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
48         /* Threshold-based recording (mWh) */
49         { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
50 };
51
52 static const struct binary_analog_channel um25c_channels[] = {
53         { "V", { 2, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
54         { "I", { 4, BVT_BE_UINT16, 0.0001, }, 4, SR_MQ_CURRENT, SR_UNIT_AMPERE },
55         { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
56         { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
57         { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
58         /* Threshold-based recording (mWh) */
59         { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
60 };
61
62 static gboolean csum_ok_fff1(const uint8_t *buf, size_t len)
63 {
64         uint16_t csum_recv;
65
66         if (len != POLL_RECV_LEN)
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;
74 }
75
76 static gboolean csum_ok_um34c(const uint8_t *buf, size_t len)
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         };
83
84         size_t i;
85         uint8_t csum_calc, csum_recv;
86
87         if (len != POLL_RECV_LEN)
88                 return FALSE;
89
90         csum_calc = 0;
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)
95                 return FALSE;
96
97         return TRUE;
98 }
99
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, },
104 };
105
106 static const struct rdtech_um_profile *find_profile(uint16_t id)
107 {
108         size_t i;
109         const struct rdtech_um_profile *profile;
110
111         for (i = 0; i < ARRAY_SIZE(um_profiles); i++) {
112                 profile = &um_profiles[i];
113                 if (profile->model_id == id)
114                         return profile;
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;
122         uint8_t req;
123         int ret;
124         uint8_t buf[RDTECH_UM_BUFSIZE];
125         int rcvd;
126         uint16_t model_id;
127
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.");
132                 return NULL;
133         }
134
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.");
138                 return NULL;
139         }
140
141         model_id = read_u16be(&buf[0]);
142         p = find_profile(model_id);
143         if (!p) {
144                 sr_err("Unrecognized UM device (0x%.4" PRIx16 ").", model_id);
145                 return NULL;
146         }
147
148         if (!p->csum_ok(buf, rcvd)) {
149                 sr_err("Probe response fails checksum verification.");
150                 return NULL;
151         }
152
153         return p;
154 }
155
156 SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force)
157 {
158         struct dev_context *devc;
159         int64_t now, elapsed;
160         struct sr_serial_dev_inst *serial;
161         uint8_t req;
162         int ret;
163
164         /* Check for expired intervals or forced requests. */
165         devc = sdi->priv;
166         now = g_get_monotonic_time() / 1000;
167         elapsed = now - devc->cmd_sent_at;
168         if (!force && elapsed < POLL_PERIOD_MS)
169                 return SR_OK;
170
171         /* Send another poll request. Update interval only on success. */
172         serial = sdi->conn;
173         req = UM_CMD_POLL;
174         ret = serial_write_blocking(serial, &req, sizeof(req), WRITE_TO_MS);
175         if (ret < 0) {
176                 sr_err("Unable to send poll request.");
177                 return SR_ERR;
178         }
179         devc->cmd_sent_at = now;
180
181         return SR_OK;
182 }
183
184 static void handle_poll_data(const struct sr_dev_inst *sdi)
185 {
186         struct dev_context *devc;
187         size_t ch_idx;
188         GSList *ch;
189
190         devc = sdi->priv;
191         sr_spew("Received poll packet (len: %zu).", devc->buflen);
192         if (devc->buflen != POLL_RECV_LEN) {
193                 sr_err("Unexpected poll packet length: %zu", devc->buflen);
194                 return;
195         }
196
197         ch_idx = 0;
198         for (ch = sdi->channels; ch; ch = g_slist_next(ch)) {
199                 bv_send_analog_channel(sdi, ch->data,
200                         &devc->profile->channels[ch_idx],
201                         devc->buf, devc->buflen);
202                 ch_idx++;
203         }
204
205         sr_sw_limits_update_samples_read(&devc->limits, 1);
206 }
207
208 static void recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial)
209 {
210         struct dev_context *devc;
211         const struct rdtech_um_profile *p;
212         int len;
213
214         /* Serial data arrived. */
215         devc = sdi->priv;
216         p = devc->profile;
217         while (devc->buflen < POLL_RECV_LEN) {
218                 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
219                 if (len < 1)
220                         return;
221                 devc->buflen += len;
222
223                 /* Check if the poll model ID matches the profile. */
224                 if (devc->buflen == 2 && RB16(devc->buf) != p->model_id) {
225                         sr_warn("Illegal model ID in poll response (0x%.4" PRIx16 "),"
226                                 " skipping 1 byte.",
227                                 RB16(devc->buf));
228                         devc->buflen--;
229                         memmove(devc->buf, devc->buf + 1, devc->buflen);
230                 }
231         }
232
233         if (devc->buflen != POLL_RECV_LEN)
234                 sr_warn("Skipping packet, unexpected receive length.");
235         else if (!p->csum_ok(devc->buf, devc->buflen))
236                 sr_warn("Skipping packet, checksum verification failed.");
237         else
238                 handle_poll_data(sdi);
239         devc->buflen = 0;
240 }
241
242 SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data)
243 {
244         struct sr_dev_inst *sdi;
245         struct dev_context *devc;
246         struct sr_serial_dev_inst *serial;
247
248         (void)fd;
249
250         if (!(sdi = cb_data))
251                 return TRUE;
252         if (!(devc = sdi->priv))
253                 return TRUE;
254
255         /* Drain and process receive data as it becomes available. */
256         serial = sdi->conn;
257         if (revents == G_IO_IN)
258                 recv_poll_data(sdi, serial);
259
260         /* Check configured acquisition limits. */
261         if (sr_sw_limits_check(&devc->limits)) {
262                 sr_dev_acquisition_stop(sdi);
263                 return TRUE;
264         }
265
266         /* Periodically emit measurement requests. */
267         (void)rdtech_um_poll(sdi, FALSE);
268
269         return TRUE;
270 }