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