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