]> sigrok.org Git - libsigrok.git/blame - src/hardware/cem-dt-885x/api.c
Fix #442 by renaming sr_dev_driver.priv to .context
[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 23#define SERIALCOMM "9600/8n1"
1beccaed 24
7fb8279c 25/* 23ms is the longest interval between tokens. */
1a46cc62 26#define MAX_SCAN_TIME_US (25 * 1000)
7fb8279c 27
a0e0bb41 28static const uint32_t scanopts[] = {
7fb8279c
BV
29 SR_CONF_CONN,
30};
31
023c73ae 32static const uint32_t drvopts[] = {
7fb8279c 33 SR_CONF_SOUNDLEVELMETER,
d7125bfa
BV
34};
35
023c73ae
BV
36static const uint32_t devopts[] = {
37 SR_CONF_CONTINUOUS | SR_CONF_SET,
38 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
5827f61b
BV
39 SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET,
43 SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
7fb8279c
BV
47};
48
be733919
BV
49static const char *weight_freq[] = {
50 "A",
51 "C",
52};
7fb8279c 53
1487ce4f
BV
54static const char *weight_time[] = {
55 "F",
56 "S",
57};
58
f157b2ee
BV
59static const uint64_t meas_ranges[][2] = {
60 { 30, 130 },
61 { 30, 80 },
62 { 50, 100 },
63 { 80, 130 },
64};
65
662172d4
BV
66static const char *data_sources[] = {
67 "Live",
68 "Memory",
69};
8fa9368e 70
1beccaed 71SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
8fa9368e 72
4f840ce9 73static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
8fa9368e
BV
74{
75 return std_init(sr_ctx, di, LOG_PREFIX);
76}
77
4f840ce9 78static GSList *scan(struct sr_dev_driver *di, GSList *options)
8fa9368e
BV
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;
41812aca 105 drvc = di->context;
7fb8279c 106 start = g_get_monotonic_time();
1a46cc62 107 while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) {
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
4f840ce9 138static GSList *dev_list(const struct sr_dev_driver *di)
8fa9368e 139{
41812aca 140 return ((struct drv_context *)(di->context))->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
4f840ce9 156static int cleanup(const struct sr_dev_driver *di)
8fa9368e 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));
e742b88f 352 break;
d7125bfa
BV
353 case SR_CONF_SPL_WEIGHT_FREQ:
354 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
355 break;
356 case SR_CONF_SPL_WEIGHT_TIME:
357 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
358 break;
359 case SR_CONF_SPL_MEASUREMENT_RANGE:
360 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
361 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
362 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
363 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
364 tuple = g_variant_new_tuple(range, 2);
365 g_variant_builder_add_value(&gvb, tuple);
366 }
367 *data = g_variant_builder_end(&gvb);
368 break;
369 case SR_CONF_DATA_SOURCE:
370 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
371 break;
372 default:
373 return SR_ERR_NA;
f157b2ee 374 }
8fa9368e
BV
375 }
376
377 return ret;
378}
379
e37c4b39 380static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
8fa9368e 381{
e37c4b39
BV
382 struct dev_context *devc;
383 struct sr_serial_dev_inst *serial;
8fa9368e
BV
384
385 if (sdi->status != SR_ST_ACTIVE)
386 return SR_ERR_DEV_CLOSED;
387
e37c4b39
BV
388 if (!(devc = sdi->priv)) {
389 sr_err("sdi->priv was NULL.");
390 return SR_ERR_BUG;
391 }
392
393 devc->cb_data = cb_data;
394 devc->state = ST_INIT;
395 devc->num_samples = 0;
396 devc->buf_len = 0;
397
398 /* Send header packet to the session bus. */
399 std_session_send_df_header(cb_data, LOG_PREFIX);
400
401 /* Poll every 100ms, or whenever some data comes in. */
402 serial = sdi->conn;
102f1239
BV
403 serial_source_add(sdi->session, serial, G_IO_IN, 150,
404 cem_dt_885x_receive_data, (void *)sdi);
8fa9368e
BV
405
406 return SR_OK;
407}
408
409static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
410{
8fa9368e
BV
411 if (sdi->status != SR_ST_ACTIVE)
412 return SR_ERR_DEV_CLOSED;
413
d43b0908 414 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
e37c4b39 415 sdi->conn, LOG_PREFIX);
8fa9368e
BV
416}
417
418SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
419 .name = "cem-dt-885x",
420 .longname = "CEM DT-885x",
421 .api_version = 1,
422 .init = init,
423 .cleanup = cleanup,
424 .scan = scan,
425 .dev_list = dev_list,
a6630742 426 .dev_clear = NULL,
8fa9368e
BV
427 .config_get = config_get,
428 .config_set = config_set,
429 .config_list = config_list,
430 .dev_open = dev_open,
bf2c987f 431 .dev_close = std_serial_dev_close,
8fa9368e
BV
432 .dev_acquisition_start = dev_acquisition_start,
433 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 434 .context = NULL,
8fa9368e 435};