Home | History | Annotate | Download | only in binder
      1 //
      2 //  Copyright 2016 The Android Open Source Project
      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_le_scanner_binder_server.h"
     18 
     19 #include <base/logging.h>
     20 
     21 #include "service/adapter.h"
     22 
     23 using android::String8;
     24 using android::String16;
     25 using android::binder::Status;
     26 
     27 namespace ipc {
     28 namespace binder {
     29 
     30 namespace {
     31 const int kInvalidInstanceId = -1;
     32 }  // namespace
     33 
     34 BluetoothLeScannerBinderServer::BluetoothLeScannerBinderServer(
     35     bluetooth::Adapter* adapter)
     36     : adapter_(adapter) {
     37   CHECK(adapter_);
     38 }
     39 
     40 BluetoothLeScannerBinderServer::~BluetoothLeScannerBinderServer() {}
     41 
     42 Status BluetoothLeScannerBinderServer::RegisterScanner(
     43     const android::sp<IBluetoothLeScannerCallback>& callback,
     44     bool* _aidl_return) {
     45   VLOG(2) << __func__;
     46   bluetooth::LowEnergyScannerFactory* ble_factory =
     47       adapter_->GetLeScannerFactory();
     48 
     49   *_aidl_return = RegisterInstanceBase(callback, ble_factory);
     50   return Status::ok();
     51 }
     52 
     53 Status BluetoothLeScannerBinderServer::UnregisterScanner(int scanner_id) {
     54   VLOG(2) << __func__;
     55   UnregisterInstanceBase(scanner_id);
     56   return Status::ok();
     57 }
     58 
     59 Status BluetoothLeScannerBinderServer::UnregisterAll() {
     60   VLOG(2) << __func__;
     61   UnregisterAllBase();
     62   return Status::ok();
     63 }
     64 
     65 Status BluetoothLeScannerBinderServer::StartScan(
     66     int scanner_id, const android::bluetooth::ScanSettings& settings,
     67     const std::vector<android::bluetooth::ScanFilter>& filters,
     68     bool* _aidl_return) {
     69   VLOG(2) << __func__ << " scanner_id: " << scanner_id;
     70   std::lock_guard<std::mutex> lock(*maps_lock());
     71 
     72   auto scanner = GetLEScanner(scanner_id);
     73   if (!scanner) {
     74     LOG(ERROR) << "Unknown scanner_id: " << scanner_id;
     75     *_aidl_return = false;
     76     return Status::ok();
     77   }
     78 
     79   std::vector<bluetooth::ScanFilter> flt;
     80   for (const auto& filter : filters) {
     81     flt.push_back(filter);
     82   }
     83 
     84   *_aidl_return = scanner->StartScan(settings, flt);
     85   return Status::ok();
     86 }
     87 
     88 Status BluetoothLeScannerBinderServer::StopScan(int scanner_id,
     89                                                 bool* _aidl_return) {
     90   VLOG(2) << __func__ << " scanner_id: " << scanner_id;
     91   std::lock_guard<std::mutex> lock(*maps_lock());
     92 
     93   auto scanner = GetLEScanner(scanner_id);
     94   if (!scanner) {
     95     LOG(ERROR) << "Unknown scanner_id: " << scanner_id;
     96     *_aidl_return = false;
     97     return Status::ok();
     98   }
     99 
    100   *_aidl_return = scanner->StopScan();
    101   return Status::ok();
    102 }
    103 
    104 void BluetoothLeScannerBinderServer::OnScanResult(
    105     bluetooth::LowEnergyScanner* scanner, const bluetooth::ScanResult& result) {
    106   VLOG(2) << __func__;
    107   std::lock_guard<std::mutex> lock(*maps_lock());
    108 
    109   int scanner_id = scanner->GetInstanceId();
    110   auto cb = GetLECallback(scanner->GetInstanceId());
    111   if (!cb.get()) {
    112     VLOG(2) << "Scanner was unregistered - scanner_id: " << scanner_id;
    113     return;
    114   }
    115 
    116   cb->OnScanResult(result);
    117 }
    118 
    119 android::sp<IBluetoothLeScannerCallback>
    120 BluetoothLeScannerBinderServer::GetLECallback(int scanner_id) {
    121   auto cb = GetCallback(scanner_id);
    122   return android::sp<IBluetoothLeScannerCallback>(
    123       static_cast<IBluetoothLeScannerCallback*>(cb.get()));
    124 }
    125 
    126 std::shared_ptr<bluetooth::LowEnergyScanner>
    127 BluetoothLeScannerBinderServer::GetLEScanner(int scanner_id) {
    128   return std::static_pointer_cast<bluetooth::LowEnergyScanner>(
    129       GetInstance(scanner_id));
    130 }
    131 
    132 void BluetoothLeScannerBinderServer::OnRegisterInstanceImpl(
    133     bluetooth::BLEStatus status, android::sp<IInterface> callback,
    134     bluetooth::BluetoothInstance* instance) {
    135   VLOG(1) << __func__ << " status: " << status;
    136   bluetooth::LowEnergyScanner* le_scanner =
    137       static_cast<bluetooth::LowEnergyScanner*>(instance);
    138   le_scanner->SetDelegate(this);
    139 
    140   android::sp<IBluetoothLeScannerCallback> cb(
    141       static_cast<IBluetoothLeScannerCallback*>(callback.get()));
    142   cb->OnScannerRegistered(status, (status == bluetooth::BLE_STATUS_SUCCESS)
    143                                       ? instance->GetInstanceId()
    144                                       : kInvalidInstanceId);
    145 }
    146 
    147 }  // namespace binder
    148 }  // namespace ipc
    149