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