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.
24 #include <base/macros.h>
26 #include "service/bluetooth_instance.h"
27 #include "service/common/bluetooth/advertise_data.h"
28 #include "service/common/bluetooth/advertise_settings.h"
29 #include "service/common/bluetooth/low_energy_constants.h"
30 #include "service/common/bluetooth/scan_filter.h"
31 #include "service/common/bluetooth/scan_result.h"
32 #include "service/common/bluetooth/scan_settings.h"
33 #include "service/common/bluetooth/uuid.h"
34 #include "service/hal/bluetooth_gatt_interface.h"
38 struct ConnComparator {
39 bool operator()(const bt_bdaddr_t& a, const bt_bdaddr_t& b) const {
40 return memcmp(&a, &b, sizeof(bt_bdaddr_t)) < 0;
46 // A LowEnergyClient represents an application's handle to perform various
47 // Bluetooth Low Energy GAP operations. Instances cannot be created directly and
48 // should be obtained through the factory.
49 class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver,
50 public BluetoothInstance {
52 // The Delegate interface is used to notify asynchronous events related to BLE
57 virtual ~Delegate() = default;
59 // Called asynchronously to notify the delegate of nearby BLE advertisers
60 // found during a device scan.
61 virtual void OnScanResult(LowEnergyClient* client,
62 const ScanResult& scan_result) = 0;
64 // Called asynchronously to notify the delegate of connection state change
65 virtual void OnConnectionState(LowEnergyClient* client, int status,
66 const char* address, bool connected) = 0;
68 // Called asynchronously to notify the delegate of mtu change
69 virtual void OnMtuChanged(LowEnergyClient* client, int status, const char* address,
73 DISALLOW_COPY_AND_ASSIGN(Delegate);
76 // The destructor automatically unregisters this client instance from the
78 ~LowEnergyClient() override;
80 // Assigns a delegate to this instance. |delegate| must out-live this
81 // LowEnergyClient instance.
82 void SetDelegate(Delegate* delegate);
84 // Callback type used to return the result of asynchronous operations below.
85 using StatusCallback = std::function<void(BLEStatus)>;
87 // Initiates a BLE connection do device with address |address|. If
88 // |is_direct| is set, use direct connect procedure. Return true on success
90 bool Connect(std::string address, bool is_direct);
92 // Disconnect from previously connected BLE device with address |address|.
93 // Return true on success, false otherwise.
94 bool Disconnect(std::string address);
96 // Sends request to set MTU to |mtu| for device with address |address|.
97 // Return true on success, false otherwise.
98 bool SetMtu(std::string address, int mtu);
100 // Initiates a BLE device scan for this client using the given |settings| and
101 // |filters|. See the documentation for ScanSettings and ScanFilter for how
102 // these parameters can be configured. Return true on success, false
103 // otherwise. Please see logs for details in case of error.
104 bool StartScan(const ScanSettings& settings,
105 const std::vector<ScanFilter>& filters);
107 // Stops an ongoing BLE device scan for this client.
110 // Starts advertising based on the given advertising and scan response
111 // data and the provided |settings|. Reports the result of the operation in
112 // |callback|. Return true on success, false otherwise. Please see logs for
113 // details in case of error.
114 bool StartAdvertising(const AdvertiseSettings& settings,
115 const AdvertiseData& advertise_data,
116 const AdvertiseData& scan_response,
117 const StatusCallback& callback);
119 // Stops advertising if it was already started. Reports the result of the
120 // operation in |callback|.
121 bool StopAdvertising(const StatusCallback& callback);
123 // Returns true if advertising has been started.
124 bool IsAdvertisingStarted() const;
126 // Returns the state of pending advertising operations.
127 bool IsStartingAdvertising() const;
128 bool IsStoppingAdvertising() const;
130 // Returns the current advertising settings.
131 const AdvertiseSettings& advertise_settings() const {
132 return advertise_settings_;
135 // Returns the current scan settings.
136 const ScanSettings& scan_settings() const { return scan_settings_; }
138 // BluetoothClientInstace overrides:
139 const UUID& GetAppIdentifier() const override;
140 int GetInstanceId() const override;
143 friend class LowEnergyClientFactory;
145 // Constructor shouldn't be called directly as instances are meant to be
146 // obtained from the factory.
147 LowEnergyClient(Adapter& adapter, const UUID& uuid, int client_id);
149 // BluetoothGattInterface::ClientObserver overrides:
150 void ScanResultCallback(
151 hal::BluetoothGattInterface* gatt_iface,
152 const bt_bdaddr_t& bda, int rssi, uint8_t* adv_data) override;
154 void ConnectCallback(
155 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
156 int client_id, const bt_bdaddr_t& bda) override;
157 void DisconnectCallback(
158 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
159 int client_id, const bt_bdaddr_t& bda) override;
160 void MtuChangedCallback(
161 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
163 void MultiAdvEnableCallback(
164 hal::BluetoothGattInterface* gatt_iface,
165 int client_id, int status) override;
166 void MultiAdvDataCallback(
167 hal::BluetoothGattInterface* gatt_iface,
168 int client_id, int status) override;
169 void MultiAdvDisableCallback(
170 hal::BluetoothGattInterface* gatt_iface,
171 int client_id, int status) override;
173 // Helper method called from SetAdvertiseData/SetScanResponse.
174 bt_status_t SetAdvertiseData(
175 hal::BluetoothGattInterface* gatt_iface,
176 const AdvertiseData& data,
179 // Handles deferred advertise/scan-response data updates. We set the data if
180 // there's data to be set, otherwise we either defer it if advertisements
181 // aren't enabled or do nothing.
182 void HandleDeferredAdvertiseData(hal::BluetoothGattInterface* gatt_iface);
184 // Calls and clears the pending callbacks.
185 void InvokeAndClearStartCallback(BLEStatus status);
186 void InvokeAndClearStopCallback(BLEStatus status);
188 // Raw pointer to the Bluetooth Adapter.
191 // See getters above for documentation.
192 UUID app_identifier_;
195 // Protects advertising-related members below.
196 std::mutex adv_fields_lock_;
198 // The advertising and scan response data fields that will be sent to the
200 AdvertiseData adv_data_;
201 AdvertiseData scan_response_;
202 std::atomic_bool adv_data_needs_update_;
203 std::atomic_bool scan_rsp_needs_update_;
205 // Latest advertising settings.
206 AdvertiseSettings advertise_settings_;
208 // Whether or not there is a pending call to update advertising or scan
210 std::atomic_bool is_setting_adv_data_;
212 std::atomic_bool adv_started_;
213 std::unique_ptr<StatusCallback> adv_start_callback_;
214 std::unique_ptr<StatusCallback> adv_stop_callback_;
216 // Protects device scan related members below.
217 std::mutex scan_fields_lock_;
219 // Current scan settings.
220 ScanSettings scan_settings_;
222 // If true, then this client have a BLE device scan in progress.
223 std::atomic_bool scan_started_;
225 // Raw handle to the Delegate, which must outlive this LowEnergyClient
227 std::mutex delegate_mutex_;
230 // Protects device connection related members below.
231 std::mutex connection_fields_lock_;
233 // Maps bluetooth address to connection id
234 //TODO(jpawlowski): change type to bimap
235 std::map<const bt_bdaddr_t, int, ConnComparator> connection_ids_;
237 DISALLOW_COPY_AND_ASSIGN(LowEnergyClient);
240 // LowEnergyClientFactory is used to register and obtain a per-application
241 // LowEnergyClient instance. Users should call RegisterInstance to obtain their
242 // own unique LowEnergyClient instance that has been registered with the
244 class LowEnergyClientFactory
245 : private hal::BluetoothGattInterface::ClientObserver,
246 public BluetoothInstanceFactory {
248 // Don't construct/destruct directly except in tests. Instead, obtain a handle
249 // from an Adapter instance.
250 LowEnergyClientFactory(Adapter& adapter);
251 ~LowEnergyClientFactory() override;
253 // BluetoothInstanceFactory override:
254 bool RegisterInstance(const UUID& uuid,
255 const RegisterCallback& callback) override;
258 friend class LowEnergyClient;
260 // BluetoothGattInterface::ClientObserver overrides:
261 void RegisterClientCallback(
262 hal::BluetoothGattInterface* gatt_iface,
263 int status, int client_id,
264 const bt_uuid_t& app_uuid) override;
266 // Map of pending calls to register.
267 std::mutex pending_calls_lock_;
268 std::map<UUID, RegisterCallback> pending_calls_;
270 // Raw pointer to the Adapter that owns this factory.
273 DISALLOW_COPY_AND_ASSIGN(LowEnergyClientFactory);
276 } // namespace bluetooth