]> sigrok.org Git - libsigrok.git/blame - src/hardware/kecheng-kc-330b/protocol.c
windows: Fix various compiler warnings.
[libsigrok.git] / src / 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 23extern struct sr_dev_driver kecheng_kc_330b_driver_info;
df035528
BV
24extern const uint64_t kecheng_kc_330b_sample_intervals[][2];
25
bc7be4a9 26SR_PRIV int kecheng_kc_330b_handle_events(int fd, int revents, void *cb_data)
ed759a08 27{
4f840ce9 28 struct sr_dev_driver *di;
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
bc7be4a9
BV
43 sdi = cb_data;
44 devc = sdi->priv;
df035528 45 usb = sdi->conn;
4f840ce9
ML
46 di = sdi->driver;
47 drvc = di->priv;
bc7be4a9 48
df035528
BV
49 memset(&tv, 0, sizeof(struct timeval));
50 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
51 NULL);
52
53 if (sdi->status == SR_ST_STOPPING) {
54 libusb_free_transfer(devc->xfer);
102f1239 55 usb_source_remove(sdi->session, drvc->sr_ctx);
df035528
BV
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) {
85ed4ab3 63 /* Request samples at the interval rate. */
df035528
BV
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) {
85ed4ab3
BV
68 buf[0] = CMD_GET_LIVE_SPL;
69 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
df035528
BV
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,
74 devc->cb_data);
75 return TRUE;
76 }
77 libusb_submit_transfer(devc->xfer);
78 devc->last_live_request = now;
79 devc->state = LIVE_SPL_WAIT;
80 }
85ed4ab3
BV
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)
87 buf[3] = 63;
88 else
89 /* Last chunk. */
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,
96 devc->cb_data);
97 return TRUE;
98 }
99 libusb_submit_transfer(devc->xfer);
100 devc->state = LIVE_SPL_WAIT;
df035528 101 }
bc7be4a9
BV
102
103 return TRUE;
104}
105
df035528
BV
106static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
107{
108 struct dev_context *devc;
109 struct sr_datafeed_packet packet;
110 struct sr_datafeed_analog analog;
111
112 devc = sdi->priv;
113
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;
ba7dd8bb 118 analog.channels = sdi->channels;
df035528
BV
119 analog.num_samples = buf_len;
120 analog.data = buf;
121 packet.type = SR_DF_ANALOG;
122 packet.payload = &analog;
123 sr_session_send(devc->cb_data, &packet);
124
125}
126
55462b8b 127SR_PRIV void LIBUSB_CALL kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
df035528
BV
128{
129 struct dev_context *devc;
130 struct sr_dev_inst *sdi;
85ed4ab3
BV
131 float fvalue[64];
132 int packet_has_error, num_samples, i;
df035528
BV
133
134 sdi = transfer->user_data;
135 devc = sdi->priv;
136
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,
142 devc->cb_data);
143 return;
144 case LIBUSB_TRANSFER_COMPLETED:
145 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
146 break;
147 default:
148 packet_has_error = TRUE;
149 break;
150 }
151
85ed4ab3
BV
152 if (packet_has_error)
153 return;
df035528 154
85ed4ab3
BV
155 if (devc->state == LIVE_SPL_WAIT) {
156 if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
157 sr_dbg("Received invalid SPL packet.");
158 } else {
159 fvalue[0] = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
160 send_data(sdi, fvalue, 1);
161 devc->num_samples++;
162 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
163 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
164 devc->cb_data);
165 } else {
df035528
BV
166 /* let USB event handler fire off another
167 * request when the time is right. */
168 devc->state = LIVE_SPL_IDLE;
169 }
170 }
85ed4ab3
BV
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.");
174 } else {
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];
179 fvalue[i] /= 10.0;
180 }
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,
185 devc->cb_data);
186 } else {
187 /* let USB event handler fire off another
188 * request when the time is right. */
189 devc->state = LOG_DATA_IDLE;
190 }
191 }
df035528
BV
192 }
193
df035528
BV
194}
195
bc7be4a9
BV
196SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
197{
ed759a08 198 struct dev_context *devc;
bc7be4a9
BV
199 struct sr_usb_dev_inst *usb;
200 int len, ret;
201 unsigned char buf[7];
ed759a08 202
bc7be4a9 203 sr_dbg("Configuring device.");
ed759a08 204
bc7be4a9
BV
205 usb = sdi->conn;
206 devc = sdi->priv;
ed759a08 207
bc7be4a9
BV
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));
218 return SR_ERR;
ed759a08
BV
219 }
220
bc7be4a9
BV
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));
225 return SR_ERR;
226 }
227 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
228 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
229 return SR_ERR;
230 }
231
85ed4ab3
BV
232 devc->config_dirty = FALSE;
233
bc7be4a9
BV
234 return SR_OK;
235}
236
237SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
238{
239 struct sr_usb_dev_inst *usb;
240 GDateTime *dt;
241 int len, ret;
242 unsigned char buf[7];
243
244 sr_dbg("Setting device date/time.");
245
246 usb = sdi->conn;
247
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));
260 return SR_ERR;
261 }
262
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));
266 return SR_ERR;
267 }
268 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
269 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
270 return SR_ERR;
271 }
272
f336618c
BV
273 return SR_OK;
274}
275
276SR_PRIV int kecheng_kc_330b_status_get(const struct sr_dev_inst *sdi,
277 int *status)
278{
279 struct sr_usb_dev_inst *usb;
280 int len, ret;
281 unsigned char buf;
282
283 sr_dbg("Getting device status.");
284
285 usb = sdi->conn;
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));
290 return SR_ERR;
291 }
292
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));
296 return SR_ERR;
297 }
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);
301 return SR_ERR;
302 }
bc7be4a9 303
f336618c
BV
304 if (buf & 0x20)
305 *status = DEVICE_INACTIVE;
306 else
307 *status = DEVICE_ACTIVE;
bc7be4a9
BV
308
309 return SR_OK;
310}
311
85ed4ab3
BV
312SR_PRIV int kecheng_kc_330b_log_info_get(const struct sr_dev_inst *sdi,
313 unsigned char *buf)
314{
315 struct sr_usb_dev_inst *usb;
316 int len, ret;
317
318 sr_dbg("Getting logging info.");
319
320 usb = sdi->conn;
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));
325 return SR_ERR;
326 }
327
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));
331 return SR_ERR;
332 }
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]);
335 return SR_ERR;
336 }
337
338 return SR_OK;
339}