Home | History | Annotate | Download | only in binder
      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_gatt_client_binder_server.h"
     22 #include "service/ipc/binder/bluetooth_gatt_server_binder_server.h"
     23 #include "service/ipc/binder/bluetooth_le_advertiser_binder_server.h"
     24 #include "service/ipc/binder/bluetooth_le_scanner_binder_server.h"
     25 #include "service/ipc/binder/bluetooth_low_energy_binder_server.h"
     26 
     27 #include "service/hal/bluetooth_interface.h"
     28 
     29 using android::sp;
     30 using android::String8;
     31 using android::String16;
     32 
     33 using android::bluetooth::IBluetoothCallback;
     34 using android::bluetooth::IBluetoothGattClient;
     35 using android::bluetooth::IBluetoothGattServer;
     36 
     37 namespace ipc {
     38 namespace binder {
     39 
     40 BluetoothBinderServer::BluetoothBinderServer(bluetooth::Adapter* adapter)
     41     : adapter_(adapter) {
     42   CHECK(adapter_);
     43   adapter_->AddObserver(this);
     44 }
     45 
     46 BluetoothBinderServer::~BluetoothBinderServer() {
     47   adapter_->RemoveObserver(this);
     48 }
     49 
     50 // binder::BnBluetooth overrides:
     51 Status BluetoothBinderServer::IsEnabled(bool* _aidl_return) {
     52   VLOG(2) << __func__;
     53   *_aidl_return = adapter_->IsEnabled();
     54   return Status::ok();
     55 }
     56 
     57 Status BluetoothBinderServer::GetState(int32_t* _aidl_return) {
     58   VLOG(2) << __func__;
     59   *_aidl_return = adapter_->GetState();
     60   return Status::ok();
     61 }
     62 
     63 Status BluetoothBinderServer::Enable(bool start_restricted,
     64                                      bool* _aidl_return) {
     65   VLOG(2) << __func__;
     66   *_aidl_return = adapter_->Enable(start_restricted);
     67   return Status::ok();
     68 }
     69 
     70 Status BluetoothBinderServer::EnableNoAutoConnect(bool* _aidl_return) {
     71   VLOG(2) << __func__;
     72   // TODO(armansito): Implement.
     73   *_aidl_return = false;
     74   return Status::ok();
     75 }
     76 
     77 Status BluetoothBinderServer::Disable(bool* _aidl_return) {
     78   VLOG(2) << __func__;
     79   *_aidl_return = adapter_->Disable();
     80   return Status::ok();
     81 }
     82 
     83 Status BluetoothBinderServer::GetAddress(::android::String16* _aidl_return) {
     84   VLOG(2) << __func__;
     85   *_aidl_return = String16(String8(adapter_->GetAddress().c_str()));
     86   return Status::ok();
     87 }
     88 
     89 Status BluetoothBinderServer::GetUUIDs(
     90     ::std::vector<::android::bluetooth::UUID>* _aidl_return) {
     91   VLOG(2) << __func__;
     92   // TODO(armansito): Implement.
     93   *_aidl_return = std::vector<android::bluetooth::UUID>();
     94   return Status::ok();
     95 }
     96 
     97 Status BluetoothBinderServer::SetName(const ::android::String16& name,
     98                                       bool* _aidl_return) {
     99   VLOG(2) << __func__;
    100   *_aidl_return = adapter_->SetName(std::string(String8(name).string()));
    101   return Status::ok();
    102 }
    103 
    104 Status BluetoothBinderServer::GetName(::android::String16* _aidl_return) {
    105   VLOG(2) << __func__;
    106   *_aidl_return = String16(String8(adapter_->GetName().c_str()));
    107   return Status::ok();
    108 }
    109 
    110 Status BluetoothBinderServer::RegisterCallback(
    111     const ::android::sp<IBluetoothCallback>& callback) {
    112   VLOG(2) << __func__;
    113   if (!callback.get()) {
    114     LOG(ERROR) << "RegisterCallback called with NULL binder. Ignoring.";
    115     return Status::ok();
    116   }
    117   callbacks_.Register(callback);
    118   return Status::ok();
    119 }
    120 
    121 Status BluetoothBinderServer::UnregisterCallback(
    122     const ::android::sp<IBluetoothCallback>& callback) {
    123   VLOG(2) << __func__;
    124   if (!callback.get()) {
    125     LOG(ERROR) << "UnregisterCallback called with NULL binder. Ignoring.";
    126     return Status::ok();
    127   }
    128   callbacks_.Unregister(callback);
    129   return Status::ok();
    130 }
    131 
    132 Status BluetoothBinderServer::IsMultiAdvertisementSupported(
    133     bool* _aidl_return) {
    134   VLOG(2) << __func__;
    135   *_aidl_return = adapter_->IsMultiAdvertisementSupported();
    136   return Status::ok();
    137 }
    138 
    139 Status BluetoothBinderServer::GetLowEnergyInterface(
    140     ::android::sp<IBluetoothLowEnergy>* _aidl_return) {
    141   VLOG(2) << __func__;
    142 
    143   if (!adapter_->IsEnabled()) {
    144     LOG(ERROR) << "Cannot obtain IBluetoothLowEnergy interface while disabled";
    145     *_aidl_return = NULL;
    146     return Status::ok();
    147   }
    148 
    149   if (!low_energy_interface_.get())
    150     low_energy_interface_ = new BluetoothLowEnergyBinderServer(adapter_);
    151 
    152   *_aidl_return = low_energy_interface_;
    153   return Status::ok();
    154 }
    155 
    156 Status BluetoothBinderServer::GetLeAdvertiserInterface(
    157     ::android::sp<IBluetoothLeAdvertiser>* _aidl_return) {
    158   VLOG(2) << __func__;
    159 
    160   if (!adapter_->IsEnabled()) {
    161     LOG(ERROR)
    162         << "Cannot obtain IBluetoothLeAdvertiser interface while disabled";
    163     *_aidl_return = NULL;
    164     return Status::ok();
    165   }
    166 
    167   if (!le_advertiser_interface_.get())
    168     le_advertiser_interface_ = new BluetoothLeAdvertiserBinderServer(adapter_);
    169 
    170   *_aidl_return = le_advertiser_interface_;
    171   return Status::ok();
    172 }
    173 
    174 Status BluetoothBinderServer::GetLeScannerInterface(
    175     ::android::sp<IBluetoothLeScanner>* _aidl_return) {
    176   VLOG(2) << __func__;
    177 
    178   if (!adapter_->IsEnabled()) {
    179     LOG(ERROR) << "Cannot obtain IBluetoothLeScanner interface while disabled";
    180     *_aidl_return = NULL;
    181     return Status::ok();
    182   }
    183 
    184   if (!le_scanner_interface_.get())
    185     le_scanner_interface_ = new BluetoothLeScannerBinderServer(adapter_);
    186 
    187   *_aidl_return = le_scanner_interface_;
    188   return Status::ok();
    189 }
    190 
    191 Status BluetoothBinderServer::GetGattClientInterface(
    192     ::android::sp<IBluetoothGattClient>* _aidl_return) {
    193   VLOG(2) << __func__;
    194 
    195   if (!adapter_->IsEnabled()) {
    196     LOG(ERROR) << "Cannot obtain IBluetoothGattClient interface while disabled";
    197     *_aidl_return = NULL;
    198     return Status::ok();
    199   }
    200 
    201   if (!gatt_client_interface_.get())
    202     gatt_client_interface_ = new BluetoothGattClientBinderServer(adapter_);
    203 
    204   *_aidl_return = gatt_client_interface_;
    205   return Status::ok();
    206 }
    207 
    208 Status BluetoothBinderServer::GetGattServerInterface(
    209     ::android::sp<IBluetoothGattServer>* _aidl_return) {
    210   VLOG(2) << __func__;
    211 
    212   if (!adapter_->IsEnabled()) {
    213     LOG(ERROR) << "Cannot obtain IBluetoothGattServer interface while disabled";
    214     *_aidl_return = NULL;
    215     return Status::ok();
    216   }
    217 
    218   if (!gatt_server_interface_.get())
    219     gatt_server_interface_ = new BluetoothGattServerBinderServer(adapter_);
    220 
    221   *_aidl_return = gatt_server_interface_;
    222   return Status::ok();
    223 }
    224 
    225 android::status_t BluetoothBinderServer::dump(
    226     int fd, const android::Vector<android::String16>& args) {
    227   VLOG(2) << __func__ << " called with fd " << fd;
    228   if (args.size() > 0) {
    229     // TODO (jamuraa): Parse arguments and switch on --proto, --proto_text
    230     for (const auto& x : args) {
    231       VLOG(2) << __func__ << "argument: " << x.string();
    232     }
    233   }
    234   // TODO (jamuraa): enumerate profiles and dump profile information
    235   const bt_interface_t* iface =
    236       bluetooth::hal::BluetoothInterface::Get()->GetHALInterface();
    237   iface->dump(fd, NULL);
    238   return android::NO_ERROR;
    239 }
    240 
    241 void BluetoothBinderServer::OnAdapterStateChanged(
    242     bluetooth::Adapter* adapter, bluetooth::AdapterState prev_state,
    243     bluetooth::AdapterState new_state) {
    244   CHECK_EQ(adapter, adapter_);
    245   VLOG(2) << "Received adapter state update - prev: " << prev_state
    246           << " new: " << new_state;
    247   callbacks_.ForEach([prev_state, new_state](IBluetoothCallback* callback) {
    248     callback->OnBluetoothStateChange(prev_state, new_state);
    249   });
    250 }
    251 
    252 }  // namespace binder
    253 }  // namespace ipc
    254