1 //
2 // Copyright 2017 Google, Inc.
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 "service/hal/fake_bluetooth_av_interface.h"
18
19 namespace bluetooth {
20 namespace hal {
21 namespace {
22
23 // The global test handler instances. We have to have globals since the HAL
24 // interface methods all have to be global and their signatures don't allow us
25 // to pass in user_data.
26 std::shared_ptr<FakeBluetoothAvInterface::TestA2dpSinkHandler>
27 g_a2dp_sink_handler;
28
FakeInit(btav_sink_callbacks_t * callbacks)29 bt_status_t FakeInit(btav_sink_callbacks_t* callbacks) {
30 return BT_STATUS_SUCCESS;
31 }
32
FakeConnect(const RawAddress & bd_addr)33 bt_status_t FakeConnect(const RawAddress& bd_addr) {
34 if (g_a2dp_sink_handler) return g_a2dp_sink_handler->Connect(bd_addr);
35 return BT_STATUS_FAIL;
36 }
37
FakeDisconnect(const RawAddress & bd_addr)38 bt_status_t FakeDisconnect(const RawAddress& bd_addr) {
39 if (g_a2dp_sink_handler) return g_a2dp_sink_handler->Disconnect(bd_addr);
40 return BT_STATUS_FAIL;
41 }
42
FakeCleanup(void)43 void FakeCleanup(void) {}
44
FakeSetAudioFocusState(int focus_state)45 void FakeSetAudioFocusState(int focus_state) {
46 if (g_a2dp_sink_handler)
47 return g_a2dp_sink_handler->SetAudioFocusState(focus_state);
48 }
49
FakeSetAudioTrackGain(float gain)50 void FakeSetAudioTrackGain(float gain) {
51 if (g_a2dp_sink_handler) return g_a2dp_sink_handler->SetAudioTrackGain(gain);
52 }
53
54 btav_source_interface_t fake_a2dp_source_interface = {
55 .size = sizeof(btav_source_interface_t),
56 .init = nullptr,
57 .connect = nullptr,
58 .disconnect = nullptr,
59 .config_codec = nullptr,
60 .cleanup = nullptr,
61 };
62
63 btav_sink_interface_t fake_a2dp_sink_interface = {
64 .size = sizeof(btav_sink_interface_t),
65 .init = FakeInit,
66 .connect = FakeConnect,
67 .disconnect = FakeDisconnect,
68 .cleanup = FakeCleanup,
69 .set_audio_focus_state = FakeSetAudioFocusState,
70 .set_audio_track_gain = FakeSetAudioTrackGain,
71 };
72
73 } // namespace
74
FakeBluetoothAvInterface(std::shared_ptr<TestA2dpSinkHandler> a2dp_sink_handler)75 FakeBluetoothAvInterface::FakeBluetoothAvInterface(
76 std::shared_ptr<TestA2dpSinkHandler> a2dp_sink_handler) {
77 CHECK(!g_a2dp_sink_handler);
78
79 if (a2dp_sink_handler) g_a2dp_sink_handler = a2dp_sink_handler;
80 }
81
~FakeBluetoothAvInterface()82 FakeBluetoothAvInterface::~FakeBluetoothAvInterface() {
83 g_a2dp_sink_handler = nullptr;
84 }
85
NotifyConnectionState(const RawAddress & bda,btav_connection_state_t state)86 void FakeBluetoothAvInterface::NotifyConnectionState(
87 const RawAddress& bda, btav_connection_state_t state) {
88 for (auto& observer : a2dp_sink_observers_) {
89 observer.ConnectionStateCallback(this, bda, state);
90 }
91 }
NotifyAudioState(const RawAddress & bda,btav_audio_state_t state)92 void FakeBluetoothAvInterface::NotifyAudioState(const RawAddress& bda,
93 btav_audio_state_t state) {
94 for (auto& observer : a2dp_sink_observers_) {
95 observer.AudioStateCallback(this, bda, state);
96 }
97 }
NotifyAudioConfig(const RawAddress & bda,uint32_t sample_rate,uint8_t channel_count)98 void FakeBluetoothAvInterface::NotifyAudioConfig(const RawAddress& bda,
99 uint32_t sample_rate,
100 uint8_t channel_count) {
101 for (auto& observer : a2dp_sink_observers_) {
102 observer.AudioConfigCallback(this, bda, sample_rate, channel_count);
103 }
104 }
105
A2dpSourceEnable(std::vector<btav_a2dp_codec_config_t> codec_priorities)106 bool FakeBluetoothAvInterface::A2dpSourceEnable(
107 std::vector<btav_a2dp_codec_config_t> codec_priorities) {
108 return true;
109 }
110
A2dpSourceDisable()111 void FakeBluetoothAvInterface::A2dpSourceDisable() {}
112
A2dpSinkEnable()113 bool FakeBluetoothAvInterface::A2dpSinkEnable() { return true; }
114
A2dpSinkDisable()115 void FakeBluetoothAvInterface::A2dpSinkDisable() {}
116
AddA2dpSourceObserver(A2dpSourceObserver * observer)117 void FakeBluetoothAvInterface::AddA2dpSourceObserver(
118 A2dpSourceObserver* observer) {
119 CHECK(observer);
120 a2dp_source_observers_.AddObserver(observer);
121 }
122
RemoveA2dpSourceObserver(A2dpSourceObserver * observer)123 void FakeBluetoothAvInterface::RemoveA2dpSourceObserver(
124 A2dpSourceObserver* observer) {
125 CHECK(observer);
126 a2dp_source_observers_.RemoveObserver(observer);
127 }
128
AddA2dpSinkObserver(A2dpSinkObserver * observer)129 void FakeBluetoothAvInterface::AddA2dpSinkObserver(A2dpSinkObserver* observer) {
130 CHECK(observer);
131 a2dp_sink_observers_.AddObserver(observer);
132 }
133
RemoveA2dpSinkObserver(A2dpSinkObserver * observer)134 void FakeBluetoothAvInterface::RemoveA2dpSinkObserver(
135 A2dpSinkObserver* observer) {
136 CHECK(observer);
137 a2dp_sink_observers_.RemoveObserver(observer);
138 }
139
140 const btav_source_interface_t*
GetA2dpSourceHALInterface()141 FakeBluetoothAvInterface::GetA2dpSourceHALInterface() {
142 return &fake_a2dp_source_interface;
143 }
144
145 const btav_sink_interface_t*
GetA2dpSinkHALInterface()146 FakeBluetoothAvInterface::GetA2dpSinkHALInterface() {
147 return &fake_a2dp_sink_interface;
148 }
149
150 } // namespace hal
151 } // namespace bluetooth
152