Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 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 | |
Arman Uguray | 2fea18e | 2015-09-23 23:43:18 -0700 | [diff] [blame] | 19 | #include <atomic> |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <mutex> |
| 23 | |
| 24 | #include <base/macros.h> |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 25 | #include <bluetooth/uuid.h> |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 26 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 27 | #include "service/bluetooth_instance.h" |
Arman Uguray | 234138e | 2015-10-06 15:56:36 -0700 | [diff] [blame] | 28 | #include "service/common/bluetooth/low_energy_constants.h" |
Arman Uguray | 480174f | 2015-11-30 15:36:17 -0800 | [diff] [blame] | 29 | #include "service/common/bluetooth/scan_filter.h" |
Arman Uguray | 82ea72f | 2015-12-02 17:29:27 -0800 | [diff] [blame] | 30 | #include "service/common/bluetooth/scan_result.h" |
Arman Uguray | 480174f | 2015-11-30 15:36:17 -0800 | [diff] [blame] | 31 | #include "service/common/bluetooth/scan_settings.h" |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 32 | #include "service/hal/bluetooth_gatt_interface.h" |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 33 | |
| 34 | namespace bluetooth { |
| 35 | |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 36 | struct ConnComparator { |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 37 | bool operator()(const RawAddress& a, const RawAddress& b) const { |
Jack He | 882aec3 | 2017-08-14 23:02:16 -0700 | [diff] [blame] | 38 | return memcmp(a.address, b.address, RawAddress::kLength) < 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 39 | } |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 42 | class Adapter; |
| 43 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 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. |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 47 | class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 48 | public BluetoothInstance { |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 49 | public: |
Arman Uguray | 82ea72f | 2015-12-02 17:29:27 -0800 | [diff] [blame] | 50 | // The Delegate interface is used to notify asynchronous events related to BLE |
| 51 | // GAP operations. |
| 52 | class Delegate { |
| 53 | public: |
| 54 | Delegate() = default; |
| 55 | virtual ~Delegate() = default; |
| 56 | |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 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; |
| 60 | |
Jakub Pawlowski | a655107 | 2016-01-26 12:58:47 -0800 | [diff] [blame] | 61 | // Called asynchronously to notify the delegate of mtu change |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 62 | virtual void OnMtuChanged(LowEnergyClient* client, int status, |
| 63 | const char* address, int mtu) = 0; |
Jakub Pawlowski | a655107 | 2016-01-26 12:58:47 -0800 | [diff] [blame] | 64 | |
Arman Uguray | 82ea72f | 2015-12-02 17:29:27 -0800 | [diff] [blame] | 65 | private: |
| 66 | DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 67 | }; |
| 68 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 69 | // The destructor automatically unregisters this client instance from the |
| 70 | // stack. |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 71 | ~LowEnergyClient() override; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 72 | |
Arman Uguray | 82ea72f | 2015-12-02 17:29:27 -0800 | [diff] [blame] | 73 | // Assigns a delegate to this instance. |delegate| must out-live this |
| 74 | // LowEnergyClient instance. |
| 75 | void SetDelegate(Delegate* delegate); |
| 76 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 77 | // Callback type used to return the result of asynchronous operations below. |
| 78 | using StatusCallback = std::function<void(BLEStatus)>; |
| 79 | |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 80 | // Initiates a BLE connection do device with address |address|. If |
| 81 | // |is_direct| is set, use direct connect procedure. Return true on success |
| 82 | //, false otherwise. |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 83 | bool Connect(const std::string& address, bool is_direct); |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 84 | |
| 85 | // Disconnect from previously connected BLE device with address |address|. |
| 86 | // Return true on success, false otherwise. |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 87 | bool Disconnect(const std::string& address); |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 88 | |
Jakub Pawlowski | a655107 | 2016-01-26 12:58:47 -0800 | [diff] [blame] | 89 | // Sends request to set MTU to |mtu| for device with address |address|. |
| 90 | // Return true on success, false otherwise. |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 91 | bool SetMtu(const std::string& address, int mtu); |
Jakub Pawlowski | a655107 | 2016-01-26 12:58:47 -0800 | [diff] [blame] | 92 | |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 93 | // BluetoothClientInstace overrides: |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 94 | const Uuid& GetAppIdentifier() const override; |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 95 | int GetInstanceId() const override; |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 96 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 97 | private: |
| 98 | friend class LowEnergyClientFactory; |
| 99 | |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 100 | // Constructor shouldn't be called directly as instances are meant to be |
| 101 | // obtained from the factory. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 102 | LowEnergyClient(Adapter& adapter, const Uuid& uuid, int client_id); |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 103 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 104 | // BluetoothGattInterface::ClientObserver overrides: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 105 | void ConnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id, |
| 106 | int status, int client_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 107 | const RawAddress& bda) override; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 108 | void DisconnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id, |
| 109 | int status, int client_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 110 | const RawAddress& bda) override; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 111 | void MtuChangedCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id, |
| 112 | int status, int mtu) override; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 113 | |
| 114 | // Calls and clears the pending callbacks. |
| 115 | void InvokeAndClearStartCallback(BLEStatus status); |
| 116 | void InvokeAndClearStopCallback(BLEStatus status); |
| 117 | |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 118 | // Raw pointer to the Bluetooth Adapter. |
| 119 | Adapter& adapter_; |
| 120 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 121 | // See getters above for documentation. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 122 | Uuid app_identifier_; |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 123 | int client_id_; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 124 | |
Arman Uguray | 82ea72f | 2015-12-02 17:29:27 -0800 | [diff] [blame] | 125 | // Raw handle to the Delegate, which must outlive this LowEnergyClient |
| 126 | // instance. |
| 127 | std::mutex delegate_mutex_; |
| 128 | Delegate* delegate_; |
| 129 | |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 130 | // Protects device connection related members below. |
| 131 | std::mutex connection_fields_lock_; |
| 132 | |
| 133 | // Maps bluetooth address to connection id |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 134 | // TODO(jpawlowski): change type to bimap |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 135 | std::map<const RawAddress, int, ConnComparator> connection_ids_; |
Jakub Pawlowski | a6372e9 | 2016-01-19 16:42:37 -0800 | [diff] [blame] | 136 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(LowEnergyClient); |
| 138 | }; |
| 139 | |
| 140 | // LowEnergyClientFactory is used to register and obtain a per-application |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 141 | // LowEnergyClient instance. Users should call RegisterInstance to obtain their |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 142 | // own unique LowEnergyClient instance that has been registered with the |
| 143 | // Bluetooth stack. |
| 144 | class LowEnergyClientFactory |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 145 | : private hal::BluetoothGattInterface::ClientObserver, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 146 | public BluetoothInstanceFactory { |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 147 | public: |
| 148 | // Don't construct/destruct directly except in tests. Instead, obtain a handle |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 149 | // from an Adapter instance. |
Chih-Hung Hsieh | 3398730 | 2016-06-30 15:45:24 -0700 | [diff] [blame] | 150 | explicit LowEnergyClientFactory(Adapter& adapter); |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 151 | ~LowEnergyClientFactory() override; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 152 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 153 | // BluetoothInstanceFactory override: |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 154 | bool RegisterInstance(const Uuid& uuid, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 155 | const RegisterCallback& callback) override; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 156 | |
| 157 | private: |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 158 | friend class LowEnergyClient; |
| 159 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 160 | // BluetoothGattInterface::ClientObserver overrides: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 161 | void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface, |
| 162 | int status, int client_id, |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 163 | const bluetooth::Uuid& app_uuid) override; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 164 | |
| 165 | // Map of pending calls to register. |
| 166 | std::mutex pending_calls_lock_; |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 167 | std::map<Uuid, RegisterCallback> pending_calls_; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 168 | |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 169 | // Raw pointer to the Adapter that owns this factory. |
| 170 | Adapter& adapter_; |
| 171 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 172 | DISALLOW_COPY_AND_ASSIGN(LowEnergyClientFactory); |
| 173 | }; |
| 174 | |
| 175 | } // namespace bluetooth |