• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "kits/c/wifi_event.h"
17 #include <set>
18 #include <vector>
19 #include "kits/c/wifi_device.h"
20 #include "kits/c/wifi_scan_info.h"
21 #include "i_wifi_device_callback.h"
22 #include "i_wifi_hotspot_callback.h"
23 #include "i_wifi_scan_callback.h"
24 #include "wifi_device.h"
25 #include "wifi_hotspot.h"
26 #include "wifi_logger.h"
27 #include "wifi_scan.h"
28 #include "wifi_p2p.h"
29 #include "wifi_common_util.h"
30 #include "wifi_sa_event.h"
31 DEFINE_WIFILOG_LABEL("WifiCEvent");
32 std::shared_ptr<OHOS::Wifi::WifiDevice> g_wifiStaPtr = OHOS::Wifi::WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
33 std::shared_ptr<OHOS::Wifi::WifiScan> g_wifiScanPtr = OHOS::Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
34 std::shared_ptr<OHOS::Wifi::WifiP2p> g_wifiP2pPtr = OHOS::Wifi::WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
35 std::shared_ptr<OHOS::Wifi::WifiHotspot> g_wifiHotspotPtr =
36     OHOS::Wifi::WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
37 
38 std::vector<std::string> WifiCDeviceEventCallback::deviceCallbackEvent = {
39     EVENT_STA_CONN_STATE_CHANGE,
40     EVENT_STA_DEVICE_CONFIG_CHANGE,
41 };
42 
OnWifiStateChanged(int state)43 void WifiCDeviceEventCallback::OnWifiStateChanged(int state)
44 {
45     WIFI_LOGI("sta received state changed event: %{public}d", state);
46 }
47 
ConvertedLinkedInfo(const OHOS::Wifi::WifiLinkedInfo & linkedInfo,WifiLinkedInfo * dstInfo)48 static OHOS::Wifi::ErrCode ConvertedLinkedInfo(const OHOS::Wifi::WifiLinkedInfo& linkedInfo, WifiLinkedInfo *dstInfo)
49 {
50     if (dstInfo == nullptr) {
51         WIFI_LOGE("Error: the ptr is null!");
52         return OHOS::Wifi::WIFI_OPT_INVALID_PARAM;
53     }
54 
55     if (memcpy_s(dstInfo->ssid, WIFI_MAX_SSID_LEN, linkedInfo.ssid.c_str(), linkedInfo.ssid.size() + 1) != EOK) {
56         return OHOS::Wifi::WIFI_OPT_FAILED;
57     }
58     if (OHOS::Wifi::MacStrToArray(linkedInfo.bssid, dstInfo->bssid) != EOK) {
59         WIFI_LOGE("linked info convert bssid error!");
60         return OHOS::Wifi::WIFI_OPT_FAILED;
61     }
62     dstInfo->rssi = linkedInfo.rssi;
63     dstInfo->band = linkedInfo.band;
64     dstInfo->frequency = linkedInfo.frequency;
65     dstInfo->connState = linkedInfo.connState == OHOS::Wifi::ConnState::CONNECTED ? WIFI_CONNECTED : WIFI_DISCONNECTED;
66     /* disconnectedReason not support */
67     dstInfo->ipAddress = linkedInfo.ipAddress;
68     dstInfo->wifiStandard = linkedInfo.wifiStandard;
69     dstInfo->maxSupportedRxLinkSpeed = linkedInfo.maxSupportedRxLinkSpeed;
70     dstInfo->maxSupportedTxLinkSpeed = linkedInfo.maxSupportedTxLinkSpeed;
71     dstInfo->rxLinkSpeed = linkedInfo.rxLinkSpeed;
72     dstInfo->txLinkSpeed = linkedInfo.txLinkSpeed;
73     return OHOS::Wifi::WIFI_OPT_SUCCESS;
74 }
75 
OnWifiConnectionChanged(int state,const OHOS::Wifi::WifiLinkedInfo & info)76 NO_SANITIZE("cfi") void WifiCDeviceEventCallback::OnWifiConnectionChanged(int state,
77     const OHOS::Wifi::WifiLinkedInfo &info)
78 {
79     WIFI_LOGI("sta connection changed event: %{public}d", state);
80     WifiLinkedInfo linkInfo;
81     OHOS::Wifi::ErrCode retValue = ConvertedLinkedInfo(info, &linkInfo);
82     if (retValue != OHOS::Wifi::WIFI_OPT_SUCCESS) {
83         WIFI_LOGE("Get linked info from cpp error!");
84         return;
85     }
86     std::unique_lock<std::mutex> lock(EventManager::callbackMutex);
87     if (EventManager::g_wifiEvent.OnWifiConnectionChanged) {
88         EventManager::g_wifiEvent.OnWifiConnectionChanged(state, &linkInfo);
89     }
90 }
91 
OnWifiRssiChanged(int rssi)92 void WifiCDeviceEventCallback::OnWifiRssiChanged(int rssi)
93 {
94     WIFI_LOGI("sta received rssi changed event: %{public}d", rssi);
95 }
96 
OnWifiWpsStateChanged(int state,const std::string & pinCode)97 void WifiCDeviceEventCallback::OnWifiWpsStateChanged(int state, const std::string &pinCode)
98 {
99 }
100 
OnStreamChanged(int direction)101 void WifiCDeviceEventCallback::OnStreamChanged(int direction)
102 {
103 }
104 
OnDeviceConfigChanged(OHOS::Wifi::ConfigChange value)105 NO_SANITIZE("cfi") void WifiCDeviceEventCallback::OnDeviceConfigChanged(OHOS::Wifi::ConfigChange value)
106 {
107     WIFI_LOGI("sta received device config changed event: %{public}d", static_cast<int>(value));
108     std::unique_lock<std::mutex> lock(EventManager::callbackMutex);
109     if (EventManager::g_wifiEvent.OnDeviceConfigChange) {
110         EventManager::g_wifiEvent.OnDeviceConfigChange(ConfigChange(static_cast<int>(value)));
111     }
112 }
113 
AsObject()114 OHOS::sptr<OHOS::IRemoteObject> WifiCDeviceEventCallback::AsObject()
115 {
116     return nullptr;
117 }
118 
119 std::vector<std::string> WifiCScanEventCallback::scanCallbackEvent = {
120     EVENT_STA_SCAN_STATE_CHANGE,
121 };
122 
OnWifiScanStateChanged(int state)123 NO_SANITIZE("cfi") void WifiCScanEventCallback::OnWifiScanStateChanged(int state)
124 {
125     WIFI_LOGI("ScanStateChanged event: %{public}d", state);
126     std::unique_lock<std::mutex> lock(EventManager::callbackMutex);
127     if (EventManager::g_wifiEvent.OnWifiScanStateChanged) {
128         EventManager::g_wifiEvent.OnWifiScanStateChanged(state, WIFI_SCAN_HOTSPOT_LIMIT);
129     }
130 }
131 
AsObject()132 OHOS::sptr<OHOS::IRemoteObject> WifiCScanEventCallback::AsObject()
133 {
134     return nullptr;
135 }
136 
137 std::vector<std::string> WifiCHotspotEventCallback::hotspotCallbackEvent = {
138     EVENT_HOTSPOT_STATE_CHANGE,
139     EVENT_HOTSPOT_STA_JOIN,
140     EVENT_HOTSPOT_STA_LEAVE,
141 };
142 
OnHotspotStateChanged(int state)143 NO_SANITIZE("cfi") void WifiCHotspotEventCallback::OnHotspotStateChanged(int state)
144 {
145     WIFI_LOGI("Hotspot received state changed event: %{public}d", state);
146     std::unique_lock<std::mutex> lock(EventManager::callbackMutex);
147     if (EventManager::g_wifiEvent.OnHotspotStateChanged) {
148         EventManager::g_wifiEvent.OnHotspotStateChanged(state);
149     }
150 }
151 
OnHotspotStaJoin(const OHOS::Wifi::StationInfo & info)152 void WifiCHotspotEventCallback::OnHotspotStaJoin(const OHOS::Wifi::StationInfo &info)
153 {
154     WIFI_LOGI("Hotspot received sta join event");
155 }
156 
OnHotspotStaLeave(const OHOS::Wifi::StationInfo & info)157 void WifiCHotspotEventCallback::OnHotspotStaLeave(const OHOS::Wifi::StationInfo &info)
158 {
159     WIFI_LOGI("Hotspot received sta leave event");
160 }
161 
AsObject()162 OHOS::sptr<OHOS::IRemoteObject> WifiCHotspotEventCallback::AsObject()
163 {
164     return nullptr;
165 }
166 
167 OHOS::sptr<WifiCDeviceEventCallback> wifiCDeviceCallback =
168     OHOS::sptr<WifiCDeviceEventCallback>(new (std::nothrow) WifiCDeviceEventCallback());
169 OHOS::sptr<WifiCScanEventCallback> wifiCScanCallback =
170     OHOS::sptr<WifiCScanEventCallback>(new (std::nothrow) WifiCScanEventCallback());
171 OHOS::sptr<WifiCHotspotEventCallback> wifiCHotspotCallback =
172     OHOS::sptr<WifiCHotspotEventCallback>(new (std::nothrow) WifiCHotspotEventCallback());
173 
EventManager()174 EventManager::EventManager()
175 {
176 }
177 
~EventManager()178 EventManager::~EventManager()
179 {
180 }
181 
RemoveEventCallback(WifiEvent * cb)182 void EventManager::RemoveEventCallback(WifiEvent *cb)
183 {
184     std::unique_lock<std::mutex> lock(callbackMutex);
185     if (cb && cb->OnWifiConnectionChanged) {
186         g_wifiEvent.OnWifiConnectionChanged = nullptr;
187     }
188     if (cb && cb->OnWifiScanStateChanged) {
189         g_wifiEvent.OnWifiScanStateChanged = nullptr;
190     }
191     if (cb && cb->OnHotspotStateChanged) {
192         g_wifiEvent.OnHotspotStateChanged = nullptr;
193     }
194     if (cb && cb->OnHotspotStaJoin) {
195         g_wifiEvent.OnHotspotStaJoin = nullptr;
196     }
197     if (cb && cb->OnHotspotStaLeave) {
198         g_wifiEvent.OnHotspotStaLeave = nullptr;
199     }
200     if (cb && cb->OnDeviceConfigChange) {
201         g_wifiEvent.OnDeviceConfigChange = nullptr;
202     }
203 }
204 
IsEventRegistered()205 bool EventManager::IsEventRegistered()
206 {
207     return m_isEventRegistered;
208 }
209 
SetIsEventRegistrated(bool isEventRegistered)210 void EventManager::SetIsEventRegistrated(bool isEventRegistered)
211 {
212     m_isEventRegistered = isEventRegistered;
213 }
214 
RegisterDeviceEvent(const std::vector<std::string> & event)215 WifiErrorCode EventManager::RegisterDeviceEvent(const std::vector<std::string> &event)
216 {
217     using namespace OHOS::Wifi;
218     if (event.empty()) {
219         WIFI_LOGE("Register sta event is empty!");
220         return ERROR_WIFI_UNKNOWN;
221     }
222     if (g_wifiStaPtr == nullptr) {
223         WIFI_LOGE("Register sta event get instance failed!");
224         return ERROR_WIFI_UNKNOWN;
225     }
226     ErrCode ret = g_wifiStaPtr->RegisterCallBack(wifiCDeviceCallback, event);
227     if (ret != WIFI_OPT_SUCCESS) {
228         WIFI_LOGE("Register sta event failed!");
229         return ERROR_WIFI_UNKNOWN;
230     }
231     return WIFI_SUCCESS;
232 }
233 
RegisterScanEvent(const std::vector<std::string> & event)234 WifiErrorCode EventManager::RegisterScanEvent(const std::vector<std::string> &event)
235 {
236     using namespace OHOS::Wifi;
237     if (event.empty()) {
238         WIFI_LOGE("Register scan event is empty!");
239         return ERROR_WIFI_UNKNOWN;
240     }
241     if (g_wifiScanPtr == nullptr) {
242         WIFI_LOGE("Register scan event get instance failed!");
243         return ERROR_WIFI_UNKNOWN;
244     }
245     ErrCode ret = g_wifiScanPtr->RegisterCallBack(wifiCScanCallback, event);
246     if (ret != WIFI_OPT_SUCCESS) {
247         WIFI_LOGE("Register scan event failed!");
248         return ERROR_WIFI_UNKNOWN;
249     }
250     return WIFI_SUCCESS;
251 }
252 
RegisterHotspotEvent(const std::vector<std::string> & event)253 WifiErrorCode EventManager::RegisterHotspotEvent(const std::vector<std::string> &event)
254 {
255     using namespace OHOS::Wifi;
256     if (event.empty()) {
257         WIFI_LOGE("Register hotspot event is empty!");
258         return ERROR_WIFI_UNKNOWN;
259     }
260     if (g_wifiHotspotPtr == nullptr) {
261         WIFI_LOGE("Register hotspot event get instance failed!");
262         return ERROR_WIFI_UNKNOWN;
263     }
264     ErrCode ret = g_wifiHotspotPtr->RegisterCallBack(wifiCHotspotCallback, event);
265     if (ret != WIFI_OPT_SUCCESS) {
266         WIFI_LOGE("Register hotspot event failed!");
267         return ERROR_WIFI_UNKNOWN;
268     }
269     return WIFI_SUCCESS;
270 }
271 
RegisterP2PEvent(const std::vector<std::string> & event)272 WifiErrorCode EventManager::RegisterP2PEvent(const std::vector<std::string> &event)
273 {
274     using namespace OHOS::Wifi;
275     if (event.empty()) {
276         WIFI_LOGE("Register p2p event is empty!");
277         return ERROR_WIFI_UNKNOWN;
278     }
279     if (g_wifiP2pPtr == nullptr) {
280         WIFI_LOGE("Register p2p event get instance failed!");
281         return ERROR_WIFI_UNKNOWN;
282     }
283     OHOS::sptr<WifiP2pCEventCallback> sptrP2PCallback = GetP2PCallbackPtr();
284     if (sptrP2PCallback == nullptr) {
285         WIFI_LOGE("Register p2p event get p2p callback ptr failed!");
286         return ERROR_WIFI_UNKNOWN;
287     }
288     ErrCode ret = g_wifiP2pPtr->RegisterCallBack(sptrP2PCallback, event);
289     if (ret != WIFI_OPT_SUCCESS) {
290         WIFI_LOGE("Register p2p event failed!");
291         return ERROR_WIFI_UNKNOWN;
292     }
293     return WIFI_SUCCESS;
294 }
295 
RegisterWifiEvents()296 NO_SANITIZE("cfi") WifiErrorCode EventManager::RegisterWifiEvents()
297 {
298     std::unique_lock<std::mutex> lock(callbackMutex);
299     if (mSaStatusListener == nullptr) {
300         int32_t ret;
301         auto samgrProxy = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
302         if (samgrProxy == nullptr) {
303             WIFI_LOGI("samgrProxy is nullptr!");
304             return ERROR_WIFI_UNKNOWN;
305         }
306         mSaStatusListener = new OHOS::Wifi::WifiAbilityStatusChange();
307         if (mSaStatusListener == nullptr) {
308             WIFI_LOGI("mSaStatusListener is nullptr!");
309             return ERROR_WIFI_UNKNOWN;
310         }
311         ret = samgrProxy->SubscribeSystemAbility((int32_t)WIFI_DEVICE_ABILITY_ID, mSaStatusListener);
312         samgrProxy->SubscribeSystemAbility((int32_t)WIFI_SCAN_ABILITY_ID, mSaStatusListener);
313         samgrProxy->SubscribeSystemAbility((int32_t)WIFI_HOTSPOT_ABILITY_ID, mSaStatusListener);
314         samgrProxy->SubscribeSystemAbility((int32_t)WIFI_P2P_ABILITY_ID, mSaStatusListener);
315         WIFI_LOGI("SubscribeSystemAbility return ret:%{public}d!", ret);
316     }
317 
318     WifiErrorCode ret = WIFI_SUCCESS;
319     ret = RegisterDeviceEvent(WifiCDeviceEventCallback::deviceCallbackEvent);
320     if (ret != WIFI_SUCCESS) {
321         return ret;
322     }
323 
324     ret = RegisterScanEvent(WifiCScanEventCallback::scanCallbackEvent);
325     if (ret != WIFI_SUCCESS) {
326         return ret;
327     }
328 
329     ret = RegisterHotspotEvent(WifiCHotspotEventCallback::hotspotCallbackEvent);
330     if (ret != WIFI_SUCCESS) {
331         return ret;
332     }
333     return WIFI_SUCCESS;
334 }
335 
SetP2PCallbackEvent(OHOS::sptr<WifiP2pCEventCallback> & sptr,const std::string & eventName)336 void EventManager::SetP2PCallbackEvent(OHOS::sptr<WifiP2pCEventCallback> &sptr, const std::string &eventName)
337 {
338     if (sptr == nullptr) {
339         WIFI_LOGE("SetP2PCallbackEvent, invalid sptr.");
340         return;
341     }
342 
343     WIFI_LOGI("SetP2PCallbackEvent, eventName:%{public}s", eventName.c_str());
344     sptrP2PCallback = sptr;
345     p2pRegisteredCallbackEvent.emplace(eventName);
346     return;
347 }
348 
RemoveP2PCallbackEvent(const std::string & eventName)349 void EventManager::RemoveP2PCallbackEvent(const std::string &eventName)
350 {
351     WIFI_LOGI("RemoveP2PCallbackEvent, eventName:%{public}s", eventName.c_str());
352     p2pRegisteredCallbackEvent.erase(eventName);
353     return;
354 }
355 
GetP2PCallbackEvent()356 std::set<std::string>& EventManager::GetP2PCallbackEvent()
357 {
358     return p2pRegisteredCallbackEvent;
359 }
360 
GetP2PCallbackPtr()361 OHOS::sptr<WifiP2pCEventCallback> EventManager::GetP2PCallbackPtr()
362 {
363     return sptrP2PCallback;
364 }
365 
GetInstance()366 EventManager& EventManager::GetInstance()
367 {
368     static EventManager g_eventManger;
369     return g_eventManger;
370 }
371 
Init()372 void EventManager::Init()
373 {
374     std::unique_lock<std::mutex> lock(callbackMutex);
375     if (mSaStatusListener == nullptr) {
376         int32_t ret;
377         WIFI_LOGI("EventManager Listener Init!");
378         auto samgrProxy = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
379         if (samgrProxy == nullptr) {
380             WIFI_LOGI("samgrProxy is nullptr!");
381             return;
382         }
383         mSaStatusListener = new OHOS::Wifi::WifiAbilityStatusChange();
384         if (mSaStatusListener == nullptr) {
385             WIFI_LOGI("mSaStatusListener is nullptr!");
386             return;
387         }
388         ret = samgrProxy->SubscribeSystemAbility((int32_t)WIFI_DEVICE_ABILITY_ID, mSaStatusListener);
389         samgrProxy->SubscribeSystemAbility((int32_t)WIFI_SCAN_ABILITY_ID, mSaStatusListener);
390         samgrProxy->SubscribeSystemAbility((int32_t)WIFI_HOTSPOT_ABILITY_ID, mSaStatusListener);
391         samgrProxy->SubscribeSystemAbility((int32_t)WIFI_P2P_ABILITY_ID, mSaStatusListener);
392         WIFI_LOGI("Init, SubscribeSystemAbility return ret:%{public}d!", ret);
393     }
394     return;
395 }
396 
397 std::mutex EventManager::callbackMutex;
398 WifiEvent EventManager::g_wifiEvent = {0};
399 bool EventManager::m_isEventRegistered = false;
400 
SaveWifiCallbackInfo(WifiEvent * event)401 void EventManager::SaveWifiCallbackInfo(WifiEvent* event)
402 {
403     std::unique_lock<std::mutex> lock(callbackMutex);
404     if (event && event->OnWifiConnectionChanged) {
405         g_wifiEvent.OnWifiConnectionChanged = event->OnWifiConnectionChanged;
406     }
407     if (event && event->OnWifiScanStateChanged) {
408         g_wifiEvent.OnWifiScanStateChanged = event->OnWifiScanStateChanged;
409     }
410     if (event && event->OnHotspotStateChanged) {
411         g_wifiEvent.OnHotspotStateChanged = event->OnHotspotStateChanged;
412     }
413     if (event && event->OnHotspotStaJoin) {
414         g_wifiEvent.OnHotspotStaJoin = event->OnHotspotStaJoin;
415     }
416     if (event && event->OnHotspotStaLeave) {
417         g_wifiEvent.OnHotspotStaLeave = event->OnHotspotStaLeave;
418     }
419     if (event && event->OnDeviceConfigChange) {
420         g_wifiEvent.OnDeviceConfigChange = event->OnDeviceConfigChange;
421     }
422 }
423 
RegisterWifiEvent(WifiEvent * event)424 WifiErrorCode RegisterWifiEvent(WifiEvent *event) {
425     WIFI_LOGI("Register wifi event");
426     EventManager::GetInstance().SaveWifiCallbackInfo(event);
427     if (!EventManager::GetInstance().IsEventRegistered()) {
428         if (EventManager::GetInstance().RegisterWifiEvents() == WIFI_SUCCESS) {
429             EventManager::GetInstance().SetIsEventRegistrated(true);
430         } else {
431             WIFI_LOGE("Wifi event register failed!");
432         }
433     }
434     return WIFI_SUCCESS;
435 }
436 
UnRegisterWifiEvent(WifiEvent * event)437 WifiErrorCode UnRegisterWifiEvent(WifiEvent *event) {
438     WIFI_LOGI("Unregister wifi event");
439     EventManager::GetInstance().RemoveEventCallback(event);
440     return WIFI_SUCCESS;
441 }
442