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 #include "service/ipc/binder/bluetooth_a2dp_sink_binder_server.h"
17
18 #include <base/logging.h>
19
20 #include "service/adapter.h"
21
22 using android::String16;
23 using android::String8;
24 using android::binder::Status;
25
26 using android::bluetooth::IBluetoothA2dpSinkCallback;
27
28 namespace ipc {
29 namespace binder {
30
BluetoothA2dpSinkBinderServer(bluetooth::Adapter * adapter)31 BluetoothA2dpSinkBinderServer::BluetoothA2dpSinkBinderServer(
32 bluetooth::Adapter* adapter)
33 : adapter_(adapter) {
34 CHECK(adapter);
35 }
36
Register(const android::sp<IBluetoothA2dpSinkCallback> & callback,bool * _aidl_return)37 Status BluetoothA2dpSinkBinderServer::Register(
38 const android::sp<IBluetoothA2dpSinkCallback>& callback,
39 bool* _aidl_return) {
40 auto factory = adapter_->GetA2dpSinkFactory();
41 *_aidl_return = RegisterInstanceBase(callback, factory);
42 return Status::ok();
43 }
44
Unregister()45 Status BluetoothA2dpSinkBinderServer::Unregister() {
46 UnregisterAllBase();
47 return Status::ok();
48 }
49
Enable(bool * _aidl_return)50 Status BluetoothA2dpSinkBinderServer::Enable(bool* _aidl_return) {
51 std::lock_guard<std::mutex> lock(*maps_lock());
52 auto a2dp_sink = GetA2dpSink();
53 if (!a2dp_sink) {
54 LOG(ERROR) << "Failed to get A2DP sink instance";
55 *_aidl_return = false;
56 return Status::ok();
57 }
58
59 if (!a2dp_sink->Enable()) {
60 LOG(ERROR) << "Failed to enable";
61 *_aidl_return = false;
62 return Status::ok();
63 }
64
65 *_aidl_return = true;
66 return Status::ok();
67 }
68
Disable(bool * _aidl_return)69 Status BluetoothA2dpSinkBinderServer::Disable(bool* _aidl_return) {
70 std::lock_guard<std::mutex> lock(*maps_lock());
71 auto a2dp_sink = GetA2dpSink();
72 if (!a2dp_sink) {
73 LOG(ERROR) << "Failed to get A2DP sink instance";
74 *_aidl_return = false;
75 return Status::ok();
76 }
77
78 a2dp_sink->Disable();
79 *_aidl_return = true;
80 return Status::ok();
81 }
82
Connect(const String16 & device_address,bool * _aidl_return)83 Status BluetoothA2dpSinkBinderServer::Connect(const String16& device_address,
84 bool* _aidl_return) {
85 std::lock_guard<std::mutex> lock(*maps_lock());
86 auto a2dp_sink = GetA2dpSink();
87 if (!a2dp_sink) {
88 LOG(ERROR) << "Failed to get A2DP sink instance";
89 *_aidl_return = false;
90 return Status::ok();
91 }
92
93 if (!a2dp_sink->Connect(String8(device_address).string())) {
94 LOG(ERROR) << "Failed to connect";
95 *_aidl_return = false;
96 return Status::ok();
97 }
98
99 *_aidl_return = true;
100 return Status::ok();
101 }
102
Disconnect(const String16 & device_address,bool * _aidl_return)103 Status BluetoothA2dpSinkBinderServer::Disconnect(const String16& device_address,
104 bool* _aidl_return) {
105 std::lock_guard<std::mutex> lock(*maps_lock());
106 auto a2dp_sink = GetA2dpSink();
107 if (!a2dp_sink) {
108 LOG(ERROR) << "Failed to get A2DP sink instance";
109 *_aidl_return = false;
110 return Status::ok();
111 }
112
113 if (!a2dp_sink->Disconnect(String8(device_address).string())) {
114 LOG(ERROR) << "Failed to connect";
115 *_aidl_return = false;
116 return Status::ok();
117 }
118
119 *_aidl_return = true;
120 return Status::ok();
121 }
122
SetAudioFocusState(int focus_state,bool * _aidl_return)123 Status BluetoothA2dpSinkBinderServer::SetAudioFocusState(int focus_state,
124 bool* _aidl_return) {
125 std::lock_guard<std::mutex> lock(*maps_lock());
126 auto a2dp_sink = GetA2dpSink();
127 if (!a2dp_sink) {
128 LOG(ERROR) << "Failed to get A2DP instance";
129 *_aidl_return = false;
130 return Status::ok();
131 }
132
133 a2dp_sink->SetAudioFocusState(focus_state);
134 *_aidl_return = true;
135 return Status::ok();
136 }
137
SetAudioTrackGain(float gain,bool * _aidl_return)138 Status BluetoothA2dpSinkBinderServer::SetAudioTrackGain(float gain,
139 bool* _aidl_return) {
140 std::lock_guard<std::mutex> lock(*maps_lock());
141 auto a2dp_sink = GetA2dpSink();
142 if (!a2dp_sink) {
143 LOG(ERROR) << "Failed to get A2DP instance";
144 *_aidl_return = false;
145 return Status::ok();
146 }
147
148 a2dp_sink->SetAudioTrackGain(gain);
149 *_aidl_return = true;
150 return Status::ok();
151 }
152
OnConnectionState(const std::string & device_address,int state)153 void BluetoothA2dpSinkBinderServer::OnConnectionState(
154 const std::string& device_address, int state) {
155 std::lock_guard<std::mutex> lock(*maps_lock());
156 auto cb = GetA2dpSinkCallback();
157 if (!cb.get()) {
158 LOG(WARNING) << "Callback for this GattServer was deleted.";
159 return;
160 }
161
162 cb->OnConnectionState(String16(device_address.c_str()), state);
163 }
164
OnAudioState(const std::string & device_address,int state)165 void BluetoothA2dpSinkBinderServer::OnAudioState(
166 const std::string& device_address, int state) {
167 std::lock_guard<std::mutex> lock(*maps_lock());
168 auto cb = GetA2dpSinkCallback();
169 if (!cb.get()) {
170 LOG(WARNING) << "Callback for this GattServer was deleted.";
171 return;
172 }
173
174 cb->OnAudioState(String16(device_address.c_str()), state);
175 }
176
OnAudioConfig(const std::string & device_address,uint32_t sample_rate,uint8_t channel_count)177 void BluetoothA2dpSinkBinderServer::OnAudioConfig(
178 const std::string& device_address, uint32_t sample_rate,
179 uint8_t channel_count) {
180 std::lock_guard<std::mutex> lock(*maps_lock());
181 auto cb = GetA2dpSinkCallback();
182 if (!cb.get()) {
183 LOG(WARNING) << "Callback for this GattServer was deleted.";
184 return;
185 }
186
187 cb->OnAudioConfig(String16(device_address.c_str()), sample_rate,
188 channel_count);
189 }
190
HasInstance()191 bool BluetoothA2dpSinkBinderServer::HasInstance() {
192 return GetA2dpSink() != nullptr;
193 }
194
195 android::sp<IBluetoothA2dpSinkCallback>
GetA2dpSinkCallback()196 BluetoothA2dpSinkBinderServer::GetA2dpSinkCallback() {
197 auto cb = GetCallback(bluetooth::A2dpSink::kSingletonInstanceId);
198 return android::sp<IBluetoothA2dpSinkCallback>(
199 static_cast<IBluetoothA2dpSinkCallback*>(cb.get()));
200 }
201
202 std::shared_ptr<bluetooth::A2dpSink>
GetA2dpSink()203 BluetoothA2dpSinkBinderServer::GetA2dpSink() {
204 return std::static_pointer_cast<bluetooth::A2dpSink>(
205 GetInstance(bluetooth::A2dpSink::kSingletonInstanceId));
206 }
207
OnRegisterInstanceImpl(bluetooth::BLEStatus status,android::sp<IInterface> callback,bluetooth::BluetoothInstance * instance)208 void BluetoothA2dpSinkBinderServer::OnRegisterInstanceImpl(
209 bluetooth::BLEStatus status, android::sp<IInterface> callback,
210 bluetooth::BluetoothInstance* instance) {
211 VLOG(1) << __func__ << " instance ID: " << instance->GetInstanceId()
212 << " status: " << status;
213 bluetooth::A2dpSink* a2dp_sink = static_cast<bluetooth::A2dpSink*>(instance);
214 a2dp_sink->SetDelegate(this);
215
216 android::sp<IBluetoothA2dpSinkCallback> cb(
217 static_cast<IBluetoothA2dpSinkCallback*>(callback.get()));
218 cb->OnRegistered(status);
219 }
220
221 } // namespace binder
222 } // namespace ipc
223