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 & configFileName)44 explicit PolicyConfig(const std::string& configFileName) 45 : mConfigFileName{configFileName}, 46 mFilePath{findExistingConfigurationFile(mConfigFileName)}, 47 mConfig{xsd::read(mFilePath.c_str())} { 48 init(); 49 } PolicyConfig(const std::string & configPath,const std::string & configFileName)50 PolicyConfig(const std::string& configPath, const std::string& configFileName) 51 : mConfigFileName{configFileName}, 52 mFilePath{configPath + "/" + mConfigFileName}, 53 mConfig{xsd::read(mFilePath.c_str())} { 54 init(); 55 } getStatus()56 android::status_t getStatus() const { return mStatus; } 57 std::string getError() const; getFilePath()58 const std::string& getFilePath() const { return mFilePath; } 59 const xsd::Module* getModuleFromName(const std::string& name) const; getPrimaryModule()60 const xsd::Module* getPrimaryModule() const { return mPrimaryModule; } getModulesWithDevicesNames()61 const std::set<std::string>& getModulesWithDevicesNames() const { 62 return mModulesWithDevicesNames; 63 } getDeviceAddressOfSinkDeviceAttachedToMixPort(const std::string & moduleName,const std::string & mixPortName)64 std::optional<DeviceAddress> getDeviceAddressOfSinkDeviceAttachedToMixPort( 65 const std::string& moduleName, const std::string& mixPortName) const { 66 const auto attachedDevicePort = getAttachedSinkDeviceForMixPort(moduleName, mixPortName); 67 if (attachedDevicePort.empty()) return {}; 68 return getDeviceAddressOfDevicePort(moduleName, attachedDevicePort); 69 } getDeviceAddressOfSourceDeviceAttachedToMixPort(const std::string & moduleName,const std::string & mixPortName)70 std::optional<DeviceAddress> getDeviceAddressOfSourceDeviceAttachedToMixPort( 71 const std::string& moduleName, const std::string& mixPortName) const { 72 const auto attachedDevicePort = getAttachedSourceDeviceForMixPort(moduleName, mixPortName); 73 if (attachedDevicePort.empty()) return {}; 74 return getDeviceAddressOfDevicePort(moduleName, attachedDevicePort); 75 } getAttachedSinkDeviceForMixPort(const std::string & moduleName,const std::string & mixPortName)76 std::string getAttachedSinkDeviceForMixPort(const std::string& moduleName, 77 const std::string& mixPortName) const { 78 return findAttachedDevice(getAttachedDevices(moduleName), 79 getSinkDevicesForMixPort(moduleName, mixPortName)); 80 } getAttachedSourceDeviceForMixPort(const std::string & moduleName,const std::string & mixPortName)81 std::string getAttachedSourceDeviceForMixPort(const std::string& moduleName, 82 const std::string& mixPortName) const { 83 return findAttachedDevice(getAttachedDevices(moduleName), 84 getSourceDevicesForMixPort(moduleName, mixPortName)); 85 } 86 std::optional<DeviceAddress> getSinkDeviceForMixPort(const std::string& moduleName, 87 const std::string& mixPortName) const; 88 std::optional<DeviceAddress> getSourceDeviceForMixPort(const std::string& moduleName, 89 const std::string& mixPortName) const; 90 bool haveInputProfilesInModule(const std::string& name) const; 91 92 private: 93 static std::string findExistingConfigurationFile(const std::string& fileName); 94 std::string findAttachedDevice(const std::vector<std::string>& attachedDevices, 95 const std::set<std::string>& possibleDevices) const; 96 const std::vector<std::string>& getAttachedDevices(const std::string& moduleName) const; 97 std::optional<DeviceAddress> getDeviceAddressOfDevicePort( 98 const std::string& moduleName, const std::string& devicePortName) const; 99 std::set<std::string> getSinkDevicesForMixPort(const std::string& moduleName, 100 const std::string& mixPortName) const; 101 std::set<std::string> getSourceDevicesForMixPort(const std::string& moduleName, 102 const std::string& mixPortName) const; 103 void init(); 104 105 const std::string mConfigFileName; 106 const std::string mFilePath; 107 std::optional<xsd::AudioPolicyConfiguration> mConfig; 108 android::status_t mStatus = android::NO_INIT; 109 const xsd::Module* mPrimaryModule; 110 std::set<std::string> mModulesWithDevicesNames; 111 }; 112