1 /* 2 * Copyright (C) 2020 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 <optional> 20 #include <set> 21 #include <string> 22 #include <vector> 23 24 #include <gtest/gtest.h> 25 #include <utils/Errors.h> 26 27 // clang-format off 28 #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/types.h) 29 #include PATH(android/hardware/audio/common/COMMON_TYPES_FILE_VERSION/types.h) 30 // clang-format on 31 32 #include PATH(APM_XSD_ENUMS_H_FILENAME) 33 #include PATH(APM_XSD_H_FILENAME) 34 35 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; 36 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; 37 namespace xsd { 38 using namespace ::android::audio::policy::configuration::CPP_VERSION; 39 using Module = Modules::Module; 40 } 41 42 class PolicyConfig { 43 public: PolicyConfig(const std::string & configPath,const std::string & configFileName)44 PolicyConfig(const std::string& configPath, const std::string& configFileName) 45 : mConfigFileName{configFileName}, 46 mFilePath{configPath.empty() ? findExistingConfigurationFile(mConfigFileName) 47 : configPath + "/" + mConfigFileName}, 48 mConfig{xsd::read(mFilePath.c_str())} { 49 init(); 50 } getStatus()51 android::status_t getStatus() const { return mStatus; } 52 std::string getError() const; getFilePath()53 const std::string& getFilePath() const { return mFilePath; } 54 const xsd::Module* getModuleFromName(const std::string& name) const; getPrimaryModule()55 const xsd::Module* getPrimaryModule() const { return mPrimaryModule; } getModulesWithDevicesNames()56 const std::set<std::string>& getModulesWithDevicesNames() const { 57 return mModulesWithDevicesNames; 58 } getDeviceAddressOfSinkDeviceAttachedToMixPort(const std::string & moduleName,const std::string & mixPortName)59 std::optional<DeviceAddress> getDeviceAddressOfSinkDeviceAttachedToMixPort( 60 const std::string& moduleName, const std::string& mixPortName) const { 61 const auto attachedDevicePort = getAttachedSinkDeviceForMixPort(moduleName, mixPortName); 62 if (attachedDevicePort.empty()) return {}; 63 return getDeviceAddressOfDevicePort(moduleName, attachedDevicePort); 64 } getDeviceAddressOfSourceDeviceAttachedToMixPort(const std::string & moduleName,const std::string & mixPortName)65 std::optional<DeviceAddress> getDeviceAddressOfSourceDeviceAttachedToMixPort( 66 const std::string& moduleName, const std::string& mixPortName) const { 67 const auto attachedDevicePort = getAttachedSourceDeviceForMixPort(moduleName, mixPortName); 68 if (attachedDevicePort.empty()) return {}; 69 return getDeviceAddressOfDevicePort(moduleName, attachedDevicePort); 70 } getAttachedSinkDeviceForMixPort(const std::string & moduleName,const std::string & mixPortName)71 std::string getAttachedSinkDeviceForMixPort(const std::string& moduleName, 72 const std::string& mixPortName) const { 73 return findAttachedDevice(getAttachedDevices(moduleName), 74 getSinkDevicesForMixPort(moduleName, mixPortName)); 75 } getAttachedSourceDeviceForMixPort(const std::string & moduleName,const std::string & mixPortName)76 std::string getAttachedSourceDeviceForMixPort(const std::string& moduleName, 77 const std::string& mixPortName) const { 78 return findAttachedDevice(getAttachedDevices(moduleName), 79 getSourceDevicesForMixPort(moduleName, mixPortName)); 80 } 81 std::optional<DeviceAddress> getSinkDeviceForMixPort(const std::string& moduleName, 82 const std::string& mixPortName) const; 83 std::optional<DeviceAddress> getSourceDeviceForMixPort(const std::string& moduleName, 84 const std::string& mixPortName) const; 85 bool haveInputProfilesInModule(const std::string& name) const; 86 87 private: 88 static std::string findExistingConfigurationFile(const std::string& fileName); 89 std::string findAttachedDevice(const std::vector<std::string>& attachedDevices, 90 const std::set<std::string>& possibleDevices) const; 91 const std::vector<std::string>& getAttachedDevices(const std::string& moduleName) const; 92 std::optional<DeviceAddress> getDeviceAddressOfDevicePort( 93 const std::string& moduleName, const std::string& devicePortName) const; 94 std::set<std::string> getSinkDevicesForMixPort(const std::string& moduleName, 95 const std::string& mixPortName) const; 96 std::set<std::string> getSourceDevicesForMixPort(const std::string& moduleName, 97 const std::string& mixPortName) const; 98 void init(); 99 100 const std::string mConfigFileName; 101 const std::string mFilePath; 102 std::optional<xsd::AudioPolicyConfiguration> mConfig; 103 android::status_t mStatus = android::NO_INIT; 104 const xsd::Module* mPrimaryModule; 105 std::set<std::string> mModulesWithDevicesNames; 106 }; 107