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