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