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 #include "ohos_bt_gatt.h" 28 #endif 29 #include "common_event_subscriber.h" 30 #include "constant_definition.h" 31 #include "event_handler.h" 32 #include "event_runner.h" 33 #include "iremote_stub.h" 34 #include "i_locating_required_data_callback.h" 35 #include "ibluetooth_scan_result_callback.h" 36 #include "bluetooth_scan_result.h" 37 #include "locating_required_data_config.h" 38 #include "system_ability_status_change_stub.h" 39 #include "app_identity.h" 40 #ifdef WIFI_ENABLE 41 #include "wifi_scan.h" 42 #include "kits/c/wifi_device.h" 43 #endif 44 45 namespace OHOS { 46 namespace Location { 47 #ifdef WIFI_ENABLE 48 class LocatorWifiScanEventCallback { 49 public: 50 static void OnWifiScanStateChanged(int state, int size); 51 }; 52 #endif 53 54 #ifdef BLUETOOTH_ENABLE 55 class LocatorBleCallbackWapper : public Bluetooth::BleCentralManagerCallback { 56 public: 57 /** 58 * @brief Scan result callback. 59 * 60 * @param result Scan result. 61 * @since 6 62 */ 63 void OnScanCallback(const Bluetooth::BleScanResult &result) override; 64 65 /** 66 * @brief Scan result for found or lost callback type. 67 * 68 * @param result Scan result. 69 * @param callbackType callback Type. 70 * @since 12 71 */ 72 void OnFoundOrLostCallback(const Bluetooth::BleScanResult &result, uint8_t callbackType) override; 73 74 /** 75 * @brief Batch scan results event callback. 76 * 77 * @param results Scan results. 78 * @since 6 79 */ 80 void OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> &results) override; 81 /** 82 * @brief Start or Stop scan event callback. 83 * 84 * @param resultCode Scan result code. 85 * @param isStartScan true->start scan, false->stop scan. 86 * @since 6 87 */ 88 void OnStartOrStopScanEvent(int32_t resultCode, bool isStartScan) override; 89 /** 90 * @brief Notify low power device msg callback. 91 * 92 * @param btUuid uuid. 93 * @param msgType notify msgType. 94 * @param value notify msg value. 95 * @since 6 96 */ 97 void OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID &btUuid, 98 int msgType, const std::vector<uint8_t> &value) override; 99 100 std::vector<std::shared_ptr<LocatingRequiredData>> GetLocatingRequiredDataByBle( 101 const Bluetooth::BleScanResult &result); 102 std::unique_ptr<BluetoothScanResult> GetBluetoothScanResultByBle(const Bluetooth::BleScanResult &result); 103 }; 104 105 class LocatorBluetoothHost : public Bluetooth::BluetoothHostObserver { 106 public: 107 /** 108 * @brief Adapter state change function. 109 * 110 * @param transport Transport type when state change. 111 * BTTransport::ADAPTER_BREDR : classic; 112 * BTTransport::ADAPTER_BLE : ble. 113 * @param state Change to the new state. 114 * BTStateID::STATE_TURNING_ON; 115 * BTStateID::STATE_TURN_ON; 116 * BTStateID::STATE_TURNING_OFF; 117 * BTStateID::STATE_TURN_OFF. 118 * @since 6 119 */ 120 void OnStateChanged(const int transport, const int status) override; 121 /** 122 * @brief Discovery state changed observer. 123 * 124 * @param status Device discovery status. 125 * @since 6 126 */ 127 void OnDiscoveryStateChanged(int status) override; 128 /** 129 * @brief Discovery result observer. 130 * 131 * @param device Remote device. 132 * @param rssi Rssi of remote device. 133 * @param deviceName Name of remote device. 134 * @param deviceClass Class of remote device. 135 * @since 6 136 */ 137 void OnDiscoveryResult(const Bluetooth::BluetoothRemoteDevice &device, int rssi, 138 const std::string deviceName, int deviceClass) override; 139 /** 140 * @brief Pair request observer. 141 * 142 * @param device Remote device. 143 * @since 6 144 */ 145 void OnPairRequested(const Bluetooth::BluetoothRemoteDevice &device) override; 146 /** 147 * @brief Pair confirmed observer. 148 * 149 * @param device Remote device. 150 * @param reqType Pair type. 151 * @param number Paired passkey. 152 * @since 6 153 */ 154 void OnPairConfirmed(const Bluetooth::BluetoothRemoteDevice &device, int reqType, int number) override; 155 /** 156 * @brief Scan mode changed observer. 157 * 158 * @param mode Device scan mode. 159 * @since 6 160 */ 161 void OnScanModeChanged(int mode) override; 162 /** 163 * @brief Device name changed observer. 164 * 165 * @param deviceName Device name. 166 * @since 6 167 */ 168 void OnDeviceNameChanged(const std::string &deviceName) override; 169 /** 170 * @brief Device address changed observer. 171 * 172 * @param address Device address. 173 * @since 6 174 */ 175 void OnDeviceAddrChanged(const std::string &address) override; 176 177 std::vector<std::shared_ptr<LocatingRequiredData>> GetLocatingRequiredDataByBtHost( 178 const Bluetooth::BluetoothRemoteDevice &device); 179 }; 180 #endif 181 182 class ScanHandler : public AppExecFwk::EventHandler { 183 public: 184 using ScanEventHandle = std::function<void(const AppExecFwk::InnerEvent::Pointer &)>; 185 using ScanEventHandleMap = std::map<int, ScanEventHandle>; 186 explicit ScanHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 187 ~ScanHandler() override; 188 private: 189 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 190 void InitScanHandlerEventMap(); 191 void StartBluetoothScanEvent(const AppExecFwk::InnerEvent::Pointer& event); 192 void StopBluetoothScanEvent(const AppExecFwk::InnerEvent::Pointer& event); 193 void StartScanEvent(const AppExecFwk::InnerEvent::Pointer& event); 194 195 ScanEventHandleMap scanHandlerEventMap_; 196 }; 197 198 class WifiSdkHandler : public AppExecFwk::EventHandler { 199 public: 200 using WifiSdkEventHandle = std::function<void(const AppExecFwk::InnerEvent::Pointer &)>; 201 using WifiSdkEventHandleMap = std::map<int, WifiSdkEventHandle>; 202 explicit WifiSdkHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 203 ~WifiSdkHandler() override; 204 205 private: 206 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 207 void InitWifiSdkHandlerEventMap(); 208 void GetWifiListEvent(const AppExecFwk::InnerEvent::Pointer& event); 209 void RegisterWifiCallbackEvent(const AppExecFwk::InnerEvent::Pointer& event); 210 void UnregisterWifiCallbackEvent(const AppExecFwk::InnerEvent::Pointer& event); 211 212 WifiSdkEventHandleMap wifiSdkHandlerEventMap_; 213 }; 214 215 class LocatorRequiredDataManager { 216 public: 217 LocatorRequiredDataManager(); 218 ~LocatorRequiredDataManager(); 219 __attribute__((no_sanitize("cfi"))) LocationErrCode RegisterCallback(AppIdentity &identity, 220 std::shared_ptr<LocatingRequiredDataConfig>& config, const sptr<IRemoteObject>& callback); 221 LocationErrCode UnregisterCallback(const sptr<IRemoteObject>& callback); 222 void ReportData(const std::vector<std::shared_ptr<LocatingRequiredData>>& result); 223 void ReportBluetoothScanResult(const std::unique_ptr<BluetoothScanResult>& bluetoothScanResult); 224 void StartScanBluetoothDevice(sptr<IBluetoothScanResultCallback> callback, AppIdentity identity); 225 void StopScanBluetoothDevice(sptr<IRemoteObject> callbackObj); 226 void StartBluetoothScan(); 227 void StoptBluetoothScan(); 228 void RemoveBluetoothScanCallback(sptr<IRemoteObject> callbackObj); 229 void RemoveBluetoothScanCallbackDeathRecipientByCallback(sptr<IRemoteObject> callbackObj); 230 __attribute__((no_sanitize("cfi"))) void StartWifiScan(int fixNumber, bool flag); 231 bool IsWifiConnecting(); 232 bool IsBluetoothConnecting(); 233 static LocatorRequiredDataManager* GetInstance(); 234 void SyncStillMovementState(bool state); 235 void SendStartBluetoothScanEvent(); 236 void SendStopBluetoothScanEvent(); 237 void SendWifiScanEvent(); 238 void SendGetWifiListEvent(int timeout, bool needRetryScan); 239 void RemoveGetWifiListEvent(); 240 void UpdateWifiScanCompleteTimestamp(); 241 int64_t GetWifiScanCompleteTimestamp(); 242 int64_t GetlastStillTime(); 243 bool IsStill(); 244 LocationErrCode GetCurrentWifiBssidForLocating(std::string& bssid); 245 246 private: 247 int timeInterval_ = 0; 248 #ifdef WIFI_ENABLE 249 public: 250 void ResetCallbackRegisteredStatus(); 251 __attribute__((no_sanitize("cfi"))) bool RegisterWifiCallBack(); 252 __attribute__((no_sanitize("cfi"))) bool UnregisterWifiCallBack(); 253 bool GetLocatingRequiredDataByWifi(std::vector<std::shared_ptr<LocatingRequiredData>>& requiredData, 254 const std::vector<Wifi::WifiScanInfo>& wifiScanInfo); 255 __attribute__((no_sanitize("cfi"))) void GetWifiScanList(std::vector<Wifi::WifiScanInfo>& wifiScanInfo); 256 int TriggerWifiScan(); 257 int64_t wifiScanStartTimeStamp_ = 0; 258 private: 259 void WifiInfoInit(); 260 bool IsWifiCallbackRegistered(); 261 void SetIsWifiCallbackRegistered(bool isWifiCallbackRegistered); 262 std::shared_ptr<Bluetooth::BleCentralManager> bleCentralManager_; 263 bool isWifiCallbackRegistered_ = false; 264 std::mutex wifiRegisteredMutex_; 265 WifiEvent wifiScanEventCallback_ = {0}; 266 #endif 267 std::mutex mutex_; 268 std::mutex bluetoothcallbacksMapMutex_; 269 std::map<sptr<IRemoteObject>, AppIdentity> callbacksMap_; 270 std::map<sptr<IRemoteObject>, 271 std::pair<AppIdentity, sptr<IRemoteObject::DeathRecipient>>> bluetoothcallbacksMap_; 272 std::shared_ptr<ScanHandler> scanHandler_; 273 std::shared_ptr<WifiSdkHandler> wifiSdkHandler_; 274 std::mutex wifiScanCompleteTimestampMutex_; 275 int64_t wifiScanCompleteTimestamp_ = 0; 276 std::mutex lastStillTimeMutex_; 277 int64_t lastStillTime_ = 0; 278 }; 279 280 class BluetoothScanCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 281 public: 282 BluetoothScanCallbackDeathRecipient(); 283 ~BluetoothScanCallbackDeathRecipient() override; 284 285 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 286 }; 287 } // namespace Location 288 } // namespace OHOS 289 #endif // LOCATOR_REQUIRED_DATA_MANAGER_H 290