1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef WIFI_NET_AGENT_H 17 #define WIFI_NET_AGENT_H 18 19 #include <memory> 20 #include <string> 21 #include <singleton.h> 22 #include <utility> 23 #include <vector> 24 25 #include "i_net_conn_service.h" 26 #include "net_all_capabilities.h" 27 #include "net_supplier_callback_base.h" 28 #include "wifi_event_handler.h" 29 #include "wifi_internal_msg.h" 30 #include "sta_service_callback.h" 31 #include "wifi_log.h" 32 #include "net_manager_constants.h" 33 namespace OHOS { 34 namespace Wifi { 35 struct WifiNetAgentCallbacks { 36 std::function<bool(const int uid, const int networkId)> OnRequestNetwork; 37 }; 38 39 class WifiNetAgent { 40 public: 41 ~WifiNetAgent(); 42 static WifiNetAgent &GetInstance(); 43 /** 44 * Register the network information with the network management module 45 * 46 * @return true if register success else return false; 47 */ 48 bool RegisterNetSupplier(); 49 /** 50 * Register the network callback with the network management module 51 * 52 * @return true if register success else return false; 53 */ 54 bool RegisterNetSupplierCallback(); 55 56 /** 57 * Cancel the registration information to the network management 58 */ 59 void UnregisterNetSupplier(); 60 61 /** 62 * Update network information 63 * 64 * @param supplierId network unique identity id returned after network registration 65 * @param netSupplierInfo network data information 66 */ 67 void UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo); 68 69 /** 70 * Update link information 71 * 72 * @param wifiIpInfo wifi network link data information 73 * @param wifiIpV6Info wifi network link IPV6 data information 74 * @param wifiProxyConfig wifi network link proxy information 75 */ 76 void UpdateNetLinkInfo(IpInfo &wifiIpInfo, IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig, int instId = 0); 77 78 /** 79 * Add route 80 * 81 * @param interface interface name 82 * @param ipAddress IP address 83 * @param prefixLength prefix length 84 */ 85 bool AddRoute(const std::string interface, const std::string ipAddress, int prefixLength); 86 87 /** 88 * Delete interface address 89 * 90 * @param interface interface name 91 * @param ipAddress IP address 92 * @param prefixLength prefix length 93 */ 94 bool DelInterfaceAddress(const std::string &interface, const std::string &ipAddress, int prefixLength); 95 96 /** 97 * Add OnStaMachineUpdateNetLinkInfo 98 * 99 * @param wifiIpInfo wifi network link data information 100 * @param wifiIpV6Info wifi ipv6 network link data information 101 * @param wifiProxyConfig wifi network link proxy information 102 */ 103 void OnStaMachineUpdateNetLinkInfo(IpInfo wifiIpInfo, IpV6Info wifiIpV6Info, WifiProxyConfig wifiProxyConfig, 104 int instId = 0); 105 106 /** 107 * Add OnStaMachineUpdateNetSupplierInfo 108 * 109 * @param netSupplierInfo net Supplier Info 110 */ 111 void OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo); 112 113 /** 114 * Add OnStaMachineWifiStart 115 * 116 * @param 117 */ 118 void OnStaMachineWifiStart(); 119 120 /** 121 * Add OnStaMachineNetManagerRestart 122 * 123 * @param netSupplierInfo net Supplier Info 124 */ 125 void OnStaMachineNetManagerRestart(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo, 126 int instId = 0); 127 128 /** 129 * Restart the Wi-Fi connection. 130 * 131 * @param 132 */ 133 void RestoreWifiConnection(); 134 135 /** 136 * Init WifiNetAgentCallbacks 137 * 138 * @param WifiNetAgentCallbacks WifiNetAgent callback 139 */ 140 void InitWifiNetAgent(const WifiNetAgentCallbacks &wifiNetAgentCallbacks); 141 142 /** 143 * Add RequestNetwork 144 * 145 * @param uid uid 146 * @param networkId deviceconfig networkId 147 */ 148 bool RequestNetwork(const int uid, const int networkId); 149 150 /** 151 * return wifi supplierId 152 */ 153 uint32_t GetSupplierId(); 154 155 /** 156 * set wifi supplierId to 0 157 */ 158 void ResetSupplierId(); 159 160 public: 161 class NetConnCallback : public NetManagerStandard::NetSupplierCallbackBase { 162 public: 163 /** 164 * @Description : Construct a new NetConn object 165 * 166 */ 167 explicit NetConnCallback(); 168 169 /** 170 * @Description : Destroy the NetConn object 171 * 172 */ 173 ~NetConnCallback() override; 174 175 /** 176 * @Description : Connection Management triggers the open automatic connection feature. 177 * 178 * @param ident - identity 179 * @param netCaps - Net capability to request a network 180 * @param registerType - Inner API or outer API 181 * 182 */ 183 int32_t RequestNetwork( 184 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps, 185 const NetManagerStandard::NetRequest &netrequest) override; 186 /** 187 * @Description : Connection Management triggers the close automatic connection feature. 188 * 189 * @param ident - identity 190 * @param netCaps - Net capability to request a network 191 */ 192 int32_t ReleaseNetwork(const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) override; 193 private: 194 void LogNetCaps(const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) const; 195 196 std::unordered_set<int> requestIds_; 197 }; 198 private: 199 WifiNetAgent(); 200 void CreateNetLinkInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 201 IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig, int instId = 0); 202 203 void SetNetLinkIPInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 204 IpV6Info &wifiIpV6Info); 205 206 void SetNetLinkRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 207 IpV6Info &wifiIpV6Info); 208 209 void SetNetLinkHostRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo); 210 211 void SetNetLinkLocalRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 212 IpV6Info &wifiIpV6Info); 213 214 void SetNetLinkDnsInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 215 IpV6Info &wifiIpV6Info); 216 private: 217 uint32_t supplierId{0}; 218 bool isWifiAvaliable_ = false; 219 WifiNetAgentCallbacks wifiNetAgentCallbacks_; 220 std::unique_ptr<WifiEventHandler> netAgentEventHandler_ = nullptr; 221 }; 222 } // namespace Wifi 223 } // namespace OHOS 224 #endif // CELLULAR_DATA_NET_AGENT_H 225