OSDN Git Service

android/hal-audio: Add support to register audio endpoints
[android-x86/external-bluetooth-bluez.git] / android / hal-audio.c
1 /*
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <errno.h>
19 #include <pthread.h>
20 #include <poll.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include <hardware/audio.h>
29 #include <hardware/hardware.h>
30
31 #include "audio-msg.h"
32 #include "hal-log.h"
33 #include "hal-msg.h"
34 #include "../profiles/audio/a2dp-codecs.h"
35
36 static const uint8_t a2dp_src_uuid[] = {
37                 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
38                 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
39
40 static int listen_sk = -1;
41 static int audio_sk = -1;
42 static bool close_thread = false;
43
44 static pthread_t ipc_th = 0;
45 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48 struct audio_input_config {
49         uint32_t rate;
50         uint32_t channels;
51         audio_format_t format;
52 };
53
54 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
55
56 struct audio_codec {
57         uint8_t type;
58
59         int (*get_presets) (struct audio_preset *preset, size_t *len);
60
61         int (*init) (struct audio_preset *preset, void **codec_data);
62         int (*cleanup) (void *codec_data);
63         int (*get_config) (void *codec_data,
64                                         struct audio_input_config *config);
65         ssize_t (*write_data) (void *codec_data, const void *buffer,
66                                 size_t bytes);
67 };
68
69 static const struct audio_codec audio_codecs[] = {
70         {
71                 .type = A2DP_CODEC_SBC,
72
73                 .get_presets = sbc_get_presets,
74         }
75 };
76
77 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
78
79 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
80
81 struct audio_endpoint {
82         uint8_t id;
83         const struct audio_codec *codec;
84         void *codec_data;
85         int fd;
86 };
87
88 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
89
90 struct a2dp_audio_dev {
91         struct audio_hw_device dev;
92         struct audio_stream_out *out;
93 };
94
95 static const a2dp_sbc_t sbc_presets[] = {
96         {
97                 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
98                 .channel_mode = SBC_CHANNEL_MODE_MONO |
99                                 SBC_CHANNEL_MODE_DUAL_CHANNEL |
100                                 SBC_CHANNEL_MODE_STEREO |
101                                 SBC_CHANNEL_MODE_JOINT_STEREO,
102                 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
103                 .allocation_method = SBC_ALLOCATION_SNR |
104                                         SBC_ALLOCATION_LOUDNESS,
105                 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
106                                 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
107                 .min_bitpool = MIN_BITPOOL,
108                 .max_bitpool = MAX_BITPOOL
109         },
110         {
111                 .frequency = SBC_SAMPLING_FREQ_44100,
112                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
113                 .subbands = SBC_SUBBANDS_8,
114                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
115                 .block_length = SBC_BLOCK_LENGTH_16,
116                 .min_bitpool = MIN_BITPOOL,
117                 .max_bitpool = MAX_BITPOOL
118         },
119         {
120                 .frequency = SBC_SAMPLING_FREQ_48000,
121                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
122                 .subbands = SBC_SUBBANDS_8,
123                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
124                 .block_length = SBC_BLOCK_LENGTH_16,
125                 .min_bitpool = MIN_BITPOOL,
126                 .max_bitpool = MAX_BITPOOL
127         },
128 };
129
130 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
131 {
132         int i;
133         int count;
134         size_t new_len = 0;
135         uint8_t *ptr = (uint8_t *) preset;
136         size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
137
138         DBG("");
139
140         count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
141
142         for (i = 0; i < count; i++) {
143                 preset = (struct audio_preset *) ptr;
144
145                 if (new_len + preset_size > *len)
146                         break;
147
148                 preset->len = sizeof(a2dp_sbc_t);
149                 memcpy(preset->data, &sbc_presets[i], preset->len);
150
151                 new_len += preset_size;
152                 ptr += preset_size;
153         }
154
155         *len = new_len;
156
157         return i;
158 }
159
160 static void audio_ipc_cleanup(void)
161 {
162         if (audio_sk >= 0) {
163                 shutdown(audio_sk, SHUT_RDWR);
164                 audio_sk = -1;
165         }
166 }
167
168 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
169                         void *param, size_t *rsp_len, void *rsp, int *fd)
170 {
171         ssize_t ret;
172         struct msghdr msg;
173         struct iovec iv[2];
174         struct hal_hdr cmd;
175         char cmsgbuf[CMSG_SPACE(sizeof(int))];
176         struct hal_status s;
177         size_t s_len = sizeof(s);
178
179         if (audio_sk < 0) {
180                 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
181                 goto failed;
182         }
183
184         if (!rsp || !rsp_len) {
185                 memset(&s, 0, s_len);
186                 rsp_len = &s_len;
187                 rsp = &s;
188         }
189
190         memset(&msg, 0, sizeof(msg));
191         memset(&cmd, 0, sizeof(cmd));
192
193         cmd.service_id = service_id;
194         cmd.opcode = opcode;
195         cmd.len = len;
196
197         iv[0].iov_base = &cmd;
198         iv[0].iov_len = sizeof(cmd);
199
200         iv[1].iov_base = param;
201         iv[1].iov_len = len;
202
203         msg.msg_iov = iv;
204         msg.msg_iovlen = 2;
205
206         pthread_mutex_lock(&sk_mutex);
207
208         ret = sendmsg(audio_sk, &msg, 0);
209         if (ret < 0) {
210                 error("audio: Sending command failed:%s", strerror(errno));
211                 pthread_mutex_unlock(&sk_mutex);
212                 goto failed;
213         }
214
215         /* socket was shutdown */
216         if (ret == 0) {
217                 error("audio: Command socket closed");
218                 goto failed;
219         }
220
221         memset(&msg, 0, sizeof(msg));
222         memset(&cmd, 0, sizeof(cmd));
223
224         iv[0].iov_base = &cmd;
225         iv[0].iov_len = sizeof(cmd);
226
227         iv[1].iov_base = rsp;
228         iv[1].iov_len = *rsp_len;
229
230         msg.msg_iov = iv;
231         msg.msg_iovlen = 2;
232
233         if (fd) {
234                 memset(cmsgbuf, 0, sizeof(cmsgbuf));
235                 msg.msg_control = cmsgbuf;
236                 msg.msg_controllen = sizeof(cmsgbuf);
237         }
238
239         ret = recvmsg(audio_sk, &msg, 0);
240         if (ret < 0) {
241                 error("audio: Receiving command response failed:%s",
242                                                         strerror(errno));
243                 pthread_mutex_unlock(&sk_mutex);
244                 goto failed;
245         }
246
247         pthread_mutex_unlock(&sk_mutex);
248
249         if (ret < (ssize_t) sizeof(cmd)) {
250                 error("audio: Too small response received(%zd bytes)", ret);
251                 goto failed;
252         }
253
254         if (cmd.service_id != service_id) {
255                 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
256                                                                 service_id);
257                 goto failed;
258         }
259
260         if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
261                 error("audio: Malformed response received(%zd bytes)", ret);
262                 goto failed;
263         }
264
265         if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
266                 error("audio: Invalid opcode received (%u vs %u)",
267                                                 cmd.opcode, opcode);
268                 goto failed;
269         }
270
271         if (cmd.opcode == AUDIO_OP_STATUS) {
272                 struct hal_status *s = rsp;
273
274                 if (sizeof(*s) != cmd.len) {
275                         error("audio: Invalid status length");
276                         goto failed;
277                 }
278
279                 if (s->code == AUDIO_STATUS_SUCCESS) {
280                         error("audio: Invalid success status response");
281                         goto failed;
282                 }
283
284                 return s->code;
285         }
286
287         /* Receive auxiliary data in msg */
288         if (fd) {
289                 struct cmsghdr *cmsg;
290
291                 *fd = -1;
292
293                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
294                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
295                         if (cmsg->cmsg_level == SOL_SOCKET
296                                         && cmsg->cmsg_type == SCM_RIGHTS) {
297                                 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
298                                 break;
299                         }
300                 }
301         }
302
303         if (rsp_len)
304                 *rsp_len = cmd.len;
305
306         return AUDIO_STATUS_SUCCESS;
307
308 failed:
309         /* Some serious issue happen on IPC - recover */
310         shutdown(audio_sk, SHUT_RDWR);
311         audio_sk = -1;
312         return AUDIO_STATUS_FAILED;
313 }
314
315 static int ipc_open_cmd(const struct audio_codec *codec)
316 {
317         uint8_t buf[BLUEZ_AUDIO_MTU];
318         struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
319         struct audio_rsp_open rsp;
320         size_t cmd_len = sizeof(buf) - sizeof(*cmd);
321         size_t rsp_len = sizeof(rsp);
322         int result;
323
324         DBG("");
325
326         memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
327
328         cmd->codec = codec->type;
329         cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
330
331         cmd_len += sizeof(*cmd);
332
333         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
334                                 &rsp_len, &rsp, NULL);
335
336         if (result != AUDIO_STATUS_SUCCESS)
337                 return 0;
338
339         return rsp.id;
340 }
341
342 static int register_endpoints(void)
343 {
344         struct audio_endpoint *ep = &audio_endpoints[0];
345         size_t i;
346
347         for (i = 0; i < NUM_CODECS; i++, ep++) {
348                 const struct audio_codec *codec = &audio_codecs[i];
349
350                 ep->id = ipc_open_cmd(codec);
351
352                 if (!ep->id)
353                         return AUDIO_STATUS_FAILED;
354
355                 ep->codec = codec;
356                 ep->codec_data = NULL;
357                 ep->fd = -1;
358         }
359
360         return AUDIO_STATUS_SUCCESS;
361 }
362
363 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
364                                                                 size_t bytes)
365 {
366         DBG("");
367         return -ENOSYS;
368 }
369
370 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
371 {
372         DBG("");
373         return -ENOSYS;
374 }
375
376 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
377 {
378         DBG("");
379         return -ENOSYS;
380 }
381
382 static size_t out_get_buffer_size(const struct audio_stream *stream)
383 {
384         DBG("");
385         return -ENOSYS;
386 }
387
388 static uint32_t out_get_channels(const struct audio_stream *stream)
389 {
390         DBG("");
391         return -ENOSYS;
392 }
393
394 static audio_format_t out_get_format(const struct audio_stream *stream)
395 {
396         DBG("");
397         return -ENOSYS;
398 }
399
400 static int out_set_format(struct audio_stream *stream, audio_format_t format)
401 {
402         DBG("");
403         return -ENOSYS;
404 }
405
406 static int out_standby(struct audio_stream *stream)
407 {
408         DBG("");
409         return -ENOSYS;
410 }
411
412 static int out_dump(const struct audio_stream *stream, int fd)
413 {
414         DBG("");
415         return -ENOSYS;
416 }
417
418 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
419 {
420         DBG("");
421         return -ENOSYS;
422 }
423
424 static char *out_get_parameters(const struct audio_stream *stream,
425                                                         const char *keys)
426 {
427         DBG("");
428         return strdup("");
429 }
430
431 static uint32_t out_get_latency(const struct audio_stream_out *stream)
432 {
433         DBG("");
434         return -ENOSYS;
435 }
436
437 static int out_set_volume(struct audio_stream_out *stream, float left,
438                                                                 float right)
439 {
440         DBG("");
441         /* volume controlled in audioflinger mixer (digital) */
442         return -ENOSYS;
443 }
444
445 static int out_get_render_position(const struct audio_stream_out *stream,
446                                                         uint32_t *dsp_frames)
447 {
448         DBG("");
449         return -ENOSYS;
450 }
451
452 static int out_add_audio_effect(const struct audio_stream *stream,
453                                                         effect_handle_t effect)
454 {
455         DBG("");
456         return -ENOSYS;
457 }
458
459 static int out_remove_audio_effect(const struct audio_stream *stream,
460                                                         effect_handle_t effect)
461 {
462         DBG("");
463         return -ENOSYS;
464 }
465
466 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
467 {
468         DBG("");
469         return -ENOSYS;
470 }
471
472 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
473 {
474         DBG("");
475         return -ENOSYS;
476 }
477
478 static size_t in_get_buffer_size(const struct audio_stream *stream)
479 {
480         DBG("");
481         return -ENOSYS;
482 }
483
484 static uint32_t in_get_channels(const struct audio_stream *stream)
485 {
486         DBG("");
487         return -ENOSYS;
488 }
489
490 static audio_format_t in_get_format(const struct audio_stream *stream)
491 {
492         DBG("");
493         return -ENOSYS;
494 }
495
496 static int in_set_format(struct audio_stream *stream, audio_format_t format)
497 {
498         DBG("");
499         return -ENOSYS;
500 }
501
502 static int in_standby(struct audio_stream *stream)
503 {
504         DBG("");
505         return -ENOSYS;
506 }
507
508 static int in_dump(const struct audio_stream *stream, int fd)
509 {
510         DBG("");
511         return -ENOSYS;
512 }
513
514 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
515 {
516         DBG("");
517         return -ENOSYS;
518 }
519
520 static char *in_get_parameters(const struct audio_stream *stream,
521                                                         const char *keys)
522 {
523         DBG("");
524         return strdup("");
525 }
526
527 static int in_set_gain(struct audio_stream_in *stream, float gain)
528 {
529         DBG("");
530         return -ENOSYS;
531 }
532
533 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
534                                                                 size_t bytes)
535 {
536         DBG("");
537         return -ENOSYS;
538 }
539
540 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
541 {
542         DBG("");
543         return -ENOSYS;
544 }
545
546 static int in_add_audio_effect(const struct audio_stream *stream,
547                                                         effect_handle_t effect)
548 {
549         DBG("");
550         return -ENOSYS;
551 }
552
553 static int in_remove_audio_effect(const struct audio_stream *stream,
554                                                         effect_handle_t effect)
555 {
556         DBG("");
557         return -ENOSYS;
558 }
559
560 static int audio_open_output_stream(struct audio_hw_device *dev,
561                                         audio_io_handle_t handle,
562                                         audio_devices_t devices,
563                                         audio_output_flags_t flags,
564                                         struct audio_config *config,
565                                         struct audio_stream_out **stream_out)
566
567 {
568         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
569         struct audio_stream_out *out;
570
571         out = calloc(1, sizeof(struct audio_stream_out));
572         if (!out)
573                 return -ENOMEM;
574
575         DBG("");
576
577         out->common.get_sample_rate = out_get_sample_rate;
578         out->common.set_sample_rate = out_set_sample_rate;
579         out->common.get_buffer_size = out_get_buffer_size;
580         out->common.get_channels = out_get_channels;
581         out->common.get_format = out_get_format;
582         out->common.set_format = out_set_format;
583         out->common.standby = out_standby;
584         out->common.dump = out_dump;
585         out->common.set_parameters = out_set_parameters;
586         out->common.get_parameters = out_get_parameters;
587         out->common.add_audio_effect = out_add_audio_effect;
588         out->common.remove_audio_effect = out_remove_audio_effect;
589         out->get_latency = out_get_latency;
590         out->set_volume = out_set_volume;
591         out->write = out_write;
592         out->get_render_position = out_get_render_position;
593
594         *stream_out = out;
595         a2dp_dev->out = out;
596
597         return 0;
598 }
599
600 static void audio_close_output_stream(struct audio_hw_device *dev,
601                                         struct audio_stream_out *stream)
602 {
603         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
604
605         DBG("");
606
607         free(stream);
608         a2dp_dev->out = NULL;
609 }
610
611 static int audio_set_parameters(struct audio_hw_device *dev,
612                                                         const char *kvpairs)
613 {
614         DBG("");
615         return -ENOSYS;
616 }
617
618 static char *audio_get_parameters(const struct audio_hw_device *dev,
619                                                         const char *keys)
620 {
621         DBG("");
622         return strdup("");
623 }
624
625 static int audio_init_check(const struct audio_hw_device *dev)
626 {
627         DBG("");
628         return -ENOSYS;
629 }
630
631 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
632 {
633         DBG("");
634         return -ENOSYS;
635 }
636
637 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
638 {
639         DBG("");
640         return -ENOSYS;
641 }
642
643 static int audio_set_mode(struct audio_hw_device *dev, int mode)
644 {
645         DBG("");
646         return -ENOSYS;
647 }
648
649 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
650 {
651         DBG("");
652         return -ENOSYS;
653 }
654
655 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
656 {
657         DBG("");
658         return -ENOSYS;
659 }
660
661 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
662                                         const struct audio_config *config)
663 {
664         DBG("");
665         return -ENOSYS;
666 }
667
668 static int audio_open_input_stream(struct audio_hw_device *dev,
669                                         audio_io_handle_t handle,
670                                         audio_devices_t devices,
671                                         struct audio_config *config,
672                                         struct audio_stream_in **stream_in)
673 {
674         struct audio_stream_in *in;
675
676         DBG("");
677
678         in = calloc(1, sizeof(struct audio_stream_in));
679         if (!in)
680                 return -ENOMEM;
681
682         in->common.get_sample_rate = in_get_sample_rate;
683         in->common.set_sample_rate = in_set_sample_rate;
684         in->common.get_buffer_size = in_get_buffer_size;
685         in->common.get_channels = in_get_channels;
686         in->common.get_format = in_get_format;
687         in->common.set_format = in_set_format;
688         in->common.standby = in_standby;
689         in->common.dump = in_dump;
690         in->common.set_parameters = in_set_parameters;
691         in->common.get_parameters = in_get_parameters;
692         in->common.add_audio_effect = in_add_audio_effect;
693         in->common.remove_audio_effect = in_remove_audio_effect;
694         in->set_gain = in_set_gain;
695         in->read = in_read;
696         in->get_input_frames_lost = in_get_input_frames_lost;
697
698         *stream_in = in;
699
700         return 0;
701 }
702
703 static void audio_close_input_stream(struct audio_hw_device *dev,
704                                         struct audio_stream_in *stream_in)
705 {
706         DBG("");
707         free(stream_in);
708 }
709
710 static int audio_dump(const audio_hw_device_t *device, int fd)
711 {
712         DBG("");
713         return -ENOSYS;
714 }
715
716 static int audio_close(hw_device_t *device)
717 {
718         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
719
720         DBG("");
721
722         pthread_mutex_lock(&close_mutex);
723         audio_ipc_cleanup();
724         close_thread = true;
725         pthread_mutex_unlock(&close_mutex);
726
727         pthread_join(ipc_th, NULL);
728
729         close(listen_sk);
730         listen_sk = -1;
731
732         free(a2dp_dev);
733         return 0;
734 }
735
736 static void *ipc_handler(void *data)
737 {
738         bool done = false;
739         struct pollfd pfd;
740
741         DBG("");
742
743         while (!done) {
744                 DBG("Waiting for connection ...");
745                 audio_sk = accept(listen_sk, NULL, NULL);
746                 if (audio_sk < 0) {
747                         int err = errno;
748                         error("audio: Failed to accept socket: %d (%s)", err,
749                                                                 strerror(err));
750                         continue;
751                 }
752
753                 DBG("Audio IPC: Connected");
754
755                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
756                         error("audio: Failed to register endpoints");
757
758                         shutdown(audio_sk, SHUT_RDWR);
759                         continue;
760                 }
761
762                 memset(&pfd, 0, sizeof(pfd));
763                 pfd.fd = audio_sk;
764                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
765
766                 /* Check if socket is still alive. Empty while loop.*/
767                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
768
769                 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
770                         info("Audio HAL: Socket closed");
771                         audio_sk = -1;
772                 }
773
774                 /*Check if audio_dev is closed */
775                 pthread_mutex_lock(&close_mutex);
776                 done = close_thread;
777                 close_thread = false;
778                 pthread_mutex_unlock(&close_mutex);
779         }
780
781         info("Closing Audio IPC thread");
782         return NULL;
783 }
784
785 static int audio_ipc_init(void)
786 {
787         struct sockaddr_un addr;
788         int err;
789         int sk;
790
791         DBG("");
792
793         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
794         if (sk < 0) {
795                 err = errno;
796                 error("audio: Failed to create socket: %d (%s)", err,
797                                                                 strerror(err));
798                 return err;
799         }
800
801         memset(&addr, 0, sizeof(addr));
802         addr.sun_family = AF_UNIX;
803
804         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
805                                         sizeof(BLUEZ_AUDIO_SK_PATH));
806
807         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
808                 err = errno;
809                 error("audio: Failed to bind socket: %d (%s)", err,
810                                                                 strerror(err));
811                 goto failed;
812         }
813
814         if (listen(sk, 1) < 0) {
815                 err = errno;
816                 error("audio: Failed to listen on the socket: %d (%s)", err,
817                                                                 strerror(err));
818                 goto failed;
819         }
820
821         listen_sk = sk;
822
823         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
824         if (err) {
825                 err = -err;
826                 ipc_th = 0;
827                 error("audio: Failed to start Audio IPC thread: %d (%s)",
828                                                         err, strerror(err));
829                 goto failed;
830         }
831
832         return 0;
833
834 failed:
835         close(sk);
836         return err;
837 }
838
839 static int audio_open(const hw_module_t *module, const char *name,
840                                                         hw_device_t **device)
841 {
842         struct a2dp_audio_dev *a2dp_dev;
843         int err;
844
845         DBG("");
846
847         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
848                 error("audio: interface %s not matching [%s]", name,
849                                                 AUDIO_HARDWARE_INTERFACE);
850                 return -EINVAL;
851         }
852
853         err = audio_ipc_init();
854         if (err)
855                 return -err;
856
857         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
858         if (!a2dp_dev)
859                 return -ENOMEM;
860
861         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
862         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
863         a2dp_dev->dev.common.close = audio_close;
864
865         a2dp_dev->dev.init_check = audio_init_check;
866         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
867         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
868         a2dp_dev->dev.set_mode = audio_set_mode;
869         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
870         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
871         a2dp_dev->dev.set_parameters = audio_set_parameters;
872         a2dp_dev->dev.get_parameters = audio_get_parameters;
873         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
874         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
875         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
876         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
877         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
878         a2dp_dev->dev.dump = audio_dump;
879
880         /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
881          * This results from the structure of following structs:a2dp_audio_dev,
882          * audio_hw_device. We will rely on this later in the code.*/
883         *device = &a2dp_dev->dev.common;
884
885         return 0;
886 }
887
888 static struct hw_module_methods_t hal_module_methods = {
889         .open = audio_open,
890 };
891
892 struct audio_module HAL_MODULE_INFO_SYM = {
893         .common = {
894         .tag = HARDWARE_MODULE_TAG,
895         .version_major = 1,
896         .version_minor = 0,
897         .id = AUDIO_HARDWARE_MODULE_ID,
898         .name = "A2DP Bluez HW HAL",
899         .author = "Intel Corporation",
900         .methods = &hal_module_methods,
901         },
902 };