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 <condition_variable>
16 #include <mutex>
17 #include <thread>
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "netmgr_ext_log_wrapper.h"
21 #include "net_manager_constants.h"
22 #include "wearable_distributed_net_client.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 static std::condition_variable g_cv;
27 static constexpr uint32_t WAIT_REMOTE_TIME_SEC = 10;
28 static constexpr uint32_t GET_SERVICE_MAX_TIMES = 5;
29 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_SEC = 10;
30
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)31 void WearableDistributedNetLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
32 const sptr<IRemoteObject> &remoteObject)
33 {
34 NETMGR_EXT_LOG_I("Loading system ability succeeded");
35 std::unique_lock<std::mutex> lock(loadMutex_);
36 remoteObject_ = remoteObject;
37 g_cv.notify_one();
38 }
39
OnLoadSystemAbilityFail(int32_t systemAbilityId)40 void WearableDistributedNetLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
41 {
42 NETMGR_EXT_LOG_I("Loading system ability failed");
43 loadSAFailed_ = true;
44 }
45
IsFailed()46 bool WearableDistributedNetLoadCallback::IsFailed()
47 {
48 return loadSAFailed_;
49 }
50
GetRemoteObject() const51 const sptr<IRemoteObject> &WearableDistributedNetLoadCallback::GetRemoteObject() const
52 {
53 return remoteObject_;
54 }
55
SetupWearableDistributedNet(const int32_t tcpPortId,const int32_t udpPortId,const bool isMetered)56 int32_t WearableDistributedNetClient::SetupWearableDistributedNet(const int32_t tcpPortId, const int32_t udpPortId,
57 const bool isMetered)
58 {
59 NETMGR_EXT_LOG_I("set up for WearableDistributedNet, isMetered:%{public}s", isMetered ? "true" : "false");
60 sptr<IWearableDistributedNet> proxy = GetProxy();
61 if (proxy == nullptr) {
62 NETMGR_EXT_LOG_E("SetupWearableDistributedNet fail, proxy is nullptr");
63 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
64 }
65 return proxy->SetupWearableDistributedNet(tcpPortId, udpPortId, isMetered);
66 }
67
TearDownWearableDistributedNet()68 int32_t WearableDistributedNetClient::TearDownWearableDistributedNet()
69 {
70 NETMGR_EXT_LOG_I("Wearable Distributed Net Client tear down");
71 sptr<IWearableDistributedNet> proxy = GetProxy();
72 if (proxy == nullptr) {
73 NETMGR_EXT_LOG_E("Tear down WearableDistributedNet fail, proxy is nullptr");
74 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
75 }
76 return proxy->TearDownWearableDistributedNet();
77 }
78
UpdateWearableDistributedNetMeteredStatus(const bool isMetered)79 int32_t WearableDistributedNetClient::UpdateWearableDistributedNetMeteredStatus(const bool isMetered)
80 {
81 NETMGR_EXT_LOG_I("update wearable distributed net metered status:%{public}s", isMetered ? "true" : "false");
82 sptr<IWearableDistributedNet> proxy = GetProxy();
83 if (proxy == nullptr) {
84 NETMGR_EXT_LOG_E("UpdateWearableDistributedNetMeteredStatus fail, proxy is nullptr");
85 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
86 }
87 return proxy->UpdateMeteredStatus(isMetered);
88 }
89
RestartWearableDistributedNetManagerSysAbility()90 void WearableDistributedNetClient::RestartWearableDistributedNetManagerSysAbility()
91 {
92 for (uint32_t retryCount = 0; retryCount < GET_SERVICE_MAX_TIMES; ++retryCount) {
93 sptr<IWearableDistributedNet> proxy = GetProxy();
94 std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_SEC));
95 if (proxy) {
96 NETMGR_EXT_LOG_I("Restart WearableDistributedNetManager success");
97 return;
98 }
99 }
100 NETMGR_EXT_LOG_E("Restart WearableDistributedNetManager failed after %{public}u attempts", GET_SERVICE_MAX_TIMES);
101 }
102
OnRemoteDied(const wptr<IRemoteObject> & remote)103 void WearableDistributedNetClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
104 {
105 NETMGR_EXT_LOG_I("WearableDistributedNetClient OnRemoteDied");
106 if (remote == nullptr) {
107 NETMGR_EXT_LOG_E("remote object is nullptr");
108 return;
109 }
110 std::lock_guard<std::mutex> lock(mutex_);
111 if (wearableDistributedNetService_ == nullptr) {
112 NETMGR_EXT_LOG_E("wearableDistributedNetService_ is nullptr");
113 return;
114 }
115 sptr<IRemoteObject> local = wearableDistributedNetService_->AsObject();
116 if (local != remote.promote()) {
117 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
118 return;
119 }
120 local->RemoveDeathRecipient(deathRecipient_);
121 wearableDistributedNetService_= nullptr;
122
123 std::thread([this]() { this->RestartWearableDistributedNetManagerSysAbility(); }).detach();
124 }
125
GetProxy()126 sptr<IWearableDistributedNet> WearableDistributedNetClient::GetProxy()
127 {
128 NETMGR_EXT_LOG_I("WearableDistributedNetClient GetProxy start");
129 std::lock_guard<std::mutex> lock(mutex_);
130 if (wearableDistributedNetService_) {
131 NETMGR_EXT_LOG_D("get proxy is ok");
132 return wearableDistributedNetService_;
133 }
134 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135 if (sam == nullptr) {
136 NETMGR_EXT_LOG_E("WearableDistributedNetClient get SystemAbilityManager failed: sam is null");
137 return nullptr;
138 }
139 sptr<WearableDistributedNetLoadCallback> callback = new (std::nothrow) WearableDistributedNetLoadCallback;
140 int32_t result = sam->LoadSystemAbility(COMM_WEARABLE_DISTRIBUTED_NET_ABILITY_ID, callback);
141 if (result != ERR_OK) {
142 NETMGR_EXT_LOG_E("LoadSystemAbility failed : [%{public}d]", result);
143 return nullptr;
144 }
145 {
146 std::unique_lock<std::mutex> uniqueLock(loadSaMutex_);
147 g_cv.wait_for(uniqueLock, std::chrono::seconds(WAIT_REMOTE_TIME_SEC),
148 [&callback]() { return callback->GetRemoteObject() != nullptr || callback->IsFailed(); });
149 }
150
151 auto remote = callback->GetRemoteObject();
152 if (remote == nullptr) {
153 NETMGR_EXT_LOG_E("get Remote service failed");
154 return nullptr;
155 }
156 deathRecipient_ = new (std::nothrow) WearableDistributedNetDeathRecipient(*this);
157 if (deathRecipient_ == nullptr) {
158 NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
159 return nullptr;
160 }
161 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
162 NETMGR_EXT_LOG_E("add death recipient failed");
163 return nullptr;
164 }
165 wearableDistributedNetService_ = iface_cast<IWearableDistributedNet>(remote);
166 if (wearableDistributedNetService_ == nullptr) {
167 NETMGR_EXT_LOG_E("get Remote service proxy failed");
168 return nullptr;
169 }
170 NETMGR_EXT_LOG_I("WearableDistributedNetClient GetProxy finish");
171 return wearableDistributedNetService_;
172 }
173
~WearableDistributedNetClient()174 WearableDistributedNetClient::~WearableDistributedNetClient()
175 {
176 auto proxy = GetProxy();
177 if (proxy == nullptr) {
178 return;
179 }
180
181 auto remote = proxy->AsObject();
182 if (remote == nullptr) {
183 return;
184 }
185
186 remote->RemoveDeathRecipient(deathRecipient_);
187 }
188 } // namespace NetManagerStandard
189 } // namespace OHOS
190