1 /*
2 * Copyright (c) 2022-2023 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 <thread>
19
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "net_manager_constants.h"
23 #include "netmgr_ext_log_wrapper.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 constexpr size_t WAIT_REMOTE_TIME_SEC = 15;
29 constexpr uint32_t WAIT_FOR_SERVICE_TIME_S = 1;
30 constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
31 constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
32 std::condition_variable g_cv;
33 std::mutex g_mutexCv;
34 } // namespace
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)35 void NetworkShareLoadCallback::OnLoadSystemAbilitySuccess(
36 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
37 {
38 NETMGR_EXT_LOG_D("OnLoadSystemAbilitySuccess systemAbilityId: [%{public}d]", systemAbilityId);
39 std::unique_lock<std::mutex> lock(g_mutexCv);
40 remoteObject_ = remoteObject;
41 g_cv.notify_one();
42 }
43
OnLoadSystemAbilityFail(int32_t systemAbilityId)44 void NetworkShareLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
45 {
46 NETMGR_EXT_LOG_D("OnLoadSystemAbilityFail: [%{public}d]", systemAbilityId);
47 loadSAFailed_ = true;
48 }
49
IsFailed()50 bool NetworkShareLoadCallback::IsFailed()
51 {
52 return loadSAFailed_;
53 }
54
GetRemoteObject() const55 const sptr<IRemoteObject> &NetworkShareLoadCallback::GetRemoteObject() const
56 {
57 return remoteObject_;
58 }
59
NetworkShareClient()60 NetworkShareClient::NetworkShareClient()
61 : networkShareService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
62
~NetworkShareClient()63 NetworkShareClient::~NetworkShareClient() {}
64
StartSharing(const SharingIfaceType & type)65 int32_t NetworkShareClient::StartSharing(const SharingIfaceType &type)
66 {
67 NETMGR_EXT_LOG_I("NetworkShare StartSharing type= %{public}d", type);
68 sptr<INetworkShareService> proxy = GetProxy();
69 if (proxy == nullptr) {
70 NETMGR_EXT_LOG_E("StartSharing proxy is nullptr");
71 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
72 }
73 return proxy->StartNetworkSharing(type);
74 }
75
StopSharing(const SharingIfaceType & type)76 int32_t NetworkShareClient::StopSharing(const SharingIfaceType &type)
77 {
78 NETMGR_EXT_LOG_I("NetworkShare StopSharing type= %{public}d", type);
79 sptr<INetworkShareService> proxy = GetProxy();
80 if (proxy == nullptr) {
81 NETMGR_EXT_LOG_E("StopSharing proxy is nullptr");
82 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
83 }
84 return proxy->StopNetworkSharing(type);
85 }
86
IsSharingSupported(int32_t & supported)87 int32_t NetworkShareClient::IsSharingSupported(int32_t &supported)
88 {
89 NETMGR_EXT_LOG_I("NetworkShare IsSharingSupported.");
90 sptr<INetworkShareService> proxy = GetProxy();
91 if (proxy == nullptr) {
92 NETMGR_EXT_LOG_E("IsSharingSupported proxy is nullptr");
93 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
94 }
95 return proxy->IsNetworkSharingSupported(supported);
96 }
97
IsSharing(int32_t & sharingStatus)98 int32_t NetworkShareClient::IsSharing(int32_t &sharingStatus)
99 {
100 NETMGR_EXT_LOG_I("NetworkShare IsSharing.");
101 sptr<INetworkShareService> proxy = GetProxy();
102 if (proxy == nullptr) {
103 NETMGR_EXT_LOG_E("IsSharing proxy is nullptr");
104 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
105 }
106 return proxy->IsSharing(sharingStatus);
107 }
108
RegisterSharingEvent(sptr<ISharingEventCallback> callback)109 int32_t NetworkShareClient::RegisterSharingEvent(sptr<ISharingEventCallback> callback)
110 {
111 NETMGR_EXT_LOG_I("NetworkShare RegisterSharingEvent.");
112 sptr<INetworkShareService> proxy = GetProxy();
113 if (proxy == nullptr) {
114 NETMGR_EXT_LOG_E("RegisterSharingEvent proxy is nullptr");
115 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
116 }
117 int32_t ret = proxy->RegisterSharingEvent(callback);
118 if (ret == NETMANAGER_EXT_SUCCESS) {
119 NETMGR_EXT_LOG_D("RegisterSharingEvent success, save callback.");
120 callback_ = callback;
121 }
122
123 return ret;
124 }
125
UnregisterSharingEvent(sptr<ISharingEventCallback> callback)126 int32_t NetworkShareClient::UnregisterSharingEvent(sptr<ISharingEventCallback> callback)
127 {
128 NETMGR_EXT_LOG_I("NetworkShare UnregisterSharingEvent.");
129 sptr<INetworkShareService> proxy = GetProxy();
130 if (proxy == nullptr) {
131 NETMGR_EXT_LOG_E("UnregisterSharingEvent proxy is nullptr");
132 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
133 }
134 int32_t ret = proxy->UnregisterSharingEvent(callback);
135 if (ret == NETMANAGER_EXT_SUCCESS) {
136 NETMGR_EXT_LOG_D("UnRegisterSharingEvent success, delete callback.");
137 callback_ = nullptr;
138 }
139
140 return ret;
141 }
142
GetSharableRegexs(const SharingIfaceType & type,std::vector<std::string> & ifaceRegexs)143 int32_t NetworkShareClient::GetSharableRegexs(const SharingIfaceType &type, std::vector<std::string> &ifaceRegexs)
144 {
145 NETMGR_EXT_LOG_I("NetworkShare GetSharableRegexs type= %{public}d.", type);
146 sptr<INetworkShareService> proxy = GetProxy();
147 if (proxy == nullptr) {
148 NETMGR_EXT_LOG_E("GetSharableRegexs proxy is nullptr");
149 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
150 }
151 return proxy->GetSharableRegexs(type, ifaceRegexs);
152 }
153
GetSharingState(const SharingIfaceType & type,SharingIfaceState & state)154 int32_t NetworkShareClient::GetSharingState(const SharingIfaceType &type, SharingIfaceState &state)
155 {
156 NETMGR_EXT_LOG_I("NetworkShare GetSharingState type= %{public}d.", type);
157 sptr<INetworkShareService> proxy = GetProxy();
158 if (proxy == nullptr) {
159 NETMGR_EXT_LOG_E("GetSharingState proxy is nullptr");
160 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
161 }
162 return proxy->GetSharingState(type, state);
163 }
164
GetSharingIfaces(const SharingIfaceState & state,std::vector<std::string> & ifaces)165 int32_t NetworkShareClient::GetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)
166 {
167 NETMGR_EXT_LOG_I("NetworkShare GetSharingIfaces type= %{public}d.", state);
168 sptr<INetworkShareService> proxy = GetProxy();
169 if (proxy == nullptr) {
170 NETMGR_EXT_LOG_E("GetSharingIfaces proxy is nullptr");
171 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
172 }
173 return proxy->GetNetSharingIfaces(state, ifaces);
174 }
175
GetStatsRxBytes(int32_t & bytes)176 int32_t NetworkShareClient::GetStatsRxBytes(int32_t &bytes)
177 {
178 NETMGR_EXT_LOG_I("NetworkShare GetStatsRxBytes.");
179 sptr<INetworkShareService> proxy = GetProxy();
180 if (proxy == nullptr) {
181 NETMGR_EXT_LOG_E("GetStatsRxBytes proxy is nullptr");
182 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
183 }
184 return proxy->GetStatsRxBytes(bytes);
185 }
186
GetStatsTxBytes(int32_t & bytes)187 int32_t NetworkShareClient::GetStatsTxBytes(int32_t &bytes)
188 {
189 NETMGR_EXT_LOG_I("NetworkShare GetStatsTxBytes.");
190 sptr<INetworkShareService> proxy = GetProxy();
191 if (proxy == nullptr) {
192 NETMGR_EXT_LOG_E("GetStatsTxBytes proxy is nullptr");
193 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
194 }
195 return proxy->GetStatsTxBytes(bytes);
196 }
197
GetStatsTotalBytes(int32_t & bytes)198 int32_t NetworkShareClient::GetStatsTotalBytes(int32_t &bytes)
199 {
200 NETMGR_EXT_LOG_I("NetworkShare GetStatsTotalBytes.");
201 sptr<INetworkShareService> proxy = GetProxy();
202 if (proxy == nullptr) {
203 NETMGR_EXT_LOG_E("GetStatsTotalBytes proxy is nullptr");
204 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
205 }
206 return proxy->GetStatsTotalBytes(bytes);
207 }
208
GetProxy()209 sptr<INetworkShareService> NetworkShareClient::GetProxy()
210 {
211 std::lock_guard locker(mutex_);
212 if (networkShareService_ != nullptr) {
213 return networkShareService_;
214 }
215 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
216 if (sam == nullptr) {
217 NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
218 return nullptr;
219 }
220 sptr<NetworkShareLoadCallback> callback = new (std::nothrow) NetworkShareLoadCallback;
221 if (sam->LoadSystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, callback) != 0) {
222 return nullptr;
223 }
224 {
225 std::unique_lock<std::mutex> uniqueLock(g_mutexCv);
226 g_cv.wait_for(uniqueLock, std::chrono::seconds(WAIT_REMOTE_TIME_SEC),
227 [&callback]() { return callback->GetRemoteObject() != nullptr || callback->IsFailed(); });
228 }
229
230 auto remote = callback->GetRemoteObject();
231 if (remote == nullptr) {
232 NETMGR_EXT_LOG_E("get Remote service failed");
233 return nullptr;
234 }
235
236 deathRecipient_ = new NetshareDeathRecipient(*this);
237 if (deathRecipient_ == nullptr) {
238 NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
239 return nullptr;
240 }
241 if (remote->IsProxyObject() && !remote->AddDeathRecipient(deathRecipient_)) {
242 NETMGR_EXT_LOG_E("add death recipient failed");
243 return nullptr;
244 }
245 networkShareService_ = iface_cast<INetworkShareService>(remote);
246 if (networkShareService_ == nullptr) {
247 NETMGR_EXT_LOG_E("get Remote service proxy failed");
248 return nullptr;
249 }
250 return networkShareService_;
251 }
252
RecoverCallback()253 void NetworkShareClient::RecoverCallback()
254 {
255 uint32_t count = 0;
256 while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
257 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
258 count++;
259 }
260 auto proxy = GetProxy();
261 NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
262 if (proxy != nullptr && callback_ != nullptr) {
263 int32_t ret = proxy->RegisterSharingEvent(callback_);
264 NETMGR_EXT_LOG_D("Register result %{public}d", ret);
265 }
266 }
267
OnRemoteDied(const wptr<IRemoteObject> & remote)268 void NetworkShareClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
269 {
270 if (remote == nullptr) {
271 NETMGR_EXT_LOG_E("remote object is nullptr");
272 return;
273 }
274 {
275 std::lock_guard lock(mutex_);
276 if (networkShareService_ == nullptr) {
277 NETMGR_EXT_LOG_E("networkShareService_ is nullptr");
278 return;
279 }
280 sptr<IRemoteObject> local = networkShareService_->AsObject();
281 if (local != remote.promote()) {
282 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
283 return;
284 }
285 local->RemoveDeathRecipient(deathRecipient_);
286 networkShareService_ = nullptr;
287 }
288
289 std::thread([this]() { this->RestartNetTetheringManagerSysAbility(); }).detach();
290
291 if (callback_ != nullptr) {
292 NETMGR_EXT_LOG_D("on remote died recover callback");
293 std::thread t([this]() {
294 RecoverCallback();
295 });
296 std::string threadName = "networkshareRecoverCallback";
297 pthread_setname_np(t.native_handle(), threadName.c_str());
298 t.detach();
299 }
300 }
301
RestartNetTetheringManagerSysAbility()302 void NetworkShareClient::RestartNetTetheringManagerSysAbility()
303 {
304 for (uint32_t i = 0; i < MAX_GET_SERVICE_COUNT; ++i) {
305 std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_S));
306 sptr<INetworkShareService> proxy = GetProxy();
307 if (proxy) {
308 NETMGR_EXT_LOG_I("Restart NetTetheringManager success.");
309 return;
310 }
311 }
312 NETMGR_EXT_LOG_E("Restart NetTetheringManager failed.");
313 }
314 } // namespace NetManagerStandard
315 } // namespace OHOS
316