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