]>
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 | ||
be733919 | 20 | #include <string.h> |
8fa9368e BV |
21 | #include "protocol.h" |
22 | ||
7fb8279c BV |
23 | #define SERIALCOMM "9600/8n1" |
24 | /* 23ms is the longest interval between tokens. */ | |
25 | #define MAX_SCAN_TIME 25 * 1000 | |
26 | ||
a0e0bb41 | 27 | static const uint32_t scanopts[] = { |
7fb8279c BV |
28 | SR_CONF_CONN, |
29 | }; | |
30 | ||
f254bc4b | 31 | static const uint32_t devopts[] = { |
7fb8279c | 32 | SR_CONF_SOUNDLEVELMETER, |
7fb8279c | 33 | SR_CONF_CONTINUOUS, |
5827f61b | 34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
d7125bfa BV |
35 | }; |
36 | ||
37 | static const uint32_t devopts_global[] = { | |
5827f61b BV |
38 | SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
39 | SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
40 | SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET, | |
42 | SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET, | |
43 | SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET, | |
44 | SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, | |
45 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
7fb8279c BV |
46 | }; |
47 | ||
be733919 BV |
48 | static const char *weight_freq[] = { |
49 | "A", | |
50 | "C", | |
51 | }; | |
7fb8279c | 52 | |
1487ce4f BV |
53 | static const char *weight_time[] = { |
54 | "F", | |
55 | "S", | |
56 | }; | |
57 | ||
f157b2ee BV |
58 | static const uint64_t meas_ranges[][2] = { |
59 | { 30, 130 }, | |
60 | { 30, 80 }, | |
61 | { 50, 100 }, | |
62 | { 80, 130 }, | |
63 | }; | |
64 | ||
662172d4 BV |
65 | static const char *data_sources[] = { |
66 | "Live", | |
67 | "Memory", | |
68 | }; | |
8fa9368e BV |
69 | SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info; |
70 | static struct sr_dev_driver *di = &cem_dt_885x_driver_info; | |
71 | ||
72 | ||
73 | static int init(struct sr_context *sr_ctx) | |
74 | { | |
75 | return std_init(sr_ctx, di, LOG_PREFIX); | |
76 | } | |
77 | ||
78 | static GSList *scan(GSList *options) | |
79 | { | |
80 | struct drv_context *drvc; | |
7fb8279c BV |
81 | struct dev_context *devc; |
82 | struct sr_config *src; | |
83 | struct sr_serial_dev_inst *serial; | |
84 | struct sr_dev_inst *sdi; | |
ba7dd8bb | 85 | struct sr_channel *ch; |
7fb8279c BV |
86 | GSList *l, *devices; |
87 | gint64 start; | |
88 | const char *conn; | |
89 | unsigned char c; | |
90 | ||
91 | conn = NULL; | |
92 | for (l = options; l; l = l->next) { | |
93 | src = l->data; | |
94 | if (src->key == SR_CONF_CONN) | |
95 | conn = g_variant_get_string(src->data, NULL); | |
96 | } | |
97 | if (!conn) | |
98 | return NULL; | |
99 | ||
100 | if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM))) | |
101 | return NULL; | |
8fa9368e | 102 | |
d7b269da | 103 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) |
7fb8279c | 104 | return NULL; |
8fa9368e BV |
105 | |
106 | devices = NULL; | |
107 | drvc = di->priv; | |
7fb8279c BV |
108 | start = g_get_monotonic_time(); |
109 | while (g_get_monotonic_time() - start < MAX_SCAN_TIME) { | |
d7b269da | 110 | if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) { |
7fb8279c | 111 | /* Found one. */ |
aed4ad0b | 112 | if (!(sdi = sr_dev_inst_new(SR_ST_INACTIVE, "CEM", |
7fb8279c BV |
113 | "DT-885x", NULL))) |
114 | return NULL; | |
115 | ||
116 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
117 | sr_dbg("Device context malloc failed."); | |
118 | return NULL; | |
119 | } | |
e1af0e85 BV |
120 | devc->cur_mqflags = 0; |
121 | devc->recording = -1; | |
f157b2ee | 122 | devc->cur_meas_range = 0; |
662172d4 | 123 | devc->cur_data_source = DATA_SOURCE_LIVE; |
cea26f6e | 124 | devc->enable_data_source_memory = FALSE; |
7fb8279c BV |
125 | |
126 | if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM))) | |
127 | return NULL; | |
128 | ||
129 | sdi->inst_type = SR_INST_SERIAL; | |
130 | sdi->priv = devc; | |
131 | sdi->driver = di; | |
3f239f08 | 132 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "SPL"))) |
7fb8279c | 133 | return NULL; |
ba7dd8bb | 134 | sdi->channels = g_slist_append(sdi->channels, ch); |
7fb8279c BV |
135 | drvc->instances = g_slist_append(drvc->instances, sdi); |
136 | devices = g_slist_append(devices, sdi); | |
137 | break; | |
138 | } | |
139 | /* It takes about 1ms for a byte to come in. */ | |
140 | g_usleep(1000); | |
141 | } | |
8fa9368e | 142 | |
7fb8279c | 143 | serial_close(serial); |
8fa9368e BV |
144 | |
145 | return devices; | |
146 | } | |
147 | ||
148 | static GSList *dev_list(void) | |
149 | { | |
ce4d26dd | 150 | return ((struct drv_context *)(di->priv))->instances; |
8fa9368e BV |
151 | } |
152 | ||
8fa9368e BV |
153 | static int dev_open(struct sr_dev_inst *sdi) |
154 | { | |
7fb8279c | 155 | struct sr_serial_dev_inst *serial; |
8fa9368e | 156 | |
7fb8279c BV |
157 | serial = sdi->conn; |
158 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
159 | return SR_ERR; | |
8fa9368e BV |
160 | |
161 | sdi->status = SR_ST_ACTIVE; | |
162 | ||
163 | return SR_OK; | |
164 | } | |
165 | ||
8fa9368e BV |
166 | static int cleanup(void) |
167 | { | |
a6630742 | 168 | return std_dev_clear(di, NULL); |
8fa9368e BV |
169 | } |
170 | ||
584560f1 | 171 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 172 | const struct sr_channel_group *cg) |
8fa9368e | 173 | { |
7fb8279c | 174 | struct dev_context *devc; |
f157b2ee BV |
175 | GVariant *range[2]; |
176 | uint64_t low, high; | |
a90e480c | 177 | int tmp, ret; |
8fa9368e | 178 | |
53b4680f | 179 | (void)cg; |
d3c74a6f | 180 | |
7fb8279c BV |
181 | if (!sdi) |
182 | return SR_ERR_ARG; | |
8fa9368e | 183 | |
7fb8279c | 184 | devc = sdi->priv; |
a90e480c | 185 | ret = SR_OK; |
8fa9368e | 186 | switch (key) { |
7fb8279c BV |
187 | case SR_CONF_LIMIT_SAMPLES: |
188 | *data = g_variant_new_uint64(devc->limit_samples); | |
189 | break; | |
e1af0e85 | 190 | case SR_CONF_DATALOG: |
0cd9107d BV |
191 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) |
192 | *data = g_variant_new_boolean(tmp); | |
e1af0e85 | 193 | break; |
be733919 BV |
194 | case SR_CONF_SPL_WEIGHT_FREQ: |
195 | tmp = cem_dt_885x_weight_freq_get(sdi); | |
196 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
197 | *data = g_variant_new_string("A"); | |
198 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
199 | *data = g_variant_new_string("C"); | |
200 | else | |
201 | return SR_ERR; | |
202 | break; | |
1487ce4f BV |
203 | case SR_CONF_SPL_WEIGHT_TIME: |
204 | tmp = cem_dt_885x_weight_time_get(sdi); | |
205 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
206 | *data = g_variant_new_string("F"); | |
207 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
208 | *data = g_variant_new_string("S"); | |
209 | else | |
210 | return SR_ERR; | |
211 | break; | |
a90e480c BV |
212 | case SR_CONF_HOLD_MAX: |
213 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
214 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX); | |
215 | break; | |
216 | case SR_CONF_HOLD_MIN: | |
217 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
218 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN); | |
219 | break; | |
f157b2ee BV |
220 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
221 | if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
222 | range[0] = g_variant_new_uint64(low); | |
223 | range[1] = g_variant_new_uint64(high); | |
224 | *data = g_variant_new_tuple(range, 2); | |
225 | } | |
226 | break; | |
4c22355f BV |
227 | case SR_CONF_POWER_OFF: |
228 | *data = g_variant_new_boolean(FALSE); | |
229 | break; | |
662172d4 BV |
230 | case SR_CONF_DATA_SOURCE: |
231 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
232 | *data = g_variant_new_string("Live"); | |
233 | else | |
234 | *data = g_variant_new_string("Memory"); | |
235 | break; | |
8fa9368e BV |
236 | default: |
237 | return SR_ERR_NA; | |
238 | } | |
239 | ||
a90e480c | 240 | return ret; |
8fa9368e BV |
241 | } |
242 | ||
584560f1 | 243 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 244 | const struct sr_channel_group *cg) |
8fa9368e | 245 | { |
7fb8279c | 246 | struct dev_context *devc; |
f157b2ee BV |
247 | uint64_t tmp_u64, low, high; |
248 | unsigned int i; | |
a90e480c | 249 | int tmp, ret; |
be733919 | 250 | const char *tmp_str; |
8fa9368e | 251 | |
53b4680f | 252 | (void)cg; |
d3c74a6f | 253 | |
8fa9368e BV |
254 | if (sdi->status != SR_ST_ACTIVE) |
255 | return SR_ERR_DEV_CLOSED; | |
256 | ||
7fb8279c BV |
257 | if (!(devc = sdi->priv)) { |
258 | sr_err("sdi->priv was NULL."); | |
259 | return SR_ERR_BUG; | |
260 | } | |
261 | ||
8fa9368e BV |
262 | ret = SR_OK; |
263 | switch (key) { | |
7fb8279c BV |
264 | case SR_CONF_LIMIT_SAMPLES: |
265 | tmp_u64 = g_variant_get_uint64(data); | |
266 | devc->limit_samples = tmp_u64; | |
267 | ret = SR_OK; | |
268 | break; | |
e1af0e85 | 269 | case SR_CONF_DATALOG: |
0cd9107d | 270 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); |
e1af0e85 | 271 | break; |
be733919 BV |
272 | case SR_CONF_SPL_WEIGHT_FREQ: |
273 | tmp_str = g_variant_get_string(data, NULL); | |
274 | if (!strcmp(tmp_str, "A")) | |
275 | ret = cem_dt_885x_weight_freq_set(sdi, | |
276 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
277 | else if (!strcmp(tmp_str, "C")) | |
278 | ret = cem_dt_885x_weight_freq_set(sdi, | |
279 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
280 | else | |
281 | return SR_ERR_ARG; | |
282 | break; | |
1487ce4f BV |
283 | case SR_CONF_SPL_WEIGHT_TIME: |
284 | tmp_str = g_variant_get_string(data, NULL); | |
285 | if (!strcmp(tmp_str, "F")) | |
286 | ret = cem_dt_885x_weight_time_set(sdi, | |
287 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
288 | else if (!strcmp(tmp_str, "S")) | |
289 | ret = cem_dt_885x_weight_time_set(sdi, | |
290 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
291 | else | |
292 | return SR_ERR_ARG; | |
293 | break; | |
a90e480c BV |
294 | case SR_CONF_HOLD_MAX: |
295 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0; | |
296 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
297 | break; | |
298 | case SR_CONF_HOLD_MIN: | |
299 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0; | |
300 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
301 | break; | |
f157b2ee BV |
302 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
303 | g_variant_get(data, "(tt)", &low, &high); | |
304 | ret = SR_ERR_ARG; | |
305 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
306 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
307 | ret = cem_dt_885x_meas_range_set(sdi, low, high); | |
308 | break; | |
309 | } | |
310 | } | |
311 | break; | |
4c22355f BV |
312 | case SR_CONF_POWER_OFF: |
313 | if (g_variant_get_boolean(data)) | |
314 | ret = cem_dt_885x_power_off(sdi); | |
315 | break; | |
662172d4 BV |
316 | case SR_CONF_DATA_SOURCE: |
317 | tmp_str = g_variant_get_string(data, NULL); | |
318 | if (!strcmp(tmp_str, "Live")) | |
319 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
320 | else if (!strcmp(tmp_str, "Memory")) | |
321 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
322 | else | |
323 | return SR_ERR; | |
cea26f6e | 324 | devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY; |
662172d4 | 325 | break; |
8fa9368e BV |
326 | default: |
327 | ret = SR_ERR_NA; | |
328 | } | |
329 | ||
330 | return ret; | |
331 | } | |
332 | ||
584560f1 | 333 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 334 | const struct sr_channel_group *cg) |
8fa9368e | 335 | { |
f157b2ee BV |
336 | GVariant *tuple, *range[2]; |
337 | GVariantBuilder gvb; | |
338 | unsigned int i; | |
8fa9368e BV |
339 | int ret; |
340 | ||
53b4680f | 341 | (void)cg; |
8fa9368e BV |
342 | |
343 | ret = SR_OK; | |
d7125bfa BV |
344 | if (!sdi) { |
345 | switch (key) { | |
346 | case SR_CONF_SCAN_OPTIONS: | |
347 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
348 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
349 | break; | |
350 | case SR_CONF_DEVICE_OPTIONS: | |
351 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
352 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
353 | break; | |
354 | default: | |
355 | return SR_ERR_NA; | |
356 | } | |
357 | } else { | |
358 | switch (key) { | |
359 | case SR_CONF_DEVICE_OPTIONS: | |
360 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
361 | devopts_global, ARRAY_SIZE(devopts_global), sizeof(uint32_t)); | |
362 | case SR_CONF_SPL_WEIGHT_FREQ: | |
363 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
364 | break; | |
365 | case SR_CONF_SPL_WEIGHT_TIME: | |
366 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
367 | break; | |
368 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
369 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
370 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
371 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
372 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
373 | tuple = g_variant_new_tuple(range, 2); | |
374 | g_variant_builder_add_value(&gvb, tuple); | |
375 | } | |
376 | *data = g_variant_builder_end(&gvb); | |
377 | break; | |
378 | case SR_CONF_DATA_SOURCE: | |
379 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
380 | break; | |
381 | default: | |
382 | return SR_ERR_NA; | |
f157b2ee | 383 | } |
8fa9368e BV |
384 | } |
385 | ||
386 | return ret; | |
387 | } | |
388 | ||
e37c4b39 | 389 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
8fa9368e | 390 | { |
e37c4b39 BV |
391 | struct dev_context *devc; |
392 | struct sr_serial_dev_inst *serial; | |
8fa9368e BV |
393 | |
394 | if (sdi->status != SR_ST_ACTIVE) | |
395 | return SR_ERR_DEV_CLOSED; | |
396 | ||
e37c4b39 BV |
397 | if (!(devc = sdi->priv)) { |
398 | sr_err("sdi->priv was NULL."); | |
399 | return SR_ERR_BUG; | |
400 | } | |
401 | ||
402 | devc->cb_data = cb_data; | |
403 | devc->state = ST_INIT; | |
404 | devc->num_samples = 0; | |
405 | devc->buf_len = 0; | |
406 | ||
407 | /* Send header packet to the session bus. */ | |
408 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
409 | ||
410 | /* Poll every 100ms, or whenever some data comes in. */ | |
411 | serial = sdi->conn; | |
102f1239 BV |
412 | serial_source_add(sdi->session, serial, G_IO_IN, 150, |
413 | cem_dt_885x_receive_data, (void *)sdi); | |
8fa9368e BV |
414 | |
415 | return SR_OK; | |
416 | } | |
417 | ||
418 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
419 | { | |
8fa9368e BV |
420 | if (sdi->status != SR_ST_ACTIVE) |
421 | return SR_ERR_DEV_CLOSED; | |
422 | ||
d43b0908 | 423 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, |
e37c4b39 | 424 | sdi->conn, LOG_PREFIX); |
8fa9368e BV |
425 | } |
426 | ||
427 | SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = { | |
428 | .name = "cem-dt-885x", | |
429 | .longname = "CEM DT-885x", | |
430 | .api_version = 1, | |
431 | .init = init, | |
432 | .cleanup = cleanup, | |
433 | .scan = scan, | |
434 | .dev_list = dev_list, | |
a6630742 | 435 | .dev_clear = NULL, |
8fa9368e BV |
436 | .config_get = config_get, |
437 | .config_set = config_set, | |
438 | .config_list = config_list, | |
439 | .dev_open = dev_open, | |
bf2c987f | 440 | .dev_close = std_serial_dev_close, |
8fa9368e BV |
441 | .dev_acquisition_start = dev_acquisition_start, |
442 | .dev_acquisition_stop = dev_acquisition_stop, | |
443 | .priv = NULL, | |
444 | }; |