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()95 ParameterManagerWrapper::ParameterManagerWrapper()
96 : mPfwConnectorLogger(new ParameterMgrPlatformConnectorLogger)
97 {
98 // Connector
99 if (access(mPolicyPfwVendorConfFileName, R_OK) == 0) {
100 mPfwConnector = new CParameterMgrPlatformConnector(mPolicyPfwVendorConfFileName);
101 } else {
102 mPfwConnector = new CParameterMgrPlatformConnector(mPolicyPfwDefaultConfFileName);
103 }
104
105 // Logger
106 mPfwConnector->setLogger(mPfwConnectorLogger);
107 }
108
addCriterion(const std::string & name,bool isInclusive,ValuePairs pairs,const std::string & defaultValue)109 status_t ParameterManagerWrapper::addCriterion(const std::string &name, bool isInclusive,
110 ValuePairs pairs, const std::string &defaultValue)
111 {
112 ALOG_ASSERT(not isStarted(), "Cannot add a criterion if PFW is already started");
113 auto criterionType = mPfwConnector->createSelectionCriterionType(isInclusive);
114
115 for (auto pair : pairs) {
116 std::string error;
117 ALOGV("%s: Adding pair %d,%s for criterionType %s", __FUNCTION__, pair.first,
118 pair.second.c_str(), name.c_str());
119 criterionType->addValuePair(pair.first, pair.second, error);
120 }
121 ALOG_ASSERT(mPolicyCriteria.find(name) == mPolicyCriteria.end(),
122 "%s: Criterion %s already added", __FUNCTION__, name.c_str());
123
124 auto criterion = mPfwConnector->createSelectionCriterion(name, criterionType);
125 mPolicyCriteria[name] = criterion;
126
127 if (not defaultValue.empty()) {
128 int numericalValue = 0;
129 if (not criterionType->getNumericalValue(defaultValue.c_str(), numericalValue)) {
130 ALOGE("%s; trying to apply invalid default literal value (%s)", __FUNCTION__,
131 defaultValue.c_str());
132 }
133 criterion->setCriterionState(numericalValue);
134 }
135 return NO_ERROR;
136 }
137
~ParameterManagerWrapper()138 ParameterManagerWrapper::~ParameterManagerWrapper()
139 {
140 // Unset logger
141 mPfwConnector->setLogger(NULL);
142 // Remove logger
143 delete mPfwConnectorLogger;
144 // Remove connector
145 delete mPfwConnector;
146 }
147
start()148 status_t ParameterManagerWrapper::start()
149 {
150 ALOGD("%s: in", __FUNCTION__);
151 /// Start PFW
152 std::string error;
153 if (!mPfwConnector->start(error)) {
154 ALOGE("%s: Policy PFW start error: %s", __FUNCTION__, error.c_str());
155 return NO_INIT;
156 }
157 ALOGD("%s: Policy PFW successfully started!", __FUNCTION__);
158 return NO_ERROR;
159 }
160
161 template <typename T>
getElement(const string & name,std::map<string,T * > & elementsMap)162 T *ParameterManagerWrapper::getElement(const string &name, std::map<string, T *> &elementsMap)
163 {
164 parameterManagerElementSupported<T>();
165 typename std::map<string, T *>::iterator it = elementsMap.find(name);
166 ALOG_ASSERT(it != elementsMap.end(), "Element %s not found", name.c_str());
167 return it != elementsMap.end() ? it->second : NULL;
168 }
169
170 template <typename T>
getElement(const string & name,const std::map<string,T * > & elementsMap) const171 const T *ParameterManagerWrapper::getElement(const string &name, const std::map<string, T *> &elementsMap) const
172 {
173 parameterManagerElementSupported<T>();
174 typename std::map<string, T *>::const_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
isStarted()179 bool ParameterManagerWrapper::isStarted()
180 {
181 return mPfwConnector && mPfwConnector->isStarted();
182 }
183
setPhoneState(audio_mode_t mode)184 status_t ParameterManagerWrapper::setPhoneState(audio_mode_t mode)
185 {
186 ISelectionCriterionInterface *criterion =
187 getElement<ISelectionCriterionInterface>(gPhoneStateCriterionName, mPolicyCriteria);
188 if (criterion == NULL) {
189 ALOGE("%s: no criterion found for %s", __FUNCTION__, gPhoneStateCriterionName);
190 return BAD_VALUE;
191 }
192 if (!isValueValidForCriterion(criterion, static_cast<int>(mode))) {
193 return BAD_VALUE;
194 }
195 criterion->setCriterionState((int)(mode));
196 applyPlatformConfiguration();
197 return NO_ERROR;
198 }
199
getPhoneState() const200 audio_mode_t ParameterManagerWrapper::getPhoneState() const
201 {
202 const ISelectionCriterionInterface *criterion =
203 getElement<ISelectionCriterionInterface>(gPhoneStateCriterionName, mPolicyCriteria);
204 if (criterion == NULL) {
205 ALOGE("%s: no criterion found for %s", __FUNCTION__, gPhoneStateCriterionName);
206 return AUDIO_MODE_NORMAL;
207 }
208 return static_cast<audio_mode_t>(criterion->getCriterionState());
209 }
210
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)211 status_t ParameterManagerWrapper::setForceUse(audio_policy_force_use_t usage,
212 audio_policy_forced_cfg_t config)
213 {
214 // @todo: return an error on a unsupported value
215 if (usage > AUDIO_POLICY_FORCE_USE_CNT) {
216 return BAD_VALUE;
217 }
218
219 ISelectionCriterionInterface *criterion =
220 getElement<ISelectionCriterionInterface>(gForceUseCriterionTag[usage], mPolicyCriteria);
221 if (criterion == NULL) {
222 ALOGE("%s: no criterion found for %s", __FUNCTION__, gForceUseCriterionTag[usage]);
223 return BAD_VALUE;
224 }
225 if (!isValueValidForCriterion(criterion, static_cast<int>(config))) {
226 return BAD_VALUE;
227 }
228 criterion->setCriterionState((int)config);
229 applyPlatformConfiguration();
230 return NO_ERROR;
231 }
232
getForceUse(audio_policy_force_use_t usage) const233 audio_policy_forced_cfg_t ParameterManagerWrapper::getForceUse(audio_policy_force_use_t usage) const
234 {
235 // @todo: return an error on a unsupported value
236 if (usage > AUDIO_POLICY_FORCE_USE_CNT) {
237 return AUDIO_POLICY_FORCE_NONE;
238 }
239 const ISelectionCriterionInterface *criterion =
240 getElement<ISelectionCriterionInterface>(gForceUseCriterionTag[usage], mPolicyCriteria);
241 if (criterion == NULL) {
242 ALOGE("%s: no criterion found for %s", __FUNCTION__, gForceUseCriterionTag[usage]);
243 return AUDIO_POLICY_FORCE_NONE;
244 }
245 return static_cast<audio_policy_forced_cfg_t>(criterion->getCriterionState());
246 }
247
isValueValidForCriterion(ISelectionCriterionInterface * criterion,int valueToCheck)248 bool ParameterManagerWrapper::isValueValidForCriterion(ISelectionCriterionInterface *criterion,
249 int valueToCheck)
250 {
251 const ISelectionCriterionTypeInterface *interface = criterion->getCriterionType();
252 string literalValue;
253 return interface->getLiteralValue(valueToCheck, literalValue);
254 }
255
setDeviceConnectionState(const sp<DeviceDescriptor> devDesc,audio_policy_dev_state_t state)256 status_t ParameterManagerWrapper::setDeviceConnectionState(const sp<DeviceDescriptor> devDesc,
257 audio_policy_dev_state_t state)
258 {
259 std::string criterionName = audio_is_output_device(devDesc->type()) ?
260 gOutputDeviceAddressCriterionName : gInputDeviceAddressCriterionName;
261
262 ALOGV("%s: device with address %s %s", __FUNCTION__, devDesc->address().string(),
263 state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE? "disconnected" : "connected");
264 ISelectionCriterionInterface *criterion =
265 getElement<ISelectionCriterionInterface>(criterionName, mPolicyCriteria);
266
267 if (criterion == NULL) {
268 ALOGE("%s: no criterion found for %s", __FUNCTION__, criterionName.c_str());
269 return DEAD_OBJECT;
270 }
271
272 auto criterionType = criterion->getCriterionType();
273 int deviceAddressId;
274 if (not criterionType->getNumericalValue(devDesc->address().string(), deviceAddressId)) {
275 ALOGW("%s: unknown device address reported (%s)", __FUNCTION__, devDesc->address().c_str());
276 return BAD_TYPE;
277 }
278 int currentValueMask = criterion->getCriterionState();
279 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
280 currentValueMask |= deviceAddressId;
281 }
282 else {
283 currentValueMask &= ~deviceAddressId;
284 }
285 criterion->setCriterionState(currentValueMask);
286 return NO_ERROR;
287 }
288
setAvailableInputDevices(audio_devices_t inputDevices)289 status_t ParameterManagerWrapper::setAvailableInputDevices(audio_devices_t inputDevices)
290 {
291 ISelectionCriterionInterface *criterion =
292 getElement<ISelectionCriterionInterface>(gInputDeviceCriterionName, mPolicyCriteria);
293 if (criterion == NULL) {
294 ALOGE("%s: no criterion found for %s", __FUNCTION__, gInputDeviceCriterionName);
295 return DEAD_OBJECT;
296 }
297 criterion->setCriterionState(inputDevices & ~AUDIO_DEVICE_BIT_IN);
298 applyPlatformConfiguration();
299 return NO_ERROR;
300 }
301
setAvailableOutputDevices(audio_devices_t outputDevices)302 status_t ParameterManagerWrapper::setAvailableOutputDevices(audio_devices_t outputDevices)
303 {
304 ISelectionCriterionInterface *criterion =
305 getElement<ISelectionCriterionInterface>(gOutputDeviceCriterionName, mPolicyCriteria);
306 if (criterion == NULL) {
307 ALOGE("%s: no criterion found for %s", __FUNCTION__, gOutputDeviceCriterionName);
308 return DEAD_OBJECT;
309 }
310 criterion->setCriterionState(outputDevices);
311 applyPlatformConfiguration();
312 return NO_ERROR;
313 }
314
applyPlatformConfiguration()315 void ParameterManagerWrapper::applyPlatformConfiguration()
316 {
317 mPfwConnector->applyConfigurations();
318 }
319
320 } // namespace audio_policy
321 } // namespace android
322