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