]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/api.c
Shorten probe_names[] arrays everywhere.
[libsigrok.git] / hardware / alsa / api.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include "protocol.h"
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
25
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 #define DEFAULT_PROBES          2
31 #define SAMPLE_WIDTH            16
32 #define DEFAULT_SAMPLERATE      44100
33 // #define AUDIO_DEV            "plughw:0,0"
34 #define AUDIO_DEV               "default"
35
36 static const int hwcaps[] = {
37         SR_HWCAP_SAMPLERATE,
38         SR_HWCAP_LIMIT_SAMPLES,
39         SR_HWCAP_CONTINUOUS,
40         0,
41 };
42
43 static const char *probe_names[] = {
44         "Channel 0", "Channel 1",
45         NULL,
46 };
47
48 SR_PRIV struct sr_dev_driver alsa_driver_info;
49 static struct sr_dev_driver *di = &alsa_driver_info;
50
51 static int clear_instances(void)
52 {
53         /* TODO */
54
55         return SR_OK;
56 }
57
58 static int hw_init(struct sr_context *sr_ctx)
59 {
60         struct drv_context *drvc;
61
62         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
63                 sr_err("Driver context malloc failed.");
64                 return SR_ERR_MALLOC;
65         }
66
67         drvc->sr_ctx = sr_ctx;
68         di->priv = drvc;
69
70         return SR_OK;
71 }
72
73 static GSList *hw_scan(GSList *options)
74 {
75         struct drv_context *drvc;
76         struct dev_context *devc;
77         struct sr_dev_inst *sdi;
78         struct sr_probe *probe;
79         GSList *devices;
80         int i;
81
82         (void)options;
83
84         drvc = di->priv;
85         drvc->instances = NULL;
86
87         devices = NULL;
88
89         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
90                 sr_err("Device context malloc failed.");
91                 return NULL;
92         }
93
94         if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
95                 sr_err("Failed to create device instance.");
96                 return NULL;
97         }
98
99         /* Set the samplerate to a default value for now. */
100         devc->cur_samplerate = DEFAULT_SAMPLERATE;
101         devc->num_probes = DEFAULT_PROBES;
102
103         sdi->priv = devc;
104         sdi->driver = di;
105
106         for (i = 0; probe_names[i]; i++) {
107                 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
108                                            probe_names[i]))) {
109                         sr_err("Failed to create probe.");
110                         return NULL;
111                 }
112                 sdi->probes = g_slist_append(sdi->probes, probe);
113         }
114
115         drvc->instances = g_slist_append(drvc->instances, sdi);
116         devices = g_slist_append(devices, sdi);
117
118         return devices;
119 }
120
121 static GSList *hw_dev_list(void)
122 {
123         struct drv_context *drvc;
124
125         drvc = di->priv;
126
127         return drvc->instances;
128 }
129
130 static int hw_dev_open(struct sr_dev_inst *sdi)
131 {
132         struct dev_context *devc;
133         int ret;
134
135         devc = sdi->priv;
136
137         sr_dbg("Opening audio device '%s' for stream capture.", AUDIO_DEV);
138         ret = snd_pcm_open(&devc->capture_handle, AUDIO_DEV,
139                            SND_PCM_STREAM_CAPTURE, 0);
140         if (ret < 0) {
141                 sr_err("Can't open audio device: %s.", snd_strerror(ret));
142                 return SR_ERR;
143         }
144
145         sr_dbg("Allocating hardware parameter structure.");
146         ret = snd_pcm_hw_params_malloc(&devc->hw_params);
147         if (ret < 0) {
148                 sr_err("Can't allocate hardware parameter structure: %s.",
149                        snd_strerror(ret));
150                 return SR_ERR_MALLOC;
151         }
152
153         sr_dbg("Initializing hardware parameter structure.");
154         ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
155         if (ret < 0) {
156                 sr_err("Can't initialize hardware parameter structure: %s.",
157                        snd_strerror(ret));
158                 return SR_ERR;
159         }
160
161         return SR_OK;
162 }
163
164 static int hw_dev_close(struct sr_dev_inst *sdi)
165 {
166         int ret;
167         struct dev_context *devc;
168
169         devc = sdi->priv;
170
171         sr_dbg("Closing device.");
172
173         if (devc->hw_params) {
174                 sr_dbg("Freeing hardware parameters.");
175                 snd_pcm_hw_params_free(devc->hw_params);
176         } else {
177                 sr_dbg("No hardware parameters, no need to free.");
178         }
179
180         if (devc->capture_handle) {
181                 sr_dbg("Closing PCM device.");
182                 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
183                         sr_err("Failed to close device: %s.",
184                                snd_strerror(ret));
185                 }
186         } else {
187                 sr_dbg("No capture handle, no need to close audio device.");
188         }
189
190         return SR_OK;
191 }
192
193 static int hw_cleanup(void)
194 {
195         clear_instances();
196
197         return SR_OK;
198 }
199
200 static int hw_info_get(int info_id, const void **data,
201                        const struct sr_dev_inst *sdi)
202 {
203         struct dev_context *devc;
204
205         if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
206                 devc = sdi->priv;
207
208         switch (info_id) {
209         case SR_DI_HWCAPS:
210                 *data = hwcaps;
211                 break;
212         case SR_DI_NUM_PROBES:
213                 *data = GINT_TO_POINTER(DEFAULT_PROBES);
214                 break;
215         case SR_DI_PROBE_NAMES:
216                 *data = probe_names;
217                 break;
218         case SR_DI_CUR_SAMPLERATE:
219                 *data = &devc->cur_samplerate;
220                 break;
221         default:
222                 sr_err("Invalid info_id: %d.", info_id);
223                 return SR_ERR_ARG;
224         }
225
226         return SR_OK;
227 }
228
229 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
230                              const void *value)
231 {
232         struct dev_context *devc;
233
234         devc = sdi->priv;
235
236         switch (hwcap) {
237         case SR_HWCAP_SAMPLERATE:
238                 devc->cur_samplerate = *(const uint64_t *)value;
239                 break;
240         case SR_HWCAP_LIMIT_SAMPLES:
241                 devc->limit_samples = *(const uint64_t *)value;
242                 break;
243         default:
244                 sr_err("Unknown capability: %d.", hwcap);
245                 return SR_ERR;
246         }
247
248         return SR_OK;
249 }
250
251 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
252                                     void *cb_data)
253 {
254         struct sr_datafeed_packet packet;
255         struct sr_datafeed_header header;
256         struct sr_datafeed_meta_analog meta;
257         struct dev_context *devc;
258         int count, ret;
259
260         devc = sdi->priv;
261         devc->cb_data = cb_data;
262
263         sr_dbg("Setting audio access type to RW/interleaved.");
264         ret = snd_pcm_hw_params_set_access(devc->capture_handle,
265                         devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
266         if (ret < 0) {
267                 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
268                 return SR_ERR;
269         }
270
271         /* FIXME: Hardcoded for 16bits. */
272         sr_dbg("Setting audio sample format to signed 16bit (little endian).");
273         ret = snd_pcm_hw_params_set_format(devc->capture_handle,
274                         devc->hw_params, SND_PCM_FORMAT_S16_LE);
275         if (ret < 0) {
276                 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
277                 return SR_ERR;
278         }
279
280         sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
281                devc->cur_samplerate);
282         ret = snd_pcm_hw_params_set_rate_near(devc->capture_handle,
283                 devc->hw_params, (unsigned int *)&devc->cur_samplerate, 0);
284         if (ret < 0) {
285                 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
286                 return SR_ERR;
287         }
288
289         sr_dbg("Setting audio channel count to %d.", devc->num_probes);
290         ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
291                                              devc->hw_params, devc->num_probes);
292         if (ret < 0) {
293                 sr_err("Can't set channel count: %s.", snd_strerror(ret));
294                 return SR_ERR;
295         }
296
297         sr_dbg("Setting audio parameters.");
298         ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
299         if (ret < 0) {
300                 sr_err("Can't set parameters: %s.", snd_strerror(ret));
301                 return SR_ERR;
302         }
303
304         sr_dbg("Preparing audio interface for use.");
305         ret = snd_pcm_prepare(devc->capture_handle);
306         if (ret < 0) {
307                 sr_err("Can't prepare audio interface for use: %s.",
308                        snd_strerror(ret));
309                 return SR_ERR;
310         }
311
312         count = snd_pcm_poll_descriptors_count(devc->capture_handle);
313         if (count < 1) {
314                 sr_err("Unable to obtain poll descriptors count.");
315                 return SR_ERR;
316         }
317         sr_spew("Obtained poll descriptor count: %d.", count);
318
319         if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
320                 sr_err("Failed to malloc ufds.");
321                 return SR_ERR_MALLOC;
322         }
323
324         sr_err("Getting %d poll descriptors.", count);
325         ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
326         if (ret < 0) {
327                 sr_err("Unable to obtain poll descriptors: %s.",
328                        snd_strerror(ret));
329                 g_free(devc->ufds);
330                 return SR_ERR;
331         }
332
333         /* Send header packet to the session bus. */
334         sr_dbg("Sending SR_DF_HEADER packet.");
335         packet.type = SR_DF_HEADER;
336         packet.payload = (uint8_t *)&header;
337         header.feed_version = 1;
338         gettimeofday(&header.starttime, NULL);
339         sr_session_send(devc->cb_data, &packet);
340
341         /* Send metadata about the SR_DF_ANALOG packets to come. */
342         sr_dbg("Sending SR_DF_META_ANALOG packet.");
343         packet.type = SR_DF_META_ANALOG;
344         packet.payload = &meta;
345         meta.num_probes = devc->num_probes;
346         sr_session_send(devc->cb_data, &packet);
347
348         /* Poll every 10ms, or whenever some data comes in. */
349         sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
350                       alsa_receive_data, (void *)sdi);
351
352         // g_free(devc->ufds); /* FIXME */
353
354         return SR_OK;
355 }
356
357 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
358 {
359         struct sr_datafeed_packet packet;
360         struct dev_context *devc;
361
362         devc = sdi->priv;
363         devc->cb_data = cb_data;
364
365         sr_source_remove(devc->ufds[0].fd);
366
367         /* Send end packet to the session bus. */
368         sr_dbg("Sending SR_DF_END packet.");
369         packet.type = SR_DF_END;
370         sr_session_send(cb_data, &packet);
371
372         return SR_OK;
373 }
374
375 SR_PRIV struct sr_dev_driver alsa_driver_info = {
376         .name = "alsa",
377         .longname = "ALSA driver",
378         .api_version = 1,
379         .init = hw_init,
380         .cleanup = hw_cleanup,
381         .scan = hw_scan,
382         .dev_list = hw_dev_list,
383         .dev_clear = clear_instances,
384         .dev_open = hw_dev_open,
385         .dev_close = hw_dev_close,
386         .info_get = hw_info_get,
387         .dev_config_set = hw_dev_config_set,
388         .dev_acquisition_start = hw_dev_acquisition_start,
389         .dev_acquisition_stop = hw_dev_acquisition_stop,
390         .priv = NULL,
391 };