]>
Commit | Line | Data |
---|---|---|
8fa9368e 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 | ||
e37c4b39 | 20 | #include <string.h> |
8fa9368e BV |
21 | #include "protocol.h" |
22 | ||
e37c4b39 BV |
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) | |
8fa9368e | 52 | { |
e37c4b39 | 53 | unsigned int i; |
8fa9368e | 54 | |
e37c4b39 BV |
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 | { | |
8fa9368e | 66 | struct dev_context *devc; |
e37c4b39 BV |
67 | struct sr_datafeed_packet packet; |
68 | struct sr_datafeed_analog analog; | |
69 | GString *dbg; | |
70 | float fvalue; | |
71 | int i; | |
8fa9368e | 72 | |
e37c4b39 BV |
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; | |
bc114328 BV |
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 | } | |
e37c4b39 BV |
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.probes = sdi->probes; | |
138 | analog.num_samples = 1; | |
bc114328 | 139 | analog.data = &devc->last_spl; |
e37c4b39 BV |
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; | |
e1af0e85 BV |
149 | case TOKEN_RECORDING_ON: |
150 | devc->recording = TRUE; | |
151 | break; | |
152 | case TOKEN_RECORDING_OFF: | |
153 | devc->recording = FALSE; | |
154 | break; | |
e37c4b39 BV |
155 | case TOKEN_TIME: |
156 | case TOKEN_STORE_OK: | |
157 | case TOKEN_STORE_FULL: | |
e37c4b39 BV |
158 | case TOKEN_BATTERY_OK: |
159 | case TOKEN_BATTERY_LOW: | |
160 | case TOKEN_MEAS_RANGE_OK: | |
161 | case TOKEN_MEAS_RANGE_OVER: | |
162 | case TOKEN_MEAS_RANGE_UNDER: | |
163 | case TOKEN_MEAS_RANGE_30_80: | |
164 | case TOKEN_MEAS_RANGE_30_130: | |
165 | case TOKEN_MEAS_RANGE_50_100: | |
166 | case TOKEN_MEAS_RANGE_80_130: | |
167 | /* Not useful, or not expressable in sigrok. */ | |
168 | break; | |
169 | } | |
170 | ||
171 | } | |
172 | ||
e1af0e85 BV |
173 | static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c, |
174 | int handle_packets) | |
e37c4b39 BV |
175 | { |
176 | struct dev_context *devc; | |
14cf708f | 177 | gint64 cur_time; |
e37c4b39 | 178 | int len; |
8fa9368e BV |
179 | |
180 | if (!(devc = sdi->priv)) | |
e37c4b39 BV |
181 | return; |
182 | ||
183 | if (c == 0xff) { | |
184 | /* Device is in hold mode */ | |
185 | devc->cur_mqflags |= SR_MQFLAG_HOLD; | |
186 | ||
14cf708f BV |
187 | if (devc->hold_last_sent == 0) { |
188 | /* First hold notification. */ | |
189 | devc->hold_last_sent = g_get_monotonic_time(); | |
190 | /* When the device leaves hold mode, it starts from scratch. */ | |
191 | devc->state = ST_INIT; | |
192 | } else { | |
193 | cur_time = g_get_monotonic_time(); | |
194 | if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) { | |
195 | /* Force the last measurement out again. */ | |
196 | devc->cmd = 0xa5; | |
197 | devc->token = TOKEN_MEAS_WAS_READOUT; | |
e1af0e85 BV |
198 | if (handle_packets) |
199 | process_mset(sdi); | |
14cf708f BV |
200 | devc->hold_last_sent = cur_time; |
201 | } | |
202 | } | |
e37c4b39 | 203 | |
e37c4b39 BV |
204 | return; |
205 | } | |
206 | devc->cur_mqflags &= ~SR_MQFLAG_HOLD; | |
14cf708f | 207 | devc->hold_last_sent = 0; |
e37c4b39 BV |
208 | |
209 | if (devc->state == ST_INIT) { | |
210 | if (c == 0xa5) { | |
211 | devc->cmd = c; | |
212 | devc->token = 0x00; | |
213 | devc->state = ST_GET_TOKEN; | |
214 | } else if (c == 0xbb) { | |
215 | devc->cmd = c; | |
216 | devc->buf_len = 0; | |
217 | devc->state = ST_GET_LOG; | |
218 | } | |
219 | } else if (devc->state == ST_GET_TOKEN) { | |
220 | devc->token = c; | |
221 | devc->buf_len = 0; | |
222 | len = find_token_payload_len(devc->token); | |
223 | if (len == -1 || len > 0) { | |
224 | devc->buf_len = 0; | |
225 | devc->state = ST_GET_DATA; | |
226 | } else { | |
e1af0e85 BV |
227 | if (handle_packets) |
228 | process_mset(sdi); | |
e37c4b39 BV |
229 | devc->state = ST_INIT; |
230 | } | |
231 | } else if (devc->state == ST_GET_DATA) { | |
232 | len = find_token_payload_len(devc->token); | |
233 | if (len == -1) { | |
234 | /* We don't know this token. */ | |
235 | sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token); | |
236 | if (c == 0xa5 || c == 0xbb) { | |
237 | /* Looks like a new command however. */ | |
e1af0e85 BV |
238 | if (handle_packets) |
239 | process_mset(sdi); | |
e37c4b39 BV |
240 | devc->state = ST_INIT; |
241 | } else { | |
242 | devc->buf[devc->buf_len++] = c; | |
243 | if (devc->buf_len > BUF_SIZE) { | |
244 | /* Shouldn't happen, ignore. */ | |
245 | devc->state = ST_INIT; | |
246 | } | |
247 | } | |
248 | } else { | |
249 | devc->buf[devc->buf_len++] = c; | |
250 | if (devc->buf_len == len) { | |
e1af0e85 BV |
251 | if (handle_packets) |
252 | process_mset(sdi); | |
e37c4b39 BV |
253 | devc->state = ST_INIT; |
254 | } else if (devc->buf_len > BUF_SIZE) { | |
255 | /* Shouldn't happen, ignore. */ | |
256 | devc->state = ST_INIT; | |
257 | } | |
258 | } | |
259 | } else if (devc->state == ST_GET_LOG) { | |
260 | } | |
261 | ||
262 | } | |
263 | ||
264 | SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data) | |
265 | { | |
266 | const struct sr_dev_inst *sdi; | |
267 | struct sr_serial_dev_inst *serial; | |
268 | unsigned char c; | |
269 | ||
270 | (void)fd; | |
271 | ||
272 | if (!(sdi = cb_data)) | |
8fa9368e BV |
273 | return TRUE; |
274 | ||
e37c4b39 | 275 | serial = sdi->conn; |
8fa9368e | 276 | if (revents == G_IO_IN) { |
e37c4b39 BV |
277 | if (serial_read(serial, &c, 1) != 1) |
278 | return TRUE; | |
e1af0e85 | 279 | process_byte(sdi, c, TRUE); |
8fa9368e BV |
280 | } |
281 | ||
282 | return TRUE; | |
283 | } | |
e1af0e85 BV |
284 | |
285 | ||
286 | static int wait_for_token(const struct sr_dev_inst *sdi, char *tokens, int timeout) | |
287 | { | |
288 | struct dev_context *devc; | |
289 | struct sr_serial_dev_inst *serial; | |
290 | gint64 start_time; | |
291 | int i; | |
292 | unsigned char c; | |
293 | ||
294 | serial = sdi->conn; | |
295 | devc = sdi->priv; | |
296 | devc->state = ST_INIT; | |
297 | start_time = g_get_monotonic_time() / 1000; | |
298 | while (TRUE) { | |
299 | if (serial_read(serial, &c, 1) != 1) | |
300 | /* Device might have gone away. */ | |
301 | return SR_ERR; | |
302 | process_byte(sdi, c, FALSE); | |
303 | if (devc->state != ST_INIT) | |
304 | /* Wait for a whole packet to get processed. */ | |
305 | continue; | |
306 | for (i = 0; tokens[i] != -1; i++) { | |
307 | if (devc->token == tokens[i]) { | |
308 | sr_spew("wait_for_token: got token 0x%.2x", devc->token); | |
309 | return SR_OK; | |
310 | } | |
311 | } | |
312 | if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout) | |
313 | return SR_ERR_TIMEOUT; | |
314 | } | |
315 | ||
316 | return SR_OK; | |
317 | } | |
318 | ||
319 | /* cmd is the command to send, tokens are the tokens that denote the state | |
320 | * which the command affects. The first token is the desired state. */ | |
be733919 BV |
321 | SR_PRIV int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd, |
322 | char *tokens, int timeout) | |
e1af0e85 BV |
323 | { |
324 | struct dev_context *devc; | |
325 | struct sr_serial_dev_inst *serial; | |
326 | ||
327 | serial = sdi->conn; | |
328 | devc = sdi->priv; | |
329 | ||
330 | /* The device doesn't respond to commands very well. The | |
331 | * only thing to do is wait for the token that will confirm | |
332 | * whether the command worked or not, and resend if needed. */ | |
333 | while (TRUE) { | |
334 | if (serial_write(serial, (const void *)&cmd, 1) != 1) | |
335 | return SR_ERR; | |
be733919 | 336 | if (wait_for_token(sdi, tokens, timeout) == SR_ERR) |
e1af0e85 BV |
337 | return SR_ERR; |
338 | if (devc->token == tokens[0]) | |
339 | /* It worked. */ | |
340 | break; | |
341 | } | |
342 | ||
343 | return SR_OK; | |
344 | } | |
345 | ||
0cd9107d BV |
346 | SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi, |
347 | int *state) | |
e1af0e85 BV |
348 | { |
349 | struct dev_context *devc; | |
350 | char tokens[5]; | |
351 | ||
352 | devc = sdi->priv; | |
e1af0e85 BV |
353 | if (devc->recording == -1) { |
354 | /* Didn't pick up device state yet. */ | |
355 | tokens[0] = TOKEN_RECORDING_ON; | |
356 | tokens[1] = TOKEN_RECORDING_OFF; | |
357 | tokens[2] = -1; | |
0cd9107d | 358 | if (wait_for_token(sdi, tokens, 510) != SR_OK) |
e1af0e85 BV |
359 | return SR_ERR; |
360 | } | |
0cd9107d | 361 | *state = devc->token == TOKEN_RECORDING_ON; |
e1af0e85 | 362 | |
0cd9107d | 363 | return SR_OK; |
e1af0e85 BV |
364 | } |
365 | ||
0cd9107d BV |
366 | SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi, |
367 | gboolean state) | |
e1af0e85 BV |
368 | { |
369 | struct dev_context *devc; | |
370 | int ret; | |
371 | char tokens[5]; | |
372 | ||
373 | devc = sdi->priv; | |
374 | ||
375 | /* The toggle below needs the desired state in first position. */ | |
0cd9107d | 376 | if (state) { |
e1af0e85 BV |
377 | tokens[0] = TOKEN_RECORDING_ON; |
378 | tokens[1] = TOKEN_RECORDING_OFF; | |
379 | } else { | |
380 | tokens[0] = TOKEN_RECORDING_OFF; | |
381 | tokens[1] = TOKEN_RECORDING_ON; | |
382 | } | |
383 | tokens[2] = -1; | |
384 | ||
385 | if (devc->recording == -1) { | |
386 | /* Didn't pick up device state yet. */ | |
387 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
388 | return SR_ERR; | |
389 | if (devc->token == tokens[0]) | |
390 | /* Nothing to do. */ | |
391 | return SR_OK; | |
0cd9107d | 392 | } else if (devc->recording == state) |
e1af0e85 BV |
393 | /* Nothing to do. */ |
394 | return SR_OK; | |
395 | ||
be733919 BV |
396 | /* Recording state notifications are sent at 2Hz, so allow just over |
397 | * that, 510ms, for the state to come in. */ | |
398 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens, 510); | |
399 | ||
400 | return ret; | |
401 | } | |
402 | ||
403 | SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi) | |
404 | { | |
405 | struct dev_context *devc; | |
406 | int cur_setting; | |
407 | char tokens[5]; | |
408 | ||
409 | devc = sdi->priv; | |
410 | ||
411 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
412 | if (cur_setting == 0) { | |
413 | /* Didn't pick up device state yet. */ | |
414 | tokens[0] = TOKEN_WEIGHT_FREQ_A; | |
415 | tokens[1] = TOKEN_WEIGHT_FREQ_C; | |
416 | tokens[2] = -1; | |
417 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
418 | return SR_ERR; | |
419 | if (devc->token == TOKEN_WEIGHT_FREQ_A) | |
420 | return SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
421 | else | |
422 | return SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
423 | } else | |
424 | return cur_setting; | |
425 | ||
426 | } | |
427 | ||
428 | SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw) | |
429 | { | |
430 | struct dev_context *devc; | |
431 | int cur_setting, ret; | |
432 | char tokens[5]; | |
433 | ||
434 | devc = sdi->priv; | |
435 | ||
436 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
437 | if (cur_setting == freqw) | |
438 | /* Already set to this frequency weighting. */ | |
439 | return SR_OK; | |
440 | ||
441 | /* The toggle below needs the desired state in first position. */ | |
442 | if (freqw == SR_MQFLAG_SPL_FREQ_WEIGHT_A) { | |
443 | tokens[0] = TOKEN_WEIGHT_FREQ_A; | |
444 | tokens[1] = TOKEN_WEIGHT_FREQ_C; | |
445 | } else { | |
446 | tokens[0] = TOKEN_WEIGHT_FREQ_C; | |
447 | tokens[1] = TOKEN_WEIGHT_FREQ_A; | |
448 | } | |
449 | tokens[2] = -1; | |
450 | ||
451 | if (cur_setting == 0) { | |
452 | /* Didn't pick up device state yet. */ | |
453 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
454 | return SR_ERR; | |
455 | if (devc->token == tokens[0]) | |
456 | /* Nothing to do. */ | |
457 | return SR_OK; | |
458 | } | |
459 | ||
460 | /* 10ms timeout seems to work best for this. */ | |
461 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10); | |
e1af0e85 BV |
462 | |
463 | return ret; | |
464 | } | |
1487ce4f BV |
465 | |
466 | SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi) | |
467 | { | |
468 | struct dev_context *devc; | |
469 | int cur_setting; | |
470 | char tokens[5]; | |
471 | ||
472 | devc = sdi->priv; | |
473 | ||
474 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
475 | if (cur_setting == 0) { | |
476 | /* Didn't pick up device state yet. */ | |
477 | tokens[0] = TOKEN_WEIGHT_TIME_FAST; | |
478 | tokens[1] = TOKEN_WEIGHT_TIME_SLOW; | |
479 | tokens[2] = -1; | |
480 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
481 | return SR_ERR; | |
482 | if (devc->token == TOKEN_WEIGHT_TIME_FAST) | |
483 | return SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
484 | else | |
485 | return SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
486 | } else | |
487 | return cur_setting; | |
488 | ||
489 | } | |
490 | ||
491 | SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew) | |
492 | { | |
493 | struct dev_context *devc; | |
494 | int cur_setting, ret; | |
495 | char tokens[5]; | |
496 | ||
497 | devc = sdi->priv; | |
498 | ||
499 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
500 | if (cur_setting == timew) | |
501 | /* Already set to this time weighting. */ | |
502 | return SR_OK; | |
503 | ||
504 | /* The toggle below needs the desired state in first position. */ | |
505 | if (timew == SR_MQFLAG_SPL_TIME_WEIGHT_F) { | |
506 | tokens[0] = TOKEN_WEIGHT_TIME_FAST; | |
507 | tokens[1] = TOKEN_WEIGHT_TIME_SLOW; | |
508 | } else { | |
509 | tokens[0] = TOKEN_WEIGHT_TIME_SLOW; | |
510 | tokens[1] = TOKEN_WEIGHT_TIME_FAST; | |
511 | } | |
512 | tokens[2] = -1; | |
513 | ||
514 | if (cur_setting == 0) { | |
515 | /* Didn't pick up device state yet. */ | |
516 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
517 | return SR_ERR; | |
518 | if (devc->token == tokens[0]) | |
519 | /* Nothing to do. */ | |
520 | return SR_OK; | |
521 | } | |
522 | ||
523 | /* 51ms timeout seems to work best for this. */ | |
524 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51); | |
525 | ||
526 | return ret; | |
527 | } | |
a90e480c BV |
528 | |
529 | SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi, | |
530 | gboolean *holdmode) | |
531 | { | |
532 | struct dev_context *devc; | |
533 | char tokens[5]; | |
534 | ||
535 | devc = sdi->priv; | |
536 | ||
537 | if (devc->cur_mqflags == 0) { | |
538 | tokens[0] = TOKEN_HOLD_MAX; | |
539 | tokens[1] = TOKEN_HOLD_MIN; | |
540 | tokens[2] = TOKEN_HOLD_NONE; | |
541 | tokens[3] = -1; | |
542 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
543 | return SR_ERR; | |
544 | if (devc->token == TOKEN_HOLD_MAX) | |
545 | devc->cur_mqflags = SR_MQFLAG_MAX; | |
546 | else if (devc->token == TOKEN_HOLD_MIN) | |
547 | devc->cur_mqflags = SR_MQFLAG_MIN; | |
548 | } | |
549 | *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
550 | ||
551 | return SR_OK; | |
552 | } | |
553 | ||
554 | SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode) | |
555 | { | |
556 | struct dev_context *devc; | |
557 | int cur_setting, ret; | |
558 | char tokens[5]; | |
559 | ||
560 | devc = sdi->priv; | |
561 | ||
562 | /* The toggle below needs the desired state in first position. */ | |
563 | if (holdmode == SR_MQFLAG_MAX) { | |
564 | tokens[0] = TOKEN_HOLD_MAX; | |
565 | tokens[1] = TOKEN_HOLD_MIN; | |
566 | tokens[2] = TOKEN_HOLD_NONE; | |
567 | } else if (holdmode == SR_MQFLAG_MIN) { | |
568 | tokens[0] = TOKEN_HOLD_MIN; | |
569 | tokens[1] = TOKEN_HOLD_MAX; | |
570 | tokens[2] = TOKEN_HOLD_NONE; | |
571 | } else { | |
572 | tokens[0] = TOKEN_HOLD_NONE; | |
573 | tokens[1] = TOKEN_HOLD_MAX; | |
574 | tokens[2] = TOKEN_HOLD_MIN; | |
575 | } | |
576 | tokens[3] = -1; | |
577 | ||
578 | if (devc->cur_mqflags == 0) { | |
579 | /* Didn't pick up device state yet. */ | |
580 | if (wait_for_token(sdi, tokens, 0) != SR_OK) | |
581 | return SR_ERR; | |
582 | if (devc->token == tokens[0]) | |
583 | /* Nothing to do. */ | |
584 | return SR_OK; | |
585 | } else { | |
586 | cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
587 | if (cur_setting == holdmode) | |
588 | /* Already set correctly. */ | |
589 | return SR_OK; | |
590 | } | |
591 | ||
592 | /* 51ms timeout seems to work best for this. */ | |
593 | ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51); | |
594 | ||
595 | return ret; | |
596 | } |