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 #include "wifi_scan_mgr_proxy.h"
25 #include "wifi_hisysevent.h"
26 #include "wifi_common_util.h"
27 
28 DEFINE_WIFILOG_SCAN_LABEL("WifiScanImpl");
29 
30 namespace OHOS {
31 namespace Wifi {
32 #define RETURN_IF_FAIL(cond)                          \
33     do {                                              \
34         if (!(cond)) {                                \
35             WIFI_LOGI("'%{public}s' failed.", #cond); \
36             return WIFI_OPT_FAILED;                   \
37         }                                             \
38     } while (0)
39 
WifiScanImpl()40 WifiScanImpl::WifiScanImpl() : systemAbilityId_(0), instId_(0), client_(nullptr)
41 {}
42 
~WifiScanImpl()43 WifiScanImpl::~WifiScanImpl()
44 {
45 #ifdef OHOS_ARCH_LITE
46     WifiScanProxy::ReleaseInstance();
47 #endif
48 }
49 
Init(int systemAbilityId,int instId)50 bool WifiScanImpl::Init(int systemAbilityId, int instId)
51 {
52 #ifdef OHOS_ARCH_LITE
53     WifiScanProxy *scanProxy = WifiScanProxy::GetInstance();
54     if (scanProxy == nullptr) {
55         WIFI_LOGE("get wifi scan proxy failed.");
56         return false;
57     }
58     if (scanProxy->Init() != WIFI_OPT_SUCCESS) {
59         WIFI_LOGE("wifi scan proxy init failed.");
60         WifiScanProxy::ReleaseInstance();
61         return false;
62     }
63     client_ = scanProxy;
64     return true;
65 #else
66     systemAbilityId_ = systemAbilityId;
67     instId_ = instId;
68     return true;
69 #endif
70 }
71 
GetWifiScanProxy()72 bool WifiScanImpl::GetWifiScanProxy()
73 {
74 #ifdef OHOS_ARCH_LITE
75     return (client_ != nullptr);
76 #else
77     WifiSaLoadManager::GetInstance().LoadWifiSa(systemAbilityId_);
78     if (IsRemoteDied() == false) {
79         return true;
80     }
81 
82     sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
83     if (sa_mgr == nullptr) {
84         WIFI_LOGE("failed to get SystemAbilityManager");
85         WriteWifiScanApiFailHiSysEvent(GetBundleName(), -1);
86         return false;
87     }
88 
89     sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
90     if (object == nullptr) {
91         WIFI_LOGE("failed to get SCAN_SERVICE");
92         WriteWifiScanApiFailHiSysEvent(GetBundleName(), -1);
93         return false;
94     }
95 
96     sptr<IWifiScanMgr> scanMgr = iface_cast<IWifiScanMgr>(object);
97     if (scanMgr == nullptr) {
98         scanMgr = new (std::nothrow) WifiScanMgrProxy(object);
99     }
100     if (scanMgr == nullptr) {
101         WIFI_LOGE("wifi scan init failed, %{public}d", systemAbilityId_.load());
102         WriteWifiScanApiFailHiSysEvent(GetBundleName(), -1);
103         return false;
104     }
105 
106     sptr<IRemoteObject> service = scanMgr->GetWifiRemote(instId_);
107     if (service == nullptr) {
108         WIFI_LOGE("wifi scan remote obj is null, %{public}d", instId_);
109         WriteWifiScanApiFailHiSysEvent(GetBundleName(), -1);
110         return false;
111     }
112 
113     client_ = iface_cast<OHOS::Wifi::IWifiScan>(service);
114     if (client_ == nullptr) {
115         client_ = new (std::nothrow) WifiScanProxy(service);
116     }
117     if (client_ == nullptr) {
118         WIFI_LOGE("wifi scan instId_ %{public}d init failed. %{public}d", instId_, systemAbilityId_.load());
119         WriteWifiScanApiFailHiSysEvent(GetBundleName(), -1);
120         return false;
121     }
122     return true;
123 #endif
124 }
125 
SetScanControlInfo(const ScanControlInfo & info)126 ErrCode WifiScanImpl::SetScanControlInfo(const ScanControlInfo &info)
127 {
128     std::lock_guard<std::mutex> lock(mutex_);
129     RETURN_IF_FAIL(GetWifiScanProxy());
130     return client_->SetScanControlInfo(info);
131 }
132 
Scan(bool compatible)133 ErrCode WifiScanImpl::Scan(bool compatible)
134 {
135     std::lock_guard<std::mutex> lock(mutex_);
136     RETURN_IF_FAIL(GetWifiScanProxy());
137     return client_->Scan(compatible);
138 }
139 
AdvanceScan(const WifiScanParams & params)140 ErrCode WifiScanImpl::AdvanceScan(const WifiScanParams &params)
141 {
142     std::lock_guard<std::mutex> lock(mutex_);
143     RETURN_IF_FAIL(GetWifiScanProxy());
144     return client_->AdvanceScan(params);
145 }
146 
IsWifiClosedScan(bool & bOpen)147 ErrCode WifiScanImpl::IsWifiClosedScan(bool &bOpen)
148 {
149     std::lock_guard<std::mutex> lock(mutex_);
150     RETURN_IF_FAIL(GetWifiScanProxy());
151     return client_->IsWifiClosedScan(bOpen);
152 }
153 
GetScanInfoList(std::vector<WifiScanInfo> & result,bool compatible)154 ErrCode WifiScanImpl::GetScanInfoList(std::vector<WifiScanInfo> &result, bool compatible)
155 {
156     std::lock_guard<std::mutex> lock(mutex_);
157     RETURN_IF_FAIL(GetWifiScanProxy());
158     return client_->GetScanInfoList(result, compatible);
159 }
160 
161 #ifdef OHOS_ARCH_LITE
RegisterCallBack(const std::shared_ptr<IWifiScanCallback> & callback,const std::vector<std::string> & event)162 ErrCode WifiScanImpl::RegisterCallBack(const std::shared_ptr<IWifiScanCallback> &callback,
163     const std::vector<std::string> &event)
164 #else
165 ErrCode WifiScanImpl::RegisterCallBack(const sptr<IWifiScanCallback> &callback, const std::vector<std::string> &event)
166 #endif
167 {
168     std::lock_guard<std::mutex> lock(mutex_);
169     RETURN_IF_FAIL(GetWifiScanProxy());
170     return client_->RegisterCallBack(callback, event);
171 }
172 
GetSupportedFeatures(long & features)173 ErrCode WifiScanImpl::GetSupportedFeatures(long &features)
174 {
175     std::lock_guard<std::mutex> lock(mutex_);
176     RETURN_IF_FAIL(GetWifiScanProxy());
177     return client_->GetSupportedFeatures(features);
178 }
179 
IsFeatureSupported(long feature)180 bool WifiScanImpl::IsFeatureSupported(long feature)
181 {
182     std::lock_guard<std::mutex> lock(mutex_);
183     RETURN_IF_FAIL(GetWifiScanProxy());
184     long tmpFeatures = 0;
185     if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
186         return false;
187     }
188     return ((static_cast<unsigned long>(tmpFeatures) & static_cast<unsigned long>(feature)) ==
189         static_cast<unsigned long>(feature));
190 }
191 
IsRemoteDied(void)192 bool WifiScanImpl::IsRemoteDied(void)
193 {
194     return (client_ == nullptr) ? true : client_->IsRemoteDied();
195 }
196 
SetScanOnlyAvailable(bool bScanOnlyAvailable)197 ErrCode WifiScanImpl::SetScanOnlyAvailable(bool bScanOnlyAvailable)
198 {
199     std::lock_guard<std::mutex> lock(mutex_);
200     RETURN_IF_FAIL(GetWifiScanProxy());
201     return client_->SetScanOnlyAvailable(bScanOnlyAvailable);
202 }
203 
GetScanOnlyAvailable(bool & bScanOnlyAvailable)204 ErrCode WifiScanImpl::GetScanOnlyAvailable(bool &bScanOnlyAvailable)
205 {
206     std::lock_guard<std::mutex> lock(mutex_);
207     RETURN_IF_FAIL(GetWifiScanProxy());
208     return client_->GetScanOnlyAvailable(bScanOnlyAvailable);
209 }
210 
StartWifiPnoScan(bool isStartAction,int periodMs,int suspendReason)211 ErrCode WifiScanImpl::StartWifiPnoScan(bool isStartAction, int periodMs, int suspendReason)
212 {
213     std::lock_guard<std::mutex> lock(mutex_);
214     RETURN_IF_FAIL(GetWifiScanProxy());
215     return client_->StartWifiPnoScan(isStartAction, periodMs, suspendReason);
216 }
217 }  // namespace Wifi
218 }  // namespace OHOS
219