• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2015 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/ipc/binder/bluetooth_binder_server.h"
18 
19 #include <base/logging.h>
20 
21 #include "service/ipc/binder/bluetooth_a2dp_sink_binder_server.h"
22 #include "service/ipc/binder/bluetooth_a2dp_source_binder_server.h"
23 #include "service/ipc/binder/bluetooth_avrcp_control_binder_server.h"
24 #include "service/ipc/binder/bluetooth_avrcp_target_binder_server.h"
25 #include "service/ipc/binder/bluetooth_gatt_client_binder_server.h"
26 #include "service/ipc/binder/bluetooth_gatt_server_binder_server.h"
27 #include "service/ipc/binder/bluetooth_le_advertiser_binder_server.h"
28 #include "service/ipc/binder/bluetooth_le_scanner_binder_server.h"
29 #include "service/ipc/binder/bluetooth_low_energy_binder_server.h"
30 
31 #include "service/hal/bluetooth_interface.h"
32 
33 using android::sp;
34 using android::String8;
35 using android::String16;
36 
37 using android::bluetooth::IBluetoothCallback;
38 using android::bluetooth::IBluetoothGattClient;
39 using android::bluetooth::IBluetoothGattServer;
40 
41 namespace ipc {
42 namespace binder {
43 
BluetoothBinderServer(bluetooth::Adapter * adapter)44 BluetoothBinderServer::BluetoothBinderServer(bluetooth::Adapter* adapter)
45     : adapter_(adapter) {
46   CHECK(adapter_);
47   adapter_->AddObserver(this);
48 }
49 
~BluetoothBinderServer()50 BluetoothBinderServer::~BluetoothBinderServer() {
51   adapter_->RemoveObserver(this);
52 }
53 
54 // binder::BnBluetooth overrides:
IsEnabled(bool * _aidl_return)55 Status BluetoothBinderServer::IsEnabled(bool* _aidl_return) {
56   VLOG(2) << __func__;
57   *_aidl_return = adapter_->IsEnabled();
58   return Status::ok();
59 }
60 
GetState(int32_t * _aidl_return)61 Status BluetoothBinderServer::GetState(int32_t* _aidl_return) {
62   VLOG(2) << __func__;
63   *_aidl_return = adapter_->GetState();
64   return Status::ok();
65 }
66 
Enable(bool * _aidl_return)67 Status BluetoothBinderServer::Enable(bool* _aidl_return) {
68   VLOG(2) << __func__;
69   *_aidl_return = adapter_->Enable();
70   return Status::ok();
71 }
72 
EnableNoAutoConnect(bool * _aidl_return)73 Status BluetoothBinderServer::EnableNoAutoConnect(bool* _aidl_return) {
74   VLOG(2) << __func__;
75   // TODO(armansito): Implement.
76   *_aidl_return = false;
77   return Status::ok();
78 }
79 
Disable(bool * _aidl_return)80 Status BluetoothBinderServer::Disable(bool* _aidl_return) {
81   VLOG(2) << __func__;
82   *_aidl_return = adapter_->Disable();
83   return Status::ok();
84 }
85 
GetAddress(::android::String16 * _aidl_return)86 Status BluetoothBinderServer::GetAddress(::android::String16* _aidl_return) {
87   VLOG(2) << __func__;
88   *_aidl_return = String16(String8(adapter_->GetAddress().c_str()));
89   return Status::ok();
90 }
91 
GetUUIDs(::std::vector<::android::bluetooth::UUID> * _aidl_return)92 Status BluetoothBinderServer::GetUUIDs(
93     ::std::vector<::android::bluetooth::UUID>* _aidl_return) {
94   VLOG(2) << __func__;
95   // TODO(armansito): Implement.
96   *_aidl_return = std::vector<android::bluetooth::UUID>();
97   return Status::ok();
98 }
99 
SetName(const::android::String16 & name,bool * _aidl_return)100 Status BluetoothBinderServer::SetName(const ::android::String16& name,
101                                       bool* _aidl_return) {
102   VLOG(2) << __func__;
103   *_aidl_return = adapter_->SetName(std::string(String8(name).string()));
104   return Status::ok();
105 }
106 
GetName(::android::String16 * _aidl_return)107 Status BluetoothBinderServer::GetName(::android::String16* _aidl_return) {
108   VLOG(2) << __func__;
109   *_aidl_return = String16(String8(adapter_->GetName().c_str()));
110   return Status::ok();
111 }
112 
SetScanMode(int32_t scan_mode,bool * _aidl_return)113 Status BluetoothBinderServer::SetScanMode(int32_t scan_mode,
114                                           bool* _aidl_return) {
115   VLOG(2) << __func__;
116   *_aidl_return = adapter_->SetScanMode(scan_mode);
117   return Status::ok();
118 }
119 
SetScanEnable(bool scan_enable,bool * _aidl_return)120 Status BluetoothBinderServer::SetScanEnable(bool scan_enable,
121                                             bool* _aidl_return) {
122   VLOG(2) << __func__;
123   *_aidl_return = adapter_->SetScanEnable(scan_enable);
124   return Status::ok();
125 }
126 
SspReply(const::android::String16 & device_address,int32_t variant,bool accept,int32_t passkey,bool * _aidl_return)127 Status BluetoothBinderServer::SspReply(
128     const ::android::String16& device_address, int32_t variant, bool accept,
129     int32_t passkey, bool* _aidl_return) {
130   VLOG(2) << __func__;
131   *_aidl_return = adapter_->SspReply(String8(device_address).string(), variant,
132                                      accept, passkey);
133   return Status::ok();
134 }
135 
CreateBond(const::android::String16 & device_address,int32_t transport,bool * _aidl_return)136 Status BluetoothBinderServer::CreateBond(
137     const ::android::String16& device_address, int32_t transport,
138     bool* _aidl_return) {
139   VLOG(2) << __func__;
140   *_aidl_return =
141       adapter_->CreateBond(String8(device_address).string(), transport);
142   return Status::ok();
143 }
144 
GetBondedDevices(bool * _aidl_return)145 Status BluetoothBinderServer::GetBondedDevices(bool* _aidl_return) {
146   VLOG(2) << __func__;
147   *_aidl_return = adapter_->GetBondedDevices();
148   return Status::ok();
149 }
150 
RemoveBond(const::android::String16 & device_address,bool * _aidl_return)151 Status BluetoothBinderServer::RemoveBond(
152     const ::android::String16& device_address, bool* _aidl_return) {
153   VLOG(2) << __func__;
154   *_aidl_return = adapter_->RemoveBond(String8(device_address).string());
155   return Status::ok();
156 }
157 
GetRemoteDeviceProperties(const::android::String16 & device_address,bool * _aidl_return)158 Status BluetoothBinderServer::GetRemoteDeviceProperties(
159     const ::android::String16& device_address, bool* _aidl_return) {
160   VLOG(2) << __func__;
161   *_aidl_return =
162       adapter_->GetRemoteDeviceProperties(String8(device_address).string());
163   return Status::ok();
164 }
165 
RegisterCallback(const::android::sp<IBluetoothCallback> & callback)166 Status BluetoothBinderServer::RegisterCallback(
167     const ::android::sp<IBluetoothCallback>& callback) {
168   VLOG(2) << __func__;
169   if (!callback.get()) {
170     LOG(ERROR) << "RegisterCallback called with NULL binder. Ignoring.";
171     return Status::ok();
172   }
173   callbacks_.Register(callback);
174   return Status::ok();
175 }
176 
UnregisterCallback(const::android::sp<IBluetoothCallback> & callback)177 Status BluetoothBinderServer::UnregisterCallback(
178     const ::android::sp<IBluetoothCallback>& callback) {
179   VLOG(2) << __func__;
180   if (!callback.get()) {
181     LOG(ERROR) << "UnregisterCallback called with NULL binder. Ignoring.";
182     return Status::ok();
183   }
184   callbacks_.Unregister(callback);
185   return Status::ok();
186 }
187 
IsMultiAdvertisementSupported(bool * _aidl_return)188 Status BluetoothBinderServer::IsMultiAdvertisementSupported(
189     bool* _aidl_return) {
190   VLOG(2) << __func__;
191   *_aidl_return = adapter_->IsMultiAdvertisementSupported();
192   return Status::ok();
193 }
194 
GetA2dpSinkInterface(::android::sp<IBluetoothA2dpSink> * _aidl_return)195 Status BluetoothBinderServer::GetA2dpSinkInterface(
196     ::android::sp<IBluetoothA2dpSink>* _aidl_return) {
197   VLOG(2) << __func__;
198 
199   if (!adapter_->IsEnabled()) {
200     LOG(ERROR) << "Cannot obtain IBluetoothA2dpSink interface while disabled";
201     *_aidl_return = nullptr;
202     return Status::ok();
203   }
204 
205   if (!a2dp_sink_interface_.get())
206     a2dp_sink_interface_ = new BluetoothA2dpSinkBinderServer(adapter_);
207 
208   if (a2dp_sink_interface_->HasInstance()) {
209     LOG(ERROR) << "Only one A2dpSink interface allowed at a time";
210     *_aidl_return = nullptr;
211     return Status::ok();
212   }
213 
214   *_aidl_return = a2dp_sink_interface_;
215   return Status::ok();
216 }
217 
GetA2dpSourceInterface(::android::sp<IBluetoothA2dpSource> * _aidl_return)218 Status BluetoothBinderServer::GetA2dpSourceInterface(
219     ::android::sp<IBluetoothA2dpSource>* _aidl_return) {
220   VLOG(2) << __func__;
221 
222   if (!adapter_->IsEnabled()) {
223     LOG(ERROR) << "Cannot obtain IBluetoothA2dpSource interface while disabled";
224     *_aidl_return = nullptr;
225     return Status::ok();
226   }
227 
228   if (!a2dp_source_interface_.get())
229     a2dp_source_interface_ = new BluetoothA2dpSourceBinderServer(adapter_);
230 
231   if (a2dp_source_interface_->HasInstance()) {
232     LOG(ERROR) << "Only one A2dpSource interface allowed at a time";
233     *_aidl_return = nullptr;
234     return Status::ok();
235   }
236 
237   *_aidl_return = a2dp_source_interface_;
238   return Status::ok();
239 }
240 
GetLowEnergyInterface(::android::sp<IBluetoothLowEnergy> * _aidl_return)241 Status BluetoothBinderServer::GetLowEnergyInterface(
242     ::android::sp<IBluetoothLowEnergy>* _aidl_return) {
243   VLOG(2) << __func__;
244 
245   if (!adapter_->IsEnabled()) {
246     LOG(ERROR) << "Cannot obtain IBluetoothLowEnergy interface while disabled";
247     *_aidl_return = NULL;
248     return Status::ok();
249   }
250 
251   if (!low_energy_interface_.get())
252     low_energy_interface_ = new BluetoothLowEnergyBinderServer(adapter_);
253 
254   *_aidl_return = low_energy_interface_;
255   return Status::ok();
256 }
257 
GetLeAdvertiserInterface(::android::sp<IBluetoothLeAdvertiser> * _aidl_return)258 Status BluetoothBinderServer::GetLeAdvertiserInterface(
259     ::android::sp<IBluetoothLeAdvertiser>* _aidl_return) {
260   VLOG(2) << __func__;
261 
262   if (!adapter_->IsEnabled()) {
263     LOG(ERROR)
264         << "Cannot obtain IBluetoothLeAdvertiser interface while disabled";
265     *_aidl_return = NULL;
266     return Status::ok();
267   }
268 
269   if (!le_advertiser_interface_.get())
270     le_advertiser_interface_ = new BluetoothLeAdvertiserBinderServer(adapter_);
271 
272   *_aidl_return = le_advertiser_interface_;
273   return Status::ok();
274 }
275 
GetLeScannerInterface(::android::sp<IBluetoothLeScanner> * _aidl_return)276 Status BluetoothBinderServer::GetLeScannerInterface(
277     ::android::sp<IBluetoothLeScanner>* _aidl_return) {
278   VLOG(2) << __func__;
279 
280   if (!adapter_->IsEnabled()) {
281     LOG(ERROR) << "Cannot obtain IBluetoothLeScanner interface while disabled";
282     *_aidl_return = NULL;
283     return Status::ok();
284   }
285 
286   if (!le_scanner_interface_.get())
287     le_scanner_interface_ = new BluetoothLeScannerBinderServer(adapter_);
288 
289   *_aidl_return = le_scanner_interface_;
290   return Status::ok();
291 }
292 
GetGattClientInterface(::android::sp<IBluetoothGattClient> * _aidl_return)293 Status BluetoothBinderServer::GetGattClientInterface(
294     ::android::sp<IBluetoothGattClient>* _aidl_return) {
295   VLOG(2) << __func__;
296 
297   if (!adapter_->IsEnabled()) {
298     LOG(ERROR) << "Cannot obtain IBluetoothGattClient interface while disabled";
299     *_aidl_return = NULL;
300     return Status::ok();
301   }
302 
303   if (!gatt_client_interface_.get())
304     gatt_client_interface_ = new BluetoothGattClientBinderServer(adapter_);
305 
306   *_aidl_return = gatt_client_interface_;
307   return Status::ok();
308 }
309 
GetGattServerInterface(::android::sp<IBluetoothGattServer> * _aidl_return)310 Status BluetoothBinderServer::GetGattServerInterface(
311     ::android::sp<IBluetoothGattServer>* _aidl_return) {
312   VLOG(2) << __func__;
313 
314   if (!adapter_->IsEnabled()) {
315     LOG(ERROR) << "Cannot obtain IBluetoothGattServer interface while disabled";
316     *_aidl_return = NULL;
317     return Status::ok();
318   }
319 
320   if (!gatt_server_interface_.get())
321     gatt_server_interface_ = new BluetoothGattServerBinderServer(adapter_);
322 
323   *_aidl_return = gatt_server_interface_;
324   return Status::ok();
325 }
326 
GetAvrcpControlInterface(::android::sp<IBluetoothAvrcpControl> * _aidl_return)327 Status BluetoothBinderServer::GetAvrcpControlInterface(
328     ::android::sp<IBluetoothAvrcpControl>* _aidl_return) {
329   VLOG(2) << __func__;
330 
331   if (!adapter_->IsEnabled()) {
332     LOG(ERROR)
333         << "Cannot obtain IBluetoothAvrcpControl interface while disabled";
334     *_aidl_return = NULL;
335     return Status::ok();
336   }
337 
338   if (!avrcp_control_interface_.get())
339     avrcp_control_interface_ = new BluetoothAvrcpControlBinderServer(adapter_);
340 
341   *_aidl_return = avrcp_control_interface_;
342   return Status::ok();
343 }
344 
GetAvrcpTargetInterface(::android::sp<IBluetoothAvrcpTarget> * _aidl_return)345 Status BluetoothBinderServer::GetAvrcpTargetInterface(
346     ::android::sp<IBluetoothAvrcpTarget>* _aidl_return) {
347   VLOG(2) << __func__;
348 
349   if (!adapter_->IsEnabled()) {
350     LOG(ERROR)
351         << "Cannot obtain IBluetoothAvrcpTarget interface while disabled";
352     *_aidl_return = NULL;
353     return Status::ok();
354   }
355 
356   if (!avrcp_target_interface_.get())
357     avrcp_target_interface_ = new BluetoothAvrcpTargetBinderServer(adapter_);
358 
359   if (avrcp_target_interface_->HasInstance()) {
360     LOG(ERROR) << "Only one AVRCP target interface allowed at a time";
361     *_aidl_return = nullptr;
362     return Status::ok();
363   }
364 
365   *_aidl_return = avrcp_target_interface_;
366   return Status::ok();
367 }
368 
dump(int fd,const android::Vector<android::String16> & args)369 android::status_t BluetoothBinderServer::dump(
370     int fd, const android::Vector<android::String16>& args) {
371   VLOG(2) << __func__ << " called with fd " << fd;
372   if (args.size() > 0) {
373     // TODO (jamuraa): Parse arguments and switch on --proto, --proto_text
374     for (const auto& x : args) {
375       VLOG(2) << __func__ << "argument: " << x.string();
376     }
377   }
378   // TODO (jamuraa): enumerate profiles and dump profile information
379   const bt_interface_t* iface =
380       bluetooth::hal::BluetoothInterface::Get()->GetHALInterface();
381   iface->dump(fd, NULL);
382   return android::NO_ERROR;
383 }
384 
OnAdapterStateChanged(bluetooth::Adapter * adapter,bluetooth::AdapterState prev_state,bluetooth::AdapterState new_state)385 void BluetoothBinderServer::OnAdapterStateChanged(
386     bluetooth::Adapter* adapter, bluetooth::AdapterState prev_state,
387     bluetooth::AdapterState new_state) {
388   CHECK_EQ(adapter, adapter_);
389   VLOG(2) << "Received adapter state update - prev: " << prev_state
390           << " new: " << new_state;
391   callbacks_.ForEach([prev_state, new_state](IBluetoothCallback* callback) {
392     callback->OnBluetoothStateChange(prev_state, new_state);
393   });
394 }
395 
OnDeviceConnectionStateChanged(bluetooth::Adapter * adapter,const std::string & device_address,bool connected)396 void BluetoothBinderServer::OnDeviceConnectionStateChanged(
397     bluetooth::Adapter* adapter, const std::string& device_address,
398     bool connected) {
399   CHECK_EQ(adapter, adapter_);
400   auto addr_s16 = String16(device_address.c_str(), device_address.size());
401   callbacks_.ForEach([&addr_s16, connected](IBluetoothCallback* callback) {
402     callback->OnDeviceConnectionStateChanged(addr_s16, connected);
403   });
404 }
405 
OnScanEnableChanged(bluetooth::Adapter * adapter,bool scan_enabled)406 void BluetoothBinderServer::OnScanEnableChanged(bluetooth::Adapter* adapter,
407                                                 bool scan_enabled) {
408   CHECK_EQ(adapter, adapter_);
409   callbacks_.ForEach([scan_enabled](IBluetoothCallback* callback) {
410     callback->OnScanEnableChanged(scan_enabled);
411   });
412 }
413 
OnSspRequest(bluetooth::Adapter * adapter,const std::string & device_address,const std::string & device_name,int cod,int pairing_variant,int pass_key)414 void BluetoothBinderServer::OnSspRequest(bluetooth::Adapter* adapter,
415                                          const std::string& device_address,
416                                          const std::string& device_name,
417                                          int cod, int pairing_variant,
418                                          int pass_key) {
419   CHECK_EQ(adapter, adapter_);
420   VLOG(2) << "Received ssp request: device_address: " << device_address
421           << ", device_name: " << device_name << ", cod: " << cod
422           << ", pairing_variant: " << pairing_variant
423           << ", pass_key: " << pass_key;
424 
425   android::String16 addr_s16(device_address.c_str());
426   android::String16 name_s16(device_name.c_str());
427   callbacks_.ForEach([&addr_s16, &name_s16, cod, pairing_variant,
428                       pass_key](IBluetoothCallback* callback) {
429     callback->OnSspRequest(addr_s16, name_s16, cod, pairing_variant, pass_key);
430   });
431 }
432 
OnBondStateChanged(bluetooth::Adapter * adapter,int status,const std::string & device_address,int state)433 void BluetoothBinderServer::OnBondStateChanged(
434     bluetooth::Adapter* adapter, int status, const std::string& device_address,
435     int state) {
436   CHECK_EQ(adapter, adapter_);
437   VLOG(2) << "Received " << __func__ << " "
438           << "status: " << status << ", device_address: " << device_address
439           << ", state: " << state;
440   android::String16 addr_s16(device_address.c_str(), device_address.size());
441   callbacks_.ForEach([status, &addr_s16, state](IBluetoothCallback* callback) {
442     callback->OnBondStateChanged(status, addr_s16, state);
443   });
444 }
445 
OnGetBondedDevices(bluetooth::Adapter * adapter,int status,const std::vector<std::string> & bonded_devices)446 void BluetoothBinderServer::OnGetBondedDevices(
447     bluetooth::Adapter* adapter, int status,
448     const std::vector<std::string>& bonded_devices) {
449   CHECK_EQ(adapter, adapter_);
450   VLOG(2) << "Received " << __func__;
451   std::vector<android::String16> devices_s16;
452   devices_s16.reserve(bonded_devices.size());
453   for (const auto& device : bonded_devices)
454     devices_s16.emplace_back(device.c_str(), device.size());
455 
456   callbacks_.ForEach([status, &devices_s16](IBluetoothCallback* callback) {
457     callback->OnGetBondedDevices(status, devices_s16);
458   });
459 }
460 
OnGetRemoteDeviceProperties(bluetooth::Adapter * adapter,int status,const std::string & device_address,const bluetooth::RemoteDeviceProps & properties)461 void BluetoothBinderServer::OnGetRemoteDeviceProperties(
462     bluetooth::Adapter* adapter, int status, const std::string& device_address,
463     const bluetooth::RemoteDeviceProps& properties) {
464   CHECK_EQ(adapter, adapter_);
465   VLOG(2) << "Received " << __func__ << " "
466           << "status: " << status << ", device_address: " << device_address;
467   android::String16 addr_s16(device_address.c_str(), device_address.size());
468   auto binder_props =
469       android::bluetooth::BluetoothRemoteDeviceProps(properties);
470   callbacks_.ForEach(
471       [status, &addr_s16, &binder_props](IBluetoothCallback* callback) {
472         callback->OnGetRemoteDeviceProperties(status, addr_s16, binder_props);
473       });
474 }
475 
OnDeviceFound(bluetooth::Adapter * adapter,const bluetooth::RemoteDeviceProps & properties)476 void BluetoothBinderServer::OnDeviceFound(
477     bluetooth::Adapter* adapter,
478     const bluetooth::RemoteDeviceProps& properties) {
479   CHECK_EQ(adapter, adapter_);
480   VLOG(2) << "Received " << __func__ << " ";
481   auto binder_props =
482       android::bluetooth::BluetoothRemoteDeviceProps(properties);
483   callbacks_.ForEach([&binder_props](IBluetoothCallback* callback) {
484     callback->OnDeviceFound(binder_props);
485   });
486 }
487 
488 }  // namespace binder
489 }  // namespace ipc
490