]> sigrok.org Git - libsigrok.git/blame - hardware/kecheng-kc-330b/protocol.c
kecheng-kc-330b: Live SPL acquisition
[libsigrok.git] / hardware / kecheng-kc-330b / protocol.c
CommitLineData
ed759a08
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
df035528 20#include <string.h>
ed759a08
BV
21#include "protocol.h"
22
df035528
BV
23extern struct sr_dev_driver kecheng_kc_330b_driver_info;
24static struct sr_dev_driver *di = &kecheng_kc_330b_driver_info;
25extern const uint64_t kecheng_kc_330b_sample_intervals[][2];
26
bc7be4a9 27SR_PRIV int kecheng_kc_330b_handle_events(int fd, int revents, void *cb_data)
ed759a08 28{
df035528 29 struct drv_context *drvc;
bc7be4a9 30 struct dev_context *devc;
df035528
BV
31 struct sr_datafeed_packet packet;
32 struct sr_dev_inst *sdi;
33 struct sr_usb_dev_inst *usb;
bc7be4a9 34 struct timeval tv;
df035528
BV
35 const uint64_t *intv_entry;
36 gint64 now, interval;
37 int len, ret, i;
38 unsigned char cmd;
bc7be4a9 39
ed759a08 40 (void)fd;
bc7be4a9 41 (void)revents;
ed759a08 42
df035528 43 drvc = di->priv;
bc7be4a9
BV
44 sdi = cb_data;
45 devc = sdi->priv;
df035528 46 usb = sdi->conn;
bc7be4a9 47
df035528
BV
48 memset(&tv, 0, sizeof(struct timeval));
49 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
50 NULL);
51
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;
59 return TRUE;
60 }
61
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,
73 devc->cb_data);
74 return TRUE;
75 }
76 libusb_submit_transfer(devc->xfer);
77 devc->last_live_request = now;
78 devc->state = LIVE_SPL_WAIT;
79 }
80 }
bc7be4a9
BV
81
82 return TRUE;
83}
84
df035528
BV
85static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
86{
87 struct dev_context *devc;
88 struct sr_datafeed_packet packet;
89 struct sr_datafeed_analog analog;
90
91 devc = sdi->priv;
92
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;
99 analog.data = buf;
100 packet.type = SR_DF_ANALOG;
101 packet.payload = &analog;
102 sr_session_send(devc->cb_data, &packet);
103
104}
105
106SR_PRIV void kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
107{
108 struct dev_context *devc;
109 struct sr_dev_inst *sdi;
110 float fvalue;
111 int packet_has_error;
112
113 sdi = transfer->user_data;
114 devc = sdi->priv;
115
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,
121 devc->cb_data);
122 return;
123 case LIBUSB_TRANSFER_COMPLETED:
124 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
125 break;
126 default:
127 packet_has_error = TRUE;
128 break;
129 }
130
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.");
135 } else {
136 fvalue = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
137 send_data(sdi, &fvalue, 1);
138 devc->num_samples++;
139 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
140 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
141 devc->cb_data);
142
143 /* let USB event handler fire off another
144 * request when the time is right. */
145 devc->state = LIVE_SPL_IDLE;
146 }
147 }
148 }
149
150
151}
152
bc7be4a9
BV
153SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
154{
ed759a08 155 struct dev_context *devc;
bc7be4a9
BV
156 struct sr_usb_dev_inst *usb;
157 int len, ret;
158 unsigned char buf[7];
ed759a08 159
bc7be4a9 160 sr_dbg("Configuring device.");
ed759a08 161
bc7be4a9
BV
162 usb = sdi->conn;
163 devc = sdi->priv;
ed759a08 164
bc7be4a9
BV
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));
175 return SR_ERR;
ed759a08
BV
176 }
177
bc7be4a9
BV
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));
182 return SR_ERR;
183 }
184 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
185 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
186 return SR_ERR;
187 }
188
189 return SR_OK;
190}
191
192SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
193{
194 struct sr_usb_dev_inst *usb;
195 GDateTime *dt;
196 int len, ret;
197 unsigned char buf[7];
198
199 sr_dbg("Setting device date/time.");
200
201 usb = sdi->conn;
202
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));
215 return SR_ERR;
216 }
217
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));
221 return SR_ERR;
222 }
223 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
224 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
225 return SR_ERR;
226 }
227
228
229
230 return SR_OK;
231}
232
233SR_PRIV int kecheng_kc_330b_recording_get(const struct sr_dev_inst *sdi,
234 gboolean *tmp)
235{
236
237 return SR_OK;
ed759a08 238}