diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn b/wifi/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn index 36f5b0c7892dfb11884c4c2885c5c33814d2464a..609f577b63326875a1677dbb687f2d50f8911d0a 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn @@ -26,21 +26,32 @@ if (defined(ohos_lite)) { if (defined(ohos_lite)) { shared_library("wifi_service_base") { sources = [ + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/handler.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/internal_message.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/message_queue.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/state.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/state_machine.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_permission_helper.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_permission_utils.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy_lite.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub_lite.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy_lite.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub_lite.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.cpp", "wifi_auth_center.cpp", "wifi_config_center.cpp", - "wifi_device_callback_proxy_lite.cpp", - "wifi_device_service_impl.cpp", - "wifi_device_stub_lite.cpp", "wifi_dumper.cpp", "wifi_internal_event_dispatcher_lite.cpp", "wifi_manager.cpp", "wifi_protect.cpp", "wifi_protect_manager.cpp", - "wifi_scan_callback_proxy_lite.cpp", - "wifi_scan_service_impl.cpp", - "wifi_scan_stub_lite.cpp", "wifi_service_manager.cpp", "wifi_system_ability_listerner.cpp", ] @@ -57,12 +68,16 @@ if (defined(ohos_lite)) { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/config", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/common", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/wifi_hal_interface", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/client/idl_client", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/client/idl_client/idl_interface", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage", "$DHCP_ROOT_DIR/interfaces/inner_api/include", "$DHCP_ROOT_DIR/interfaces/inner_api/interfaces", "$WIFI_ROOT_DIR/utils/inc", @@ -92,6 +107,7 @@ if (defined(ohos_lite)) { defines += [ "PERMISSION_ALWAYS_GRANT" ] } + configs -= [ "//build/lite/config:language_cpp" ] cflags_cc = [ "-std=c++17", "-fno-rtti", @@ -100,9 +116,9 @@ if (defined(ohos_lite)) { executable("wifi_manager_service") { sources = [ - "wifi_device_feature_lite.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_feature_lite.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_feature_lite.cpp", "wifi_sa_service_lite.c", - "wifi_scan_feature_lite.cpp", "wifi_service_main_lite.c", ] @@ -123,7 +139,9 @@ if (defined(ohos_lite)) { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/client/idl_client", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/client/idl_client/idl_interface", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa", "$DHCP_ROOT_DIR/interfaces/inner_api/include", "$DHCP_ROOT_DIR/interfaces/inner_api/interfaces", "$WIFI_ROOT_DIR/utils/inc", @@ -166,16 +184,22 @@ if (defined(ohos_lite)) { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/config", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/common", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/wifi_hal_interface", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/client/idl_client", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_native/client/idl_client/idl_interface", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_power_saving/rx_listen", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa", "$WIFI_ROOT_DIR/utils/inc", "//foundation/appexecfwk/adapter/interfaces/innerkits/appexecfwk_base/include", "//base/notification/common_event_service/interfaces/inner_api", @@ -211,12 +235,12 @@ if (defined(ohos_lite)) { part_name = "wifi" sources = [ - "wifi_scan_callback_proxy.cpp", - "wifi_scan_death_recipient.cpp", - "wifi_scan_mgr_service_impl.cpp", - "wifi_scan_mgr_stub.cpp", - "wifi_scan_service_impl.cpp", - "wifi_scan_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_death_recipient.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub.cpp", ] configs = [ ":wifi_manager_service_config", @@ -251,12 +275,12 @@ if (defined(ohos_lite)) { part_name = "wifi" sources = [ - "wifi_device_callback_proxy.cpp", - "wifi_device_death_recipient.cpp", - "wifi_device_mgr_service_impl.cpp", - "wifi_device_mgr_stub.cpp", - "wifi_device_service_impl.cpp", - "wifi_device_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_death_recipient.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub.cpp", ] configs = [ ":wifi_manager_service_config", @@ -312,12 +336,12 @@ if (defined(ohos_lite)) { part_name = "wifi" sources = [ - "wifi_hotspot_callback_proxy.cpp", - "wifi_hotspot_death_recipient.cpp", - "wifi_hotspot_mgr_service_impl.cpp", - "wifi_hotspot_mgr_stub.cpp", - "wifi_hotspot_service_impl.cpp", - "wifi_hotspot_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_callback_proxy.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_death_recipient.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_stub.cpp", ] configs = [ ":wifi_manager_service_config", @@ -364,10 +388,10 @@ if (defined(ohos_lite)) { sources = [ "$WIFI_ROOT_DIR/frameworks/native/src/wifi_hid2d_msg.cpp", "$WIFI_ROOT_DIR/frameworks/native/src/wifi_p2p_msg.cpp", - "wifi_p2p_callback_proxy.cpp", - "wifi_p2p_death_recipient.cpp", - "wifi_p2p_service_impl.cpp", - "wifi_p2p_stub.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_callback_proxy.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_service_impl.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_stub.cpp", ] configs = [ ":wifi_manager_service_config", @@ -417,15 +441,21 @@ if (defined(ohos_lite)) { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_datashare_utils.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_permission_helper.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_permission_utils.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_country_code/wifi_country_code_manager.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_country_code/wifi_country_code_policy.cpp", - "concrete_clientmode_manager.cpp", - "concrete_manager_state_machine.cpp", - "softap_manager.cpp", - "softap_manager_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.cpp", "wifi_auth_center.cpp", "wifi_config_center.cpp", - "wifi_controller_state_machine.cpp", "wifi_dumper.cpp", "wifi_internal_event_dispatcher.cpp", "wifi_location_mode_observer.cpp", diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_callback_proxy.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_callback_proxy.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_callback_proxy.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_callback_proxy.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_callback_proxy.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_callback_proxy.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_callback_proxy.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_callback_proxy.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_death_recipient.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_death_recipient.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_death_recipient.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_death_recipient.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_death_recipient.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_death_recipient.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_death_recipient.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_death_recipient.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_service_impl.cpp similarity index 96% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_service_impl.cpp index 53f9c56eb4b0c5f49f67060bf5826f3fb6f65048..8c9b3c2edcebd7107126bb49c0ee6c751a666a3a 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_service_impl.cpp @@ -85,8 +85,8 @@ void WifiHotspotMgrServiceImpl::OnStart() WIFI_LOGI("Start ap service!"); WifiManager::GetInstance().AddSupportedFeatures(WifiFeatures::WIFI_FEATURE_MOBILE_HOTSPOT); // Get airplane mode by datashare - WifiManager::GetInstance().GetAirplaneModeByDatashare(); - WifiManager::GetInstance().StartUnloadApSaTimer(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetAirplaneModeByDatashare(); + WifiManager::GetInstance().GetWifiHotspotManager()->StartUnloadApSaTimer(); } void WifiHotspotMgrServiceImpl::OnStop() diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_service_impl.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_service_impl.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_mgr_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_mgr_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_service_impl.cpp similarity index 86% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_service_impl.cpp index f6deeef56cbc46b0ce1ef0c8370b1572f6f6591c..9a91b125268c5d1092674a703dee176696407b51 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_service_impl.cpp @@ -333,7 +333,7 @@ ErrCode WifiHotspotServiceImpl::CheckCanEnableHotspot(const ServiceType type) return WIFI_OPT_PERMISSION_DENIED; } - WifiManager::GetInstance().GetAirplaneModeByDatashare(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetAirplaneModeByDatashare(); if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { WIFI_LOGI("current airplane mode and can not use ap, open failed!"); return WIFI_OPT_FORBID_AIRPLANE; @@ -352,74 +352,8 @@ ErrCode WifiHotspotServiceImpl::EnableHotspot(const ServiceType type) if (errCode != WIFI_OPT_SUCCESS) { return errCode; } -#ifdef OHOS_ARCH_LITE - WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(); - if (staState != WifiOprMidState::CLOSED) { -#ifdef FEATURE_STA_AP_EXCLUSION - errCode = WifiManager::GetInstance().AutoStopStaService(AutoStartOrStopServiceReason::STA_AP_EXCLUSION); - if (errCode != WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED) { - return errCode; - } -#else - WIFI_LOGI("current wifi state is %{public}d, please close sta first!", - static_cast(curState)); - return WIFI_OPT_NOT_SUPPORTED; -#endif - } - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetApMidState(m_id); - if (curState != WifiOprMidState::CLOSED) { - WIFI_LOGI("current ap is %{public}d, state is %{public}d", m_id, static_cast(curState)); - if (curState == WifiOprMidState::CLOSING) { /* when ap is closing, return */ - return WIFI_OPT_OPEN_FAIL_WHEN_CLOSING; - } else { - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - } - if (!WifiConfigCenter::GetInstance().SetApMidState(curState, WifiOprMidState::OPENING, m_id)) { - WIFI_LOGI("current ap is %{public}d, set ap mid state opening failed!" - "may be other activity has been operated", m_id); - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - errCode = WIFI_OPT_FAILED; - do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_AP) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_AP); - break; - } - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(m_id); - if (pService == nullptr) { - WIFI_LOGE("Instance %{public}d get hotspot service is null!", m_id); - break; - } - errCode = pService->RegisterApServiceCallbacks(WifiManager::GetInstance().GetApCallback()); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("Register ap service callback failed!"); - break; - } - errCode = pService->RegisterApServiceCallbacks(WifiCountryCodeManager::GetInstance().GetApCallback()); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("WifiCountryCodeManager Register ap service callback failed! ret=%{public}d!", - static_cast(errCode)); - break; - } - errCode = pService->EnableHotspot(); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service enable ap failed, ret %{public}d!", static_cast(errCode)); - break; - } - } while (false); - if (errCode != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, m_id); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, m_id); - } else { - WifiManager::GetInstance().StopUnloadApSaTimer(); - WifiConfigCenter::GetInstance().SetStaApExclusionType(static_cast(StaApExclusionType::INITIAL_TYPE)); - } -#else - errCode = WifiManager::GetInstance().SoftapToggled(1, m_id); -#endif - return errCode; + return WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(1, m_id); } ErrCode WifiHotspotServiceImpl::DisableHotspot(const ServiceType type) @@ -433,44 +367,8 @@ ErrCode WifiHotspotServiceImpl::DisableHotspot(const ServiceType type) WIFI_LOGE("EnableHotspot:VerifyManageWifiHotspotPermission PERMISSION_DENIED!"); return WIFI_OPT_PERMISSION_DENIED; } -#ifdef OHOS_ARCH_LITE - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetApMidState(m_id); - if (curState != WifiOprMidState::RUNNING) { - WIFI_LOGI("current ap state is %{public}d", static_cast(curState)); - if (curState == WifiOprMidState::OPENING) { /* when ap is opening, return */ - return WIFI_OPT_CLOSE_FAIL_WHEN_OPENING; - } else { - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - } - if (!WifiConfigCenter::GetInstance().SetApMidState(curState, WifiOprMidState::CLOSING, m_id)) { - WIFI_LOGI("set ap mid state closing failed! may be other activity has been operated"); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(m_id); - if (pService == nullptr) { - WIFI_LOGE("Instance %{public}d get hotspot service is null!", m_id); - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSED, m_id); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, m_id); - return WIFI_OPT_SUCCESS; - } - ErrCode ret = pService->DisableHotspot(); - if (ret != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSING, WifiOprMidState::RUNNING, m_id); - return ret; - } -#ifdef FEATURE_STA_AP_EXCLUSION - if (WifiConfigCenter::GetInstance().GetStaApExclusionType() - == static_cast(StaApExclusionType::USER_OPEN_AP_AUTO_STOP_WIFI)) { - WifiConfigCenter::GetInstance().SetStaApExclusionType( - static_cast(StaApExclusionType::USER_CLOSE_AP_AUTO_START_WIFI)); - } -#endif - return WIFI_OPT_SUCCESS; -#else - ErrCode errCode = WifiManager::GetInstance().SoftapToggled(0, m_id); - return errCode; -#endif + + return WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(0, m_id); } ErrCode WifiHotspotServiceImpl::AddBlockList(const StationInfo &info) diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_service_impl.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_service_impl.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa/wifi_hotspot_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_clientmode_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_clientmode_manager.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_clientmode_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_clientmode_manager.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.cpp similarity index 90% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.cpp index 03295c11a5db38d740b8fd377712938293c27e05..a5c66ed600fa59c3d27d3ba673ac5f51ba5c8772 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.cpp @@ -18,13 +18,10 @@ #include "wifi_manager.h" #include "wifi_service_manager.h" #include "wifi_config_center.h" -#include "wifi_chip_hal_interface.h" -#include "wifi_internal_event_dispatcher.h" #include "wifi_internal_msg.h" -#include "wifi_hisysevent.h" -#include "wifi_settings.h" -#include "wifi_common_event_helper.h" +#ifndef OHOS_ARCH_LITE #include "wifi_country_code_manager.h" +#endif namespace OHOS { namespace Wifi { @@ -103,7 +100,6 @@ void ConcreteMangerMachine::RegisterCallback(ConcreteModeCallback &callback) void ConcreteMangerMachine::SetTargetRole(ConcreteManagerRole role) { mTargetRole = static_cast(role); - StopConcreteTimer(); } ConcreteMangerMachine::DefaultState::DefaultState(ConcreteMangerMachine *concreteMangerMachine) @@ -446,8 +442,7 @@ ErrCode ConcreteMangerMachine::AutoStartStaService(int instId) WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_STA); break; } - StaServiceCallback mStaCallback = WifiManager::GetInstance().GetStaCallback(); - errCode = pService->RegisterStaServiceCallback(mStaCallback); + errCode = pService->RegisterStaServiceCallback(WifiManager::GetInstance().GetWifiStaManager()->GetStaCallback()); if (errCode != WIFI_OPT_SUCCESS) { WIFI_LOGE("Register sta service callback failed!"); break; @@ -471,9 +466,9 @@ ErrCode ConcreteMangerMachine::AutoStartStaService(int instId) WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA); return errCode; } - WifiManager::GetInstance().StopUnloadStaSaTimer(); + WifiManager::GetInstance().GetWifiStaManager()->StopUnloadStaSaTimer(); #ifdef FEATURE_P2P_SUPPORT - errCode = WifiManager::GetInstance().AutoStartP2pService(AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP); + errCode = WifiManager::GetInstance().GetWifiP2pManager()->AutoStartP2pService(); if (errCode != WIFI_OPT_SUCCESS && errCode != WIFI_OPT_OPEN_SUCC_WHEN_OPENED) { WIFI_LOGE("AutoStartStaService, AutoStartP2pService failed!"); } @@ -490,7 +485,7 @@ ErrCode ConcreteMangerMachine::AutoStopStaService(int instId) } ErrCode ret = WIFI_OPT_FAILED; #ifdef FEATURE_P2P_SUPPORT - ret = WifiManager::GetInstance().AutoStopP2pService(AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP); + ret = WifiManager::GetInstance().GetWifiP2pManager()->AutoStopP2pService(); if (ret != WIFI_OPT_SUCCESS && ret != WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED) { WIFI_LOGE("AutoStopStaService,AutoStopP2pService failed!"); } @@ -537,7 +532,7 @@ ErrCode ConcreteMangerMachine::AutoStartScanOnly(int instId) } WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::OPENING, instId); - WifiManager::GetInstance().CheckAndStartScanService(instId); + WifiManager::GetInstance().GetWifiScanManager()->CheckAndStartScanService(instId); IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); if (pService == nullptr) { WIFI_LOGE("[AutoStartScanOnly] scan service is null."); @@ -545,7 +540,7 @@ ErrCode ConcreteMangerMachine::AutoStartScanOnly(int instId) return WIFI_OPT_FAILED; } ErrCode ret = pService->StartWpa(); - if (ret != static_cast(WIFI_IDL_OPT_OK)) { + if (ret != static_cast(WIFI_OPT_SUCCESS)) { WIFI_LOGE("Start Wpa failed."); WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); return WIFI_OPT_FAILED; @@ -579,12 +574,12 @@ ErrCode ConcreteMangerMachine::AutoStopScanOnly(int instId) return WIFI_OPT_FAILED; } ErrCode ret = pService->CloseWpa(); - if (ret != static_cast(WIFI_IDL_OPT_OK)) { + if (ret != static_cast(WIFI_OPT_SUCCESS)) { WIFI_LOGE("Stop Wpa failed!"); WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); return WIFI_OPT_FAILED; } - WifiManager::GetInstance().CheckAndStopScanService(instId); + WifiManager::GetInstance().GetWifiScanManager()->CheckAndStopScanService(instId); WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); return WIFI_OPT_SUCCESS; } @@ -665,62 +660,32 @@ void ConcreteMangerMachine::HandleStaStart() void ConcreteMangerMachine::checkAndContinueToStopWifi(InternalMessage *msg) { if (WifiSettings::GetInstance().GetWifiStopState()) { - WIFI_LOGE("wifi is stoping"); + WIFI_LOGE("checkAndContinueToStopWifi: wifi is stoping"); return; } - mTargetRole = static_cast(ConcreteManagerRole::ROLE_UNKNOW); - StartConcreteStopTimer(); -} -uint32_t ConcreteMangerMachine::concreteStopTimerId{0}; -std::mutex ConcreteMangerMachine::concreteStopTimerMutex{}; -void ConcreteMangerMachine::ConcreteStopTimerCallback() -{ - if (mTargetRole != static_cast(ConcreteManagerRole::ROLE_UNKNOW)) { - StopConcreteTimer(); - return; - } - StopConcreteTimer(); + mTargetRole = static_cast(ConcreteManagerRole::ROLE_UNKNOW); WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(mid); - WIFI_LOGE("ConcreteStopTimerCallback, current sta state: %{public}d", staState); + WIFI_LOGI("checkAndContinueToStopWifi: current sta state: %{public}d", staState); if (staState == WifiOprMidState::CLOSING || staState == WifiOprMidState::OPENING) { return; } - WIFI_LOGE("Set WifiStopState is true."); + WifiSettings::GetInstance().SetWifiStopState(true); + WIFI_LOGI("Set WifiStopState is true."); if (staState == WifiOprMidState::RUNNING) { ErrCode ret = AutoStopStaService(mid); if (ret != WIFI_OPT_SUCCESS) { WIFI_LOGE("stop sta failed in timer ret = %{public}d", ret); WifiSettings::GetInstance().SetWifiStopState(false); - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); + auto &ins = WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine(); ins->HandleStaClose(mid); } } else { - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); + auto &ins = WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine(); ins->HandleStaClose(mid); } } -void ConcreteMangerMachine::StopConcreteTimer(void) -{ - WIFI_LOGE("StopConcreteTimer! concreteStopTimerId:%{public}u", concreteStopTimerId); - std::unique_lock lock(concreteStopTimerMutex); - WifiTimer::GetInstance()->UnRegister(concreteStopTimerId); - concreteStopTimerId = 0; - return; -} - -void ConcreteMangerMachine::StartConcreteStopTimer(void) -{ - WIFI_LOGE("StartConcreteStopTimer! concreteStopTimerId:%{public}u", concreteStopTimerId); - std::unique_lock lock(concreteStopTimerMutex); - if (concreteStopTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(ConcreteMangerMachine::ConcreteStopTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, concreteStopTimerId, STOP_WIFI_WAIT_TIME); - WIFI_LOGE("StartConcreteStopTimer success! concreteStopTimerId:%{public}u", concreteStopTimerId); - } - return; -} } // namespace Wifi } // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.h similarity index 95% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.h index eeba6df11a64aba228cb8d421560e681b470d425..25d4251e36b07fdc9cd4bb089f8aa86c52e636dd 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.h @@ -137,10 +137,6 @@ private: void HandleStaStop(); void HandleStaStart(); - static void StartConcreteStopTimer(void); - static void StopConcreteTimer(void); - static void ConcreteStopTimerCallback(); - DefaultState *pDefaultState; IdleState *pIdleState; ConnectState *pConnectState; @@ -149,8 +145,6 @@ private: static int mTargetRole; ConcreteModeCallback mcb; static int mid; - static uint32_t concreteStopTimerId; - static std::mutex concreteStopTimerMutex; }; } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.cpp similarity index 97% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.cpp index 2113f10ebf36af8c0ac317e5ef8ef5ef4254de5a..c5f829e63971251ed9edf5e00c72563aef1bc380 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.cpp @@ -232,8 +232,7 @@ ErrCode SoftapManagerMachine::AutoStartApService(int id) WIFI_LOGE("Instance get hotspot service is null!"); break; } - IApServiceCallbacks cb = WifiManager::GetInstance().GetApCallback(); - errCode = pService->RegisterApServiceCallbacks(cb); + errCode = pService->RegisterApServiceCallbacks(WifiManager::GetInstance().GetWifiHotspotManager()->GetApCallback()); if (errCode != WIFI_OPT_SUCCESS) { WIFI_LOGE("Register ap service callback failed!"); break; @@ -255,7 +254,7 @@ ErrCode SoftapManagerMachine::AutoStartApService(int id) WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, mid); return errCode; } - WifiManager::GetInstance().StopUnloadApSaTimer(); + WifiManager::GetInstance().GetWifiHotspotManager()->StopUnloadApSaTimer(); return WIFI_OPT_SUCCESS; } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.h similarity index 93% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.h index 4f05b9f6eb15fe9c797947d53082e6720bccd77b..56e584a32890947ee1efcf7d76f6660eb1e4c2fa 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.h @@ -97,9 +97,6 @@ private: ErrCode InitSoftapManagerStates(); ErrCode AutoStartApService(int id); ErrCode AutoStopApService(int id); - static void DealApStateChanged(ApState bState, int id = 0); - static void DealApGetStaJoin(const StationInfo &info, int id = 0); - static void DealApGetStaLeave(const StationInfo &info, int id = 0); void StopSoftap(); DefaultState *pDefaultState; IdleState *pIdleState; diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_define.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_define.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_define.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_define.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.cpp similarity index 94% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.cpp index 95d8c210cf830cec42852eb7d03c93afad7bc588..2f64c1a401eb8b6e49ea3b036ad38963f426297b 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.cpp @@ -103,6 +103,7 @@ bool WifiControllerMachine::DisableState::ExecuteStateMsg(InternalMessage *msg) } WIFI_LOGE("DisableState-msgCode=%{public}d is received.\n", msg->GetMessageName()); switch (msg->GetMessageName()) { +#ifdef FEATURE_AP_SUPPORT case CMD_SOFTAP_TOGGLED: if (msg->GetParam1()) { int id = msg->GetParam2(); @@ -111,6 +112,7 @@ bool WifiControllerMachine::DisableState::ExecuteStateMsg(InternalMessage *msg) pWifiControllerMachine->SwitchState(pWifiControllerMachine->pEnableState); } break; +#endif case CMD_WIFI_TOGGLED: case CMD_SCAN_ALWAYS_MODE_CHANGED: if (pWifiControllerMachine->ShouldEnableWifi()) { @@ -167,31 +169,19 @@ bool WifiControllerMachine::EnableState::ExecuteStateMsg(InternalMessage *msg) case CMD_SCAN_ALWAYS_MODE_CHANGED: HandleWifiToggleChangeInEnabledState(msg); break; +#ifdef FEATURE_AP_SUPPORT case CMD_SOFTAP_TOGGLED: HandleSoftapToggleChangeInEnabledState(msg); break; - case CMD_STA_START_FAILURE: - pWifiControllerMachine->HandleStaStartFailure(msg->GetParam1()); - break; - case CMD_CONCRETE_STOPPED: - pWifiControllerMachine->HandleConcreteStop(msg->GetParam1()); - break; - case CMD_AP_START: - pWifiControllerMachine->StopTimer(CMD_AP_START_TIME); - HandleApStart(msg->GetParam1()); - break; case CMD_AP_STOPPED: case CMD_AP_START_FAILURE: pWifiControllerMachine->StopTimer(CMD_AP_STOP_TIME); pWifiControllerMachine->StopSoftapCloseTimer(); pWifiControllerMachine->HandleSoftapStop(msg->GetParam1()); break; - case CMD_AIRPLANE_TOGGLED: - if (msg->GetParam1()) { - pWifiControllerMachine->HandleAirplaneOpen(); - } else { - pWifiControllerMachine->HandleAirplaneClose(); - } + case CMD_AP_START: + pWifiControllerMachine->StopTimer(CMD_AP_START_TIME); + HandleApStart(msg->GetParam1()); break; case CMD_AP_START_TIME: WriteSoftApOpenAndCloseFailedEvent(static_cast(SoftApperateType::OPEN_SOFT_AP_FAILED), "TIME_OUT"); @@ -199,6 +189,20 @@ bool WifiControllerMachine::EnableState::ExecuteStateMsg(InternalMessage *msg) case CMD_AP_STOP_TIME: WriteSoftApOpenAndCloseFailedEvent(static_cast(SoftApperateType::CLOSE_SOFT_AP_FAILED), "TIME_OUT"); break; +#endif + case CMD_STA_START_FAILURE: + pWifiControllerMachine->HandleStaStartFailure(msg->GetParam1()); + break; + case CMD_CONCRETE_STOPPED: + pWifiControllerMachine->HandleConcreteStop(msg->GetParam1()); + break; + case CMD_AIRPLANE_TOGGLED: + if (msg->GetParam1()) { + pWifiControllerMachine->HandleAirplaneOpen(); + } else { + pWifiControllerMachine->HandleAirplaneClose(); + } + break; default: break; } @@ -234,11 +238,13 @@ bool WifiControllerMachine::DefaultState::ExecuteStateMsg(InternalMessage *msg) void WifiControllerMachine::HandleAirplaneOpen() { WIFI_LOGI("airplane open set softap false"); +#ifdef FEATURE_AP_SUPPORT WifiSettings::GetInstance().SetSoftapToggledState(false); + StopAllSoftapManagers(); +#endif if (!WifiConfigCenter::GetInstance().GetOpenWifiWhenAirplaneMode()) { StopAllConcreteManagers(); } - StopAllSoftapManagers(); } void WifiControllerMachine::HandleAirplaneClose() @@ -246,10 +252,12 @@ void WifiControllerMachine::HandleAirplaneClose() if (!ShouldEnableWifi() || WifiSettings::GetInstance().GetWifiStopState()) { return; } +#ifdef FEATURE_AP_SUPPORT if (!WifiSettings::GetInstance().GetCoexSupport() && HasAnySoftApManager()) { WIFI_LOGE("HandleAirplaneClose, has softap in runing return."); return; } +#endif ConcreteManagerRole role = GetWifiRole(); if (role == ConcreteManagerRole::ROLE_UNKNOW) { WIFI_LOGE("Get unknow wifi role in HandleAirplaneClose."); @@ -263,6 +271,7 @@ void WifiControllerMachine::HandleAirplaneClose() } } +#ifdef FEATURE_AP_SUPPORT bool WifiControllerMachine::SoftApIdExit(int id) { if (!HasAnySoftApManager()) { @@ -276,6 +285,7 @@ bool WifiControllerMachine::SoftApIdExit(int id) } return false; } +#endif bool WifiControllerMachine::ConcreteIdExit(int id) { @@ -300,6 +310,7 @@ bool WifiControllerMachine::HasAnyConcreteManager() return true; } +#ifdef FEATURE_AP_SUPPORT bool WifiControllerMachine::HasAnySoftApManager() { std::unique_lock lock(softapManagerMutex); @@ -308,10 +319,15 @@ bool WifiControllerMachine::HasAnySoftApManager() } return true; } +#endif bool WifiControllerMachine::HasAnyManager() { - if (!HasAnyConcreteManager() && !HasAnySoftApManager()) { + if (!HasAnyConcreteManager() +#ifdef FEATURE_AP_SUPPORT + && !HasAnySoftApManager() +#endif + ) { return false; } return true; @@ -321,19 +337,18 @@ void WifiControllerMachine::MakeConcreteManager(ConcreteManagerRole role, int id { WIFI_LOGE("Enter MakeConcreteManager"); ConcreteClientModeManager *clientmode = new (std::nothrow) ConcreteClientModeManager(role, id); - ConcreteModeCallback cb = WifiManager::GetInstance().GetConcreteCallback(); - clientmode->RegisterCallback(cb); + clientmode->RegisterCallback(WifiManager::GetInstance().GetWifiTogglerManager()->GetConcreteCallback()); clientmode->InitConcreteManager(); std::unique_lock lock(concreteManagerMutex); concreteManagers.push_back(clientmode); } +#ifdef FEATURE_AP_SUPPORT void WifiControllerMachine::MakeSoftapManager(SoftApManager::Role role, int id) { WIFI_LOGE("Enter MakeSoftapManager"); SoftApManager *softapmode = new (std::nothrow) SoftApManager(role, id); - SoftApModeCallback cb = WifiManager::GetInstance().GetSoftApCallback(); - softapmode->RegisterCallback(cb); + softapmode->RegisterCallback(WifiManager::GetInstance().GetWifiTogglerManager()->GetSoftApCallback()); softapmode->InitSoftapManager(); std::unique_lock lock(softapManagerMutex); softapManagers.push_back(softapmode); @@ -347,13 +362,16 @@ bool WifiControllerMachine::ShouldEnableSoftap() } return false; } +#endif bool WifiControllerMachine::ShouldEnableWifi() { WIFI_LOGE("Enter ShouldEnableWifi"); - if (WifiManager::GetInstance().IsMdmForbidden()) { +#ifndef OHOS_ARCH_LITE + if (WifiManager::GetInstance().GetWifiEventSubscriberManager()->IsMdmForbidden()) { return false; } +#endif if (WifiSettings::GetInstance().GetWifiToggledState() || IsScanOnlyEnable()) { return true; } @@ -380,8 +398,11 @@ bool WifiControllerMachine::IsWifiEnable() bool WifiControllerMachine::IsScanOnlyEnable() { - if (WifiSettings::GetInstance().CheckScanOnlyAvailable(0) && - WifiManager::GetInstance().GetLocationModeByDatashare()) { + if (WifiSettings::GetInstance().CheckScanOnlyAvailable(0) +#ifndef OHOS_ARCH_LITE + && WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetLocationModeByDatashare() +#endif + ) { return true; } WIFI_LOGE("No need to StartScanOnly,return."); @@ -400,6 +421,7 @@ void WifiControllerMachine::StopAllConcreteManagers() } } +#ifdef FEATURE_AP_SUPPORT void WifiControllerMachine::StopSoftapManager(int id) { if (!HasAnySoftApManager()) { @@ -424,6 +446,7 @@ void WifiControllerMachine::StopAllSoftapManagers() (*iter)->GetSoftapMachine()->SendMessage(SOFTAP_CMD_STOP); } } +#endif void WifiControllerMachine::RmoveConcreteManager(int id) { @@ -445,6 +468,7 @@ void WifiControllerMachine::RmoveConcreteManager(int id) } } +#ifdef FEATURE_AP_SUPPORT void WifiControllerMachine::RmoveSoftapManager(int id) { SoftApManager *softapManager = nullptr; @@ -464,6 +488,7 @@ void WifiControllerMachine::RmoveSoftapManager(int id) delete softapManager; } } +#endif void WifiControllerMachine::HandleStaClose(int id) { @@ -510,11 +535,13 @@ void WifiControllerMachine::EnableState::HandleWifiToggleChangeInEnabledState(In return; } WifiSettings::GetInstance().SetWifiStopState(false); +#ifdef FEATURE_AP_SUPPORT if (!WifiSettings::GetInstance().GetCoexSupport() && pWifiControllerMachine->HasAnySoftApManager()) { pWifiControllerMachine->StopAllSoftapManagers(); return; } +#endif presentRole = pWifiControllerMachine->GetWifiRole(); if (presentRole == ConcreteManagerRole::ROLE_UNKNOW) { WIFI_LOGE("Get unknow wifi role in EnableState."); @@ -524,6 +551,7 @@ void WifiControllerMachine::EnableState::HandleWifiToggleChangeInEnabledState(In return; } +#ifdef FEATURE_AP_SUPPORT void WifiControllerMachine::EnableState::HandleSoftapToggleChangeInEnabledState(InternalMessage *msg) { int id = msg->GetParam2(); @@ -558,6 +586,7 @@ void WifiControllerMachine::EnableState::HandleSoftapToggleChangeInEnabledState( return; } } +#endif void WifiControllerMachine::HandleStaStartFailure(int id) { @@ -579,6 +608,7 @@ void WifiControllerMachine::HandleStaStart(int id) } } +#ifdef FEATURE_AP_SUPPORT void WifiControllerMachine::EnableState::HandleApStart(int id) { if (!pWifiControllerMachine->ShouldEnableSoftap()) { @@ -587,18 +617,21 @@ void WifiControllerMachine::EnableState::HandleApStart(int id) } pWifiControllerMachine->StartSoftapCloseTimer(); } +#endif void WifiControllerMachine::HandleConcreteStop(int id) { int airplanstate = WifiConfigCenter::GetInstance().GetAirplaneModeState(); RmoveConcreteManager(id); if (!WifiSettings::GetInstance().GetCoexSupport()) { +#ifdef FEATURE_AP_SUPPORT if (ShouldEnableSoftap() && airplanstate != MODE_STATE_OPEN && !SoftApIdExit(mApidStopWifi)) { MakeSoftapManager(SoftApManager::Role::ROLE_SOFTAP, mApidStopWifi); return; } - if (airplanstate != MODE_STATE_OPEN && !WifiManager::GetInstance().HasAnyApRuning()) { +#endif + if (airplanstate != MODE_STATE_OPEN && !WifiManager::GetInstance().GetWifiTogglerManager()->HasAnyApRuning()) { if (ShouldEnableWifi()) { ConcreteManagerRole presentRole = GetWifiRole(); MakeConcreteManager(presentRole, 0); @@ -619,6 +652,7 @@ void WifiControllerMachine::HandleConcreteStop(int id) } } +#ifdef FEATURE_AP_SUPPORT void WifiControllerMachine::HandleSoftapStop(int id) { int airplanstate; @@ -648,7 +682,7 @@ void WifiControllerMachine::HandleSoftapStop(int id) static void AlarmStopSoftap() { - WifiManager::GetInstance().SoftapToggled(0, 0); + WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(0, 0); } void WifiControllerMachine::StartSoftapCloseTimer() @@ -683,6 +717,7 @@ void WifiControllerMachine::StopSoftapCloseTimer() MiscServices::TimeServiceClient::GetInstance()->DestroyTimer(stopSoftapTimerId_); stopSoftapTimerId_ = 0; } +#endif } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.h similarity index 96% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.h index e90d6d8e37b12a5a8635a1774b287eed5f734fc7..0568980b7e428c899566b6d451d11512bf0e93bc 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.h @@ -23,7 +23,9 @@ #include "wifi_logger.h" #include "wifi_errcode.h" #include "concrete_clientmode_manager.h" +#ifdef FEATURE_AP_SUPPORT #include "softap_manager.h" +#endif namespace OHOS { namespace Wifi { @@ -55,7 +57,9 @@ public: private: void HandleApStart(int id); void HandleWifiToggleChangeInEnabledState(InternalMessage *msg); +#ifdef FEATURE_AP_SUPPORT void HandleSoftapToggleChangeInEnabledState(InternalMessage *msg); +#endif WifiControllerMachine *pWifiControllerMachine; }; @@ -75,14 +79,16 @@ public: ErrCode InitWifiControllerMachine(); void RmoveConcreteManager(int id); - void RmoveSoftapManager(int id); void HandleStaClose(int id); void HandleStaStart(int id); void HandleStaStartFailure(int id); void HandleConcreteStop(int id); +#ifdef FEATURE_AP_SUPPORT + void RmoveSoftapManager(int id); void HandleSoftapStop(int id); void StartSoftapCloseTimer(); void StopSoftapCloseTimer(); +#endif private: template @@ -106,18 +112,20 @@ private: void BuildStateTree(); ErrCode InitWifiStates(); bool HasAnyConcreteManager(); - bool HasAnySoftApManager(); bool HasAnyManager(); - bool SoftApIdExit(int id); bool ConcreteIdExit(int id); void MakeConcreteManager(ConcreteManagerRole role, int id); +#ifdef FEATURE_AP_SUPPORT + bool HasAnySoftApManager(); + bool SoftApIdExit(int id); void MakeSoftapManager(SoftApManager::Role role, int id); - bool ShouldEnableWifi(); bool ShouldEnableSoftap(); - ConcreteManagerRole GetWifiRole(); - void StopAllConcreteManagers(); void StopAllSoftapManagers(); void StopSoftapManager(int id); +#endif + bool ShouldEnableWifi(); + ConcreteManagerRole GetWifiRole(); + void StopAllConcreteManagers(); void SwitchRole(ConcreteManagerRole role); void HandleAirplaneOpen(); void HandleAirplaneClose(); @@ -129,12 +137,12 @@ private: DisableState *pDisableState; DefaultState *pDefaultState; std::vector concreteManagers; - std::vector softapManagers; - ConcreteModeCallback mConcreteCallback; - SoftApModeCallback mSoftapCallback; mutable std::mutex concreteManagerMutex; +#ifdef FEATURE_AP_SUPPORT + std::vector softapManagers; mutable std::mutex softapManagerMutex; uint64_t stopSoftapTimerId_ {0}; +#endif }; } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_location_mode_observer.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_location_mode_observer.cpp index 57e33804429ea631d57709863c8610ec2cf11c08..662e911f0f389e7200c1fefae727a355299d80cb 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_location_mode_observer.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_location_mode_observer.cpp @@ -22,18 +22,18 @@ namespace Wifi { void WifiLocationModeObserver::OnChange() { - WifiManager::GetInstance().DealLocationModeChangeEvent(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->DealLocationModeChangeEvent(); } void WifiDeviceProvisionObserver::OnChange() { - WifiManager::GetInstance().GetDeviceProvisionByDatashare(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetDeviceProvisionByDatashare(); } #ifndef OHOS_ARCH_LITE void SettingsMigrateObserver::OnChange() { - WifiManager::GetInstance().CheckAndStartStaByDatashare(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->CheckAndStartStaByDatashare(); } #endif diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp index c710ca86302e5ef67af4a305f1290f1561c31b95..2dcf2a3aa9250934bb9110c22d07d304ba28b359 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp @@ -16,8 +16,6 @@ #include "wifi_manager.h" #include #include "wifi_auth_center.h" -#include "wifi_chip_hal_interface.h" -#include "wifi_common_event_helper.h" #include "wifi_config_center.h" #include "wifi_global_func.h" #include "wifi_logger.h" @@ -25,67 +23,15 @@ #include "wifi_internal_event_dispatcher_lite.h" #else #include "wifi_internal_event_dispatcher.h" -#include "common_event_manager.h" -#include "common_event_support.h" -#include "wifi_sa_manager.h" -#include "common_timer_errors.h" -#include "wifi_datashare_utils.h" -#include "wifi_location_mode_observer.h" #include "wifi_country_code_manager.h" -#ifdef HAS_MOVEMENT_PART -#include "wifi_msdp_state_listener.h" #endif -#include "wifi_protect_manager.h" -#ifdef HAS_POWERMGR_PART -#include "wifi_power_state_listener.h" -#endif -#include "suspend/sleep_priority.h" -#endif -#include "wifi_sta_hal_interface.h" #include "wifi_service_manager.h" -#include "wifi_settings.h" -#include "define.h" -#include "wifi_config_center.h" #include "wifi_common_def.h" -#include "wifi_hisysevent.h" #include "wifi_common_util.h" namespace OHOS { namespace Wifi { DEFINE_WIFILOG_LABEL("WifiManager"); -int WifiManager::mCloseApIndex = 0; -#ifndef OHOS_ARCH_LITE -bool WifiManager::mIsMdmForbidden = false; -const uint32_t PROP_LEN = 26; -const uint32_t PROP_TRUE_LEN = 4; -const uint32_t PROP_FALSE_LEN = 5; -const uint32_t PROP_SUBCHIPTYPE_LEN = 10; -const std::string SUBCHIP_WIFI_PROP = "ohos.boot.odm.conn.schiptype"; -const std::string MDM_WIFI_PROP = "persist.edm.wifi_enable"; -const std::string SUPPORT_COEXCHIP = "bisheng"; -const std::string COEX_IFACENAME = "wlan1"; -const int SUPPORT_COEXCHIP_LEN = 7; -const std::string PROP_TRUE = "true"; -const std::string PROP_FALSE = "false"; -const uint32_t TIMEOUT_SCREEN_EVENT = 3000; -const uint32_t TIMEOUT_AIRPLANE_MODE_EVENT = 3000; -const uint32_t TIMEOUT_LOCATION_EVENT = 3000; -const uint32_t TIMEOUT_CHECK_LAST_STA_STATE_EVENT = 10 * 1000; -const uint32_t TIMEOUT_UNLOAD_WIFI_SA = 5 * 60 * 1000; -const uint32_t TIMEOUT_BATTERY_EVENT = 3000; -using TimeOutCallback = std::function; - -static sptr locationModeObserver_ = nullptr; -static bool islocationModeObservered = false; -static sptr deviceProvisionObserver_ = nullptr; -static sptr settingsMigrateObserver_ = nullptr; -#ifdef HAS_POWERMGR_PART -static sptr powerStateListener_ = nullptr; -#endif -#ifdef HAS_MOVEMENT_PART -static sptr deviceMovementCallback_ = nullptr; -#endif -#endif WifiManager &WifiManager::GetInstance() { @@ -97,7 +43,6 @@ WifiManager &WifiManager::GetInstance() if (gWifiManager.Init() != 0) { WIFI_LOGE("Failed to `WifiManager::Init` !"); } - gWifiManager.InitSubscribeListener(); } } @@ -112,232 +57,297 @@ WifiManager::~WifiManager() Exit(); } -#ifdef OHOS_ARCH_LITE -ErrCode WifiManager::AutoStartStaService(AutoStartOrStopServiceReason reason, int instId) +int WifiManager::Init() { - WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(instId); - WIFI_LOGD("AutoStartStaService, current sta state:%{public}d, reason:%{public}d", staState, reason); - if (staState != WifiOprMidState::CLOSED) { - if (staState == WifiOprMidState::CLOSING) { - return WIFI_OPT_OPEN_FAIL_WHEN_CLOSING; - } else { - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } +#ifndef OHOS_ARCH_LITE + if (WifiCountryCodeManager::GetInstance().Init() < 0) { + WIFI_LOGE("WifiCountryCodeManager Init failed!"); + mInitStatus = WIFI_COUNTRY_CODE_MANAGER_INIT_FAILED; + return -1; + } +#endif + if (WifiConfigCenter::GetInstance().Init() < 0) { + WIFI_LOGE("WifiConfigCenter Init failed!"); + mInitStatus = CONFIG_CENTER_INIT_FAILED; + return -1; + } + if (WifiAuthCenter::GetInstance().Init() < 0) { + WIFI_LOGE("WifiAuthCenter Init failed!"); + mInitStatus = AUTH_CENTER_INIT_FAILED; + return -1; + } + if (WifiServiceManager::GetInstance().Init() < 0) { + WIFI_LOGE("WifiServiceManager Init failed!"); + mInitStatus = SERVICE_MANAGER_INIT_FAILED; + return -1; + } + if (WifiInternalEventDispatcher::GetInstance().Init() < 0) { + WIFI_LOGE("WifiInternalEventDispatcher Init failed!"); + mInitStatus = EVENT_BROADCAST_INIT_FAILED; + return -1; + } + mCloseServiceThread = std::thread(&WifiManager::DealCloseServiceMsg, this); + pthread_setname_np(mCloseServiceThread.native_handle(), "WifiCloseThread"); +#ifndef OHOS_ARCH_LITE + wifiEventSubscriberManager = std::make_unique(); +#endif + wifiStaManager = std::make_unique(); + wifiScanManager = std::make_unique(); + wifiTogglerManager = std::make_unique(); +#ifdef FEATURE_AP_SUPPORT + wifiHotspotManager = std::make_unique(); +#endif +#ifdef FEATURE_P2P_SUPPORT + wifiP2pManager = std::make_unique(); +#endif + mInitStatus = INIT_OK; + if (WifiServiceManager::GetInstance().CheckPreLoadService() < 0) { + WIFI_LOGE("WifiServiceManager check preload feature service failed!"); + WifiManager::GetInstance().Exit(); + return -1; + } + if (WifiConfigCenter::GetInstance().GetStaLastRunState()) { /* Automatic startup upon startup */ + WIFI_LOGI("AutoStartServiceThread"); + WifiSettings::GetInstance().SetWifiToggledState(true); + std::thread startStaSrvThread(&WifiManager::AutoStartServiceThread, this); + pthread_setname_np(startStaSrvThread.native_handle(), "AutoStartThread"); + startStaSrvThread.detach(); + } else { + /** + * The sta service automatically starts upon startup. After the sta + * service is started, the scanning is directly started. + */ + AutoStartEnhanceService(); + wifiScanManager->CheckAndStartScanService(); + } + InitPidfile(); + return 0; +} + +void WifiManager::Exit() +{ + WIFI_LOGI("[WifiManager] Exit."); + WifiServiceManager::GetInstance().UninstallAllService(); + WifiInternalEventDispatcher::GetInstance().Exit(); + if (mCloseServiceThread.joinable()) { + PushServiceCloseMsg(WifiCloseServiceCode::SERVICE_THREAD_EXIT); + mCloseServiceThread.join(); + } + if (wifiStaManager) { + wifiStaManager.reset(); + } + if (wifiScanManager) { + wifiScanManager.reset(); + } + if (wifiTogglerManager) { + wifiTogglerManager.reset(); + } +#ifdef FEATURE_AP_SUPPORT + if (wifiHotspotManager) { + wifiHotspotManager.reset(); + } +#endif +#ifdef FEATURE_P2P_SUPPORT + if (wifiP2pManager) { + wifiP2pManager.reset(); } - - if (!WifiConfigCenter::GetInstance().SetWifiMidState(staState, WifiOprMidState::OPENING, instId)) { - WIFI_LOGD("AutoStartStaService, set wifi mid state opening failed!"); - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; +#endif +#ifndef OHOS_ARCH_LITE + if (wifiEventSubscriberManager) { + wifiEventSubscriberManager.reset(); } +#endif + return; +} + +int WifiManager::GetSupportedFeatures(long &features) const +{ + long supportedFeatures = mSupportedFeatures; + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_INFRA); + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_INFRA_5G); + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_PASSPOINT); + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_AP_STA); + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_WPA3_SAE); + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_WPA3_SUITE_B); + supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_OWE); + features = supportedFeatures; + return 0; +} + +void WifiManager::AddSupportedFeatures(WifiFeatures feature) +{ + mSupportedFeatures |= static_cast(feature); +} + +void WifiManager::PushServiceCloseMsg(WifiCloseServiceCode code, int instId) +{ + std::unique_lock lock(mMutex); + WifiCloseServiceMsg msg; + msg.code = code; + msg.instId = instId; + mEventQue.push_back(msg); + mCondition.notify_one(); + return; +} + +void WifiManager::AutoStartEnhanceService(void) +{ + WIFI_LOGI("AutoStartEnhanceService start"); ErrCode errCode = WIFI_OPT_FAILED; do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_STA) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_STA); - break; - } - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(instId); - if (pService == nullptr) { - WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_STA); + if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_ENHANCE) < 0) { + WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_ENHANCE); break; } - errCode = pService->RegisterStaServiceCallback(WifiManager::GetInstance().GetStaCallback()); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("Register sta service callback failed!"); + IEnhanceService *pEnhanceService = WifiServiceManager::GetInstance().GetEnhanceServiceInst(); + if (pEnhanceService == nullptr) { + WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_ENHANCE); break; } - errCode = pService->EnableWifi(); + errCode = pEnhanceService->Init(); if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service enable sta failed, ret %{public}d!", static_cast(errCode)); + WIFI_LOGE("init Enhance service failed, ret %{public}d!", static_cast(errCode)); break; } } while (0); - if (errCode != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, instId); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, instId); - return errCode; - } + return; +} - StopUnloadStaSaTimer(); - if (reason == AutoStartOrStopServiceReason::STA_AP_EXCLUSION) { - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(instId) == - static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_AIRPLANEMODE)) { - WIFI_LOGI("AutoStartStaService, user opened wifi in airplane mode!"); - return WIFI_OPT_SUCCESS; - } +std::unique_ptr& WifiManager::GetWifiStaManager() +{ + return wifiStaManager; +} - int operatorWifiType = static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_NO_AIRPLANEMODE); - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - operatorWifiType = static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_AIRPLANEMODE); - WIFI_LOGI("AutoStartStaService, current airplane mode is opened, user open wifi!"); - } - WifiConfigCenter::GetInstance().SetOperatorWifiType(operatorWifiType, instId); - } -#ifdef FEATURE_P2P_SUPPORT - errCode = AutoStartP2pService(reason); - if (errCode != WIFI_OPT_SUCCESS && errCode != WIFI_OPT_OPEN_SUCC_WHEN_OPENED) { - WIFI_LOGE("AutoStartStaService, AutoStartP2pService failed!"); - } -#endif - return WIFI_OPT_SUCCESS; +std::unique_ptr& WifiManager::GetWifiScanManager() +{ + return wifiScanManager; } -ErrCode WifiManager::AutoStopStaService(AutoStartOrStopServiceReason reason, int instId) +std::unique_ptr& WifiManager::GetWifiTogglerManager() { - WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(instId); - WIFI_LOGI("AutoStopStaService, current sta state:%{public}d, reason:%{public}d", staState, reason); - if (staState != WifiOprMidState::RUNNING) { - if (staState == WifiOprMidState::OPENING) { - return WIFI_OPT_CLOSE_FAIL_WHEN_OPENING; - } else { - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - } + return wifiTogglerManager; +} - ErrCode ret = WIFI_OPT_FAILED; -#ifdef FEATURE_P2P_SUPPORT - ret = AutoStopP2pService(reason); - if (ret != WIFI_OPT_SUCCESS && ret != WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED) { - WIFI_LOGE("AutoStopStaService, AutoStopP2pService failed!"); - } +#ifdef FEATURE_AP_SUPPORT +std::unique_ptr& WifiManager::GetWifiHotspotManager() +{ + return wifiHotspotManager; +} #endif - if (!WifiConfigCenter::GetInstance().SetWifiMidState(staState, WifiOprMidState::CLOSING, instId)) { - WIFI_LOGI("AutoStopStaService, set wifi mid state opening failed!"); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(instId); - if (pService == nullptr) { - WIFI_LOGE("AutoStopStaService, Instance get sta service is null!"); - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSED, instId); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, instId); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - - ret = pService->DisableWifi(); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service disable sta failed, ret %{public}d!", static_cast(ret)); - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSING, WifiOprMidState::RUNNING, instId); - return ret; - } - - WifiConfigCenter::GetInstance().SetStaLastRunState(false, instId); - if (reason == AutoStartOrStopServiceReason::AIRPLANE_MODE) { - WIFI_LOGI("DealOpenAirplaneModeEvent, auto stop wifi success!"); - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::CLOSE_WIFI_DUE_TO_AIRPLANEMODE_OPENED), instId); - } else if (reason == AutoStartOrStopServiceReason::STA_AP_EXCLUSION) { - WifiConfigCenter::GetInstance().SetStaApExclusionType( - static_cast(StaApExclusionType::USER_OPEN_AP_AUTO_STOP_WIFI)); - } - return WIFI_OPT_SUCCESS; +#ifdef FEATURE_P2P_SUPPORT +std::unique_ptr& WifiManager::GetWifiP2pManager() +{ + return wifiP2pManager; } +#endif -void WifiManager::AutoStartScanOnly(int instId) +#ifndef OHOS_ARCH_LITE +std::unique_ptr& WifiManager::GetWifiEventSubscriberManager() { - if (!WifiSettings::GetInstance().CheckScanOnlyAvailable(instId) || - !WifiManager::GetInstance().GetLocationModeByDatashare()) { - WIFI_LOGI("No need to StartScanOnly, return"); - return; - } - - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId); - WIFI_LOGI("Wifi scan only state is %{public}d", static_cast(curState)); - - if (curState != WifiOprMidState::CLOSED) { - WIFI_LOGI("scanOnly state is not closed, return"); - return; - } - - if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiMidState(instId)) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, instId); - return; - } - - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::OPENING, instId); - CheckAndStartScanService(instId); - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); - if (pService == nullptr) { - WIFI_LOGE("[AutoStartScanOnly] scan service is null."); - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); - return; - } - ErrCode ret = pService->StartWpa(); - if (ret != static_cast(WIFI_IDL_OPT_OK)) { - WIFI_LOGE("Start Wpa failed"); - } - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, instId); + return wifiEventSubscriberManager; } +#endif -void WifiManager::AutoStopScanOnly(int instId) +#ifdef FEATURE_HPF_SUPPORT +void WifiManager::InstallPacketFilterProgram(int screenState, int instId) { - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId); - WIFI_LOGI("current wifi scan only state is %{public}d", static_cast(curState)); - if (curState != WifiOprMidState::RUNNING) { + WIFI_LOGD("%{public}s enter screenState: %{public}d, instId: %{public}d", __FUNCTION__, screenState, instId); + IEnhanceService *pEnhanceService = WifiServiceManager::GetInstance().GetEnhanceServiceInst(); + if (pEnhanceService == nullptr) { + WIFI_LOGW("%{public}s pEnhanceService is nullptr", __FUNCTION__); return; } - - if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiMidState(instId)) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); - return; + // fill mac address arr + unsigned char macAddr[WIFI_MAC_LEN] = {0}; + std::string macStr; + WifiSettings::GetInstance().GetRealMacAddress(macStr, instId); + WIFI_LOGD("%{public}s convert mac from str to arr success, macStr: %{public}s", + __FUNCTION__, OHOS::Wifi::MacAnonymize(macStr).c_str()); + if (OHOS::Wifi::MacStrToArray(macStr, macAddr) != EOK) { + WIFI_LOGW("%{public}s get mac addr fail, set default mac addr", __FUNCTION__); + if (memset_s(macAddr, WIFI_MAC_LEN, 0x00, WIFI_MAC_LEN) != EOK) { + WIFI_LOGE("%{public}s set default mac addr fail", __FUNCTION__); + } } - - if (!WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(curState, WifiOprMidState::CLOSING, instId)) { - WIFI_LOGI("set wifi scan only mid state opening failed!"); - return; + // get number ip and net mask + IpInfo ipInfo; + WifiSettings::GetInstance().GetIpInfo(ipInfo, instId); + if (ipInfo.ipAddress == 0 || ipInfo.netmask == 0) { + WIFI_LOGW("%{public}s cannot get device ip address", __FUNCTION__); } - - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); - if (pService == nullptr) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); + std::string ipAddrStr = IpTools::ConvertIpv4Address(ipInfo.ipAddress); + std::string ipMaskStr = IpTools::ConvertIpv4Mask(ipInfo.netmask); + int netMaskLen = IpTools::GetMaskLength(ipMaskStr); + WIFI_LOGD("%{public}s get ip info ipaddrStr: %{public}s, ipMaskStr: %{public}s, netMaskLen: %{public}d", + __FUNCTION__, + OHOS::Wifi::MacAnonymize(ipAddrStr).c_str(), OHOS::Wifi::MacAnonymize(ipMaskStr).c_str(), netMaskLen); + if (pEnhanceService->InstallFilterProgram( + ipInfo.ipAddress, netMaskLen, macAddr, WIFI_MAC_LEN, screenState) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("%{public}s InstallFilterProgram fail", __FUNCTION__); return; } - ErrCode ret = pService->CloseWpa(); - if (ret != static_cast(WIFI_IDL_OPT_OK)) { - WIFI_LOGE("Stop Wpa failed"); - } - ret = pService->CloseScanOnly(); - WifiManager::GetInstance().CheckAndStopScanService(instId); - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); + WIFI_LOGE("%{public}s InstallFilterProgram success", __FUNCTION__); } #endif -void WifiManager::ForceStopWifi(int instId) +InitStatus WifiManager::GetInitStatus() { - WIFI_LOGI("Enter ForceStopWifi"); - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(instId); - if (pService == nullptr || (pService->DisableWifi() != WIFI_OPT_SUCCESS)) { - WIFI_LOGE("service is null or disable wifi failed."); - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSED, instId); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; - cbMsg.msgData = static_cast(WifiState::UNKNOWN); - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, instId); - return; + return mInitStatus; +} + +void WifiManager::DealCloseServiceMsg() +{ + const int waitDealTime = 10 * 1000; /* 10 ms */ + while (true) { + std::unique_lock lock(mMutex); + while (mEventQue.empty()) { + mCondition.wait(lock); + } + WifiCloseServiceMsg msg = mEventQue.front(); + mEventQue.pop_front(); + lock.unlock(); + usleep(waitDealTime); + switch (msg.code) { + case WifiCloseServiceCode::STA_SERVICE_CLOSE: + wifiStaManager->CloseStaService(msg.instId); + break; + case WifiCloseServiceCode::SCAN_SERVICE_CLOSE: + wifiScanManager->CloseScanService(msg.instId); + break; +#ifdef FEATURE_AP_SUPPORT + case WifiCloseServiceCode::AP_SERVICE_CLOSE: + wifiHotspotManager->CloseApService(msg.instId); + break; +#endif +#ifdef FEATURE_P2P_SUPPORT + case WifiCloseServiceCode::P2P_SERVICE_CLOSE: + wifiP2pManager->CloseP2pService(); + break; +#endif + case WifiCloseServiceCode::SERVICE_THREAD_EXIT: + WIFI_LOGI("DealCloseServiceMsg thread exit!"); + return; + default: + WIFI_LOGW("Unknown message code, %{public}d", static_cast(msg.code)); + break; + } } - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiMidState(instId); - WIFI_LOGI("In force stop wifi, state: %{public}d", static_cast(curState)); - WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::CLOSED, instId); + WIFI_LOGD("WifiManager Thread exit"); + return; } -void WifiManager::CheckAndStartSta(AutoStartOrStopServiceReason reason) +void WifiManager::CheckAndStartSta() { DIR *dir = nullptr; struct dirent *dent = nullptr; int currentWaitTime = 0; const int sleepTime = 1; const int maxWaitTimes = 30; - while (currentWaitTime < maxWaitTimes) { dir = opendir("/sys/class/net"); if (dir == nullptr) { -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().AutoStartStaService(reason, 0); -#else - WifiManager::GetInstance().WifiToggled(1, 0); -#endif + wifiTogglerManager->WifiToggled(1, 0); return; } while ((dent = readdir(dir)) != nullptr) { @@ -346,11 +356,7 @@ void WifiManager::CheckAndStartSta(AutoStartOrStopServiceReason reason) } if (strncmp(dent->d_name, "wlan", strlen("wlan")) == 0) { closedir(dir); -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().AutoStartStaService(reason, 0); -#else - WifiManager::GetInstance().WifiToggled(1, 0); -#endif + wifiTogglerManager->WifiToggled(1, 0); return; } } @@ -358,2473 +364,56 @@ void WifiManager::CheckAndStartSta(AutoStartOrStopServiceReason reason) sleep(sleepTime); currentWaitTime++; } -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().AutoStartStaService(reason, 0); -#else - WifiManager::GetInstance().WifiToggled(1, 0); -#endif + wifiTogglerManager->WifiToggled(1, 0); } -void WifiManager::AutoStartServiceThread(AutoStartOrStopServiceReason reason) +void WifiManager::AutoStartServiceThread() { WIFI_LOGI("Auto start service..."); - CheckAndStartSta(reason); + CheckAndStartSta(); } -#ifdef OHOS_ARCH_LITE -#ifdef FEATURE_AP_SUPPORT -ErrCode WifiManager::AutoStartApService(AutoStartOrStopServiceReason reason) +void WifiManager::InitPidfile() { - WifiOprMidState apState = WifiConfigCenter::GetInstance().GetApMidState(); - WIFI_LOGI("AutoStartApService, current ap state:%{public}d, reason:%{public}d", apState, reason); - if (apState != WifiOprMidState::CLOSED) { - if (apState == WifiOprMidState::CLOSING) { - return WIFI_OPT_OPEN_FAIL_WHEN_CLOSING; - } else { - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - } - if (!WifiConfigCenter::GetInstance().SetApMidState(apState, WifiOprMidState::OPENING, 0)) { - WIFI_LOGI("AutoStartApService, set ap mid state opening failed!"); - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - ErrCode errCode = WIFI_OPT_FAILED; - do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_AP) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_AP); - break; - } - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(); - if (pService == nullptr) { - WIFI_LOGE("Instance get hotspot service is null!"); - break; - } - errCode = pService->RegisterApServiceCallbacks(WifiManager::GetInstance().GetApCallback()); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("Register ap service callback failed!"); - break; - } - errCode = pService->EnableHotspot(); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service enable ap failed, ret %{public}d!", static_cast(errCode)); - break; - } - } while (false); - if (errCode != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, 0); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, 0); - return errCode; - } - StopUnloadApSaTimer(); - return WIFI_OPT_SUCCESS; -} - -ErrCode WifiManager::AutoStopApService(AutoStartOrStopServiceReason reason) -{ - WifiOprMidState apState = WifiConfigCenter::GetInstance().GetApMidState(); - WIFI_LOGI("AutoStopApService, current ap state:%{public}d, reason:%{public}d", apState, reason); - if (apState != WifiOprMidState::RUNNING) { - if (apState == WifiOprMidState::OPENING) { - return WIFI_OPT_CLOSE_FAIL_WHEN_OPENING; - } else { - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - } - - if (!WifiConfigCenter::GetInstance().SetApMidState(apState, WifiOprMidState::CLOSING, 0)) { - WIFI_LOGI("AutoStopApService, set ap mid state closing failed!"); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(); - if (pService == nullptr) { - WIFI_LOGE("AutoStopApService, Instance get hotspot service is null!"); - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSED, 0); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, 0); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - - ErrCode ret = pService->DisableHotspot(); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service disable ap failed, ret %{public}d!", static_cast(ret)); - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSING, WifiOprMidState::RUNNING, 0); - return ret; - } - - if (reason == AutoStartOrStopServiceReason::STA_AP_EXCLUSION) { - WifiConfigCenter::GetInstance().SetStaApExclusionType( - static_cast(StaApExclusionType::USER_OPEN_WIFI_AUTO_STOP_AP)); - } - return WIFI_OPT_SUCCESS; -} -#endif -#endif - -#ifndef OHOS_ARCH_LITE -ErrCode WifiManager::WifiToggled(int isOpen, int id) -{ - if (!WifiSettings::GetInstance().GetCoexSupport() && isOpen) { - WIFI_LOGI("set softap toggled false"); - WifiSettings::GetInstance().SetSoftapToggledState(false); - } - pWifiControllerMachine->SendMessage(CMD_WIFI_TOGGLED, isOpen, id); - return WIFI_OPT_SUCCESS; -} - -ErrCode WifiManager::SoftapToggled(int isOpen, int id) -{ - if (isOpen) { - WIFI_LOGI("set softap toggled true"); - WifiSettings::GetInstance().SetSoftapToggledState(true); - } else { - WIFI_LOGI("set softap toggled false"); - WifiSettings::GetInstance().SetSoftapToggledState(false); - } - pWifiControllerMachine->SendMessage(CMD_SOFTAP_TOGGLED, isOpen, id); - return WIFI_OPT_SUCCESS; -} - -bool WifiManager::HasAnyApRuning() -{ - WifiOprMidState apState0 = WifiConfigCenter::GetInstance().GetApMidState(0); - WifiOprMidState apState1 = WifiConfigCenter::GetInstance().GetApMidState(1); - if (apState0 == WifiOprMidState::RUNNING || apState0 == WifiOprMidState::OPENING || - apState1 == WifiOprMidState::RUNNING || apState1 == WifiOprMidState::OPENING) { - return true; - } - return false; -} - -ErrCode WifiManager::ScanOnlyToggled(int isOpen) -{ - int airplanState = WifiConfigCenter::GetInstance().GetAirplaneModeState(); - if (airplanState == MODE_STATE_OPEN) { - WIFI_LOGE("Airplane mode do not start scanonly."); - return WIFI_OPT_FAILED; - } - if (!WifiSettings::GetInstance().GetCoexSupport() && WifiManager::GetInstance().HasAnyApRuning()) { - WIFI_LOGE("Softap mode do not start scanonly."); - return WIFI_OPT_FAILED; - } - pWifiControllerMachine->SendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED, isOpen, 0); - return WIFI_OPT_SUCCESS; -} - -ErrCode WifiManager::AirplaneToggled(int isOpen) -{ - pWifiControllerMachine->SendMessage(CMD_AIRPLANE_TOGGLED, isOpen); - return WIFI_OPT_SUCCESS; -} -#endif - - -#ifdef FEATURE_P2P_SUPPORT -WifiCfgMonitorEventCallback WifiManager::cfgMonitorCallback = { - nullptr, -}; - -ErrCode WifiManager::AutoStartP2pService(AutoStartOrStopServiceReason reason) -{ - WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); - WIFI_LOGI("AutoStartP2pService, current p2p state:%{public}d, reason:%{public}d", p2pState, reason); - if (p2pState != WifiOprMidState::CLOSED) { - if (p2pState == WifiOprMidState::CLOSING) { - return WIFI_OPT_OPEN_FAIL_WHEN_CLOSING; - } else { - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - } - - if (!WifiConfigCenter::GetInstance().SetP2pMidState(p2pState, WifiOprMidState::OPENING)) { - WIFI_LOGE("AutoStartP2pService, set p2p mid state opening failed!"); - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - - ErrCode ret = WIFI_OPT_FAILED; - do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_P2P) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_P2P); - break; - } - IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst(); - if (pService == nullptr) { - WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_P2P); - break; - } - ret = pService->RegisterP2pServiceCallbacks(WifiManager::GetInstance().GetP2pCallback()); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("Register p2p service callback failed!"); - break; - } - ret = pService->EnableP2p(); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service EnableP2p failed, ret %{public}d!", static_cast(ret)); - break; - } - } while (false); - if (ret != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); - return ret; - } - - StopUnloadP2PSaTimer(); - return WIFI_OPT_SUCCESS; -} - -ErrCode WifiManager::AutoStopP2pService(AutoStartOrStopServiceReason reason) -{ - WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); - WIFI_LOGI("AutoStopP2pService, current p2p state:%{public}d, reason:%{public}d", p2pState, reason); - if (p2pState != WifiOprMidState::RUNNING) { - if (p2pState == WifiOprMidState::OPENING) { - return WIFI_OPT_CLOSE_FAIL_WHEN_OPENING; - } else { - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - } - - if (!WifiConfigCenter::GetInstance().SetP2pMidState(p2pState, WifiOprMidState::CLOSING)) { - WIFI_LOGE("AutoStopP2pService, set p2p mid state opening failed!"); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - - IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst(); - if (pService == nullptr) { - WIFI_LOGE("AutoStopP2pService, Instance get p2p service is null!"); - WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSED); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - - ErrCode ret = pService->DisableP2p(); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service disable p2p failed, ret %{public}d!", static_cast(ret)); - WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSING, WifiOprMidState::RUNNING); - return ret; - } - - return WIFI_OPT_SUCCESS; -} -#endif - -void WifiManager::AutoStartScanService(int instId) -{ - WIFI_LOGI("AutoStartScanService"); - if (!WifiConfigCenter::GetInstance().IsScanAlwaysActive()) { - WIFI_LOGW("Scan always is not open, not open scan service."); - return; - } - ScanControlInfo info; - WifiConfigCenter::GetInstance().GetScanControlInfo(info, instId); - if (!IsAllowScanAnyTime(info)) { - WIFI_LOGW("Scan control does not support scan always, not open scan service here."); - return; - } - WifiManager::GetInstance().CheckAndStartScanService(instId); - return; -} - -int WifiManager::Init() -{ -#ifndef OHOS_ARCH_LITE - if (WifiCountryCodeManager::GetInstance().Init() < 0) { - WIFI_LOGE("WifiCountryCodeManager Init failed!"); - mInitStatus = WIFI_COUNTRY_CODE_MANAGER_INIT_FAILED; - return -1; - } -#endif - if (WifiConfigCenter::GetInstance().Init() < 0) { - WIFI_LOGE("WifiConfigCenter Init failed!"); - mInitStatus = CONFIG_CENTER_INIT_FAILED; - return -1; - } - if (WifiAuthCenter::GetInstance().Init() < 0) { - WIFI_LOGE("WifiAuthCenter Init failed!"); - mInitStatus = AUTH_CENTER_INIT_FAILED; - return -1; - } - if (WifiServiceManager::GetInstance().Init() < 0) { - WIFI_LOGE("WifiServiceManager Init failed!"); - mInitStatus = SERVICE_MANAGER_INIT_FAILED; - return -1; - } - if (WifiInternalEventDispatcher::GetInstance().Init() < 0) { - WIFI_LOGE("WifiInternalEventDispatcher Init failed!"); - mInitStatus = EVENT_BROADCAST_INIT_FAILED; - return -1; - } -#ifndef OHOS_ARCH_LITE - pWifiControllerMachine = new (std::nothrow) WifiControllerMachine(); - if (pWifiControllerMachine == nullptr) { - WIFI_LOGE("Alloc pWifiControllerMachine failed.\n"); - return -1; - } - if (pWifiControllerMachine->InitWifiControllerMachine() != WIFI_OPT_SUCCESS) { - WIFI_LOGE("InitWifiControllerMachine failed.\n"); - return WIFI_OPT_FAILED; - } -#endif - mCloseServiceThread = std::thread(WifiManager::DealCloseServiceMsg, std::ref(*this)); - pthread_setname_np(mCloseServiceThread.native_handle(), "WifiCloseThread"); - -#ifndef OHOS_ARCH_LITE - RegisterDeviceProvisionEvent(); - if (!isScreenEventSubscribered && screenTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(&WifiManager::RegisterScreenEvent, this); - WifiTimer::GetInstance()->Register(timeoutCallback, screenTimerId, TIMEOUT_SCREEN_EVENT, false); - WIFI_LOGI("RegisterScreenEvent success! screenTimerId:%{public}u", screenTimerId); - } - if (!isAirplaneModeEventSubscribered && airplaneModeTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(&WifiManager::RegisterAirplaneModeEvent, this); - WifiTimer::GetInstance()->Register(timeoutCallback, airplaneModeTimerId, TIMEOUT_AIRPLANE_MODE_EVENT, false); - WIFI_LOGI("RegisterAirplaneModeEvent success! airplaneModeTimerId:%{public}u", airplaneModeTimerId); - } - if (!islocationModeObservered && locationTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(&WifiManager::RegisterLocationEvent, this); - WifiTimer::GetInstance()->Register(timeoutCallback, locationTimerId, TIMEOUT_LOCATION_EVENT, false); - WIFI_LOGI("RegisterLocationEvent success! locationTimerId:%{public}u", locationTimerId); - } - if (batterySubscriber_ == nullptr && batteryTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(&WifiManager::RegisterBatteryEvent, this); - WifiTimer::GetInstance()->Register(timeoutCallback, batteryTimerId, TIMEOUT_BATTERY_EVENT, false); - WIFI_LOGI("RegisterBatteryEvent success! locationTimerId:%{public}u", batteryTimerId); - } - if (!std::filesystem::exists(WIFI_CONFIG_FILE_PATH) && migrateTimerId == 0) { - RegisterSettingsMigrateEvent(); - TimeOutCallback timeoutCallback = std::bind(&WifiManager::CheckAndStartStaByDatashare, this); - WifiTimer::GetInstance()->Register(timeoutCallback, migrateTimerId, TIMEOUT_CHECK_LAST_STA_STATE_EVENT); - WIFI_LOGI("CheckAndStartStaByDatashare register success! migrateTimerId:%{public}u", migrateTimerId); - } -#ifdef HAS_POWERMGR_PART - if (!isPowerStateListenerSubscribered) { - RegisterPowerStateListener(); - } -#endif -#endif - mInitStatus = INIT_OK; - InitStaCallback(); - InitScanCallback(); -#ifndef OHOS_ARCH_LITE - GetMdmProp(); - GetChipProp(); - RegisterMdmPropListener(); - InitConcreteCallback(); -#endif -#ifdef FEATURE_AP_SUPPORT -#ifndef OHOS_ARCH_LITE - InitSoftapCallback(); -#endif - InitApCallback(); -#endif -#ifdef FEATURE_P2P_SUPPORT - InitP2pCallback(); -#endif - if (WifiServiceManager::GetInstance().CheckPreLoadService() < 0) { - WIFI_LOGE("WifiServiceManager check preload feature service failed!"); - WifiManager::GetInstance().Exit(); - return -1; - } - if (WifiConfigCenter::GetInstance().GetStaLastRunState() - || (WifiConfigCenter::GetInstance().GetStaApExclusionType() - == static_cast(StaApExclusionType::USER_OPEN_AP_AUTO_STOP_WIFI) - && WifiConfigCenter::GetInstance().GetApMidState() == WifiOprMidState::CLOSED)) { /* Automatic startup upon startup */ - if (WifiConfigCenter::GetInstance().GetStaApExclusionType() - == static_cast(StaApExclusionType::USER_OPEN_AP_AUTO_STOP_WIFI)) { - WifiConfigCenter::GetInstance().SetStaApExclusionType(static_cast(StaApExclusionType::INITIAL_TYPE)); - } - WIFI_LOGI("AutoStartServiceThread"); -#ifndef OHOS_ARCH_LITE - WifiSettings::GetInstance().SetWifiToggledState(true); -#endif - std::thread startStaSrvThread(WifiManager::AutoStartServiceThread, - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP); - pthread_setname_np(startStaSrvThread.native_handle(), "AutoStartThread"); - startStaSrvThread.detach(); - } else { - /** - * The sta service automatically starts upon startup. After the sta - * service is started, the scanning is directly started. - */ - AutoStartEnhanceService(); - CheckAndStartScanService(); - } - InitPidfile(); - return 0; -} - -void WifiManager::InitSubscribeListener() -{ -#ifndef OHOS_ARCH_LITE - SubscribeSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID); - SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID); -#ifdef HAS_POWERMGR_PART - SubscribeSystemAbility(POWER_MANAGER_SERVICE_ID); -#endif -#ifdef HAS_MOVEMENT_PART - SubscribeSystemAbility(MSDP_MOVEMENT_SERVICE_ID); -#endif -#endif -} - -#ifndef OHOS_ARCH_LITE -void WifiManager::GetMdmProp() -{ - char preValue[PROP_FALSE_LEN + 1] = {0}; - - int errorCode = GetParamValue(MDM_WIFI_PROP.c_str(), 0, preValue, PROP_FALSE_LEN + 1); - if (errorCode > 0) { - if (strncmp(preValue, PROP_TRUE.c_str(), PROP_TRUE_LEN) == 0) { - mIsMdmForbidden = true; - } - } -} - -void WifiManager::GetChipProp() -{ - char preValue[PROP_SUBCHIPTYPE_LEN] = {0}; - int errorCode = GetParamValue(SUBCHIP_WIFI_PROP.c_str(), 0, preValue, PROP_SUBCHIPTYPE_LEN); - if (errorCode > 0) { - if (strncmp(preValue, SUPPORT_COEXCHIP.c_str(), SUPPORT_COEXCHIP_LEN) == 0) { - WifiSettings::GetInstance().SetApIfaceName(COEX_IFACENAME); - WifiSettings::GetInstance().SetCoexSupport(true); - } - } -} - -void WifiManager::RegisterMdmPropListener() -{ - int ret = WatchParamValue(MDM_WIFI_PROP.c_str(), MdmPropChangeEvt, nullptr); - if (ret != 0) { - WIFI_LOGI("RegisterMdmPropListener failed"); - } -} - -void WifiManager::MdmPropChangeEvt(const char *key, const char *value, void *context) -{ - if (strncmp(key, MDM_WIFI_PROP.c_str(), PROP_LEN) != 0) { - WIFI_LOGI("not mdm prop change"); - return; - } - WIFI_LOGI("mdm prop change"); - if (strncmp(value, PROP_TRUE.c_str(), PROP_TRUE_LEN) == 0) { - mIsMdmForbidden = true; - return; - } - if (strncmp(value, PROP_FALSE.c_str(), PROP_FALSE_LEN) == 0) { - mIsMdmForbidden = false; - } -} - -bool WifiManager::IsMdmForbidden() -{ - return mIsMdmForbidden; -} -#endif - -void WifiManager::Exit() -{ - WIFI_LOGI("[WifiManager] Exit."); - WifiServiceManager::GetInstance().UninstallAllService(); - /* NOTE:: DO NOT allow call hal interface function, delete at 2022.10.16 */ - /* Refer to WifiStaHalInterface::GetInstance().ExitAllIdlClient(); */ - WifiInternalEventDispatcher::GetInstance().Exit(); - if (mCloseServiceThread.joinable()) { - PushServiceCloseMsg(WifiCloseServiceCode::SERVICE_THREAD_EXIT); - mCloseServiceThread.join(); - } -#ifndef OHOS_ARCH_LITE - if (isScreenEventSubscribered) { - UnRegisterScreenEvent(); - } - - if (isAirplaneModeEventSubscribered) { - UnRegisterAirplaneModeEvent(); - } - - if (islocationModeObservered) { - UnRegisterLocationEvent(); - } - if (deviceProvisionObserver_ != nullptr) { - UnRegisterDeviceProvisionEvent(); - } - if (batterySubscriber_ != nullptr) { - UnRegisterBatteryEvent(); - } -#ifdef HAS_POWERMGR_PART - if (isPowerStateListenerSubscribered) { - UnRegisterPowerStateListener(); - } -#endif -#endif - return; -} - -void WifiManager::PushServiceCloseMsg(WifiCloseServiceCode code, int instId) -{ - std::unique_lock lock(mMutex); - WifiCloseServiceMsg msg; - msg.code = code; - msg.instId = instId; - mEventQue.push_back(msg); - mCondition.notify_one(); - return; -} - -void WifiManager::AddSupportedFeatures(WifiFeatures feature) -{ - mSupportedFeatures |= static_cast(feature); -} - -int WifiManager::GetSupportedFeatures(long &features) const -{ - long supportedFeatures = mSupportedFeatures; - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_INFRA); - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_INFRA_5G); - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_PASSPOINT); - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_AP_STA); - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_WPA3_SAE); - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_WPA3_SUITE_B); - supportedFeatures |= static_cast(WifiFeatures::WIFI_FEATURE_OWE); - /* NOTE: features = supportedFeatures & WifiChipHalInterface::GetInstance().GetChipCapabilities */ - /* It does NOT allow call HalInterface from wifi_manager */ - features = supportedFeatures; - return 0; -} - -InitStatus WifiManager::GetInitStatus() -{ - return mInitStatus; -} - -#ifndef OHOS_ARCH_LITE -WifiControllerMachine* WifiManager::GetControllerMachine() -{ - return pWifiControllerMachine; -} -#endif - -#ifndef OHOS_ARCH_LITE -uint32_t WifiManager::unloadStaSaTimerId{0}; -std::mutex WifiManager::unloadStaSaTimerMutex{}; -void WifiManager::UnloadStaSaTimerCallback() -{ - WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_DEVICE_ABILITY_ID); - if (static_cast(ApState::AP_STATE_CLOSED) == WifiConfigCenter::GetInstance().GetHotspotState(0)) { - WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_HOTSPOT_ABILITY_ID); - } - WifiManager::GetInstance().StopUnloadStaSaTimer(); -} - -void WifiManager::StopUnloadStaSaTimer(void) -{ - WIFI_LOGI("StopUnloadStaSaTimer! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); - std::unique_lock lock(unloadStaSaTimerMutex); - WifiTimer::GetInstance()->UnRegister(unloadStaSaTimerId); - unloadStaSaTimerId = 0; - return; -} - -void WifiManager::StartUnloadStaSaTimer(void) -{ - WIFI_LOGI("StartUnloadStaSaTimer! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); - std::unique_lock lock(unloadStaSaTimerMutex); - if (unloadStaSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadStaSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadStaSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("StartUnloadStaSaTimer success! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); - } - return; -} -#endif - -void WifiManager::CloseStaService(int instId) -{ - WIFI_LOGI("close sta service"); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, instId); - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSED, instId); -#ifndef OHOS_ARCH_LITE - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->HandleStaClose(instId); -#endif - WifiConfigCenter::GetInstance().SetWifiStaCloseTime(instId); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; - cbMsg.msgData = static_cast(WifiState::DISABLED); - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); -#ifdef OHOS_ARCH_LITE - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_CLOSE) { - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(instId) == - static_cast(OperatorWifiType::CLOSE_WIFI_DUE_TO_AIRPLANEMODE_OPENED)) { - DealAirplaneExceptionWhenStaClose(); - return; - } - } -#endif -#ifdef FEATURE_P2P_SUPPORT - WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); - WIFI_LOGI("CloseStaService, current p2p state: %{public}d", p2pState); - if (p2pState == WifiOprMidState::RUNNING) { - WifiManager::GetInstance().AutoStopP2pService( - AutoStartOrStopServiceReason::TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE); - } -#endif -#ifndef OHOS_ARCH_LITE - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - WIFI_LOGI("airplaneMode not close sta SA!"); - return; - } - if (WifiConfigCenter::GetInstance().GetPowerSleepState() == MODE_STATE_OPEN) { - WifiManager::GetInstance().StopUnloadStaSaTimer(); - return; - } -#ifdef OHOS_ARCH_LITE - if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId)) { - WIFI_LOGI("scanonly not close sta SA!"); - return; - } -#endif - WifiManager::GetInstance().StartUnloadStaSaTimer(); -#endif - return; -} - -#ifdef FEATURE_AP_SUPPORT - -#ifndef OHOS_ARCH_LITE -uint32_t WifiManager::unloadHotspotSaTimerId{0}; -std::mutex WifiManager::unloadHotspotSaTimerMutex{}; -void WifiManager::UnloadHotspotSaTimerCallback() -{ - WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_HOTSPOT_ABILITY_ID); - WifiManager::GetInstance().StopUnloadApSaTimer(); -} - -void WifiManager::StopUnloadApSaTimer(void) -{ - WIFI_LOGI("StopUnloadApSaTimer! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); - std::unique_lock lock(unloadHotspotSaTimerMutex); - WifiTimer::GetInstance()->UnRegister(unloadHotspotSaTimerId); - unloadHotspotSaTimerId = 0; - return; -} - -void WifiManager::StartUnloadApSaTimer(void) -{ - WIFI_LOGI("StartUnloadApSaTimer! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); - std::unique_lock lock(unloadHotspotSaTimerMutex); - if (unloadHotspotSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadHotspotSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadHotspotSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("RegisterUnloadHotspotSaTimer success! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); - } - return; -} -#endif - -void WifiManager::CloseApService(int id) -{ - WIFI_LOGI("close %{public}d ap service", id); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, id); - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSED, id); - WifiSettings::GetInstance().SetHotspotState(static_cast(ApState::AP_STATE_CLOSED), id); -#ifndef OHOS_ARCH_LITE - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->SendMessage(CMD_AP_STOPPED, id); -#endif - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE; - cbMsg.msgData = static_cast(ApState::AP_STATE_CLOSED); - cbMsg.id = id; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - #ifndef OHOS_ARCH_LITE - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - WIFI_LOGI("airplaneMode not close ap SA!"); - return; - } - WifiManager::GetInstance().StartUnloadApSaTimer(); - #endif - return; -} -#endif - -void WifiManager::CloseScanService(int instId) -{ - WIFI_LOGI("close scan service"); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_SCAN, instId); - WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::CLOSED, instId); - WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(instId); - WIFI_LOGI("CloseScanService, current sta state:%{public}d", staState); - if (staState == WifiOprMidState::OPENING || staState == WifiOprMidState::RUNNING) { - WifiManager::GetInstance().CheckAndStartScanService(instId); - return; - } - #ifndef OHOS_ARCH_LITE - WifiManager::GetInstance().StartUnloadScanSaTimer(); - #endif - return; -} - -#ifdef FEATURE_P2P_SUPPORT - -#ifndef OHOS_ARCH_LITE -uint32_t WifiManager::unloadP2PSaTimerId{0}; -std::mutex WifiManager::unloadP2PSaTimerMutex{}; -void WifiManager::UnloadP2PSaTimerCallback() -{ - WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_P2P_ABILITY_ID); - WifiManager::GetInstance().StopUnloadP2PSaTimer(); -} - -void WifiManager::StopUnloadP2PSaTimer(void) -{ - WIFI_LOGI("StopUnloadP2PSaTimer! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); - std::unique_lock lock(unloadP2PSaTimerMutex); - WifiTimer::GetInstance()->UnRegister(unloadP2PSaTimerId); - unloadP2PSaTimerId = 0; - return; -} - -void WifiManager::StartUnloadP2PSaTimer(void) -{ - WIFI_LOGI("StartUnloadP2PSaTimer! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); - std::unique_lock lock(unloadP2PSaTimerMutex); - if (unloadP2PSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadP2PSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadP2PSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("StartUnloadP2PSaTimer success! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); - } - return; -} -#endif - -void WifiManager::CloseP2pService(void) -{ - WIFI_LOGD("close p2p service"); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); - WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSED); - WifiSettings::GetInstance().SetP2pState(static_cast(P2pState::P2P_STATE_CLOSED)); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_P2P_STATE_CHANGE; - cbMsg.msgData = static_cast(P2pState::P2P_STATE_CLOSED); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - #ifndef OHOS_ARCH_LITE - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - WIFI_LOGI("airplaneMode not close p2p SA!"); - return; - } - WifiManager::GetInstance().StartUnloadP2PSaTimer(); - #endif - WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(); - WIFI_LOGI("CloseP2pService, current sta state:%{public}d", staState); - if (staState == WifiOprMidState::OPENING || staState == WifiOprMidState::RUNNING) { - WifiManager::GetInstance().AutoStartP2pService( - AutoStartOrStopServiceReason::TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE); - } - return; -} -#endif - -#ifdef FEATURE_HPF_SUPPORT -void WifiManager::InstallPacketFilterProgram(int screenState, int instId) -{ - WIFI_LOGD("%{public}s enter screenState: %{public}d, instId: %{public}d", __FUNCTION__, screenState, instId); - IEnhanceService *pEnhanceService = WifiServiceManager::GetInstance().GetEnhanceServiceInst(); - if (pEnhanceService == nullptr) { - WIFI_LOGW("%{public}s pEnhanceService is nullptr", __FUNCTION__); - return; - } - // fill mac address arr - unsigned char macAddr[WIFI_MAC_LEN] = {0}; - std::string macStr; - WifiSettings::GetInstance().GetRealMacAddress(macStr, instId); - WIFI_LOGD("%{public}s convert mac from str to arr success, macStr: %{public}s", - __FUNCTION__, OHOS::Wifi::MacAnonymize(macStr).c_str()); - if (OHOS::Wifi::MacStrToArray(macStr, macAddr) != EOK) { - WIFI_LOGW("%{public}s get mac addr fail, set default mac addr", __FUNCTION__); - if (memset_s(macAddr, WIFI_MAC_LEN, 0x00, WIFI_MAC_LEN) != EOK) { - WIFI_LOGE("%{public}s set default mac addr fail", __FUNCTION__); - } - } - // get number ip and net mask - IpInfo ipInfo; - WifiSettings::GetInstance().GetIpInfo(ipInfo, instId); - if (ipInfo.ipAddress == 0 || ipInfo.netmask == 0) { - WIFI_LOGW("%{public}s cannot get device ip address", __FUNCTION__); - } - std::string ipAddrStr = IpTools::ConvertIpv4Address(ipInfo.ipAddress); - std::string ipMaskStr = IpTools::ConvertIpv4Mask(ipInfo.netmask); - int netMaskLen = IpTools::GetMaskLength(ipMaskStr); - WIFI_LOGD("%{public}s get ip info ipaddrStr: %{public}s, ipMaskStr: %{public}s, netMaskLen: %{public}d", - __FUNCTION__, - OHOS::Wifi::MacAnonymize(ipAddrStr).c_str(), OHOS::Wifi::MacAnonymize(ipMaskStr).c_str(), netMaskLen); - if (pEnhanceService->InstallFilterProgram( - ipInfo.ipAddress, netMaskLen, macAddr, WIFI_MAC_LEN, screenState) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("%{public}s InstallFilterProgram fail", __FUNCTION__); - return; - } - WIFI_LOGE("%{public}s InstallFilterProgram success", __FUNCTION__); -} -#endif - -void WifiManager::DealCloseServiceMsg(WifiManager &manager) -{ - const int waitDealTime = 10 * 1000; /* 10 ms */ - while (true) { - std::unique_lock lock(manager.mMutex); - while (manager.mEventQue.empty()) { - manager.mCondition.wait(lock); - } - WifiCloseServiceMsg msg = manager.mEventQue.front(); - manager.mEventQue.pop_front(); - lock.unlock(); - usleep(waitDealTime); - switch (msg.code) { - case WifiCloseServiceCode::STA_SERVICE_CLOSE: - CloseStaService(msg.instId); - break; - case WifiCloseServiceCode::SCAN_SERVICE_CLOSE: - CloseScanService(msg.instId); - break; -#ifdef FEATURE_AP_SUPPORT - case WifiCloseServiceCode::AP_SERVICE_CLOSE: - CloseApService(mCloseApIndex); - break; -#endif -#ifdef FEATURE_P2P_SUPPORT - case WifiCloseServiceCode::P2P_SERVICE_CLOSE: - CloseP2pService(); - break; -#endif - case WifiCloseServiceCode::SERVICE_THREAD_EXIT: - WIFI_LOGI("DealCloseServiceMsg thread exit!"); - return; - default: - WIFI_LOGW("Unknown message code, %{public}d", static_cast(msg.code)); - break; - } - } - WIFI_LOGD("WifiManager Thread exit"); - return; -} - -void WifiManager::InitStaCallback(void) -{ - mStaCallback.callbackModuleName = "WifiManager"; - mStaCallback.OnStaOpenRes = DealStaOpenRes; - mStaCallback.OnStaCloseRes = DealStaCloseRes; - mStaCallback.OnStaConnChanged = DealStaConnChanged; - mStaCallback.OnWpsChanged = DealWpsChanged; - mStaCallback.OnStaStreamChanged = DealStreamChanged; - mStaCallback.OnStaRssiLevelChanged = DealRssiChanged; - return; -} - -StaServiceCallback WifiManager::GetStaCallback() -{ - return mStaCallback; -} - -void WifiManager::DealStaOpenRes(OperateResState state, int instId) -{ - WIFI_LOGD("Enter DealStaOpenRes: %{public}d", static_cast(state)); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; - cbMsg.id = instId; - if (state == OperateResState::OPEN_WIFI_OPENING) { - cbMsg.msgData = static_cast(WifiState::ENABLING); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_OPEN), - static_cast(WifiOperateState::STA_OPENING)); - return; - } - if ((state == OperateResState::OPEN_WIFI_FAILED) || (state == OperateResState::OPEN_WIFI_DISABLED)) { - WIFI_LOGE("DealStaOpenRes:wifi open failed!"); - WifiOprMidState apstate = WifiConfigCenter::GetInstance().GetApMidState(instId); - WriteWifiOpenAndCloseFailedHiSysEvent(static_cast(OperateResState::OPEN_WIFI_FAILED), "TIME_OUT", - static_cast(apstate)); - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, instId); - DealStaCloseRes(state, instId); - return; - } - - WIFI_LOGI("DealStaOpenRes:wifi open successfully!"); -#ifdef OHOS_ARCH_LITE - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - WifiConfigCenter::GetInstance().SetWifiStateWhenAirplaneMode(true); - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(instId) == - static_cast(OperatorWifiType::OPEN_WIFI_DUE_TO_AIRPLANEMODE_CLOSED)) { - DealAirplaneExceptionWhenStaOpen(instId); - return; - } - } -#endif - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, instId); - WifiConfigCenter::GetInstance().SetStaLastRunState(true, instId); -#ifndef OHOS_ARCH_LITE - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->HandleStaStart(instId); -#endif - cbMsg.msgData = static_cast(WifiState::ENABLED); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_OPEN), - static_cast(WifiOperateState::STA_OPENED)); -#ifdef FEATURE_P2P_SUPPORT - WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); - WIFI_LOGI("DealStaOpenRes, current p2p state:%{public}d", p2pState); - if (p2pState == WifiOprMidState::CLOSED) { - WifiManager::GetInstance().AutoStartP2pService( - AutoStartOrStopServiceReason::TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE); - } -#endif - AutoStartEnhanceService(); - CheckAndStartScanService(instId); -#ifdef OHOS_ARCH_LITE - if (WifiSettings::GetInstance().CheckScanOnlyAvailable(instId) && - WifiManager::GetInstance().GetLocationModeByDatashare()) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, instId); - } -#endif -} - -void WifiManager::DealStaCloseRes(OperateResState state, int instId) -{ - WIFI_LOGD("Enter DealStaCloseRes: %{public}d", static_cast(state)); -#ifndef OHOS_ARCH_LITE - WifiProtectManager::GetInstance().UpdateWifiClientConnected(false); -#endif - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; - cbMsg.id = instId; - if (state == OperateResState::CLOSE_WIFI_CLOSING) { - cbMsg.msgData = static_cast(WifiState::DISABLING); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_CLOSING)); - return; - } - if (state == OperateResState::CLOSE_WIFI_FAILED) { - WIFI_LOGI("DealStaCloseRes: broadcast wifi close failed event!"); - WifiOprMidState apstate = WifiConfigCenter::GetInstance().GetApMidState(instId); - WriteWifiOpenAndCloseFailedHiSysEvent(static_cast(OperateResState::CLOSE_WIFI_FAILED), "TIME_OUT", - static_cast(apstate)); - WifiManager::GetInstance().ForceStopWifi(instId); - cbMsg.msgData = static_cast(WifiState::UNKNOWN); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - } -#ifdef OHOS_ARCH_LITE - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - WifiConfigCenter::GetInstance().SetWifiStateWhenAirplaneMode(false); - } -#ifdef FEATURE_STA_AP_EXCLUSION - if (WifiConfigCenter::GetInstance().GetStaApExclusionType() - == static_cast(StaApExclusionType::USER_OPEN_AP_AUTO_STOP_WIFI)) { - WifiManager::GetInstance().AutoStartApService(AutoStartOrStopServiceReason::STA_AP_EXCLUSION); - } -#endif -#endif - if (WifiOprMidState::RUNNING != WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId)) { - WIFI_LOGI("DealStaCloseRes: wifi scan only state is not running,to CheckAndStopScanService!"); - WifiManager::GetInstance().CheckAndStopScanService(instId); - } - - WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::STA_SERVICE_CLOSE, instId); - return; -} - -void WifiManager::DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId) -{ - WIFI_LOGI("Enter, DealStaConnChanged, state: %{public}d!\n", static_cast(state)); - bool isReport = true; - int reportStateNum = static_cast(ConvertConnStateInternal(state, isReport)); - if (isReport) { - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_CONNECTION_CHANGE; - cbMsg.msgData = reportStateNum; - cbMsg.linkInfo = info; - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - } - - if (state == OperateResState::CONNECT_CONNECTING || state == OperateResState::CONNECT_AP_CONNECTED || - state == OperateResState::DISCONNECT_DISCONNECTING || state == OperateResState::DISCONNECT_DISCONNECTED || - state == OperateResState::CONNECT_OBTAINING_IP || state == OperateResState::CONNECT_ASSOCIATING || - state == OperateResState::CONNECT_ASSOCIATED || state == OperateResState::CONNECT_NETWORK_ENABLED || - state == OperateResState::CONNECT_NETWORK_DISABLED) { - if (WifiConfigCenter::GetInstance().GetScanMidState(instId) == WifiOprMidState::RUNNING) { - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); - if (pService != nullptr) { - pService->OnClientModeStatusChanged(static_cast(state)); - } - } - } - switch (state) { - case OperateResState::CONNECT_CONNECTING: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_CONNECT), - static_cast(WifiOperateState::STA_CONNECTING)); - break; - case OperateResState::CONNECT_AP_CONNECTED: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_CONNECT), - static_cast(WifiOperateState::STA_CONNECTED)); - break; - case OperateResState::DISCONNECT_DISCONNECTED: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_CONNECT), - static_cast(WifiOperateState::STA_DISCONNECTED)); - break; - case OperateResState::CONNECT_ASSOCIATING: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_ASSOC), - static_cast(WifiOperateState::STA_ASSOCIATING)); - WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_ASSOCIATING)); - break; - case OperateResState::CONNECT_ASSOCIATED: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_ASSOC), - static_cast(WifiOperateState::STA_ASSOCIATED)); - WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_ASSOCIATED)); - break; - case OperateResState::CONNECT_CONNECTION_FULL: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_ASSOC), - static_cast(WifiOperateState::STA_ASSOC_FULL_REJECT)); - break; - case OperateResState::CONNECT_OBTAINING_IP: - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_DHCP), - static_cast(WifiOperateState::STA_DHCP)); - break; - case OperateResState::DISCONNECT_DISCONNECTING: - case OperateResState::CONNECT_CONNECTING_TIMEOUT: - WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_DISCONNECT)); - break; - default: - break; - } - if (info.connState == ConnState::AUTHENTICATING) - { - WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_AUTH), - static_cast(WifiOperateState::STA_AUTHING)); - } - -#ifdef FEATURE_P2P_SUPPORT - if (cfgMonitorCallback.onStaConnectionChange != nullptr) { - cfgMonitorCallback.onStaConnectionChange(static_cast(state)); - } -#endif -#ifdef FEATURE_HPF_SUPPORT - if (state == OperateResState::CONNECT_AP_CONNECTED) { - int screenState = WifiSettings::GetInstance().GetScreenState(); - WifiManager::GetInstance().InstallPacketFilterProgram(screenState, instId); - } -#endif -#ifndef OHOS_ARCH_LITE - bool isConnected = (info.connState == CONNECTED) ? true : false; - WifiProtectManager::GetInstance().UpdateWifiClientConnected(isConnected); -#endif - return; -} - -void WifiManager::DealWpsChanged(WpsStartState state, const int pinCode, int instId) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_WPS_STATE_CHANGE; - cbMsg.msgData = static_cast(state); - cbMsg.id = instId; - cbMsg.pinCode = std::to_string(pinCode); - int len = cbMsg.pinCode.length(); - if (len < 8) { /* Fill in 8 digits. */ - cbMsg.pinCode = std::string(8 - len, '0') + cbMsg.pinCode; - } - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -void WifiManager::DealStreamChanged(StreamDirection direction, int instId) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_STREAM_DIRECTION; - cbMsg.msgData = static_cast(direction); - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -void WifiManager::DealRssiChanged(int rssi, int instId) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_RSSI_CHANGE; - cbMsg.msgData = rssi; - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -#ifndef OHOS_ARCH_LITE -uint32_t WifiManager::unloadScanSaTimerId{0}; -std::mutex WifiManager::unloadScanSaTimerMutex{}; -void WifiManager::UnloadScanSaTimerCallback() -{ - WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_SCAN_ABILITY_ID); - WifiManager::GetInstance().StopUnloadScanSaTimer(); -} - -void WifiManager::StopUnloadScanSaTimer(void) -{ - WIFI_LOGI("StopUnloadScanSaTimer! unloadScanSaTimerId:%{public}u", unloadScanSaTimerId); - std::unique_lock lock(unloadScanSaTimerMutex); - WifiTimer::GetInstance()->UnRegister(unloadScanSaTimerId); - unloadScanSaTimerId = 0; - return; -} - -void WifiManager::StartUnloadScanSaTimer(void) -{ - WIFI_LOGI("StartUnloadScanSaTimer! unloadScanSaTimerId:%{public}u", unloadScanSaTimerId); - std::unique_lock lock(unloadScanSaTimerMutex); - if (unloadScanSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadScanSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadScanSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("StartUnloadScanSaTimer success! unloadScanSaTimerId:%{public}u", unloadScanSaTimerId); - } - return; -} -#endif - -void WifiManager::OnSystemAbilityChanged(int systemAbilityId, bool add) -{ -#ifndef OHOS_ARCH_LITE - switch (systemAbilityId) { - case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID: { - if (!add) { - break; - } - for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i); - if (pService != nullptr) { - pService->OnSystemAbilityChanged(systemAbilityId, add); - } - } - break; - } - case COMMON_EVENT_SERVICE_ID: { - if (add) { - RegisterScreenEvent(); - RegisterAirplaneModeEvent(); - RegisterLocationEvent(); - } else { - UnRegisterScreenEvent(); - UnRegisterAirplaneModeEvent(); - UnRegisterLocationEvent(); - } - - WIFI_LOGI("OnSystemAbilityChanged, id[%{public}d], mode=[%{public}d]!", - systemAbilityId, add); - for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(i); - if (pService != nullptr) { - pService->OnSystemAbilityChanged(systemAbilityId, add); - } - } - break; - } -#ifdef HAS_POWERMGR_PART - case POWER_MANAGER_SERVICE_ID: { - if (add) { - RegisterPowerStateListener(); - } else { - UnRegisterPowerStateListener(); - } - - WIFI_LOGI("OnSystemAbilityChanged, id[%{public}d], mode=[%{public}d]!", - systemAbilityId, add); - - break; - } -#endif -#ifdef HAS_MOVEMENT_PART - case MSDP_MOVEMENT_SERVICE_ID: { - if (add) { - RegisterMovementCallBack(); - } else { - UnRegisterMovementCallBack(); - } - break; - } -#endif - default: - break; - } -#endif -} - -void WifiManager::CheckAndStartScanService(int instId) -{ - WifiOprMidState scanState = WifiConfigCenter::GetInstance().GetScanMidState(instId); - WIFI_LOGI("CheckAndStartScanService scanState: %{public}d", static_cast(scanState)); - if (scanState != WifiOprMidState::CLOSED) { - /* If the scanning function is enabled when the STA is not enabled, you need to start the scheduled - scanning function immediately when the STA is enabled. */ - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); - if (pService != nullptr) { - pService->OnClientModeStatusChanged(static_cast(OperateResState::DISCONNECT_DISCONNECTED)); - } - return; - } - if (!WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::OPENING, instId)) { - WIFI_LOGW("Failed to set scan mid state opening! may be other activity has been operated"); - return; - } - ErrCode errCode = WIFI_OPT_FAILED; - do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_SCAN) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_SCAN); - break; - } - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); - if (pService == nullptr) { - WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_SCAN); - break; - } - errCode = pService->RegisterScanCallbacks(WifiManager::GetInstance().GetScanCallback()); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("Register scan service callback failed!"); - break; - } - errCode = pService->Init(); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("init scan service failed, ret %{public}d!", static_cast(errCode)); - break; - } - IEnhanceService *pEnhanceService = WifiServiceManager::GetInstance().GetEnhanceServiceInst(); - if (pEnhanceService == nullptr) { - WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_ENHANCE); - break; - } - errCode = pService->SetEnhanceService(pEnhanceService); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("SetEnhanceService failed, ret %{public}d!", static_cast(errCode)); - break; - } - } while (0); - if (errCode != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, instId); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_SCAN, instId); - } - return; -} - -void WifiManager::AutoStartEnhanceService(void) -{ - WIFI_LOGI("AutoStartEnhanceService start"); - ErrCode errCode = WIFI_OPT_FAILED; - do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_ENHANCE) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_ENHANCE); - break; - } - IEnhanceService *pEnhanceService = WifiServiceManager::GetInstance().GetEnhanceServiceInst(); - if (pEnhanceService == nullptr) { - WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_ENHANCE); - break; - } - errCode = pEnhanceService->Init(); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("init Enhance service failed, ret %{public}d!", static_cast(errCode)); - break; - } - } while (0); - return; -} - -void WifiManager::CheckAndStopScanService(int instId) -{ - /** - * Check unload SCAN service - * When anytime scanning is enabled and the control policy allows, airplane - * mode and power saving mode are disabled. --- Do not disable the scan - * service. Otherwise, disable the SCAN service. - */ - WifiOprMidState scanState = WifiConfigCenter::GetInstance().GetScanMidState(instId); - WIFI_LOGI("[CheckAndStopScanService] scanState %{public}d!", static_cast(scanState)); - if (scanState != WifiOprMidState::OPENING && scanState != WifiOprMidState::RUNNING) { - return; - } - ScanControlInfo info; - WifiConfigCenter::GetInstance().GetScanControlInfo(info, instId); - if (WifiConfigCenter::GetInstance().IsScanAlwaysActive() && IsAllowScanAnyTime(info) && - WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_CLOSE && - WifiConfigCenter::GetInstance().GetPowerSavingModeState() == MODE_STATE_CLOSE) { - return; - } - /* After check condition over, begin unload SCAN service */ - if (WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::CLOSING, instId)) { - IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); - if (pService == nullptr) { - WIFI_LOGE("[CheckAndStopScanService] scan service is null."); - WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::SCAN_SERVICE_CLOSE, instId); - WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::CLOSED, instId); - return; - } - ErrCode ret = pService->UnInit(); - if (ret != WIFI_OPT_SUCCESS) { // scan service is not exist - WIFI_LOGE("[CheckAndStopScanService] UnInit service failed!"); - } - WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::CLOSED, instId); - } -} - -void WifiManager::InitScanCallback(void) -{ - mScanCallback.OnScanStartEvent = DealScanOpenRes; - mScanCallback.OnScanStopEvent = DealScanCloseRes; - mScanCallback.OnScanFinishEvent = DealScanFinished; - mScanCallback.OnScanInfoEvent = DealScanInfoNotify; - mScanCallback.OnStoreScanInfoEvent = DealStoreScanInfoEvent; -} - -IScanSerivceCallbacks WifiManager::GetScanCallback() -{ - return mScanCallback; -} - -#ifndef OHOS_ARCH_LITE -ConcreteModeCallback WifiManager::GetConcreteCallback() -{ - return mConcreteModeCb; -} - -SoftApModeCallback WifiManager::GetSoftApCallback() -{ - return mSoftApModeCb; -} - -void WifiManager::InitConcreteCallback() -{ - mConcreteModeCb.onStartFailure = DealConcreateStartFailure; - mConcreteModeCb.onStopped = DealConcreateStop; -} - -void WifiManager::InitSoftapCallback() -{ - mSoftApModeCb.onStartFailure = DealSoftapStartFailure; - mSoftApModeCb.onStopped = DealSoftapStop; -} - -void WifiManager::DealConcreateStop(int id) -{ - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->SendMessage(CMD_CONCRETE_STOPPED, id); -} - -void WifiManager::DealConcreateStartFailure(int id) -{ - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->SendMessage(CMD_STA_START_FAILURE, id); -} - -void WifiManager::DealSoftapStop(int id) -{ - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->SendMessage(CMD_AP_STOPPED, id); -} - -void WifiManager::DealSoftapStartFailure(int id) -{ - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->SendMessage(CMD_AP_START_FAILURE, id); -} -#endif - -void WifiManager::DealScanOpenRes(int instId) -{ - WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, instId); -} - -void WifiManager::DealScanCloseRes(int instId) -{ - WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::SCAN_SERVICE_CLOSE, instId); -} - -void WifiManager::DealScanFinished(int state, int instId) -{ - WIFI_LOGE("%{public}s, state: %{public}d!", __func__, state); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_SCAN_STATE_CHANGE; - cbMsg.msgData = state; - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WifiCommonEventHelper::PublishScanFinishedEvent(state,"OnScanFinished"); -} - -void WifiManager::DealScanInfoNotify(std::vector &results, int instId) -{ - if (WifiConfigCenter::GetInstance().GetWifiMidState(instId) == WifiOprMidState::RUNNING) { - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(instId); - if (pService != nullptr) { - pService->ConnectivityManager(results); - } - } -} - -void WifiManager::DealStoreScanInfoEvent(std::vector &results, int instId) -{ -} - -void WifiManager::InitPidfile() -{ - char pidFile[DIR_MAX_LENGTH] = {0, }; - int n = snprintf_s(pidFile, DIR_MAX_LENGTH, DIR_MAX_LENGTH - 1, "%s/%s.pid", CONFIG_ROOR_DIR, WIFI_MANAGGER_PID_NAME); - if (n < 0) { - LOGE("InitPidfile: construct pidFile name failed."); - return; - } - unlink(pidFile); - - pid_t pid = getpid(); - char buf[PID_MAX_LENGTH] = {0}; - if (snprintf_s(buf, PID_MAX_LENGTH, PID_MAX_LENGTH - 1, "%d", pid) < 0) { - LOGE("InitPidfile: pidFile:%{public}s failed, snprintf_s error:%{public}d!", pidFile, errno); - return; - } - - int fd; - if ((fd = open(pidFile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { - LOGE("InitPidfile: open pidFile:%{public}s error:%{public}d!", pidFile, errno); - return; - } - - ssize_t bytes; - if ((bytes = write(fd, buf, strlen(buf))) <= 0) { - LOGE("InitPidfile failed, write pidFile:%{public}s error:%{public}d, bytes:%{public}zd!", - pidFile, errno, bytes); - close(fd); - return; - } - LOGI("InitPidfile: buf:%{public}s write pidFile:%{public}s, bytes:%{public}zd!", buf, pidFile, bytes); - close(fd); - - if (chdir(CONFIG_ROOR_DIR) != 0) { - LOGE("InitPidfile failed, chdir pidDir:%{public}s error:%{public}d!", CONFIG_ROOR_DIR, errno); - return; - } - - umask(DEFAULT_UMASK_VALUE); - chmod(pidFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - return; -} -void WifiManager::DealOpenScanOnlyRes(int instId) -{ - WIFI_LOGI("WifiManager::DealOpenScanOnlyRes"); - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, instId); - return; -} - -void WifiManager::DealCloseScanOnlyRes(int instId) -{ - WIFI_LOGI("DealCloseScanOnlyRes State:%{public}d", WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId)); - if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiMidState(instId)) { - WIFI_LOGI("WifiManager::DealCloseScanOnlyRes wifi is on"); - } else { - WIFI_LOGI("WifiManager::DealCloseScanOnlyRes wifi is off"); - WifiManager::GetInstance().CheckAndStopScanService(instId); - } - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, instId); - return; -} - -#ifdef OHOS_ARCH_LITE -void WifiManager::DealAirplaneExceptionWhenStaOpen(int instId) -{ - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, instId); - WifiConfigCenter::GetInstance().SetStaLastRunState(true, instId); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; - cbMsg.msgData = static_cast(WifiState::ENABLED); - cbMsg.id = instId; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - ErrCode ret = WifiManager::GetInstance().AutoStopStaService(AutoStartOrStopServiceReason::AIRPLANE_MODE, instId); - if (ret != WIFI_OPT_SUCCESS && ret != WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED) { - WIFI_LOGE("DealAirplaneExceptionWhenStaOpen, AutoStopStaService failed!"); -#ifdef FEATURE_P2P_SUPPORT - WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); - WIFI_LOGI("DealAirplaneExceptionWhenStaOpen, current p2p state:%{public}d", p2pState); - if (p2pState == WifiOprMidState::CLOSED) { - WifiManager::GetInstance().AutoStartP2pService( - AutoStartOrStopServiceReason::TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE); - } -#endif - AutoStartEnhanceService(); - CheckAndStartScanService(instId); - if (WifiSettings::GetInstance().CheckScanOnlyAvailable(instId) && - WifiManager::GetInstance().GetLocationModeByDatashare()) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, instId); - } - } - return; -} - -void WifiManager::DealAirplaneExceptionWhenStaClose(int instId) -{ - ErrCode ret = WifiManager::GetInstance().AutoStartStaService(AutoStartOrStopServiceReason::AIRPLANE_MODE, instId); - if (ret != WIFI_OPT_SUCCESS && ret != WIFI_OPT_OPEN_SUCC_WHEN_OPENED) { - WIFI_LOGE("DealAirplaneExceptionWhenStaClose, AutoStartStaService failed!"); -#ifdef FEATURE_P2P_SUPPORT - WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); - WIFI_LOGI("CloseStaService, current p2p state: %{public}d", p2pState); - if (p2pState == WifiOprMidState::RUNNING) { - WifiManager::GetInstance().AutoStopP2pService( - AutoStartOrStopServiceReason::TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE); - } -#endif - return; - } - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::OPEN_WIFI_DUE_TO_AIRPLANEMODE_CLOSED), instId); - return; -} -#endif - -#ifdef FEATURE_AP_SUPPORT -void WifiManager::InitApCallback(void) -{ - mApCallback.callbackModuleName = "WifiManager"; - mApCallback.OnApStateChangedEvent = DealApStateChanged; - mApCallback.OnHotspotStaJoinEvent = DealApGetStaJoin; - mApCallback.OnHotspotStaLeaveEvent = DealApGetStaLeave; - return; -} - -IApServiceCallbacks WifiManager::GetApCallback() -{ - return mApCallback; -} - -void WifiManager::DealApStateChanged(ApState state, int id) -{ - WIFI_LOGE("%{public}s, state: %{public}d!", __func__, state); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE; - cbMsg.msgData = static_cast(state); - cbMsg.id = id; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - if (state == ApState::AP_STATE_IDLE) { -#ifdef OHOS_ARCH_LITE -#ifdef FEATURE_STA_AP_EXCLUSION - if (WifiConfigCenter::GetInstance().GetStaApExclusionType() - == static_cast(StaApExclusionType::USER_OPEN_WIFI_AUTO_STOP_AP) || - WifiConfigCenter::GetInstance().GetStaApExclusionType() - == static_cast(StaApExclusionType::USER_CLOSE_AP_AUTO_START_WIFI)) { - WifiManager::GetInstance().AutoStartStaService(AutoStartOrStopServiceReason::STA_AP_EXCLUSION); - } -#endif -#endif - mCloseApIndex = id; - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSING, id); - WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::AP_SERVICE_CLOSE); - } - if (state == ApState::AP_STATE_STARTED) { - WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, id); -#ifndef OHOS_ARCH_LITE - WifiControllerMachine *ins = WifiManager::GetInstance().GetControllerMachine(); - ins->SendMessage(CMD_AP_START, id); -#endif - } - - std::string msg = std::string("OnHotspotStateChanged") + std::string("id = ") + std::to_string(id); - WifiCommonEventHelper::PublishHotspotStateChangedEvent((int)state, msg); - return; -} - -void WifiManager::DealApGetStaJoin(const StationInfo &info, int id) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_JOIN; - cbMsg.staInfo = info; - cbMsg.id = id; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - std::string msg = std::string("ApStaJoined") + std::string("id = ") + std::to_string(id); - WifiCommonEventHelper::PublishApStaJoinEvent(0, msg); - WifiManager::GetInstance().GetControllerMachine()->StopSoftapCloseTimer(); - return; -} - -void WifiManager::DealApGetStaLeave(const StationInfo &info, int id) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE; - cbMsg.staInfo = info; - cbMsg.id = id; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - std::string msg = std::string("ApStaLeaved") + std::string("id = ") + std::to_string(id); - WifiCommonEventHelper::PublishApStaLeaveEvent(0, msg); - std::vector result; - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(id); - if (pService == nullptr) { - WIFI_LOGE("Instance %{public}d get hotspot service is null!", id); - return; - } - ErrCode errCode = pService->GetStationList(result); - if (errCode != ErrCode::WIFI_OPT_SUCCESS) { - return; - } - if (result.empty()) { - WifiManager::GetInstance().GetControllerMachine()->StartSoftapCloseTimer(); - } - return; -} -#endif - -#ifdef FEATURE_P2P_SUPPORT -void WifiManager::InitP2pCallback(void) -{ - mP2pCallback.OnP2pStateChangedEvent = DealP2pStateChanged; - mP2pCallback.OnP2pPeersChangedEvent = DealP2pPeersChanged; - mP2pCallback.OnP2pServicesChangedEvent = DealP2pServiceChanged; - mP2pCallback.OnP2pConnectionChangedEvent = DealP2pConnectionChanged; - mP2pCallback.OnP2pThisDeviceChangedEvent = DealP2pThisDeviceChanged; - mP2pCallback.OnP2pDiscoveryChangedEvent = DealP2pDiscoveryChanged; - mP2pCallback.OnP2pGroupsChangedEvent = DealP2pGroupsChanged; - mP2pCallback.OnP2pActionResultEvent = DealP2pActionResult; - mP2pCallback.OnConfigChangedEvent = DealConfigChanged; - return; -} - -IP2pServiceCallbacks WifiManager::GetP2pCallback(void) -{ - return mP2pCallback; -} - -void WifiManager::DealP2pStateChanged(P2pState state) -{ - WIFI_LOGI("DealP2pStateChanged, state: %{public}d", static_cast(state)); - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_P2P_STATE_CHANGE; - cbMsg.msgData = static_cast(state); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - if (state == P2pState::P2P_STATE_IDLE) { - WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::P2P_SERVICE_CLOSE); - } - if (state == P2pState::P2P_STATE_STARTED) { - WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING); - WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(); - WIFI_LOGI("DealP2pStateChanged, current sta state:%{public}d", staState); - if (staState == WifiOprMidState::CLOSING || staState == WifiOprMidState::CLOSED) { - WifiManager::GetInstance().AutoStopP2pService( - AutoStartOrStopServiceReason::TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE); - } - } - if (state == P2pState::P2P_STATE_CLOSED) { - bool ret = WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); - if (ret) { - WIFI_LOGE("P2p start failed, stop wifi!"); -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().ForceStopWifi(); -#else - WifiManager::GetInstance().WifiToggled(0, 0); -#endif - cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; - cbMsg.msgData = static_cast(WifiState::DISABLED); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - } - } - WifiCommonEventHelper::PublishP2pStateChangedEvent((int)state, "OnP2pStateChanged"); - return; -} - -void WifiManager::DealP2pPeersChanged(const std::vector &vPeers) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_PEER_CHANGE; - cbMsg.device = vPeers; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WifiCommonEventHelper::PublishP2pPeersStateChangedEvent(vPeers.size(), "OnP2pPeersChanged"); - return; -} - -void WifiManager::DealP2pServiceChanged(const std::vector &vServices) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_SERVICE_CHANGE; - cbMsg.serviceInfo = vServices; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -void WifiManager::DealP2pConnectionChanged(const WifiP2pLinkedInfo &info) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_CONNECT_CHANGE; - cbMsg.p2pInfo = info; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WifiCommonEventHelper::PublishP2pConnStateEvent((int)info.GetConnectState(), "OnP2pConnectStateChanged"); - return; -} - -void WifiManager::DealP2pThisDeviceChanged(const WifiP2pDevice &info) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_THIS_DEVICE_CHANGE; - cbMsg.p2pDevice = info; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WifiCommonEventHelper::PublishP2pCurrentDeviceStateChangedEvent( - (int)info.GetP2pDeviceStatus(), "OnP2pThisDeviceChanged"); - return; -} - -void WifiManager::DealP2pDiscoveryChanged(bool bState) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_DISCOVERY_CHANGE; - cbMsg.msgData = static_cast(bState); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -void WifiManager::DealP2pGroupsChanged() __attribute__((no_sanitize("cfi"))) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - WifiCommonEventHelper::PublishP2pGroupStateChangedEvent(0, "OnP2pGroupStateChanged"); - return; -} - -void WifiManager::DealP2pActionResult(P2pActionCallback action, ErrCode code) -{ - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_P2P_ACTION_RESULT; - cbMsg.p2pAction = action; - cbMsg.msgData = static_cast(code); - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -void WifiManager::DealConfigChanged(CfgType type, char* data, int dataLen) -{ - if (data == nullptr || dataLen <= 0) { - return; - } - WifiEventCallbackMsg cbMsg; - cbMsg.msgCode = WIFI_CBK_MSG_CFG_CHANGE; - CfgInfo* cfgInfoPtr = new (std::nothrow) CfgInfo(); - if (cfgInfoPtr == nullptr) { - WIFI_LOGE("DealConfigChanged: new CfgInfo failed"); - return; - } - cfgInfoPtr->type = type; - char* cfgData = new (std::nothrow) char[dataLen]; - if (cfgData == nullptr) { - WIFI_LOGE("DealConfigChanged: new data failed"); - delete cfgInfoPtr; - return; - } - if (memcpy_s(cfgData, dataLen, data, dataLen) != EOK) { - WIFI_LOGE("DealConfigChanged: memcpy_s failed"); - delete cfgInfoPtr; - delete[] cfgData; - return; - } - cfgInfoPtr->data = cfgData; - cfgInfoPtr->dataLen = dataLen; - cbMsg.cfgInfo = cfgInfoPtr; - WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); - return; -} - -void WifiManager::RegisterCfgMonitorCallback(WifiCfgMonitorEventCallback callback) -{ - cfgMonitorCallback = callback; -} -#endif - -#ifndef OHOS_ARCH_LITE -void WifiManager::RegisterScreenEvent() -{ - std::unique_lock lock(screenEventMutex); - if (isScreenEventSubscribered) { - return; - } - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON); - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); - EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); - screenEventSubscriber_ = std::make_shared(subscriberInfo); - if (!EventFwk::CommonEventManager::SubscribeCommonEvent(screenEventSubscriber_)) { - WIFI_LOGE("ScreenEvent SubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("ScreenEvent SubscribeCommonEvent() OK"); - isScreenEventSubscribered = true; - WifiTimer::GetInstance()->UnRegister(screenTimerId); - } -} - -void WifiManager::UnRegisterScreenEvent() -{ - std::unique_lock lock(screenEventMutex); - if (!isScreenEventSubscribered) { - return; - } - if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(screenEventSubscriber_)) { - WIFI_LOGE("ScreenEvent UnSubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("ScreenEvent UnSubscribeCommonEvent() OK"); - isScreenEventSubscribered = false; - } -} - -void ScreenEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) -{ - std::string action = data.GetWant().GetAction(); - WIFI_LOGI("ScreenEventSubscriber::OnReceiveEvent: %{public}s.", action.c_str()); - - int screenState = WifiSettings::GetInstance().GetScreenState(); - int screenStateNew = (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) - ? MODE_STATE_OPEN : MODE_STATE_CLOSE; - WifiSettings::GetInstance().SetScreenState(screenStateNew); - for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i); - if (pService == nullptr) { - WIFI_LOGE("sta service is NOT start!"); - return; - } - - IScanService *pScanService = WifiServiceManager::GetInstance().GetScanServiceInst(i); - if (pScanService == nullptr) { - WIFI_LOGE("scan service is NOT start!"); - return; - } - #ifndef OHOS_ARCH_LITE - bool isScreenOn = (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) ? true : false; - WifiProtectManager::GetInstance().HandleScreenStateChanged(isScreenOn); - #endif - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF && - screenState == MODE_STATE_OPEN) { - if (pScanService->OnScreenStateChanged(MODE_STATE_CLOSE) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("OnScreenStateChanged failed"); - } - /* Send suspend to wpa */ - if (pService->SetSuspendMode(true) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("SetSuspendMode failed"); - } - pService->OnScreenStateChanged(MODE_STATE_CLOSE); - return; - } - - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON && - screenState == MODE_STATE_CLOSE) { - if (pScanService->OnScreenStateChanged(MODE_STATE_OPEN) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("OnScreenStateChanged failed"); - } - /* Send resume to wpa */ - if (pService->SetSuspendMode(false) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("SetSuspendMode failed"); - } - pService->OnScreenStateChanged(MODE_STATE_OPEN); - return; - } - } -} - -ScreenEventSubscriber::ScreenEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) - : CommonEventSubscriber(subscriberInfo) -{ - WIFI_LOGI("ScreenEventSubscriber enter"); -} - -ScreenEventSubscriber::~ScreenEventSubscriber() -{ - WIFI_LOGI("~ScreenEventSubscriber enter"); -} - -void WifiManager::RegisterAirplaneModeEvent() -{ - std::unique_lock lock(airplaneModeEventMutex); - if (isAirplaneModeEventSubscribered) { - return; - } - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED); - EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); - subscriberInfo.SetPriority(1); - airplaneModeEventSubscriber_ = std::make_shared(subscriberInfo); - if (!EventFwk::CommonEventManager::SubscribeCommonEvent(airplaneModeEventSubscriber_)) { - WIFI_LOGE("AirplaneModeEvent SubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("AirplaneModeEvent SubscribeCommonEvent() OK"); - isAirplaneModeEventSubscribered = true; - WifiTimer::GetInstance()->UnRegister(airplaneModeTimerId); - } -} - -void WifiManager::UnRegisterAirplaneModeEvent() -{ - std::unique_lock lock(airplaneModeEventMutex); - if (!isAirplaneModeEventSubscribered) { - return; - } - if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(airplaneModeEventSubscriber_)) { - WIFI_LOGE("AirplaneModeEvent UnSubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("AirplaneModeEvent UnSubscribeCommonEvent() OK"); - isAirplaneModeEventSubscribered = false; - } -} - -AirplaneModeEventSubscriber::AirplaneModeEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) - : CommonEventSubscriber(subscriberInfo) -{ - WIFI_LOGE("AirplaneModeEventSubscriber enter"); -} - -AirplaneModeEventSubscriber::~AirplaneModeEventSubscriber() -{ - WIFI_LOGE("~AirplaneModeEventSubscriber enter"); -} - -void AirplaneModeEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) -{ - const auto &action = eventData.GetWant().GetAction(); - const auto &data = eventData.GetData(); - const auto &code = eventData.GetCode(); - WIFI_LOGI("AirplaneModeEventSubscriber::OnReceiveEvent: %{public}s, %{public}s, %{public}d", action.c_str(), - data.c_str(), code); - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED) { - if (code == 1) { - /* open airplane mode */ -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().DealOpenAirplaneModeEvent(); -#else - WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_OPEN); - WifiManager::GetInstance().AirplaneToggled(1); -#endif - } else { - /* close airplane mode */ -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().DealCloseAirplaneModeEvent(); -#else - WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_CLOSE); - WifiManager::GetInstance().AirplaneToggled(0); -#endif - } - } -} - -void WifiManager::DealOpenAirplaneModeEvent() -{ - WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_OPEN); -#ifdef OHOS_ARCH_LITE -#ifdef FEATURE_AP_SUPPORT - AutoStopApService(AutoStartOrStopServiceReason::AIRPLANE_MODE); -#endif - - for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(i) == - static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_AIRPLANEMODE)) { - WIFI_LOGI("DealOpenAirplaneModeEvent, user opened sta in airplane mode, ignore openairplanemode event!"); - continue; - } - AutoStopStaService(AutoStartOrStopServiceReason::AIRPLANE_MODE, i); - } -#endif -} - -void WifiManager::DealCloseAirplaneModeEvent() -{ - WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_CLOSE); -#ifdef OHOS_ARCH_LITE - for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(i) == - static_cast(OperatorWifiType::CLOSE_WIFI_DUE_TO_AIRPLANEMODE_OPENED) && - !WifiConfigCenter::GetInstance().GetStaLastRunState(i)) { - ErrCode ret = AutoStartStaService(AutoStartOrStopServiceReason::AIRPLANE_MODE, i); - if (ret != WIFI_OPT_SUCCESS && ret != WIFI_OPT_OPEN_SUCC_WHEN_OPENED) { - WIFI_LOGE("DealCloseAirplaneModeEvent, AutoStartStaService failed!"); - continue; - } - WIFI_LOGI("DealCloseAirplaneModeEvent, auto start wifi success!"); - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::OPEN_WIFI_DUE_TO_AIRPLANEMODE_CLOSED), i); - continue; - } - } -#endif - - if (!WifiConfigCenter::GetInstance().GetStaLastRunState()) { - { - std::unique_lock lock(unloadStaSaTimerMutex); - if (unloadStaSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadStaSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadStaSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("StartUnloadStaSaTimer success! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); - } - } -#ifdef FEATURE_P2P_SUPPORT - { - std::unique_lock lock(unloadP2PSaTimerMutex); - if (unloadP2PSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadP2PSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadP2PSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("StartUnloadP2PSaTimer success! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); - } - } -#endif - } -#ifdef FEATURE_AP_SUPPORT - if (WifiConfigCenter::GetInstance().GetHotspotState() == static_cast(ApState::AP_STATE_CLOSED)) { - std::unique_lock lock(unloadHotspotSaTimerMutex); - if (unloadHotspotSaTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadHotspotSaTimerCallback); - WifiTimer::GetInstance()->Register(timeoutCallback, unloadHotspotSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); - WIFI_LOGI("RegisterUnloadHotspotSaTimer success!unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); - } - } -#endif - return; -} - -void WifiManager::GetAirplaneModeByDatashare() -{ - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("GetAirplaneModeByDatashare, datashareHelper is nullptr!"); - return; - } - - std::string airplaneMode; - Uri uri(SETTINGS_DATASHARE_URL_AIRPLANE_MODE); - int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_AIRPLANE_MODE, airplaneMode); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("GetAirplaneModeByDatashare, Query airplaneMode fail!"); - return; - } - - WIFI_LOGD("GetAirplaneModeByDatashare, airplaneMode:%{public}s", airplaneMode.c_str()); - if (airplaneMode.compare("1") == 0) { - WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_OPEN); - } - return; -} - -void WifiManager::GetDeviceProvisionByDatashare() -{ - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("GetDeviceProvisionByDatashare, datashareHelper is nullptr!"); - return; - } - - std::string provision; - Uri uri(SETTINGS_DATASHARE_URI_DEVICE_PROVISIONED); - int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_DEVICE_PROVISIONED, provision); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("GetDeviceProvisionByDatashare, Query provision fail!"); - return; - } - - WIFI_LOGI("GetDeviceProvisionByDatashare, provision:%{public}s", provision.c_str()); - if (provision.compare("1") == 0) { - WifiConfigCenter::GetInstance().SetDeviceProvisionState(MODE_STATE_CLOSE); - return; - } - WifiConfigCenter::GetInstance().SetDeviceProvisionState(MODE_STATE_OPEN); - return; -} - -bool WifiManager::GetLocationModeByDatashare() -{ - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("GetLocationModeByDatashare, datashareHelper is nullptr!"); - return false; - } - - std::string locationMode; - Uri uri(SETTINGS_DATASHARE_URI_LOCATION_MODE); - int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_LOCATION_MODE, locationMode); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("GetLocationModeByDatashare, Query locationMode fail!"); - return false; - } - - WIFI_LOGD("GetLocationModeByDatashare, locationMode:%{public}s", locationMode.c_str()); - return (locationMode.compare("1") == 0); -} - -int WifiManager::GetLastStaStateByDatashare() -{ - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("GetLastStaStateByDatashare, datashareHelper is nullptr!"); - return 0; - } - - std::string lastStaState; - Uri uri(SETTINGS_DATASHARE_URI_WIFI_ON); - int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_WIFI_ON, lastStaState); - if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("GetLastStaStateByDatashare, Query lastStaState fail!"); - return 0; - } - - WIFI_LOGI("GetLastStaStateByDatashare, lastStaState:%{public}s", lastStaState.c_str()); - int lastStaStateType = ConvertStringToInt(lastStaState); - return lastStaStateType; -} - -void WifiManager::RegisterSettingsMigrateEvent() -{ - std::unique_lock lock(settingsMigrateMutex); - if (settingsMigrateObserver_) { - return; - } - - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("RegisterSettingsMigrateEvent datashareHelper is nullptr"); - return; - } - settingsMigrateObserver_ = sptr(new (std::nothrow)SettingsMigrateObserver()); - Uri uri(SETTINGS_DATASHARE_URI_SETTINGS_MIGRATE); - datashareHelper->RegisterObserver(uri, settingsMigrateObserver_); -} - -void WifiManager::UnRegisterSettingsMigrateEvent() -{ - std::unique_lock lock(settingsMigrateMutex); - if (settingsMigrateObserver_ == nullptr) { - WIFI_LOGE("UnRegisterSettingsMigrateEvent settingsMigrateObserver_ is nullptr"); - return; - } - - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("UnRegisterSettingsMigrateEvent datashareHelper is nullptr"); - return; - } - Uri uri(SETTINGS_DATASHARE_URI_SETTINGS_MIGRATE); - datashareHelper->UnRegisterObserver(uri, settingsMigrateObserver_); - settingsMigrateObserver_ = nullptr; -} - -void WifiManager::RegisterLocationEvent() -{ - std::unique_lock lock(locationEventMutex); - if (islocationModeObservered) { - return; - } - - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("LocationEvent datashareHelper is nullptr"); - return; - } - locationModeObserver_ = sptr(new (std::nothrow)WifiLocationModeObserver()); - Uri uri(SETTINGS_DATASHARE_URI_LOCATION_MODE); - datashareHelper->RegisterObserver(uri, locationModeObserver_); - islocationModeObservered = true; -} - -void WifiManager::UnRegisterLocationEvent() -{ - std::unique_lock lock(locationEventMutex); - if (!islocationModeObservered) { - WIFI_LOGE("UnRegisterLocationEvent islocationModeObservered is false"); - return; - } - - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("UnRegisterLocationEvent datashareHelper is nullptr"); - return; - } - Uri uri(SETTINGS_DATASHARE_URI_LOCATION_MODE); - datashareHelper->UnRegisterObserver(uri, locationModeObserver_); - islocationModeObservered = false; -} - -void WifiManager::RegisterBatteryEvent() -{ - std::unique_lock lock(batteryEventMutex); - if (batterySubscriber_) { - return; - } - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED); - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED); - EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); - batterySubscriber_ = std::make_shared(subscriberInfo); - if (!EventFwk::CommonEventManager::SubscribeCommonEvent(batterySubscriber_)) { - WIFI_LOGE("BatteryEvent SubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("BatteryEvent SubscribeCommonEvent() OK"); - WifiTimer::GetInstance()->UnRegister(batteryTimerId); - } -} - -void WifiManager::UnRegisterBatteryEvent() -{ - std::unique_lock lock(batteryEventMutex); - if (!batterySubscriber_) { - return; - } - if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(batterySubscriber_)) { - WIFI_LOGE("BatteryEvent UnSubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("BatteryEvent UnSubscribeCommonEvent() OK"); - } -} - -void BatteryEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) -{ - std::string action = data.GetWant().GetAction(); - WIFI_LOGI("BatteryEventSubscriber::OnReceiveEvent: %{public}s.", action.c_str()); - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) { - WifiSettings::GetInstance().SetNoChargerPlugModeState(MODE_STATE_CLOSE); - } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) { - WifiSettings::GetInstance().SetNoChargerPlugModeState(MODE_STATE_OPEN); - } - for (int i = 0; i < AP_INSTANCE_MAX_NUM; ++i) { - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(i); - if (pService == nullptr) { - WIFI_LOGE("ap service is NOT start!"); - return; - } - - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) { - WIFI_LOGE("usb connect do not stop hostapd!"); - WifiManager::GetInstance().GetControllerMachine()->StopSoftapCloseTimer(); - return; - } - - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) { - WIFI_LOGE("usb disconnect stop hostapd!"); - std::vector result; - IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(0); - if (pService == nullptr) { - WIFI_LOGE("get hotspot service is null!"); - return; - } - ErrCode errCode = pService->GetStationList(result); - if (errCode != ErrCode::WIFI_OPT_SUCCESS) { - return; - } - if (result.empty()) { - WifiManager::GetInstance().GetControllerMachine()->StartSoftapCloseTimer(); - } - return; - } - } -} - -BatteryEventSubscriber::BatteryEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) - : CommonEventSubscriber(subscriberInfo) -{ - WIFI_LOGI("BatteryEventSubscriber enter"); -} - -BatteryEventSubscriber::~BatteryEventSubscriber() -{ - WIFI_LOGI("~BatteryEventSubscriber exit"); -} - -void WifiManager::RegisterDeviceProvisionEvent() -{ - if (deviceProvisionObserver_) { - return; - } - - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("RegisterDeviceProvisionEvent datashareHelper is nullptr"); - return; - } - WIFI_LOGI("RegisterDeviceProvisionEvent"); - deviceProvisionObserver_ = sptr(new (std::nothrow)WifiDeviceProvisionObserver()); - Uri uri(SETTINGS_DATASHARE_URI_DEVICE_PROVISIONED); - datashareHelper->RegisterObserver(uri, deviceProvisionObserver_); -} - -void WifiManager::UnRegisterDeviceProvisionEvent() -{ - if (deviceProvisionObserver_ == nullptr) { - WIFI_LOGE("UnRegisterLocationEvent deviceProvisionObserver_ is nullptr"); - return; - } - - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - WIFI_LOGE("UnRegisterLocationEvent datashareHelper is nullptr"); - return; - } - WIFI_LOGI("UnRegisterDeviceProvisionEvent"); - Uri uri(SETTINGS_DATASHARE_URI_DEVICE_PROVISIONED); - datashareHelper->UnRegisterObserver(uri, deviceProvisionObserver_); -} - -void WifiManager::DealLocationModeChangeEvent() -{ - if (WifiManager::GetInstance().GetLocationModeByDatashare()) { - WIFI_LOGI("DealLocationModeChangeEvent open"); -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().AutoStartScanOnly(); -#else - WifiManager::GetInstance().ScanOnlyToggled(1); -#endif - } else { - WIFI_LOGI("DealLocationModeChangeEvent close"); -#ifdef OHOS_ARCH_LITE - WifiManager::GetInstance().AutoStopScanOnly(); -#else - WifiManager::GetInstance().ScanOnlyToggled(0); -#endif - } -} - -void WifiManager::CheckAndStartStaByDatashare() -{ - const int openWifi = 1; - const int openWifiInAirplanemode = 2; - const int closeWifiByAirplanemodeOpen = 3; - - int lastStaState = WifiManager::GetInstance().GetLastStaStateByDatashare(); - if (lastStaState == openWifi) { - WifiSettings::GetInstance().SetWifiToggledState(true); - WifiManager::GetInstance().WifiToggled(1, 0); - } else if (lastStaState == openWifiInAirplanemode) { - WifiConfigCenter::GetInstance().SetOpenWifiWhenAirplaneMode(true); - WifiSettings::GetInstance().SetWifiToggledState(true); - WifiManager::GetInstance().WifiToggled(1, 0); - } else if (lastStaState == closeWifiByAirplanemodeOpen) { - WifiSettings::GetInstance().SetWifiToggledState(true); - } - - UnRegisterSettingsMigrateEvent(); - std::unique_lock lock(settingsMigrateMutex); - WifiTimer::GetInstance()->UnRegister(migrateTimerId); - migrateTimerId = 0; -} - -void WifiManager::RegisterPowerStateListener() -{ -#ifdef HAS_POWERMGR_PART - WIFI_LOGD("Enter WifiManager::RegisterPowerStateListener"); - std::unique_lock lock(powerStateEventMutex); - - if (isPowerStateListenerSubscribered) { - WIFI_LOGI("WifiManager::RegisterPowerStateListener, powerStateListener_ already exist!"); - return; - } - - auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); - powerStateListener_ = new (std::nothrow) WifiPowerStateListener(); - if (!powerStateListener_) { - WIFI_LOGE("WifiManager::RegisterPowerStateListener, create power state listener failed"); + char pidFile[DIR_MAX_LENGTH] = {0, }; + int n = snprintf_s(pidFile, DIR_MAX_LENGTH, DIR_MAX_LENGTH - 1, "%s/%s.pid", CONFIG_ROOR_DIR, WIFI_MANAGGER_PID_NAME); + if (n < 0) { + LOGE("InitPidfile: construct pidFile name failed."); return; } + unlink(pidFile); - bool ret = powerManagerClient.RegisterSyncSleepCallback(powerStateListener_, SleepPriority::HIGH); - if (!ret) { - WIFI_LOGE("WifiManager::RegisterPowerStateListener, register power state callback failed"); - } else { - WIFI_LOGI("WifiManager::RegisterPowerStateListener OK!"); - isPowerStateListenerSubscribered = true; - } -#endif -} - -void WifiManager::UnRegisterPowerStateListener() -{ -#ifdef HAS_POWERMGR_PART - WIFI_LOGD("Enter WifiManager::UnRegisterPowerStateListener"); - std::unique_lock lock(powerStateEventMutex); - - if (!isPowerStateListenerSubscribered) { - WIFI_LOGE("WifiManager::UnRegisterPowerStateListener, powerStateListener_ is nullptr"); + pid_t pid = getpid(); + char buf[PID_MAX_LENGTH] = {0}; + if (snprintf_s(buf, PID_MAX_LENGTH, PID_MAX_LENGTH - 1, "%d", pid) < 0) { + LOGE("InitPidfile: pidFile:%{public}s failed, snprintf_s error:%{public}d!", pidFile, errno); return; } - auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); - bool ret = powerManagerClient.UnRegisterSyncSleepCallback(powerStateListener_); - if (!ret) { - WIFI_LOGE("WifiManager::UnRegisterPowerStateListener, unregister power state callback failed"); - } else { - isPowerStateListenerSubscribered = false; - WIFI_LOGI("WifiManager::UnRegisterPowerStateListener OK!"); - } -#endif -} - -#ifdef HAS_MOVEMENT_PART -void WifiManager::RegisterMovementCallBack() -{ - WIFI_LOGI("RegisterMovementCallBack"); - if (!deviceMovementCallback_) { - deviceMovementCallback_ = sptr(new DeviceMovementCallback()); - } - if (Msdp::MovementClient::GetInstance().SubscribeCallback( - Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_) != ERR_OK) { - WIFI_LOGE("Register a device movement observer failed!"); - } -} - -void WifiManager::UnRegisterMovementCallBack() -{ - WIFI_LOGI("UnRegisterMovementCallBack"); - if (!deviceMovementCallback_) { + int fd; + if ((fd = open(pidFile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { + LOGE("InitPidfile: open pidFile:%{public}s error:%{public}d!", pidFile, errno); return; } - Msdp::MovementClient::GetInstance().UnSubscribeCallback( - Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_); - deviceMovementCallback_ = nullptr; -} -#endif - -WifiTimer *WifiTimer::GetInstance() -{ - static WifiTimer instance; - return &instance; -} - -WifiTimer::WifiTimer() : timer_(std::make_unique("WifiManagerTimer")) -{ - timer_->Setup(); -} - -WifiTimer::~WifiTimer() -{ - if (timer_) { - timer_->Shutdown(true); - } -} - -ErrCode WifiTimer::Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval, bool once) -{ - if (timer_ == nullptr) { - WIFI_LOGE("timer_ is nullptr"); - return WIFI_OPT_FAILED; - } - - uint32_t ret = timer_->Register(callback, interval, once); - if (ret == Utils::TIMER_ERR_DEAL_FAILED) { - WIFI_LOGE("Register timer failed"); - return WIFI_OPT_FAILED; - } - - outTimerId = ret; - return WIFI_OPT_SUCCESS; -} -void WifiTimer::UnRegister(uint32_t timerId) -{ - if (timerId == 0) { - WIFI_LOGE("timerId is 0, no register timer"); + ssize_t bytes; + if ((bytes = write(fd, buf, strlen(buf))) <= 0) { + LOGE("InitPidfile failed, write pidFile:%{public}s error:%{public}d, bytes:%{public}zd!", + pidFile, errno, bytes); + close(fd); return; } + LOGI("InitPidfile: buf:%{public}s write pidFile:%{public}s, bytes:%{public}zd!", buf, pidFile, bytes); + close(fd); - if (timer_ == nullptr) { - WIFI_LOGE("timer_ is nullptr"); + if (chdir(CONFIG_ROOR_DIR) != 0) { + LOGE("InitPidfile failed, chdir pidDir:%{public}s error:%{public}d!", CONFIG_ROOR_DIR, errno); return; } - timer_->Unregister(timerId); + umask(DEFAULT_UMASK_VALUE); + chmod(pidFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); return; } -#endif } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h index c27d3dcf76686d75712ccc44e86122cd6e4e0a5a..dc564fb9b33161b6fe52e50c7f7ec3dc55e6a390 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h @@ -16,34 +16,26 @@ #define OHOS_WIFIMANAGER_H #include -#include #include #include #include #include -#include - #include #include #include "define.h" #include "wifi_internal_msg.h" -#include "sta_service_callback.h" -#include "iscan_service_callbacks.h" #include "wifi_errcode.h" -#include "wifi_system_ability_listerner.h" +#include "wifi_sta_manager.h" +#include "wifi_scan_manager.h" +#include "wifi_toggler_manager.h" #ifdef FEATURE_AP_SUPPORT -#include "i_ap_service_callbacks.h" +#include "wifi_hotspot_manager.h" #endif #ifdef FEATURE_P2P_SUPPORT -#include "ip2p_service_callbacks.h" -#endif -#ifndef OHOS_ARCH_LITE -#include "common_event_manager.h" -#include "timer.h" +#include "wifi_p2p_manager.h" #endif #ifndef OHOS_ARCH_LITE -#include "wifi_controller_define.h" -#include "wifi_controller_state_machine.h" +#include "wifi_event_subscriber_manager.h" #endif namespace OHOS { @@ -74,53 +66,9 @@ struct WifiCloseServiceMsg int instId; }; -struct WifiCfgMonitorEventCallback { - std::function onStaConnectionChange = nullptr; -}; - -#ifndef OHOS_ARCH_LITE -class ScreenEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { -public: - explicit ScreenEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); - virtual ~ScreenEventSubscriber(); - void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; -}; - -class AirplaneModeEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { -public: - explicit AirplaneModeEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); - virtual ~AirplaneModeEventSubscriber(); - void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override; -}; - -class BatteryEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { +class WifiManager { public: - explicit BatteryEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); - ~BatteryEventSubscriber(); - void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override; -}; - -class WifiTimer { -public: - using TimerCallback = std::function; - static constexpr uint32_t DEFAULT_TIMEROUT = 10000; - static WifiTimer *GetInstance(void); - - WifiTimer(); - ~WifiTimer(); - - ErrCode Register( - const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT, bool once = true); - void UnRegister(uint32_t timerId); - -private: - std::unique_ptr timer_{nullptr}; -}; -#endif - -class WifiManager : WifiSystemAbilityListener { -public: - WifiManager(); + static WifiManager &GetInstance(); ~WifiManager(); /** * @Description Initialize submodules and message processing threads. @@ -144,68 +92,6 @@ public: */ void Exit(); - /** - * @Description Get the sta callback object. - * - * @return StaServiceCallback - return mStaCallback - */ - StaServiceCallback GetStaCallback(void); - - /** - * @Description Get the scan callback object. - * - * @return IScanSerivceCallbacks - return mScanCallback - */ - IScanSerivceCallbacks GetScanCallback(void); -#ifdef OHOS_ARCH_LITE - ErrCode AutoStartStaService(AutoStartOrStopServiceReason reason, int instId = 0); - ErrCode AutoStopStaService(AutoStartOrStopServiceReason reason, int instId = 0); -#endif -#ifndef OHOS_ARCH_LITE - ConcreteModeCallback GetConcreteCallback(void); - SoftApModeCallback GetSoftApCallback(void); -#endif - void StopUnloadStaSaTimer(void); - void StartUnloadStaSaTimer(void); - void StopUnloadScanSaTimer(void); - void StartUnloadScanSaTimer(void); - - void OnSystemAbilityChanged(int systemAbilityId, bool add) override; - -#ifdef FEATURE_AP_SUPPORT - /** - * @Description Get the ap callback object. - * - * @return IApServiceCallbacks - return mApCallback - */ - IApServiceCallbacks GetApCallback(void); - -#ifdef OHOS_ARCH_LITE - ErrCode AutoStartApService(AutoStartOrStopServiceReason reason); - ErrCode AutoStopApService(AutoStartOrStopServiceReason reason); -#endif - void StopUnloadApSaTimer(void); - void StartUnloadApSaTimer(void); -#endif - -#ifdef FEATURE_P2P_SUPPORT - /** - * @Description Get the p2p callback object. - * - * @return IP2pServiceCallbacks - return mP2pCallback - */ - IP2pServiceCallbacks GetP2pCallback(void); - - ErrCode AutoStartP2pService(AutoStartOrStopServiceReason reason); - ErrCode AutoStopP2pService(AutoStartOrStopServiceReason reason); - void StopUnloadP2PSaTimer(void); - void StartUnloadP2PSaTimer(void); -#endif - -#ifdef FEATURE_HPF_SUPPORT - void InstallPacketFilterProgram(int screenState, int instId); -#endif - /** * @Description Get supported features * @@ -220,189 +106,52 @@ public: * @param feature */ void AddSupportedFeatures(WifiFeatures feature); - - static WifiManager &GetInstance(); - - void RegisterCfgMonitorCallback(WifiCfgMonitorEventCallback callback); - void GetAirplaneModeByDatashare(); - void GetDeviceProvisionByDatashare(); - void DealOpenAirplaneModeEvent(); - void DealCloseAirplaneModeEvent(); - void DealLocationModeChangeEvent(); - bool GetLocationModeByDatashare(); void PushServiceCloseMsg(WifiCloseServiceCode code, int instId = 0); - static void CheckAndStartScanService(int instId = 0); - void CheckAndStopScanService(int instId = 0); - void DealOpenScanOnlyRes(int instId = 0); - void DealCloseScanOnlyRes(int instId = 0); - void ForceStopWifi(int instId = 0); -#ifndef OHOS_ARCH_LITE - bool IsMdmForbidden(void); - int GetLastStaStateByDatashare(); - void CheckAndStartStaByDatashare(); - ErrCode WifiToggled(int isOpen, int id); - ErrCode SoftapToggled(int isOpen, int id); - ErrCode ScanOnlyToggled(int isOpen); - ErrCode AirplaneToggled(int isOpen); - WifiControllerMachine *GetControllerMachine(); - bool HasAnyApRuning(); -#endif - -private: - void InitStaCallback(void); - void InitScanCallback(void); - void InitSubscribeListener(); -#ifdef FEATURE_AP_SUPPORT - void InitApCallback(void); -#ifndef OHOS_ARCH_LITE - void InitSoftapCallback(void); -#endif -#endif -#ifndef OHOS_ARCH_LITE - void InitConcreteCallback(void); -#endif -#ifdef FEATURE_P2P_SUPPORT - void InitP2pCallback(void); -#endif - InitStatus GetInitStatus(); - static void DealCloseServiceMsg(WifiManager &manager); - static void CloseStaService(int instId = 0); - static void UnloadStaSaTimerCallback(); - static void UnloadScanSaTimerCallback(); + void AutoStartEnhanceService(void); + std::unique_ptr& GetWifiStaManager(); + std::unique_ptr& GetWifiScanManager(); + std::unique_ptr& GetWifiTogglerManager(); #ifdef FEATURE_AP_SUPPORT - static void CloseApService(int id = 0); - static void UnloadHotspotSaTimerCallback(); + std::unique_ptr& GetWifiHotspotManager(); #endif - static void CloseScanService(int instId = 0); #ifdef FEATURE_P2P_SUPPORT - static void CloseP2pService(void); - static void UnloadP2PSaTimerCallback(); + std::unique_ptr& GetWifiP2pManager(); #endif #ifndef OHOS_ARCH_LITE - static void DealConcreateStop(int id); - static void DealConcreateStartFailure(int id); - static void DealSoftapStop(int id); - static void DealSoftapStartFailure(int id); -#endif - static void DealStaOpenRes(OperateResState state, int instId = 0); - static void DealStaCloseRes(OperateResState state, int instId = 0); - static void DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId = 0); - static void DealWpsChanged(WpsStartState state, const int pinCode, int instId = 0); - static void DealStreamChanged(StreamDirection direction, int instId = 0); - static void DealRssiChanged(int rssi, int instId = 0); - static void DealScanOpenRes(int instId = 0); - static void DealScanCloseRes(int instId = 0); - static void DealScanFinished(int state, int instId = 0); - static void DealScanInfoNotify(std::vector &results, int instId = 0); - static void DealStoreScanInfoEvent(std::vector &results, int instId = 0); - static void DealAirplaneExceptionWhenStaOpen(int instId = 0); - static void DealAirplaneExceptionWhenStaClose(int instId = 0); -#ifdef FEATURE_AP_SUPPORT - static void DealApStateChanged(ApState bState, int id = 0); - static void DealApGetStaJoin(const StationInfo &info, int id = 0); - static void DealApGetStaLeave(const StationInfo &info, int id = 0); -#endif -#ifdef FEATURE_P2P_SUPPORT - static void DealP2pStateChanged(P2pState bState); - static void DealP2pPeersChanged(const std::vector &vPeers); - static void DealP2pServiceChanged(const std::vector &vServices); - static void DealP2pConnectionChanged(const WifiP2pLinkedInfo &info); - static void DealP2pThisDeviceChanged(const WifiP2pDevice &info); - static void DealP2pDiscoveryChanged(bool bState); - static void DealP2pGroupsChanged(void); - static void DealP2pActionResult(P2pActionCallback action, ErrCode code); - static void DealConfigChanged(CfgType type, char* data, int dataLen); + std::unique_ptr& GetWifiEventSubscriberManager(); #endif -#ifdef OHOS_ARCH_LITE - static void AutoStartScanOnly(int instId = 0); - static void AutoStopScanOnly(int instId = 0); +#ifdef FEATURE_HPF_SUPPORT + void InstallPacketFilterProgram(int screenState, int instId); #endif - static void AutoStartScanService(int instId = 0); - static void AutoStartEnhanceService(void); - static void CheckAndStartSta(AutoStartOrStopServiceReason reason); - static void AutoStartServiceThread(AutoStartOrStopServiceReason reason); +private: + WifiManager(); + InitStatus GetInitStatus(); + void DealCloseServiceMsg(); + void CheckAndStartSta(); + void AutoStartServiceThread(); void InitPidfile(void); + private: + InitStatus mInitStatus; + long mSupportedFeatures; std::thread mCloseServiceThread; std::mutex mMutex; - std::mutex screenEventMutex; - std::mutex airplaneModeEventMutex; - std::mutex locationEventMutex; - std::mutex batteryEventMutex; -#ifdef HAS_POWERMGR_PART - std::mutex powerStateEventMutex; -#endif std::condition_variable mCondition; std::deque mEventQue; -#ifndef OHOS_ARCH_LITE - ConcreteModeCallback mConcreteModeCb; - SoftApModeCallback mSoftApModeCb; - WifiControllerMachine *pWifiControllerMachine; -#endif - StaServiceCallback mStaCallback; - IScanSerivceCallbacks mScanCallback; -#ifndef OHOS_ARCH_LITE - std::mutex settingsMigrateMutex; - static uint32_t unloadStaSaTimerId; - static std::mutex unloadStaSaTimerMutex; - static uint32_t unloadScanSaTimerId; - static std::mutex unloadScanSaTimerMutex; -#endif + + std::unique_ptr wifiStaManager = nullptr; + std::unique_ptr wifiScanManager = nullptr; + std::unique_ptr wifiTogglerManager = nullptr; #ifdef FEATURE_AP_SUPPORT - IApServiceCallbacks mApCallback; -#ifndef OHOS_ARCH_LITE - static uint32_t unloadHotspotSaTimerId; - static std::mutex unloadHotspotSaTimerMutex; -#endif + std::unique_ptr wifiHotspotManager = nullptr; #endif #ifdef FEATURE_P2P_SUPPORT - IP2pServiceCallbacks mP2pCallback; - static WifiCfgMonitorEventCallback cfgMonitorCallback; -#ifndef OHOS_ARCH_LITE - static uint32_t unloadP2PSaTimerId; - static std::mutex unloadP2PSaTimerMutex; -#endif + std::unique_ptr wifiP2pManager = nullptr; #endif #ifndef OHOS_ARCH_LITE - void RegisterScreenEvent(); - void UnRegisterScreenEvent(); - std::shared_ptr screenEventSubscriber_ = nullptr; - bool isScreenEventSubscribered = false; - uint32_t screenTimerId{0}; - void RegisterAirplaneModeEvent(); - void UnRegisterAirplaneModeEvent(); - std::shared_ptr airplaneModeEventSubscriber_ = nullptr; - bool isAirplaneModeEventSubscribered = false; - uint32_t airplaneModeTimerId{0}; - void RegisterLocationEvent(); - void UnRegisterLocationEvent(); - void RegisterDeviceProvisionEvent(); - void UnRegisterDeviceProvisionEvent(); - uint32_t locationTimerId{0}; - std::shared_ptr batterySubscriber_ = nullptr; - uint32_t batteryTimerId{0}; - void RegisterBatteryEvent(); - void UnRegisterBatteryEvent(); - void RegisterSettingsMigrateEvent(); - void UnRegisterSettingsMigrateEvent(); - void GetMdmProp(); - void GetChipProp(); - void RegisterMdmPropListener(); - static void MdmPropChangeEvt(const char *key, const char *value, void *context); - uint32_t migrateTimerId{0}; - static bool mIsMdmForbidden; - void RegisterPowerStateListener(); - void UnRegisterPowerStateListener(); - bool isPowerStateListenerSubscribered = false; -#ifdef HAS_MOVEMENT_PART - void RegisterMovementCallBack(); - void UnRegisterMovementCallBack(); + std::unique_ptr wifiEventSubscriberManager = nullptr; #endif -#endif - InitStatus mInitStatus; - long mSupportedFeatures; - static int mCloseApIndex; }; } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_callback_proxy.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_callback_proxy.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_callback_proxy.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_callback_proxy.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_callback_proxy.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_callback_proxy.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_callback_proxy.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_callback_proxy.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_death_recipient.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_death_recipient.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_death_recipient.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_death_recipient.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_service_impl.cpp similarity index 99% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_service_impl.cpp index 55c4ea93c8b9a0946c389e2fdfea0b488be1ddd4..1ba8a5454c59c37c135c58168f83ad233be4e957 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_service_impl.cpp @@ -75,7 +75,7 @@ void WifiP2pServiceImpl::OnStart() WifiManager::GetInstance().AddSupportedFeatures(WifiFeatures::WIFI_FEATURE_P2P); WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); if (p2pState == WifiOprMidState::CLOSED) { - WifiManager::GetInstance().StartUnloadP2PSaTimer(); + WifiManager::GetInstance().GetWifiP2pManager()->StartUnloadP2PSaTimer(); } } @@ -139,7 +139,7 @@ ErrCode WifiP2pServiceImpl::EnableP2p(void) } } if (!WifiConfigCenter::GetInstance().SetP2pMidState(curState, WifiOprMidState::OPENING)) { - WIFI_LOGD("set p2p mid state opening failed! may be other activity has been operated"); + WIFI_LOGD("set p2p mid state opening failed!"); return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; } ErrCode ret = WIFI_OPT_FAILED; @@ -153,7 +153,7 @@ ErrCode WifiP2pServiceImpl::EnableP2p(void) WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_P2P); break; } - ret = pService->RegisterP2pServiceCallbacks(WifiManager::GetInstance().GetP2pCallback()); + ret = pService->RegisterP2pServiceCallbacks(WifiManager::GetInstance().GetWifiP2pManager()->GetP2pCallback()); if (ret != WIFI_OPT_SUCCESS) { WIFI_LOGE("Register p2p service callback failed!"); break; @@ -168,7 +168,7 @@ ErrCode WifiP2pServiceImpl::EnableP2p(void) WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); } else { - WifiManager::GetInstance().StopUnloadP2PSaTimer(); + WifiManager::GetInstance().GetWifiP2pManager()->StopUnloadP2PSaTimer(); } return ret; } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_service_impl.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_service_impl.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_power_state_listener.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_power_state_listener.cpp index 9a3f969fa34fbac31bea344596d4ea64121e15a1..6cc18c17b7571d974a79e97c7862870106bd49a9 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_power_state_listener.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_power_state_listener.cpp @@ -35,7 +35,6 @@ WifiPowerStateListener::WifiPowerStateListener() void WifiPowerStateListener::OnSyncSleep(bool onForceSleep) { WIFI_LOGI("Enter WifiPowerStateListener::OnSyncSleep"); - if (!onForceSleep) { WIFI_LOGI("WifiPowerStateListener::OnSyncSleep not force sleep"); return; @@ -47,7 +46,6 @@ void WifiPowerStateListener::OnSyncSleep(bool onForceSleep) void WifiPowerStateListener::OnSyncWakeup(bool onForceSleep) { WIFI_LOGI("Enter WifiPowerStateListener::OnSyncWakeup"); - if (!onForceSleep) { WIFI_LOGI("WifiPowerStateListener::OnSyncWakeup not force sleep"); return; @@ -59,13 +57,11 @@ void WifiPowerStateListener::OnSyncWakeup(bool onForceSleep) void WifiPowerStateListener::DealPowerEnterSleepEvent() { WIFI_LOGI("DealPowerEnterSleepEvent Enter!"); - WifiConfigCenter::GetInstance().SetPowerSleepState(MODE_STATE_OPEN); -#ifndef OHOS_ARCH_LITE #ifdef FEATURE_AP_SUPPORT for (int idx = 0; idx < AP_INSTANCE_MAX_NUM; idx++) { - WifiManager::GetInstance().SoftapToggled(0, idx); + WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(0, idx); } #endif @@ -74,29 +70,25 @@ void WifiPowerStateListener::DealPowerEnterSleepEvent() if (staState == WifiOprMidState::RUNNING) { bWifiStateBeforeSleep[idx] = true; WifiSettings::GetInstance().SetWifiToggledState(false); - WifiManager::GetInstance().WifiToggled(0, idx); + WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(0, idx); /* For the forced sleep mode, the final status does not need to be stored. */ WifiConfigCenter::GetInstance().SetStaLastRunState(true, idx); } } -#endif - return; } void WifiPowerStateListener::DealPowerExitSleepEvent() { WIFI_LOGI("DealPowerExitSleepEvent Enter!"); - WifiConfigCenter::GetInstance().SetPowerSleepState(MODE_STATE_CLOSE); -#ifndef OHOS_ARCH_LITE /* Re-opening is required only if it was previously turned on and then turned off when entering enforce sleep. */ for (int idx = 0; idx < STA_INSTANCE_MAX_NUM; idx++) { if (bWifiStateBeforeSleep[idx]) { WifiSettings::GetInstance().SetWifiToggledState(true); - ErrCode ret = WifiManager::GetInstance().WifiToggled(1, idx); + ErrCode ret = WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(1, idx); if (ret != WIFI_OPT_SUCCESS) { WIFI_LOGE("DealPowerExitSleepEvent, AutoStartStaService failed, err_code = %{public}d!", ret); } else { @@ -105,7 +97,6 @@ void WifiPowerStateListener::DealPowerExitSleepEvent() bWifiStateBeforeSleep[idx] = false; } } -#endif return; } } // namespace Wifi diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_callback_proxy.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_callback_proxy.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_callback_proxy.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_callback_proxy.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_callback_proxy_lite.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy_lite.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_callback_proxy_lite.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_callback_proxy_lite.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_death_recipient.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_death_recipient.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_death_recipient.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_death_recipient.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_death_recipient.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_death_recipient.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_death_recipient.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_death_recipient.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_feature_lite.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_feature_lite.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_feature_lite.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_feature_lite.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_service_impl.cpp similarity index 98% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_service_impl.cpp index ee2e547ce0f366d7e252a5a55b59a1d6b80fb0b6..e9b4da902ad639b0dfb6fe41cd4d714e3fcdd9bb 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_service_impl.cpp @@ -74,7 +74,7 @@ void WifiScanMgrServiceImpl::OnStart() } if (isStartUnloadScanSaTimer) { - WifiManager::GetInstance().StartUnloadScanSaTimer(); + WifiManager::GetInstance().GetWifiScanManager()->StartUnloadScanSaTimer(); } return; } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_service_impl.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_service_impl.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_mgr_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_mgr_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.cpp similarity index 81% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.cpp index 13f3a96b4f3116fb81e2ee475e46bcd7372f883e..9196ad40b2f71da7955ff205e283c8e45c32513c 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.cpp @@ -258,17 +258,9 @@ ErrCode WifiScanServiceImpl::SetScanOnlyAvailable(bool bScanOnlyAvailable) WifiSettings::GetInstance().SetScanOnlySwitchState(bScanOnlyAvailable, m_instId); ErrCode errCode = WIFI_OPT_SUCCESS; if (bScanOnlyAvailable) { -#ifdef OHOS_ARCH_LITE - OpenScanOnlyAvailable(); -#else - errCode = WifiManager::GetInstance().ScanOnlyToggled(1); -#endif + errCode = WifiManager::GetInstance().GetWifiTogglerManager()->ScanOnlyToggled(1); } else { -#ifdef OHOS_ARCH_LITE - CloseScanOnlyAvailable(); -#else - errCode = WifiManager::GetInstance().ScanOnlyToggled(0); -#endif + errCode = WifiManager::GetInstance().GetWifiTogglerManager()->ScanOnlyToggled(0); } return errCode; } @@ -308,73 +300,6 @@ ErrCode WifiScanServiceImpl::StartWifiPnoScan(bool isStartAction, int periodMs, return pService->StartWifiPnoScan(isStartAction, periodMs, suspendReason); } -ErrCode WifiScanServiceImpl::OpenScanOnlyAvailable() -{ - WIFI_LOGI("WifiScanServiceImpl OpenScanOnlyAvailable"); - if (!WifiSettings::GetInstance().CheckScanOnlyAvailable(m_instId) || - !WifiManager::GetInstance().GetLocationModeByDatashare()) { - return WIFI_OPT_FAILED; - } - - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(m_instId); - WIFI_LOGI("current wifi scan only state is %{public}d", static_cast(curState)); - if (curState != WifiOprMidState::CLOSED) { - if (curState == WifiOprMidState::CLOSING) { /* when current wifi is closing, return */ - return WIFI_OPT_FAILED; - } else { - return WIFI_OPT_SUCCESS; - } - } - - if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiMidState(m_instId)) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, m_instId); - return WIFI_OPT_SUCCESS; - } - - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::OPENING, m_instId); - WifiManager::GetInstance().CheckAndStartScanService(m_instId); - int res = WifiStaHalInterface::GetInstance().StartWifi(); - if (res != static_cast(WIFI_IDL_OPT_OK)) { - WIFI_LOGE("Start Wpa failed"); - return WIFI_OPT_FAILED; - } - WifiManager::GetInstance().DealOpenScanOnlyRes(m_instId); - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::RUNNING, m_instId); - - return WIFI_OPT_SUCCESS; -} - -ErrCode WifiScanServiceImpl::CloseScanOnlyAvailable() -{ - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(m_instId); - WIFI_LOGI("current wifi scan only state is %{public}d", static_cast(curState)); - if (curState != WifiOprMidState::RUNNING) { - if (curState == WifiOprMidState::OPENING) { /* when current wifi is opening, return */ - return WIFI_OPT_FAILED; - } else { - return WIFI_OPT_SUCCESS; - } - } - - if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiMidState(m_instId)) { - WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(WifiOprMidState::CLOSED, m_instId); - return WIFI_OPT_SUCCESS; - } - - if (!WifiConfigCenter::GetInstance().SetWifiScanOnlyMidState(curState, WifiOprMidState::CLOSING, m_instId)) { - WIFI_LOGI("set wifi scan only mid state opening failed!"); - return WIFI_OPT_FAILED; - } - - int res = WifiStaHalInterface::GetInstance().StopWifi(); - if (res != static_cast(WIFI_IDL_OPT_OK)) { - WIFI_LOGE("Stop Wpa failed"); - return WIFI_OPT_FAILED; - } - WifiManager::GetInstance().DealCloseScanOnlyRes(m_instId); - return WIFI_OPT_SUCCESS; -} - #ifdef OHOS_ARCH_LITE ErrCode WifiScanServiceImpl::RegisterCallBack(const std::shared_ptr &callback, const std::vector &event) diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.h similarity index 97% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.h index e33448ea3606902c7e4f016265a922181b48d517..4eba8c0794c2ee3fafb5b4e6c4999fb1d7d4d2ff 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_service_impl.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_service_impl.h @@ -63,8 +63,6 @@ public: private: bool Init(); bool IsScanServiceRunning(); - ErrCode OpenScanOnlyAvailable(); - ErrCode CloseScanOnlyAvailable(); #ifndef OHOS_ARCH_LITE void UpdateScanMode(); #endif diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub_lite.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub_lite.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub_lite.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub_lite.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub_lite.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub_lite.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_stub_lite.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa/wifi_scan_stub_lite.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_service_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_service_manager.cpp index 73fe35947f29bb9a88649577d6086f031480d3d6..70daeaa8703655235c4cd918757440b2a7d795d5 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_service_manager.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_service_manager.cpp @@ -122,7 +122,7 @@ int WifiServiceManager::LoadStaService(const std::string &dlname, bool bCreate) IStaService *service = mStaServiceHandle.create(0); mStaServiceHandle.pService[0] = service; } - WifiManager::GetInstance().StopUnloadStaSaTimer(); + WifiManager::GetInstance().GetWifiStaManager()->StopUnloadStaSaTimer(); return 0; } @@ -151,7 +151,7 @@ int WifiServiceManager::LoadScanService(const std::string &dlname, bool bCreate) IScanService *service = mScanServiceHandle.create(0); mScanServiceHandle.pService[0] = service; } - WifiManager::GetInstance().StopUnloadScanSaTimer(); + WifiManager::GetInstance().GetWifiScanManager()->StopUnloadScanSaTimer(); return 0; } @@ -184,7 +184,7 @@ int WifiServiceManager::LoadApService(const std::string &dlname, bool bCreate) mApServiceHandle.pService[0] = service; } } - WifiManager::GetInstance().StopUnloadApSaTimer(); + WifiManager::GetInstance().GetWifiHotspotManager()->StopUnloadApSaTimer(); return 0; } #endif @@ -214,7 +214,7 @@ int WifiServiceManager::LoadP2pService(const std::string &dlname, bool bCreate) if (bCreate) { mP2pServiceHandle.pService = mP2pServiceHandle.create(); } - WifiManager::GetInstance().StopUnloadP2PSaTimer(); + WifiManager::GetInstance().GetWifiP2pManager()->StopUnloadP2PSaTimer(); return 0; } #endif diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service.cpp index cb1bad9deccfe758af4d1225865dc2932adca94b..f19987c80adcf06f4bfe1a5ad4fe0514535b87af 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service.cpp @@ -27,7 +27,6 @@ #include "wifi_cert_utils.h" #include "wifi_common_util.h" #include "wifi_config_center.h" -#include "wifi_manager.h" DEFINE_WIFILOG_LABEL("StaService"); @@ -623,9 +622,6 @@ void StaService::HandleScreenStatusChanged(int screenState) } pStaStateMachine->SendMessage(WIFI_SCREEN_STATE_CHANGED_NOTIFY_EVENT, screenState); -#endif -#ifdef FEATURE_HPF_SUPPORT - WifiManager::GetInstance().InstallPacketFilterProgram(screenState, m_instId); #endif return; } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_callback_proxy.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_callback_proxy.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_callback_proxy.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_callback_proxy.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_callback_proxy_lite.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy_lite.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_callback_proxy_lite.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_callback_proxy_lite.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_death_recipient.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_death_recipient.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_death_recipient.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_death_recipient.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_death_recipient.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_death_recipient.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_death_recipient.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_death_recipient.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_feature_lite.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_feature_lite.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_feature_lite.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_feature_lite.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_service_impl.cpp similarity index 93% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_service_impl.cpp index 3b00427cb0f7b265c95f985709dc01b391e9e4c9..4a80c6bbbc7dbdfbfcadf7c7786dc07f4279c1b9 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_service_impl.cpp @@ -63,9 +63,9 @@ void WifiDeviceMgrServiceImpl::OnStart() } mState = ServiceRunningState::STATE_RUNNING; WIFI_LOGI("Start sta service!"); - WifiManager::GetInstance().GetAirplaneModeByDatashare(); - WifiManager::GetInstance().GetDeviceProvisionByDatashare(); - WifiManager::GetInstance().StartUnloadStaSaTimer(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetAirplaneModeByDatashare(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetDeviceProvisionByDatashare(); + WifiManager::GetInstance().GetWifiStaManager()->StartUnloadStaSaTimer(); WifiDeviceServiceImpl::StartWatchdog(); } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_service_impl.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_service_impl.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_mgr_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_mgr_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.cpp similarity index 81% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.cpp index 73e6caa2463f8f0c62b9dae398130a1cde06ca58..6156890baf883d23d8c52676a831edb9f142d493 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.cpp @@ -96,33 +96,12 @@ WifiDeviceServiceImpl::WifiDeviceServiceImpl() WifiDeviceServiceImpl::WifiDeviceServiceImpl(int instId) : WifiDeviceStub(instId) { WIFI_LOGI("enter WifiDeviceServiceImpl"); - using TimeOutCallback = std::function; - const uint32_t TIMEOUT_APP_EVENT = 3000; - const uint32_t TIMEOUT_THERMAL_EVENT = 3000; - if (eventSubscriber_ == nullptr && appEventTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(&WifiDeviceServiceImpl::RegisterAppRemoved, this); - WifiTimer::GetInstance()->Register(timeoutCallback, appEventTimerId, TIMEOUT_APP_EVENT, false); - } - - if (thermalLevelSubscriber_ == nullptr && thermalTimerId == 0) { - TimeOutCallback timeoutCallback = std::bind(&WifiDeviceServiceImpl::RegisterThermalLevel, this); - WifiTimer::GetInstance()->Register(timeoutCallback, thermalTimerId, TIMEOUT_THERMAL_EVENT, false); - } } #endif WifiDeviceServiceImpl::~WifiDeviceServiceImpl() { WIFI_LOGI("enter ~WifiDeviceServiceImpl"); -#ifndef OHOS_ARCH_LITE - if (eventSubscriber_ != nullptr) { - UnRegisterAppRemoved(); - } - - if (thermalLevelSubscriber_ != nullptr) { - UnRegisterThermalLevel(); - } -#endif } ErrCode WifiDeviceServiceImpl::EnableWifi() @@ -131,95 +110,12 @@ ErrCode WifiDeviceServiceImpl::EnableWifi() if (errCode != WIFI_OPT_SUCCESS) { return errCode; } -#ifdef OHOS_ARCH_LITE - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiMidState(m_instId); - if (curState != WifiOprMidState::CLOSED) { - WIFI_LOGI("current wifi state is %{public}d", static_cast(curState)); - if (curState == WifiOprMidState::CLOSING) { /* when current wifi is closing, return */ - return WIFI_OPT_OPEN_FAIL_WHEN_CLOSING; - } else { - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - } - -#ifdef FEATURE_AP_SUPPORT - WifiOprMidState apState = WifiConfigCenter::GetInstance().GetApMidState(0); - if (apState != WifiOprMidState::CLOSED) { -#ifdef FEATURE_STA_AP_EXCLUSION - errCode = WifiManager::GetInstance().AutoStopApService(AutoStartOrStopServiceReason::STA_AP_EXCLUSION); - if (errCode != WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED) { - return errCode; - } -#else - WIFI_LOGW("current ap state is %{public}d, please close SoftAp first!", - static_cast(curState)); - return WIFI_OPT_NOT_SUPPORTED; -#endif - } -#endif - - if (!WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::OPENING, m_instId)) { - WIFI_LOGI("set wifi mid state opening failed!"); - return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; - } - - errCode = WIFI_OPT_FAILED; - do { - if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_STA) < 0) { - break; - } - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(m_instId); - if (pService == nullptr) { - WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_STA); - break; - } - errCode = pService->RegisterStaServiceCallback(WifiManager::GetInstance().GetStaCallback()); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("Register sta service callback failed!"); - break; - } - errCode = pService->EnableWifi(); - if (errCode != WIFI_OPT_SUCCESS) { - WIFI_LOGE("service enable sta failed, ret %{public}d!", static_cast(errCode)); - break; - } - } while (false); - if (errCode != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, m_instId); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, m_instId); - return errCode; - } -#ifdef FEATURE_P2P_SUPPORT - sptr p2pService = WifiP2pServiceImpl::GetInstance(); - if (p2pService != nullptr && p2pService->EnableP2p() != WIFI_OPT_SUCCESS) { - // only record to log - WIFI_LOGE("Enable P2p failed!"); - } -#endif - - WifiSettings::GetInstance().SyncWifiConfig(); - WifiManager::GetInstance().StopUnloadStaSaTimer(); - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(m_instId) == - static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_AIRPLANEMODE)) { - WIFI_LOGI("EnableWifi, user opened wifi in airplane mode!"); - return WIFI_OPT_SUCCESS; - } - - int operatorWifiType = static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_NO_AIRPLANEMODE); - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - operatorWifiType = static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_AIRPLANEMODE); - WIFI_LOGI("EnableWifi, current airplane mode is opened, user open wifi!"); - } - WifiConfigCenter::GetInstance().SetOperatorWifiType(operatorWifiType, m_instId); - return WIFI_OPT_SUCCESS; -#else if (m_instId == 0) { WifiSettings::GetInstance().SetWifiToggledState(true); } - errCode = WifiManager::GetInstance().WifiToggled(1, m_instId); - return errCode; -#endif + + return WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(1, m_instId); } ErrCode WifiDeviceServiceImpl::DisableWifi() @@ -237,64 +133,12 @@ ErrCode WifiDeviceServiceImpl::DisableWifi() WIFI_LOGE("DisableWifi:VerifyWifiConnectionPermission PERMISSION_DENIED!"); return WIFI_OPT_PERMISSION_DENIED; } -#ifdef OHOS_ARCH_LITE - WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiMidState(m_instId); - if (curState != WifiOprMidState::RUNNING) { - WIFI_LOGI("current wifi state is %{public}d", static_cast(curState)); - if (curState == WifiOprMidState::OPENING) { /* when current wifi is opening, return */ - return WIFI_OPT_CLOSE_FAIL_WHEN_OPENING; - } else { - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - } - -#ifdef FEATURE_P2P_SUPPORT - sptr p2pService = WifiP2pServiceImpl::GetInstance(); - if (p2pService != nullptr) { - ErrCode errCode = p2pService->DisableP2p(); - if (errCode != WIFI_OPT_SUCCESS && errCode != WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED) { - WIFI_LOGE("Disable P2p failed!"); - return WIFI_OPT_FAILED; - } - } -#endif - if (!WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::CLOSING, m_instId)) { - WIFI_LOGI("set wifi mid state opening failed! may be other activity has been operated"); - return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; - } - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(m_instId); - if (pService == nullptr) { - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSED, m_instId); - WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, m_instId); - return WIFI_OPT_SUCCESS; - } - ErrCode ret = pService->DisableWifi(); - if (ret != WIFI_OPT_SUCCESS) { - WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSING, WifiOprMidState::RUNNING, m_instId); - } else { - WifiConfigCenter::GetInstance().SetStaLastRunState(false, m_instId); - WifiManager::GetInstance().GetAirplaneModeByDatashare(); - if (WifiConfigCenter::GetInstance().GetOperatorWifiType(m_instId) == - static_cast(OperatorWifiType::USER_OPEN_WIFI_IN_AIRPLANEMODE)) { - if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::USER_CLOSE_WIFI_IN_AIRPLANEMODE), m_instId); - WIFI_LOGI("DisableWifi, current airplane mode is opened, user close wifi!"); - } - } else { - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::USER_CLOSE_WIFI_IN_NO_AIRPLANEMODE), m_instId); - } - } - return ret; -#else if (m_instId == 0) { WifiSettings::GetInstance().SetWifiToggledState(false); } - ErrCode errCode = WifiManager::GetInstance().WifiToggled(0, m_instId); - return errCode; -#endif + + return WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(0, m_instId); } ErrCode WifiDeviceServiceImpl::InitWifiProtect(const WifiProtectType &protectType, const std::string &protectName) @@ -1363,16 +1207,16 @@ ErrCode WifiDeviceServiceImpl::CheckCanEnableWifi(void) return WIFI_OPT_PERMISSION_DENIED; } #ifndef OHOS_ARCH_LITE - if (WifiManager::GetInstance().IsMdmForbidden()) { + if (WifiManager::GetInstance().GetWifiEventSubscriberManager()->IsMdmForbidden()) { WIFI_LOGE("EnableWifi: Mdm forbidden PERMISSION_DENIED!"); return WIFI_OPT_ENTERPRISE_DENIED; } -#endif /** * when airplane mode opened, if the config "can_open_sta_when_airplanemode" * opened, then can open sta; other, return forbid. */ - WifiManager::GetInstance().GetAirplaneModeByDatashare(); + WifiManager::GetInstance().GetWifiEventSubscriberManager()->GetAirplaneModeByDatashare(); +#endif if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN && !WifiConfigCenter::GetInstance().GetCanOpenStaWhenAirplaneMode(m_instId)) { WIFI_LOGI("current airplane mode and can not use sta, open failed!"); @@ -1589,22 +1433,17 @@ ErrCode WifiDeviceServiceImpl::FactoryReset() } WIFI_LOGE("WifiDeviceServiceImpl FactoryReset sta,p2p,hotspot!"); -#ifndef OHOS_ARCH_LITE - ErrCode errCode; if (IsStaServiceRunning()) { WIFI_LOGI("WifiDeviceServiceImpl FactoryReset IsStaServiceRunning, m_instId:%{public}d", m_instId); if (m_instId == 0) { WifiSettings::GetInstance().SetWifiToggledState(false); } - errCode = WifiManager::GetInstance().WifiToggled(0, m_instId); - WIFI_LOGI("WifiDeviceServiceImpl FactoryReset WifiToggled, errCode:%{public}d", errCode); + WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(0, m_instId); } WifiOprMidState curState = WifiConfigCenter::GetInstance().GetApMidState(m_instId); if (curState == WifiOprMidState::RUNNING) { - errCode = WifiManager::GetInstance().SoftapToggled(0, m_instId); - WIFI_LOGI("WifiDeviceServiceImpl FactoryReset SoftapToggled, errCode:%{public}d", errCode); + WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(0, m_instId); } -#endif // wifi device WifiSettings::GetInstance().ClearDeviceConfig(); WifiSettings::GetInstance().SyncDeviceConfig(); @@ -1636,68 +1475,6 @@ void WifiDeviceServiceImpl::StartWatchdog(void) WATCHDOG_INTERVAL_MS, WATCHDOG_DELAY_MS); } -void WifiDeviceServiceImpl::RegisterAppRemoved() -{ - std::unique_lock lock(appEventMutex); - if (eventSubscriber_) { - return; - } - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); - eventSubscriber_ = std::make_shared(subscriberInfo); - if (!EventFwk::CommonEventManager::SubscribeCommonEvent(eventSubscriber_)) { - WIFI_LOGE("AppEvent SubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("AppEvent SubscribeCommonEvent() OK"); - WifiTimer::GetInstance()->UnRegister(appEventTimerId); - } -} - -void WifiDeviceServiceImpl::UnRegisterAppRemoved() -{ - std::unique_lock lock(appEventMutex); - if (!eventSubscriber_) { - return; - } - if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(eventSubscriber_)) { - WIFI_LOGE("AppEvent UnSubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("AppEvent UnSubscribeCommonEvent() OK"); - } -} - -void WifiDeviceServiceImpl::RegisterThermalLevel() -{ - std::unique_lock lock(thermalEventMutex); - if (thermalLevelSubscriber_) { - return; - } - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED); - EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); - thermalLevelSubscriber_ = std::make_shared(subscriberInfo); - if (!EventFwk::CommonEventManager::SubscribeCommonEvent(thermalLevelSubscriber_)) { - WIFI_LOGE("THERMAL_LEVEL_CHANGED SubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("THERMAL_LEVEL_CHANGED SubscribeCommonEvent() OK"); - WifiTimer::GetInstance()->UnRegister(thermalTimerId); - } -} - -void WifiDeviceServiceImpl::UnRegisterThermalLevel() -{ - std::unique_lock lock(thermalEventMutex); - if (!thermalLevelSubscriber_) { - return; - } - if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(thermalLevelSubscriber_)) { - WIFI_LOGE("THERMAL_LEVEL_CHANGED UnSubscribeCommonEvent() failed"); - } else { - WIFI_LOGI("THERMAL_LEVEL_CHANGED UnSubscribeCommonEvent() OK"); - } -} - ErrCode WifiDeviceServiceImpl::SetAppFrozen(int uid, bool isFrozen) { if (!WifiAuthCenter::IsNativeProcess()) { @@ -1717,73 +1494,6 @@ ErrCode WifiDeviceServiceImpl::ResetAllFrozenApp() WifiInternalEventDispatcher::GetInstance().ResetAllFrozenApp(); return WIFI_OPT_SUCCESS; } - -AppEventSubscriber::AppEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) - : CommonEventSubscriber(subscriberInfo) -{ - WIFI_LOGI("AppEventSubscriber enter"); -} - -AppEventSubscriber::~AppEventSubscriber() -{ - WIFI_LOGI("~AppEventSubscriber enter"); -} - -void AppEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) -{ - std::string action = data.GetWant().GetAction(); - WIFI_LOGI("AppEventSubscriber::OnReceiveEvent : %{public}s.", action.c_str()); - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { - auto wantTemp = data.GetWant(); - auto uid = wantTemp.GetIntParam(AppExecFwk::Constants::UID, -1); - if (uid == -1) { - WIFI_LOGE("%{public}s getPackage uid is illegal.", __func__); - return; - } - WIFI_LOGI("Package removed of uid %{public}d.", uid); - for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i); - if (pService == nullptr) { - WIFI_LOGI("Sta service not opend!"); - std::vector tempConfigs; - WifiSettings::GetInstance().GetAllCandidateConfig(uid, tempConfigs); - for (const auto &config : tempConfigs) { - if (WifiSettings::GetInstance().RemoveDevice(config.networkId) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("RemoveAllCandidateConfig-RemoveDevice() failed!"); - } - } - WifiSettings::GetInstance().SyncDeviceConfig(); - return; - } - if (pService->RemoveAllCandidateConfig(uid) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("RemoveAllCandidateConfig failed"); - } - } - } -} - -ThermalLevelSubscriber::ThermalLevelSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) - : CommonEventSubscriber(subscriberInfo) -{ - WIFI_LOGI("ThermalLevelSubscriber enter"); -} - -ThermalLevelSubscriber::~ThermalLevelSubscriber() -{ - WIFI_LOGI("~ThermalLevelSubscriber enter"); -} - -void ThermalLevelSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) -{ - std::string action = data.GetWant().GetAction(); - WIFI_LOGI("ThermalLevelSubscriber::OnReceiveEvent: %{public}s.", action.c_str()); - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) { - static const std::string THERMAL_EVENT_ID = "0"; - int level = data.GetWant().GetIntParam(THERMAL_EVENT_ID, 0); - WifiSettings::GetInstance().SetThermalLevel(level); - WIFI_LOGI("ThermalLevelSubscriber SetThermalLevel: %{public}d.", level); - } -} #endif } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.h similarity index 83% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.h index a1a68fd8c161d786029a97c64c0e480918b15297..84d1c393aa7b2c1f9824e640ca9e3070b4758ba3 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_service_impl.h @@ -26,30 +26,13 @@ #include "system_ability.h" #include "wifi_device_stub.h" #include "iremote_object.h" -#include "common_event_manager.h" -#include "common_event_support.h" #include "bundle_constants.h" -#include "timer.h" #endif namespace OHOS { namespace Wifi { #ifdef OHOS_ARCH_LITE enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; -#else -class AppEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { -public: - explicit AppEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); - virtual ~AppEventSubscriber(); - virtual void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; -}; - -class ThermalLevelSubscriber : public OHOS::EventFwk::CommonEventSubscriber { -public: - explicit ThermalLevelSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); - virtual ~ThermalLevelSubscriber(); - void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; -}; #endif class WifiDeviceServiceImpl : public WifiDeviceStub { @@ -173,10 +156,6 @@ private: ErrCode CheckRemoveCandidateConfig(void); void SetWifiConnectedMode(void); #ifndef OHOS_ARCH_LITE - void RegisterAppRemoved(); - void UnRegisterAppRemoved(); - void RegisterThermalLevel(); - void UnRegisterThermalLevel(); bool InitWifiBrokerProcessInfo(const WifiDeviceConfig &config); #endif @@ -193,13 +172,6 @@ private: static std::mutex g_instanceLock; static std::shared_ptr g_instance; ServiceRunningState mState; -#else - std::shared_ptr eventSubscriber_ = nullptr; - std::shared_ptr thermalLevelSubscriber_ = nullptr; - uint32_t appEventTimerId{0}; - uint32_t thermalTimerId{0}; - std::mutex appEventMutex; - std::mutex thermalEventMutex; #endif }; } // namespace Wifi diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub_lite.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub_lite.cpp similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub_lite.cpp rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub_lite.cpp diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub_lite.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub_lite.h similarity index 100% rename from wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_stub_lite.h rename to wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa/wifi_device_stub_lite.h diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18a24edd9c270fe0b28e3627ca8d19de9e04a2ed --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.cpp @@ -0,0 +1,961 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ARCH_LITE +#include "wifi_event_subscriber_manager.h" +#include "wifi_manager.h" +#include "wifi_service_manager.h" +#include "wifi_config_center.h" +#include "wifi_logger.h" +#include "wifi_protect_manager.h" +#include "wifi_global_func.h" +#include "wifi_system_timer.h" +#include "common_event_support.h" +#include "wifi_datashare_utils.h" +#include "wifi_location_mode_observer.h" +#ifdef HAS_POWERMGR_PART +#include "wifi_power_state_listener.h" +#include "suspend/sleep_priority.h" +#endif +#ifdef HAS_MOVEMENT_PART +#include "wifi_msdp_state_listener.h" +#endif + +DEFINE_WIFILOG_LABEL("WifiEventSubscriberManager"); + +namespace OHOS { +namespace Wifi { +constexpr uint32_t TIMEOUT_EVENT_SUBSCRIBER = 3000; +constexpr uint32_t TIMEOUT_CHECK_LAST_STA_STATE_EVENT = 10 * 1000; +constexpr uint32_t PROP_LEN = 26; +constexpr uint32_t PROP_SUBCHIPTYPE_LEN = 10; +constexpr uint32_t SUPPORT_COEXCHIP_LEN = 7; +constexpr uint32_t PROP_TRUE_LEN = 4; +constexpr uint32_t PROP_FALSE_LEN = 5; +const std::string PROP_TRUE = "true"; +const std::string PROP_FALSE = "false"; +const std::string SUBCHIP_WIFI_PROP = "ohos.boot.odm.conn.schiptype"; +const std::string MDM_WIFI_PROP = "persist.edm.wifi_enable"; +const std::string SUPPORT_COEXCHIP = "bisheng"; +const std::string COEX_IFACENAME = "wlan1"; + +bool WifiEventSubscriberManager::mIsMdmForbidden = false; +static sptr locationModeObserver_ = nullptr; +static sptr deviceProvisionObserver_ = nullptr; +static sptr settingsMigrateObserver_ = nullptr; +#ifdef HAS_POWERMGR_PART +static sptr powerStateListener_ = nullptr; +#endif +#ifdef HAS_MOVEMENT_PART +static sptr deviceMovementCallback_ = nullptr; +#endif + +WifiEventSubscriberManager::WifiEventSubscriberManager() +{ + WIFI_LOGI("create WifiEventSubscriberManager"); + RegisterDeviceProvisionEvent(); + if (!isScreenEventSubscribered && screenTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::RegisterScreenEvent, this); + WifiTimer::GetInstance()->Register(timeoutCallback, screenTimerId, TIMEOUT_EVENT_SUBSCRIBER, false); + WIFI_LOGI("RegisterScreenEvent success! screenTimerId:%{public}u", screenTimerId); + } + if (!isAirplaneModeEventSubscribered && airplaneModeTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::RegisterAirplaneModeEvent, this); + WifiTimer::GetInstance()->Register(timeoutCallback, airplaneModeTimerId, TIMEOUT_EVENT_SUBSCRIBER, false); + WIFI_LOGI("RegisterAirplaneModeEvent success! airplaneModeTimerId:%{public}u", airplaneModeTimerId); + } + if (!islocationModeObservered && locationTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::RegisterLocationEvent, this); + WifiTimer::GetInstance()->Register(timeoutCallback, locationTimerId, TIMEOUT_EVENT_SUBSCRIBER, false); + WIFI_LOGI("RegisterLocationEvent success! locationTimerId:%{public}u", locationTimerId); + } + if (batterySubscriber_ == nullptr && batteryTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::RegisterBatteryEvent, this); + WifiTimer::GetInstance()->Register(timeoutCallback, batteryTimerId, TIMEOUT_EVENT_SUBSCRIBER, false); + WIFI_LOGI("RegisterBatteryEvent success! locationTimerId:%{public}u", batteryTimerId); + } + if (eventSubscriber_ == nullptr && appEventTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::RegisterAppRemoved, this); + WifiTimer::GetInstance()->Register(timeoutCallback, appEventTimerId, TIMEOUT_EVENT_SUBSCRIBER, false); + } + + if (thermalLevelSubscriber_ == nullptr && thermalTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::RegisterThermalLevel, this); + WifiTimer::GetInstance()->Register(timeoutCallback, thermalTimerId, TIMEOUT_EVENT_SUBSCRIBER, false); + } + if (!std::filesystem::exists(WIFI_CONFIG_FILE_PATH) && migrateTimerId == 0) { + RegisterSettingsMigrateEvent(); + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiEventSubscriberManager::CheckAndStartStaByDatashare, this); + WifiTimer::GetInstance()->Register(timeoutCallback, migrateTimerId, TIMEOUT_CHECK_LAST_STA_STATE_EVENT); + WIFI_LOGI("CheckAndStartStaByDatashare register success! migrateTimerId:%{public}u", migrateTimerId); + } +#ifdef HAS_POWERMGR_PART + if (!isPowerStateListenerSubscribered) { + RegisterPowerStateListener(); + } +#endif + InitSubscribeListener(); + GetMdmProp(); + GetChipProp(); + RegisterMdmPropListener(); +} + +WifiEventSubscriberManager::~WifiEventSubscriberManager() +{ + WIFI_LOGI("~WifiEventSubscriberManager"); + if (isScreenEventSubscribered) { + UnRegisterScreenEvent(); + } + if (isAirplaneModeEventSubscribered) { + UnRegisterAirplaneModeEvent(); + } + if (islocationModeObservered) { + UnRegisterLocationEvent(); + } + if (deviceProvisionObserver_) { + UnRegisterDeviceProvisionEvent(); + } + if (batterySubscriber_) { + UnRegisterBatteryEvent(); + } + if (eventSubscriber_) { + UnRegisterAppRemoved(); + } + if (thermalLevelSubscriber_) { + UnRegisterThermalLevel(); + } +#ifdef HAS_POWERMGR_PART + if (isPowerStateListenerSubscribered) { + UnRegisterPowerStateListener(); + } +#endif +} + +void WifiEventSubscriberManager::OnSystemAbilityChanged(int systemAbilityId, bool add) +{ + switch (systemAbilityId) { + case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID: { + if (!add) { + break; + } + for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { + IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i); + if (pService != nullptr) { + pService->OnSystemAbilityChanged(systemAbilityId, add); + } + } + break; + } + case COMMON_EVENT_SERVICE_ID: { + if (add) { + RegisterScreenEvent(); + RegisterAirplaneModeEvent(); + RegisterLocationEvent(); + } else { + UnRegisterScreenEvent(); + UnRegisterAirplaneModeEvent(); + UnRegisterLocationEvent(); + } + + WIFI_LOGI("OnSystemAbilityChanged, id[%{public}d], mode=[%{public}d]!", + systemAbilityId, add); + for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { + IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(i); + if (pService != nullptr) { + pService->OnSystemAbilityChanged(systemAbilityId, add); + } + } + break; + } +#ifdef HAS_POWERMGR_PART + case POWER_MANAGER_SERVICE_ID: { + if (add) { + RegisterPowerStateListener(); + } else { + UnRegisterPowerStateListener(); + } + + WIFI_LOGI("OnSystemAbilityChanged, id[%{public}d], mode=[%{public}d]!", + systemAbilityId, add); + + break; + } +#endif +#ifdef HAS_MOVEMENT_PART + case MSDP_MOVEMENT_SERVICE_ID: { + if (add) { + RegisterMovementCallBack(); + } else { + UnRegisterMovementCallBack(); + } + break; + } +#endif + default: + break; + } +} + +void WifiEventSubscriberManager::GetAirplaneModeByDatashare() +{ + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("GetAirplaneModeByDatashare, datashareHelper is nullptr!"); + return; + } + + std::string airplaneMode; + Uri uri(SETTINGS_DATASHARE_URL_AIRPLANE_MODE); + int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_AIRPLANE_MODE, airplaneMode); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("GetAirplaneModeByDatashare, Query airplaneMode fail!"); + return; + } + + WIFI_LOGD("GetAirplaneModeByDatashare, airplaneMode:%{public}s", airplaneMode.c_str()); + if (airplaneMode.compare("1") == 0) { + WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_OPEN); + } + return; +} + +void WifiEventSubscriberManager::GetDeviceProvisionByDatashare() +{ + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("GetDeviceProvisionByDatashare, datashareHelper is nullptr!"); + return; + } + + std::string provision; + Uri uri(SETTINGS_DATASHARE_URI_DEVICE_PROVISIONED); + int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_DEVICE_PROVISIONED, provision); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("GetDeviceProvisionByDatashare, Query provision fail!"); + return; + } + + WIFI_LOGI("GetDeviceProvisionByDatashare, provision:%{public}s", provision.c_str()); + if (provision.compare("1") == 0) { + WifiConfigCenter::GetInstance().SetDeviceProvisionState(MODE_STATE_CLOSE); + return; + } + WifiConfigCenter::GetInstance().SetDeviceProvisionState(MODE_STATE_OPEN); + return; +} + +bool WifiEventSubscriberManager::GetLocationModeByDatashare() +{ + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("GetLocationModeByDatashare, datashareHelper is nullptr!"); + return false; + } + + std::string locationMode; + Uri uri(SETTINGS_DATASHARE_URI_LOCATION_MODE); + int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_LOCATION_MODE, locationMode); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("GetLocationModeByDatashare, Query locationMode fail!"); + return false; + } + + WIFI_LOGD("GetLocationModeByDatashare, locationMode:%{public}s", locationMode.c_str()); + return (locationMode.compare("1") == 0); +} + +void WifiEventSubscriberManager::DealLocationModeChangeEvent() +{ + if (GetLocationModeByDatashare()) { + WIFI_LOGI("DealLocationModeChangeEvent open"); + WifiManager::GetInstance().GetWifiTogglerManager()->ScanOnlyToggled(1); + } else { + WIFI_LOGI("DealLocationModeChangeEvent close"); + WifiManager::GetInstance().GetWifiTogglerManager()->ScanOnlyToggled(0); + } +} + +void WifiEventSubscriberManager::CheckAndStartStaByDatashare() +{ + constexpr int openWifi = 1; + constexpr int openWifiInAirplanemode = 2; + constexpr int closeWifiByAirplanemodeOpen = 3; + + int lastStaState = GetLastStaStateByDatashare(); + if (lastStaState == openWifi) { + WifiSettings::GetInstance().SetWifiToggledState(true); + WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(1, 0); + } else if (lastStaState == openWifiInAirplanemode) { + WifiConfigCenter::GetInstance().SetOpenWifiWhenAirplaneMode(true); + WifiSettings::GetInstance().SetWifiToggledState(true); + WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(1, 0); + } else if (lastStaState == closeWifiByAirplanemodeOpen) { + WifiSettings::GetInstance().SetWifiToggledState(true); + } + + UnRegisterSettingsMigrateEvent(); + std::unique_lock lock(settingsMigrateMutex); + WifiTimer::GetInstance()->UnRegister(migrateTimerId); + migrateTimerId = 0; +} + +bool WifiEventSubscriberManager::IsMdmForbidden() +{ + return mIsMdmForbidden; +} + +void WifiEventSubscriberManager::InitSubscribeListener() +{ + SubscribeSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID); + SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID); +#ifdef HAS_POWERMGR_PART + SubscribeSystemAbility(POWER_MANAGER_SERVICE_ID); +#endif +#ifdef HAS_MOVEMENT_PART + SubscribeSystemAbility(MSDP_MOVEMENT_SERVICE_ID); +#endif +} + +int WifiEventSubscriberManager::GetLastStaStateByDatashare() +{ + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("GetLastStaStateByDatashare, datashareHelper is nullptr!"); + return 0; + } + + std::string lastStaState; + Uri uri(SETTINGS_DATASHARE_URI_WIFI_ON); + int ret = datashareHelper->Query(uri, SETTINGS_DATASHARE_KEY_WIFI_ON, lastStaState); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("GetLastStaStateByDatashare, Query lastStaState fail!"); + return 0; + } + + WIFI_LOGI("GetLastStaStateByDatashare, lastStaState:%{public}s", lastStaState.c_str()); + int lastStaStateType = ConvertStringToInt(lastStaState); + return lastStaStateType; +} + +void WifiEventSubscriberManager::RegisterScreenEvent() +{ + std::unique_lock lock(screenEventMutex); + if (isScreenEventSubscribered) { + return; + } + OHOS::EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON); + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + screenEventSubscriber_ = std::make_shared(subscriberInfo); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(screenEventSubscriber_)) { + WIFI_LOGE("ScreenEvent SubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("ScreenEvent SubscribeCommonEvent() OK"); + isScreenEventSubscribered = true; + WifiTimer::GetInstance()->UnRegister(screenTimerId); + } +} + +void WifiEventSubscriberManager::UnRegisterScreenEvent() +{ + std::unique_lock lock(screenEventMutex); + if (!isScreenEventSubscribered) { + return; + } + if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(screenEventSubscriber_)) { + WIFI_LOGE("ScreenEvent UnSubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("ScreenEvent UnSubscribeCommonEvent() OK"); + isScreenEventSubscribered = false; + } +} + +void WifiEventSubscriberManager::RegisterAirplaneModeEvent() +{ + std::unique_lock lock(airplaneModeEventMutex); + if (isAirplaneModeEventSubscribered) { + return; + } + OHOS::EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + subscriberInfo.SetPriority(1); + airplaneModeEventSubscriber_ = std::make_shared(subscriberInfo); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(airplaneModeEventSubscriber_)) { + WIFI_LOGE("AirplaneModeEvent SubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("AirplaneModeEvent SubscribeCommonEvent() OK"); + isAirplaneModeEventSubscribered = true; + WifiTimer::GetInstance()->UnRegister(airplaneModeTimerId); + } +} + +void WifiEventSubscriberManager::UnRegisterAirplaneModeEvent() +{ + std::unique_lock lock(airplaneModeEventMutex); + if (!isAirplaneModeEventSubscribered) { + return; + } + if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(airplaneModeEventSubscriber_)) { + WIFI_LOGE("AirplaneModeEvent UnSubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("AirplaneModeEvent UnSubscribeCommonEvent() OK"); + isAirplaneModeEventSubscribered = false; + } +} + +void WifiEventSubscriberManager::RegisterLocationEvent() +{ + std::unique_lock lock(locationEventMutex); + if (islocationModeObservered) { + return; + } + + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("LocationEvent datashareHelper is nullptr"); + return; + } + locationModeObserver_ = sptr(new (std::nothrow)WifiLocationModeObserver()); + Uri uri(SETTINGS_DATASHARE_URI_LOCATION_MODE); + datashareHelper->RegisterObserver(uri, locationModeObserver_); + islocationModeObservered = true; +} + +void WifiEventSubscriberManager::UnRegisterLocationEvent() +{ + std::unique_lock lock(locationEventMutex); + if (!islocationModeObservered) { + WIFI_LOGE("UnRegisterLocationEvent islocationModeObservered is false"); + return; + } + + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("UnRegisterLocationEvent datashareHelper is nullptr"); + return; + } + Uri uri(SETTINGS_DATASHARE_URI_LOCATION_MODE); + datashareHelper->UnRegisterObserver(uri, locationModeObserver_); + islocationModeObservered = false; +} + +void WifiEventSubscriberManager::RegisterDeviceProvisionEvent() +{ + if (deviceProvisionObserver_) { + return; + } + + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("RegisterDeviceProvisionEvent datashareHelper is nullptr"); + return; + } + WIFI_LOGI("RegisterDeviceProvisionEvent"); + deviceProvisionObserver_ = sptr(new (std::nothrow)WifiDeviceProvisionObserver()); + Uri uri(SETTINGS_DATASHARE_URI_DEVICE_PROVISIONED); + datashareHelper->RegisterObserver(uri, deviceProvisionObserver_); +} + +void WifiEventSubscriberManager::UnRegisterDeviceProvisionEvent() +{ + if (deviceProvisionObserver_ == nullptr) { + WIFI_LOGE("UnRegisterLocationEvent deviceProvisionObserver_ is nullptr"); + return; + } + + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("UnRegisterLocationEvent datashareHelper is nullptr"); + return; + } + WIFI_LOGI("UnRegisterDeviceProvisionEvent"); + Uri uri(SETTINGS_DATASHARE_URI_DEVICE_PROVISIONED); + datashareHelper->UnRegisterObserver(uri, deviceProvisionObserver_); +} + +void WifiEventSubscriberManager::RegisterBatteryEvent() +{ + std::unique_lock lock(batteryEventMutex); + if (batterySubscriber_) { + return; + } + OHOS::EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED); + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + batterySubscriber_ = std::make_shared(subscriberInfo); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(batterySubscriber_)) { + WIFI_LOGE("BatteryEvent SubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("BatteryEvent SubscribeCommonEvent() OK"); + WifiTimer::GetInstance()->UnRegister(batteryTimerId); + } +} + +void WifiEventSubscriberManager::UnRegisterBatteryEvent() +{ + std::unique_lock lock(batteryEventMutex); + if (!batterySubscriber_) { + return; + } + if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(batterySubscriber_)) { + WIFI_LOGE("BatteryEvent UnSubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("BatteryEvent UnSubscribeCommonEvent() OK"); + } +} + +void WifiEventSubscriberManager::RegisterSettingsMigrateEvent() +{ + std::unique_lock lock(settingsMigrateMutex); + if (settingsMigrateObserver_) { + return; + } + + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("RegisterSettingsMigrateEvent datashareHelper is nullptr"); + return; + } + settingsMigrateObserver_ = sptr(new (std::nothrow)SettingsMigrateObserver()); + Uri uri(SETTINGS_DATASHARE_URI_SETTINGS_MIGRATE); + datashareHelper->RegisterObserver(uri, settingsMigrateObserver_); +} + +void WifiEventSubscriberManager::UnRegisterSettingsMigrateEvent() +{ + std::unique_lock lock(settingsMigrateMutex); + if (settingsMigrateObserver_ == nullptr) { + WIFI_LOGE("UnRegisterSettingsMigrateEvent settingsMigrateObserver_ is nullptr"); + return; + } + + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + WIFI_LOGE("UnRegisterSettingsMigrateEvent datashareHelper is nullptr"); + return; + } + Uri uri(SETTINGS_DATASHARE_URI_SETTINGS_MIGRATE); + datashareHelper->UnRegisterObserver(uri, settingsMigrateObserver_); + settingsMigrateObserver_ = nullptr; +} + +void WifiEventSubscriberManager::GetMdmProp() +{ + char preValue[PROP_FALSE_LEN + 1] = {0}; + + int errorCode = GetParamValue(MDM_WIFI_PROP.c_str(), 0, preValue, PROP_FALSE_LEN + 1); + if (errorCode > 0) { + if (strncmp(preValue, PROP_TRUE.c_str(), PROP_TRUE_LEN) == 0) { + mIsMdmForbidden = true; + } + } +} + +void WifiEventSubscriberManager::GetChipProp() +{ + char preValue[PROP_SUBCHIPTYPE_LEN] = {0}; + int errorCode = GetParamValue(SUBCHIP_WIFI_PROP.c_str(), 0, preValue, PROP_SUBCHIPTYPE_LEN); + if (errorCode > 0) { + if (strncmp(preValue, SUPPORT_COEXCHIP.c_str(), SUPPORT_COEXCHIP_LEN) == 0) { + WifiSettings::GetInstance().SetApIfaceName(COEX_IFACENAME); + WifiSettings::GetInstance().SetCoexSupport(true); + } + } +} + +void WifiEventSubscriberManager::RegisterMdmPropListener() +{ + int ret = WatchParamValue(MDM_WIFI_PROP.c_str(), MdmPropChangeEvt, nullptr); + if (ret != 0) { + WIFI_LOGI("RegisterMdmPropListener failed"); + } +} + +void WifiEventSubscriberManager::MdmPropChangeEvt(const char *key, const char *value, void *context) +{ + if (strncmp(key, MDM_WIFI_PROP.c_str(), PROP_LEN) != 0) { + WIFI_LOGI("not mdm prop change"); + return; + } + WIFI_LOGI("mdm prop change"); + if (strncmp(value, PROP_TRUE.c_str(), PROP_TRUE_LEN) == 0) { + mIsMdmForbidden = true; + return; + } + if (strncmp(value, PROP_FALSE.c_str(), PROP_FALSE_LEN) == 0) { + mIsMdmForbidden = false; + } +} + +#ifdef HAS_POWERMGR_PART +void WifiEventSubscriberManager::RegisterPowerStateListener() +{ + WIFI_LOGD("Enter RegisterPowerStateListener"); + std::unique_lock lock(powerStateEventMutex); + if (isPowerStateListenerSubscribered) { + WIFI_LOGI("RegisterPowerStateListener, powerStateListener_ already exist!"); + return; + } + + auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); + powerStateListener_ = new (std::nothrow) WifiPowerStateListener(); + if (!powerStateListener_) { + WIFI_LOGE("RegisterPowerStateListener, create power state listener failed"); + return; + } + + bool ret = powerManagerClient.RegisterSyncSleepCallback(powerStateListener_, SleepPriority::HIGH); + if (!ret) { + WIFI_LOGE("RegisterPowerStateListener, register power state callback failed"); + } else { + WIFI_LOGI("RegisterPowerStateListener OK!"); + isPowerStateListenerSubscribered = true; + } +} + +void WifiEventSubscriberManager::UnRegisterPowerStateListener() +{ + WIFI_LOGD("Enter UnRegisterPowerStateListener"); + std::unique_lock lock(powerStateEventMutex); + if (!isPowerStateListenerSubscribered) { + WIFI_LOGE("UnRegisterPowerStateListener, powerStateListener_ is nullptr"); + return; + } + + auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); + bool ret = powerManagerClient.UnRegisterSyncSleepCallback(powerStateListener_); + if (!ret) { + WIFI_LOGE("UnRegisterPowerStateListener, unregister power state callback failed"); + } else { + isPowerStateListenerSubscribered = false; + WIFI_LOGI("UnRegisterPowerStateListener OK!"); + } +} +#endif + +void WifiEventSubscriberManager::RegisterAppRemoved() +{ + std::unique_lock lock(appEventMutex); + if (eventSubscriber_) { + return; + } + OHOS::EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + eventSubscriber_ = std::make_shared(subscriberInfo); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(eventSubscriber_)) { + WIFI_LOGE("AppEvent SubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("AppEvent SubscribeCommonEvent() OK"); + WifiTimer::GetInstance()->UnRegister(appEventTimerId); + } +} + +void WifiEventSubscriberManager::UnRegisterAppRemoved() +{ + std::unique_lock lock(appEventMutex); + if (!eventSubscriber_) { + return; + } + if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(eventSubscriber_)) { + WIFI_LOGE("AppEvent UnSubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("AppEvent UnSubscribeCommonEvent() OK"); + } +} + +void WifiEventSubscriberManager::RegisterThermalLevel() +{ + std::unique_lock lock(thermalEventMutex); + if (thermalLevelSubscriber_) { + return; + } + OHOS::EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + thermalLevelSubscriber_ = std::make_shared(subscriberInfo); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(thermalLevelSubscriber_)) { + WIFI_LOGE("THERMAL_LEVEL_CHANGED SubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("THERMAL_LEVEL_CHANGED SubscribeCommonEvent() OK"); + WifiTimer::GetInstance()->UnRegister(thermalTimerId); + } +} + +void WifiEventSubscriberManager::UnRegisterThermalLevel() +{ + std::unique_lock lock(thermalEventMutex); + if (!thermalLevelSubscriber_) { + return; + } + if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(thermalLevelSubscriber_)) { + WIFI_LOGE("THERMAL_LEVEL_CHANGED UnSubscribeCommonEvent() failed"); + } else { + WIFI_LOGI("THERMAL_LEVEL_CHANGED UnSubscribeCommonEvent() OK"); + } +} + +#ifdef HAS_MOVEMENT_PART +void WifiEventSubscriberManager::RegisterMovementCallBack() +{ + WIFI_LOGI("RegisterMovementCallBack"); + if (!deviceMovementCallback_) { + deviceMovementCallback_ = sptr(new DeviceMovementCallback()); + } + if (Msdp::MovementClient::GetInstance().SubscribeCallback( + Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_) != ERR_OK) { + WIFI_LOGE("Register a device movement observer failed!"); + } +} + +void WifiEventSubscriberManager::UnRegisterMovementCallBack() +{ + WIFI_LOGI("UnRegisterMovementCallBack"); + if (!deviceMovementCallback_) { + return; + } + Msdp::MovementClient::GetInstance().UnSubscribeCallback( + Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_); + deviceMovementCallback_ = nullptr; +} +#endif + +ScreenEventSubscriber::ScreenEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{ + WIFI_LOGI("ScreenEventSubscriber enter"); +} + +ScreenEventSubscriber::~ScreenEventSubscriber() +{ + WIFI_LOGI("~ScreenEventSubscriber enter"); +} + +void ScreenEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + WIFI_LOGI("ScreenEventSubscriber::OnReceiveEvent: %{public}s.", action.c_str()); + + int screenState = WifiSettings::GetInstance().GetScreenState(); + int screenStateNew = (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) + ? MODE_STATE_OPEN : MODE_STATE_CLOSE; + WifiSettings::GetInstance().SetScreenState(screenStateNew); + for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { + IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i); + if (pService == nullptr) { + WIFI_LOGE("sta service is NOT start!"); + return; + } + + IScanService *pScanService = WifiServiceManager::GetInstance().GetScanServiceInst(i); + if (pScanService == nullptr) { + WIFI_LOGE("scan service is NOT start!"); + return; + } +#ifndef OHOS_ARCH_LITE + bool isScreenOn = (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) ? true : false; + WifiProtectManager::GetInstance().HandleScreenStateChanged(isScreenOn); +#endif + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF && + screenState == MODE_STATE_OPEN) { + if (pScanService->OnScreenStateChanged(MODE_STATE_CLOSE) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("OnScreenStateChanged failed"); + } + /* Send suspend to wpa */ + if (pService->SetSuspendMode(true) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("SetSuspendMode failed"); + } + pService->OnScreenStateChanged(MODE_STATE_CLOSE); +#ifdef FEATURE_HPF_SUPPORT + WifiManager::GetInstance().InstallPacketFilterProgram(screenState, i); +#endif + return; + } + + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON && + screenState == MODE_STATE_CLOSE) { + if (pScanService->OnScreenStateChanged(MODE_STATE_OPEN) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("OnScreenStateChanged failed"); + } + /* Send resume to wpa */ + if (pService->SetSuspendMode(false) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("SetSuspendMode failed"); + } + pService->OnScreenStateChanged(MODE_STATE_OPEN); +#ifdef FEATURE_HPF_SUPPORT + WifiManager::GetInstance().InstallPacketFilterProgram(screenState, i); +#endif + return; + } + } +} + +AirplaneModeEventSubscriber::AirplaneModeEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{ + WIFI_LOGE("AirplaneModeEventSubscriber enter"); +} + +AirplaneModeEventSubscriber::~AirplaneModeEventSubscriber() +{ + WIFI_LOGE("~AirplaneModeEventSubscriber enter"); +} + +void AirplaneModeEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) +{ + const auto &action = eventData.GetWant().GetAction(); + const auto &data = eventData.GetData(); + const auto &code = eventData.GetCode(); + WIFI_LOGI("AirplaneModeEventSubscriber::OnReceiveEvent: %{public}s, %{public}s, %{public}d", action.c_str(), + data.c_str(), code); + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED) { + if (code == 1) { + /* open airplane mode */ + WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_OPEN); + WifiManager::GetInstance().GetWifiTogglerManager()->AirplaneToggled(1); + } else { + /* close airplane mode */ + WifiConfigCenter::GetInstance().SetAirplaneModeState(MODE_STATE_CLOSE); + WifiManager::GetInstance().GetWifiTogglerManager()->AirplaneToggled(0); + } + } +} + +BatteryEventSubscriber::BatteryEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{ + WIFI_LOGI("BatteryEventSubscriber enter"); +} + +BatteryEventSubscriber::~BatteryEventSubscriber() +{ + WIFI_LOGI("~BatteryEventSubscriber exit"); +} + +void BatteryEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + WIFI_LOGI("BatteryEventSubscriber::OnReceiveEvent: %{public}s.", action.c_str()); + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) { + WifiSettings::GetInstance().SetNoChargerPlugModeState(MODE_STATE_CLOSE); + } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) { + WifiSettings::GetInstance().SetNoChargerPlugModeState(MODE_STATE_OPEN); + } + for (int i = 0; i < AP_INSTANCE_MAX_NUM; ++i) { + IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(i); + if (pService == nullptr) { + WIFI_LOGE("ap service is NOT start!"); + return; + } + + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) { + WIFI_LOGE("usb connect do not stop hostapd!"); + WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine()->StopSoftapCloseTimer(); + return; + } + + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) { + WIFI_LOGE("usb disconnect stop hostapd!"); + std::vector result; + IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(0); + if (pService == nullptr) { + WIFI_LOGE("get hotspot service is null!"); + return; + } + ErrCode errCode = pService->GetStationList(result); + if (errCode != ErrCode::WIFI_OPT_SUCCESS) { + return; + } + if (result.empty()) { + WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine()->StartSoftapCloseTimer(); + } + return; + } + } +} + +AppEventSubscriber::AppEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{ + WIFI_LOGI("AppEventSubscriber enter"); +} + +AppEventSubscriber::~AppEventSubscriber() +{ + WIFI_LOGI("~AppEventSubscriber enter"); +} + +void AppEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + WIFI_LOGI("AppEventSubscriber::OnReceiveEvent : %{public}s.", action.c_str()); + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { + auto wantTemp = data.GetWant(); + auto uid = wantTemp.GetIntParam(AppExecFwk::Constants::UID, -1); + if (uid == -1) { + WIFI_LOGE("%{public}s getPackage uid is illegal.", __func__); + return; + } + WIFI_LOGI("Package removed of uid %{public}d.", uid); + for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) { + IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(i); + if (pService == nullptr) { + WIFI_LOGI("Sta service not opend!"); + std::vector tempConfigs; + WifiSettings::GetInstance().GetAllCandidateConfig(uid, tempConfigs); + for (const auto &config : tempConfigs) { + if (WifiSettings::GetInstance().RemoveDevice(config.networkId) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("RemoveAllCandidateConfig-RemoveDevice() failed!"); + } + } + WifiSettings::GetInstance().SyncDeviceConfig(); + return; + } + if (pService->RemoveAllCandidateConfig(uid) != WIFI_OPT_SUCCESS) { + WIFI_LOGE("RemoveAllCandidateConfig failed"); + } + } + } +} + +ThermalLevelSubscriber::ThermalLevelSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{ + WIFI_LOGI("ThermalLevelSubscriber enter"); +} + +ThermalLevelSubscriber::~ThermalLevelSubscriber() +{ + WIFI_LOGI("~ThermalLevelSubscriber enter"); +} + +void ThermalLevelSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + WIFI_LOGI("ThermalLevelSubscriber::OnReceiveEvent: %{public}s.", action.c_str()); + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) { + static const std::string THERMAL_EVENT_ID = "0"; + int level = data.GetWant().GetIntParam(THERMAL_EVENT_ID, 0); + WifiSettings::GetInstance().SetThermalLevel(level); + WIFI_LOGI("ThermalLevelSubscriber SetThermalLevel: %{public}d.", level); + } +} + +} // namespace Wifi +} // namespace OHOS +#endif \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..f72538cb14d04d3d1a449d1d2f42240784a2a57c --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.h @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WIFI_EVENT_SUBSCRIBER_MANAGER_H +#define OHOS_WIFI_EVENT_SUBSCRIBER_MANAGER_H + +#ifndef OHOS_ARCH_LITE +#include +#include +#include "wifi_errcode.h" +#include "wifi_internal_msg.h" +#include "wifi_system_ability_listerner.h" +#include "common_event_manager.h" + +namespace OHOS { +namespace Wifi { +class ScreenEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { +public: + explicit ScreenEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); + virtual ~ScreenEventSubscriber(); + void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; +}; + +class AirplaneModeEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { +public: + explicit AirplaneModeEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); + virtual ~AirplaneModeEventSubscriber(); + void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override; +}; + +class BatteryEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { +public: + explicit BatteryEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); + ~BatteryEventSubscriber(); + void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override; +}; + +class AppEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { +public: + explicit AppEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); + virtual ~AppEventSubscriber(); + virtual void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; +}; + +class ThermalLevelSubscriber : public OHOS::EventFwk::CommonEventSubscriber { +public: + explicit ThermalLevelSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo); + virtual ~ThermalLevelSubscriber(); + void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; +}; + + +class WifiEventSubscriberManager : public WifiSystemAbilityListener { +public: + WifiEventSubscriberManager(); + virtual ~WifiEventSubscriberManager(); + + void OnSystemAbilityChanged(int systemAbilityId, bool add) override; + void GetAirplaneModeByDatashare(); + void GetDeviceProvisionByDatashare(); + bool GetLocationModeByDatashare(); + void DealLocationModeChangeEvent(); + void CheckAndStartStaByDatashare(); + bool IsMdmForbidden(void); + +private: + void InitSubscribeListener(); + int GetLastStaStateByDatashare(); + void RegisterScreenEvent(); + void UnRegisterScreenEvent(); + void RegisterAirplaneModeEvent(); + void UnRegisterAirplaneModeEvent(); + void RegisterLocationEvent(); + void UnRegisterLocationEvent(); + void RegisterDeviceProvisionEvent(); + void UnRegisterDeviceProvisionEvent(); + void RegisterBatteryEvent(); + void UnRegisterBatteryEvent(); + void RegisterSettingsMigrateEvent(); + void UnRegisterSettingsMigrateEvent(); + void GetMdmProp(); + void GetChipProp(); + void RegisterMdmPropListener(); + static void MdmPropChangeEvt(const char *key, const char *value, void *context); + void RegisterPowerStateListener(); + void UnRegisterPowerStateListener(); + void RegisterAppRemoved(); + void UnRegisterAppRemoved(); + void RegisterThermalLevel(); + void UnRegisterThermalLevel(); +#ifdef HAS_MOVEMENT_PART + void RegisterMovementCallBack(); + void UnRegisterMovementCallBack(); +#endif + +private: + std::mutex screenEventMutex; + std::mutex airplaneModeEventMutex; + std::mutex locationEventMutex; + std::mutex batteryEventMutex; + std::mutex appEventMutex; + std::mutex thermalEventMutex; + std::mutex settingsMigrateMutex; +#ifdef HAS_POWERMGR_PART + std::mutex powerStateEventMutex; +#endif + uint32_t screenTimerId{0}; + bool isScreenEventSubscribered = false; + std::shared_ptr screenEventSubscriber_ = nullptr; + uint32_t airplaneModeTimerId{0}; + bool isAirplaneModeEventSubscribered = false; + std::shared_ptr airplaneModeEventSubscriber_ = nullptr; + uint32_t batteryTimerId{0}; + std::shared_ptr batterySubscriber_ = nullptr; + uint32_t locationTimerId{0}; + uint32_t migrateTimerId{0}; + static bool mIsMdmForbidden; + bool isPowerStateListenerSubscribered = false; + bool islocationModeObservered = false; + uint32_t appEventTimerId{0}; + std::shared_ptr eventSubscriber_ = nullptr; + uint32_t thermalTimerId{0}; + std::shared_ptr thermalLevelSubscriber_ = nullptr; +}; + +} // namespace Wifi +} // namespace OHOS +#endif +#endif // OHOS_WIFI_EVENT_SUBSCRIBER_MANAGER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b7e934e06cb3f0b13963668a8ae2ca69663e822 --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_AP_SUPPORT +#include "wifi_hotspot_manager.h" +#include "wifi_manager.h" +#include "wifi_service_manager.h" +#include "wifi_config_center.h" +#include "wifi_logger.h" +#include "wifi_common_event_helper.h" +#include "wifi_system_timer.h" +#ifdef OHOS_ARCH_LITE +#include "wifi_internal_event_dispatcher_lite.h" +#else +#include "wifi_internal_event_dispatcher.h" +#include "wifi_sa_manager.h" +#endif + +DEFINE_WIFILOG_LABEL("WifiHotspotManager"); + +namespace OHOS { +namespace Wifi { +WifiHotspotManager::WifiHotspotManager() +{ + WIFI_LOGI("create WifiHotspotManager"); + InitApCallback(); +} + +IApServiceCallbacks& WifiHotspotManager::GetApCallback() +{ + return mApCallback; +} + +#ifndef OHOS_ARCH_LITE +void WifiHotspotManager::UnloadHotspotSaTimerCallback() +{ + WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_HOTSPOT_ABILITY_ID); + StopUnloadApSaTimer(); +} + +void WifiHotspotManager::StopUnloadApSaTimer(void) +{ + WIFI_LOGI("StopUnloadApSaTimer! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); + std::unique_lock lock(unloadHotspotSaTimerMutex); + WifiTimer::GetInstance()->UnRegister(unloadHotspotSaTimerId); + unloadHotspotSaTimerId = 0; + return; +} + +void WifiHotspotManager::StartUnloadApSaTimer(void) +{ + WIFI_LOGI("StartUnloadApSaTimer! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); + std::unique_lock lock(unloadHotspotSaTimerMutex); + if (unloadHotspotSaTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiHotspotManager::UnloadHotspotSaTimerCallback, this); + WifiTimer::GetInstance()->Register(timeoutCallback, unloadHotspotSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("RegisterUnloadHotspotSaTimer success! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); + } + return; +} +#endif + +void WifiHotspotManager::CloseApService(int id) +{ + WIFI_LOGI("close %{public}d ap service", id); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, id); + WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSED, id); + WifiSettings::GetInstance().SetHotspotState(static_cast(ApState::AP_STATE_CLOSED), id); + auto &ins = WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine(); + ins->SendMessage(CMD_AP_STOPPED, id); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE; + cbMsg.msgData = static_cast(ApState::AP_STATE_CLOSED); + cbMsg.id = id; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); +#ifndef OHOS_ARCH_LITE + if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { + WIFI_LOGI("airplaneMode not close ap SA!"); + return; + } + StartUnloadApSaTimer(); +#endif + return; +} + +void WifiHotspotManager::InitApCallback(void) +{ + using namespace std::placeholders; + mApCallback.callbackModuleName = "WifiHotspotManager"; + mApCallback.OnApStateChangedEvent = std::bind(&WifiHotspotManager::DealApStateChanged, this, _1, _2); + mApCallback.OnHotspotStaJoinEvent = std::bind(&WifiHotspotManager::DealApGetStaJoin, this, _1, _2); + mApCallback.OnHotspotStaLeaveEvent = std::bind(&WifiHotspotManager::DealApGetStaLeave, this, _1, _2); + return; +} + +void WifiHotspotManager::DealApStateChanged(ApState state, int id) +{ + WIFI_LOGE("%{public}s, state: %{public}d!", __func__, state); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE; + cbMsg.msgData = static_cast(state); + cbMsg.id = id; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + if (state == ApState::AP_STATE_IDLE) { + WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::CLOSING, id); + WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::AP_SERVICE_CLOSE); + } + if (state == ApState::AP_STATE_STARTED) { + WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, id); + auto &ins = WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine(); + ins->SendMessage(CMD_AP_START, id); + } + + std::string msg = std::string("OnHotspotStateChanged") + std::string("id = ") + std::to_string(id); + WifiCommonEventHelper::PublishHotspotStateChangedEvent((int)state, msg); + return; +} + +void WifiHotspotManager::DealApGetStaJoin(const StationInfo &info, int id) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_JOIN; + cbMsg.staInfo = info; + cbMsg.id = id; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + std::string msg = std::string("ApStaJoined") + std::string("id = ") + std::to_string(id); + WifiCommonEventHelper::PublishApStaJoinEvent(0, msg); + WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine()->StopSoftapCloseTimer(); + return; +} + +void WifiHotspotManager::DealApGetStaLeave(const StationInfo &info, int id) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE; + cbMsg.staInfo = info; + cbMsg.id = id; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + std::string msg = std::string("ApStaLeaved") + std::string("id = ") + std::to_string(id); + WifiCommonEventHelper::PublishApStaLeaveEvent(0, msg); + std::vector result; + IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(id); + if (pService == nullptr) { + WIFI_LOGE("Instance %{public}d get hotspot service is null!", id); + return; + } + ErrCode errCode = pService->GetStationList(result); + if (errCode != ErrCode::WIFI_OPT_SUCCESS) { + return; + } + if (result.empty()) { + WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine()->StartSoftapCloseTimer(); + } + return; +} + +} // namespace Wifi +} // namespace OHOS +#endif \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..a084f28ba81b3e127cdaa25cb6d17fc25c5b02be --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WIFI_HOTSPOT_MANAGER_H +#define OHOS_WIFI_HOTSPOT_MANAGER_H + +#ifdef FEATURE_AP_SUPPORT +#include +#include +#include "wifi_errcode.h" +#include "wifi_internal_msg.h" +#include "i_ap_service_callbacks.h" + +namespace OHOS { +namespace Wifi { +class WifiHotspotManager { +public: + WifiHotspotManager(); + ~WifiHotspotManager() = default; + + IApServiceCallbacks& GetApCallback(void); + void StopUnloadApSaTimer(void); + void StartUnloadApSaTimer(void); + void CloseApService(int id = 0); + +private: + void InitApCallback(void); + void UnloadHotspotSaTimerCallback(); + void DealApStateChanged(ApState bState, int id = 0); + void DealApGetStaJoin(const StationInfo &info, int id = 0); + void DealApGetStaLeave(const StationInfo &info, int id = 0); + +private: + IApServiceCallbacks mApCallback; + uint32_t unloadHotspotSaTimerId{0}; + std::mutex unloadHotspotSaTimerMutex; +}; + +} // namespace Wifi +} // namespace OHOS +#endif +#endif // OHOS_WIFI_HOTSPOT_MANAGER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b93d331a4d136cf2c8ba290a5df6d87c081adb5d --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.cpp @@ -0,0 +1,333 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_P2P_SUPPORT +#include "wifi_p2p_manager.h" +#include "wifi_manager.h" +#include "wifi_service_manager.h" +#include "wifi_config_center.h" +#include "wifi_logger.h" +#include "wifi_common_event_helper.h" +#include "wifi_system_timer.h" +#ifdef OHOS_ARCH_LITE +#include "wifi_internal_event_dispatcher_lite.h" +#else +#include "wifi_internal_event_dispatcher.h" +#include "wifi_sa_manager.h" +#endif + +DEFINE_WIFILOG_LABEL("WifiP2pManager"); + +namespace OHOS { +namespace Wifi { +WifiP2pManager::WifiP2pManager() +{ + WIFI_LOGI("create WifiP2pManager"); + InitP2pCallback(); +} + +IP2pServiceCallbacks& WifiP2pManager::GetP2pCallback(void) +{ + return mP2pCallback; +} + +ErrCode WifiP2pManager::AutoStartP2pService() +{ + WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); + WIFI_LOGI("AutoStartP2pService, current p2p state:%{public}d", p2pState); + if (p2pState != WifiOprMidState::CLOSED) { + if (p2pState == WifiOprMidState::CLOSING) { + return WIFI_OPT_OPEN_FAIL_WHEN_CLOSING; + } else { + return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; + } + } + + if (!WifiConfigCenter::GetInstance().SetP2pMidState(p2pState, WifiOprMidState::OPENING)) { + WIFI_LOGE("AutoStartP2pService, set p2p mid state opening failed!"); + return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; + } + + ErrCode ret = WIFI_OPT_FAILED; + do { + if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_P2P) < 0) { + WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_P2P); + break; + } + IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst(); + if (pService == nullptr) { + WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_P2P); + break; + } + ret = pService->RegisterP2pServiceCallbacks(mP2pCallback); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Register p2p service callback failed!"); + break; + } + ret = pService->EnableP2p(); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("service EnableP2p failed, ret %{public}d!", static_cast(ret)); + break; + } + } while (false); + if (ret != WIFI_OPT_SUCCESS) { + WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); + return ret; + } +#ifndef OHOS_ARCH_LITE + StopUnloadP2PSaTimer(); +#endif + return WIFI_OPT_SUCCESS; +} + +ErrCode WifiP2pManager::AutoStopP2pService() +{ + WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); + WIFI_LOGI("AutoStopP2pService, current p2p state:%{public}d", p2pState); + if (p2pState != WifiOprMidState::RUNNING) { + if (p2pState == WifiOprMidState::OPENING) { + return WIFI_OPT_CLOSE_FAIL_WHEN_OPENING; + } else { + return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; + } + } + + if (!WifiConfigCenter::GetInstance().SetP2pMidState(p2pState, WifiOprMidState::CLOSING)) { + WIFI_LOGE("AutoStopP2pService, set p2p mid state opening failed!"); + return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; + } + + IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst(); + if (pService == nullptr) { + WIFI_LOGE("AutoStopP2pService, Instance get p2p service is null!"); + WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSED); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); + return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; + } + + ErrCode ret = pService->DisableP2p(); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("service disable p2p failed, ret %{public}d!", static_cast(ret)); + WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSING, WifiOprMidState::RUNNING); + return ret; + } + + return WIFI_OPT_SUCCESS; +} + +#ifndef OHOS_ARCH_LITE +void WifiP2pManager::UnloadP2PSaTimerCallback() +{ + WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_P2P_ABILITY_ID); + StopUnloadP2PSaTimer(); +} + +void WifiP2pManager::StopUnloadP2PSaTimer(void) +{ + WIFI_LOGI("StopUnloadP2PSaTimer! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); + std::unique_lock lock(unloadP2PSaTimerMutex); + WifiTimer::GetInstance()->UnRegister(unloadP2PSaTimerId); + unloadP2PSaTimerId = 0; + return; +} + +void WifiP2pManager::StartUnloadP2PSaTimer(void) +{ + WIFI_LOGI("StartUnloadP2PSaTimer! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); + std::unique_lock lock(unloadP2PSaTimerMutex); + if (unloadP2PSaTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiP2pManager::UnloadP2PSaTimerCallback, this); + WifiTimer::GetInstance()->Register(timeoutCallback, unloadP2PSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("StartUnloadP2PSaTimer success! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); + } + return; +} +#endif + +void WifiP2pManager::CloseP2pService(void) +{ + WIFI_LOGD("close p2p service"); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); + WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSED); + WifiSettings::GetInstance().SetP2pState(static_cast(P2pState::P2P_STATE_CLOSED)); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_P2P_STATE_CHANGE; + cbMsg.msgData = static_cast(P2pState::P2P_STATE_CLOSED); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); +#ifndef OHOS_ARCH_LITE + if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { + WIFI_LOGI("airplaneMode not close p2p SA!"); + return; + } + StartUnloadP2PSaTimer(); +#endif + WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(); + WIFI_LOGI("CloseP2pService, current sta state:%{public}d", staState); + if (staState == WifiOprMidState::OPENING || staState == WifiOprMidState::RUNNING) { + AutoStartP2pService(); + } + return; +} + +void WifiP2pManager::InitP2pCallback(void) +{ + using namespace std::placeholders; + mP2pCallback.OnP2pStateChangedEvent = std::bind(&WifiP2pManager::DealP2pStateChanged, this, _1); + mP2pCallback.OnP2pPeersChangedEvent = std::bind(&WifiP2pManager::DealP2pPeersChanged, this, _1); + mP2pCallback.OnP2pServicesChangedEvent = std::bind(&WifiP2pManager::DealP2pServiceChanged, this, _1); + mP2pCallback.OnP2pConnectionChangedEvent = std::bind(&WifiP2pManager::DealP2pConnectionChanged, this, _1); + mP2pCallback.OnP2pThisDeviceChangedEvent = std::bind(&WifiP2pManager::DealP2pThisDeviceChanged, this, _1); + mP2pCallback.OnP2pDiscoveryChangedEvent = std::bind(&WifiP2pManager::DealP2pDiscoveryChanged, this, _1); + mP2pCallback.OnP2pGroupsChangedEvent = std::bind(&WifiP2pManager::DealP2pGroupsChanged, this); + mP2pCallback.OnP2pActionResultEvent = std::bind(&WifiP2pManager::DealP2pActionResult, this, _1, _2); + mP2pCallback.OnConfigChangedEvent = std::bind(&WifiP2pManager::DealConfigChanged, this, _1, _2, _3); + return; +} + +void WifiP2pManager::DealP2pStateChanged(P2pState state) +{ + WIFI_LOGI("DealP2pStateChanged, state: %{public}d", static_cast(state)); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_P2P_STATE_CHANGE; + cbMsg.msgData = static_cast(state); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + if (state == P2pState::P2P_STATE_IDLE) { + WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::P2P_SERVICE_CLOSE); + } + if (state == P2pState::P2P_STATE_STARTED) { + WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING); + WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(); + WIFI_LOGI("DealP2pStateChanged, current sta state:%{public}d", staState); + if (staState == WifiOprMidState::CLOSING || staState == WifiOprMidState::CLOSED) { + AutoStopP2pService(); + } + } + if (state == P2pState::P2P_STATE_CLOSED) { + bool ret = WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); + if (ret) { + WIFI_LOGE("P2p start failed, stop wifi!"); + WifiManager::GetInstance().GetWifiTogglerManager()->WifiToggled(0, 0); + cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; + cbMsg.msgData = static_cast(WifiState::DISABLED); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + } + } + WifiCommonEventHelper::PublishP2pStateChangedEvent((int)state, "OnP2pStateChanged"); + return; +} + +void WifiP2pManager::DealP2pPeersChanged(const std::vector &vPeers) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_PEER_CHANGE; + cbMsg.device = vPeers; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WifiCommonEventHelper::PublishP2pPeersStateChangedEvent(vPeers.size(), "OnP2pPeersChanged"); + return; +} + +void WifiP2pManager::DealP2pServiceChanged(const std::vector &vServices) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_SERVICE_CHANGE; + cbMsg.serviceInfo = vServices; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} + +void WifiP2pManager::DealP2pConnectionChanged(const WifiP2pLinkedInfo &info) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_CONNECT_CHANGE; + cbMsg.p2pInfo = info; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WifiCommonEventHelper::PublishP2pConnStateEvent((int)info.GetConnectState(), "OnP2pConnectStateChanged"); + return; +} + +void WifiP2pManager::DealP2pThisDeviceChanged(const WifiP2pDevice &info) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_THIS_DEVICE_CHANGE; + cbMsg.p2pDevice = info; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WifiCommonEventHelper::PublishP2pCurrentDeviceStateChangedEvent( + (int)info.GetP2pDeviceStatus(), "OnP2pThisDeviceChanged"); + return; +} + +void WifiP2pManager::DealP2pDiscoveryChanged(bool bState) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_DISCOVERY_CHANGE; + cbMsg.msgData = static_cast(bState); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} + +void WifiP2pManager::DealP2pGroupsChanged() __attribute__((no_sanitize("cfi"))) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WifiCommonEventHelper::PublishP2pGroupStateChangedEvent(0, "OnP2pGroupStateChanged"); + return; +} + +void WifiP2pManager::DealP2pActionResult(P2pActionCallback action, ErrCode code) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_P2P_ACTION_RESULT; + cbMsg.p2pAction = action; + cbMsg.msgData = static_cast(code); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} + +void WifiP2pManager::DealConfigChanged(CfgType type, char* data, int dataLen) +{ + if (data == nullptr || dataLen <= 0) { + return; + } + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_CFG_CHANGE; + CfgInfo* cfgInfoPtr = new (std::nothrow) CfgInfo(); + if (cfgInfoPtr == nullptr) { + WIFI_LOGE("DealConfigChanged: new CfgInfo failed"); + return; + } + cfgInfoPtr->type = type; + char* cfgData = new (std::nothrow) char[dataLen]; + if (cfgData == nullptr) { + WIFI_LOGE("DealConfigChanged: new data failed"); + delete cfgInfoPtr; + return; + } + if (memcpy_s(cfgData, dataLen, data, dataLen) != EOK) { + WIFI_LOGE("DealConfigChanged: memcpy_s failed"); + delete cfgInfoPtr; + delete[] cfgData; + return; + } + cfgInfoPtr->data = cfgData; + cfgInfoPtr->dataLen = dataLen; + cbMsg.cfgInfo = cfgInfoPtr; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} +} // namespace Wifi +} // namespace OHOS +#endif \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..128612c5e56389d0d55cbcd7192ef64551f36e28 --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WIFI_P2P_MANAGER_H +#define OHOS_WIFI_P2P_MANAGER_H + +#ifdef FEATURE_P2P_SUPPORT +#include +#include +#include "wifi_errcode.h" +#include "wifi_internal_msg.h" +#include "ip2p_service_callbacks.h" + +namespace OHOS { +namespace Wifi { +class WifiP2pManager { +public: + WifiP2pManager(); + ~WifiP2pManager() = default; + + IP2pServiceCallbacks& GetP2pCallback(void); + ErrCode AutoStartP2pService(); + ErrCode AutoStopP2pService(); + void StopUnloadP2PSaTimer(void); + void StartUnloadP2PSaTimer(void); + void CloseP2pService(void); + +private: + void InitP2pCallback(void); + void UnloadP2PSaTimerCallback(); + void DealP2pStateChanged(P2pState bState); + void DealP2pPeersChanged(const std::vector &vPeers); + void DealP2pServiceChanged(const std::vector &vServices); + void DealP2pConnectionChanged(const WifiP2pLinkedInfo &info); + void DealP2pThisDeviceChanged(const WifiP2pDevice &info); + void DealP2pDiscoveryChanged(bool bState); + void DealP2pGroupsChanged(void); + void DealP2pActionResult(P2pActionCallback action, ErrCode code); + void DealConfigChanged(CfgType type, char* data, int dataLen); + +private: + IP2pServiceCallbacks mP2pCallback; + uint32_t unloadP2PSaTimerId{0}; + std::mutex unloadP2PSaTimerMutex; +}; + +} // namespace Wifi +} // namespace OHOS +#endif +#endif // OHOS_WIFI_P2P_MANAGER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac3d314b16327852a249b04a2e59c7e22a6cfdeb --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wifi_scan_manager.h" +#include "wifi_manager.h" +#include "wifi_service_manager.h" +#include "wifi_config_center.h" +#include "wifi_global_func.h" +#include "wifi_logger.h" +#include "wifi_common_event_helper.h" +#include "wifi_system_timer.h" +#ifdef OHOS_ARCH_LITE +#include "wifi_internal_event_dispatcher_lite.h" +#else +#include "wifi_internal_event_dispatcher.h" +#include "wifi_sa_manager.h" +#endif + +DEFINE_WIFILOG_LABEL("WifiScanManager"); + +namespace OHOS { +namespace Wifi { +WifiScanManager::WifiScanManager() +{ + WIFI_LOGI("create WifiScanManager"); + InitScanCallback(); +} + +IScanSerivceCallbacks& WifiScanManager::GetScanCallback() +{ + return mScanCallback; +} + +#ifndef OHOS_ARCH_LITE +void WifiScanManager::UnloadScanSaTimerCallback() +{ + WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_SCAN_ABILITY_ID); + StopUnloadScanSaTimer(); +} + +void WifiScanManager::StopUnloadScanSaTimer(void) +{ + WIFI_LOGI("StopUnloadScanSaTimer! unloadScanSaTimerId:%{public}u", unloadScanSaTimerId); + std::unique_lock lock(unloadScanSaTimerMutex); + WifiTimer::GetInstance()->UnRegister(unloadScanSaTimerId); + unloadScanSaTimerId = 0; + return; +} + +void WifiScanManager::StartUnloadScanSaTimer(void) +{ + WIFI_LOGI("StartUnloadScanSaTimer! unloadScanSaTimerId:%{public}u", unloadScanSaTimerId); + std::unique_lock lock(unloadScanSaTimerMutex); + if (unloadScanSaTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiScanManager::UnloadScanSaTimerCallback, this); + WifiTimer::GetInstance()->Register(timeoutCallback, unloadScanSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("StartUnloadScanSaTimer success! unloadScanSaTimerId:%{public}u", unloadScanSaTimerId); + } + return; +} +#endif + +void WifiScanManager::CheckAndStartScanService(int instId) +{ + WifiOprMidState scanState = WifiConfigCenter::GetInstance().GetScanMidState(instId); + WIFI_LOGI("CheckAndStartScanService scanState: %{public}d", static_cast(scanState)); + if (scanState != WifiOprMidState::CLOSED) { + /* If the scanning function is enabled when the STA is not enabled, you need to start the scheduled + scanning function immediately when the STA is enabled. */ + IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); + if (pService != nullptr) { + pService->OnClientModeStatusChanged(static_cast(OperateResState::DISCONNECT_DISCONNECTED)); + } + return; + } + if (!WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::OPENING, instId)) { + WIFI_LOGW("Failed to set scan mid state opening!"); + return; + } + ErrCode errCode = WIFI_OPT_FAILED; + do { + if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_SCAN) < 0) { + WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_SCAN); + break; + } + IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); + if (pService == nullptr) { + WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_SCAN); + break; + } + errCode = pService->RegisterScanCallbacks(mScanCallback); + if (errCode != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Register scan service callback failed!"); + break; + } + errCode = pService->Init(); + if (errCode != WIFI_OPT_SUCCESS) { + WIFI_LOGE("init scan service failed, ret %{public}d!", static_cast(errCode)); + break; + } + IEnhanceService *pEnhanceService = WifiServiceManager::GetInstance().GetEnhanceServiceInst(); + if (pEnhanceService == nullptr) { + WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_ENHANCE); + break; + } + errCode = pService->SetEnhanceService(pEnhanceService); + if (errCode != WIFI_OPT_SUCCESS) { + WIFI_LOGE("SetEnhanceService failed, ret %{public}d!", static_cast(errCode)); + break; + } + } while (0); + if (errCode != WIFI_OPT_SUCCESS) { + WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, instId); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_SCAN, instId); + } + return; +} + +void WifiScanManager::CheckAndStopScanService(int instId) +{ + /** + * Check unload SCAN service + * When anytime scanning is enabled and the control policy allows, airplane + * mode and power saving mode are disabled. --- Do not disable the scan + * service. Otherwise, disable the SCAN service. + */ + WifiOprMidState scanState = WifiConfigCenter::GetInstance().GetScanMidState(instId); + WIFI_LOGI("[CheckAndStopScanService] scanState %{public}d!", static_cast(scanState)); + if (scanState != WifiOprMidState::OPENING && scanState != WifiOprMidState::RUNNING) { + return; + } + ScanControlInfo info; + WifiConfigCenter::GetInstance().GetScanControlInfo(info, instId); + if (WifiConfigCenter::GetInstance().IsScanAlwaysActive() && IsAllowScanAnyTime(info) && + WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_CLOSE && + WifiConfigCenter::GetInstance().GetPowerSavingModeState() == MODE_STATE_CLOSE) { + return; + } + /* After check condition over, begin unload SCAN service */ + if (WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::CLOSING, instId)) { + IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); + if (pService == nullptr) { + WIFI_LOGE("[CheckAndStopScanService] scan service is null."); + WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::SCAN_SERVICE_CLOSE, instId); + WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::CLOSED, instId); + return; + } + ErrCode ret = pService->UnInit(); + if (ret != WIFI_OPT_SUCCESS) { // scan service is not exist + WIFI_LOGE("[CheckAndStopScanService] UnInit service failed!"); + } + WifiConfigCenter::GetInstance().SetScanMidState(scanState, WifiOprMidState::CLOSED, instId); + } +} + +void WifiScanManager::CloseScanService(int instId) +{ + WIFI_LOGI("close scan service"); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_SCAN, instId); + WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::CLOSED, instId); + WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(instId); + WIFI_LOGI("CloseScanService, current sta state:%{public}d", staState); + if (staState == WifiOprMidState::OPENING || staState == WifiOprMidState::RUNNING) { + CheckAndStartScanService(instId); + return; + } +#ifndef OHOS_ARCH_LITE + StartUnloadScanSaTimer(); +#endif + return; +} + +void WifiScanManager::InitScanCallback(void) +{ + using namespace std::placeholders; + mScanCallback.OnScanStartEvent = std::bind(&WifiScanManager::DealScanOpenRes, this, _1); + mScanCallback.OnScanStopEvent = std::bind(&WifiScanManager::DealScanCloseRes, this, _1); + mScanCallback.OnScanFinishEvent = std::bind(&WifiScanManager::DealScanFinished, this, _1, _2); + mScanCallback.OnScanInfoEvent = std::bind(&WifiScanManager::DealScanInfoNotify, this, _1, _2); + mScanCallback.OnStoreScanInfoEvent = std::bind(&WifiScanManager::DealStoreScanInfoEvent, this, _1, _2); +} + +void WifiScanManager::DealScanOpenRes(int instId) +{ + WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, instId); +} + +void WifiScanManager::DealScanCloseRes(int instId) +{ + WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::SCAN_SERVICE_CLOSE, instId); +} + +void WifiScanManager::DealScanFinished(int state, int instId) +{ + WIFI_LOGE("%{public}s, state: %{public}d!", __func__, state); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_SCAN_STATE_CHANGE; + cbMsg.msgData = state; + cbMsg.id = instId; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WifiCommonEventHelper::PublishScanFinishedEvent(state,"OnScanFinished"); +} + +void WifiScanManager::DealScanInfoNotify(std::vector &results, int instId) +{ + if (WifiConfigCenter::GetInstance().GetWifiMidState(instId) == WifiOprMidState::RUNNING) { + IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(instId); + if (pService != nullptr) { + pService->ConnectivityManager(results); + } + } +} + +void WifiScanManager::DealStoreScanInfoEvent(std::vector &results, int instId) +{ + WIFI_LOGI("DealStoreScanInfoEvent"); +} +} // namespace Wifi +} // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..b9c0b9be86bfffb0c3099d11c08c3de9a28c5c25 --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WIFI_SCAN_MANAGER_H +#define OHOS_WIFI_SCAN_MANAGER_H + +#include +#include +#include "wifi_errcode.h" +#include "iscan_service_callbacks.h" +#include "wifi_internal_msg.h" + +namespace OHOS { +namespace Wifi { +class WifiScanManager { +public: + WifiScanManager(); + ~WifiScanManager() = default; + + IScanSerivceCallbacks& GetScanCallback(void); + void StopUnloadScanSaTimer(void); + void StartUnloadScanSaTimer(void); + void CheckAndStartScanService(int instId = 0); + void CheckAndStopScanService(int instId = 0); + void CloseScanService(int instId = 0); + +private: + void InitScanCallback(void); + void UnloadScanSaTimerCallback(); + void DealScanOpenRes(int instId = 0); + void DealScanCloseRes(int instId = 0); + void DealScanFinished(int state, int instId = 0); + void DealScanInfoNotify(std::vector &results, int instId = 0); + void DealStoreScanInfoEvent(std::vector &results, int instId = 0); + +private: + IScanSerivceCallbacks mScanCallback; + uint32_t unloadScanSaTimerId{0}; + std::mutex unloadScanSaTimerMutex; +}; + +} // namespace Wifi +} // namespace OHOS +#endif // OHOS_WIFI_SCAN_MANAGER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6be587b6ca5b0e5d2c81daa6d8abe8c30c499077 --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.cpp @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wifi_sta_manager.h" +#include "wifi_manager.h" +#include "wifi_service_manager.h" +#include "wifi_config_center.h" +#include "wifi_global_func.h" +#include "wifi_logger.h" +#include "wifi_hisysevent.h" +#include "wifi_protect_manager.h" +#include "wifi_system_timer.h" +#ifdef OHOS_ARCH_LITE +#include "wifi_internal_event_dispatcher_lite.h" +#else +#include "wifi_internal_event_dispatcher.h" +#include "wifi_sa_manager.h" +#endif + +DEFINE_WIFILOG_LABEL("WifiStaManager"); + +namespace OHOS { +namespace Wifi { +WifiStaManager::WifiStaManager() +{ + WIFI_LOGI("create WifiStaManager"); + InitStaCallback(); +} + +StaServiceCallback& WifiStaManager::GetStaCallback() +{ + return mStaCallback; +} + +#ifndef OHOS_ARCH_LITE +void WifiStaManager::UnloadStaSaTimerCallback() +{ + WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_DEVICE_ABILITY_ID); + StopUnloadStaSaTimer(); +} + +void WifiStaManager::StopUnloadStaSaTimer(void) +{ + WIFI_LOGI("StopUnloadStaSaTimer! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); + std::unique_lock lock(unloadStaSaTimerMutex); + WifiTimer::GetInstance()->UnRegister(unloadStaSaTimerId); + unloadStaSaTimerId = 0; + return; +} + +void WifiStaManager::StartUnloadStaSaTimer(void) +{ + WIFI_LOGI("StartUnloadStaSaTimer! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); + std::unique_lock lock(unloadStaSaTimerMutex); + if (unloadStaSaTimerId == 0) { + WifiTimer::TimerCallback timeoutCallback = std::bind(&WifiStaManager::UnloadStaSaTimerCallback, this); + WifiTimer::GetInstance()->Register(timeoutCallback, unloadStaSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("StartUnloadStaSaTimer success! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); + } + return; +} +#endif + +void WifiStaManager::CloseStaService(int instId) +{ + WIFI_LOGI("close sta service"); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, instId); + WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSED, instId); + auto &ins = WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine(); + ins->HandleStaClose(instId); + WifiConfigCenter::GetInstance().SetWifiStaCloseTime(instId); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; + cbMsg.msgData = static_cast(WifiState::DISABLED); + cbMsg.id = instId; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); +#ifdef FEATURE_P2P_SUPPORT + WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); + WIFI_LOGI("CloseStaService, current p2p state: %{public}d", p2pState); + if (p2pState == WifiOprMidState::RUNNING) { + WifiManager::GetInstance().GetWifiP2pManager()->AutoStopP2pService(); + } +#endif +#ifndef OHOS_ARCH_LITE + if (WifiConfigCenter::GetInstance().GetAirplaneModeState() == MODE_STATE_OPEN) { + WIFI_LOGI("airplaneMode not close sta SA!"); + return; + } + if (WifiConfigCenter::GetInstance().GetPowerSleepState() == MODE_STATE_OPEN) { + StopUnloadStaSaTimer(); + return; + } + StartUnloadStaSaTimer(); +#endif + return; +} + +void WifiStaManager::InitStaCallback(void) +{ + using namespace std::placeholders; + mStaCallback.callbackModuleName = "WifiStaManager"; + mStaCallback.OnStaOpenRes = std::bind(&WifiStaManager::DealStaOpenRes, this, _1, _2); + mStaCallback.OnStaCloseRes = std::bind(&WifiStaManager::DealStaCloseRes, this, _1, _2); + mStaCallback.OnStaConnChanged = std::bind(&WifiStaManager::DealStaConnChanged, this, _1, _2, _3); + mStaCallback.OnWpsChanged = std::bind(&WifiStaManager::DealWpsChanged, this, _1, _2, _3); + mStaCallback.OnStaStreamChanged = std::bind(&WifiStaManager::DealStreamChanged, this, _1, _2); + mStaCallback.OnStaRssiLevelChanged = std::bind(&WifiStaManager::DealRssiChanged, this, _1, _2); + return; +} + +void WifiStaManager::ForceStopWifi(int instId) +{ + WIFI_LOGI("Enter ForceStopWifi"); + IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(instId); + if (pService == nullptr || (pService->DisableWifi() != WIFI_OPT_SUCCESS)) { + WIFI_LOGE("service is null or disable wifi failed."); + WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::CLOSED, instId); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; + cbMsg.msgData = static_cast(WifiState::UNKNOWN); + cbMsg.id = instId; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_STA, instId); + return; + } + WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiMidState(instId); + WIFI_LOGI("In force stop wifi, state: %{public}d", static_cast(curState)); + WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::CLOSED, instId); +} + +void WifiStaManager::DealStaOpenRes(OperateResState state, int instId) +{ + WIFI_LOGD("Enter DealStaOpenRes: %{public}d", static_cast(state)); + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; + cbMsg.id = instId; + if (state == OperateResState::OPEN_WIFI_OPENING) { + cbMsg.msgData = static_cast(WifiState::ENABLING); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_OPEN), + static_cast(WifiOperateState::STA_OPENING)); + return; + } + if ((state == OperateResState::OPEN_WIFI_FAILED) || (state == OperateResState::OPEN_WIFI_DISABLED)) { + WIFI_LOGE("DealStaOpenRes:wifi open failed!"); + WifiOprMidState apstate = WifiConfigCenter::GetInstance().GetApMidState(instId); + WriteWifiOpenAndCloseFailedHiSysEvent(static_cast(OperateResState::OPEN_WIFI_FAILED), "TIME_OUT", + static_cast(apstate)); + WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, instId); + DealStaCloseRes(state, instId); + return; + } + + WIFI_LOGI("DealStaOpenRes:wifi open successfully!"); + WifiConfigCenter::GetInstance().SetWifiMidState(WifiOprMidState::OPENING, WifiOprMidState::RUNNING, instId); + WifiConfigCenter::GetInstance().SetStaLastRunState(true, instId); + auto &ins = WifiManager::GetInstance().GetWifiTogglerManager()->GetControllerMachine(); + ins->HandleStaStart(instId); + cbMsg.msgData = static_cast(WifiState::ENABLED); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_OPEN), + static_cast(WifiOperateState::STA_OPENED)); + if (WifiOprMidState::RUNNING == WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId)) { + WIFI_LOGI("DealStaOpenRes: wifi scan only state notify scan result!"); + IScanSerivceCallbacks &scanCallback = WifiManager::GetInstance().GetWifiScanManager()->GetScanCallback(); + scanCallback.OnScanFinishEvent(static_cast(ScanHandleNotify::SCAN_OK), instId); + + } +#ifdef FEATURE_P2P_SUPPORT + WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); + WIFI_LOGI("DealStaOpenRes, current p2p state:%{public}d", p2pState); + if (p2pState == WifiOprMidState::CLOSED) { + WifiManager::GetInstance().GetWifiP2pManager()->AutoStartP2pService(); + } +#endif + WifiManager::GetInstance().AutoStartEnhanceService(); + WifiManager::GetInstance().GetWifiScanManager()->CheckAndStartScanService(instId); + return; +} + +void WifiStaManager::DealStaCloseRes(OperateResState state, int instId) +{ + WIFI_LOGD("Enter DealStaCloseRes: %{public}d", static_cast(state)); +#ifndef OHOS_ARCH_LITE + WifiProtectManager::GetInstance().UpdateWifiClientConnected(false); +#endif + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_STATE_CHANGE; + cbMsg.id = instId; + if (state == OperateResState::CLOSE_WIFI_CLOSING) { + cbMsg.msgData = static_cast(WifiState::DISABLING); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_CLOSING)); + return; + } + if (state == OperateResState::CLOSE_WIFI_FAILED) { + WIFI_LOGI("DealStaCloseRes: broadcast wifi close failed event!"); + WifiOprMidState apstate = WifiConfigCenter::GetInstance().GetApMidState(instId); + WriteWifiOpenAndCloseFailedHiSysEvent(static_cast(OperateResState::CLOSE_WIFI_FAILED), "TIME_OUT", + static_cast(apstate)); + ForceStopWifi(instId); + cbMsg.msgData = static_cast(WifiState::UNKNOWN); + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + } + if (WifiOprMidState::RUNNING != WifiConfigCenter::GetInstance().GetWifiScanOnlyMidState(instId)) { + WIFI_LOGI("DealStaCloseRes: wifi scan only state is not running,to CheckAndStopScanService!"); + WifiManager::GetInstance().GetWifiScanManager()->CheckAndStopScanService(instId); + } + + WifiManager::GetInstance().PushServiceCloseMsg(WifiCloseServiceCode::STA_SERVICE_CLOSE, instId); + return; +} +void WifiStaManager::DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId) +{ + WIFI_LOGI("Enter, DealStaConnChanged, state: %{public}d!\n", static_cast(state)); + bool isReport = true; + int reportStateNum = static_cast(ConvertConnStateInternal(state, isReport)); + if (isReport) { + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_CONNECTION_CHANGE; + cbMsg.msgData = reportStateNum; + cbMsg.linkInfo = info; + cbMsg.id = instId; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + } + + if (state == OperateResState::CONNECT_CONNECTING || state == OperateResState::CONNECT_AP_CONNECTED || + state == OperateResState::DISCONNECT_DISCONNECTING || state == OperateResState::DISCONNECT_DISCONNECTED || + state == OperateResState::CONNECT_OBTAINING_IP || state == OperateResState::CONNECT_ASSOCIATING || + state == OperateResState::CONNECT_ASSOCIATED || state == OperateResState::CONNECT_NETWORK_ENABLED || + state == OperateResState::CONNECT_NETWORK_DISABLED) { + if (WifiConfigCenter::GetInstance().GetScanMidState(instId) == WifiOprMidState::RUNNING) { + IScanService *pService = WifiServiceManager::GetInstance().GetScanServiceInst(instId); + if (pService != nullptr) { + pService->OnClientModeStatusChanged(static_cast(state)); + } + } + } + switch (state) { + case OperateResState::CONNECT_CONNECTING: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_CONNECT), + static_cast(WifiOperateState::STA_CONNECTING)); + break; + case OperateResState::CONNECT_AP_CONNECTED: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_CONNECT), + static_cast(WifiOperateState::STA_CONNECTED)); + break; + case OperateResState::DISCONNECT_DISCONNECTED: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_CONNECT), + static_cast(WifiOperateState::STA_DISCONNECTED)); + break; + case OperateResState::CONNECT_ASSOCIATING: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_ASSOC), + static_cast(WifiOperateState::STA_ASSOCIATING)); + WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_ASSOCIATING)); + break; + case OperateResState::CONNECT_ASSOCIATED: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_ASSOC), + static_cast(WifiOperateState::STA_ASSOCIATED)); + WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_ASSOCIATED)); + break; + case OperateResState::CONNECT_CONNECTION_FULL: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_ASSOC), + static_cast(WifiOperateState::STA_ASSOC_FULL_REJECT)); + break; + case OperateResState::CONNECT_OBTAINING_IP: + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_DHCP), + static_cast(WifiOperateState::STA_DHCP)); + break; + case OperateResState::DISCONNECT_DISCONNECTING: + case OperateResState::CONNECT_CONNECTING_TIMEOUT: + WriteWifiConnectFailedEventHiSysEvent(static_cast(WifiOperateState::STA_DISCONNECT)); + break; + default: + break; + } + if (info.connState == ConnState::AUTHENTICATING) + { + WriteWifiOperateStateHiSysEvent(static_cast(WifiOperateType::STA_AUTH), + static_cast(WifiOperateState::STA_AUTHING)); + } +#ifdef FEATURE_HPF_SUPPORT + if (state == OperateResState::CONNECT_AP_CONNECTED) { + int screenState = WifiSettings::GetInstance().GetScreenState(); + WifiManager::GetInstance().InstallPacketFilterProgram(screenState, instId); + } +#endif +#ifndef OHOS_ARCH_LITE + bool isConnected = (info.connState == CONNECTED) ? true : false; + WifiProtectManager::GetInstance().UpdateWifiClientConnected(isConnected); +#endif + return; +} + +void WifiStaManager::DealWpsChanged(WpsStartState state, const int pinCode, int instId) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_WPS_STATE_CHANGE; + cbMsg.msgData = static_cast(state); + cbMsg.id = instId; + cbMsg.pinCode = std::to_string(pinCode); + int len = cbMsg.pinCode.length(); + if (len < 8) { /* Fill in 8 digits. */ + cbMsg.pinCode = std::string(8 - len, '0') + cbMsg.pinCode; + } + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} + +void WifiStaManager::DealStreamChanged(StreamDirection direction, int instId) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_STREAM_DIRECTION; + cbMsg.msgData = static_cast(direction); + cbMsg.id = instId; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} + +void WifiStaManager::DealRssiChanged(int rssi, int instId) +{ + WifiEventCallbackMsg cbMsg; + cbMsg.msgCode = WIFI_CBK_MSG_RSSI_CHANGE; + cbMsg.msgData = rssi; + cbMsg.id = instId; + WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); + return; +} +} // namespace Wifi +} // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..6ab5c822a98f3bc1fad33eee2d54b1f164c3a51d --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WIFI_STA_MANAGER_H +#define OHOS_WIFI_STA_MANAGER_H + +#include +#include +#include "wifi_errcode.h" +#include "sta_service_callback.h" +#include "wifi_internal_msg.h" + +namespace OHOS { +namespace Wifi { +class WifiStaManager { +public: + WifiStaManager(); + ~WifiStaManager() = default; + + StaServiceCallback& GetStaCallback(void); + void StopUnloadStaSaTimer(void); + void StartUnloadStaSaTimer(void); + void CloseStaService(int instId = 0); + +private: + void InitStaCallback(void); + void ForceStopWifi(int instId = 0); + void UnloadStaSaTimerCallback(); + void DealStaOpenRes(OperateResState state, int instId = 0); + void DealStaCloseRes(OperateResState state, int instId = 0); + void DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId = 0); + void DealWpsChanged(WpsStartState state, const int pinCode, int instId = 0); + void DealStreamChanged(StreamDirection direction, int instId = 0); + void DealRssiChanged(int rssi, int instId = 0); + +private: + StaServiceCallback mStaCallback; + uint32_t unloadStaSaTimerId{0}; + std::mutex unloadStaSaTimerMutex; +}; + +} // namespace Wifi +} // namespace OHOS +#endif // OHOS_WIFI_STA_MANAGER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8241d614a152b4868bbb68a0f72ecd1ff1d9a70 --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.cpp @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wifi_toggler_manager.h" +#include "wifi_manager.h" +#include "wifi_service_manager.h" +#include "wifi_config_center.h" +#include "wifi_logger.h" + +DEFINE_WIFILOG_LABEL("WifiTogglerManager") + +namespace OHOS { +namespace Wifi { +WifiTogglerManager::WifiTogglerManager() +{ + WIFI_LOGI("create WifiTogglerManager"); + InitConcreteCallback(); + InitSoftapCallback(); + pWifiControllerMachine = std::make_unique(); + if (pWifiControllerMachine) { + pWifiControllerMachine->InitWifiControllerMachine(); + } +} + +ConcreteModeCallback& WifiTogglerManager::GetConcreteCallback() +{ + return mConcreteModeCb; +} + +SoftApModeCallback& WifiTogglerManager::GetSoftApCallback() +{ + return mSoftApModeCb; +} + +ErrCode WifiTogglerManager::WifiToggled(int isOpen, int id) +{ + if (!WifiSettings::GetInstance().GetCoexSupport() && isOpen) { + WIFI_LOGI("set softap toggled false"); + WifiSettings::GetInstance().SetSoftapToggledState(false); + } + pWifiControllerMachine->SendMessage(CMD_WIFI_TOGGLED, isOpen, id); + return WIFI_OPT_SUCCESS; +} + +ErrCode WifiTogglerManager::SoftapToggled(int isOpen, int id) +{ + if (isOpen) { + WIFI_LOGI("set softap toggled true"); + WifiSettings::GetInstance().SetSoftapToggledState(true); + } else { + WIFI_LOGI("set softap toggled false"); + WifiSettings::GetInstance().SetSoftapToggledState(false); + } + pWifiControllerMachine->SendMessage(CMD_SOFTAP_TOGGLED, isOpen, id); + return WIFI_OPT_SUCCESS; +} + +ErrCode WifiTogglerManager::ScanOnlyToggled(int isOpen) +{ + int airplanState = WifiConfigCenter::GetInstance().GetAirplaneModeState(); + if (airplanState == MODE_STATE_OPEN) { + WIFI_LOGE("Airplane mode do not start scanonly."); + return WIFI_OPT_FAILED; + } + if (!WifiSettings::GetInstance().GetCoexSupport() && HasAnyApRuning()) { + WIFI_LOGE("Softap mode do not start scanonly."); + return WIFI_OPT_FAILED; + } + pWifiControllerMachine->SendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED, isOpen, 0); + return WIFI_OPT_SUCCESS; +} + +ErrCode WifiTogglerManager::AirplaneToggled(int isOpen) +{ + pWifiControllerMachine->SendMessage(CMD_AIRPLANE_TOGGLED, isOpen); + return WIFI_OPT_SUCCESS; +} + +bool WifiTogglerManager::HasAnyApRuning() +{ + WifiOprMidState apState0 = WifiConfigCenter::GetInstance().GetApMidState(0); + WifiOprMidState apState1 = WifiConfigCenter::GetInstance().GetApMidState(1); + if (apState0 == WifiOprMidState::RUNNING || apState0 == WifiOprMidState::OPENING || + apState1 == WifiOprMidState::RUNNING || apState1 == WifiOprMidState::OPENING) { + return true; + } + return false; +} + +std::unique_ptr& WifiTogglerManager::GetControllerMachine() +{ + return pWifiControllerMachine; +} + +void WifiTogglerManager::InitConcreteCallback() +{ + using namespace std::placeholders; + mConcreteModeCb.onStartFailure = std::bind(&WifiTogglerManager::DealConcreateStartFailure, this, _1); + mConcreteModeCb.onStopped = std::bind(&WifiTogglerManager::DealConcreateStop, this, _1); +} + +void WifiTogglerManager::InitSoftapCallback() +{ + using namespace std::placeholders; + mSoftApModeCb.onStartFailure = std::bind(&WifiTogglerManager::DealSoftapStartFailure, this, _1); + mSoftApModeCb.onStopped = std::bind(&WifiTogglerManager::DealSoftapStop, this, _1); +} + +void WifiTogglerManager::DealConcreateStop(int id) +{ + if (pWifiControllerMachine) { + pWifiControllerMachine->SendMessage(CMD_CONCRETE_STOPPED, id); + } +} + +void WifiTogglerManager::DealConcreateStartFailure(int id) +{ + if (pWifiControllerMachine) { + pWifiControllerMachine->SendMessage(CMD_STA_START_FAILURE, id); + } +} + +void WifiTogglerManager::DealSoftapStop(int id) +{ + if (pWifiControllerMachine) { + pWifiControllerMachine->SendMessage(CMD_AP_STOPPED, id); + } +} + +void WifiTogglerManager::DealSoftapStartFailure(int id) +{ + if (pWifiControllerMachine) { + pWifiControllerMachine->SendMessage(CMD_AP_START_FAILURE, id); + } +} + +} // namespace Wifi +} // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..0a0f76d36b6c8bee345ee425317c97e9bec77107 --- /dev/null +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WIFI_TOGGLRT_MANAGER_H +#define OHOS_WIFI_TOGGLRT_MANAGER_H + +#include +#include +#include "wifi_errcode.h" +#include "wifi_internal_msg.h" +#include "wifi_controller_define.h" +#include "wifi_controller_state_machine.h" + +namespace OHOS { +namespace Wifi { +class WifiTogglerManager { +public: + WifiTogglerManager(); + ~WifiTogglerManager() = default; + + ConcreteModeCallback& GetConcreteCallback(void); + SoftApModeCallback& GetSoftApCallback(void); + ErrCode WifiToggled(int isOpen, int id = 0); + ErrCode SoftapToggled(int isOpen, int id = 0); + ErrCode ScanOnlyToggled(int isOpen); + ErrCode AirplaneToggled(int isOpen); + bool HasAnyApRuning(); + std::unique_ptr& GetControllerMachine(); + +private: + void InitConcreteCallback(void); + void InitSoftapCallback(void); + void DealConcreateStop(int id = 0); + void DealConcreateStartFailure(int id = 0); + void DealSoftapStop(int id = 0); + void DealSoftapStartFailure(int id = 0); + +private: + ConcreteModeCallback mConcreteModeCb; + SoftApModeCallback mSoftApModeCb; + std::unique_ptr pWifiControllerMachine = nullptr; +}; + +} // namespace Wifi +} // namespace OHOS +#endif // OHOS_WIFI_TOGGLRT_MANAGER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_ability_listerner.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_ability_listerner.h index 66e7e3108c428afbd5eaca362a81989bc2398374..7a422cdeb5970e9d60d833c8e132c15045b19a3e 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_ability_listerner.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_ability_listerner.h @@ -27,7 +27,7 @@ namespace Wifi { class WifiSystemAbilityListener { public: WifiSystemAbilityListener(); - ~WifiSystemAbilityListener(); + virtual ~WifiSystemAbilityListener(); void SubscribeSystemAbility(int systemAbilityId); void UnSubscribeSystemAbility(int systemAbilityId); virtual void OnSystemAbilityChanged(int systemAbilityId, bool add) = 0; diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.cpp index 10c8d2b386001f197af41b1e30b772e9430aa028..374f81248344ff9ca5dcc669a34cf3252e606ceb 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.cpp @@ -13,10 +13,15 @@ * limitations under the License. */ +#ifndef OHOS_ARCH_LITE #include "wifi_system_timer.h" +#include "wifi_logger.h" +#include "common_timer_errors.h" namespace OHOS { namespace Wifi { +DEFINE_WIFILOG_LABEL("WifiTimer"); + WifiSysTimer::WifiSysTimer() {} @@ -67,5 +72,57 @@ void WifiSysTimer::SetWantAgent(std::shared_ptrwantAgent = wantAgent; } + +WifiTimer *WifiTimer::GetInstance() +{ + static WifiTimer instance; + return &instance; +} + +WifiTimer::WifiTimer() : timer_(std::make_unique("WifiManagerTimer")) +{ + timer_->Setup(); +} + +WifiTimer::~WifiTimer() +{ + if (timer_) { + timer_->Shutdown(true); + } +} + +bool WifiTimer::Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval, bool once) +{ + if (timer_ == nullptr) { + WIFI_LOGE("timer_ is nullptr"); + return false; + } + + uint32_t ret = timer_->Register(callback, interval, once); + if (ret == Utils::TIMER_ERR_DEAL_FAILED) { + WIFI_LOGE("Register timer failed"); + return false; + } + + outTimerId = ret; + return true; +} + +void WifiTimer::UnRegister(uint32_t timerId) +{ + if (timerId == 0) { + WIFI_LOGE("timerId is 0, no register timer"); + return; + } + + if (timer_ == nullptr) { + WIFI_LOGE("timer_ is nullptr"); + return; + } + + timer_->Unregister(timerId); + return; } -} \ No newline at end of file +} // namespace Wifi +} // namespace OHOS +#endif \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.h index d437004ce7cb4d25bde979a121ea2d1ef39b3fbd..7ea2459a834a8a2775e38ad2fe5afccbe59738b6 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.h @@ -16,6 +16,7 @@ #ifndef WIFI_SYSTEM_TIMER_H #define WIFI_SYSTEM_TIMER_H +#ifndef OHOS_ARCH_LITE #include #include #include @@ -23,6 +24,7 @@ #include #include "time_service_client.h" #include "itimer_info.h" +#include "timer.h" namespace OHOS { namespace Wifi { @@ -40,6 +42,24 @@ public: private: std::function callBack_ = nullptr; }; -} -} -#endif \ No newline at end of file + +class WifiTimer { +public: + using TimerCallback = std::function; + static constexpr uint32_t DEFAULT_TIMEROUT = 10000; + static WifiTimer *GetInstance(void); + + WifiTimer(); + ~WifiTimer(); + + bool Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT, + bool once = true); + void UnRegister(uint32_t timerId); + +private: + std::unique_ptr timer_{nullptr}; +}; +} // namespace Wifi +} // namespace OHOS +#endif +#endif // WIFI_SYSTEM_TIMER_H \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.cpp index fadcffd3c0003279f3b4a7169da816f7152a1be0..08a45220124193f707fa39541104a68398e09baf 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.cpp @@ -48,13 +48,11 @@ WifiSettings &WifiSettings::GetInstance() WifiSettings::WifiSettings() : mWifiStaCapabilities(0), -#ifndef OHOS_ARCH_LITE mWifiToggled(false), mWifiStoping(false), mSoftapToggled(false), mIsSupportCoex(false), mApIfaceName(DEFAULT_IFACENAME), -#endif mP2pState(static_cast(P2pState::P2P_STATE_CLOSED)), mP2pDiscoverState(0), mP2pConnectState(0), @@ -387,7 +385,6 @@ int WifiSettings::ClearScanInfoList() return 0; } -#ifndef OHOS_ARCH_LITE void WifiSettings::SetWifiToggledState(bool state) { std::unique_lock lock(mWifiToggledMutex); @@ -440,7 +437,6 @@ std::string WifiSettings::GetApIfaceName() const { return mApIfaceName; } -#endif int WifiSettings::GetScanInfoList(std::vector &results) { diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.h b/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.h index 2380993648b9814b64d948a546856c649a75a7b9..d857944d9dc52fd0a662d75366a01849921f3030 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.h @@ -127,7 +127,6 @@ public: */ int SetWifiState(int state, int instId = 0); -#ifndef OHOS_ARCH_LITE void SetWifiToggledState(bool state); bool GetWifiToggledState() const; void SetSoftapToggledState(bool state); @@ -138,7 +137,6 @@ public: bool GetCoexSupport() const; void SetApIfaceName(const std::string &ifaceName); std::string GetApIfaceName() const; -#endif /** * @Description Has STA service running @@ -1615,13 +1613,11 @@ private: private: int mWifiStaCapabilities; /* Sta capability */ std::map > mWifiState; /* Sta service state */ -#ifndef OHOS_ARCH_LITE bool mWifiToggled; bool mWifiStoping; bool mSoftapToggled; bool mIsSupportCoex; std::string mApIfaceName; -#endif std::vector mWifiScanInfoList; std::vector mGroupInfoList; std::vector mWifiStoreRandomMac; @@ -1684,11 +1680,9 @@ private: std::mutex mInfoMutex; std::mutex mP2pMutex; std::mutex mWifiConfigMutex; -#ifndef OHOS_ARCH_LITE std::mutex mWifiToggledMutex; std::mutex mWifiStopMutex; std::mutex mSoftapToggledMutex; -#endif std::atomic_flag deviceConfigLoadFlag = ATOMIC_FLAG_INIT; diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/include/wifi_internal_msg.h b/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/include/wifi_internal_msg.h index c109710bba01f503931c7ca33a33e5419774752e..cf92de71de82f2d79652c67fc6ebd6799c32927d 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/include/wifi_internal_msg.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_toolkit/include/wifi_internal_msg.h @@ -138,13 +138,6 @@ enum class Ant { NETWORK_ANT_INVALID = 16 }; -enum class AutoStartOrStopServiceReason { - AUTO_START_UPON_STARTUP = 0, /* auto-start upon startup */ - AIRPLANE_MODE, /* airplane mode */ - STA_AP_EXCLUSION, /* sta and ap exclusion */ - TRYTO_OPERATE_P2P_WHEN_STA_STATE_CHANGE, /* Try to operate p2p service again, when the sta state change */ -}; - struct InterScanInfo { std::string bssid; std::string ssid; diff --git a/wifi/test/fuzztest/wifi_sta/wifidevicestub_fuzzer/BUILD.gn b/wifi/test/fuzztest/wifi_sta/wifidevicestub_fuzzer/BUILD.gn index 2d3edd19bf0f344eba6d0b578e922c9c155f488a..af921a885c0fe2c2e8da989d55420295b50257b2 100644 --- a/wifi/test/fuzztest/wifi_sta/wifidevicestub_fuzzer/BUILD.gn +++ b/wifi/test/fuzztest/wifi_sta/wifidevicestub_fuzzer/BUILD.gn @@ -27,6 +27,7 @@ ohos_fuzztest("WifiDeviceStubFuzzTest") { "$WIFI_ROOT_DIR/frameworks/native/include", "$WIFI_ROOT_DIR/frameworks/native/interfaces", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper", "$WIFI_ROOT_DIR/test/fuzztest/fuzz_common_func", ] diff --git a/wifi/test/fuzztest/wifi_sta/wifihotspotstub_fuzzer/BUILD.gn b/wifi/test/fuzztest/wifi_sta/wifihotspotstub_fuzzer/BUILD.gn index ced8247c1cda0398586ba57580d604f06295d310..7fdfb1de83e9645f4d6dadaef0044fca636e3692 100644 --- a/wifi/test/fuzztest/wifi_sta/wifihotspotstub_fuzzer/BUILD.gn +++ b/wifi/test/fuzztest/wifi_sta/wifihotspotstub_fuzzer/BUILD.gn @@ -28,6 +28,7 @@ ohos_fuzztest("WifiHotSpotStubFuzzTest") { "$WIFI_ROOT_DIR/frameworks/native/interfaces", "$WIFI_ROOT_DIR/services/wifi_standard/include", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_ap_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/config", "$WIFI_ROOT_DIR/test/fuzztest/fuzz_common_func", diff --git a/wifi/test/fuzztest/wifi_sta/wifip2pstub_fuzzer/BUILD.gn b/wifi/test/fuzztest/wifi_sta/wifip2pstub_fuzzer/BUILD.gn index c14a92407addf277c9f54fd0f0b9fa8d84d57d2c..2419e6e4dd8acd2a686483e5d89a5b658c52a136 100644 --- a/wifi/test/fuzztest/wifi_sta/wifip2pstub_fuzzer/BUILD.gn +++ b/wifi/test/fuzztest/wifi_sta/wifip2pstub_fuzzer/BUILD.gn @@ -26,6 +26,7 @@ ohos_fuzztest("WifiP2pStubFuzzTest") { "$WIFI_ROOT_DIR/frameworks/native/include", "$WIFI_ROOT_DIR/frameworks/native/interfaces", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper", "$WIFI_ROOT_DIR/test/fuzztest/fuzz_common_func", ] diff --git a/wifi/test/fuzztest/wifi_sta/wifiscanstub_fuzzer/BUILD.gn b/wifi/test/fuzztest/wifi_sta/wifiscanstub_fuzzer/BUILD.gn index c2b7a29e0791d332b619380e79dc7c0382461ca7..1eedb98d14b1f10b8946b939d24a8d304a4ea38c 100644 --- a/wifi/test/fuzztest/wifi_sta/wifiscanstub_fuzzer/BUILD.gn +++ b/wifi/test/fuzztest/wifi_sta/wifiscanstub_fuzzer/BUILD.gn @@ -26,6 +26,7 @@ ohos_fuzztest("WifiScanStubFuzzTest") { "$WIFI_ROOT_DIR/frameworks/native/include", "$WIFI_ROOT_DIR/frameworks/native/interfaces", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan_sa", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper", "$WIFI_ROOT_DIR/test/fuzztest/fuzz_common_func", ] diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/BUILD.gn b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/BUILD.gn index 3478909c3be0767d7e8a0a24d883f64e4411750b..8a133be10feae7a249a58d11dc7e95b5e1c6d946 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/BUILD.gn +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/BUILD.gn @@ -33,13 +33,13 @@ ohos_unittest("manager_unittest") { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_datashare_utils.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_permission_helper.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common/wifi_permission_utils.cpp", - "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/concrete_clientmode_manager.cpp", - "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/concrete_manager_state_machine.cpp", - "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/softap_manager.cpp", - "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/softap_manager_state_machine.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_auth_center.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_config_center.cpp", - "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_clientmode_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/concrete_manager_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/softap_manager_state_machine.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller/wifi_controller_state_machine.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_country_code/wifi_country_code_manager.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_country_code/wifi_country_code_policy.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_dumper.cpp", @@ -51,6 +51,12 @@ ohos_unittest("manager_unittest") { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_protect.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_protect_manager.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_service_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_event_subscriber_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_hotspot_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_scan_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_sta_manager.cpp", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_toggler_manager.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_ability_listerner.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_system_timer.cpp", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/config/wifi_settings.cpp", @@ -74,6 +80,8 @@ ohos_unittest("manager_unittest") { "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_country_code", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/config", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/common", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_controller", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/include", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper", "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/utils", @@ -187,6 +195,8 @@ ohos_unittest("manager_unittest") { # Do not modify the permission configuration of the unit test defines += [ "PERMISSION_ALWAYS_GRANT" ] + defines += [ "HAS_POWERMGR_PART" ] + part_name = "wifi" subsystem_name = "communication" testonly = true diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_manager_test.cpp b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_manager_test.cpp index 1cb7041390de0d2d3e7f09c974b2591023e954e4..cc3d064d96a8bd14bf437168268ff50ebc43e168 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_manager_test.cpp +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_manager_test.cpp @@ -41,7 +41,10 @@ class WifiManagerTest : public testing::Test { public: static void SetUpTestCase() {} static void TearDownTestCase() {} - virtual void SetUp() {} + virtual void SetUp() + { + wifiManager.Init(); + } virtual void TearDown() {} public: WifiManager wifiManager; @@ -50,63 +53,57 @@ public: HWTEST_F(WifiManagerTest, StartUnloadStaSaTimerTest, TestSize.Level1) { WIFI_LOGE("StartUnloadStaSaTimerTest enter!"); - wifiManager.StartUnloadStaSaTimer(); + wifiManager.wifiStaManager->StartUnloadStaSaTimer(); } HWTEST_F(WifiManagerTest, StartUnloadScanSaTimerTest, TestSize.Level1) { WIFI_LOGE("StartUnloadScanSaTimerTest enter!"); - wifiManager.StartUnloadScanSaTimer(); + wifiManager.wifiScanManager->StartUnloadScanSaTimer(); } HWTEST_F(WifiManagerTest, StartUnloadApSaTimerTest, TestSize.Level1) { WIFI_LOGE("StartUnloadApSaTimerTest enter!"); - wifiManager.StartUnloadApSaTimer(); + wifiManager.wifiHotspotManager->StartUnloadApSaTimer(); } HWTEST_F(WifiManagerTest, AutoStartP2pService_001, TestSize.Level1) { WIFI_LOGE("AutoStartP2pService_001 enter!"); WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSING); - EXPECT_EQ(wifiManager.AutoStartP2pService( - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP), WIFI_OPT_OPEN_FAIL_WHEN_CLOSING); + EXPECT_EQ(wifiManager.wifiP2pManager->AutoStartP2pService(), WIFI_OPT_OPEN_FAIL_WHEN_CLOSING); WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::RUNNING); - EXPECT_EQ(wifiManager.AutoStartP2pService( - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP), WIFI_OPT_OPEN_SUCC_WHEN_OPENED); + EXPECT_EQ(wifiManager.wifiP2pManager->AutoStartP2pService(), WIFI_OPT_OPEN_SUCC_WHEN_OPENED); } HWTEST_F(WifiManagerTest, AutoStartP2pService_002, TestSize.Level1) { WIFI_LOGE("AutoStartP2pService_002 enter!"); WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSED); - EXPECT_EQ(wifiManager.AutoStartP2pService( - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP), WIFI_OPT_FAILED); + EXPECT_EQ(wifiManager.wifiP2pManager->AutoStartP2pService(), WIFI_OPT_FAILED); } HWTEST_F(WifiManagerTest, AutoStopP2pService_001, TestSize.Level1) { WIFI_LOGE("AutoStopP2pService_001 enter!"); WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING); - EXPECT_EQ(wifiManager.AutoStopP2pService( - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP), WIFI_OPT_CLOSE_FAIL_WHEN_OPENING); + EXPECT_EQ(wifiManager.wifiP2pManager->AutoStopP2pService(), WIFI_OPT_CLOSE_FAIL_WHEN_OPENING); WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::CLOSED); - EXPECT_EQ(wifiManager.AutoStopP2pService( - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP), WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED); + EXPECT_EQ(wifiManager.wifiP2pManager->AutoStopP2pService(), WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED); } HWTEST_F(WifiManagerTest, AutoStopP2pService_002, TestSize.Level1) { WIFI_LOGE("AutoStopP2pService_002 enter!"); WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::RUNNING); - EXPECT_EQ(wifiManager.AutoStopP2pService( - AutoStartOrStopServiceReason::AUTO_START_UPON_STARTUP), WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED); + EXPECT_EQ(wifiManager.wifiP2pManager->AutoStopP2pService(), WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED); } HWTEST_F(WifiManagerTest, StartUnloadP2PSaTimerTest, TestSize.Level1) { WIFI_LOGE("StartUnloadP2PSaTimerTest enter!"); - wifiManager.StartUnloadP2PSaTimer(); + wifiManager.wifiP2pManager->StartUnloadP2PSaTimer(); } HWTEST_F(WifiManagerTest, GetSupportedFeaturesTest, TestSize.Level1) @@ -121,132 +118,106 @@ HWTEST_F(WifiManagerTest, GetSupportedFeaturesTest, TestSize.Level1) HWTEST_F(WifiManagerTest, GetAirplaneModeByDatashareTest, TestSize.Level1) { WIFI_LOGE("DealOpenAirplGetAirplaneModeByDatashareTestaneModeEventTest enter!"); - wifiManager.GetAirplaneModeByDatashare(); -} - -HWTEST_F(WifiManagerTest, DealOpenAirplaneModeEventTest, TestSize.Level1) -{ - WIFI_LOGE("DealOpenAirplaneModeEventTest enter!"); - wifiManager.DealOpenAirplaneModeEvent(); -} - -HWTEST_F(WifiManagerTest, DealCloseAirplaneModeEvent_001, TestSize.Level1) -{ - WIFI_LOGE("DealCloseAirplaneModeEvent_001 enter!"); - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::USER_CLOSE_WIFI_IN_AIRPLANEMODE)); - WifiConfigCenter::GetInstance().SetStaLastRunState(false); - wifiManager.unloadP2PSaTimerId = 0; - WifiSettings::GetInstance().SetHotspotState(static_cast(ApState::AP_STATE_CLOSED), 0); - wifiManager.DealCloseAirplaneModeEvent(); -} - -HWTEST_F(WifiManagerTest, DealCloseAirplaneModeEvent_002, TestSize.Level1) -{ - WIFI_LOGE("DealCloseAirplaneModeEvent_002 enter!"); - WifiConfigCenter::GetInstance().SetOperatorWifiType( - static_cast(OperatorWifiType::CLOSE_WIFI_DUE_TO_AIRPLANEMODE_OPENED)); - WifiConfigCenter::GetInstance().SetStaLastRunState(false); - wifiManager.DealCloseAirplaneModeEvent(); + wifiManager.wifiEventSubscriberManager->GetAirplaneModeByDatashare(); } HWTEST_F(WifiManagerTest, GetLocationModeByDatashareTest, TestSize.Level1) { WIFI_LOGE("GetLocationModeByDatashareTest enter!"); - bool result = wifiManager.GetLocationModeByDatashare(); + bool result = wifiManager.wifiEventSubscriberManager->GetLocationModeByDatashare(); WIFI_LOGE("GetLocationModeByDatashareTest result(%{public}d)", result); } HWTEST_F(WifiManagerTest, GetLastStaStateByDatashareTest, TestSize.Level1) { WIFI_LOGE("GetLastStaStateByDatashareTest enter!"); - wifiManager.GetLastStaStateByDatashare(); + wifiManager.wifiEventSubscriberManager->GetLastStaStateByDatashare(); } HWTEST_F(WifiManagerTest, CheckAndStartScanService_001, TestSize.Level1) { WIFI_LOGE("CheckAndStartScanService_001 enter!"); WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::RUNNING); - wifiManager.CheckAndStartScanService(); + wifiManager.wifiScanManager->CheckAndStartScanService(); } HWTEST_F(WifiManagerTest, CheckAndStartScanService_002, TestSize.Level1) { WIFI_LOGE("CheckAndStartScanService_002 enter!"); WifiConfigCenter::GetInstance().SetScanMidState(WifiOprMidState::CLOSED); - wifiManager.CheckAndStartScanService(); + wifiManager.wifiScanManager->CheckAndStartScanService(); } HWTEST_F(WifiManagerTest, RegisterScreenEventTest, TestSize.Level1) { WIFI_LOGE("RegisterScreenEventTest enter!"); - wifiManager.RegisterScreenEvent(); + wifiManager.wifiEventSubscriberManager->RegisterScreenEvent(); } HWTEST_F(WifiManagerTest, UnRegisterScreenEventTest, TestSize.Level1) { WIFI_LOGE("UnRegisterScreenEventTest enter!"); - wifiManager.UnRegisterScreenEvent(); + wifiManager.wifiEventSubscriberManager->UnRegisterScreenEvent(); } HWTEST_F(WifiManagerTest, RegisterSettingsMigrateEventTest, TestSize.Level1) { WIFI_LOGE("RegisterSettingsMigrateEventTest enter!"); - wifiManager.RegisterSettingsMigrateEvent(); + wifiManager.wifiEventSubscriberManager->RegisterSettingsMigrateEvent(); } HWTEST_F(WifiManagerTest, UnRegisterSettingsMigrateEventTest, TestSize.Level1) { WIFI_LOGE("UnRegisterSettingsMigrateEventTest enter!"); - wifiManager.UnRegisterSettingsMigrateEvent(); + wifiManager.wifiEventSubscriberManager->UnRegisterSettingsMigrateEvent(); } HWTEST_F(WifiManagerTest, RegisterAirplaneModeEventTest, TestSize.Level1) { WIFI_LOGE("RegisterAirplaneModeEventTest enter!"); - wifiManager.RegisterAirplaneModeEvent(); + wifiManager.wifiEventSubscriberManager->RegisterAirplaneModeEvent(); } HWTEST_F(WifiManagerTest, UnRegisterAirplaneModeEventTest, TestSize.Level1) { WIFI_LOGE("UnRegisterAirplaneModeEventTest enter!"); - wifiManager.UnRegisterAirplaneModeEvent(); + wifiManager.wifiEventSubscriberManager->UnRegisterAirplaneModeEvent(); } HWTEST_F(WifiManagerTest, RegisterLocationEventTest, TestSize.Level1) { WIFI_LOGE("RegisterLocationEventTest enter!"); - wifiManager.RegisterLocationEvent(); + wifiManager.wifiEventSubscriberManager->RegisterLocationEvent(); } HWTEST_F(WifiManagerTest, UnRegisterLocationEventTest, TestSize.Level1) { WIFI_LOGE("UnRegisterLocationEventTest enter!"); - wifiManager.UnRegisterLocationEvent(); + wifiManager.wifiEventSubscriberManager->UnRegisterLocationEvent(); } HWTEST_F(WifiManagerTest, RegisterBatteryEventTest, TestSize.Level1) { WIFI_LOGE("RegisterBatteryEventTest enter!"); - wifiManager.RegisterBatteryEvent(); + wifiManager.wifiEventSubscriberManager->RegisterBatteryEvent(); } HWTEST_F(WifiManagerTest, UnRegisterBatteryEventTest, TestSize.Level1) { WIFI_LOGE("UnRegisterBatteryEventTest enter!"); - wifiManager.UnRegisterBatteryEvent(); + wifiManager.wifiEventSubscriberManager->UnRegisterBatteryEvent(); } HWTEST_F(WifiManagerTest, RegisterPowerStateListenerTest, TestSize.Level1) { WIFI_LOGE("RegisterPowerStateListenerTest enter!"); - wifiManager.RegisterPowerStateListener(); + wifiManager.wifiEventSubscriberManager->RegisterPowerStateListener(); } HWTEST_F(WifiManagerTest, UnRegisterPowerStateListenerTest, TestSize.Level1) { WIFI_LOGE("UnRegisterPowerStateListenerTest enter!"); - wifiManager.UnRegisterPowerStateListener(); + wifiManager.wifiEventSubscriberManager->UnRegisterPowerStateListener(); } HWTEST_F(WifiManagerTest, ExitTest, TestSize.Level1) @@ -264,25 +235,19 @@ HWTEST_F(WifiManagerTest, PushServiceCloseMsgTest, TestSize.Level1) HWTEST_F(WifiManagerTest, GetDeviceProvisionByDatashareTest, TestSize.Level1) { WIFI_LOGE("ExitTest enter!"); - wifiManager.GetDeviceProvisionByDatashare(); + wifiManager.wifiEventSubscriberManager->GetDeviceProvisionByDatashare(); } HWTEST_F(WifiManagerTest, CheckAndStopScanServiceTest, TestSize.Level1) { WIFI_LOGE("ExitTest enter!"); - wifiManager.CheckAndStopScanService(); + wifiManager.wifiScanManager->CheckAndStopScanService(); } HWTEST_F(WifiManagerTest, ForceStopWifiTest, TestSize.Level1) { WIFI_LOGE("ExitTest enter!"); - wifiManager.ForceStopWifi(); -} - -HWTEST_F(WifiManagerTest, AutoStartScanServiceTest, TestSize.Level1) -{ - WIFI_LOGE("ExitTest enter!"); - wifiManager.AutoStartScanService(); + wifiManager.wifiStaManager->ForceStopWifi(); } HWTEST_F(WifiManagerTest, AutoStartEnhanceServiceTest, TestSize.Level1) diff --git a/wifi/utils/inc/wifi_sa_manager.h b/wifi/utils/inc/wifi_sa_manager.h index fa2b47b8d957a6cb7294e167315d67952914e04b..572d3abc80d626173148c09a959d7d7386e71c8f 100644 --- a/wifi/utils/inc/wifi_sa_manager.h +++ b/wifi/utils/inc/wifi_sa_manager.h @@ -24,6 +24,8 @@ namespace OHOS { namespace Wifi { +constexpr uint32_t TIMEOUT_UNLOAD_WIFI_SA = 5 * 60 * 1000; + class WifiSaLoadCallback : public SystemAbilityLoadCallbackStub { public: void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override;