1 /*
2  * Copyright (C) 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 #include "tel_event_handler.h"
17 
18 #include "tel_event_queue.h"
19 #include "telephony_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Telephony {
23 
Submit(const TelTask & task)24 void TelFFRTUtils::Submit(const TelTask &task)
25 {
26     ffrt::submit(task);
27 }
28 
Submit(TelTask && task)29 void TelFFRTUtils::Submit(TelTask &&task)
30 {
31     ffrt::submit(task);
32 }
33 
SubmitSync(const TelTask & task)34 void TelFFRTUtils::SubmitSync(const TelTask &task)
35 {
36     ffrt::submit(task);
37     ffrt::wait();
38 }
39 
SubmitSync(TelTask && task)40 void TelFFRTUtils::SubmitSync(TelTask &&task)
41 {
42     ffrt::submit(task);
43     ffrt::wait();
44 }
45 
SleepFor(uint32_t timeoutMs)46 void TelFFRTUtils::SleepFor(uint32_t timeoutMs)
47 {
48     ffrt::this_task::sleep_for(std::chrono::milliseconds(timeoutMs));
49 }
50 
TelEventHandler(const std::string & name)51 TelEventHandler::TelEventHandler(const std::string &name)
52 {
53     queue_ = std::make_shared<TelEventQueue>(name);
54 }
55 
SendEvent(AppExecFwk::InnerEvent::Pointer & event,int64_t delayTime,Priority priority)56 bool TelEventHandler::SendEvent(AppExecFwk::InnerEvent::Pointer &event, int64_t delayTime, Priority priority)
57 {
58     if (!event) {
59         TELEPHONY_LOGE("Could not send an invalid event");
60         return false;
61     }
62 
63     AppExecFwk::InnerEvent::TimePoint now = AppExecFwk::InnerEvent::Clock::now();
64     if (delayTime > 0) {
65         event->SetHandleTime(now + std::chrono::milliseconds(delayTime));
66     } else {
67         event->SetHandleTime(now);
68     }
69     event->SetOwner(shared_from_this());
70     if (!queue_) {
71         TELEPHONY_LOGE("queue is nullptr");
72         return false;
73     }
74     queue_->Submit(event, priority);
75     return true;
76 }
77 
SendExecuteNowEvent(AppExecFwk::InnerEvent::Pointer & event)78 bool TelEventHandler::SendExecuteNowEvent(AppExecFwk::InnerEvent::Pointer &event)
79 {
80     if (!event) {
81         TELEPHONY_LOGE("Could not send an invalid event");
82         return false;
83     }
84     event->SetHandleTime(AppExecFwk::InnerEvent::TimePoint::min());
85     event->SetOwner(shared_from_this());
86     if (!queue_) {
87         TELEPHONY_LOGE("queue is nullptr");
88         return false;
89     }
90     queue_->Submit(event, Priority::IMMEDIATE);
91     return true;
92 }
93 
RemoveEvent(uint32_t innerEventId)94 void TelEventHandler::RemoveEvent(uint32_t innerEventId)
95 {
96     if (!queue_) {
97         TELEPHONY_LOGE("queue is nullptr");
98         return;
99     }
100     queue_->RemoveEvent(innerEventId);
101 }
102 
HasInnerEvent(uint32_t innerEventId)103 bool TelEventHandler::HasInnerEvent(uint32_t innerEventId)
104 {
105     if (!queue_) {
106         TELEPHONY_LOGE("queue is nullptr");
107         return false;
108     }
109     return queue_->HasInnerEvent(innerEventId);
110 }
111 
RemoveAllEvents()112 void TelEventHandler::RemoveAllEvents()
113 {
114     if (!queue_) {
115         TELEPHONY_LOGE("queue is nullptr");
116         return;
117     }
118     queue_->RemoveAllEvents();
119 }
120 
ClearFfrt(bool isNeedEnd)121 void TelEventHandler::ClearFfrt(bool isNeedEnd)
122 {
123     queue_->ClearCurrentTask(isNeedEnd);
124 }
125 } // namespace Telephony
126 } // namespace OHOS