Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 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 | #include "AudioControl.h" |
| 18 | |
| 19 | #include <aidl/android/hardware/automotive/audiocontrol/AudioFocusChange.h> |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 20 | #include <aidl/android/hardware/automotive/audiocontrol/DuckingInfo.h> |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 21 | #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 Gomes | e502a60 | 2020-11-06 15:15:26 -0800 | [diff] [blame] | 27 | #include <android_audio_policy_configuration_V7_0.h> |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | namespace aidl::android::hardware::automotive::audiocontrol { |
| 33 | |
| 34 | using ::android::base::EqualsIgnoreCase; |
| 35 | using ::android::base::ParseInt; |
| 36 | using ::std::string; |
| 37 | |
| 38 | namespace xsd { |
Hayden Gomes | e502a60 | 2020-11-06 15:15:26 -0800 | [diff] [blame] | 39 | using namespace ::android::audio::policy::configuration::V7_0; |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | namespace { |
| 43 | const float kLowerBound = -1.0f; |
| 44 | const float kUpperBound = 1.0f; |
| 45 | bool 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 | |
| 55 | bool isValidValue(float value) { |
| 56 | return (value >= kLowerBound) && (value <= kUpperBound); |
| 57 | } |
| 58 | |
| 59 | bool safelyParseInt(string s, int* out) { |
| 60 | if (!ParseInt(s, out)) { |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | } // namespace |
| 66 | |
| 67 | ndk::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 | |
| 79 | ndk::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 | |
| 89 | ndk::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 | |
| 99 | ndk::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 Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 106 | ndk::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:"; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 112 | for (const auto& addressToDuck : duckingInfo.deviceAddressesToDuck) { |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 113 | LOG(INFO) << addressToDuck; |
| 114 | } |
| 115 | LOG(INFO) << "Devices to unduck:"; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 116 | for (const auto& addressToUnduck : duckingInfo.deviceAddressesToUnduck) { |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 117 | LOG(INFO) << addressToUnduck; |
| 118 | } |
| 119 | LOG(INFO) << "Usages holding focus:"; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 120 | for (const auto& usage : duckingInfo.usagesHoldingFocus) { |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 121 | LOG(INFO) << usage; |
| 122 | } |
| 123 | } |
| 124 | return ndk::ScopedAStatus::ok(); |
| 125 | } |
| 126 | |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 127 | ndk::ScopedAStatus AudioControl::onDevicesToMuteChange( |
| 128 | const std::vector<MutingInfo>& in_mutingInfos) { |
| 129 | LOG(INFO) << "AudioControl::onDevicesToMuteChange"; |
| 130 | for (const MutingInfo& mutingInfo : in_mutingInfos) { |
| 131 | LOG(INFO) << "zone: " << mutingInfo.zoneId; |
| 132 | LOG(INFO) << "Devices to mute:"; |
| 133 | for (const auto& addressToMute : mutingInfo.deviceAddressesToMute) { |
| 134 | LOG(INFO) << addressToMute; |
| 135 | } |
| 136 | LOG(INFO) << "Devices to unmute:"; |
| 137 | for (const auto& addressToUnmute : mutingInfo.deviceAddressesToUnmute) { |
| 138 | LOG(INFO) << addressToUnmute; |
| 139 | } |
| 140 | } |
| 141 | return ndk::ScopedAStatus::ok(); |
| 142 | } |
| 143 | |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 144 | binder_status_t AudioControl::dump(int fd, const char** args, uint32_t numArgs) { |
| 145 | if (numArgs == 0) { |
| 146 | return dumpsys(fd); |
| 147 | } |
| 148 | |
| 149 | string option = string(args[0]); |
| 150 | if (EqualsIgnoreCase(option, "--help")) { |
| 151 | return cmdHelp(fd); |
| 152 | } else if (EqualsIgnoreCase(option, "--request")) { |
| 153 | return cmdRequestFocus(fd, args, numArgs); |
| 154 | } else if (EqualsIgnoreCase(option, "--abandon")) { |
| 155 | return cmdAbandonFocus(fd, args, numArgs); |
| 156 | } else { |
| 157 | dprintf(fd, "Invalid option: %s\n", option.c_str()); |
| 158 | return STATUS_BAD_VALUE; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | binder_status_t AudioControl::dumpsys(int fd) { |
| 163 | if (mFocusListener == nullptr) { |
| 164 | dprintf(fd, "No focus listener registered\n"); |
| 165 | } else { |
| 166 | dprintf(fd, "Focus listener registered\n"); |
| 167 | } |
| 168 | return STATUS_OK; |
| 169 | } |
| 170 | |
| 171 | binder_status_t AudioControl::cmdHelp(int fd) const { |
| 172 | dprintf(fd, "Usage: \n\n"); |
| 173 | dprintf(fd, "[no args]: dumps focus listener status\n"); |
| 174 | dprintf(fd, "--help: shows this help\n"); |
| 175 | dprintf(fd, |
| 176 | "--request <USAGE> <ZONE_ID> <FOCUS_GAIN>: requests audio focus for specified " |
| 177 | "usage (string), audio zone ID (int), and focus gain type (int)\n"); |
| 178 | dprintf(fd, |
| 179 | "--abandon <USAGE> <ZONE_ID>: abandons audio focus for specified usage (string) and " |
| 180 | "audio zone ID (int)\n"); |
| 181 | dprintf(fd, "See audio_policy_configuration.xsd for valid AudioUsage values.\n"); |
| 182 | return STATUS_OK; |
| 183 | } |
| 184 | |
| 185 | binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) { |
| 186 | if (!checkCallerHasWritePermissions(fd)) { |
| 187 | return STATUS_PERMISSION_DENIED; |
| 188 | } |
| 189 | if (numArgs != 4) { |
| 190 | dprintf(fd, |
| 191 | "Invalid number of arguments: please provide --request <USAGE> <ZONE_ID> " |
| 192 | "<FOCUS_GAIN>\n"); |
| 193 | return STATUS_BAD_VALUE; |
| 194 | } |
| 195 | |
| 196 | string usage = string(args[1]); |
| 197 | if (xsd::stringToAudioUsage(usage) == xsd::AudioUsage::UNKNOWN) { |
| 198 | dprintf(fd, |
| 199 | "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 " |
| 200 | "for supported values\n", |
| 201 | usage.c_str()); |
| 202 | return STATUS_BAD_VALUE; |
| 203 | } |
| 204 | |
| 205 | int zoneId; |
| 206 | if (!safelyParseInt(string(args[2]), &zoneId)) { |
| 207 | dprintf(fd, "Non-integer zoneId provided with request: %s\n", string(args[2]).c_str()); |
| 208 | return STATUS_BAD_VALUE; |
| 209 | } |
| 210 | |
| 211 | int focusGainValue; |
| 212 | if (!safelyParseInt(string(args[3]), &focusGainValue)) { |
| 213 | dprintf(fd, "Non-integer focusGain provided with request: %s\n", string(args[3]).c_str()); |
| 214 | return STATUS_BAD_VALUE; |
| 215 | } |
| 216 | AudioFocusChange focusGain = AudioFocusChange(focusGainValue); |
| 217 | |
| 218 | if (mFocusListener == nullptr) { |
| 219 | dprintf(fd, "Unable to request focus - no focus listener registered\n"); |
| 220 | return STATUS_BAD_VALUE; |
| 221 | } |
| 222 | |
| 223 | mFocusListener->requestAudioFocus(usage, zoneId, focusGain); |
| 224 | dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(), |
| 225 | zoneId, focusGain); |
| 226 | return STATUS_OK; |
| 227 | } |
| 228 | |
| 229 | binder_status_t AudioControl::cmdAbandonFocus(int fd, const char** args, uint32_t numArgs) { |
| 230 | if (!checkCallerHasWritePermissions(fd)) { |
| 231 | return STATUS_PERMISSION_DENIED; |
| 232 | } |
| 233 | if (numArgs != 3) { |
| 234 | dprintf(fd, "Invalid number of arguments: please provide --abandon <USAGE> <ZONE_ID>\n"); |
| 235 | return STATUS_BAD_VALUE; |
| 236 | } |
| 237 | |
| 238 | string usage = string(args[1]); |
| 239 | if (xsd::stringToAudioUsage(usage) == xsd::AudioUsage::UNKNOWN) { |
| 240 | dprintf(fd, |
| 241 | "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 " |
| 242 | "for supported values\n", |
| 243 | usage.c_str()); |
| 244 | return STATUS_BAD_VALUE; |
| 245 | } |
| 246 | |
| 247 | int zoneId; |
| 248 | if (!safelyParseInt(string(args[2]), &zoneId)) { |
| 249 | dprintf(fd, "Non-integer zoneId provided with abandon: %s\n", string(args[2]).c_str()); |
| 250 | return STATUS_BAD_VALUE; |
| 251 | } |
| 252 | |
| 253 | if (mFocusListener == nullptr) { |
| 254 | dprintf(fd, "Unable to abandon focus - no focus listener registered\n"); |
| 255 | return STATUS_BAD_VALUE; |
| 256 | } |
| 257 | |
| 258 | mFocusListener->abandonAudioFocus(usage, zoneId); |
| 259 | dprintf(fd, "Abandoned focus for usage %s and zoneId %d\n", usage.c_str(), zoneId); |
| 260 | return STATUS_OK; |
| 261 | } |
| 262 | |
| 263 | } // namespace aidl::android::hardware::automotive::audiocontrol |