1 //
2 //  Copyright (C) 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_avrcp_target_binder_server.h"
17 
18 #include <string>
19 
20 #include "base/logging.h"
21 
22 #include "service/adapter.h"
23 
24 #define AIDL_RET(value)      \
25   do {                       \
26     *_aidl_return = (value); \
27     return Status::ok();     \
28   } while (0)
29 
30 #define TRY_GET_TARGET()                                    \
31   ({                                                        \
32     auto target = GetAvrcpTarget();                         \
33     if (!target) {                                          \
34       LOG(ERROR) << __func__ << ": "                        \
35                  << "Failed to get AVRCP target interface"; \
36       AIDL_RET(false);                                      \
37     }                                                       \
38     target;                                                 \
39   })
40 
41 #define TRY_GET_CB()                                           \
42   ({                                                           \
43     auto cb = GetAvrcpTargetCallback();                        \
44     if (!cb.get()) {                                           \
45       LOG(WARNING) << "Callback for AVRCP target was deleted"; \
46       return;                                                  \
47     }                                                          \
48     cb;                                                        \
49   })
50 
51 #define TRY_RET(expr, msg) \
52   do {                     \
53     if (!(expr)) {         \
54       LOG(ERROR) << msg;   \
55       AIDL_RET(false);     \
56     }                      \
57     AIDL_RET(true);        \
58   } while (0)
59 
60 #define TRY_RET_FUNC(expr) TRY_RET(expr, __func__ << " failed")
61 
62 using android::String16;
63 using android::String8;
64 using android::binder::Status;
65 using android::bluetooth::BluetoothAvrcpIntValue;
66 using android::bluetooth::IBluetoothAvrcpTargetCallback;
67 
68 using LockGuard = std::lock_guard<std::mutex>;
69 
70 namespace ipc {
71 namespace binder {
72 
BluetoothAvrcpTargetBinderServer(bluetooth::Adapter * adapter)73 BluetoothAvrcpTargetBinderServer::BluetoothAvrcpTargetBinderServer(
74     bluetooth::Adapter* adapter)
75     : adapter_(adapter) {
76   CHECK(adapter_);
77 }
78 
79 BluetoothAvrcpTargetBinderServer::~BluetoothAvrcpTargetBinderServer() = default;
80 
HasInstance()81 bool BluetoothAvrcpTargetBinderServer::HasInstance() {
82   return GetAvrcpTarget() != nullptr;
83 }
84 
Register(const android::sp<IBluetoothAvrcpTargetCallback> & callback,bool * _aidl_return)85 Status BluetoothAvrcpTargetBinderServer::Register(
86     const android::sp<IBluetoothAvrcpTargetCallback>& callback,
87     bool* _aidl_return) {
88   VLOG(2) << __func__;
89 
90   bluetooth::AvrcpTargetFactory* gatt_client_factory =
91       adapter_->GetAvrcpTargetFactory();
92 
93   *_aidl_return = RegisterInstanceBase(callback, gatt_client_factory);
94   return Status::ok();
95 }
96 
Unregister(int32_t id)97 Status BluetoothAvrcpTargetBinderServer::Unregister(int32_t id) {
98   VLOG(2) << __func__;
99   UnregisterInstanceBase(id);
100   return Status::ok();
101 }
102 
UnregisterAll()103 Status BluetoothAvrcpTargetBinderServer::UnregisterAll() {
104   VLOG(2) << __func__;
105   UnregisterAllBase();
106   return Status::ok();
107 }
108 
Enable(bool * _aidl_return)109 Status BluetoothAvrcpTargetBinderServer::Enable(bool* _aidl_return) {
110   auto avrcp_target = TRY_GET_TARGET();
111   TRY_RET_FUNC(avrcp_target->Enable());
112 }
113 
Disable(bool * _aidl_return)114 Status BluetoothAvrcpTargetBinderServer::Disable(bool* _aidl_return) {
115   auto avrcp_target = TRY_GET_TARGET();
116   avrcp_target->Disable();
117   AIDL_RET(true);
118 }
119 
GetPlayStatusResponse(const android::String16 & addr,int32_t play_status,int32_t song_len,int32_t song_pos,bool * _aidl_return)120 Status BluetoothAvrcpTargetBinderServer::GetPlayStatusResponse(
121     const android::String16& addr, int32_t play_status, int32_t song_len,
122     int32_t song_pos, bool* _aidl_return) {
123   auto avrcp_target = TRY_GET_TARGET();
124   TRY_RET_FUNC(avrcp_target->GetPlayStatusResponse(
125       String8(addr).string(), play_status, song_len, song_pos));
126 }
127 
ListPlayerAppAttrResponse(const android::String16 & addr,const std::vector<int32_t> & attrs,bool * _aidl_return)128 Status BluetoothAvrcpTargetBinderServer::ListPlayerAppAttrResponse(
129     const android::String16& addr, const std::vector<int32_t>& attrs,
130     bool* _aidl_return) {
131   auto avrcp_target = TRY_GET_TARGET();
132   TRY_RET_FUNC(
133       avrcp_target->ListPlayerAppAttrResponse(String8(addr).string(), attrs));
134 }
135 
GetPlayerAppValueResponse(const android::String16 & addr,const std::vector<::android::bluetooth::BluetoothAvrcpIntValue> & values,bool * _aidl_return)136 Status BluetoothAvrcpTargetBinderServer::GetPlayerAppValueResponse(
137     const android::String16& addr,
138     const std::vector<::android::bluetooth::BluetoothAvrcpIntValue>& values,
139     bool* _aidl_return) {
140   auto avrcp_target = TRY_GET_TARGET();
141   std::vector<bluetooth::AvrcpIntValue> non_binder;
142   non_binder.reserve(values.size());
143   for (const auto& val : values) {
144     non_binder.push_back(val);
145   }
146   TRY_RET_FUNC(avrcp_target->GetPlayerAppValueResponse(String8(addr).string(),
147                                                        non_binder));
148 }
149 
GetPlayerAppAttrTextResponse(const android::String16 & addr,const std::vector<::android::bluetooth::BluetoothAvrcpStringValue> & attrs,bool * _aidl_return)150 Status BluetoothAvrcpTargetBinderServer::GetPlayerAppAttrTextResponse(
151     const android::String16& addr,
152     const std::vector<::android::bluetooth::BluetoothAvrcpStringValue>& attrs,
153     bool* _aidl_return) {
154   auto avrcp_target = TRY_GET_TARGET();
155   std::vector<bluetooth::AvrcpStringValue> non_binder;
156   non_binder.reserve(attrs.size());
157   for (const auto& val : attrs) {
158     non_binder.push_back(val);
159   }
160   TRY_RET_FUNC(avrcp_target->GetPlayerAppAttrTextResponse(
161       String8(addr).string(), non_binder));
162 }
163 
GetPlayerAppValueTextResponse(const android::String16 & addr,const std::vector<::android::bluetooth::BluetoothAvrcpStringValue> & values,bool * _aidl_return)164 Status BluetoothAvrcpTargetBinderServer::GetPlayerAppValueTextResponse(
165     const android::String16& addr,
166     const std::vector<::android::bluetooth::BluetoothAvrcpStringValue>& values,
167     bool* _aidl_return) {
168   auto avrcp_target = TRY_GET_TARGET();
169   std::vector<bluetooth::AvrcpStringValue> non_binder;
170   non_binder.reserve(values.size());
171   for (const auto& val : values) {
172     non_binder.push_back(val);
173   }
174   TRY_RET_FUNC(avrcp_target->GetPlayerAppValueTextResponse(
175       String8(addr).string(), non_binder));
176 }
177 
GetElementAttrResponse(const android::String16 & addr,const std::vector<::android::bluetooth::BluetoothAvrcpStringValue> & attrs,bool * _aidl_return)178 Status BluetoothAvrcpTargetBinderServer::GetElementAttrResponse(
179     const android::String16& addr,
180     const std::vector<::android::bluetooth::BluetoothAvrcpStringValue>& attrs,
181     bool* _aidl_return) {
182   auto avrcp_target = TRY_GET_TARGET();
183   std::vector<bluetooth::AvrcpStringValue> non_binder;
184   non_binder.reserve(attrs.size());
185   for (const auto& val : attrs) {
186     non_binder.push_back(val);
187   }
188   TRY_RET_FUNC(
189       avrcp_target->GetElementAttrResponse(String8(addr).string(), non_binder));
190 }
191 
SetPlayerAppValueResponse(const android::String16 & addr,int32_t rsp_status,bool * _aidl_return)192 Status BluetoothAvrcpTargetBinderServer::SetPlayerAppValueResponse(
193     const android::String16& addr, int32_t rsp_status, bool* _aidl_return) {
194   auto avrcp_target = TRY_GET_TARGET();
195   TRY_RET_FUNC(avrcp_target->SetPlayerAppValueResponse(String8(addr).string(),
196                                                        rsp_status));
197 }
198 
RegisterNotificationResponse(int32_t event_id,int32_t type,const::android::bluetooth::BluetoothAvrcpRegisterNotificationResponse & param,bool * _aidl_return)199 Status BluetoothAvrcpTargetBinderServer::RegisterNotificationResponse(
200     int32_t event_id, int32_t type,
201     const ::android::bluetooth::BluetoothAvrcpRegisterNotificationResponse&
202         param,
203     bool* _aidl_return) {
204   auto avrcp_target = TRY_GET_TARGET();
205   TRY_RET_FUNC(
206       avrcp_target->RegisterNotificationResponse(event_id, type, param));
207 }
208 
SetVolume(int32_t volume,bool * _aidl_return)209 Status BluetoothAvrcpTargetBinderServer::SetVolume(int32_t volume,
210                                                    bool* _aidl_return) {
211   auto avrcp_target = TRY_GET_TARGET();
212   TRY_RET_FUNC(avrcp_target->SetVolume(volume));
213 }
214 
OnGetRemoteFeatures(const std::string & addr,int32_t features)215 void BluetoothAvrcpTargetBinderServer::OnGetRemoteFeatures(
216     const std::string& addr, int32_t features) {
217   LockGuard lock(*maps_lock());
218   auto cb = TRY_GET_CB();
219   cb->OnGetRemoteFeatures(String16(addr.data(), addr.size()), features);
220 }
221 
OnGetPlayStatus(const std::string & addr)222 void BluetoothAvrcpTargetBinderServer::OnGetPlayStatus(
223     const std::string& addr) {
224   LockGuard lock(*maps_lock());
225   auto cb = TRY_GET_CB();
226   cb->OnGetPlayStatus(String16(addr.data(), addr.size()));
227 }
228 
OnListPlayerAppAttr(const std::string & addr)229 void BluetoothAvrcpTargetBinderServer::OnListPlayerAppAttr(
230     const std::string& addr) {
231   LockGuard lock(*maps_lock());
232   auto cb = TRY_GET_CB();
233   cb->OnListPlayerAppAttr(String16(addr.data(), addr.size()));
234 }
235 
OnListPlayerAppValues(const std::string & addr,int32_t attr_id)236 void BluetoothAvrcpTargetBinderServer::OnListPlayerAppValues(
237     const std::string& addr, int32_t attr_id) {
238   LockGuard lock(*maps_lock());
239   auto cb = TRY_GET_CB();
240   cb->OnListPlayerAppValues(String16(addr.data(), addr.size()), attr_id);
241 }
242 
OnGetPlayerAppValue(const std::string & addr,const std::vector<int32_t> & attrs)243 void BluetoothAvrcpTargetBinderServer::OnGetPlayerAppValue(
244     const std::string& addr, const std::vector<int32_t>& attrs) {
245   LockGuard lock(*maps_lock());
246   auto cb = TRY_GET_CB();
247   cb->OnGetPlayerAppValue(String16(addr.data(), addr.size()), attrs);
248 }
249 
OnGetPlayerAppAttrsText(const std::string & addr,const std::vector<int32_t> & attrs)250 void BluetoothAvrcpTargetBinderServer::OnGetPlayerAppAttrsText(
251     const std::string& addr, const std::vector<int32_t>& attrs) {
252   LockGuard lock(*maps_lock());
253   auto cb = TRY_GET_CB();
254   cb->OnGetPlayerAppAttrsText(String16(addr.data(), addr.size()), attrs);
255 }
256 
OnGetPlayerAppValuesText(const std::string & addr,int32_t attr_id,const std::vector<int32_t> & values)257 void BluetoothAvrcpTargetBinderServer::OnGetPlayerAppValuesText(
258     const std::string& addr, int32_t attr_id,
259     const std::vector<int32_t>& values) {
260   LockGuard lock(*maps_lock());
261   auto cb = TRY_GET_CB();
262   cb->OnGetPlayerAppValuesText(String16(addr.data(), addr.size()), attr_id,
263                                values);
264 }
265 
OnSetPlayerAppValue(const std::string & addr,const std::vector<bluetooth::AvrcpIntValue> & values)266 void BluetoothAvrcpTargetBinderServer::OnSetPlayerAppValue(
267     const std::string& addr,
268     const std::vector<bluetooth::AvrcpIntValue>& values) {
269   std::vector<BluetoothAvrcpIntValue> binder_values;
270   binder_values.reserve(values.size());
271   for (const auto& val : values) {
272     binder_values.push_back(val);
273   }
274 
275   LockGuard lock(*maps_lock());
276   auto cb = TRY_GET_CB();
277   cb->OnSetPlayerAppValue(String16(addr.data(), addr.size()), binder_values);
278 }
279 
OnGetElementAttrs(const std::string & addr,const std::vector<int32_t> & attrs)280 void BluetoothAvrcpTargetBinderServer::OnGetElementAttrs(
281     const std::string& addr, const std::vector<int32_t>& attrs) {
282   LockGuard lock(*maps_lock());
283   auto cb = TRY_GET_CB();
284   cb->OnGetElementAttrs(String16(addr.data(), addr.size()), attrs);
285 }
286 
OnRegisterNotification(const std::string & addr,int32_t event_id,uint32_t param)287 void BluetoothAvrcpTargetBinderServer::OnRegisterNotification(
288     const std::string& addr, int32_t event_id, uint32_t param) {
289   LockGuard lock(*maps_lock());
290   auto cb = TRY_GET_CB();
291   cb->OnRegisterNotification(String16(addr.data(), addr.size()), event_id,
292                              param);
293 }
294 
OnVolumeChange(const std::string & addr,int32_t volume,int32_t ctype)295 void BluetoothAvrcpTargetBinderServer::OnVolumeChange(const std::string& addr,
296                                                       int32_t volume,
297                                                       int32_t ctype) {
298   LockGuard lock(*maps_lock());
299   auto cb = TRY_GET_CB();
300   cb->OnVolumeChange(String16(addr.data(), addr.size()), volume, ctype);
301 }
302 
OnPassThroughCommand(const std::string & addr,int32_t id,int32_t key_state)303 void BluetoothAvrcpTargetBinderServer::OnPassThroughCommand(
304     const std::string& addr, int32_t id, int32_t key_state) {
305   LockGuard lock(*maps_lock());
306   auto cb = TRY_GET_CB();
307   cb->OnPassThroughCommand(String16(addr.data(), addr.size()), id, key_state);
308 }
309 
310 android::sp<IBluetoothAvrcpTargetCallback>
GetAvrcpTargetCallback()311 BluetoothAvrcpTargetBinderServer::GetAvrcpTargetCallback() {
312   auto cb = GetCallback(bluetooth::AvrcpTarget::kSingletonInstanceId);
313   return android::sp<IBluetoothAvrcpTargetCallback>(
314       static_cast<IBluetoothAvrcpTargetCallback*>(cb.get()));
315 }
316 
317 std::shared_ptr<bluetooth::AvrcpTarget>
GetAvrcpTarget()318 BluetoothAvrcpTargetBinderServer::GetAvrcpTarget() {
319   return std::static_pointer_cast<bluetooth::AvrcpTarget>(
320       GetInstance(bluetooth::AvrcpTarget::kSingletonInstanceId));
321 }
322 
OnRegisterInstanceImpl(bluetooth::BLEStatus status,android::sp<IInterface> callback,bluetooth::BluetoothInstance * instance)323 void BluetoothAvrcpTargetBinderServer::OnRegisterInstanceImpl(
324     bluetooth::BLEStatus status, android::sp<IInterface> callback,
325     bluetooth::BluetoothInstance* instance) {
326   VLOG(1) << __func__ << " client ID: " << instance->GetInstanceId()
327           << " status: " << status;
328 
329   bluetooth::AvrcpTarget* avrcp_target =
330       static_cast<bluetooth::AvrcpTarget*>(instance);
331   avrcp_target->SetDelegate(this);
332 
333   android::sp<IBluetoothAvrcpTargetCallback> cb(
334       static_cast<IBluetoothAvrcpTargetCallback*>(callback.get()));
335   cb->OnRegistered(status);
336 }
337 
338 }  // namespace binder
339 }  // namespace ipc
340