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