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