2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #define USB_CONN "1041.8101"
24 #define VENDOR "Kecheng"
25 #define USB_INTERFACE 0
27 static const int32_t hwcaps[] = {
28 SR_CONF_SOUNDLEVELMETER,
29 SR_CONF_LIMIT_SAMPLES,
32 SR_CONF_SPL_WEIGHT_FREQ,
33 SR_CONF_SPL_WEIGHT_TIME,
37 SR_PRIV const uint64_t kecheng_kc_330b_sample_intervals[][2] = {
47 static const char *weight_freq[] = {
52 static const char *weight_time[] = {
57 static const char *data_sources[] = {
62 SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info;
63 static struct sr_dev_driver *di = &kecheng_kc_330b_driver_info;
66 static int init(struct sr_context *sr_ctx)
68 return std_init(sr_ctx, di, LOG_PREFIX);
71 static int scan_kecheng(struct sr_usb_dev_inst *usb, char **model)
73 struct drv_context *drvc;
75 unsigned char cmd, buf[32];
78 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
82 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5);
84 libusb_close(usb->devhdl);
85 sr_dbg("Failed to send Identify command: %s", libusb_error_name(ret));
89 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 32, &len, 10);
91 libusb_close(usb->devhdl);
92 sr_dbg("Failed to receive response: %s", libusb_error_name(ret));
96 libusb_close(usb->devhdl);
99 if (len < 2 || buf[0] != (CMD_IDENTIFY | 0x80) || buf[1] > 30) {
100 sr_dbg("Invalid response to Identify command");
104 buf[buf[1] + 2] = '\x0';
105 *model = g_strndup((const gchar *)buf + 2, 30);
106 //g_strstrip(*model);
111 static GSList *scan(GSList *options)
113 struct drv_context *drvc;
114 struct dev_context *devc;
115 struct sr_dev_inst *sdi;
116 struct sr_probe *probe;
117 GSList *usb_devices, *devices, *l;
123 drvc->instances = 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)
132 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR,
137 sdi->inst_type = SR_INST_USB;
139 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
141 sdi->probes = g_slist_append(sdi->probes, probe);
143 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
144 sr_dbg("Device context malloc failed.");
148 devc->limit_samples = 0;
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;
157 /* TODO: Set date/time? */
159 drvc->instances = g_slist_append(drvc->instances, sdi);
160 devices = g_slist_append(devices, sdi);
162 g_slist_free(usb_devices);
164 g_slist_free_full(usb_devices, g_free);
169 static GSList *dev_list(void)
171 struct drv_context *drvc;
175 return drvc->instances;
178 static int dev_clear(void)
180 return std_dev_clear(di, NULL);
183 static int dev_open(struct sr_dev_inst *sdi)
185 struct drv_context *drvc;
186 struct sr_usb_dev_inst *usb;
189 if (!(drvc = di->priv)) {
190 sr_err("Driver was not initialized.");
196 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
199 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
200 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
203 sdi->status = SR_ST_ACTIVE;
205 /* Force configuration. */
206 ret = kecheng_kc_330b_configure(sdi);
211 static int dev_close(struct sr_dev_inst *sdi)
213 struct sr_usb_dev_inst *usb;
216 sr_err("Driver was not initialized.");
226 libusb_release_interface(usb->devhdl, USB_INTERFACE);
227 libusb_close(usb->devhdl);
229 sdi->status = SR_ST_INACTIVE;
234 static int cleanup(void)
237 struct drv_context *drvc;
239 if (!(drvc = di->priv))
240 /* Can get called on an unused driver, doesn't matter. */
250 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
252 struct dev_context *devc;
253 GVariant *rational[2];
259 case SR_CONF_LIMIT_SAMPLES:
260 *data = g_variant_new_uint64(devc->limit_samples);
262 case SR_CONF_SAMPLE_INTERVAL:
263 si = kecheng_kc_330b_sample_intervals[devc->sample_interval];
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);
268 case SR_CONF_DATALOG:
269 if ((ret = kecheng_kc_330b_recording_get(sdi, &tmp)) == SR_OK)
270 *data = g_variant_new_boolean(tmp);
274 case SR_CONF_SPL_WEIGHT_FREQ:
275 if (devc->mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
276 *data = g_variant_new_string("A");
278 *data = g_variant_new_string("C");
280 case SR_CONF_SPL_WEIGHT_TIME:
281 if (devc->mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
282 *data = g_variant_new_string("F");
284 *data = g_variant_new_string("S");
286 case SR_CONF_DATA_SOURCE:
287 if (devc->data_source == DATA_SOURCE_LIVE)
288 *data = g_variant_new_string("Live");
290 *data = g_variant_new_string("Memory");
299 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
301 struct dev_context *devc;
307 if (sdi->status != SR_ST_ACTIVE)
308 return SR_ERR_DEV_CLOSED;
311 sr_err("Driver was not initialized.");
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);
323 case SR_CONF_SAMPLE_INTERVAL:
324 g_variant_get(data, "(tt)", &p, &q);
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)
328 devc->sample_interval = i;
329 kecheng_kc_330b_configure(sdi);
332 if (i == ARRAY_SIZE(kecheng_kc_330b_sample_intervals))
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;
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);
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;
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);
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;
375 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
377 GVariant *tuple, *rational[2];
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));
388 case SR_CONF_SAMPLE_INTERVAL:
389 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
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]);
393 tuple = g_variant_new_tuple(rational, 2);
394 g_variant_builder_add_value(&gvb, tuple);
396 *data = g_variant_builder_end(&gvb);
398 case SR_CONF_SPL_WEIGHT_FREQ:
399 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
401 case SR_CONF_SPL_WEIGHT_TIME:
402 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
404 case SR_CONF_DATA_SOURCE:
405 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
414 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
417 struct drv_context *drvc;
418 struct dev_context *devc;
419 struct sr_usb_dev_inst *usb;
420 const struct libusb_pollfd **pfd;
424 if (sdi->status != SR_ST_ACTIVE)
425 return SR_ERR_DEV_CLOSED;
431 devc->cb_data = cb_data;
432 devc->num_samples = 0;
434 if (devc->data_source == DATA_SOURCE_LIVE) {
435 if (kecheng_kc_330b_status_get(sdi, &ret) != SR_OK)
437 sr_dbg("st %s", ret == DEVICE_ACTIVE ? "act" : "deact");
438 if (ret != DEVICE_ACTIVE) {
439 sr_err("Device is inactive");
440 /* Still continue though, since the device will
441 * just return 30.0 until the user hits the button
442 * on the device -- and then start feeding good
447 if (!(devc->xfer = libusb_alloc_transfer(0)))
450 /* Send header packet to the session bus. */
451 std_session_send_df_header(cb_data, LOG_PREFIX);
453 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
454 for (i = 0; pfd[i]; i++) {
455 /* Handle USB events every 10ms. */
456 sr_source_add(pfd[i]->fd, pfd[i]->events, 10,
457 kecheng_kc_330b_handle_events, (void *)sdi);
458 /* We'll need to remove this fd later. */
459 devc->usbfd[i] = pfd[i]->fd;
463 cmd = CMD_GET_LIVE_SPL;
464 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5);
465 if (ret != 0 || len != 1) {
466 sr_dbg("Failed to start acquisition: %s", libusb_error_name(ret));
467 libusb_free_transfer(devc->xfer);
470 devc->last_live_request = g_get_monotonic_time() / 1000;
472 libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf,
473 16, kecheng_kc_330b_receive_transfer, (void *)sdi, 15);
474 if (libusb_submit_transfer(devc->xfer) != 0) {
475 libusb_free_transfer(devc->xfer);
478 devc->state = LIVE_SPL_WAIT;
483 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
487 if (sdi->status != SR_ST_ACTIVE)
488 return SR_ERR_DEV_CLOSED;
490 /* Signal USB transfer handler to clean up and stop. */
491 sdi->status = SR_ST_STOPPING;
496 SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info = {
497 .name = "kecheng-kc-330b",
498 .longname = "Kecheng KC-330B",
503 .dev_list = dev_list,
504 .dev_clear = dev_clear,
505 .config_get = config_get,
506 .config_set = config_set,
507 .config_list = config_list,
508 .dev_open = dev_open,
509 .dev_close = dev_close,
510 .dev_acquisition_start = dev_acquisition_start,
511 .dev_acquisition_stop = dev_acquisition_stop,