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