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;
37 int offset, len, ret, i;
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 /* Request samples at the interval rate. */
64 now = g_get_monotonic_time() / 1000;
65 intv_entry = kecheng_kc_330b_sample_intervals[devc->sample_interval];
66 interval = intv_entry[0] * 1000 / intv_entry[1];
67 if (now - devc->last_live_request > interval) {
68 buf[0] = CMD_GET_LIVE_SPL;
69 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
70 if (ret != 0 || len != 1) {
71 sr_dbg("Failed to request new acquisition: %s",
72 libusb_error_name(ret));
73 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
77 libusb_submit_transfer(devc->xfer);
78 devc->last_live_request = now;
79 devc->state = LIVE_SPL_WAIT;
81 } else if (devc->state == LIVE_SPL_IDLE) {
82 buf[0] = CMD_GET_LOG_DATA;
83 offset = devc->num_samples / 63;
84 buf[1] = (offset >> 8) & 0xff;
85 buf[2] = offset & 0xff;
86 if (devc->stored_samples - devc->num_samples > 63)
90 buf[3] = devc->stored_samples - devc->num_samples;
91 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 4, &len, 5);
92 if (ret != 0 || len != 4) {
93 sr_dbg("Failed to request next chunk: %s",
94 libusb_error_name(ret));
95 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
99 libusb_submit_transfer(devc->xfer);
100 devc->state = LIVE_SPL_WAIT;
106 static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
108 struct dev_context *devc;
109 struct sr_datafeed_packet packet;
110 struct sr_datafeed_analog analog;
114 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
115 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
116 analog.mqflags = devc->mqflags;
117 analog.unit = SR_UNIT_DECIBEL_SPL;
118 analog.probes = sdi->probes;
119 analog.num_samples = buf_len;
121 packet.type = SR_DF_ANALOG;
122 packet.payload = &analog;
123 sr_session_send(devc->cb_data, &packet);
127 SR_PRIV void kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
129 struct dev_context *devc;
130 struct sr_dev_inst *sdi;
132 int packet_has_error, num_samples, i;
134 sdi = transfer->user_data;
137 packet_has_error = FALSE;
138 switch (transfer->status) {
139 case LIBUSB_TRANSFER_NO_DEVICE:
140 /* USB device was unplugged. */
141 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
144 case LIBUSB_TRANSFER_COMPLETED:
145 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
148 packet_has_error = TRUE;
152 if (packet_has_error)
155 if (devc->state == LIVE_SPL_WAIT) {
156 if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
157 sr_dbg("Received invalid SPL packet.");
159 fvalue[0] = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
160 send_data(sdi, fvalue, 1);
162 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
163 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
166 /* let USB event handler fire off another
167 * request when the time is right. */
168 devc->state = LIVE_SPL_IDLE;
171 } else if (devc->state == LOG_DATA_WAIT) {
172 if (transfer->actual_length < 1 || !(transfer->actual_length & 0x01)) {
173 sr_dbg("Received invalid stored SPL packet.");
175 num_samples = (transfer->actual_length - 1) / 2;
176 for (i = 0; i < num_samples; i++) {
177 fvalue[i] = transfer->buffer[1 + i * 2] << 8;
178 fvalue[i] += transfer->buffer[1 + i * 2 + 1];
181 send_data(sdi, fvalue, 1);
182 devc->num_samples += num_samples;
183 if (devc->num_samples >= devc->stored_samples) {
184 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
187 /* let USB event handler fire off another
188 * request when the time is right. */
189 devc->state = LOG_DATA_IDLE;
196 SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
198 struct dev_context *devc;
199 struct sr_usb_dev_inst *usb;
201 unsigned char buf[7];
203 sr_dbg("Configuring device.");
208 buf[0] = CMD_CONFIGURE;
209 buf[1] = devc->sample_interval;
210 buf[2] = devc->alarm_low;
211 buf[3] = devc->alarm_high;
212 buf[4] = devc->mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F ? 0 : 1;
213 buf[5] = devc->mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A ? 0 : 1;
214 buf[6] = devc->data_source;
215 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
216 if (ret != 0 || len != 7) {
217 sr_dbg("Failed to configure device: %s", libusb_error_name(ret));
221 /* The configure command ack takes about 32ms to come in. */
222 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 40);
223 if (ret != 0 || len != 1) {
224 sr_dbg("Failed to configure device (no ack): %s", libusb_error_name(ret));
227 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
228 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
232 devc->config_dirty = FALSE;
237 SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
239 struct sr_usb_dev_inst *usb;
242 unsigned char buf[7];
244 sr_dbg("Setting device date/time.");
248 dt = g_date_time_new_now_local();
249 buf[0] = CMD_SET_DATE_TIME;
250 buf[1] = g_date_time_get_year(dt) - 2000;
251 buf[2] = g_date_time_get_month(dt);
252 buf[3] = g_date_time_get_day_of_month(dt);
253 buf[4] = g_date_time_get_hour(dt);
254 buf[5] = g_date_time_get_minute(dt);
255 buf[6] = g_date_time_get_second(dt);
256 g_date_time_unref(dt);
257 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
258 if (ret != 0 || len != 7) {
259 sr_dbg("Failed to set date/time: %s", libusb_error_name(ret));
263 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 10);
264 if (ret != 0 || len != 1) {
265 sr_dbg("Failed to set date/time (no ack): %s", libusb_error_name(ret));
268 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
269 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
276 SR_PRIV int kecheng_kc_330b_status_get(const struct sr_dev_inst *sdi,
279 struct sr_usb_dev_inst *usb;
283 sr_dbg("Getting device status.");
286 buf = CMD_GET_STATUS;
287 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &buf, 1, &len, 5);
288 if (ret != 0 || len != 1) {
289 sr_dbg("Failed to get status: %s", libusb_error_name(ret));
293 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, &buf, 1, &len, 10);
294 if (ret != 0 || len != 1) {
295 sr_dbg("Failed to get status (no ack): %s", libusb_error_name(ret));
298 /* Need either 0x84 or 0xa4. */
299 if (buf != (CMD_GET_STATUS | 0x80) && buf != (CMD_GET_STATUS | 0xa0)) {
300 sr_dbg("Failed to get status: invalid response 0x%2.x", buf);
305 *status = DEVICE_INACTIVE;
307 *status = DEVICE_ACTIVE;
312 SR_PRIV int kecheng_kc_330b_log_info_get(const struct sr_dev_inst *sdi,
315 struct sr_usb_dev_inst *usb;
318 sr_dbg("Getting logging info.");
321 buf[0] = CMD_GET_LOG_INFO;
322 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
323 if (ret != 0 || len != 1) {
324 sr_dbg("Failed to get status: %s", libusb_error_name(ret));
328 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 9, &len, 10);
329 if (ret != 0 || len != 9) {
330 sr_dbg("Failed to get status (no ack): %s", libusb_error_name(ret));
333 if (buf[0] != (CMD_GET_LOG_INFO | 0x80) || buf[1] > 6) {
334 sr_dbg("Failed to get log info: invalid response 0x%2.x", buf[0]);
341 SR_PRIV int kecheng_kc_330b_log_date_time_get(const struct sr_dev_inst *sdi,