1 /*
2 * Copyright (c) 2021 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 "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 #include "netmgr_ext_log_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
EthernetClient()25 EthernetClient::EthernetClient() : ethernetService_(nullptr), deathRecipient_(nullptr) {}
26
~EthernetClient()27 EthernetClient::~EthernetClient() {}
28
SetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ic)29 int32_t EthernetClient::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic)
30 {
31 sptr<IEthernetService> proxy = GetProxy();
32 if (proxy == nullptr) {
33 NETMGR_EXT_LOG_E("proxy is nullptr");
34 return IPC_PROXY_ERR;
35 }
36 return proxy->SetIfaceConfig(iface, ic);
37 }
38
GetIfaceConfig(const std::string & iface)39 sptr<InterfaceConfiguration> EthernetClient::GetIfaceConfig(const std::string &iface)
40 {
41 sptr<IEthernetService> proxy = GetProxy();
42 if (proxy == nullptr) {
43 NETMGR_EXT_LOG_E("proxy is nullptr");
44 return nullptr;
45 }
46 return proxy->GetIfaceConfig(iface);
47 }
48
IsIfaceActive(const std::string & iface)49 int32_t EthernetClient::IsIfaceActive(const std::string &iface)
50 {
51 sptr<IEthernetService> proxy = GetProxy();
52 if (proxy == nullptr) {
53 NETMGR_EXT_LOG_E("proxy is nullptr");
54 return IPC_PROXY_ERR;
55 }
56 return proxy->IsIfaceActive(iface);
57 }
58
GetAllActiveIfaces()59 std::vector<std::string> EthernetClient::GetAllActiveIfaces()
60 {
61 sptr<IEthernetService> proxy = GetProxy();
62 if (proxy == nullptr) {
63 NETMGR_EXT_LOG_E("proxy is nullptr");
64 return {};
65 }
66 return proxy->GetAllActiveIfaces();
67 }
68
ResetFactory()69 int32_t EthernetClient::ResetFactory()
70 {
71 sptr<IEthernetService> proxy = GetProxy();
72 if (proxy == nullptr) {
73 NETMGR_EXT_LOG_E("proxy is nullptr");
74 return IPC_PROXY_ERR;
75 }
76 return proxy->ResetFactory();
77 }
78
GetProxy()79 sptr<IEthernetService> EthernetClient::GetProxy()
80 {
81 std::lock_guard lock(mutex_);
82 if (ethernetService_) {
83 NETMGR_EXT_LOG_D("get proxy is ok");
84 return ethernetService_;
85 }
86 NETMGR_EXT_LOG_D("execute GetSystemAbilityManager");
87 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (sam == nullptr) {
89 NETMGR_EXT_LOG_E("GetProxy, get SystemAbilityManager failed");
90 return nullptr;
91 }
92 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_ETHERNET_MANAGER_SYS_ABILITY_ID);
93 if (remote == nullptr) {
94 NETMGR_EXT_LOG_E("get Remote service failed");
95 return nullptr;
96 }
97 deathRecipient_ = (std::make_unique<EthernetDeathRecipient>(*this)).release();
98 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
99 NETMGR_EXT_LOG_E("add death recipient failed");
100 return nullptr;
101 }
102 ethernetService_ = iface_cast<IEthernetService>(remote);
103 if (ethernetService_ == nullptr) {
104 NETMGR_EXT_LOG_E("get Remote service proxy failed");
105 return nullptr;
106 }
107 return ethernetService_;
108 }
109
OnRemoteDied(const wptr<IRemoteObject> & remote)110 void EthernetClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
111 {
112 NETMGR_EXT_LOG_D("on remote died");
113 if (remote == nullptr) {
114 NETMGR_EXT_LOG_E("remote object is nullptr");
115 return;
116 }
117 std::lock_guard lock(mutex_);
118 if (ethernetService_ == nullptr) {
119 NETMGR_EXT_LOG_E("ethernetService_ is nullptr");
120 return;
121 }
122 sptr<IRemoteObject> local = ethernetService_->AsObject();
123 if (local != remote.promote()) {
124 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
125 return;
126 }
127 local->RemoveDeathRecipient(deathRecipient_);
128 ethernetService_ = nullptr;
129 }
130 } // namespace NetManagerStandard
131 } // namespace OHOS