2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <unordered_set>
24 #include <base/macros.h>
25 #include <base/observer_list.h>
27 #include "service/common/bluetooth/adapter_state.h"
28 #include "service/common/bluetooth/util/atomic_string.h"
29 #include "service/gatt_client.h"
30 #include "service/gatt_server.h"
31 #include "service/hal/bluetooth_interface.h"
32 #include "service/low_energy_client.h"
36 // Represents the local Bluetooth adapter.
37 class Adapter : public hal::BluetoothInterface::Observer {
39 // The default values returned before the Adapter is fully initialized and
40 // powered. The complete values for these fields are obtained following a
41 // successful call to "Enable".
42 static const char kDefaultAddress[];
43 static const char kDefaultName[];
45 // Observer interface allows other classes to receive notifications from us.
46 // All of the methods in this interface are declared as optional to allow
47 // different layers to process only those events that they are interested in.
49 // All methods take in an |adapter| argument which points to the Adapter
50 // object that the Observer instance was added to.
53 virtual ~Observer() = default;
55 // Called when there is a change in the state of the local Bluetooth
56 // |adapter| from |prev_state| to |new_state|.
57 virtual void OnAdapterStateChanged(Adapter* adapter,
58 AdapterState prev_state,
59 AdapterState new_state);
61 // Called when there is a change in the connection state between the local
62 // |adapter| and a remote device with address |device_address|. If the ACL
63 // state changes from disconnected to connected, then |connected| will be
64 // true and vice versa.
65 virtual void OnDeviceConnectionStateChanged(
66 Adapter* adapter, const std::string& device_address, bool connected);
72 // Add or remove an observer.
73 void AddObserver(Observer* observer);
74 void RemoveObserver(Observer* observer);
76 // Returns the current Adapter state.
77 AdapterState GetState() const;
79 // Returns true, if the adapter radio is current powered.
80 bool IsEnabled() const;
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.
89 // Powers off the Bluetooth radio. Returns true, if the disable request was
90 // successfully sent to the Bluetooth controller.
93 // Returns the name currently assigned to the local adapter.
94 std::string GetName() const;
96 // Sets the name assigned to the local Bluetooth adapter. This is the name
97 // that the local controller will present to remote devices.
98 bool SetName(const std::string& name);
100 // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
101 std::string GetAddress() const;
103 // Returns true if the local adapter supports the Low-Energy
104 // multi-advertisement feature.
105 bool IsMultiAdvertisementSupported() const;
107 // Returns true if the remote device with address |device_address| is
108 // currently connected. This is not a const method as it modifies the state of
109 // the associated internal mutex.
110 bool IsDeviceConnected(const std::string& device_address);
112 // Returns a pointer to the LowEnergyClientFactory. This can be used to
113 // register per-application LowEnergyClient instances to perform BLE GAP
115 LowEnergyClientFactory* GetLowEnergyClientFactory() const;
117 // Returns a pointer to the GattClientFactory. This can be used to register
118 // per-application GATT server instances.
119 GattClientFactory* GetGattClientFactory() const;
121 // Returns a pointer to the GattServerFactory. This can be used to register
122 // per-application GATT server instances.
123 GattServerFactory* GetGattServerFactory() const;
126 // hal::BluetoothInterface::Observer overrides.
127 void AdapterStateChangedCallback(bt_state_t state) override;
128 void AdapterPropertiesCallback(bt_status_t status,
130 bt_property_t* properties) override;
131 void AclStateChangedCallback(bt_status_t status,
132 const bt_bdaddr_t& remote_bdaddr,
133 bt_acl_state_t state) override;
135 // Sends a request to set the given HAL adapter property type and value.
136 bool SetAdapterProperty(bt_property_type_t type, void* value, int length);
138 // Helper for invoking observer method.
139 void NotifyAdapterStateChanged(AdapterState prev_state,
140 AdapterState new_state);
142 // The current adapter state.
143 std::atomic<AdapterState> state_;
145 // The Bluetooth device address of the local adapter in string from
146 // (i.e.. XX:XX:XX:XX:XX:XX)
147 util::AtomicString address_;
149 // The current local adapter name.
150 util::AtomicString name_;
152 // The current set of supported LE features as obtained from the stack. The
153 // values here are all initially set to 0 and updated when the corresponding
154 // adapter property has been received from the stack.
155 bt_local_le_features_t local_le_features_;
157 // List of observers that are interested in notifications from us.
158 std::mutex observers_lock_;
159 base::ObserverList<Observer> observers_;
161 // List of devices addresses that are currently connected.
162 std::mutex connected_devices_lock_;
163 std::unordered_set<std::string> connected_devices_;
165 // Factory used to create per-app LowEnergyClient instances.
166 std::unique_ptr<LowEnergyClientFactory> ble_client_factory_;
168 // Factory used to create per-app GattClient instances.
169 std::unique_ptr<GattClientFactory> gatt_client_factory_;
171 // Factory used to create per-app GattServer instances.
172 std::unique_ptr<GattServerFactory> gatt_server_factory_;
174 DISALLOW_COPY_AND_ASSIGN(Adapter);
177 } // namespace bluetooth