]>
Commit | Line | Data |
---|---|---|
aa2af324 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
c073af80 | 21 | #include <fcntl.h> |
aa2af324 UH |
22 | #include <glib.h> |
23 | #include "libsigrok.h" | |
24 | #include "libsigrok-internal.h" | |
25 | #include "protocol.h" | |
26 | ||
c073af80 UH |
27 | static const int hwopts[] = { |
28 | SR_HWOPT_CONN, | |
29 | SR_HWOPT_SERIALCOMM, | |
30 | 0, | |
31 | }; | |
32 | ||
33 | static const int hwcaps[] = { | |
34 | SR_HWCAP_SOUNDLEVELMETER, | |
35 | SR_HWCAP_LIMIT_SAMPLES, | |
36 | SR_HWCAP_CONTINUOUS, | |
37 | 0, | |
38 | }; | |
39 | ||
40 | static const char *probe_names[] = { | |
41 | "P1", | |
42 | NULL, | |
43 | }; | |
44 | ||
aa2af324 UH |
45 | SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info; |
46 | static struct sr_dev_driver *di = &tondaj_sl_814_driver_info; | |
47 | ||
48 | /* Properly close and free all devices. */ | |
49 | static int clear_instances(void) | |
50 | { | |
51 | struct sr_dev_inst *sdi; | |
52 | struct drv_context *drvc; | |
53 | struct dev_context *devc; | |
54 | GSList *l; | |
55 | ||
56 | if (!(drvc = di->priv)) | |
57 | return SR_OK; | |
58 | ||
59 | for (l = drvc->instances; l; l = l->next) { | |
60 | if (!(sdi = l->data)) | |
61 | continue; | |
62 | if (!(devc = sdi->priv)) | |
63 | continue; | |
c073af80 | 64 | sr_serial_dev_inst_free(devc->serial); |
aa2af324 UH |
65 | sr_dev_inst_free(sdi); |
66 | } | |
67 | ||
68 | g_slist_free(drvc->instances); | |
69 | drvc->instances = NULL; | |
70 | ||
71 | return SR_OK; | |
72 | } | |
73 | ||
74 | static int hw_init(void) | |
75 | { | |
76 | struct drv_context *drvc; | |
77 | ||
78 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
79 | sr_err("Driver context malloc failed."); | |
80 | return SR_ERR_MALLOC; | |
81 | } | |
82 | ||
aa2af324 UH |
83 | di->priv = drvc; |
84 | ||
85 | return SR_OK; | |
86 | } | |
87 | ||
88 | static GSList *hw_scan(GSList *options) | |
89 | { | |
90 | struct drv_context *drvc; | |
c073af80 UH |
91 | struct dev_context *devc; |
92 | struct sr_dev_inst *sdi; | |
93 | struct sr_hwopt *opt; | |
94 | struct sr_probe *probe; | |
95 | GSList *devices, *l; | |
96 | const char *conn, *serialcomm; | |
aa2af324 UH |
97 | |
98 | devices = NULL; | |
99 | drvc = di->priv; | |
100 | drvc->instances = NULL; | |
101 | ||
c073af80 UH |
102 | conn = serialcomm = NULL; |
103 | for (l = options; l; l = l->next) { | |
104 | if (!(opt = l->data)) { | |
105 | sr_err("Invalid option data, skipping."); | |
106 | continue; | |
107 | } | |
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 | default: | |
116 | sr_err("Unknown option %d, skipping.", opt->hwopt); | |
117 | break; | |
118 | } | |
119 | } | |
120 | if (!conn) { | |
121 | sr_dbg("Couldn't determine connection options."); | |
122 | return NULL; | |
123 | } | |
124 | ||
125 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Tondaj", | |
126 | "SL-814", NULL))) { | |
127 | sr_err("Failed to create device instance."); | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
132 | sr_err("Device context malloc failed."); | |
133 | return NULL; | |
134 | } | |
135 | ||
136 | if (!serialcomm) | |
137 | serialcomm = "9600/8e1"; | |
138 | ||
139 | if (!(devc->serial = sr_serial_dev_inst_new(conn, -1))) { | |
140 | sr_err("Failed to create serial device instance."); | |
141 | return NULL; | |
142 | } | |
143 | devc->serialcomm = g_strdup(serialcomm); | |
144 | sdi->priv = devc; | |
145 | sdi->driver = di; | |
146 | probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probe_names[0]); | |
147 | if (!probe) { | |
148 | sr_err("Failed to create probe."); | |
149 | return NULL; | |
150 | } | |
151 | sdi->probes = g_slist_append(sdi->probes, probe); | |
152 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
153 | devices = g_slist_append(devices, sdi); | |
aa2af324 UH |
154 | |
155 | return devices; | |
156 | } | |
157 | ||
158 | static GSList *hw_dev_list(void) | |
159 | { | |
160 | struct drv_context *drvc; | |
161 | ||
162 | drvc = di->priv; | |
163 | ||
164 | return drvc->instances; | |
165 | } | |
166 | ||
167 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
168 | { | |
c073af80 UH |
169 | int ret; |
170 | struct dev_context *devc; | |
171 | ||
172 | devc = sdi->priv; | |
173 | ||
174 | sr_dbg("Opening '%s' with '%s'.", devc->serial->port, devc->serialcomm); | |
175 | ||
176 | ret = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); | |
177 | if (ret < 0) { | |
178 | sr_err("Unable to open serial port: %d.", ret); | |
179 | return SR_ERR; | |
180 | } | |
181 | devc->serial->fd = ret; | |
182 | ||
183 | ret = serial_set_paramstr(devc->serial->fd, devc->serialcomm); | |
184 | if (ret < 0) { | |
185 | sr_err("Unable to set serial parameters: %d.", ret); | |
186 | return SR_ERR; | |
187 | } | |
188 | ||
189 | sdi->status = SR_ST_ACTIVE; | |
aa2af324 UH |
190 | |
191 | return SR_OK; | |
192 | } | |
193 | ||
194 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
195 | { | |
c073af80 UH |
196 | int ret; |
197 | struct dev_context *devc; | |
198 | ||
199 | devc = sdi->priv; | |
200 | ||
201 | if (!devc->serial) { | |
202 | sr_err("Invalid serial device instance, cannot close."); | |
203 | return SR_ERR_BUG; | |
204 | } | |
205 | ||
206 | if (devc->serial->fd == -1) { | |
207 | sr_dbg("Serial device instance FD was -1, no need to close."); | |
208 | return SR_OK; | |
209 | } | |
210 | ||
211 | if ((ret = serial_close(devc->serial->fd)) < 0) { | |
212 | sr_err("Error closing serial port: %d.", ret); | |
213 | return SR_ERR; | |
214 | } | |
215 | ||
216 | devc->serial->fd = -1; | |
217 | sdi->status = SR_ST_INACTIVE; | |
aa2af324 UH |
218 | |
219 | return SR_OK; | |
220 | } | |
221 | ||
222 | static int hw_cleanup(void) | |
223 | { | |
224 | clear_instances(); | |
225 | ||
aa2af324 UH |
226 | return SR_OK; |
227 | } | |
228 | ||
229 | static int hw_info_get(int info_id, const void **data, | |
230 | const struct sr_dev_inst *sdi) | |
231 | { | |
c073af80 UH |
232 | (void)sdi; |
233 | ||
aa2af324 | 234 | switch (info_id) { |
c073af80 UH |
235 | case SR_DI_HWOPTS: |
236 | *data = hwopts; | |
237 | break; | |
238 | case SR_DI_HWCAPS: | |
239 | *data = hwcaps; | |
240 | break; | |
241 | case SR_DI_NUM_PROBES: | |
242 | *data = GINT_TO_POINTER(1); | |
243 | break; | |
244 | case SR_DI_PROBE_NAMES: | |
245 | *data = probe_names; | |
246 | break; | |
aa2af324 UH |
247 | default: |
248 | sr_err("Unknown info_id: %d.", info_id); | |
249 | return SR_ERR_ARG; | |
250 | } | |
251 | ||
252 | return SR_OK; | |
253 | } | |
254 | ||
255 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
256 | const void *value) | |
257 | { | |
c073af80 | 258 | struct dev_context *devc; |
aa2af324 UH |
259 | |
260 | if (sdi->status != SR_ST_ACTIVE) { | |
261 | sr_err("Device inactive, can't set config options."); | |
262 | return SR_ERR; | |
263 | } | |
264 | ||
c073af80 UH |
265 | devc = sdi->priv; |
266 | ||
aa2af324 | 267 | switch (hwcap) { |
c073af80 UH |
268 | case SR_HWCAP_LIMIT_SAMPLES: |
269 | devc->limit_samples = *(const uint64_t *)value; | |
270 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
271 | devc->limit_samples); | |
272 | break; | |
aa2af324 UH |
273 | default: |
274 | sr_err("Unknown hardware capability: %d.", hwcap); | |
c073af80 | 275 | return SR_ERR_ARG; |
aa2af324 UH |
276 | } |
277 | ||
c073af80 | 278 | return SR_OK; |
aa2af324 UH |
279 | } |
280 | ||
281 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
282 | void *cb_data) | |
283 | { | |
c073af80 UH |
284 | struct sr_datafeed_packet packet; |
285 | struct sr_datafeed_header header; | |
286 | struct sr_datafeed_meta_analog meta; | |
287 | struct dev_context *devc; | |
288 | ||
289 | devc = sdi->priv; | |
290 | devc->cb_data = cb_data; | |
291 | ||
292 | /* Send header packet to the session bus. */ | |
293 | sr_dbg("Sending SR_DF_HEADER."); | |
294 | packet.type = SR_DF_HEADER; | |
295 | packet.payload = (uint8_t *)&header; | |
296 | header.feed_version = 1; | |
297 | gettimeofday(&header.starttime, NULL); | |
298 | sr_session_send(devc->cb_data, &packet); | |
299 | ||
300 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
301 | sr_dbg("Sending SR_DF_META_ANALOG."); | |
302 | packet.type = SR_DF_META_ANALOG; | |
303 | packet.payload = &meta; | |
304 | meta.num_probes = 1; | |
305 | sr_session_send(devc->cb_data, &packet); | |
306 | ||
307 | /* Poll every 500ms, or whenever some data comes in. */ | |
308 | sr_source_add(devc->serial->fd, G_IO_IN, 500, | |
309 | tondaj_sl_814_receive_data, (void *)sdi); | |
aa2af324 UH |
310 | |
311 | return SR_OK; | |
312 | } | |
313 | ||
c073af80 | 314 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
aa2af324 | 315 | { |
c073af80 UH |
316 | int ret, final_ret; |
317 | struct sr_datafeed_packet packet; | |
318 | struct dev_context *devc; | |
319 | ||
320 | final_ret = SR_OK; | |
aa2af324 UH |
321 | |
322 | if (sdi->status != SR_ST_ACTIVE) { | |
323 | sr_err("Device inactive, can't stop acquisition."); | |
324 | return SR_ERR; | |
325 | } | |
326 | ||
c073af80 | 327 | devc = sdi->priv; |
aa2af324 | 328 | |
c073af80 UH |
329 | if ((ret = sr_source_remove(devc->serial->fd)) < 0) { |
330 | sr_err("Error removing source: %d.", ret); | |
331 | final_ret = SR_ERR; | |
332 | } | |
333 | ||
334 | if ((ret = hw_dev_close(sdi)) < 0) { | |
335 | sr_err("Error closing device: %d.", ret); | |
336 | final_ret = SR_ERR; | |
337 | } | |
338 | ||
339 | /* Send end packet to the session bus. */ | |
340 | sr_dbg("Sending SR_DF_END."); | |
341 | packet.type = SR_DF_END; | |
342 | sr_session_send(cb_data, &packet); | |
343 | ||
344 | return final_ret; | |
aa2af324 UH |
345 | } |
346 | ||
347 | SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info = { | |
348 | .name = "tondaj-sl-814", | |
349 | .longname = "Tondaj SL-814", | |
350 | .api_version = 1, | |
351 | .init = hw_init, | |
352 | .cleanup = hw_cleanup, | |
353 | .scan = hw_scan, | |
354 | .dev_list = hw_dev_list, | |
355 | .dev_clear = clear_instances, | |
356 | .dev_open = hw_dev_open, | |
357 | .dev_close = hw_dev_close, | |
358 | .info_get = hw_info_get, | |
359 | .dev_config_set = hw_dev_config_set, | |
360 | .dev_acquisition_start = hw_dev_acquisition_start, | |
361 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
362 | .priv = NULL, | |
363 | }; |