]>
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 <string.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | /* Length of expected payload for each token. */ | |
24 | static int token_payloads[][2] = { | |
25 | { TOKEN_WEIGHT_TIME_FAST, 0 }, | |
26 | { TOKEN_WEIGHT_TIME_SLOW, 0 }, | |
27 | { TOKEN_HOLD_MAX, 0 }, | |
28 | { TOKEN_HOLD_MIN, 0 }, | |
29 | { TOKEN_TIME, 3 }, | |
30 | { TOKEN_MEAS_RANGE_OVER, 0 }, | |
31 | { TOKEN_MEAS_RANGE_UNDER, 0 }, | |
32 | { TOKEN_STORE_FULL, 0 }, | |
33 | { TOKEN_RECORDING_ON, 0 }, | |
34 | { TOKEN_MEAS_WAS_READOUT, 1 }, | |
35 | { TOKEN_MEAS_WAS_BARGRAPH, 0 }, | |
36 | { TOKEN_MEASUREMENT, 2 }, | |
37 | { TOKEN_HOLD_NONE, 0 }, | |
38 | { TOKEN_BATTERY_LOW, 0 }, | |
39 | { TOKEN_MEAS_RANGE_OK, 0 }, | |
40 | { TOKEN_STORE_OK, 0 }, | |
41 | { TOKEN_RECORDING_OFF, 0 }, | |
42 | { TOKEN_WEIGHT_FREQ_A, 1 }, | |
43 | { TOKEN_WEIGHT_FREQ_C, 1 }, | |
44 | { TOKEN_BATTERY_OK, 0 }, | |
45 | { TOKEN_MEAS_RANGE_30_80, 0 }, | |
46 | { TOKEN_MEAS_RANGE_30_130, 0 }, | |
47 | { TOKEN_MEAS_RANGE_50_100, 0 }, | |
48 | { TOKEN_MEAS_RANGE_80_130, 0 }, | |
49 | }; | |
50 | ||
51 | static int find_token_payload_len(unsigned char c) | |
52 | { | |
53 | unsigned int i; | |
54 | ||
55 | for (i = 0; i < ARRAY_SIZE(token_payloads); i++) { | |
56 | if (token_payloads[i][0] == c) | |
57 | return token_payloads[i][1]; | |
58 | } | |
59 | ||
60 | return -1; | |
61 | } | |
62 | ||
63 | /* Process measurement or setting (0xa5 command). */ | |
64 | static void process_mset(const struct sr_dev_inst *sdi) | |
65 | { | |
66 | struct dev_context *devc; | |
67 | struct sr_datafeed_packet packet; | |
68 | struct sr_datafeed_analog analog; | |
69 | GString *dbg; | |
70 | float fvalue; | |
71 | int i; | |
72 | ||
73 | devc = sdi->priv; | |
74 | if (sr_log_loglevel_get() >= SR_LOG_SPEW) { | |
75 | dbg = g_string_sized_new(128); | |
76 | g_string_printf(dbg, "got command 0x%.2x token 0x%.2x", | |
77 | devc->cmd, devc->token); | |
78 | if (devc->buf_len) { | |
79 | g_string_append_printf(dbg, " payload"); | |
80 | for (i = 0; i < devc->buf_len; i++) | |
81 | g_string_append_printf(dbg, " %.2x", devc->buf[i]); | |
82 | } | |
83 | sr_spew("%s", dbg->str); | |
84 | g_string_free(dbg, TRUE); | |
85 | } | |
86 | ||
87 | switch(devc->token) { | |
88 | case TOKEN_WEIGHT_TIME_FAST: | |
89 | devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
90 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
91 | break; | |
92 | case TOKEN_WEIGHT_TIME_SLOW: | |
93 | devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
94 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
95 | break; | |
96 | case TOKEN_WEIGHT_FREQ_A: | |
97 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
98 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
99 | break; | |
100 | case TOKEN_WEIGHT_FREQ_C: | |
101 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
102 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
103 | break; | |
104 | case TOKEN_HOLD_MAX: | |
105 | devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX; | |
106 | devc->cur_mqflags &= ~SR_MQFLAG_MIN; | |
107 | break; | |
108 | case TOKEN_HOLD_MIN: | |
109 | devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN; | |
110 | devc->cur_mqflags &= ~SR_MQFLAG_MAX; | |
111 | break; | |
112 | case TOKEN_HOLD_NONE: | |
113 | devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD); | |
114 | break; | |
115 | case TOKEN_MEASUREMENT: | |
116 | fvalue = ((devc->buf[0] & 0xf0) >> 4) * 100; | |
117 | fvalue += (devc->buf[0] & 0x0f) * 10; | |
118 | fvalue += ((devc->buf[1] & 0xf0) >> 4); | |
119 | fvalue += (devc->buf[1] & 0x0f) / 10.0; | |
120 | devc->last_spl = fvalue; | |
121 | break; | |
122 | case TOKEN_MEAS_WAS_READOUT: | |
123 | case TOKEN_MEAS_WAS_BARGRAPH: | |
124 | if (devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN)) { | |
125 | if (devc->token == TOKEN_MEAS_WAS_BARGRAPH) { | |
126 | /* The device still sends bargraph measurements even | |
127 | * when in max/min hold mode. Suppress them here, unless | |
128 | * they're readout values. This duplicates the behavior | |
129 | * of the device display exactly. */ | |
130 | break; | |
131 | } | |
132 | } | |
133 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
134 | analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL; | |
135 | analog.mqflags = devc->cur_mqflags; | |
136 | analog.unit = SR_UNIT_DECIBEL_SPL; | |
137 | analog.channels = sdi->channels; | |
138 | analog.num_samples = 1; | |
139 | analog.data = &devc->last_spl; | |
140 | packet.type = SR_DF_ANALOG; | |
141 | packet.payload = &analog; | |
142 | sr_session_send(devc->cb_data, &packet); | |
143 | ||
144 | devc->num_samples++; | |
145 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) | |
146 | sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi, | |
147 | devc->cb_data); | |
148 | break; | |
149 | case TOKEN_RECORDING_ON: | |
150 | devc->recording = TRUE; | |
151 | break; | |
152 | case TOKEN_RECORDING_OFF: | |
153 | devc->recording = FALSE; | |
154 | break; | |
155 | case TOKEN_MEAS_RANGE_30_80: | |
156 | case TOKEN_MEAS_RANGE_30_130: | |
157 | case TOKEN_MEAS_RANGE_50_100: | |
158 | case TOKEN_MEAS_RANGE_80_130: | |
159 | devc->cur_meas_range = devc->token; | |
160 | break; | |
161 | case TOKEN_TIME: | |
162 | case TOKEN_STORE_OK: | |
163 | case TOKEN_STORE_FULL: | |
164 | case TOKEN_BATTERY_OK: | |
165 | case TOKEN_BATTERY_LOW: | |
166 | case TOKEN_MEAS_RANGE_OK: | |
167 | case TOKEN_MEAS_RANGE_OVER: | |
168 | case TOKEN_MEAS_RANGE_UNDER: | |
169 | /* Not useful, or not expressable in sigrok. */ | |
170 | break; | |
171 | } | |
172 | ||
173 | } | |
174 | ||
175 | static void send_data(const struct sr_dev_inst *sdi, unsigned char *data, | |
176 | uint64_t num_samples) | |
177 | { | |
178 | struct dev_context *devc; | |
179 | struct sr_datafeed_packet packet; | |
180 | struct sr_datafeed_analog analog; | |
181 | float fbuf[SAMPLES_PER_PACKET]; | |
182 | unsigned int i; | |
183 | ||
184 | devc = sdi->priv; | |
185 | ||
186 | for (i = 0; i < num_samples; i ++) { | |
187 | fbuf[i] = ((data[i * 2] & 0xf0) >> 4) * 100; | |
188 | fbuf[i] += (data[i * 2] & 0x0f) * 10; | |
189 | fbuf[i] += ((data[i * 2 + 1] & 0xf0) >> 4); | |
190 | fbuf[i] += (data[i * 2 + 1] & 0x0f) / 10.0; | |
191 | } | |
192 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
193 | analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL; | |
194 | analog.mqflags = devc->cur_mqflags; | |
195 | analog.unit = SR_UNIT_DECIBEL_SPL; | |
196 | analog.channels = sdi->channels; | |
197 | analog.num_samples = num_samples; | |
198 | analog.data = fbuf; | |
199 | packet.type = SR_DF_ANALOG; | |
200 | packet.payload = &analog; | |
201 | sr_session_send(devc->cb_data, &packet); | |
202 | ||
203 | devc->num_samples += analog.num_samples; | |
204 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) | |
205 | sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi, | |
206 | devc->cb_data); | |
207 | ||
208 | return; | |
209 | } | |
210 | ||
211 | static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c, | |
212 | int handle_packets) | |
213 | { | |
214 | struct dev_context *devc; | |
215 | struct sr_datafeed_packet packet; | |
216 | struct sr_datafeed_meta meta; | |
217 | struct sr_config *src; | |
218 | gint64 cur_time; | |
219 | int len; | |
220 | ||
221 | if (!(devc = sdi->priv)) | |
222 | return; | |
223 | ||
224 | if (c == 0xff) { | |
225 | /* Device is in hold mode */ | |
226 | devc->cur_mqflags |= SR_MQFLAG_HOLD; | |
227 | ||
228 | if (devc->hold_last_sent == 0) { | |
229 | /* First hold notification. */ | |
230 | devc->hold_last_sent = g_get_monotonic_time(); | |
231 | /* When the device leaves hold mode, it starts from scratch. */ | |
232 | devc->state = ST_INIT; | |
233 | } else { | |
234 | cur_time = g_get_monotonic_time(); | |
235 | if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) { | |
236 | /* Force the last measurement out again. */ | |
237 | devc->cmd = 0xa5; | |
238 | devc->token = TOKEN_MEAS_WAS_READOUT; | |
239 | if (handle_packets) | |
240 | process_mset(sdi); | |
241 | devc->hold_last_sent = cur_time; | |
242 | } | |
243 | } | |
244 | ||
245 | return; | |
246 | } | |
247 | devc->cur_mqflags &= ~SR_MQFLAG_HOLD; | |
248 | devc->hold_last_sent = 0; | |
249 | ||
250 | if (devc->state == ST_INIT) { | |
251 | if (c == 0xa5) { | |
252 | devc->cmd = c; | |
253 | devc->token = 0x00; | |
254 | devc->state = ST_GET_TOKEN; | |
255 | } else if (c == 0xbb) { | |
256 | devc->cmd = c; | |
257 | devc->buf_len = 0; | |
258 | devc->state = ST_GET_LOG_HEADER; | |
259 | sr_dbg("got command 0xbb"); | |
260 | } | |
261 | } else if (devc->state == ST_GET_TOKEN) { | |
262 | devc->token = c; | |
263 | devc->buf_len = 0; | |
264 | len = find_token_payload_len(devc->token); | |
265 | if (len == -1 || len > 0) { | |
266 | devc->buf_len = 0; | |
267 | devc->state = ST_GET_DATA; | |
268 | } else { | |
269 | if (handle_packets) | |
270 | process_mset(sdi); | |
271 | devc->state = ST_INIT; | |
272 | } | |
273 | } else if (devc->state == ST_GET_DATA) { | |
274 | len = find_token_payload_len(devc->token); | |
275 | if (len == -1) { | |
276 | /* We don't know this token. */ | |
277 | sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token); | |
278 | if (c == 0xa5 || c == 0xbb) { | |
279 | /* Looks like a new command however. */ | |
280 | if (handle_packets) | |
281 | process_mset(sdi); | |
282 | devc->state = ST_INIT; | |
283 | } else { | |
284 | devc->buf[devc->buf_len++] = c; | |
285 | if (devc->buf_len > BUF_SIZE) { | |
286 | /* Shouldn't happen, ignore. */ | |
287 | devc->state = ST_INIT; | |
288 | } | |
289 | } | |
290 | } else { | |
291 | devc->buf[devc->buf_len++] = c; | |
292 | if (devc->buf_len == len) { | |
293 | if (handle_packets) | |
294 | process_mset(sdi); | |
295 | devc->state = ST_INIT; | |
296 | } else if (devc->buf_len > BUF_SIZE) { | |
297 | /* Shouldn't happen, ignore. */ | |
298 | devc->state = ST_INIT; | |
299 | } | |
300 | } | |
301 | } else if (devc->state == ST_GET_LOG_HEADER) { | |
302 | sr_dbg("log header: 0x%.2x", c); | |
303 | if (devc->buf_len < 2) | |
304 | devc->buf[devc->buf_len++] = c; | |
305 | if (devc->buf_len == 2) { | |
306 | sr_dbg("Device says it has %d bytes stored.", | |
307 | ((devc->buf[0] << 8) + devc->buf[1]) - 100); | |
308 | devc->buf_len = 0; | |
309 | devc->state = ST_GET_LOG_RECORD_META; | |
310 | } | |
311 | } else if (devc->state == ST_GET_LOG_RECORD_META) { | |
312 | sr_dbg("log meta: 0x%.2x", c); | |
313 | if (c == RECORD_END) { | |
314 | devc->state = ST_INIT; | |
315 | /* Stop acquisition after transferring all stored | |
316 | * records. Otherwise the frontend would have no | |
317 | * way to tell where stored data ends and live | |
318 | * measurements begin. */ | |
319 | sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi, | |
320 | devc->cb_data); | |
321 | } else if (c == RECORD_DATA) { | |
322 | devc->buf_len = 0; | |
323 | devc->state = ST_GET_LOG_RECORD_DATA; | |
324 | } else { | |
325 | /* RECORD_DBA/RECORD_DBC + 7 bytes of metadata */ | |
326 | devc->buf[devc->buf_len++] = c; | |
327 | if (devc->buf_len < 8) | |
328 | /* Keep filling up the record header. */ | |
329 | return; | |
330 | if (devc->buf[0] == RECORD_DBA) | |
331 | devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
332 | else if (devc->buf[0] == RECORD_DBC) | |
333 | devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
334 | else { | |
335 | /* Shouldn't happen. */ | |
336 | sr_dbg("Unknown record token 0x%.2x", c); | |
337 | return; | |
338 | } | |
339 | packet.type = SR_DF_META; | |
340 | packet.payload = &meta; | |
341 | src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, | |
342 | g_variant_new_uint64(devc->buf[7] * 1000)); | |
343 | meta.config = g_slist_append(NULL, src); | |
344 | sr_session_send(devc->cb_data, &packet); | |
345 | g_free(src); | |
346 | devc->buf_len = 0; | |
347 | } | |
348 | } else if (devc->state == ST_GET_LOG_RECORD_DATA) { | |
349 | sr_dbg("log data: 0x%.2x", c); | |
350 | if (c == RECORD_DBA || c == RECORD_DBC || c == RECORD_DATA || c == RECORD_END) { | |
351 | /* Work around off-by-one bug in device firmware. This | |
352 | * happens only on the last record, i.e. before RECORD_END */ | |
353 | if (devc->buf_len & 1) | |
354 | devc->buf_len--; | |
355 | /* Done with this set of samples */ | |
356 | send_data(sdi, devc->buf, devc->buf_len / 2); | |
357 | devc->buf_len = 0; | |
358 | ||
359 | /* Process this meta marker in the right state. */ | |
360 | devc->state = ST_GET_LOG_RECORD_META; | |
361 | process_byte(sdi, c, handle_packets); | |
362 | } else { | |
363 | devc->buf[devc->buf_len++] = c; | |
364 | if (devc->buf_len == SAMPLES_PER_PACKET * 2) { | |
365 | send_data(sdi, devc->buf, devc->buf_len / 2); | |
366 | devc->buf_len = 0; | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
371 | } | |
372 | ||
373 | SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data) | |
374 | { | |
375 | const struct sr_dev_inst *sdi; | |
376 | struct dev_context *devc; | |
377 | struct sr_serial_dev_inst *serial; | |
378 | unsigned char c, cmd; | |
379 | ||
380 | (void)fd; | |
381 | ||
382 | if (!(sdi = cb_data)) | |
383 | return TRUE; | |
384 | ||
385 | devc = sdi->priv; | |
386 | serial = sdi->conn; | |
387 | if (revents == G_IO_IN) { | |
388 | if (serial_read(serial, &c, 1) != 1) | |
389 | return TRUE; | |
390 | process_byte(sdi, c, TRUE); | |
391 | ||
392 | if (devc->enable_data_source_memory) { | |
393 | if (devc->state == ST_GET_LOG_HEADER) { | |
394 | /* Memory transfer started. */ | |
395 | devc->enable_data_source_memory = FALSE; | |
396 | } else { | |
397 | /* Tell device to start transferring from memory. */ | |
398 | cmd = CMD_TRANSFER_MEMORY; | |
399 | serial_write(serial, &cmd, 1); | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
404 | return TRUE; | |
405 | } | |
406 | ||
407 | ||
408 | static int wait_for_token(const struct sr_dev_inst *sdi, int8_t *tokens, int timeout) | |
409 | { | |
410 | struct dev_context *devc; | |
411 | struct sr_serial_dev_inst *serial; | |
412 | gint64 start_time; | |
413 | int i; | |
414 | unsigned char c; | |
415 | ||
416 | serial = sdi->conn; | |
417 | devc = sdi->priv; | |
418 | devc->state = ST_INIT; | |
419 | start_time = g_get_monotonic_time() / 1000; | |
420 | while (TRUE) { | |
421 | if (serial_read(serial, &c, 1) != 1) | |
422 | /* Device might have gone away. */ | |
423 | return SR_ERR; | |
424 | process_byte(sdi, c, FALSE); | |
425 | if (devc->state != ST_INIT) | |
426 | /* Wait for a whole packet to get processed. */ | |
427 | continue; | |
428 | for (i = 0; tokens[i] != -1; i++) { | |
429 | if (devc->token == tokens[i]) { | |
430 | sr_spew("wait_for_token: got token 0x%.2x", devc->token); | |
431 | return SR_OK; | |
432 | } | |
433 | } | |
434 | if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout) | |
435 | return SR_ERR_TIMEOUT; | |
436 | } | |
437 | ||
438 | return SR_OK; | |
439 | } | |
440 | ||
441 | /* cmd is the command to send, tokens are the tokens that denote the state | |
442 | * which the command affects. The first token is the desired state. */ | |
443 | static int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd, | |
444 | int8_t *tokens, int timeout) | |
445 | { | |
446 | struct dev_context *devc; | |
447 | struct sr_serial_dev_inst *serial; | |
448 | ||
449 | serial = sdi->conn; | |
450 | devc = sdi->priv; | |
451 | ||
452 | /* The device doesn't respond to commands very well. The | |
453 | * only thing to do is wait for the token that will confirm | |
454 | * whether the command worked or not, and resend if needed. */ | |
455 | while (TRUE) { | |
456 | if (serial_write(serial, (const void *)&cmd, 1) != 1) | |
457 | return SR_ERR; | |
458 | if (wait_for_token(sdi, tokens, timeout) == SR_ERR) | |
459 | return SR_ERR; | |
460 | if (devc->token == tokens[0]) | |
461 | /* It worked. */ | |
462 | break; | |
463 | } | |
464 | ||
465 | return SR_OK; | |
466 | } | |
467 | ||
468 | SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi, | |
469 | int *state) | |
470 | { | |
471 | struct dev_context *devc; | |
472 | int8_t tokens[5]; | |
473 | ||
474 | devc = sdi->priv; | |
475 | if (devc->recording == -1) { | |
476 | /* Didn't pick up device state yet. */ | |
477 | tokens[0] = TOKEN_RECORDING_ON; | |
478 | tokens[1] = TOKEN_RECORDING_OFF; | |
479 | tokens[2] = -1; | |
480 | if (wait_for_token(sdi, tokens, 510) != SR_OK) | |
481 | return SR_ERR; | |
482 | } | |
483 | *state = devc->token == TOKEN_RECORDING_ON; | |
484 | ||
485 | return SR_OK; | |
486 | } | |
487 | ||
488 | SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi, | |
489 | gboolean state) | |
490 | { | |
491 | struct dev_context *devc; | |
492 | int ret; | |
493 | int8_t tokens[5]; | |
494 | ||
495 | devc = sdi->priv; | |
496 | ||
497 | /* The toggle below needs the desired state in first position. */ | |
498 | if (state) { | |
499 | tokens[0] = TOKEN_RECORDING_ON; | |
500 | tokens[1] = TOKEN_RECORDING_OFF; | |
501 | } else { | |
502 | tokens[0] = TOKEN_RECORDING_OFF; | |
503 | tokens[1] = TOKEN_RECORDING_ON; | |
504 | } | |
505 | tokens[2] = -1; | |
506 | ||
507 | if (devc->recording == -1) { | |
508 | /* Didn't pick up device state yet. */ | |
509 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
510 | return SR_ERR; | |
511 | if (devc->token == tokens[0]) | |
512 | /* Nothing to do. */ | |
513 | return SR_OK; | |
514 | } else if (devc->recording == state) | |
515 | /* Nothing to do. */ | |
516 | return SR_OK; | |
517 | ||
518 | /* Recording state notifications are sent at 2Hz, so allow just over | |
519 | * that, 510ms, for the state to come in. */ | |
520 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens, 510); | |
521 | ||
522 | return ret; | |
523 | } | |
524 | ||
525 | SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi) | |
526 | { | |
527 | struct dev_context *devc; | |
528 | int cur_setting; | |
529 | int8_t tokens[5]; | |
530 | ||
531 | devc = sdi->priv; | |
532 | ||
533 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
534 | if (cur_setting == 0) { | |
535 | /* Didn't pick up device state yet. */ | |
536 | tokens[0] = TOKEN_WEIGHT_FREQ_A; | |
537 | tokens[1] = TOKEN_WEIGHT_FREQ_C; | |
538 | tokens[2] = -1; | |
539 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
540 | return SR_ERR; | |
541 | if (devc->token == TOKEN_WEIGHT_FREQ_A) | |
542 | return SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
543 | else | |
544 | return SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
545 | } else | |
546 | return cur_setting; | |
547 | } | |
548 | ||
549 | SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw) | |
550 | { | |
551 | struct dev_context *devc; | |
552 | int cur_setting, ret; | |
553 | int8_t tokens[5]; | |
554 | ||
555 | devc = sdi->priv; | |
556 | ||
557 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
558 | if (cur_setting == freqw) | |
559 | /* Already set to this frequency weighting. */ | |
560 | return SR_OK; | |
561 | ||
562 | /* The toggle below needs the desired state in first position. */ | |
563 | if (freqw == SR_MQFLAG_SPL_FREQ_WEIGHT_A) { | |
564 | tokens[0] = TOKEN_WEIGHT_FREQ_A; | |
565 | tokens[1] = TOKEN_WEIGHT_FREQ_C; | |
566 | } else { | |
567 | tokens[0] = TOKEN_WEIGHT_FREQ_C; | |
568 | tokens[1] = TOKEN_WEIGHT_FREQ_A; | |
569 | } | |
570 | tokens[2] = -1; | |
571 | ||
572 | if (cur_setting == 0) { | |
573 | /* Didn't pick up device state yet. */ | |
574 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
575 | return SR_ERR; | |
576 | if (devc->token == tokens[0]) | |
577 | /* Nothing to do. */ | |
578 | return SR_OK; | |
579 | } | |
580 | ||
581 | /* 10ms timeout seems to work best for this. */ | |
582 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10); | |
583 | ||
584 | return ret; | |
585 | } | |
586 | ||
587 | SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi) | |
588 | { | |
589 | struct dev_context *devc; | |
590 | int cur_setting; | |
591 | int8_t tokens[5]; | |
592 | ||
593 | devc = sdi->priv; | |
594 | ||
595 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
596 | if (cur_setting == 0) { | |
597 | /* Didn't pick up device state yet. */ | |
598 | tokens[0] = TOKEN_WEIGHT_TIME_FAST; | |
599 | tokens[1] = TOKEN_WEIGHT_TIME_SLOW; | |
600 | tokens[2] = -1; | |
601 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
602 | return SR_ERR; | |
603 | if (devc->token == TOKEN_WEIGHT_TIME_FAST) | |
604 | return SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
605 | else | |
606 | return SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
607 | } else | |
608 | return cur_setting; | |
609 | } | |
610 | ||
611 | SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew) | |
612 | { | |
613 | struct dev_context *devc; | |
614 | int cur_setting, ret; | |
615 | int8_t tokens[5]; | |
616 | ||
617 | devc = sdi->priv; | |
618 | ||
619 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
620 | if (cur_setting == timew) | |
621 | /* Already set to this time weighting. */ | |
622 | return SR_OK; | |
623 | ||
624 | /* The toggle below needs the desired state in first position. */ | |
625 | if (timew == SR_MQFLAG_SPL_TIME_WEIGHT_F) { | |
626 | tokens[0] = TOKEN_WEIGHT_TIME_FAST; | |
627 | tokens[1] = TOKEN_WEIGHT_TIME_SLOW; | |
628 | } else { | |
629 | tokens[0] = TOKEN_WEIGHT_TIME_SLOW; | |
630 | tokens[1] = TOKEN_WEIGHT_TIME_FAST; | |
631 | } | |
632 | tokens[2] = -1; | |
633 | ||
634 | if (cur_setting == 0) { | |
635 | /* Didn't pick up device state yet. */ | |
636 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
637 | return SR_ERR; | |
638 | if (devc->token == tokens[0]) | |
639 | /* Nothing to do. */ | |
640 | return SR_OK; | |
641 | } | |
642 | ||
643 | /* 51ms timeout seems to work best for this. */ | |
644 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51); | |
645 | ||
646 | return ret; | |
647 | } | |
648 | ||
649 | SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi, | |
650 | gboolean *holdmode) | |
651 | { | |
652 | struct dev_context *devc; | |
653 | int8_t tokens[5]; | |
654 | ||
655 | devc = sdi->priv; | |
656 | ||
657 | if (devc->cur_mqflags == 0) { | |
658 | tokens[0] = TOKEN_HOLD_MAX; | |
659 | tokens[1] = TOKEN_HOLD_MIN; | |
660 | tokens[2] = TOKEN_HOLD_NONE; | |
661 | tokens[3] = -1; | |
662 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
663 | return SR_ERR; | |
664 | if (devc->token == TOKEN_HOLD_MAX) | |
665 | devc->cur_mqflags = SR_MQFLAG_MAX; | |
666 | else if (devc->token == TOKEN_HOLD_MIN) | |
667 | devc->cur_mqflags = SR_MQFLAG_MIN; | |
668 | } | |
669 | *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
670 | ||
671 | return SR_OK; | |
672 | } | |
673 | ||
674 | SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode) | |
675 | { | |
676 | struct dev_context *devc; | |
677 | int cur_setting, ret; | |
678 | int8_t tokens[5]; | |
679 | ||
680 | devc = sdi->priv; | |
681 | ||
682 | /* The toggle below needs the desired state in first position. */ | |
683 | if (holdmode == SR_MQFLAG_MAX) { | |
684 | tokens[0] = TOKEN_HOLD_MAX; | |
685 | tokens[1] = TOKEN_HOLD_MIN; | |
686 | tokens[2] = TOKEN_HOLD_NONE; | |
687 | } else if (holdmode == SR_MQFLAG_MIN) { | |
688 | tokens[0] = TOKEN_HOLD_MIN; | |
689 | tokens[1] = TOKEN_HOLD_MAX; | |
690 | tokens[2] = TOKEN_HOLD_NONE; | |
691 | } else { | |
692 | tokens[0] = TOKEN_HOLD_NONE; | |
693 | tokens[1] = TOKEN_HOLD_MAX; | |
694 | tokens[2] = TOKEN_HOLD_MIN; | |
695 | } | |
696 | tokens[3] = -1; | |
697 | ||
698 | if (devc->cur_mqflags == 0) { | |
699 | /* Didn't pick up device state yet. */ | |
700 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
701 | return SR_ERR; | |
702 | if (devc->token == tokens[0]) | |
703 | /* Nothing to do. */ | |
704 | return SR_OK; | |
705 | } else { | |
706 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
707 | if (cur_setting == holdmode) | |
708 | /* Already set correctly. */ | |
709 | return SR_OK; | |
710 | } | |
711 | ||
712 | /* 51ms timeout seems to work best for this. */ | |
713 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51); | |
714 | ||
715 | return ret; | |
716 | } | |
717 | ||
718 | SR_PRIV int cem_dt_885x_meas_range_get(const struct sr_dev_inst *sdi, | |
719 | uint64_t *low, uint64_t *high) | |
720 | { | |
721 | struct dev_context *devc; | |
722 | int8_t tokens[5]; | |
723 | ||
724 | devc = sdi->priv; | |
725 | if (devc->cur_meas_range == 0) { | |
726 | tokens[0] = TOKEN_MEAS_RANGE_30_130; | |
727 | tokens[1] = TOKEN_MEAS_RANGE_30_80; | |
728 | tokens[2] = TOKEN_MEAS_RANGE_50_100; | |
729 | tokens[3] = TOKEN_MEAS_RANGE_80_130; | |
730 | tokens[4] = -1; | |
731 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
732 | return SR_ERR; | |
733 | devc->cur_meas_range = devc->token; | |
734 | } | |
735 | ||
736 | switch (devc->cur_meas_range) { | |
737 | case TOKEN_MEAS_RANGE_30_130: | |
738 | *low = 30; | |
739 | *high = 130; | |
740 | break; | |
741 | case TOKEN_MEAS_RANGE_30_80: | |
742 | *low = 30; | |
743 | *high = 80; | |
744 | break; | |
745 | case TOKEN_MEAS_RANGE_50_100: | |
746 | *low = 50; | |
747 | *high = 100; | |
748 | break; | |
749 | case TOKEN_MEAS_RANGE_80_130: | |
750 | *low = 80; | |
751 | *high = 130; | |
752 | break; | |
753 | default: | |
754 | return SR_ERR; | |
755 | } | |
756 | ||
757 | return SR_OK; | |
758 | } | |
759 | ||
760 | SR_PRIV int cem_dt_885x_meas_range_set(const struct sr_dev_inst *sdi, | |
761 | uint64_t low, uint64_t high) | |
762 | { | |
763 | struct dev_context *devc; | |
764 | int ret; | |
765 | int8_t token, tokens[6]; | |
766 | ||
767 | devc = sdi->priv; | |
768 | if (low == 30 && high == 130) | |
769 | token = TOKEN_MEAS_RANGE_30_130; | |
770 | else if (low == 30 && high == 80) | |
771 | token = TOKEN_MEAS_RANGE_30_80; | |
772 | else if (low == 50 && high == 100) | |
773 | token = TOKEN_MEAS_RANGE_50_100; | |
774 | else if (low == 80 && high == 130) | |
775 | token = TOKEN_MEAS_RANGE_80_130; | |
776 | else | |
777 | return SR_ERR; | |
778 | ||
779 | sr_dbg("want 0x%.2x", token); | |
780 | /* The toggle below needs the desired state in first position. */ | |
781 | tokens[0] = token; | |
782 | tokens[1] = TOKEN_MEAS_RANGE_30_130; | |
783 | tokens[2] = TOKEN_MEAS_RANGE_30_80; | |
784 | tokens[3] = TOKEN_MEAS_RANGE_50_100; | |
785 | tokens[4] = TOKEN_MEAS_RANGE_80_130; | |
786 | tokens[5] = -1; | |
787 | ||
788 | if (devc->cur_meas_range == 0) { | |
789 | /* 110ms should be enough for two of these announcements */ | |
790 | if (wait_for_token(sdi, tokens, 110) != SR_OK) | |
791 | return SR_ERR; | |
792 | devc->cur_meas_range = devc->token; | |
793 | } | |
794 | ||
795 | if (devc->cur_meas_range == token) | |
796 | /* Already set to this range. */ | |
797 | return SR_OK; | |
798 | ||
799 | /* For measurement range, it works best to ignore announcements of the | |
800 | * current setting and keep resending the toggle quickly. */ | |
801 | tokens[1] = -1; | |
802 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_MEAS_RANGE, tokens, 11); | |
803 | ||
804 | return ret; | |
805 | } | |
806 | ||
807 | SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi) | |
808 | { | |
809 | struct sr_serial_dev_inst *serial; | |
810 | char c, cmd; | |
811 | ||
812 | serial = sdi->conn; | |
813 | ||
814 | /* Reopen the port in non-blocking mode, so we can properly | |
815 | * detect when the device stops communicating. */ | |
816 | serial_close(serial); | |
817 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
818 | return SR_ERR; | |
819 | ||
820 | cmd = CMD_TOGGLE_POWER_OFF; | |
821 | while (TRUE) { | |
822 | serial_flush(serial); | |
823 | if (serial_write(serial, (const void *)&cmd, 1) != 1) | |
824 | return SR_ERR; | |
825 | /* It never takes more than 23ms for the next token to arrive. */ | |
826 | g_usleep(25 * 1000); | |
827 | if (serial_read(serial, &c, 1) != 1) | |
828 | /* Device is no longer responding. Good! */ | |
829 | break; | |
830 | } | |
831 | ||
832 | /* In case the user manually turns on the device again, reset | |
833 | * the port back to blocking. */ | |
834 | serial_close(serial); | |
835 | serial_open(serial, SERIAL_RDWR); | |
836 | ||
837 | return SR_OK; | |
838 | } |