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/>.
24 extern struct sr_dev_driver 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 sr_dev_driver *di;
30 struct drv_context *drvc;
31 struct dev_context *devc;
32 struct sr_dev_inst *sdi;
33 struct sr_usb_dev_inst *usb;
35 const uint64_t *intv_entry;
49 memset(&tv, 0, sizeof(struct timeval));
50 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
53 if (sdi->status == SR_ST_STOPPING) {
54 libusb_free_transfer(devc->xfer);
55 usb_source_remove(sdi->session, drvc->sr_ctx);
56 std_session_send_df_end(sdi, LOG_PREFIX);
57 sdi->status = SR_ST_ACTIVE;
61 if (devc->state == LIVE_SPL_IDLE) {
62 /* Request samples at the interval rate. */
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 buf[0] = CMD_GET_LIVE_SPL;
68 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 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(sdi);
75 libusb_submit_transfer(devc->xfer);
76 devc->last_live_request = now;
77 devc->state = LIVE_SPL_WAIT;
79 } else if (devc->state == LIVE_SPL_IDLE) {
80 buf[0] = CMD_GET_LOG_DATA;
81 offset = devc->num_samples / 63;
82 buf[1] = (offset >> 8) & 0xff;
83 buf[2] = offset & 0xff;
84 if (devc->stored_samples - devc->num_samples > 63)
88 buf[3] = devc->stored_samples - devc->num_samples;
89 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 4, &len, 5);
90 if (ret != 0 || len != 4) {
91 sr_dbg("Failed to request next chunk: %s",
92 libusb_error_name(ret));
93 sdi->driver->dev_acquisition_stop(sdi);
96 libusb_submit_transfer(devc->xfer);
97 devc->state = LIVE_SPL_WAIT;
103 static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
105 struct dev_context *devc;
106 struct sr_datafeed_packet packet;
107 struct sr_datafeed_analog_old analog;
111 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
112 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
113 analog.mqflags = devc->mqflags;
114 analog.unit = SR_UNIT_DECIBEL_SPL;
115 analog.channels = sdi->channels;
116 analog.num_samples = buf_len;
118 packet.type = SR_DF_ANALOG_OLD;
119 packet.payload = &analog;
120 sr_session_send(sdi, &packet);
123 SR_PRIV void LIBUSB_CALL kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
125 struct dev_context *devc;
126 struct sr_dev_inst *sdi;
128 int packet_has_error, num_samples, i;
130 sdi = transfer->user_data;
133 packet_has_error = FALSE;
134 switch (transfer->status) {
135 case LIBUSB_TRANSFER_NO_DEVICE:
136 /* USB device was unplugged. */
137 sdi->driver->dev_acquisition_stop(sdi);
139 case LIBUSB_TRANSFER_COMPLETED:
140 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
143 packet_has_error = TRUE;
147 if (packet_has_error)
150 if (devc->state == LIVE_SPL_WAIT) {
151 if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
152 sr_dbg("Received invalid SPL packet.");
154 fvalue[0] = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
155 send_data(sdi, fvalue, 1);
157 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
158 sdi->driver->dev_acquisition_stop(sdi);
160 /* let USB event handler fire off another
161 * request when the time is right. */
162 devc->state = LIVE_SPL_IDLE;
165 } else if (devc->state == LOG_DATA_WAIT) {
166 if (transfer->actual_length < 1 || !(transfer->actual_length & 0x01)) {
167 sr_dbg("Received invalid stored SPL packet.");
169 num_samples = (transfer->actual_length - 1) / 2;
170 for (i = 0; i < num_samples; i++) {
171 fvalue[i] = transfer->buffer[1 + i * 2] << 8;
172 fvalue[i] += transfer->buffer[1 + i * 2 + 1];
175 send_data(sdi, fvalue, 1);
176 devc->num_samples += num_samples;
177 if (devc->num_samples >= devc->stored_samples) {
178 sdi->driver->dev_acquisition_stop(sdi);
180 /* let USB event handler fire off another
181 * request when the time is right. */
182 devc->state = LOG_DATA_IDLE;
189 SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
191 struct dev_context *devc;
192 struct sr_usb_dev_inst *usb;
194 unsigned char buf[7];
196 sr_dbg("Configuring device.");
201 buf[0] = CMD_CONFIGURE;
202 buf[1] = devc->sample_interval;
203 buf[2] = devc->alarm_low;
204 buf[3] = devc->alarm_high;
205 buf[4] = devc->mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F ? 0 : 1;
206 buf[5] = devc->mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A ? 0 : 1;
207 buf[6] = devc->data_source;
208 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
209 if (ret != 0 || len != 7) {
210 sr_dbg("Failed to configure device: %s", libusb_error_name(ret));
214 /* The configure command ack takes about 32ms to come in. */
215 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 40);
216 if (ret != 0 || len != 1) {
217 sr_dbg("Failed to configure device (no ack): %s", libusb_error_name(ret));
220 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
221 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
225 devc->config_dirty = FALSE;
230 SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
232 struct sr_usb_dev_inst *usb;
235 unsigned char buf[7];
237 sr_dbg("Setting device date/time.");
241 dt = g_date_time_new_now_local();
242 buf[0] = CMD_SET_DATE_TIME;
243 buf[1] = g_date_time_get_year(dt) - 2000;
244 buf[2] = g_date_time_get_month(dt);
245 buf[3] = g_date_time_get_day_of_month(dt);
246 buf[4] = g_date_time_get_hour(dt);
247 buf[5] = g_date_time_get_minute(dt);
248 buf[6] = g_date_time_get_second(dt);
249 g_date_time_unref(dt);
250 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
251 if (ret != 0 || len != 7) {
252 sr_dbg("Failed to set date/time: %s", libusb_error_name(ret));
256 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 10);
257 if (ret != 0 || len != 1) {
258 sr_dbg("Failed to set date/time (no ack): %s", libusb_error_name(ret));
261 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
262 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
269 SR_PRIV int kecheng_kc_330b_status_get(const struct sr_dev_inst *sdi,
272 struct sr_usb_dev_inst *usb;
276 sr_dbg("Getting device status.");
279 buf = CMD_GET_STATUS;
280 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &buf, 1, &len, 5);
281 if (ret != 0 || len != 1) {
282 sr_dbg("Failed to get status: %s", libusb_error_name(ret));
286 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, &buf, 1, &len, 10);
287 if (ret != 0 || len != 1) {
288 sr_dbg("Failed to get status (no ack): %s", libusb_error_name(ret));
291 /* Need either 0x84 or 0xa4. */
292 if (buf != (CMD_GET_STATUS | 0x80) && buf != (CMD_GET_STATUS | 0xa0)) {
293 sr_dbg("Failed to get status: invalid response 0x%2.x", buf);
298 *status = DEVICE_INACTIVE;
300 *status = DEVICE_ACTIVE;
305 SR_PRIV int kecheng_kc_330b_log_info_get(const struct sr_dev_inst *sdi,
308 struct sr_usb_dev_inst *usb;
311 sr_dbg("Getting logging info.");
314 buf[0] = CMD_GET_LOG_INFO;
315 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
316 if (ret != 0 || len != 1) {
317 sr_dbg("Failed to get status: %s", libusb_error_name(ret));
321 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 9, &len, 10);
322 if (ret != 0 || len != 9) {
323 sr_dbg("Failed to get status (no ack): %s", libusb_error_name(ret));
326 if (buf[0] != (CMD_GET_LOG_INFO | 0x80) || buf[1] > 6) {
327 sr_dbg("Failed to get log info: invalid response 0x%2.x", buf[0]);