• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "wifi_settings.h"
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     pthread_setname_np(handleThread, "RunHandleThread");
55     return true;
56 }
57 
StopHandlerThread()58 void Handler::StopHandlerThread()
59 {
60     LOGI("Enter StopHandlerThread");
61     if (isRunning) {
62         isRunning = false;
63         if (pMyQueue != nullptr) {
64             pMyQueue->StopQueueLoop();
65         }
66         if (handleThread != 0) {
67             pthread_join(handleThread, nullptr);
68         }
69     }
70     LOGI("Leave StopHandlerThread");
71     return;
72 }
73 
RunHandleThreadFunc(void * pInstance)74 void *Handler::RunHandleThreadFunc(void *pInstance)
75 {
76     if (pInstance == nullptr) {
77         LOGE("pInstance is null.\n");
78         return nullptr;
79     }
80 
81     LOGI("Run handler func.");
82     Handler *pHandler = (Handler *)pInstance;
83     pHandler->GetAndDistributeMessage();
84 
85     return nullptr;
86 }
87 
GetAndDistributeMessage()88 void Handler::GetAndDistributeMessage()
89 {
90     if (pMyQueue == nullptr) {
91         LOGE("pMyQueue is null.\n");
92         return;
93     }
94 
95     while (isRunning) {
96         InternalMessage *msg = pMyQueue->GetNextMessage();
97         if (msg == nullptr) {
98             LOGE("GetNextMessage null.\n");
99             continue;
100         }
101         LOGD("Handler get message: %{public}d\n", msg->GetMessageName());
102         WifiSettings::GetInstance().SetThreadStatusFlag(true);
103         DistributeMessage(msg);
104         MessageManage::GetInstance().ReclaimMsg(msg);
105         WifiSettings::GetInstance().SetThreadStatusFlag(false);
106     }
107 
108     return;
109 }
110 
SendMessage(InternalMessage * msg)111 void Handler::SendMessage(InternalMessage *msg)
112 {
113     if (msg == nullptr) {
114         LOGE("Handler::SendMessage: msg is null.");
115         return;
116     }
117 
118     LOGD("Handler::SendMessage msg:%{public}d", msg->GetMessageName());
119     MessageExecutedLater(msg, 0);
120     return;
121 }
122 
MessageExecutedLater(InternalMessage * msg,int64_t delayTimeMs)123 void Handler::MessageExecutedLater(InternalMessage *msg, int64_t delayTimeMs)
124 {
125     if (msg == nullptr) {
126         LOGE("Handler::MessageExecutedLater: msg is null.");
127         return;
128     }
129 
130     LOGD("Handler::MessageExecutedLater msg:%{public}d", msg->GetMessageName());
131     int64_t delayTime = delayTimeMs;
132     if (delayTime < 0) {
133         delayTime = 0;
134     }
135 
136     /* Obtains the current time, accurate to milliseconds. */
137     struct timeval curTime = {0, 0};
138     if (gettimeofday(&curTime, nullptr) != 0) {
139         LOGE("gettimeofday failed.\n");
140         MessageManage::GetInstance().ReclaimMsg(msg);
141         return;
142     }
143     int64_t nowTime = static_cast<int64_t>(curTime.tv_sec) * USEC_1000 + curTime.tv_usec / USEC_1000;
144 
145     MessageExecutedAtTime(msg, nowTime + delayTime);
146     return;
147 }
148 
MessageExecutedAtTime(InternalMessage * msg,int64_t execTime)149 void Handler::MessageExecutedAtTime(InternalMessage *msg, int64_t execTime)
150 {
151     if (msg == nullptr) {
152         LOGE("Handler::MessageExecutedAtTime: msg is null.");
153         return;
154     }
155 
156     LOGD("Handler::MessageExecutedAtTime msg: %{public}d", msg->GetMessageName());
157     if (pMyQueue == nullptr) {
158         LOGE("pMyQueue is null.\n");
159         MessageManage::GetInstance().ReclaimMsg(msg);
160         return;
161     }
162 
163     if (pMyQueue->AddMessageToQueue(msg, execTime) != true) {
164         LOGE("AddMessageToQueue failed.\n");
165         return;
166     }
167 
168     return;
169 }
170 
PlaceMessageTopOfQueue(InternalMessage * msg)171 void Handler::PlaceMessageTopOfQueue(InternalMessage *msg)
172 {
173     if (msg == nullptr) {
174         LOGE("Handler::PlaceMessageTopOfQueue: msg is null.");
175         return;
176     }
177 
178     LOGD("Handler::PlaceMessageTopOfQueue msg: %{public}d", msg->GetMessageName());
179     if (pMyQueue == nullptr) {
180         LOGE("pMyQueue is null.\n");
181         MessageManage::GetInstance().ReclaimMsg(msg);
182         return;
183     }
184 
185     if (!pMyQueue->AddMessageToQueue(msg, 0)) {
186         LOGE("AddMessageToQueue failed.\n");
187         return;
188     }
189 
190     return;
191 }
192 
DeleteMessageFromQueue(int messageName)193 void Handler::DeleteMessageFromQueue(int messageName)
194 {
195     LOGD("Handler::DeleteMessageFromQueue msg is: %{public}d", messageName);
196     if (pMyQueue == nullptr) {
197         LOGE("pMyQueue is null.\n");
198         return;
199     }
200 
201     if (!pMyQueue->DeleteMessageFromQueue(messageName)) {
202         LOGE("DeleteMessageFromQueue failed.\n");
203         return;
204     }
205 
206     return;
207 }
208 
DistributeMessage(InternalMessage * msg)209 void Handler::DistributeMessage(InternalMessage *msg)
210 {
211     if (msg == nullptr) {
212         return;
213     }
214     ExecuteMessage(msg);
215     return;
216 }
217 }  // namespace Wifi
218 }  // namespace OHOS