]> sigrok.org Git - libsigrok.git/blob - src/hardware/kecheng-kc-330b/protocol.c
e1c8da17b910b4e25b703c923760591939976fa9
[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_dev_inst *sdi;
33         struct sr_usb_dev_inst *usb;
34         struct timeval tv;
35         const uint64_t *intv_entry;
36         gint64 now, interval;
37         int offset, len, ret;
38         unsigned char buf[4];
39
40         (void)fd;
41         (void)revents;
42
43         sdi = cb_data;
44         devc = sdi->priv;
45         usb = sdi->conn;
46         di = sdi->driver;
47         drvc = di->context;
48
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);
55                 usb_source_remove(sdi->session, drvc->sr_ctx);
56                 std_session_send_df_end(sdi, LOG_PREFIX);
57                 sdi->status = SR_ST_ACTIVE;
58                 return TRUE;
59         }
60
61         if (devc->state == LIVE_SPL_IDLE) {
62                 /* Request samples at the interval rate. */
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) {
67                         buf[0] = CMD_GET_LIVE_SPL;
68                         ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, buf, 1, &len, 5);
69                         if (ret != 0 || len != 1) {
70                                 sr_dbg("Failed to request new acquisition: %s",
71                                                 libusb_error_name(ret));
72                                 sdi->driver->dev_acquisition_stop(sdi);
73                                 return TRUE;
74                         }
75                         libusb_submit_transfer(devc->xfer);
76                         devc->last_live_request = now;
77                         devc->state = LIVE_SPL_WAIT;
78                 }
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));
93                         sdi->driver->dev_acquisition_stop(sdi);
94                         return TRUE;
95                 }
96                 libusb_submit_transfer(devc->xfer);
97                 devc->state = LIVE_SPL_WAIT;
98         }
99
100         return TRUE;
101 }
102
103 static 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;
107         struct sr_datafeed_analog_old analog;
108
109         devc = sdi->priv;
110
111         memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
112         analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
113         analog.mqflags = devc->mqflags;
114         analog.unit = SR_UNIT_DECIBEL_SPL;
115         analog.channels = sdi->channels;
116         analog.num_samples = buf_len;
117         analog.data = buf;
118         packet.type = SR_DF_ANALOG_OLD;
119         packet.payload = &analog;
120         sr_session_send(sdi, &packet);
121 }
122
123 SR_PRIV void LIBUSB_CALL kecheng_kc_330b_receive_transfer(struct libusb_transfer *transfer)
124 {
125         struct dev_context *devc;
126         struct sr_dev_inst *sdi;
127         float fvalue[64];
128         int packet_has_error, num_samples, i;
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. */
137                 sdi->driver->dev_acquisition_stop(sdi);
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
147         if (packet_has_error)
148                 return;
149
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) {
158                                 sdi->driver->dev_acquisition_stop(sdi);
159                         } else {
160                                 /* let USB event handler fire off another
161                                  * request when the time is right. */
162                                 devc->state = LIVE_SPL_IDLE;
163                         }
164                 }
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) {
178                                 sdi->driver->dev_acquisition_stop(sdi);
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                 }
185         }
186
187 }
188
189 SR_PRIV int kecheng_kc_330b_configure(const struct sr_dev_inst *sdi)
190 {
191         struct dev_context *devc;
192         struct sr_usb_dev_inst *usb;
193         int len, ret;
194         unsigned char buf[7];
195
196         sr_dbg("Configuring device.");
197
198         usb = sdi->conn;
199         devc = sdi->priv;
200
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;
212         }
213
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
225         devc->config_dirty = FALSE;
226
227         return SR_OK;
228 }
229
230 SR_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
266         return SR_OK;
267 }
268
269 SR_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         }
296
297         if (buf & 0x20)
298                 *status = DEVICE_INACTIVE;
299         else
300                 *status = DEVICE_ACTIVE;
301
302         return SR_OK;
303 }
304
305 SR_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 }