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