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_avrcp_control_binder_server.h"
17 
18 #include <string>
19 
20 #include "base/logging.h"
21 
22 #include "service/adapter.h"
23 
24 using android::String16;
25 using android::String8;
26 using android::binder::Status;
27 using android::bluetooth::IBluetoothAvrcpControlCallback;
28 
29 namespace ipc {
30 namespace binder {
31 
32 namespace {
33 const int kInvalidInstanceId = -1;
34 }  // namespace
35 
BluetoothAvrcpControlBinderServer(bluetooth::Adapter * adapter)36 BluetoothAvrcpControlBinderServer::BluetoothAvrcpControlBinderServer(
37     bluetooth::Adapter* adapter)
38     : adapter_(adapter) {
39   CHECK(adapter_);
40 }
41 
Register(const android::sp<IBluetoothAvrcpControlCallback> & callback,bool * _aidl_return)42 Status BluetoothAvrcpControlBinderServer::Register(
43     const android::sp<IBluetoothAvrcpControlCallback>& callback,
44     bool* _aidl_return) {
45   VLOG(2) << __func__;
46 
47   bluetooth::AvrcpControlFactory* gatt_client_factory =
48       adapter_->GetAvrcpControlFactory();
49 
50   *_aidl_return = RegisterInstanceBase(callback, gatt_client_factory);
51   return Status::ok();
52 }
53 
Unregister(int32_t id)54 Status BluetoothAvrcpControlBinderServer::Unregister(int32_t id) {
55   VLOG(2) << __func__;
56   UnregisterInstanceBase(id);
57   return Status::ok();
58 }
59 
UnregisterAll()60 Status BluetoothAvrcpControlBinderServer::UnregisterAll() {
61   VLOG(2) << __func__;
62   UnregisterAllBase();
63   return Status::ok();
64 }
65 
Enable(int32_t id,bool * _aidl_return)66 Status BluetoothAvrcpControlBinderServer::Enable(int32_t id,
67                                                  bool* _aidl_return) {
68   std::lock_guard<std::mutex> lock(*maps_lock());
69   auto avrcp_control = GetAvrcpControl(id);
70   if (!avrcp_control) {
71     LOG(ERROR) << "Failed to get avrcp control instance";
72     *_aidl_return = false;
73     return Status::ok();
74   }
75 
76   if (!avrcp_control->Enable()) {
77     LOG(ERROR) << "Failed to enable";
78     *_aidl_return = false;
79     return Status::ok();
80   }
81 
82   *_aidl_return = true;
83   return Status::ok();
84 }
85 
Disable(int32_t id,bool * _aidl_return)86 Status BluetoothAvrcpControlBinderServer::Disable(int32_t id,
87                                                   bool* _aidl_return) {
88   std::lock_guard<std::mutex> lock(*maps_lock());
89   auto avrcp_control = GetAvrcpControl(id);
90   if (!avrcp_control) {
91     LOG(ERROR) << "Failed to get avrcp control instance";
92     *_aidl_return = false;
93     return Status::ok();
94   }
95 
96   avrcp_control->Disable();
97   *_aidl_return = true;
98   return Status::ok();
99 }
100 
SendPassThroughCommand(int32_t id,const String16 & device_address,int32_t key_code,bool key_pressed,bool * _aidl_return)101 Status BluetoothAvrcpControlBinderServer::SendPassThroughCommand(
102     int32_t id, const String16& device_address, int32_t key_code,
103     bool key_pressed, bool* _aidl_return) {
104   std::lock_guard<std::mutex> lock(*maps_lock());
105   auto avrcp_control = GetAvrcpControl(id);
106   if (!avrcp_control) {
107     LOG(ERROR) << "Failed to get avrcp control instance";
108     *_aidl_return = false;
109     return Status::ok();
110   }
111 
112   if (!avrcp_control->SendPassThroughCommand(String8(device_address).string(),
113                                              key_code, key_pressed)) {
114     LOG(ERROR) << "Failed to send pass through command";
115     *_aidl_return = false;
116     return Status::ok();
117   }
118 
119   *_aidl_return = true;
120   return Status::ok();
121 }
122 
SetAbsVolumeResponse(int32_t id,const android::String16 & device_address,int32_t abs_vol,int32_t label,bool * _aidl_return)123 android::binder::Status BluetoothAvrcpControlBinderServer::SetAbsVolumeResponse(
124     int32_t id, const android::String16& device_address, int32_t abs_vol,
125     int32_t label, bool* _aidl_return) {
126   std::lock_guard<std::mutex> lock(*maps_lock());
127   auto avrcp_control = GetAvrcpControl(id);
128   if (!avrcp_control) {
129     LOG(ERROR) << "Failed to get avrcp control instance";
130     *_aidl_return = false;
131     return Status::ok();
132   }
133 
134   if (!avrcp_control->SetAbsVolumeResponse(String8(device_address).string(),
135                                            abs_vol, label)) {
136     LOG(ERROR) << "Failed to send set absolute volume response";
137     *_aidl_return = false;
138     return Status::ok();
139   }
140   *_aidl_return = true;
141   return Status::ok();
142 }
143 
144 android::binder::Status
RegisterForAbsVolumeCallbackResponse(int32_t id,const android::String16 & device_address,int32_t response_type,int32_t abs_vol,int32_t label,bool * _aidl_return)145 BluetoothAvrcpControlBinderServer::RegisterForAbsVolumeCallbackResponse(
146     int32_t id, const android::String16& device_address, int32_t response_type,
147     int32_t abs_vol, int32_t label, bool* _aidl_return) {
148   std::lock_guard<std::mutex> lock(*maps_lock());
149   auto avrcp_control = GetAvrcpControl(id);
150   if (!avrcp_control) {
151     LOG(ERROR) << "Failed to get avrcp control instance";
152     *_aidl_return = false;
153     return Status::ok();
154   }
155 
156   if (!avrcp_control->RegisterForAbsVolumeCallbackResponse(
157           String8(device_address).string(), response_type, abs_vol, label)) {
158     LOG(ERROR)
159         << "Failed to send register for absolute volume callback response";
160     *_aidl_return = false;
161     return Status::ok();
162   }
163   *_aidl_return = true;
164   return Status::ok();
165 }
166 
OnConnectionState(bool rc_connect,bool bt_connect,const std::string & device_address)167 void BluetoothAvrcpControlBinderServer::OnConnectionState(
168     bool rc_connect, bool bt_connect, const std::string& device_address) {
169   auto func = [rc_connect, bt_connect, &device_address](IInterface* cb) {
170     auto avrcp_control_cb = static_cast<IBluetoothAvrcpControlCallback*>(cb);
171     avrcp_control_cb->OnConnectionState(rc_connect, bt_connect,
172                                         String16(device_address.c_str()));
173   };
174 
175   ForEachCallback(func);
176 }
177 
OnTrackChanged(const std::string & device_address,const bluetooth::AvrcpMediaAttr & attr)178 void BluetoothAvrcpControlBinderServer::OnTrackChanged(
179     const std::string& device_address, const bluetooth::AvrcpMediaAttr& attr) {
180   auto binder_attr = android::bluetooth::BluetoothAvrcpMediaAttr(attr);
181 
182   auto func = [&device_address, &binder_attr](IInterface* cb) {
183     auto avrcp_control_cb = static_cast<IBluetoothAvrcpControlCallback*>(cb);
184     avrcp_control_cb->OnTrackChanged(String16(device_address.c_str()),
185                                      binder_attr);
186   };
187 
188   ForEachCallback(func);
189 }
190 
OnSetAbsVolumeRequest(const std::string & device_address,int32_t abs_vol,int32_t label)191 void BluetoothAvrcpControlBinderServer::OnSetAbsVolumeRequest(
192     const std::string& device_address, int32_t abs_vol, int32_t label) {
193   auto addr_s16 = String16(device_address.c_str(), device_address.size());
194   auto func = [&addr_s16, abs_vol, label](IInterface* cb) {
195     auto avrcp_control_cb = static_cast<IBluetoothAvrcpControlCallback*>(cb);
196     avrcp_control_cb->OnSetAbsVolumeRequest(addr_s16, abs_vol, label);
197   };
198 
199   ForEachCallback(func);
200 }
201 
OnRegisterForAbsVolumeCallbackRequest(const std::string & device_address,int32_t label)202 void BluetoothAvrcpControlBinderServer::OnRegisterForAbsVolumeCallbackRequest(
203     const std::string& device_address, int32_t label) {
204   auto addr_s16 = String16(device_address.c_str(), device_address.size());
205   auto func = [&addr_s16, label](IInterface* cb) {
206     auto avrcp_control_cb = static_cast<IBluetoothAvrcpControlCallback*>(cb);
207     avrcp_control_cb->OnRegisterForAbsVolumeCallbackRequest(addr_s16, label);
208   };
209 
210   ForEachCallback(func);
211 }
212 
213 std::shared_ptr<bluetooth::AvrcpControl>
GetAvrcpControl(int id)214 BluetoothAvrcpControlBinderServer::GetAvrcpControl(int id) {
215   return std::static_pointer_cast<bluetooth::AvrcpControl>(GetInstance(id));
216 }
217 
OnRegisterInstanceImpl(bluetooth::BLEStatus status,android::sp<IInterface> callback,bluetooth::BluetoothInstance * instance)218 void BluetoothAvrcpControlBinderServer::OnRegisterInstanceImpl(
219     bluetooth::BLEStatus status, android::sp<IInterface> callback,
220     bluetooth::BluetoothInstance* instance) {
221   VLOG(1) << __func__ << " client ID: " << instance->GetInstanceId()
222           << " status: " << status;
223 
224   bluetooth::AvrcpControl* avrcp_control =
225       static_cast<bluetooth::AvrcpControl*>(instance);
226   avrcp_control->SetDelegate(this);
227 
228   android::sp<IBluetoothAvrcpControlCallback> cb(
229       static_cast<IBluetoothAvrcpControlCallback*>(callback.get()));
230   cb->OnRegistered(status, (status == bluetooth::BLE_STATUS_SUCCESS)
231                                ? instance->GetInstanceId()
232                                : kInvalidInstanceId);
233 }
234 
235 }  // namespace binder
236 }  // namespace ipc
237