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