mirror of
https://gitee.com/openharmony/base_location
synced 2025-02-12 22:29:56 +00:00
bugfix:wifiscan
Signed-off-by: 李文龙 <liwenlong22@huawei.com>
This commit is contained in:
parent
f10591d314
commit
7c38cbc693
@ -33,6 +33,7 @@ LOCATION_NOTIFICATION_DIR = "$LOCATION_ROOT_DIR/frameworks/native/notification"
|
||||
LOCATION_COMMON_DIR = "$LOCATION_ROOT_DIR/frameworks/location_common/common"
|
||||
ability_runtime_path = "//foundation/ability/ability_runtime"
|
||||
ability_runtime_napi_path = "${ability_runtime_path}/frameworks/js/napi"
|
||||
WIFI_MANAGER_ROOT = "//foundation/communication/wifi/wifi"
|
||||
|
||||
declare_args() {
|
||||
location_feature_with_geocode = true
|
||||
|
@ -100,6 +100,10 @@ ohos_shared_library("lbsservice_locator") {
|
||||
]
|
||||
|
||||
if (communication_wifi_enable) {
|
||||
include_dirs += [
|
||||
"$WIFI_MANAGER_ROOT/interfaces/kits/c",
|
||||
"$WIFI_MANAGER_ROOT/frameworks/native/interfaces",
|
||||
]
|
||||
external_deps += [ "wifi:wifi_sdk" ]
|
||||
defines += [ "WIFI_ENABLE" ]
|
||||
}
|
||||
|
@ -35,20 +35,15 @@
|
||||
#include "system_ability_status_change_stub.h"
|
||||
#ifdef WIFI_ENABLE
|
||||
#include "wifi_scan.h"
|
||||
#include "wifi_device.h"
|
||||
#endif
|
||||
|
||||
namespace OHOS {
|
||||
namespace Location {
|
||||
#ifdef WIFI_ENABLE
|
||||
class LocatorWifiScanEventCallback : public Wifi::IWifiScanCallback {
|
||||
class LocatorWifiScanEventCallback {
|
||||
public:
|
||||
explicit LocatorWifiScanEventCallback() {}
|
||||
~LocatorWifiScanEventCallback() {}
|
||||
void OnWifiScanStateChanged(int state) override;
|
||||
sptr<IRemoteObject> AsObject() override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
static void OnWifiScanStateChanged(int state, int size);
|
||||
};
|
||||
#endif
|
||||
|
||||
@ -193,12 +188,6 @@ public:
|
||||
void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
|
||||
};
|
||||
|
||||
class WifiServiceStatusChange : public SystemAbilityStatusChangeStub {
|
||||
public:
|
||||
void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
|
||||
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
|
||||
};
|
||||
|
||||
class LocatorRequiredDataManager {
|
||||
public:
|
||||
LocatorRequiredDataManager();
|
||||
@ -217,18 +206,15 @@ private:
|
||||
public:
|
||||
void ResetCallbackRegisteredStatus();
|
||||
__attribute__((no_sanitize("cfi"))) bool RegisterWifiCallBack();
|
||||
__attribute__((no_sanitize("cfi"))) bool UnRegisterWifiCallBack();
|
||||
std::vector<std::shared_ptr<LocatingRequiredData>> GetLocatingRequiredDataByWifi(
|
||||
const std::vector<Wifi::WifiScanInfo>& wifiScanInfo);
|
||||
__attribute__((no_sanitize("cfi"))) void GetWifiScanList(std::vector<Wifi::WifiScanInfo>& wifiScanInfo);
|
||||
private:
|
||||
void WifiInfoInit();
|
||||
bool isWifiCallbackRegistered();
|
||||
std::shared_ptr<Wifi::WifiScan> wifiScanPtr_;
|
||||
sptr<LocatorWifiScanEventCallback> wifiScanEventCallback_;
|
||||
bool isWifiCallbackRegistered_ = false;
|
||||
std::mutex wifiRegisteredMutex_;
|
||||
sptr<ISystemAbilityStatusChange> saStatusListener_ =
|
||||
sptr<WifiServiceStatusChange>(new WifiServiceStatusChange());
|
||||
WifiEvent wifiScanEventCallback_ = {0};
|
||||
#endif
|
||||
std::mutex mutex_;
|
||||
std::vector<sptr<ILocatingRequiredDataCallback>> callbacks_;
|
||||
|
@ -55,18 +55,16 @@ __attribute__((no_sanitize("cfi"))) LocationErrCode LocatorRequiredDataManager::
|
||||
}
|
||||
if (config->GetType() == LocatingRequiredDataType::WIFI) {
|
||||
#ifdef WIFI_ENABLE
|
||||
if (!isWifiCallbackRegistered()) {
|
||||
bool ret = RegisterWifiCallBack();
|
||||
if (!ret) {
|
||||
LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
|
||||
return ERRCODE_SERVICE_UNAVAILABLE;
|
||||
}
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(mutex_, std::defer_lock);
|
||||
lock.lock();
|
||||
callbacks_.push_back(dataCallback);
|
||||
LBSLOGD(LOCATOR, "after RegisterCallback, callback size:%{public}s", std::to_string(callbacks_.size()).c_str());
|
||||
if (config->GetNeedStartScan() && callbacks_.size() == 1) {
|
||||
bool ret = RegisterWifiCallBack();
|
||||
if (!ret) {
|
||||
LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
|
||||
return ERRCODE_SERVICE_UNAVAILABLE;
|
||||
}
|
||||
timeInterval_ = config->GetScanIntervalMs();
|
||||
if (scanHandler_ != nullptr) {
|
||||
scanHandler_->SendEvent(EVENT_START_SCAN, 0, 0);
|
||||
@ -104,6 +102,11 @@ LocationErrCode LocatorRequiredDataManager::UnregisterCallback(const sptr<IRemot
|
||||
}
|
||||
LBSLOGD(LOCATOR, "after UnregisterCallback, callback size:%{public}s", std::to_string(callbacks_.size()).c_str());
|
||||
if (scanHandler_ != nullptr && callbacks_.size() == 0) {
|
||||
bool ret = UnRegisterWifiCallBack();
|
||||
if (!ret) {
|
||||
LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
|
||||
return ERRCODE_SERVICE_UNAVAILABLE;
|
||||
}
|
||||
scanHandler_->SendEvent(EVENT_STOP_SCAN, 0, 0);
|
||||
}
|
||||
return ERRCODE_SUCCESS;
|
||||
@ -191,54 +194,29 @@ void LocatorBleCallbackWapper::OnNotifyMsgReportFromLpDevice(const Bluetooth::UU
|
||||
void LocatorRequiredDataManager::WifiInfoInit()
|
||||
{
|
||||
wifiScanPtr_ = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
|
||||
wifiScanEventCallback_ =
|
||||
sptr<LocatorWifiScanEventCallback>(new (std::nothrow) LocatorWifiScanEventCallback());
|
||||
bool ret = RegisterWifiCallBack();
|
||||
if (!ret) {
|
||||
LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
|
||||
}
|
||||
int32_t result;
|
||||
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
|
||||
if (samgrProxy == nullptr) {
|
||||
LBSLOGE(LOCATOR, "%{public}s samgrProxy is nullptr!", __func__);
|
||||
return;
|
||||
}
|
||||
saStatusListener_ = sptr<WifiServiceStatusChange>(new WifiServiceStatusChange());
|
||||
if (saStatusListener_ == nullptr) {
|
||||
LBSLOGE(LOCATOR, "%{public}s saStatusListener_ is nullptr!", __func__);
|
||||
return;
|
||||
}
|
||||
result = samgrProxy->SubscribeSystemAbility(static_cast<int32_t>(WIFI_SCAN_ABILITY_ID), saStatusListener_);
|
||||
LBSLOGI(LOCATOR, "%{public}s SubcribeSystemAbility result is %{public}d!", __func__, result);
|
||||
}
|
||||
|
||||
bool LocatorRequiredDataManager::isWifiCallbackRegistered()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(wifiRegisteredMutex_);
|
||||
return isWifiCallbackRegistered_;
|
||||
}
|
||||
|
||||
void LocatorRequiredDataManager::ResetCallbackRegisteredStatus()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(wifiRegisteredMutex_);
|
||||
isWifiCallbackRegistered_ = false;
|
||||
}
|
||||
|
||||
__attribute__((no_sanitize("cfi"))) bool LocatorRequiredDataManager::RegisterWifiCallBack()
|
||||
{
|
||||
LBSLOGD(LOCATOR, "%{public}s enter", __func__);
|
||||
if (wifiScanPtr_ == nullptr || wifiScanEventCallback_ == nullptr) {
|
||||
LBSLOGE(LOCATOR, "%{public}s param unexpected.", __func__);
|
||||
std::unique_lock<std::mutex> lock(wifiRegisteredMutex_);
|
||||
wifiScanEventCallback_.OnWifiScanStateChanged = LocatorWifiScanEventCallback::OnWifiScanStateChanged;
|
||||
int32_t ret = RegisterWifiEvent(&wifiScanEventCallback_);
|
||||
if (ret != Wifi::WIF_SUCCESS) {
|
||||
LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
|
||||
return false;
|
||||
}
|
||||
std::vector<std::string> events = {EVENT_STA_SCAN_STATE_CHANGE};
|
||||
return true;
|
||||
}
|
||||
__attribute__((no_sanitize("cfi"))) bool LocatorRequiredDataManager::UnRegisterWifiCallBack()
|
||||
{
|
||||
LBSLOGD(LOCATOR, "%{public}s enter", __func__);
|
||||
std::unique_lock<std::mutex> lock(wifiRegisteredMutex_);
|
||||
int ret = wifiScanPtr_->RegisterCallBack(wifiScanEventCallback_, events);
|
||||
int ret = UnRegisterWifiEvent(wifiScanEventCallback_);
|
||||
if (ret != Wifi::WIFI_OPT_SUCCESS) {
|
||||
LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
|
||||
return false;
|
||||
}
|
||||
isWifiCallbackRegistered_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -279,7 +257,7 @@ std::vector<std::shared_ptr<LocatingRequiredData>> LocatorRequiredDataManager::G
|
||||
return res;
|
||||
}
|
||||
|
||||
void LocatorWifiScanEventCallback::OnWifiScanStateChanged(int state)
|
||||
void LocatorWifiScanEventCallback::OnWifiScanStateChanged(int state, int size)
|
||||
{
|
||||
LBSLOGD(LOCATOR, "OnWifiScanStateChanged state=%{public}d", state);
|
||||
if (state == 0) {
|
||||
@ -416,17 +394,5 @@ void ScanListHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void WifiServiceStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
|
||||
{
|
||||
LBSLOGI(LOCATOR, "WifiServiceStatusChange::OnAddSystemAbility systemAbilityId :%{public}d", systemAbilityId);
|
||||
LocatorRequiredDataManager::GetInstance()->RegisterWifiCallBack();
|
||||
}
|
||||
|
||||
void WifiServiceStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
|
||||
{
|
||||
LBSLOGI(LOCATOR, "WifiServiceStatusChange::OnRemoveSystemAbility systemAbilityId :%{public}d", systemAbilityId);
|
||||
LocatorRequiredDataManager::GetInstance()->ResetCallbackRegisteredStatus();
|
||||
}
|
||||
} // namespace Location
|
||||
} // namespace OHOS
|
||||
|
@ -45,8 +45,8 @@ HWTEST_F(LocatorRequiredDataManagerTest, OnWifiScanStateChanged001, TestSize.Lev
|
||||
#ifdef WIFI_ENABLE
|
||||
auto wifiScanEventCallback =
|
||||
sptr<LocatorWifiScanEventCallback>(new (std::nothrow) LocatorWifiScanEventCallback());
|
||||
wifiScanEventCallback->OnWifiScanStateChanged(0);
|
||||
wifiScanEventCallback->OnWifiScanStateChanged(1);
|
||||
wifiScanEventCallback->OnWifiScanStateChanged(0, 0);
|
||||
wifiScanEventCallback->OnWifiScanStateChanged(1, 1);
|
||||
LBSLOGI(LOCATOR_CALLBACK, "[LocatorRequiredDataManagerTest] OnWifiScanStateChanged001 end");
|
||||
#endif
|
||||
}
|
||||
@ -63,7 +63,9 @@ HWTEST_F(LocatorRequiredDataManagerTest, RegisterCallback001, TestSize.Level1)
|
||||
dataConfig->SetNeedStartScan(true);
|
||||
dataConfig->SetScanIntervalMs(1);
|
||||
dataConfig->SetScanTimeoutMs(1);
|
||||
LocationErrCode errorCode = locatorDataManager->RegisterCallback(dataConfig, nullptr);
|
||||
AppIdentity identity;
|
||||
identity.SetPid(1);
|
||||
LocationErrCode errorCode = locatorDataManager->RegisterCallback(identity, dataConfig, nullptr);
|
||||
EXPECT_EQ(ERRCODE_INVALID_PARAM, errorCode);
|
||||
errorCode = locatorDataManager->UnregisterCallback(nullptr);
|
||||
EXPECT_EQ(ERRCODE_SERVICE_UNAVAILABLE, errorCode);
|
||||
@ -82,9 +84,11 @@ HWTEST_F(LocatorRequiredDataManagerTest, RegisterCallback002, TestSize.Level1)
|
||||
dataConfig->SetNeedStartScan(false);
|
||||
dataConfig->SetScanIntervalMs(1);
|
||||
dataConfig->SetScanTimeoutMs(1);
|
||||
AppIdentity identity;
|
||||
identity.SetPid(1);
|
||||
auto callback =
|
||||
sptr<LocatingRequiredDataCallbackNapi>(new (std::nothrow) LocatingRequiredDataCallbackNapi());
|
||||
LocationErrCode errorCode = locatorDataManager->RegisterCallback(dataConfig, callback->AsObject());
|
||||
LocationErrCode errorCode = locatorDataManager->RegisterCallback(identity, dataConfig, callback->AsObject());
|
||||
EXPECT_EQ(ERRCODE_NOT_SUPPORTED, errorCode);
|
||||
LBSLOGI(LOCATOR_CALLBACK, "[LocatorRequiredDataManagerTest] RegisterCallback002 end");
|
||||
}
|
||||
@ -101,9 +105,11 @@ HWTEST_F(LocatorRequiredDataManagerTest, RegisterCallback003, TestSize.Level1)
|
||||
dataConfig->SetNeedStartScan(false);
|
||||
dataConfig->SetScanIntervalMs(1);
|
||||
dataConfig->SetScanTimeoutMs(1);
|
||||
AppIdentity identity;
|
||||
identity.SetPid(1);
|
||||
auto callback =
|
||||
sptr<LocatingRequiredDataCallbackNapi>(new (std::nothrow) LocatingRequiredDataCallbackNapi());
|
||||
LocationErrCode errorCode = locatorDataManager->RegisterCallback(dataConfig, callback->AsObject());
|
||||
LocationErrCode errorCode = locatorDataManager->RegisterCallback(identity, dataConfig, callback->AsObject());
|
||||
EXPECT_EQ(ERRCODE_SUCCESS, errorCode);
|
||||
LBSLOGI(LOCATOR_CALLBACK, "[LocatorRequiredDataManagerTest] RegisterCallback003 end");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user