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