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