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