]>
Commit | Line | Data |
---|---|---|
46697e38 BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 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 | ||
6ec6c43b | 20 | #include <config.h> |
46697e38 | 21 | #include <stdlib.h> |
851d5b22 BV |
22 | #include <sys/time.h> |
23 | #include <string.h> | |
6aa1eb4e | 24 | #include <math.h> |
46697e38 | 25 | #include <glib.h> |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
46697e38 BV |
27 | #include "libsigrok-internal.h" |
28 | #include "protocol.h" | |
29 | ||
0350e2e0 | 30 | extern SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info; |
4f840ce9 | 31 | struct sr_dev_driver *di = &lascar_el_usb_driver_info; |
851d5b22 BV |
32 | |
33 | static const struct elusb_profile profiles[] = { | |
34 | { 1, "EL-USB-1", LOG_UNSUPPORTED }, | |
35 | { 2, "EL-USB-1", LOG_UNSUPPORTED }, | |
36 | { 3, "EL-USB-2", LOG_TEMP_RH }, | |
37 | { 4, "EL-USB-3", LOG_UNSUPPORTED }, | |
38 | { 5, "EL-USB-4", LOG_UNSUPPORTED }, | |
39 | { 6, "EL-USB-3", LOG_UNSUPPORTED }, | |
40 | { 7, "EL-USB-4", LOG_UNSUPPORTED }, | |
41 | { 8, "EL-USB-LITE", LOG_UNSUPPORTED }, | |
42 | { 9, "EL-USB-CO", LOG_CO }, | |
43 | { 10, "EL-USB-TC", LOG_UNSUPPORTED }, | |
b6506d5e | 44 | { 11, "EL-USB-CO300", LOG_CO }, |
6787f404 BV |
45 | { 12, "EL-USB-2-LCD", LOG_TEMP_RH }, |
46 | { 13, "EL-USB-2+", LOG_TEMP_RH }, | |
851d5b22 BV |
47 | { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED }, |
48 | { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED }, | |
6787f404 | 49 | { 16, "EL-USB-2-LCD+", LOG_TEMP_RH }, |
851d5b22 BV |
50 | { 17, "EL-USB-5", LOG_UNSUPPORTED }, |
51 | { 18, "EL-USB-1-RCG", LOG_UNSUPPORTED }, | |
52 | { 19, "EL-USB-1-LCD", LOG_UNSUPPORTED }, | |
53 | { 20, "EL-OEM-3", LOG_UNSUPPORTED }, | |
54 | { 21, "EL-USB-1-LCD", LOG_UNSUPPORTED }, | |
9e9dba7b | 55 | ALL_ZERO |
851d5b22 BV |
56 | }; |
57 | ||
d87c1766 | 58 | static libusb_device_handle *lascar_open(struct libusb_device *dev) |
6aa1eb4e BV |
59 | { |
60 | libusb_device_handle *dev_hdl; | |
61 | int ret; | |
62 | ||
63 | if ((ret = libusb_open(dev, &dev_hdl)) != 0) { | |
64 | sr_dbg("failed to open device for scan: %s", | |
65 | libusb_error_name(ret)); | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | /* Some of these fail, but it needs doing -- some sort of mode | |
f3f19d11 | 70 | * setup for the SiLabs F32x. */ |
6aa1eb4e BV |
71 | libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, |
72 | 0x00, 0xffff, 0x00, NULL, 0, 50); | |
73 | libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, | |
74 | 0x02, 0x0002, 0x00, NULL, 0, 50); | |
75 | libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, | |
76 | 0x02, 0x0001, 0x00, NULL, 0, 50); | |
77 | ||
78 | return dev_hdl; | |
79 | } | |
80 | ||
55462b8b | 81 | static void LIBUSB_CALL mark_xfer(struct libusb_transfer *xfer) |
851d5b22 BV |
82 | { |
83 | ||
84 | xfer->user_data = GINT_TO_POINTER(1); | |
85 | ||
86 | } | |
87 | ||
361d1511 BV |
88 | SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl, |
89 | unsigned char *configblock, int *configlen) | |
851d5b22 BV |
90 | { |
91 | struct drv_context *drvc; | |
851d5b22 BV |
92 | struct libusb_transfer *xfer_in, *xfer_out; |
93 | struct timeval tv; | |
94 | int64_t start; | |
6aa1eb4e | 95 | int buflen; |
361d1511 BV |
96 | unsigned char cmd[3], buf[MAX_CONFIGBLOCK_SIZE]; |
97 | ||
98 | sr_spew("Reading config block."); | |
851d5b22 | 99 | |
41812aca | 100 | drvc = di->context; |
361d1511 | 101 | *configlen = 0; |
851d5b22 BV |
102 | |
103 | if (!(xfer_in = libusb_alloc_transfer(0)) || | |
104 | !(xfer_out = libusb_alloc_transfer(0))) | |
361d1511 | 105 | return SR_ERR; |
851d5b22 BV |
106 | |
107 | /* Flush anything the F321 still has queued. */ | |
108 | while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen, | |
109 | 5) == 0 && buflen > 0) | |
110 | ; | |
111 | ||
112 | /* Keep a read request waiting in the wings, ready to pounce | |
113 | * the moment the device sends something. */ | |
114 | libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN, | |
1a46cc62 | 115 | buf, 256, mark_xfer, 0, BULK_XFER_TIMEOUT); |
851d5b22 BV |
116 | if (libusb_submit_transfer(xfer_in) != 0) |
117 | goto cleanup; | |
118 | ||
119 | /* Request device configuration structure. */ | |
120 | cmd[0] = 0x00; | |
121 | cmd[1] = 0xff; | |
122 | cmd[2] = 0xff; | |
123 | libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, | |
6aa1eb4e | 124 | cmd, 3, mark_xfer, 0, 100); |
851d5b22 BV |
125 | if (libusb_submit_transfer(xfer_out) != 0) |
126 | goto cleanup; | |
127 | ||
128 | tv.tv_sec = 0; | |
129 | tv.tv_usec = 0; | |
130 | start = g_get_monotonic_time(); | |
131 | while (!xfer_in->user_data || !xfer_out->user_data) { | |
132 | if (g_get_monotonic_time() - start > SCAN_TIMEOUT) { | |
133 | start = 0; | |
134 | break; | |
135 | } | |
1a46cc62 | 136 | g_usleep(SLEEP_US_LONG); |
851d5b22 BV |
137 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
138 | } | |
139 | if (!start) { | |
140 | sr_dbg("no response"); | |
141 | goto cleanup; | |
142 | } | |
143 | if (xfer_in->actual_length != 3) { | |
144 | sr_dbg("expected 3-byte header, got %d bytes", xfer_in->actual_length); | |
145 | goto cleanup; | |
146 | } | |
147 | ||
148 | /* Got configuration structure header. */ | |
361d1511 | 149 | sr_spew("Response to config request: 0x%.2x 0x%.2x 0x%.2x ", |
851d5b22 BV |
150 | buf[0], buf[1], buf[2]); |
151 | buflen = buf[1] | (buf[2] << 8); | |
361d1511 | 152 | if (buf[0] != 0x02 || buflen > MAX_CONFIGBLOCK_SIZE) { |
851d5b22 BV |
153 | sr_dbg("Invalid response to config request: " |
154 | "0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]); | |
155 | libusb_close(dev_hdl); | |
156 | goto cleanup; | |
157 | } | |
158 | ||
159 | /* Get configuration structure. */ | |
160 | xfer_in->length = buflen; | |
161 | xfer_in->user_data = 0; | |
162 | if (libusb_submit_transfer(xfer_in) != 0) | |
163 | goto cleanup; | |
164 | while (!xfer_in->user_data) { | |
165 | if (g_get_monotonic_time() - start > SCAN_TIMEOUT) { | |
166 | start = 0; | |
167 | break; | |
168 | } | |
1a46cc62 | 169 | g_usleep(SLEEP_US_LONG); |
851d5b22 BV |
170 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
171 | } | |
172 | if (!start) { | |
173 | sr_dbg("Timeout waiting for configuration structure."); | |
174 | goto cleanup; | |
175 | } | |
176 | if (xfer_in->actual_length != buflen) { | |
177 | sr_dbg("expected %d-byte structure, got %d bytes", buflen, | |
178 | xfer_in->actual_length); | |
179 | goto cleanup; | |
180 | } | |
6aa1eb4e | 181 | |
361d1511 BV |
182 | memcpy(configblock, buf, buflen); |
183 | *configlen = buflen; | |
851d5b22 BV |
184 | |
185 | cleanup: | |
9851d35b | 186 | if (!xfer_in->user_data || !xfer_out->user_data) { |
851d5b22 BV |
187 | if (!xfer_in->user_data) |
188 | libusb_cancel_transfer(xfer_in); | |
189 | if (!xfer_out->user_data) | |
190 | libusb_cancel_transfer(xfer_out); | |
191 | start = g_get_monotonic_time(); | |
192 | while (!xfer_in->user_data || !xfer_out->user_data) { | |
1a46cc62 | 193 | if (g_get_monotonic_time() - start > EVENTS_TIMEOUT) |
851d5b22 | 194 | break; |
1a46cc62 | 195 | g_usleep(SLEEP_US_SHORT); |
851d5b22 BV |
196 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
197 | } | |
198 | } | |
199 | libusb_free_transfer(xfer_in); | |
200 | libusb_free_transfer(xfer_out); | |
201 | ||
361d1511 | 202 | return *configlen ? SR_OK : SR_ERR; |
6aa1eb4e BV |
203 | } |
204 | ||
d87c1766 | 205 | static int lascar_save_config(libusb_device_handle *dev_hdl, |
b0c95747 BV |
206 | unsigned char *config, int configlen) |
207 | { | |
208 | struct drv_context *drvc; | |
209 | struct libusb_transfer *xfer_in, *xfer_out; | |
210 | struct timeval tv; | |
211 | int64_t start; | |
212 | int buflen, ret; | |
213 | unsigned char cmd[3], buf[256]; | |
214 | ||
361d1511 BV |
215 | sr_spew("Writing config block."); |
216 | ||
41812aca | 217 | drvc = di->context; |
b0c95747 BV |
218 | |
219 | if (!(xfer_in = libusb_alloc_transfer(0)) || | |
220 | !(xfer_out = libusb_alloc_transfer(0))) | |
221 | return SR_ERR; | |
222 | ||
223 | /* Flush anything the F321 still has queued. */ | |
224 | while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen, | |
225 | 5) == 0 && buflen > 0) | |
226 | ; | |
227 | ret = SR_OK; | |
228 | ||
229 | /* Keep a read request waiting in the wings, ready to pounce | |
230 | * the moment the device sends something. */ | |
231 | libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN, | |
1a46cc62 | 232 | buf, 256, mark_xfer, 0, BULK_XFER_TIMEOUT); |
b0c95747 BV |
233 | if (libusb_submit_transfer(xfer_in) != 0) { |
234 | ret = SR_ERR; | |
235 | goto cleanup; | |
236 | } | |
237 | ||
238 | /* Request device configuration structure. */ | |
239 | cmd[0] = 0x01; | |
240 | cmd[1] = configlen & 0xff; | |
241 | cmd[2] = (configlen >> 8) & 0xff; | |
242 | libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, | |
243 | cmd, 3, mark_xfer, 0, 100); | |
244 | if (libusb_submit_transfer(xfer_out) != 0) { | |
245 | ret = SR_ERR; | |
246 | goto cleanup; | |
247 | } | |
248 | tv.tv_sec = 0; | |
249 | tv.tv_usec = 0; | |
250 | while (!xfer_out->user_data) { | |
1a46cc62 | 251 | g_usleep(SLEEP_US_LONG); |
b0c95747 BV |
252 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
253 | } | |
254 | ||
255 | libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, | |
256 | config, configlen, mark_xfer, 0, 100); | |
257 | if (libusb_submit_transfer(xfer_out) != 0) { | |
258 | ret = SR_ERR; | |
259 | goto cleanup; | |
260 | } | |
261 | while (!xfer_in->user_data || !xfer_out->user_data) { | |
1a46cc62 | 262 | g_usleep(SLEEP_US_LONG); |
b0c95747 BV |
263 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
264 | } | |
265 | ||
266 | if (xfer_in->actual_length != 1 || buf[0] != 0xff) { | |
267 | sr_dbg("unexpected response after transfer"); | |
268 | ret = SR_ERR; | |
269 | } | |
270 | ||
271 | cleanup: | |
9851d35b | 272 | if (!xfer_in->user_data || !xfer_out->user_data) { |
b0c95747 BV |
273 | if (!xfer_in->user_data) |
274 | libusb_cancel_transfer(xfer_in); | |
275 | if (!xfer_out->user_data) | |
276 | libusb_cancel_transfer(xfer_out); | |
277 | start = g_get_monotonic_time(); | |
278 | while (!xfer_in->user_data || !xfer_out->user_data) { | |
1a46cc62 | 279 | if (g_get_monotonic_time() - start > EVENTS_TIMEOUT) |
b0c95747 | 280 | break; |
1a46cc62 | 281 | g_usleep(SLEEP_US_SHORT); |
b0c95747 BV |
282 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
283 | } | |
284 | } | |
285 | libusb_free_transfer(xfer_in); | |
286 | libusb_free_transfer(xfer_out); | |
287 | ||
288 | return ret; | |
289 | } | |
290 | ||
6aa1eb4e BV |
291 | static struct sr_dev_inst *lascar_identify(unsigned char *config) |
292 | { | |
293 | struct dev_context *devc; | |
294 | const struct elusb_profile *profile; | |
295 | struct sr_dev_inst *sdi; | |
6aa1eb4e BV |
296 | int modelid, i; |
297 | char firmware[5]; | |
298 | ||
299 | modelid = config[0]; | |
851d5b22 | 300 | sdi = NULL; |
851d5b22 | 301 | |
3782e571 UH |
302 | if (!modelid) |
303 | return sdi; | |
851d5b22 | 304 | |
3782e571 UH |
305 | profile = NULL; |
306 | for (i = 0; profiles[i].modelid; i++) { | |
307 | if (profiles[i].modelid == modelid) { | |
308 | profile = &profiles[i]; | |
309 | break; | |
851d5b22 | 310 | } |
3782e571 UH |
311 | } |
312 | if (!profile) { | |
313 | sr_dbg("unknown EL-USB modelid %d", modelid); | |
314 | return NULL; | |
315 | } | |
851d5b22 | 316 | |
3782e571 UH |
317 | i = config[52] | (config[53] << 8); |
318 | memcpy(firmware, config + 0x30, 4); | |
319 | firmware[4] = '\0'; | |
320 | sr_dbg("found %s with firmware version %s serial %d", | |
321 | profile->modelname, firmware, i); | |
322 | ||
323 | if (profile->logformat == LOG_UNSUPPORTED) { | |
324 | sr_dbg("unsupported EL-USB logformat for %s", profile->modelname); | |
325 | return NULL; | |
326 | } | |
6aa1eb4e | 327 | |
3782e571 UH |
328 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
329 | sdi->status = SR_ST_INACTIVE; | |
330 | sdi->vendor = g_strdup("Lascar"); | |
331 | sdi->model = g_strdup(profile->modelname); | |
332 | sdi->version = g_strdup(firmware); | |
333 | ||
334 | if (profile->logformat == LOG_TEMP_RH) { | |
335 | /* Model this as two channels: temperature and humidity. */ | |
336 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temp"); | |
337 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Hum"); | |
338 | } else if (profile->logformat == LOG_CO) { | |
339 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CO"); | |
340 | } else { | |
341 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
851d5b22 BV |
342 | } |
343 | ||
3782e571 UH |
344 | devc = g_malloc0(sizeof(struct dev_context)); |
345 | sdi->priv = devc; | |
346 | devc->profile = profile; | |
347 | ||
851d5b22 BV |
348 | return sdi; |
349 | } | |
350 | ||
351 | SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address) | |
352 | { | |
353 | struct drv_context *drvc; | |
354 | struct sr_dev_inst *sdi; | |
355 | struct libusb_device **devlist; | |
851d5b22 | 356 | libusb_device_handle *dev_hdl; |
8de8551b | 357 | int dummy, i; |
ceb2da17 | 358 | int ret; |
361d1511 | 359 | unsigned char config[MAX_CONFIGBLOCK_SIZE]; |
851d5b22 | 360 | |
41812aca | 361 | drvc = di->context; |
851d5b22 BV |
362 | sdi = NULL; |
363 | ||
ceb2da17 LPC |
364 | ret = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
365 | if (ret < 0) | |
366 | return NULL; | |
367 | ||
851d5b22 | 368 | for (i = 0; devlist[i]; i++) { |
851d5b22 BV |
369 | if (libusb_get_bus_number(devlist[i]) != bus || |
370 | libusb_get_device_address(devlist[i]) != address) | |
371 | continue; | |
372 | ||
6aa1eb4e BV |
373 | if (!(dev_hdl = lascar_open(devlist[i]))) |
374 | continue; | |
375 | ||
361d1511 | 376 | if (lascar_get_config(dev_hdl, config, &dummy) != SR_OK) |
851d5b22 | 377 | continue; |
851d5b22 | 378 | |
851d5b22 | 379 | libusb_close(dev_hdl); |
6aa1eb4e | 380 | sdi = lascar_identify(config); |
851d5b22 | 381 | } |
ceb2da17 | 382 | libusb_free_device_list(devlist, 1); |
851d5b22 BV |
383 | |
384 | return sdi; | |
385 | } | |
386 | ||
6aa1eb4e BV |
387 | static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf, |
388 | int buflen) | |
389 | { | |
390 | struct dev_context *devc; | |
391 | struct sr_datafeed_packet packet; | |
5ba41759 UH |
392 | struct sr_datafeed_analog analog; |
393 | struct sr_analog_encoding encoding; | |
394 | struct sr_analog_meaning meaning; | |
395 | struct sr_analog_spec spec; | |
ba7dd8bb | 396 | struct sr_channel *ch; |
6787f404 | 397 | float *temp, *rh; |
6aa1eb4e | 398 | uint16_t s; |
6787f404 | 399 | int samples, samples_left, i, j; |
6aa1eb4e BV |
400 | |
401 | devc = sdi->priv; | |
69e19dd7 | 402 | |
869c8375 | 403 | /* Note: digits/spec_digits will be overridden later. */ |
5ba41759 UH |
404 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
405 | ||
6aa1eb4e BV |
406 | samples = buflen / devc->sample_size; |
407 | samples_left = devc->logged_samples - devc->rcvd_samples; | |
408 | if (samples_left < samples) | |
409 | samples = samples_left; | |
410 | switch (devc->profile->logformat) { | |
411 | case LOG_TEMP_RH: | |
5ba41759 | 412 | packet.type = SR_DF_ANALOG; |
6787f404 | 413 | packet.payload = &analog; |
5ba41759 | 414 | analog.meaning->mqflags = 0; |
b65adced GS |
415 | temp = g_try_malloc(sizeof(float) * samples); |
416 | rh = g_try_malloc(sizeof(float) * samples); | |
417 | if (!temp || !rh) { | |
418 | g_free(temp); | |
419 | g_free(rh); | |
6787f404 | 420 | break; |
b65adced | 421 | } |
6787f404 | 422 | for (i = 0, j = 0; i < samples; i++) { |
f3f19d11 | 423 | /* Both Celsius and Fahrenheit stored at base -40. */ |
6787f404 | 424 | if (devc->temp_unit == 0) |
f3f19d11 | 425 | /* Celsius is stored in half-degree increments. */ |
6787f404 BV |
426 | temp[j] = buf[i * 2] / 2 - 40; |
427 | else | |
428 | temp[j] = buf[i * 2] - 40; | |
429 | ||
430 | rh[j] = buf[i * 2 + 1] / 2; | |
431 | ||
432 | if (temp[j] == 0.0 && rh[j] == 0.0) | |
433 | /* Skip invalid measurement. */ | |
434 | continue; | |
435 | j++; | |
436 | } | |
437 | analog.num_samples = j; | |
438 | ||
ba7dd8bb UH |
439 | ch = sdi->channels->data; |
440 | if (ch->enabled) { | |
5ba41759 UH |
441 | analog.meaning->channels = g_slist_append(NULL, ch); |
442 | analog.meaning->mq = SR_MQ_TEMPERATURE; | |
fa2b1922 | 443 | if (devc->temp_unit == 1) { |
5ba41759 | 444 | analog.meaning->unit = SR_UNIT_FAHRENHEIT; |
fa2b1922 AJ |
445 | analog.encoding->digits = 0; |
446 | analog.spec->spec_digits = 0; | |
447 | } else { | |
5ba41759 | 448 | analog.meaning->unit = SR_UNIT_CELSIUS; |
fa2b1922 AJ |
449 | analog.encoding->digits = 1; |
450 | analog.spec->spec_digits = 1; | |
451 | } | |
5ba41759 | 452 | analog.data = (void *)temp; |
695dc859 | 453 | sr_session_send(sdi, &packet); |
5ba41759 | 454 | g_slist_free(analog.meaning->channels); |
1d166757 BV |
455 | } |
456 | ||
ba7dd8bb UH |
457 | ch = sdi->channels->next->data; |
458 | if (ch->enabled) { | |
5ba41759 UH |
459 | analog.meaning->channels = g_slist_append(NULL, ch); |
460 | analog.meaning->mq = SR_MQ_RELATIVE_HUMIDITY; | |
461 | analog.meaning->unit = SR_UNIT_PERCENTAGE; | |
fa2b1922 AJ |
462 | analog.encoding->digits = 1; |
463 | analog.spec->spec_digits = 1; | |
5ba41759 | 464 | analog.data = (void *)rh; |
695dc859 | 465 | sr_session_send(sdi, &packet); |
5ba41759 | 466 | g_slist_free(analog.meaning->channels); |
1d166757 BV |
467 | } |
468 | ||
6787f404 BV |
469 | g_free(temp); |
470 | g_free(rh); | |
6aa1eb4e BV |
471 | break; |
472 | case LOG_CO: | |
5ba41759 | 473 | packet.type = SR_DF_ANALOG; |
6aa1eb4e | 474 | packet.payload = &analog; |
5ba41759 | 475 | analog.meaning->channels = sdi->channels; |
6aa1eb4e | 476 | analog.num_samples = samples; |
5ba41759 UH |
477 | analog.meaning->mq = SR_MQ_CARBON_MONOXIDE; |
478 | analog.meaning->unit = SR_UNIT_CONCENTRATION; | |
479 | analog.meaning->mqflags = 0; | |
6aa1eb4e BV |
480 | if (!(analog.data = g_try_malloc(sizeof(float) * samples))) |
481 | break; | |
482 | for (i = 0; i < samples; i++) { | |
483 | s = (buf[i * 2] << 8) | buf[i * 2 + 1]; | |
5ba41759 UH |
484 | ((float *)analog.data)[i] = (s * devc->co_high + devc->co_low) / (1000 * 1000); |
485 | if (((float *)analog.data)[i] < 0.0) | |
486 | ((float *)analog.data)[i] = 0.0; | |
6aa1eb4e | 487 | } |
695dc859 | 488 | sr_session_send(sdi, &packet); |
6aa1eb4e BV |
489 | g_free(analog.data); |
490 | break; | |
491 | default: | |
492 | /* How did we even get this far? */ | |
493 | break; | |
494 | } | |
495 | devc->rcvd_samples += samples; | |
496 | ||
497 | } | |
851d5b22 | 498 | |
6aa1eb4e | 499 | SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data) |
46697e38 | 500 | { |
41812aca | 501 | struct drv_context *drvc = di->context; |
6aa1eb4e BV |
502 | struct sr_dev_inst *sdi; |
503 | struct timeval tv; | |
46697e38 | 504 | |
6aa1eb4e BV |
505 | (void)fd; |
506 | (void)revents; | |
46697e38 | 507 | |
6aa1eb4e | 508 | sdi = cb_data; |
46697e38 | 509 | |
6aa1eb4e | 510 | if (sdi->status == SR_ST_STOPPING) { |
102f1239 | 511 | usb_source_remove(sdi->session, drvc->sr_ctx); |
bee2b016 | 512 | std_session_send_df_end(sdi); |
46697e38 BV |
513 | } |
514 | ||
6aa1eb4e BV |
515 | memset(&tv, 0, sizeof(struct timeval)); |
516 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
517 | NULL); | |
518 | ||
46697e38 BV |
519 | return TRUE; |
520 | } | |
6aa1eb4e | 521 | |
55462b8b | 522 | SR_PRIV void LIBUSB_CALL lascar_el_usb_receive_transfer(struct libusb_transfer *transfer) |
6aa1eb4e BV |
523 | { |
524 | struct dev_context *devc; | |
525 | struct sr_dev_inst *sdi; | |
526 | int ret; | |
527 | gboolean packet_has_error; | |
528 | ||
529 | sdi = transfer->user_data; | |
530 | devc = sdi->priv; | |
531 | ||
532 | packet_has_error = FALSE; | |
533 | switch (transfer->status) { | |
534 | case LIBUSB_TRANSFER_NO_DEVICE: | |
535 | /* USB device was unplugged. */ | |
d2f7c417 | 536 | sr_dev_acquisition_stop(sdi); |
6aa1eb4e BV |
537 | return; |
538 | case LIBUSB_TRANSFER_COMPLETED: | |
539 | case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */ | |
540 | break; | |
541 | default: | |
542 | packet_has_error = TRUE; | |
543 | break; | |
544 | } | |
545 | ||
546 | if (!packet_has_error) { | |
547 | if (devc->rcvd_samples < devc->logged_samples) | |
548 | lascar_el_usb_dispatch(sdi, transfer->buffer, | |
549 | transfer->actual_length); | |
550 | devc->rcvd_bytes += transfer->actual_length; | |
551 | sr_spew("received %d/%d bytes (%d/%d samples)", | |
552 | devc->rcvd_bytes, devc->log_size, | |
553 | devc->rcvd_samples, devc->logged_samples); | |
554 | if (devc->rcvd_bytes >= devc->log_size) | |
d2f7c417 | 555 | sr_dev_acquisition_stop(sdi); |
6aa1eb4e BV |
556 | } |
557 | ||
558 | if (sdi->status == SR_ST_ACTIVE) { | |
559 | /* Send the same request again. */ | |
560 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
561 | sr_err("Unable to resubmit transfer: %s.", | |
562 | libusb_error_name(ret)); | |
563 | g_free(transfer->buffer); | |
564 | libusb_free_transfer(transfer); | |
d2f7c417 | 565 | sr_dev_acquisition_stop(sdi); |
6aa1eb4e BV |
566 | } |
567 | } else { | |
568 | /* This was the last transfer we're going to receive, so | |
569 | * clean up now. */ | |
570 | g_free(transfer->buffer); | |
571 | libusb_free_transfer(transfer); | |
572 | } | |
573 | ||
574 | } | |
361d1511 BV |
575 | |
576 | static int get_flags(unsigned char *configblock) | |
577 | { | |
578 | int flags; | |
579 | ||
580 | flags = (configblock[32] | (configblock[33] << 8)) & 0x1fff; | |
581 | sr_spew("Read flags (0x%.4x).", flags); | |
582 | ||
583 | return flags; | |
584 | } | |
585 | ||
586 | static int set_flags(unsigned char *configblock, int flags) | |
587 | { | |
588 | ||
589 | sr_spew("Setting flags to 0x%.4x.", flags); | |
590 | configblock[32] = flags & 0xff; | |
591 | configblock[33] = (flags >> 8) & 0x1f; | |
592 | ||
593 | return flags; | |
594 | } | |
595 | ||
596 | SR_PRIV int lascar_is_logging(const struct sr_dev_inst *sdi) | |
597 | { | |
598 | struct dev_context *devc; | |
0f150649 | 599 | struct sr_usb_dev_inst *usb; |
361d1511 BV |
600 | int dummy, flags, ret; |
601 | ||
602 | devc = sdi->priv; | |
0f150649 BV |
603 | usb = sdi->conn; |
604 | ||
605 | if (lascar_get_config(usb->devhdl, devc->config, &dummy) != SR_OK) | |
361d1511 BV |
606 | return -1; |
607 | ||
608 | flags = get_flags(devc->config); | |
609 | if (flags & 0x0100) | |
610 | ret = 1; | |
611 | else | |
612 | ret = 0; | |
613 | ||
614 | return ret; | |
615 | } | |
616 | ||
617 | SR_PRIV int lascar_start_logging(const struct sr_dev_inst *sdi) | |
618 | { | |
619 | struct dev_context *devc; | |
0f150649 | 620 | struct sr_usb_dev_inst *usb; |
361d1511 BV |
621 | int len, flags, ret; |
622 | ||
623 | devc = sdi->priv; | |
0f150649 BV |
624 | usb = sdi->conn; |
625 | ||
626 | if (lascar_get_config(usb->devhdl, devc->config, &len) != SR_OK) | |
361d1511 BV |
627 | return SR_ERR; |
628 | ||
629 | /* Turn on logging. */ | |
630 | flags = get_flags(devc->config); | |
631 | flags |= 0x0100; | |
632 | set_flags(devc->config, flags); | |
633 | ||
634 | /* Start logging in 0 seconds. */ | |
635 | memset(devc->config + 24, 0, 4); | |
636 | ||
0f150649 | 637 | ret = lascar_save_config(usb->devhdl, devc->config, len); |
361d1511 BV |
638 | sr_info("Started internal logging."); |
639 | ||
640 | return ret; | |
641 | } | |
642 | ||
643 | SR_PRIV int lascar_stop_logging(const struct sr_dev_inst *sdi) | |
644 | { | |
645 | struct dev_context *devc; | |
0f150649 | 646 | struct sr_usb_dev_inst *usb; |
361d1511 BV |
647 | int len, flags, ret; |
648 | ||
649 | devc = sdi->priv; | |
0f150649 BV |
650 | usb = sdi->conn; |
651 | ||
652 | if (lascar_get_config(usb->devhdl, devc->config, &len) != SR_OK) | |
361d1511 BV |
653 | return SR_ERR; |
654 | ||
655 | flags = get_flags(devc->config); | |
656 | flags &= ~0x0100; | |
657 | set_flags(devc->config, flags); | |
658 | ||
0f150649 | 659 | ret = lascar_save_config(usb->devhdl, devc->config, len); |
361d1511 BV |
660 | sr_info("Stopped internal logging."); |
661 | ||
662 | return ret; | |
663 | } |