1 /*
2 * Copyright (C) 2021 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 #include <fcntl.h>
18 #include <unistd.h>
19
20 #include <algorithm>
21
22 #include <HidlUtils.h>
23 #include <system/audio.h>
24 #include <system/audio_config.h>
25
26 #include "DeviceManager.h"
27 #include "PolicyConfig.h"
28 #include "common/all-versions/HidlSupport.h"
29
30 using ::android::NO_ERROR;
31 using ::android::OK;
32
33 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
34 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
35 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
36 using ::android::hardware::audio::common::utils::splitString;
37 namespace xsd {
38 using namespace ::android::audio::policy::configuration::CPP_VERSION;
39 using Module = Modules::Module;
40 } // namespace xsd
41
getError() const42 std::string PolicyConfig::getError() const {
43 if (mFilePath.empty()) {
44 return "Could not find " + mConfigFileName +
45 " file in: " + testing::PrintToString(android::audio_get_configuration_paths());
46 } else {
47 return "Invalid config file: " + mFilePath;
48 }
49 }
50
getModuleFromName(const std::string & name) const51 const xsd::Module* PolicyConfig::getModuleFromName(const std::string& name) const {
52 if (mConfig && mConfig->getFirstModules()) {
53 for (const auto& module : mConfig->getFirstModules()->get_module()) {
54 if (module.getName() == name) return &module;
55 }
56 }
57 return nullptr;
58 }
59
getSinkDeviceForMixPort(const std::string & moduleName,const std::string & mixPortName) const60 std::optional<DeviceAddress> PolicyConfig::getSinkDeviceForMixPort(
61 const std::string& moduleName, const std::string& mixPortName) const {
62 std::string device;
63 if (auto module = getModuleFromName(moduleName); module) {
64 auto possibleDevices = getSinkDevicesForMixPort(moduleName, mixPortName);
65 if (module->hasDefaultOutputDevice() &&
66 possibleDevices.count(module->getDefaultOutputDevice())) {
67 device = module->getDefaultOutputDevice();
68 } else {
69 device = getAttachedSinkDeviceForMixPort(moduleName, mixPortName);
70 }
71 }
72 if (!device.empty()) {
73 return getDeviceAddressOfDevicePort(moduleName, device);
74 }
75 ALOGE("Could not find a route for the mix port \"%s\" in module \"%s\"", mixPortName.c_str(),
76 moduleName.c_str());
77 return std::optional<DeviceAddress>{};
78 }
79
getSourceDeviceForMixPort(const std::string & moduleName,const std::string & mixPortName) const80 std::optional<DeviceAddress> PolicyConfig::getSourceDeviceForMixPort(
81 const std::string& moduleName, const std::string& mixPortName) const {
82 const std::string device = getAttachedSourceDeviceForMixPort(moduleName, mixPortName);
83 if (!device.empty()) {
84 return getDeviceAddressOfDevicePort(moduleName, device);
85 }
86 ALOGE("Could not find a route for the mix port \"%s\" in module \"%s\"", mixPortName.c_str(),
87 moduleName.c_str());
88 return std::optional<DeviceAddress>{};
89 }
90
haveInputProfilesInModule(const std::string & name) const91 bool PolicyConfig::haveInputProfilesInModule(const std::string& name) const {
92 auto module = getModuleFromName(name);
93 if (module && module->getFirstMixPorts()) {
94 for (const auto& mixPort : module->getFirstMixPorts()->getMixPort()) {
95 if (mixPort.getRole() == xsd::Role::sink) return true;
96 }
97 }
98 return false;
99 }
100
101 // static
findExistingConfigurationFile(const std::string & fileName)102 std::string PolicyConfig::findExistingConfigurationFile(const std::string& fileName) {
103 for (const auto& location : android::audio_get_configuration_paths()) {
104 std::string path = location + '/' + fileName;
105 if (access(path.c_str(), F_OK) == 0) {
106 return path;
107 }
108 }
109 return {};
110 }
111
findAttachedDevice(const std::vector<std::string> & attachedDevices,const std::set<std::string> & possibleDevices) const112 std::string PolicyConfig::findAttachedDevice(const std::vector<std::string>& attachedDevices,
113 const std::set<std::string>& possibleDevices) const {
114 for (const auto& device : attachedDevices) {
115 if (possibleDevices.count(device)) return device;
116 }
117 return {};
118 }
119
getAttachedDevices(const std::string & moduleName) const120 const std::vector<std::string>& PolicyConfig::getAttachedDevices(
121 const std::string& moduleName) const {
122 static const std::vector<std::string> empty;
123 auto module = getModuleFromName(moduleName);
124 if (module && module->getFirstAttachedDevices()) {
125 return module->getFirstAttachedDevices()->getItem();
126 }
127 return empty;
128 }
129
getDeviceAddressOfDevicePort(const std::string & moduleName,const std::string & devicePortName) const130 std::optional<DeviceAddress> PolicyConfig::getDeviceAddressOfDevicePort(
131 const std::string& moduleName, const std::string& devicePortName) const {
132 auto module = getModuleFromName(moduleName);
133 if (module->getFirstDevicePorts()) {
134 const auto& devicePorts = module->getFirstDevicePorts()->getDevicePort();
135 const auto& devicePort = std::find_if(
136 devicePorts.begin(), devicePorts.end(),
137 [&devicePortName](auto dp) { return dp.getTagName() == devicePortName; });
138 if (devicePort != devicePorts.end()) {
139 audio_devices_t halDeviceType;
140 if (HidlUtils::audioDeviceTypeToHal(devicePort->getType(), &halDeviceType) ==
141 NO_ERROR) {
142 // For AOSP device types use the standard parser for the device address.
143 const std::string address =
144 devicePort->hasAddress() ? devicePort->getAddress() : "";
145 DeviceAddress result;
146 if (HidlUtils::deviceAddressFromHal(halDeviceType, address.c_str(), &result) ==
147 NO_ERROR) {
148 return result;
149 }
150 } else if (xsd::isVendorExtension(devicePort->getType())) {
151 DeviceAddress result;
152 result.deviceType = devicePort->getType();
153 if (devicePort->hasAddress()) {
154 result.address.id(devicePort->getAddress());
155 }
156 return result;
157 }
158 } else {
159 ALOGE("Device port \"%s\" not found in module \"%s\"", devicePortName.c_str(),
160 moduleName.c_str());
161 }
162 } else {
163 ALOGE("Module \"%s\" has no device ports", moduleName.c_str());
164 }
165 return std::optional<DeviceAddress>{};
166 }
167
getSinkDevicesForMixPort(const std::string & moduleName,const std::string & mixPortName) const168 std::set<std::string> PolicyConfig::getSinkDevicesForMixPort(const std::string& moduleName,
169 const std::string& mixPortName) const {
170 std::set<std::string> result;
171 auto module = getModuleFromName(moduleName);
172 if (module && module->getFirstRoutes()) {
173 for (const auto& route : module->getFirstRoutes()->getRoute()) {
174 const auto sources = splitString(route.getSources(), ',');
175 if (std::find(sources.begin(), sources.end(), mixPortName) != sources.end()) {
176 result.insert(route.getSink());
177 }
178 }
179 }
180 return result;
181 }
182
getSourceDevicesForMixPort(const std::string & moduleName,const std::string & mixPortName) const183 std::set<std::string> PolicyConfig::getSourceDevicesForMixPort(
184 const std::string& moduleName, const std::string& mixPortName) const {
185 std::set<std::string> result;
186 auto module = getModuleFromName(moduleName);
187 if (module && module->getFirstRoutes()) {
188 const auto& routes = module->getFirstRoutes()->getRoute();
189 const auto route = std::find_if(routes.begin(), routes.end(), [&mixPortName](auto rte) {
190 return rte.getSink() == mixPortName;
191 });
192 if (route != routes.end()) {
193 const auto sources = splitString(route->getSources(), ',');
194 std::copy(sources.begin(), sources.end(), std::inserter(result, result.end()));
195 }
196 }
197 return result;
198 }
199
init()200 void PolicyConfig::init() {
201 if (mConfig) {
202 mStatus = OK;
203 mPrimaryModule = getModuleFromName(DeviceManager::kPrimaryDevice);
204 if (mConfig->getFirstModules()) {
205 for (const auto& module : mConfig->getFirstModules()->get_module()) {
206 if (module.getFirstAttachedDevices()) {
207 auto attachedDevices = module.getFirstAttachedDevices()->getItem();
208 if (!attachedDevices.empty()) {
209 mModulesWithDevicesNames.insert(module.getName());
210 }
211 }
212 }
213 }
214 }
215 }
216