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