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