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/power_policy.xml. If the XML file is invalid, 71 * no power policy is registered and the system power policy is set to default. 72 */ 73 class PolicyManager { 74 public: 75 void init(); 76 android::base::Result<CarPowerPolicyMeta> getPowerPolicy(const std::string& policyId) const; 77 android::base::Result<CarPowerPolicyPtr> getDefaultPowerPolicyForState( 78 const std::string& groupId, 79 aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReport state) const; 80 bool isPowerPolicyGroupAvailable(const std::string& groupId) const; 81 bool isPreemptivePowerPolicy(const std::string& policyId) const; 82 android::base::Result<void> definePowerPolicy( 83 const std::string& policyId, const std::vector<std::string>& enabledComponents, 84 const std::vector<std::string>& disabledComponents); 85 android::base::Result<void> dump(int fd, const android::Vector<String16>& args); 86 std::string getDefaultPolicyGroup() const; 87 88 private: 89 void initRegularPowerPolicy(bool override); 90 void initPreemptivePowerPolicy(); 91 void readPowerPolicyConfiguration(); 92 void readPowerPolicyFromXml(const tinyxml2::XMLDocument& xmlDoc); 93 void reconstructNoUserInteractionPolicy(const std::vector<CarPowerPolicyPtr>& policyOverrides); 94 95 private: 96 std::unordered_map<std::string, CarPowerPolicyPtr> mRegisteredPowerPolicies; 97 std::unordered_map<std::string, CarPowerPolicyPtr> mPreemptivePowerPolicies; 98 std::unordered_map<std::string, PolicyGroup> mPolicyGroups; 99 std::string mDefaultPolicyGroup; 100 101 // For unit tests. 102 friend class internal::PolicyManagerPeer; 103 }; 104 105 } // namespace powerpolicy 106 } // namespace automotive 107 } // namespace frameworks 108 } // namespace android 109 110 #endif // CPP_POWERPOLICY_SERVER_SRC_POLICYMANAGER_H_ 111