1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NET_SUPPLIER_H 17 #define NET_SUPPLIER_H 18 19 #include <map> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "i_net_supplier_callback.h" 26 #include "i_net_conn_callback.h" 27 #include "http_proxy.h" 28 #include "network.h" 29 #include "net_caps.h" 30 #include "net_specifier.h" 31 #include "net_supplier_info.h" 32 33 namespace OHOS { 34 namespace NetManagerStandard { 35 enum CallbackType { 36 CALL_TYPE_UNKNOWN = 0, 37 CALL_TYPE_AVAILABLE = 1, 38 CALL_TYPE_LOSTING = 2, 39 CALL_TYPE_LOST = 3, 40 CALL_TYPE_UPDATE_CAP = 4, 41 CALL_TYPE_UPDATE_LINK = 5, 42 CALL_TYPE_UNAVAILABLE = 6, 43 CALL_TYPE_BLOCK_STATUS = 7, 44 }; 45 46 using NetTypeScore = std::unordered_map<NetBearType, int32_t>; 47 constexpr int32_t NET_TYPE_SCORE_INTERVAL = 10; 48 constexpr int32_t NET_VALID_SCORE = 4 * NET_TYPE_SCORE_INTERVAL; 49 constexpr int32_t DIFF_SCORE_BETWEEN_GOOD_POOR = 2 * NET_TYPE_SCORE_INTERVAL; 50 enum class NetTypeScoreValue : int32_t { 51 USB_VALUE = 4 * NET_TYPE_SCORE_INTERVAL, 52 BLUETOOTH_VALUE = 5 * NET_TYPE_SCORE_INTERVAL, 53 CELLULAR_VALUE = 6 * NET_TYPE_SCORE_INTERVAL, 54 WIFI_VALUE = 7 * NET_TYPE_SCORE_INTERVAL, 55 ETHERNET_VALUE = 8 * NET_TYPE_SCORE_INTERVAL, 56 VPN_VALUE = 9 * NET_TYPE_SCORE_INTERVAL, 57 WIFI_AWARE_VALUE = 10 * NET_TYPE_SCORE_INTERVAL, 58 MAX_SCORE = 10 * NET_TYPE_SCORE_INTERVAL 59 }; 60 61 static inline NetTypeScore netTypeScore_ = { 62 {BEARER_CELLULAR, static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE)}, 63 {BEARER_WIFI, static_cast<int32_t>(NetTypeScoreValue::WIFI_VALUE)}, 64 {BEARER_BLUETOOTH, static_cast<int32_t>(NetTypeScoreValue::BLUETOOTH_VALUE)}, 65 {BEARER_ETHERNET, static_cast<int32_t>(NetTypeScoreValue::ETHERNET_VALUE)}, 66 {BEARER_VPN, static_cast<int32_t>(NetTypeScoreValue::VPN_VALUE)}, 67 {BEARER_WIFI_AWARE, static_cast<int32_t>(NetTypeScoreValue::WIFI_AWARE_VALUE)}}; 68 69 class NetSupplier : public virtual RefBase { 70 public: 71 NetSupplier(NetBearType bearerType, const std::string &netSupplierIdent, const std::set<NetCap> &netCaps); 72 ~NetSupplier() = default; 73 void InitNetScore(); 74 /** 75 * Resets all attributes that may change in the supplier, such as detection progress and network quality. 76 */ 77 void ResetNetSupplier(); 78 bool operator==(const NetSupplier &netSupplier) const; 79 void SetNetwork(const std::shared_ptr<Network> &network); 80 void UpdateNetSupplierInfo(const NetSupplierInfo &netSupplierInfo); 81 int32_t UpdateNetLinkInfo(NetLinkInfo &netLinkInfo); 82 uint32_t GetSupplierId() const; 83 NetBearType GetNetSupplierType() const; 84 std::string GetNetSupplierIdent() const; 85 bool CompareNetCaps(const std::set<NetCap> caps) const; 86 bool HasNetCap(NetCap cap) const; 87 bool HasNetCaps(const std::set<NetCap> &caps) const; 88 const NetCaps &GetNetCaps() const; 89 NetAllCapabilities GetNetCapabilities() const; 90 bool GetRoaming() const; 91 int8_t GetStrength() const; 92 uint16_t GetFrequency() const; 93 int32_t GetSupplierUid() const; 94 bool IsAvailable() const; 95 std::shared_ptr<Network> GetNetwork() const; 96 int32_t GetNetId() const; 97 sptr<NetHandle> GetNetHandle() const; 98 void GetHttpProxy(HttpProxy &httpProxy); 99 void UpdateNetConnState(NetConnState netConnState); 100 bool IsConnecting() const; 101 bool IsConnected() const; 102 void SetNetValid(NetDetectionStatus netState); 103 bool IsNetValidated() const; 104 /** 105 * This method returns the score of the current network supplier. 106 * 107 * It is used to prioritize network suppliers so that higher priority producers can activate when lower 108 * priority networks are available. 109 * 110 * @return the score of the current network supplier. 111 */ 112 int32_t GetNetScore() const; 113 114 /** 115 * This method returns the real score of current network supplier. 116 * 117 * This method subtracts the score depending on different conditions, or returns netScore_ if the conditions are not 118 * met. 119 * It is used to compare the priorities of different networks. 120 * 121 * @return the real score of current network supplier. 122 */ 123 int32_t GetRealScore(); 124 bool SupplierConnection(const std::set<NetCap> &netCaps, const NetRequest &netrequest = {}); 125 bool SupplierDisconnection(const std::set<NetCap> &netCaps); 126 void SetRestrictBackground(bool restrictBackground); 127 bool GetRestrictBackground() const; 128 bool RequestToConnect(uint32_t reqId, const NetRequest &netrequest = {}); 129 int32_t SelectAsBestNetwork(uint32_t reqId); 130 void ReceiveBestScore(uint32_t reqId, int32_t bestScore, uint32_t supplierId); 131 int32_t CancelRequest(uint32_t reqId); 132 void RemoveBestRequest(uint32_t reqId); 133 std::set<uint32_t> &GetBestRequestList(); 134 void SetDefault(); 135 void ClearDefault(); 136 sptr<INetSupplierCallback> GetSupplierCallback(); 137 void RegisterSupplierCallback(const sptr<INetSupplierCallback> &callback); 138 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 139 void SetSupplierType(int32_t type); 140 std::string GetSupplierType(); 141 std::string TechToType(NetSlotTech type); 142 void SetDetectionDone(); 143 144 bool ResumeNetworkInfo(); 145 bool IsNetQualityPoor(); 146 bool IsInFirstTimeDetecting() const; 147 148 private: 149 NetBearType netSupplierType_; 150 std::string netSupplierIdent_; 151 NetCaps netCaps_; 152 NetLinkInfo netLinkInfo_; 153 NetSupplierInfo netSupplierInfo_; 154 NetAllCapabilities netAllCapabilities_; 155 uint32_t supplierId_ = 0; 156 int32_t netScore_ = 0; 157 std::set<uint32_t> requestList_; 158 std::set<uint32_t> bestReqList_; 159 sptr<INetSupplierCallback> netController_ = nullptr; 160 std::shared_ptr<Network> network_ = nullptr; 161 sptr<NetHandle> netHandle_ = nullptr; 162 bool restrictBackground_ = true; 163 std::string type_ = ""; 164 NetDetectionStatus netQuality_ = QUALITY_NORMAL_STATE; 165 bool isFirstTimeDetectionDone = false; 166 bool isAcceptUnvaliad = false; 167 enum RegisterType { 168 UNKOWN, 169 REGISTER, 170 REQUEST, 171 }; 172 }; 173 } // namespace NetManagerStandard 174 } // namespace OHOS 175 #endif // NET_SUPPLIER_H 176