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 #ifndef CPP_POWERPOLICY_SERVER_SRC_POLICYMANAGER_H_ 18 #define CPP_POWERPOLICY_SERVER_SRC_POLICYMANAGER_H_ 19 20 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicy.h> 21 #include <aidl/android/hardware/automotive/vehicle/VehicleApPowerStateReport.h> 22 #include <android-base/result.h> 23 #include <utils/Vector.h> 24 25 #include <tinyxml2.h> 26 27 #include <memory> 28 #include <string> 29 #include <unordered_map> 30 #include <vector> 31 32 namespace android { 33 namespace frameworks { 34 namespace automotive { 35 namespace powerpolicy { 36 37 std::string toString( 38 const ::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy& policy); 39 std::string toString( 40 const std::vector<::aidl::android::frameworks::automotive::powerpolicy::PowerComponent>& 41 components); 42 43 bool isSystemPowerPolicy(const std::string& policyId); 44 45 using CarPowerPolicyPtr = 46 std::shared_ptr<::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy>; 47 using PolicyGroup = std::unordered_map<int32_t, std::string>; 48 49 constexpr const char kSystemPolicyIdNoUserInteraction[] = "system_power_policy_no_user_interaction"; 50 constexpr const char kSystemPolicyIdAllOn[] = "system_power_policy_all_on"; 51 constexpr const char kSystemPolicyIdInitialOn[] = "system_power_policy_initial_on"; 52 constexpr const char kSystemPolicyIdSuspendPrep[] = "system_power_policy_suspend_prep"; 53 54 // Forward declaration for testing use only. 55 namespace internal { 56 57 class PolicyManagerPeer; 58 class CarPowerPolicyServerPeer; 59 60 } // namespace internal 61 62 // CarPowerPolicyMeta includes a car power policy and its meta information. 63 struct CarPowerPolicyMeta { 64 CarPowerPolicyPtr powerPolicy = nullptr; 65 bool isPreemptive = false; 66 }; 67 68 /** 69 * PolicyManager manages power policies, power policy mapping to power transision, and system power 70 * policy. 71 * It reads vendor policy information from /vendor/etc/automotive/power_policy.xml. 72 * If the XML file is invalid, no power policy is registered and the system power policy is set to 73 * default. 74 */ 75 class PolicyManager { 76 public: 77 void init(); 78 android::base::Result<CarPowerPolicyMeta> getPowerPolicy(const std::string& policyId) const; 79 android::base::Result<CarPowerPolicyPtr> getDefaultPowerPolicyForState( 80 const std::string& groupId, 81 aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReport state) const; 82 bool isPowerPolicyGroupAvailable(const std::string& groupId) const; 83 bool isPreemptivePowerPolicy(const std::string& policyId) const; 84 android::base::Result<void> definePowerPolicy( 85 const std::string& policyId, const std::vector<std::string>& enabledComponents, 86 const std::vector<std::string>& disabledComponents); 87 android::base::Result<void> definePowerPolicyGroup( 88 const std::string& policyGroupId, const std::vector<std::string>& powerPolicyPerState); 89 android::base::Result<void> dump(int fd, const android::Vector<String16>& args); 90 std::string getDefaultPolicyGroup() const; 91 std::vector<int32_t> getCustomComponents() const; 92 std::vector<aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy> 93 getRegisteredPolicies() const; 94 95 private: 96 void initRegularPowerPolicy(bool override); 97 void initPreemptivePowerPolicy(); 98 void readPowerPolicyConfiguration(); 99 void readPowerPolicyFromXml(const tinyxml2::XMLDocument& xmlDoc); 100 void reconstructNoUserInteractionPolicy(const std::vector<CarPowerPolicyPtr>& policyOverrides); 101 102 private: 103 std::unordered_map<std::string, CarPowerPolicyPtr> mRegisteredPowerPolicies; 104 std::unordered_map<std::string, CarPowerPolicyPtr> mPreemptivePowerPolicies; 105 std::unordered_map<std::string, PolicyGroup> mPolicyGroups; 106 std::string mDefaultPolicyGroup; 107 std::unordered_map<std::string, int32_t> mCustomComponents; 108 109 // For unit tests. 110 friend class android::frameworks::automotive::powerpolicy::internal::PolicyManagerPeer; 111 friend class android::frameworks::automotive::powerpolicy::internal::CarPowerPolicyServerPeer; 112 }; 113 114 } // namespace powerpolicy 115 } // namespace automotive 116 } // namespace frameworks 117 } // namespace android 118 119 #endif // CPP_POWERPOLICY_SERVER_SRC_POLICYMANAGER_H_ 120