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 <AudioGain.h>
20 #include <AudioPort.h>
21 #include <HwModule.h>
22 #include <DeviceDescriptor.h>
23 #include <system/audio.h>
24 #include <system/audio_policy.h>
25 #include <utils/Errors.h>
26 #include <utils/RWLock.h>
27 #include <utils/RefBase.h>
28 #include <list>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 class CParameterMgrPlatformConnector;
34 class ISelectionCriterionInterface;
35 struct cnode;
36 
37 class ParameterMgrPlatformConnectorLogger;
38 
39 namespace android {
40 namespace audio_policy {
41 
42 using ValuePair = std::pair<uint32_t, std::string>;
43 using ValuePairs = std::vector<ValuePair>;
44 
45 class ParameterManagerWrapper
46 {
47 private:
48     using Criteria = std::map<std::string, ISelectionCriterionInterface *>;
49 
50 public:
51     ParameterManagerWrapper();
52     ~ParameterManagerWrapper();
53 
54     /**
55      * Starts the platform state service.
56      * It starts the parameter framework policy instance.
57      *
58      * @return NO_ERROR if success, error code otherwise.
59      */
60     status_t start();
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(audio_devices_t inputDevices);
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(audio_devices_t outputDevices);
120 
121     status_t setDeviceConnectionState(const sp<DeviceDescriptor> devDesc,
122                                       audio_policy_dev_state_t state);
123 
124     /**
125      * @brief addCriterion to the policy pfw
126      * @param name of the criterion
127      * @param isInclusive if true, inclusive, if false exclusive criterion type
128      * @param pairs of numerical/literal values of the criterion
129      * @param defaultValue provided as literal.
130      * @return
131      */
132     status_t addCriterion(const std::string &name, bool isInclusive, ValuePairs pairs,
133                           const std::string &defaultValue);
134 
135 private:
136     /**
137      * Apply the configuration of the platform on the policy parameter manager.
138      * Once all the criteria have been set, the client of the platform state must call
139      * this function in order to have the route PFW taking into account these criteria.
140      *
141      * OPENS: shall we expose this?
142      *      - Yes if atomic set operation.
143      *          In this case, abstract it behind the "STAGE AND COMMIT" pattern
144      *      - no if need to set more than one before triggering an apply configuration.
145      */
146     void applyPlatformConfiguration();
147 
148      /**
149      * Retrieve an element from a map by its name.
150      *
151      * @tparam T type of element to search.
152      * @param[in] name name of the element to find.
153      * @param[in] elementsMap maps of elements to search into.
154      *
155      * @return valid pointer on element if found, NULL otherwise.
156      */
157     template <typename T>
158     T *getElement(const std::string &name, std::map<std::string, T *> &elementsMap);
159 
160     /**
161      * Retrieve an element from a map by its name. Const version.
162      *
163      * @tparam T type of element to search.
164      * @param[in] name name of the element to find.
165      * @param[in] elementsMap maps of elements to search into.
166      *
167      * @return valid pointer on element if found, NULL otherwise.
168      */
169     template <typename T>
170     const T *getElement(const std::string &name,
171                         const std::map<std::string, T *> &elementsMap) const;
172 
173     /**
174      * set the value of a component state.
175      *
176      * @param[in] value new value to set to the component state.
177      * @param[in] stateName of the component state.
178      */
179     void setValue(int value, const std::string &stateName);
180 
181     /**
182      * get the value of a component state.
183      *
184      * @param[in] name of the component state.
185      *
186      * @return value of the component state
187      */
188     int getValue(const std::string &stateName) const;
189 
190     bool isValueValidForCriterion(ISelectionCriterionInterface *criterion, int valueToCheck);
191 
192     Criteria mPolicyCriteria; /**< Policy Criterion Map. */
193 
194     CParameterMgrPlatformConnector *mPfwConnector; /**< Policy Parameter Manager connector. */
195     ParameterMgrPlatformConnectorLogger *mPfwConnectorLogger; /**< Policy PFW logger. */
196 
197 
198     /**
199      * provide a compile time error if no specialization is provided for a given type.
200      *
201      * @tparam T: type of the parameter manager element. Supported one are:
202      *                      - Criterion
203      *                      - CriterionType.
204      */
205     template <typename T>
206     struct parameterManagerElementSupported;
207 
208     static const char *const mPolicyPfwDefaultConfFileName; /**< Default Policy PFW top file name.*/
209     static const char *const mPolicyPfwVendorConfFileName; /**< Vendor Policy PFW top file name.*/
210 };
211 
212 } // namespace audio_policy
213 } // namespace android
214