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 #ifndef OHOS_ARCH_LITE
17 #include "wifi_event_subscriber_manager.h"
18 #include "wifi_manager.h"
19 #include "wifi_service_manager.h"
20 #include "wifi_config_center.h"
21 #include "wifi_logger.h"
22 #include "wifi_global_func.h"
23 #include "wifi_system_timer.h"
24 #include "common_event_support.h"
25 #include "wifi_datashare_utils.h"
26 #include "wifi_location_mode_observer.h"
27 #include "wifi_common_util.h"
28 #include "wifi_notification_util.h"
29 #include "wifi_app_state_aware.h"
30 #include "wifi_net_agent.h"
31 #ifdef HAS_MOVEMENT_PART
32 #include "wifi_msdp_state_listener.h"
33 #endif
34 #ifdef SUPPORT_ClOUD_WIFI_ASSET
35 #include "wifi_asset_manager.h"
36 #endif
37 #include "wifi_country_code_manager.h"
38 #include "wifi_country_code_define.h"
39
40 DEFINE_WIFILOG_LABEL("WifiEventSubscriberManager");
41
42 namespace OHOS {
43 namespace Wifi {
44 constexpr uint32_t TIMEOUT_EVENT_SUBSCRIBER = 3000;
45 constexpr uint32_t TIMEOUT_EVENT_DELAY_ACCESS_DATASHARE = 10 * 1000;
46 constexpr uint32_t PROP_LEN = 26;
47 constexpr uint32_t PROP_SUBCHIPTYPE_LEN = 10;
48 constexpr uint32_t SUPPORT_COEXCHIP_LEN = 7;
49 constexpr uint32_t PROP_TRUE_LEN = 4;
50 constexpr uint32_t PROP_FALSE_LEN = 5;
51 const std::string PROP_TRUE = "true";
52 const std::string PROP_FALSE = "false";
53 const std::string SUBCHIP_WIFI_PROP = "ohos.boot.odm.conn.schiptype";
54 const std::string MDM_WIFI_PROP = "persist.edm.wifi_enable";
55 const std::string SUPPORT_COEXCHIP = "";
56 const std::string COEX_IFACENAME = "wlan1";
57 const std::string WIFI_STANDBY_NAP = "napped";
58 const std::string WIFI_STANDBY_SLEEPING = "sleeping";
59
60 bool WifiEventSubscriberManager::mIsMdmForbidden = false;
61 static sptr<WifiLocationModeObserver> locationModeObserver_ = nullptr;
62 static sptr<WifiCloneModeObserver> cloneModeObserver_ = nullptr;
63 #ifdef HAS_MOVEMENT_PART
64 static sptr<DeviceMovementCallback> deviceMovementCallback_ = nullptr;
65 #endif
66
67 using CesFuncType = void (CesEventSubscriber::*)(const OHOS::EventFwk::CommonEventData &eventData);
68
69 const std::map<std::string, CesFuncType> CES_REQUEST_MAP = {
70 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON, &CesEventSubscriber::OnReceiveScreenEvent},
71 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF, &CesEventSubscriber::OnReceiveScreenEvent},
72 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED, &
73 CesEventSubscriber::OnReceiveAirplaneEvent},
74 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED, &
75 CesEventSubscriber::OnReceiveBatteryEvent},
76 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED, &
77 CesEventSubscriber::OnReceiveBatteryEvent},
78 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, &CesEventSubscriber::OnReceiveAppEvent},
79 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED, &
80 CesEventSubscriber::OnReceiveThermalEvent},
81 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED, &
82 CesEventSubscriber::OnReceiveStandbyEvent},
83 {OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED, &
84 CesEventSubscriber::OnReceiveUserUnlockedEvent}
85 };
86
WifiEventSubscriberManager()87 WifiEventSubscriberManager::WifiEventSubscriberManager()
88 {
89 WIFI_LOGI("create WifiEventSubscriberManager");
90 if (accessDatashareTimerId == 0) {
91 WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::DelayedAccessDataShare, this);
92 WifiTimer::GetInstance()->Register(
93 timeoutCallback, accessDatashareTimerId, TIMEOUT_EVENT_DELAY_ACCESS_DATASHARE);
94 WIFI_LOGI("DelayedAccessDataShare register success! accessDatashareTimerId:%{public}u", accessDatashareTimerId);
95 }
96
97 RegisterCesEvent();
98 RegisterNotificationEvent();
99 #ifdef HAS_POWERMGR_PART
100 RegisterPowermgrEvent();
101 #endif
102 #ifdef SUPPORT_ClOUD_WIFI_ASSET
103 RegisterAssetEvent();
104 #endif
105 if (IsDataMgrServiceActive()) {
106 RegisterCloneEvent();
107 }
108 InitSubscribeListener();
109 GetMdmProp();
110 GetChipProp();
111 RegisterMdmPropListener();
112 }
113
~WifiEventSubscriberManager()114 WifiEventSubscriberManager::~WifiEventSubscriberManager()
115 {
116 WIFI_LOGI("~WifiEventSubscriberManager");
117 UnRegisterCesEvent();
118 #ifdef SUPPORT_ClOUD_WIFI_ASSET
119 UnRegisterAssetEvent();
120 #endif
121 UnRegisterNotificationEvent();
122 UnRegisterCloneEvent();
123 UnRegisterLocationEvent();
124 UnRegisterNetworkStateChangeEvent();
125 UnRegisterWifiScanChangeEvent();
126 }
127
RegisterCesEvent()128 void WifiEventSubscriberManager::RegisterCesEvent()
129 {
130 std::unique_lock<std::mutex> lock(cesEventMutex);
131 if (cesTimerId != 0) {
132 WifiTimer::GetInstance()->UnRegister(cesTimerId);
133 }
134 if (isCesEventSubscribered) {
135 return;
136 }
137 OHOS::EventFwk::MatchingSkills matchingSkills;
138 for (auto itFunc : CES_REQUEST_MAP) {
139 matchingSkills.AddEvent(itFunc.first);
140 }
141 WIFI_LOGI("RegisterCesEvent start");
142 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
143 cesEventSubscriber_ = std::make_shared<CesEventSubscriber>(subscriberInfo);
144 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(cesEventSubscriber_)) {
145 WIFI_LOGE("CesEvent SubscribeCommonEvent() failed");
146 cesEventSubscriber_ = nullptr;
147 WifiTimer::TimerCallback timeoutCallBack = std::bind(&WifiEventSubscriberManager::RegisterCesEvent, this);
148 WifiTimer::GetInstance()->Register(timeoutCallBack, cesTimerId, TIMEOUT_EVENT_SUBSCRIBER, false);
149 WIFI_LOGI("RegisterCesEvent retry, cesTimerId = %{public}u", cesTimerId);
150 } else {
151 WIFI_LOGI("RegisterCesEvent success");
152 isCesEventSubscribered = true;
153 }
154 }
155
UnRegisterCesEvent()156 void WifiEventSubscriberManager::UnRegisterCesEvent()
157 {
158 std::unique_lock<std::mutex> lock(cesEventMutex);
159 if (cesTimerId != 0) {
160 WifiTimer::GetInstance()->UnRegister(cesTimerId);
161 }
162 if (!isCesEventSubscribered) {
163 return;
164 }
165 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(cesEventSubscriber_)) {
166 WIFI_LOGE("UnRegisterCesEvent failed");
167 }
168 cesEventSubscriber_ = nullptr;
169 isCesEventSubscribered = false;
170 WIFI_LOGI("UnRegisterCesEvent finished");
171 }
172
HandleAppMgrServiceChange(bool add)173 void WifiEventSubscriberManager::HandleAppMgrServiceChange(bool add)
174 {
175 WIFI_LOGI("%{public}s enter, add flag: %{public}d", __FUNCTION__, add);
176 if (add) {
177 WifiAppStateAware::GetInstance().RegisterAppStateObserver();
178 } else {
179 WifiAppStateAware::GetInstance().UnSubscribeAppState();
180 }
181 }
182
HandleCommNetConnManagerSysChange(int systemAbilityId,bool add)183 void WifiEventSubscriberManager::HandleCommNetConnManagerSysChange(int systemAbilityId, bool add)
184 {
185 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
186 IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i);
187 if (pService != nullptr) {
188 pService->OnSystemAbilityChanged(systemAbilityId, add);
189 }
190 }
191 }
192
HandleCommonEventServiceChange(int systemAbilityId,bool add)193 void WifiEventSubscriberManager::HandleCommonEventServiceChange(int systemAbilityId, bool add)
194 {
195 WIFI_LOGI("OnSystemAbilityChanged, id[%{public}d], mode=[%{public}d]!", systemAbilityId, add);
196 if (add) {
197 RegisterCesEvent();
198 RegisterNotificationEvent();
199 #ifdef SUPPORT_ClOUD_WIFI_ASSET
200 RegisterAssetEvent();
201 #endif
202 RegisterNetworkStateChangeEvent();
203 RegisterWifiScanChangeEvent();
204 } else {
205 UnRegisterCesEvent();
206 UnRegisterNotificationEvent();
207 #ifdef SUPPORT_ClOUD_WIFI_ASSET
208 UnRegisterAssetEvent();
209 #endif
210 UnRegisterNetworkStateChangeEvent();
211 UnRegisterWifiScanChangeEvent();
212 }
213 }
214
215 #ifdef HAS_MOVEMENT_PART
HandleHasMovementPartChange(int systemAbilityId,bool add)216 void WifiEventSubscriberManager::HandleHasMovementPartChange(int systemAbilityId, bool add)
217 {
218 if (add) {
219 RegisterMovementCallBack();
220 } else {
221 UnRegisterMovementCallBack();
222 }
223 }
224 #endif
225
HandleDistributedKvDataServiceChange(bool add)226 void WifiEventSubscriberManager::HandleDistributedKvDataServiceChange(bool add)
227 {
228 if (!add) {
229 UnRegisterCloneEvent();
230 return;
231 }
232 RegisterLocationEvent();
233 RegisterCloneEvent();
234 }
235
HandleCastServiceChange(bool add)236 void WifiEventSubscriberManager::HandleCastServiceChange(bool add)
237 {
238 if (!add) {
239 WifiConfigCenter::GetInstance().ClearLocalHid2dInfo(CAST_ENGINE_SERVICE_UID);
240 }
241 }
242
HandleShareServiceChange(bool add)243 void WifiEventSubscriberManager::HandleShareServiceChange(bool add)
244 {
245 if (!add) {
246 WifiConfigCenter::GetInstance().ClearLocalHid2dInfo(SHARE_SERVICE_UID);
247 }
248 }
249
HandleMouseCrossServiceChange(bool add)250 void WifiEventSubscriberManager::HandleMouseCrossServiceChange(bool add)
251 {
252 if (!add) {
253 WifiConfigCenter::GetInstance().ClearLocalHid2dInfo(MOUSE_CROSS_SERVICE_UID);
254 }
255 }
256
257 #ifdef FEATURE_P2P_SUPPORT
HandleP2pBusinessChange(int systemAbilityId,bool add)258 void WifiEventSubscriberManager::HandleP2pBusinessChange(int systemAbilityId, bool add)
259 {
260 WIFI_LOGI("HandleP2pBusinessChange, id[%{public}d], mode=[%{public}d]!", systemAbilityId, add);
261 if (add) {
262 return;
263 }
264 if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
265 WifiConfigCenter::GetInstance().ClearLocalHid2dInfo(SOFT_BUS_SERVICE_UID);
266 }
267 if (systemAbilityId == MIRACAST_SERVICE_SA_ID) {
268 WifiConfigCenter::GetInstance().ClearLocalHid2dInfo(MIRACAST_SERVICE_UID);
269 }
270 IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst();
271 if (pService == nullptr) {
272 WIFI_LOGE("Get P2P service failed!");
273 return;
274 }
275 pService->HandleBusinessSAException(systemAbilityId);
276 return;
277 }
278 #endif
279
OnSystemAbilityChanged(int systemAbilityId,bool add)280 void WifiEventSubscriberManager::OnSystemAbilityChanged(int systemAbilityId, bool add)
281 {
282 WIFI_LOGI("%{public}s enter, systemAbilityId: %{public}d", __FUNCTION__, systemAbilityId);
283 switch (systemAbilityId) {
284 case APP_MGR_SERVICE_ID:
285 HandleAppMgrServiceChange(add);
286 break;
287 case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID:
288 HandleCommNetConnManagerSysChange(systemAbilityId, add);
289 break;
290 case COMMON_EVENT_SERVICE_ID:
291 HandleCommonEventServiceChange(systemAbilityId, add);
292 break;
293 #ifdef HAS_MOVEMENT_PART
294 case MSDP_MOVEMENT_SERVICE_ID:
295 HandleHasMovementPartChange(systemAbilityId, add);
296 break;
297 #endif
298 case DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID:
299 HandleDistributedKvDataServiceChange(add);
300 break;
301 #ifdef FEATURE_P2P_SUPPORT
302 case SOFTBUS_SERVER_SA_ID:
303 case MIRACAST_SERVICE_SA_ID:
304 HandleP2pBusinessChange(systemAbilityId, add);
305 break;
306 #endif
307 case CAST_ENGINE_SA_ID:
308 HandleCastServiceChange(add);
309 break;
310 case SHARE_SERVICE_ID:
311 HandleShareServiceChange(add);
312 break;
313 case MOUSE_CROSS_SERVICE_ID:
314 HandleMouseCrossServiceChange(add);
315 break;
316 default:
317 break;
318 }
319 }
320
GetAirplaneModeByDatashare()321 void WifiEventSubscriberManager::GetAirplaneModeByDatashare()
322 {
323 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
324 if (datashareHelper == nullptr) {
325 WIFI_LOGE("GetAirplaneModeByDatashare, datashareHelper is nullptr!");
326 return;
327 }
328
329 std::string airplaneMode;
330 Uri uri(SETTINGS_DATASHARE_URL_AIRPLANE_MODE);
331 int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_AIRPLANE_MODE, airplaneMode);
332 if (ret != WIFI_OPT_SUCCESS) {
333 WIFI_LOGE("GetAirplaneModeByDatashare, Query airplaneMode again!");
334 ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_AIRPLANE_MODE, airplaneMode, true);
335 if (ret != WIFI_OPT_SUCCESS) {
336 WIFI_LOGE("GetAirplaneModeByDatashare, Query airplaneMode fail!");
337 return;
338 }
339 }
340
341 WIFI_LOGI("GetAirplaneModeByDatashare, airplaneMode:%{public}s", airplaneMode.c_str());
342 if (airplaneMode.compare("1") == 0) {
343 WifiConfigCenter::GetInstance().SetWifiStateOnAirplaneChanged(MODE_STATE_OPEN);
344 } else {
345 WifiConfigCenter::GetInstance().SetWifiStateOnAirplaneChanged(MODE_STATE_CLOSE);
346 }
347 return;
348 }
349
GetWifiAllowSemiActiveByDatashare()350 void WifiEventSubscriberManager::GetWifiAllowSemiActiveByDatashare()
351 {
352 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
353 if (datashareHelper == nullptr) {
354 WIFI_LOGE("GetWifiAllowSemiActiveByDatashare, datashareHelper is nullptr!");
355 return;
356 }
357
358 std::string isAllowed;
359 Uri uri(SETTINGS_DATASHARE_URI_WIFI_ALLOW_SEMI_ACTIVE);
360 int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_WIFI_ALLOW_SEMI_ACTIVE, isAllowed);
361 if (ret != WIFI_OPT_SUCCESS) {
362 WIFI_LOGE("GetWifiAllowSemiActiveByDatashare, Query wifiAllowSemiActive fail!");
363 return;
364 }
365
366 WIFI_LOGI("GetWifiAllowSemiActiveByDatashare, isAllowed:%{public}s", isAllowed.c_str());
367 WifiConfigCenter::GetInstance().SetWifiAllowSemiActive(isAllowed.compare("1") == 0);
368 return;
369 }
370
GetLocationModeByDatashare()371 bool WifiEventSubscriberManager::GetLocationModeByDatashare()
372 {
373 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
374 if (datashareHelper == nullptr) {
375 WIFI_LOGE("GetLocationModeByDatashare, datashareHelper is nullptr!");
376 return false;
377 }
378
379 std::string locationMode;
380 Uri uri(datashareHelper->GetLoactionDataShareUri());
381 int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_LOCATION_MODE, locationMode);
382 if (ret != WIFI_OPT_SUCCESS) {
383 WIFI_LOGE("GetLocationModeByDatashare, Query locationMode fail!");
384 return false;
385 }
386
387 WIFI_LOGD("GetLocationModeByDatashare, locationMode:%{public}s", locationMode.c_str());
388 return (locationMode.compare("1") == 0);
389 }
390
DealLocationModeChangeEvent()391 void WifiEventSubscriberManager::DealLocationModeChangeEvent()
392 {
393 if (GetLocationModeByDatashare()) {
394 WIFI_LOGI("DealLocationModeChangeEvent open");
395 WifiManager::GetInstance().GetWifiTogglerManager()->ScanOnlyToggled(1);
396 } else {
397 WIFI_LOGI("DealLocationModeChangeEvent close");
398 WifiManager::GetInstance().GetWifiTogglerManager()->ScanOnlyToggled(0);
399 }
400 }
401
GetCloneDataByDatashare(std::string & cloneData)402 void WifiEventSubscriberManager::GetCloneDataByDatashare(std::string &cloneData)
403 {
404 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
405 if (datashareHelper == nullptr) {
406 WIFI_LOGE("GetCloneDataByDatashare, datashareHelper is nullptr!");
407 return;
408 }
409
410 Uri uri(SETTINGS_DATASHARE_URI_CLONE_DATA);
411 int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_CLONE_DATA, cloneData);
412 if (ret != WIFI_OPT_SUCCESS) {
413 WIFI_LOGE("GetCloneDataByDatashare, Query cloneMode fail!");
414 return;
415 }
416 WIFI_LOGI("GetCloneDataByDatashare success");
417 }
418
SetCloneDataByDatashare(const std::string & cloneData)419 void WifiEventSubscriberManager::SetCloneDataByDatashare(const std::string &cloneData)
420 {
421 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
422 if (datashareHelper == nullptr) {
423 WIFI_LOGE("SetCloneDataByDatashare, datashareHelper is nullptr!");
424 return;
425 }
426
427 Uri uri(SETTINGS_DATASHARE_URI_CLONE_DATA);
428 int ret = datashareHelper->Update(uri, SETTINGS_DATASHARE_KEY_CLONE_DATA, cloneData);
429 if (ret != WIFI_OPT_SUCCESS) {
430 WIFI_LOGE("SetCloneDataByDatashare, Update cloneData fail!");
431 return;
432 }
433 WIFI_LOGI("SetCloneDataByDatashare success");
434 }
435
DealCloneDataChangeEvent()436 void WifiEventSubscriberManager::DealCloneDataChangeEvent()
437 {
438 WIFI_LOGI("DealCloneDataChangeEvent enter");
439 mWifiEventSubsThread = std::make_unique<WifiEventHandler>("WifiEventSubsThread");
440 mWifiEventSubsThread->PostAsyncTask([this]() {
441 std::string cloneData;
442 GetCloneDataByDatashare(cloneData);
443 if (cloneData.empty()) {
444 return;
445 }
446 WifiSettings::GetInstance().MergeWifiCloneConfig(cloneData);
447 cloneData.clear();
448 SetCloneDataByDatashare("");
449 });
450 }
451
CheckAndStartStaByDatashare()452 void WifiEventSubscriberManager::CheckAndStartStaByDatashare()
453 {
454 constexpr int openWifi = 1;
455 constexpr int openWifiInAirplanemode = 2;
456 constexpr int closeWifiByAirplanemodeOpen = 3;
457
458 int lastStaState = GetLastStaStateByDatashare();
459 if (lastStaState == openWifi) {
460 WifiConfigCenter::GetInstance().SetWifiToggledState(WIFI_STATE_ENABLED, INSTID_WLAN0);
461 WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(1, 0);
462 } else if (lastStaState == openWifiInAirplanemode) {
463 WifiSettings::GetInstance().SetWifiFlagOnAirplaneMode(true);
464 WifiConfigCenter::GetInstance().SetWifiToggledState(WIFI_STATE_ENABLED, INSTID_WLAN0);
465 WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(1, 0);
466 } else if (lastStaState == closeWifiByAirplanemodeOpen) {
467 WifiConfigCenter::GetInstance().SetWifiToggledState(WIFI_STATE_ENABLED, INSTID_WLAN0);
468 }
469 }
470
IsMdmForbidden()471 bool WifiEventSubscriberManager::IsMdmForbidden()
472 {
473 return mIsMdmForbidden;
474 }
475
DelayedAccessDataShare()476 void WifiEventSubscriberManager::DelayedAccessDataShare()
477 {
478 WIFI_LOGI("DelayedAccessDataShare enter!");
479 std::filesystem::path pathName = WIFI_CONFIG_FILE_PATH;
480 std::error_code code;
481 if (!std::filesystem::exists(pathName, code)) {
482 CheckAndStartStaByDatashare();
483 }
484 GetAirplaneModeByDatashare();
485
486 if (accessDatashareTimerId != 0) {
487 WifiTimer::GetInstance()->UnRegister(accessDatashareTimerId);
488 accessDatashareTimerId = 0;
489 }
490 }
491
InitSubscribeListener()492 void WifiEventSubscriberManager::InitSubscribeListener()
493 {
494 SubscribeSystemAbility(APP_MGR_SERVICE_ID);
495 SubscribeSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
496 SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID);
497 #ifdef HAS_MOVEMENT_PART
498 SubscribeSystemAbility(MSDP_MOVEMENT_SERVICE_ID);
499 #endif
500 SubscribeSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); // subscribe data management service done
501 SubscribeSystemAbility(SOFTBUS_SERVER_SA_ID);
502 SubscribeSystemAbility(CAST_ENGINE_SA_ID);
503 SubscribeSystemAbility(MIRACAST_SERVICE_SA_ID);
504 SubscribeSystemAbility(SHARE_SERVICE_ID);
505 SubscribeSystemAbility(MOUSE_CROSS_SERVICE_ID);
506 }
507
IsDataMgrServiceActive()508 bool WifiEventSubscriberManager::IsDataMgrServiceActive()
509 {
510 sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
511 if (sa_mgr == nullptr) {
512 WIFI_LOGE("Failed to get SystemAbilityManager!");
513 return false;
514 }
515 sptr<IRemoteObject> object = sa_mgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
516 if (object == nullptr) {
517 WIFI_LOGE("Failed to get DataMgrService!");
518 return false;
519 }
520 return true;
521 }
522
GetLastStaStateByDatashare()523 int WifiEventSubscriberManager::GetLastStaStateByDatashare()
524 {
525 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
526 if (datashareHelper == nullptr) {
527 WIFI_LOGE("GetLastStaStateByDatashare, datashareHelper is nullptr!");
528 return 0;
529 }
530
531 std::string lastStaState;
532 Uri uri(SETTINGS_DATASHARE_URI_WIFI_ON);
533 int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_WIFI_ON, lastStaState);
534 if (ret != WIFI_OPT_SUCCESS) {
535 WIFI_LOGW("Query lastStaState fail, query settingsdata again!");
536 ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_WIFI_ON, lastStaState, true);
537 if (ret != WIFI_OPT_SUCCESS) {
538 WIFI_LOGE("GetLastStaStateByDatashare Query lastStaState fail!");
539 return 0;
540 }
541 }
542
543 WIFI_LOGI("GetLastStaStateByDatashare, lastStaState:%{public}s", lastStaState.c_str());
544 int lastStaStateType = ConvertStringToInt(lastStaState);
545 return lastStaStateType;
546 }
547
RegisterLocationEvent()548 void WifiEventSubscriberManager::RegisterLocationEvent()
549 {
550 std::unique_lock<std::mutex> lock(locationEventMutex);
551 if (islocationModeObservered) {
552 return;
553 }
554
555 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
556 if (datashareHelper == nullptr) {
557 WIFI_LOGE("LocationEvent datashareHelper is nullptr");
558 return;
559 }
560 locationModeObserver_ = sptr<WifiLocationModeObserver>(new (std::nothrow)WifiLocationModeObserver());
561 Uri uri(datashareHelper->GetLoactionDataShareUri());
562 datashareHelper->RegisterObserver(uri, locationModeObserver_);
563 islocationModeObservered = true;
564 WIFI_LOGI("registerLocationEvent success");
565 }
566
UnRegisterLocationEvent()567 void WifiEventSubscriberManager::UnRegisterLocationEvent()
568 {
569 std::unique_lock<std::mutex> lock(locationEventMutex);
570 if (!islocationModeObservered) {
571 WIFI_LOGE("UnRegisterLocationEvent islocationModeObservered is false");
572 return;
573 }
574
575 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
576 if (datashareHelper == nullptr) {
577 WIFI_LOGE("UnRegisterLocationEvent datashareHelper is nullptr");
578 return;
579 }
580 Uri uri(datashareHelper->GetLoactionDataShareUri());
581 datashareHelper->UnRegisterObserver(uri, locationModeObserver_);
582 islocationModeObservered = false;
583 }
584
RegisterCloneEvent()585 void WifiEventSubscriberManager::RegisterCloneEvent()
586 {
587 std::unique_lock<std::mutex> lock(cloneEventMutex);
588 if (cloneModeObserver_) {
589 return;
590 }
591
592 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
593 if (datashareHelper == nullptr) {
594 WIFI_LOGE("RegisterCloneEvent datashareHelper is nullptr");
595 return;
596 }
597 cloneModeObserver_ = sptr<WifiCloneModeObserver>(new (std::nothrow)WifiCloneModeObserver());
598 Uri uri(SETTINGS_DATASHARE_URI_CLONE_DATA);
599 datashareHelper->RegisterObserver(uri, cloneModeObserver_);
600 WIFI_LOGI("RegisterCloneEvent success");
601 }
602
UnRegisterCloneEvent()603 void WifiEventSubscriberManager::UnRegisterCloneEvent()
604 {
605 std::unique_lock<std::mutex> lock(cloneEventMutex);
606 if (cloneModeObserver_ == nullptr) {
607 WIFI_LOGE("UnRegisterCloneEvent cloneModeObserver_ is nullptr");
608 return;
609 }
610
611 auto datashareHelper = DelayedSingleton<WifiDataShareHelperUtils>::GetInstance();
612 if (datashareHelper == nullptr) {
613 cloneModeObserver_ = nullptr;
614 WIFI_LOGE("UnRegisterCloneEvent datashareHelper is nullptr");
615 return;
616 }
617 Uri uri(SETTINGS_DATASHARE_URI_CLONE_DATA);
618 datashareHelper->UnRegisterObserver(uri, cloneModeObserver_);
619 cloneModeObserver_ = nullptr;
620 WIFI_LOGI("UnRegisterCloneEvent success");
621 }
622
GetMdmProp()623 void WifiEventSubscriberManager::GetMdmProp()
624 {
625 char preValue[PROP_FALSE_LEN + 1] = {0};
626 mIsMdmForbidden = false;
627 int errorCode = GetParamValue(MDM_WIFI_PROP.c_str(), 0, preValue, PROP_FALSE_LEN + 1);
628 if (errorCode > 0) {
629 if (strncmp(preValue, PROP_TRUE.c_str(), PROP_TRUE_LEN) == 0) {
630 mIsMdmForbidden = true;
631 }
632 }
633 }
634
GetChipProp()635 void WifiEventSubscriberManager::GetChipProp()
636 {
637 char preValue[PROP_SUBCHIPTYPE_LEN] = {0};
638 int errorCode = GetParamValue(SUBCHIP_WIFI_PROP.c_str(), 0, preValue, PROP_SUBCHIPTYPE_LEN);
639 if (errorCode > 0) {
640 if (strncmp(preValue, SUPPORT_COEXCHIP.c_str(), SUPPORT_COEXCHIP_LEN) == 0) {
641 WifiConfigCenter::GetInstance().SetApIfaceName(COEX_IFACENAME);
642 WifiConfigCenter::GetInstance().SetCoexSupport(true);
643 }
644 }
645 }
646
RegisterMdmPropListener()647 void WifiEventSubscriberManager::RegisterMdmPropListener()
648 {
649 int ret = WatchParamValue(MDM_WIFI_PROP.c_str(), MdmPropChangeEvt, nullptr);
650 if (ret != 0) {
651 WIFI_LOGI("RegisterMdmPropListener failed");
652 }
653 }
654
MdmPropChangeEvt(const char * key,const char * value,void * context)655 void WifiEventSubscriberManager::MdmPropChangeEvt(const char *key, const char *value, void *context)
656 {
657 if (strncmp(key, MDM_WIFI_PROP.c_str(), PROP_LEN) != 0) {
658 WIFI_LOGI("not mdm prop change");
659 return;
660 }
661 WIFI_LOGI("mdm prop change");
662 if (strncmp(value, PROP_TRUE.c_str(), PROP_TRUE_LEN) == 0) {
663 mIsMdmForbidden = true;
664 return;
665 }
666 if (strncmp(value, PROP_FALSE.c_str(), PROP_FALSE_LEN) == 0) {
667 mIsMdmForbidden = false;
668 }
669 }
670
671 #ifdef HAS_MOVEMENT_PART
RegisterMovementCallBack()672 void WifiEventSubscriberManager::RegisterMovementCallBack()
673 {
674 WIFI_LOGI("RegisterMovementCallBack");
675 std::unique_lock<std::mutex> lock(deviceMovementEventMutex);
676 if (!deviceMovementCallback_) {
677 deviceMovementCallback_ = sptr<DeviceMovementCallback>(new DeviceMovementCallback());
678 }
679 if (Msdp::MovementClient::GetInstance().SubscribeCallback(
680 Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_) != ERR_OK) {
681 WIFI_LOGE("Register movement still observer failed!");
682 }
683 if (Msdp::MovementClient::GetInstance().SubscribeCallback(
684 Msdp::MovementDataUtils::MovementType::TYPE_STAY, deviceMovementCallback_) != ERR_OK) {
685 WIFI_LOGE("Register movement stay observer failed!");
686 }
687 }
688
UnRegisterMovementCallBack()689 void WifiEventSubscriberManager::UnRegisterMovementCallBack()
690 {
691 WIFI_LOGI("UnRegisterMovementCallBack");
692 std::unique_lock<std::mutex> lock(deviceMovementEventMutex);
693 if (!deviceMovementCallback_) {
694 return;
695 }
696 Msdp::MovementClient::GetInstance().UnSubscribeCallback(
697 Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_);
698 deviceMovementCallback_ = nullptr;
699 }
700 #endif
701
CesEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)702 CesEventSubscriber::CesEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
703 : CommonEventSubscriber(subscriberInfo)
704 {
705 WIFI_LOGI("CesEventSubscriber enter");
706 }
707
~CesEventSubscriber()708 CesEventSubscriber::~CesEventSubscriber()
709 {
710 WIFI_LOGI("~CesEventSubscriber enter");
711 }
712
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & eventData)713 void CesEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData)
714 {
715 std::string action = eventData.GetWant().GetAction();
716 WIFI_LOGI("CesEventSubscriber OnReceiveEvent: %{public}s", action.c_str());
717 auto itFunc = CES_REQUEST_MAP.find(action);
718 if (itFunc != CES_REQUEST_MAP.end()) {
719 auto requestFunc = itFunc->second;
720 if (requestFunc != nullptr) {
721 return (this->*requestFunc)(eventData);
722 }
723 }
724 WIFI_LOGE("CesEventSubscriber OnReceiveEvent unknown Event: %{public}s", action.c_str());
725 }
726
OnReceiveScreenEvent(const OHOS::EventFwk::CommonEventData & eventData)727 void CesEventSubscriber::OnReceiveScreenEvent(const OHOS::EventFwk::CommonEventData &eventData)
728 {
729 std::string action = eventData.GetWant().GetAction();
730 WIFI_LOGI("OnReceiveScreenEvent: %{public}s.", action.c_str());
731
732 int screenState = WifiConfigCenter::GetInstance().GetScreenState();
733 int screenStateNew = (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON)
734 ? MODE_STATE_OPEN : MODE_STATE_CLOSE;
735 WifiConfigCenter::GetInstance().SetScreenState(screenStateNew);
736 if (screenStateNew == screenState) {
737 return;
738 }
739 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
740 IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i);
741 if (pService != nullptr) {
742 pService->OnScreenStateChanged(screenStateNew);
743 #ifdef FEATURE_HPF_SUPPORT
744 WifiManager::GetInstance().InstallPacketFilterProgram(screenStateNew, i);
745 #endif
746 }
747 IScanService *pScanService = WifiServiceManager::GetInstance().GetScanServiceInst(i);
748 if (pScanService != nullptr) {
749 pScanService->OnScreenStateChanged(screenStateNew);
750 }
751 }
752 }
753
754
OnReceiveAirplaneEvent(const OHOS::EventFwk::CommonEventData & eventData)755 void CesEventSubscriber::OnReceiveAirplaneEvent(const OHOS::EventFwk::CommonEventData &eventData)
756 {
757 const auto &action = eventData.GetWant().GetAction();
758 const auto &data = eventData.GetData();
759 const auto &code = eventData.GetCode();
760 WIFI_LOGI("AirplaneModeEventSubscriber::OnReceiveEvent: %{public}s, %{public}s, %{public}d", action.c_str(),
761 data.c_str(), code);
762 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED) {
763 if (code == 1) {
764 /* open airplane mode */
765 if (WifiConfigCenter::GetInstance().SetWifiStateOnAirplaneChanged(MODE_STATE_OPEN)) {
766 WifiManager::GetInstance().GetWifiTogglerManager()->AirplaneToggled(1);
767 } else {
768 WifiConfigCenter::GetInstance().SetSoftapToggledState(false);
769 WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(0);
770 }
771 } else {
772 /* close airplane mode */
773 if (WifiConfigCenter::GetInstance().SetWifiStateOnAirplaneChanged(MODE_STATE_CLOSE)) {
774 WifiManager::GetInstance().GetWifiTogglerManager()->AirplaneToggled(0);
775 }
776 }
777 }
778 }
779
OnReceiveBatteryEvent(const OHOS::EventFwk::CommonEventData & eventData)780 void CesEventSubscriber::OnReceiveBatteryEvent(const OHOS::EventFwk::CommonEventData &eventData)
781 {
782 std::string action = eventData.GetWant().GetAction();
783 WIFI_LOGI("BatteryEventSubscriber::OnReceiveEvent: %{public}s.", action.c_str());
784 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) {
785 WifiConfigCenter::GetInstance().SetNoChargerPlugModeState(MODE_STATE_CLOSE);
786 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) {
787 WifiConfigCenter::GetInstance().SetNoChargerPlugModeState(MODE_STATE_OPEN);
788 }
789 for (int i = 0; i < AP_INSTANCE_MAX_NUM; ++i) {
790 IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(i);
791 if (pService == nullptr) {
792 WIFI_LOGE("ap service is NOT start!");
793 return;
794 }
795
796 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) {
797 WIFI_LOGE("usb connect do not stop hostapd!");
798 WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine()->StopSoftapCloseTimer();
799 return;
800 }
801
802 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) {
803 WIFI_LOGE("usb disconnect stop hostapd!");
804 std::vector<StationInfo> result;
805 IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(0);
806 if (pService == nullptr) {
807 WIFI_LOGE("get hotspot service is null!");
808 return;
809 }
810 ErrCode errCode = pService->GetStationList(result);
811 if (errCode != ErrCode::WIFI_OPT_SUCCESS) {
812 return;
813 }
814 if (result.empty()) {
815 WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine()->StartSoftapCloseTimer();
816 }
817 return;
818 }
819 }
820 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
821 IScanService *pScanService = WifiServiceManager::GetInstance().GetScanServiceInst(i);
822 if (pScanService == nullptr) {
823 WIFI_LOGE("scan service is NOT start!");
824 return;
825 }
826 if (pScanService->OnMovingFreezeStateChange() != WIFI_OPT_SUCCESS) {
827 WIFI_LOGE("OnMovingFreezeStateChange failed");
828 }
829 }
830 }
831
OnReceiveAppEvent(const OHOS::EventFwk::CommonEventData & eventData)832 void CesEventSubscriber::OnReceiveAppEvent(const OHOS::EventFwk::CommonEventData &eventData)
833 {
834 std::string action = eventData.GetWant().GetAction();
835 WIFI_LOGI("AppEventSubscriber::OnReceiveEvent : %{public}s.", action.c_str());
836 auto wantTemp = eventData.GetWant();
837 auto uid = wantTemp.GetIntParam(AppExecFwk::Constants::UID, -1);
838 if (uid == -1) {
839 WIFI_LOGE("%{public}s getPackage uid is illegal.", __func__);
840 return;
841 }
842 WIFI_LOGI("Package removed of uid %{public}d.", uid);
843 bool removeFlag = false;
844 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
845 IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i);
846 if (pService != nullptr) {
847 pService->RemoveAllCandidateConfig(uid);
848 removeFlag = true;
849 }
850 }
851 if (!removeFlag) {
852 std::vector<WifiDeviceConfig> tempConfigs;
853 WifiSettings::GetInstance().GetAllCandidateConfig(uid, tempConfigs);
854 for (const auto &config : tempConfigs) {
855 if (WifiSettings::GetInstance().RemoveDevice(config.networkId) != WIFI_OPT_SUCCESS) {
856 WIFI_LOGE("RemoveAllCandidateConfig-RemoveDevice() failed!");
857 }
858 }
859 WifiSettings::GetInstance().SyncDeviceConfig();
860 }
861 return;
862 }
863
OnReceiveThermalEvent(const OHOS::EventFwk::CommonEventData & eventData)864 void CesEventSubscriber::OnReceiveThermalEvent(const OHOS::EventFwk::CommonEventData &eventData)
865 {
866 std::string action = eventData.GetWant().GetAction();
867 WIFI_LOGI("ThermalLevelSubscriber::OnReceiveEvent: %{public}s.", action.c_str());
868 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) {
869 static const std::string THERMAL_EVENT_ID = "0";
870 int level = eventData.GetWant().GetIntParam(THERMAL_EVENT_ID, 0);
871 WifiConfigCenter::GetInstance().SetThermalLevel(level);
872 WIFI_LOGI("ThermalLevelSubscriber SetThermalLevel: %{public}d.", level);
873 }
874 }
875
OnReceiveStandbyEvent(const OHOS::EventFwk::CommonEventData & eventData)876 void CesEventSubscriber::OnReceiveStandbyEvent(const OHOS::EventFwk::CommonEventData &eventData)
877 {
878 const auto &action = eventData.GetWant().GetAction();
879 const bool napped = eventData.GetWant().GetBoolParam(WIFI_STANDBY_NAP, 0);
880 const bool sleeping = eventData.GetWant().GetBoolParam(WIFI_STANDBY_SLEEPING, 0);
881 WIFI_LOGI("StandByListerner OnReceiveEvent action[%{public}s], napped[%{public}d], sleeping[%{public}d]",
882 action.c_str(), napped, sleeping);
883 int state = WifiConfigCenter::GetInstance().GetScreenState();
884 if (lastSleepState != sleeping && state != MODE_STATE_CLOSE) {
885 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
886 IScanService *pScanService = WifiServiceManager::GetInstance().GetScanServiceInst(i);
887 if (pScanService == nullptr) {
888 WIFI_LOGE("scan service is NOT start!");
889 continue;
890 }
891 pScanService->OnStandbyStateChanged(sleeping);
892 }
893 lastSleepState = sleeping;
894 }
895 if (napped || sleeping) {
896 WifiConfigCenter::GetInstance().SetPowerIdelState(MODE_STATE_OPEN);
897 } else {
898 WifiConfigCenter::GetInstance().SetPowerIdelState(MODE_STATE_CLOSE);
899 }
900 }
901
RegisterNotificationEvent()902 void WifiEventSubscriberManager::RegisterNotificationEvent()
903 {
904 std::unique_lock<std::mutex> lock(notificationEventMutex);
905 if (notificationTimerId != 0) {
906 WifiTimer::GetInstance()->UnRegister(notificationTimerId);
907 }
908 if (wifiNotificationSubsciber_) {
909 return;
910 }
911 OHOS::EventFwk::MatchingSkills matchingSkills;
912 matchingSkills.AddEvent(WIFI_EVENT_TAP_NOTIFICATION);
913 matchingSkills.AddEvent(WIFI_EVENT_DIALOG_ACCEPT);
914 matchingSkills.AddEvent(WIFI_EVENT_DIALOG_REJECT);
915 matchingSkills.AddEvent(EVENT_SETTINGS_WLAN_KEEP_CONNECTED);
916 WIFI_LOGI("RegisterNotificationEvent start");
917 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
918 subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
919 subscriberInfo.SetPermission("ohos.permission.SET_WIFI_CONFIG");
920 wifiNotificationSubsciber_ = std::make_shared<NotificationEventSubscriber>(subscriberInfo);
921 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(wifiNotificationSubsciber_)) {
922 WIFI_LOGE("WifiNotification SubscribeCommonEvent() failed");
923 wifiNotificationSubsciber_ = nullptr;
924 WifiTimer::TimerCallback timeoutCallBack =
925 std::bind(&WifiEventSubscriberManager::RegisterNotificationEvent, this);
926 WifiTimer::GetInstance()->Register(timeoutCallBack, notificationTimerId, TIMEOUT_EVENT_SUBSCRIBER, false);
927 WIFI_LOGI("RegisterNotificationEvent retry, notificationTimerId = %{public}u", notificationTimerId);
928 } else {
929 WIFI_LOGI("RegisterNotificationEvent success");
930 }
931 }
932
UnRegisterNotificationEvent()933 void WifiEventSubscriberManager::UnRegisterNotificationEvent()
934 {
935 std::unique_lock<std::mutex> lock(notificationEventMutex);
936 if (notificationTimerId != 0) {
937 WifiTimer::GetInstance()->UnRegister(notificationTimerId);
938 }
939 if (!wifiNotificationSubsciber_) {
940 return;
941 }
942 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(wifiNotificationSubsciber_)) {
943 WIFI_LOGE("UnRegisterNotificationEvent failed");
944 }
945 wifiNotificationSubsciber_ = nullptr;
946 WIFI_LOGI("UnRegisterNotificationEvent finished");
947 }
948
NotificationEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)949 NotificationEventSubscriber::NotificationEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
950 : CommonEventSubscriber(subscriberInfo)
951 {
952 WIFI_LOGI("NotificationEventSubscriber enter");
953 }
954
~NotificationEventSubscriber()955 NotificationEventSubscriber::~NotificationEventSubscriber()
956 {
957 WIFI_LOGI("~NotificationEventSubscriber enter");
958 }
959
OnReceiveWlanKeepConnected(const OHOS::EventFwk::CommonEventData & eventData)960 void NotificationEventSubscriber::OnReceiveWlanKeepConnected(const OHOS::EventFwk::CommonEventData &eventData)
961 {
962 const int code = eventData.GetCode();
963 WifiLinkedInfo linkedInfo;
964 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
965 int networkId = linkedInfo.networkId;
966 WIFI_LOGI("received the WlanKeepConnected, code == %{public}d", code);
967 if (code == 1) { // The user clicks the use button.
968 WifiNetAgent::GetInstance().RestoreWifiConnection();
969 WIFI_LOGI("change the value of AcceptUnvalidated to true");
970 WifiSettings::GetInstance().SetAcceptUnvalidated(networkId);
971 WifiSettings::GetInstance().SyncDeviceConfig();
972 }
973 }
974
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & eventData)975 void NotificationEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData)
976 {
977 std::string action = eventData.GetWant().GetAction();
978 WIFI_LOGI("OnReceiveNotificationEvent action[%{public}s]", action.c_str());
979 if (action == WIFI_EVENT_TAP_NOTIFICATION) {
980 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
981 IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i);
982 if (pService != nullptr) {
983 pService->StartPortalCertification();
984 }
985 }
986 } else if (action == WIFI_EVENT_DIALOG_ACCEPT) {
987 int dialogType = eventData.GetWant().GetIntParam("dialogType", 0);
988 WIFI_LOGI("dialogType[%{public}d]", dialogType);
989 if (dialogType == static_cast<int>(WifiDialogType::CANDIDATE_CONNECT)) {
990 int candidateNetworkId = WifiConfigCenter::GetInstance().GetSelectedCandidateNetworkId();
991 if (candidateNetworkId == INVALID_NETWORK_ID) {
992 WIFI_LOGI("OnReceiveNotificationEvent networkid is invalid");
993 return;
994 }
995 IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(0);
996 if (pService != nullptr) {
997 pService->ConnectToNetwork(candidateNetworkId);
998 }
999 }
1000 } else if (action == EVENT_SETTINGS_WLAN_KEEP_CONNECTED) {
1001 OnReceiveWlanKeepConnected(eventData);
1002 } else {
1003 int dialogType = eventData.GetWant().GetIntParam("dialogType", 0);
1004 WIFI_LOGI("dialogType[%{public}d]", dialogType);
1005 if (dialogType == static_cast<int>(WifiDialogType::CANDIDATE_CONNECT)) {
1006 WifiConfigCenter::GetInstance().SetSelectedCandidateNetworkId(INVALID_NETWORK_ID);
1007 }
1008 }
1009 }
1010
1011 #ifdef HAS_POWERMGR_PART
RegisterPowermgrEvent()1012 void WifiEventSubscriberManager::RegisterPowermgrEvent()
1013 {
1014 std::unique_lock<std::mutex> lock(powermgrEventMutex);
1015 if (wifiPowermgrEventSubsciber_) {
1016 return;
1017 }
1018 OHOS::EventFwk::MatchingSkills matchingSkills;
1019 matchingSkills.AddEvent(COMMON_EVENT_POWER_MANAGER_STATE_CHANGED);
1020 WIFI_LOGI("RegisterPowermgrEvent start");
1021 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
1022 subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
1023 subscriberInfo.SetPermission("ohos.permission.SET_WIFI_CONFIG");
1024 wifiPowermgrEventSubsciber_ = std::make_shared<PowermgrEventSubscriber>(subscriberInfo);
1025 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(wifiPowermgrEventSubsciber_)) {
1026 WIFI_LOGE("Powermgr SubscribeCommonEvent() failed");
1027 wifiPowermgrEventSubsciber_ = nullptr;
1028 } else {
1029 WIFI_LOGI("RegisterCesEvent success");
1030 }
1031 }
1032
UnRegisterPowermgrEvent()1033 void WifiEventSubscriberManager::UnRegisterPowermgrEvent()
1034 {
1035 std::unique_lock<std::mutex> lock(powermgrEventMutex);
1036 if (!wifiPowermgrEventSubsciber_) {
1037 return;
1038 }
1039 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(wifiPowermgrEventSubsciber_)) {
1040 WIFI_LOGE("UnRegisterPowermgrEvent failed");
1041 }
1042 wifiPowermgrEventSubsciber_ = nullptr;
1043 WIFI_LOGI("UnRegisterPowermgrEvent finished");
1044 }
1045
PowermgrEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)1046 PowermgrEventSubscriber::PowermgrEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
1047 : CommonEventSubscriber(subscriberInfo)
1048 {
1049 WIFI_LOGI("PowermgrEventSubscriber enter");
1050 }
1051
~PowermgrEventSubscriber()1052 PowermgrEventSubscriber::~PowermgrEventSubscriber()
1053 {
1054 WIFI_LOGI("~PowermgrEventSubscriber enter");
1055 }
1056
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & eventData)1057 void PowermgrEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData)
1058 {
1059 std::string action = eventData.GetWant().GetAction();
1060 WIFI_LOGI("Receive ForceSleep Event: %{public}s", action.c_str());
1061 #ifdef FEATURE_HPF_SUPPORT
1062 const int enterForceSleep = 0x30;
1063 const int exitForceSleep = 0x31;
1064 if (action == COMMON_EVENT_POWER_MANAGER_STATE_CHANGED) {
1065 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
1066 if (eventData.GetCode() == enterForceSleep) { // STATE_ENTER_FORCESLEEP
1067 WIFI_LOGI("Receive ForceSleep Event: %{public}d", enterForceSleep);
1068 WifiManager::GetInstance().InstallPacketFilterProgram(MODE_STATE_FORCESLEEP, i);
1069 }
1070 if (eventData.GetCode() == exitForceSleep) {
1071 WIFI_LOGI("Receive ForceSleep Event: %{public}d", exitForceSleep);
1072 WifiManager::GetInstance().InstallPacketFilterProgram(MODE_STATE_EXIT_FORCESLEEP, i);
1073 }
1074 }
1075 }
1076 #endif
1077 }
1078
1079 #endif
1080 #ifdef SUPPORT_ClOUD_WIFI_ASSET
RegisterAssetEvent()1081 void WifiEventSubscriberManager::RegisterAssetEvent()
1082 {
1083 std::unique_lock<std::mutex> lock(AssetEventMutex);
1084 if (assetMgrId != 0) {
1085 WifiTimer::GetInstance()->UnRegister(assetMgrId);
1086 }
1087 if (wifiAssetrEventSubsciber_) {
1088 return;
1089 }
1090 OHOS::EventFwk::MatchingSkills matchingSkills;
1091 matchingSkills.AddEvent(COMMON_EVENT_ASSETCLOUD_MANAGER_STATE_CHANGED);
1092 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
1093 subscriberInfo.SetPublisherUid(ASSETID);
1094 wifiAssetrEventSubsciber_ = std::make_shared<AssetEventSubscriber>(subscriberInfo);
1095 WIFI_LOGI("RegisterAssetEvent start");
1096 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(wifiAssetrEventSubsciber_)) {
1097 WIFI_LOGE("AssetCloud SubscribeCommonEvent() failed");
1098 wifiAssetrEventSubsciber_ = nullptr;
1099 WifiTimer::TimerCallback timeoutCallBack = std::bind(&WifiEventSubscriberManager::RegisterAssetEvent, this);
1100 WifiTimer::GetInstance()->Register(timeoutCallBack, assetMgrId, TIMEOUT_EVENT_SUBSCRIBER, false);
1101 WIFI_LOGI("RegisterAssetEvent retry, powerMgrId = %{public}u", assetMgrId);
1102 } else {
1103 WIFI_LOGI("RegisterAssetEvent success");
1104 }
1105 }
1106
UnRegisterAssetEvent()1107 void WifiEventSubscriberManager::UnRegisterAssetEvent()
1108 {
1109 std::unique_lock<std::mutex> lock(AssetEventMutex);
1110 if (assetMgrId != 0) {
1111 WifiTimer::GetInstance()->UnRegister(assetMgrId);
1112 }
1113 if (!wifiAssetrEventSubsciber_) {
1114 return;
1115 }
1116 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(wifiAssetrEventSubsciber_)) {
1117 WIFI_LOGE("UnRegisterAssetEvent failed");
1118 }
1119 wifiAssetrEventSubsciber_ = nullptr;
1120 WIFI_LOGI("UnRegisterAssetEvent finished");
1121 }
1122
AssetEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)1123 AssetEventSubscriber::AssetEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
1124 : CommonEventSubscriber(subscriberInfo)
1125 {
1126 WIFI_LOGI("AssetEventSubscriber enter");
1127 }
1128
~AssetEventSubscriber()1129 AssetEventSubscriber::~AssetEventSubscriber()
1130 {
1131 WIFI_LOGI("~AssetEventSubscriber enter");
1132 }
1133
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & eventData)1134 void AssetEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData)
1135 {
1136 std::string action = eventData.GetWant().GetAction();
1137 WIFI_LOGI("AssetListerner OnReceiveEvent action: %{public}s", action.c_str());
1138 if (action != COMMON_EVENT_ASSETCLOUD_MANAGER_STATE_CHANGED) {
1139 return;
1140 }
1141 // Do not sync from cloud during connecting
1142 for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
1143 IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i);
1144 if (pService != nullptr) {
1145 WifiLinkedInfo linkedInfo;
1146 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo, i);
1147 if (linkedInfo.connState == ConnState::CONNECTING) {
1148 return;
1149 }
1150 }
1151 }
1152 WifiAssetManager::GetInstance().CloudAssetSync();
1153 }
1154 #endif
OnReceiveUserUnlockedEvent(const OHOS::EventFwk::CommonEventData & eventData)1155 void CesEventSubscriber::OnReceiveUserUnlockedEvent(const OHOS::EventFwk::CommonEventData &eventData)
1156 {
1157 WIFI_LOGI("OnReceiveUserUnlockedEvent");
1158 #ifdef SUPPORT_ClOUD_WIFI_ASSET
1159 WifiAssetManager::GetInstance().InitUpLoadLocalDeviceSync();
1160 #endif
1161 }
1162
RegisterNetworkStateChangeEvent()1163 void WifiEventSubscriberManager::RegisterNetworkStateChangeEvent()
1164 {
1165 std::unique_lock<std::mutex> lock(networkStateChangeEventMutex);
1166 if (networkStateChangeTimerId != 0) {
1167 WifiTimer::GetInstance()->UnRegister(networkStateChangeTimerId);
1168 }
1169 if (networkStateChangeSubsciber_) {
1170 return;
1171 }
1172 EventFwk::MatchingSkills matchingSkills;
1173 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED);
1174 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
1175 networkStateChangeSubsciber_
1176 = std::make_shared<NetworkStateChangeSubscriber>(subscriberInfo);
1177 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(networkStateChangeSubsciber_)) {
1178 WIFI_LOGE("network state change subscribe failed");
1179 networkStateChangeSubsciber_ = nullptr;
1180 WifiTimer::TimerCallback timeoutCallBack =
1181 std::bind(&WifiEventSubscriberManager::RegisterNetworkStateChangeEvent, this);
1182 WifiTimer::GetInstance()->Register(timeoutCallBack, networkStateChangeTimerId, TIMEOUT_EVENT_SUBSCRIBER, false);
1183 WIFI_LOGI("RegisterNetworkStateChangeEvent retry, timerId = %{public}u", networkStateChangeTimerId);
1184 } else {
1185 WIFI_LOGI("RegisterNetworkStateChangeEvent success");
1186 }
1187 }
1188
UnRegisterNetworkStateChangeEvent()1189 void WifiEventSubscriberManager::UnRegisterNetworkStateChangeEvent()
1190 {
1191 std::unique_lock<std::mutex> lock(networkStateChangeEventMutex);
1192 if (networkStateChangeTimerId != 0) {
1193 WifiTimer::GetInstance()->UnRegister(networkStateChangeTimerId);
1194 }
1195 if (!networkStateChangeSubsciber_) {
1196 return;
1197 }
1198 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(networkStateChangeSubsciber_)) {
1199 WIFI_LOGE("UnRegisterNetworkStateChangeEvent failed");
1200 }
1201 networkStateChangeSubsciber_ = nullptr;
1202 WIFI_LOGI("UnRegisterNetworkStateChangeEvent finished");
1203 }
1204
NetworkStateChangeSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)1205 NetworkStateChangeSubscriber::NetworkStateChangeSubscriber(
1206 const EventFwk::CommonEventSubscribeInfo &subscriberInfo) : CommonEventSubscriber(subscriberInfo)
1207 {
1208 WIFI_LOGI("NetworkStateChangeSubscriber enter");
1209 }
1210
OnReceiveEvent(const EventFwk::CommonEventData & eventData)1211 void NetworkStateChangeSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
1212 {
1213 const auto &action = eventData.GetWant().GetAction();
1214 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED) {
1215 WifiCountryCodeManager::GetInstance().TriggerUpdateWifiCountryCode(TRIGGER_UPDATE_REASON_TEL_NET_CHANGE);
1216 }
1217 }
1218
RegisterWifiScanChangeEvent()1219 void WifiEventSubscriberManager::RegisterWifiScanChangeEvent()
1220 {
1221 std::unique_lock<std::mutex> lock(wifiScanChangeEventMutex);
1222 if (wifiScanChangeTimerId != 0) {
1223 WifiTimer::GetInstance()->UnRegister(wifiScanChangeTimerId);
1224 }
1225 if (wifiScanEventChangeSubscriber_) {
1226 return;
1227 }
1228 EventFwk::MatchingSkills matchingSkills;
1229 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_SCAN_FINISHED);
1230 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
1231 wifiScanEventChangeSubscriber_
1232 = std::make_shared<WifiScanEventChangeSubscriber>(subscriberInfo);
1233 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(wifiScanEventChangeSubscriber_)) {
1234 WIFI_LOGE("network state change subscribe failed");
1235 wifiScanEventChangeSubscriber_ = nullptr;
1236 WifiTimer::TimerCallback timeoutCallBack =
1237 std::bind(&WifiEventSubscriberManager::RegisterWifiScanChangeEvent, this);
1238 WifiTimer::GetInstance()->Register(timeoutCallBack, wifiScanChangeTimerId, TIMEOUT_EVENT_SUBSCRIBER, false);
1239 WIFI_LOGI("RegisterWifiScanChangeEvent retry, wifiScanChangeTimerId = %{public}u", wifiScanChangeTimerId);
1240 } else {
1241 WIFI_LOGI("RegisterWifiScanChangeEvent success");
1242 }
1243 }
1244
UnRegisterWifiScanChangeEvent()1245 void WifiEventSubscriberManager::UnRegisterWifiScanChangeEvent()
1246 {
1247 std::unique_lock<std::mutex> lock(wifiScanChangeEventMutex);
1248 if (wifiScanChangeTimerId != 0) {
1249 WifiTimer::GetInstance()->UnRegister(wifiScanChangeTimerId);
1250 }
1251 if (!wifiScanEventChangeSubscriber_) {
1252 return;
1253 }
1254 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(wifiScanEventChangeSubscriber_)) {
1255 WIFI_LOGE("UnRegisterWifiScanChangeEvent failed");
1256 }
1257 wifiScanEventChangeSubscriber_ = nullptr;
1258 WIFI_LOGI("UnRegisterWifiScanChangeEvent finished");
1259 }
1260
WifiScanEventChangeSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)1261 WifiScanEventChangeSubscriber::WifiScanEventChangeSubscriber(
1262 const EventFwk::CommonEventSubscribeInfo &subscriberInfo) : CommonEventSubscriber(subscriberInfo)
1263 {
1264 WIFI_LOGI("WifiScanEventChangeSubscriber enter");
1265 }
1266
OnReceiveEvent(const EventFwk::CommonEventData & eventData)1267 void WifiScanEventChangeSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
1268 {
1269 const auto &action = eventData.GetWant().GetAction();
1270 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_SCAN_FINISHED &&
1271 eventData.GetCode() == static_cast<int>(ScanHandleNotify::SCAN_OK)) {
1272 WifiCountryCodeManager::GetInstance().TriggerUpdateWifiCountryCode(TRIGGER_UPDATE_REASON_SCAN_CHANGE);
1273 }
1274 }
1275 } // namespace Wifi
1276 } // namespace OHOS
1277 #endif
1278