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 <system/audio.h> 20 #include <system/audio_policy.h> 21 #include <utils/Errors.h> 22 #include <utils/RWLock.h> 23 #include <list> 24 #include <map> 25 #include <string> 26 #include <vector> 27 28 class CParameterMgrPlatformConnector; 29 class ISelectionCriterionInterface; 30 class ISelectionCriterionTypeInterface; 31 struct cnode; 32 33 class ParameterMgrPlatformConnectorLogger; 34 35 namespace android 36 { 37 namespace audio_policy 38 { 39 40 class ParameterManagerWrapper 41 { 42 private: 43 typedef std::pair<int, const char *> CriterionTypeValuePair; 44 45 typedef std::map<std::string, ISelectionCriterionInterface *> CriterionCollection; 46 typedef std::map<std::string, ISelectionCriterionTypeInterface *> CriterionTypeCollection; 47 typedef CriterionCollection::iterator CriterionMapIterator; 48 typedef CriterionCollection::const_iterator CriterionMapConstIterator; 49 typedef CriterionTypeCollection::iterator CriterionTypeMapIterator; 50 typedef CriterionTypeCollection::const_iterator CriteriaTypeMapConstIterator; 51 52 public: 53 ParameterManagerWrapper(); 54 ~ParameterManagerWrapper(); 55 56 /** 57 * Starts the platform state service. 58 * It starts the parameter framework policy instance. 59 * 60 * @return NO_ERROR if success, error code otherwise. 61 */ 62 status_t start(); 63 64 /** 65 * The following API wrap policy action to criteria 66 */ 67 68 /** 69 * Checks if the platform state was correctly started (ie the policy parameter manager 70 * has been instantiated and started correctly). 71 * 72 * @todo: map on initCheck? 73 * 74 * @return true if platform state is started correctly, false otherwise. 75 */ 76 bool isStarted(); 77 78 /** 79 * Set Telephony Mode. 80 * It will set the telephony mode criterion accordingly and apply the configuration in order 81 * to select the right configuration on domains depending on this mode criterion. 82 * 83 * @param[in] mode: Android Phone state (normal, ringtone, csv, in communication) 84 * 85 * @return NO_ERROR if criterion set correctly, error code otherwise. 86 */ 87 status_t setPhoneState(audio_mode_t mode); 88 89 audio_mode_t getPhoneState() const; 90 91 /** 92 * Set Force Use config for a given usage. 93 * It will set the corresponding policy parameter framework criterion. 94 * 95 * @param[in] usage for which a configuration shall be forced. 96 * @param[in] config wished to be forced for the given shall. 97 * 98 * @return NO_ERROR if the criterion was set correctly, error code otherwise (e.g. config not 99 * allowed a given usage...) 100 */ 101 status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); 102 103 audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const; 104 105 /** 106 * Set the connection state of device(s). 107 * It will set the associated policy parameter framework criterion. 108 * 109 * @param[in] devices mask of devices for which the state has changed. 110 * @param[in] state of availability of this(these) device(s). 111 * @param[in] deviceAddress: the mask might not be enough, as it may represents a type of 112 * device, so address of the device will help precise identification. 113 * 114 * @return NO_ERROR if devices criterion updated correctly, error code otherwise. 115 */ 116 status_t setDeviceConnectionState(audio_devices_t devices, audio_policy_dev_state_t state, 117 const char *deviceAddress); 118 119 private: 120 /** 121 * Apply the configuration of the platform on the policy parameter manager. 122 * Once all the criteria have been set, the client of the platform state must call 123 * this function in order to have the route PFW taking into account these criteria. 124 * 125 * OPENS: shall we expose this? 126 * - Yes if atomic set operation. 127 * In this case, abstract it behind the "STAGE AND COMMIT" pattern 128 * - no if need to set more than one before triggering an apply configuration. 129 */ 130 void applyPlatformConfiguration(); 131 132 /** 133 * Load the criterion configuration file. 134 * 135 * @param[in] path Criterion conf file path. 136 * 137 * @return NO_ERROR is parsing successful, error code otherwise. 138 */ 139 status_t loadAudioPolicyCriteriaConfig(const char *path); 140 141 /** 142 * Add a criterion type to AudioPolicyPfw. 143 * 144 * @param[in] typeName of the PFW criterion type. 145 * @param[in] isInclusive attribute of the criterion type. 146 */ 147 void addCriterionType(const std::string &typeName, bool isInclusive); 148 149 /** 150 * Add a criterion type value pair to AudioPolicyPfw. 151 * 152 * @param[in] typeName criterion type name to which this value pair is added to. 153 * @param[in] numeric part of the value pair. 154 * @param[in] literal part of the value pair. 155 */ 156 void addCriterionTypeValuePair(const std::string &typeName, uint32_t numeric, 157 const std::string &literal); 158 159 /** 160 * Add a criterion to AudioPolicyPfw. 161 * 162 * @param[in] name of the PFW criterion. 163 * @param[in] typeName criterion type name to which this criterion is associated to. 164 * @param[in] defaultLiteralValue of the PFW criterion. 165 */ 166 void addCriterion(const std::string &name, 167 const std::string &typeName, 168 const std::string &defaultLiteralValue); 169 /** 170 * Parse and load the inclusive criterion type from configuration file. 171 * 172 * @param[in] root node of the configuration file. 173 */ 174 void loadInclusiveCriterionType(cnode *root); 175 176 /** 177 * Parse and load the exclusive criterion type from configuration file. 178 * 179 * @param[in] root node of the configuration file. 180 */ 181 void loadExclusiveCriterionType(cnode *root); 182 183 /** 184 * Parse and load the criteria from configuration file. 185 * 186 * @param[in] root node of the configuration file. 187 */ 188 void loadCriteria(cnode *root); 189 190 /** 191 * Parse and load a criterion from configuration file. 192 * 193 * @param[in] root node of the configuration file. 194 */ 195 void loadCriterion(cnode *root); 196 197 /** 198 * Parse and load the criterion types from configuration file. 199 * 200 * @param[in] root node of the configuration file 201 * @param[in] isInclusive true if inclusive, false is exclusive. 202 */ 203 void loadCriterionType(cnode *root, bool isInclusive); 204 205 /** 206 * Load the configuration file. 207 * 208 * @param[in] root node of the configuration file. 209 */ 210 void loadConfig(cnode *root); 211 212 /** 213 * Parse and load the chidren node from a given root node. 214 * 215 * @param[in] root node of the configuration file 216 * @param[out] defaultValue of the parameter manager element to retrieve. 217 * @param[out] type of the parameter manager element to retrieve. 218 */ 219 void parseChildren(cnode *root, std::string &defaultValue, std::string &type); 220 221 /** 222 * Retrieve an element from a map by its name. 223 * 224 * @tparam T type of element to search. 225 * @param[in] name name of the element to find. 226 * @param[in] elementsMap maps of elements to search into. 227 * 228 * @return valid pointer on element if found, NULL otherwise. 229 */ 230 template <typename T> 231 T *getElement(const std::string &name, std::map<std::string, T *> &elementsMap); 232 233 /** 234 * Retrieve an element from a map by its name. Const version. 235 * 236 * @tparam T type of element to search. 237 * @param[in] name name of the element to find. 238 * @param[in] elementsMap maps of elements to search into. 239 * 240 * @return valid pointer on element if found, NULL otherwise. 241 */ 242 template <typename T> 243 const T *getElement(const std::string &name, 244 const std::map<std::string, T *> &elementsMap) const; 245 246 /** 247 * set the value of a component state. 248 * 249 * @param[in] value new value to set to the component state. 250 * @param[in] stateName of the component state. 251 */ 252 void setValue(int value, const std::string &stateName); 253 254 /** 255 * get the value of a component state. 256 * 257 * @param[in] name of the component state. 258 * 259 * @return value of the component state 260 */ 261 int getValue(const std::string &stateName) const; 262 263 bool isValueValidForCriterion(ISelectionCriterionInterface *criterion, int valueToCheck); 264 265 CriterionTypeCollection mPolicyCriterionTypes; /**< Policy Criterion Type map. */ 266 CriterionCollection mPolicyCriteria; /**< Policy Criterion Map. */ 267 268 CParameterMgrPlatformConnector *mPfwConnector; /**< Policy Parameter Manager connector. */ 269 ParameterMgrPlatformConnectorLogger *mPfwConnectorLogger; /**< Policy PFW logger. */ 270 271 272 /** 273 * provide a compile time error if no specialization is provided for a given type. 274 * 275 * @tparam T: type of the parameter manager element. Supported one are: 276 * - Criterion 277 * - CriterionType. 278 */ 279 template <typename T> 280 struct parameterManagerElementSupported; 281 282 static const char *const mPolicyPfwDefaultConfFileName; /**< Default Policy PFW top file name.*/ 283 }; 284 285 } // namespace audio_policy 286 } // namespace android 287