]> sigrok.org Git - libsigrok.git/blame - src/hardware/cem-dt-885x/api.c
drivers: Add and use STD_CONFIG_LIST().
[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 73{
7fb8279c
BV
74 struct dev_context *devc;
75 struct sr_config *src;
76 struct sr_serial_dev_inst *serial;
77 struct sr_dev_inst *sdi;
7fb8279c
BV
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
91219afc 92 serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
8fa9368e 93
d7b269da 94 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
7fb8279c 95 return NULL;
8fa9368e
BV
96
97 devices = NULL;
7fb8279c 98 start = g_get_monotonic_time();
1a46cc62 99 while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) {
d7b269da 100 if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) {
7fb8279c 101 /* Found one. */
aac29cc1 102 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
103 sdi->status = SR_ST_INACTIVE;
104 sdi->vendor = g_strdup("CEM");
105 sdi->model = g_strdup("DT-885x");
f57d8ffe 106 devc = g_malloc0(sizeof(struct dev_context));
e1af0e85
BV
107 devc->cur_mqflags = 0;
108 devc->recording = -1;
f157b2ee 109 devc->cur_meas_range = 0;
662172d4 110 devc->cur_data_source = DATA_SOURCE_LIVE;
cea26f6e 111 devc->enable_data_source_memory = FALSE;
91219afc 112 sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
7fb8279c
BV
113 sdi->inst_type = SR_INST_SERIAL;
114 sdi->priv = devc;
5e23fcab 115 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
7fb8279c
BV
116 devices = g_slist_append(devices, sdi);
117 break;
118 }
119 /* It takes about 1ms for a byte to come in. */
120 g_usleep(1000);
121 }
8fa9368e 122
7fb8279c 123 serial_close(serial);
8fa9368e 124
15a5bfe4 125 return std_scan_complete(di, devices);
8fa9368e
BV
126}
127
584560f1 128static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 129 const struct sr_channel_group *cg)
8fa9368e 130{
7fb8279c 131 struct dev_context *devc;
f157b2ee
BV
132 GVariant *range[2];
133 uint64_t low, high;
a90e480c 134 int tmp, ret;
8fa9368e 135
53b4680f 136 (void)cg;
d3c74a6f 137
7fb8279c
BV
138 if (!sdi)
139 return SR_ERR_ARG;
8fa9368e 140
7fb8279c 141 devc = sdi->priv;
a90e480c 142 ret = SR_OK;
8fa9368e 143 switch (key) {
7fb8279c
BV
144 case SR_CONF_LIMIT_SAMPLES:
145 *data = g_variant_new_uint64(devc->limit_samples);
146 break;
e1af0e85 147 case SR_CONF_DATALOG:
0cd9107d
BV
148 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
149 *data = g_variant_new_boolean(tmp);
e1af0e85 150 break;
be733919
BV
151 case SR_CONF_SPL_WEIGHT_FREQ:
152 tmp = cem_dt_885x_weight_freq_get(sdi);
153 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
154 *data = g_variant_new_string("A");
155 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
156 *data = g_variant_new_string("C");
157 else
158 return SR_ERR;
159 break;
1487ce4f
BV
160 case SR_CONF_SPL_WEIGHT_TIME:
161 tmp = cem_dt_885x_weight_time_get(sdi);
162 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
163 *data = g_variant_new_string("F");
164 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
165 *data = g_variant_new_string("S");
166 else
167 return SR_ERR;
168 break;
a90e480c
BV
169 case SR_CONF_HOLD_MAX:
170 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
171 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
172 break;
173 case SR_CONF_HOLD_MIN:
174 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
175 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
176 break;
f157b2ee
BV
177 case SR_CONF_SPL_MEASUREMENT_RANGE:
178 if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
179 range[0] = g_variant_new_uint64(low);
180 range[1] = g_variant_new_uint64(high);
181 *data = g_variant_new_tuple(range, 2);
182 }
183 break;
4c22355f
BV
184 case SR_CONF_POWER_OFF:
185 *data = g_variant_new_boolean(FALSE);
186 break;
662172d4
BV
187 case SR_CONF_DATA_SOURCE:
188 if (devc->cur_data_source == DATA_SOURCE_LIVE)
189 *data = g_variant_new_string("Live");
190 else
191 *data = g_variant_new_string("Memory");
192 break;
8fa9368e
BV
193 default:
194 return SR_ERR_NA;
195 }
196
a90e480c 197 return ret;
8fa9368e
BV
198}
199
584560f1 200static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 201 const struct sr_channel_group *cg)
8fa9368e 202{
7fb8279c 203 struct dev_context *devc;
f157b2ee
BV
204 uint64_t tmp_u64, low, high;
205 unsigned int i;
a90e480c 206 int tmp, ret;
be733919 207 const char *tmp_str;
8fa9368e 208
53b4680f 209 (void)cg;
d3c74a6f 210
b0baddef 211 devc = sdi->priv;
7fb8279c 212
8fa9368e
BV
213 ret = SR_OK;
214 switch (key) {
7fb8279c
BV
215 case SR_CONF_LIMIT_SAMPLES:
216 tmp_u64 = g_variant_get_uint64(data);
217 devc->limit_samples = tmp_u64;
218 ret = SR_OK;
219 break;
e1af0e85 220 case SR_CONF_DATALOG:
0cd9107d 221 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
e1af0e85 222 break;
be733919
BV
223 case SR_CONF_SPL_WEIGHT_FREQ:
224 tmp_str = g_variant_get_string(data, NULL);
225 if (!strcmp(tmp_str, "A"))
226 ret = cem_dt_885x_weight_freq_set(sdi,
227 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
228 else if (!strcmp(tmp_str, "C"))
229 ret = cem_dt_885x_weight_freq_set(sdi,
230 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
231 else
232 return SR_ERR_ARG;
233 break;
1487ce4f
BV
234 case SR_CONF_SPL_WEIGHT_TIME:
235 tmp_str = g_variant_get_string(data, NULL);
236 if (!strcmp(tmp_str, "F"))
237 ret = cem_dt_885x_weight_time_set(sdi,
238 SR_MQFLAG_SPL_TIME_WEIGHT_F);
239 else if (!strcmp(tmp_str, "S"))
240 ret = cem_dt_885x_weight_time_set(sdi,
241 SR_MQFLAG_SPL_TIME_WEIGHT_S);
242 else
243 return SR_ERR_ARG;
244 break;
a90e480c
BV
245 case SR_CONF_HOLD_MAX:
246 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
247 ret = cem_dt_885x_holdmode_set(sdi, tmp);
248 break;
249 case SR_CONF_HOLD_MIN:
250 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
251 ret = cem_dt_885x_holdmode_set(sdi, tmp);
252 break;
f157b2ee
BV
253 case SR_CONF_SPL_MEASUREMENT_RANGE:
254 g_variant_get(data, "(tt)", &low, &high);
255 ret = SR_ERR_ARG;
256 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
257 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
258 ret = cem_dt_885x_meas_range_set(sdi, low, high);
259 break;
260 }
261 }
262 break;
4c22355f
BV
263 case SR_CONF_POWER_OFF:
264 if (g_variant_get_boolean(data))
265 ret = cem_dt_885x_power_off(sdi);
266 break;
662172d4
BV
267 case SR_CONF_DATA_SOURCE:
268 tmp_str = g_variant_get_string(data, NULL);
269 if (!strcmp(tmp_str, "Live"))
270 devc->cur_data_source = DATA_SOURCE_LIVE;
271 else if (!strcmp(tmp_str, "Memory"))
272 devc->cur_data_source = DATA_SOURCE_MEMORY;
273 else
274 return SR_ERR;
cea26f6e 275 devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY;
662172d4 276 break;
8fa9368e
BV
277 default:
278 ret = SR_ERR_NA;
279 }
280
281 return ret;
282}
283
584560f1 284static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 285 const struct sr_channel_group *cg)
8fa9368e 286{
f157b2ee
BV
287 GVariant *tuple, *range[2];
288 GVariantBuilder gvb;
289 unsigned int i;
8fa9368e 290
e66d1892
UH
291 switch (key) {
292 case SR_CONF_SCAN_OPTIONS:
293 case SR_CONF_DEVICE_OPTIONS:
294 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
295 case SR_CONF_SPL_WEIGHT_FREQ:
296 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
297 break;
298 case SR_CONF_SPL_WEIGHT_TIME:
299 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
300 break;
301 case SR_CONF_SPL_MEASUREMENT_RANGE:
302 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
303 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
304 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
305 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
306 tuple = g_variant_new_tuple(range, 2);
307 g_variant_builder_add_value(&gvb, tuple);
f157b2ee 308 }
e66d1892
UH
309 *data = g_variant_builder_end(&gvb);
310 break;
311 case SR_CONF_DATA_SOURCE:
312 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
313 break;
314 default:
315 return SR_ERR_NA;
8fa9368e
BV
316 }
317
a9010323 318 return SR_OK;
8fa9368e
BV
319}
320
695dc859 321static int dev_acquisition_start(const struct sr_dev_inst *sdi)
8fa9368e 322{
e37c4b39
BV
323 struct dev_context *devc;
324 struct sr_serial_dev_inst *serial;
8fa9368e 325
208c1d35 326 devc = sdi->priv;
e37c4b39
BV
327 devc->state = ST_INIT;
328 devc->num_samples = 0;
329 devc->buf_len = 0;
330
bee2b016 331 std_session_send_df_header(sdi);
e37c4b39
BV
332
333 /* Poll every 100ms, or whenever some data comes in. */
334 serial = sdi->conn;
102f1239
BV
335 serial_source_add(sdi->session, serial, G_IO_IN, 150,
336 cem_dt_885x_receive_data, (void *)sdi);
8fa9368e
BV
337
338 return SR_OK;
339}
340
dd5c48a6 341static struct sr_dev_driver cem_dt_885x_driver_info = {
8fa9368e
BV
342 .name = "cem-dt-885x",
343 .longname = "CEM DT-885x",
344 .api_version = 1,
c2fdcc25 345 .init = std_init,
700d6b64 346 .cleanup = std_cleanup,
8fa9368e 347 .scan = scan,
c01bf34c 348 .dev_list = std_dev_list,
f778bf02 349 .dev_clear = std_dev_clear,
8fa9368e
BV
350 .config_get = config_get,
351 .config_set = config_set,
352 .config_list = config_list,
8f878dc6 353 .dev_open = std_serial_dev_open,
bf2c987f 354 .dev_close = std_serial_dev_close,
8fa9368e 355 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 356 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
41812aca 357 .context = NULL,
8fa9368e 358};
dd5c48a6 359SR_REGISTER_DEV_DRIVER(cem_dt_885x_driver_info);