François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "APM::AudioPolicyEngine/PFWWrapper" |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 19 | |
| 20 | #include "ParameterManagerWrapper.h" |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 21 | #include <ParameterMgrPlatformConnector.h> |
| 22 | #include <SelectionCriterionTypeInterface.h> |
| 23 | #include <SelectionCriterionInterface.h> |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 24 | #include <media/convert.h> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 25 | #include <algorithm> |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 26 | #include <cutils/bitops.h> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 27 | #include <cutils/config_utils.h> |
| 28 | #include <cutils/misc.h> |
| 29 | #include <fstream> |
| 30 | #include <limits> |
| 31 | #include <sstream> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | #include <stdint.h> |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 35 | #include <cinttypes> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 36 | #include <cmath> |
| 37 | #include <utils/Log.h> |
| 38 | |
| 39 | using std::string; |
| 40 | using std::map; |
| 41 | using std::vector; |
| 42 | |
| 43 | /// PFW related definitions |
| 44 | // Logger |
| 45 | class ParameterMgrPlatformConnectorLogger : public CParameterMgrPlatformConnector::ILogger |
| 46 | { |
| 47 | public: |
| 48 | ParameterMgrPlatformConnectorLogger() {} |
| 49 | |
François Gaffie | c1391f9 | 2015-12-10 09:43:48 +0100 | [diff] [blame] | 50 | virtual void info(const string &log) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 51 | { |
Liu Changcheng | 30cf41f | 2016-12-07 18:58:19 +0800 | [diff] [blame] | 52 | ALOGV("policy-parameter-manager: %s", log.c_str()); |
François Gaffie | c1391f9 | 2015-12-10 09:43:48 +0100 | [diff] [blame] | 53 | } |
| 54 | virtual void warning(const string &log) |
| 55 | { |
| 56 | ALOGW("policy-parameter-manager: %s", log.c_str()); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
François Gaffie | f19cf79 | 2018-05-30 17:22:17 +0200 | [diff] [blame] | 60 | namespace android { |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 61 | |
| 62 | using utilities::convertTo; |
| 63 | |
François Gaffie | f19cf79 | 2018-05-30 17:22:17 +0200 | [diff] [blame] | 64 | namespace audio_policy { |
| 65 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 66 | const char *const ParameterManagerWrapper::mPolicyPfwDefaultConfFileName = |
| 67 | "/etc/parameter-framework/ParameterFrameworkConfigurationPolicy.xml"; |
Mingwei Shi | c41dc11 | 2017-07-26 10:02:40 +0800 | [diff] [blame] | 68 | const char *const ParameterManagerWrapper::mPolicyPfwVendorConfFileName = |
| 69 | "/vendor/etc/parameter-framework/ParameterFrameworkConfigurationPolicy.xml"; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 70 | |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 71 | static const char *const gInputDeviceCriterionName = "AvailableInputDevices"; |
| 72 | static const char *const gOutputDeviceCriterionName = "AvailableOutputDevices"; |
| 73 | static const char *const gPhoneStateCriterionName = "TelephonyMode"; |
| 74 | static const char *const gOutputDeviceAddressCriterionName = "AvailableOutputDevicesAddresses"; |
| 75 | static const char *const gInputDeviceAddressCriterionName = "AvailableInputDevicesAddresses"; |
| 76 | |
| 77 | /** |
| 78 | * Order MUST be align with defintiion of audio_policy_force_use_t within audio_policy.h |
| 79 | */ |
| 80 | static const char *const gForceUseCriterionTag[AUDIO_POLICY_FORCE_USE_CNT] = |
| 81 | { |
| 82 | [AUDIO_POLICY_FORCE_FOR_COMMUNICATION] = "ForceUseForCommunication", |
| 83 | [AUDIO_POLICY_FORCE_FOR_MEDIA] = "ForceUseForMedia", |
| 84 | [AUDIO_POLICY_FORCE_FOR_RECORD] = "ForceUseForRecord", |
| 85 | [AUDIO_POLICY_FORCE_FOR_DOCK] = "ForceUseForDock", |
| 86 | [AUDIO_POLICY_FORCE_FOR_SYSTEM] = "ForceUseForSystem", |
| 87 | [AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] = "ForceUseForHdmiSystemAudio", |
| 88 | [AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND] = "ForceUseForEncodedSurround", |
| 89 | [AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING] = "ForceUseForVibrateRinging" |
| 90 | }; |
| 91 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 92 | template <> |
| 93 | struct ParameterManagerWrapper::parameterManagerElementSupported<ISelectionCriterionInterface> {}; |
| 94 | template <> |
| 95 | struct ParameterManagerWrapper::parameterManagerElementSupported<ISelectionCriterionTypeInterface> {}; |
| 96 | |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 97 | ParameterManagerWrapper::ParameterManagerWrapper(bool enableSchemaVerification, |
| 98 | const std::string &schemaUri) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 99 | : mPfwConnectorLogger(new ParameterMgrPlatformConnectorLogger) |
| 100 | { |
| 101 | // Connector |
Mingwei Shi | c41dc11 | 2017-07-26 10:02:40 +0800 | [diff] [blame] | 102 | if (access(mPolicyPfwVendorConfFileName, R_OK) == 0) { |
| 103 | mPfwConnector = new CParameterMgrPlatformConnector(mPolicyPfwVendorConfFileName); |
| 104 | } else { |
| 105 | mPfwConnector = new CParameterMgrPlatformConnector(mPolicyPfwDefaultConfFileName); |
| 106 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 107 | |
| 108 | // Logger |
| 109 | mPfwConnector->setLogger(mPfwConnectorLogger); |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 110 | |
| 111 | // Schema validation |
| 112 | std::string error; |
| 113 | bool ret = mPfwConnector->setValidateSchemasOnStart(enableSchemaVerification, error); |
| 114 | ALOGE_IF(!ret, "Failed to activate schema validation: %s", error.c_str()); |
| 115 | if (enableSchemaVerification && ret && !schemaUri.empty()) { |
| 116 | ALOGE("Schema verification activated with schema URI: %s", schemaUri.c_str()); |
| 117 | mPfwConnector->setSchemaUri(schemaUri); |
| 118 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 119 | } |
| 120 | |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 121 | status_t ParameterManagerWrapper::addCriterion(const std::string &name, bool isInclusive, |
| 122 | ValuePairs pairs, const std::string &defaultValue) |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 123 | { |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 124 | ALOG_ASSERT(not isStarted(), "Cannot add a criterion if PFW is already started"); |
| 125 | auto criterionType = mPfwConnector->createSelectionCriterionType(isInclusive); |
| 126 | |
| 127 | for (auto pair : pairs) { |
| 128 | std::string error; |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 129 | ALOGV("%s: Adding pair %" PRIu64", %s for criterionType %s", __func__, std::get<0>(pair), |
| 130 | std::get<2>(pair).c_str(), name.c_str()); |
| 131 | criterionType->addValuePair(std::get<0>(pair), std::get<2>(pair), error); |
| 132 | |
| 133 | if (name == gOutputDeviceCriterionName) { |
| 134 | ALOGV("%s: Adding mOutputDeviceToCriterionTypeMap %d %" PRIu64" for criterionType %s", |
| 135 | __func__, std::get<1>(pair), std::get<0>(pair), name.c_str()); |
| 136 | audio_devices_t androidType = static_cast<audio_devices_t>(std::get<1>(pair)); |
| 137 | mOutputDeviceToCriterionTypeMap[androidType] = std::get<0>(pair); |
| 138 | } |
| 139 | if (name == gInputDeviceCriterionName) { |
| 140 | ALOGV("%s: Adding mInputDeviceToCriterionTypeMap %d %" PRIu64" for criterionType %s", |
| 141 | __func__, std::get<1>(pair), std::get<0>(pair), name.c_str()); |
| 142 | audio_devices_t androidType = static_cast<audio_devices_t>(std::get<1>(pair)); |
| 143 | mInputDeviceToCriterionTypeMap[androidType] = std::get<0>(pair); |
| 144 | } |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 145 | } |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 146 | ALOG_ASSERT(mPolicyCriteria.find(name) == mPolicyCriteria.end(), |
| 147 | "%s: Criterion %s already added", __FUNCTION__, name.c_str()); |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 148 | |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 149 | auto criterion = mPfwConnector->createSelectionCriterion(name, criterionType); |
| 150 | mPolicyCriteria[name] = criterion; |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 151 | |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 152 | if (not defaultValue.empty()) { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 153 | uint64_t numericalValue = 0; |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 154 | if (not criterionType->getNumericalValue(defaultValue.c_str(), numericalValue)) { |
| 155 | ALOGE("%s; trying to apply invalid default literal value (%s)", __FUNCTION__, |
| 156 | defaultValue.c_str()); |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 157 | } |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 158 | criterion->setCriterionState(numericalValue); |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 159 | } |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 160 | return NO_ERROR; |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 161 | } |
| 162 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 163 | ParameterManagerWrapper::~ParameterManagerWrapper() |
| 164 | { |
| 165 | // Unset logger |
| 166 | mPfwConnector->setLogger(NULL); |
| 167 | // Remove logger |
| 168 | delete mPfwConnectorLogger; |
| 169 | // Remove connector |
| 170 | delete mPfwConnector; |
| 171 | } |
| 172 | |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 173 | status_t ParameterManagerWrapper::start(std::string &error) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 174 | { |
| 175 | ALOGD("%s: in", __FUNCTION__); |
| 176 | /// Start PFW |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 177 | if (!mPfwConnector->start(error)) { |
| 178 | ALOGE("%s: Policy PFW start error: %s", __FUNCTION__, error.c_str()); |
| 179 | return NO_INIT; |
| 180 | } |
| 181 | ALOGD("%s: Policy PFW successfully started!", __FUNCTION__); |
| 182 | return NO_ERROR; |
| 183 | } |
| 184 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 185 | template <typename T> |
| 186 | T *ParameterManagerWrapper::getElement(const string &name, std::map<string, T *> &elementsMap) |
| 187 | { |
| 188 | parameterManagerElementSupported<T>(); |
| 189 | typename std::map<string, T *>::iterator it = elementsMap.find(name); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 190 | ALOG_ASSERT(it != elementsMap.end(), "Element %s not found", name.c_str()); |
| 191 | return it != elementsMap.end() ? it->second : NULL; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | template <typename T> |
| 195 | const T *ParameterManagerWrapper::getElement(const string &name, const std::map<string, T *> &elementsMap) const |
| 196 | { |
| 197 | parameterManagerElementSupported<T>(); |
| 198 | typename std::map<string, T *>::const_iterator it = elementsMap.find(name); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 199 | ALOG_ASSERT(it != elementsMap.end(), "Element %s not found", name.c_str()); |
| 200 | return it != elementsMap.end() ? it->second : NULL; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 201 | } |
| 202 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 203 | bool ParameterManagerWrapper::isStarted() |
| 204 | { |
| 205 | return mPfwConnector && mPfwConnector->isStarted(); |
| 206 | } |
| 207 | |
| 208 | status_t ParameterManagerWrapper::setPhoneState(audio_mode_t mode) |
| 209 | { |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 210 | ISelectionCriterionInterface *criterion = |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 211 | getElement<ISelectionCriterionInterface>(gPhoneStateCriterionName, mPolicyCriteria); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 212 | if (criterion == NULL) { |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 213 | ALOGE("%s: no criterion found for %s", __FUNCTION__, gPhoneStateCriterionName); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 214 | return BAD_VALUE; |
| 215 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 216 | if (!isValueValidForCriterion(criterion, static_cast<int>(mode))) { |
| 217 | return BAD_VALUE; |
| 218 | } |
| 219 | criterion->setCriterionState((int)(mode)); |
| 220 | applyPlatformConfiguration(); |
| 221 | return NO_ERROR; |
| 222 | } |
| 223 | |
| 224 | audio_mode_t ParameterManagerWrapper::getPhoneState() const |
| 225 | { |
| 226 | const ISelectionCriterionInterface *criterion = |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 227 | getElement<ISelectionCriterionInterface>(gPhoneStateCriterionName, mPolicyCriteria); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 228 | if (criterion == NULL) { |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 229 | ALOGE("%s: no criterion found for %s", __FUNCTION__, gPhoneStateCriterionName); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 230 | return AUDIO_MODE_NORMAL; |
| 231 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 232 | return static_cast<audio_mode_t>(criterion->getCriterionState()); |
| 233 | } |
| 234 | |
| 235 | status_t ParameterManagerWrapper::setForceUse(audio_policy_force_use_t usage, |
| 236 | audio_policy_forced_cfg_t config) |
| 237 | { |
| 238 | // @todo: return an error on a unsupported value |
| 239 | if (usage > AUDIO_POLICY_FORCE_USE_CNT) { |
| 240 | return BAD_VALUE; |
| 241 | } |
| 242 | |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 243 | ISelectionCriterionInterface *criterion = |
| 244 | getElement<ISelectionCriterionInterface>(gForceUseCriterionTag[usage], mPolicyCriteria); |
| 245 | if (criterion == NULL) { |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 246 | ALOGE("%s: no criterion found for %s", __FUNCTION__, gForceUseCriterionTag[usage]); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 247 | return BAD_VALUE; |
| 248 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 249 | if (!isValueValidForCriterion(criterion, static_cast<int>(config))) { |
| 250 | return BAD_VALUE; |
| 251 | } |
| 252 | criterion->setCriterionState((int)config); |
| 253 | applyPlatformConfiguration(); |
| 254 | return NO_ERROR; |
| 255 | } |
| 256 | |
| 257 | audio_policy_forced_cfg_t ParameterManagerWrapper::getForceUse(audio_policy_force_use_t usage) const |
| 258 | { |
| 259 | // @todo: return an error on a unsupported value |
| 260 | if (usage > AUDIO_POLICY_FORCE_USE_CNT) { |
| 261 | return AUDIO_POLICY_FORCE_NONE; |
| 262 | } |
| 263 | const ISelectionCriterionInterface *criterion = |
| 264 | getElement<ISelectionCriterionInterface>(gForceUseCriterionTag[usage], mPolicyCriteria); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 265 | if (criterion == NULL) { |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 266 | ALOGE("%s: no criterion found for %s", __FUNCTION__, gForceUseCriterionTag[usage]); |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 267 | return AUDIO_POLICY_FORCE_NONE; |
| 268 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 269 | return static_cast<audio_policy_forced_cfg_t>(criterion->getCriterionState()); |
| 270 | } |
| 271 | |
| 272 | bool ParameterManagerWrapper::isValueValidForCriterion(ISelectionCriterionInterface *criterion, |
| 273 | int valueToCheck) |
| 274 | { |
| 275 | const ISelectionCriterionTypeInterface *interface = criterion->getCriterionType(); |
| 276 | string literalValue; |
| 277 | return interface->getLiteralValue(valueToCheck, literalValue); |
| 278 | } |
| 279 | |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 280 | status_t ParameterManagerWrapper::setDeviceConnectionState( |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 281 | audio_devices_t type, const std::string &address, audio_policy_dev_state_t state) |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 282 | { |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 283 | std::string criterionName = audio_is_output_device(type) ? |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 284 | gOutputDeviceAddressCriterionName : gInputDeviceAddressCriterionName; |
| 285 | |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 286 | ALOGV("%s: device with address %s %s", __FUNCTION__, address.c_str(), |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 287 | state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE? "disconnected" : "connected"); |
| 288 | ISelectionCriterionInterface *criterion = |
| 289 | getElement<ISelectionCriterionInterface>(criterionName, mPolicyCriteria); |
| 290 | |
| 291 | if (criterion == NULL) { |
| 292 | ALOGE("%s: no criterion found for %s", __FUNCTION__, criterionName.c_str()); |
| 293 | return DEAD_OBJECT; |
| 294 | } |
| 295 | |
| 296 | auto criterionType = criterion->getCriterionType(); |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 297 | uint64_t deviceAddressId; |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 298 | if (not criterionType->getNumericalValue(address.c_str(), deviceAddressId)) { |
François Gaffie | 376740f | 2020-01-16 09:08:04 +0100 | [diff] [blame] | 299 | ALOGW("%s: unknown device address reported (%s) for criterion %s", __FUNCTION__, |
| 300 | address.c_str(), criterionName.c_str()); |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 301 | return BAD_TYPE; |
| 302 | } |
| 303 | int currentValueMask = criterion->getCriterionState(); |
| 304 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 305 | currentValueMask |= deviceAddressId; |
| 306 | } |
| 307 | else { |
| 308 | currentValueMask &= ~deviceAddressId; |
| 309 | } |
| 310 | criterion->setCriterionState(currentValueMask); |
| 311 | return NO_ERROR; |
| 312 | } |
| 313 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 314 | status_t ParameterManagerWrapper::setAvailableInputDevices(const DeviceTypeSet &types) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 315 | { |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 316 | ISelectionCriterionInterface *criterion = |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 317 | getElement<ISelectionCriterionInterface>(gInputDeviceCriterionName, mPolicyCriteria); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 318 | if (criterion == NULL) { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 319 | ALOGE("%s: no criterion found for %s", __func__, gInputDeviceCriterionName); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 320 | return DEAD_OBJECT; |
| 321 | } |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 322 | criterion->setCriterionState(convertDeviceTypesToCriterionValue(types)); |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 323 | applyPlatformConfiguration(); |
| 324 | return NO_ERROR; |
| 325 | } |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 326 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 327 | status_t ParameterManagerWrapper::setAvailableOutputDevices(const DeviceTypeSet &types) |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 328 | { |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 329 | ISelectionCriterionInterface *criterion = |
François Gaffie | a56b5c2 | 2018-02-21 18:04:39 +0100 | [diff] [blame] | 330 | getElement<ISelectionCriterionInterface>(gOutputDeviceCriterionName, mPolicyCriteria); |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 331 | if (criterion == NULL) { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 332 | ALOGE("%s: no criterion found for %s", __func__, gOutputDeviceCriterionName); |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 333 | return DEAD_OBJECT; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 334 | } |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 335 | criterion->setCriterionState(convertDeviceTypesToCriterionValue(types)); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 336 | applyPlatformConfiguration(); |
| 337 | return NO_ERROR; |
| 338 | } |
| 339 | |
| 340 | void ParameterManagerWrapper::applyPlatformConfiguration() |
| 341 | { |
| 342 | mPfwConnector->applyConfigurations(); |
| 343 | } |
| 344 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 345 | uint64_t ParameterManagerWrapper::convertDeviceTypeToCriterionValue(audio_devices_t type) const { |
| 346 | bool isOut = audio_is_output_devices(type); |
| 347 | uint32_t typeMask = isOut ? type : (type & ~AUDIO_DEVICE_BIT_IN); |
| 348 | |
| 349 | const auto &adapters = isOut ? mOutputDeviceToCriterionTypeMap : mInputDeviceToCriterionTypeMap; |
| 350 | // Only multibit devices need adaptation. |
| 351 | if (popcount(typeMask) > 1) { |
| 352 | const auto &adapter = adapters.find(type); |
| 353 | if (adapter != adapters.end()) { |
| 354 | ALOGV("%s: multibit device %d converted to criterion %" PRIu64, __func__, type, |
| 355 | adapter->second); |
| 356 | return adapter->second; |
| 357 | } |
| 358 | ALOGE("%s: failed to find map for multibit device %d", __func__, type); |
| 359 | return 0; |
| 360 | } |
| 361 | return typeMask; |
| 362 | } |
| 363 | |
| 364 | uint64_t ParameterManagerWrapper::convertDeviceTypesToCriterionValue( |
| 365 | const DeviceTypeSet &types) const { |
| 366 | uint64_t criterionValue = 0; |
| 367 | for (const auto &type : types) { |
| 368 | criterionValue += convertDeviceTypeToCriterionValue(type); |
| 369 | } |
| 370 | return criterionValue; |
| 371 | } |
| 372 | |
| 373 | DeviceTypeSet ParameterManagerWrapper::convertDeviceCriterionValueToDeviceTypes( |
| 374 | uint64_t criterionValue, bool isOut) const { |
| 375 | DeviceTypeSet deviceTypes; |
| 376 | const auto &adapters = isOut ? mOutputDeviceToCriterionTypeMap : mInputDeviceToCriterionTypeMap; |
| 377 | for (const auto &adapter : adapters) { |
| 378 | if ((adapter.second & criterionValue) == adapter.second) { |
| 379 | deviceTypes.insert(adapter.first); |
| 380 | } |
| 381 | } |
| 382 | return deviceTypes; |
| 383 | } |
| 384 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 385 | } // namespace audio_policy |
| 386 | } // namespace android |