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.
23 #include <base/macros.h>
24 #include <base/observer_list.h>
26 #include "service/adapter_state.h"
27 #include "service/gatt_server.h"
28 #include "service/hal/bluetooth_interface.h"
29 #include "service/low_energy_client.h"
30 #include "service/util/atomic_string.h"
34 // Represents the local Bluetooth adapter.
35 class Adapter : public hal::BluetoothInterface::Observer {
37 // The default values returned before the Adapter is fully initialized and
38 // powered. The complete values for these fields are obtained following a
39 // successful call to "Enable".
40 static const char kDefaultAddress[];
41 static const char kDefaultName[];
43 // Observer interface allows other classes to receive notifications from us.
44 // All of the methods in this interface are declared as optional to allow
45 // different layers to process only those events that they are interested in.
48 virtual ~Observer() = default;
50 virtual void OnAdapterStateChanged(Adapter* adapter,
51 AdapterState prev_state,
52 AdapterState new_state);
58 // Add or remove an observer.
59 void AddObserver(Observer* observer);
60 void RemoveObserver(Observer* observer);
62 // Returns the current Adapter state.
63 AdapterState GetState() const;
65 // Returns true, if the adapter radio is current powered.
66 bool IsEnabled() const;
68 // Enables Bluetooth. This method will send a request to the Bluetooth adapter
69 // to power up its radio. Returns true, if the request was successfully sent
70 // to the controller, otherwise returns false. A successful call to this
71 // method only means that the enable request has been sent to the Bluetooth
72 // controller and does not imply that the operation itself succeeded.
75 // Powers off the Bluetooth radio. Returns true, if the disable request was
76 // successfully sent to the Bluetooth controller.
79 // Returns the name currently assigned to the local adapter.
80 std::string GetName() const;
82 // Sets the name assigned to the local Bluetooth adapter. This is the name
83 // that the local controller will present to remote devices.
84 bool SetName(const std::string& name);
86 // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
87 std::string GetAddress() const;
89 // Returns true if the local adapter supports the Low-Energy
90 // multi-advertisement feature.
91 bool IsMultiAdvertisementSupported() const;
93 // Returns a pointer to the LowEnergyClientFactory. This can be used to
94 // register per-application LowEnergyClient instances to perform BLE GAP
96 LowEnergyClientFactory* GetLowEnergyClientFactory() const;
98 // Returns a pointer to the GattServerFactory. This can be used to register
99 // per-application GATT server instances.
100 GattServerFactory* GetGattServerFactory() const;
103 // hal::BluetoothInterface::Observer overrides.
104 void AdapterStateChangedCallback(bt_state_t state) override;
105 void AdapterPropertiesCallback(bt_status_t status,
107 bt_property_t* properties) override;
109 // Sends a request to set the given HAL adapter property type and value.
110 bool SetAdapterProperty(bt_property_type_t type, void* value, int length);
112 // Helper for invoking observer method.
113 void NotifyAdapterStateChanged(AdapterState prev_state,
114 AdapterState new_state);
116 // The current adapter state.
117 std::atomic<AdapterState> state_;
119 // The Bluetooth device address of the local adapter in string from
120 // (i.e.. XX:XX:XX:XX:XX:XX)
121 util::AtomicString address_;
123 // The current local adapter name.
124 util::AtomicString name_;
126 // The current set of supported LE features as obtained from the stack. The
127 // values here are all initially set to 0 and updated when the corresponding
128 // adapter property has been received from the stack.
129 bt_local_le_features_t local_le_features_;
131 // List of observers that are interested in notifications from us.
132 std::mutex observers_lock_;
133 base::ObserverList<Observer> observers_;
135 // Factory used to create per-app LowEnergyClient instances.
136 std::unique_ptr<LowEnergyClientFactory> ble_client_factory_;
138 // Factory used to create per-app GattServer instances.
139 std::unique_ptr<GattServerFactory> gatt_server_factory_;
141 DISALLOW_COPY_AND_ASSIGN(Adapter);
144 } // namespace bluetooth