]>
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 devopts[] = { | |
32 | SR_CONF_SOUNDLEVELMETER, | |
33 | SR_CONF_CONTINUOUS, | |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
35 | SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
36 | SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
37 | SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
38 | SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET, | |
39 | SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET, | |
40 | SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET, | |
41 | SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, | |
42 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
43 | }; | |
44 | ||
45 | static const char *weight_freq[] = { | |
46 | "A", | |
47 | "C", | |
48 | }; | |
49 | ||
50 | static const char *weight_time[] = { | |
51 | "F", | |
52 | "S", | |
53 | }; | |
54 | ||
55 | static const uint64_t meas_ranges[][2] = { | |
56 | { 30, 130 }, | |
57 | { 30, 80 }, | |
58 | { 50, 100 }, | |
59 | { 80, 130 }, | |
60 | }; | |
61 | ||
62 | static const char *data_sources[] = { | |
63 | "Live", | |
64 | "Memory", | |
65 | }; | |
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; | |
78 | struct dev_context *devc; | |
79 | struct sr_config *src; | |
80 | struct sr_serial_dev_inst *serial; | |
81 | struct sr_dev_inst *sdi; | |
82 | struct sr_channel *ch; | |
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; | |
99 | ||
100 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
101 | return NULL; | |
102 | ||
103 | devices = NULL; | |
104 | drvc = di->priv; | |
105 | start = g_get_monotonic_time(); | |
106 | while (g_get_monotonic_time() - start < MAX_SCAN_TIME) { | |
107 | if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) { | |
108 | /* Found one. */ | |
109 | if (!(sdi = sr_dev_inst_new(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 | } | |
117 | devc->cur_mqflags = 0; | |
118 | devc->recording = -1; | |
119 | devc->cur_meas_range = 0; | |
120 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
121 | devc->enable_data_source_memory = FALSE; | |
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; | |
129 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "SPL"))) | |
130 | return NULL; | |
131 | sdi->channels = g_slist_append(sdi->channels, ch); | |
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 | } | |
139 | ||
140 | serial_close(serial); | |
141 | ||
142 | return devices; | |
143 | } | |
144 | ||
145 | static GSList *dev_list(void) | |
146 | { | |
147 | return ((struct drv_context *)(di->priv))->instances; | |
148 | } | |
149 | ||
150 | static int dev_open(struct sr_dev_inst *sdi) | |
151 | { | |
152 | struct sr_serial_dev_inst *serial; | |
153 | ||
154 | serial = sdi->conn; | |
155 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
156 | return SR_ERR; | |
157 | ||
158 | sdi->status = SR_ST_ACTIVE; | |
159 | ||
160 | return SR_OK; | |
161 | } | |
162 | ||
163 | static int cleanup(void) | |
164 | { | |
165 | return std_dev_clear(di, NULL); | |
166 | } | |
167 | ||
168 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
169 | const struct sr_channel_group *cg) | |
170 | { | |
171 | struct dev_context *devc; | |
172 | GVariant *range[2]; | |
173 | uint64_t low, high; | |
174 | int tmp, ret; | |
175 | ||
176 | (void)cg; | |
177 | ||
178 | if (!sdi) | |
179 | return SR_ERR_ARG; | |
180 | ||
181 | devc = sdi->priv; | |
182 | ret = SR_OK; | |
183 | switch (key) { | |
184 | case SR_CONF_LIMIT_SAMPLES: | |
185 | *data = g_variant_new_uint64(devc->limit_samples); | |
186 | break; | |
187 | case SR_CONF_DATALOG: | |
188 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) | |
189 | *data = g_variant_new_boolean(tmp); | |
190 | break; | |
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; | |
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; | |
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; | |
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; | |
224 | case SR_CONF_POWER_OFF: | |
225 | *data = g_variant_new_boolean(FALSE); | |
226 | break; | |
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; | |
233 | default: | |
234 | return SR_ERR_NA; | |
235 | } | |
236 | ||
237 | return ret; | |
238 | } | |
239 | ||
240 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
241 | const struct sr_channel_group *cg) | |
242 | { | |
243 | struct dev_context *devc; | |
244 | uint64_t tmp_u64, low, high; | |
245 | unsigned int i; | |
246 | int tmp, ret; | |
247 | const char *tmp_str; | |
248 | ||
249 | (void)cg; | |
250 | ||
251 | if (sdi->status != SR_ST_ACTIVE) | |
252 | return SR_ERR_DEV_CLOSED; | |
253 | ||
254 | if (!(devc = sdi->priv)) { | |
255 | sr_err("sdi->priv was NULL."); | |
256 | return SR_ERR_BUG; | |
257 | } | |
258 | ||
259 | ret = SR_OK; | |
260 | switch (key) { | |
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; | |
266 | case SR_CONF_DATALOG: | |
267 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); | |
268 | break; | |
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; | |
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; | |
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; | |
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; | |
309 | case SR_CONF_POWER_OFF: | |
310 | if (g_variant_get_boolean(data)) | |
311 | ret = cem_dt_885x_power_off(sdi); | |
312 | break; | |
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; | |
321 | devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY; | |
322 | break; | |
323 | default: | |
324 | ret = SR_ERR_NA; | |
325 | } | |
326 | ||
327 | return ret; | |
328 | } | |
329 | ||
330 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
331 | const struct sr_channel_group *cg) | |
332 | { | |
333 | GVariant *tuple, *range[2]; | |
334 | GVariantBuilder gvb; | |
335 | unsigned int i; | |
336 | int ret; | |
337 | ||
338 | (void)sdi; | |
339 | (void)cg; | |
340 | ||
341 | ret = SR_OK; | |
342 | switch (key) { | |
343 | case SR_CONF_SCAN_OPTIONS: | |
344 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
345 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
346 | break; | |
347 | case SR_CONF_DEVICE_OPTIONS: | |
348 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
349 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
350 | break; | |
351 | case SR_CONF_SPL_WEIGHT_FREQ: | |
352 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
353 | break; | |
354 | case SR_CONF_SPL_WEIGHT_TIME: | |
355 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
356 | break; | |
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; | |
367 | case SR_CONF_DATA_SOURCE: | |
368 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
369 | break; | |
370 | default: | |
371 | return SR_ERR_NA; | |
372 | } | |
373 | ||
374 | return ret; | |
375 | } | |
376 | ||
377 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
378 | { | |
379 | struct dev_context *devc; | |
380 | struct sr_serial_dev_inst *serial; | |
381 | ||
382 | if (sdi->status != SR_ST_ACTIVE) | |
383 | return SR_ERR_DEV_CLOSED; | |
384 | ||
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; | |
400 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
401 | cem_dt_885x_receive_data, (void *)sdi); | |
402 | ||
403 | return SR_OK; | |
404 | } | |
405 | ||
406 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
407 | { | |
408 | if (sdi->status != SR_ST_ACTIVE) | |
409 | return SR_ERR_DEV_CLOSED; | |
410 | ||
411 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, | |
412 | sdi->conn, LOG_PREFIX); | |
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, | |
423 | .dev_clear = NULL, | |
424 | .config_get = config_get, | |
425 | .config_set = config_set, | |
426 | .config_list = config_list, | |
427 | .dev_open = dev_open, | |
428 | .dev_close = std_serial_dev_close, | |
429 | .dev_acquisition_start = dev_acquisition_start, | |
430 | .dev_acquisition_stop = dev_acquisition_stop, | |
431 | .priv = NULL, | |
432 | }; |