]>
Commit | Line | Data |
---|---|---|
4d729ddc BV |
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" | |
a8d09e13 BV |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | |
26 | #include <fcntl.h> | |
27 | #include <errno.h> | |
28 | #include <string.h> | |
29 | ||
f306ca61 BV |
30 | /* The Colead SL-5868P uses this. */ |
31 | #define SERIALCOMM "2400/8n1" | |
32 | ||
a8d09e13 | 33 | static const int hwopts[] = { |
1953564a BV |
34 | SR_CONF_CONN, |
35 | SR_CONF_SERIALCOMM, | |
a8d09e13 BV |
36 | 0, |
37 | }; | |
38 | ||
39 | static const int hwcaps[] = { | |
1953564a BV |
40 | SR_CONF_SOUNDLEVELMETER, |
41 | SR_CONF_LIMIT_SAMPLES, | |
42 | SR_CONF_LIMIT_MSEC, | |
43 | SR_CONF_CONTINUOUS, | |
a8d09e13 BV |
44 | 0, |
45 | }; | |
46 | ||
4d729ddc BV |
47 | SR_PRIV struct sr_dev_driver colead_slm_driver_info; |
48 | static struct sr_dev_driver *di = &colead_slm_driver_info; | |
49 | ||
50 | /* Properly close and free all devices. */ | |
51 | static int clear_instances(void) | |
52 | { | |
53 | struct sr_dev_inst *sdi; | |
54 | struct drv_context *drvc; | |
55 | struct dev_context *devc; | |
56 | GSList *l; | |
57 | ||
538ac9a9 BV |
58 | if (!(drvc = di->priv)) |
59 | return SR_OK; | |
60 | ||
4d729ddc BV |
61 | for (l = drvc->instances; l; l = l->next) { |
62 | if (!(sdi = l->data)) | |
63 | continue; | |
64 | if (!(devc = sdi->priv)) | |
65 | continue; | |
a8d09e13 | 66 | sr_serial_dev_inst_free(devc->serial); |
4d729ddc BV |
67 | sr_dev_inst_free(sdi); |
68 | } | |
4d729ddc BV |
69 | g_slist_free(drvc->instances); |
70 | drvc->instances = NULL; | |
71 | ||
72 | return SR_OK; | |
73 | } | |
74 | ||
34f06b90 | 75 | static int hw_init(struct sr_context *sr_ctx) |
4d729ddc BV |
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 | ||
1ebe4b4e | 84 | drvc->sr_ctx = sr_ctx; |
4d729ddc BV |
85 | di->priv = drvc; |
86 | ||
87 | return SR_OK; | |
88 | } | |
89 | ||
90 | static GSList *hw_scan(GSList *options) | |
91 | { | |
92 | struct drv_context *drvc; | |
a8d09e13 BV |
93 | struct dev_context *devc; |
94 | struct sr_dev_inst *sdi; | |
1987b8d6 | 95 | struct sr_config *src; |
a8d09e13 BV |
96 | struct sr_probe *probe; |
97 | GSList *devices, *l; | |
98 | const char *conn, *serialcomm; | |
4d729ddc BV |
99 | |
100 | devices = NULL; | |
101 | drvc = di->priv; | |
102 | drvc->instances = NULL; | |
103 | ||
a8d09e13 BV |
104 | conn = serialcomm = NULL; |
105 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
106 | src = l->data; |
107 | switch (src->key) { | |
1953564a | 108 | case SR_CONF_CONN: |
1987b8d6 | 109 | conn = src->value; |
a8d09e13 | 110 | break; |
1953564a | 111 | case SR_CONF_SERIALCOMM: |
1987b8d6 | 112 | serialcomm = src->value; |
a8d09e13 BV |
113 | break; |
114 | } | |
115 | } | |
116 | if (!conn) | |
117 | return NULL; | |
f306ca61 BV |
118 | if (!serialcomm) |
119 | serialcomm = SERIALCOMM; | |
a8d09e13 BV |
120 | |
121 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead", | |
122 | "SL-5868P", NULL))) | |
123 | return NULL; | |
124 | ||
125 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
126 | sr_dbg("failed to malloc devc"); | |
127 | return NULL; | |
128 | } | |
129 | ||
f306ca61 BV |
130 | if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) |
131 | return NULL; | |
a8d09e13 | 132 | |
a8d09e13 BV |
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); | |
4d729ddc BV |
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 | { | |
a8d09e13 BV |
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 | ||
a54dd31e | 162 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) |
a8d09e13 | 163 | return SR_ERR; |
f306ca61 | 164 | |
a8d09e13 | 165 | sdi->status = SR_ST_ACTIVE; |
4d729ddc BV |
166 | |
167 | return SR_OK; | |
168 | } | |
169 | ||
170 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
171 | { | |
a8d09e13 BV |
172 | struct dev_context *devc; |
173 | ||
174 | if (!(devc = sdi->priv)) { | |
175 | sr_err("sdi->priv was NULL."); | |
176 | return SR_ERR_BUG; | |
177 | } | |
178 | ||
179 | if (devc->serial && devc->serial->fd != -1) { | |
f306ca61 | 180 | serial_close(devc->serial); |
a8d09e13 BV |
181 | sdi->status = SR_ST_INACTIVE; |
182 | } | |
4d729ddc BV |
183 | |
184 | return SR_OK; | |
185 | } | |
186 | ||
187 | static int hw_cleanup(void) | |
188 | { | |
189 | clear_instances(); | |
190 | ||
4d729ddc BV |
191 | return SR_OK; |
192 | } | |
193 | ||
035a1078 | 194 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) |
4d729ddc | 195 | { |
a8d09e13 BV |
196 | (void)sdi; |
197 | ||
035a1078 | 198 | switch (id) { |
a8d09e13 BV |
199 | case SR_DI_HWOPTS: |
200 | *data = hwopts; | |
201 | break; | |
4d729ddc | 202 | default: |
4d729ddc BV |
203 | return SR_ERR_ARG; |
204 | } | |
205 | ||
206 | return SR_OK; | |
207 | } | |
208 | ||
035a1078 | 209 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
4d729ddc | 210 | { |
a8d09e13 | 211 | struct dev_context *devc; |
4d729ddc | 212 | |
a8d09e13 | 213 | if (sdi->status != SR_ST_ACTIVE) |
4d729ddc | 214 | return SR_ERR; |
a8d09e13 BV |
215 | |
216 | if (!(devc = sdi->priv)) { | |
217 | sr_err("sdi->priv was NULL."); | |
218 | return SR_ERR_BUG; | |
4d729ddc BV |
219 | } |
220 | ||
035a1078 | 221 | switch (id) { |
1953564a | 222 | case SR_CONF_LIMIT_MSEC: |
a8d09e13 BV |
223 | /* TODO: not yet implemented */ |
224 | if (*(const uint64_t *)value == 0) { | |
225 | sr_err("LIMIT_MSEC can't be 0."); | |
226 | return SR_ERR; | |
227 | } | |
228 | devc->limit_msec = *(const uint64_t *)value; | |
229 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
230 | devc->limit_msec); | |
231 | break; | |
1953564a | 232 | case SR_CONF_LIMIT_SAMPLES: |
a8d09e13 BV |
233 | devc->limit_samples = *(const uint64_t *)value; |
234 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
235 | devc->limit_samples); | |
236 | break; | |
4d729ddc | 237 | default: |
035a1078 | 238 | sr_err("Unknown capability: %d.", id); |
a8d09e13 BV |
239 | return SR_ERR; |
240 | break; | |
4d729ddc BV |
241 | } |
242 | ||
a8d09e13 | 243 | return SR_OK; |
4d729ddc BV |
244 | } |
245 | ||
a1c743fc BV |
246 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
247 | { | |
248 | ||
249 | (void)sdi; | |
250 | ||
251 | switch (key) { | |
9a6517d1 BV |
252 | case SR_CONF_DEVICE_OPTIONS: |
253 | *data = hwcaps; | |
254 | break; | |
a1c743fc BV |
255 | default: |
256 | return SR_ERR_ARG; | |
257 | } | |
258 | ||
259 | return SR_OK; | |
260 | } | |
261 | ||
4d729ddc BV |
262 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
263 | void *cb_data) | |
264 | { | |
a8d09e13 BV |
265 | struct sr_datafeed_packet packet; |
266 | struct sr_datafeed_header header; | |
a8d09e13 BV |
267 | struct dev_context *devc; |
268 | ||
269 | if (!(devc = sdi->priv)) { | |
270 | sr_err("sdi->priv was NULL."); | |
271 | return SR_ERR_BUG; | |
272 | } | |
273 | ||
274 | sr_dbg("Starting acquisition."); | |
275 | ||
276 | devc->cb_data = cb_data; | |
277 | ||
278 | /* Send header packet to the session bus. */ | |
279 | sr_dbg("Sending SR_DF_HEADER."); | |
280 | packet.type = SR_DF_HEADER; | |
281 | packet.payload = (uint8_t *)&header; | |
282 | header.feed_version = 1; | |
283 | gettimeofday(&header.starttime, NULL); | |
284 | sr_session_send(devc->cb_data, &packet); | |
285 | ||
a8d09e13 BV |
286 | /* Poll every 150ms, or whenever some data comes in. */ |
287 | sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data, | |
288 | (void *)sdi); | |
4d729ddc BV |
289 | |
290 | return SR_OK; | |
291 | } | |
292 | ||
69b07d14 | 293 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
4d729ddc | 294 | { |
a8d09e13 BV |
295 | struct sr_datafeed_packet packet; |
296 | struct dev_context *devc; | |
4d729ddc | 297 | |
a8d09e13 | 298 | if (sdi->status != SR_ST_ACTIVE) |
4d729ddc | 299 | return SR_ERR; |
a8d09e13 BV |
300 | |
301 | if (!(devc = sdi->priv)) { | |
302 | sr_err("sdi->priv was NULL."); | |
303 | return SR_ERR_BUG; | |
4d729ddc BV |
304 | } |
305 | ||
a8d09e13 BV |
306 | sr_dbg("Stopping acquisition."); |
307 | ||
308 | sr_source_remove(devc->serial->fd); | |
309 | hw_dev_close((struct sr_dev_inst *)sdi); | |
310 | ||
311 | /* Send end packet to the session bus. */ | |
312 | sr_dbg("Sending SR_DF_END."); | |
313 | packet.type = SR_DF_END; | |
314 | sr_session_send(cb_data, &packet); | |
4d729ddc BV |
315 | |
316 | return SR_OK; | |
317 | } | |
318 | ||
319 | SR_PRIV struct sr_dev_driver colead_slm_driver_info = { | |
320 | .name = "colead-slm", | |
321 | .longname = "Colead SLM", | |
322 | .api_version = 1, | |
323 | .init = hw_init, | |
324 | .cleanup = hw_cleanup, | |
325 | .scan = hw_scan, | |
326 | .dev_list = hw_dev_list, | |
327 | .dev_clear = clear_instances, | |
035a1078 BV |
328 | .config_get = config_get, |
329 | .config_set = config_set, | |
a1c743fc | 330 | .config_list = config_list, |
4d729ddc BV |
331 | .dev_open = hw_dev_open, |
332 | .dev_close = hw_dev_close, | |
4d729ddc BV |
333 | .dev_acquisition_start = hw_dev_acquisition_start, |
334 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
335 | .priv = NULL, | |
336 | }; |