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