]> sigrok.org Git - libsigrok.git/blame - hardware/cem-dt-885x/api.c
Add SR_CONF key for sound pressure level measurement range
[libsigrok.git] / 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
27static const int32_t hwopts[] = {
28 SR_CONF_CONN,
29};
30
31static const int32_t hwcaps[] = {
32 SR_CONF_SOUNDLEVELMETER,
33 SR_CONF_LIMIT_SAMPLES,
34 SR_CONF_CONTINUOUS,
e1af0e85 35 SR_CONF_DATALOG,
be733919 36 SR_CONF_SPL_WEIGHT_FREQ,
1487ce4f 37 SR_CONF_SPL_WEIGHT_TIME,
a90e480c
BV
38 SR_CONF_HOLD_MAX,
39 SR_CONF_HOLD_MIN,
7fb8279c
BV
40};
41
be733919
BV
42static const char *weight_freq[] = {
43 "A",
44 "C",
45};
7fb8279c 46
1487ce4f
BV
47static const char *weight_time[] = {
48 "F",
49 "S",
50};
51
8fa9368e
BV
52SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
53static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
54
55
56static int init(struct sr_context *sr_ctx)
57{
58 return std_init(sr_ctx, di, LOG_PREFIX);
59}
60
61static GSList *scan(GSList *options)
62{
63 struct drv_context *drvc;
7fb8279c
BV
64 struct dev_context *devc;
65 struct sr_config *src;
66 struct sr_serial_dev_inst *serial;
67 struct sr_dev_inst *sdi;
68 struct sr_probe *probe;
69 GSList *l, *devices;
70 gint64 start;
71 const char *conn;
72 unsigned char c;
73
74 conn = NULL;
75 for (l = options; l; l = l->next) {
76 src = l->data;
77 if (src->key == SR_CONF_CONN)
78 conn = g_variant_get_string(src->data, NULL);
79 }
80 if (!conn)
81 return NULL;
82
83 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
84 return NULL;
8fa9368e 85
7fb8279c
BV
86 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
87 return NULL;
8fa9368e
BV
88
89 devices = NULL;
90 drvc = di->priv;
7fb8279c
BV
91 start = g_get_monotonic_time();
92 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
93 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
94 /* Found one. */
95 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
96 "DT-885x", NULL)))
97 return NULL;
98
99 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
100 sr_dbg("Device context malloc failed.");
101 return NULL;
102 }
e1af0e85
BV
103 devc->cur_mqflags = 0;
104 devc->recording = -1;
7fb8279c
BV
105
106 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
107 return NULL;
108
109 sdi->inst_type = SR_INST_SERIAL;
110 sdi->priv = devc;
111 sdi->driver = di;
e37c4b39 112 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
7fb8279c
BV
113 return NULL;
114 sdi->probes = g_slist_append(sdi->probes, probe);
115 drvc->instances = g_slist_append(drvc->instances, sdi);
116 devices = g_slist_append(devices, sdi);
117 break;
118 }
119 /* It takes about 1ms for a byte to come in. */
120 g_usleep(1000);
121 }
8fa9368e 122
7fb8279c 123 serial_close(serial);
8fa9368e
BV
124
125 return devices;
126}
127
128static GSList *dev_list(void)
129{
130 struct drv_context *drvc;
131
132 drvc = di->priv;
133
134 return drvc->instances;
135}
136
137static int dev_clear(void)
138{
139 return std_dev_clear(di, NULL);
140}
141
142static int dev_open(struct sr_dev_inst *sdi)
143{
7fb8279c 144 struct sr_serial_dev_inst *serial;
8fa9368e 145
7fb8279c
BV
146 serial = sdi->conn;
147 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
148 return SR_ERR;
8fa9368e
BV
149
150 sdi->status = SR_ST_ACTIVE;
151
152 return SR_OK;
153}
154
155static int dev_close(struct sr_dev_inst *sdi)
156{
7fb8279c 157 struct sr_serial_dev_inst *serial;
8fa9368e 158
7fb8279c
BV
159 serial = sdi->conn;
160 if (serial && serial->fd != -1) {
161 serial_close(serial);
162 sdi->status = SR_ST_INACTIVE;
163 }
8fa9368e
BV
164
165 return SR_OK;
166}
167
168static int cleanup(void)
169{
7fb8279c 170 return dev_clear();
8fa9368e
BV
171}
172
173static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
174{
7fb8279c 175 struct dev_context *devc;
a90e480c 176 int tmp, ret;
8fa9368e 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;
8fa9368e
BV
217 default:
218 return SR_ERR_NA;
219 }
220
a90e480c 221 return ret;
8fa9368e
BV
222}
223
224static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
225{
7fb8279c
BV
226 struct dev_context *devc;
227 uint64_t tmp_u64;
a90e480c 228 int tmp, ret;
be733919 229 const char *tmp_str;
8fa9368e 230
8fa9368e
BV
231 if (sdi->status != SR_ST_ACTIVE)
232 return SR_ERR_DEV_CLOSED;
233
7fb8279c
BV
234 if (!(devc = sdi->priv)) {
235 sr_err("sdi->priv was NULL.");
236 return SR_ERR_BUG;
237 }
238
8fa9368e
BV
239 ret = SR_OK;
240 switch (key) {
7fb8279c
BV
241 case SR_CONF_LIMIT_SAMPLES:
242 tmp_u64 = g_variant_get_uint64(data);
243 devc->limit_samples = tmp_u64;
244 ret = SR_OK;
245 break;
e1af0e85 246 case SR_CONF_DATALOG:
0cd9107d 247 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
e1af0e85 248 break;
be733919
BV
249 case SR_CONF_SPL_WEIGHT_FREQ:
250 tmp_str = g_variant_get_string(data, NULL);
251 if (!strcmp(tmp_str, "A"))
252 ret = cem_dt_885x_weight_freq_set(sdi,
253 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
254 else if (!strcmp(tmp_str, "C"))
255 ret = cem_dt_885x_weight_freq_set(sdi,
256 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
257 else
258 return SR_ERR_ARG;
259 break;
1487ce4f
BV
260 case SR_CONF_SPL_WEIGHT_TIME:
261 tmp_str = g_variant_get_string(data, NULL);
262 if (!strcmp(tmp_str, "F"))
263 ret = cem_dt_885x_weight_time_set(sdi,
264 SR_MQFLAG_SPL_TIME_WEIGHT_F);
265 else if (!strcmp(tmp_str, "S"))
266 ret = cem_dt_885x_weight_time_set(sdi,
267 SR_MQFLAG_SPL_TIME_WEIGHT_S);
268 else
269 return SR_ERR_ARG;
270 break;
a90e480c
BV
271 case SR_CONF_HOLD_MAX:
272 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
273 ret = cem_dt_885x_holdmode_set(sdi, tmp);
274 break;
275 case SR_CONF_HOLD_MIN:
276 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
277 ret = cem_dt_885x_holdmode_set(sdi, tmp);
278 break;
8fa9368e
BV
279 default:
280 ret = SR_ERR_NA;
281 }
282
283 return ret;
284}
285
286static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
287{
288 int ret;
289
290 (void)sdi;
8fa9368e
BV
291
292 ret = SR_OK;
293 switch (key) {
7fb8279c
BV
294 case SR_CONF_SCAN_OPTIONS:
295 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
296 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
297 break;
298 case SR_CONF_DEVICE_OPTIONS:
299 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
300 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
301 break;
be733919
BV
302 case SR_CONF_SPL_WEIGHT_FREQ:
303 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
304 break;
1487ce4f
BV
305 case SR_CONF_SPL_WEIGHT_TIME:
306 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
307 break;
8fa9368e
BV
308 default:
309 return SR_ERR_NA;
310 }
311
312 return ret;
313}
314
e37c4b39 315static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
8fa9368e 316{
e37c4b39
BV
317 struct dev_context *devc;
318 struct sr_serial_dev_inst *serial;
8fa9368e
BV
319
320 if (sdi->status != SR_ST_ACTIVE)
321 return SR_ERR_DEV_CLOSED;
322
e37c4b39
BV
323 if (!(devc = sdi->priv)) {
324 sr_err("sdi->priv was NULL.");
325 return SR_ERR_BUG;
326 }
327
328 devc->cb_data = cb_data;
329 devc->state = ST_INIT;
330 devc->num_samples = 0;
331 devc->buf_len = 0;
332
333 /* Send header packet to the session bus. */
334 std_session_send_df_header(cb_data, LOG_PREFIX);
335
336 /* Poll every 100ms, or whenever some data comes in. */
337 serial = sdi->conn;
338 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
339 (void *)sdi);
8fa9368e
BV
340
341 return SR_OK;
342}
343
344static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
345{
346 (void)cb_data;
347
348 if (sdi->status != SR_ST_ACTIVE)
349 return SR_ERR_DEV_CLOSED;
350
e37c4b39
BV
351 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
352 sdi->conn, LOG_PREFIX);
8fa9368e
BV
353
354 return SR_OK;
355}
356
357SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
358 .name = "cem-dt-885x",
359 .longname = "CEM DT-885x",
360 .api_version = 1,
361 .init = init,
362 .cleanup = cleanup,
363 .scan = scan,
364 .dev_list = dev_list,
365 .dev_clear = dev_clear,
366 .config_get = config_get,
367 .config_set = config_set,
368 .config_list = config_list,
369 .dev_open = dev_open,
370 .dev_close = dev_close,
371 .dev_acquisition_start = dev_acquisition_start,
372 .dev_acquisition_stop = dev_acquisition_stop,
373 .priv = NULL,
374};