]> sigrok.org Git - libsigrok.git/blame - src/hardware/lascar-el-usb/protocol.c
windows: Fix various compiler warnings.
[libsigrok.git] / src / 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 29extern struct sr_dev_driver lascar_el_usb_driver_info;
4f840ce9 30struct sr_dev_driver *di = &lascar_el_usb_driver_info;
851d5b22
BV
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
d87c1766 57static 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 80static 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
87SR_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
BV
98
99 drvc = di->priv;
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
184cleanup:
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 204static 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
b0c95747
BV
216 drvc = di->priv;
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
270cleanup:
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
290static 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
349SR_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
BV
358
359 drvc = di->priv;
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
386static 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);
439 }
440
ba7dd8bb
UH
441 ch = sdi->channels->next->data;
442 if (ch->enabled) {
443 analog.channels = g_slist_append(NULL, ch);
1d166757
BV
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
6787f404
BV
450 g_free(temp);
451 g_free(rh);
6aa1eb4e
BV
452 break;
453 case LOG_CO:
454 packet.type = SR_DF_ANALOG;
455 packet.payload = &analog;
ba7dd8bb 456 analog.channels = sdi->channels;
6aa1eb4e 457 analog.num_samples = samples;
4f3bd685
BV
458 analog.mq = SR_MQ_CARBON_MONOXIDE;
459 analog.unit = SR_UNIT_CONCENTRATION;
6aa1eb4e
BV
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];
1a46cc62 465 analog.data[i] = (s * devc->co_high + devc->co_low) / (1000 * 1000);
6aa1eb4e
BV
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}
851d5b22 479
6aa1eb4e 480SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
46697e38 481{
6aa1eb4e
BV
482 struct drv_context *drvc = di->priv;
483 struct sr_datafeed_packet packet;
484 struct sr_dev_inst *sdi;
485 struct timeval tv;
46697e38 486
6aa1eb4e
BV
487 (void)fd;
488 (void)revents;
46697e38 489
6aa1eb4e 490 sdi = cb_data;
46697e38 491
6aa1eb4e 492 if (sdi->status == SR_ST_STOPPING) {
102f1239 493 usb_source_remove(sdi->session, drvc->sr_ctx);
6aa1eb4e 494
6aa1eb4e
BV
495 packet.type = SR_DF_END;
496 sr_session_send(cb_data, &packet);
46697e38
BV
497 }
498
6aa1eb4e
BV
499 memset(&tv, 0, sizeof(struct timeval));
500 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
501 NULL);
502
46697e38
BV
503 return TRUE;
504}
6aa1eb4e 505
55462b8b 506SR_PRIV void LIBUSB_CALL lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
6aa1eb4e
BV
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. */
6078d2c9 520 dev_acquisition_stop(sdi, sdi);
6aa1eb4e
BV
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)
6078d2c9 539 dev_acquisition_stop(sdi, sdi);
6aa1eb4e
BV
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);
6078d2c9 549 dev_acquisition_stop(sdi, sdi);
6aa1eb4e
BV
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}
361d1511
BV
559
560static 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
570static 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
580SR_PRIV int lascar_is_logging(const struct sr_dev_inst *sdi)
581{
582 struct dev_context *devc;
0f150649 583 struct sr_usb_dev_inst *usb;
361d1511
BV
584 int dummy, flags, ret;
585
586 devc = sdi->priv;
0f150649
BV
587 usb = sdi->conn;
588
589 if (lascar_get_config(usb->devhdl, devc->config, &dummy) != SR_OK)
361d1511
BV
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
601SR_PRIV int lascar_start_logging(const struct sr_dev_inst *sdi)
602{
603 struct dev_context *devc;
0f150649 604 struct sr_usb_dev_inst *usb;
361d1511
BV
605 int len, flags, ret;
606
607 devc = sdi->priv;
0f150649
BV
608 usb = sdi->conn;
609
610 if (lascar_get_config(usb->devhdl, devc->config, &len) != SR_OK)
361d1511
BV
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
0f150649 621 ret = lascar_save_config(usb->devhdl, devc->config, len);
361d1511
BV
622 sr_info("Started internal logging.");
623
624 return ret;
625}
626
627SR_PRIV int lascar_stop_logging(const struct sr_dev_inst *sdi)
628{
629 struct dev_context *devc;
0f150649 630 struct sr_usb_dev_inst *usb;
361d1511
BV
631 int len, flags, ret;
632
633 devc = sdi->priv;
0f150649
BV
634 usb = sdi->conn;
635
636 if (lascar_get_config(usb->devhdl, devc->config, &len) != SR_OK)
361d1511
BV
637 return SR_ERR;
638
639 flags = get_flags(devc->config);
640 flags &= ~0x0100;
641 set_flags(devc->config, flags);
642
0f150649 643 ret = lascar_save_config(usb->devhdl, devc->config, len);
361d1511
BV
644 sr_info("Stopped internal logging.");
645
646 return ret;
647}