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::TestA2dpSourceHandler>
27     g_a2dp_source_handler{};
28 std::shared_ptr<FakeBluetoothAvInterface::TestA2dpSinkHandler>
29     g_a2dp_sink_handler{};
30 
FakeSourceInit(btav_source_callbacks_t * callbacks,int max_connected_audio_devices,const std::vector<btav_a2dp_codec_config_t> & codec_priorities,const std::vector<btav_a2dp_codec_config_t> & offloading_preference)31 bt_status_t FakeSourceInit(
32     btav_source_callbacks_t* callbacks, int max_connected_audio_devices,
33     const std::vector<btav_a2dp_codec_config_t>& codec_priorities,
34     const std::vector<btav_a2dp_codec_config_t>& offloading_preference) {
35   return BT_STATUS_SUCCESS;
36 }
37 
FakeSinkInit(btav_sink_callbacks_t * callbacks,int max_connected_audio_devices)38 bt_status_t FakeSinkInit(btav_sink_callbacks_t* callbacks,
39                          int max_connected_audio_devices) {
40   return BT_STATUS_SUCCESS;
41 }
42 
FakeConnect(const RawAddress & bd_addr)43 bt_status_t FakeConnect(const RawAddress& bd_addr) {
44   if (g_a2dp_source_handler) return g_a2dp_source_handler->Connect(bd_addr);
45   if (g_a2dp_sink_handler) return g_a2dp_sink_handler->Connect(bd_addr);
46   return BT_STATUS_FAIL;
47 }
48 
FakeDisconnect(const RawAddress & bd_addr)49 bt_status_t FakeDisconnect(const RawAddress& bd_addr) {
50   if (g_a2dp_source_handler) return g_a2dp_source_handler->Disconnect(bd_addr);
51   if (g_a2dp_sink_handler) return g_a2dp_sink_handler->Disconnect(bd_addr);
52   return BT_STATUS_FAIL;
53 }
54 
FakeCleanup(void)55 void FakeCleanup(void) {}
56 
FakeSetAudioFocusState(int focus_state)57 void FakeSetAudioFocusState(int focus_state) {
58   if (g_a2dp_sink_handler)
59     return g_a2dp_sink_handler->SetAudioFocusState(focus_state);
60 }
61 
FakeSetAudioTrackGain(float gain)62 void FakeSetAudioTrackGain(float gain) {
63   if (g_a2dp_sink_handler) return g_a2dp_sink_handler->SetAudioTrackGain(gain);
64 }
65 
66 btav_source_interface_t fake_a2dp_source_interface = {
67     .size = sizeof(btav_source_interface_t),
68     .init = FakeSourceInit,
69     .connect = FakeConnect,
70     .disconnect = FakeDisconnect,
71     .set_silence_device = nullptr,
72     .set_active_device = nullptr,
73     .config_codec = nullptr,
74     .cleanup = FakeCleanup,
75 };
76 
77 btav_sink_interface_t fake_a2dp_sink_interface = {
78     .size = sizeof(btav_sink_interface_t),
79     .init = FakeSinkInit,
80     .connect = FakeConnect,
81     .disconnect = FakeDisconnect,
82     .cleanup = FakeCleanup,
83     .set_audio_focus_state = FakeSetAudioFocusState,
84     .set_audio_track_gain = FakeSetAudioTrackGain,
85     .set_active_device = nullptr,
86 };
87 
88 }  // namespace
89 
FakeBluetoothAvInterface(std::shared_ptr<TestA2dpSourceHandler> a2dp_source_handler)90 FakeBluetoothAvInterface::FakeBluetoothAvInterface(
91     std::shared_ptr<TestA2dpSourceHandler> a2dp_source_handler) {
92   CHECK(!g_a2dp_source_handler);
93 
94   if (a2dp_source_handler) g_a2dp_source_handler = a2dp_source_handler;
95 }
96 
FakeBluetoothAvInterface(std::shared_ptr<TestA2dpSinkHandler> a2dp_sink_handler)97 FakeBluetoothAvInterface::FakeBluetoothAvInterface(
98     std::shared_ptr<TestA2dpSinkHandler> a2dp_sink_handler) {
99   CHECK(!g_a2dp_sink_handler);
100 
101   if (a2dp_sink_handler) g_a2dp_sink_handler = a2dp_sink_handler;
102 }
103 
~FakeBluetoothAvInterface()104 FakeBluetoothAvInterface::~FakeBluetoothAvInterface() {
105   g_a2dp_source_handler = {};
106   g_a2dp_sink_handler = {};
107 }
108 
NotifyConnectionState(const RawAddress & bda,btav_connection_state_t state)109 void FakeBluetoothAvInterface::NotifyConnectionState(
110     const RawAddress& bda, btav_connection_state_t state) {
111   for (auto& observer : a2dp_source_observers_) {
112     observer.ConnectionStateCallback(this, bda, state);
113   }
114   for (auto& observer : a2dp_sink_observers_) {
115     observer.ConnectionStateCallback(this, bda, state);
116   }
117 }
NotifyAudioState(const RawAddress & bda,btav_audio_state_t state)118 void FakeBluetoothAvInterface::NotifyAudioState(const RawAddress& bda,
119                                                 btav_audio_state_t state) {
120   for (auto& observer : a2dp_source_observers_) {
121     observer.AudioStateCallback(this, bda, state);
122   }
123   for (auto& observer : a2dp_sink_observers_) {
124     observer.AudioStateCallback(this, bda, state);
125   }
126 }
NotifyAudioConfig(const RawAddress & bda,const btav_a2dp_codec_config_t & codec_config,const std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,const std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities)127 void FakeBluetoothAvInterface::NotifyAudioConfig(
128     const RawAddress& bda, const btav_a2dp_codec_config_t& codec_config,
129     const std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
130     const std::vector<btav_a2dp_codec_config_t>
131         codecs_selectable_capabilities) {
132   for (auto& observer : a2dp_source_observers_) {
133     observer.AudioConfigCallback(this, bda, codec_config,
134                                  codecs_local_capabilities,
135                                  codecs_selectable_capabilities);
136   }
137 }
QueryMandatoryCodecPreferred(const RawAddress & bda)138 bool FakeBluetoothAvInterface::QueryMandatoryCodecPreferred(
139     const RawAddress& bda) {
140   // The mandatory codec is preferred only when all observers disable their
141   // optional codecs.
142   for (auto& observer : a2dp_source_observers_) {
143     if (!observer.MandatoryCodecPreferredCallback(this, bda)) {
144       return false;
145     }
146   }
147   return true;
148 }
NotifyAudioConfig(const RawAddress & bda,uint32_t sample_rate,uint8_t channel_count)149 void FakeBluetoothAvInterface::NotifyAudioConfig(const RawAddress& bda,
150                                                  uint32_t sample_rate,
151                                                  uint8_t channel_count) {
152   for (auto& observer : a2dp_sink_observers_) {
153     observer.AudioConfigCallback(this, bda, sample_rate, channel_count);
154   }
155 }
156 
A2dpSourceEnable(std::vector<btav_a2dp_codec_config_t> codec_priorities)157 bool FakeBluetoothAvInterface::A2dpSourceEnable(
158     std::vector<btav_a2dp_codec_config_t> codec_priorities) {
159   return true;
160 }
161 
A2dpSourceDisable()162 void FakeBluetoothAvInterface::A2dpSourceDisable() {}
163 
A2dpSinkEnable()164 bool FakeBluetoothAvInterface::A2dpSinkEnable() { return true; }
165 
A2dpSinkDisable()166 void FakeBluetoothAvInterface::A2dpSinkDisable() {}
167 
AddA2dpSourceObserver(A2dpSourceObserver * observer)168 void FakeBluetoothAvInterface::AddA2dpSourceObserver(
169     A2dpSourceObserver* observer) {
170   CHECK(observer);
171   a2dp_source_observers_.AddObserver(observer);
172 }
173 
RemoveA2dpSourceObserver(A2dpSourceObserver * observer)174 void FakeBluetoothAvInterface::RemoveA2dpSourceObserver(
175     A2dpSourceObserver* observer) {
176   CHECK(observer);
177   a2dp_source_observers_.RemoveObserver(observer);
178 }
179 
AddA2dpSinkObserver(A2dpSinkObserver * observer)180 void FakeBluetoothAvInterface::AddA2dpSinkObserver(A2dpSinkObserver* observer) {
181   CHECK(observer);
182   a2dp_sink_observers_.AddObserver(observer);
183 }
184 
RemoveA2dpSinkObserver(A2dpSinkObserver * observer)185 void FakeBluetoothAvInterface::RemoveA2dpSinkObserver(
186     A2dpSinkObserver* observer) {
187   CHECK(observer);
188   a2dp_sink_observers_.RemoveObserver(observer);
189 }
190 
191 const btav_source_interface_t*
GetA2dpSourceHALInterface()192 FakeBluetoothAvInterface::GetA2dpSourceHALInterface() {
193   return &fake_a2dp_source_interface;
194 }
195 
196 const btav_sink_interface_t*
GetA2dpSinkHALInterface()197 FakeBluetoothAvInterface::GetA2dpSinkHALInterface() {
198   return &fake_a2dp_sink_interface;
199 }
200 
201 }  // namespace hal
202 }  // namespace bluetooth
203