• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "powerhal_helper.h"
18 
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <android-base/properties.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/strings.h>
24 #include <android/binder_manager.h>
25 #include <hidl/HidlTransportSupport.h>
26 
27 #include <iterator>
28 #include <set>
29 #include <sstream>
30 #include <thread>
31 #include <vector>
32 
33 #include "thermal_info.h"
34 #include "thermal_throttling.h"
35 
36 namespace android {
37 namespace hardware {
38 namespace thermal {
39 namespace V2_0 {
40 namespace implementation {
41 
42 using android::base::StringPrintf;
43 
PowerHalService()44 PowerHalService::PowerHalService()
45     : power_hal_aidl_exist_(true), power_hal_aidl_(nullptr), power_hal_ext_aidl_(nullptr) {
46     connect();
47 }
48 
connect()49 bool PowerHalService::connect() {
50     std::lock_guard<std::mutex> lock(lock_);
51 
52     if (!power_hal_aidl_exist_) {
53         return false;
54     }
55 
56     if (power_hal_aidl_ && power_hal_ext_aidl_) {
57         return true;
58     }
59 
60     const std::string kInstance = std::string(IPower::descriptor) + "/default";
61     ndk::SpAIBinder power_binder = ndk::SpAIBinder(AServiceManager_getService(kInstance.c_str()));
62     ndk::SpAIBinder ext_power_binder;
63 
64     if (power_binder.get() == nullptr) {
65         LOG(ERROR) << "Cannot get Power Hal Binder";
66         power_hal_aidl_exist_ = false;
67         return false;
68     }
69 
70     power_hal_aidl_ = IPower::fromBinder(power_binder);
71 
72     if (power_hal_aidl_ == nullptr) {
73         power_hal_aidl_exist_ = false;
74         LOG(ERROR) << "Cannot get Power Hal AIDL" << kInstance.c_str();
75         return false;
76     }
77 
78     if (STATUS_OK != AIBinder_getExtension(power_binder.get(), ext_power_binder.getR()) ||
79         ext_power_binder.get() == nullptr) {
80         LOG(ERROR) << "Cannot get Power Hal Extension Binder";
81         power_hal_aidl_exist_ = false;
82         return false;
83     }
84 
85     power_hal_ext_aidl_ = IPowerExt::fromBinder(ext_power_binder);
86     if (power_hal_ext_aidl_ == nullptr) {
87         LOG(ERROR) << "Cannot get Power Hal Extension AIDL";
88         power_hal_aidl_exist_ = false;
89     }
90 
91     return true;
92 }
93 
isModeSupported(const std::string & type,const ThrottlingSeverity & t)94 bool PowerHalService::isModeSupported(const std::string &type, const ThrottlingSeverity &t) {
95     bool isSupported = false;
96     if (!connect()) {
97         return false;
98     }
99     std::string power_hint = StringPrintf("THERMAL_%s_%s", type.c_str(), toString(t).c_str());
100     lock_.lock();
101     if (!power_hal_ext_aidl_->isModeSupported(power_hint, &isSupported).isOk()) {
102         LOG(ERROR) << "Fail to check supported mode, Hint: " << power_hint;
103         power_hal_ext_aidl_ = nullptr;
104         power_hal_aidl_ = nullptr;
105         lock_.unlock();
106         return false;
107     }
108     lock_.unlock();
109     return isSupported;
110 }
111 
setMode(const std::string & type,const ThrottlingSeverity & t,const bool & enable,const bool error_on_exit)112 void PowerHalService::setMode(const std::string &type, const ThrottlingSeverity &t,
113                               const bool &enable, const bool error_on_exit) {
114     if (!connect()) {
115         return;
116     }
117 
118     std::string power_hint = StringPrintf("THERMAL_%s_%s", type.c_str(), toString(t).c_str());
119     LOG(INFO) << (error_on_exit ? "Resend Hint " : "Send Hint ") << power_hint
120               << " Enable: " << std::boolalpha << enable;
121     lock_.lock();
122     if (!power_hal_ext_aidl_->setMode(power_hint, enable).isOk()) {
123         LOG(ERROR) << "Fail to set mode, Hint: " << power_hint;
124         power_hal_ext_aidl_ = nullptr;
125         power_hal_aidl_ = nullptr;
126         lock_.unlock();
127         if (!error_on_exit) {
128             setMode(type, t, enable, true);
129         }
130         return;
131     }
132     lock_.unlock();
133 }
134 
135 }  // namespace implementation
136 }  // namespace V2_0
137 }  // namespace thermal
138 }  // namespace hardware
139 }  // namespace android
140