Home | History | Annotate | Download | only in service
      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 #pragma once
     18 
     19 #include <memory>
     20 
     21 #include <base/macros.h>
     22 
     23 #include "service/common/bluetooth/adapter_state.h"
     24 
     25 namespace bluetooth {
     26 
     27 class GattClientFactory;
     28 class GattServerFactory;
     29 class LowEnergyAdvertiserFactory;
     30 class LowEnergyScannerFactory;
     31 class LowEnergyClientFactory;
     32 
     33 // Represents the local Bluetooth adapter.
     34 class Adapter {
     35  public:
     36   // The default values returned before the Adapter is fully initialized and
     37   // powered. The complete values for these fields are obtained following a
     38   // successful call to "Enable".
     39   static const char kDefaultAddress[];
     40   static const char kDefaultName[];
     41 
     42   // Observer interface allows other classes to receive notifications from us.
     43   // All of the methods in this interface are declared as optional to allow
     44   // different layers to process only those events that they are interested in.
     45   //
     46   // All methods take in an |adapter| argument which points to the Adapter
     47   // object that the Observer instance was added to.
     48   class Observer {
     49    public:
     50     virtual ~Observer() = default;
     51 
     52     // Called when there is a change in the state of the local Bluetooth
     53     // |adapter| from |prev_state| to |new_state|.
     54     virtual void OnAdapterStateChanged(Adapter* adapter,
     55                                        AdapterState prev_state,
     56                                        AdapterState new_state);
     57 
     58     // Called when there is a change in the connection state between the local
     59     // |adapter| and a remote device with address |device_address|. If the ACL
     60     // state changes from disconnected to connected, then |connected| will be
     61     // true and vice versa.
     62     virtual void OnDeviceConnectionStateChanged(
     63         Adapter* adapter, const std::string& device_address, bool connected);
     64   };
     65 
     66   // Returns an Adapter implementation to be used in production. Don't use these
     67   // in tests; use MockAdapter instead.
     68   static std::unique_ptr<Adapter> Create();
     69 
     70   virtual ~Adapter() = default;
     71 
     72   // Add or remove an observer.
     73   virtual void AddObserver(Observer* observer) = 0;
     74   virtual void RemoveObserver(Observer* observer) = 0;
     75 
     76   // Returns the current Adapter state.
     77   virtual AdapterState GetState() const = 0;
     78 
     79   // Returns true, if the adapter radio is current powered.
     80   virtual bool IsEnabled() const = 0;
     81 
     82   // Enables Bluetooth. This method will send a request to the Bluetooth adapter
     83   // to power up its radio. Returns true, if the request was successfully sent
     84   // to the controller, otherwise returns false. A successful call to this
     85   // method only means that the enable request has been sent to the Bluetooth
     86   // controller and does not imply that the operation itself succeeded.
     87   // The |start_restricted| flag enables the adapter in restricted mode. In
     88   // restricted mode, bonds that are created are marked as restricted in the
     89   // config file. These devices are deleted upon leaving restricted mode.
     90   virtual bool Enable(bool start_restricted) = 0;
     91 
     92   // Powers off the Bluetooth radio. Returns true, if the disable request was
     93   // successfully sent to the Bluetooth controller.
     94   virtual bool Disable() = 0;
     95 
     96   // Returns the name currently assigned to the local adapter.
     97   virtual std::string GetName() const = 0;
     98 
     99   // Sets the name assigned to the local Bluetooth adapter. This is the name
    100   // that the local controller will present to remote devices.
    101   virtual bool SetName(const std::string& name) = 0;
    102 
    103   // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
    104   virtual std::string GetAddress() const = 0;
    105 
    106   // Returns true if the local adapter supports the Low-Energy
    107   // multi-advertisement feature.
    108   virtual bool IsMultiAdvertisementSupported() = 0;
    109 
    110   // Returns true if the remote device with address |device_address| is
    111   // currently connected. This is not a const method as it modifies the state of
    112   // the associated internal mutex.
    113   virtual bool IsDeviceConnected(const std::string& device_address) = 0;
    114 
    115   // Returns the total number of trackable advertisements as supported by the
    116   // underlying hardware.
    117   virtual int GetTotalNumberOfTrackableAdvertisements() = 0;
    118 
    119   // Returns true if hardware-backed scan filtering is supported.
    120   virtual bool IsOffloadedFilteringSupported() = 0;
    121 
    122   // Returns true if hardware-backed batch scanning is supported.
    123   virtual bool IsOffloadedScanBatchingSupported() = 0;
    124 
    125   // Returns a pointer to the LowEnergyClientFactory. This can be used to
    126   // register per-application LowEnergyClient instances to perform BLE GAP
    127   // operations.
    128   virtual LowEnergyClientFactory* GetLowEnergyClientFactory() const = 0;
    129 
    130   // Returns a pointer to the LowEnergyScannerFactory. This can be used to
    131   // register per-application LowEnergyScanner instances to perform scanning.
    132   virtual LowEnergyScannerFactory* GetLeScannerFactory() const = 0;
    133 
    134   // Returns a pointer to the LowEnergyAdvertiserFactory. This can be used to
    135   // register per-application LowEnergyAdvertiser instances to perform
    136   // advertising.
    137   virtual LowEnergyAdvertiserFactory* GetLeAdvertiserFactory() const = 0;
    138 
    139   // Returns a pointer to the GattClientFactory. This can be used to register
    140   // per-application GATT server instances.
    141   virtual GattClientFactory* GetGattClientFactory() const = 0;
    142 
    143   // Returns a pointer to the GattServerFactory. This can be used to register
    144   // per-application GATT server instances.
    145   virtual GattServerFactory* GetGattServerFactory() const = 0;
    146 
    147  protected:
    148   Adapter() = default;
    149 
    150  private:
    151   DISALLOW_COPY_AND_ASSIGN(Adapter);
    152 };
    153 
    154 }  // namespace bluetooth
    155