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 #include <mutex> 25 26 #include "define.h" 27 #include "i_net_conn_service.h" 28 #include "net_all_capabilities.h" 29 #include "net_supplier_callback_base.h" 30 #include "wifi_event_handler.h" 31 #include "wifi_internal_msg.h" 32 #include "sta_service_callback.h" 33 #include "wifi_log.h" 34 #include "net_manager_constants.h" 35 #include "net_conn_callback_stub.h" 36 namespace OHOS { 37 namespace Wifi { 38 struct WifiNetAgentCallbacks { 39 std::function<bool(const int uid, const int networkId)> OnRequestNetwork; 40 }; 41 42 class WifiNetAgent { 43 public: 44 ~WifiNetAgent(); 45 static WifiNetAgent &GetInstance(); 46 /** 47 * Register the network information with the network management module 48 * 49 * @return true if register success else return false; 50 */ 51 bool RegisterNetSupplier(int instId); 52 /** 53 * Register the network callback with the network management module 54 * 55 * @return true if register success else return false; 56 */ 57 bool RegisterNetSupplierCallback(int instId); 58 59 /** 60 * Cancel the registration information to the network management 61 */ 62 void UnregisterNetSupplier(int instId); 63 64 /** 65 * Update network information 66 * 67 * @param supplierId network unique identity id returned after network registration 68 * @param netSupplierInfo network data information 69 */ 70 void UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo, int instId); 71 72 /** 73 * Add route 74 * 75 * @param interface interface name 76 * @param ipAddress IP address 77 * @param prefixLength prefix length 78 */ 79 bool AddRoute(const std::string interface, const std::string ipAddress, int prefixLength); 80 81 /** 82 * Delete interface address 83 * 84 * @param interface interface name 85 * @param ipAddress IP address 86 * @param prefixLength prefix length 87 */ 88 bool DelInterfaceAddress(const std::string &interface, const std::string &ipAddress, int prefixLength); 89 90 /** 91 * Add OnStaMachineUpdateNetLinkInfo 92 * 93 * @param wifiIpInfo wifi network link data information 94 * @param wifiIpV6Info wifi ipv6 network link data information 95 * @param wifiProxyConfig wifi network link proxy information 96 */ 97 void OnStaMachineUpdateNetLinkInfo(IpInfo wifiIpInfo, IpV6Info wifiIpV6Info, WifiProxyConfig wifiProxyConfig, 98 int instId = 0); 99 100 /** 101 * Add OnStaMachineUpdateNetSupplierInfo 102 * 103 * @param netSupplierInfo net Supplier Info 104 */ 105 void OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo, int instId); 106 107 /** 108 * Add OnStaMachineWifiStart 109 * 110 * @param 111 */ 112 void OnStaMachineWifiStart(int instId); 113 114 /** 115 * Register network connect call back 116 * 117 * @return true if register success else return false; 118 */ 119 bool RegisterNetConnObserver(int instId); 120 121 /** 122 * Add OnStaMachineNetManagerRestart 123 * 124 * @param netSupplierInfo net Supplier Info 125 */ 126 void OnStaMachineNetManagerRestart(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo, 127 int instId = 0); 128 129 /** 130 * Restart the Wi-Fi connection. 131 * 132 * @param 133 */ 134 void RestoreWifiConnection(); 135 136 /** 137 * Init WifiNetAgentCallbacks 138 * 139 * @param WifiNetAgentCallbacks WifiNetAgent callback 140 */ 141 void InitWifiNetAgent(const WifiNetAgentCallbacks &wifiNetAgentCallbacks); 142 143 /** 144 * Add RequestNetwork 145 * 146 * @param uid uid 147 * @param networkId deviceconfig networkId 148 */ 149 bool RequestNetwork(const int uid, const int networkId); 150 151 /** 152 * return wifi supplierId 153 */ 154 uint32_t GetSupplierId(); 155 156 /** 157 * set wifi supplierId to 0 158 */ 159 void ResetSupplierId(); 160 161 public: 162 class NetConnCallback : public NetManagerStandard::NetSupplierCallbackBase { 163 public: 164 /** 165 * @Description : Construct a new NetConn object 166 * 167 */ 168 explicit NetConnCallback(); 169 170 /** 171 * @Description : Destroy the NetConn object 172 * 173 */ 174 ~NetConnCallback() override; 175 176 /** 177 * @Description : Connection Management triggers the open automatic connection feature. 178 * 179 * @param ident - identity 180 * @param netCaps - Net capability to request a network 181 * @param registerType - Inner API or outer API 182 * 183 */ 184 int32_t RequestNetwork( 185 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps, 186 const NetManagerStandard::NetRequest &netrequest) override; 187 /** 188 * @Description : Connection Management triggers the close automatic connection feature. 189 * 190 * @param ident - identity 191 * @param netCaps - Net capability to request a network 192 */ 193 int32_t ReleaseNetwork(const NetManagerStandard::NetRequest &netrequest) override; 194 private: 195 void LogNetCaps(const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) const; 196 197 std::unordered_set<int> requestIds_; 198 }; 199 private: 200 WifiNetAgent(); 201 void CreateNetLinkInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 202 IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig, int instId = 0); 203 204 void SetNetLinkIPInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 205 IpV6Info &wifiIpV6Info); 206 207 void SetNetLinkRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 208 IpV6Info &wifiIpV6Info); 209 210 void SetNetLinkHostRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo); 211 212 void SetNetLinkLocalRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 213 IpV6Info &wifiIpV6Info); 214 215 void SetNetLinkDnsInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo, 216 IpV6Info &wifiIpV6Info); 217 218 /** 219 * Update link information 220 * 221 * @param wifiIpInfo wifi network link data information 222 * @param wifiIpV6Info wifi network link IPV6 data information 223 * @param wifiProxyConfig wifi network link proxy information 224 */ 225 void UpdateNetLinkInfo(IpInfo &wifiIpInfo, IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig, 226 int instId = 0); 227 private: 228 uint32_t supplierId{0}; 229 uint32_t supplierIdForWlan1{0}; 230 bool isWifiAvailable_[STA_INSTANCE_MAX_NUM] = {false}; 231 WifiNetAgentCallbacks wifiNetAgentCallbacks_; 232 std::unique_ptr<WifiEventHandler> netAgentEventHandler_ = nullptr; 233 std::mutex netAgentMutex_; 234 class NetInfoObserver final : public NetManagerStandard::NetConnCallbackStub { 235 public: 236 int32_t NetAvailable(sptr<NetManagerStandard::NetHandle> &netHandle) override; 237 }; 238 sptr<NetInfoObserver> netConnCallback_ { nullptr }; 239 static bool IsDefaultBtNet(); 240 }; 241 } // namespace Wifi 242 } // namespace OHOS 243 #endif // CELLULAR_DATA_NET_AGENT_H 244