]>
Commit | Line | Data |
---|---|---|
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 | ||
33 | static const int hwopts[] = { | |
34 | SR_CONF_CONN, | |
35 | SR_CONF_SERIALCOMM, | |
36 | 0, | |
37 | }; | |
38 | ||
39 | static const int hwcaps[] = { | |
40 | SR_CONF_SOUNDLEVELMETER, | |
41 | SR_CONF_LIMIT_SAMPLES, | |
42 | SR_CONF_LIMIT_MSEC, | |
43 | SR_CONF_CONTINUOUS, | |
44 | 0, | |
45 | }; | |
46 | ||
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 | ||
58 | if (!(drvc = di->priv)) | |
59 | return SR_OK; | |
60 | ||
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(struct sr_context *sr_ctx) | |
76 | { | |
77 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); | |
78 | } | |
79 | ||
80 | static 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 = src->value; | |
101 | break; | |
102 | case SR_CONF_SERIALCOMM: | |
103 | serialcomm = src->value; | |
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 (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
122 | return NULL; | |
123 | ||
124 | sdi->priv = devc; | |
125 | sdi->driver = di; | |
126 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
127 | return NULL; | |
128 | sdi->probes = g_slist_append(sdi->probes, probe); | |
129 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
130 | devices = g_slist_append(devices, sdi); | |
131 | ||
132 | return devices; | |
133 | } | |
134 | ||
135 | static GSList *hw_dev_list(void) | |
136 | { | |
137 | struct drv_context *drvc; | |
138 | ||
139 | drvc = di->priv; | |
140 | ||
141 | return drvc->instances; | |
142 | } | |
143 | ||
144 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
145 | { | |
146 | struct dev_context *devc; | |
147 | ||
148 | if (!(devc = sdi->priv)) { | |
149 | sr_err("sdi->priv was NULL."); | |
150 | return SR_ERR_BUG; | |
151 | } | |
152 | ||
153 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) | |
154 | return SR_ERR; | |
155 | ||
156 | sdi->status = SR_ST_ACTIVE; | |
157 | ||
158 | return SR_OK; | |
159 | } | |
160 | ||
161 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
162 | { | |
163 | struct dev_context *devc; | |
164 | ||
165 | if (!(devc = sdi->priv)) { | |
166 | sr_err("sdi->priv was NULL."); | |
167 | return SR_ERR_BUG; | |
168 | } | |
169 | ||
170 | if (devc->serial && devc->serial->fd != -1) { | |
171 | serial_close(devc->serial); | |
172 | sdi->status = SR_ST_INACTIVE; | |
173 | } | |
174 | ||
175 | return SR_OK; | |
176 | } | |
177 | ||
178 | static int hw_cleanup(void) | |
179 | { | |
180 | clear_instances(); | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
185 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) | |
186 | { | |
187 | struct dev_context *devc; | |
188 | ||
189 | if (sdi->status != SR_ST_ACTIVE) | |
190 | return SR_ERR; | |
191 | ||
192 | if (!(devc = sdi->priv)) { | |
193 | sr_err("sdi->priv was NULL."); | |
194 | return SR_ERR_BUG; | |
195 | } | |
196 | ||
197 | switch (id) { | |
198 | case SR_CONF_LIMIT_MSEC: | |
199 | /* TODO: not yet implemented */ | |
200 | if (*(const uint64_t *)value == 0) { | |
201 | sr_err("LIMIT_MSEC can't be 0."); | |
202 | return SR_ERR; | |
203 | } | |
204 | devc->limit_msec = *(const uint64_t *)value; | |
205 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
206 | devc->limit_msec); | |
207 | break; | |
208 | case SR_CONF_LIMIT_SAMPLES: | |
209 | devc->limit_samples = *(const uint64_t *)value; | |
210 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
211 | devc->limit_samples); | |
212 | break; | |
213 | default: | |
214 | sr_err("Unknown capability: %d.", id); | |
215 | return SR_ERR; | |
216 | break; | |
217 | } | |
218 | ||
219 | return SR_OK; | |
220 | } | |
221 | ||
222 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) | |
223 | { | |
224 | ||
225 | (void)sdi; | |
226 | ||
227 | switch (key) { | |
228 | case SR_CONF_SCAN_OPTIONS: | |
229 | *data = hwopts; | |
230 | break; | |
231 | case SR_CONF_DEVICE_OPTIONS: | |
232 | *data = hwcaps; | |
233 | break; | |
234 | default: | |
235 | return SR_ERR_ARG; | |
236 | } | |
237 | ||
238 | return SR_OK; | |
239 | } | |
240 | ||
241 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
242 | void *cb_data) | |
243 | { | |
244 | struct sr_datafeed_packet packet; | |
245 | struct sr_datafeed_header header; | |
246 | struct dev_context *devc; | |
247 | ||
248 | if (!(devc = sdi->priv)) { | |
249 | sr_err("sdi->priv was NULL."); | |
250 | return SR_ERR_BUG; | |
251 | } | |
252 | ||
253 | sr_dbg("Starting acquisition."); | |
254 | ||
255 | devc->cb_data = cb_data; | |
256 | ||
257 | /* Send header packet to the session bus. */ | |
258 | sr_dbg("Sending SR_DF_HEADER."); | |
259 | packet.type = SR_DF_HEADER; | |
260 | packet.payload = (uint8_t *)&header; | |
261 | header.feed_version = 1; | |
262 | gettimeofday(&header.starttime, NULL); | |
263 | sr_session_send(devc->cb_data, &packet); | |
264 | ||
265 | /* Poll every 150ms, or whenever some data comes in. */ | |
266 | sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data, | |
267 | (void *)sdi); | |
268 | ||
269 | return SR_OK; | |
270 | } | |
271 | ||
272 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
273 | { | |
274 | struct sr_datafeed_packet packet; | |
275 | struct dev_context *devc; | |
276 | ||
277 | if (sdi->status != SR_ST_ACTIVE) | |
278 | return SR_ERR; | |
279 | ||
280 | if (!(devc = sdi->priv)) { | |
281 | sr_err("sdi->priv was NULL."); | |
282 | return SR_ERR_BUG; | |
283 | } | |
284 | ||
285 | sr_dbg("Stopping acquisition."); | |
286 | ||
287 | sr_source_remove(devc->serial->fd); | |
288 | hw_dev_close((struct sr_dev_inst *)sdi); | |
289 | ||
290 | /* Send end packet to the session bus. */ | |
291 | sr_dbg("Sending SR_DF_END."); | |
292 | packet.type = SR_DF_END; | |
293 | sr_session_send(cb_data, &packet); | |
294 | ||
295 | return SR_OK; | |
296 | } | |
297 | ||
298 | SR_PRIV struct sr_dev_driver colead_slm_driver_info = { | |
299 | .name = "colead-slm", | |
300 | .longname = "Colead SLM", | |
301 | .api_version = 1, | |
302 | .init = hw_init, | |
303 | .cleanup = hw_cleanup, | |
304 | .scan = hw_scan, | |
305 | .dev_list = hw_dev_list, | |
306 | .dev_clear = clear_instances, | |
307 | .config_set = config_set, | |
308 | .config_list = config_list, | |
309 | .dev_open = hw_dev_open, | |
310 | .dev_close = hw_dev_close, | |
311 | .dev_acquisition_start = hw_dev_acquisition_start, | |
312 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
313 | .priv = NULL, | |
314 | }; |