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