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