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