• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "ethernet_client.h"
17 
18 #include <thread>
19 
20 #include "i_ethernet_service.h"
21 #include "if_system_ability_manager.h"
22 #include "interface_configuration.h"
23 #include "ipc_types.h"
24 #include "iremote_broker.h"
25 #include "iremote_object.h"
26 #include "iservice_registry.h"
27 #include "net_manager_constants.h"
28 #include "netmgr_ext_log_wrapper.h"
29 #include "refbase.h"
30 #include "system_ability_definition.h"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 
35 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
36 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
37 
EthernetClient()38 EthernetClient::EthernetClient() : ethernetService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
39 
40 EthernetClient::~EthernetClient() = default;
41 
SetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ic)42 int32_t EthernetClient::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic)
43 {
44     sptr<IEthernetService> proxy = GetProxy();
45     if (proxy == nullptr) {
46         NETMGR_EXT_LOG_E("proxy is nullptr");
47         return IPC_PROXY_ERR;
48     }
49     return proxy->SetIfaceConfig(iface, ic);
50 }
51 
GetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)52 int32_t EthernetClient::GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
53 {
54     sptr<IEthernetService> proxy = GetProxy();
55     if (proxy == nullptr) {
56         NETMGR_EXT_LOG_E("proxy is nullptr");
57         return IPC_PROXY_ERR;
58     }
59     return proxy->GetIfaceConfig(iface, ifaceConfig);
60 }
61 
IsIfaceActive(const std::string & iface,int32_t & activeStatus)62 int32_t EthernetClient::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
63 {
64     sptr<IEthernetService> proxy = GetProxy();
65     if (proxy == nullptr) {
66         NETMGR_EXT_LOG_E("proxy is nullptr");
67         return IPC_PROXY_ERR;
68     }
69     return proxy->IsIfaceActive(iface, activeStatus);
70 }
71 
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)72 int32_t EthernetClient::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
73 {
74     sptr<IEthernetService> proxy = GetProxy();
75     if (proxy == nullptr) {
76         NETMGR_EXT_LOG_E("proxy is nullptr");
77         return IPC_PROXY_ERR;
78     }
79     return proxy->GetAllActiveIfaces(activeIfaces);
80 }
81 
ResetFactory()82 int32_t EthernetClient::ResetFactory()
83 {
84     sptr<IEthernetService> proxy = GetProxy();
85     if (proxy == nullptr) {
86         NETMGR_EXT_LOG_E("proxy is nullptr");
87         return IPC_PROXY_ERR;
88     }
89     return proxy->ResetFactory();
90 }
91 
GetProxy()92 sptr<IEthernetService> EthernetClient::GetProxy()
93 {
94     std::lock_guard lock(mutex_);
95     if (ethernetService_) {
96         NETMGR_EXT_LOG_D("get proxy is ok");
97         return ethernetService_;
98     }
99     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
100     if (sam == nullptr) {
101         NETMGR_EXT_LOG_E("GetProxy, get SystemAbilityManager failed");
102         return nullptr;
103     }
104     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_ETHERNET_MANAGER_SYS_ABILITY_ID);
105     if (remote == nullptr) {
106         NETMGR_EXT_LOG_E("get Remote service failed");
107         return nullptr;
108     }
109     deathRecipient_ = new (std::nothrow) EthernetDeathRecipient(*this);
110     if (deathRecipient_ == nullptr) {
111         NETMGR_EXT_LOG_E("Recipient new failed!");
112     }
113     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
114         NETMGR_EXT_LOG_E("add death recipient failed");
115         return nullptr;
116     }
117     ethernetService_ = iface_cast<IEthernetService>(remote);
118     if (ethernetService_ == nullptr) {
119         NETMGR_EXT_LOG_E("get Remote service proxy failed");
120         return nullptr;
121     }
122     return ethernetService_;
123 }
124 
RecoverCallback()125 void EthernetClient::RecoverCallback()
126 {
127     uint32_t count = 0;
128     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
129         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
130         count++;
131     }
132     auto proxy = GetProxy();
133     NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
134     if (proxy != nullptr && callback_ != nullptr) {
135         int32_t ret = proxy->RegisterIfacesStateChanged(callback_);
136         NETMGR_EXT_LOG_D("Register result %{public}d", ret);
137     }
138 }
139 
OnRemoteDied(const wptr<IRemoteObject> & remote)140 void EthernetClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
141 {
142     if (remote == nullptr) {
143         NETMGR_EXT_LOG_E("remote object is nullptr");
144         return;
145     }
146     std::lock_guard lock(mutex_);
147     if (ethernetService_ == nullptr) {
148         NETMGR_EXT_LOG_E("ethernetService_ is nullptr");
149         return;
150     }
151     sptr<IRemoteObject> local = ethernetService_->AsObject();
152     if (local == nullptr) {
153         NETMGR_EXT_LOG_E("local is nullptr");
154         return;
155     }
156     if (local != remote.promote()) {
157         NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
158         return;
159     }
160     local->RemoveDeathRecipient(deathRecipient_);
161     ethernetService_ = nullptr;
162 
163     if (callback_ != nullptr) {
164         NETMGR_EXT_LOG_D("on remote died recover callback");
165         std::thread t([this]() {
166             RecoverCallback();
167         });
168         std::string threadName = "ethernetRecoverCallback";
169         pthread_setname_np(t.native_handle(), threadName.c_str());
170         t.detach();
171     }
172 }
173 
RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> & callback)174 int32_t EthernetClient::RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback)
175 {
176     NETMGR_EXT_LOG_D("RegisterIfacesStateChanged client in.");
177     sptr<IEthernetService> proxy = GetProxy();
178     if (proxy == nullptr) {
179         NETMGR_EXT_LOG_E("proxy is nullptr");
180         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
181     }
182     int32_t ret = proxy->RegisterIfacesStateChanged(callback);
183     if (ret == NETMANAGER_EXT_SUCCESS) {
184         NETMGR_EXT_LOG_D("RegisterIfacesStateChanged success, save callback.");
185         callback_ = callback;
186     }
187 
188     return ret;
189 }
190 
UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> & callback)191 int32_t EthernetClient::UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback)
192 {
193     NETMGR_EXT_LOG_D("UnRegisterIfacesStateChanged client in.");
194     sptr<IEthernetService> proxy = GetProxy();
195     if (proxy == nullptr) {
196         NETMGR_EXT_LOG_E("proxy is nullptr");
197         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
198     }
199     int32_t ret = proxy->UnregisterIfacesStateChanged(callback);
200     if (ret == NETMANAGER_EXT_SUCCESS) {
201         NETMGR_EXT_LOG_D("UnRegisterIfacesStateChanged success, delete callback.");
202         callback_ = nullptr;
203     }
204 
205     return ret;
206 }
207 
SetInterfaceUp(const std::string & iface)208 int32_t EthernetClient::SetInterfaceUp(const std::string &iface)
209 {
210     sptr<IEthernetService> proxy = GetProxy();
211     if (proxy == nullptr) {
212         NETMGR_EXT_LOG_E("proxy is nullptr");
213         return IPC_PROXY_ERR;
214     }
215     return proxy->SetInterfaceUp(iface);
216 }
217 
SetInterfaceDown(const std::string & iface)218 int32_t EthernetClient::SetInterfaceDown(const std::string &iface)
219 {
220     sptr<IEthernetService> proxy = GetProxy();
221     if (proxy == nullptr) {
222         NETMGR_EXT_LOG_E("proxy is nullptr");
223         return IPC_PROXY_ERR;
224     }
225     return proxy->SetInterfaceDown(iface);
226 }
227 
GetInterfaceConfig(const std::string & iface,OHOS::nmd::InterfaceConfigurationParcel & cfg)228 int32_t EthernetClient::GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg)
229 {
230     sptr<IEthernetService> proxy = GetProxy();
231     if (proxy == nullptr) {
232         NETMGR_EXT_LOG_E("proxy is nullptr");
233         return IPC_PROXY_ERR;
234     }
235     return proxy->GetInterfaceConfig(iface, cfg);
236 }
237 
SetInterfaceConfig(const std::string & iface,OHOS::nmd::InterfaceConfigurationParcel & cfg)238 int32_t EthernetClient::SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg)
239 {
240     sptr<IEthernetService> proxy = GetProxy();
241     if (proxy == nullptr) {
242         NETMGR_EXT_LOG_E("proxy is nullptr");
243         return IPC_PROXY_ERR;
244     }
245     return proxy->SetInterfaceConfig(iface, cfg);
246 }
247 } // namespace NetManagerStandard
248 } // namespace OHOS
249