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 
dumpsys(int fd)176 binder_status_t AudioControl::dumpsys(int fd) {
177     dprintf(fd, "*%s*\n", LOG_TAG);
178     std::shared_ptr<IFocusListener> focusListener = std::atomic_load(&mFocusListener);
179     if (focusListener == nullptr) {
180         dprintf(fd, "No focus listener registered\n");
181     } else {
182         dprintf(fd, "Focus listener registered\n");
183     }
184     return STATUS_OK;
185 }
186 
cmdHelp(int fd) const187 binder_status_t AudioControl::cmdHelp(int fd) const {
188     dprintf(fd, "Usage: \n\n");
189     dprintf(fd, "[no args]: dumps focus listener status\n");
190     dprintf(fd, "--help: shows this help\n");
191     dprintf(fd,
192             "--request <USAGE> <ZONE_ID> <FOCUS_GAIN>: requests audio focus for specified "
193             "usage (string), audio zone ID (int), and focus gain type (int)\n");
194     dprintf(fd,
195             "--abandon <USAGE> <ZONE_ID>: abandons audio focus for specified usage (string) and "
196             "audio zone ID (int)\n");
197     dprintf(fd, "See audio_policy_configuration.xsd for valid AudioUsage values.\n");
198     return STATUS_OK;
199 }
200 
cmdRequestFocus(int fd,const char ** args,uint32_t numArgs)201 binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) {
202     if (!checkCallerHasWritePermissions(fd)) {
203         return STATUS_PERMISSION_DENIED;
204     }
205     if (numArgs != 4) {
206         dprintf(fd,
207                 "Invalid number of arguments: please provide --request <USAGE> <ZONE_ID> "
208                 "<FOCUS_GAIN>\n");
209         return STATUS_BAD_VALUE;
210     }
211 
212     string usage = string(args[1]);
213     if (xsd::isUnknownAudioUsage(usage)) {
214         dprintf(fd,
215                 "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 "
216                 "for supported values\n",
217                 usage.c_str());
218         return STATUS_BAD_VALUE;
219     }
220 
221     int zoneId;
222     if (!safelyParseInt(string(args[2]), &zoneId)) {
223         dprintf(fd, "Non-integer zoneId provided with request: %s\n", string(args[2]).c_str());
224         return STATUS_BAD_VALUE;
225     }
226 
227     int focusGainValue;
228     if (!safelyParseInt(string(args[3]), &focusGainValue)) {
229         dprintf(fd, "Non-integer focusGain provided with request: %s\n", string(args[3]).c_str());
230         return STATUS_BAD_VALUE;
231     }
232     AudioFocusChange focusGain = AudioFocusChange(focusGainValue);
233 
234     std::shared_ptr<IFocusListener> focusListener = std::atomic_load(&mFocusListener);
235     if (focusListener == nullptr) {
236         dprintf(fd, "Unable to request focus - no focus listener registered\n");
237         return STATUS_BAD_VALUE;
238     }
239 
240     focusListener->requestAudioFocus(usage, zoneId, focusGain);
241     dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(),
242             zoneId, focusGain);
243     return STATUS_OK;
244 }
245 
cmdAbandonFocus(int fd,const char ** args,uint32_t numArgs)246 binder_status_t AudioControl::cmdAbandonFocus(int fd, const char** args, uint32_t numArgs) {
247     if (!checkCallerHasWritePermissions(fd)) {
248         return STATUS_PERMISSION_DENIED;
249     }
250     if (numArgs != 3) {
251         dprintf(fd, "Invalid number of arguments: please provide --abandon <USAGE> <ZONE_ID>\n");
252         return STATUS_BAD_VALUE;
253     }
254 
255     string usage = string(args[1]);
256     if (xsd::isUnknownAudioUsage(usage)) {
257         dprintf(fd,
258                 "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 "
259                 "for supported values\n",
260                 usage.c_str());
261         return STATUS_BAD_VALUE;
262     }
263 
264     int zoneId;
265     if (!safelyParseInt(string(args[2]), &zoneId)) {
266         dprintf(fd, "Non-integer zoneId provided with abandon: %s\n", string(args[2]).c_str());
267         return STATUS_BAD_VALUE;
268     }
269 
270     std::shared_ptr<IFocusListener> focusListener = std::atomic_load(&mFocusListener);
271     if (focusListener == nullptr) {
272         dprintf(fd, "Unable to abandon focus - no focus listener registered\n");
273         return STATUS_BAD_VALUE;
274     }
275 
276     focusListener->abandonAudioFocus(usage, zoneId);
277     dprintf(fd, "Abandoned focus for usage %s and zoneId %d\n", usage.c_str(), zoneId);
278     return STATUS_OK;
279 }
280 
281 }  // namespace audiocontrol
282 }  // namespace automotive
283 }  // namespace hardware
284 }  // namespace android
285 }  // namespace aidl
286