• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
EnableWearableDistributedNet(bool enableFlag)68 int32_t WearableDistributedNetClient::EnableWearableDistributedNet(bool enableFlag)
69 {
70     NETMGR_EXT_LOG_I("enable WearableDistributedNet : %{public}u", enableFlag);
71     sptr<IWearableDistributedNet> proxy = GetProxy();
72     if (proxy == nullptr) {
73         NETMGR_EXT_LOG_E("SetupWearableDistributedNet fail, proxy is nullptr");
74         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
75     }
76     return proxy->EnableWearableDistributedNet(enableFlag);
77 }
78 
TearDownWearableDistributedNet()79 int32_t WearableDistributedNetClient::TearDownWearableDistributedNet()
80 {
81     NETMGR_EXT_LOG_I("Wearable Distributed Net Client tear down");
82     sptr<IWearableDistributedNet> proxy = GetProxy();
83     if (proxy == nullptr) {
84         NETMGR_EXT_LOG_E("Tear down WearableDistributedNet fail, proxy is nullptr");
85         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
86     }
87     return proxy->TearDownWearableDistributedNet();
88 }
89 
UpdateWearableDistributedNetMeteredStatus(const bool isMetered)90 int32_t WearableDistributedNetClient::UpdateWearableDistributedNetMeteredStatus(const bool isMetered)
91 {
92     NETMGR_EXT_LOG_I("update wearable distributed net metered status:%{public}s", isMetered ? "true" : "false");
93     sptr<IWearableDistributedNet> proxy = GetProxy();
94     if (proxy == nullptr) {
95         NETMGR_EXT_LOG_E("UpdateWearableDistributedNetMeteredStatus fail, proxy is nullptr");
96         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
97     }
98     return proxy->UpdateMeteredStatus(isMetered);
99 }
100 
RestartWearableDistributedNetManagerSysAbility()101 void WearableDistributedNetClient::RestartWearableDistributedNetManagerSysAbility()
102 {
103     for (uint32_t retryCount = 0; retryCount < GET_SERVICE_MAX_TIMES; ++retryCount) {
104         sptr<IWearableDistributedNet> proxy = GetProxy();
105         std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_SEC));
106         if (proxy) {
107             NETMGR_EXT_LOG_I("Restart WearableDistributedNetManager success");
108             return;
109         }
110     }
111     NETMGR_EXT_LOG_E("Restart WearableDistributedNetManager failed after %{public}u attempts", GET_SERVICE_MAX_TIMES);
112 }
113 
OnRemoteDied(const wptr<IRemoteObject> & remote)114 void WearableDistributedNetClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
115 {
116     NETMGR_EXT_LOG_I("WearableDistributedNetClient OnRemoteDied");
117     if (remote == nullptr) {
118         NETMGR_EXT_LOG_E("remote object is nullptr");
119         return;
120     }
121     std::lock_guard<std::mutex> lock(mutex_);
122     if (wearableDistributedNetService_ == nullptr) {
123         NETMGR_EXT_LOG_E("wearableDistributedNetService_ is nullptr");
124         return;
125     }
126     sptr<IRemoteObject> local = wearableDistributedNetService_->AsObject();
127     if (local != remote.promote()) {
128         NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
129         return;
130     }
131     local->RemoveDeathRecipient(deathRecipient_);
132     wearableDistributedNetService_= nullptr;
133 
134     std::thread([this]() { this->RestartWearableDistributedNetManagerSysAbility(); }).detach();
135 }
136 
GetProxy()137 sptr<IWearableDistributedNet> WearableDistributedNetClient::GetProxy()
138 {
139     NETMGR_EXT_LOG_I("WearableDistributedNetClient GetProxy start");
140     std::lock_guard<std::mutex> lock(mutex_);
141     if (wearableDistributedNetService_) {
142         NETMGR_EXT_LOG_D("get proxy is ok");
143         return wearableDistributedNetService_;
144     }
145     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
146     if (sam == nullptr) {
147         NETMGR_EXT_LOG_E("WearableDistributedNetClient get SystemAbilityManager failed: sam is null");
148         return nullptr;
149     }
150     sptr<WearableDistributedNetLoadCallback> callback = new (std::nothrow) WearableDistributedNetLoadCallback;
151     if (callback == nullptr) {
152         NETMGR_EXT_LOG_E("Failed to create WearableDistributedNetLoadCallback instance");
153         return nullptr;
154     }
155     int32_t result = sam->LoadSystemAbility(COMM_WEARABLE_DISTRIBUTED_NET_ABILITY_ID, callback);
156     if (result != ERR_OK) {
157         NETMGR_EXT_LOG_E("LoadSystemAbility failed : [%{public}d]", result);
158         return nullptr;
159     }
160     {
161         std::unique_lock<std::mutex> uniqueLock(loadSaMutex_);
162         g_cv.wait_for(uniqueLock, std::chrono::seconds(WAIT_REMOTE_TIME_SEC),
163             [&callback]() { return callback->GetRemoteObject() != nullptr || callback->IsFailed(); });
164     }
165 
166     auto remote = callback->GetRemoteObject();
167     if (remote == nullptr) {
168         NETMGR_EXT_LOG_E("get Remote service failed");
169         return nullptr;
170     }
171     deathRecipient_ = new (std::nothrow) WearableDistributedNetDeathRecipient(*this);
172     if (deathRecipient_ == nullptr) {
173         NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
174         return nullptr;
175     }
176     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
177         NETMGR_EXT_LOG_E("add death recipient failed");
178         return nullptr;
179     }
180     wearableDistributedNetService_ = iface_cast<IWearableDistributedNet>(remote);
181     if (wearableDistributedNetService_ == nullptr) {
182         NETMGR_EXT_LOG_E("get Remote service proxy failed");
183         return nullptr;
184     }
185     NETMGR_EXT_LOG_I("WearableDistributedNetClient GetProxy finish");
186     return wearableDistributedNetService_;
187 }
188 
~WearableDistributedNetClient()189 WearableDistributedNetClient::~WearableDistributedNetClient()
190 {
191     auto proxy = GetProxy();
192     if (proxy == nullptr) {
193         return;
194     }
195 
196     auto remote = proxy->AsObject();
197     if (remote == nullptr) {
198         return;
199     }
200 
201     remote->RemoveDeathRecipient(deathRecipient_);
202 }
203 } // namespace NetManagerStandard
204 } // namespace OHOS
205