• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include "beacon_fence_manager.h"
16 #include "location_log.h"
17 #include "common_utils.h"
18 #include "hook_utils.h"
19 #include <sstream>
20 #include <iomanip>
21 #include "location_data_rdb_manager.h"
22 #include "proxy_freeze_manager.h"
23 #include "locator_background_proxy.h"
24 
25 namespace OHOS {
26 namespace Location {
27 const int32_t MAX_REQUEST_MAP_NUM = 16;
28 constexpr int32_t FENCE_MAX_ID = 1000000;
29 const int32_t STOI_BYTE_LIMIT = 7;
30 const int BEACON_FENCE_OPERATE_RESULT_ENTER = 1;
31 const int BEACON_FENCE_OPERATE_RESULT_EXIT = 2;
32 const int BEACON_FENCE_DATA_LENGTH = 18;
33 const int BEACON_FENCE_UUID_OFFSET_2 = 2;
34 const int BEACON_FENCE_UUID_OFFSET_6 = 6;
35 const int BEACON_FENCE_UUID_OFFSET_8 = 8;
36 const int BEACON_FENCE_UUID_OFFSET_10 = 10;
37 const int BEACON_FENCE_UUID_OFFSET_12 = 12;
38 
BeaconFenceManager()39 BeaconFenceManager::BeaconFenceManager()
40 {
41     beaconFenceId_ = 0;
42 #ifdef BLUETOOTH_ENABLE
43     std::shared_ptr<BeaconBleCallbackWapper> bleCallback = std::make_shared<BeaconBleCallbackWapper>();
44     bleCentralManager_ = std::make_shared<Bluetooth::BleCentralManager>(bleCallback);
45 #endif
46 }
47 
GetInstance()48 BeaconFenceManager* BeaconFenceManager::GetInstance()
49 {
50     static BeaconFenceManager data;
51     return &data;
52 }
53 
~BeaconFenceManager()54 BeaconFenceManager::~BeaconFenceManager() {}
55 
GenerateBeaconFenceId()56 int32_t BeaconFenceManager::GenerateBeaconFenceId()
57 {
58     beaconFenceId_++;
59     std::int32_t id = beaconFenceId_;
60     return id;
61 }
62 
AddBeaconFence(std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest,const AppIdentity & identity)63 ErrCode BeaconFenceManager::AddBeaconFence(std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest,
64     const AppIdentity& identity)
65 {
66     int beaconFenceId = GenerateBeaconFenceId();
67     beaconFenceRequest->SetFenceId(std::to_string(beaconFenceId));
68 
69     // 解析请求数据
70     std::shared_ptr<BeaconFence> beaconFence = beaconFenceRequest->GetBeaconFence();
71     BeaconManufactureData beaconManufactureData = beaconFence->GetBeaconManufactureData();
72     std::string uuid = ExtractiBeaconUUID(beaconManufactureData.manufactureData);
73     std::string uuidMask = ExtractiBeaconUUID(beaconManufactureData.manufactureDataMask);
74 
75     beaconFenceRequest->SetServiceUuid(uuid);
76     beaconFenceRequest->SetServiceUuidMask(uuidMask);
77 #ifdef BLUETOOTH_ENABLE
78     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
79     if (beaconFenceRequestMap_.size() >= MAX_REQUEST_MAP_NUM) {
80         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, beaconfence request is exceed max number!", __func__);
81         return ERRCODE_BEACONFENCE_EXCEED_MAXIMUM;
82     }
83     if (isBeaconFenceRequestExists(beaconFenceRequest)) {
84         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, Request is exists!", __func__);
85         return ERRCODE_BEACONFENCE_DUPLICATE_INFORMATION;
86     }
87     RegisterBeaconFenceCallback(beaconFenceRequest, identity);
88     StartAddBeaconFence(beaconFenceRequest, identity);
89 #endif
90     return ERRCODE_SUCCESS;
91 }
92 
RegisterBeaconFenceCallback(std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest,const AppIdentity & appIdentity)93 void BeaconFenceManager::RegisterBeaconFenceCallback(std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest,
94     const AppIdentity& appIdentity)
95 {
96     sptr<IRemoteObject> callback = beaconFenceRequest->GetBeaconFenceTransitionCallback();
97     if (callback != nullptr) {
98         sptr<IRemoteObject::DeathRecipient> death(new (std::nothrow) BeaconFenceCallbackDeathRecipient());
99         callback->AddDeathRecipient(death);
100     }
101     beaconFenceRequestMap_.insert(std::make_pair(beaconFenceRequest, std::make_pair(callback, appIdentity)));
102     LBSLOGI(BEACON_FENCE_MANAGER, "after AddBeaconFence, request size:%{public}zu",
103         beaconFenceRequestMap_.size());
104 }
105 
isBeaconFenceRequestExists(const std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest)106 bool BeaconFenceManager::isBeaconFenceRequestExists(const std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest)
107 {
108     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
109         auto request = iter->first;
110         if (CompareUUID(request->GetServiceUuid(), beaconFenceRequest->GetServiceUuid()) &&
111             request->GetBundleName().compare(beaconFenceRequest->GetBundleName()) == 0) {
112             return true;
113         }
114     }
115     return false;
116 }
117 
StartAddBeaconFence(std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest,const AppIdentity & identity)118 void BeaconFenceManager::StartAddBeaconFence(std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest,
119     const AppIdentity& identity)
120 {
121     StopBluetoothScan();
122     StartBluetoothScan();
123     OnReportOperationResultByCallback(
124         beaconFenceRequest, GnssGeofenceOperateType::GNSS_GEOFENCE_OPT_TYPE_ADD,
125         GnssGeofenceOperateResult::GNSS_GEOFENCE_OPERATION_SUCCESS);
126 }
127 
RemoveBeaconFence(const std::shared_ptr<BeaconFence> & beaconFence)128 ErrCode BeaconFenceManager::RemoveBeaconFence(const std::shared_ptr<BeaconFence>& beaconFence)
129 {
130     // 解析请求数据
131     BeaconManufactureData beaconManufactureData = beaconFence->GetBeaconManufactureData();
132     std::string uuid = ExtractiBeaconUUID(beaconManufactureData.manufactureData);
133     std::string uuidMask = ExtractiBeaconUUID(beaconManufactureData.manufactureDataMask);
134 
135     // 判断是否在请求集合
136     std::shared_ptr<BeaconFenceRequest> beaconFenceRequest = GetBeaconFenceRequestByServiceUuid(uuid);
137     if (beaconFenceRequest == nullptr) {
138         LBSLOGE(BEACON_FENCE_MANAGER, "beaconFence is not registered");
139         return ERRCODE_BEACONFENCE_INCORRECT_ID;
140     }
141     if (!CompareBeaconFence(beaconFence, beaconFenceRequest->GetBeaconFence())) {
142         LBSLOGE(BEACON_FENCE_MANAGER, "CompareBeaconFence false");
143         return ERRCODE_BEACONFENCE_INCORRECT_ID;
144     }
145     RemoveBeaconFenceRequestByBeacon(beaconFence);
146 #ifdef BLUETOOTH_ENABLE
147     StopBluetoothScan();
148     {
149         std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
150         LBSLOGI(BEACON_FENCE_MANAGER, "after RemoveBeaconFence, request size:%{public}zu",
151             beaconFenceRequestMap_.size());
152         if (beaconFenceRequestMap_.size() != 0) {
153             StartBluetoothScan();
154         }
155     }
156     OnReportOperationResultByCallback(
157         beaconFenceRequest, GnssGeofenceOperateType::GNSS_GEOFENCE_OPT_TYPE_DELETE,
158         GnssGeofenceOperateResult::GNSS_GEOFENCE_OPERATION_SUCCESS);
159 #endif
160     return ERRCODE_SUCCESS;
161 }
162 
163 #ifdef BLUETOOTH_ENABLE
StartBluetoothScan()164 void BeaconFenceManager::StartBluetoothScan()
165 {
166     if (bleCentralManager_ == nullptr) {
167         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, bleCentralManager_ == nullptr", __func__);
168         return;
169     }
170     Bluetooth::BleScanSettings settings;
171     settings.SetScanMode(Bluetooth::SCAN_MODE::SCAN_MODE_LOW_POWER);
172     // 仅进入,或者丢失时上报,
173     settings.SetReportMode(static_cast<int>(Bluetooth::REPORT_MODE::REPORT_MODE_FENCE_SENSITIVITY_LOW));
174     settings.SetCallbackType(Bluetooth::BLE_SCAN_CALLBACK_TYPE_FIRST_AND_LOST_MATCH);
175 
176     // 添加过滤条件
177     std::vector<Bluetooth::BleScanFilter> filters;
178     ConstructFilter(filters);
179     bleCentralManager_->StartScan(settings, filters);
180 }
181 
StopBluetoothScan()182 void BeaconFenceManager::StopBluetoothScan()
183 {
184     if (bleCentralManager_ == nullptr) {
185         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, bleCentralManager_ == nullptr", __func__);
186         return;
187     }
188     bleCentralManager_->StopScan();
189 }
190 
ConstructFilter(std::vector<Bluetooth::BleScanFilter> & filters)191 void BeaconFenceManager::ConstructFilter(std::vector<Bluetooth::BleScanFilter>& filters)
192 {
193     std::vector<BeaconManufactureData> beaconManufactureDataVector = GetBeaconManufactureDataForFilter();
194     for (const auto& data : beaconManufactureDataVector) {
195         Bluetooth::BleScanFilter scanFilter;
196         scanFilter.SetManufacturerId(data.manufactureId);
197         scanFilter.SetManufactureData(data.manufactureData);
198         scanFilter.SetManufactureDataMask(data.manufactureDataMask);
199         filters.push_back(scanFilter);
200     }
201 }
202 #endif
203 
OnReportOperationResultByCallback(const std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest,GnssGeofenceOperateType type,GnssGeofenceOperateResult result)204 void BeaconFenceManager::OnReportOperationResultByCallback(
205     const std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest,
206     GnssGeofenceOperateType type, GnssGeofenceOperateResult result)
207 {
208     auto callback = beaconFenceRequest->GetBeaconFenceTransitionCallback();
209     if (callback != nullptr) {
210         std::string fenceId = beaconFenceRequest->GetFenceId();
211         if (IsStrValidForStoi(fenceId)) {
212             sptr<IGnssGeofenceCallback> beaconFenceCallback = iface_cast<IGnssGeofenceCallback>(callback);
213             beaconFenceCallback->OnReportOperationResult(stoi(fenceId), type, result);
214         }
215     }
216 }
217 
ExtractiBeaconUUID(const std::vector<uint8_t> & data)218 std::string BeaconFenceManager::ExtractiBeaconUUID(const std::vector<uint8_t>& data)
219 {
220     // 数据最小长度检查
221     if (data.size() < BEACON_FENCE_DATA_LENGTH) {
222         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s manufactureData len invalid", __func__);
223         return "";
224     }
225     std::ostringstream oss;
226     oss << std::hex << std::setfill('0');
227     // 提取UUID部分
228     for (int i = BEACON_FENCE_UUID_OFFSET_2; i < BEACON_FENCE_DATA_LENGTH; ++i) {
229         if (i == BEACON_FENCE_UUID_OFFSET_6 || i == BEACON_FENCE_UUID_OFFSET_8 ||
230             i == BEACON_FENCE_UUID_OFFSET_10 || i == BEACON_FENCE_UUID_OFFSET_12) {
231                 oss << "-";
232             }
233         oss << std::setw(BEACON_FENCE_UUID_OFFSET_2) << static_cast<int>(data[i]);
234     }
235     std::string uuid = oss.str();
236     return uuid;
237 }
238 
239 #ifdef BLUETOOTH_ENABLE
ReportFoundOrLost(const Bluetooth::BleScanResult & result,uint8_t type)240 void BeaconFenceManager::ReportFoundOrLost(const Bluetooth::BleScanResult &result, uint8_t type)
241 {
242     std::shared_ptr<BeaconFenceRequest> beaconFenceRequest = GetBeaconFenceRequestByScanResult(result);
243     if (beaconFenceRequest == nullptr) {
244         return;
245     }
246     AppIdentity identity = GetAppIdentityByBeaconFenceRequest(beaconFenceRequest);
247     if (type == Bluetooth::BLE_SCAN_CALLBACK_TYPE_FIRST_MATCH) {
248         // 首次进入围栏
249         TransitionStatusChange(beaconFenceRequest, GeofenceTransitionEvent::GEOFENCE_TRANSITION_EVENT_ENTER, identity);
250     } else if (type == Bluetooth::BLE_SCAN_CALLBACK_TYPE_LOST_MATCH) {
251         // 首次退出围栏
252         TransitionStatusChange(beaconFenceRequest, GeofenceTransitionEvent::GEOFENCE_TRANSITION_EVENT_EXIT, identity);
253     }
254 }
255 
GetBeaconFenceRequestByScanResult(const Bluetooth::BleScanResult & result)256 std::shared_ptr<BeaconFenceRequest> BeaconFenceManager::GetBeaconFenceRequestByScanResult(
257     const Bluetooth::BleScanResult &result)
258 {
259     std::map<uint16_t, std::string> dataMap = result.GetManufacturerData();
260     std::string scanData;
261     for (const auto& pair : dataMap) {
262         scanData = pair.second;
263     }
264 
265     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
266     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
267         auto request = iter->first;
268         if (MatchesData(request->GetBeaconFence()->GetBeaconManufactureData().manufactureData, scanData)) {
269             return request;
270         }
271     }
272     LBSLOGE(BEACON_FENCE_MANAGER, "can not get request by BleScanResult");
273     return nullptr;
274 }
275 
MatchesData(std::vector<uint8_t> fData,std::string scanData)276 bool BeaconFenceManager::MatchesData(std::vector<uint8_t> fData, std::string scanData)
277 {
278     if (scanData.empty()) {
279         return false;
280     }
281     size_t len = fData.size();
282     std::vector<uint8_t> vec(scanData.begin(), scanData.end());
283     if (vec.size() < len) {
284         return false;
285     }
286     for (size_t i = 0; i < len; i++) {
287         if (fData[i] != vec[i]) {
288             return false;
289         }
290     }
291     return true;
292 }
293 #endif
294 
TransitionStatusChange(std::shared_ptr<BeaconFenceRequest> beaconFenceRequest,GeofenceTransitionEvent event,const AppIdentity & identity)295 void BeaconFenceManager::TransitionStatusChange(std::shared_ptr<BeaconFenceRequest> beaconFenceRequest,
296     GeofenceTransitionEvent event, const AppIdentity &identity)
297 {
298     // 判断开关状态
299     int state = DEFAULT_SWITCH_STATE;
300     state = LocationDataRdbManager::QuerySwitchState();
301     if (state == DISABLED) {
302         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s location switch is off", __func__);
303         return;
304     }
305     // 判断应用是否卸载
306     std::shared_ptr<BeaconFence> beacon = beaconFenceRequest->GetBeaconFence();
307     if (!CommonUtils::CheckAppInstalled(beaconFenceRequest->GetBundleName())) {
308         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s service is not available", __func__);
309         RemoveBeaconFence(beacon);
310         return;
311     }
312     // 是否后台
313     if (LocatorBackgroundProxy::GetInstance()->IsAppBackground(identity.GetUid(), identity.GetBundleName())) {
314         if (beaconFenceRequest->GetFenceExtensionAbilityName().empty()) {
315             LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s app is background", __func__);
316             return;
317         }
318         if (!HookUtils::ExecuteHookWhenBeaconFenceTransitionStatusChange(identity.GetBundleName())) {
319             LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s can not start extension", __func__);
320             return;
321         }
322     }
323     std::string fenceId = beaconFenceRequest->GetFenceId();
324     // 通过callback回调事件
325     auto callback = beaconFenceRequest->GetBeaconFenceTransitionCallback();
326     if (callback != nullptr) {
327         if (IsStrValidForStoi(fenceId)) {
328             sptr<IGnssGeofenceCallback> gnssGeofenceCallback = iface_cast<IGnssGeofenceCallback>(callback);
329             GeofenceTransition geofenceTransition;
330             geofenceTransition.fenceId = stoi(fenceId);
331             geofenceTransition.event = event;
332             geofenceTransition.beaconFence = beacon;
333             gnssGeofenceCallback->OnTransitionStatusChange(geofenceTransition);
334         }
335     }
336     // 拉起fenceExtension
337     if (!beaconFenceRequest->GetFenceExtensionAbilityName().empty()) {
338         HookUtils::ExecuteHookWhenReportBeaconFenceOperateResult(fenceId, static_cast<int>(event),
339             beaconFenceRequest->GetFenceExtensionAbilityName(), beaconFenceRequest->GetBundleName());
340     }
341 }
342 
IsStrValidForStoi(const std::string & str)343 bool BeaconFenceManager::IsStrValidForStoi(const std::string &str)
344 {
345     if (str.length() > STOI_BYTE_LIMIT) {
346         return false;
347     }
348     return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit);
349 }
350 
GetAppIdentityByBeaconFenceRequest(const std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest)351 AppIdentity BeaconFenceManager::GetAppIdentityByBeaconFenceRequest(
352     const std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest)
353 {
354     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
355     AppIdentity appIdentity;
356     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
357         auto request = iter->first;
358         if (beaconFenceRequest->GetServiceUuid() == request->GetServiceUuid()) {
359             auto callbackPair = iter->second;
360             auto identity = callbackPair.second;
361             return identity;
362         }
363     }
364     return appIdentity;
365 }
366 
GetBeaconFenceRequestByServiceUuid(std::string serviceUuid)367 std::shared_ptr<BeaconFenceRequest> BeaconFenceManager::GetBeaconFenceRequestByServiceUuid(std::string serviceUuid)
368 {
369     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
370     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
371         auto request = iter->first;
372         if (CompareUUID(request->GetServiceUuid(), serviceUuid)) {
373             return request;
374         }
375     }
376     LBSLOGE(BEACON_FENCE_MANAGER, "can not get request by serviceUuid");
377     return nullptr;
378 }
379 
GetBeaconFenceRequestByCallback(sptr<IRemoteObject> callbackObj)380 std::shared_ptr<BeaconFenceRequest> BeaconFenceManager::GetBeaconFenceRequestByCallback(sptr<IRemoteObject> callbackObj)
381 {
382     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
383     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
384         auto callbackPair = iter->second;
385         auto callback = callbackPair.first;
386         if (callback == callbackObj) {
387             return iter->first;
388         }
389     }
390     return nullptr;
391 }
392 
GetBeaconFenceRequestByPackageName(std::string & packageName)393 std::shared_ptr<BeaconFenceRequest> BeaconFenceManager::GetBeaconFenceRequestByPackageName(std::string& packageName)
394 {
395     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
396     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
397         auto callbackPair = iter->second;
398         auto identity = callbackPair.second;
399         if (packageName.compare(identity.GetBundleName()) == 0) {
400             return iter->first;
401         }
402     }
403     return nullptr;
404 }
405 
RemoveBeaconFenceRequestByBeacon(std::shared_ptr<BeaconFence> beaconFence)406 void BeaconFenceManager::RemoveBeaconFenceRequestByBeacon(std::shared_ptr<BeaconFence> beaconFence)
407 {
408     std::lock_guard<std::mutex> lock(beaconFenceRequestMapMutex_);
409     std::shared_ptr<BeaconFenceRequest> beaconFenceRequest = GetBeaconFenceRequestByBeacon(beaconFence);
410     if (beaconFenceRequest != nullptr) {
411         beaconFenceRequestMap_.erase(beaconFenceRequest);
412     }
413 }
414 
GetBeaconFenceRequestByBeacon(std::shared_ptr<BeaconFence> beaconFence)415 std::shared_ptr<BeaconFenceRequest> BeaconFenceManager::GetBeaconFenceRequestByBeacon(
416     std::shared_ptr<BeaconFence> beaconFence)
417 {
418     for (auto iter = beaconFenceRequestMap_.begin();
419         iter != beaconFenceRequestMap_.end(); iter++) {
420         auto request = iter->first;
421         auto beacon = request->GetBeaconFence();
422         if (CompareBeaconFence(beaconFence, beacon)) {
423             return request;
424         }
425     }
426     return nullptr;
427 }
428 
RemoveBeaconFenceRequestByCallback(sptr<IRemoteObject> callbackObj)429 void BeaconFenceManager::RemoveBeaconFenceRequestByCallback(sptr<IRemoteObject> callbackObj)
430 {
431     std::shared_ptr<BeaconFenceRequest> beaconFenceRequest = GetBeaconFenceRequestByCallback(callbackObj);
432     if (beaconFenceRequest == nullptr) {
433         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, beaconFenceRequest == nullptr", __func__);
434         return;
435     }
436     std::string fenceExtensionAbilityName = beaconFenceRequest->GetFenceExtensionAbilityName();
437     if (fenceExtensionAbilityName.empty() ||
438         !HookUtils::ExecuteHookWhenRemoveBeaconFenceByCallback(beaconFenceRequest->GetBundleName())) {
439         std::shared_ptr<BeaconFence> beacon = beaconFenceRequest->GetBeaconFence();
440         RemoveBeaconFence(beacon);
441     }
442 }
443 
RemoveBeaconFenceByPackageName(std::string & packageName)444 void BeaconFenceManager::RemoveBeaconFenceByPackageName(std::string& packageName)
445 {
446     std::shared_ptr<BeaconFenceRequest> beaconFenceRequest = GetBeaconFenceRequestByPackageName(packageName);
447     if (beaconFenceRequest != nullptr) {
448         std::shared_ptr<BeaconFence> beaconFence = beaconFenceRequest->GetBeaconFence();
449         RemoveBeaconFence(beaconFence);
450     }
451 }
452 
CompareUUID(const std::string & uuid1,const std::string & uuid2)453 bool BeaconFenceManager::CompareUUID(const std::string& uuid1, const std::string& uuid2)
454 {
455     std::string lower1 = uuid1;
456     std::string lower2 = uuid2;
457     std::transform(lower1.begin(), lower1.end(), lower1.begin(), ::tolower);
458     std::transform(lower2.begin(), lower2.end(), lower2.begin(), ::tolower);
459     return lower1 == lower2;
460 }
461 
CompareBeaconFence(std::shared_ptr<BeaconFence> beaconFence1,std::shared_ptr<BeaconFence> beaconFence2)462 bool BeaconFenceManager::CompareBeaconFence(
463     std::shared_ptr<BeaconFence> beaconFence1, std::shared_ptr<BeaconFence> beaconFence2)
464 {
465     if (beaconFence1->GetIdentifier().compare(beaconFence2->GetIdentifier()) != 0) {
466         LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, compare Identifier fail", __func__);
467         return false;
468     }
469     if (static_cast<int>(beaconFence1->GetBeaconFenceInfoType()) !=
470         static_cast<int>(beaconFence2->GetBeaconFenceInfoType())) {
471             LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, compare BeaconFenceInfoType fail", __func__);
472         return false;
473     }
474     if (beaconFence1->GetBeaconManufactureData().manufactureId !=
475         beaconFence2->GetBeaconManufactureData().manufactureId) {
476             LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, compare manufactureId fail", __func__);
477         return false;
478     }
479     if (beaconFence1->GetBeaconManufactureData().manufactureData !=
480         beaconFence2->GetBeaconManufactureData().manufactureData) {
481             LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, compare manufactureData fail", __func__);
482         return false;
483     }
484     if (beaconFence1->GetBeaconManufactureData().manufactureDataMask !=
485         beaconFence2->GetBeaconManufactureData().manufactureDataMask) {
486             LBSLOGE(BEACON_FENCE_MANAGER, "%{public}s, compare manufactureDataMask fail", __func__);
487         return false;
488     }
489     LBSLOGD(BEACON_FENCE_MANAGER, "%{public}s res:true", __func__);
490     return true;
491 }
492 
GetBeaconManufactureDataForFilter()493 std::vector<BeaconManufactureData> BeaconFenceManager::GetBeaconManufactureDataForFilter()
494 {
495     std::vector<BeaconManufactureData> filters;
496     for (auto iter = beaconFenceRequestMap_.begin(); iter != beaconFenceRequestMap_.end(); iter++) {
497         auto request = iter->first;
498         BeaconManufactureData beaconManufactureData = request->GetBeaconFence()->GetBeaconManufactureData();
499         filters.push_back(beaconManufactureData);
500     }
501     return filters;
502 }
503 
504 #ifdef BLUETOOTH_ENABLE
OnFoundOrLostCallback(const Bluetooth::BleScanResult & result,uint8_t callbackType)505 void BeaconBleCallbackWapper::OnFoundOrLostCallback(const Bluetooth::BleScanResult &result, uint8_t callbackType)
506 {
507     // 2进,4出  10s无设备扫描到认为出围栏
508     LBSLOGI(BEACON_FENCE_MANAGER, "%{public}s, callbackType:%{public}hhu", __func__, callbackType);
509     BeaconFenceManager::GetInstance()->ReportFoundOrLost(result, callbackType);
510 }
511 
OnScanCallback(const Bluetooth::BleScanResult & result)512 void BeaconBleCallbackWapper::OnScanCallback(const Bluetooth::BleScanResult &result) {}
513 
OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> & results)514 void BeaconBleCallbackWapper::OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> &results) {}
515 
OnStartOrStopScanEvent(int32_t resultCode,bool isStartScan)516 void BeaconBleCallbackWapper::OnStartOrStopScanEvent(int32_t resultCode, bool isStartScan) {}
517 
OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID & btUuid,int msgType,const std::vector<uint8_t> & value)518 void BeaconBleCallbackWapper::OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID &btUuid, int msgType,
519     const std::vector<uint8_t> &value) {}
520 #endif
521 
SetBeaconFenceRequest(std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest)522 void BeaconFenceRequestMessage::SetBeaconFenceRequest(std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest)
523 {
524     beaconFenceRequest_ = beaconFenceRequest;
525 }
526 
GetBeaconFenceRequest()527 std::shared_ptr<BeaconFenceRequest> BeaconFenceRequestMessage::GetBeaconFenceRequest()
528 {
529     return beaconFenceRequest_;
530 }
531 
SetAppIdentity(AppIdentity & appIdentity)532 void BeaconFenceRequestMessage::SetAppIdentity(AppIdentity& appIdentity)
533 {
534     appIdentity_ = appIdentity;
535 }
536 
GetAppIdentity()537 AppIdentity BeaconFenceRequestMessage::GetAppIdentity()
538 {
539     return appIdentity_;
540 }
541 
BeaconFenceCallbackDeathRecipient()542 BeaconFenceCallbackDeathRecipient::BeaconFenceCallbackDeathRecipient()
543 {
544 }
545 
~BeaconFenceCallbackDeathRecipient()546 BeaconFenceCallbackDeathRecipient::~BeaconFenceCallbackDeathRecipient()
547 {
548 }
549 
OnRemoteDied(const wptr<IRemoteObject> & remote)550 void BeaconFenceCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
551 {
552     LBSLOGI(BEACON_FENCE_MANAGER, "beaconFence callback OnRemoteDied");
553     BeaconFenceManager::GetInstance()->RemoveBeaconFenceRequestByCallback(remote.promote());
554 }
555 } // namespace Location
556 } // namespace OHOS
557