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