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 #pragma once
     18 
     19 #include <memory>
     20 
     21 #include <base/macros.h>
     22 
     23 #include "service/common/bluetooth/binder/IBluetoothLowEnergy.h"
     24 #include "service/common/bluetooth/binder/IBluetoothLowEnergyCallback.h"
     25 #include "service/common/bluetooth/low_energy_constants.h"
     26 #include "service/ipc/binder/interface_with_instances_base.h"
     27 #include "service/low_energy_client.h"
     28 
     29 namespace bluetooth {
     30 class Adapter;
     31 }  // namespace bluetooth
     32 
     33 namespace ipc {
     34 namespace binder {
     35 
     36 // Implements the server side of the IBluetoothLowEnergy interface.
     37 class BluetoothLowEnergyBinderServer
     38     : public BnBluetoothLowEnergy,
     39       public InterfaceWithInstancesBase,
     40       public bluetooth::LowEnergyClient::Delegate {
     41  public:
     42   explicit BluetoothLowEnergyBinderServer(bluetooth::Adapter* adapter);
     43   ~BluetoothLowEnergyBinderServer() override;
     44 
     45   // IBluetoothLowEnergy overrides:
     46   bool RegisterClient(
     47       const android::sp<IBluetoothLowEnergyCallback>& callback) override;
     48   void UnregisterClient(int client_id) override;
     49   void UnregisterAll() override;
     50   bool Connect(int client_id, const char* address, bool is_direct) override;
     51   bool Disconnect(int client_id, const char* address) override;
     52   bool SetMtu(int client_id, const char* address, int mtu) override;
     53   bool StartScan(
     54       int client_id,
     55       const bluetooth::ScanSettings& settings,
     56       const std::vector<bluetooth::ScanFilter>& filters) override;
     57   bool StopScan(int client_id) override;
     58   bool StartMultiAdvertising(
     59       int client_id,
     60       const bluetooth::AdvertiseData& advertise_data,
     61       const bluetooth::AdvertiseData& scan_response,
     62       const bluetooth::AdvertiseSettings& settings) override;
     63   bool StopMultiAdvertising(int client_id) override;
     64 
     65   // bluetooth::LowEnergyClient::Delegate overrides:
     66   void OnConnectionState(bluetooth::LowEnergyClient* client, int status,
     67                          const char* address, bool connected) override;
     68   void OnMtuChanged(bluetooth::LowEnergyClient* client, int status,
     69                     const char* address, int mtu) override;
     70   void OnScanResult(bluetooth::LowEnergyClient* client,
     71                     const bluetooth::ScanResult& result) override;
     72 
     73  private:
     74   // Returns a pointer to the IBluetoothLowEnergyCallback instance associated
     75   // with |client_id|. Returns NULL if such a callback cannot be found.
     76   android::sp<IBluetoothLowEnergyCallback> GetLECallback(int client_id);
     77 
     78   // Returns a pointer to the LowEnergyClient instance associated with
     79   // |client_id|. Returns NULL if such a client cannot be found.
     80   std::shared_ptr<bluetooth::LowEnergyClient> GetLEClient(int client_id);
     81 
     82   // InterfaceWithInstancesBase override:
     83   void OnRegisterInstanceImpl(
     84       bluetooth::BLEStatus status,
     85       android::sp<IInterface> callback,
     86       bluetooth::BluetoothInstance* instance) override;
     87 
     88   bluetooth::Adapter* adapter_;  // weak
     89 
     90   DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyBinderServer);
     91 };
     92 
     93 }  // namespace binder
     94 }  // namespace ipc
     95