]> sigrok.org Git - libsigrok.git/blob - hardware/colead-slm/api.c
colead-slm: SL-5868P support
[libsigrok.git] / hardware / colead-slm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 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 <glib.h>
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23 #include "protocol.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <string.h>
29
30 static const int hwopts[] = {
31         SR_HWOPT_CONN,
32         SR_HWOPT_SERIALCOMM,
33         0,
34 };
35
36 static const int hwcaps[] = {
37         SR_HWCAP_SOUNDLEVELMETER,
38         SR_HWCAP_LIMIT_SAMPLES,
39         SR_HWCAP_LIMIT_MSEC,
40         SR_HWCAP_CONTINUOUS,
41         0,
42 };
43
44 static const char *probe_names[] = {
45         "P1",
46         NULL,
47 };
48
49 SR_PRIV struct sr_dev_driver colead_slm_driver_info;
50 static struct sr_dev_driver *di = &colead_slm_driver_info;
51
52 /* Properly close and free all devices. */
53 static int clear_instances(void)
54 {
55         struct sr_dev_inst *sdi;
56         struct drv_context *drvc;
57         struct dev_context *devc;
58         GSList *l;
59
60         drvc = di->priv;
61         for (l = drvc->instances; l; l = l->next) {
62                 if (!(sdi = l->data))
63                         continue;
64                 if (!(devc = sdi->priv))
65                         continue;
66                 sr_serial_dev_inst_free(devc->serial);
67                 sr_dev_inst_free(sdi);
68         }
69         g_slist_free(drvc->instances);
70         drvc->instances = NULL;
71
72         return SR_OK;
73 }
74
75 static int hw_init(void)
76 {
77         struct drv_context *drvc;
78
79         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
80                 sr_err("Driver context malloc failed.");
81                 return SR_ERR;
82         }
83
84         di->priv = drvc;
85
86         return SR_OK;
87 }
88
89 static GSList *hw_scan(GSList *options)
90 {
91         struct drv_context *drvc;
92         struct dev_context *devc;
93         struct sr_dev_inst *sdi;
94         struct sr_hwopt *opt;
95         struct sr_probe *probe;
96         GSList *devices, *l;
97         const char *conn, *serialcomm;
98
99         devices = NULL;
100         drvc = di->priv;
101         drvc->instances = NULL;
102
103         conn = serialcomm = NULL;
104         for (l = options; l; l = l->next) {
105                 opt = l->data;
106                 switch (opt->hwopt) {
107                 case SR_HWOPT_CONN:
108                         conn = opt->value;
109                         break;
110                 case SR_HWOPT_SERIALCOMM:
111                         serialcomm = opt->value;
112                         break;
113                 }
114         }
115         if (!conn)
116                 return NULL;
117
118         if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
119                         "SL-5868P", NULL)))
120                 return NULL;
121
122         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
123                 sr_dbg("failed to malloc devc");
124                 return NULL;
125         }
126
127         if (!serialcomm)
128                 /* The Colead SL-5868P uses this. */
129                 serialcomm = "2400/8n1";
130
131         devc->serial = sr_serial_dev_inst_new(conn, -1);
132         devc->serialcomm = g_strdup(serialcomm);
133         sdi->priv = devc;
134         sdi->driver = di;
135         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
136                 return NULL;
137         sdi->probes = g_slist_append(sdi->probes, probe);
138         drvc->instances = g_slist_append(drvc->instances, sdi);
139         devices = g_slist_append(devices, sdi);
140
141         return devices;
142 }
143
144 static GSList *hw_dev_list(void)
145 {
146         struct drv_context *drvc;
147
148         drvc = di->priv;
149
150         return drvc->instances;
151 }
152
153 static int hw_dev_open(struct sr_dev_inst *sdi)
154 {
155         struct dev_context *devc;
156
157         if (!(devc = sdi->priv)) {
158                 sr_err("sdi->priv was NULL.");
159                 return SR_ERR_BUG;
160         }
161
162         sr_dbg("opening %s with %s", devc->serial->port, devc->serialcomm);
163         devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
164         if (devc->serial->fd == -1) {
165                 sr_err("Couldn't open serial port '%s'.",
166                        devc->serial->port);
167                 return SR_ERR;
168         }
169         if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
170                 sr_err("Unable to set serial parameters.");
171                 return SR_ERR;
172         }
173         sdi->status = SR_ST_ACTIVE;
174
175         return SR_OK;
176 }
177
178 static int hw_dev_close(struct sr_dev_inst *sdi)
179 {
180         struct dev_context *devc;
181
182         if (!(devc = sdi->priv)) {
183                 sr_err("sdi->priv was NULL.");
184                 return SR_ERR_BUG;
185         }
186
187         if (devc->serial && devc->serial->fd != -1) {
188                 serial_close(devc->serial->fd);
189                 devc->serial->fd = -1;
190                 sdi->status = SR_ST_INACTIVE;
191         }
192
193         return SR_OK;
194 }
195
196 static int hw_cleanup(void)
197 {
198         clear_instances();
199
200         return SR_OK;
201 }
202
203 static int hw_info_get(int info_id, const void **data,
204                        const struct sr_dev_inst *sdi)
205 {
206         
207         (void)sdi;
208
209         switch (info_id) {
210         case SR_DI_HWOPTS:
211                 *data = hwopts;
212                 break;
213         case SR_DI_HWCAPS:
214                 *data = hwcaps;
215                 break;
216         case SR_DI_NUM_PROBES:
217                 *data = GINT_TO_POINTER(1);
218                 break;
219         case SR_DI_PROBE_NAMES:
220                 *data = probe_names;
221                 break;
222         default:
223                 return SR_ERR_ARG;
224         }
225
226         return SR_OK;
227 }
228
229 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
230                              const void *value)
231 {
232         struct dev_context *devc;
233
234         if (sdi->status != SR_ST_ACTIVE)
235                 return SR_ERR;
236
237         if (!(devc = sdi->priv)) {
238                 sr_err("sdi->priv was NULL.");
239                 return SR_ERR_BUG;
240         }
241
242         switch (hwcap) {
243         case SR_HWCAP_LIMIT_MSEC:
244                 /* TODO: not yet implemented */
245                 if (*(const uint64_t *)value == 0) {
246                         sr_err("LIMIT_MSEC can't be 0.");
247                         return SR_ERR;
248                 }
249                 devc->limit_msec = *(const uint64_t *)value;
250                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
251                        devc->limit_msec);
252                 break;
253         case SR_HWCAP_LIMIT_SAMPLES:
254                 devc->limit_samples = *(const uint64_t *)value;
255                 sr_dbg("Setting sample limit to %" PRIu64 ".",
256                        devc->limit_samples);
257                 break;
258         default:
259                 sr_err("Unknown capability: %d.", hwcap);
260                 return SR_ERR;
261                 break;
262         }
263
264         return SR_OK;
265 }
266
267 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
268                                     void *cb_data)
269 {
270         struct sr_datafeed_packet packet;
271         struct sr_datafeed_header header;
272         struct sr_datafeed_meta_analog meta;
273         struct dev_context *devc;
274
275         if (!(devc = sdi->priv)) {
276                 sr_err("sdi->priv was NULL.");
277                 return SR_ERR_BUG;
278         }
279
280         sr_dbg("Starting acquisition.");
281
282         devc->cb_data = cb_data;
283
284         /* Send header packet to the session bus. */
285         sr_dbg("Sending SR_DF_HEADER.");
286         packet.type = SR_DF_HEADER;
287         packet.payload = (uint8_t *)&header;
288         header.feed_version = 1;
289         gettimeofday(&header.starttime, NULL);
290         sr_session_send(devc->cb_data, &packet);
291
292         /* Send metadata about the SR_DF_ANALOG packets to come. */
293         sr_dbg("Sending SR_DF_META_ANALOG.");
294         packet.type = SR_DF_META_ANALOG;
295         packet.payload = &meta;
296         meta.num_probes = 1;
297         sr_session_send(devc->cb_data, &packet);
298
299         /* Poll every 150ms, or whenever some data comes in. */
300         sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
301                         (void *)sdi);
302
303         return SR_OK;
304 }
305
306 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
307                                    void *cb_data)
308 {
309         struct sr_datafeed_packet packet;
310         struct dev_context *devc;
311
312         if (sdi->status != SR_ST_ACTIVE)
313                 return SR_ERR;
314
315         if (!(devc = sdi->priv)) {
316                 sr_err("sdi->priv was NULL.");
317                 return SR_ERR_BUG;
318         }
319
320         sr_dbg("Stopping acquisition.");
321
322         sr_source_remove(devc->serial->fd);
323         hw_dev_close((struct sr_dev_inst *)sdi);
324
325         /* Send end packet to the session bus. */
326         sr_dbg("Sending SR_DF_END.");
327         packet.type = SR_DF_END;
328         sr_session_send(cb_data, &packet);
329
330         return SR_OK;
331 }
332
333 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
334         .name = "colead-slm",
335         .longname = "Colead SLM",
336         .api_version = 1,
337         .init = hw_init,
338         .cleanup = hw_cleanup,
339         .scan = hw_scan,
340         .dev_list = hw_dev_list,
341         .dev_clear = clear_instances,
342         .dev_open = hw_dev_open,
343         .dev_close = hw_dev_close,
344         .info_get = hw_info_get,
345         .dev_config_set = hw_dev_config_set,
346         .dev_acquisition_start = hw_dev_acquisition_start,
347         .dev_acquisition_stop = hw_dev_acquisition_stop,
348         .priv = NULL,
349 };