• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 MAX_GET_SERVICE_COUNT = 10;
31 std::condition_variable g_cv;
32 std::mutex g_mutexCv;
33 } // namespace
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)34 void NetworkShareLoadCallback::OnLoadSystemAbilitySuccess(
35     int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
36 {
37     NETMGR_EXT_LOG_D("OnLoadSystemAbilitySuccess systemAbilityId: [%{public}d]", systemAbilityId);
38     std::unique_lock<std::mutex> lock(g_mutexCv);
39     remoteObject_ = remoteObject;
40     g_cv.notify_one();
41 }
42 
OnLoadSystemAbilityFail(int32_t systemAbilityId)43 void NetworkShareLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
44 {
45     NETMGR_EXT_LOG_D("OnLoadSystemAbilityFail: [%{public}d]", systemAbilityId);
46     loadSAFailed_ = true;
47 }
48 
IsFailed()49 bool NetworkShareLoadCallback::IsFailed()
50 {
51     return loadSAFailed_;
52 }
53 
GetRemoteObject() const54 const sptr<IRemoteObject> &NetworkShareLoadCallback::GetRemoteObject() const
55 {
56     return remoteObject_;
57 }
58 
NetworkShareClient()59 NetworkShareClient::NetworkShareClient() : networkShareService_(nullptr), deathRecipient_(nullptr) {}
60 
~NetworkShareClient()61 NetworkShareClient::~NetworkShareClient() {}
62 
StartSharing(const SharingIfaceType & type)63 int32_t NetworkShareClient::StartSharing(const SharingIfaceType &type)
64 {
65     sptr<INetworkShareService> proxy = GetProxy();
66     if (proxy == nullptr) {
67         NETMGR_EXT_LOG_E("StartSharing proxy is nullptr");
68         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
69     }
70     return proxy->StartNetworkSharing(type);
71 }
72 
StopSharing(const SharingIfaceType & type)73 int32_t NetworkShareClient::StopSharing(const SharingIfaceType &type)
74 {
75     sptr<INetworkShareService> proxy = GetProxy();
76     if (proxy == nullptr) {
77         NETMGR_EXT_LOG_E("StopSharing proxy is nullptr");
78         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
79     }
80     return proxy->StopNetworkSharing(type);
81 }
82 
IsSharingSupported(int32_t & supported)83 int32_t NetworkShareClient::IsSharingSupported(int32_t &supported)
84 {
85     sptr<INetworkShareService> proxy = GetProxy();
86     if (proxy == nullptr) {
87         NETMGR_EXT_LOG_E("IsSharingSupported proxy is nullptr");
88         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
89     }
90     return proxy->IsNetworkSharingSupported(supported);
91 }
92 
IsSharing(int32_t & sharingStatus)93 int32_t NetworkShareClient::IsSharing(int32_t &sharingStatus)
94 {
95     sptr<INetworkShareService> proxy = GetProxy();
96     if (proxy == nullptr) {
97         NETMGR_EXT_LOG_E("IsSharing proxy is nullptr");
98         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
99     }
100     return proxy->IsSharing(sharingStatus);
101 }
102 
RegisterSharingEvent(sptr<ISharingEventCallback> callback)103 int32_t NetworkShareClient::RegisterSharingEvent(sptr<ISharingEventCallback> callback)
104 {
105     sptr<INetworkShareService> proxy = GetProxy();
106     if (proxy == nullptr) {
107         NETMGR_EXT_LOG_E("RegisterSharingEvent proxy is nullptr");
108         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
109     }
110     return proxy->RegisterSharingEvent(callback);
111 }
112 
UnregisterSharingEvent(sptr<ISharingEventCallback> callback)113 int32_t NetworkShareClient::UnregisterSharingEvent(sptr<ISharingEventCallback> callback)
114 {
115     sptr<INetworkShareService> proxy = GetProxy();
116     if (proxy == nullptr) {
117         NETMGR_EXT_LOG_E("UnregisterSharingEvent proxy is nullptr");
118         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
119     }
120     return proxy->UnregisterSharingEvent(callback);
121 }
122 
GetSharableRegexs(const SharingIfaceType & type,std::vector<std::string> & ifaceRegexs)123 int32_t NetworkShareClient::GetSharableRegexs(const SharingIfaceType &type, std::vector<std::string> &ifaceRegexs)
124 {
125     sptr<INetworkShareService> proxy = GetProxy();
126     if (proxy == nullptr) {
127         NETMGR_EXT_LOG_E("GetSharableRegexs proxy is nullptr");
128         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
129     }
130     return proxy->GetSharableRegexs(type, ifaceRegexs);
131 }
132 
GetSharingState(const SharingIfaceType & type,SharingIfaceState & state)133 int32_t NetworkShareClient::GetSharingState(const SharingIfaceType &type, SharingIfaceState &state)
134 {
135     sptr<INetworkShareService> proxy = GetProxy();
136     if (proxy == nullptr) {
137         NETMGR_EXT_LOG_E("GetSharingState proxy is nullptr");
138         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
139     }
140     return proxy->GetSharingState(type, state);
141 }
142 
GetSharingIfaces(const SharingIfaceState & state,std::vector<std::string> & ifaces)143 int32_t NetworkShareClient::GetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)
144 {
145     sptr<INetworkShareService> proxy = GetProxy();
146     if (proxy == nullptr) {
147         NETMGR_EXT_LOG_E("GetSharingIfaces proxy is nullptr");
148         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
149     }
150     return proxy->GetNetSharingIfaces(state, ifaces);
151 }
152 
GetStatsRxBytes(int32_t & bytes)153 int32_t NetworkShareClient::GetStatsRxBytes(int32_t &bytes)
154 {
155     sptr<INetworkShareService> proxy = GetProxy();
156     if (proxy == nullptr) {
157         NETMGR_EXT_LOG_E("GetStatsRxBytes proxy is nullptr");
158         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
159     }
160     return proxy->GetStatsRxBytes(bytes);
161 }
162 
GetStatsTxBytes(int32_t & bytes)163 int32_t NetworkShareClient::GetStatsTxBytes(int32_t &bytes)
164 {
165     sptr<INetworkShareService> proxy = GetProxy();
166     if (proxy == nullptr) {
167         NETMGR_EXT_LOG_E("GetStatsTxBytes proxy is nullptr");
168         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
169     }
170     return proxy->GetStatsTxBytes(bytes);
171 }
172 
GetStatsTotalBytes(int32_t & bytes)173 int32_t NetworkShareClient::GetStatsTotalBytes(int32_t &bytes)
174 {
175     sptr<INetworkShareService> proxy = GetProxy();
176     if (proxy == nullptr) {
177         NETMGR_EXT_LOG_E("GetStatsTotalBytes proxy is nullptr");
178         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
179     }
180     return proxy->GetStatsTotalBytes(bytes);
181 }
182 
GetProxy()183 sptr<INetworkShareService> NetworkShareClient::GetProxy()
184 {
185     std::lock_guard lock(mutex_);
186     if (networkShareService_ != nullptr) {
187         return networkShareService_;
188     }
189     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
190     if (sam == nullptr) {
191         NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
192         return nullptr;
193     }
194     sptr<NetworkShareLoadCallback> callback = new (std::nothrow) NetworkShareLoadCallback;
195     if (sam->LoadSystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, callback) != 0) {
196         return nullptr;
197     }
198     {
199         std::unique_lock<std::mutex> lock(g_mutexCv);
200         g_cv.wait_for(lock, std::chrono::seconds(WAIT_REMOTE_TIME_SEC),
201             [&callback]() { return callback->GetRemoteObject() != nullptr || callback->IsFailed(); });
202     }
203 
204     auto remote = callback->GetRemoteObject();
205     if (remote == nullptr || !remote->IsProxyObject()) {
206         NETMGR_EXT_LOG_E("get Remote service failed");
207         return nullptr;
208     }
209 
210     deathRecipient_ = new NetshareDeathRecipient(*this);
211     if (deathRecipient_ == nullptr) {
212         NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
213         return nullptr;
214     }
215     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
216         NETMGR_EXT_LOG_E("add death recipient failed");
217         return nullptr;
218     }
219     networkShareService_ = iface_cast<INetworkShareService>(remote);
220     if (networkShareService_ == nullptr) {
221         NETMGR_EXT_LOG_E("get Remote service proxy failed");
222         return nullptr;
223     }
224     return networkShareService_;
225 }
226 
OnRemoteDied(const wptr<IRemoteObject> & remote)227 void NetworkShareClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
228 {
229     if (remote == nullptr) {
230         NETMGR_EXT_LOG_E("remote object is nullptr");
231         return;
232     }
233     std::lock_guard lock(mutex_);
234     if (networkShareService_ == nullptr) {
235         NETMGR_EXT_LOG_E("networkShareService_ is nullptr");
236         return;
237     }
238     sptr<IRemoteObject> local = networkShareService_->AsObject();
239     if (local != remote.promote()) {
240         NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
241         return;
242     }
243     local->RemoveDeathRecipient(deathRecipient_);
244     networkShareService_ = nullptr;
245 
246     std::thread([this]() { this->RestartNetTetheringManagerSysAbility(); }).detach();
247 }
248 
RestartNetTetheringManagerSysAbility()249 void NetworkShareClient::RestartNetTetheringManagerSysAbility()
250 {
251     for (uint32_t i = 0; i < MAX_GET_SERVICE_COUNT; ++i) {
252         std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_S));
253         sptr<INetworkShareService> proxy = GetProxy();
254         if (proxy) {
255             NETMGR_EXT_LOG_I("Restart NetTetheringManager success.");
256             return;
257         }
258     }
259     NETMGR_EXT_LOG_E("Restart NetTetheringManager failed.");
260 }
261 } // namespace NetManagerStandard
262 } // namespace OHOS
263