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 extern struct sr_dev_driver kecheng_kc_330b_driver_info;
24 static struct sr_dev_driver *di = &kecheng_kc_330b_driver_info;
25 extern const uint64_t kecheng_kc_330b_sample_intervals[][2];
27 SR_PRIV int kecheng_kc_330b_handle_events(int fd, int revents, void *cb_data)
29 struct drv_context *drvc;
30 struct dev_context *devc;
31 struct sr_datafeed_packet packet;
32 struct sr_dev_inst *sdi;
33 struct sr_usb_dev_inst *usb;
35 const uint64_t *intv_entry;
48 memset(&tv, 0, sizeof(struct timeval));
49 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
52 if (sdi->status == SR_ST_STOPPING) {
53 libusb_free_transfer(devc->xfer);
54 for (i = 0; devc->usbfd[i] != -1; i++)
55 sr_source_remove(devc->usbfd[i]);
56 packet.type = SR_DF_END;
57 sr_session_send(cb_data, &packet);
58 sdi->status = SR_ST_ACTIVE;
62 if (devc->state == LIVE_SPL_IDLE) {
63 now = g_get_monotonic_time() / 1000;
64 intv_entry = kecheng_kc_330b_sample_intervals[devc->sample_interval];
65 interval = intv_entry[0] * 1000 / intv_entry[1];
66 if (now - devc->last_live_request > interval) {
67 cmd = CMD_GET_LIVE_SPL;
68 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5);
69 if (ret != 0 || len != 1) {
70 sr_dbg("Failed to request new acquisition: %s",
71 libusb_error_name(ret));
72 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
76 libusb_submit_transfer(devc->xfer);
77 devc->last_live_request = now;
78 devc->state = LIVE_SPL_WAIT;
85 static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
87 struct dev_context *devc;
88 struct sr_datafeed_packet packet;
89 struct sr_datafeed_analog analog;
93 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
94 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
95 analog.mqflags = devc->mqflags;
96 analog.unit = SR_UNIT_DECIBEL_SPL;
97 analog.probes = sdi->probes;
98 analog.num_samples = buf_len;
100 packet.type = SR_DF_ANALOG;
101 packet.payload = &analog;
102 sr_session_send(devc->cb_data, &packet);
106 SR_PRIV void kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
108 struct dev_context *devc;
109 struct sr_dev_inst *sdi;
111 int packet_has_error;
113 sdi = transfer->user_data;
116 packet_has_error = FALSE;
117 switch (transfer->status) {
118 case LIBUSB_TRANSFER_NO_DEVICE:
119 /* USB device was unplugged. */
120 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
123 case LIBUSB_TRANSFER_COMPLETED:
124 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
127 packet_has_error = TRUE;
131 if (!packet_has_error) {
132 if (devc->state == LIVE_SPL_WAIT) {
133 if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
134 sr_dbg("Received invalid SPL packet.");
136 fvalue = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
137 send_data(sdi, &fvalue, 1);
139 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
140 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
143 /* let USB event handler fire off another
144 * request when the time is right. */
145 devc->state = LIVE_SPL_IDLE;
153 SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
155 struct dev_context *devc;
156 struct sr_usb_dev_inst *usb;
158 unsigned char buf[7];
160 sr_dbg("Configuring device.");
165 buf[0] = CMD_CONFIGURE;
166 buf[1] = devc->sample_interval;
167 buf[2] = devc->alarm_low;
168 buf[3] = devc->alarm_high;
169 buf[4] = devc->mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F ? 0 : 1;
170 buf[5] = devc->mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A ? 0 : 1;
171 buf[6] = devc->data_source;
172 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
173 if (ret != 0 || len != 7) {
174 sr_dbg("Failed to configure device: %s", libusb_error_name(ret));
178 /* The configure command ack takes about 32ms to come in. */
179 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 40);
180 if (ret != 0 || len != 1) {
181 sr_dbg("Failed to configure device (no ack): %s", libusb_error_name(ret));
184 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
185 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
192 SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
194 struct sr_usb_dev_inst *usb;
197 unsigned char buf[7];
199 sr_dbg("Setting device date/time.");
203 dt = g_date_time_new_now_local();
204 buf[0] = CMD_SET_DATE_TIME;
205 buf[1] = g_date_time_get_year(dt) - 2000;
206 buf[2] = g_date_time_get_month(dt);
207 buf[3] = g_date_time_get_day_of_month(dt);
208 buf[4] = g_date_time_get_hour(dt);
209 buf[5] = g_date_time_get_minute(dt);
210 buf[6] = g_date_time_get_second(dt);
211 g_date_time_unref(dt);
212 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
213 if (ret != 0 || len != 7) {
214 sr_dbg("Failed to set date/time: %s", libusb_error_name(ret));
218 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 10);
219 if (ret != 0 || len != 1) {
220 sr_dbg("Failed to set date/time (no ack): %s", libusb_error_name(ret));
223 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
224 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
233 SR_PRIV int kecheng_kc_330b_recording_get(const struct sr_dev_inst *sdi,