]> sigrok.org Git - libsigrok.git/blame - src/hardware/cem-dt-885x/api.c
Match std_init() parameter order to the driver init() callback
[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
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 75{
1f8f5bc0 76 return std_init(di, sr_ctx, LOG_PREFIX);
8fa9368e
BV
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
584560f1 139static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 140 const struct sr_channel_group *cg)
8fa9368e 141{
7fb8279c 142 struct dev_context *devc;
f157b2ee
BV
143 GVariant *range[2];
144 uint64_t low, high;
a90e480c 145 int tmp, ret;
8fa9368e 146
53b4680f 147 (void)cg;
d3c74a6f 148
7fb8279c
BV
149 if (!sdi)
150 return SR_ERR_ARG;
8fa9368e 151
7fb8279c 152 devc = sdi->priv;
a90e480c 153 ret = SR_OK;
8fa9368e 154 switch (key) {
7fb8279c
BV
155 case SR_CONF_LIMIT_SAMPLES:
156 *data = g_variant_new_uint64(devc->limit_samples);
157 break;
e1af0e85 158 case SR_CONF_DATALOG:
0cd9107d
BV
159 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
160 *data = g_variant_new_boolean(tmp);
e1af0e85 161 break;
be733919
BV
162 case SR_CONF_SPL_WEIGHT_FREQ:
163 tmp = cem_dt_885x_weight_freq_get(sdi);
164 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
165 *data = g_variant_new_string("A");
166 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
167 *data = g_variant_new_string("C");
168 else
169 return SR_ERR;
170 break;
1487ce4f
BV
171 case SR_CONF_SPL_WEIGHT_TIME:
172 tmp = cem_dt_885x_weight_time_get(sdi);
173 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
174 *data = g_variant_new_string("F");
175 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
176 *data = g_variant_new_string("S");
177 else
178 return SR_ERR;
179 break;
a90e480c
BV
180 case SR_CONF_HOLD_MAX:
181 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
182 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
183 break;
184 case SR_CONF_HOLD_MIN:
185 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
186 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
187 break;
f157b2ee
BV
188 case SR_CONF_SPL_MEASUREMENT_RANGE:
189 if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
190 range[0] = g_variant_new_uint64(low);
191 range[1] = g_variant_new_uint64(high);
192 *data = g_variant_new_tuple(range, 2);
193 }
194 break;
4c22355f
BV
195 case SR_CONF_POWER_OFF:
196 *data = g_variant_new_boolean(FALSE);
197 break;
662172d4
BV
198 case SR_CONF_DATA_SOURCE:
199 if (devc->cur_data_source == DATA_SOURCE_LIVE)
200 *data = g_variant_new_string("Live");
201 else
202 *data = g_variant_new_string("Memory");
203 break;
8fa9368e
BV
204 default:
205 return SR_ERR_NA;
206 }
207
a90e480c 208 return ret;
8fa9368e
BV
209}
210
584560f1 211static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 212 const struct sr_channel_group *cg)
8fa9368e 213{
7fb8279c 214 struct dev_context *devc;
f157b2ee
BV
215 uint64_t tmp_u64, low, high;
216 unsigned int i;
a90e480c 217 int tmp, ret;
be733919 218 const char *tmp_str;
8fa9368e 219
53b4680f 220 (void)cg;
d3c74a6f 221
8fa9368e
BV
222 if (sdi->status != SR_ST_ACTIVE)
223 return SR_ERR_DEV_CLOSED;
224
7fb8279c
BV
225 if (!(devc = sdi->priv)) {
226 sr_err("sdi->priv was NULL.");
227 return SR_ERR_BUG;
228 }
229
8fa9368e
BV
230 ret = SR_OK;
231 switch (key) {
7fb8279c
BV
232 case SR_CONF_LIMIT_SAMPLES:
233 tmp_u64 = g_variant_get_uint64(data);
234 devc->limit_samples = tmp_u64;
235 ret = SR_OK;
236 break;
e1af0e85 237 case SR_CONF_DATALOG:
0cd9107d 238 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
e1af0e85 239 break;
be733919
BV
240 case SR_CONF_SPL_WEIGHT_FREQ:
241 tmp_str = g_variant_get_string(data, NULL);
242 if (!strcmp(tmp_str, "A"))
243 ret = cem_dt_885x_weight_freq_set(sdi,
244 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
245 else if (!strcmp(tmp_str, "C"))
246 ret = cem_dt_885x_weight_freq_set(sdi,
247 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
248 else
249 return SR_ERR_ARG;
250 break;
1487ce4f
BV
251 case SR_CONF_SPL_WEIGHT_TIME:
252 tmp_str = g_variant_get_string(data, NULL);
253 if (!strcmp(tmp_str, "F"))
254 ret = cem_dt_885x_weight_time_set(sdi,
255 SR_MQFLAG_SPL_TIME_WEIGHT_F);
256 else if (!strcmp(tmp_str, "S"))
257 ret = cem_dt_885x_weight_time_set(sdi,
258 SR_MQFLAG_SPL_TIME_WEIGHT_S);
259 else
260 return SR_ERR_ARG;
261 break;
a90e480c
BV
262 case SR_CONF_HOLD_MAX:
263 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
264 ret = cem_dt_885x_holdmode_set(sdi, tmp);
265 break;
266 case SR_CONF_HOLD_MIN:
267 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
268 ret = cem_dt_885x_holdmode_set(sdi, tmp);
269 break;
f157b2ee
BV
270 case SR_CONF_SPL_MEASUREMENT_RANGE:
271 g_variant_get(data, "(tt)", &low, &high);
272 ret = SR_ERR_ARG;
273 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
274 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
275 ret = cem_dt_885x_meas_range_set(sdi, low, high);
276 break;
277 }
278 }
279 break;
4c22355f
BV
280 case SR_CONF_POWER_OFF:
281 if (g_variant_get_boolean(data))
282 ret = cem_dt_885x_power_off(sdi);
283 break;
662172d4
BV
284 case SR_CONF_DATA_SOURCE:
285 tmp_str = g_variant_get_string(data, NULL);
286 if (!strcmp(tmp_str, "Live"))
287 devc->cur_data_source = DATA_SOURCE_LIVE;
288 else if (!strcmp(tmp_str, "Memory"))
289 devc->cur_data_source = DATA_SOURCE_MEMORY;
290 else
291 return SR_ERR;
cea26f6e 292 devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY;
662172d4 293 break;
8fa9368e
BV
294 default:
295 ret = SR_ERR_NA;
296 }
297
298 return ret;
299}
300
584560f1 301static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 302 const struct sr_channel_group *cg)
8fa9368e 303{
f157b2ee
BV
304 GVariant *tuple, *range[2];
305 GVariantBuilder gvb;
306 unsigned int i;
8fa9368e
BV
307 int ret;
308
53b4680f 309 (void)cg;
8fa9368e
BV
310
311 ret = SR_OK;
d7125bfa
BV
312 if (!sdi) {
313 switch (key) {
314 case SR_CONF_SCAN_OPTIONS:
315 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
316 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
317 break;
318 case SR_CONF_DEVICE_OPTIONS:
319 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
023c73ae 320 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
d7125bfa
BV
321 break;
322 default:
323 return SR_ERR_NA;
324 }
325 } else {
326 switch (key) {
327 case SR_CONF_DEVICE_OPTIONS:
328 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
023c73ae 329 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
e742b88f 330 break;
d7125bfa
BV
331 case SR_CONF_SPL_WEIGHT_FREQ:
332 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
333 break;
334 case SR_CONF_SPL_WEIGHT_TIME:
335 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
336 break;
337 case SR_CONF_SPL_MEASUREMENT_RANGE:
338 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
339 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
340 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
341 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
342 tuple = g_variant_new_tuple(range, 2);
343 g_variant_builder_add_value(&gvb, tuple);
344 }
345 *data = g_variant_builder_end(&gvb);
346 break;
347 case SR_CONF_DATA_SOURCE:
348 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
349 break;
350 default:
351 return SR_ERR_NA;
f157b2ee 352 }
8fa9368e
BV
353 }
354
355 return ret;
356}
357
695dc859 358static int dev_acquisition_start(const struct sr_dev_inst *sdi)
8fa9368e 359{
e37c4b39
BV
360 struct dev_context *devc;
361 struct sr_serial_dev_inst *serial;
8fa9368e
BV
362
363 if (sdi->status != SR_ST_ACTIVE)
364 return SR_ERR_DEV_CLOSED;
365
208c1d35 366 devc = sdi->priv;
e37c4b39
BV
367 devc->state = ST_INIT;
368 devc->num_samples = 0;
369 devc->buf_len = 0;
370
695dc859 371 std_session_send_df_header(sdi, LOG_PREFIX);
e37c4b39
BV
372
373 /* Poll every 100ms, or whenever some data comes in. */
374 serial = sdi->conn;
102f1239
BV
375 serial_source_add(sdi->session, serial, G_IO_IN, 150,
376 cem_dt_885x_receive_data, (void *)sdi);
8fa9368e
BV
377
378 return SR_OK;
379}
380
695dc859 381static int dev_acquisition_stop(struct sr_dev_inst *sdi)
8fa9368e 382{
8fa9368e
BV
383 if (sdi->status != SR_ST_ACTIVE)
384 return SR_ERR_DEV_CLOSED;
385
6525d819 386 return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
e37c4b39 387 sdi->conn, LOG_PREFIX);
8fa9368e
BV
388}
389
390SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
391 .name = "cem-dt-885x",
392 .longname = "CEM DT-885x",
393 .api_version = 1,
394 .init = init,
700d6b64 395 .cleanup = std_cleanup,
8fa9368e 396 .scan = scan,
c01bf34c 397 .dev_list = std_dev_list,
a6630742 398 .dev_clear = NULL,
8fa9368e
BV
399 .config_get = config_get,
400 .config_set = config_set,
401 .config_list = config_list,
8f878dc6 402 .dev_open = std_serial_dev_open,
bf2c987f 403 .dev_close = std_serial_dev_close,
8fa9368e
BV
404 .dev_acquisition_start = dev_acquisition_start,
405 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 406 .context = NULL,
8fa9368e 407};