• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 #define LOG_TAG "AudioControl"
18 // #define LOG_NDEBUG 0
19 
20 #include "AudioControl.h"
21 #include "audio_hw_control.h"
22 
23 #include <aidl/android/hardware/automotive/audiocontrol/AudioFocusChange.h>
24 #include <aidl/android/hardware/automotive/audiocontrol/DuckingInfo.h>
25 #include <aidl/android/hardware/automotive/audiocontrol/IFocusListener.h>
26 
27 #include <android-base/logging.h>
28 #include <android-base/parseint.h>
29 #include <android-base/strings.h>
30 
31 #include <android_audio_policy_configuration_V7_0-enums.h>
32 #include <private/android_filesystem_config.h>
33 
34 #include <stdio.h>
35 
36 namespace aidl {
37 namespace android {
38 namespace hardware {
39 namespace automotive {
40 namespace audiocontrol {
41 
42 using ::android::base::EqualsIgnoreCase;
43 using ::android::base::ParseInt;
44 using ::std::shared_ptr;
45 using ::std::string;
46 
47 namespace xsd {
48 using namespace ::android::audio::policy::configuration::V7_0;
49 }
50 
51 namespace {
52 const float kLowerBound = -1.0f;
53 const float kUpperBound = 1.0f;
checkCallerHasWritePermissions(int fd)54 bool checkCallerHasWritePermissions(int fd) {
55     // Double check that's only called by root - it should be be blocked at debug() level,
56     // but it doesn't hurt to make sure...
57     if (AIBinder_getCallingUid() != AID_ROOT) {
58         dprintf(fd, "Must be root\n");
59         return false;
60     }
61     return true;
62 }
63 
isValidValue(float value)64 bool isValidValue(float value) {
65     return (value >= kLowerBound) && (value <= kUpperBound);
66 }
67 
safelyParseInt(string s,int * out)68 bool safelyParseInt(string s, int* out) {
69     if (!ParseInt(s, out)) {
70         return false;
71     }
72     return true;
73 }
74 }  // namespace
75 
registerFocusListener(const shared_ptr<IFocusListener> & in_listener)76 ndk::ScopedAStatus AudioControl::registerFocusListener(
77         const shared_ptr<IFocusListener>& in_listener) {
78 
79     if (in_listener) {
80         std::atomic_store(&mFocusListener, in_listener);
81     } else {
82         LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op.";
83     }
84     return ndk::ScopedAStatus::ok();
85 }
86 
setBalanceTowardRight(float value)87 ndk::ScopedAStatus AudioControl::setBalanceTowardRight(float value) {
88     if (isValidValue(value)) {
89         // Just log in this default mock implementation
90         LOG(INFO) << "Balance set to " << value;
91         return ndk::ScopedAStatus::ok();
92     }
93 
94     LOG(ERROR) << "Balance value out of range -1 to 1 at " << value;
95     return ndk::ScopedAStatus::ok();
96 }
97 
setFadeTowardFront(float value)98 ndk::ScopedAStatus AudioControl::setFadeTowardFront(float value) {
99     if (isValidValue(value)) {
100         // Just log in this default mock implementation
101         LOG(INFO) << "Fader set to " << value;
102         return ndk::ScopedAStatus::ok();
103     }
104     LOG(ERROR) << "Fader value out of range -1 to 1 at " << value;
105     return ndk::ScopedAStatus::ok();
106 }
107 
onAudioFocusChange(const string & in_usage,int32_t in_zoneId,AudioFocusChange in_focusChange)108 ndk::ScopedAStatus AudioControl::onAudioFocusChange(const string& in_usage, int32_t in_zoneId,
109                                                     AudioFocusChange in_focusChange) {
110     LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for usage "
111               << in_usage.c_str() << " in zone " << in_zoneId;
112     return ndk::ScopedAStatus::ok();
113 }
114 
onDevicesToDuckChange(const std::vector<DuckingInfo> & in_duckingInfos)115 ndk::ScopedAStatus AudioControl::onDevicesToDuckChange(
116         const std::vector<DuckingInfo>& in_duckingInfos) {
117     LOG(DEBUG) << "AudioControl::onDevicesToDuckChange";
118     for (const DuckingInfo& duckingInfo : in_duckingInfos) {
119         LOG(DEBUG) << "zone: " << duckingInfo.zoneId;
120         LOG(DEBUG) << "Devices to duck:";
121         for (const auto& addressToDuck : duckingInfo.deviceAddressesToDuck) {
122             LOG(DEBUG) << addressToDuck;
123             set_device_address_is_ducked(addressToDuck.c_str(), true);
124         }
125 
126         LOG(DEBUG) << "Devices to unduck:";
127         for (const auto& addressToUnduck : duckingInfo.deviceAddressesToUnduck) {
128             LOG(DEBUG) << addressToUnduck;
129             set_device_address_is_ducked(addressToUnduck.c_str(), false);
130         }
131         LOG(DEBUG) << "Usages holding focus:";
132         for (const auto& usage : duckingInfo.usagesHoldingFocus) {
133             LOG(DEBUG) << usage;
134         }
135     }
136     return ndk::ScopedAStatus::ok();
137 }
138 
onDevicesToMuteChange(const std::vector<MutingInfo> & in_mutingInfos)139 ndk::ScopedAStatus AudioControl::onDevicesToMuteChange(
140         const std::vector<MutingInfo>& in_mutingInfos) {
141     LOG(DEBUG) << "AudioControl::onDevicesToMuteChange";
142     for (const MutingInfo& mutingInfo : in_mutingInfos) {
143         LOG(DEBUG) << "zone: " << mutingInfo.zoneId;
144         LOG(DEBUG) << "Devices to mute:";
145         for (const auto& addressToMute : mutingInfo.deviceAddressesToMute) {
146             LOG(DEBUG) << addressToMute;
147             set_device_address_is_muted(addressToMute.c_str(), true);
148         }
149         LOG(DEBUG) << "Devices to unmute:";
150         for (const auto& addressToUnmute : mutingInfo.deviceAddressesToUnmute) {
151             LOG(DEBUG) << addressToUnmute;
152             set_device_address_is_muted(addressToUnmute.c_str(), false);
153         }
154     }
155     return ndk::ScopedAStatus::ok();
156 }
157 
dump(int fd,const char ** args,uint32_t numArgs)158 binder_status_t AudioControl::dump(int fd, const char** args, uint32_t numArgs) {
159     if (numArgs == 0) {
160         return dumpsys(fd);
161     }
162 
163     string option = string(args[0]);
164     if (EqualsIgnoreCase(option, "--help")) {
165         return cmdHelp(fd);
166     } else if (EqualsIgnoreCase(option, "--request")) {
167         return cmdRequestFocus(fd, args, numArgs);
168     } else if (EqualsIgnoreCase(option, "--abandon")) {
169         return cmdAbandonFocus(fd, args, numArgs);
170     } else {
171         dprintf(fd, "Invalid option: %s\n", option.c_str());
172         return STATUS_BAD_VALUE;
173     }
174 }
175 
setAudioEnabled(bool isEnabled)176 void AudioControl::setAudioEnabled(bool isEnabled) {
177     LOG(DEBUG) << "setAudioEnabled called with value: " << isEnabled;
178     set_audio_enabled(isEnabled);
179 }
180 
dumpsys(int fd)181 binder_status_t AudioControl::dumpsys(int fd) {
182     dprintf(fd, "*%s*\n", LOG_TAG);
183     std::shared_ptr<IFocusListener> focusListener = std::atomic_load(&mFocusListener);
184     if (focusListener == nullptr) {
185         dprintf(fd, "No focus listener registered\n");
186     } else {
187         dprintf(fd, "Focus listener registered\n");
188     }
189     return STATUS_OK;
190 }
191 
cmdHelp(int fd) const192 binder_status_t AudioControl::cmdHelp(int fd) const {
193     dprintf(fd, "Usage: \n\n");
194     dprintf(fd, "[no args]: dumps focus listener status\n");
195     dprintf(fd, "--help: shows this help\n");
196     dprintf(fd,
197             "--request <USAGE> <ZONE_ID> <FOCUS_GAIN>: requests audio focus for specified "
198             "usage (string), audio zone ID (int), and focus gain type (int)\n");
199     dprintf(fd,
200             "--abandon <USAGE> <ZONE_ID>: abandons audio focus for specified usage (string) and "
201             "audio zone ID (int)\n");
202     dprintf(fd, "See audio_policy_configuration.xsd for valid AudioUsage values.\n");
203     return STATUS_OK;
204 }
205 
cmdRequestFocus(int fd,const char ** args,uint32_t numArgs)206 binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) {
207     if (!checkCallerHasWritePermissions(fd)) {
208         return STATUS_PERMISSION_DENIED;
209     }
210     if (numArgs != 4) {
211         dprintf(fd,
212                 "Invalid number of arguments: please provide --request <USAGE> <ZONE_ID> "
213                 "<FOCUS_GAIN>\n");
214         return STATUS_BAD_VALUE;
215     }
216 
217     string usage = string(args[1]);
218     if (xsd::isUnknownAudioUsage(usage)) {
219         dprintf(fd,
220                 "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 "
221                 "for supported values\n",
222                 usage.c_str());
223         return STATUS_BAD_VALUE;
224     }
225 
226     int zoneId;
227     if (!safelyParseInt(string(args[2]), &zoneId)) {
228         dprintf(fd, "Non-integer zoneId provided with request: %s\n", string(args[2]).c_str());
229         return STATUS_BAD_VALUE;
230     }
231 
232     int focusGainValue;
233     if (!safelyParseInt(string(args[3]), &focusGainValue)) {
234         dprintf(fd, "Non-integer focusGain provided with request: %s\n", string(args[3]).c_str());
235         return STATUS_BAD_VALUE;
236     }
237     AudioFocusChange focusGain = AudioFocusChange(focusGainValue);
238 
239     std::shared_ptr<IFocusListener> focusListener = std::atomic_load(&mFocusListener);
240     if (focusListener == nullptr) {
241         dprintf(fd, "Unable to request focus - no focus listener registered\n");
242         return STATUS_BAD_VALUE;
243     }
244 
245     focusListener->requestAudioFocus(usage, zoneId, focusGain);
246     dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(),
247             zoneId, focusGain);
248     return STATUS_OK;
249 }
250 
cmdAbandonFocus(int fd,const char ** args,uint32_t numArgs)251 binder_status_t AudioControl::cmdAbandonFocus(int fd, const char** args, uint32_t numArgs) {
252     if (!checkCallerHasWritePermissions(fd)) {
253         return STATUS_PERMISSION_DENIED;
254     }
255     if (numArgs != 3) {
256         dprintf(fd, "Invalid number of arguments: please provide --abandon <USAGE> <ZONE_ID>\n");
257         return STATUS_BAD_VALUE;
258     }
259 
260     string usage = string(args[1]);
261     if (xsd::isUnknownAudioUsage(usage)) {
262         dprintf(fd,
263                 "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 "
264                 "for supported values\n",
265                 usage.c_str());
266         return STATUS_BAD_VALUE;
267     }
268 
269     int zoneId;
270     if (!safelyParseInt(string(args[2]), &zoneId)) {
271         dprintf(fd, "Non-integer zoneId provided with abandon: %s\n", string(args[2]).c_str());
272         return STATUS_BAD_VALUE;
273     }
274 
275     std::shared_ptr<IFocusListener> focusListener = std::atomic_load(&mFocusListener);
276     if (focusListener == nullptr) {
277         dprintf(fd, "Unable to abandon focus - no focus listener registered\n");
278         return STATUS_BAD_VALUE;
279     }
280 
281     focusListener->abandonAudioFocus(usage, zoneId);
282     dprintf(fd, "Abandoned focus for usage %s and zoneId %d\n", usage.c_str(), zoneId);
283     return STATUS_OK;
284 }
285 
286 }  // namespace audiocontrol
287 }  // namespace automotive
288 }  // namespace hardware
289 }  // namespace android
290 }  // namespace aidl
291