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