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 59 } // namespace internal 60 61 // CarPowerPolicyMeta includes a car power policy and its meta information. 62 struct CarPowerPolicyMeta { 63 CarPowerPolicyPtr powerPolicy = nullptr; 64 bool isPreemptive = false; 65 }; 66 67 /** 68 * PolicyManager manages power policies, power policy mapping to power transision, and system power 69 * policy. 70 * It reads vendor policy information from /vendor/etc/automotive/power_policy.xml. 71 * If the XML file is invalid, no power policy is registered and the system power policy is set to 72 * default. 73 */ 74 class PolicyManager { 75 public: 76 void init(); 77 android::base::Result<CarPowerPolicyMeta> getPowerPolicy(const std::string& policyId) const; 78 android::base::Result<CarPowerPolicyPtr> getDefaultPowerPolicyForState( 79 const std::string& groupId, 80 aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReport state) const; 81 bool isPowerPolicyGroupAvailable(const std::string& groupId) const; 82 bool isPreemptivePowerPolicy(const std::string& policyId) const; 83 android::base::Result<void> definePowerPolicy( 84 const std::string& policyId, const std::vector<std::string>& enabledComponents, 85 const std::vector<std::string>& disabledComponents); 86 android::base::Result<void> dump(int fd, const android::Vector<String16>& args); 87 std::string getDefaultPolicyGroup() const; 88 89 private: 90 void initRegularPowerPolicy(bool override); 91 void initPreemptivePowerPolicy(); 92 void readPowerPolicyConfiguration(); 93 void readPowerPolicyFromXml(const tinyxml2::XMLDocument& xmlDoc); 94 void reconstructNoUserInteractionPolicy(const std::vector<CarPowerPolicyPtr>& policyOverrides); 95 96 private: 97 std::unordered_map<std::string, CarPowerPolicyPtr> mRegisteredPowerPolicies; 98 std::unordered_map<std::string, CarPowerPolicyPtr> mPreemptivePowerPolicies; 99 std::unordered_map<std::string, PolicyGroup> mPolicyGroups; 100 std::string mDefaultPolicyGroup; 101 std::unordered_map<std::string, int> mCustomComponents; 102 103 // For unit tests. 104 friend class internal::PolicyManagerPeer; 105 }; 106 107 } // namespace powerpolicy 108 } // namespace automotive 109 } // namespace frameworks 110 } // namespace android 111 112 #endif // CPP_POWERPOLICY_SERVER_SRC_POLICYMANAGER_H_ 113