]> sigrok.org Git - libsigrok.git/blame - hardware/kecheng-kc-330b/api.c
kecheng-kc-330b: Live SPL acquisition
[libsigrok.git] / hardware / kecheng-kc-330b / api.c
CommitLineData
ed759a08
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 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
bc7be4a9 20#include <string.h>
ed759a08
BV
21#include "protocol.h"
22
a8806f03
BV
23#define USB_CONN "1041.8101"
24#define VENDOR "Kecheng"
25#define USB_INTERFACE 0
a8806f03
BV
26
27static const int32_t hwcaps[] = {
28 SR_CONF_SOUNDLEVELMETER,
29 SR_CONF_LIMIT_SAMPLES,
30 SR_CONF_CONTINUOUS,
bc7be4a9
BV
31 SR_CONF_DATALOG,
32 SR_CONF_SPL_WEIGHT_FREQ,
33 SR_CONF_SPL_WEIGHT_TIME,
34 SR_CONF_DATA_SOURCE,
35};
36
df035528 37SR_PRIV const uint64_t kecheng_kc_330b_sample_intervals[][2] = {
bc7be4a9
BV
38 { 1, 8 },
39 { 1, 2 },
40 { 1, 1 },
41 { 2, 1 },
42 { 5, 1 },
43 { 10, 1 },
44 { 60, 1 },
45};
46
47static const char *weight_freq[] = {
48 "A",
49 "C",
50};
51
52static const char *weight_time[] = {
53 "F",
54 "S",
55};
56
57static const char *data_sources[] = {
58 "Live",
59 "Memory",
a8806f03
BV
60};
61
ed759a08
BV
62SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info;
63static struct sr_dev_driver *di = &kecheng_kc_330b_driver_info;
64
65
66static int init(struct sr_context *sr_ctx)
67{
68 return std_init(sr_ctx, di, LOG_PREFIX);
69}
70
a8806f03
BV
71static int scan_kecheng(struct sr_usb_dev_inst *usb, char **model)
72{
73 struct drv_context *drvc;
74 int len, ret;
75 unsigned char cmd, buf[32];
76
77 drvc = di->priv;
78 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
79 return SR_ERR;
80
81 cmd = CMD_IDENTIFY;
82 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5);
83 if (ret != 0) {
84 libusb_close(usb->devhdl);
85 sr_dbg("Failed to send Identify command: %s", libusb_error_name(ret));
86 return SR_ERR;
87 }
88
89 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 32, &len, 10);
90 if (ret != 0) {
91 libusb_close(usb->devhdl);
92 sr_dbg("Failed to receive response: %s", libusb_error_name(ret));
93 return SR_ERR;
94 }
95
96 libusb_close(usb->devhdl);
97 usb->devhdl = NULL;
98
99 if (len < 2 || buf[0] != (CMD_IDENTIFY | 0x80) || buf[1] > 30) {
100 sr_dbg("Invalid response to Identify command");
101 return SR_ERR;
102 }
103
104 buf[buf[1] + 2] = '\x0';
105 *model = g_strndup((const gchar *)buf + 2, 30);
106 //g_strstrip(*model);
107
108 return SR_OK;
109}
110
ed759a08
BV
111static GSList *scan(GSList *options)
112{
113 struct drv_context *drvc;
a8806f03
BV
114 struct dev_context *devc;
115 struct sr_dev_inst *sdi;
116 struct sr_probe *probe;
117 GSList *usb_devices, *devices, *l;
118 char *model;
ed759a08
BV
119
120 (void)options;
121
ed759a08
BV
122 drvc = di->priv;
123 drvc->instances = NULL;
124
a8806f03
BV
125 devices = NULL;
126 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_CONN))) {
127 /* We have a list of sr_usb_dev_inst matching the connection
128 * string. Wrap them in sr_dev_inst and we're done. */
129 for (l = usb_devices; l; l = l->next) {
130 if (scan_kecheng(l->data, &model) != SR_OK)
131 continue;
132 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR,
133 model, NULL)))
134 return NULL;
135 g_free(model);
136 sdi->driver = di;
137 sdi->inst_type = SR_INST_USB;
138 sdi->conn = l->data;
139 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
140 return NULL;
141 sdi->probes = g_slist_append(sdi->probes, probe);
142
143 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
144 sr_dbg("Device context malloc failed.");
145 return NULL;
146 }
147 sdi->priv = devc;
148 devc->limit_samples = 0;
bc7be4a9
BV
149 /* The protocol provides no way to read the current
150 * settings, so we'll enforce these. */
151 devc->sample_interval = DEFAULT_SAMPLE_INTERVAL;
152 devc->alarm_low = DEFAULT_ALARM_LOW;
153 devc->alarm_high = DEFAULT_ALARM_HIGH;
154 devc->mqflags = DEFAULT_WEIGHT_TIME | DEFAULT_WEIGHT_FREQ;
155 devc->data_source = DEFAULT_DATA_SOURCE;
156
157 /* TODO: Set date/time? */
a8806f03
BV
158
159 drvc->instances = g_slist_append(drvc->instances, sdi);
160 devices = g_slist_append(devices, sdi);
161 }
162 g_slist_free(usb_devices);
163 } else
164 g_slist_free_full(usb_devices, g_free);
ed759a08
BV
165
166 return devices;
167}
168
169static GSList *dev_list(void)
170{
171 struct drv_context *drvc;
172
173 drvc = di->priv;
174
175 return drvc->instances;
176}
177
178static int dev_clear(void)
179{
180 return std_dev_clear(di, NULL);
181}
182
183static int dev_open(struct sr_dev_inst *sdi)
184{
a8806f03
BV
185 struct drv_context *drvc;
186 struct sr_usb_dev_inst *usb;
187 int ret;
188
189 if (!(drvc = di->priv)) {
190 sr_err("Driver was not initialized.");
191 return SR_ERR;
192 }
193
194 usb = sdi->conn;
ed759a08 195
a8806f03
BV
196 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
197 return SR_ERR;
ed759a08 198
a8806f03
BV
199 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
200 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
201 return SR_ERR;
202 }
ed759a08
BV
203 sdi->status = SR_ST_ACTIVE;
204
bc7be4a9
BV
205 /* Force configuration. */
206 ret = kecheng_kc_330b_configure(sdi);
207
a8806f03 208 return ret;
ed759a08
BV
209}
210
211static int dev_close(struct sr_dev_inst *sdi)
212{
a8806f03
BV
213 struct sr_usb_dev_inst *usb;
214
215 if (!di->priv) {
216 sr_err("Driver was not initialized.");
217 return SR_ERR;
218 }
219
220 usb = sdi->conn;
ed759a08 221
a8806f03
BV
222 if (!usb->devhdl)
223 /* Nothing to do. */
224 return SR_OK;
ed759a08 225
a8806f03
BV
226 libusb_release_interface(usb->devhdl, USB_INTERFACE);
227 libusb_close(usb->devhdl);
228 usb->devhdl = NULL;
ed759a08
BV
229 sdi->status = SR_ST_INACTIVE;
230
231 return SR_OK;
232}
233
234static int cleanup(void)
235{
a8806f03
BV
236 int ret;
237 struct drv_context *drvc;
ed759a08 238
a8806f03
BV
239 if (!(drvc = di->priv))
240 /* Can get called on an unused driver, doesn't matter. */
241 return SR_OK;
ed759a08 242
a8806f03
BV
243 ret = dev_clear();
244 g_free(drvc);
245 di->priv = NULL;
246
247 return ret;
ed759a08
BV
248}
249
250static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
251{
a8806f03 252 struct dev_context *devc;
bc7be4a9
BV
253 GVariant *rational[2];
254 const uint64_t *si;
255 int tmp, ret;
ed759a08 256
a8806f03 257 devc = sdi->priv;
ed759a08 258 switch (key) {
a8806f03
BV
259 case SR_CONF_LIMIT_SAMPLES:
260 *data = g_variant_new_uint64(devc->limit_samples);
261 break;
bc7be4a9 262 case SR_CONF_SAMPLE_INTERVAL:
df035528 263 si = kecheng_kc_330b_sample_intervals[devc->sample_interval];
bc7be4a9
BV
264 rational[0] = g_variant_new_uint64(si[0]);
265 rational[1] = g_variant_new_uint64(si[1]);
266 *data = g_variant_new_tuple(rational, 2);
267 break;
268 case SR_CONF_DATALOG:
269 if ((ret = kecheng_kc_330b_recording_get(sdi, &tmp)) == SR_OK)
270 *data = g_variant_new_boolean(tmp);
271 else
272 return SR_ERR;
273 break;
274 case SR_CONF_SPL_WEIGHT_FREQ:
275 if (devc->mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
276 *data = g_variant_new_string("A");
277 else
278 *data = g_variant_new_string("C");
279 break;
280 case SR_CONF_SPL_WEIGHT_TIME:
281 if (devc->mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
282 *data = g_variant_new_string("F");
283 else
284 *data = g_variant_new_string("S");
285 break;
286 case SR_CONF_DATA_SOURCE:
287 if (devc->data_source == DATA_SOURCE_LIVE)
288 *data = g_variant_new_string("Live");
289 else
290 *data = g_variant_new_string("Memory");
291 break;
ed759a08
BV
292 default:
293 return SR_ERR_NA;
294 }
295
a8806f03 296 return SR_OK;
ed759a08
BV
297}
298
299static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
300{
a8806f03 301 struct dev_context *devc;
bc7be4a9
BV
302 uint64_t p, q;
303 unsigned int i;
304 int tmp, ret;
305 const char *tmp_str;
ed759a08 306
ed759a08
BV
307 if (sdi->status != SR_ST_ACTIVE)
308 return SR_ERR_DEV_CLOSED;
309
a8806f03
BV
310 if (!di->priv) {
311 sr_err("Driver was not initialized.");
312 return SR_ERR;
313 }
314
315 devc = sdi->priv;
ed759a08
BV
316 ret = SR_OK;
317 switch (key) {
a8806f03
BV
318 case SR_CONF_LIMIT_SAMPLES:
319 devc->limit_samples = g_variant_get_uint64(data);
320 sr_dbg("Setting sample limit to %" PRIu64 ".",
321 devc->limit_samples);
322 break;
bc7be4a9
BV
323 case SR_CONF_SAMPLE_INTERVAL:
324 g_variant_get(data, "(tt)", &p, &q);
df035528
BV
325 for (i = 0; i < ARRAY_SIZE(kecheng_kc_330b_sample_intervals); i++) {
326 if (kecheng_kc_330b_sample_intervals[i][0] != p || kecheng_kc_330b_sample_intervals[i][1] != q)
bc7be4a9
BV
327 continue;
328 devc->sample_interval = i;
329 kecheng_kc_330b_configure(sdi);
330 break;
331 }
df035528 332 if (i == ARRAY_SIZE(kecheng_kc_330b_sample_intervals))
bc7be4a9
BV
333 ret = SR_ERR_ARG;
334 break;
335 case SR_CONF_SPL_WEIGHT_FREQ:
336 tmp_str = g_variant_get_string(data, NULL);
337 if (!strcmp(tmp_str, "A"))
338 tmp = SR_MQFLAG_SPL_FREQ_WEIGHT_A;
339 else if (!strcmp(tmp_str, "C"))
340 tmp = SR_MQFLAG_SPL_FREQ_WEIGHT_C;
341 else
342 return SR_ERR_ARG;
343 devc->mqflags &= ~(SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
344 devc->mqflags |= tmp;
345 kecheng_kc_330b_configure(sdi);
346 break;
347 case SR_CONF_SPL_WEIGHT_TIME:
348 tmp_str = g_variant_get_string(data, NULL);
349 if (!strcmp(tmp_str, "F"))
350 tmp = SR_MQFLAG_SPL_TIME_WEIGHT_F;
351 else if (!strcmp(tmp_str, "S"))
352 tmp = SR_MQFLAG_SPL_TIME_WEIGHT_S;
353 else
354 return SR_ERR_ARG;
355 devc->mqflags &= ~(SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
356 devc->mqflags |= tmp;
357 kecheng_kc_330b_configure(sdi);
358 break;
359 case SR_CONF_DATA_SOURCE:
360 tmp_str = g_variant_get_string(data, NULL);
361 if (!strcmp(tmp_str, "Live"))
362 devc->data_source = DATA_SOURCE_LIVE;
363 else if (!strcmp(tmp_str, "Memory"))
364 devc->data_source = DATA_SOURCE_MEMORY;
365 else
366 return SR_ERR;
367 break;
ed759a08
BV
368 default:
369 ret = SR_ERR_NA;
370 }
371
372 return ret;
373}
374
375static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
376{
bc7be4a9
BV
377 GVariant *tuple, *rational[2];
378 GVariantBuilder gvb;
379 unsigned int i;
ed759a08
BV
380
381 (void)sdi;
ed759a08 382
ed759a08 383 switch (key) {
a8806f03
BV
384 case SR_CONF_DEVICE_OPTIONS:
385 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
386 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
387 break;
bc7be4a9
BV
388 case SR_CONF_SAMPLE_INTERVAL:
389 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
df035528
BV
390 for (i = 0; i < ARRAY_SIZE(kecheng_kc_330b_sample_intervals); i++) {
391 rational[0] = g_variant_new_uint64(kecheng_kc_330b_sample_intervals[i][0]);
392 rational[1] = g_variant_new_uint64(kecheng_kc_330b_sample_intervals[i][1]);
bc7be4a9
BV
393 tuple = g_variant_new_tuple(rational, 2);
394 g_variant_builder_add_value(&gvb, tuple);
395 }
396 *data = g_variant_builder_end(&gvb);
397 break;
398 case SR_CONF_SPL_WEIGHT_FREQ:
399 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
400 break;
401 case SR_CONF_SPL_WEIGHT_TIME:
402 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
403 break;
404 case SR_CONF_DATA_SOURCE:
405 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
406 break;
ed759a08
BV
407 default:
408 return SR_ERR_NA;
409 }
410
a8806f03 411 return SR_OK;
ed759a08
BV
412}
413
414static int dev_acquisition_start(const struct sr_dev_inst *sdi,
415 void *cb_data)
416{
bc7be4a9
BV
417 struct drv_context *drvc;
418 struct dev_context *devc;
419 struct sr_usb_dev_inst *usb;
420 const struct libusb_pollfd **pfd;
421 int len, ret, i;
422 unsigned char cmd;
ed759a08
BV
423
424 if (sdi->status != SR_ST_ACTIVE)
425 return SR_ERR_DEV_CLOSED;
426
bc7be4a9
BV
427 drvc = di->priv;
428 devc = sdi->priv;
429 usb = sdi->conn;
430
431 devc->cb_data = cb_data;
432 devc->num_samples = 0;
433
df035528
BV
434 if (!(devc->xfer = libusb_alloc_transfer(0)))
435 return SR_ERR;
436
bc7be4a9
BV
437 /* Send header packet to the session bus. */
438 std_session_send_df_header(cb_data, LOG_PREFIX);
439
440 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
441 for (i = 0; pfd[i]; i++) {
df035528
BV
442 /* Handle USB events every 10ms. */
443 sr_source_add(pfd[i]->fd, pfd[i]->events, 10,
bc7be4a9
BV
444 kecheng_kc_330b_handle_events, (void *)sdi);
445 /* We'll need to remove this fd later. */
446 devc->usbfd[i] = pfd[i]->fd;
447 }
448 devc->usbfd[i] = -1;
449
450 cmd = CMD_GET_LIVE_SPL;
451 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5);
452 if (ret != 0 || len != 1) {
453 sr_dbg("Failed to start acquisition: %s", libusb_error_name(ret));
df035528
BV
454 libusb_free_transfer(devc->xfer);
455 return SR_ERR;
456 }
457 devc->last_live_request = g_get_monotonic_time() / 1000;
458
459 libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf,
460 16, kecheng_kc_330b_receive_transfer, (void *)sdi, 15);
461 if (libusb_submit_transfer(devc->xfer) != 0) {
462 libusb_free_transfer(devc->xfer);
bc7be4a9
BV
463 return SR_ERR;
464 }
df035528 465 devc->state = LIVE_SPL_WAIT;
ed759a08
BV
466
467 return SR_OK;
468}
469
470static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
471{
472 (void)cb_data;
473
474 if (sdi->status != SR_ST_ACTIVE)
475 return SR_ERR_DEV_CLOSED;
476
df035528
BV
477 /* Signal USB transfer handler to clean up and stop. */
478 sdi->status = SR_ST_STOPPING;
ed759a08
BV
479
480 return SR_OK;
481}
482
483SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info = {
484 .name = "kecheng-kc-330b",
485 .longname = "Kecheng KC-330B",
486 .api_version = 1,
487 .init = init,
488 .cleanup = cleanup,
489 .scan = scan,
490 .dev_list = dev_list,
491 .dev_clear = dev_clear,
492 .config_get = config_get,
493 .config_set = config_set,
494 .config_list = config_list,
495 .dev_open = dev_open,
496 .dev_close = dev_close,
497 .dev_acquisition_start = dev_acquisition_start,
498 .dev_acquisition_stop = dev_acquisition_stop,
499 .priv = NULL,
500};