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