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