]>
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 int32_t hwopts[] = { | |
28 | SR_CONF_CONN, | |
29 | }; | |
30 | ||
31 | static const int32_t hwcaps[] = { | |
32 | SR_CONF_SOUNDLEVELMETER, | |
33 | SR_CONF_LIMIT_SAMPLES, | |
34 | SR_CONF_CONTINUOUS, | |
35 | SR_CONF_SPL_WEIGHT_FREQ, | |
36 | SR_CONF_SPL_WEIGHT_TIME, | |
37 | SR_CONF_SPL_MEASUREMENT_RANGE, | |
38 | SR_CONF_DATALOG, | |
39 | SR_CONF_HOLD_MAX, | |
40 | SR_CONF_HOLD_MIN, | |
41 | SR_CONF_POWER_OFF, | |
42 | SR_CONF_DATA_SOURCE, | |
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_probe *probe; | |
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 | SERIAL_NONBLOCK) != 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(serial, &c, 1) == 1 && c == 0xa5) { | |
108 | /* Found one. */ | |
109 | if (!(sdi = sr_dev_inst_new(0, 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 (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL"))) | |
130 | return NULL; | |
131 | sdi->probes = g_slist_append(sdi->probes, probe); | |
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_clear(void) | |
151 | { | |
152 | return std_dev_clear(di, NULL); | |
153 | } | |
154 | ||
155 | static int dev_open(struct sr_dev_inst *sdi) | |
156 | { | |
157 | struct sr_serial_dev_inst *serial; | |
158 | ||
159 | serial = sdi->conn; | |
160 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
161 | return SR_ERR; | |
162 | ||
163 | sdi->status = SR_ST_ACTIVE; | |
164 | ||
165 | return SR_OK; | |
166 | } | |
167 | ||
168 | static int dev_close(struct sr_dev_inst *sdi) | |
169 | { | |
170 | struct sr_serial_dev_inst *serial; | |
171 | ||
172 | serial = sdi->conn; | |
173 | if (serial && serial->fd != -1) { | |
174 | serial_close(serial); | |
175 | sdi->status = SR_ST_INACTIVE; | |
176 | } | |
177 | ||
178 | return SR_OK; | |
179 | } | |
180 | ||
181 | static int cleanup(void) | |
182 | { | |
183 | return dev_clear(); | |
184 | } | |
185 | ||
186 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
187 | { | |
188 | struct dev_context *devc; | |
189 | GVariant *range[2]; | |
190 | uint64_t low, high; | |
191 | int tmp, ret; | |
192 | ||
193 | if (!sdi) | |
194 | return SR_ERR_ARG; | |
195 | ||
196 | devc = sdi->priv; | |
197 | ret = SR_OK; | |
198 | switch (key) { | |
199 | case SR_CONF_LIMIT_SAMPLES: | |
200 | *data = g_variant_new_uint64(devc->limit_samples); | |
201 | break; | |
202 | case SR_CONF_DATALOG: | |
203 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) | |
204 | *data = g_variant_new_boolean(tmp); | |
205 | break; | |
206 | case SR_CONF_SPL_WEIGHT_FREQ: | |
207 | tmp = cem_dt_885x_weight_freq_get(sdi); | |
208 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
209 | *data = g_variant_new_string("A"); | |
210 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
211 | *data = g_variant_new_string("C"); | |
212 | else | |
213 | return SR_ERR; | |
214 | break; | |
215 | case SR_CONF_SPL_WEIGHT_TIME: | |
216 | tmp = cem_dt_885x_weight_time_get(sdi); | |
217 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
218 | *data = g_variant_new_string("F"); | |
219 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
220 | *data = g_variant_new_string("S"); | |
221 | else | |
222 | return SR_ERR; | |
223 | break; | |
224 | case SR_CONF_HOLD_MAX: | |
225 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
226 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX); | |
227 | break; | |
228 | case SR_CONF_HOLD_MIN: | |
229 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
230 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN); | |
231 | break; | |
232 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
233 | if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
234 | range[0] = g_variant_new_uint64(low); | |
235 | range[1] = g_variant_new_uint64(high); | |
236 | *data = g_variant_new_tuple(range, 2); | |
237 | } | |
238 | break; | |
239 | case SR_CONF_POWER_OFF: | |
240 | *data = g_variant_new_boolean(FALSE); | |
241 | break; | |
242 | case SR_CONF_DATA_SOURCE: | |
243 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
244 | *data = g_variant_new_string("Live"); | |
245 | else | |
246 | *data = g_variant_new_string("Memory"); | |
247 | break; | |
248 | default: | |
249 | return SR_ERR_NA; | |
250 | } | |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
255 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi) | |
256 | { | |
257 | struct dev_context *devc; | |
258 | uint64_t tmp_u64, low, high; | |
259 | unsigned int i; | |
260 | int tmp, ret; | |
261 | const char *tmp_str; | |
262 | ||
263 | if (sdi->status != SR_ST_ACTIVE) | |
264 | return SR_ERR_DEV_CLOSED; | |
265 | ||
266 | if (!(devc = sdi->priv)) { | |
267 | sr_err("sdi->priv was NULL."); | |
268 | return SR_ERR_BUG; | |
269 | } | |
270 | ||
271 | ret = SR_OK; | |
272 | switch (key) { | |
273 | case SR_CONF_LIMIT_SAMPLES: | |
274 | tmp_u64 = g_variant_get_uint64(data); | |
275 | devc->limit_samples = tmp_u64; | |
276 | ret = SR_OK; | |
277 | break; | |
278 | case SR_CONF_DATALOG: | |
279 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); | |
280 | break; | |
281 | case SR_CONF_SPL_WEIGHT_FREQ: | |
282 | tmp_str = g_variant_get_string(data, NULL); | |
283 | if (!strcmp(tmp_str, "A")) | |
284 | ret = cem_dt_885x_weight_freq_set(sdi, | |
285 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
286 | else if (!strcmp(tmp_str, "C")) | |
287 | ret = cem_dt_885x_weight_freq_set(sdi, | |
288 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
289 | else | |
290 | return SR_ERR_ARG; | |
291 | break; | |
292 | case SR_CONF_SPL_WEIGHT_TIME: | |
293 | tmp_str = g_variant_get_string(data, NULL); | |
294 | if (!strcmp(tmp_str, "F")) | |
295 | ret = cem_dt_885x_weight_time_set(sdi, | |
296 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
297 | else if (!strcmp(tmp_str, "S")) | |
298 | ret = cem_dt_885x_weight_time_set(sdi, | |
299 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
300 | else | |
301 | return SR_ERR_ARG; | |
302 | break; | |
303 | case SR_CONF_HOLD_MAX: | |
304 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0; | |
305 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
306 | break; | |
307 | case SR_CONF_HOLD_MIN: | |
308 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0; | |
309 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
310 | break; | |
311 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
312 | g_variant_get(data, "(tt)", &low, &high); | |
313 | ret = SR_ERR_ARG; | |
314 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
315 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
316 | ret = cem_dt_885x_meas_range_set(sdi, low, high); | |
317 | break; | |
318 | } | |
319 | } | |
320 | break; | |
321 | case SR_CONF_POWER_OFF: | |
322 | if (g_variant_get_boolean(data)) | |
323 | ret = cem_dt_885x_power_off(sdi); | |
324 | break; | |
325 | case SR_CONF_DATA_SOURCE: | |
326 | tmp_str = g_variant_get_string(data, NULL); | |
327 | if (!strcmp(tmp_str, "Live")) | |
328 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
329 | else if (!strcmp(tmp_str, "Memory")) | |
330 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
331 | else | |
332 | return SR_ERR; | |
333 | devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY; | |
334 | break; | |
335 | default: | |
336 | ret = SR_ERR_NA; | |
337 | } | |
338 | ||
339 | return ret; | |
340 | } | |
341 | ||
342 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
343 | { | |
344 | GVariant *tuple, *range[2]; | |
345 | GVariantBuilder gvb; | |
346 | unsigned int i; | |
347 | int ret; | |
348 | ||
349 | (void)sdi; | |
350 | ||
351 | ret = SR_OK; | |
352 | switch (key) { | |
353 | case SR_CONF_SCAN_OPTIONS: | |
354 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
355 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
356 | break; | |
357 | case SR_CONF_DEVICE_OPTIONS: | |
358 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
359 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
360 | break; | |
361 | case SR_CONF_SPL_WEIGHT_FREQ: | |
362 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
363 | break; | |
364 | case SR_CONF_SPL_WEIGHT_TIME: | |
365 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
366 | break; | |
367 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
368 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
369 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
370 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
371 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
372 | tuple = g_variant_new_tuple(range, 2); | |
373 | g_variant_builder_add_value(&gvb, tuple); | |
374 | } | |
375 | *data = g_variant_builder_end(&gvb); | |
376 | break; | |
377 | case SR_CONF_DATA_SOURCE: | |
378 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
379 | break; | |
380 | default: | |
381 | return SR_ERR_NA; | |
382 | } | |
383 | ||
384 | return ret; | |
385 | } | |
386 | ||
387 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
388 | { | |
389 | struct dev_context *devc; | |
390 | struct sr_serial_dev_inst *serial; | |
391 | ||
392 | if (sdi->status != SR_ST_ACTIVE) | |
393 | return SR_ERR_DEV_CLOSED; | |
394 | ||
395 | if (!(devc = sdi->priv)) { | |
396 | sr_err("sdi->priv was NULL."); | |
397 | return SR_ERR_BUG; | |
398 | } | |
399 | ||
400 | devc->cb_data = cb_data; | |
401 | devc->state = ST_INIT; | |
402 | devc->num_samples = 0; | |
403 | devc->buf_len = 0; | |
404 | ||
405 | /* Send header packet to the session bus. */ | |
406 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
407 | ||
408 | /* Poll every 100ms, or whenever some data comes in. */ | |
409 | serial = sdi->conn; | |
410 | sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data, | |
411 | (void *)sdi); | |
412 | ||
413 | return SR_OK; | |
414 | } | |
415 | ||
416 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
417 | { | |
418 | (void)cb_data; | |
419 | ||
420 | if (sdi->status != SR_ST_ACTIVE) | |
421 | return SR_ERR_DEV_CLOSED; | |
422 | ||
423 | return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, | |
424 | sdi->conn, LOG_PREFIX); | |
425 | ||
426 | return SR_OK; | |
427 | } | |
428 | ||
429 | SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = { | |
430 | .name = "cem-dt-885x", | |
431 | .longname = "CEM DT-885x", | |
432 | .api_version = 1, | |
433 | .init = init, | |
434 | .cleanup = cleanup, | |
435 | .scan = scan, | |
436 | .dev_list = dev_list, | |
437 | .dev_clear = dev_clear, | |
438 | .config_get = config_get, | |
439 | .config_set = config_set, | |
440 | .config_list = config_list, | |
441 | .dev_open = dev_open, | |
442 | .dev_close = dev_close, | |
443 | .dev_acquisition_start = dev_acquisition_start, | |
444 | .dev_acquisition_stop = dev_acquisition_stop, | |
445 | .priv = NULL, | |
446 | }; |