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