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 "i_ethernet_service.h"
19 #include "if_system_ability_manager.h"
20 #include "interface_configuration.h"
21 #include "ipc_types.h"
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "iservice_registry.h"
25 #include "netmgr_ext_log_wrapper.h"
26 #include "refbase.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 namespace NetManagerStandard {
EthernetClient()31 EthernetClient::EthernetClient() : ethernetService_(nullptr), deathRecipient_(nullptr) {}
32
33 EthernetClient::~EthernetClient() = default;
34
SetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ic)35 int32_t EthernetClient::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic)
36 {
37 sptr<IEthernetService> proxy = GetProxy();
38 if (proxy == nullptr) {
39 NETMGR_EXT_LOG_E("proxy is nullptr");
40 return IPC_PROXY_ERR;
41 }
42 return proxy->SetIfaceConfig(iface, ic);
43 }
44
GetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)45 int32_t EthernetClient::GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
46 {
47 sptr<IEthernetService> proxy = GetProxy();
48 if (proxy == nullptr) {
49 NETMGR_EXT_LOG_E("proxy is nullptr");
50 return IPC_PROXY_ERR;
51 }
52 return proxy->GetIfaceConfig(iface, ifaceConfig);
53 }
54
IsIfaceActive(const std::string & iface,int32_t & activeStatus)55 int32_t EthernetClient::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
56 {
57 sptr<IEthernetService> proxy = GetProxy();
58 if (proxy == nullptr) {
59 NETMGR_EXT_LOG_E("proxy is nullptr");
60 return IPC_PROXY_ERR;
61 }
62 return proxy->IsIfaceActive(iface, activeStatus);
63 }
64
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)65 int32_t EthernetClient::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
66 {
67 sptr<IEthernetService> proxy = GetProxy();
68 if (proxy == nullptr) {
69 NETMGR_EXT_LOG_E("proxy is nullptr");
70 return IPC_PROXY_ERR;
71 }
72 return proxy->GetAllActiveIfaces(activeIfaces);
73 }
74
ResetFactory()75 int32_t EthernetClient::ResetFactory()
76 {
77 sptr<IEthernetService> proxy = GetProxy();
78 if (proxy == nullptr) {
79 NETMGR_EXT_LOG_E("proxy is nullptr");
80 return IPC_PROXY_ERR;
81 }
82 return proxy->ResetFactory();
83 }
84
GetProxy()85 sptr<IEthernetService> EthernetClient::GetProxy()
86 {
87 std::lock_guard lock(mutex_);
88 if (ethernetService_) {
89 NETMGR_EXT_LOG_D("get proxy is ok");
90 return ethernetService_;
91 }
92 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
93 if (sam == nullptr) {
94 NETMGR_EXT_LOG_E("GetProxy, get SystemAbilityManager failed");
95 return nullptr;
96 }
97 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_ETHERNET_MANAGER_SYS_ABILITY_ID);
98 if (remote == nullptr) {
99 NETMGR_EXT_LOG_E("get Remote service failed");
100 return nullptr;
101 }
102 deathRecipient_ = new (std::nothrow) EthernetDeathRecipient(*this);
103 if (deathRecipient_ == nullptr) {
104 NETMGR_EXT_LOG_E("Recipient new failed!");
105 }
106 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
107 NETMGR_EXT_LOG_E("add death recipient failed");
108 return nullptr;
109 }
110 ethernetService_ = iface_cast<IEthernetService>(remote);
111 if (ethernetService_ == nullptr) {
112 NETMGR_EXT_LOG_E("get Remote service proxy failed");
113 return nullptr;
114 }
115 return ethernetService_;
116 }
117
OnRemoteDied(const wptr<IRemoteObject> & remote)118 void EthernetClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
119 {
120 if (remote == nullptr) {
121 NETMGR_EXT_LOG_E("remote object is nullptr");
122 return;
123 }
124 std::lock_guard lock(mutex_);
125 if (ethernetService_ == nullptr) {
126 NETMGR_EXT_LOG_E("ethernetService_ is nullptr");
127 return;
128 }
129 sptr<IRemoteObject> local = ethernetService_->AsObject();
130 if (local == nullptr) {
131 NETMGR_EXT_LOG_E("local is nullptr");
132 return;
133 }
134 if (local != remote.promote()) {
135 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
136 return;
137 }
138 local->RemoveDeathRecipient(deathRecipient_);
139 ethernetService_ = nullptr;
140 }
141
SetInterfaceUp(const std::string & iface)142 int32_t EthernetClient::SetInterfaceUp(const std::string &iface)
143 {
144 sptr<IEthernetService> proxy = GetProxy();
145 if (proxy == nullptr) {
146 NETMGR_EXT_LOG_E("proxy is nullptr");
147 return IPC_PROXY_ERR;
148 }
149 return proxy->SetInterfaceUp(iface);
150 }
151
SetInterfaceDown(const std::string & iface)152 int32_t EthernetClient::SetInterfaceDown(const std::string &iface)
153 {
154 sptr<IEthernetService> proxy = GetProxy();
155 if (proxy == nullptr) {
156 NETMGR_EXT_LOG_E("proxy is nullptr");
157 return IPC_PROXY_ERR;
158 }
159 return proxy->SetInterfaceDown(iface);
160 }
161
GetInterfaceConfig(const std::string & iface,OHOS::nmd::InterfaceConfigurationParcel & cfg)162 int32_t EthernetClient::GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg)
163 {
164 sptr<IEthernetService> proxy = GetProxy();
165 if (proxy == nullptr) {
166 NETMGR_EXT_LOG_E("proxy is nullptr");
167 return IPC_PROXY_ERR;
168 }
169 return proxy->GetInterfaceConfig(iface, cfg);
170 }
171 } // namespace NetManagerStandard
172 } // namespace OHOS