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