• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t GetUid() const;
95     void SetUid(int32_t uid);
96     bool IsAvailable() const;
97     std::shared_ptr<Network> GetNetwork() const;
98     int32_t GetNetId() const;
99     sptr<NetHandle> GetNetHandle() const;
100     void GetHttpProxy(HttpProxy &httpProxy);
101     void UpdateNetConnState(NetConnState netConnState);
102     bool IsConnecting() const;
103     bool IsConnected() const;
104     void SetNetValid(NetDetectionStatus netState);
105     bool IsNetValidated() const;
106     /**
107      * This method returns the score of the current network supplier.
108      *
109      * It is used to prioritize network suppliers so that higher priority producers can activate when lower
110      * priority networks are available.
111      *
112      * @return the score of the current network supplier.
113      */
114     int32_t GetNetScore() const;
115 
116     /**
117      * This method returns the real score of current network supplier.
118      *
119      * This method subtracts the score depending on different conditions, or returns netScore_ if the conditions are not
120      * met.
121      * It is used to compare the priorities of different networks.
122      *
123      * @return the real score of current network supplier.
124      */
125     int32_t GetRealScore();
126     bool SupplierConnection(const std::set<NetCap> &netCaps, const NetRequest &netrequest = {});
127     bool SupplierDisconnection(const std::set<NetCap> &netCaps);
128     void SetRestrictBackground(bool restrictBackground);
129     bool GetRestrictBackground() const;
130     bool RequestToConnect(const NetRequest &netrequest = {});
131     void AddRequest(const NetRequest &netrequest);
132     void RemoveRequest(const NetRequest &netrequest);
133     int32_t SelectAsBestNetwork(const NetRequest &netrequest);
134     void ReceiveBestScore(int32_t bestScore, uint32_t supplierId, const NetRequest &netrequest);
135     int32_t CancelRequest(const NetRequest &netrequest);
136     void RemoveBestRequest(uint32_t reqId);
137     std::set<uint32_t> &GetBestRequestList();
138     void SetDefault();
139     void ClearDefault();
140     sptr<INetSupplierCallback> GetSupplierCallback();
141     void RegisterSupplierCallback(const sptr<INetSupplierCallback> &callback);
142     void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
143     void SetSupplierType(int32_t type);
144     std::string GetSupplierType();
145     std::string TechToType(NetSlotTech type);
146     void SetDetectionDone();
147 
148     bool ResumeNetworkInfo();
149     bool IsNetQualityPoor();
150     bool IsNetAcceptUnavalidate();
151     bool IsInFirstTimeDetecting() const;
152 
153 private:
154     NetBearType netSupplierType_;
155     std::string netSupplierIdent_;
156     NetCaps netCaps_;
157     NetLinkInfo netLinkInfo_;
158     NetSupplierInfo netSupplierInfo_;
159     NetAllCapabilities netAllCapabilities_;
160     uint32_t supplierId_ = 0;
161     int32_t netScore_ = 0;
162     std::set<uint32_t> requestList_;
163     std::set<uint32_t> bestReqList_;
164     std::set<uint32_t> uidReqList_;
165     sptr<INetSupplierCallback> netController_ = nullptr;
166     std::shared_ptr<Network> network_ = nullptr;
167     sptr<NetHandle> netHandle_ = nullptr;
168     bool restrictBackground_ = true;
169     std::string type_ = "";
170     NetDetectionStatus netQuality_ = QUALITY_NORMAL_STATE;
171     bool isFirstTimeDetectionDone = false;
172     bool isAcceptUnvaliad = false;
173     int32_t uid_ = 0;
174 };
175 } // namespace NetManagerStandard
176 } // namespace OHOS
177 #endif // NET_SUPPLIER_H
178