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