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 "wifi_scan_impl.h"
17 #include "i_wifi_scan.h"
18 #ifndef OHOS_ARCH_LITE
19 #include "iservice_registry.h"
20 #include "wifi_sa_manager.h"
21 #endif
22 #include "wifi_logger.h"
23 #include "wifi_scan_proxy.h"
24
25 DEFINE_WIFILOG_SCAN_LABEL("WifiScanImpl");
26
27 namespace OHOS {
28 namespace Wifi {
29 #define RETURN_IF_FAIL(cond) \
30 do { \
31 if (!(cond)) { \
32 WIFI_LOGI("'%{public}s' failed.", #cond); \
33 return WIFI_OPT_FAILED; \
34 } \
35 } while (0)
36
WifiScanImpl(int systemAbilityId)37 WifiScanImpl::WifiScanImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId), client_(nullptr)
38 {}
39
~WifiScanImpl()40 WifiScanImpl::~WifiScanImpl()
41 {
42 #ifdef OHOS_ARCH_LITE
43 WifiScanProxy::ReleaseInstance();
44 #endif
45 }
46
Init()47 bool WifiScanImpl::Init()
48 {
49 #ifdef OHOS_ARCH_LITE
50 WifiScanProxy *scanProxy = WifiScanProxy::GetInstance();
51 if (scanProxy == nullptr) {
52 WIFI_LOGE("get wifi scan proxy failed.");
53 return false;
54 }
55 if (scanProxy->Init() != WIFI_OPT_SUCCESS) {
56 WIFI_LOGE("wifi scan proxy init failed.");
57 WifiScanProxy::ReleaseInstance();
58 return false;
59 }
60 client_ = scanProxy;
61 return true;
62 #else
63 return true;
64 #endif
65 }
66
GetWifiScanProxy()67 bool WifiScanImpl::GetWifiScanProxy()
68 {
69 #ifdef OHOS_ARCH_LITE
70 return (client_ != nullptr);
71 #else
72 WifiSaLoadManager::GetInstance().LoadWifiSa(systemAbilityId_);
73 if (IsRemoteDied() == false) {
74 return true;
75 }
76 sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77 if (sa_mgr == nullptr) {
78 WIFI_LOGE("failed to get SystemAbilityManager");
79 return false;
80 }
81 sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
82 if (object == nullptr) {
83 WIFI_LOGE("failed to get SCAN_SERVICE");
84 return false;
85 }
86 client_ = iface_cast<OHOS::Wifi::IWifiScan>(object);
87 if (client_ == nullptr) {
88 client_ = new (std::nothrow) WifiScanProxy(object);
89 }
90 if (client_ == nullptr) {
91 WIFI_LOGE("wifi scan init failed. %{public}d", systemAbilityId_);
92 return false;
93 }
94 return true;
95 #endif
96 }
97
SetScanControlInfo(const ScanControlInfo & info)98 ErrCode WifiScanImpl::SetScanControlInfo(const ScanControlInfo &info)
99 {
100 std::lock_guard<std::mutex> lock(mutex_);
101 RETURN_IF_FAIL(GetWifiScanProxy());
102 return client_->SetScanControlInfo(info);
103 }
104
Scan(bool compatible)105 ErrCode WifiScanImpl::Scan(bool compatible)
106 {
107 std::lock_guard<std::mutex> lock(mutex_);
108 RETURN_IF_FAIL(GetWifiScanProxy());
109 return client_->Scan(compatible);
110 }
111
AdvanceScan(const WifiScanParams & params)112 ErrCode WifiScanImpl::AdvanceScan(const WifiScanParams ¶ms)
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 RETURN_IF_FAIL(GetWifiScanProxy());
116 return client_->AdvanceScan(params);
117 }
118
IsWifiClosedScan(bool & bOpen)119 ErrCode WifiScanImpl::IsWifiClosedScan(bool &bOpen)
120 {
121 std::lock_guard<std::mutex> lock(mutex_);
122 RETURN_IF_FAIL(GetWifiScanProxy());
123 return client_->IsWifiClosedScan(bOpen);
124 }
125
GetScanInfoList(std::vector<WifiScanInfo> & result,bool compatible)126 ErrCode WifiScanImpl::GetScanInfoList(std::vector<WifiScanInfo> &result, bool compatible)
127 {
128 std::lock_guard<std::mutex> lock(mutex_);
129 RETURN_IF_FAIL(GetWifiScanProxy());
130 return client_->GetScanInfoList(result, compatible);
131 }
132
133 #ifdef OHOS_ARCH_LITE
RegisterCallBack(const std::shared_ptr<IWifiScanCallback> & callback,const std::vector<std::string> & event)134 ErrCode WifiScanImpl::RegisterCallBack(const std::shared_ptr<IWifiScanCallback> &callback,
135 const std::vector<std::string> &event)
136 #else
137 ErrCode WifiScanImpl::RegisterCallBack(const sptr<IWifiScanCallback> &callback, const std::vector<std::string> &event)
138 #endif
139 {
140 std::lock_guard<std::mutex> lock(mutex_);
141 RETURN_IF_FAIL(GetWifiScanProxy());
142 return client_->RegisterCallBack(callback, event);
143 }
144
GetSupportedFeatures(long & features)145 ErrCode WifiScanImpl::GetSupportedFeatures(long &features)
146 {
147 std::lock_guard<std::mutex> lock(mutex_);
148 RETURN_IF_FAIL(GetWifiScanProxy());
149 return client_->GetSupportedFeatures(features);
150 }
151
IsFeatureSupported(long feature)152 bool WifiScanImpl::IsFeatureSupported(long feature)
153 {
154 std::lock_guard<std::mutex> lock(mutex_);
155 RETURN_IF_FAIL(GetWifiScanProxy());
156 long tmpFeatures = 0;
157 if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
158 return false;
159 }
160 return ((tmpFeatures & feature) == feature);
161 }
162
IsRemoteDied(void)163 bool WifiScanImpl::IsRemoteDied(void)
164 {
165 return (client_ == nullptr) ? true : client_->IsRemoteDied();
166 }
167
SetScanOnlyAvailable(bool bScanOnlyAvailable)168 ErrCode WifiScanImpl::SetScanOnlyAvailable(bool bScanOnlyAvailable)
169 {
170 std::lock_guard<std::mutex> lock(mutex_);
171 RETURN_IF_FAIL(GetWifiScanProxy());
172 return client_->SetScanOnlyAvailable(bScanOnlyAvailable);
173 }
174
GetScanOnlyAvailable(bool & bScanOnlyAvailable)175 ErrCode WifiScanImpl::GetScanOnlyAvailable(bool &bScanOnlyAvailable)
176 {
177 std::lock_guard<std::mutex> lock(mutex_);
178 RETURN_IF_FAIL(GetWifiScanProxy());
179 return client_->GetScanOnlyAvailable(bScanOnlyAvailable);
180 }
181 } // namespace Wifi
182 } // namespace OHOS
183