]> sigrok.org Git - libsigrok.git/blob - hardware/cem-dt-885x/api.c
cem-dt-885x: Support for changing time weighting
[libsigrok.git] / hardware / cem-dt-885x / api.c
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 <string.h>
21 #include "protocol.h"
22
23 #define SERIALCOMM "9600/8n1"
24 /* 23ms is the longest interval between tokens. */
25 #define MAX_SCAN_TIME 25 * 1000
26
27 static const int32_t hwopts[] = {
28         SR_CONF_CONN,
29 };
30
31 static const int32_t hwcaps[] = {
32         SR_CONF_SOUNDLEVELMETER,
33         SR_CONF_LIMIT_SAMPLES,
34         SR_CONF_CONTINUOUS,
35         SR_CONF_DATALOG,
36         SR_CONF_SPL_WEIGHT_FREQ,
37         SR_CONF_SPL_WEIGHT_TIME,
38 };
39
40 static const char *weight_freq[] = {
41         "A",
42         "C",
43 };
44
45 static const char *weight_time[] = {
46         "F",
47         "S",
48 };
49
50 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
51 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
52
53
54 static int init(struct sr_context *sr_ctx)
55 {
56         return std_init(sr_ctx, di, LOG_PREFIX);
57 }
58
59 static GSList *scan(GSList *options)
60 {
61         struct drv_context *drvc;
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;
83
84         if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
85                 return NULL;
86
87         devices = NULL;
88         drvc = di->priv;
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                         }
101                         devc->cur_mqflags = 0;
102                         devc->recording = -1;
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;
110                         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
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         }
120
121         serial_close(serial);
122
123         return devices;
124 }
125
126 static GSList *dev_list(void)
127 {
128         struct drv_context *drvc;
129
130         drvc = di->priv;
131
132         return drvc->instances;
133 }
134
135 static int dev_clear(void)
136 {
137         return std_dev_clear(di, NULL);
138 }
139
140 static int dev_open(struct sr_dev_inst *sdi)
141 {
142         struct sr_serial_dev_inst *serial;
143
144         serial = sdi->conn;
145         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
146                 return SR_ERR;
147
148         sdi->status = SR_ST_ACTIVE;
149
150         return SR_OK;
151 }
152
153 static int dev_close(struct sr_dev_inst *sdi)
154 {
155         struct sr_serial_dev_inst *serial;
156
157         serial = sdi->conn;
158         if (serial && serial->fd != -1) {
159                 serial_close(serial);
160                 sdi->status = SR_ST_INACTIVE;
161         }
162
163         return SR_OK;
164 }
165
166 static int cleanup(void)
167 {
168         return dev_clear();
169 }
170
171 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
172 {
173         struct dev_context *devc;
174         int tmp;
175
176         if (!sdi)
177                 return SR_ERR_ARG;
178
179         devc = sdi->priv;
180         switch (key) {
181         case SR_CONF_LIMIT_SAMPLES:
182                 *data = g_variant_new_uint64(devc->limit_samples);
183                 break;
184         case SR_CONF_DATALOG:
185                 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
186                 break;
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;
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;
205         default:
206                 return SR_ERR_NA;
207         }
208
209         return SR_OK;
210 }
211
212 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
213 {
214         struct dev_context *devc;
215         uint64_t tmp_u64;
216         int ret;
217         const char *tmp_str;
218
219         if (sdi->status != SR_ST_ACTIVE)
220                 return SR_ERR_DEV_CLOSED;
221
222         if (!(devc = sdi->priv)) {
223                 sr_err("sdi->priv was NULL.");
224                 return SR_ERR_BUG;
225         }
226
227         ret = SR_OK;
228         switch (key) {
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;
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;
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;
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;
265         default:
266                 ret = SR_ERR_NA;
267         }
268
269         return ret;
270 }
271
272 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
273 {
274         int ret;
275
276         (void)sdi;
277
278         ret = SR_OK;
279         switch (key) {
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;
288         case SR_CONF_SPL_WEIGHT_FREQ:
289                 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
290                 break;
291         case SR_CONF_SPL_WEIGHT_TIME:
292                 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
293                 break;
294         default:
295                 return SR_ERR_NA;
296         }
297
298         return ret;
299 }
300
301 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
302 {
303         struct dev_context *devc;
304         struct sr_serial_dev_inst *serial;
305
306         if (sdi->status != SR_ST_ACTIVE)
307                 return SR_ERR_DEV_CLOSED;
308
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);
326
327         return SR_OK;
328 }
329
330 static 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
337         return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
338                         sdi->conn, LOG_PREFIX);
339
340         return SR_OK;
341 }
342
343 SR_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 };