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