• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "ethernet_client.h"
17 
18 #include <thread>
19 
20 #include "iethernet_service.h"
21 #include "if_system_ability_manager.h"
22 #include "mac_address_info.h"
23 #include "interface_configuration.h"
24 #include "ipc_types.h"
25 #include "iremote_broker.h"
26 #include "iremote_object.h"
27 #include "iservice_registry.h"
28 #include "net_manager_constants.h"
29 #include "netmgr_ext_log_wrapper.h"
30 #include "refbase.h"
31 #include "system_ability_definition.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 
36 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
37 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
38 
EthernetClient()39 EthernetClient::EthernetClient() : ethernetService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
40 
~EthernetClient()41 EthernetClient::~EthernetClient()
42 {
43     NETMGR_EXT_LOG_I("~EthernetClient : Destroy EthernetClient");
44     sptr<IEthernetService> proxy = GetProxy();
45     if (proxy == nullptr) {
46         return;
47     }
48 
49     auto serviceRemote = proxy->AsObject();
50     if (serviceRemote == nullptr) {
51         return;
52     }
53     if (deathRecipient_) {
54         serviceRemote->RemoveDeathRecipient(deathRecipient_);
55     }
56 }
57 
GetMacAddress(std::vector<MacAddressInfo> & macAddrList)58 int32_t EthernetClient::GetMacAddress(std::vector<MacAddressInfo> &macAddrList)
59 {
60     sptr<IEthernetService> proxy = GetProxy();
61     if (proxy == nullptr) {
62         NETMGR_EXT_LOG_E("proxy is nullptr");
63         return IPC_PROXY_ERR;
64     }
65     return proxy->GetMacAddress(macAddrList);
66 }
67 
SetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ic)68 int32_t EthernetClient::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic)
69 {
70     sptr<IEthernetService> proxy = GetProxy();
71     if (proxy == nullptr) {
72         NETMGR_EXT_LOG_E("proxy is nullptr");
73         return IPC_PROXY_ERR;
74     }
75     return proxy->SetIfaceConfig(iface, ic);
76 }
77 
GetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)78 int32_t EthernetClient::GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
79 {
80     sptr<IEthernetService> proxy = GetProxy();
81     if (proxy == nullptr) {
82         NETMGR_EXT_LOG_E("proxy is nullptr");
83         return IPC_PROXY_ERR;
84     }
85     return proxy->GetIfaceConfig(iface, ifaceConfig);
86 }
87 
IsIfaceActive(const std::string & iface,int32_t & activeStatus)88 int32_t EthernetClient::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
89 {
90     sptr<IEthernetService> proxy = GetProxy();
91     if (proxy == nullptr) {
92         NETMGR_EXT_LOG_E("proxy is nullptr");
93         return IPC_PROXY_ERR;
94     }
95     return proxy->IsIfaceActive(iface, activeStatus);
96 }
97 
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)98 int32_t EthernetClient::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
99 {
100     sptr<IEthernetService> proxy = GetProxy();
101     if (proxy == nullptr) {
102         NETMGR_EXT_LOG_E("proxy is nullptr");
103         return IPC_PROXY_ERR;
104     }
105     return proxy->GetAllActiveIfaces(activeIfaces);
106 }
107 
ResetFactory()108 int32_t EthernetClient::ResetFactory()
109 {
110     sptr<IEthernetService> proxy = GetProxy();
111     if (proxy == nullptr) {
112         NETMGR_EXT_LOG_E("proxy is nullptr");
113         return IPC_PROXY_ERR;
114     }
115     return proxy->ResetFactory();
116 }
117 
GetProxy()118 sptr<IEthernetService> EthernetClient::GetProxy()
119 {
120     std::lock_guard lock(mutex_);
121     if (ethernetService_) {
122         NETMGR_EXT_LOG_D("get proxy is ok");
123         return ethernetService_;
124     }
125     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
126     if (sam == nullptr) {
127         NETMGR_EXT_LOG_E("GetProxy, get SystemAbilityManager failed");
128         return nullptr;
129     }
130     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_ETHERNET_MANAGER_SYS_ABILITY_ID);
131     if (remote == nullptr) {
132         NETMGR_EXT_LOG_E("get Remote service failed");
133         return nullptr;
134     }
135     deathRecipient_ = sptr<EthernetDeathRecipient>::MakeSptr(*this);
136     if (deathRecipient_ == nullptr) {
137         NETMGR_EXT_LOG_E("Recipient new failed!");
138     }
139     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
140         NETMGR_EXT_LOG_E("add death recipient failed");
141         return nullptr;
142     }
143     ethernetService_ = iface_cast<IEthernetService>(remote);
144     if (ethernetService_ == nullptr) {
145         NETMGR_EXT_LOG_E("get Remote service proxy failed");
146         return nullptr;
147     }
148     return ethernetService_;
149 }
150 
RecoverCallback()151 void EthernetClient::RecoverCallback()
152 {
153     uint32_t count = 0;
154     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
155         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
156         count++;
157     }
158     auto proxy = GetProxy();
159     NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
160     if (proxy != nullptr && callback_ != nullptr) {
161         int32_t ret = proxy->RegisterIfacesStateChanged(callback_);
162         NETMGR_EXT_LOG_D("Register result %{public}d", ret);
163     }
164 }
165 
OnRemoteDied(const wptr<IRemoteObject> & remote)166 void EthernetClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
167 {
168     if (remote == nullptr) {
169         NETMGR_EXT_LOG_E("remote object is nullptr");
170         return;
171     }
172     std::lock_guard lock(mutex_);
173     if (ethernetService_ == nullptr) {
174         NETMGR_EXT_LOG_E("ethernetService_ is nullptr");
175         return;
176     }
177     sptr<IRemoteObject> local = ethernetService_->AsObject();
178     if (local == nullptr) {
179         NETMGR_EXT_LOG_E("local is nullptr");
180         return;
181     }
182     if (local != remote.promote()) {
183         NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
184         return;
185     }
186     local->RemoveDeathRecipient(deathRecipient_);
187     ethernetService_ = nullptr;
188 
189     if (callback_ != nullptr) {
190         NETMGR_EXT_LOG_D("on remote died recover callback");
191         std::thread t([this]() {
192             RecoverCallback();
193         });
194         std::string threadName = "ethernetRecoverCallback";
195         pthread_setname_np(t.native_handle(), threadName.c_str());
196         t.detach();
197     }
198 }
199 
RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> & callback)200 int32_t EthernetClient::RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback)
201 {
202     NETMGR_EXT_LOG_D("RegisterIfacesStateChanged client in.");
203     sptr<IEthernetService> proxy = GetProxy();
204     if (proxy == nullptr) {
205         NETMGR_EXT_LOG_E("proxy is nullptr");
206         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
207     }
208     int32_t ret = proxy->RegisterIfacesStateChanged(callback);
209     if (ret == NETMANAGER_EXT_SUCCESS) {
210         NETMGR_EXT_LOG_D("RegisterIfacesStateChanged success, save callback.");
211         callback_ = callback;
212     }
213 
214     return ret;
215 }
216 
UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> & callback)217 int32_t EthernetClient::UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback)
218 {
219     NETMGR_EXT_LOG_D("UnRegisterIfacesStateChanged client in.");
220     sptr<IEthernetService> proxy = GetProxy();
221     if (proxy == nullptr) {
222         NETMGR_EXT_LOG_E("proxy is nullptr");
223         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
224     }
225     int32_t ret = proxy->UnregisterIfacesStateChanged(callback);
226     if (ret == NETMANAGER_EXT_SUCCESS) {
227         NETMGR_EXT_LOG_D("UnRegisterIfacesStateChanged success, delete callback.");
228         callback_ = nullptr;
229     }
230 
231     return ret;
232 }
233 
SetInterfaceUp(const std::string & iface)234 int32_t EthernetClient::SetInterfaceUp(const std::string &iface)
235 {
236     sptr<IEthernetService> proxy = GetProxy();
237     if (proxy == nullptr) {
238         NETMGR_EXT_LOG_E("proxy is nullptr");
239         return IPC_PROXY_ERR;
240     }
241     return proxy->SetInterfaceUp(iface);
242 }
243 
SetInterfaceDown(const std::string & iface)244 int32_t EthernetClient::SetInterfaceDown(const std::string &iface)
245 {
246     sptr<IEthernetService> proxy = GetProxy();
247     if (proxy == nullptr) {
248         NETMGR_EXT_LOG_E("proxy is nullptr");
249         return IPC_PROXY_ERR;
250     }
251     return proxy->SetInterfaceDown(iface);
252 }
253 
GetInterfaceConfig(const std::string & iface,OHOS::nmd::InterfaceConfigurationParcel & cfg)254 int32_t EthernetClient::GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg)
255 {
256     sptr<IEthernetService> proxy = GetProxy();
257     if (proxy == nullptr) {
258         NETMGR_EXT_LOG_E("proxy is nullptr");
259         return IPC_PROXY_ERR;
260     }
261     ConfigurationParcelIpc configIpc;
262     int32_t ret = proxy->GetInterfaceConfig(iface, configIpc);
263     ConfigurationParcelIpc::ConvertEtherConfigParcelToNmd(configIpc, cfg);
264     return ret;
265 }
266 
SetInterfaceConfig(const std::string & iface,OHOS::nmd::InterfaceConfigurationParcel & cfg)267 int32_t EthernetClient::SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg)
268 {
269     sptr<IEthernetService> proxy = GetProxy();
270     if (proxy == nullptr) {
271         NETMGR_EXT_LOG_E("proxy is nullptr");
272         return IPC_PROXY_ERR;
273     }
274     ConfigurationParcelIpc configIpc;
275     ConfigurationParcelIpc::ConvertNmdToEtherConfigParcel(configIpc, cfg);
276     return proxy->SetInterfaceConfig(iface, configIpc);
277 }
278 
RegCustomEapHandler(NetType netType,const std::string & regCmd,const sptr<INetEapPostbackCallback> & callback)279 int32_t EthernetClient::RegCustomEapHandler(NetType netType, const std::string &regCmd,
280     const sptr<INetEapPostbackCallback> &callback)
281 {
282 #ifdef NET_EXTENSIBLE_AUTHENTICATION
283     if (callback == nullptr) {
284         NETMGR_EXT_LOG_E("%{public}s callback is nullptr.", __func__);
285         return EAP_ERRCODE_INTERNAL_ERROR;
286     }
287     sptr<IEthernetService> proxy = GetProxy();
288     if (proxy == nullptr) {
289         NETMGR_EXT_LOG_E("%{public}s proxy is nullptr.", __func__);
290         return EAP_ERRCODE_NETMANAGER_STOP;
291     }
292     return proxy->RegCustomEapHandler(static_cast<int>(netType), regCmd, callback);
293 #else
294     return NETMANAGER_SUCCESS;
295 #endif
296 }
297 
ReplyCustomEapData(int result,const sptr<EapData> & eapData)298 int32_t EthernetClient::ReplyCustomEapData(int result, const sptr<EapData> &eapData)
299 {
300 #ifdef NET_EXTENSIBLE_AUTHENTICATION
301     if (eapData == nullptr) {
302         NETMGR_EXT_LOG_E("%{public}s, eapData is nullptr", __func__);
303         return EAP_ERRCODE_INTERNAL_ERROR;
304     }
305     sptr<IEthernetService> proxy = GetProxy();
306     if (proxy == nullptr) {
307         NETMGR_EXT_LOG_E("%{public}s, proxy is nullptr.", __func__);
308         return EAP_ERRCODE_NETMANAGER_STOP;
309     }
310     return proxy->ReplyCustomEapData(result, eapData);
311 #else
312     return NETMANAGER_SUCCESS;
313 #endif
314 }
315 
RegisterCustomEapCallback(const NetType netType,const sptr<INetRegisterEapCallback> & callback)316 int32_t EthernetClient::RegisterCustomEapCallback(const NetType netType, const sptr<INetRegisterEapCallback> &callback)
317 {
318 #ifdef NET_EXTENSIBLE_AUTHENTICATION
319     sptr<IEthernetService> proxy = GetProxy();
320     if (proxy == nullptr) {
321         NETMGR_EXT_LOG_E("%{public}s proxy is nullptr.", __func__);
322         return NETMANAGER_ERR_GET_PROXY_FAIL;
323     }
324     return proxy->RegisterCustomEapCallback(static_cast<int>(netType), callback);
325 #else
326     return NETMANAGER_SUCCESS;
327 #endif
328 }
329 
UnRegisterCustomEapCallback(const NetType netType,const sptr<INetRegisterEapCallback> & callback)330 int32_t EthernetClient::UnRegisterCustomEapCallback(const NetType netType,
331     const sptr<INetRegisterEapCallback> &callback)
332 {
333 #ifdef NET_EXTENSIBLE_AUTHENTICATION
334     sptr<IEthernetService> proxy = GetProxy();
335     if (proxy == nullptr) {
336         NETMGR_EXT_LOG_E("%{public}s proxy is nullptr.", __func__);
337         return NETMANAGER_ERR_GET_PROXY_FAIL;
338     }
339 
340     return proxy->UnRegisterCustomEapCallback(static_cast<int>(netType), callback);
341 #else
342     return NETMANAGER_SUCCESS;
343 #endif
344 }
345 
NotifyWpaEapInterceptInfo(const NetType netType,const sptr<EapData> & eapData)346 int32_t EthernetClient::NotifyWpaEapInterceptInfo(const NetType netType, const sptr<EapData> &eapData)
347 {
348 #ifdef NET_EXTENSIBLE_AUTHENTICATION
349     if (eapData == nullptr) {
350         NETMGR_EXT_LOG_E("%{public}s eapData is nullptr.", __func__);
351         return NETMANAGER_ERR_LOCAL_PTR_NULL;
352     }
353     sptr<IEthernetService> proxy = GetProxy();
354     if (proxy == nullptr) {
355         NETMGR_EXT_LOG_E("%{public}s proxy is nullptr.", __func__);
356         return NETMANAGER_ERR_GET_PROXY_FAIL;
357     }
358 
359     return proxy->NotifyWpaEapInterceptInfo(static_cast<int>(netType), eapData);
360 #else
361     return NETMANAGER_SUCCESS;
362 #endif
363 }
364 
GetDeviceInformation(std::vector<EthernetDeviceInfo> & deviceInfoList)365 int32_t EthernetClient::GetDeviceInformation(std::vector<EthernetDeviceInfo> &deviceInfoList)
366 {
367     sptr<IEthernetService> proxy = GetProxy();
368     if (proxy == nullptr) {
369         NETMGR_EXT_LOG_E("proxy is nullptr");
370         return IPC_PROXY_ERR;
371     }
372     return proxy->GetDeviceInformation(deviceInfoList);
373 }
374 
StartEthEap(int32_t netId,const EthEapProfile & profile)375 int32_t EthernetClient::StartEthEap(int32_t netId, const EthEapProfile& profile)
376 {
377 #ifdef NET_EXTENSIBLE_AUTHENTICATION
378     sptr<IEthernetService> proxy = GetProxy();
379     if (proxy == nullptr) {
380         NETMGR_EXT_LOG_E("proxy is nullptr");
381         return EAP_ERRCODE_NETMANAGER_STOP;
382     }
383     return proxy->StartEthEap(netId, profile);
384 #else
385     return NETMANAGER_SUCCESS;
386 #endif
387 }
388 
LogOffEthEap(int32_t netId)389 int32_t EthernetClient::LogOffEthEap(int32_t netId)
390 {
391 #ifdef NET_EXTENSIBLE_AUTHENTICATION
392     sptr<IEthernetService> proxy = GetProxy();
393     if (proxy == nullptr) {
394         NETMGR_EXT_LOG_E("proxy is nullptr");
395         return EAP_ERRCODE_NETMANAGER_STOP;
396     }
397     return proxy->LogOffEthEap(netId);
398 #else
399     return NETMANAGER_SUCCESS;
400 #endif
401 }
402 } // namespace NetManagerStandard
403 } // namespace OHOS
404