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