]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <stdlib.h> | |
24 | #include <unistd.h> | |
25 | #include <string.h> | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | #include "protocol.h" | |
29 | ||
30 | static const int32_t hwcaps[] = { | |
31 | SR_CONF_SAMPLERATE, | |
32 | SR_CONF_LIMIT_SAMPLES, | |
33 | SR_CONF_CONTINUOUS, | |
34 | }; | |
35 | ||
36 | SR_PRIV struct sr_dev_driver alsa_driver_info; | |
37 | static struct sr_dev_driver *di = &alsa_driver_info; | |
38 | ||
39 | static void clear_helper(void *priv) | |
40 | { | |
41 | struct dev_context *devc; | |
42 | ||
43 | devc = priv; | |
44 | ||
45 | snd_pcm_hw_params_free(devc->hw_params); | |
46 | devc->hw_params = NULL; | |
47 | ||
48 | g_free((void *)devc->samplerates); | |
49 | devc->samplerates = NULL; | |
50 | } | |
51 | ||
52 | static int dev_clear(void) | |
53 | { | |
54 | return std_dev_clear(di, clear_helper); | |
55 | } | |
56 | ||
57 | static int init(struct sr_context *sr_ctx) | |
58 | { | |
59 | return std_init(sr_ctx, di, LOG_PREFIX); | |
60 | } | |
61 | ||
62 | static GSList *scan(GSList *options) | |
63 | { | |
64 | return alsa_scan(options, di); | |
65 | } | |
66 | ||
67 | static GSList *dev_list(void) | |
68 | { | |
69 | return ((struct drv_context *)(di->priv))->instances; | |
70 | } | |
71 | ||
72 | static int dev_open(struct sr_dev_inst *sdi) | |
73 | { | |
74 | struct dev_context *devc; | |
75 | int ret; | |
76 | ||
77 | devc = sdi->priv; | |
78 | ||
79 | if (!(devc->hwdev)) { | |
80 | sr_err("devc->hwdev was NULL."); | |
81 | return SR_ERR_BUG; | |
82 | } | |
83 | ||
84 | sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev); | |
85 | ret = snd_pcm_open(&devc->capture_handle, devc->hwdev, | |
86 | SND_PCM_STREAM_CAPTURE, 0); | |
87 | if (ret < 0) { | |
88 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); | |
89 | return SR_ERR; | |
90 | } | |
91 | ||
92 | sr_dbg("Initializing hardware parameter structure."); | |
93 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
94 | if (ret < 0) { | |
95 | sr_err("Can't initialize hardware parameter structure: %s.", | |
96 | snd_strerror(ret)); | |
97 | return SR_ERR; | |
98 | } | |
99 | ||
100 | sdi->status = SR_ST_ACTIVE; | |
101 | ||
102 | return SR_OK; | |
103 | } | |
104 | ||
105 | static int dev_close(struct sr_dev_inst *sdi) | |
106 | { | |
107 | int ret; | |
108 | struct dev_context *devc; | |
109 | ||
110 | devc = sdi->priv; | |
111 | ||
112 | if (devc->capture_handle) { | |
113 | sr_dbg("Closing PCM device."); | |
114 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) | |
115 | sr_err("Failed to close device: %s.", | |
116 | snd_strerror(ret)); | |
117 | devc->capture_handle = NULL; | |
118 | sdi->status = SR_ST_INACTIVE; | |
119 | } else { | |
120 | sr_dbg("No capture handle, no need to close audio device."); | |
121 | } | |
122 | ||
123 | return SR_OK; | |
124 | } | |
125 | ||
126 | static int cleanup(void) | |
127 | { | |
128 | return dev_clear(); | |
129 | } | |
130 | ||
131 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, | |
132 | const struct sr_probe_group *probe_group) | |
133 | { | |
134 | struct dev_context *devc; | |
135 | ||
136 | (void)probe_group; | |
137 | ||
138 | switch (id) { | |
139 | case SR_CONF_SAMPLERATE: | |
140 | devc = sdi->priv; | |
141 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
142 | break; | |
143 | default: | |
144 | return SR_ERR_NA; | |
145 | } | |
146 | ||
147 | return SR_OK; | |
148 | } | |
149 | ||
150 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, | |
151 | const struct sr_probe_group *probe_group) | |
152 | { | |
153 | struct dev_context *devc; | |
154 | ||
155 | (void)probe_group; | |
156 | ||
157 | if (sdi->status != SR_ST_ACTIVE) | |
158 | return SR_ERR_DEV_CLOSED; | |
159 | ||
160 | devc = sdi->priv; | |
161 | ||
162 | switch (id) { | |
163 | case SR_CONF_SAMPLERATE: | |
164 | alsa_set_samplerate(sdi, g_variant_get_uint64(data)); | |
165 | break; | |
166 | case SR_CONF_LIMIT_SAMPLES: | |
167 | devc->limit_samples = g_variant_get_uint64(data); | |
168 | break; | |
169 | default: | |
170 | return SR_ERR_NA; | |
171 | } | |
172 | ||
173 | return SR_OK; | |
174 | } | |
175 | ||
176 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
177 | const struct sr_probe_group *probe_group) | |
178 | { | |
179 | struct dev_context *devc; | |
180 | GVariant *gvar; | |
181 | GVariantBuilder gvb; | |
182 | int i; | |
183 | ||
184 | (void)probe_group; | |
185 | ||
186 | switch (key) { | |
187 | case SR_CONF_DEVICE_OPTIONS: | |
188 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
189 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
190 | break; | |
191 | case SR_CONF_SAMPLERATE: | |
192 | if (!sdi || !sdi->priv) | |
193 | return SR_ERR_ARG; | |
194 | devc = sdi->priv; | |
195 | if (!devc->samplerates) { | |
196 | sr_err("Instance did not contain a samplerate list."); | |
197 | return SR_ERR_ARG; | |
198 | } | |
199 | for (i = 0; devc->samplerates[i]; i++) | |
200 | ; | |
201 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
202 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
203 | devc->samplerates, i, sizeof(uint64_t)); | |
204 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
205 | *data = g_variant_builder_end(&gvb); | |
206 | break; | |
207 | default: | |
208 | return SR_ERR_NA; | |
209 | } | |
210 | ||
211 | return SR_OK; | |
212 | } | |
213 | ||
214 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
215 | { | |
216 | struct dev_context *devc; | |
217 | int count, ret; | |
218 | char *endianness; | |
219 | ||
220 | if (sdi->status != SR_ST_ACTIVE) | |
221 | return SR_ERR_DEV_CLOSED; | |
222 | ||
223 | devc = sdi->priv; | |
224 | devc->cb_data = cb_data; | |
225 | devc->num_samples = 0; | |
226 | ||
227 | sr_dbg("Setting audio access type to RW/interleaved."); | |
228 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
229 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
230 | if (ret < 0) { | |
231 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); | |
232 | return SR_ERR; | |
233 | } | |
234 | ||
235 | /* FIXME: Hardcoded for 16bits. */ | |
236 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) | |
237 | endianness = "little endian"; | |
238 | else | |
239 | endianness = "big endian"; | |
240 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
241 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, | |
242 | devc->hw_params, | |
243 | SND_PCM_FORMAT_S16); | |
244 | if (ret < 0) { | |
245 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); | |
246 | return SR_ERR; | |
247 | } | |
248 | ||
249 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", | |
250 | devc->cur_samplerate); | |
251 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, | |
252 | (unsigned int)devc->cur_samplerate, 0); | |
253 | if (ret < 0) { | |
254 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); | |
255 | return SR_ERR; | |
256 | } | |
257 | ||
258 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); | |
259 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, | |
260 | devc->hw_params, devc->num_probes); | |
261 | if (ret < 0) { | |
262 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); | |
263 | return SR_ERR; | |
264 | } | |
265 | ||
266 | sr_dbg("Setting audio parameters."); | |
267 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
268 | if (ret < 0) { | |
269 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); | |
270 | return SR_ERR; | |
271 | } | |
272 | ||
273 | sr_dbg("Preparing audio interface for use."); | |
274 | ret = snd_pcm_prepare(devc->capture_handle); | |
275 | if (ret < 0) { | |
276 | sr_err("Can't prepare audio interface for use: %s.", | |
277 | snd_strerror(ret)); | |
278 | return SR_ERR; | |
279 | } | |
280 | ||
281 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); | |
282 | if (count < 1) { | |
283 | sr_err("Unable to obtain poll descriptors count."); | |
284 | return SR_ERR; | |
285 | } | |
286 | ||
287 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { | |
288 | sr_err("Failed to malloc ufds."); | |
289 | return SR_ERR_MALLOC; | |
290 | } | |
291 | ||
292 | sr_spew("Getting %d poll descriptors.", count); | |
293 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); | |
294 | if (ret < 0) { | |
295 | sr_err("Unable to obtain poll descriptors: %s.", | |
296 | snd_strerror(ret)); | |
297 | g_free(devc->ufds); | |
298 | return SR_ERR; | |
299 | } | |
300 | ||
301 | /* Send header packet to the session bus. */ | |
302 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
303 | ||
304 | /* Poll every 10ms, or whenever some data comes in. */ | |
305 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
306 | alsa_receive_data, (void *)sdi); | |
307 | ||
308 | // g_free(devc->ufds); /* FIXME */ | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
313 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
314 | { | |
315 | struct sr_datafeed_packet packet; | |
316 | struct dev_context *devc; | |
317 | ||
318 | devc = sdi->priv; | |
319 | devc->cb_data = cb_data; | |
320 | ||
321 | sr_source_remove(devc->ufds[0].fd); | |
322 | ||
323 | /* Send end packet to the session bus. */ | |
324 | sr_dbg("Sending SR_DF_END packet."); | |
325 | packet.type = SR_DF_END; | |
326 | sr_session_send(cb_data, &packet); | |
327 | ||
328 | return SR_OK; | |
329 | } | |
330 | ||
331 | SR_PRIV struct sr_dev_driver alsa_driver_info = { | |
332 | .name = "alsa", | |
333 | .longname = "ALSA driver", | |
334 | .api_version = 1, | |
335 | .init = init, | |
336 | .cleanup = cleanup, | |
337 | .scan = scan, | |
338 | .dev_list = dev_list, | |
339 | .dev_clear = dev_clear, | |
340 | .config_get = config_get, | |
341 | .config_set = config_set, | |
342 | .config_list = config_list, | |
343 | .dev_open = dev_open, | |
344 | .dev_close = dev_close, | |
345 | .dev_acquisition_start = dev_acquisition_start, | |
346 | .dev_acquisition_stop = dev_acquisition_stop, | |
347 | .priv = NULL, | |
348 | }; |