• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ITHERMAL_ACTION_H
17 #define ITHERMAL_ACTION_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "constants.h"
24 #include "thermal_common.h"
25 #include "thermal_timer.h"
26 
27 namespace OHOS {
28 namespace PowerMgr {
29 
30 struct PolicyDelayAction {
31     bool isCompleted;
32     int32_t intDelayValue;
33     uint32_t uintDelayValue;
34     float floatDelayValue;
35     uint32_t delayTime;
36     uint64_t delayTimerId;
37 };
38 
39 class IThermalAction {
40 public:
41     IThermalAction() = default;
42     virtual ~IThermalAction() = default;
43 
44     virtual void InitParams(const std::string& params) = 0;
45     virtual void SetStrict(bool enable) = 0;
46     virtual void SetEnableEvent(bool enable) = 0;
47     virtual void AddActionValue(uint32_t actionId, std::string value) = 0;
48     virtual void ResetActionValue() = 0;
49 
AddActionValue(std::string value)50     virtual void AddActionValue(std::string value)
51     {
52         AddActionValue(0, value);
53     }
54 
AddActionDelayTime(uint32_t actionId,const PolicyDelayAction & delayTime)55     virtual void AddActionDelayTime(uint32_t actionId, const PolicyDelayAction& delayTime)
56     {
57         if (policyActionMap_.count(actionId) == 0) {
58             policyActionMap_.insert(std::make_pair(actionId, delayTime));
59         }
60         actionIdList_.insert(actionId);
61     }
62 
Execute()63     virtual void Execute()
64     {
65         auto thermalTimer = std::make_shared<ThermalTimer>();
66         for (auto it = policyActionMap_.begin(); it != policyActionMap_.end();) {
67             if (actionIdList_.count(it->first) == 0) {
68                 if (!it->second.isCompleted) {
69                     thermalTimer->StopTimer(it->second.delayTimerId);
70                     thermalTimer->DestroyTimer(it->second.delayTimerId);
71                 }
72                 it = policyActionMap_.erase(it);
73             } else {
74                 ++it;
75             }
76         }
77         actionIdList_.clear();
78         int64_t curMsecTimestam = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs();
79 
80         for (auto &policyAction : policyActionMap_) {
81             if (policyAction.second.isCompleted || policyAction.second.delayTimerId > 0) {
82                 continue;
83             }
84             auto timerInfo = std::make_shared<ThermalTimerInfo>();
85             timerInfo->SetType(ThermalTimer::TIMER_TYPE_WAKEUP | ThermalTimer::TIMER_TYPE_EXACT);
86             auto callback = [this, &policyAction]() {
87                 policyAction.second.isCompleted = true;
88                 ExecuteInner();
89                 auto thermalTimer = std::make_shared<ThermalTimer>();
90                 thermalTimer->StopTimer(policyAction.second.delayTimerId);
91                 thermalTimer->DestroyTimer(policyAction.second.delayTimerId);
92             };
93             timerInfo->SetCallbackInfo(callback);
94             policyAction.second.delayTimerId = thermalTimer->CreateTimer(timerInfo);
95             THERMAL_HILOGI(COMP_SVC, "%{public}s startTimer, delayTime = %{public}d,", actionName_.c_str(),
96                 policyAction.second.delayTime);
97             thermalTimer->StartTimer(policyAction.second.delayTimerId,
98                 static_cast<uint64_t>(policyAction.second.delayTime + curMsecTimestam));
99         }
100         ExecuteInner();
101     }
102 
103     virtual void ExecuteInner() = 0;
104 protected:
105     bool isStrict_ {true};
106     bool enableEvent_ {false};
107     std::string actionName_ = "";
108     std::unordered_map<uint32_t, PolicyDelayAction> policyActionMap_;
109     std::unordered_set<uint32_t> actionIdList_;
110 };
111 } // namespace PowerMgr
112 } // namespace OHOS
113 #endif // ITHERMAL_ACTION_H
114