• 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 #pragma once
18 
19 #include <aidl/android/hardware/power/IPower.h>
20 #include <aidl/android/hardware/thermal/IThermal.h>
21 #include <aidl/android/hardware/thermal/ThrottlingSeverity.h>
22 #include <aidl/google/hardware/power/extension/pixel/IPowerExt.h>
23 #include <utils/Trace.h>
24 
25 #include <queue>
26 #include <shared_mutex>
27 #include <string>
28 #include <unordered_map>
29 #include <unordered_set>
30 
31 #include "thermal_info.h"
32 
33 namespace aidl {
34 namespace android {
35 namespace hardware {
36 namespace thermal {
37 namespace implementation {
38 
39 using ::aidl::android::hardware::power::IPower;
40 using ::aidl::google::hardware::power::extension::pixel::IPowerExt;
41 
42 using CdevRequestStatus = std::unordered_map<std::string, int>;
43 
44 struct PowerHintstatus {
45     std::unordered_map<ThrottlingSeverity, ThrottlingSeverity> hint_severity_map;
46     ThrottlingSeverity prev_hint_severity;
47 };
48 
49 class PowerHalService {
50   public:
51     PowerHalService();
52     ~PowerHalService() = default;
53     bool connect();
54     void reconnect();
isAidlPowerHalExist()55     bool isAidlPowerHalExist() { return power_hal_aidl_exist_; }
56     bool isModeSupported(const std::string &type, const ThrottlingSeverity &t);
isPowerHalConnected()57     bool isPowerHalConnected() { return power_hal_aidl_ != nullptr; }
isPowerHalExtConnected()58     bool isPowerHalExtConnected() { return power_hal_ext_aidl_ != nullptr; }
59     void setMode(const std::string &type, const ThrottlingSeverity &t, const bool &enable,
60                  const bool error_on_exit = false);
61     void updateSupportedPowerHints(
62             const std::unordered_map<std::string, SensorInfo> &sensor_info_map_);
63     void sendPowerExtHint(const Temperature &t);
64 
65   private:
66     ndk::ScopedAIBinder_DeathRecipient power_hal_ext_aidl_death_recipient_;
onPowerHalExtAidlBinderDied(void * cookie)67     static void onPowerHalExtAidlBinderDied(void *cookie) {
68         if (cookie) {
69             auto *e = static_cast<PowerHalService *>(cookie);
70             {
71                 std::lock_guard<std::mutex> lock(e->lock_);
72                 e->power_hal_aidl_ = nullptr;
73                 e->power_hal_ext_aidl_ = nullptr;
74             }
75             e->reconnect();
76         }
77     }
78 
79     bool power_hal_aidl_exist_;
80     std::shared_ptr<IPower> power_hal_aidl_;
81     std::shared_ptr<IPowerExt> power_hal_ext_aidl_;
82     std::mutex lock_;
83     std::unordered_map<std::string, PowerHintstatus> supported_powerhint_map_;
84     mutable std::shared_mutex powerhint_status_mutex_;
85 };
86 
87 }  // namespace implementation
88 }  // namespace thermal
89 }  // namespace hardware
90 }  // namespace android
91 }  // namespace aidl
92