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