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