]> sigrok.org Git - libsigrok.git/blame - hardware/lascar-el-usb/protocol.c
add SR_CONF_DATALOG
[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
6aa1eb4e 88SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl)
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
BV
94 int buflen;
95 unsigned char cmd[3], buf[256], *config;
851d5b22
BV
96
97 drvc = di->priv;
6aa1eb4e 98 config = NULL;
851d5b22
BV
99
100 if (!(xfer_in = libusb_alloc_transfer(0)) ||
101 !(xfer_out = libusb_alloc_transfer(0)))
6aa1eb4e 102 return NULL;
851d5b22
BV
103
104 /* Flush anything the F321 still has queued. */
105 while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
106 5) == 0 && buflen > 0)
107 ;
108
109 /* Keep a read request waiting in the wings, ready to pounce
110 * the moment the device sends something. */
111 libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
6aa1eb4e 112 buf, 256, mark_xfer, 0, 10000);
851d5b22
BV
113 if (libusb_submit_transfer(xfer_in) != 0)
114 goto cleanup;
115
116 /* Request device configuration structure. */
117 cmd[0] = 0x00;
118 cmd[1] = 0xff;
119 cmd[2] = 0xff;
120 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
6aa1eb4e 121 cmd, 3, mark_xfer, 0, 100);
851d5b22
BV
122 if (libusb_submit_transfer(xfer_out) != 0)
123 goto cleanup;
124
125 tv.tv_sec = 0;
126 tv.tv_usec = 0;
127 start = g_get_monotonic_time();
128 while (!xfer_in->user_data || !xfer_out->user_data) {
129 if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
130 start = 0;
131 break;
132 }
133 g_usleep(5000);
134 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
135 }
136 if (!start) {
137 sr_dbg("no response");
138 goto cleanup;
139 }
140 if (xfer_in->actual_length != 3) {
141 sr_dbg("expected 3-byte header, got %d bytes", xfer_in->actual_length);
142 goto cleanup;
143 }
144
145 /* Got configuration structure header. */
6aa1eb4e 146 sr_dbg("response to config request: 0x%.2x 0x%.2x 0x%.2x ",
851d5b22
BV
147 buf[0], buf[1], buf[2]);
148 buflen = buf[1] | (buf[2] << 8);
149 if (buf[0] != 0x02 || buflen > 256) {
150 sr_dbg("Invalid response to config request: "
151 "0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]);
152 libusb_close(dev_hdl);
153 goto cleanup;
154 }
155
156 /* Get configuration structure. */
157 xfer_in->length = buflen;
158 xfer_in->user_data = 0;
159 if (libusb_submit_transfer(xfer_in) != 0)
160 goto cleanup;
161 while (!xfer_in->user_data) {
162 if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
163 start = 0;
164 break;
165 }
166 g_usleep(5000);
167 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
168 }
169 if (!start) {
170 sr_dbg("Timeout waiting for configuration structure.");
171 goto cleanup;
172 }
173 if (xfer_in->actual_length != buflen) {
174 sr_dbg("expected %d-byte structure, got %d bytes", buflen,
175 xfer_in->actual_length);
176 goto cleanup;
177 }
6aa1eb4e
BV
178
179 if (!(config = g_try_malloc(256)))
180 return NULL;
181 memcpy(config, buf, buflen);
851d5b22
BV
182
183cleanup:
184 if (!xfer_in->user_data || !xfer_in->user_data) {
185 if (!xfer_in->user_data)
186 libusb_cancel_transfer(xfer_in);
187 if (!xfer_out->user_data)
188 libusb_cancel_transfer(xfer_out);
189 start = g_get_monotonic_time();
190 while (!xfer_in->user_data || !xfer_out->user_data) {
191 if (g_get_monotonic_time() - start > 10000)
192 break;
193 g_usleep(1000);
194 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
195 }
196 }
197 libusb_free_transfer(xfer_in);
198 libusb_free_transfer(xfer_out);
199
6aa1eb4e
BV
200 return config;
201}
202
b0c95747
BV
203/* Currently unused. */
204SR_PRIV 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 drvc = di->priv;
215
216 if (!(xfer_in = libusb_alloc_transfer(0)) ||
217 !(xfer_out = libusb_alloc_transfer(0)))
218 return SR_ERR;
219
220 /* Flush anything the F321 still has queued. */
221 while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
222 5) == 0 && buflen > 0)
223 ;
224 ret = SR_OK;
225
226 /* Keep a read request waiting in the wings, ready to pounce
227 * the moment the device sends something. */
228 libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
229 buf, 256, mark_xfer, 0, 10000);
230 if (libusb_submit_transfer(xfer_in) != 0) {
231 ret = SR_ERR;
232 goto cleanup;
233 }
234
235 /* Request device configuration structure. */
236 cmd[0] = 0x01;
237 cmd[1] = configlen & 0xff;
238 cmd[2] = (configlen >> 8) & 0xff;
239 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
240 cmd, 3, mark_xfer, 0, 100);
241 if (libusb_submit_transfer(xfer_out) != 0) {
242 ret = SR_ERR;
243 goto cleanup;
244 }
245 tv.tv_sec = 0;
246 tv.tv_usec = 0;
247 while (!xfer_out->user_data) {
248 g_usleep(5000);
249 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
250 }
251
252 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
253 config, configlen, mark_xfer, 0, 100);
254 if (libusb_submit_transfer(xfer_out) != 0) {
255 ret = SR_ERR;
256 goto cleanup;
257 }
258 while (!xfer_in->user_data || !xfer_out->user_data) {
259 g_usleep(5000);
260 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
261 }
262
263 if (xfer_in->actual_length != 1 || buf[0] != 0xff) {
264 sr_dbg("unexpected response after transfer");
265 ret = SR_ERR;
266 }
267
268cleanup:
269 if (!xfer_in->user_data || !xfer_in->user_data) {
270 if (!xfer_in->user_data)
271 libusb_cancel_transfer(xfer_in);
272 if (!xfer_out->user_data)
273 libusb_cancel_transfer(xfer_out);
274 start = g_get_monotonic_time();
275 while (!xfer_in->user_data || !xfer_out->user_data) {
276 if (g_get_monotonic_time() - start > 10000)
277 break;
278 g_usleep(1000);
279 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
280 }
281 }
282 libusb_free_transfer(xfer_in);
283 libusb_free_transfer(xfer_out);
284
285 return ret;
286}
287
6aa1eb4e
BV
288static struct sr_dev_inst *lascar_identify(unsigned char *config)
289{
290 struct dev_context *devc;
291 const struct elusb_profile *profile;
292 struct sr_dev_inst *sdi;
293 struct sr_probe *probe;
294 int modelid, i;
295 char firmware[5];
296
297 modelid = config[0];
851d5b22
BV
298 sdi = NULL;
299 if (modelid) {
300 profile = NULL;
301 for (i = 0; profiles[i].modelid; i++) {
302 if (profiles[i].modelid == modelid) {
303 profile = &profiles[i];
304 break;
305 }
306 }
307 if (!profile) {
308 sr_dbg("unknown EL-USB modelid %d", modelid);
309 return NULL;
310 }
311
6aa1eb4e
BV
312 i = config[52] | (config[53] << 8);
313 memcpy(firmware, config + 0x30, 4);
851d5b22
BV
314 firmware[4] = '\0';
315 sr_dbg("found %s with firmware version %s serial %d",
316 profile->modelname, firmware, i);
317
318 if (profile->logformat == LOG_UNSUPPORTED) {
319 sr_dbg("unsupported EL-USB logformat for %s", profile->modelname);
320 return NULL;
321 }
322
323 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, LASCAR_VENDOR,
324 profile->modelname, firmware)))
325 return NULL;
326 sdi->driver = di;
6aa1eb4e
BV
327
328 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
329 return NULL;
330 sdi->probes = g_slist_append(NULL, probe);
331
332 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
333 return NULL;
334 sdi->priv = devc;
335 devc->profile = profile;
851d5b22
BV
336 }
337
338 return sdi;
339}
340
341SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
342{
343 struct drv_context *drvc;
344 struct sr_dev_inst *sdi;
345 struct libusb_device **devlist;
346 struct libusb_device_descriptor des;
347 libusb_device_handle *dev_hdl;
348 int ret, i;
6aa1eb4e 349 unsigned char *config;
851d5b22
BV
350
351 drvc = di->priv;
352 sdi = NULL;
353
354 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
355 for (i = 0; devlist[i]; i++) {
356 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
357 sr_err("Failed to get device descriptor: %d.", ret);
358 continue;
359 }
360
361 if (libusb_get_bus_number(devlist[i]) != bus ||
362 libusb_get_device_address(devlist[i]) != address)
363 continue;
364
6aa1eb4e
BV
365 if (!(dev_hdl = lascar_open(devlist[i])))
366 continue;
367
368 if (!(config = lascar_get_config(dev_hdl)))
851d5b22 369 continue;
851d5b22 370
851d5b22 371 libusb_close(dev_hdl);
6aa1eb4e
BV
372 sdi = lascar_identify(config);
373 g_free(config);
851d5b22
BV
374 }
375
376 return sdi;
377}
378
6aa1eb4e
BV
379static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
380 int buflen)
381{
382 struct dev_context *devc;
383 struct sr_datafeed_packet packet;
384 struct sr_datafeed_analog analog;
6787f404 385 float *temp, *rh;
6aa1eb4e 386 uint16_t s;
6787f404 387 int samples, samples_left, i, j;
6aa1eb4e
BV
388
389 devc = sdi->priv;
69e19dd7
BV
390 analog.probes = sdi->probes;
391
6aa1eb4e
BV
392 samples = buflen / devc->sample_size;
393 samples_left = devc->logged_samples - devc->rcvd_samples;
394 if (samples_left < samples)
395 samples = samples_left;
396 switch (devc->profile->logformat) {
397 case LOG_TEMP_RH:
6787f404
BV
398 packet.type = SR_DF_ANALOG;
399 packet.payload = &analog;
400 analog.mqflags = 0;
401 if (!(temp = g_try_malloc(sizeof(float) * samples)))
402 break;
403 if (!(rh = g_try_malloc(sizeof(float) * samples)))
404 break;
405 for (i = 0, j = 0; i < samples; i++) {
406 /* Both Celcius and Fahrenheit stored at base -40. */
407 if (devc->temp_unit == 0)
408 /* Celcius is stored in half-degree increments. */
409 temp[j] = buf[i * 2] / 2 - 40;
410 else
411 temp[j] = buf[i * 2] - 40;
412
413 rh[j] = buf[i * 2 + 1] / 2;
414
415 if (temp[j] == 0.0 && rh[j] == 0.0)
416 /* Skip invalid measurement. */
417 continue;
418 j++;
419 }
420 analog.num_samples = j;
421
422 analog.mq = SR_MQ_TEMPERATURE;
423 if (devc->temp_unit == 1)
424 analog.unit = SR_UNIT_FAHRENHEIT;
425 else
426 analog.unit = SR_UNIT_CELSIUS;
427 analog.data = temp;
428 sr_session_send(devc->cb_data, &packet);
429
430 analog.mq = SR_MQ_RELATIVE_HUMIDITY;
431 analog.unit = SR_UNIT_PERCENTAGE;
432 analog.data = rh;
433 sr_session_send(devc->cb_data, &packet);
434 g_free(temp);
435 g_free(rh);
6aa1eb4e
BV
436 break;
437 case LOG_CO:
438 packet.type = SR_DF_ANALOG;
439 packet.payload = &analog;
440 analog.num_samples = samples;
4f3bd685
BV
441 analog.mq = SR_MQ_CARBON_MONOXIDE;
442 analog.unit = SR_UNIT_CONCENTRATION;
6aa1eb4e
BV
443 analog.mqflags = 0;
444 if (!(analog.data = g_try_malloc(sizeof(float) * samples)))
445 break;
446 for (i = 0; i < samples; i++) {
447 s = (buf[i * 2] << 8) | buf[i * 2 + 1];
7f00750c 448 analog.data[i] = (s * devc->co_high + devc->co_low) / 1000000;
6aa1eb4e
BV
449 if (analog.data[i] < 0.0)
450 analog.data[i] = 0.0;
451 }
452 sr_session_send(devc->cb_data, &packet);
453 g_free(analog.data);
454 break;
455 default:
456 /* How did we even get this far? */
457 break;
458 }
459 devc->rcvd_samples += samples;
460
461}
851d5b22 462
6aa1eb4e 463SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
46697e38 464{
46697e38 465 struct dev_context *devc;
6aa1eb4e
BV
466 struct drv_context *drvc = di->priv;
467 struct sr_datafeed_packet packet;
468 struct sr_dev_inst *sdi;
469 struct timeval tv;
470 int i;
46697e38 471
6aa1eb4e
BV
472 (void)fd;
473 (void)revents;
46697e38 474
6aa1eb4e
BV
475 sdi = cb_data;
476 devc = sdi->priv;
46697e38 477
6aa1eb4e
BV
478 if (sdi->status == SR_ST_STOPPING) {
479 for (i = 0; devc->usbfd[i] != -1; i++)
480 sr_source_remove(devc->usbfd[i]);
481
482 sdi->driver->dev_close(sdi);
483
484 packet.type = SR_DF_END;
485 sr_session_send(cb_data, &packet);
46697e38
BV
486 }
487
6aa1eb4e
BV
488 memset(&tv, 0, sizeof(struct timeval));
489 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
490 NULL);
491
46697e38
BV
492 return TRUE;
493}
6aa1eb4e
BV
494
495SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
496{
497 struct dev_context *devc;
498 struct sr_dev_inst *sdi;
499 int ret;
500 gboolean packet_has_error;
501
502 sdi = transfer->user_data;
503 devc = sdi->priv;
504
505 packet_has_error = FALSE;
506 switch (transfer->status) {
507 case LIBUSB_TRANSFER_NO_DEVICE:
508 /* USB device was unplugged. */
509 hw_dev_acquisition_stop(sdi, sdi);
510 return;
511 case LIBUSB_TRANSFER_COMPLETED:
512 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
513 break;
514 default:
515 packet_has_error = TRUE;
516 break;
517 }
518
519 if (!packet_has_error) {
520 if (devc->rcvd_samples < devc->logged_samples)
521 lascar_el_usb_dispatch(sdi, transfer->buffer,
522 transfer->actual_length);
523 devc->rcvd_bytes += transfer->actual_length;
524 sr_spew("received %d/%d bytes (%d/%d samples)",
525 devc->rcvd_bytes, devc->log_size,
526 devc->rcvd_samples, devc->logged_samples);
527 if (devc->rcvd_bytes >= devc->log_size)
528 hw_dev_acquisition_stop(sdi, sdi);
529 }
530
531 if (sdi->status == SR_ST_ACTIVE) {
532 /* Send the same request again. */
533 if ((ret = libusb_submit_transfer(transfer) != 0)) {
534 sr_err("Unable to resubmit transfer: %s.",
535 libusb_error_name(ret));
536 g_free(transfer->buffer);
537 libusb_free_transfer(transfer);
538 hw_dev_acquisition_stop(sdi, sdi);
539 }
540 } else {
541 /* This was the last transfer we're going to receive, so
542 * clean up now. */
543 g_free(transfer->buffer);
544 libusb_free_transfer(transfer);
545 }
546
547}