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 
16 #ifndef LOCATOR_REQUIRED_DATA_MANAGER_H
17 #define LOCATOR_REQUIRED_DATA_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <singleton.h>
22 #include <string>
23 
24 #ifdef BLUETOOTH_ENABLE
25 #include "bluetooth_ble_central_manager.h"
26 #include "bluetooth_host.h"
27 #endif
28 #include "common_event_subscriber.h"
29 #include "constant_definition.h"
30 #include "event_handler.h"
31 #include "event_runner.h"
32 #include "iremote_stub.h"
33 #include "i_locating_required_data_callback.h"
34 #include "locating_required_data_config.h"
35 #include "system_ability_status_change_stub.h"
36 #include "app_identity.h"
37 #ifdef WIFI_ENABLE
38 #include "wifi_scan.h"
39 #include "kits/c/wifi_device.h"
40 #endif
41 
42 namespace OHOS {
43 namespace Location {
44 #ifdef WIFI_ENABLE
45 class LocatorWifiScanEventCallback {
46 public:
47     static void OnWifiScanStateChanged(int state, int size);
48 };
49 #endif
50 
51 #ifdef BLUETOOTH_ENABLE
52 class LocatorBleCallbackWapper : public Bluetooth::BleCentralManagerCallback {
53 public:
54     /**
55      * @brief Scan result callback.
56      *
57      * @param result Scan result.
58      * @since 6
59      */
60     void OnScanCallback(const Bluetooth::BleScanResult &result) override;
61 
62     /**
63      * @brief Scan result for found or lost callback type.
64      *
65      * @param result Scan result.
66      * @param callbackType callback Type.
67      * @since 12
68      */
69     void OnFoundOrLostCallback(const Bluetooth::BleScanResult &result, uint8_t callbackType) override;
70 
71     /**
72      * @brief Batch scan results event callback.
73      *
74      * @param results Scan results.
75      * @since 6
76      */
77     void OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> &results) override;
78     /**
79      * @brief Start or Stop scan event callback.
80      *
81      * @param resultCode Scan result code.
82      * @param isStartScan true->start scan, false->stop scan.
83      * @since 6
84      */
85     void OnStartOrStopScanEvent(int32_t resultCode, bool isStartScan) override;
86     /**
87      * @brief Notify low power device msg callback.
88      *
89      * @param btUuid uuid.
90      * @param msgType notify msgType.
91      * @param value notify msg value.
92      * @since 6
93      */
94     void OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID &btUuid,
95         int msgType, const std::vector<uint8_t> &value) override;
96 
97     std::vector<std::shared_ptr<LocatingRequiredData>> GetLocatingRequiredDataByBle(
98         const Bluetooth::BleScanResult &result);
99 };
100 
101 class LocatorBluetoothHost : public Bluetooth::BluetoothHostObserver {
102 public:
103     /**
104      * @brief Adapter state change function.
105      *
106      * @param transport Transport type when state change.
107      *        BTTransport::ADAPTER_BREDR : classic;
108      *        BTTransport::ADAPTER_BLE : ble.
109      * @param state Change to the new state.
110      *        BTStateID::STATE_TURNING_ON;
111      *        BTStateID::STATE_TURN_ON;
112      *        BTStateID::STATE_TURNING_OFF;
113      *        BTStateID::STATE_TURN_OFF.
114      * @since 6
115      */
116     void OnStateChanged(const int transport, const int status) override;
117     /**
118      * @brief Discovery state changed observer.
119      *
120      * @param status Device discovery status.
121      * @since 6
122      */
123     void OnDiscoveryStateChanged(int status) override;
124     /**
125      * @brief Discovery result observer.
126      *
127      * @param device Remote device.
128      * @param rssi Rssi of remote device.
129      * @param deviceName Name of remote device.
130      * @param deviceClass Class of remote device.
131      * @since 6
132      */
133     void OnDiscoveryResult(const Bluetooth::BluetoothRemoteDevice &device, int rssi,
134         const std::string deviceName, int deviceClass) override;
135     /**
136      * @brief Pair request observer.
137      *
138      * @param device Remote device.
139      * @since 6
140      */
141     void OnPairRequested(const Bluetooth::BluetoothRemoteDevice &device) override;
142     /**
143      * @brief Pair confirmed observer.
144      *
145      * @param device Remote device.
146      * @param reqType Pair type.
147      * @param number Paired passkey.
148      * @since 6
149      */
150     void OnPairConfirmed(const Bluetooth::BluetoothRemoteDevice &device, int reqType, int number) override;
151     /**
152      * @brief Scan mode changed observer.
153      *
154      * @param mode Device scan mode.
155      * @since 6
156      */
157     void OnScanModeChanged(int mode) override;
158     /**
159      * @brief Device name changed observer.
160      *
161      * @param deviceName Device name.
162      * @since 6
163      */
164     void OnDeviceNameChanged(const std::string &deviceName) override;
165     /**
166      * @brief Device address changed observer.
167      *
168      * @param address Device address.
169      * @since 6
170      */
171     void OnDeviceAddrChanged(const std::string &address) override;
172 
173     std::vector<std::shared_ptr<LocatingRequiredData>> GetLocatingRequiredDataByBtHost(
174         const Bluetooth::BluetoothRemoteDevice &device);
175 };
176 #endif
177 
178 class ScanHandler : public AppExecFwk::EventHandler {
179 public:
180     explicit ScanHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
181     ~ScanHandler() override;
182     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
183 };
184 
185 class WifiSdkHandler : public AppExecFwk::EventHandler {
186 public:
187     explicit WifiSdkHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
188     ~WifiSdkHandler() override;
189     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
190 };
191 
192 class LocatorRequiredDataManager {
193 public:
194     LocatorRequiredDataManager();
195     ~LocatorRequiredDataManager();
196     __attribute__((no_sanitize("cfi"))) LocationErrCode RegisterCallback(AppIdentity &identity,
197         std::shared_ptr<LocatingRequiredDataConfig>& config, const sptr<IRemoteObject>& callback);
198     LocationErrCode UnregisterCallback(const sptr<IRemoteObject>& callback);
199     void ReportData(const std::vector<std::shared_ptr<LocatingRequiredData>>& result);
200     __attribute__((no_sanitize("cfi"))) void StartWifiScan(int fixNumber, bool flag);
201     bool IsConnecting();
202     static LocatorRequiredDataManager* GetInstance();
203     void SyncStillMovementState(bool state);
204     void SendWifiScanEvent();
205     void SendGetWifiListEvent(int timeout);
206     void UpdateWifiScanCompleteTimestamp();
207     int64_t GetWifiScanCompleteTimestamp();
208     int64_t GetlastStillTime();
209     bool IsStill();
210     LocationErrCode GetCurrentWifiBssidForLocating(std::string& bssid);
211 
212 private:
213     int timeInterval_ = 0;
214 #ifdef WIFI_ENABLE
215 public:
216     void ResetCallbackRegisteredStatus();
217     __attribute__((no_sanitize("cfi"))) bool RegisterWifiCallBack();
218     __attribute__((no_sanitize("cfi"))) bool UnregisterWifiCallBack();
219     std::vector<std::shared_ptr<LocatingRequiredData>> GetLocatingRequiredDataByWifi(
220         const std::vector<Wifi::WifiScanInfo>& wifiScanInfo);
221     __attribute__((no_sanitize("cfi"))) void GetWifiScanList(std::vector<Wifi::WifiScanInfo>& wifiScanInfo);
222 private:
223     void WifiInfoInit();
224     bool IsWifiCallbackRegistered();
225     void SetIsWifiCallbackRegistered(bool isWifiCallbackRegistered);
226     std::shared_ptr<Wifi::WifiScan> wifiScanPtr_;
227     bool isWifiCallbackRegistered_ = false;
228     std::mutex wifiRegisteredMutex_;
229     WifiEvent wifiScanEventCallback_ = {0};
230 #endif
231     std::mutex mutex_;
232     std::map<sptr<IRemoteObject>, AppIdentity> callbacksMap_;
233     std::shared_ptr<ScanHandler> scanHandler_;
234     std::shared_ptr<WifiSdkHandler> wifiSdkHandler_;
235     std::mutex wifiScanCompleteTimestampMutex_;
236     int64_t wifiScanCompleteTimestamp_ = 0;
237     std::mutex lastStillTimeMutex_;
238     int64_t lastStillTime_ = 0;
239 };
240 } // namespace Location
241 } // namespace OHOS
242 #endif // LOCATOR_REQUIRED_DATA_MANAGER_H
243