]> sigrok.org Git - libsigrok.git/blob - hardware/cem-dt-885x/api.c
cem-dt-885x: Fix datalog on/off setting in max/min hold mode
[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         SR_CONF_HOLD_MAX,
39         SR_CONF_HOLD_MIN,
40 };
41
42 static const char *weight_freq[] = {
43         "A",
44         "C",
45 };
46
47 static const char *weight_time[] = {
48         "F",
49         "S",
50 };
51
52 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
53 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
54
55
56 static int init(struct sr_context *sr_ctx)
57 {
58         return std_init(sr_ctx, di, LOG_PREFIX);
59 }
60
61 static GSList *scan(GSList *options)
62 {
63         struct drv_context *drvc;
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;
85
86         if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
87                 return NULL;
88
89         devices = NULL;
90         drvc = di->priv;
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                         }
103                         devc->cur_mqflags = 0;
104                         devc->recording = -1;
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;
112                         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
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         }
122
123         serial_close(serial);
124
125         return devices;
126 }
127
128 static GSList *dev_list(void)
129 {
130         struct drv_context *drvc;
131
132         drvc = di->priv;
133
134         return drvc->instances;
135 }
136
137 static int dev_clear(void)
138 {
139         return std_dev_clear(di, NULL);
140 }
141
142 static int dev_open(struct sr_dev_inst *sdi)
143 {
144         struct sr_serial_dev_inst *serial;
145
146         serial = sdi->conn;
147         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
148                 return SR_ERR;
149
150         sdi->status = SR_ST_ACTIVE;
151
152         return SR_OK;
153 }
154
155 static int dev_close(struct sr_dev_inst *sdi)
156 {
157         struct sr_serial_dev_inst *serial;
158
159         serial = sdi->conn;
160         if (serial && serial->fd != -1) {
161                 serial_close(serial);
162                 sdi->status = SR_ST_INACTIVE;
163         }
164
165         return SR_OK;
166 }
167
168 static int cleanup(void)
169 {
170         return dev_clear();
171 }
172
173 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
174 {
175         struct dev_context *devc;
176         int tmp, ret;
177
178         if (!sdi)
179                 return SR_ERR_ARG;
180
181         devc = sdi->priv;
182         ret = SR_OK;
183         switch (key) {
184         case SR_CONF_LIMIT_SAMPLES:
185                 *data = g_variant_new_uint64(devc->limit_samples);
186                 break;
187         case SR_CONF_DATALOG:
188                 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
189                         *data = g_variant_new_boolean(tmp);
190                 break;
191         case SR_CONF_SPL_WEIGHT_FREQ:
192                 tmp = cem_dt_885x_weight_freq_get(sdi);
193                 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
194                         *data = g_variant_new_string("A");
195                 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
196                         *data = g_variant_new_string("C");
197                 else
198                         return SR_ERR;
199                 break;
200         case SR_CONF_SPL_WEIGHT_TIME:
201                 tmp = cem_dt_885x_weight_time_get(sdi);
202                 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
203                         *data = g_variant_new_string("F");
204                 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
205                         *data = g_variant_new_string("S");
206                 else
207                         return SR_ERR;
208                 break;
209         case SR_CONF_HOLD_MAX:
210                 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
211                         *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
212                 break;
213         case SR_CONF_HOLD_MIN:
214                 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
215                         *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
216                 break;
217         default:
218                 return SR_ERR_NA;
219         }
220
221         return ret;
222 }
223
224 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
225 {
226         struct dev_context *devc;
227         uint64_t tmp_u64;
228         int tmp, ret;
229         const char *tmp_str;
230
231         if (sdi->status != SR_ST_ACTIVE)
232                 return SR_ERR_DEV_CLOSED;
233
234         if (!(devc = sdi->priv)) {
235                 sr_err("sdi->priv was NULL.");
236                 return SR_ERR_BUG;
237         }
238
239         ret = SR_OK;
240         switch (key) {
241         case SR_CONF_LIMIT_SAMPLES:
242                 tmp_u64 = g_variant_get_uint64(data);
243                 devc->limit_samples = tmp_u64;
244                 ret = SR_OK;
245                 break;
246         case SR_CONF_DATALOG:
247                 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
248                 break;
249         case SR_CONF_SPL_WEIGHT_FREQ:
250                 tmp_str = g_variant_get_string(data, NULL);
251                 if (!strcmp(tmp_str, "A"))
252                         ret = cem_dt_885x_weight_freq_set(sdi,
253                                         SR_MQFLAG_SPL_FREQ_WEIGHT_A);
254                 else if (!strcmp(tmp_str, "C"))
255                         ret = cem_dt_885x_weight_freq_set(sdi,
256                                         SR_MQFLAG_SPL_FREQ_WEIGHT_C);
257                 else
258                         return SR_ERR_ARG;
259                 break;
260         case SR_CONF_SPL_WEIGHT_TIME:
261                 tmp_str = g_variant_get_string(data, NULL);
262                 if (!strcmp(tmp_str, "F"))
263                         ret = cem_dt_885x_weight_time_set(sdi,
264                                         SR_MQFLAG_SPL_TIME_WEIGHT_F);
265                 else if (!strcmp(tmp_str, "S"))
266                         ret = cem_dt_885x_weight_time_set(sdi,
267                                         SR_MQFLAG_SPL_TIME_WEIGHT_S);
268                 else
269                         return SR_ERR_ARG;
270                 break;
271         case SR_CONF_HOLD_MAX:
272                 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
273                 ret = cem_dt_885x_holdmode_set(sdi, tmp);
274                 break;
275         case SR_CONF_HOLD_MIN:
276                 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
277                 ret = cem_dt_885x_holdmode_set(sdi, tmp);
278                 break;
279         default:
280                 ret = SR_ERR_NA;
281         }
282
283         return ret;
284 }
285
286 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
287 {
288         int ret;
289
290         (void)sdi;
291
292         ret = SR_OK;
293         switch (key) {
294         case SR_CONF_SCAN_OPTIONS:
295                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
296                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
297                 break;
298         case SR_CONF_DEVICE_OPTIONS:
299                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
300                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
301                 break;
302         case SR_CONF_SPL_WEIGHT_FREQ:
303                 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
304                 break;
305         case SR_CONF_SPL_WEIGHT_TIME:
306                 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
307                 break;
308         default:
309                 return SR_ERR_NA;
310         }
311
312         return ret;
313 }
314
315 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
316 {
317         struct dev_context *devc;
318         struct sr_serial_dev_inst *serial;
319
320         if (sdi->status != SR_ST_ACTIVE)
321                 return SR_ERR_DEV_CLOSED;
322
323         if (!(devc = sdi->priv)) {
324                 sr_err("sdi->priv was NULL.");
325                 return SR_ERR_BUG;
326         }
327
328         devc->cb_data = cb_data;
329         devc->state = ST_INIT;
330         devc->num_samples = 0;
331         devc->buf_len = 0;
332
333         /* Send header packet to the session bus. */
334         std_session_send_df_header(cb_data, LOG_PREFIX);
335
336         /* Poll every 100ms, or whenever some data comes in. */
337         serial = sdi->conn;
338         sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
339                         (void *)sdi);
340
341         return SR_OK;
342 }
343
344 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
345 {
346         (void)cb_data;
347
348         if (sdi->status != SR_ST_ACTIVE)
349                 return SR_ERR_DEV_CLOSED;
350
351         return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
352                         sdi->conn, LOG_PREFIX);
353
354         return SR_OK;
355 }
356
357 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
358         .name = "cem-dt-885x",
359         .longname = "CEM DT-885x",
360         .api_version = 1,
361         .init = init,
362         .cleanup = cleanup,
363         .scan = scan,
364         .dev_list = dev_list,
365         .dev_clear = dev_clear,
366         .config_get = config_get,
367         .config_set = config_set,
368         .config_list = config_list,
369         .dev_open = dev_open,
370         .dev_close = dev_close,
371         .dev_acquisition_start = dev_acquisition_start,
372         .dev_acquisition_stop = dev_acquisition_stop,
373         .priv = NULL,
374 };