• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 BLUETOOTH_SCO_MANAGER_H
17 #define BLUETOOTH_SCO_MANAGER_H
18 
19 #include "bluetooth_a2dp_src.h"
20 #include "bluetooth_a2dp_codec.h"
21 #include "bluetooth_avrcp_tg.h"
22 #include "bluetooth_hfp_ag.h"
23 #include "audio_info.h"
24 #include "bluetooth_device_utils.h"
25 #include "audio_timer.h"
26 
27 namespace OHOS {
28 namespace Bluetooth {
29 class BluetoothScoManager {
30 public:
31     static BluetoothScoManager &GetInstance();
32 
33     void UpdateScoState(HfpScoConnectState scoState, const BluetoothRemoteDevice &device, int reason = 0);
34     int32_t HandleScoConnect(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
35     int32_t HandleScoDisconnect(const BluetoothRemoteDevice &device);
36     AudioScoState GetAudioScoState();
37     ScoCategory GetAudioScoCategory();
38     void ResetScoState(const BluetoothRemoteDevice &device);
39     BluetoothRemoteDevice GetAudioScoDevice();
40     bool IsInScoCategory(ScoCategory scoCategory);
41 
42 private:
43     struct ScoCacheRequest {
44         bool connectReq = false;
45         ScoCategory category = ScoCategory::SCO_DEFAULT;
46         BluetoothRemoteDevice device;
47     };
48 
49     BluetoothScoManager();
50     ~BluetoothScoManager() = default;
51 
52     uint32_t scoStateDuration_ = 3; /* 3: seconds */
53     BluetoothRemoteDevice currentScoDevice_;
54     AudioScoState currentScoState_ = AudioScoState::INIT;
55     ScoCategory  currentScoCategory_ = ScoCategory::SCO_DEFAULT;
56     std::shared_ptr<ScoCacheRequest> cacheReq_;
57     std::shared_ptr<AudioStandard::AudioTimer> scoTimer_;
58     std::mutex scoLock_;
59 
60     void UpdateScoStateWhenDisconnected(HfpScoConnectState scoState, const BluetoothRemoteDevice &device, int reason);
61     void UpdateScoStateWhenConnected(HfpScoConnectState scoState, const BluetoothRemoteDevice &device, int reason);
62     void UpdateScoStateWhenConnecting(HfpScoConnectState scoState, const BluetoothRemoteDevice &device, int reason);
63     void UpdateScoStateWhenDisconnecting(HfpScoConnectState scoState, const BluetoothRemoteDevice &device, int reason);
64     void WriteScoStateFaultEvent(HfpScoConnectState scoState, const BluetoothRemoteDevice &device, int reason);
65     int32_t HandleScoConnectNoLock(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
66     int32_t HandleScoDisconnectNoLock(const BluetoothRemoteDevice &device);
67     int32_t ProcConnectReqWhenDisconnected(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
68     int32_t ProcConnectReqWhenConnected(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
69     int32_t ProcConnectReqWhenConnecting(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
70     int32_t ProcConnectReqWhenDiconnecting(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
71     int32_t ProcDisconnectReqWhenConnected(const BluetoothRemoteDevice &device);
72     int32_t ProcDisconnectReqWhenConnecting(const BluetoothRemoteDevice &device);
73     bool IsNeedSwitchScoCategory(ScoCategory scoCategory);
74     int32_t SaveRequestToCache(bool isConnect, ScoCategory scoCategory, const BluetoothRemoteDevice &device,
75         const std::string &reason);
76     bool IsSameHfpDevice(const BluetoothRemoteDevice &device1, const BluetoothRemoteDevice &device2);
77     int32_t ConnectSco(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
78     int32_t TryRestoreHfpDevice(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
79     int32_t DisconnectSco(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
80     int32_t DisconnectScoReliable(ScoCategory scoCategory, const BluetoothRemoteDevice &device);
81     void SetAudioScoState(AudioScoState state);
82     void OnScoStateTimeOut();
83     void ForceUpdateScoCategory();
84     void ProcCacheRequest();
85 };
86 }
87 }
88 #endif