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 public std::enable_shared_from_this<BluetoothConnection> { 45 #else 46 class BluetoothConnection { 47 #endif 48 DECLARE_DELAYED_SINGLETON(BluetoothConnection) 49 public: 50 void Init(); 51 int32_t SendBtCallState(int32_t numActive, int32_t numHeld, int32_t callState, const std::string &number); 52 int32_t SendCallDetailsChange(int32_t callId, int32_t callState); 53 void RemoveBtDevice(const std::string &address); 54 bool IsBtAvailble(); 55 void ResetBtConnection(); 56 void RegisterObserver(); 57 58 #ifdef ABILITY_BLUETOOTH_SUPPORT 59 void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, 60 int32_t state, int32_t cause) override; 61 Bluetooth::BluetoothRemoteDevice *GetBtDevice(const std::string &address); 62 void AddBtDevice(const std::string &address, Bluetooth::BluetoothRemoteDevice device); 63 #endif 64 65 private: 66 bool IsAudioActivated(); 67 std::atomic<BtScoState> btScoState_{BtScoState::SCO_STATE_DISCONNECTED}; 68 std::mutex scoAddrMutex_; 69 std::mutex scoNameMutex_; 70 std::string connectedScoAddr_; 71 std::string connectedScoName_; 72 73 #ifdef ABILITY_BLUETOOTH_SUPPORT 74 private: 75 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 76 std::mutex bluetoothMutex_; 77 std::unordered_map<std::string, Bluetooth::BluetoothRemoteDevice> mapConnectedBtDevices_; 78 #endif 79 }; 80 81 #ifdef ABILITY_BLUETOOTH_SUPPORT 82 class SystemAbilityListener : public SystemAbilityStatusChangeStub { 83 public: 84 SystemAbilityListener() = default; 85 ~SystemAbilityListener() = default; 86 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 87 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 88 }; 89 #endif 90 } // namespace Telephony 91 } // namespace OHOS 92 #endif 93