1 /* 2 * Copyright (C) 2021 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 TELEPHONY_BLUETOOTH_CONNECTION_H 17 #define TELEPHONY_BLUETOOTH_CONNECTION_H 18 19 #include <atomic> 20 #include <memory> 21 #include <string> 22 23 #include "singleton.h" 24 #include "unordered_map" 25 26 #ifdef ABILITY_BLUETOOTH_SUPPORT 27 #include "bluetooth_hfp_ag.h" 28 #include "iservice_registry.h" 29 #include "system_ability_definition.h" 30 #include "system_ability_status_change_stub.h" 31 #endif 32 33 namespace OHOS { 34 namespace Telephony { 35 enum BtScoState { 36 SCO_STATE_UNKNOWN = 0, 37 SCO_STATE_CONNECTED, 38 SCO_STATE_DISCONNECTED, 39 SCO_STATE_PENDING, 40 }; 41 42 #ifdef ABILITY_BLUETOOTH_SUPPORT 43 class BluetoothConnection : public OHOS::Bluetooth::HandsFreeAudioGatewayObserver { 44 #else 45 class BluetoothConnection { 46 #endif 47 DECLARE_DELAYED_SINGLETON(BluetoothConnection) 48 public: 49 void Init(); 50 bool ConnectBtSco(); 51 bool ConnectBtSco(const std::string &bluetoothAddress); 52 bool DisconnectBtSco(); 53 bool IsBtScoConnected(); 54 BtScoState GetBtScoState(); 55 void SetBtScoState(BtScoState state); 56 int32_t SendBtCallState(int32_t numActive, int32_t numHeld, int32_t callState, const std::string &number); 57 void RemoveBtDevice(const std::string &address); 58 bool IsBtAvailble(); 59 std::string GetConnectedScoAddr(); 60 void ResetBtConnection(); 61 void RegisterObserver(); 62 63 #ifdef ABILITY_BLUETOOTH_SUPPORT 64 void OnScoStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t state) override; 65 void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t state) override; 66 Bluetooth::BluetoothRemoteDevice *GetBtDevice(const std::string &address); 67 void AddBtDevice(const std::string &address, Bluetooth::BluetoothRemoteDevice device); 68 #endif 69 70 private: 71 bool IsAudioActivated(); 72 std::atomic<BtScoState> btScoState_{BtScoState::SCO_STATE_DISCONNECTED}; 73 std::mutex scoAddrMutex_; 74 std::string connectedScoAddr_; 75 76 #ifdef ABILITY_BLUETOOTH_SUPPORT 77 void SetConnectedScoAddr(std::string connectedScoAddr); 78 bool ConnectBtSco(const Bluetooth::BluetoothRemoteDevice &device); 79 bool DisconnectBtSco(const Bluetooth::BluetoothRemoteDevice &device); 80 81 private: 82 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 83 std::mutex bluetoothMutex_; 84 std::unordered_map<std::string, Bluetooth::BluetoothRemoteDevice> mapConnectedBtDevices_; 85 #endif 86 }; 87 88 #ifdef ABILITY_BLUETOOTH_SUPPORT 89 class SystemAbilityListener : public SystemAbilityStatusChangeStub { 90 public: 91 SystemAbilityListener() = default; 92 ~SystemAbilityListener() = default; 93 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 94 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 95 }; 96 #endif 97 } // namespace Telephony 98 } // namespace OHOS 99 #endif 100