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