]> sigrok.org Git - libsigrok.git/blame - hardware/cem-dt-885x/api.c
Add SR_CONF keys for sound pressure level time and frequency weighting
[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
20#include "protocol.h"
21
7fb8279c
BV
22#define SERIALCOMM "9600/8n1"
23/* 23ms is the longest interval between tokens. */
24#define MAX_SCAN_TIME 25 * 1000
25
26static const int32_t hwopts[] = {
27 SR_CONF_CONN,
28};
29
30static const int32_t hwcaps[] = {
31 SR_CONF_SOUNDLEVELMETER,
32 SR_CONF_LIMIT_SAMPLES,
33 SR_CONF_CONTINUOUS,
e1af0e85 34 SR_CONF_DATALOG,
7fb8279c
BV
35};
36
37
8fa9368e
BV
38SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
39static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
40
41
42static int init(struct sr_context *sr_ctx)
43{
44 return std_init(sr_ctx, di, LOG_PREFIX);
45}
46
47static GSList *scan(GSList *options)
48{
49 struct drv_context *drvc;
7fb8279c
BV
50 struct dev_context *devc;
51 struct sr_config *src;
52 struct sr_serial_dev_inst *serial;
53 struct sr_dev_inst *sdi;
54 struct sr_probe *probe;
55 GSList *l, *devices;
56 gint64 start;
57 const char *conn;
58 unsigned char c;
59
60 conn = NULL;
61 for (l = options; l; l = l->next) {
62 src = l->data;
63 if (src->key == SR_CONF_CONN)
64 conn = g_variant_get_string(src->data, NULL);
65 }
66 if (!conn)
67 return NULL;
68
69 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
70 return NULL;
8fa9368e 71
7fb8279c
BV
72 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
73 return NULL;
8fa9368e
BV
74
75 devices = NULL;
76 drvc = di->priv;
7fb8279c
BV
77 start = g_get_monotonic_time();
78 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
79 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
80 /* Found one. */
81 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
82 "DT-885x", NULL)))
83 return NULL;
84
85 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
86 sr_dbg("Device context malloc failed.");
87 return NULL;
88 }
e1af0e85
BV
89 devc->cur_mqflags = 0;
90 devc->recording = -1;
7fb8279c
BV
91
92 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
93 return NULL;
94
95 sdi->inst_type = SR_INST_SERIAL;
96 sdi->priv = devc;
97 sdi->driver = di;
e37c4b39 98 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
7fb8279c
BV
99 return NULL;
100 sdi->probes = g_slist_append(sdi->probes, probe);
101 drvc->instances = g_slist_append(drvc->instances, sdi);
102 devices = g_slist_append(devices, sdi);
103 break;
104 }
105 /* It takes about 1ms for a byte to come in. */
106 g_usleep(1000);
107 }
8fa9368e 108
7fb8279c 109 serial_close(serial);
8fa9368e
BV
110
111 return devices;
112}
113
114static GSList *dev_list(void)
115{
116 struct drv_context *drvc;
117
118 drvc = di->priv;
119
120 return drvc->instances;
121}
122
123static int dev_clear(void)
124{
125 return std_dev_clear(di, NULL);
126}
127
128static int dev_open(struct sr_dev_inst *sdi)
129{
7fb8279c 130 struct sr_serial_dev_inst *serial;
8fa9368e 131
7fb8279c
BV
132 serial = sdi->conn;
133 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
134 return SR_ERR;
8fa9368e
BV
135
136 sdi->status = SR_ST_ACTIVE;
137
138 return SR_OK;
139}
140
141static int dev_close(struct sr_dev_inst *sdi)
142{
7fb8279c 143 struct sr_serial_dev_inst *serial;
8fa9368e 144
7fb8279c
BV
145 serial = sdi->conn;
146 if (serial && serial->fd != -1) {
147 serial_close(serial);
148 sdi->status = SR_ST_INACTIVE;
149 }
8fa9368e
BV
150
151 return SR_OK;
152}
153
154static int cleanup(void)
155{
7fb8279c 156 return dev_clear();
8fa9368e
BV
157}
158
159static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
160{
7fb8279c 161 struct dev_context *devc;
8fa9368e 162
7fb8279c
BV
163 if (!sdi)
164 return SR_ERR_ARG;
8fa9368e 165
7fb8279c 166 devc = sdi->priv;
8fa9368e 167 switch (key) {
7fb8279c
BV
168 case SR_CONF_LIMIT_SAMPLES:
169 *data = g_variant_new_uint64(devc->limit_samples);
170 break;
e1af0e85
BV
171 case SR_CONF_DATALOG:
172 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
173 break;
8fa9368e
BV
174 default:
175 return SR_ERR_NA;
176 }
177
7fb8279c 178 return SR_OK;
8fa9368e
BV
179}
180
181static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
182{
7fb8279c
BV
183 struct dev_context *devc;
184 uint64_t tmp_u64;
8fa9368e
BV
185 int ret;
186
8fa9368e
BV
187 if (sdi->status != SR_ST_ACTIVE)
188 return SR_ERR_DEV_CLOSED;
189
7fb8279c
BV
190 if (!(devc = sdi->priv)) {
191 sr_err("sdi->priv was NULL.");
192 return SR_ERR_BUG;
193 }
194
8fa9368e
BV
195 ret = SR_OK;
196 switch (key) {
7fb8279c
BV
197 case SR_CONF_LIMIT_SAMPLES:
198 tmp_u64 = g_variant_get_uint64(data);
199 devc->limit_samples = tmp_u64;
200 ret = SR_OK;
201 break;
e1af0e85
BV
202 case SR_CONF_DATALOG:
203 if (g_variant_get_boolean(data)) {
204 /* Start logging. */
205 ret = cem_dt_885x_recording_set(sdi, TRUE);
206 } else {
207 /* Stop logging. */
208 ret = cem_dt_885x_recording_set(sdi, FALSE);
209 }
210 break;
8fa9368e
BV
211 default:
212 ret = SR_ERR_NA;
213 }
214
215 return ret;
216}
217
218static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
219{
220 int ret;
221
222 (void)sdi;
8fa9368e
BV
223
224 ret = SR_OK;
225 switch (key) {
7fb8279c
BV
226 case SR_CONF_SCAN_OPTIONS:
227 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
228 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
229 break;
230 case SR_CONF_DEVICE_OPTIONS:
231 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
232 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
233 break;
8fa9368e
BV
234 default:
235 return SR_ERR_NA;
236 }
237
238 return ret;
239}
240
e37c4b39 241static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
8fa9368e 242{
e37c4b39
BV
243 struct dev_context *devc;
244 struct sr_serial_dev_inst *serial;
8fa9368e
BV
245
246 if (sdi->status != SR_ST_ACTIVE)
247 return SR_ERR_DEV_CLOSED;
248
e37c4b39
BV
249 if (!(devc = sdi->priv)) {
250 sr_err("sdi->priv was NULL.");
251 return SR_ERR_BUG;
252 }
253
254 devc->cb_data = cb_data;
255 devc->state = ST_INIT;
256 devc->num_samples = 0;
257 devc->buf_len = 0;
258
259 /* Send header packet to the session bus. */
260 std_session_send_df_header(cb_data, LOG_PREFIX);
261
262 /* Poll every 100ms, or whenever some data comes in. */
263 serial = sdi->conn;
264 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
265 (void *)sdi);
8fa9368e
BV
266
267 return SR_OK;
268}
269
270static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
271{
272 (void)cb_data;
273
274 if (sdi->status != SR_ST_ACTIVE)
275 return SR_ERR_DEV_CLOSED;
276
e37c4b39
BV
277 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
278 sdi->conn, LOG_PREFIX);
8fa9368e
BV
279
280 return SR_OK;
281}
282
283SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
284 .name = "cem-dt-885x",
285 .longname = "CEM DT-885x",
286 .api_version = 1,
287 .init = init,
288 .cleanup = cleanup,
289 .scan = scan,
290 .dev_list = dev_list,
291 .dev_clear = dev_clear,
292 .config_get = config_get,
293 .config_set = config_set,
294 .config_list = config_list,
295 .dev_open = dev_open,
296 .dev_close = dev_close,
297 .dev_acquisition_start = dev_acquisition_start,
298 .dev_acquisition_stop = dev_acquisition_stop,
299 .priv = NULL,
300};