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#ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
34#define BLUETOOTH_SCORE_FACTOR 8
35#else
36#define BLUETOOTH_SCORE_FACTOR 5
37#endif
38namespace OHOS {
39namespace NetManagerStandard {
40enum CallbackType {
41    CALL_TYPE_UNKNOWN = 0,
42    CALL_TYPE_AVAILABLE = 1,
43    CALL_TYPE_LOSTING = 2,
44    CALL_TYPE_LOST = 3,
45    CALL_TYPE_UPDATE_CAP = 4,
46    CALL_TYPE_UPDATE_LINK = 5,
47    CALL_TYPE_UNAVAILABLE = 6,
48    CALL_TYPE_BLOCK_STATUS = 7,
49};
50
51using NetTypeScore = std::unordered_map<NetBearType, int32_t>;
52constexpr int32_t NET_TYPE_SCORE_INTERVAL = 10;
53constexpr int32_t NET_VALID_SCORE = 4 * NET_TYPE_SCORE_INTERVAL;
54constexpr int32_t DIFF_SCORE_BETWEEN_GOOD_POOR = 2 * NET_TYPE_SCORE_INTERVAL;
55enum class NetTypeScoreValue : int32_t {
56    USB_VALUE = 4 * NET_TYPE_SCORE_INTERVAL,
57    BLUETOOTH_VALUE = BLUETOOTH_SCORE_FACTOR * NET_TYPE_SCORE_INTERVAL,
58    CELLULAR_VALUE = 6 * NET_TYPE_SCORE_INTERVAL,
59    WIFI_VALUE = 7 * NET_TYPE_SCORE_INTERVAL,
60    ETHERNET_VALUE = 8 * NET_TYPE_SCORE_INTERVAL,
61    VPN_VALUE = 9 * NET_TYPE_SCORE_INTERVAL,
62    WIFI_AWARE_VALUE = 10 * NET_TYPE_SCORE_INTERVAL,
63    MAX_SCORE = 10 * NET_TYPE_SCORE_INTERVAL
64};
65
66static inline NetTypeScore netTypeScore_ = {
67    {BEARER_CELLULAR, static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE)},
68    {BEARER_WIFI, static_cast<int32_t>(NetTypeScoreValue::WIFI_VALUE)},
69    {BEARER_BLUETOOTH, static_cast<int32_t>(NetTypeScoreValue::BLUETOOTH_VALUE)},
70    {BEARER_ETHERNET, static_cast<int32_t>(NetTypeScoreValue::ETHERNET_VALUE)},
71    {BEARER_VPN, static_cast<int32_t>(NetTypeScoreValue::VPN_VALUE)},
72    {BEARER_WIFI_AWARE, static_cast<int32_t>(NetTypeScoreValue::WIFI_AWARE_VALUE)}};
73
74class NetSupplier : public virtual RefBase {
75public:
76    NetSupplier(NetBearType bearerType, const std::string &netSupplierIdent, const std::set<NetCap> &netCaps);
77    ~NetSupplier() = default;
78    void InitNetScore();
79    /**
80     * Resets all attributes that may change in the supplier, such as detection progress and network quality.
81     */
82    void ResetNetSupplier();
83    bool operator==(const NetSupplier &netSupplier) const;
84    void SetNetwork(const std::shared_ptr<Network> &network);
85    void UpdateNetSupplierInfo(const NetSupplierInfo &netSupplierInfo);
86    int32_t UpdateNetLinkInfo(NetLinkInfo &netLinkInfo);
87    uint32_t GetSupplierId() const;
88    NetBearType GetNetSupplierType() const;
89    std::string GetNetSupplierIdent() const;
90    bool CompareNetCaps(const std::set<NetCap> caps) const;
91    bool HasNetCap(NetCap cap) const;
92    bool HasNetCaps(const std::set<NetCap> &caps) const;
93    const NetCaps &GetNetCaps() const;
94    NetAllCapabilities GetNetCapabilities() const;
95    bool GetRoaming() const;
96    int8_t GetStrength() const;
97    uint16_t GetFrequency() const;
98    int32_t GetSupplierUid() const;
99    int32_t GetUid() const;
100    void SetUid(int32_t uid);
101    bool IsAvailable() const;
102    std::shared_ptr<Network> GetNetwork() const;
103    int32_t GetNetId() const;
104    sptr<NetHandle> GetNetHandle() const;
105    void GetHttpProxy(HttpProxy &httpProxy);
106    void UpdateNetConnState(NetConnState netConnState);
107    bool IsConnecting() const;
108    bool IsConnected() const;
109    void SetNetValid(NetDetectionStatus netState);
110    bool IsNetValidated() const;
111    /**
112     * This method returns the score of the current network supplier.
113     *
114     * It is used to prioritize network suppliers so that higher priority producers can activate when lower
115     * priority networks are available.
116     *
117     * @return the score of the current network supplier.
118     */
119    int32_t GetNetScore() const;
120
121    /**
122     * This method returns the real score of current network supplier.
123     *
124     * This method subtracts the score depending on different conditions, or returns netScore_ if the conditions are not
125     * met.
126     * It is used to compare the priorities of different networks.
127     *
128     * @return the real score of current network supplier.
129     */
130    int32_t GetRealScore();
131    bool SupplierConnection(const std::set<NetCap> &netCaps, const NetRequest &netrequest = {});
132    bool SupplierDisconnection(const std::set<NetCap> &netCaps);
133    void SetRestrictBackground(bool restrictBackground);
134    bool GetRestrictBackground() const;
135    bool RequestToConnect(const NetRequest &netrequest = {});
136    void AddRequest(const NetRequest &netrequest);
137    void RemoveRequest(const NetRequest &netrequest);
138    int32_t SelectAsBestNetwork(const NetRequest &netrequest);
139    void ReceiveBestScore(int32_t bestScore, uint32_t supplierId, const NetRequest &netrequest);
140    int32_t CancelRequest(const NetRequest &netrequest);
141    void RemoveBestRequest(uint32_t reqId);
142    std::set<uint32_t> &GetBestRequestList();
143    void SetDefault();
144    void ClearDefault();
145    sptr<INetSupplierCallback> GetSupplierCallback();
146    void RegisterSupplierCallback(const sptr<INetSupplierCallback> &callback);
147    void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
148    void SetSupplierType(int32_t type);
149    std::string GetSupplierType();
150    std::string TechToType(NetSlotTech type);
151    void SetDetectionDone();
152
153    bool ResumeNetworkInfo();
154    bool IsNetQualityPoor();
155    bool IsInFirstTimeDetecting() const;
156
157private:
158    NetBearType netSupplierType_;
159    std::string netSupplierIdent_;
160    NetCaps netCaps_;
161    NetLinkInfo netLinkInfo_;
162    NetSupplierInfo netSupplierInfo_;
163    NetAllCapabilities netAllCapabilities_;
164    uint32_t supplierId_ = 0;
165    int32_t netScore_ = 0;
166    std::set<uint32_t> requestList_;
167    std::set<uint32_t> bestReqList_;
168    std::set<uint32_t> uidReqList_;
169    sptr<INetSupplierCallback> netController_ = nullptr;
170    std::shared_ptr<Network> network_ = nullptr;
171    sptr<NetHandle> netHandle_ = nullptr;
172    bool restrictBackground_ = true;
173    std::string type_ = "";
174    NetDetectionStatus netQuality_ = QUALITY_NORMAL_STATE;
175    bool isFirstTimeDetectionDone = false;
176    bool isAcceptUnvaliad = false;
177    int32_t uid_ = 0;
178};
179} // namespace NetManagerStandard
180} // namespace OHOS
181#endif // NET_SUPPLIER_H
182