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 #pragma once 18 19 #include <media/AudioContainers.h> 20 #include <system/audio.h> 21 #include <system/audio_policy.h> 22 #include <utils/Errors.h> 23 #include <utils/RWLock.h> 24 #include <utils/RefBase.h> 25 #include <list> 26 #include <map> 27 #include <string> 28 #include <vector> 29 30 class CParameterMgrPlatformConnector; 31 class ISelectionCriterionInterface; 32 struct cnode; 33 34 class ParameterMgrPlatformConnectorLogger; 35 36 namespace android { 37 namespace audio_policy { 38 39 using ValuePair = std::tuple<uint64_t, uint32_t, std::string>; 40 using DeviceToCriterionTypeAdapter = std::map<audio_devices_t, uint64_t>; 41 using ValuePairs = std::vector<ValuePair>; 42 43 class ParameterManagerWrapper 44 { 45 private: 46 using Criteria = std::map<std::string, ISelectionCriterionInterface *>; 47 48 public: 49 ParameterManagerWrapper(bool enableSchemaVerification = false, 50 const std::string &schemaUri = {}); 51 ~ParameterManagerWrapper(); 52 53 /** 54 * Starts the platform state service. 55 * It starts the parameter framework policy instance. 56 * @param[out] contains human readable error if starts failed 57 * 58 * @return NO_ERROR if success, error code otherwise, and error is set to human readable string. 59 */ 60 status_t start(std::string &error); 61 62 /** 63 * The following API wrap policy action to criteria 64 */ 65 66 /** 67 * Checks if the platform state was correctly started (ie the policy parameter manager 68 * has been instantiated and started correctly). 69 * 70 * @todo: map on initCheck? 71 * 72 * @return true if platform state is started correctly, false otherwise. 73 */ 74 bool isStarted(); 75 76 /** 77 * Set Telephony Mode. 78 * It will set the telephony mode criterion accordingly and apply the configuration in order 79 * to select the right configuration on domains depending on this mode criterion. 80 * 81 * @param[in] mode: Android Phone state (normal, ringtone, csv, in communication) 82 * 83 * @return NO_ERROR if criterion set correctly, error code otherwise. 84 */ 85 status_t setPhoneState(audio_mode_t mode); 86 87 audio_mode_t getPhoneState() const; 88 89 /** 90 * Set Force Use config for a given usage. 91 * It will set the corresponding policy parameter framework criterion. 92 * 93 * @param[in] usage for which a configuration shall be forced. 94 * @param[in] config wished to be forced for the given shall. 95 * 96 * @return NO_ERROR if the criterion was set correctly, error code otherwise (e.g. config not 97 * allowed a given usage...) 98 */ 99 status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); 100 101 audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const; 102 103 /** 104 * Set the available input devices i.e. set the associated policy parameter framework criterion 105 * 106 * @param[in] inputDevices mask of available input devices. 107 * 108 * @return NO_ERROR if devices criterion updated correctly, error code otherwise. 109 */ 110 status_t setAvailableInputDevices(const DeviceTypeSet &inputDeviceTypes); 111 112 /** 113 * Set the available output devices i.e. set the associated policy parameter framework criterion 114 * 115 * @param[in] outputDevices mask of available output devices. 116 * 117 * @return NO_ERROR if devices criterion updated correctly, error code otherwise. 118 */ 119 status_t setAvailableOutputDevices(const DeviceTypeSet &outputDeviceTypes); 120 121 /** 122 * @brief setDeviceConnectionState propagates a state event on a given device(s) 123 * @param type bit mask of the device whose state has changed 124 * @param address of the device whose state has changed 125 * @param state new state of the given device 126 * @return NO_ERROR if new state corretly propagated to Engine Parameter-Framework, error 127 * code otherwise. 128 */ 129 status_t setDeviceConnectionState(audio_devices_t type, const std::string &address, 130 audio_policy_dev_state_t state); 131 132 /** 133 * @brief addCriterion to the policy pfw 134 * @param name of the criterion 135 * @param isInclusive if true, inclusive, if false exclusive criterion type 136 * @param pairs of numerical/literal values of the criterion 137 * @param defaultValue provided as literal. 138 * @return 139 */ 140 status_t addCriterion(const std::string &name, bool isInclusive, ValuePairs pairs, 141 const std::string &defaultValue); 142 143 uint64_t convertDeviceTypeToCriterionValue(audio_devices_t type) const; 144 145 uint64_t convertDeviceTypesToCriterionValue(const DeviceTypeSet &types) const; 146 147 DeviceTypeSet convertDeviceCriterionValueToDeviceTypes( 148 uint64_t criterionValue, bool isOut) const; 149 150 private: 151 /** 152 * Apply the configuration of the platform on the policy parameter manager. 153 * Once all the criteria have been set, the client of the platform state must call 154 * this function in order to have the route PFW taking into account these criteria. 155 * 156 * OPENS: shall we expose this? 157 * - Yes if atomic set operation. 158 * In this case, abstract it behind the "STAGE AND COMMIT" pattern 159 * - no if need to set more than one before triggering an apply configuration. 160 */ 161 void applyPlatformConfiguration(); 162 163 /** 164 * Retrieve an element from a map by its name. 165 * 166 * @tparam T type of element to search. 167 * @param[in] name name of the element to find. 168 * @param[in] elementsMap maps of elements to search into. 169 * 170 * @return valid pointer on element if found, NULL otherwise. 171 */ 172 template <typename T> 173 T *getElement(const std::string &name, std::map<std::string, T *> &elementsMap); 174 175 /** 176 * Retrieve an element from a map by its name. Const version. 177 * 178 * @tparam T type of element to search. 179 * @param[in] name name of the element to find. 180 * @param[in] elementsMap maps of elements to search into. 181 * 182 * @return valid pointer on element if found, NULL otherwise. 183 */ 184 template <typename T> 185 const T *getElement(const std::string &name, 186 const std::map<std::string, T *> &elementsMap) const; 187 188 /** 189 * set the value of a component state. 190 * 191 * @param[in] value new value to set to the component state. 192 * @param[in] stateName of the component state. 193 */ 194 void setValue(int value, const std::string &stateName); 195 196 /** 197 * get the value of a component state. 198 * 199 * @param[in] name of the component state. 200 * 201 * @return value of the component state 202 */ 203 int getValue(const std::string &stateName) const; 204 205 bool isValueValidForCriterion(ISelectionCriterionInterface *criterion, int valueToCheck); 206 207 Criteria mPolicyCriteria; /**< Policy Criterion Map. */ 208 209 CParameterMgrPlatformConnector *mPfwConnector; /**< Policy Parameter Manager connector. */ 210 ParameterMgrPlatformConnectorLogger *mPfwConnectorLogger; /**< Policy PFW logger. */ 211 212 213 /** 214 * provide a compile time error if no specialization is provided for a given type. 215 * 216 * @tparam T: type of the parameter manager element. Supported one are: 217 * - Criterion 218 * - CriterionType. 219 */ 220 template <typename T> 221 struct parameterManagerElementSupported; 222 223 DeviceToCriterionTypeAdapter mOutputDeviceToCriterionTypeMap; 224 DeviceToCriterionTypeAdapter mInputDeviceToCriterionTypeMap; 225 226 static const char *const mPolicyPfwDefaultConfFileName; /**< Default Policy PFW top file name.*/ 227 static const char *const mPolicyPfwVendorConfFileName; /**< Vendor Policy PFW top file name.*/ 228 }; 229 230 } // namespace audio_policy 231 } // namespace android 232