• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 NETWORKSHARE_NETWORK_MONITOR_H
17 #define NETWORKSHARE_NETWORK_MONITOR_H
18 
19 #include <any>
20 #include <map>
21 
22 #include "event_handler.h"
23 #include "net_conn_callback_stub.h"
24 #include "net_conn_client.h"
25 #include "networkshare_hisysevent.h"
26 #include "networkshare_state_common.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 constexpr int32_t INVALID_NETID = -1;
31 class NetworkShareUpstreamMonitor : public std::enable_shared_from_this<NetworkShareUpstreamMonitor> {
32 public:
GetInstance()33     static std::shared_ptr<NetworkShareUpstreamMonitor> &GetInstance()
34     {
35         static std::shared_ptr<NetworkShareUpstreamMonitor> instance = std::make_shared<NetworkShareUpstreamMonitor>();
36         return instance;
37     }
38     NetworkShareUpstreamMonitor();
39     virtual ~NetworkShareUpstreamMonitor();
40     class NetConnectionCallback : public NetConnCallbackStub {
41     public:
42         NetConnectionCallback(const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor, int32_t callbackType);
43         ~NetConnectionCallback() = default;
44 
45         int32_t NetAvailable(sptr<NetHandle> &netHandle) override;
46         int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap) override;
47         int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info) override;
48         int32_t NetLost(sptr<NetHandle> &netHandle) override;
49         int32_t NetUnavailable() override;
50         int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked) override;
51 
52     private:
53         std::shared_ptr<NetworkShareUpstreamMonitor> NetworkMonitor_;
54     };
55 
56 public:
57     class NotifyUpstreamCallback {
58     public:
59         virtual void OnUpstreamStateChanged(int32_t msgName, int32_t param1) = 0;
60         virtual void OnUpstreamStateChanged(int32_t msgName, int32_t param1, int32_t param2,
61                                             const std::any &messageObj) = 0;
62     };
63 
64     class MonitorEventHandler : public AppExecFwk::EventHandler {
65     public:
66         MonitorEventHandler(const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor,
67                             const std::shared_ptr<AppExecFwk::EventRunner> &runner);
68         ~MonitorEventHandler() = default;
69 
70     private:
71         std::shared_ptr<NetworkShareUpstreamMonitor> networkMonitor_;
72     };
73 
74     /**
75      * set eventhandler
76      */
77     void SetOptionData(int what, std::weak_ptr<MonitorEventHandler> &handler);
78 
79     /**
80      * set callback to listen default network modify
81      */
82     void ListenDefaultNetwork();
83 
84     /**
85      * get current upstream networ (default network now)
86      */
87     bool GetCurrentGoodUpstream(std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo);
88 
89     /**
90      * register main state machine callback
91      */
92     void RegisterUpstreamChangedCallback(const std::shared_ptr<NotifyUpstreamCallback> &callback);
93 
94 private:
95     void NotifyMainStateMachine(int32_t which, const std::shared_ptr<UpstreamNetworkInfo> &obj);
96     void NotifyMainStateMachine(int32_t which);
97     void HandleNetAvailable(sptr<NetHandle> &netHandle);
98     void HandleNetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &newNetAllCap);
99     void HandleConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &newNetLinkInfo);
100     void HandleNetLost(sptr<NetHandle> &netHandle);
101 
102 private:
103     int32_t eventId_ = 0;
104     sptr<NetConnectionCallback> defaultNetworkCallback_ = nullptr;
105     std::map<int32_t, std::shared_ptr<UpstreamNetworkInfo>> networkMaps_;
106     std::mutex networkMapMutex_;
107     int32_t defaultNetworkId_ = INVALID_NETID;
108     std::weak_ptr<MonitorEventHandler> eventHandler_;
109     std::shared_ptr<NotifyUpstreamCallback> notifyUpstreamCallback_ = nullptr;
110 };
111 } // namespace NetManagerStandard
112 } // namespace OHOS
113 #endif // NETWORKSHARE_NETWORK_MONITOR_H
114