2 // Copyright 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>
25 #include <bluetooth/uuid.h>
27 #include "service/bluetooth_instance.h"
28 #include "service/common/bluetooth/low_energy_constants.h"
29 #include "service/common/bluetooth/scan_filter.h"
30 #include "service/common/bluetooth/scan_result.h"
31 #include "service/common/bluetooth/scan_settings.h"
32 #include "service/hal/bluetooth_gatt_interface.h"
36 struct ConnComparator {
37 bool operator()(const RawAddress& a, const RawAddress& b) const {
38 return memcmp(a.address, b.address, RawAddress::kLength) < 0;
44 // A LowEnergyClient represents an application's handle to perform various
45 // Bluetooth Low Energy GAP operations. Instances cannot be created directly and
46 // should be obtained through the factory.
47 class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver,
48 public BluetoothInstance {
50 // The Delegate interface is used to notify asynchronous events related to BLE
55 virtual ~Delegate() = default;
57 // Called asynchronously to notify the delegate of connection state change
58 virtual void OnConnectionState(LowEnergyClient* client, int status,
59 const char* address, bool connected) = 0;
61 // Called asynchronously to notify the delegate of mtu change
62 virtual void OnMtuChanged(LowEnergyClient* client, int status,
63 const char* address, int mtu) = 0;
66 DISALLOW_COPY_AND_ASSIGN(Delegate);
69 // The destructor automatically unregisters this client instance from the
71 ~LowEnergyClient() override;
73 // Assigns a delegate to this instance. |delegate| must out-live this
74 // LowEnergyClient instance.
75 void SetDelegate(Delegate* delegate);
77 // Callback type used to return the result of asynchronous operations below.
78 using StatusCallback = std::function<void(BLEStatus)>;
80 // Initiates a BLE connection do device with address |address|. If
81 // |is_direct| is set, use direct connect procedure. Return true on success
83 bool Connect(const std::string& address, bool is_direct);
85 // Disconnect from previously connected BLE device with address |address|.
86 // Return true on success, false otherwise.
87 bool Disconnect(const std::string& address);
89 // Sends request to set MTU to |mtu| for device with address |address|.
90 // Return true on success, false otherwise.
91 bool SetMtu(const std::string& address, int mtu);
93 // BluetoothClientInstace overrides:
94 const Uuid& GetAppIdentifier() const override;
95 int GetInstanceId() const override;
98 friend class LowEnergyClientFactory;
100 // Constructor shouldn't be called directly as instances are meant to be
101 // obtained from the factory.
102 LowEnergyClient(Adapter& adapter, const Uuid& uuid, int client_id);
104 // BluetoothGattInterface::ClientObserver overrides:
105 void ConnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id,
106 int status, int client_id,
107 const RawAddress& bda) override;
108 void DisconnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id,
109 int status, int client_id,
110 const RawAddress& bda) override;
111 void MtuChangedCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id,
112 int status, int mtu) override;
114 // Calls and clears the pending callbacks.
115 void InvokeAndClearStartCallback(BLEStatus status);
116 void InvokeAndClearStopCallback(BLEStatus status);
118 // Raw pointer to the Bluetooth Adapter.
121 // See getters above for documentation.
122 Uuid app_identifier_;
125 // Raw handle to the Delegate, which must outlive this LowEnergyClient
127 std::mutex delegate_mutex_;
130 // Protects device connection related members below.
131 std::mutex connection_fields_lock_;
133 // Maps bluetooth address to connection id
134 // TODO(jpawlowski): change type to bimap
135 std::map<const RawAddress, int, ConnComparator> connection_ids_;
137 DISALLOW_COPY_AND_ASSIGN(LowEnergyClient);
140 // LowEnergyClientFactory is used to register and obtain a per-application
141 // LowEnergyClient instance. Users should call RegisterInstance to obtain their
142 // own unique LowEnergyClient instance that has been registered with the
144 class LowEnergyClientFactory
145 : private hal::BluetoothGattInterface::ClientObserver,
146 public BluetoothInstanceFactory {
148 // Don't construct/destruct directly except in tests. Instead, obtain a handle
149 // from an Adapter instance.
150 explicit LowEnergyClientFactory(Adapter& adapter);
151 ~LowEnergyClientFactory() override;
153 // BluetoothInstanceFactory override:
154 bool RegisterInstance(const Uuid& uuid,
155 const RegisterCallback& callback) override;
158 friend class LowEnergyClient;
160 // BluetoothGattInterface::ClientObserver overrides:
161 void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface,
162 int status, int client_id,
163 const bluetooth::Uuid& app_uuid) override;
165 // Map of pending calls to register.
166 std::mutex pending_calls_lock_;
167 std::map<Uuid, RegisterCallback> pending_calls_;
169 // Raw pointer to the Adapter that owns this factory.
172 DISALLOW_COPY_AND_ASSIGN(LowEnergyClientFactory);
175 } // namespace bluetooth