]> sigrok.org Git - libsigrok.git/blame - src/hardware/kecheng-kc-330b/protocol.c
dev_acquisition_{start,stop}(): Drop duplicate 'cb_data' parameter.
[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
6ec6c43b 20#include <config.h>
df035528 21#include <string.h>
ed759a08
BV
22#include "protocol.h"
23
df035528 24extern struct sr_dev_driver kecheng_kc_330b_driver_info;
df035528
BV
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{
4f840ce9 29 struct sr_dev_driver *di;
df035528 30 struct drv_context *drvc;
bc7be4a9 31 struct dev_context *devc;
df035528
BV
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 46 di = sdi->driver;
41812aca 47 drvc = di->context;
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);
695dc859 56 std_session_send_df_end(sdi, LOG_PREFIX);
df035528
BV
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));
695dc859 72 sdi->driver->dev_acquisition_stop(sdi);
df035528
BV
73 return TRUE;
74 }
75 libusb_submit_transfer(devc->xfer);
76 devc->last_live_request = now;
77 devc->state = LIVE_SPL_WAIT;
78 }
85ed4ab3
BV
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)
85 buf[3] = 63;
86 else
87 /* Last chunk. */
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));
695dc859 93 sdi->driver->dev_acquisition_stop(sdi);
85ed4ab3
BV
94 return TRUE;
95 }
96 libusb_submit_transfer(devc->xfer);
97 devc->state = LIVE_SPL_WAIT;
df035528 98 }
bc7be4a9
BV
99
100 return TRUE;
101}
102
df035528
BV
103static void send_data(const struct sr_dev_inst *sdi, void *buf, unsigned int buf_len)
104{
105 struct dev_context *devc;
106 struct sr_datafeed_packet packet;
5faebab2 107 struct sr_datafeed_analog_old analog;
df035528
BV
108
109 devc = sdi->priv;
110
5faebab2 111 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
df035528
BV
112 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
113 analog.mqflags = devc->mqflags;
114 analog.unit = SR_UNIT_DECIBEL_SPL;
ba7dd8bb 115 analog.channels = sdi->channels;
df035528
BV
116 analog.num_samples = buf_len;
117 analog.data = buf;
5faebab2 118 packet.type = SR_DF_ANALOG_OLD;
df035528 119 packet.payload = &analog;
695dc859 120 sr_session_send(sdi, &packet);
df035528
BV
121}
122
55462b8b 123SR_PRIV void LIBUSB_CALL kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
df035528
BV
124{
125 struct dev_context *devc;
126 struct sr_dev_inst *sdi;
85ed4ab3
BV
127 float fvalue[64];
128 int packet_has_error, num_samples, i;
df035528
BV
129
130 sdi = transfer->user_data;
131 devc = sdi->priv;
132
133 packet_has_error = FALSE;
134 switch (transfer->status) {
135 case LIBUSB_TRANSFER_NO_DEVICE:
136 /* USB device was unplugged. */
695dc859 137 sdi->driver->dev_acquisition_stop(sdi);
df035528
BV
138 return;
139 case LIBUSB_TRANSFER_COMPLETED:
140 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
141 break;
142 default:
143 packet_has_error = TRUE;
144 break;
145 }
146
85ed4ab3
BV
147 if (packet_has_error)
148 return;
df035528 149
85ed4ab3
BV
150 if (devc->state == LIVE_SPL_WAIT) {
151 if (transfer->actual_length != 3 || transfer->buffer[0] != 0x88) {
152 sr_dbg("Received invalid SPL packet.");
153 } else {
154 fvalue[0] = ((transfer->buffer[1] << 8) + transfer->buffer[2]) / 10.0;
155 send_data(sdi, fvalue, 1);
156 devc->num_samples++;
157 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
695dc859 158 sdi->driver->dev_acquisition_stop(sdi);
85ed4ab3 159 } else {
df035528
BV
160 /* let USB event handler fire off another
161 * request when the time is right. */
162 devc->state = LIVE_SPL_IDLE;
163 }
164 }
85ed4ab3
BV
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.");
168 } else {
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];
173 fvalue[i] /= 10.0;
174 }
175 send_data(sdi, fvalue, 1);
176 devc->num_samples += num_samples;
177 if (devc->num_samples >= devc->stored_samples) {
695dc859 178 sdi->driver->dev_acquisition_stop(sdi);
85ed4ab3
BV
179 } else {
180 /* let USB event handler fire off another
181 * request when the time is right. */
182 devc->state = LOG_DATA_IDLE;
183 }
184 }
df035528
BV
185 }
186
df035528
BV
187}
188
bc7be4a9
BV
189SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
190{
ed759a08 191 struct dev_context *devc;
bc7be4a9
BV
192 struct sr_usb_dev_inst *usb;
193 int len, ret;
194 unsigned char buf[7];
ed759a08 195
bc7be4a9 196 sr_dbg("Configuring device.");
ed759a08 197
bc7be4a9
BV
198 usb = sdi->conn;
199 devc = sdi->priv;
ed759a08 200
bc7be4a9
BV
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));
211 return SR_ERR;
ed759a08
BV
212 }
213
bc7be4a9
BV
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));
218 return SR_ERR;
219 }
220 if (buf[0] != (CMD_CONFIGURE | 0x80)) {
221 sr_dbg("Failed to configure device: invalid response 0x%2.x", buf[0]);
222 return SR_ERR;
223 }
224
85ed4ab3
BV
225 devc->config_dirty = FALSE;
226
bc7be4a9
BV
227 return SR_OK;
228}
229
230SR_PRIV int kecheng_kc_330b_set_date_time(struct sr_dev_inst *sdi)
231{
232 struct sr_usb_dev_inst *usb;
233 GDateTime *dt;
234 int len, ret;
235 unsigned char buf[7];
236
237 sr_dbg("Setting device date/time.");
238
239 usb = sdi->conn;
240
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));
253 return SR_ERR;
254 }
255
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));
259 return SR_ERR;
260 }
261 if (buf[0] != (CMD_SET_DATE_TIME | 0x80)) {
262 sr_dbg("Failed to set date/time: invalid response 0x%2.x", buf[0]);
263 return SR_ERR;
264 }
265
f336618c
BV
266 return SR_OK;
267}
268
269SR_PRIV int kecheng_kc_330b_status_get(const struct sr_dev_inst *sdi,
270 int *status)
271{
272 struct sr_usb_dev_inst *usb;
273 int len, ret;
274 unsigned char buf;
275
276 sr_dbg("Getting device status.");
277
278 usb = sdi->conn;
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));
283 return SR_ERR;
284 }
285
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));
289 return SR_ERR;
290 }
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);
294 return SR_ERR;
295 }
bc7be4a9 296
f336618c
BV
297 if (buf & 0x20)
298 *status = DEVICE_INACTIVE;
299 else
300 *status = DEVICE_ACTIVE;
bc7be4a9
BV
301
302 return SR_OK;
303}
304
85ed4ab3
BV
305SR_PRIV int kecheng_kc_330b_log_info_get(const struct sr_dev_inst *sdi,
306 unsigned char *buf)
307{
308 struct sr_usb_dev_inst *usb;
309 int len, ret;
310
311 sr_dbg("Getting logging info.");
312
313 usb = sdi->conn;
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));
318 return SR_ERR;
319 }
320
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));
324 return SR_ERR;
325 }
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]);
328 return SR_ERR;
329 }
330
331 return SR_OK;
332}