]>
Commit | Line | Data |
---|---|---|
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> | |
21 | #include <sys/time.h> | |
22 | #include <string.h> | |
23 | #include <math.h> | |
24 | #include <glib.h> | |
25 | #include "libsigrok.h" | |
26 | #include "libsigrok-internal.h" | |
27 | #include "protocol.h" | |
28 | ||
29 | extern struct sr_dev_driver lascar_el_usb_driver_info; | |
30 | struct sr_dev_driver *di = &lascar_el_usb_driver_info; | |
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 }, | |
43 | { 11, "EL-USB-CO300", LOG_CO }, | |
44 | { 12, "EL-USB-2-LCD", LOG_TEMP_RH }, | |
45 | { 13, "EL-USB-2+", LOG_TEMP_RH }, | |
46 | { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED }, | |
47 | { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED }, | |
48 | { 16, "EL-USB-2-LCD+", LOG_TEMP_RH }, | |
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 | ||
57 | static libusb_device_handle *lascar_open(struct libusb_device *dev) | |
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 | |
69 | * setup for the SiLabs F32x. */ | |
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 | ||
80 | static void LIBUSB_CALL mark_xfer(struct libusb_transfer *xfer) | |
81 | { | |
82 | ||
83 | xfer->user_data = GINT_TO_POINTER(1); | |
84 | ||
85 | } | |
86 | ||
87 | SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl, | |
88 | unsigned char *configblock, int *configlen) | |
89 | { | |
90 | struct drv_context *drvc; | |
91 | struct libusb_transfer *xfer_in, *xfer_out; | |
92 | struct timeval tv; | |
93 | int64_t start; | |
94 | int buflen; | |
95 | unsigned char cmd[3], buf[MAX_CONFIGBLOCK_SIZE]; | |
96 | ||
97 | sr_spew("Reading config block."); | |
98 | ||
99 | drvc = di->context; | |
100 | *configlen = 0; | |
101 | ||
102 | if (!(xfer_in = libusb_alloc_transfer(0)) || | |
103 | !(xfer_out = libusb_alloc_transfer(0))) | |
104 | return SR_ERR; | |
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, | |
114 | buf, 256, mark_xfer, 0, BULK_XFER_TIMEOUT); | |
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, | |
123 | cmd, 3, mark_xfer, 0, 100); | |
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 | } | |
135 | g_usleep(SLEEP_US_LONG); | |
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. */ | |
148 | sr_spew("Response to config request: 0x%.2x 0x%.2x 0x%.2x ", | |
149 | buf[0], buf[1], buf[2]); | |
150 | buflen = buf[1] | (buf[2] << 8); | |
151 | if (buf[0] != 0x02 || buflen > MAX_CONFIGBLOCK_SIZE) { | |
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 | } | |
168 | g_usleep(SLEEP_US_LONG); | |
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 | } | |
180 | ||
181 | memcpy(configblock, buf, buflen); | |
182 | *configlen = buflen; | |
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) { | |
192 | if (g_get_monotonic_time() - start > EVENTS_TIMEOUT) | |
193 | break; | |
194 | g_usleep(SLEEP_US_SHORT); | |
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 | ||
201 | return *configlen ? SR_OK : SR_ERR; | |
202 | } | |
203 | ||
204 | static int lascar_save_config(libusb_device_handle *dev_hdl, | |
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 | ||
214 | sr_spew("Writing config block."); | |
215 | ||
216 | drvc = di->context; | |
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, | |
231 | buf, 256, mark_xfer, 0, BULK_XFER_TIMEOUT); | |
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) { | |
250 | g_usleep(SLEEP_US_LONG); | |
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) { | |
261 | g_usleep(SLEEP_US_LONG); | |
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) { | |
278 | if (g_get_monotonic_time() - start > EVENTS_TIMEOUT) | |
279 | break; | |
280 | g_usleep(SLEEP_US_SHORT); | |
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 | ||
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; | |
295 | int modelid, i; | |
296 | char firmware[5]; | |
297 | ||
298 | modelid = config[0]; | |
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 | ||
313 | i = config[52] | (config[53] << 8); | |
314 | memcpy(firmware, config + 0x30, 4); | |
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 | ||
324 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
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); | |
329 | sdi->driver = di; | |
330 | ||
331 | if (profile->logformat == LOG_TEMP_RH) { | |
332 | /* Model this as two channels: temperature and humidity. */ | |
333 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temp"); | |
334 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Hum"); | |
335 | } else if (profile->logformat == LOG_CO) { | |
336 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CO"); | |
337 | } else { | |
338 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
339 | } | |
340 | ||
341 | devc = g_malloc0(sizeof(struct dev_context)); | |
342 | sdi->priv = devc; | |
343 | devc->profile = profile; | |
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; | |
356 | int dummy, ret, i; | |
357 | unsigned char config[MAX_CONFIGBLOCK_SIZE]; | |
358 | ||
359 | drvc = di->context; | |
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 | ||
373 | if (!(dev_hdl = lascar_open(devlist[i]))) | |
374 | continue; | |
375 | ||
376 | if (lascar_get_config(dev_hdl, config, &dummy) != SR_OK) | |
377 | continue; | |
378 | ||
379 | libusb_close(dev_hdl); | |
380 | sdi = lascar_identify(config); | |
381 | } | |
382 | ||
383 | return sdi; | |
384 | } | |
385 | ||
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; | |
392 | struct sr_channel *ch; | |
393 | float *temp, *rh; | |
394 | uint16_t s; | |
395 | int samples, samples_left, i, j; | |
396 | ||
397 | devc = sdi->priv; | |
398 | ||
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: | |
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++) { | |
413 | /* Both Celsius and Fahrenheit stored at base -40. */ | |
414 | if (devc->temp_unit == 0) | |
415 | /* Celsius is stored in half-degree increments. */ | |
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 | ||
429 | ch = sdi->channels->data; | |
430 | if (ch->enabled) { | |
431 | analog.channels = g_slist_append(NULL, ch); | |
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); | |
439 | } | |
440 | ||
441 | ch = sdi->channels->next->data; | |
442 | if (ch->enabled) { | |
443 | analog.channels = g_slist_append(NULL, ch); | |
444 | analog.mq = SR_MQ_RELATIVE_HUMIDITY; | |
445 | analog.unit = SR_UNIT_PERCENTAGE; | |
446 | analog.data = rh; | |
447 | sr_session_send(devc->cb_data, &packet); | |
448 | } | |
449 | ||
450 | g_free(temp); | |
451 | g_free(rh); | |
452 | break; | |
453 | case LOG_CO: | |
454 | packet.type = SR_DF_ANALOG; | |
455 | packet.payload = &analog; | |
456 | analog.channels = sdi->channels; | |
457 | analog.num_samples = samples; | |
458 | analog.mq = SR_MQ_CARBON_MONOXIDE; | |
459 | analog.unit = SR_UNIT_CONCENTRATION; | |
460 | analog.mqflags = 0; | |
461 | if (!(analog.data = g_try_malloc(sizeof(float) * samples))) | |
462 | break; | |
463 | for (i = 0; i < samples; i++) { | |
464 | s = (buf[i * 2] << 8) | buf[i * 2 + 1]; | |
465 | analog.data[i] = (s * devc->co_high + devc->co_low) / (1000 * 1000); | |
466 | if (analog.data[i] < 0.0) | |
467 | analog.data[i] = 0.0; | |
468 | } | |
469 | sr_session_send(devc->cb_data, &packet); | |
470 | g_free(analog.data); | |
471 | break; | |
472 | default: | |
473 | /* How did we even get this far? */ | |
474 | break; | |
475 | } | |
476 | devc->rcvd_samples += samples; | |
477 | ||
478 | } | |
479 | ||
480 | SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data) | |
481 | { | |
482 | struct drv_context *drvc = di->context; | |
483 | struct sr_datafeed_packet packet; | |
484 | struct sr_dev_inst *sdi; | |
485 | struct timeval tv; | |
486 | ||
487 | (void)fd; | |
488 | (void)revents; | |
489 | ||
490 | sdi = cb_data; | |
491 | ||
492 | if (sdi->status == SR_ST_STOPPING) { | |
493 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
494 | ||
495 | packet.type = SR_DF_END; | |
496 | sr_session_send(cb_data, &packet); | |
497 | } | |
498 | ||
499 | memset(&tv, 0, sizeof(struct timeval)); | |
500 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
501 | NULL); | |
502 | ||
503 | return TRUE; | |
504 | } | |
505 | ||
506 | SR_PRIV void LIBUSB_CALL lascar_el_usb_receive_transfer(struct libusb_transfer *transfer) | |
507 | { | |
508 | struct dev_context *devc; | |
509 | struct sr_dev_inst *sdi; | |
510 | int ret; | |
511 | gboolean packet_has_error; | |
512 | ||
513 | sdi = transfer->user_data; | |
514 | devc = sdi->priv; | |
515 | ||
516 | packet_has_error = FALSE; | |
517 | switch (transfer->status) { | |
518 | case LIBUSB_TRANSFER_NO_DEVICE: | |
519 | /* USB device was unplugged. */ | |
520 | dev_acquisition_stop(sdi, sdi); | |
521 | return; | |
522 | case LIBUSB_TRANSFER_COMPLETED: | |
523 | case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */ | |
524 | break; | |
525 | default: | |
526 | packet_has_error = TRUE; | |
527 | break; | |
528 | } | |
529 | ||
530 | if (!packet_has_error) { | |
531 | if (devc->rcvd_samples < devc->logged_samples) | |
532 | lascar_el_usb_dispatch(sdi, transfer->buffer, | |
533 | transfer->actual_length); | |
534 | devc->rcvd_bytes += transfer->actual_length; | |
535 | sr_spew("received %d/%d bytes (%d/%d samples)", | |
536 | devc->rcvd_bytes, devc->log_size, | |
537 | devc->rcvd_samples, devc->logged_samples); | |
538 | if (devc->rcvd_bytes >= devc->log_size) | |
539 | dev_acquisition_stop(sdi, sdi); | |
540 | } | |
541 | ||
542 | if (sdi->status == SR_ST_ACTIVE) { | |
543 | /* Send the same request again. */ | |
544 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
545 | sr_err("Unable to resubmit transfer: %s.", | |
546 | libusb_error_name(ret)); | |
547 | g_free(transfer->buffer); | |
548 | libusb_free_transfer(transfer); | |
549 | dev_acquisition_stop(sdi, sdi); | |
550 | } | |
551 | } else { | |
552 | /* This was the last transfer we're going to receive, so | |
553 | * clean up now. */ | |
554 | g_free(transfer->buffer); | |
555 | libusb_free_transfer(transfer); | |
556 | } | |
557 | ||
558 | } | |
559 | ||
560 | static int get_flags(unsigned char *configblock) | |
561 | { | |
562 | int flags; | |
563 | ||
564 | flags = (configblock[32] | (configblock[33] << 8)) & 0x1fff; | |
565 | sr_spew("Read flags (0x%.4x).", flags); | |
566 | ||
567 | return flags; | |
568 | } | |
569 | ||
570 | static int set_flags(unsigned char *configblock, int flags) | |
571 | { | |
572 | ||
573 | sr_spew("Setting flags to 0x%.4x.", flags); | |
574 | configblock[32] = flags & 0xff; | |
575 | configblock[33] = (flags >> 8) & 0x1f; | |
576 | ||
577 | return flags; | |
578 | } | |
579 | ||
580 | SR_PRIV int lascar_is_logging(const struct sr_dev_inst *sdi) | |
581 | { | |
582 | struct dev_context *devc; | |
583 | struct sr_usb_dev_inst *usb; | |
584 | int dummy, flags, ret; | |
585 | ||
586 | devc = sdi->priv; | |
587 | usb = sdi->conn; | |
588 | ||
589 | if (lascar_get_config(usb->devhdl, devc->config, &dummy) != SR_OK) | |
590 | return -1; | |
591 | ||
592 | flags = get_flags(devc->config); | |
593 | if (flags & 0x0100) | |
594 | ret = 1; | |
595 | else | |
596 | ret = 0; | |
597 | ||
598 | return ret; | |
599 | } | |
600 | ||
601 | SR_PRIV int lascar_start_logging(const struct sr_dev_inst *sdi) | |
602 | { | |
603 | struct dev_context *devc; | |
604 | struct sr_usb_dev_inst *usb; | |
605 | int len, flags, ret; | |
606 | ||
607 | devc = sdi->priv; | |
608 | usb = sdi->conn; | |
609 | ||
610 | if (lascar_get_config(usb->devhdl, devc->config, &len) != SR_OK) | |
611 | return SR_ERR; | |
612 | ||
613 | /* Turn on logging. */ | |
614 | flags = get_flags(devc->config); | |
615 | flags |= 0x0100; | |
616 | set_flags(devc->config, flags); | |
617 | ||
618 | /* Start logging in 0 seconds. */ | |
619 | memset(devc->config + 24, 0, 4); | |
620 | ||
621 | ret = lascar_save_config(usb->devhdl, devc->config, len); | |
622 | sr_info("Started internal logging."); | |
623 | ||
624 | return ret; | |
625 | } | |
626 | ||
627 | SR_PRIV int lascar_stop_logging(const struct sr_dev_inst *sdi) | |
628 | { | |
629 | struct dev_context *devc; | |
630 | struct sr_usb_dev_inst *usb; | |
631 | int len, flags, ret; | |
632 | ||
633 | devc = sdi->priv; | |
634 | usb = sdi->conn; | |
635 | ||
636 | if (lascar_get_config(usb->devhdl, devc->config, &len) != SR_OK) | |
637 | return SR_ERR; | |
638 | ||
639 | flags = get_flags(devc->config); | |
640 | flags &= ~0x0100; | |
641 | set_flags(devc->config, flags); | |
642 | ||
643 | ret = lascar_save_config(usb->devhdl, devc->config, len); | |
644 | sr_info("Stopped internal logging."); | |
645 | ||
646 | return ret; | |
647 | } |