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