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