1 /*
2  * Copyright (c) 2024 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_CONNECTION_IMPL_H
17 #define NET_CONNECTION_IMPL_H
18 
19 #include "common.h"
20 #include "ffi_remote_data.h"
21 #include "net_connection_callback.h"
22 #include "net_manager_constants.h"
23 #include "net_specifier.h"
24 #include <map>
25 #include <vector>
26 namespace OHOS::NetManagerStandard {
27 
28 class NetConnectionImpl final {
29 public:
30     bool hasNetSpecifier_;
31 
32     bool hasTimeout_;
33 
34     NetManagerStandard::NetSpecifier netSpecifier_;
35 
36     uint32_t timeout_;
37 
38     std::vector<std::function<void(int32_t)>> netAvailible;
39 
40     std::vector<std::function<void(int32_t, bool)>> netBlockStatusChange;
41 
42     std::vector<std::function<void(CNetCapabilityInfo)>> netCapabilitiesChange;
43 
44     std::vector<std::function<void(int32_t, CConnectionProperties)>> netConnectionPropertiesChange;
45 
46     std::vector<std::function<void(int32_t)>> netLost;
47 
48     std::vector<std::function<void()>> netUnavailable;
49 
50 public:
51     [[nodiscard]] sptr<OHOS::NetManagerStandard::ConnectionCallbackObserver> GetObserver() const;
52 
53     static NetConnectionImpl *MakeNetConnection();
54 
55     static void DeleteNetConnection(OHOS::NetManagerStandard::NetConnectionImpl *netConnection);
56 
57 private:
58     sptr<ConnectionCallbackObserver> observer_;
59 
60     explicit NetConnectionImpl();
61 
62     ~NetConnectionImpl() = default;
63 };
64 
65 class NetConnectionProxy : public OHOS::FFI::FFIData {
66     DECL_TYPE(NetConnectionProxy, OHOS::FFI::FFIData);
67 public:
68     NetConnectionProxy(CNetSpecifier specifier, uint32_t timeout);
69 
70     int32_t RegisterCallback();
71 
72     int32_t UnregisterCallback();
73 
74     void OnNetAvailible(void (*callback)(int32_t));
75 
76     void OnNetBlockStatusChange(void (*callback)(int32_t, bool));
77 
78     void OnNetCapabilitiesChange(void (*callback)(CNetCapabilityInfo));
79 
80     void OnNetConnectionPropertiesChange(void (*callback)(int32_t, CConnectionProperties));
81 
82     void OnNetLost(void (*callback)(int32_t));
83 
84     void OnNetUnavailable(void (*callback)());
85 
86     void Release();
87 
88 private:
89     NetConnectionImpl *netConn_;
90 };
91 
92 extern std::map<OHOS::NetManagerStandard::ConnectionCallbackObserver *, OHOS::NetManagerStandard::NetConnectionImpl *>
93     NET_CONNECTIONS;
94 extern std::mutex g_netConnectionsMutex;
95 } // namespace OHOS::NetManagerStandard
96 
97 #endif