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 #include "networkshare_client.h"
17
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "net_manager_constants.h"
21 #include "netmgr_ext_log_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
NetworkShareClient()25 NetworkShareClient::NetworkShareClient() : networkShareService_(nullptr), deathRecipient_(nullptr) {}
26
~NetworkShareClient()27 NetworkShareClient::~NetworkShareClient() {}
28
StartSharing(const SharingIfaceType & type)29 int32_t NetworkShareClient::StartSharing(const SharingIfaceType &type)
30 {
31 sptr<INetworkShareService> proxy = GetProxy();
32 if (proxy == nullptr) {
33 NETMGR_EXT_LOG_E("StartSharing proxy is nullptr");
34 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
35 }
36 return proxy->StartNetworkSharing(type);
37 }
38
StopSharing(const SharingIfaceType & type)39 int32_t NetworkShareClient::StopSharing(const SharingIfaceType &type)
40 {
41 sptr<INetworkShareService> proxy = GetProxy();
42 if (proxy == nullptr) {
43 NETMGR_EXT_LOG_E("StopSharing proxy is nullptr");
44 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
45 }
46 return proxy->StopNetworkSharing(type);
47 }
48
IsSharingSupported(int32_t & supported)49 int32_t NetworkShareClient::IsSharingSupported(int32_t &supported)
50 {
51 sptr<INetworkShareService> proxy = GetProxy();
52 if (proxy == nullptr) {
53 NETMGR_EXT_LOG_E("IsSharingSupported proxy is nullptr");
54 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
55 }
56 return proxy->IsNetworkSharingSupported(supported);
57 }
58
IsSharing(int32_t & sharingStatus)59 int32_t NetworkShareClient::IsSharing(int32_t &sharingStatus)
60 {
61 sptr<INetworkShareService> proxy = GetProxy();
62 if (proxy == nullptr) {
63 NETMGR_EXT_LOG_E("IsSharing proxy is nullptr");
64 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
65 }
66 return proxy->IsSharing(sharingStatus);
67 }
68
RegisterSharingEvent(sptr<ISharingEventCallback> callback)69 int32_t NetworkShareClient::RegisterSharingEvent(sptr<ISharingEventCallback> callback)
70 {
71 sptr<INetworkShareService> proxy = GetProxy();
72 if (proxy == nullptr) {
73 NETMGR_EXT_LOG_E("RegisterSharingEvent proxy is nullptr");
74 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
75 }
76 return proxy->RegisterSharingEvent(callback);
77 }
78
UnregisterSharingEvent(sptr<ISharingEventCallback> callback)79 int32_t NetworkShareClient::UnregisterSharingEvent(sptr<ISharingEventCallback> callback)
80 {
81 sptr<INetworkShareService> proxy = GetProxy();
82 if (proxy == nullptr) {
83 NETMGR_EXT_LOG_E("UnregisterSharingEvent proxy is nullptr");
84 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
85 }
86 return proxy->UnregisterSharingEvent(callback);
87 }
88
GetSharableRegexs(const SharingIfaceType & type,std::vector<std::string> & ifaceRegexs)89 int32_t NetworkShareClient::GetSharableRegexs(const SharingIfaceType &type, std::vector<std::string> &ifaceRegexs)
90 {
91 sptr<INetworkShareService> proxy = GetProxy();
92 if (proxy == nullptr) {
93 NETMGR_EXT_LOG_E("GetSharableRegexs proxy is nullptr");
94 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
95 }
96 return proxy->GetSharableRegexs(type, ifaceRegexs);
97 }
98
GetSharingState(const SharingIfaceType & type,SharingIfaceState & state)99 int32_t NetworkShareClient::GetSharingState(const SharingIfaceType &type, SharingIfaceState &state)
100 {
101 sptr<INetworkShareService> proxy = GetProxy();
102 if (proxy == nullptr) {
103 NETMGR_EXT_LOG_E("GetSharingState proxy is nullptr");
104 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
105 }
106 return proxy->GetSharingState(type, state);
107 }
108
GetSharingIfaces(const SharingIfaceState & state,std::vector<std::string> & ifaces)109 int32_t NetworkShareClient::GetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)
110 {
111 sptr<INetworkShareService> proxy = GetProxy();
112 if (proxy == nullptr) {
113 NETMGR_EXT_LOG_E("GetSharingIfaces proxy is nullptr");
114 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
115 }
116 return proxy->GetNetSharingIfaces(state, ifaces);
117 }
118
GetStatsRxBytes(int32_t & bytes)119 int32_t NetworkShareClient::GetStatsRxBytes(int32_t &bytes)
120 {
121 sptr<INetworkShareService> proxy = GetProxy();
122 if (proxy == nullptr) {
123 NETMGR_EXT_LOG_E("GetStatsRxBytes proxy is nullptr");
124 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
125 }
126 return proxy->GetStatsRxBytes(bytes);
127 }
128
GetStatsTxBytes(int32_t & bytes)129 int32_t NetworkShareClient::GetStatsTxBytes(int32_t &bytes)
130 {
131 sptr<INetworkShareService> proxy = GetProxy();
132 if (proxy == nullptr) {
133 NETMGR_EXT_LOG_E("GetStatsTxBytes proxy is nullptr");
134 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
135 }
136 return proxy->GetStatsTxBytes(bytes);
137 }
138
GetStatsTotalBytes(int32_t & bytes)139 int32_t NetworkShareClient::GetStatsTotalBytes(int32_t &bytes)
140 {
141 sptr<INetworkShareService> proxy = GetProxy();
142 if (proxy == nullptr) {
143 NETMGR_EXT_LOG_E("GetStatsTotalBytes proxy is nullptr");
144 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
145 }
146 return proxy->GetStatsTotalBytes(bytes);
147 }
148
GetProxy()149 sptr<INetworkShareService> NetworkShareClient::GetProxy()
150 {
151 std::lock_guard lock(mutex_);
152 if (networkShareService_ != nullptr) {
153 return networkShareService_;
154 }
155 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
156 if (sam == nullptr) {
157 NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
158 return nullptr;
159 }
160 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID);
161 if (remote == nullptr) {
162 NETMGR_EXT_LOG_E("get Remote service failed");
163 return nullptr;
164 }
165 deathRecipient_ = new NetshareDeathRecipient(*this);
166 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
167 NETMGR_EXT_LOG_E("add death recipient failed");
168 return nullptr;
169 }
170 networkShareService_ = iface_cast<INetworkShareService>(remote);
171 if (networkShareService_ == nullptr) {
172 NETMGR_EXT_LOG_E("get Remote service proxy failed");
173 return nullptr;
174 }
175 return networkShareService_;
176 }
177
OnRemoteDied(const wptr<IRemoteObject> & remote)178 void NetworkShareClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
179 {
180 if (remote == nullptr) {
181 NETMGR_EXT_LOG_E("remote object is nullptr");
182 return;
183 }
184 std::lock_guard lock(mutex_);
185 if (networkShareService_ == nullptr) {
186 NETMGR_EXT_LOG_E("networkShareService_ is nullptr");
187 return;
188 }
189 sptr<IRemoteObject> local = networkShareService_->AsObject();
190 if (local != remote.promote()) {
191 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
192 return;
193 }
194 local->RemoveDeathRecipient(deathRecipient_);
195 networkShareService_ = nullptr;
196 }
197 } // namespace NetManagerStandard
198 } // namespace OHOS
199