]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/alsa/api.c
Add and use std_session_send_df_header().
[libsigrok.git] / hardware / alsa / api.c
... / ...
CommitLineData
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
30static const int hwcaps[] = {
31 SR_CONF_SAMPLERATE,
32 SR_CONF_LIMIT_SAMPLES,
33 SR_CONF_CONTINUOUS,
34 0,
35};
36
37SR_PRIV struct sr_dev_driver alsa_driver_info;
38static struct sr_dev_driver *di = &alsa_driver_info;
39
40static int clear_instances(void)
41{
42 struct drv_context *drvc;
43
44 if (!(drvc = di->priv))
45 return SR_OK;
46
47 g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
48 drvc->instances = NULL;
49
50 return SR_OK;
51}
52
53static int hw_init(struct sr_context *sr_ctx)
54{
55 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
56}
57
58static GSList *hw_scan(GSList *options)
59{
60 return alsa_scan(options, di);
61}
62
63static GSList *hw_dev_list(void)
64{
65 return ((struct drv_context *)(di->priv))->instances;
66}
67
68static int hw_dev_open(struct sr_dev_inst *sdi)
69{
70 struct dev_context *devc;
71 int ret;
72
73 devc = sdi->priv;
74
75 if (!(devc->hwdev)) {
76 sr_err("devc->hwdev was NULL.");
77 return SR_ERR_BUG;
78 }
79
80 sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev);
81 ret = snd_pcm_open(&devc->capture_handle, devc->hwdev,
82 SND_PCM_STREAM_CAPTURE, 0);
83 if (ret < 0) {
84 sr_err("Can't open audio device: %s.", snd_strerror(ret));
85 return SR_ERR;
86 }
87
88 sr_dbg("Initializing hardware parameter structure.");
89 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
90 if (ret < 0) {
91 sr_err("Can't initialize hardware parameter structure: %s.",
92 snd_strerror(ret));
93 return SR_ERR;
94 }
95
96 return SR_OK;
97}
98
99static int hw_dev_close(struct sr_dev_inst *sdi)
100{
101 int ret;
102 struct dev_context *devc;
103
104 devc = sdi->priv;
105
106 if (devc->capture_handle) {
107 sr_dbg("Closing PCM device.");
108 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
109 sr_err("Failed to close device: %s.",
110 snd_strerror(ret));
111 devc->capture_handle = NULL;
112 }
113 } else {
114 sr_dbg("No capture handle, no need to close audio device.");
115 }
116
117 return SR_OK;
118}
119
120static int hw_cleanup(void)
121{
122 clear_instances();
123
124 return SR_OK;
125}
126
127static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
128{
129 struct dev_context *devc;
130
131 switch (id) {
132 case SR_CONF_SAMPLERATE:
133 devc = sdi->priv;
134 *data = &devc->cur_samplerate;
135 break;
136 default:
137 return SR_ERR_ARG;
138 }
139
140 return SR_OK;
141}
142
143static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
144{
145 struct dev_context *devc;
146
147 devc = sdi->priv;
148
149 switch (id) {
150 case SR_CONF_SAMPLERATE:
151 alsa_set_samplerate(sdi, *(const uint64_t *)value);
152 break;
153 case SR_CONF_LIMIT_SAMPLES:
154 devc->limit_samples = *(const uint64_t *)value;
155 break;
156 default:
157 sr_err("Unknown capability: %d.", id);
158 return SR_ERR;
159 }
160
161 return SR_OK;
162}
163
164static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
165{
166 struct dev_context *devc;
167
168 (void)sdi;
169
170 switch (key) {
171 case SR_CONF_DEVICE_OPTIONS:
172 *data = hwcaps;
173 break;
174 case SR_CONF_SAMPLERATE:
175 if (!sdi || !sdi->priv)
176 return SR_ERR_ARG;
177 devc = sdi->priv;
178 if (!devc->supp_rates.list) {
179 sr_err("Instance did not contain a samplerate list.");
180 return SR_ERR_ARG;
181 }
182 *data = &devc->supp_rates;
183 break;
184 default:
185 return SR_ERR_ARG;
186 }
187
188 return SR_OK;
189}
190
191static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
192 void *cb_data)
193{
194 struct dev_context *devc;
195 int count, ret;
196 char *endianness;
197
198 devc = sdi->priv;
199 devc->cb_data = cb_data;
200 devc->num_samples = 0;
201
202 sr_dbg("Setting audio access type to RW/interleaved.");
203 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
204 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
205 if (ret < 0) {
206 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
207 return SR_ERR;
208 }
209
210 /* FIXME: Hardcoded for 16bits. */
211 if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE)
212 endianness = "little endian";
213 else
214 endianness = "big endian";
215 sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness);
216 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
217 devc->hw_params,
218 SND_PCM_FORMAT_S16);
219 if (ret < 0) {
220 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
221 return SR_ERR;
222 }
223
224 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
225 devc->cur_samplerate);
226 ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
227 (unsigned int)devc->cur_samplerate, 0);
228 if (ret < 0) {
229 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
230 return SR_ERR;
231 }
232
233 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
234 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
235 devc->hw_params, devc->num_probes);
236 if (ret < 0) {
237 sr_err("Can't set channel count: %s.", snd_strerror(ret));
238 return SR_ERR;
239 }
240
241 sr_dbg("Setting audio parameters.");
242 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
243 if (ret < 0) {
244 sr_err("Can't set parameters: %s.", snd_strerror(ret));
245 return SR_ERR;
246 }
247
248 sr_dbg("Preparing audio interface for use.");
249 ret = snd_pcm_prepare(devc->capture_handle);
250 if (ret < 0) {
251 sr_err("Can't prepare audio interface for use: %s.",
252 snd_strerror(ret));
253 return SR_ERR;
254 }
255
256 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
257 if (count < 1) {
258 sr_err("Unable to obtain poll descriptors count.");
259 return SR_ERR;
260 }
261
262 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
263 sr_err("Failed to malloc ufds.");
264 return SR_ERR_MALLOC;
265 }
266
267 sr_spew("Getting %d poll descriptors.", count);
268 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
269 if (ret < 0) {
270 sr_err("Unable to obtain poll descriptors: %s.",
271 snd_strerror(ret));
272 g_free(devc->ufds);
273 return SR_ERR;
274 }
275
276 /* Send header packet to the session bus. */
277 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
278
279 /* Poll every 10ms, or whenever some data comes in. */
280 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
281 alsa_receive_data, (void *)sdi);
282
283 // g_free(devc->ufds); /* FIXME */
284
285 return SR_OK;
286}
287
288static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
289{
290 struct sr_datafeed_packet packet;
291 struct dev_context *devc;
292
293 devc = sdi->priv;
294 devc->cb_data = cb_data;
295
296 sr_source_remove(devc->ufds[0].fd);
297
298 /* Send end packet to the session bus. */
299 sr_dbg("Sending SR_DF_END packet.");
300 packet.type = SR_DF_END;
301 sr_session_send(cb_data, &packet);
302
303 return SR_OK;
304}
305
306SR_PRIV struct sr_dev_driver alsa_driver_info = {
307 .name = "alsa",
308 .longname = "ALSA driver",
309 .api_version = 1,
310 .init = hw_init,
311 .cleanup = hw_cleanup,
312 .scan = hw_scan,
313 .dev_list = hw_dev_list,
314 .dev_clear = clear_instances,
315 .config_get = config_get,
316 .config_set = config_set,
317 .config_list = config_list,
318 .dev_open = hw_dev_open,
319 .dev_close = hw_dev_close,
320 .dev_acquisition_start = hw_dev_acquisition_start,
321 .dev_acquisition_stop = hw_dev_acquisition_stop,
322 .priv = NULL,
323};