blob: 6cc8de84a52113c78c1ad9643b0796f7a38fa165 [file] [log] [blame]
Ravishankar Sarawadic410e122013-01-03 15:28:38 -08001/* AudioDaemon.cpp
2Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
Duy Truongeb337332013-01-17 10:33:22 -080013 * Neither the name of The Linux Foundation nor the names of its
Ravishankar Sarawadic410e122013-01-03 15:28:38 -080014 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
28
29#define LOG_TAG "AudioDaemon"
30#define LOG_NDEBUG 0
31#define LOG_NDDEBUG 0
32
33#include <media/AudioSystem.h>
34#include <sys/poll.h>
35
36#include "AudioDaemon.h"
37
38int pil_fd;
39#define NUM_FD 1
40int bootup_complete = 0;
Ravishankar Sarawadif0792ac2013-01-25 10:34:46 -080041char adsp_pil_state_file[] = "/sys/kernel/audio_voice_service/status";
Ravishankar Sarawadic410e122013-01-03 15:28:38 -080042
43namespace android {
44
45 AudioDaemon::AudioDaemon() : Thread(false) {
46 }
47
48 AudioDaemon::~AudioDaemon() {
49 }
50
51 void AudioDaemon::onFirstRef() {
52 ALOGV("Start audiod daemon");
53 run("AudioDaemon", PRIORITY_AUDIO);
54 }
55
56 void AudioDaemon::binderDied(const wp<IBinder>& who)
57 {
58 requestExit();
59 }
60
61
62 status_t AudioDaemon::readyToRun() {
63
64 ALOGV("readyToRun: open adsp sysfs node file: %s", adsp_pil_state_file);
65
66 pil_fd = open(adsp_pil_state_file, O_RDONLY);
67
68 if (pil_fd < 0) {
69 ALOGE("File %s open failed (%s)", adsp_pil_state_file, strerror(errno));
70 return errno;
71 }
72 return NO_ERROR;
73 }
74
75 bool AudioDaemon::threadLoop()
76 {
77 int max = -1;
78 int ret;
79 adsp_status cur_state = adsp_offline;
80 struct pollfd pfd[NUM_FD];
81 char rd_buf[9];
82
83 ALOGV("Start threadLoop()");
84
85 if (pil_fd < 0)
86 pil_fd = open(adsp_pil_state_file, O_RDONLY);
87
88 if (pil_fd < 0) {
89 ALOGE("File %s open failed (%s)", adsp_pil_state_file, strerror(errno));
90 return false;
91 }
92
93 pfd[0].fd = pil_fd;
94 pfd[0].events = POLLPRI;
95
96 while (1) {
97 ALOGD("poll() for adsp state change ");
98 if (poll(pfd, 1, -1) < 0) {
99 ALOGE("poll() failed (%s)", strerror(errno));
100 return false;
101 }
102
103 ALOGD("out of poll() for adsp state change ");
104 if (pfd[0].revents & POLLPRI) {
105 if (!read(pil_fd, (void *)rd_buf, 8)) {
106 ALOGE("Error receiving adsp_state event (%s)", strerror(errno));
107 } else {
108 rd_buf[8] = '\0';
109 ALOGV("adsp state file content: %s",rd_buf);
110 lseek(pil_fd, 0, SEEK_SET);
111
112 if (!strncmp(rd_buf, "OFFLINE", strlen("OFFLINE"))) {
113 cur_state = adsp_offline;
114 } else if (!strncmp(rd_buf, "ONLINE", strlen("ONLINE"))) {
115 cur_state = adsp_online;
116 }
117
118 if (bootup_complete) {
119 notifyAudioSystem(cur_state);
120 }
121
122 if (cur_state == adsp_online && !bootup_complete) {
123 bootup_complete = 1;
124 ALOGV("adsp is up, device bootup time");
125 }
126 }
127 }
128 }
129
130 ALOGV("Exiting Poll ThreadLoop");
131 return true;
132 }
133
134 void AudioDaemon::notifyAudioSystem(adsp_status status) {
135
136 ALOGV("notifyAudioSystem : %d", status);
137 switch (status) {
138 case adsp_online:
139 ALOGV("notifyAudioSystem : ADSP_STATUS=ONLINE");
140 AudioSystem::setParameters(0, String8("ADSP_STATUS=ONLINE"));
141 break;
142 case adsp_offline:
143 ALOGV("notifyAudioSystem : ADSP_STATUS=OFFLINE");
144 AudioSystem::setParameters(0, String8("ADSP_STATUS=OFFLINE"));
145 break;
146 }
147 }
148}