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