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 #include "net_connection_impl.h"
16 #include "cj_lambda.h"
17 #include "mutex"
18 #include "net_conn_client.h"
19 #include "netmanager_base_log.h"
20 #include "net_specifier.h"
21 
22 namespace OHOS::NetManagerStandard {
23 std::map<ConnectionCallbackObserver *, NetConnectionImpl *> NET_CONNECTIONS;
24 std::mutex g_netConnectionsMutex;
25 
NetConnectionProxy(CNetSpecifier specifier,uint32_t timeout)26 NetConnectionProxy::NetConnectionProxy(CNetSpecifier specifier, uint32_t timeout)
27 {
28     NetConnectionImpl *netConnection = NetConnectionImpl::MakeNetConnection();
29     if (netConnection == nullptr) {
30         return;
31     }
32     if (specifier.hasSpecifier) {
33         netConnection->hasNetSpecifier_ = true;
34         NetSpecifier netSpecifier;
35         netSpecifier.ident_ = std::string(specifier.bearerPrivateIdentifier);
36         for (int64_t i = 0; i < specifier.netCapabilities.bearedTypeSize; i++) {
37             auto netbear = static_cast<NetBearType>(specifier.netCapabilities.bearerTypes[i]);
38             netSpecifier.netCapabilities_.bearerTypes_.insert(netbear);
39         }
40         for (int64_t i = 0; i < specifier.netCapabilities.networkCapSize; i++) {
41             auto cap = static_cast<NetCap>(specifier.netCapabilities.networkCap[i]);
42             netSpecifier.netCapabilities_.netCaps_.insert(cap);
43         }
44         netSpecifier.netCapabilities_.linkUpBandwidthKbps_ = specifier.netCapabilities.linkUpBandwidthKbps;
45         netSpecifier.netCapabilities_.linkDownBandwidthKbps_ = specifier.netCapabilities.linkDownBandwidthKbps;
46         netConnection->netSpecifier_ = netSpecifier;
47     }
48     if (timeout > 0) {
49         netConnection->hasTimeout_ = true;
50         netConnection->timeout_ = timeout;
51     }
52     netConn_ = netConnection;
53 }
54 
RegisterCallback()55 int32_t NetConnectionProxy::RegisterCallback()
56 {
57     sptr<INetConnCallback> callback = netConn_->GetObserver();
58     if (netConn_->hasNetSpecifier_) {
59         sptr<NetSpecifier> specifier = new NetSpecifier(netConn_->netSpecifier_);
60         int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, netConn_->timeout_);
61         NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ and hasTimeout_ %{public}d", ret);
62         return ret;
63     }
64     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
65     return ret;
66 }
67 
UnregisterCallback()68 int32_t NetConnectionProxy::UnregisterCallback()
69 {
70     sptr<INetConnCallback> callback = netConn_->GetObserver();
71 
72     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
73     if (ret != NETMANAGER_SUCCESS) {
74         NETMANAGER_BASE_LOGE("Unregister result %{public}d", ret);
75     }
76     return ret;
77 }
78 
OnNetAvailible(void (* callback)(int32_t))79 void NetConnectionProxy::OnNetAvailible(void (*callback)(int32_t))
80 {
81     netConn_->netAvailible.push_back(CJLambda::Create(callback));
82 }
83 
OnNetBlockStatusChange(void (* callback)(int32_t,bool))84 void NetConnectionProxy::OnNetBlockStatusChange(void (*callback)(int32_t, bool))
85 {
86     netConn_->netBlockStatusChange.push_back(CJLambda::Create(callback));
87 }
88 
OnNetCapabilitiesChange(void (* callback)(CNetCapabilityInfo))89 void NetConnectionProxy::OnNetCapabilitiesChange(void (*callback)(CNetCapabilityInfo))
90 {
91     netConn_->netCapabilitiesChange.push_back(CJLambda::Create(callback));
92 }
93 
OnNetConnectionPropertiesChange(void (* callback)(int32_t,CConnectionProperties))94 void NetConnectionProxy::OnNetConnectionPropertiesChange(void (*callback)(int32_t, CConnectionProperties))
95 {
96     netConn_->netConnectionPropertiesChange.push_back(CJLambda::Create(callback));
97 }
98 
OnNetLost(void (* callback)(int32_t))99 void NetConnectionProxy::OnNetLost(void (*callback)(int32_t))
100 {
101     netConn_->netLost.push_back(CJLambda::Create(callback));
102 }
103 
OnNetUnavailable(void (* callback)())104 void NetConnectionProxy::OnNetUnavailable(void (*callback)())
105 {
106     netConn_->netUnavailable.push_back(CJLambda::Create(callback));
107 }
108 
Release()109 void NetConnectionProxy::Release()
110 {
111     netConn_->DeleteNetConnection(netConn_);
112 }
113 
NetConnectionImpl()114 NetConnectionImpl::NetConnectionImpl()
115     : hasNetSpecifier_(false), hasTimeout_(false), timeout_(0), observer_(new ConnectionCallbackObserver)
116 {
117 }
118 
MakeNetConnection()119 NetConnectionImpl *NetConnectionImpl::MakeNetConnection()
120 {
121     std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
122     auto netConnection = new NetConnectionImpl();
123     if (netConnection) {
124         NET_CONNECTIONS[netConnection->observer_.GetRefPtr()] = netConnection;
125     }
126     return netConnection;
127 }
128 
DeleteNetConnection(NetConnectionImpl * netConnection)129 void NetConnectionImpl::DeleteNetConnection(NetConnectionImpl *netConnection)
130 {
131     std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
132     NET_CONNECTIONS.erase(netConnection->observer_.GetRefPtr());
133     delete netConnection;
134 }
135 
GetObserver() const136 sptr<ConnectionCallbackObserver> NetConnectionImpl::GetObserver() const
137 {
138     return observer_;
139 }
140 } // namespace OHOS::NetManagerStandard