• 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 "locator_required_data_manager.h"
16 #include "location_log.h"
17 #include "wifi_errcode.h"
18 
19 namespace OHOS {
20 namespace Location {
21 const uint32_t EVENT_START_SCAN = 0x0100;
22 const uint32_t EVENT_STOP_SCAN = 0x0200;
LocatorRequiredDataManager()23 LocatorRequiredDataManager::LocatorRequiredDataManager()
24 {
25     wifiScanPtr_ = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
26     wifiScanEventCallback_ =
27 		sptr<LocatorWifiScanEventCallback>(new (std::nothrow) LocatorWifiScanEventCallback());
28     std::shared_ptr<LocatorBleCallbackWapper> callback = std::make_shared<LocatorBleCallbackWapper>();
29     bleCentralManager_ = std::make_shared<Bluetooth::BleCentralManager>(callback);
30     bluetoothHost_ = &Bluetooth::BluetoothHost::GetDefaultHost();
31     scanHandler_ = std::make_shared<ScanHandler>(AppExecFwk::EventRunner::Create(true));
32 }
33 
~LocatorRequiredDataManager()34 LocatorRequiredDataManager::~LocatorRequiredDataManager()
35 {
36 }
37 
RegisterCallback(std::shared_ptr<LocatingRequiredDataConfig> & config,const sptr<IRemoteObject> & callback)38 LocationErrCode LocatorRequiredDataManager::RegisterCallback(std::shared_ptr<LocatingRequiredDataConfig>& config,
39     const sptr<IRemoteObject>& callback)
40 {
41     int ret = 0;
42     LBSLOGI(LOCATOR, "%{public}s enter", __func__);
43     sptr<ILocatingRequiredDataCallback> dataCallback = iface_cast<ILocatingRequiredDataCallback>(callback);
44     if (dataCallback == nullptr) {
45         LBSLOGE(LOCATOR, "%{public}s iface_cast ILocatingRequiredDataCallback failed!", __func__);
46         return ERRCODE_INVALID_PARAM;
47     }
48     if (config->GetType() == LocatingRequiredDataType::WIFI) {
49         if (wifiScanPtr_ == nullptr) {
50             LBSLOGE(LOCATOR, "%{public}s WifiScan get instance failed", __func__);
51             return ERRCODE_SERVICE_UNAVAILABLE;
52         }
53         if (wifiScanEventCallback_ == nullptr) {
54             LBSLOGE(LOCATOR, "%{public}s wifi scanInfo callback is nullptr!", __func__);
55             return ERRCODE_SERVICE_UNAVAILABLE;
56         }
57         std::vector<std::string> events = {EVENT_STA_SCAN_STATE_CHANGE};
58         ret = wifiScanPtr_->RegisterCallBack(wifiScanEventCallback_, events);
59         if (ret != Wifi::WIFI_OPT_SUCCESS) {
60             LBSLOGE(LOCATOR, "%{public}s WifiScan RegisterCallBack failed!", __func__);
61             return ERRCODE_SERVICE_UNAVAILABLE;
62         }
63         std::unique_lock<std::mutex> lock(mutex_, std::defer_lock);
64         lock.lock();
65         callbacks_.push_back(dataCallback);
66         lock.unlock();
67         LBSLOGI(LOCATOR, "after RegisterCallback, callback size:%{public}s", std::to_string(callbacks_.size()).c_str());
68         if (config->GetNeedStartScan()) {
69             timeInterval_ = config->GetScanIntervalMs();
70             if (scanHandler_ != nullptr) {
71                 scanHandler_->SendEvent(EVENT_START_SCAN, 0, 0);
72             }
73         }
74     } else if (config->GetType() == LocatingRequiredDataType::BLUE_TOOTH) {
75         return ERRCODE_NOT_SUPPORTED;
76         if (bluetoothHost_ == nullptr) {
77             LBSLOGE(LOCATOR, "%{public}s bluetoothHost is nullptr", __func__);
78             return ERRCODE_SERVICE_UNAVAILABLE;
79         }
80         std::unique_lock<std::mutex> lock(mutex_, std::defer_lock);
81         lock.lock();
82         callbacks_.push_back(dataCallback);
83         lock.unlock();
84         LBSLOGI(LOCATOR, "after RegisterCallback,  callback size:%{public}s",
85             std::to_string(callbacks_.size()).c_str());
86         if (config->GetNeedStartScan()) {
87             bleCentralManager_->StartScan();
88             bluetoothHost_->RegisterObserver(locatorBluetoothHost_);
89         }
90     }
91     return ERRCODE_SUCCESS;
92 }
93 
UnregisterCallback(const sptr<IRemoteObject> & callback)94 LocationErrCode LocatorRequiredDataManager::UnregisterCallback(const sptr<IRemoteObject>& callback)
95 {
96     LBSLOGI(LOCATOR, "%{public}s enter", __func__);
97     sptr<ILocatingRequiredDataCallback> dataCallback = iface_cast<ILocatingRequiredDataCallback>(callback);
98     if (dataCallback == nullptr) {
99         LBSLOGE(LOCATOR, "%{public}s iface_cast ILocatingRequiredDataCallback failed!", __func__);
100         return ERRCODE_SERVICE_UNAVAILABLE;
101     }
102     if (scanHandler_ != nullptr) {
103         scanHandler_->SendEvent(EVENT_STOP_SCAN, 0, 0);
104     }
105     std::unique_lock<std::mutex> lock(mutex_);
106     size_t i = 0;
107     for (; i < callbacks_.size(); i++) {
108         sptr<IRemoteObject> remoteObject = callbacks_[i]->AsObject();
109         if (remoteObject == callback) {
110             break;
111         }
112     }
113     if (callbacks_.size() > 0) {
114         callbacks_.erase(callbacks_.begin() + i);
115     }
116     LBSLOGI(LOCATOR, "after UnregisterCallback,  callback size:%{public}s", std::to_string(callbacks_.size()).c_str());
117     return ERRCODE_SUCCESS;
118 }
119 
GetLocatingRequiredDataByBtHost(const Bluetooth::BluetoothRemoteDevice & device)120 std::vector<std::shared_ptr<LocatingRequiredData>> LocatorBluetoothHost::GetLocatingRequiredDataByBtHost(
121     const Bluetooth::BluetoothRemoteDevice &device)
122 {
123     std::vector<std::shared_ptr<LocatingRequiredData>> result;
124     std::shared_ptr<LocatingRequiredData> info = std::make_shared<LocatingRequiredData>();
125     std::shared_ptr<BluetoothScanInfo> btData = std::make_shared<BluetoothScanInfo>();
126     btData->SetMac(device.GetDeviceAddr());
127     btData->SetDeviceName(device.GetDeviceName());
128     info->SetType(LocatingRequiredDataType::BLUE_TOOTH);
129     info->SetBluetoothScanInfo(btData);
130     result.push_back(info);
131     return result;
132 }
133 
GetLocatingRequiredDataByBle(const Bluetooth::BleScanResult & result)134 std::vector<std::shared_ptr<LocatingRequiredData>> LocatorBleCallbackWapper::GetLocatingRequiredDataByBle(
135     const Bluetooth::BleScanResult &result)
136 {
137     std::vector<std::shared_ptr<LocatingRequiredData>> res;
138     std::shared_ptr<LocatingRequiredData> info = std::make_shared<LocatingRequiredData>();
139     std::shared_ptr<BluetoothScanInfo> btData = std::make_shared<BluetoothScanInfo>();
140     btData->SetMac(result.GetPeripheralDevice().GetDeviceAddr());
141     btData->SetDeviceName(result.GetPeripheralDevice().GetDeviceName());
142     btData->SetRssi(result.GetRssi());
143     info->SetType(LocatingRequiredDataType::BLUE_TOOTH);
144     info->SetBluetoothScanInfo(btData);
145     res.push_back(info);
146     return res;
147 }
148 
OnStateChanged(const int transport,const int status)149 void LocatorBluetoothHost::OnStateChanged(const int transport, const int status) {}
150 
OnDiscoveryStateChanged(int status)151 void LocatorBluetoothHost::OnDiscoveryStateChanged(int status) {}
152 
OnDiscoveryResult(const Bluetooth::BluetoothRemoteDevice & device)153 void LocatorBluetoothHost::OnDiscoveryResult(const Bluetooth::BluetoothRemoteDevice &device)
154 {
155     std::vector<std::shared_ptr<LocatingRequiredData>> result = GetLocatingRequiredDataByBtHost(device);
156     auto dataManager = DelayedSingleton<LocatorRequiredDataManager>::GetInstance();
157     if (dataManager == nullptr) {
158         LBSLOGE(NETWORK, "ProcessEvent: dataManager is nullptr");
159         return;
160     }
161     dataManager->ReportData(result);
162 }
163 
OnPairRequested(const Bluetooth::BluetoothRemoteDevice & device)164 void LocatorBluetoothHost::OnPairRequested(const Bluetooth::BluetoothRemoteDevice &device) {}
165 
OnPairConfirmed(const Bluetooth::BluetoothRemoteDevice & device,int reqType,int number)166 void LocatorBluetoothHost::OnPairConfirmed(const Bluetooth::BluetoothRemoteDevice &device, int reqType, int number) {}
167 
OnScanModeChanged(int mode)168 void LocatorBluetoothHost::OnScanModeChanged(int mode) {}
169 
OnDeviceNameChanged(const std::string & deviceName)170 void LocatorBluetoothHost::OnDeviceNameChanged(const std::string &deviceName) {}
171 
OnDeviceAddrChanged(const std::string & address)172 void LocatorBluetoothHost::OnDeviceAddrChanged(const std::string &address) {}
173 
OnScanCallback(const Bluetooth::BleScanResult & result)174 void LocatorBleCallbackWapper::OnScanCallback(const Bluetooth::BleScanResult &result)
175 {
176     std::vector<std::shared_ptr<LocatingRequiredData>> res = GetLocatingRequiredDataByBle(result);
177     auto dataManager = DelayedSingleton<LocatorRequiredDataManager>::GetInstance();
178     if (dataManager == nullptr) {
179         LBSLOGE(NETWORK, "ProcessEvent: dataManager is nullptr");
180         return;
181     }
182     dataManager->ReportData(res);
183 }
184 
OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> & results)185 void LocatorBleCallbackWapper::OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> &results) {}
186 
OnStartOrStopScanEvent(int32_t resultCode,bool isStartScan)187 void LocatorBleCallbackWapper::OnStartOrStopScanEvent(int32_t resultCode, bool isStartScan) {}
188 
OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID & btUuid,int msgType,const std::vector<uint8_t> & value)189 void LocatorBleCallbackWapper::OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID &btUuid, int msgType,
190     const std::vector<uint8_t> &value) {}
191 
OnWifiScanStateChanged(int state)192 void LocatorWifiScanEventCallback::OnWifiScanStateChanged(int state)
193 {
194     LBSLOGE(LOCATOR, "OnWifiScanStateChanged state=%{public}d", state);
195     if (state == 0) {
196         LBSLOGE(LOCATOR, "OnWifiScanStateChanged false");
197         return;
198     }
199     std::vector<Wifi::WifiScanInfo> wifiScanInfo;
200     std::unique_ptr<Wifi::WifiScan> ptrWifiScan = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
201     if (ptrWifiScan == nullptr) {
202         LBSLOGE(LOCATOR, "%{public}s WifiScan get instance failed", __func__);
203         return;
204     }
205     int ret = ptrWifiScan->GetScanInfoList(wifiScanInfo);
206     if (ret != Wifi::WIFI_OPT_SUCCESS) {
207         LBSLOGE(LOCATOR, "GetScanInfoList failed");
208         return;
209     }
210     std::vector<std::shared_ptr<LocatingRequiredData>> result = GetLocatingRequiredDataByWifi(wifiScanInfo);
211     auto dataManager = DelayedSingleton<LocatorRequiredDataManager>::GetInstance();
212     if (dataManager == nullptr) {
213         LBSLOGE(NETWORK, "ProcessEvent: dataManager is nullptr");
214         return;
215     }
216     dataManager->ReportData(result);
217     return;
218 }
219 
GetLocatingRequiredDataByWifi(const std::vector<Wifi::WifiScanInfo> & wifiScanInfo)220 std::vector<std::shared_ptr<LocatingRequiredData>> LocatorWifiScanEventCallback::GetLocatingRequiredDataByWifi(
221     const std::vector<Wifi::WifiScanInfo>& wifiScanInfo)
222 {
223     std::vector<std::shared_ptr<LocatingRequiredData>> res;
224     for (size_t i = 0; i < wifiScanInfo.size(); i++) {
225         std::shared_ptr<LocatingRequiredData> info = std::make_shared<LocatingRequiredData>();
226         std::shared_ptr<WifiScanInfo> wifiData = std::make_shared<WifiScanInfo>();
227         wifiData->SetSsid(wifiScanInfo[i].ssid);
228         wifiData->SetBssid(wifiScanInfo[i].bssid);
229         wifiData->SetRssi(wifiScanInfo[i].rssi);
230         wifiData->SetFrequency(wifiScanInfo[i].frequency);
231         wifiData->SetTimestamp(wifiScanInfo[i].timestamp);
232         info->SetType(LocatingRequiredDataType::WIFI);
233         info->SetWifiScanInfo(wifiData);
234         res.push_back(info);
235     }
236     return res;
237 }
238 
ReportData(const std::vector<std::shared_ptr<LocatingRequiredData>> & result)239 void LocatorRequiredDataManager::ReportData(const std::vector<std::shared_ptr<LocatingRequiredData>>& result)
240 {
241     std::unique_lock<std::mutex> lock(mutex_);
242     for (size_t i = 0; i < callbacks_.size(); i++) {
243         callbacks_[i]->OnLocatingDataChange(result);
244     }
245 }
246 
StartWifiScan(bool flag)247 void LocatorRequiredDataManager::StartWifiScan(bool flag)
248 {
249     if (!flag) {
250         LBSLOGI(LOCATOR, "%{public}s stop WifiScan.", __func__);
251         scanHandler_->RemoveEvent(EVENT_START_SCAN);
252         return;
253     }
254     int ret = wifiScanPtr_->Scan();
255     LBSLOGE(LOCATOR, "%{public}s ret=%{public}d", __func__, ret);
256     if (ret != Wifi::WIFI_OPT_SUCCESS) {
257         LBSLOGE(LOCATOR, "%{public}s WifiScan failed, ret=%{public}d", __func__, ret);
258         return;
259     }
260     LBSLOGE(LOCATOR, "StartWifiScan timeInterval_=%{public}d", timeInterval_);
261     if (scanHandler_ != nullptr) {
262         scanHandler_->SendHighPriorityEvent(EVENT_START_SCAN, 0, timeInterval_);
263     }
264 }
265 
IsConnecting()266 bool LocatorRequiredDataManager::IsConnecting()
267 {
268     std::unique_lock<std::mutex> lock(mutex_);
269     if (callbacks_.size() > 0) {
270         return true;
271     }
272     return false;
273 }
274 
ScanHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)275 ScanHandler::ScanHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner) {}
276 
~ScanHandler()277 ScanHandler::~ScanHandler() {}
278 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)279 void ScanHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
280 {
281     auto dataManager = DelayedSingleton<LocatorRequiredDataManager>::GetInstance();
282     if (dataManager == nullptr) {
283         LBSLOGE(NETWORK, "ProcessEvent: dataManager is nullptr");
284         return;
285     }
286     uint32_t eventId = event->GetInnerEventId();
287     LBSLOGI(LOCATOR, "ScanHandler ProcessEvent event:%{public}d", eventId);
288     switch (eventId) {
289         case EVENT_START_SCAN: {
290             dataManager->StartWifiScan(true);
291             break;
292         }
293         case EVENT_STOP_SCAN: {
294             dataManager->StartWifiScan(false);
295             break;
296         }
297         default:
298             break;
299     }
300 }
301 } // namespace Location
302 } // namespace OHOS
303