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