• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetConfigureForShare(bool enabled)209 int32_t NetworkShareClient::SetConfigureForShare(bool enabled)
210 {
211     NETMGR_EXT_LOG_I("SetConfigureForShare NetworkShare.");
212     sptr<INetworkShareService> proxy = GetProxy();
213     if (proxy == nullptr) {
214         NETMGR_EXT_LOG_E("SetConfigureForShare proxy is nullptr");
215         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
216     }
217     return proxy->SetConfigureForShare(enabled);
218 }
219 
GetProxy()220 sptr<INetworkShareService> NetworkShareClient::GetProxy()
221 {
222     std::lock_guard locker(mutex_);
223     if (networkShareService_ != nullptr) {
224         return networkShareService_;
225     }
226     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
227     if (sam == nullptr) {
228         NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
229         return nullptr;
230     }
231     sptr<NetworkShareLoadCallback> callback = new (std::nothrow) NetworkShareLoadCallback;
232     if (sam->LoadSystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, callback) != 0) {
233         return nullptr;
234     }
235     {
236         std::unique_lock<std::mutex> uniqueLock(g_mutexCv);
237         g_cv.wait_for(uniqueLock, std::chrono::seconds(WAIT_REMOTE_TIME_SEC),
238             [&callback]() { return callback->GetRemoteObject() != nullptr || callback->IsFailed(); });
239     }
240 
241     auto remote = callback->GetRemoteObject();
242     if (remote == nullptr) {
243         NETMGR_EXT_LOG_E("get Remote service failed");
244         return nullptr;
245     }
246 
247     deathRecipient_ = new NetshareDeathRecipient(*this);
248     if (deathRecipient_ == nullptr) {
249         NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
250         return nullptr;
251     }
252     if (remote->IsProxyObject() && !remote->AddDeathRecipient(deathRecipient_)) {
253         NETMGR_EXT_LOG_E("add death recipient failed");
254         return nullptr;
255     }
256     networkShareService_ = iface_cast<INetworkShareService>(remote);
257     if (networkShareService_ == nullptr) {
258         NETMGR_EXT_LOG_E("get Remote service proxy failed");
259         return nullptr;
260     }
261     return networkShareService_;
262 }
263 
RecoverCallback()264 void NetworkShareClient::RecoverCallback()
265 {
266     uint32_t count = 0;
267     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
268         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
269         count++;
270     }
271     auto proxy = GetProxy();
272     NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
273     if (proxy != nullptr && callback_ != nullptr) {
274         int32_t ret = proxy->RegisterSharingEvent(callback_);
275         NETMGR_EXT_LOG_D("Register result %{public}d", ret);
276     }
277 }
278 
OnRemoteDied(const wptr<IRemoteObject> & remote)279 void NetworkShareClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
280 {
281     if (remote == nullptr) {
282         NETMGR_EXT_LOG_E("remote object is nullptr");
283         return;
284     }
285     {
286         std::lock_guard lock(mutex_);
287         if (networkShareService_ == nullptr) {
288             NETMGR_EXT_LOG_E("networkShareService_ is nullptr");
289             return;
290         }
291         sptr<IRemoteObject> local = networkShareService_->AsObject();
292         if (local != remote.promote()) {
293             NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
294             return;
295         }
296         local->RemoveDeathRecipient(deathRecipient_);
297         networkShareService_ = nullptr;
298     }
299 
300     std::thread([this]() { this->RestartNetTetheringManagerSysAbility(); }).detach();
301 
302     if (callback_ != nullptr) {
303         NETMGR_EXT_LOG_D("on remote died recover callback");
304         std::thread t([this]() {
305             RecoverCallback();
306         });
307         std::string threadName = "networkshareRecoverCallback";
308         pthread_setname_np(t.native_handle(), threadName.c_str());
309         t.detach();
310     }
311 }
312 
RestartNetTetheringManagerSysAbility()313 void NetworkShareClient::RestartNetTetheringManagerSysAbility()
314 {
315     for (uint32_t i = 0; i < MAX_GET_SERVICE_COUNT; ++i) {
316         std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_S));
317         sptr<INetworkShareService> proxy = GetProxy();
318         if (proxy) {
319             NETMGR_EXT_LOG_I("Restart NetTetheringManager success.");
320             return;
321         }
322     }
323     NETMGR_EXT_LOG_E("Restart NetTetheringManager failed.");
324 }
325 } // namespace NetManagerStandard
326 } // namespace OHOS
327