]>
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 | 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; | |
66 | ||
67 | devc = sdi->priv; | |
68 | usb = sdi->conn; | |
69 | ||
70 | sr_dbg("Probing for channels."); | |
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; | |
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 | ||
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; | |
84 | ||
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; | |
93 | if (packet_len + len - 2 > MAX_REPLY_SIZE) | |
94 | return SR_ERR; | |
95 | ||
96 | memcpy(packet + packet_len, buf + 2, len - 2); | |
97 | packet_len += len - 2; | |
98 | if (packet_len < 5) | |
99 | /* Not even enough to check prefix yet. */ | |
100 | continue; | |
101 | ||
102 | if (!testo_check_packet_prefix(packet, packet_len)) { | |
103 | /* Tail end of some previous data, drop it. */ | |
104 | packet_len = 0; | |
105 | continue; | |
106 | } | |
107 | ||
108 | if (packet_len >= 7 + packet[6] * 7 + 2) | |
109 | /* Got a complete packet. */ | |
110 | break; | |
111 | } | |
112 | sdi->driver->dev_close(sdi); | |
113 | ||
114 | if (packet[6] > MAX_CHANNELS) { | |
115 | sr_err("Device says it has %d channels!", packet[6]); | |
116 | return SR_ERR; | |
117 | } | |
118 | ||
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]; | |
143 | sr_dbg("Found %d channel%s.", devc->num_channels, | |
144 | devc->num_channels > 1 ? "s" : ""); | |
145 | ||
146 | return SR_OK; | |
147 | } | |
148 | ||
149 | SR_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. */ | |
174 | SR_PRIV gboolean testo_check_packet_prefix(unsigned char *buf, int len) | |
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 | ||
189 | return TRUE; | |
190 | } | |
191 | ||
192 | SR_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 | ||
212 | static 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 | ||
224 | SR_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 | ||
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); | |
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: | |
274 | sr_dbg("Unsupported measurement unit %d.", buf[4]); | |
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); | |
289 | analog.channels = g_slist_append(NULL, ch); | |
290 | sr_session_send(sdi, &packet); | |
291 | g_slist_free(analog.channels); | |
292 | } | |
293 | } | |
294 |