From 02cf68cb09cf8cea6d0bbac3a2b8cc072052ac86 Mon Sep 17 00:00:00 2001 From: zhangfeng Date: Fri, 11 Feb 2022 09:27:05 +0000 Subject: [PATCH] feat(*): Add net manager interface for wifi Signed-off-by: zhangfeng --- bundle.json | 3 +- .../wifi_manage/wifi_manager.cpp | 2 +- .../wifi_manage/wifi_scan/scan_service.cpp | 2 +- .../wifi_manage/wifi_sta/BUILD.gn | 7 +- .../wifi_sta/sta_service_callback.h | 2 +- .../wifi_sta/sta_state_machine.cpp | 23 +++ .../wifi_manage/wifi_sta/sta_state_machine.h | 2 + .../wifi_manage/wifi_sta/wifi_net_agent.cpp | 179 ++++++++++++++++++ .../wifi_manage/wifi_sta/wifi_net_agent.h | 114 +++++++++++ .../wifi_manage/wifi_sta/BUILD.gn | 10 +- 10 files changed, 335 insertions(+), 9 deletions(-) create mode 100644 services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.cpp create mode 100644 services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.h diff --git a/bundle.json b/bundle.json index 398150025..bfcc7273c 100755 --- a/bundle.json +++ b/bundle.json @@ -50,7 +50,8 @@ "components": [ "ipc", "ces_standard", - "hiviewdfx_hilog_native" + "hiviewdfx_hilog_native", + "netmanager_base" ], "third_party": [ "wpa_supplicant", diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp index 61f2263a9..ca6517029 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan/scan_service.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan/scan_service.cpp index e8cd6720b..fd3b84dc9 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan/scan_service.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_scan/scan_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn index 3a18bc5a6..e36c7bb61 100644 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("wifi_sta_service") { "sta_saved_device_appraisal.cpp", "sta_service.cpp", "sta_state_machine.cpp", + "wifi_net_agent.cpp", ] include_dirs = [ "//foundation/communication/wifi/services/wifi_standard/sdk/include", @@ -59,6 +60,7 @@ ohos_shared_library("wifi_sta_service") { "//foundation/communication/wifi/services/wifi_standard/depends/include/system/libbase/include", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/dhcp_manage/mgr_service/include", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/dhcp_manage/mgr_service/interfaces", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", ] cflags_cc = [ @@ -78,7 +80,10 @@ ohos_shared_library("wifi_sta_service") { "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "netmanager_base:net_conn_manager_if", + ] part_name = "wifi_standard" subsystem_name = "communication" diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service_callback.h b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service_callback.h index 950e8dc58..257e9d614 100644 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service_callback.h +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service_callback.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp index 4161befd5..68c12751d 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp @@ -130,6 +130,7 @@ ErrCode StaStateMachine::InitStaStateMachine() return WIFI_OPT_FAILED; } pNetcheck->InitNetCheckThread(); + NetSupplierInfo = std::make_unique().release(); return WIFI_OPT_SUCCESS; } @@ -387,6 +388,8 @@ void StaStateMachine::StartWifiProcess() /* callback the InterfaceService that wifi is enabled successfully. */ WifiSettings::GetInstance().SetWifiState(static_cast(WifiState::ENABLED)); staCallback.OnStaOpenRes(OperateResState::OPEN_WIFI_SUCCEED); + WifiNetAgent::GetInstance().RegisterNetSupplier(); + WifiNetAgent::GetInstance().RegisterNetSupplierCallback(staCallback); /* Initialize Connection Information. */ InitWifiLinkedInfo(); InitLastWifiLinkedInfo(); @@ -498,6 +501,7 @@ bool StaStateMachine::WpaStartedState::ExecuteStateMsg(InternalMessage *msg) void StaStateMachine::StopWifiProcess() { WIFI_LOGD("Enter StaStateMachine::StopWifiProcess.\n"); + WifiNetAgent::GetInstance().UnregisterNetSupplier(); WifiSettings::GetInstance().SetWifiState(static_cast(WifiState::DISABLING)); staCallback.OnStaCloseRes(OperateResState::CLOSE_WIFI_CLOSING); StopTimer(static_cast(CMD_SIGNAL_POLL)); @@ -662,6 +666,13 @@ void StaStateMachine::DealSignalPollResult(InternalMessage *msg) if (staCallback.OnStaRssiLevelChanged != nullptr) { staCallback.OnStaRssiLevelChanged(linkedInfo.rssi); } + if (NetSupplierInfo != nullptr) { + NetSupplierInfo->isAvailable_ = true; + NetSupplierInfo->isRoaming_ = isRoam; + NetSupplierInfo->strength_ = linkedInfo.rssi; + NetSupplierInfo->frequency_ = linkedInfo.frequency; + WifiNetAgent::GetInstance().UpdateNetSupplierInfo(NetSupplierInfo); + } lastSignalLevel = currentSignalLevel; } } else { @@ -780,6 +791,12 @@ void StaStateMachine::DealConnectionEvent(InternalMessage *msg) wpsState = SetupMethod::INVALID; } + if (NetSupplierInfo != nullptr) { + NetSupplierInfo->isAvailable_ = true; + NetSupplierInfo->isRoaming_ = isRoam; + WifiNetAgent::GetInstance().UpdateNetSupplierInfo(NetSupplierInfo); + } + /* Callback result to InterfaceService. */ staCallback.OnStaConnChanged(OperateResState::CONNECT_OBTAINING_IP, linkedInfo); @@ -1383,6 +1400,10 @@ void StaStateMachine::DisConnectProcess() staCallback.OnStaConnChanged(OperateResState::DISCONNECT_DISCONNECTING, linkedInfo); if (WifiStaHalInterface::GetInstance().Disconnect() == WIFI_IDL_OPT_OK) { WIFI_LOGI("Disconnect() succeed!"); + if (NetSupplierInfo != nullptr) { + NetSupplierInfo->isAvailable_ = false; + WifiNetAgent::GetInstance().UpdateNetSupplierInfo(NetSupplierInfo); + } /* Save connection information to WifiSettings. */ SaveLinkstate(ConnState::DISCONNECTED, DetailedState::DISCONNECTED); DisableNetwork(linkedInfo.networkId); @@ -1894,6 +1915,8 @@ void StaStateMachine::DhcpResultNotify::OnSuccess(int status, const std::string pStaStateMachine->linkedInfo.isDataRestricted = (result.strVendor.find("ANDROID_METERED") == std::string::npos) ? 0 : 1; WifiSettings::GetInstance().SaveLinkedInfo(pStaStateMachine->linkedInfo); + WifiNetAgent::GetInstance().UpdateNetLinkInfo(result.strYourCli, result.strSubnet, result.strRouter1, + result.strDns1, result.strDns2); } IfConfig::GetInstance().SetIfDnsAndRoute(result, result.iptype); diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h index b877dc27c..969fefeb6 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h @@ -29,6 +29,7 @@ #include "sta_service_callback.h" #include "i_dhcp_service.h" #include "sta_define.h" +#include "wifi_net_agent.h" namespace OHOS { namespace Wifi { @@ -625,6 +626,7 @@ private: private: StaSmHandleFuncMap staSmHandleFuncMap; StaServiceCallback staCallback; + sptr NetSupplierInfo; int lastNetworkId; int operationalMode; diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.cpp new file mode 100644 index 000000000..7f8d4bd2c --- /dev/null +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2022 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_net_agent.h" + +#include + +#include "wifi_logger.h" + +#include "net_conn_client.h" +#include "inet_addr.h" + +DEFINE_WIFILOG_LABEL("WifiNetAgent"); + +namespace OHOS { +namespace Wifi { +using namespace NetManagerStandard; + +WifiNetAgent::WifiNetAgent() = default; +WifiNetAgent::~WifiNetAgent() = default; + +bool WifiNetAgent::RegisterNetSupplier() +{ + WIFI_LOGI("Enter RegisterNetSupplier."); + auto netManager = DelayedSingleton::GetInstance(); + if (netManager == nullptr) { + WIFI_LOGE("NetConnClient is null"); + return false; + } + std::string ident = "wifi"; + using NetManagerStandard::NetBearType; + using NetManagerStandard::NetCap; + std::set netCaps {NetCap::NET_CAPABILITY_INTERNET}; + int32_t result = netManager->RegisterNetSupplier(NetBearType::BEARER_WIFI, ident, netCaps, supplierId); + if (result == ERR_NONE) { + WIFI_LOGI("Register NetSupplier successful"); + return true; + } + WIFI_LOGI("Register NetSupplier failed"); + return false; +} + +bool WifiNetAgent::RegisterNetSupplierCallback(const StaServiceCallback &callback) +{ + WIFI_LOGI("Enter RegisterNetSupplierCallback."); + auto netManager = DelayedSingleton::GetInstance(); + if (netManager == nullptr) { + WIFI_LOGE("NetConnClient is null"); + return false; + } + + sptr pNetConnCallback = (std::make_unique(callback)).release(); + if (pNetConnCallback == nullptr) { + WIFI_LOGE("pNetConnCallback is null\n"); + return false; + } + + int32_t result = netManager->RegisterNetSupplierCallback(supplierId, pNetConnCallback); + if (result == ERR_NONE) { + WIFI_LOGI("Register NetSupplierCallback successful"); + return true; + } + WIFI_LOGE("Register NetSupplierCallback failed [%{public}d]", result); + return false; +} + +void WifiNetAgent::UnregisterNetSupplier() +{ + WIFI_LOGI("Enter UnregisterNetSupplier."); + auto netManager = DelayedSingleton::GetInstance(); + if (netManager == nullptr) { + WIFI_LOGE("NetConnClient is null"); + return; + } + int32_t result = netManager->UnregisterNetSupplier(supplierId); + WIFI_LOGI("Unregister network result:%{public}d", result); +} + +void WifiNetAgent::UpdateNetSupplierInfo(sptr &netSupplierInfo) +{ + WIFI_LOGI("Enter UpdateNetSupplierInfo."); + auto netManager = DelayedSingleton::GetInstance(); + if (netManager == nullptr) { + WIFI_LOGE("NetConnClient is null"); + return; + } + + int32_t result = netManager->UpdateNetSupplierInfo(supplierId, netSupplierInfo); + WIFI_LOGI("Update network result:%{public}d", result); +} + +void WifiNetAgent::UpdateNetLinkInfo(std::string &ip, std::string &mask, std::string &gateWay, + std::string &strDns, std::string &strBakDns) +{ + WIFI_LOGI("Enter UpdateNetLinkInfo."); + auto netManager = DelayedSingleton::GetInstance(); + if (netManager == nullptr) { + WIFI_LOGE("NetConnClient is null"); + return; + } + + sptr netLinkInfo = (std::make_unique()).release(); + netLinkInfo->ifaceName_ = "wlan0"; + + sptr netAddr = (std::make_unique()).release(); + netAddr->type_ = NetManagerStandard::INetAddr::IPV4; + netAddr->address_ = ip; + netAddr->netMask_ = mask; + netLinkInfo->netAddrList_.push_back(*netAddr); + + sptr dns = (std::make_unique()).release(); + dns->type_ = NetManagerStandard::INetAddr::IPV4; + dns->address_ = strDns; + netLinkInfo->dnsList_.push_back(*dns); + dns->address_ = strBakDns; + netLinkInfo->dnsList_.push_back(*dns); + + sptr route = (std::make_unique()).release(); + route->iface_ = "wlan0"; + route->destination_.type_ = NetManagerStandard::INetAddr::IPV4; + route->destination_.address_ = gateWay; + netLinkInfo->routeList_.push_back(*route); + + int32_t result = netManager->UpdateNetLinkInfo(supplierId, netLinkInfo); + WIFI_LOGI("result:%{public}d", result); +} + +WifiNetAgent::NetConnCallback::NetConnCallback(const StaServiceCallback &callback) +{ + staCallback = callback; +} + +WifiNetAgent::NetConnCallback::~NetConnCallback() +{} + +int32_t WifiNetAgent::NetConnCallback::RequestNetwork( + const std::string &ident, const std::set &netCaps) +{ + WIFI_LOGD("Enter NetConnCallback::RequestNetwork"); + LogNetCaps(ident, netCaps); + return 0; +} + +int32_t WifiNetAgent::NetConnCallback::ReleaseNetwork( + const std::string &ident, const std::set &netCaps) +{ + WIFI_LOGD("Enter NetConnCallback::ReleaseNetwork"); + LogNetCaps(ident, netCaps); + return 0; +} + +void WifiNetAgent::NetConnCallback::LogNetCaps( + const std::string &ident, const std::set &netCaps) const +{ + WIFI_LOGD("ident=[%s]", ident.c_str()); + std::string logStr; + const std::string logStrEnd("]"); + logStr = "netCaps["; + for (auto netCap : netCaps) { + logStr += std::to_string(static_cast(netCap)); + logStr += " "; + } + logStr += logStrEnd; + WIFI_LOGD("%{public}s", logStr.c_str()); +} +} +} diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.h b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.h new file mode 100644 index 000000000..e574e8544 --- /dev/null +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.h @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2022 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 WIFI_NET_AGENT_H +#define WIFI_NET_AGENT_H + +#include +#include +#include +#include +#include + +#include "i_net_conn_service.h" +#include "net_all_capabilities.h" +#include "net_supplier_callback_base.h" + +#include "wifi_internal_msg.h" +#include "sta_service_callback.h" +#include "wifi_log.h" + +namespace OHOS { +namespace Wifi { +class WifiNetAgent : public DelayedRefSingleton { + DECLARE_DELAYED_REF_SINGLETON(WifiNetAgent); + +public: + /** + * Register the network information with the network management module + * + * @return true if register success else return false; + */ + bool RegisterNetSupplier(); + /** + * Register the network callback with the network management module + * + * @return true if register success else return false; + */ + bool RegisterNetSupplierCallback(const StaServiceCallback &callback); + + /** + * Cancel the registration information to the network management + */ + void UnregisterNetSupplier(); + + /** + * Update network information + * + * @param supplierId network unique identity id returned after network registration + * @param netSupplierInfo network data information + */ + void UpdateNetSupplierInfo(sptr &netSupplierInfo); + + /** + * Update link information + * + * @param supplierId network unique identity id returned after network registration + * @param netLinkInfo network link data information + */ + void UpdateNetLinkInfo(std::string &ip, std::string &mask, std::string &gateWay, + std::string &strDns, std::string &strBakDns); + +public: + class NetConnCallback : public NetManagerStandard::NetSupplierCallbackBase { + public: + /** + * @Description : Construct a new NetConn object + * + */ + explicit NetConnCallback(const StaServiceCallback &callback); + + /** + * @Description : Destroy the NetConn object + * + */ + ~NetConnCallback() override; + + /** + * @Description : Connection Management triggers the open automatic connection feature. + * + * @param ident - identity + * @param netCaps - Net capability to request a network + */ + int32_t RequestNetwork(const std::string &ident, const std::set &netCaps) override; + /** + * @Description : Connection Management triggers the close automatic connection feature. + * + * @param ident - identity + * @param netCaps - Net capability to request a network + */ + int32_t ReleaseNetwork(const std::string &ident, const std::set &netCaps) override; + private: + void LogNetCaps(const std::string &ident, const std::set &netCaps) const; + private: + StaServiceCallback staCallback; + }; + +private: + uint32_t supplierId; +}; +} // namespace Wifi +} // namespace OHOS +#endif // CELLULAR_DATA_NET_AGENT_H diff --git a/tests/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn b/tests/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn index 223070739..2322c68ba 100644 --- a/tests/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn +++ b/tests/wifi_standard/wifi_framework/wifi_manage/wifi_sta/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Huawei Device Co., Ltd. +# Copyright (C) 2021-2022 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 @@ -38,14 +38,13 @@ ohos_unittest("wifi_sta_unittest") { "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/common/state.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/common/state_machine.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_auto_connect_service.cpp", - - #"//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_extern_device_appraisal.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_interface.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_monitor.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_network_check.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_saved_device_appraisal.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_service.cpp", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp", + "//foundation/communication/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/wifi_net_agent.cpp", "Mock/mock_dhcp_service.cpp", "Mock/mock_if_config.cpp", "Mock/mock_mac_address.cpp", @@ -117,7 +116,10 @@ ohos_unittest("wifi_sta_unittest") { "//third_party/googletest:gmock_main", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "netmanager_base:net_conn_manager_if", + ] part_name = "wifi_standard" subsystem_name = "communication" } -- Gitee