1 /* 2 * Copyright (C) 2024 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 #ifndef NFC_BT_CONN_MGR_H 16 #define NFC_BT_CONN_MGR_H 17 18 #include <shared_mutex> 19 #include "bluetooth_a2dp_src.h" 20 #include "bluetooth_hfp_ag.h" 21 #include "bluetooth_hid_host.h" 22 #include "bluetooth_host.h" 23 #include "bluetooth_remote_device.h" 24 #include "ndef_bt_data_parser.h" 25 #include "nfc_service.h" 26 27 namespace OHOS { 28 namespace NFC { 29 namespace TAG { 30 class BtConnectionInfo { 31 public: 32 std::string macAddr_; 33 int32_t state_; 34 uint8_t type_; 35 }; 36 37 class BtConnectionManager { 38 public: 39 static BtConnectionManager& GetInstance(); 40 void Initialize(std::weak_ptr<NfcService> nfcService); 41 42 void TryPairBt(std::shared_ptr<BtData> data); 43 void OnBtNtfClicked(); 44 void HandleBtEnableFailed(); 45 void HandleBtPairFailed(); 46 void HandleBtConnectFailed(); 47 void OnBtEnabled(); 48 void OnPairStatusChanged(std::shared_ptr<BtConnectionInfo> info); 49 void OnConnectionStateChanged(std::shared_ptr<BtConnectionInfo> info); 50 51 enum BtProfileType { 52 A2DP_SRC, 53 HFP_AG, 54 HID_HOST 55 }; 56 57 // Bt status observers 58 class BtStateObserver : public Bluetooth::BluetoothHostObserver { 59 public: 60 BtStateObserver() = default; 61 virtual ~BtStateObserver() = default; GetInstance()62 static std::shared_ptr<BtStateObserver> &GetInstance() 63 { 64 static std::shared_ptr<BtStateObserver> instance = std::make_shared<BtStateObserver>(); 65 return instance; 66 } 67 void OnStateChanged(const int transport, const int status) override; OnDiscoveryStateChanged(int status)68 void OnDiscoveryStateChanged(int status) override {}; OnDiscoveryResult(const Bluetooth::BluetoothRemoteDevice & device,int rssi,const std::string deviceName,int deviceClass)69 void OnDiscoveryResult(const Bluetooth::BluetoothRemoteDevice &device, 70 int rssi, const std::string deviceName, int deviceClass) override {}; OnPairRequested(const Bluetooth::BluetoothRemoteDevice & device)71 void OnPairRequested(const Bluetooth::BluetoothRemoteDevice &device) override {}; OnPairConfirmed(const Bluetooth::BluetoothRemoteDevice & device,int reqType,int number)72 void OnPairConfirmed(const Bluetooth::BluetoothRemoteDevice &device, int reqType, int number) override {}; OnScanModeChanged(int mode)73 void OnScanModeChanged(int mode) override {}; OnDeviceNameChanged(const std::string & deviceName)74 void OnDeviceNameChanged(const std::string &deviceName) override {}; OnDeviceAddrChanged(const std::string & address)75 void OnDeviceAddrChanged(const std::string &address) override {}; 76 }; 77 78 class BtRemoteDevObserver : public Bluetooth::BluetoothRemoteDeviceObserver { 79 public: 80 BtRemoteDevObserver() = default; 81 virtual ~BtRemoteDevObserver() = default; OnAclStateChanged(const Bluetooth::BluetoothRemoteDevice & device,int state,unsigned int reason)82 void OnAclStateChanged(const Bluetooth::BluetoothRemoteDevice &device, 83 int state, unsigned int reason) override {}; 84 void OnPairStatusChanged(const Bluetooth::BluetoothRemoteDevice &device, int status, int cause) override; OnRemoteUuidChanged(const Bluetooth::BluetoothRemoteDevice & device,const std::vector<Bluetooth::ParcelUuid> & uuids)85 void OnRemoteUuidChanged(const Bluetooth::BluetoothRemoteDevice &device, 86 const std::vector<Bluetooth::ParcelUuid> &uuids) override {}; OnRemoteNameChanged(const Bluetooth::BluetoothRemoteDevice & device,const std::string & deviceName)87 void OnRemoteNameChanged(const Bluetooth::BluetoothRemoteDevice &device, 88 const std::string &deviceName) override {}; OnRemoteAliasChanged(const Bluetooth::BluetoothRemoteDevice & device,const std::string & alias)89 void OnRemoteAliasChanged(const Bluetooth::BluetoothRemoteDevice &device, 90 const std::string &alias) override {}; OnRemoteCodChanged(const Bluetooth::BluetoothRemoteDevice & device,const Bluetooth::BluetoothDeviceClass & cod)91 void OnRemoteCodChanged(const Bluetooth::BluetoothRemoteDevice &device, 92 const Bluetooth::BluetoothDeviceClass &cod) override {}; OnRemoteBatteryLevelChanged(const Bluetooth::BluetoothRemoteDevice & device,int batteryLevel)93 void OnRemoteBatteryLevelChanged(const Bluetooth::BluetoothRemoteDevice &device, int batteryLevel) override {}; OnReadRemoteRssiEvent(const Bluetooth::BluetoothRemoteDevice & device,int rssi,int status)94 void OnReadRemoteRssiEvent(const Bluetooth::BluetoothRemoteDevice &device, int rssi, int status) override {}; 95 }; 96 97 class BtA2dpObserver : public Bluetooth::A2dpSourceObserver { 98 public: 99 BtA2dpObserver() = default; 100 virtual ~BtA2dpObserver() = default; OnPlayingStatusChanged(const Bluetooth::BluetoothRemoteDevice & device,int playingState,int error)101 void OnPlayingStatusChanged(const Bluetooth::BluetoothRemoteDevice &device, 102 int playingState, int error) override {}; OnConfigurationChanged(const Bluetooth::BluetoothRemoteDevice & device,const Bluetooth::A2dpCodecInfo & info,int error)103 void OnConfigurationChanged(const Bluetooth::BluetoothRemoteDevice &device, 104 const Bluetooth::A2dpCodecInfo &info, int error) override {}; 105 void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int state, int cause) override; OnMediaStackChanged(const Bluetooth::BluetoothRemoteDevice & device,int action)106 void OnMediaStackChanged(const Bluetooth::BluetoothRemoteDevice &device, int action) override {}; 107 }; 108 109 class BtHfpObserver : public Bluetooth::HandsFreeAudioGatewayObserver { 110 public: 111 BtHfpObserver() = default; 112 virtual ~BtHfpObserver() = default; 113 void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, 114 int32_t state, int32_t cause) override; OnScoStateChanged(const Bluetooth::BluetoothRemoteDevice & device,int32_t state,int32_t reason)115 void OnScoStateChanged(const Bluetooth::BluetoothRemoteDevice &device, 116 int32_t state, int32_t reason) override {}; OnActiveDeviceChanged(const Bluetooth::BluetoothRemoteDevice & device)117 void OnActiveDeviceChanged(const Bluetooth::BluetoothRemoteDevice &device) override {}; OnHfEnhancedDriverSafetyChanged(const Bluetooth::BluetoothRemoteDevice & device,int32_t indValue)118 void OnHfEnhancedDriverSafetyChanged(const Bluetooth::BluetoothRemoteDevice &device, 119 int32_t indValue) override {}; OnHfpStackChanged(const Bluetooth::BluetoothRemoteDevice & device,int32_t action)120 void OnHfpStackChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t action) override {}; 121 }; 122 123 class BtHidObserver : public Bluetooth::HidHostObserver { 124 public: 125 BtHidObserver() = default; 126 virtual ~BtHidObserver() = default; 127 void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int state, int cause) override; 128 }; 129 130 private: 131 BtConnectionManager(); ~BtConnectionManager()132 ~BtConnectionManager() {} 133 // Bt observers 134 void RegisterBtObserver(); 135 void UnregisterBtObserverAndProfile(); 136 void RegisterProfileObserver(BtProfileType type); 137 // timeout event messages 138 void SendMsgToEvtHandler(NfcCommonEvent evt, int64_t delay); 139 void SendConnMsgToEvtHandler(NfcCommonEvent evt, const Bluetooth::BluetoothRemoteDevice &device, 140 int32_t state, BtProfileType type); 141 void RemoveMsgFromEvtHandler(NfcCommonEvent evt); 142 // Bt state check 143 bool IsBtEnabled(); 144 bool HandleEnableBt(); 145 // status and action jump 146 void NextAction(); 147 void NextActionInit(); 148 void NextActionConnect(); 149 void NextActionDisconnect(); 150 151 bool GetProfileList(); 152 bool IsA2dpSupported(); 153 bool IsHfpSupported(); 154 bool HandleBtInit(); 155 bool DecideInitNextAction(); 156 157 bool HandleBtPair(); 158 bool HandleBtConnect(); 159 bool HandleBtConnectWaiting(); 160 bool HandleBtDisconnect(); 161 bool HandleBtDisconnectWaiting(); 162 163 // clear function 164 void OnFinish(); 165 166 private: 167 std::weak_ptr<NfcService> nfcService_ {}; 168 std::shared_mutex mutex_ {}; 169 bool isInitialized_ = false; 170 171 std::shared_ptr<BtStateObserver> btObserver_ {}; 172 std::shared_ptr<BtRemoteDevObserver> btRemoteDevObserver_ {}; 173 std::shared_ptr<BtA2dpObserver> btA2dpObserver_ {}; 174 std::shared_ptr<BtHfpObserver> btHfpObserver_ {}; 175 std::shared_ptr<BtHidObserver> btHidObserver_ {}; 176 }; 177 } // namespace TAG 178 } // namespace NFC 179 } // namespace OHOS 180 #endif // NFC_BT_CONN_MGR_H