]> sigrok.org Git - libsigrok.git/blame - hardware/kecheng-kc-330b/protocol.c
Replace 'probe' with 'channel' in most places.
[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;
6c60facc 37 int offset, len, ret;
85ed4ab3 38 unsigned char buf[4];
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);
6c60facc 54 usb_source_remove(drvc->sr_ctx);
df035528
BV
55 packet.type = SR_DF_END;
56 sr_session_send(cb_data, &packet);
57 sdi->status = SR_ST_ACTIVE;
58 return TRUE;
59 }
60
61 if (devc->state == LIVE_SPL_IDLE) {
85ed4ab3 62 /* Request samples at the interval rate. */
df035528
BV
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) {
85ed4ab3
BV
67 buf[0] = CMD_GET_LIVE_SPL;
68 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
df035528
BV
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 }
85ed4ab3
BV
80 } else if (devc->state == LIVE_SPL_IDLE) {
81 buf[0] = CMD_GET_LOG_DATA;
82 offset = devc->num_samples / 63;
83 buf[1] = (offset >> 8) & 0xff;
84 buf[2] = offset & 0xff;
85 if (devc->stored_samples - devc->num_samples > 63)
86 buf[3] = 63;
87 else
88 /* Last chunk. */
89 buf[3] = devc->stored_samples - devc->num_samples;
90 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 4, &len, 5);
91 if (ret != 0 || len != 4) {
92 sr_dbg("Failed to request next chunk: %s",
93 libusb_error_name(ret));
94 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
95 devc->cb_data);
96 return TRUE;
97 }
98 libusb_submit_transfer(devc->xfer);
99 devc->state = LIVE_SPL_WAIT;
df035528 100 }
bc7be4a9
BV
101
102 return TRUE;
103}
104
df035528
BV
105static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
106{
107 struct dev_context *devc;
108 struct sr_datafeed_packet packet;
109 struct sr_datafeed_analog analog;
110
111 devc = sdi->priv;
112
113 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
114 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
115 analog.mqflags = devc->mqflags;
116 analog.unit = SR_UNIT_DECIBEL_SPL;
ba7dd8bb 117 analog.channels = sdi->channels;
df035528
BV
118 analog.num_samples = buf_len;
119 analog.data = buf;
120 packet.type = SR_DF_ANALOG;
121 packet.payload = &analog;
122 sr_session_send(devc->cb_data, &packet);
123
124}
125
126SR_PRIV void kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
127{
128 struct dev_context *devc;
129 struct sr_dev_inst *sdi;
85ed4ab3
BV
130 float fvalue[64];
131 int packet_has_error, num_samples, i;
df035528
BV
132
133 sdi = transfer->user_data;
134 devc = sdi->priv;
135
136 packet_has_error = FALSE;
137 switch (transfer->status) {
138 case LIBUSB_TRANSFER_NO_DEVICE:
139 /* USB device was unplugged. */
140 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
141 devc->cb_data);
142 return;
143 case LIBUSB_TRANSFER_COMPLETED:
144 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
145 break;
146 default:
147 packet_has_error = TRUE;
148 break;
149 }
150
85ed4ab3
BV
151 if (packet_has_error)
152 return;
df035528 153
85ed4ab3
BV
154 if (devc->state == LIVE_SPL_WAIT) {
155 if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
156 sr_dbg("Received invalid SPL packet.");
157 } else {
158 fvalue[0] = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
159 send_data(sdi, fvalue, 1);
160 devc->num_samples++;
161 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
162 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
163 devc->cb_data);
164 } else {
df035528
BV
165 /* let USB event handler fire off another
166 * request when the time is right. */
167 devc->state = LIVE_SPL_IDLE;
168 }
169 }
85ed4ab3
BV
170 } else if (devc->state == LOG_DATA_WAIT) {
171 if (transfer->actual_length < 1 || !(transfer->actual_length & 0x01)) {
172 sr_dbg("Received invalid stored SPL packet.");
173 } else {
174 num_samples = (transfer->actual_length - 1) / 2;
175 for (i = 0; i < num_samples; i++) {
176 fvalue[i] = transfer->buffer[1 + i * 2] << 8;
177 fvalue[i] += transfer->buffer[1 + i * 2 + 1];
178 fvalue[i] /= 10.0;
179 }
180 send_data(sdi, fvalue, 1);
181 devc->num_samples += num_samples;
182 if (devc->num_samples >= devc->stored_samples) {
183 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
184 devc->cb_data);
185 } else {
186 /* let USB event handler fire off another
187 * request when the time is right. */
188 devc->state = LOG_DATA_IDLE;
189 }
190 }
df035528
BV
191 }
192
df035528
BV
193}
194
bc7be4a9
BV
195SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
196{
ed759a08 197 struct dev_context *devc;
bc7be4a9
BV
198 struct sr_usb_dev_inst *usb;
199 int len, ret;
200 unsigned char buf[7];
ed759a08 201
bc7be4a9 202 sr_dbg("Configuring device.");
ed759a08 203
bc7be4a9
BV
204 usb = sdi->conn;
205 devc = sdi->priv;
ed759a08 206
bc7be4a9
BV
207 buf[0] = CMD_CONFIGURE;
208 buf[1] = devc->sample_interval;
209 buf[2] = devc->alarm_low;
210 buf[3] = devc->alarm_high;
211 buf[4] = devc->mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F ? 0 : 1;
212 buf[5] = devc->mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A ? 0 : 1;
213 buf[6] = devc->data_source;
214 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
215 if (ret != 0 || len != 7) {
216 sr_dbg("Failed to configure device: %s", libusb_error_name(ret));
217 return SR_ERR;
ed759a08
BV
218 }
219
bc7be4a9
BV
220 /* The configure command ack takes about 32ms to come in. */
221 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 40);
222 if (ret != 0 || len != 1) {
223 sr_dbg("Failed to configure device (no ack): %s", libusb_error_name(ret));
224 return SR_ERR;
225 }
226 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
227 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
228 return SR_ERR;
229 }
230
85ed4ab3
BV
231 devc->config_dirty = FALSE;
232
bc7be4a9
BV
233 return SR_OK;
234}
235
236SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
237{
238 struct sr_usb_dev_inst *usb;
239 GDateTime *dt;
240 int len, ret;
241 unsigned char buf[7];
242
243 sr_dbg("Setting device date/time.");
244
245 usb = sdi->conn;
246
247 dt = g_date_time_new_now_local();
248 buf[0] = CMD_SET_DATE_TIME;
249 buf[1] = g_date_time_get_year(dt) - 2000;
250 buf[2] = g_date_time_get_month(dt);
251 buf[3] = g_date_time_get_day_of_month(dt);
252 buf[4] = g_date_time_get_hour(dt);
253 buf[5] = g_date_time_get_minute(dt);
254 buf[6] = g_date_time_get_second(dt);
255 g_date_time_unref(dt);
256 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 7, &len, 5);
257 if (ret != 0 || len != 7) {
258 sr_dbg("Failed to set date/time: %s", libusb_error_name(ret));
259 return SR_ERR;
260 }
261
262 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 1, &len, 10);
263 if (ret != 0 || len != 1) {
264 sr_dbg("Failed to set date/time (no ack): %s", libusb_error_name(ret));
265 return SR_ERR;
266 }
267 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
268 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
269 return SR_ERR;
270 }
271
f336618c
BV
272 return SR_OK;
273}
274
275SR_PRIV int kecheng_kc_330b_status_get(const struct sr_dev_inst *sdi,
276 int *status)
277{
278 struct sr_usb_dev_inst *usb;
279 int len, ret;
280 unsigned char buf;
281
282 sr_dbg("Getting device status.");
283
284 usb = sdi->conn;
285 buf = CMD_GET_STATUS;
286 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &buf, 1, &len, 5);
287 if (ret != 0 || len != 1) {
288 sr_dbg("Failed to get status: %s", libusb_error_name(ret));
289 return SR_ERR;
290 }
291
292 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, &buf, 1, &len, 10);
293 if (ret != 0 || len != 1) {
294 sr_dbg("Failed to get status (no ack): %s", libusb_error_name(ret));
295 return SR_ERR;
296 }
297 /* Need either 0x84 or 0xa4. */
298 if (buf != (CMD_GET_STATUS | 0x80) && buf != (CMD_GET_STATUS | 0xa0)) {
299 sr_dbg("Failed to get status: invalid response 0x%2.x", buf);
300 return SR_ERR;
301 }
bc7be4a9 302
f336618c
BV
303 if (buf & 0x20)
304 *status = DEVICE_INACTIVE;
305 else
306 *status = DEVICE_ACTIVE;
bc7be4a9
BV
307
308 return SR_OK;
309}
310
85ed4ab3
BV
311SR_PRIV int kecheng_kc_330b_log_info_get(const struct sr_dev_inst *sdi,
312 unsigned char *buf)
313{
314 struct sr_usb_dev_inst *usb;
315 int len, ret;
316
317 sr_dbg("Getting logging info.");
318
319 usb = sdi->conn;
320 buf[0] = CMD_GET_LOG_INFO;
321 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
322 if (ret != 0 || len != 1) {
323 sr_dbg("Failed to get status: %s", libusb_error_name(ret));
324 return SR_ERR;
325 }
326
327 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 9, &len, 10);
328 if (ret != 0 || len != 9) {
329 sr_dbg("Failed to get status (no ack): %s", libusb_error_name(ret));
330 return SR_ERR;
331 }
332 if (buf[0] != (CMD_GET_LOG_INFO | 0x80) || buf[1] > 6) {
333 sr_dbg("Failed to get log info: invalid response 0x%2.x", buf[0]);
334 return SR_ERR;
335 }
336
337 return SR_OK;
338}