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 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, triggerTime);
80 if (!ret) {
81 THERMAL_HILOGE(COMP_SVC, "failed to start timer");
82 return ret;
83 }
84 return true;
85 }
86
StopTimer(uint64_t timerId)87 bool ThermalTimer::StopTimer(uint64_t timerId)
88 {
89 auto ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
90 if (!ret) {
91 THERMAL_HILOGE(COMP_SVC, "failed to stop timer");
92 return ret;
93 }
94 return true;
95 }
96
DestroyTimer(uint64_t timerId)97 bool ThermalTimer::DestroyTimer(uint64_t timerId)
98 {
99 auto ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
100 if (!ret) {
101 THERMAL_HILOGE(COMP_SVC, "failed to destroy timer");
102 return ret;
103 }
104 return true;
105 }
106
Start(int interval,NotifyTask & task)107 void ThermalTimerUtils::Start(int interval, NotifyTask &task)
108 {
109 // is started, do not start again
110 if (expired_ == false)
111 return;
112
113 // start async timer, launch thread and wait in that thread
114 expired_ = false;
115 std::thread([this, interval, task]() {
116 while (!tryToExpire_) {
117 // sleep every interval and do the task again and again until times up
118 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
119 task();
120 }
121
122 {
123 // timer be stopped, update the condition variable expired and wake main thread
124 std::lock_guard<std::mutex> locker(mutex_);
125 expired_ = true;
126 expiredCond_.notify_one();
127 }
128 }).detach();
129 }
130
StartOnce(int delay,NotifyTask & task)131 void ThermalTimerUtils::StartOnce(int delay, NotifyTask &task)
132 {
133 std::thread([delay, task]() {
134 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
135 task();
136 }).detach();
137 }
138
Stop()139 void ThermalTimerUtils::Stop()
140 {
141 // do not stop again
142 if (expired_)
143 return;
144
145 if (tryToExpire_)
146 return;
147
148 // wait until timer
149 tryToExpire_ = true; // change this bool value to make timer while loop stop
150 {
151 std::unique_lock<std::mutex> locker(mutex_);
152 expiredCond_.wait(locker, [this] {
153 return expired_ == true;
154 });
155
156 // reset the timer
157 if (expired_ == true)
158 tryToExpire_ = false;
159 }
160 }
161 } // namespace PowerMgr
162 } // namespace OHOS
163