]> sigrok.org Git - libsigrok.git/blame - src/hardware/testo/protocol.c
Constify a few arrays and variables.
[libsigrok.git] / src / hardware / testo / protocol.c
CommitLineData
0acdd793
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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
6dcb9723 20#include <string.h>
0acdd793
BV
21#include "protocol.h"
22
6dcb9723
BV
23SR_PRIV int testo_set_serial_params(struct sr_usb_dev_inst *usb)
24{
25 int ret;
26
27 if ((ret = libusb_control_transfer(usb->devhdl, 0x40, FTDI_SET_BAUDRATE,
28 FTDI_BAUDRATE_115200, FTDI_INDEX, NULL, 0, 10)) < 0) {
29 sr_err("Failed to set baudrate: %s", libusb_error_name(ret));
30 return SR_ERR;
31 }
32
33 if ((ret = libusb_control_transfer(usb->devhdl, 0x40, FTDI_SET_PARAMS,
34 FTDI_PARAMS_8N1, FTDI_INDEX, NULL, 0, 10)) < 0) {
35 sr_err("Failed to set comm parameters: %s", libusb_error_name(ret));
36 return SR_ERR;
37 }
38
39 if ((ret = libusb_control_transfer(usb->devhdl, 0x40, FTDI_SET_FLOWCTRL,
40 FTDI_FLOW_NONE, FTDI_INDEX, NULL, 0, 10)) < 0) {
41 sr_err("Failed to set flow control: %s", libusb_error_name(ret));
42 return SR_ERR;
43 }
44
45 if ((ret = libusb_control_transfer(usb->devhdl, 0x40, FTDI_SET_MODEMCTRL,
46 FTDI_MODEM_ALLHIGH, FTDI_INDEX, NULL, 0, 10)) < 0) {
47 sr_err("Failed to set modem control: %s", libusb_error_name(ret));
48 return SR_ERR;
49 }
50
51 return SR_OK;
52}
53
54/* Due to the modular nature of the Testo hardware, you can't assume
55 * which measurements the device will supply. Fetch a single result
56 * set synchronously to see which measurements it has. */
57SR_PRIV int testo_probe_channels(struct sr_dev_inst *sdi)
0acdd793 58{
0acdd793 59 struct dev_context *devc;
6dcb9723 60 struct sr_usb_dev_inst *usb;
6dcb9723
BV
61 int unit, packet_len, len, i;
62 unsigned char packet[MAX_REPLY_SIZE], buf[MAX_REPLY_SIZE];
63 char *probe_name;
0acdd793 64
6dcb9723
BV
65 devc = sdi->priv;
66 usb = sdi->conn;
0acdd793 67
87895640 68 sr_dbg("Probing for channels.");
6dcb9723
BV
69 if (sdi->driver->dev_open(sdi) != SR_OK)
70 return SR_ERR;
71 if (testo_set_serial_params(usb) != SR_OK)
72 return SR_ERR;
87895640
BV
73
74 /* Flush anything buffered from a previous run. */
75 do {
76 libusb_bulk_transfer(usb->devhdl, EP_IN, buf, MAX_REPLY_SIZE, &len, 10);
77 } while (len > 2);
78
329733d9 79 if (libusb_bulk_transfer(usb->devhdl, EP_OUT, (unsigned char *)devc->model->request,
6dcb9723
BV
80 devc->model->request_size, &devc->reply_size, 10) < 0)
81 return SR_ERR;
87895640 82
6dcb9723
BV
83 packet_len = 0;
84 while(TRUE) {
85 if (libusb_bulk_transfer(usb->devhdl, EP_IN, buf, MAX_REPLY_SIZE,
86 &len, 250) < 0)
87 return SR_ERR;
88 if (len == 2)
89 /* FTDI cruft */
90 continue;
6dcb9723
BV
91 if (packet_len + len - 2 > MAX_REPLY_SIZE)
92 return SR_ERR;
87895640 93
6dcb9723
BV
94 memcpy(packet + packet_len, buf + 2, len - 2);
95 packet_len += len - 2;
87895640
BV
96 if (packet_len < 5)
97 /* Not even enough to check prefix yet. */
98 continue;
0acdd793 99
87895640
BV
100 if (!testo_check_packet_prefix(packet, packet_len)) {
101 /* Tail end of some previous data, drop it. */
102 packet_len = 0;
103 continue;
6dcb9723 104 }
87895640
BV
105
106 if (packet_len >= 7 + packet[6] * 7 + 2)
107 /* Got a complete packet. */
108 break;
6dcb9723 109 }
87895640 110 sdi->driver->dev_close(sdi);
0acdd793 111
6dcb9723
BV
112 if (packet[6] > MAX_CHANNELS) {
113 sr_err("Device says it has %d channels!", packet[6]);
114 return SR_ERR;
0acdd793
BV
115 }
116
6dcb9723
BV
117 for (i = 0; i < packet[6]; i++) {
118 unit = packet[7 + i * 7 + 4];
119 devc->channel_units[i] = unit;
120 switch (unit) {
121 case 1:
122 probe_name = "Temperature";
123 break;
124 case 3:
125 probe_name = "Humidity";
126 break;
127 case 5:
128 probe_name = "Windspeed";
129 break;
130 case 24:
131 probe_name = "Pressure";
132 break;
133 default:
134 sr_dbg("Unsupported measurement unit %d", unit);
135 return SR_ERR;
136 }
5e23fcab 137 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, probe_name);
6dcb9723
BV
138 }
139 devc->num_channels = packet[6];
87895640
BV
140 sr_dbg("Found %d channel%s.", devc->num_channels,
141 devc->num_channels > 1 ? "s" : "");
6dcb9723
BV
142
143 return SR_OK;
144}
145
146SR_PRIV int testo_request_packet(const struct sr_dev_inst *sdi)
147{
148 struct dev_context *devc;
149 struct sr_usb_dev_inst *usb;
150 int ret;
151
152 devc = sdi->priv;
153 usb = sdi->conn;
154
155 libusb_fill_bulk_transfer(devc->out_transfer, usb->devhdl, EP_OUT,
329733d9 156 (unsigned char *)devc->model->request, devc->model->request_size,
6dcb9723
BV
157 receive_transfer, (void *)sdi, 100);
158 if ((ret = libusb_submit_transfer(devc->out_transfer) != 0)) {
159 sr_err("Failed to request packet: %s.", libusb_error_name(ret));
160 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
161 devc->cb_data);
162 return SR_ERR;
163 }
164 sr_dbg("Requested new packet.");
165
166 return SR_OK;
167}
168
169/* Check if the packet is well-formed. This matches packets for the
170 * Testo 175/177/400/650/950/435/635/735/445/645/945/946/545. */
87895640 171SR_PRIV gboolean testo_check_packet_prefix(unsigned char *buf, int len)
6dcb9723
BV
172{
173 int i;
174 unsigned char check[] = { 0x21, 0, 0, 0, 1 };
175
176 if (len < 5)
177 return FALSE;
178
179 for (i = 0; i < 5; i++) {
180 if (buf[i] != check[i]) {
181 sr_dbg("Packet has invalid prefix.");
182 return FALSE;
183 }
184 }
185
0acdd793
BV
186 return TRUE;
187}
6dcb9723 188
87895640
BV
189SR_PRIV uint16_t crc16_mcrf4xx(uint16_t crc, uint8_t *data, size_t len)
190{
191 int i;
192
193 if (!data || !len)
194 return crc;
195
196 while (len--) {
197 crc ^= *data++;
198 for (i = 0; i < 8; i++) {
199 if (crc & 1)
200 crc = (crc >> 1) ^ 0x8408;
201 else
202 crc = (crc >> 1);
203 }
204 }
205
206 return crc;
207}
208
6dcb9723
BV
209static float binary32_le_to_float(unsigned char *buf)
210{
211 GFloatIEEE754 f;
212
213 f.v_float = 0;
214 f.mpn.sign = (buf[3] & 0x80) ? 1 : 0;
215 f.mpn.biased_exponent = (buf[3] << 1) | (buf[2] >> 7);
216 f.mpn.mantissa = buf[0] | (buf[1] << 8) | ((buf[2] & 0x7f) << 16);
217
218 return f.v_float;
219}
220
221SR_PRIV void testo_receive_packet(const struct sr_dev_inst *sdi)
222{
223 struct dev_context *devc;
224 struct sr_datafeed_packet packet;
225 struct sr_datafeed_analog analog;
226 struct sr_channel *ch;
227 GString *dbg;
228 float value;
229 int i;
230 unsigned char *buf;
231
232 devc = sdi->priv;
233 sr_dbg("Got %d-byte packet.", devc->reply_size);
234
235 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
236 dbg = g_string_sized_new(128);
237 g_string_printf(dbg, "Packet:");
238 for (i = 0; i < devc->reply_size; i++)
239 g_string_append_printf(dbg, " %.2x", devc->reply[i]);
240 sr_spew("%s", dbg->str);
241 g_string_free(dbg, TRUE);
242 }
243
6dcb9723
BV
244 packet.type = SR_DF_ANALOG;
245 packet.payload = &analog;
246 analog.num_samples = 1;
247 analog.mqflags = 0;
248 analog.data = &value;
249 /* Decode 7-byte values */
250 for (i = 0; i < devc->reply[6]; i++) {
251 buf = devc->reply + 7 + i * 7;
252 value = binary32_le_to_float(buf);
6dcb9723
BV
253 switch (buf[4]) {
254 case 1:
255 analog.mq = SR_MQ_TEMPERATURE;
256 analog.unit = SR_UNIT_CELSIUS;
257 break;
258 case 3:
259 analog.mq = SR_MQ_RELATIVE_HUMIDITY;
260 analog.unit = SR_UNIT_HUMIDITY_293K;
261 break;
262 case 5:
263 analog.mq = SR_MQ_WIND_SPEED;
264 analog.unit = SR_UNIT_METER_SECOND;
265 break;
266 case 24:
267 analog.mq = SR_MQ_PRESSURE;
268 analog.unit = SR_UNIT_HECTOPASCAL;
269 break;
270 default:
87895640 271 sr_dbg("Unsupported measurement unit %d.", buf[4]);
6dcb9723
BV
272 return;
273 }
274
275 /* Match this measurement with its channel. */
276 for (i = 0; i < devc->num_channels; i++) {
277 if (devc->channel_units[i] == buf[4])
278 break;
279 }
280 if (i == devc->num_channels) {
281 /* Shouldn't happen. */
282 sr_err("Some channel hotswapped in!");
283 return;
284 }
285 ch = g_slist_nth_data(sdi->channels, i);
6dcb9723
BV
286 analog.channels = g_slist_append(NULL, ch);
287 sr_session_send(sdi, &packet);
288 g_slist_free(analog.channels);
289 }
290}