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