]> sigrok.org Git - libsigrok.git/blame - src/hardware/rdtech-um/protocol.c
rdtech-um: address style nits (whitespace, braces)
[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>
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
24#include <glib.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27#include "protocol.h"
28
29#define SERIAL_WRITE_TIMEOUT_MS 1
30
31#define UM_POLL_LEN 130
32#define UM_POLL_PERIOD_MS 100
33#define UM_TIMEOUT_MS 1000
34
35#define UM_CMD_POLL 0xf0
36
37static const struct binary_analog_channel rdtech_default_channels[] = {
323b32f4
GS
38 { "V", { 2, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
39 { "I", { 4, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_CURRENT, SR_UNIT_AMPERE },
8b607a24
AS
40 { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
41 { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
323b32f4 42 { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
8b607a24
AS
43 /* Threshold-based recording (mWh) */
44 { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
0988d4ae 45 ALL_ZERO,
8b607a24
AS
46};
47
48static const struct binary_analog_channel rdtech_um25c_channels[] = {
238a6ef2
GS
49 { "V", { 2, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
50 { "I", { 4, BVT_BE_UINT16, 0.0001, }, 4, SR_MQ_CURRENT, SR_UNIT_AMPERE },
8b607a24
AS
51 { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
52 { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
53 { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
54 /* Threshold-based recording (mWh) */
55 { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
0988d4ae 56 ALL_ZERO,
8b607a24
AS
57};
58
59static int poll_csum_fff1(char buf[], int len)
60{
61 if (len != UM_POLL_LEN)
62 return 0;
63 else
64 return RB16(&buf[len - 2]) == 0xFFF1;
65}
66
67static int poll_csum_um34c(char buf[], int len)
68{
69 static const int positions[] = {
70 1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53,
71 55, 57, 59, 63, 67, 69, 73, 79, 83, 89, 97, 99, 109,
72 111, 113, 119, 121, 127,
73 };
74 unsigned int i;
75 uint8_t csum = 0;
76
77 if (len != UM_POLL_LEN)
78 return 0;
79
80 for (i = 0; i < ARRAY_SIZE(positions); i++)
81 csum ^= buf[positions[i]];
82
83 return csum == (uint8_t)buf[len - 1];
84}
85
86static const struct rdtech_um_profile um_profiles[] = {
87 { "UM24C", RDTECH_UM24C, rdtech_default_channels, &poll_csum_fff1, },
88 { "UM25C", RDTECH_UM25C, rdtech_um25c_channels, &poll_csum_fff1, },
89 { "UM34C", RDTECH_UM34C, rdtech_default_channels, &poll_csum_um34c, },
90};
91
92static const struct rdtech_um_profile *find_profile(uint16_t id)
93{
94 unsigned int i;
95 for (i = 0; i < ARRAY_SIZE(um_profiles); i++) {
96 if (um_profiles[i].model_id == id)
97 return &um_profiles[i];
98 }
99 return NULL;
100}
101
102SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial)
103{
104 const struct rdtech_um_profile *p;
105 static const uint8_t request = UM_CMD_POLL;
106 char buf[RDTECH_UM_BUFSIZE];
107 int len;
108
109 if (serial_write_blocking(serial, &request, sizeof(request),
323b32f4 110 SERIAL_WRITE_TIMEOUT_MS) < 0) {
8b607a24
AS
111 sr_err("Unable to send probe request.");
112 return NULL;
113 }
114
115 len = serial_read_blocking(serial, buf, UM_POLL_LEN, UM_TIMEOUT_MS);
116 if (len != UM_POLL_LEN) {
117 sr_err("Failed to read probe response.");
118 return NULL;
119 }
120
121 p = find_profile(RB16(&buf[0]));
122 if (!p) {
123 sr_err("Unrecognized UM device (0x%.4" PRIx16 ")!", RB16(&buf[0]));
124 return NULL;
125 }
126
127 if (!p->poll_csum(buf, len)) {
128 sr_err("Probe response contains illegal checksum or end marker.\n");
129 return NULL;
130 }
131
132 return p;
133}
134
135SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi)
136{
137 struct dev_context *devc = sdi->priv;
138 struct sr_serial_dev_inst *serial = sdi->conn;
139 static const uint8_t request = UM_CMD_POLL;
140
141 if (serial_write_blocking(serial, &request, sizeof(request),
323b32f4 142 SERIAL_WRITE_TIMEOUT_MS) < 0) {
8b607a24
AS
143 sr_err("Unable to send poll request.");
144 return SR_ERR;
145 }
146
147 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
148
149 return SR_OK;
150}
151
152static void handle_poll_data(const struct sr_dev_inst *sdi)
153{
154 struct dev_context *devc = sdi->priv;
155 int i;
156 GSList *ch;
157
158 sr_spew("Received poll packet (len: %d).", devc->buflen);
159 if (devc->buflen != UM_POLL_LEN) {
160 sr_err("Unexpected poll packet length: %i", devc->buflen);
161 return;
162 }
163
323b32f4 164 for (ch = sdi->channels, i = 0; ch; ch = g_slist_next(ch), i++) {
8b607a24 165 bv_send_analog_channel(sdi, ch->data,
323b32f4
GS
166 &devc->profile->channels[i],
167 devc->buf, devc->buflen);
168 }
8b607a24
AS
169
170 sr_sw_limits_update_samples_read(&devc->limits, 1);
171}
172
173static void recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial)
174{
175 struct dev_context *devc = sdi->priv;
176 const struct rdtech_um_profile *p = devc->profile;
177 int len;
178
179 /* Serial data arrived. */
180 while (devc->buflen < UM_POLL_LEN) {
181 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
182 if (len < 1)
183 return;
184
185 devc->buflen++;
186
187 /* Check if the poll model ID matches the profile. */
188 if (devc->buflen == 2 && RB16(devc->buf) != p->model_id) {
189 sr_warn("Illegal model ID in poll response (0x%.4" PRIx16 "),"
190 " skipping 1 byte.",
191 RB16(devc->buf));
192 devc->buflen--;
193 memmove(devc->buf, devc->buf + 1, devc->buflen);
194 }
195 }
196
197 if (devc->buflen == UM_POLL_LEN && p->poll_csum(devc->buf, devc->buflen))
198 handle_poll_data(sdi);
199 else
200 sr_warn("Skipping packet with illegal checksum / end marker.");
201
202 devc->buflen = 0;
203}
204
205SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data)
206{
207 struct sr_dev_inst *sdi;
208 struct dev_context *devc;
209 struct sr_serial_dev_inst *serial;
210 int64_t now, elapsed;
211
212 (void)fd;
213
214 if (!(sdi = cb_data))
215 return TRUE;
216
217 if (!(devc = sdi->priv))
218 return TRUE;
219
220 serial = sdi->conn;
221 if (revents == G_IO_IN)
222 recv_poll_data(sdi, serial);
223
224 if (sr_sw_limits_check(&devc->limits)) {
225 sr_dev_acquisition_stop(sdi);
226 return TRUE;
227 }
228
229 now = g_get_monotonic_time() / 1000;
230 elapsed = now - devc->cmd_sent_at;
231
232 if (elapsed > UM_POLL_PERIOD_MS)
233 rdtech_um_poll(sdi);
234
235 return TRUE;
236}