1 /*
2 * Copyright (C) 2021-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 "sta_monitor.h"
16 #include "wifi_idl_define.h"
17 #include "sta_define.h"
18 #include "wifi_logger.h"
19 #include "wifi_supplicant_hal_interface.h"
20 #include "wifi_sta_hal_interface.h"
21 #include "wifi_common_util.h"
22
23 DEFINE_WIFILOG_LABEL("StaMonitor");
24
25 namespace OHOS {
26 namespace Wifi {
StaMonitor()27 StaMonitor::StaMonitor() : pStaStateMachine(nullptr)
28 {}
29
~StaMonitor()30 StaMonitor::~StaMonitor()
31 {
32 WIFI_LOGI("StaMonitor::~StaMonitor");
33 }
34
InitStaMonitor()35 ErrCode StaMonitor::InitStaMonitor()
36 {
37 WIFI_LOGI("Enter StaMonitor::InitStaMonitor.\n");
38 using namespace std::placeholders;
39 WifiEventCallback callBack = {
40 std::bind(&StaMonitor::OnConnectChangedCallBack, this, _1, _2, _3),
41 std::bind(&StaMonitor::OnBssidChangedCallBack, this, _1, _2),
42 std::bind(&StaMonitor::OnWpaStateChangedCallBack, this, _1),
43 std::bind(&StaMonitor::OnWpaSsidWrongKeyCallBack, this, _1),
44 std::bind(&StaMonitor::OnWpsPbcOverlapCallBack, this, _1),
45 std::bind(&StaMonitor::OnWpsTimeOutCallBack, this, _1),
46 std::bind(&StaMonitor::onWpaConnectionFullCallBack, this, _1),
47 std::bind(&StaMonitor::onWpaConnectionRejectCallBack, this, _1)
48 };
49
50 if (WifiStaHalInterface::GetInstance().RegisterStaEventCallback(callBack) != WIFI_IDL_OPT_OK) {
51 WIFI_LOGE("StaMonitor::InitStaMonitor RegisterStaEventCallback failed!");
52 return WIFI_OPT_FAILED;
53 }
54 return WIFI_OPT_SUCCESS;
55 }
56
UnInitStaMonitor() const57 ErrCode StaMonitor::UnInitStaMonitor() const
58 {
59 WIFI_LOGI("Enter StaMonitor::UnInitStaMonitor.\n");
60 WifiEventCallback callBack;
61 if (WifiStaHalInterface::GetInstance().RegisterStaEventCallback(callBack) != WIFI_IDL_OPT_OK) {
62 WIFI_LOGE("StaMonitor::~StaMonitor RegisterStaEventCallback failed!");
63 return WIFI_OPT_FAILED;
64 }
65 return WIFI_OPT_SUCCESS;
66 }
67
SetStateMachine(StaStateMachine * paraStaStateMachine)68 void StaMonitor::SetStateMachine(StaStateMachine *paraStaStateMachine)
69 {
70 if (paraStaStateMachine == nullptr) {
71 WIFI_LOGE("The statemachine pointer is null.");
72 return;
73 }
74 pStaStateMachine = paraStaStateMachine;
75 return;
76 }
77
OnConnectChangedCallBack(int status,int networkId,const std::string & bssid)78 void StaMonitor::OnConnectChangedCallBack(int status, int networkId, const std::string &bssid)
79 {
80 WIFI_LOGI("OnConnectChangedCallBack() status:%{public}d,networkId=%{public}d,bssid=%{public}s",
81 status,
82 networkId,
83 MacAnonymize(bssid).c_str());
84 if (pStaStateMachine == nullptr) {
85 WIFI_LOGE("The statemachine pointer is null.");
86 return;
87 }
88
89 WifiLinkedInfo linkedInfo;
90 pStaStateMachine->GetLinkedInfo(linkedInfo);
91 /* P2P affects STA, causing problems or incorrect data updates */
92 if ((linkedInfo.connState == ConnState::CONNECTED) &&
93 (linkedInfo.bssid != bssid) && (!pStaStateMachine->IsRoaming())) {
94 WIFI_LOGI("Sta ignored the event for bssid is mismatch, isRoam:%{public}d.",
95 pStaStateMachine->IsRoaming());
96 return;
97 }
98 switch (status) {
99 case WPA_CB_CONNECTED: {
100 pStaStateMachine->OnNetworkConnectionEvent(networkId, bssid);
101 break;
102 }
103 case WPA_CB_DISCONNECTED: {
104 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_NETWORK_DISCONNECTION_EVENT);
105 break;
106 }
107 default:
108 break;
109 }
110 }
111
OnBssidChangedCallBack(const std::string & reason,const std::string & bssid)112 void StaMonitor::OnBssidChangedCallBack(const std::string &reason, const std::string &bssid)
113 {
114 WIFI_LOGI("OnBssidChangedCallBack() reason:%{public}s,bssid=%{public}s",
115 reason.c_str(),
116 MacAnonymize(bssid).c_str());
117 if (pStaStateMachine == nullptr) {
118 WIFI_LOGE("The statemachine pointer is null.");
119 return;
120 }
121
122 WifiLinkedInfo linkedInfo;
123 pStaStateMachine->GetLinkedInfo(linkedInfo);
124 if (linkedInfo.connState != ConnState::CONNECTED) {
125 WIFI_LOGW("Sta ignored the event for NOT in connected status!, connState: %{public}d",
126 linkedInfo.connState);
127 return;
128 }
129 if (linkedInfo.bssid == bssid) {
130 WIFI_LOGW("Sta ignored the event for bssid is the same.");
131 return;
132 }
133 pStaStateMachine->OnBssidChangedEvent(reason, bssid);
134 }
135
OnWpaStateChangedCallBack(int status)136 void StaMonitor::OnWpaStateChangedCallBack(int status)
137 {
138 WIFI_LOGI("OnWpaStateChangedCallBack() status:%{public}d\n", status);
139 if (pStaStateMachine == nullptr) {
140 WIFI_LOGE("The statemachine pointer is null.");
141 return;
142 }
143 /* Notification state machine wpa state changed event. */
144 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_WPA_STATE_CHANGE_EVENT, status);
145 }
146
OnWpaSsidWrongKeyCallBack(int status)147 void StaMonitor::OnWpaSsidWrongKeyCallBack(int status)
148 {
149 WIFI_LOGI("OnWpaSsidWrongKeyCallBack() status:%{public}d\n", status);
150 if (pStaStateMachine == nullptr) {
151 WIFI_LOGE("The statemachine pointer is null.");
152 return;
153 }
154
155 if (status != 1) {
156 WIFI_LOGE("OnWpaSsidWrongKeyCallBack error.");
157 return;
158 }
159 /* Notification state machine wpa password wrong event. */
160 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_WPA_PASSWD_WRONG_EVENT);
161 }
162
onWpaConnectionFullCallBack(int status)163 void StaMonitor::onWpaConnectionFullCallBack(int status)
164 {
165 LOGI("onWpaConnectionFullCallBack() status:%d.\n", status);
166 if (pStaStateMachine == nullptr) {
167 WIFI_LOGE("The statemachine pointer is null.");
168 return;
169 }
170
171 /* Notification state machine wpa password wrong event. */
172 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_WPA_FULL_CONNECT_EVENT);
173 }
174
onWpaConnectionRejectCallBack(int status)175 void StaMonitor::onWpaConnectionRejectCallBack(int status)
176 {
177 LOGI("onWpsConnectionRejectCallBack() status:%d.\n", status);
178 if (pStaStateMachine == nullptr) {
179 WIFI_LOGE("The statemachine pointer is null.");
180 return;
181 }
182
183 /* Notification state machine wpa password wrong event. */
184 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_WPA_ASSOC_REJECT_EVENT);
185 }
186
OnWpsPbcOverlapCallBack(int status)187 void StaMonitor::OnWpsPbcOverlapCallBack(int status)
188 {
189 WIFI_LOGI("OnWpsPbcOverlapCallBack() status:%{public}d\n", status);
190 if (pStaStateMachine == nullptr) {
191 WIFI_LOGE("The statemachine pointer is null.");
192 return;
193 }
194 /* Notification state machine WPS overlap event. */
195 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_WPS_OVERLAP_EVENT);
196 }
197
OnWpsTimeOutCallBack(int status)198 void StaMonitor::OnWpsTimeOutCallBack(int status)
199 {
200 WIFI_LOGI("OnWpsTimeOutCallBack() status:%{public}d\n", status);
201 if (pStaStateMachine == nullptr) {
202 WIFI_LOGE("The statemachine pointer is null.");
203 return;
204 }
205 /* Notification state machine WPS timeout event */
206 pStaStateMachine->SendMessage(WIFI_SVR_CMD_STA_WPS_TIMEOUT_EVNET);
207 }
208 } // namespace Wifi
209 } // namespace OHOS