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