]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/colead-slm/api.c
Enforce open device before config_set()/dev_acquisition_start()
[libsigrok.git] / hardware / colead-slm / api.c
... / ...
CommitLineData
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/* The Colead SL-5868P uses this. */
31#define SERIALCOMM "2400/8n1"
32
33static const int32_t hwopts[] = {
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
36};
37
38static const int32_t hwcaps[] = {
39 SR_CONF_SOUNDLEVELMETER,
40 SR_CONF_LIMIT_SAMPLES,
41 SR_CONF_LIMIT_MSEC,
42 SR_CONF_CONTINUOUS,
43};
44
45SR_PRIV struct sr_dev_driver colead_slm_driver_info;
46static struct sr_dev_driver *di = &colead_slm_driver_info;
47
48/* Properly close and free all devices. */
49static int clear_instances(void)
50{
51 struct sr_dev_inst *sdi;
52 struct drv_context *drvc;
53 struct dev_context *devc;
54 struct sr_serial_dev_inst *serial;
55 GSList *l;
56
57 if (!(drvc = di->priv))
58 return SR_OK;
59
60 for (l = drvc->instances; l; l = l->next) {
61 if (!(sdi = l->data))
62 continue;
63 if (!(devc = sdi->priv))
64 continue;
65 serial = sdi->conn;
66 sr_serial_dev_inst_free(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
75static int hw_init(struct sr_context *sr_ctx)
76{
77 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
78}
79
80static GSList *hw_scan(GSList *options)
81{
82 struct drv_context *drvc;
83 struct dev_context *devc;
84 struct sr_dev_inst *sdi;
85 struct sr_config *src;
86 struct sr_probe *probe;
87 GSList *devices, *l;
88 const char *conn, *serialcomm;
89
90 drvc = di->priv;
91 drvc->instances = NULL;
92
93 devices = NULL;
94
95 conn = serialcomm = NULL;
96 for (l = options; l; l = l->next) {
97 src = l->data;
98 switch (src->key) {
99 case SR_CONF_CONN:
100 conn = g_variant_get_string(src->data, NULL);
101 break;
102 case SR_CONF_SERIALCOMM:
103 serialcomm = g_variant_get_string(src->data, NULL);
104 break;
105 }
106 }
107 if (!conn)
108 return NULL;
109 if (!serialcomm)
110 serialcomm = SERIALCOMM;
111
112 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
113 "SL-5868P", NULL)))
114 return NULL;
115
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
117 sr_dbg("Device context malloc failed.");
118 return NULL;
119 }
120
121 if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm)))
122 return NULL;
123
124 sdi->inst_type = SR_INST_SERIAL;
125 sdi->priv = devc;
126 sdi->driver = di;
127 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
128 return NULL;
129 sdi->probes = g_slist_append(sdi->probes, probe);
130 drvc->instances = g_slist_append(drvc->instances, sdi);
131 devices = g_slist_append(devices, sdi);
132
133 return devices;
134}
135
136static GSList *hw_dev_list(void)
137{
138 return ((struct drv_context *)(di->priv))->instances;
139}
140
141static int hw_dev_open(struct sr_dev_inst *sdi)
142{
143 struct sr_serial_dev_inst *serial;
144
145 serial = sdi->conn;
146 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
147 return SR_ERR;
148
149 sdi->status = SR_ST_ACTIVE;
150
151 return SR_OK;
152}
153
154static int hw_dev_close(struct sr_dev_inst *sdi)
155{
156 struct sr_serial_dev_inst *serial;
157
158 serial = sdi->conn;
159 if (serial && serial->fd != -1) {
160 serial_close(serial);
161 sdi->status = SR_ST_INACTIVE;
162 }
163
164 return SR_OK;
165}
166
167static int hw_cleanup(void)
168{
169 clear_instances();
170
171 return SR_OK;
172}
173
174static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
175{
176 struct dev_context *devc;
177
178 if (sdi->status != SR_ST_ACTIVE)
179 return SR_ERR_DEV_CLOSED;
180
181 if (!(devc = sdi->priv)) {
182 sr_err("sdi->priv was NULL.");
183 return SR_ERR_BUG;
184 }
185
186 switch (id) {
187 case SR_CONF_LIMIT_MSEC:
188 /* TODO: not yet implemented */
189 if (g_variant_get_uint64(data) == 0) {
190 sr_err("LIMIT_MSEC can't be 0.");
191 return SR_ERR;
192 }
193 devc->limit_msec = g_variant_get_uint64(data);;
194 sr_dbg("Setting time limit to %" PRIu64 "ms.",
195 devc->limit_msec);
196 break;
197 case SR_CONF_LIMIT_SAMPLES:
198 devc->limit_samples = g_variant_get_uint64(data);
199 sr_dbg("Setting sample limit to %" PRIu64 ".",
200 devc->limit_samples);
201 break;
202 default:
203 return SR_ERR_NA;
204 }
205
206 return SR_OK;
207}
208
209static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
210{
211
212 (void)sdi;
213
214 switch (key) {
215 case SR_CONF_SCAN_OPTIONS:
216 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
217 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
218 break;
219 case SR_CONF_DEVICE_OPTIONS:
220 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
221 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
222 break;
223 default:
224 return SR_ERR_NA;
225 }
226
227 return SR_OK;
228}
229
230static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
231 void *cb_data)
232{
233 struct dev_context *devc;
234 struct sr_serial_dev_inst *serial;
235
236 if (sdi->status != SR_ST_ACTIVE)
237 return SR_ERR_DEV_CLOSED;
238
239 if (!(devc = sdi->priv)) {
240 sr_err("sdi->priv was NULL.");
241 return SR_ERR_BUG;
242 }
243
244 devc->cb_data = cb_data;
245
246 /* Send header packet to the session bus. */
247 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
248
249 /* Poll every 150ms, or whenever some data comes in. */
250 serial = sdi->conn;
251 sr_source_add(serial->fd, G_IO_IN, 150, colead_slm_receive_data,
252 (void *)sdi);
253
254 return SR_OK;
255}
256
257static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
258{
259 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
260 sdi->conn, DRIVER_LOG_DOMAIN);
261}
262
263SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
264 .name = "colead-slm",
265 .longname = "Colead SLM",
266 .api_version = 1,
267 .init = hw_init,
268 .cleanup = hw_cleanup,
269 .scan = hw_scan,
270 .dev_list = hw_dev_list,
271 .dev_clear = clear_instances,
272 .config_get = NULL,
273 .config_set = config_set,
274 .config_list = config_list,
275 .dev_open = hw_dev_open,
276 .dev_close = hw_dev_close,
277 .dev_acquisition_start = hw_dev_acquisition_start,
278 .dev_acquisition_stop = hw_dev_acquisition_stop,
279 .priv = NULL,
280};