1 /*
2 * Copyright (c) 2022 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 #include "conditions/timer_listener.h"
16
17 #include "work_queue_event_handler.h"
18 #include "work_sched_hilog.h"
19 #include "conditions/timer_info.h"
20 #include <cinttypes>
21
22 namespace OHOS {
23 namespace WorkScheduler {
TimerListener(std::shared_ptr<WorkQueueManager> workQueueManager,const std::shared_ptr<AppExecFwk::EventRunner> & runner)24 TimerListener::TimerListener(std::shared_ptr<WorkQueueManager> workQueueManager,
25 const std::shared_ptr<AppExecFwk::EventRunner>& runner)
26 {
27 workQueueManager_ = workQueueManager;
28 }
29
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)30 void TimerListener::OnConditionChanged(WorkCondition::Type conditionType,
31 std::shared_ptr<DetectorValue> conditionVal)
32 {
33 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
34 }
35
Start()36 bool TimerListener::Start()
37 {
38 if (workQueueManager_ == nullptr) {
39 WS_HILOGE("workQueueManager_ is null");
40 return false;
41 }
42 uint32_t time = workQueueManager_->GetTimeCycle();
43 WS_HILOGI("TimerListener start with time = %{public}u.", time);
44 auto task = [=]() {
45 WS_HILOGI("begin check repeat work");
46 workQueueManager_->OnConditionChanged(WorkCondition::Type::TIMER, std::make_shared<
47 DetectorValue>(0, 0, 0, std::string()));
48 };
49 auto timerInfo = std::make_shared<TimerInfo>();
50 uint8_t type = static_cast<uint8_t>(timerInfo->TIMER_TYPE_EXACT) |
51 static_cast<uint8_t>(timerInfo->TIMER_TYPE_REALTIME);
52 timerInfo->SetType(static_cast<int>(type));
53 timerInfo->SetRepeat(true);
54 timerInfo->SetInterval(time);
55 timerInfo->SetCallbackInfo(task);
56 timerId_ = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
57 if (timerId_ == 0) {
58 WS_HILOGE("TimerListener CreateTimer failed");
59 return false;
60 }
61 bool res = TimeServiceClient::GetInstance()->StartTimer(timerId_,
62 TimeServiceClient::GetInstance()->GetBootTimeMs() + time);
63 WS_HILOGI("res is %{public}d, timerId = %{public}" PRIu64, res, timerId_);
64 return true;
65 }
66
Stop()67 bool TimerListener::Stop()
68 {
69 WS_HILOGI("TimerListener stop");
70 if (timerId_ > 0) {
71 MiscServices::TimeServiceClient::GetInstance()->StopTimer(timerId_);
72 MiscServices::TimeServiceClient::GetInstance()->DestroyTimer(timerId_);
73 timerId_ = 0;
74 }
75 return true;
76 }
77 } // namespace WorkScheduler
78 } // namespace OHOS