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 #endif 21 #include "wifi_logger.h" 22 #include "wifi_scan_proxy.h" 23 24 DEFINE_WIFILOG_SCAN_LABEL("WifiScanImpl"); 25 26 namespace OHOS { 27 namespace Wifi { 28 #define RETURN_IF_FAIL(cond) \ 29 do { \ 30 if (!(cond)) { \ 31 WIFI_LOGI("'%{public}s' failed.", #cond); \ 32 return WIFI_OPT_FAILED; \ 33 } \ 34 } while (0) 35 WifiScanImpl(int systemAbilityId)36WifiScanImpl::WifiScanImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId), client_(nullptr) 37 {} 38 ~WifiScanImpl()39WifiScanImpl::~WifiScanImpl() 40 { 41 #ifdef OHOS_ARCH_LITE 42 WifiScanProxy::ReleaseInstance(); 43 #endif 44 } 45 Init()46bool WifiScanImpl::Init() 47 { 48 #ifdef OHOS_ARCH_LITE 49 WifiScanProxy *scanProxy = WifiScanProxy::GetInstance(); 50 if (scanProxy == nullptr) { 51 WIFI_LOGE("get wifi scan proxy failed."); 52 return false; 53 } 54 if (scanProxy->Init() != WIFI_OPT_SUCCESS) { 55 WIFI_LOGE("wifi scan proxy init failed."); 56 WifiScanProxy::ReleaseInstance(); 57 return false; 58 } 59 client_ = scanProxy; 60 return true; 61 #else 62 return GetWifiScanProxy(); 63 #endif 64 } 65 GetWifiScanProxy(void)66bool WifiScanImpl::GetWifiScanProxy(void) 67 { 68 #ifdef OHOS_ARCH_LITE 69 return (client_ != nullptr); 70 #else 71 if (IsRemoteDied() == false) { 72 return true; 73 } 74 75 WIFI_LOGI("GetWifiScanProxy, get new sa from remote!"); 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)98ErrCode WifiScanImpl::SetScanControlInfo(const ScanControlInfo &info) 99 { 100 RETURN_IF_FAIL(GetWifiScanProxy()); 101 return client_->SetScanControlInfo(info); 102 } 103 Scan()104ErrCode WifiScanImpl::Scan() 105 { 106 RETURN_IF_FAIL(GetWifiScanProxy()); 107 return client_->Scan(); 108 } 109 AdvanceScan(const WifiScanParams & params)110ErrCode WifiScanImpl::AdvanceScan(const WifiScanParams ¶ms) 111 { 112 RETURN_IF_FAIL(GetWifiScanProxy()); 113 return client_->AdvanceScan(params); 114 } 115 IsWifiClosedScan(bool & bOpen)116ErrCode WifiScanImpl::IsWifiClosedScan(bool &bOpen) 117 { 118 RETURN_IF_FAIL(GetWifiScanProxy()); 119 return client_->IsWifiClosedScan(bOpen); 120 } 121 GetScanInfoList(std::vector<WifiScanInfo> & result)122ErrCode WifiScanImpl::GetScanInfoList(std::vector<WifiScanInfo> &result) 123 { 124 RETURN_IF_FAIL(GetWifiScanProxy()); 125 return client_->GetScanInfoList(result); 126 } 127 128 #ifdef OHOS_ARCH_LITE RegisterCallBack(const std::shared_ptr<IWifiScanCallback> & callback)129ErrCode WifiScanImpl::RegisterCallBack(const std::shared_ptr<IWifiScanCallback> &callback) 130 #else 131 ErrCode WifiScanImpl::RegisterCallBack(const sptr<IWifiScanCallback> &callback) 132 #endif 133 { 134 RETURN_IF_FAIL(GetWifiScanProxy()); 135 return client_->RegisterCallBack(callback); 136 } 137 GetSupportedFeatures(long & features)138ErrCode WifiScanImpl::GetSupportedFeatures(long &features) 139 { 140 RETURN_IF_FAIL(GetWifiScanProxy()); 141 return client_->GetSupportedFeatures(features); 142 } 143 IsFeatureSupported(long feature)144bool WifiScanImpl::IsFeatureSupported(long feature) 145 { 146 RETURN_IF_FAIL(GetWifiScanProxy()); 147 long tmpFeatures = 0; 148 if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) { 149 return false; 150 } 151 return ((tmpFeatures & feature) == feature); 152 } 153 IsRemoteDied(void)154bool WifiScanImpl::IsRemoteDied(void) 155 { 156 return (client_ == nullptr) ? true : client_->IsRemoteDied(); 157 } 158 } // namespace Wifi 159 } // namespace OHOS