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