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