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
28class CParameterMgrPlatformConnector;
29class ISelectionCriterionInterface;
30class ISelectionCriterionTypeInterface;
31struct cnode;
32
33class ParameterMgrPlatformConnectorLogger;
34
35namespace android
36{
37namespace audio_policy
38{
39
40class ParameterManagerWrapper
41{
42private:
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
52public:
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 available input devices i.e. set the associated policy parameter framework criterion
107     *
108     * @param[in] inputDevices mask of available input devices.
109     *
110     * @return NO_ERROR if devices criterion updated correctly, error code otherwise.
111     */
112    status_t setAvailableInputDevices(audio_devices_t inputDevices);
113
114    /**
115     * Set the available output devices i.e. set the associated policy parameter framework criterion
116     *
117     * @param[in] outputDevices mask of available output devices.
118     *
119     * @return NO_ERROR if devices criterion updated correctly, error code otherwise.
120     */
121    status_t setAvailableOutputDevices(audio_devices_t outputDevices);
122
123private:
124    /**
125     * Apply the configuration of the platform on the policy parameter manager.
126     * Once all the criteria have been set, the client of the platform state must call
127     * this function in order to have the route PFW taking into account these criteria.
128     *
129     * OPENS: shall we expose this?
130     *      - Yes if atomic set operation.
131     *          In this case, abstract it behind the "STAGE AND COMMIT" pattern
132     *      - no if need to set more than one before triggering an apply configuration.
133     */
134    void applyPlatformConfiguration();
135
136    /**
137     * Load the criterion configuration file.
138     *
139     * @param[in] path Criterion conf file path.
140     *
141     * @return NO_ERROR is parsing successful, error code otherwise.
142     */
143    status_t loadAudioPolicyCriteriaConfig(const char *path);
144
145    /**
146     * Add a criterion type to AudioPolicyPfw.
147     *
148     * @param[in] typeName of the PFW criterion type.
149     * @param[in] isInclusive attribute of the criterion type.
150     */
151    void addCriterionType(const std::string &typeName, bool isInclusive);
152
153    /**
154     * Add a criterion type value pair to AudioPolicyPfw.
155     *
156     * @param[in] typeName criterion type name to which this value pair is added to.
157     * @param[in] numeric part of the value pair.
158     * @param[in] literal part of the value pair.
159     */
160    void addCriterionTypeValuePair(const std::string &typeName, uint32_t numeric,
161                                   const std::string &literal);
162
163    /**
164     * Add a criterion to AudioPolicyPfw.
165     *
166     * @param[in] name of the PFW criterion.
167     * @param[in] typeName criterion type name to which this criterion is associated to.
168     * @param[in] defaultLiteralValue of the PFW criterion.
169     */
170    void addCriterion(const std::string &name,
171                      const std::string &typeName,
172                      const std::string &defaultLiteralValue);
173    /**
174     * Parse and load the inclusive criterion type from configuration file.
175     *
176     * @param[in] root node of the configuration file.
177     */
178    void loadInclusiveCriterionType(cnode *root);
179
180    /**
181     * Parse and load the exclusive criterion type from configuration file.
182     *
183     * @param[in] root node of the configuration file.
184     */
185    void loadExclusiveCriterionType(cnode *root);
186
187    /**
188     * Parse and load the criteria from configuration file.
189     *
190     * @param[in] root node of the configuration file.
191     */
192    void loadCriteria(cnode *root);
193
194    /**
195     * Parse and load a criterion from configuration file.
196     *
197     * @param[in] root node of the configuration file.
198     */
199    void loadCriterion(cnode *root);
200
201    /**
202     * Parse and load the criterion types from configuration file.
203     *
204     * @param[in] root node of the configuration file
205     * @param[in] isInclusive true if inclusive, false is exclusive.
206     */
207    void loadCriterionType(cnode *root, bool isInclusive);
208
209    /**
210     * Load the configuration file.
211     *
212     * @param[in] root node of the configuration file.
213     */
214    void loadConfig(cnode *root);
215
216    /**
217     * Parse and load the chidren node from a given root node.
218     *
219     * @param[in] root node of the configuration file
220     * @param[out] defaultValue of the parameter manager element to retrieve.
221     * @param[out] type of the parameter manager element to retrieve.
222    */
223    void parseChildren(cnode *root, std::string &defaultValue, std::string &type);
224
225    /**
226     * Retrieve an element from a map by its name.
227     *
228     * @tparam T type of element to search.
229     * @param[in] name name of the element to find.
230     * @param[in] elementsMap maps of elements to search into.
231     *
232     * @return valid pointer on element if found, NULL otherwise.
233     */
234    template <typename T>
235    T *getElement(const std::string &name, std::map<std::string, T *> &elementsMap);
236
237    /**
238     * Retrieve an element from a map by its name. Const version.
239     *
240     * @tparam T type of element to search.
241     * @param[in] name name of the element to find.
242     * @param[in] elementsMap maps of elements to search into.
243     *
244     * @return valid pointer on element if found, NULL otherwise.
245     */
246    template <typename T>
247    const T *getElement(const std::string &name,
248                        const std::map<std::string, T *> &elementsMap) const;
249
250    /**
251     * set the value of a component state.
252     *
253     * @param[in] value new value to set to the component state.
254     * @param[in] stateName of the component state.
255     */
256    void setValue(int value, const std::string &stateName);
257
258    /**
259     * get the value of a component state.
260     *
261     * @param[in] name of the component state.
262     *
263     * @return value of the component state
264     */
265    int getValue(const std::string &stateName) const;
266
267    bool isValueValidForCriterion(ISelectionCriterionInterface *criterion, int valueToCheck);
268
269    CriterionTypeCollection mPolicyCriterionTypes; /**< Policy Criterion Type map. */
270    CriterionCollection mPolicyCriteria; /**< Policy Criterion Map. */
271
272    CParameterMgrPlatformConnector *mPfwConnector; /**< Policy Parameter Manager connector. */
273    ParameterMgrPlatformConnectorLogger *mPfwConnectorLogger; /**< Policy PFW logger. */
274
275
276    /**
277     * provide a compile time error if no specialization is provided for a given type.
278     *
279     * @tparam T: type of the parameter manager element. Supported one are:
280     *                      - Criterion
281     *                      - CriterionType.
282     */
283    template <typename T>
284    struct parameterManagerElementSupported;
285
286    static const char *const mPolicyPfwDefaultConfFileName; /**< Default Policy PFW top file name.*/
287};
288
289} // namespace audio_policy
290} // namespace android
291