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