]> sigrok.org Git - libsigrok.git/blame - hardware/cem-dt-885x/api.c
cem-dt-885x: Support 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,
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
BV
187 case SR_CONF_DATALOG:
188 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
189 break;
be733919
BV
190 case SR_CONF_SPL_WEIGHT_FREQ:
191 tmp = cem_dt_885x_weight_freq_get(sdi);
192 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
193 *data = g_variant_new_string("A");
194 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
195 *data = g_variant_new_string("C");
196 else
197 return SR_ERR;
198 break;
1487ce4f
BV
199 case SR_CONF_SPL_WEIGHT_TIME:
200 tmp = cem_dt_885x_weight_time_get(sdi);
201 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
202 *data = g_variant_new_string("F");
203 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
204 *data = g_variant_new_string("S");
205 else
206 return SR_ERR;
207 break;
a90e480c
BV
208 case SR_CONF_HOLD_MAX:
209 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
210 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
211 break;
212 case SR_CONF_HOLD_MIN:
213 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
214 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
215 break;
8fa9368e
BV
216 default:
217 return SR_ERR_NA;
218 }
219
a90e480c 220 return ret;
8fa9368e
BV
221}
222
223static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
224{
7fb8279c
BV
225 struct dev_context *devc;
226 uint64_t tmp_u64;
a90e480c 227 int tmp, ret;
be733919 228 const char *tmp_str;
8fa9368e 229
8fa9368e
BV
230 if (sdi->status != SR_ST_ACTIVE)
231 return SR_ERR_DEV_CLOSED;
232
7fb8279c
BV
233 if (!(devc = sdi->priv)) {
234 sr_err("sdi->priv was NULL.");
235 return SR_ERR_BUG;
236 }
237
8fa9368e
BV
238 ret = SR_OK;
239 switch (key) {
7fb8279c
BV
240 case SR_CONF_LIMIT_SAMPLES:
241 tmp_u64 = g_variant_get_uint64(data);
242 devc->limit_samples = tmp_u64;
243 ret = SR_OK;
244 break;
e1af0e85
BV
245 case SR_CONF_DATALOG:
246 if (g_variant_get_boolean(data)) {
247 /* Start logging. */
248 ret = cem_dt_885x_recording_set(sdi, TRUE);
249 } else {
250 /* Stop logging. */
251 ret = cem_dt_885x_recording_set(sdi, FALSE);
252 }
253 break;
be733919
BV
254 case SR_CONF_SPL_WEIGHT_FREQ:
255 tmp_str = g_variant_get_string(data, NULL);
256 if (!strcmp(tmp_str, "A"))
257 ret = cem_dt_885x_weight_freq_set(sdi,
258 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
259 else if (!strcmp(tmp_str, "C"))
260 ret = cem_dt_885x_weight_freq_set(sdi,
261 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
262 else
263 return SR_ERR_ARG;
264 break;
1487ce4f
BV
265 case SR_CONF_SPL_WEIGHT_TIME:
266 tmp_str = g_variant_get_string(data, NULL);
267 if (!strcmp(tmp_str, "F"))
268 ret = cem_dt_885x_weight_time_set(sdi,
269 SR_MQFLAG_SPL_TIME_WEIGHT_F);
270 else if (!strcmp(tmp_str, "S"))
271 ret = cem_dt_885x_weight_time_set(sdi,
272 SR_MQFLAG_SPL_TIME_WEIGHT_S);
273 else
274 return SR_ERR_ARG;
275 break;
a90e480c
BV
276 case SR_CONF_HOLD_MAX:
277 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
278 ret = cem_dt_885x_holdmode_set(sdi, tmp);
279 break;
280 case SR_CONF_HOLD_MIN:
281 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
282 ret = cem_dt_885x_holdmode_set(sdi, tmp);
283 break;
8fa9368e
BV
284 default:
285 ret = SR_ERR_NA;
286 }
287
288 return ret;
289}
290
291static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
292{
293 int ret;
294
295 (void)sdi;
8fa9368e
BV
296
297 ret = SR_OK;
298 switch (key) {
7fb8279c
BV
299 case SR_CONF_SCAN_OPTIONS:
300 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
301 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
302 break;
303 case SR_CONF_DEVICE_OPTIONS:
304 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
305 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
306 break;
be733919
BV
307 case SR_CONF_SPL_WEIGHT_FREQ:
308 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
309 break;
1487ce4f
BV
310 case SR_CONF_SPL_WEIGHT_TIME:
311 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
312 break;
8fa9368e
BV
313 default:
314 return SR_ERR_NA;
315 }
316
317 return ret;
318}
319
e37c4b39 320static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
8fa9368e 321{
e37c4b39
BV
322 struct dev_context *devc;
323 struct sr_serial_dev_inst *serial;
8fa9368e
BV
324
325 if (sdi->status != SR_ST_ACTIVE)
326 return SR_ERR_DEV_CLOSED;
327
e37c4b39
BV
328 if (!(devc = sdi->priv)) {
329 sr_err("sdi->priv was NULL.");
330 return SR_ERR_BUG;
331 }
332
333 devc->cb_data = cb_data;
334 devc->state = ST_INIT;
335 devc->num_samples = 0;
336 devc->buf_len = 0;
337
338 /* Send header packet to the session bus. */
339 std_session_send_df_header(cb_data, LOG_PREFIX);
340
341 /* Poll every 100ms, or whenever some data comes in. */
342 serial = sdi->conn;
343 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
344 (void *)sdi);
8fa9368e
BV
345
346 return SR_OK;
347}
348
349static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
350{
351 (void)cb_data;
352
353 if (sdi->status != SR_ST_ACTIVE)
354 return SR_ERR_DEV_CLOSED;
355
e37c4b39
BV
356 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
357 sdi->conn, LOG_PREFIX);
8fa9368e
BV
358
359 return SR_OK;
360}
361
362SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
363 .name = "cem-dt-885x",
364 .longname = "CEM DT-885x",
365 .api_version = 1,
366 .init = init,
367 .cleanup = cleanup,
368 .scan = scan,
369 .dev_list = dev_list,
370 .dev_clear = dev_clear,
371 .config_get = config_get,
372 .config_set = config_set,
373 .config_list = config_list,
374 .dev_open = dev_open,
375 .dev_close = dev_close,
376 .dev_acquisition_start = dev_acquisition_start,
377 .dev_acquisition_stop = dev_acquisition_stop,
378 .priv = NULL,
379};