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