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 #define LOG_TAG "APM::AudioPolicyEngine/PFWWrapper"
18 //#define LOG_NDEBUG 0
19
20 #include "ParameterManagerWrapper.h"
21 #include <ParameterMgrPlatformConnector.h>
22 #include <SelectionCriterionTypeInterface.h>
23 #include <SelectionCriterionInterface.h>
24 #include <media/convert.h>
25 #include <algorithm>
26 #include <cutils/config_utils.h>
27 #include <cutils/misc.h>
28 #include <fstream>
29 #include <limits>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 #include <stdint.h>
34 #include <cmath>
35 #include <utils/Log.h>
36
37 using std::string;
38 using std::map;
39 using std::vector;
40
41 /// PFW related definitions
42 // Logger
43 class ParameterMgrPlatformConnectorLogger : public CParameterMgrPlatformConnector::ILogger
44 {
45 public:
ParameterMgrPlatformConnectorLogger()46 ParameterMgrPlatformConnectorLogger() {}
47
info(const string & log)48 virtual void info(const string &log)
49 {
50 ALOGV("policy-parameter-manager: %s", log.c_str());
51 }
warning(const string & log)52 virtual void warning(const string &log)
53 {
54 ALOGW("policy-parameter-manager: %s", log.c_str());
55 }
56 };
57
58 namespace android {
59
60 using utilities::convertTo;
61
62 namespace audio_policy {
63
64 const char *const ParameterManagerWrapper::mPolicyPfwDefaultConfFileName =
65 "/etc/parameter-framework/ParameterFrameworkConfigurationPolicy.xml";
66 const char *const ParameterManagerWrapper::mPolicyPfwVendorConfFileName =
67 "/vendor/etc/parameter-framework/ParameterFrameworkConfigurationPolicy.xml";
68
69 static const char *const gInputDeviceCriterionName = "AvailableInputDevices";
70 static const char *const gOutputDeviceCriterionName = "AvailableOutputDevices";
71 static const char *const gPhoneStateCriterionName = "TelephonyMode";
72 static const char *const gOutputDeviceAddressCriterionName = "AvailableOutputDevicesAddresses";
73 static const char *const gInputDeviceAddressCriterionName = "AvailableInputDevicesAddresses";
74
75 /**
76 * Order MUST be align with defintiion of audio_policy_force_use_t within audio_policy.h
77 */
78 static const char *const gForceUseCriterionTag[AUDIO_POLICY_FORCE_USE_CNT] =
79 {
80 [AUDIO_POLICY_FORCE_FOR_COMMUNICATION] = "ForceUseForCommunication",
81 [AUDIO_POLICY_FORCE_FOR_MEDIA] = "ForceUseForMedia",
82 [AUDIO_POLICY_FORCE_FOR_RECORD] = "ForceUseForRecord",
83 [AUDIO_POLICY_FORCE_FOR_DOCK] = "ForceUseForDock",
84 [AUDIO_POLICY_FORCE_FOR_SYSTEM] = "ForceUseForSystem",
85 [AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] = "ForceUseForHdmiSystemAudio",
86 [AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND] = "ForceUseForEncodedSurround",
87 [AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING] = "ForceUseForVibrateRinging"
88 };
89
90 template <>
91 struct ParameterManagerWrapper::parameterManagerElementSupported<ISelectionCriterionInterface> {};
92 template <>
93 struct ParameterManagerWrapper::parameterManagerElementSupported<ISelectionCriterionTypeInterface> {};
94
ParameterManagerWrapper(bool enableSchemaVerification,const std::string & schemaUri)95 ParameterManagerWrapper::ParameterManagerWrapper(bool enableSchemaVerification,
96 const std::string &schemaUri)
97 : mPfwConnectorLogger(new ParameterMgrPlatformConnectorLogger)
98 {
99 // Connector
100 if (access(mPolicyPfwVendorConfFileName, R_OK) == 0) {
101 mPfwConnector = new CParameterMgrPlatformConnector(mPolicyPfwVendorConfFileName);
102 } else {
103 mPfwConnector = new CParameterMgrPlatformConnector(mPolicyPfwDefaultConfFileName);
104 }
105
106 // Logger
107 mPfwConnector->setLogger(mPfwConnectorLogger);
108
109 // Schema validation
110 std::string error;
111 bool ret = mPfwConnector->setValidateSchemasOnStart(enableSchemaVerification, error);
112 ALOGE_IF(!ret, "Failed to activate schema validation: %s", error.c_str());
113 if (enableSchemaVerification && ret && !schemaUri.empty()) {
114 ALOGE("Schema verification activated with schema URI: %s", schemaUri.c_str());
115 mPfwConnector->setSchemaUri(schemaUri);
116 }
117 }
118
addCriterion(const std::string & name,bool isInclusive,ValuePairs pairs,const std::string & defaultValue)119 status_t ParameterManagerWrapper::addCriterion(const std::string &name, bool isInclusive,
120 ValuePairs pairs, const std::string &defaultValue)
121 {
122 ALOG_ASSERT(not isStarted(), "Cannot add a criterion if PFW is already started");
123 auto criterionType = mPfwConnector->createSelectionCriterionType(isInclusive);
124
125 for (auto pair : pairs) {
126 std::string error;
127 ALOGV("%s: Adding pair %d,%s for criterionType %s", __FUNCTION__, pair.first,
128 pair.second.c_str(), name.c_str());
129 criterionType->addValuePair(pair.first, pair.second, error);
130 }
131 ALOG_ASSERT(mPolicyCriteria.find(name) == mPolicyCriteria.end(),
132 "%s: Criterion %s already added", __FUNCTION__, name.c_str());
133
134 auto criterion = mPfwConnector->createSelectionCriterion(name, criterionType);
135 mPolicyCriteria[name] = criterion;
136
137 if (not defaultValue.empty()) {
138 int numericalValue = 0;
139 if (not criterionType->getNumericalValue(defaultValue.c_str(), numericalValue)) {
140 ALOGE("%s; trying to apply invalid default literal value (%s)", __FUNCTION__,
141 defaultValue.c_str());
142 }
143 criterion->setCriterionState(numericalValue);
144 }
145 return NO_ERROR;
146 }
147
~ParameterManagerWrapper()148 ParameterManagerWrapper::~ParameterManagerWrapper()
149 {
150 // Unset logger
151 mPfwConnector->setLogger(NULL);
152 // Remove logger
153 delete mPfwConnectorLogger;
154 // Remove connector
155 delete mPfwConnector;
156 }
157
start(std::string & error)158 status_t ParameterManagerWrapper::start(std::string &error)
159 {
160 ALOGD("%s: in", __FUNCTION__);
161 /// Start PFW
162 if (!mPfwConnector->start(error)) {
163 ALOGE("%s: Policy PFW start error: %s", __FUNCTION__, error.c_str());
164 return NO_INIT;
165 }
166 ALOGD("%s: Policy PFW successfully started!", __FUNCTION__);
167 return NO_ERROR;
168 }
169
170 template <typename T>
getElement(const string & name,std::map<string,T * > & elementsMap)171 T *ParameterManagerWrapper::getElement(const string &name, std::map<string, T *> &elementsMap)
172 {
173 parameterManagerElementSupported<T>();
174 typename std::map<string, T *>::iterator it = elementsMap.find(name);
175 ALOG_ASSERT(it != elementsMap.end(), "Element %s not found", name.c_str());
176 return it != elementsMap.end() ? it->second : NULL;
177 }
178
179 template <typename T>
getElement(const string & name,const std::map<string,T * > & elementsMap) const180 const T *ParameterManagerWrapper::getElement(const string &name, const std::map<string, T *> &elementsMap) const
181 {
182 parameterManagerElementSupported<T>();
183 typename std::map<string, T *>::const_iterator it = elementsMap.find(name);
184 ALOG_ASSERT(it != elementsMap.end(), "Element %s not found", name.c_str());
185 return it != elementsMap.end() ? it->second : NULL;
186 }
187
isStarted()188 bool ParameterManagerWrapper::isStarted()
189 {
190 return mPfwConnector && mPfwConnector->isStarted();
191 }
192
setPhoneState(audio_mode_t mode)193 status_t ParameterManagerWrapper::setPhoneState(audio_mode_t mode)
194 {
195 ISelectionCriterionInterface *criterion =
196 getElement<ISelectionCriterionInterface>(gPhoneStateCriterionName, mPolicyCriteria);
197 if (criterion == NULL) {
198 ALOGE("%s: no criterion found for %s", __FUNCTION__, gPhoneStateCriterionName);
199 return BAD_VALUE;
200 }
201 if (!isValueValidForCriterion(criterion, static_cast<int>(mode))) {
202 return BAD_VALUE;
203 }
204 criterion->setCriterionState((int)(mode));
205 applyPlatformConfiguration();
206 return NO_ERROR;
207 }
208
getPhoneState() const209 audio_mode_t ParameterManagerWrapper::getPhoneState() const
210 {
211 const ISelectionCriterionInterface *criterion =
212 getElement<ISelectionCriterionInterface>(gPhoneStateCriterionName, mPolicyCriteria);
213 if (criterion == NULL) {
214 ALOGE("%s: no criterion found for %s", __FUNCTION__, gPhoneStateCriterionName);
215 return AUDIO_MODE_NORMAL;
216 }
217 return static_cast<audio_mode_t>(criterion->getCriterionState());
218 }
219
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)220 status_t ParameterManagerWrapper::setForceUse(audio_policy_force_use_t usage,
221 audio_policy_forced_cfg_t config)
222 {
223 // @todo: return an error on a unsupported value
224 if (usage > AUDIO_POLICY_FORCE_USE_CNT) {
225 return BAD_VALUE;
226 }
227
228 ISelectionCriterionInterface *criterion =
229 getElement<ISelectionCriterionInterface>(gForceUseCriterionTag[usage], mPolicyCriteria);
230 if (criterion == NULL) {
231 ALOGE("%s: no criterion found for %s", __FUNCTION__, gForceUseCriterionTag[usage]);
232 return BAD_VALUE;
233 }
234 if (!isValueValidForCriterion(criterion, static_cast<int>(config))) {
235 return BAD_VALUE;
236 }
237 criterion->setCriterionState((int)config);
238 applyPlatformConfiguration();
239 return NO_ERROR;
240 }
241
getForceUse(audio_policy_force_use_t usage) const242 audio_policy_forced_cfg_t ParameterManagerWrapper::getForceUse(audio_policy_force_use_t usage) const
243 {
244 // @todo: return an error on a unsupported value
245 if (usage > AUDIO_POLICY_FORCE_USE_CNT) {
246 return AUDIO_POLICY_FORCE_NONE;
247 }
248 const ISelectionCriterionInterface *criterion =
249 getElement<ISelectionCriterionInterface>(gForceUseCriterionTag[usage], mPolicyCriteria);
250 if (criterion == NULL) {
251 ALOGE("%s: no criterion found for %s", __FUNCTION__, gForceUseCriterionTag[usage]);
252 return AUDIO_POLICY_FORCE_NONE;
253 }
254 return static_cast<audio_policy_forced_cfg_t>(criterion->getCriterionState());
255 }
256
isValueValidForCriterion(ISelectionCriterionInterface * criterion,int valueToCheck)257 bool ParameterManagerWrapper::isValueValidForCriterion(ISelectionCriterionInterface *criterion,
258 int valueToCheck)
259 {
260 const ISelectionCriterionTypeInterface *interface = criterion->getCriterionType();
261 string literalValue;
262 return interface->getLiteralValue(valueToCheck, literalValue);
263 }
264
setDeviceConnectionState(audio_devices_t type,const std::string address,audio_policy_dev_state_t state)265 status_t ParameterManagerWrapper::setDeviceConnectionState(
266 audio_devices_t type, const std::string address, audio_policy_dev_state_t state)
267 {
268 std::string criterionName = audio_is_output_device(type) ?
269 gOutputDeviceAddressCriterionName : gInputDeviceAddressCriterionName;
270
271 ALOGV("%s: device with address %s %s", __FUNCTION__, address.c_str(),
272 state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE? "disconnected" : "connected");
273 ISelectionCriterionInterface *criterion =
274 getElement<ISelectionCriterionInterface>(criterionName, mPolicyCriteria);
275
276 if (criterion == NULL) {
277 ALOGE("%s: no criterion found for %s", __FUNCTION__, criterionName.c_str());
278 return DEAD_OBJECT;
279 }
280
281 auto criterionType = criterion->getCriterionType();
282 int deviceAddressId;
283 if (not criterionType->getNumericalValue(address.c_str(), deviceAddressId)) {
284 ALOGW("%s: unknown device address reported (%s) for criterion %s", __FUNCTION__,
285 address.c_str(), criterionName.c_str());
286 return BAD_TYPE;
287 }
288 int currentValueMask = criterion->getCriterionState();
289 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
290 currentValueMask |= deviceAddressId;
291 }
292 else {
293 currentValueMask &= ~deviceAddressId;
294 }
295 criterion->setCriterionState(currentValueMask);
296 return NO_ERROR;
297 }
298
setAvailableInputDevices(audio_devices_t inputDevices)299 status_t ParameterManagerWrapper::setAvailableInputDevices(audio_devices_t inputDevices)
300 {
301 ISelectionCriterionInterface *criterion =
302 getElement<ISelectionCriterionInterface>(gInputDeviceCriterionName, mPolicyCriteria);
303 if (criterion == NULL) {
304 ALOGE("%s: no criterion found for %s", __FUNCTION__, gInputDeviceCriterionName);
305 return DEAD_OBJECT;
306 }
307 criterion->setCriterionState(inputDevices & ~AUDIO_DEVICE_BIT_IN);
308 applyPlatformConfiguration();
309 return NO_ERROR;
310 }
311
setAvailableOutputDevices(audio_devices_t outputDevices)312 status_t ParameterManagerWrapper::setAvailableOutputDevices(audio_devices_t outputDevices)
313 {
314 ISelectionCriterionInterface *criterion =
315 getElement<ISelectionCriterionInterface>(gOutputDeviceCriterionName, mPolicyCriteria);
316 if (criterion == NULL) {
317 ALOGE("%s: no criterion found for %s", __FUNCTION__, gOutputDeviceCriterionName);
318 return DEAD_OBJECT;
319 }
320 criterion->setCriterionState(outputDevices);
321 applyPlatformConfiguration();
322 return NO_ERROR;
323 }
324
applyPlatformConfiguration()325 void ParameterManagerWrapper::applyPlatformConfiguration()
326 {
327 mPfwConnector->applyConfigurations();
328 }
329
330 } // namespace audio_policy
331 } // namespace android
332