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