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 #define LOG_TAG "carpowerpolicyd"
18 #define DEBUG false
19 
20 #include "PowerComponentHandler.h"
21 
22 #include <android-base/file.h>
23 #include <android-base/stringprintf.h>
24 #include <binder/Enums.h>
25 
26 namespace android {
27 namespace frameworks {
28 namespace automotive {
29 namespace powerpolicy {
30 
31 using ::android::base::Error;
32 using ::android::base::Result;
33 using ::android::base::StringPrintf;
34 using ::android::base::WriteStringToFd;
35 
init()36 void PowerComponentHandler::init() {
37     Mutex::Autolock lock(mMutex);
38     mAccumulatedPolicy = std::make_shared<CarPowerPolicy>();
39     for (const auto componentId : enum_range<PowerComponent>()) {
40         mAccumulatedPolicy->disabledComponents.push_back(componentId);
41     }
42 }
43 
applyPowerPolicy(const CarPowerPolicyPtr & powerPolicy)44 void PowerComponentHandler::applyPowerPolicy(const CarPowerPolicyPtr& powerPolicy) {
45     Mutex::Autolock lock(mMutex);
46     std::unordered_map<PowerComponent, bool> componentStates;
47     mAccumulatedPolicy->policyId = powerPolicy->policyId;
48     for (const auto component : mAccumulatedPolicy->enabledComponents) {
49         componentStates[component] = true;
50     }
51     for (const auto component : mAccumulatedPolicy->disabledComponents) {
52         componentStates[component] = false;
53     }
54     for (const auto component : powerPolicy->enabledComponents) {
55         componentStates[component] = true;
56     }
57     for (const auto component : powerPolicy->disabledComponents) {
58         componentStates[component] = false;
59     }
60     mAccumulatedPolicy->enabledComponents.clear();
61     mAccumulatedPolicy->disabledComponents.clear();
62     for (const auto [component, state] : componentStates) {
63         if (state) {
64             mAccumulatedPolicy->enabledComponents.push_back(component);
65         } else {
66             mAccumulatedPolicy->disabledComponents.push_back(component);
67         }
68     }
69 }
70 
getPowerComponentState(const PowerComponent componentId) const71 Result<bool> PowerComponentHandler::getPowerComponentState(const PowerComponent componentId) const {
72     Mutex::Autolock lock(mMutex);
73     auto findComponent = [componentId](const std::vector<PowerComponent>& components) -> bool {
74         return std::find(components.begin(), components.end(), componentId) != components.end();
75     };
76     if (findComponent(mAccumulatedPolicy->enabledComponents)) {
77         return true;
78     }
79     if (findComponent(mAccumulatedPolicy->disabledComponents)) {
80         return false;
81     }
82     return Error() << StringPrintf("Invalid power component(%d)", componentId);
83 }
84 
getAccumulatedPolicy() const85 CarPowerPolicyPtr PowerComponentHandler::getAccumulatedPolicy() const {
86     Mutex::Autolock lock(mMutex);
87     return mAccumulatedPolicy;
88 }
89 
dump(int fd)90 Result<void> PowerComponentHandler::dump(int fd) {
91     Mutex::Autolock lock(mMutex);
92     const char* indent = "  ";
93     const char* doubleIndent = "    ";
94     auto printComponents = [fd](const std::vector<PowerComponent>& components) {
95         bool isNotFirst = false;
96         for (const auto component : components) {
97             if (isNotFirst) {
98                 WriteStringToFd(", ", fd);
99             } else {
100                 isNotFirst = true;
101             }
102             WriteStringToFd(toString(component), fd);
103         }
104         WriteStringToFd("\n", fd);
105     };
106 
107     WriteStringToFd(StringPrintf("%sCurrent state of power components:\n", indent), fd);
108     WriteStringToFd(StringPrintf("%sEnabled components: ", doubleIndent), fd);
109     printComponents(mAccumulatedPolicy->enabledComponents);
110     WriteStringToFd(StringPrintf("%sDisabled components: ", doubleIndent), fd);
111     printComponents(mAccumulatedPolicy->disabledComponents);
112 
113     return {};
114 }
115 
116 }  // namespace powerpolicy
117 }  // namespace automotive
118 }  // namespace frameworks
119 }  // namespace android
120