]> sigrok.org Git - libsigrok.git/blame_incremental - 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
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
20#include <config.h>
21#include <string.h>
22#include "protocol.h"
23
24extern const uint64_t kecheng_kc_330b_sample_intervals[][2];
25
26SR_PRIV int kecheng_kc_330b_handle_events(int fd, int revents, void *cb_data)
27{
28 struct sr_dev_driver *di;
29 struct drv_context *drvc;
30 struct dev_context *devc;
31 struct sr_dev_inst *sdi;
32 struct sr_usb_dev_inst *usb;
33 struct timeval tv;
34 const uint64_t *intv_entry;
35 gint64 now, interval;
36 int offset, len, ret;
37 unsigned char buf[4];
38
39 (void)fd;
40 (void)revents;
41
42 sdi = cb_data;
43 devc = sdi->priv;
44 usb = sdi->conn;
45 di = sdi->driver;
46 drvc = di->context;
47
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 usb_source_remove(sdi->session, drvc->sr_ctx);
55 std_session_send_df_end(sdi);
56 sdi->status = SR_ST_ACTIVE;
57 return TRUE;
58 }
59
60 if (devc->state == LIVE_SPL_IDLE) {
61 /* Request samples at the interval rate. */
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) {
66 buf[0] = CMD_GET_LIVE_SPL;
67 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
68 if (ret != 0 || len != 1) {
69 sr_dbg("Failed to request new acquisition: %s",
70 libusb_error_name(ret));
71 sr_dev_acquisition_stop(sdi);
72 return TRUE;
73 }
74 libusb_submit_transfer(devc->xfer);
75 devc->last_live_request = now;
76 devc->state = LIVE_SPL_WAIT;
77 }
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));
92 sr_dev_acquisition_stop(sdi);
93 return TRUE;
94 }
95 libusb_submit_transfer(devc->xfer);
96 devc->state = LIVE_SPL_WAIT;
97 }
98
99 return TRUE;
100}
101
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;
106 struct sr_datafeed_analog analog;
107 struct sr_analog_encoding encoding;
108 struct sr_analog_meaning meaning;
109 struct sr_analog_spec spec;
110
111 devc = sdi->priv;
112
113 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
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;
118 analog.num_samples = buf_len;
119 analog.data = buf;
120 packet.type = SR_DF_ANALOG;
121 packet.payload = &analog;
122 sr_session_send(sdi, &packet);
123}
124
125SR_PRIV void LIBUSB_CALL kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
126{
127 struct dev_context *devc;
128 struct sr_dev_inst *sdi;
129 float fvalue[64];
130 int packet_has_error, num_samples, i;
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. */
139 sr_dev_acquisition_stop(sdi);
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
149 if (packet_has_error)
150 return;
151
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) {
160 sr_dev_acquisition_stop(sdi);
161 } else {
162 /* let USB event handler fire off another
163 * request when the time is right. */
164 devc->state = LIVE_SPL_IDLE;
165 }
166 }
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) {
180 sr_dev_acquisition_stop(sdi);
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 }
187 }
188
189}
190
191SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
192{
193 struct dev_context *devc;
194 struct sr_usb_dev_inst *usb;
195 int len, ret;
196 unsigned char buf[7];
197
198 sr_dbg("Configuring device.");
199
200 usb = sdi->conn;
201 devc = sdi->priv;
202
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;
214 }
215
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
227 devc->config_dirty = FALSE;
228
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
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 }
298
299 if (buf & 0x20)
300 *status = DEVICE_INACTIVE;
301 else
302 *status = DEVICE_ACTIVE;
303
304 return SR_OK;
305}
306
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}