• 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     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     void OnVirtualDeviceChanged(int32_t action, std::string address) override;
62     Bluetooth::BluetoothRemoteDevice *GetBtDevice(const std::string &address);
63     void AddBtDevice(const std::string &address, Bluetooth::BluetoothRemoteDevice device);
64     std::string GetWearBtHeadsetAddress();
65 #endif
66 
67 private:
68     bool IsAudioActivated();
69     std::atomic<BtScoState> btScoState_{BtScoState::SCO_STATE_DISCONNECTED};
70     std::mutex scoAddrMutex_;
71     std::mutex scoNameMutex_;
72     std::string connectedScoAddr_;
73     std::string connectedScoName_;
74 
75 #ifdef ABILITY_BLUETOOTH_SUPPORT
76 private:
77     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
78     std::mutex bluetoothMutex_;
79     std::unordered_map<std::string, Bluetooth::BluetoothRemoteDevice> mapConnectedBtDevices_;
80 #endif
81 };
82 
83 #ifdef ABILITY_BLUETOOTH_SUPPORT
84 class SystemAbilityListener : public SystemAbilityStatusChangeStub {
85 public:
86     SystemAbilityListener() = default;
87     ~SystemAbilityListener() = default;
88     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
89     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
90 };
91 #endif
92 } // namespace Telephony
93 } // namespace OHOS
94 #endif
95