• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "iservice_registry.h"
19 #include "wifi_logger.h"
20 #include "wifi_scan_proxy.h"
21 
22 DEFINE_WIFILOG_SCAN_LABEL("WifiScanImpl");
23 
24 namespace OHOS {
25 namespace Wifi {
26 #define RETURN_IF_FAIL(cond)                          \
27     do {                                              \
28         if (!(cond)) {                                \
29             WIFI_LOGI("'%{public}s' failed.", #cond); \
30             return WIFI_OPT_FAILED;                   \
31         }                                             \
32     } while (0)
33 
WifiScanImpl(int systemAbilityId)34 WifiScanImpl::WifiScanImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId), client_(nullptr)
35 {}
36 
~WifiScanImpl()37 WifiScanImpl::~WifiScanImpl()
38 {}
39 
Init()40 bool WifiScanImpl::Init()
41 {
42     sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43     if (sa_mgr == nullptr) {
44         WIFI_LOGE("failed to get SystemAbilityManager");
45         return false;
46     }
47     sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
48     if (object == nullptr) {
49         WIFI_LOGE("failed to get SCAN_SERVICE");
50         return false;
51     }
52     client_ = iface_cast<OHOS::Wifi::IWifiScan>(object);
53     if (client_ == nullptr) {
54         client_ = new (std::nothrow) WifiScanProxy(object);
55     }
56     if (client_ == nullptr) {
57         WIFI_LOGE("wifi scan init failed. %{public}d", systemAbilityId_);
58         return false;
59     }
60     return true;
61 }
62 
SetScanControlInfo(const ScanControlInfo & info)63 ErrCode WifiScanImpl::SetScanControlInfo(const ScanControlInfo &info)
64 {
65     RETURN_IF_FAIL(client_);
66     return client_->SetScanControlInfo(info);
67 }
68 
Scan()69 ErrCode WifiScanImpl::Scan()
70 {
71     RETURN_IF_FAIL(client_);
72     return client_->Scan();
73 }
74 
AdvanceScan(const WifiScanParams & params)75 ErrCode WifiScanImpl::AdvanceScan(const WifiScanParams &params)
76 {
77     RETURN_IF_FAIL(client_);
78     return client_->AdvanceScan(params);
79 }
80 
IsWifiClosedScan(bool & bOpen)81 ErrCode WifiScanImpl::IsWifiClosedScan(bool &bOpen)
82 {
83     RETURN_IF_FAIL(client_);
84     return client_->IsWifiClosedScan(bOpen);
85 }
86 
GetScanInfoList(std::vector<WifiScanInfo> & result)87 ErrCode WifiScanImpl::GetScanInfoList(std::vector<WifiScanInfo> &result)
88 {
89     RETURN_IF_FAIL(client_);
90     return client_->GetScanInfoList(result);
91 }
92 
RegisterCallBack(const sptr<IWifiScanCallback> & callback)93 ErrCode WifiScanImpl::RegisterCallBack(const sptr<IWifiScanCallback> &callback)
94 {
95     RETURN_IF_FAIL(client_);
96     return client_->RegisterCallBack(callback);
97 }
98 
GetSupportedFeatures(long & features)99 ErrCode WifiScanImpl::GetSupportedFeatures(long &features)
100 {
101     RETURN_IF_FAIL(client_);
102     return client_->GetSupportedFeatures(features);
103 }
104 
IsFeatureSupported(long feature)105 bool WifiScanImpl::IsFeatureSupported(long feature)
106 {
107     RETURN_IF_FAIL(client_);
108     long tmpFeatures = 0;
109     if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
110         return false;
111     }
112     return ((tmpFeatures & feature) == feature);
113 }
114 }  // namespace Wifi
115 }  // namespace OHOS