1 /*
2 * Copyright (c) 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 #ifndef LOG_TAG
16 #define LOG_TAG "BluetoothHfpWrapInterface"
17 #endif
18
19 #include "bluetooth_hfp_interface.h"
20 #include <mutex>
21 #include "audio_log.h"
22 #include "audio_errors.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
26 using namespace AudioStandard;
27
28 static HandsFreeAudioGateway *g_hfpInstance = nullptr;
29
30 class BluetoothHfpWrapInterface : public BluetoothHfpInterface {
31 public:
32 BluetoothHfpWrapInterface();
33 ~BluetoothHfpWrapInterface() override;
34
35 int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) override;
36 AudioScoState GetScoState(const BluetoothRemoteDevice &device) override;
37 int32_t GetCurrentCategory(ScoCategory &category) override;
38 int32_t ConnectSco(uint8_t callType) override;
39 int32_t DisconnectSco(uint8_t callType) override;
40 int32_t OpenVoiceRecognition(const BluetoothRemoteDevice &device) override;
41 int32_t CloseVoiceRecognition(const BluetoothRemoteDevice &device) override;
42 int32_t SetActiveDevice(const BluetoothRemoteDevice &device) override;
43
44 void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer) override;
45 void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer) override;
46 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states) override;
47 void GetVirtualDeviceList(std::vector<std::string> &devices) override;
48 BluetoothRemoteDevice GetActiveDevice() override;
49 int32_t Connect(const BluetoothRemoteDevice &device) override;
50 int32_t IsInbandRingingEnabled(bool &isEnabled) override;
51
52 int32_t GetLastError() override;
53 std::string GetLastOpration() override;
54
55 private:
56 int32_t lastErrno_ = 0;
57 std::string lastOpr_;
58 std::mutex oprLock_;
59
60 void SetLastOprInfo(int32_t error, const std::string &func);
61 };
62
GetInstance()63 BluetoothHfpInterface &BluetoothHfpInterface::GetInstance()
64 {
65 static BluetoothHfpWrapInterface interface;
66 return interface;
67 }
68
BluetoothHfpWrapInterface()69 BluetoothHfpWrapInterface::BluetoothHfpWrapInterface()
70 {
71 if (g_hfpInstance == nullptr) {
72 g_hfpInstance = HandsFreeAudioGateway::GetProfile();
73 }
74 }
75
~BluetoothHfpWrapInterface()76 BluetoothHfpWrapInterface::~BluetoothHfpWrapInterface()
77 {
78 g_hfpInstance = nullptr;
79 }
80
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)81 int32_t BluetoothHfpWrapInterface::GetDeviceState(const BluetoothRemoteDevice &device,
82 int32_t &state)
83 {
84 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
85 int32_t error = g_hfpInstance->GetDeviceState(device, state);
86 SetLastOprInfo(error, __func__);
87 return error;
88 }
89
GetScoState(const BluetoothRemoteDevice & device)90 AudioScoState BluetoothHfpWrapInterface::GetScoState(const BluetoothRemoteDevice &device)
91 {
92 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, AudioScoState::DISCONNECTED, "HFP AG profile unavailable");
93 SetLastOprInfo(0, __func__);
94 HfpScoConnectState state = static_cast<HfpScoConnectState>(g_hfpInstance->GetScoState(device));
95 if (state == HfpScoConnectState::SCO_CONNECTED) {
96 return AudioScoState::CONNECTED;
97 }
98 return AudioScoState::DISCONNECTED;
99 }
100
GetCurrentCategory(ScoCategory & category)101 int32_t BluetoothHfpWrapInterface::GetCurrentCategory(ScoCategory &category)
102 {
103 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
104 int callType = 0;
105 int32_t error = g_hfpInstance->GetCurrentCallType(callType);
106 SetLastOprInfo(error, __func__);
107 CHECK_AND_RETURN_RET_LOG(error == SUCCESS, ERROR, "GetCurrentCallType failed ret is %{public}d", error);
108 category = static_cast<ScoCategory>(callType);
109 return SUCCESS;
110 }
111
ConnectSco(uint8_t callType)112 int32_t BluetoothHfpWrapInterface::ConnectSco(uint8_t callType)
113 {
114 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
115 int32_t error = g_hfpInstance->ConnectSco(callType);
116 SetLastOprInfo(error, __func__);
117 return error;
118 }
119
DisconnectSco(uint8_t callType)120 int32_t BluetoothHfpWrapInterface::DisconnectSco(uint8_t callType)
121 {
122 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
123 int32_t error = g_hfpInstance->DisconnectSco(callType);
124 SetLastOprInfo(error, __func__);
125 return error;
126 }
127
OpenVoiceRecognition(const BluetoothRemoteDevice & device)128 int32_t BluetoothHfpWrapInterface::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
129 {
130 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
131 int32_t error = g_hfpInstance->OpenVoiceRecognition(device) ? SUCCESS : ERROR;
132 SetLastOprInfo(error, __func__);
133 return error;
134 }
135
CloseVoiceRecognition(const BluetoothRemoteDevice & device)136 int32_t BluetoothHfpWrapInterface::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
137 {
138 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
139 int32_t error = g_hfpInstance->CloseVoiceRecognition(device) ? SUCCESS : ERROR;
140 SetLastOprInfo(error, __func__);
141 return error;
142 }
143
SetActiveDevice(const BluetoothRemoteDevice & device)144 int32_t BluetoothHfpWrapInterface::SetActiveDevice(const BluetoothRemoteDevice &device)
145 {
146 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
147 int32_t error = g_hfpInstance->SetActiveDevice(device) ? SUCCESS : ERROR;
148 SetLastOprInfo(error, __func__);
149 return error;
150 }
151
RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)152 void BluetoothHfpWrapInterface::RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
153 {
154 CHECK_AND_RETURN_LOG(g_hfpInstance != nullptr, "HFP AG profile unavailable");
155 g_hfpInstance->RegisterObserver(observer);
156 SetLastOprInfo(0, __func__);
157 }
158
DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)159 void BluetoothHfpWrapInterface::DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
160 {
161 CHECK_AND_RETURN_LOG(g_hfpInstance != nullptr, "HFP AG profile unavailable");
162 g_hfpInstance->DeregisterObserver(observer);
163 SetLastOprInfo(0, __func__);
164 }
165
GetDevicesByStates(std::vector<int> states)166 std::vector<BluetoothRemoteDevice> BluetoothHfpWrapInterface::GetDevicesByStates(std::vector<int> states)
167 {
168 std::vector<BluetoothRemoteDevice> devices;
169 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, devices, "HFP AG profile unavailable");
170 SetLastOprInfo(0, __func__);
171 return g_hfpInstance->GetDevicesByStates(states);
172 }
173
GetVirtualDeviceList(std::vector<std::string> & devices)174 void BluetoothHfpWrapInterface::GetVirtualDeviceList(std::vector<std::string> &devices)
175 {
176 CHECK_AND_RETURN_LOG(g_hfpInstance != nullptr, "HFP AG profile unavailable");
177 g_hfpInstance->GetVirtualDeviceList(devices);
178 SetLastOprInfo(0, __func__);
179 }
180
GetActiveDevice()181 BluetoothRemoteDevice BluetoothHfpWrapInterface::GetActiveDevice()
182 {
183 BluetoothRemoteDevice device;
184 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, device, "HFP AG profile unavailable");
185 SetLastOprInfo(0, __func__);
186 return g_hfpInstance->GetActiveDevice();
187 }
188
Connect(const BluetoothRemoteDevice & device)189 int32_t BluetoothHfpWrapInterface::Connect(const BluetoothRemoteDevice &device)
190 {
191 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
192 int32_t error = g_hfpInstance->Connect(device);
193 SetLastOprInfo(error, __func__);
194 return error;
195 }
196
IsInbandRingingEnabled(bool & isEnabled)197 int32_t BluetoothHfpWrapInterface::IsInbandRingingEnabled(bool &isEnabled)
198 {
199 CHECK_AND_RETURN_RET_LOG(g_hfpInstance != nullptr, ERROR, "HFP AG profile unavailable");
200 return g_hfpInstance->IsInbandRingingEnabled(isEnabled);
201 }
202
SetLastOprInfo(int32_t error,const std::string & func)203 void BluetoothHfpWrapInterface::SetLastOprInfo(int32_t error, const std::string &func)
204 {
205 std::unique_lock<std::mutex> lock(oprLock_);
206 lastOpr_ = func;
207 lastErrno_ = error;
208 }
209
GetLastError()210 int32_t BluetoothHfpWrapInterface::GetLastError()
211 {
212 std::unique_lock<std::mutex> lock(oprLock_);
213 return lastErrno_;
214 }
215
GetLastOpration()216 std::string BluetoothHfpWrapInterface::GetLastOpration()
217 {
218 std::unique_lock<std::mutex> lock(oprLock_);
219 return lastOpr_;
220 }
221 } // Bluetooth
222 } // OHOS