]> sigrok.org Git - libsigrok.git/blame - hardware/lascar-el-usb/protocol.c
hantek-dso: Consistently return SR_ERR_ARG if sdi is needed
[libsigrok.git] / hardware / lascar-el-usb / protocol.c
CommitLineData
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
BV
24#include <glib.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27#include "protocol.h"
28
851d5b22
BV
29extern struct sr_dev_driver lascar_el_usb_driver_info;
30static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
31
32static 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
57
6aa1eb4e
BV
58SR_PRIV libusb_device_handle *lascar_open(struct libusb_device *dev)
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
70 * setup for the SILabs F32x. */
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
81static void mark_xfer(struct libusb_transfer *xfer)
851d5b22
BV
82{
83
84 xfer->user_data = GINT_TO_POINTER(1);
85
86}
87
361d1511
BV
88SR_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
BV
99
100 drvc = di->priv;
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,
6aa1eb4e 115 buf, 256, mark_xfer, 0, 10000);
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 }
136 g_usleep(5000);
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 }
169 g_usleep(5000);
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
185cleanup:
186 if (!xfer_in->user_data || !xfer_in->user_data) {
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) {
193 if (g_get_monotonic_time() - start > 10000)
194 break;
195 g_usleep(1000);
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
b0c95747
BV
205SR_PRIV int lascar_save_config(libusb_device_handle *dev_hdl,
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
b0c95747
BV
217 drvc = di->priv;
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,
232 buf, 256, mark_xfer, 0, 10000);
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) {
251 g_usleep(5000);
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) {
262 g_usleep(5000);
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
271cleanup:
272 if (!xfer_in->user_data || !xfer_in->user_data) {
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) {
279 if (g_get_monotonic_time() - start > 10000)
280 break;
281 g_usleep(1000);
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
291static 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;
296 struct sr_probe *probe;
297 int modelid, i;
298 char firmware[5];
299
300 modelid = config[0];
851d5b22
BV
301 sdi = NULL;
302 if (modelid) {
303 profile = NULL;
304 for (i = 0; profiles[i].modelid; i++) {
305 if (profiles[i].modelid == modelid) {
306 profile = &profiles[i];
307 break;
308 }
309 }
310 if (!profile) {
311 sr_dbg("unknown EL-USB modelid %d", modelid);
312 return NULL;
313 }
314
6aa1eb4e
BV
315 i = config[52] | (config[53] << 8);
316 memcpy(firmware, config + 0x30, 4);
851d5b22
BV
317 firmware[4] = '\0';
318 sr_dbg("found %s with firmware version %s serial %d",
319 profile->modelname, firmware, i);
320
321 if (profile->logformat == LOG_UNSUPPORTED) {
322 sr_dbg("unsupported EL-USB logformat for %s", profile->modelname);
323 return NULL;
324 }
325
326 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, LASCAR_VENDOR,
327 profile->modelname, firmware)))
328 return NULL;
329 sdi->driver = di;
6aa1eb4e 330
1d166757
BV
331 if (profile->logformat == LOG_TEMP_RH) {
332 /* Model this as two probes: temperature and humidity. */
333 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Temp")))
334 return NULL;
335 sdi->probes = g_slist_append(NULL, probe);
336 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Hum")))
337 return NULL;
338 sdi->probes = g_slist_append(sdi->probes, probe);
339 } else if (profile->logformat == LOG_CO) {
340 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "CO")))
341 return NULL;
342 sdi->probes = g_slist_append(NULL, probe);
343 } else {
344 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
345 return NULL;
346 sdi->probes = g_slist_append(NULL, probe);
347 }
6aa1eb4e
BV
348
349 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
350 return NULL;
351 sdi->priv = devc;
352 devc->profile = profile;
851d5b22
BV
353 }
354
355 return sdi;
356}
357
358SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
359{
360 struct drv_context *drvc;
361 struct sr_dev_inst *sdi;
362 struct libusb_device **devlist;
363 struct libusb_device_descriptor des;
364 libusb_device_handle *dev_hdl;
361d1511
BV
365 int dummy, ret, i;
366 unsigned char config[MAX_CONFIGBLOCK_SIZE];
851d5b22
BV
367
368 drvc = di->priv;
369 sdi = NULL;
370
371 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
372 for (i = 0; devlist[i]; i++) {
373 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
374 sr_err("Failed to get device descriptor: %d.", ret);
375 continue;
376 }
377
378 if (libusb_get_bus_number(devlist[i]) != bus ||
379 libusb_get_device_address(devlist[i]) != address)
380 continue;
381
6aa1eb4e
BV
382 if (!(dev_hdl = lascar_open(devlist[i])))
383 continue;
384
361d1511 385 if (lascar_get_config(dev_hdl, config, &dummy) != SR_OK)
851d5b22 386 continue;
851d5b22 387
851d5b22 388 libusb_close(dev_hdl);
6aa1eb4e 389 sdi = lascar_identify(config);
851d5b22
BV
390 }
391
392 return sdi;
393}
394
6aa1eb4e
BV
395static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
396 int buflen)
397{
398 struct dev_context *devc;
399 struct sr_datafeed_packet packet;
400 struct sr_datafeed_analog analog;
1d166757 401 struct sr_probe *probe;
6787f404 402 float *temp, *rh;
6aa1eb4e 403 uint16_t s;
6787f404 404 int samples, samples_left, i, j;
6aa1eb4e
BV
405
406 devc = sdi->priv;
69e19dd7 407
6aa1eb4e
BV
408 samples = buflen / devc->sample_size;
409 samples_left = devc->logged_samples - devc->rcvd_samples;
410 if (samples_left < samples)
411 samples = samples_left;
412 switch (devc->profile->logformat) {
413 case LOG_TEMP_RH:
6787f404
BV
414 packet.type = SR_DF_ANALOG;
415 packet.payload = &analog;
416 analog.mqflags = 0;
417 if (!(temp = g_try_malloc(sizeof(float) * samples)))
418 break;
419 if (!(rh = g_try_malloc(sizeof(float) * samples)))
420 break;
421 for (i = 0, j = 0; i < samples; i++) {
422 /* Both Celcius and Fahrenheit stored at base -40. */
423 if (devc->temp_unit == 0)
424 /* Celcius is stored in half-degree increments. */
425 temp[j] = buf[i * 2] / 2 - 40;
426 else
427 temp[j] = buf[i * 2] - 40;
428
429 rh[j] = buf[i * 2 + 1] / 2;
430
431 if (temp[j] == 0.0 && rh[j] == 0.0)
432 /* Skip invalid measurement. */
433 continue;
434 j++;
435 }
436 analog.num_samples = j;
437
1d166757
BV
438 probe = sdi->probes->data;
439 if (probe->enabled) {
440 analog.probes = g_slist_append(NULL, probe);
441 analog.mq = SR_MQ_TEMPERATURE;
442 if (devc->temp_unit == 1)
443 analog.unit = SR_UNIT_FAHRENHEIT;
444 else
445 analog.unit = SR_UNIT_CELSIUS;
446 analog.data = temp;
447 sr_session_send(devc->cb_data, &packet);
448 }
449
450 probe = sdi->probes->next->data;
451 if (probe->enabled) {
452 analog.probes = g_slist_append(NULL, probe);
453 analog.mq = SR_MQ_RELATIVE_HUMIDITY;
454 analog.unit = SR_UNIT_PERCENTAGE;
455 analog.data = rh;
456 sr_session_send(devc->cb_data, &packet);
457 }
458
6787f404
BV
459 g_free(temp);
460 g_free(rh);
6aa1eb4e
BV
461 break;
462 case LOG_CO:
463 packet.type = SR_DF_ANALOG;
464 packet.payload = &analog;
1d166757 465 analog.probes = sdi->probes;
6aa1eb4e 466 analog.num_samples = samples;
4f3bd685
BV
467 analog.mq = SR_MQ_CARBON_MONOXIDE;
468 analog.unit = SR_UNIT_CONCENTRATION;
6aa1eb4e
BV
469 analog.mqflags = 0;
470 if (!(analog.data = g_try_malloc(sizeof(float) * samples)))
471 break;
472 for (i = 0; i < samples; i++) {
473 s = (buf[i * 2] << 8) | buf[i * 2 + 1];
7f00750c 474 analog.data[i] = (s * devc->co_high + devc->co_low) / 1000000;
6aa1eb4e
BV
475 if (analog.data[i] < 0.0)
476 analog.data[i] = 0.0;
477 }
478 sr_session_send(devc->cb_data, &packet);
479 g_free(analog.data);
480 break;
481 default:
482 /* How did we even get this far? */
483 break;
484 }
485 devc->rcvd_samples += samples;
486
487}
851d5b22 488
6aa1eb4e 489SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
46697e38 490{
46697e38 491 struct dev_context *devc;
6aa1eb4e
BV
492 struct drv_context *drvc = di->priv;
493 struct sr_datafeed_packet packet;
494 struct sr_dev_inst *sdi;
495 struct timeval tv;
496 int i;
46697e38 497
6aa1eb4e
BV
498 (void)fd;
499 (void)revents;
46697e38 500
6aa1eb4e
BV
501 sdi = cb_data;
502 devc = sdi->priv;
46697e38 503
6aa1eb4e
BV
504 if (sdi->status == SR_ST_STOPPING) {
505 for (i = 0; devc->usbfd[i] != -1; i++)
506 sr_source_remove(devc->usbfd[i]);
507
508 sdi->driver->dev_close(sdi);
509
510 packet.type = SR_DF_END;
511 sr_session_send(cb_data, &packet);
46697e38
BV
512 }
513
6aa1eb4e
BV
514 memset(&tv, 0, sizeof(struct timeval));
515 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
516 NULL);
517
46697e38
BV
518 return TRUE;
519}
6aa1eb4e
BV
520
521SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
522{
523 struct dev_context *devc;
524 struct sr_dev_inst *sdi;
525 int ret;
526 gboolean packet_has_error;
527
528 sdi = transfer->user_data;
529 devc = sdi->priv;
530
531 packet_has_error = FALSE;
532 switch (transfer->status) {
533 case LIBUSB_TRANSFER_NO_DEVICE:
534 /* USB device was unplugged. */
535 hw_dev_acquisition_stop(sdi, sdi);
536 return;
537 case LIBUSB_TRANSFER_COMPLETED:
538 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
539 break;
540 default:
541 packet_has_error = TRUE;
542 break;
543 }
544
545 if (!packet_has_error) {
546 if (devc->rcvd_samples < devc->logged_samples)
547 lascar_el_usb_dispatch(sdi, transfer->buffer,
548 transfer->actual_length);
549 devc->rcvd_bytes += transfer->actual_length;
550 sr_spew("received %d/%d bytes (%d/%d samples)",
551 devc->rcvd_bytes, devc->log_size,
552 devc->rcvd_samples, devc->logged_samples);
553 if (devc->rcvd_bytes >= devc->log_size)
554 hw_dev_acquisition_stop(sdi, sdi);
555 }
556
557 if (sdi->status == SR_ST_ACTIVE) {
558 /* Send the same request again. */
559 if ((ret = libusb_submit_transfer(transfer) != 0)) {
560 sr_err("Unable to resubmit transfer: %s.",
561 libusb_error_name(ret));
562 g_free(transfer->buffer);
563 libusb_free_transfer(transfer);
564 hw_dev_acquisition_stop(sdi, sdi);
565 }
566 } else {
567 /* This was the last transfer we're going to receive, so
568 * clean up now. */
569 g_free(transfer->buffer);
570 libusb_free_transfer(transfer);
571 }
572
573}
361d1511
BV
574
575static int get_flags(unsigned char *configblock)
576{
577 int flags;
578
579 flags = (configblock[32] | (configblock[33] << 8)) & 0x1fff;
580 sr_spew("Read flags (0x%.4x).", flags);
581
582 return flags;
583}
584
585static int set_flags(unsigned char *configblock, int flags)
586{
587
588 sr_spew("Setting flags to 0x%.4x.", flags);
589 configblock[32] = flags & 0xff;
590 configblock[33] = (flags >> 8) & 0x1f;
591
592 return flags;
593}
594
595SR_PRIV int lascar_is_logging(const struct sr_dev_inst *sdi)
596{
597 struct dev_context *devc;
598 int dummy, flags, ret;
599
600 devc = sdi->priv;
601 if (lascar_get_config(devc->usb->devhdl, devc->config, &dummy) != SR_OK)
602 return -1;
603
604 flags = get_flags(devc->config);
605 if (flags & 0x0100)
606 ret = 1;
607 else
608 ret = 0;
609
610 return ret;
611}
612
613SR_PRIV int lascar_start_logging(const struct sr_dev_inst *sdi)
614{
615 struct dev_context *devc;
616 int len, flags, ret;
617
618 devc = sdi->priv;
619 if (lascar_get_config(devc->usb->devhdl, devc->config, &len) != SR_OK)
620 return SR_ERR;
621
622 /* Turn on logging. */
623 flags = get_flags(devc->config);
624 flags |= 0x0100;
625 set_flags(devc->config, flags);
626
627 /* Start logging in 0 seconds. */
628 memset(devc->config + 24, 0, 4);
629
630 ret = lascar_save_config(devc->usb->devhdl, devc->config, len);
631 sr_info("Started internal logging.");
632
633 return ret;
634}
635
636SR_PRIV int lascar_stop_logging(const struct sr_dev_inst *sdi)
637{
638 struct dev_context *devc;
639 int len, flags, ret;
640
641 devc = sdi->priv;
642 if (lascar_get_config(devc->usb->devhdl, devc->config, &len) != SR_OK)
643 return SR_ERR;
644
645 flags = get_flags(devc->config);
646 flags &= ~0x0100;
647 set_flags(devc->config, flags);
648
649 ret = lascar_save_config(devc->usb->devhdl, devc->config, len);
650 sr_info("Stopped internal logging.");
651
652 return ret;
653}
654