Home | History | Annotate | Download | only in binder
      1 //
      2 //  Copyright (C) 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_low_energy_binder_server.h"
     24 
     25 #include "service/hal/bluetooth_interface.h"
     26 
     27 using android::sp;
     28 
     29 namespace ipc {
     30 namespace binder {
     31 
     32 BluetoothBinderServer::BluetoothBinderServer(bluetooth::Adapter* adapter)
     33     : adapter_(adapter) {
     34   CHECK(adapter_);
     35   adapter_->AddObserver(this);
     36 }
     37 
     38 BluetoothBinderServer::~BluetoothBinderServer() {
     39   adapter_->RemoveObserver(this);
     40 }
     41 
     42 // binder::BnBluetooth overrides:
     43 bool BluetoothBinderServer::IsEnabled() {
     44   VLOG(2) << __func__;
     45   return adapter_->IsEnabled();
     46 }
     47 
     48 int BluetoothBinderServer::GetState() {
     49   VLOG(2) << __func__;
     50   return adapter_->GetState();
     51 }
     52 
     53 bool BluetoothBinderServer::Enable(bool start_restricted) {
     54   VLOG(2) << __func__;
     55   return adapter_->Enable(start_restricted);
     56 }
     57 
     58 bool BluetoothBinderServer::EnableNoAutoConnect() {
     59   VLOG(2) << __func__;
     60   // TODO(armansito): Implement.
     61   return false;
     62 }
     63 
     64 bool BluetoothBinderServer::Disable() {
     65   VLOG(2) << __func__;
     66   return adapter_->Disable();
     67 }
     68 
     69 std::string BluetoothBinderServer::GetAddress() {
     70   VLOG(2) << __func__;
     71   return adapter_->GetAddress();
     72 }
     73 
     74 std::vector<bluetooth::UUID> BluetoothBinderServer::GetUUIDs() {
     75   VLOG(2) << __func__;
     76   // TODO(armansito): Implement.
     77   return std::vector<bluetooth::UUID>();
     78 }
     79 
     80 bool BluetoothBinderServer::SetName(const std::string& name) {
     81   VLOG(2) << __func__;
     82   return adapter_->SetName(name);
     83 }
     84 
     85 std::string BluetoothBinderServer::GetName() {
     86   VLOG(2) << __func__;
     87   return adapter_->GetName();
     88 }
     89 
     90 void BluetoothBinderServer::RegisterCallback(
     91     const sp<IBluetoothCallback>& callback) {
     92   VLOG(2) << __func__;
     93   if (!callback.get() ) {
     94     LOG(ERROR) << "RegisterCallback called with NULL binder. Ignoring.";
     95     return;
     96   }
     97   callbacks_.Register(callback);
     98 }
     99 
    100 void BluetoothBinderServer::UnregisterCallback(
    101     const sp<IBluetoothCallback>& callback) {
    102   VLOG(2) << __func__;
    103   if (!callback.get() ) {
    104     LOG(ERROR) << "UnregisterCallback called with NULL binder. Ignoring.";
    105     return;
    106   }
    107   callbacks_.Unregister(callback);
    108 }
    109 
    110 bool BluetoothBinderServer::IsMultiAdvertisementSupported() {
    111   VLOG(2) << __func__;
    112   return adapter_->IsMultiAdvertisementSupported();
    113 }
    114 
    115 sp<IBluetoothLowEnergy>
    116 BluetoothBinderServer::GetLowEnergyInterface() {
    117   VLOG(2) << __func__;
    118 
    119   if (!adapter_->IsEnabled()) {
    120     LOG(ERROR) << "Cannot obtain IBluetoothLowEnergy interface while disabled";
    121     return nullptr;
    122   }
    123 
    124   if (!low_energy_interface_.get())
    125     low_energy_interface_ = new BluetoothLowEnergyBinderServer(adapter_);
    126 
    127   return low_energy_interface_;
    128 }
    129 
    130 sp<IBluetoothGattClient>
    131 BluetoothBinderServer::GetGattClientInterface() {
    132   VLOG(2) << __func__;
    133 
    134   if (!adapter_->IsEnabled()) {
    135     LOG(ERROR) << "Cannot obtain IBluetoothGattClient interface while disabled";
    136     return nullptr;
    137   }
    138 
    139   if (!gatt_client_interface_.get())
    140     gatt_client_interface_ = new BluetoothGattClientBinderServer(adapter_);
    141 
    142   return gatt_client_interface_;
    143 }
    144 
    145 sp<IBluetoothGattServer>
    146 BluetoothBinderServer::GetGattServerInterface() {
    147   VLOG(2) << __func__;
    148 
    149   if (!adapter_->IsEnabled()) {
    150     LOG(ERROR) << "Cannot obtain IBluetoothGattServer interface while disabled";
    151     return nullptr;
    152   }
    153 
    154   if (!gatt_server_interface_.get())
    155     gatt_server_interface_ = new BluetoothGattServerBinderServer(adapter_);
    156 
    157   return gatt_server_interface_;
    158 }
    159 
    160 android::status_t BluetoothBinderServer::dump(int fd, const android::Vector<android::String16>& args) {
    161   VLOG(2) << __func__ << " called with fd " << fd;
    162   if  (args.size() > 0) {
    163     // TODO (jamuraa): Parse arguments and switch on --proto, --proto_text
    164     for (auto x : args) {
    165       VLOG(2) << __func__ << "argument: " << x.string();
    166     }
    167   }
    168   // TODO (jamuraa): enumerate profiles and dump profile information
    169   const bt_interface_t *iface = bluetooth::hal::BluetoothInterface::Get()->GetHALInterface();
    170   iface->dump(fd, NULL);
    171   return android::NO_ERROR;
    172 }
    173 
    174 void BluetoothBinderServer::OnAdapterStateChanged(
    175     bluetooth::Adapter* adapter,
    176     bluetooth::AdapterState prev_state,
    177     bluetooth::AdapterState new_state) {
    178   CHECK_EQ(adapter, adapter_);
    179   VLOG(2) << "Received adapter state update - prev: " << prev_state
    180           << " new: " << new_state;
    181   callbacks_.ForEach([prev_state, new_state](IBluetoothCallback* callback) {
    182     callback->OnBluetoothStateChange(prev_state, new_state);
    183   });
    184 }
    185 
    186 }  // namespace binder
    187 }  // namespace ipc
    188