1 /*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "le_audio_software.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <hardware/audio.h>
22 #include <log/log.h>
23
24 #include <cerrno>
25
26 #include "aidl/android/hardware/bluetooth/audio/IBluetoothAudioProvider.h"
27 #include "aidl/audio_ctrl_ack.h"
28 #include "aidl/le_audio_software_aidl.h"
29 #include "audio_hal_interface/hal_version_manager.h"
30 #include "gmock/gmock.h"
31 #include "hidl/le_audio_software_hidl.h"
32
33 using testing::Return;
34 using testing::Test;
35
36 using bluetooth::audio::le_audio::LeAudioClientInterface;
37 using bluetooth::audio::le_audio::StreamCallbacks;
38
39 extern "C" {
android_get_exported_namespace(const char *)40 struct android_namespace_t* android_get_exported_namespace(const char*) {
41 return nullptr;
42 }
android_load_sphal_library(const char *,int)43 void* android_load_sphal_library(const char* /*name*/, int /*flag*/) {
44 return nullptr;
45 }
46 }
47
48 // MOCKS
49 namespace {
50 class MockHalVersionManager {
51 public:
52 MockHalVersionManager() = default;
53 MOCK_METHOD((bluetooth::audio::BluetoothAudioHalVersion), GetHalVersion, ());
54 MOCK_METHOD((bluetooth::audio::BluetoothAudioHalTransport), GetHalTransport,
55 ());
56 MOCK_METHOD(
57 (android::sp<bluetooth::audio::IBluetoothAudioProvidersFactory_2_1>),
58 GetProvidersFactory_2_1, ());
59 MOCK_METHOD(
60 (android::sp<bluetooth::audio::IBluetoothAudioProvidersFactory_2_0>),
61 GetProvidersFactory_2_0, ());
62
SetInstance(MockHalVersionManager * ptr)63 static void SetInstance(MockHalVersionManager* ptr) {
64 MockHalVersionManager::instance_ptr = ptr;
65 }
66
GetInstance()67 static MockHalVersionManager* GetInstance() { return instance_ptr; }
68
69 private:
70 static MockHalVersionManager* instance_ptr;
71 };
72 MockHalVersionManager* MockHalVersionManager::instance_ptr = nullptr;
73
74 class MockBluetoothAudioClientInterfaceBidirEndpoint {
75 public:
76 MOCK_METHOD((size_t), WriteAudioData,
77 (const uint8_t* /*p_buf*/, uint32_t /*len*/), ());
78 MOCK_METHOD((size_t), ReadAudioData, (uint8_t* /*p_buf*/, uint32_t /*len*/),
79 ());
80 };
81
82 class MockBluetoothAudioClientInterfaceHidl {
83 public:
84 MockBluetoothAudioClientInterfaceBidirEndpoint endpoint;
85
86 MOCK_METHOD((bool), IsValid, (), (const));
87 MOCK_METHOD((void), FlushAudioData, ());
88 MOCK_METHOD((bool), UpdateAudioConfig_2_1,
89 (const bluetooth::audio::hidl::
90 AudioConfiguration_2_1& /*audio_config_2_1*/));
91 MOCK_METHOD((int), StartSession_2_1, ());
92 MOCK_METHOD((void), StreamStarted,
93 (const bluetooth::audio::hidl::BluetoothAudioCtrlAck& /*ack*/));
94 MOCK_METHOD((int), EndSession, ());
95 MOCK_METHOD((void), StreamSuspended,
96 (const bluetooth::audio::hidl::BluetoothAudioCtrlAck& /*ack*/));
97
SetInstance(MockBluetoothAudioClientInterfaceHidl * ptr)98 static void SetInstance(MockBluetoothAudioClientInterfaceHidl* ptr) {
99 instance_ptr = ptr;
100 }
101
GetInstance()102 static MockBluetoothAudioClientInterfaceHidl* GetInstance() {
103 return instance_ptr;
104 }
105
106 private:
107 static MockBluetoothAudioClientInterfaceHidl* instance_ptr;
108 };
109 MockBluetoothAudioClientInterfaceHidl*
110 MockBluetoothAudioClientInterfaceHidl::instance_ptr = nullptr;
111
112 class MockBluetoothAudioClientInterfaceAidl {
113 public:
114 MockBluetoothAudioClientInterfaceBidirEndpoint endpoint;
115
116 MOCK_METHOD((bool), IsValid, (), (const));
117 MOCK_METHOD(
118 (bool), SetAllowedLatencyModes,
119 (std::vector<bluetooth::audio::aidl::LatencyMode> /*latency_modes*/));
120 MOCK_METHOD((void), FlushAudioData, ());
121 MOCK_METHOD(
122 (bool), UpdateAudioConfig,
123 (const bluetooth::audio::aidl::AudioConfiguration& /*audio_config*/));
124 MOCK_METHOD((int), StartSession, ());
125 MOCK_METHOD((void), StreamStarted,
126 (const bluetooth::audio::aidl::BluetoothAudioCtrlAck& /*ack*/));
127 MOCK_METHOD((int), EndSession, ());
128 MOCK_METHOD((void), StreamSuspended,
129 (const bluetooth::audio::aidl::BluetoothAudioCtrlAck& /*ack*/));
130 MOCK_METHOD((std::vector<bluetooth::audio::aidl::AudioCapabilities>),
131 GetAudioCapabilities,
132 (bluetooth::audio::aidl::SessionType /*session_type*/));
133 MOCK_METHOD(
134 (std::vector<
135 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
136 LeAudioAseConfigurationSetting>),
137 GetLeAudioAseConfiguration,
138 ((std::optional<std::vector<std::optional<
139 ::aidl::android::hardware::bluetooth::audio::
140 IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&),
141 (std::optional<std::vector<std::optional<
142 ::aidl::android::hardware::bluetooth::audio::
143 IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&),
144 (std::vector<
145 ::aidl::android::hardware::bluetooth::audio::
146 IBluetoothAudioProvider::LeAudioConfigurationRequirement>&)));
147 MOCK_METHOD(
148 (::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
149 LeAudioBroadcastConfigurationSetting),
150 getLeAudioBroadcastConfiguration,
151 ((const std::optional<std::vector<std::optional<
152 ::aidl::android::hardware::bluetooth::audio::
153 IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&),
154 (const ::aidl::android::hardware::bluetooth::audio::
155 IBluetoothAudioProvider::
156 LeAudioBroadcastConfigurationRequirement&)));
157
SetInstance(MockBluetoothAudioClientInterfaceAidl * ptr)158 static void SetInstance(MockBluetoothAudioClientInterfaceAidl* ptr) {
159 instance_ptr = ptr;
160 }
161
GetInstance()162 static MockBluetoothAudioClientInterfaceAidl* GetInstance() {
163 return instance_ptr;
164 }
165
166 private:
167 static MockBluetoothAudioClientInterfaceAidl* instance_ptr;
168 };
169 MockBluetoothAudioClientInterfaceAidl*
170 MockBluetoothAudioClientInterfaceAidl::instance_ptr = nullptr;
171
172 class MockStreamCallbacks {
173 public:
174 MOCK_METHOD((bool), OnResume, (bool));
175 MOCK_METHOD((bool), OnSuspend, ());
176 MOCK_METHOD((bool), OnSourceMetadataUpdate,
177 ((const source_metadata_v7_t&), ::bluetooth::le_audio::DsaMode));
178 MOCK_METHOD((bool), OnSinkMetadataUpdate, (const sink_metadata_v7_t&));
179 };
180 } // namespace
181
182 namespace bluetooth::audio {
183 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_UNAVAILABLE =
184 BluetoothAudioHalVersion();
185 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_2_1 =
186 BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 1);
187
GetHalTransport()188 BluetoothAudioHalTransport HalVersionManager::GetHalTransport() {
189 auto instance = MockHalVersionManager::GetInstance();
190 if (instance) {
191 return instance->GetHalTransport();
192 }
193 return BluetoothAudioHalTransport::UNKNOWN;
194 }
195
GetHalVersion()196 BluetoothAudioHalVersion HalVersionManager::GetHalVersion() {
197 auto instance = MockHalVersionManager::GetInstance();
198 if (instance) {
199 return instance->GetHalVersion();
200 }
201 return BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
202 }
203
204 namespace hidl {
205 class BluetoothAudioDeathRecipient
206 : public ::android::hardware::hidl_death_recipient {
207 public:
BluetoothAudioDeathRecipient(BluetoothAudioClientInterface * clientif,bluetooth::common::MessageLoopThread * message_loop)208 BluetoothAudioDeathRecipient(
209 BluetoothAudioClientInterface* clientif,
210 bluetooth::common::MessageLoopThread* message_loop)
211 : bluetooth_audio_clientif_(clientif), message_loop_(message_loop) {}
212
213 MOCK_METHOD(
214 (void), serviceDied,
215 (uint64_t /*cookie*/,
216 const ::android::wp<::android::hidl::base::V1_0::IBase>& /*who*/),
217 (override));
218
219 private:
220 BluetoothAudioClientInterface* bluetooth_audio_clientif_;
221 bluetooth::common::MessageLoopThread* message_loop_;
222 };
223
BluetoothAudioClientInterface(android::sp<BluetoothAudioDeathRecipient> death_recipient,IBluetoothTransportInstance * instance)224 BluetoothAudioClientInterface::BluetoothAudioClientInterface(
225 android::sp<BluetoothAudioDeathRecipient> death_recipient,
226 IBluetoothTransportInstance* instance)
227 : provider_(nullptr),
228 provider_2_1_(nullptr),
229 session_started_(false),
230 mDataMQ(nullptr),
231 transport_(instance) {
232 death_recipient_ = death_recipient;
233 }
234
BluetoothAudioSinkClientInterface(IBluetoothSinkTransportInstance * sink,bluetooth::common::MessageLoopThread * message_loop)235 BluetoothAudioSinkClientInterface::BluetoothAudioSinkClientInterface(
236 IBluetoothSinkTransportInstance* sink,
237 bluetooth::common::MessageLoopThread* message_loop)
238 : BluetoothAudioClientInterface{new BluetoothAudioDeathRecipient(
239 this, message_loop),
240 sink},
241 sink_(sink) {}
~BluetoothAudioSinkClientInterface()242 BluetoothAudioSinkClientInterface::~BluetoothAudioSinkClientInterface() {}
243
ReadAudioData(uint8_t * p_buf,uint32_t len)244 size_t BluetoothAudioSinkClientInterface::ReadAudioData(uint8_t* p_buf,
245 uint32_t len) {
246 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
247 if (instance) {
248 return instance->endpoint.ReadAudioData(p_buf, len);
249 }
250 return 0;
251 }
252
BluetoothAudioSourceClientInterface(IBluetoothSourceTransportInstance * source,bluetooth::common::MessageLoopThread * message_loop)253 BluetoothAudioSourceClientInterface::BluetoothAudioSourceClientInterface(
254 IBluetoothSourceTransportInstance* source,
255 bluetooth::common::MessageLoopThread* message_loop)
256 : BluetoothAudioClientInterface{new BluetoothAudioDeathRecipient(
257 this, message_loop),
258 source},
259 source_(source) {}
~BluetoothAudioSourceClientInterface()260 BluetoothAudioSourceClientInterface::~BluetoothAudioSourceClientInterface() {}
261
IsValid() const262 bool BluetoothAudioClientInterface::IsValid() const {
263 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
264 if (instance) {
265 return instance->IsValid();
266 }
267 return false;
268 }
269
WriteAudioData(const uint8_t * p_buf,uint32_t len)270 size_t BluetoothAudioSourceClientInterface::WriteAudioData(const uint8_t* p_buf,
271 uint32_t len) {
272 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
273 if (instance) {
274 return instance->endpoint.WriteAudioData(p_buf, len);
275 }
276 return 0;
277 }
278
FlushAudioData()279 void BluetoothAudioClientInterface::FlushAudioData() {
280 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
281 if (instance) {
282 instance->FlushAudioData();
283 }
284 }
285
UpdateAudioConfig_2_1(const AudioConfiguration_2_1 & cfg)286 bool BluetoothAudioClientInterface::UpdateAudioConfig_2_1(
287 const AudioConfiguration_2_1& cfg) {
288 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
289 if (instance) {
290 return instance->UpdateAudioConfig_2_1(cfg);
291 }
292 return false;
293 }
294
StartSession_2_1()295 int BluetoothAudioClientInterface::StartSession_2_1() {
296 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
297 if (instance) {
298 return instance->StartSession_2_1();
299 }
300 return -EINVAL;
301 }
302
StreamStarted(const BluetoothAudioCtrlAck & ack)303 void BluetoothAudioClientInterface::StreamStarted(
304 const BluetoothAudioCtrlAck& ack) {
305 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
306 if (instance) {
307 instance->StreamStarted(ack);
308 }
309 }
310
EndSession()311 int BluetoothAudioClientInterface::EndSession() {
312 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
313 if (instance) {
314 return instance->EndSession();
315 }
316 return -EINVAL;
317 }
318
StreamSuspended(const BluetoothAudioCtrlAck & ack)319 void BluetoothAudioClientInterface::StreamSuspended(
320 const BluetoothAudioCtrlAck& ack) {
321 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
322 if (instance) {
323 instance->StreamSuspended(ack);
324 }
325 }
326
operator <<(std::ostream & os,const BluetoothAudioCtrlAck & ack)327 std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack) {
328 switch (ack) {
329 case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
330 os << "SUCCESS_FINISHED";
331 break;
332 case BluetoothAudioCtrlAck::PENDING:
333 os << "PENDING";
334 break;
335 case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
336 os << "FAILURE_UNSUPPORTED";
337 break;
338 case BluetoothAudioCtrlAck::FAILURE_BUSY:
339 os << "FAILURE_BUSY";
340 break;
341 case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
342 os << "FAILURE_DISCONNECTING";
343 break;
344 case BluetoothAudioCtrlAck::FAILURE:
345 os << "FAILURE";
346 break;
347 default:
348 os << "UNKNOWN";
349 break;
350 };
351 return os;
352 }
353 } // namespace hidl
354
355 namespace aidl {
BluetoothAudioClientInterface(IBluetoothTransportInstance * instance)356 BluetoothAudioClientInterface::BluetoothAudioClientInterface(
357 IBluetoothTransportInstance* instance)
358 : provider_(nullptr),
359 provider_factory_(nullptr),
360 session_started_(false),
361 data_mq_(nullptr),
362 transport_(instance),
363 latency_modes_({LatencyMode::FREE}) {}
364
BluetoothAudioSinkClientInterface(IBluetoothSinkTransportInstance * sink)365 BluetoothAudioSinkClientInterface::BluetoothAudioSinkClientInterface(
366 IBluetoothSinkTransportInstance* sink)
367 : BluetoothAudioClientInterface{sink}, sink_(sink) {}
~BluetoothAudioSinkClientInterface()368 BluetoothAudioSinkClientInterface::~BluetoothAudioSinkClientInterface() {}
369
ReadAudioData(uint8_t * p_buf,uint32_t len)370 size_t BluetoothAudioSinkClientInterface::ReadAudioData(uint8_t* p_buf,
371 uint32_t len) {
372 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
373 if (instance) {
374 return instance->endpoint.ReadAudioData(p_buf, len);
375 }
376 return 0;
377 }
378
BluetoothAudioSourceClientInterface(IBluetoothSourceTransportInstance * source)379 BluetoothAudioSourceClientInterface::BluetoothAudioSourceClientInterface(
380 IBluetoothSourceTransportInstance* source)
381 : BluetoothAudioClientInterface{source}, source_(source) {}
~BluetoothAudioSourceClientInterface()382 BluetoothAudioSourceClientInterface::~BluetoothAudioSourceClientInterface() {}
383
WriteAudioData(const uint8_t * p_buf,uint32_t len)384 size_t BluetoothAudioSourceClientInterface::WriteAudioData(const uint8_t* p_buf,
385 uint32_t len) {
386 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
387 if (instance) {
388 return instance->endpoint.WriteAudioData(p_buf, len);
389 }
390 return 0;
391 }
392
IsValid() const393 bool BluetoothAudioClientInterface::IsValid() const {
394 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
395 if (instance) {
396 return instance->IsValid();
397 }
398 return false;
399 }
400
SetAllowedLatencyModes(std::vector<LatencyMode> latency_modes)401 bool BluetoothAudioClientInterface::SetAllowedLatencyModes(
402 std::vector<LatencyMode> latency_modes) {
403 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
404 if (instance) {
405 return instance->SetAllowedLatencyModes(latency_modes);
406 }
407 return false;
408 }
409
FlushAudioData()410 void BluetoothAudioClientInterface::FlushAudioData() {
411 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
412 if (instance) {
413 instance->FlushAudioData();
414 }
415 }
416
UpdateAudioConfig(const AudioConfiguration & audio_config)417 bool BluetoothAudioClientInterface::UpdateAudioConfig(
418 const AudioConfiguration& audio_config) {
419 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
420 if (instance) {
421 return instance->UpdateAudioConfig(audio_config);
422 }
423 return false;
424 }
425
StartSession()426 int BluetoothAudioClientInterface::StartSession() {
427 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
428 if (instance) {
429 return instance->StartSession();
430 }
431 return -EINVAL;
432 }
433
StreamStarted(const BluetoothAudioCtrlAck & ack)434 void BluetoothAudioClientInterface::StreamStarted(
435 const BluetoothAudioCtrlAck& ack) {
436 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
437 if (instance) {
438 instance->StreamStarted(ack);
439 }
440 }
441
EndSession()442 int BluetoothAudioClientInterface::EndSession() {
443 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
444 if (instance) {
445 return instance->EndSession();
446 }
447 return -EINVAL;
448 }
449
StreamSuspended(const BluetoothAudioCtrlAck & ack)450 void BluetoothAudioClientInterface::StreamSuspended(
451 const BluetoothAudioCtrlAck& ack) {
452 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
453 if (instance) {
454 return instance->StreamSuspended(ack);
455 }
456 }
457
458 std::vector<AudioCapabilities>
GetAudioCapabilities(SessionType session_type)459 BluetoothAudioClientInterface::GetAudioCapabilities(SessionType session_type) {
460 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
461 if (instance) {
462 return instance->GetAudioCapabilities(session_type);
463 }
464 return std::vector<AudioCapabilities>(0);
465 }
466
467 std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>
GetLeAudioAseConfiguration(std::optional<std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & remoteSinkAudioCapabilities,std::optional<std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & remoteSourceAudioCapabilities,std::vector<IBluetoothAudioProvider::LeAudioConfigurationRequirement> & requirements)468 BluetoothAudioClientInterface::GetLeAudioAseConfiguration(
469 std::optional<std::vector<
470 std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
471 remoteSinkAudioCapabilities,
472 std::optional<std::vector<
473 std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
474 remoteSourceAudioCapabilities,
475 std::vector<IBluetoothAudioProvider::LeAudioConfigurationRequirement>&
476 requirements) {
477 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
478 if (instance) {
479 return instance->GetLeAudioAseConfiguration(remoteSinkAudioCapabilities,
480 remoteSourceAudioCapabilities,
481 requirements);
482 }
483
484 return std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>();
485 }
486
487 IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting
getLeAudioBroadcastConfiguration(const std::optional<std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & remoteSinkAudioCapabilities,const IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement & requirement)488 BluetoothAudioClientInterface::getLeAudioBroadcastConfiguration(
489 const std::optional<std::vector<
490 std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
491 remoteSinkAudioCapabilities,
492 const IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement&
493 requirement) {
494 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
495 if (instance) {
496 return instance->getLeAudioBroadcastConfiguration(
497 remoteSinkAudioCapabilities, requirement);
498 }
499
500 return IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting();
501 }
502
operator <<(std::ostream & os,const BluetoothAudioCtrlAck & ack)503 std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack) {
504 switch (ack) {
505 case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
506 os << "SUCCESS_FINISHED";
507 break;
508 case BluetoothAudioCtrlAck::SUCCESS_RECONFIGURATION:
509 os << "SUCCESS_RECONFIGURATION";
510 break;
511 case BluetoothAudioCtrlAck::PENDING:
512 os << "PENDING";
513 break;
514 case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
515 os << "FAILURE_UNSUPPORTED";
516 break;
517 case BluetoothAudioCtrlAck::FAILURE_BUSY:
518 os << "FAILURE_BUSY";
519 break;
520 case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
521 os << "FAILURE_DISCONNECTING";
522 break;
523 case BluetoothAudioCtrlAck::FAILURE:
524 os << "FAILURE";
525 break;
526 default:
527 os << "UNKNOWN";
528 break;
529 };
530 return os;
531 }
532 } // namespace aidl
533 } // namespace bluetooth::audio
534
535 namespace bluetooth::le_audio::broadcaster {
operator <<(std::ostream & os,const BroadcastConfiguration &)536 std::ostream& operator<<(std::ostream& os, const BroadcastConfiguration&) {
537 return os;
538 }
539 } // namespace bluetooth::le_audio::broadcaster
540
541 namespace server_configurable_flags {
GetServerConfigurableFlag(const std::string &,const std::string &,const std::string &)542 std::string GetServerConfigurableFlag(
543 const std::string& /* experiment_category_name */,
544 const std::string& /* experiment_flag_name */,
545 const std::string& /* default_value */) {
546 return "";
547 }
548 } // namespace server_configurable_flags
549
550 namespace {
551
552 bluetooth::common::MessageLoopThread message_loop_thread("test message loop");
553 static base::MessageLoop* message_loop_;
554
init_message_loop_thread()555 static void init_message_loop_thread() {
556 message_loop_thread.StartUp();
557 if (!message_loop_thread.IsRunning()) {
558 FAIL() << "unable to create message loop thread.";
559 }
560
561 if (!message_loop_thread.EnableRealTimeScheduling()) {
562 bluetooth::log::warn("Unable to set real time scheduling");
563 }
564
565 message_loop_ = message_loop_thread.message_loop();
566 if (message_loop_ == nullptr) FAIL() << "unable to get message loop.";
567 }
568
cleanup_message_loop_thread()569 static void cleanup_message_loop_thread() {
570 message_loop_ = nullptr;
571 message_loop_thread.ShutDown();
572 }
573
574 class LeAudioSoftwareUnicastTest : public Test {
575 protected:
SetUp()576 virtual void SetUp() override {
577 init_message_loop_thread();
578 MockHalVersionManager::SetInstance(&hal_version_manager_);
579
580 unicast_sink_stream_cb_.reset(new StreamCallbacks{
581 std::bind(&MockStreamCallbacks::OnResume, &sink_stream_callbacks_,
582 std::placeholders::_1),
583 std::bind(&MockStreamCallbacks::OnSuspend, &sink_stream_callbacks_),
584 std::bind(&MockStreamCallbacks::OnSourceMetadataUpdate,
585 &sink_stream_callbacks_, std::placeholders::_1,
586 std::placeholders::_2),
587 std::bind(&MockStreamCallbacks::OnSinkMetadataUpdate,
588 &sink_stream_callbacks_, std::placeholders::_1),
589 });
590
591 unicast_source_stream_cb_.reset(new StreamCallbacks{
592 std::bind(&MockStreamCallbacks::OnResume, &source_stream_callbacks_,
593 std::placeholders::_1),
594 std::bind(&MockStreamCallbacks::OnSuspend, &source_stream_callbacks_),
595 std::bind(&MockStreamCallbacks::OnSourceMetadataUpdate,
596 &source_stream_callbacks_, std::placeholders::_1,
597 std::placeholders::_2),
598 std::bind(&MockStreamCallbacks::OnSinkMetadataUpdate,
599 &source_stream_callbacks_, std::placeholders::_1),
600 });
601
602 sink_ = LeAudioClientInterface::Get()->GetSink(
603 *unicast_sink_stream_cb_, &message_loop_thread, is_broadcast_);
604 source_ = LeAudioClientInterface::Get()->GetSource(
605 *unicast_source_stream_cb_, &message_loop_thread);
606
607 if (is_broadcast_) {
608 ASSERT_TRUE(LeAudioClientInterface::Get()->IsBroadcastSinkAcquired());
609 } else {
610 ASSERT_TRUE(LeAudioClientInterface::Get()->IsSourceAcquired());
611 ASSERT_TRUE(LeAudioClientInterface::Get()->IsUnicastSinkAcquired());
612 }
613 }
614
TearDown()615 virtual void TearDown() override {
616 if (LeAudioClientInterface::Get()->IsUnicastSinkAcquired() ||
617 LeAudioClientInterface::Get()->IsBroadcastSinkAcquired()) {
618 if (is_broadcast_) {
619 ASSERT_FALSE(LeAudioClientInterface::Get()->IsBroadcastSinkAcquired());
620 } else {
621 LeAudioClientInterface::Get()->ReleaseSink(sink_);
622 ASSERT_FALSE(LeAudioClientInterface::Get()->IsUnicastSinkAcquired());
623 }
624 }
625
626 if (LeAudioClientInterface::Get()->IsSourceAcquired()) {
627 LeAudioClientInterface::Get()->ReleaseSource(source_);
628 ASSERT_FALSE(LeAudioClientInterface::Get()->IsSourceAcquired());
629 }
630
631 cleanup_message_loop_thread();
632
633 unicast_sink_stream_cb_.reset();
634 unicast_source_stream_cb_.reset();
635
636 MockBluetoothAudioClientInterfaceHidl::SetInstance(nullptr);
637 MockBluetoothAudioClientInterfaceAidl::SetInstance(nullptr);
638 MockHalVersionManager::SetInstance(nullptr);
639 }
640
641 bool is_broadcast_ = false;
642 LeAudioClientInterface::Sink* sink_ = nullptr;
643 LeAudioClientInterface::Source* source_ = nullptr;
644
645 MockHalVersionManager hal_version_manager_;
646 MockStreamCallbacks sink_stream_callbacks_;
647 MockStreamCallbacks source_stream_callbacks_;
648
649 std::unique_ptr<StreamCallbacks> unicast_sink_stream_cb_;
650 std::unique_ptr<StreamCallbacks> unicast_source_stream_cb_;
651 };
652
653 class LeAudioSoftwareUnicastTestAidl : public LeAudioSoftwareUnicastTest {
654 protected:
SetUp()655 virtual void SetUp() override {
656 ON_CALL(hal_version_manager_, GetHalTransport)
657 .WillByDefault(
658 Return(bluetooth::audio::BluetoothAudioHalTransport::AIDL));
659
660 MockBluetoothAudioClientInterfaceAidl::SetInstance(
661 &audio_client_interface_);
662 ON_CALL(audio_client_interface_, IsValid).WillByDefault(Return(true));
663
664 LeAudioSoftwareUnicastTest::SetUp();
665 }
666
667 MockBluetoothAudioClientInterfaceAidl audio_client_interface_;
668 };
669
TEST_F(LeAudioSoftwareUnicastTestAidl,AcquireAndRelease)670 TEST_F(LeAudioSoftwareUnicastTestAidl, AcquireAndRelease) {
671 ASSERT_NE(nullptr, sink_);
672 ASSERT_NE(nullptr, source_);
673 }
674
675 class LeAudioSoftwareUnicastTestHidl : public LeAudioSoftwareUnicastTest {
676 protected:
SetUp()677 virtual void SetUp() override {
678 ON_CALL(hal_version_manager_, GetHalTransport)
679 .WillByDefault(
680 Return(bluetooth::audio::BluetoothAudioHalTransport::HIDL));
681
682 MockBluetoothAudioClientInterfaceHidl::SetInstance(
683 &audio_client_interface_);
684 ON_CALL(audio_client_interface_, IsValid).WillByDefault(Return(true));
685
686 LeAudioSoftwareUnicastTest::SetUp();
687 }
688
689 MockBluetoothAudioClientInterfaceHidl audio_client_interface_;
690 };
691
TEST_F(LeAudioSoftwareUnicastTestHidl,AcquireAndRelease)692 TEST_F(LeAudioSoftwareUnicastTestHidl, AcquireAndRelease) {
693 ASSERT_NE(nullptr, sink_);
694 ASSERT_NE(nullptr, source_);
695 }
696 } // namespace
697