+static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
+{
+ struct dev_context *devc;
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_analog analog;
+
+ devc = sdi->priv;
+
+ memset(&analog, 0, sizeof(struct sr_datafeed_analog));
+ analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
+ analog.mqflags = devc->mqflags;
+ analog.unit = SR_UNIT_DECIBEL_SPL;
+ analog.channels = sdi->channels;
+ analog.num_samples = buf_len;
+ analog.data = buf;
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+ sr_session_send(devc->cb_data, &packet);
+
+}
+
+SR_PRIV void kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
+{
+ struct dev_context *devc;
+ struct sr_dev_inst *sdi;
+ float fvalue[64];
+ int packet_has_error, num_samples, i;
+
+ sdi = transfer->user_data;
+ devc = sdi->priv;
+
+ packet_has_error = FALSE;
+ switch (transfer->status) {
+ case LIBUSB_TRANSFER_NO_DEVICE:
+ /* USB device was unplugged. */
+ sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
+ devc->cb_data);
+ return;
+ case LIBUSB_TRANSFER_COMPLETED:
+ case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
+ break;
+ default:
+ packet_has_error = TRUE;
+ break;
+ }
+
+ if (packet_has_error)
+ return;
+
+ if (devc->state == LIVE_SPL_WAIT) {
+ if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
+ sr_dbg("Received invalid SPL packet.");
+ } else {
+ fvalue[0] = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
+ send_data(sdi, fvalue, 1);
+ devc->num_samples++;
+ if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
+ sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
+ devc->cb_data);
+ } else {
+ /* let USB event handler fire off another
+ * request when the time is right. */
+ devc->state = LIVE_SPL_IDLE;
+ }
+ }
+ } else if (devc->state == LOG_DATA_WAIT) {
+ if (transfer->actual_length < 1 || !(transfer->actual_length & 0x01)) {
+ sr_dbg("Received invalid stored SPL packet.");
+ } else {
+ num_samples = (transfer->actual_length - 1) / 2;
+ for (i = 0; i < num_samples; i++) {
+ fvalue[i] = transfer->buffer[1 + i * 2] << 8;
+ fvalue[i] += transfer->buffer[1 + i * 2 + 1];
+ fvalue[i] /= 10.0;
+ }
+ send_data(sdi, fvalue, 1);
+ devc->num_samples += num_samples;
+ if (devc->num_samples >= devc->stored_samples) {
+ sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
+ devc->cb_data);
+ } else {
+ /* let USB event handler fire off another
+ * request when the time is right. */
+ devc->state = LOG_DATA_IDLE;
+ }
+ }
+ }
+
+}
+