2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 SR_PRIV int testo_set_serial_params(struct sr_usb_dev_inst *usb)
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));
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));
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));
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));
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. */
57 SR_PRIV int testo_probe_channels(struct sr_dev_inst *sdi)
59 struct dev_context *devc;
60 struct sr_usb_dev_inst *usb;
61 struct sr_channel *ch;
62 int unit, packet_len, len, i;
63 unsigned char packet[MAX_REPLY_SIZE], buf[MAX_REPLY_SIZE];
69 if (sdi->driver->dev_open(sdi) != SR_OK)
71 if (testo_set_serial_params(usb) != SR_OK)
73 if (libusb_bulk_transfer(usb->devhdl, EP_OUT, devc->model->request,
74 devc->model->request_size, &devc->reply_size, 10) < 0)
78 if (libusb_bulk_transfer(usb->devhdl, EP_IN, buf, MAX_REPLY_SIZE,
84 sr_dbg("got %d", len);
85 if (packet_len + len - 2 > MAX_REPLY_SIZE)
87 memcpy(packet + packet_len, buf + 2, len - 2);
88 packet_len += len - 2;
90 if (packet_len >= 7) {
91 if (!testo_check_packet(packet, packet_len))
93 if (packet_len >= 7 + packet[6] * 7 + 2)
94 /* Got a complete packet. */
99 if (packet[6] > MAX_CHANNELS) {
100 sr_err("Device says it has %d channels!", packet[6]);
104 for (i = 0; i < packet[6]; i++) {
105 unit = packet[7 + i * 7 + 4];
106 devc->channel_units[i] = unit;
109 probe_name = "Temperature";
112 probe_name = "Humidity";
115 probe_name = "Windspeed";
118 probe_name = "Pressure";
121 sr_dbg("Unsupported measurement unit %d", unit);
124 ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, probe_name);
125 sdi->channels = g_slist_append(sdi->channels, ch);
127 devc->num_channels = packet[6];
129 sdi->driver->dev_close(sdi);
134 SR_PRIV int testo_request_packet(const struct sr_dev_inst *sdi)
136 struct dev_context *devc;
137 struct sr_usb_dev_inst *usb;
143 libusb_fill_bulk_transfer(devc->out_transfer, usb->devhdl, EP_OUT,
144 devc->model->request, devc->model->request_size,
145 receive_transfer, (void *)sdi, 100);
146 if ((ret = libusb_submit_transfer(devc->out_transfer) != 0)) {
147 sr_err("Failed to request packet: %s.", libusb_error_name(ret));
148 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
152 sr_dbg("Requested new packet.");
157 /* Check if the packet is well-formed. This matches packets for the
158 * Testo 175/177/400/650/950/435/635/735/445/645/945/946/545. */
159 SR_PRIV gboolean testo_check_packet(unsigned char *buf, int len)
162 unsigned char check[] = { 0x21, 0, 0, 0, 1 };
167 for (i = 0; i < 5; i++) {
168 if (buf[i] != check[i]) {
169 sr_dbg("Packet has invalid prefix.");
174 /* TODO: check FCS, algorithm corresponds to python crcmod crc-16-mcrf4xx */
179 static float binary32_le_to_float(unsigned char *buf)
184 f.mpn.sign = (buf[3] & 0x80) ? 1 : 0;
185 f.mpn.biased_exponent = (buf[3] << 1) | (buf[2] >> 7);
186 f.mpn.mantissa = buf[0] | (buf[1] << 8) | ((buf[2] & 0x7f) << 16);
191 SR_PRIV void testo_receive_packet(const struct sr_dev_inst *sdi)
193 struct dev_context *devc;
194 struct sr_datafeed_packet packet;
195 struct sr_datafeed_analog analog;
196 struct sr_channel *ch;
203 sr_dbg("Got %d-byte packet.", devc->reply_size);
205 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
206 dbg = g_string_sized_new(128);
207 g_string_printf(dbg, "Packet:");
208 for (i = 0; i < devc->reply_size; i++)
209 g_string_append_printf(dbg, " %.2x", devc->reply[i]);
210 sr_spew("%s", dbg->str);
211 g_string_free(dbg, TRUE);
214 if (!testo_check_packet(devc->reply, devc->reply_size))
217 packet.type = SR_DF_ANALOG;
218 packet.payload = &analog;
219 analog.num_samples = 1;
221 analog.data = &value;
222 /* Decode 7-byte values */
223 for (i = 0; i < devc->reply[6]; i++) {
224 buf = devc->reply + 7 + i * 7;
225 value = binary32_le_to_float(buf);
226 sr_dbg("value: %f unit %d exp %d", value, buf[4], buf[6]);
229 analog.mq = SR_MQ_TEMPERATURE;
230 analog.unit = SR_UNIT_CELSIUS;
233 analog.mq = SR_MQ_RELATIVE_HUMIDITY;
234 analog.unit = SR_UNIT_HUMIDITY_293K;
237 analog.mq = SR_MQ_WIND_SPEED;
238 analog.unit = SR_UNIT_METER_SECOND;
241 analog.mq = SR_MQ_PRESSURE;
242 analog.unit = SR_UNIT_HECTOPASCAL;
245 sr_dbg("Unsupported measurement unit %d", buf[4]);
249 /* Match this measurement with its channel. */
250 for (i = 0; i < devc->num_channels; i++) {
251 if (devc->channel_units[i] == buf[4])
254 if (i == devc->num_channels) {
255 /* Shouldn't happen. */
256 sr_err("Some channel hotswapped in!");
259 ch = g_slist_nth_data(sdi->channels, i);
260 sr_dbg("channel %d name %s unit %d", i, ch->name, analog.unit);
261 analog.channels = g_slist_append(NULL, ch);
262 sr_session_send(sdi, &packet);
263 g_slist_free(analog.channels);