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