• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 NETWORK_H
17 #define NETWORK_H
18 
19 #include "event_report.h"
20 #include "inet_addr.h"
21 #include "i_net_detection_callback.h"
22 #include "i_net_monitor_callback.h"
23 #include "net_conn_event_handler.h"
24 #include "net_conn_types.h"
25 #include "net_link_info.h"
26 #include "net_monitor.h"
27 #include "net_supplier_info.h"
28 #include "route.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 constexpr uint32_t INVALID_NET_ID = 0;
33 constexpr int32_t MIN_NET_ID = 100;
34 constexpr int32_t MAX_NET_ID = 0xFFFF - 0x400;
35 using NetDetectionHandler = std::function<void(uint32_t supplierId, bool ifValid)>;
36 class Network : public virtual RefBase, public INetMonitorCallback, public std::enable_shared_from_this<Network> {
37 public:
38     Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType,
39             const std::shared_ptr<NetConnEventHandler> &eventHandler);
40     ~Network();
41     bool operator==(const Network &network) const;
42     int32_t GetNetId() const;
43     bool UpdateBasicNetwork(bool isAvailable_);
44     bool UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo);
45     NetLinkInfo GetNetLinkInfo() const;
46     void UpdateIpAddrs(const NetLinkInfo &netLinkInfo);
47     void UpdateInterfaces(const NetLinkInfo &netLinkInfo);
48     void UpdateRoutes(const NetLinkInfo &netLinkInfo);
49     void UpdateDns(const NetLinkInfo &netLinkInfo);
50     void UpdateMtu(const NetLinkInfo &netLinkInfo);
51     void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback);
52     int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback);
53     void StartNetDetection(bool needReport);
54     void SetDefaultNetWork();
55     void ClearDefaultNetWorkNetId();
56     bool IsConnecting() const;
57     bool IsConnected() const;
58     void UpdateNetConnState(NetConnState netConnState);
59 
60     void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override;
61 
62 private:
63     void StopNetDetection();
64     bool CreateBasicNetwork();
65     bool ReleaseBasicNetwork();
66     void InitNetMonitor();
67     void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect);
68     void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect);
69     NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet);
70     void SendConnectionChangedBroadcast(const NetConnState &netConnState) const;
71     void SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg);
72     void ResetNetlinkInfo();
73 
74 private:
75     int32_t netId_ = 0;
76     uint32_t supplierId_ = 0;
77     NetLinkInfo netLinkInfo_;
78     NetConnState state_ = NET_CONN_STATE_UNKNOWN;
79     NetDetectionStatus detectResult_ = UNKNOWN_STATE;
80     bool isPhyNetCreated_ = false;
81     std::shared_ptr<NetMonitor> netMonitor_ = nullptr;
82     NetDetectionHandler netCallback_;
83     NetBearType netSupplierType_;
84     std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_;
85     std::shared_ptr<NetConnEventHandler> eventHandler_;
86 };
87 } // namespace NetManagerStandard
88 } // namespace OHOS
89 #endif // NETWORK_H
90