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