1 /*
2 * Copyright (c) 2021 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 #include "thermal_timer.h"
17
18 #include "thermal_common.h"
19
20 using namespace OHOS::MiscServices;
21 namespace OHOS {
22 namespace PowerMgr {
ThermalTimerInfo()23 ThermalTimerInfo::ThermalTimerInfo()
24 {
25 }
26
~ThermalTimerInfo()27 ThermalTimerInfo::~ThermalTimerInfo()
28 {
29 }
30
OnTrigger()31 void ThermalTimerInfo::OnTrigger()
32 {
33 THERMAL_HILOGD(COMP_SVC, "Enter");
34 if (callback_ != nullptr) {
35 THERMAL_HILOGI(COMP_SVC, "call back");
36 callback_();
37 }
38 THERMAL_HILOGD(COMP_SVC, "Exit");
39 }
40
SetCallbackInfo(const TimerInfoCallback & callBack)41 void ThermalTimerInfo::SetCallbackInfo(const TimerInfoCallback &callBack)
42 {
43 callback_ = callBack;
44 }
45
SetType(const int & _type)46 void ThermalTimerInfo::SetType(const int &_type)
47 {
48 type = _type;
49 }
50
SetRepeat(bool _repeat)51 void ThermalTimerInfo::SetRepeat(bool _repeat)
52 {
53 repeat = _repeat;
54 }
55
SetInterval(const uint64_t & _interval)56 void ThermalTimerInfo::SetInterval(const uint64_t &_interval)
57 {
58 interval = _interval;
59 }
60
SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent)61 void ThermalTimerInfo::SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent)
62 {
63 wantAgent = _wantAgent;
64 }
65
CreateTimer(std::shared_ptr<ThermalTimerInfo> & timerInfo)66 uint64_t ThermalTimer::CreateTimer(std::shared_ptr<ThermalTimerInfo>& timerInfo)
67 {
68 uint64_t timerId = 0;
69 timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
70 if (timerId <= 0) {
71 THERMAL_HILOGE(COMP_SVC, "failed to create timer id");
72 return timerId;
73 }
74 return timerId;
75 }
76
StartTimer(uint64_t timerId,uint64_t triggerTime)77 bool ThermalTimer::StartTimer(uint64_t timerId, uint64_t triggerTime)
78 {
79 int delayTime = 6000;
80 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, triggerTime);
81 std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
82 if (!ret) {
83 THERMAL_HILOGE(COMP_SVC, "failed to start timer");
84 return ret;
85 }
86 return true;
87 }
88
StopTimer(uint64_t timerId)89 bool ThermalTimer::StopTimer(uint64_t timerId)
90 {
91 auto ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
92 if (!ret) {
93 THERMAL_HILOGE(COMP_SVC, "failed to stop timer");
94 return ret;
95 }
96 return true;
97 }
98
DestroyTimer(uint64_t timerId)99 bool ThermalTimer::DestroyTimer(uint64_t timerId)
100 {
101 auto ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
102 if (!ret) {
103 THERMAL_HILOGE(COMP_SVC, "failed to destroy timer");
104 return ret;
105 }
106 return true;
107 }
108
Start(int interval,NotifyTask & task)109 void ThermalTimerUtils::Start(int interval, NotifyTask &task)
110 {
111 // is started, do not start again
112 if (expired_ == false)
113 return;
114
115 // start async timer, launch thread and wait in that thread
116 expired_ = false;
117 std::thread([this, interval, task]() {
118 while (!tryToExpire_) {
119 // sleep every interval and do the task again and again until times up
120 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
121 task();
122 }
123
124 {
125 // timer be stopped, update the condition variable expired and wake main thread
126 std::lock_guard<std::mutex> locker(mutex_);
127 expired_ = true;
128 expiredCond_.notify_one();
129 }
130 }).detach();
131 }
132
StartOnce(int delay,NotifyTask & task)133 void ThermalTimerUtils::StartOnce(int delay, NotifyTask &task)
134 {
135 std::thread([delay, task]() {
136 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
137 task();
138 }).detach();
139 }
140
Stop()141 void ThermalTimerUtils::Stop()
142 {
143 // do not stop again
144 if (expired_)
145 return;
146
147 if (tryToExpire_)
148 return;
149
150 // wait until timer
151 tryToExpire_ = true; // change this bool value to make timer while loop stop
152 {
153 std::unique_lock<std::mutex> locker(mutex_);
154 expiredCond_.wait(locker, [this] {
155 return expired_ == true;
156 });
157
158 // reset the timer
159 if (expired_ == true)
160 tryToExpire_ = false;
161 }
162 }
163 } // namespace PowerMgr
164 } // namespace OHOS
165