]>
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 | ||
168 | static int dev_close(struct sr_dev_inst *sdi) | |
169 | { | |
7fb8279c | 170 | struct sr_serial_dev_inst *serial; |
8fa9368e | 171 | |
7fb8279c BV |
172 | serial = sdi->conn; |
173 | if (serial && serial->fd != -1) { | |
174 | serial_close(serial); | |
175 | sdi->status = SR_ST_INACTIVE; | |
176 | } | |
8fa9368e BV |
177 | |
178 | return SR_OK; | |
179 | } | |
180 | ||
181 | static int cleanup(void) | |
182 | { | |
7fb8279c | 183 | return dev_clear(); |
8fa9368e BV |
184 | } |
185 | ||
d3c74a6f BV |
186 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, |
187 | const struct sr_probe_group *probe_group) | |
8fa9368e | 188 | { |
7fb8279c | 189 | struct dev_context *devc; |
f157b2ee BV |
190 | GVariant *range[2]; |
191 | uint64_t low, high; | |
a90e480c | 192 | int tmp, ret; |
8fa9368e | 193 | |
d3c74a6f BV |
194 | (void)probe_group; |
195 | ||
7fb8279c BV |
196 | if (!sdi) |
197 | return SR_ERR_ARG; | |
8fa9368e | 198 | |
7fb8279c | 199 | devc = sdi->priv; |
a90e480c | 200 | ret = SR_OK; |
8fa9368e | 201 | switch (key) { |
7fb8279c BV |
202 | case SR_CONF_LIMIT_SAMPLES: |
203 | *data = g_variant_new_uint64(devc->limit_samples); | |
204 | break; | |
e1af0e85 | 205 | case SR_CONF_DATALOG: |
0cd9107d BV |
206 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) |
207 | *data = g_variant_new_boolean(tmp); | |
e1af0e85 | 208 | break; |
be733919 BV |
209 | case SR_CONF_SPL_WEIGHT_FREQ: |
210 | tmp = cem_dt_885x_weight_freq_get(sdi); | |
211 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
212 | *data = g_variant_new_string("A"); | |
213 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
214 | *data = g_variant_new_string("C"); | |
215 | else | |
216 | return SR_ERR; | |
217 | break; | |
1487ce4f BV |
218 | case SR_CONF_SPL_WEIGHT_TIME: |
219 | tmp = cem_dt_885x_weight_time_get(sdi); | |
220 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
221 | *data = g_variant_new_string("F"); | |
222 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
223 | *data = g_variant_new_string("S"); | |
224 | else | |
225 | return SR_ERR; | |
226 | break; | |
a90e480c BV |
227 | case SR_CONF_HOLD_MAX: |
228 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
229 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX); | |
230 | break; | |
231 | case SR_CONF_HOLD_MIN: | |
232 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
233 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN); | |
234 | break; | |
f157b2ee BV |
235 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
236 | if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
237 | range[0] = g_variant_new_uint64(low); | |
238 | range[1] = g_variant_new_uint64(high); | |
239 | *data = g_variant_new_tuple(range, 2); | |
240 | } | |
241 | break; | |
4c22355f BV |
242 | case SR_CONF_POWER_OFF: |
243 | *data = g_variant_new_boolean(FALSE); | |
244 | break; | |
662172d4 BV |
245 | case SR_CONF_DATA_SOURCE: |
246 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
247 | *data = g_variant_new_string("Live"); | |
248 | else | |
249 | *data = g_variant_new_string("Memory"); | |
250 | break; | |
8fa9368e BV |
251 | default: |
252 | return SR_ERR_NA; | |
253 | } | |
254 | ||
a90e480c | 255 | return ret; |
8fa9368e BV |
256 | } |
257 | ||
d3c74a6f BV |
258 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, |
259 | const struct sr_probe_group *probe_group) | |
8fa9368e | 260 | { |
7fb8279c | 261 | struct dev_context *devc; |
f157b2ee BV |
262 | uint64_t tmp_u64, low, high; |
263 | unsigned int i; | |
a90e480c | 264 | int tmp, ret; |
be733919 | 265 | const char *tmp_str; |
8fa9368e | 266 | |
d3c74a6f BV |
267 | (void)probe_group; |
268 | ||
8fa9368e BV |
269 | if (sdi->status != SR_ST_ACTIVE) |
270 | return SR_ERR_DEV_CLOSED; | |
271 | ||
7fb8279c BV |
272 | if (!(devc = sdi->priv)) { |
273 | sr_err("sdi->priv was NULL."); | |
274 | return SR_ERR_BUG; | |
275 | } | |
276 | ||
8fa9368e BV |
277 | ret = SR_OK; |
278 | switch (key) { | |
7fb8279c BV |
279 | case SR_CONF_LIMIT_SAMPLES: |
280 | tmp_u64 = g_variant_get_uint64(data); | |
281 | devc->limit_samples = tmp_u64; | |
282 | ret = SR_OK; | |
283 | break; | |
e1af0e85 | 284 | case SR_CONF_DATALOG: |
0cd9107d | 285 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); |
e1af0e85 | 286 | break; |
be733919 BV |
287 | case SR_CONF_SPL_WEIGHT_FREQ: |
288 | tmp_str = g_variant_get_string(data, NULL); | |
289 | if (!strcmp(tmp_str, "A")) | |
290 | ret = cem_dt_885x_weight_freq_set(sdi, | |
291 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
292 | else if (!strcmp(tmp_str, "C")) | |
293 | ret = cem_dt_885x_weight_freq_set(sdi, | |
294 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
295 | else | |
296 | return SR_ERR_ARG; | |
297 | break; | |
1487ce4f BV |
298 | case SR_CONF_SPL_WEIGHT_TIME: |
299 | tmp_str = g_variant_get_string(data, NULL); | |
300 | if (!strcmp(tmp_str, "F")) | |
301 | ret = cem_dt_885x_weight_time_set(sdi, | |
302 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
303 | else if (!strcmp(tmp_str, "S")) | |
304 | ret = cem_dt_885x_weight_time_set(sdi, | |
305 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
306 | else | |
307 | return SR_ERR_ARG; | |
308 | break; | |
a90e480c BV |
309 | case SR_CONF_HOLD_MAX: |
310 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0; | |
311 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
312 | break; | |
313 | case SR_CONF_HOLD_MIN: | |
314 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0; | |
315 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
316 | break; | |
f157b2ee BV |
317 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
318 | g_variant_get(data, "(tt)", &low, &high); | |
319 | ret = SR_ERR_ARG; | |
320 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
321 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
322 | ret = cem_dt_885x_meas_range_set(sdi, low, high); | |
323 | break; | |
324 | } | |
325 | } | |
326 | break; | |
4c22355f BV |
327 | case SR_CONF_POWER_OFF: |
328 | if (g_variant_get_boolean(data)) | |
329 | ret = cem_dt_885x_power_off(sdi); | |
330 | break; | |
662172d4 BV |
331 | case SR_CONF_DATA_SOURCE: |
332 | tmp_str = g_variant_get_string(data, NULL); | |
333 | if (!strcmp(tmp_str, "Live")) | |
334 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
335 | else if (!strcmp(tmp_str, "Memory")) | |
336 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
337 | else | |
338 | return SR_ERR; | |
cea26f6e | 339 | devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY; |
662172d4 | 340 | break; |
8fa9368e BV |
341 | default: |
342 | ret = SR_ERR_NA; | |
343 | } | |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
d3c74a6f BV |
348 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
349 | const struct sr_probe_group *probe_group) | |
8fa9368e | 350 | { |
f157b2ee BV |
351 | GVariant *tuple, *range[2]; |
352 | GVariantBuilder gvb; | |
353 | unsigned int i; | |
8fa9368e BV |
354 | int ret; |
355 | ||
356 | (void)sdi; | |
d3c74a6f | 357 | (void)probe_group; |
8fa9368e BV |
358 | |
359 | ret = SR_OK; | |
360 | switch (key) { | |
7fb8279c BV |
361 | case SR_CONF_SCAN_OPTIONS: |
362 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
363 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
364 | break; | |
365 | case SR_CONF_DEVICE_OPTIONS: | |
366 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
367 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
368 | break; | |
be733919 BV |
369 | case SR_CONF_SPL_WEIGHT_FREQ: |
370 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
371 | break; | |
1487ce4f BV |
372 | case SR_CONF_SPL_WEIGHT_TIME: |
373 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
374 | break; | |
f157b2ee BV |
375 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
376 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
377 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
378 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
379 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
380 | tuple = g_variant_new_tuple(range, 2); | |
381 | g_variant_builder_add_value(&gvb, tuple); | |
382 | } | |
383 | *data = g_variant_builder_end(&gvb); | |
384 | break; | |
662172d4 BV |
385 | case SR_CONF_DATA_SOURCE: |
386 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
387 | break; | |
8fa9368e BV |
388 | default: |
389 | return SR_ERR_NA; | |
390 | } | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
e37c4b39 | 395 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
8fa9368e | 396 | { |
e37c4b39 BV |
397 | struct dev_context *devc; |
398 | struct sr_serial_dev_inst *serial; | |
8fa9368e BV |
399 | |
400 | if (sdi->status != SR_ST_ACTIVE) | |
401 | return SR_ERR_DEV_CLOSED; | |
402 | ||
e37c4b39 BV |
403 | if (!(devc = sdi->priv)) { |
404 | sr_err("sdi->priv was NULL."); | |
405 | return SR_ERR_BUG; | |
406 | } | |
407 | ||
408 | devc->cb_data = cb_data; | |
409 | devc->state = ST_INIT; | |
410 | devc->num_samples = 0; | |
411 | devc->buf_len = 0; | |
412 | ||
413 | /* Send header packet to the session bus. */ | |
414 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
415 | ||
416 | /* Poll every 100ms, or whenever some data comes in. */ | |
417 | serial = sdi->conn; | |
418 | sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data, | |
419 | (void *)sdi); | |
8fa9368e BV |
420 | |
421 | return SR_OK; | |
422 | } | |
423 | ||
424 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
425 | { | |
426 | (void)cb_data; | |
427 | ||
428 | if (sdi->status != SR_ST_ACTIVE) | |
429 | return SR_ERR_DEV_CLOSED; | |
430 | ||
e37c4b39 BV |
431 | return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, |
432 | sdi->conn, LOG_PREFIX); | |
8fa9368e BV |
433 | |
434 | return SR_OK; | |
435 | } | |
436 | ||
437 | SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = { | |
438 | .name = "cem-dt-885x", | |
439 | .longname = "CEM DT-885x", | |
440 | .api_version = 1, | |
441 | .init = init, | |
442 | .cleanup = cleanup, | |
443 | .scan = scan, | |
444 | .dev_list = dev_list, | |
445 | .dev_clear = dev_clear, | |
446 | .config_get = config_get, | |
447 | .config_set = config_set, | |
448 | .config_list = config_list, | |
449 | .dev_open = dev_open, | |
450 | .dev_close = dev_close, | |
451 | .dev_acquisition_start = dev_acquisition_start, | |
452 | .dev_acquisition_stop = dev_acquisition_stop, | |
453 | .priv = NULL, | |
454 | }; |