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_POWERCOMPONENTHANDLER_H_
18 #define CPP_POWERPOLICY_SERVER_SRC_POWERCOMPONENTHANDLER_H_
19 
20 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicy.h>
21 #include <android-base/result.h>
22 #include <utils/Mutex.h>
23 
24 #include <memory>
25 #include <unordered_map>
26 
27 namespace android {
28 namespace frameworks {
29 namespace automotive {
30 namespace powerpolicy {
31 
32 using CarPowerPolicyPtr =
33         std::shared_ptr<::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy>;
34 
35 class PowerComponentHandler final {
36 public:
PowerComponentHandler()37     PowerComponentHandler() {}
38 
39     // Sets the initial state of all power components.
40     void init();
41     // Applies the given power policy and updates the latest state of all power components.
42     void applyPowerPolicy(const CarPowerPolicyPtr& powerPolicy);
43     // Gets the current state of the given power component.
44     android::base::Result<bool> getPowerComponentState(
45             const ::aidl::android::frameworks::automotive::powerpolicy::PowerComponent componentId)
46             const;
47     // Gets the current state of the given custom component.
48     android::base::Result<bool> getCustomPowerComponentState(const int componentId) const;
49     // Gets the accumulated state of all components after applying power policies.
50     CarPowerPolicyPtr getAccumulatedPolicy() const;
51     // Dumps the internal status.
52     android::base::Result<void> dump(int fd);
53 
54 private:
55     mutable android::Mutex mMutex;
56     CarPowerPolicyPtr mAccumulatedPolicy GUARDED_BY(mMutex);
57 };
58 
59 }  // namespace powerpolicy
60 }  // namespace automotive
61 }  // namespace frameworks
62 }  // namespace android
63 
64 #endif  // CPP_POWERPOLICY_SERVER_SRC_POWERCOMPONENTHANDLER_H_
65