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 #include "handler.h"
16 #include <iostream>
17 #include <sys/time.h>
18 #include "wifi_log.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "OHWIFI_HANDLER"
22
23 namespace OHOS {
24 namespace Wifi {
Handler()25 Handler::Handler() : pMyQueue(nullptr), handleThread(0), isRunning(true)
26 {}
27
~Handler()28 Handler::~Handler()
29 {
30 LOGI("Handler::~Handler");
31 StopHandlerThread();
32 return;
33 }
34
InitialHandler()35 bool Handler::InitialHandler()
36 {
37 if (handleThread != 0) {
38 return true;
39 }
40 if (pMyQueue == nullptr) {
41 pMyQueue = std::make_unique<MessageQueue>();
42 if (pMyQueue == nullptr) {
43 LOGE("pMyQueue alloc failed.\n");
44 return false;
45 }
46 }
47
48 int ret = pthread_create(&handleThread, nullptr, RunHandleThreadFunc, this);
49 if (ret != 0) {
50 LOGE("pthread_create failed.\n");
51 return false;
52 }
53 LOGI("pthread_create ret: %{public}d\n", ret);
54 return true;
55 }
56
StopHandlerThread()57 void Handler::StopHandlerThread()
58 {
59 LOGI("Enter StopHandlerThread");
60 if (isRunning) {
61 isRunning = false;
62 if (pMyQueue != nullptr) {
63 pMyQueue->StopQueueLoop();
64 }
65 if (handleThread != 0) {
66 pthread_join(handleThread, nullptr);
67 }
68 }
69 LOGI("Leave StopHandlerThread");
70 return;
71 }
72
RunHandleThreadFunc(void * pInstance)73 void *Handler::RunHandleThreadFunc(void *pInstance)
74 {
75 if (pInstance == nullptr) {
76 LOGE("pInstance is null.\n");
77 return nullptr;
78 }
79
80 LOGI("Run handler func.");
81 Handler *pHandler = (Handler *)pInstance;
82 pHandler->GetAndDistributeMessage();
83
84 return nullptr;
85 }
86
GetAndDistributeMessage()87 void Handler::GetAndDistributeMessage()
88 {
89 if (pMyQueue == nullptr) {
90 LOGE("pMyQueue is null.\n");
91 return;
92 }
93
94 while (isRunning) {
95 InternalMessage *msg = pMyQueue->GetNextMessage();
96 if (msg == nullptr) {
97 LOGE("GetNextMessage null.\n");
98 continue;
99 }
100 LOGI("Handler get message: %{public}d\n", msg->GetMessageName());
101 DistributeMessage(msg);
102 MessageManage::GetInstance().ReclaimMsg(msg);
103 }
104
105 return;
106 }
107
SendMessage(InternalMessage * msg)108 void Handler::SendMessage(InternalMessage *msg)
109 {
110 if (msg == nullptr) {
111 LOGE("Handler::SendMessage: msg is null.");
112 return;
113 }
114
115 LOGD("Handler::SendMessage msg:%{public}d", msg->GetMessageName());
116 MessageExecutedLater(msg, 0);
117 return;
118 }
119
MessageExecutedLater(InternalMessage * msg,int64_t delayTimeMs)120 void Handler::MessageExecutedLater(InternalMessage *msg, int64_t delayTimeMs)
121 {
122 if (msg == nullptr) {
123 LOGE("Handler::MessageExecutedLater: msg is null.");
124 return;
125 }
126
127 LOGD("Handler::MessageExecutedLater msg:%{public}d", msg->GetMessageName());
128 int64_t delayTime = delayTimeMs;
129 if (delayTime < 0) {
130 delayTime = 0;
131 }
132
133 /* Obtains the current time, accurate to milliseconds. */
134 struct timeval curTime = {0, 0};
135 if (gettimeofday(&curTime, nullptr) != 0) {
136 LOGE("gettimeofday failed.\n");
137 MessageManage::GetInstance().ReclaimMsg(msg);
138 return;
139 }
140 int64_t nowTime = static_cast<int64_t>(curTime.tv_sec) * USEC_1000 + curTime.tv_usec / USEC_1000;
141
142 MessageExecutedAtTime(msg, nowTime + delayTime);
143 return;
144 }
145
MessageExecutedAtTime(InternalMessage * msg,int64_t execTime)146 void Handler::MessageExecutedAtTime(InternalMessage *msg, int64_t execTime)
147 {
148 if (msg == nullptr) {
149 LOGE("Handler::MessageExecutedAtTime: msg is null.");
150 return;
151 }
152
153 LOGD("Handler::MessageExecutedAtTime msg: %{public}d", msg->GetMessageName());
154 if (pMyQueue == nullptr) {
155 LOGE("pMyQueue is null.\n");
156 MessageManage::GetInstance().ReclaimMsg(msg);
157 return;
158 }
159
160 if (pMyQueue->AddMessageToQueue(msg, execTime) != true) {
161 LOGE("AddMessageToQueue failed.\n");
162 return;
163 }
164
165 return;
166 }
167
PlaceMessageTopOfQueue(InternalMessage * msg)168 void Handler::PlaceMessageTopOfQueue(InternalMessage *msg)
169 {
170 if (msg == nullptr) {
171 LOGE("Handler::PlaceMessageTopOfQueue: msg is null.");
172 return;
173 }
174
175 LOGD("Handler::PlaceMessageTopOfQueue msg: %{public}d", msg->GetMessageName());
176 if (pMyQueue == nullptr) {
177 LOGE("pMyQueue is null.\n");
178 MessageManage::GetInstance().ReclaimMsg(msg);
179 return;
180 }
181
182 if (!pMyQueue->AddMessageToQueue(msg, 0)) {
183 LOGE("AddMessageToQueue failed.\n");
184 return;
185 }
186
187 return;
188 }
189
DeleteMessageFromQueue(int messageName)190 void Handler::DeleteMessageFromQueue(int messageName)
191 {
192 LOGD("Handler::DeleteMessageFromQueue msg is: %{public}d", messageName);
193 if (pMyQueue == nullptr) {
194 LOGE("pMyQueue is null.\n");
195 return;
196 }
197
198 if (!pMyQueue->DeleteMessageFromQueue(messageName)) {
199 LOGE("DeleteMessageFromQueue failed.\n");
200 return;
201 }
202
203 return;
204 }
205
DistributeMessage(InternalMessage * msg)206 void Handler::DistributeMessage(InternalMessage *msg)
207 {
208 if (msg == nullptr) {
209 return;
210 }
211 ExecuteMessage(msg);
212 return;
213 }
214 } // namespace Wifi
215 } // namespace OHOS