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