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