]> sigrok.org Git - libsigrok.git/blob - hardware/cem-dt-885x/api.c
cem-dt-885x: Support for max/min hold modes
[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                 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
189                 break;
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;
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;
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;
216         default:
217                 return SR_ERR_NA;
218         }
219
220         return ret;
221 }
222
223 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
224 {
225         struct dev_context *devc;
226         uint64_t tmp_u64;
227         int tmp, ret;
228         const char *tmp_str;
229
230         if (sdi->status != SR_ST_ACTIVE)
231                 return SR_ERR_DEV_CLOSED;
232
233         if (!(devc = sdi->priv)) {
234                 sr_err("sdi->priv was NULL.");
235                 return SR_ERR_BUG;
236         }
237
238         ret = SR_OK;
239         switch (key) {
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;
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;
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;
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;
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;
284         default:
285                 ret = SR_ERR_NA;
286         }
287
288         return ret;
289 }
290
291 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
292 {
293         int ret;
294
295         (void)sdi;
296
297         ret = SR_OK;
298         switch (key) {
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;
307         case SR_CONF_SPL_WEIGHT_FREQ:
308                 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
309                 break;
310         case SR_CONF_SPL_WEIGHT_TIME:
311                 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
312                 break;
313         default:
314                 return SR_ERR_NA;
315         }
316
317         return ret;
318 }
319
320 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
321 {
322         struct dev_context *devc;
323         struct sr_serial_dev_inst *serial;
324
325         if (sdi->status != SR_ST_ACTIVE)
326                 return SR_ERR_DEV_CLOSED;
327
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);
345
346         return SR_OK;
347 }
348
349 static 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
356         return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
357                         sdi->conn, LOG_PREFIX);
358
359         return SR_OK;
360 }
361
362 SR_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 };