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 #include "wifi_device_impl.h"
16 #include "iservice_registry.h"
17 #include "wifi_logger.h"
18
19 DEFINE_WIFILOG_LABEL("WifiDeviceImpl");
20
21 namespace OHOS {
22 namespace Wifi {
23 #define RETURN_IF_FAIL(cond) \
24 do { \
25 if (!(cond)) { \
26 WIFI_LOGI("'%{public}s' failed.", #cond); \
27 return WIFI_OPT_FAILED; \
28 } \
29 } while (0)
30
WifiDeviceImpl(int systemAbilityId)31 WifiDeviceImpl::WifiDeviceImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId)
32 {}
33
~WifiDeviceImpl()34 WifiDeviceImpl::~WifiDeviceImpl()
35 {}
36
Init()37 bool WifiDeviceImpl::Init()
38 {
39 sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 if (sa_mgr == nullptr) {
41 WIFI_LOGE("failed to get SystemAbilityManager");
42 return false;
43 }
44
45 sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
46 if (object == nullptr) {
47 WIFI_LOGE("failed to get DEVICE_SERVICE");
48 return false;
49 }
50
51 client_ = iface_cast<IWifiDevice>(object);
52 if (client_ == nullptr) {
53 client_ = new (std::nothrow) WifiDeviceProxy(object);
54 }
55
56 if (client_ == nullptr) {
57 WIFI_LOGE("wifi device init failed. %{public}d", systemAbilityId_);
58 return false;
59 }
60
61 return true;
62 }
63
EnableWifi()64 ErrCode WifiDeviceImpl::EnableWifi()
65 {
66 RETURN_IF_FAIL(client_);
67 return client_->EnableWifi();
68 }
69
DisableWifi()70 ErrCode WifiDeviceImpl::DisableWifi()
71 {
72 RETURN_IF_FAIL(client_);
73 return client_->DisableWifi();
74 }
75
InitWifiProtect(const WifiProtectType & protectType,const std::string & protectName)76 ErrCode WifiDeviceImpl::InitWifiProtect(const WifiProtectType &protectType, const std::string &protectName)
77 {
78 RETURN_IF_FAIL(client_);
79 return client_->InitWifiProtect(protectType, protectName);
80 }
81
GetWifiProtectRef(const WifiProtectMode & protectMode,const std::string & protectName)82 ErrCode WifiDeviceImpl::GetWifiProtectRef(const WifiProtectMode &protectMode, const std::string &protectName)
83 {
84 RETURN_IF_FAIL(client_);
85 return client_->GetWifiProtectRef(protectMode, protectName);
86 }
87
PutWifiProtectRef(const std::string & protectName)88 ErrCode WifiDeviceImpl::PutWifiProtectRef(const std::string &protectName)
89 {
90 RETURN_IF_FAIL(client_);
91 return client_->PutWifiProtectRef(protectName);
92 }
93
AddDeviceConfig(const WifiDeviceConfig & config,int & result)94 ErrCode WifiDeviceImpl::AddDeviceConfig(const WifiDeviceConfig &config, int &result)
95 {
96 RETURN_IF_FAIL(client_);
97 return client_->AddDeviceConfig(config, result);
98 }
99
UpdateDeviceConfig(const WifiDeviceConfig & config,int & result)100 ErrCode WifiDeviceImpl::UpdateDeviceConfig(const WifiDeviceConfig &config, int &result)
101 {
102 RETURN_IF_FAIL(client_);
103 return client_->UpdateDeviceConfig(config, result);
104 }
105
RemoveDevice(int networkId)106 ErrCode WifiDeviceImpl::RemoveDevice(int networkId)
107 {
108 RETURN_IF_FAIL(client_);
109 return client_->RemoveDevice(networkId);
110 }
111
RemoveAllDevice()112 ErrCode WifiDeviceImpl::RemoveAllDevice()
113 {
114 RETURN_IF_FAIL(client_);
115 return client_->RemoveAllDevice();
116 }
117
GetDeviceConfigs(std::vector<WifiDeviceConfig> & result)118 ErrCode WifiDeviceImpl::GetDeviceConfigs(std::vector<WifiDeviceConfig> &result)
119 {
120 RETURN_IF_FAIL(client_);
121 return client_->GetDeviceConfigs(result);
122 }
123
EnableDeviceConfig(int networkId,bool attemptEnable)124 ErrCode WifiDeviceImpl::EnableDeviceConfig(int networkId, bool attemptEnable)
125 {
126 RETURN_IF_FAIL(client_);
127 return client_->EnableDeviceConfig(networkId, attemptEnable);
128 }
129
DisableDeviceConfig(int networkId)130 ErrCode WifiDeviceImpl::DisableDeviceConfig(int networkId)
131 {
132 RETURN_IF_FAIL(client_);
133 return client_->DisableDeviceConfig(networkId);
134 }
135
ConnectToNetwork(int networkId)136 ErrCode WifiDeviceImpl::ConnectToNetwork(int networkId)
137 {
138 RETURN_IF_FAIL(client_);
139 return client_->ConnectToNetwork(networkId);
140 }
141
ConnectToDevice(const WifiDeviceConfig & config)142 ErrCode WifiDeviceImpl::ConnectToDevice(const WifiDeviceConfig &config)
143 {
144 RETURN_IF_FAIL(client_);
145 return client_->ConnectToDevice(config);
146 }
147
IsConnected()148 bool WifiDeviceImpl::IsConnected()
149 {
150 RETURN_IF_FAIL(client_);
151 return client_->IsConnected();
152 }
153
ReConnect()154 ErrCode WifiDeviceImpl::ReConnect()
155 {
156 RETURN_IF_FAIL(client_);
157 return client_->ReConnect();
158 }
159
ReAssociate()160 ErrCode WifiDeviceImpl::ReAssociate()
161 {
162 RETURN_IF_FAIL(client_);
163 return client_->ReAssociate();
164 }
165
Disconnect()166 ErrCode WifiDeviceImpl::Disconnect()
167 {
168 RETURN_IF_FAIL(client_);
169 return client_->Disconnect();
170 }
171
StartWps(const WpsConfig & config)172 ErrCode WifiDeviceImpl::StartWps(const WpsConfig &config)
173 {
174 RETURN_IF_FAIL(client_);
175 return client_->StartWps(config);
176 }
177
CancelWps()178 ErrCode WifiDeviceImpl::CancelWps()
179 {
180 RETURN_IF_FAIL(client_);
181 return client_->CancelWps();
182 }
183
IsWifiActive(bool & bActive)184 ErrCode WifiDeviceImpl::IsWifiActive(bool &bActive)
185 {
186 RETURN_IF_FAIL(client_);
187 return client_->IsWifiActive(bActive);
188 }
189
GetWifiState(int & state)190 ErrCode WifiDeviceImpl::GetWifiState(int &state)
191 {
192 RETURN_IF_FAIL(client_);
193 return client_->GetWifiState(state);
194 }
195
GetLinkedInfo(WifiLinkedInfo & info)196 ErrCode WifiDeviceImpl::GetLinkedInfo(WifiLinkedInfo &info)
197 {
198 RETURN_IF_FAIL(client_);
199 return client_->GetLinkedInfo(info);
200 }
201
GetIpInfo(IpInfo & info)202 ErrCode WifiDeviceImpl::GetIpInfo(IpInfo &info)
203 {
204 RETURN_IF_FAIL(client_);
205 return client_->GetIpInfo(info);
206 }
207
SetCountryCode(const std::string & countryCode)208 ErrCode WifiDeviceImpl::SetCountryCode(const std::string &countryCode)
209 {
210 RETURN_IF_FAIL(client_);
211 return client_->SetCountryCode(countryCode);
212 }
213
GetCountryCode(std::string & countryCode)214 ErrCode WifiDeviceImpl::GetCountryCode(std::string &countryCode)
215 {
216 RETURN_IF_FAIL(client_);
217 return client_->GetCountryCode(countryCode);
218 }
219
GetSignalLevel(const int & rssi,const int & band,int & level)220 ErrCode WifiDeviceImpl::GetSignalLevel(const int &rssi, const int &band, int &level)
221 {
222 RETURN_IF_FAIL(client_);
223 return client_->GetSignalLevel(rssi, band, level);
224 }
225
RegisterCallBack(const sptr<IWifiDeviceCallBack> & callback)226 ErrCode WifiDeviceImpl::RegisterCallBack(const sptr<IWifiDeviceCallBack> &callback)
227 {
228 RETURN_IF_FAIL(client_);
229 return client_->RegisterCallBack(callback);
230 }
231
GetSupportedFeatures(long & features)232 ErrCode WifiDeviceImpl::GetSupportedFeatures(long &features)
233 {
234 RETURN_IF_FAIL(client_);
235 return client_->GetSupportedFeatures(features);
236 }
237
IsFeatureSupported(long feature)238 bool WifiDeviceImpl::IsFeatureSupported(long feature)
239 {
240 RETURN_IF_FAIL(client_);
241 long tmpFeatures = 0;
242 if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
243 return false;
244 }
245 return ((tmpFeatures & feature) == feature);
246 }
247
GetDeviceMacAddress(std::string & result)248 ErrCode WifiDeviceImpl::GetDeviceMacAddress(std::string &result)
249 {
250 RETURN_IF_FAIL(client_);
251 return client_->GetDeviceMacAddress(result);
252 }
253
SetLowLatencyMode(bool enabled)254 bool WifiDeviceImpl::SetLowLatencyMode(bool enabled)
255 {
256 RETURN_IF_FAIL(client_);
257 return client_->SetLowLatencyMode(enabled);
258 }
259 } // namespace Wifi
260 } // namespace OHOS