• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool IsBtScoConnected();
52     BtScoState GetBtScoState();
53     void SetBtScoState(BtScoState state);
54     int32_t SendBtCallState(int32_t numActive, int32_t numHeld, int32_t callState, const std::string &number);
55     void RemoveBtDevice(const std::string &address);
56     bool IsBtAvailble();
57     std::string GetConnectedScoAddr();
58     void ResetBtConnection();
59     void RegisterObserver();
60 
61 #ifdef ABILITY_BLUETOOTH_SUPPORT
62     void OnScoStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t state) override;
63     void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t state) override;
64     Bluetooth::BluetoothRemoteDevice *GetBtDevice(const std::string &address);
65     void AddBtDevice(const std::string &address, Bluetooth::BluetoothRemoteDevice device);
66 #endif
67 
68 private:
69     bool IsAudioActivated();
70     std::atomic<BtScoState> btScoState_{BtScoState::SCO_STATE_DISCONNECTED};
71     std::mutex scoAddrMutex_;
72     std::string connectedScoAddr_;
73 
74 #ifdef ABILITY_BLUETOOTH_SUPPORT
75     void SetConnectedScoAddr(std::string connectedScoAddr);
76 
77 private:
78     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
79     std::mutex bluetoothMutex_;
80     std::unordered_map<std::string, Bluetooth::BluetoothRemoteDevice> mapConnectedBtDevices_;
81 #endif
82 };
83 
84 #ifdef ABILITY_BLUETOOTH_SUPPORT
85 class SystemAbilityListener : public SystemAbilityStatusChangeStub {
86 public:
87     SystemAbilityListener() = default;
88     ~SystemAbilityListener() = default;
89     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
90     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
91 };
92 #endif
93 } // namespace Telephony
94 } // namespace OHOS
95 #endif
96