[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_ |
| 6 | #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/callback.h" |
avi | 176e269 | 2015-12-22 19:26:52 | [diff] [blame] | 11 | #include "base/macros.h" |
scheib | 7c0d277 | 2014-12-06 16:02:04 | [diff] [blame] | 12 | #include "device/bluetooth/bluetooth_export.h" |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 13 | |
| 14 | namespace device { |
| 15 | |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 16 | class BluetoothAdapter; |
| 17 | class BluetoothDevice; |
| 18 | |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 19 | // BluetoothGattConnection represents a GATT connection to a Bluetooth device |
| 20 | // that has GATT services. Instances are obtained from a BluetoothDevice, |
| 21 | // and the connection is kept alive as long as there is at least one |
| 22 | // active BluetoothGattConnection object. BluetoothGattConnection objects |
| 23 | // automatically update themselves, when the connection is terminated by the |
| 24 | // operating system (e.g. due to user action). |
scheib | 7c0d277 | 2014-12-06 16:02:04 | [diff] [blame] | 25 | class DEVICE_BLUETOOTH_EXPORT BluetoothGattConnection { |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 26 | public: |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 27 | BluetoothGattConnection(scoped_refptr<device::BluetoothAdapter> adapter, |
| 28 | const std::string& device_address); |
| 29 | |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 30 | // Destructor automatically closes this GATT connection. If this is the last |
| 31 | // remaining GATT connection and this results in a call to the OS, that call |
| 32 | // may not always succeed. Users can make an explicit call to |
| 33 | // BluetoothGattConnection::Close to make sure that they are notified of |
| 34 | // a possible error via the callback. |
| 35 | virtual ~BluetoothGattConnection(); |
| 36 | |
| 37 | // Returns the Bluetooth address of the device that this connection is open |
| 38 | // to. |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 39 | const std::string& GetDeviceAddress() const; |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 40 | |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 41 | // Returns true if this GATT connection is open. |
| 42 | virtual bool IsConnected(); |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 43 | |
scheib | c89c898 | 2015-09-16 21:26:17 | [diff] [blame] | 44 | // Disconnects this GATT connection. The device may still remain connected due |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 45 | // to other GATT connections. When all BluetoothGattConnection objects are |
| 46 | // disconnected the BluetoothDevice object will disconnect GATT. |
| 47 | virtual void Disconnect(); |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 48 | |
| 49 | protected: |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 50 | friend BluetoothDevice; // For InvalidateConnectionReference. |
| 51 | |
| 52 | // Sets this object to no longer have a reference maintaining the connection. |
| 53 | // Only to be called by BluetoothDevice to avoid reentrant code to |
| 54 | // RemoveGattConnection in that destructor after BluetoothDevice subclasses |
| 55 | // have already been destroyed. |
| 56 | void InvalidateConnectionReference(); |
| 57 | |
| 58 | // The Bluetooth adapter that this connection is associated with. A reference |
| 59 | // is held because BluetoothGattConnection keeps the connection alive. |
| 60 | scoped_refptr<BluetoothAdapter> adapter_; |
| 61 | |
| 62 | // Bluetooth address of the underlying device. |
| 63 | std::string device_address_; |
| 64 | BluetoothDevice* device_ = nullptr; |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 65 | |
| 66 | private: |
scheib | 9556087 | 2015-09-21 05:09:33 | [diff] [blame] | 67 | bool owns_reference_for_connection_ = false; |
| 68 | |
[email protected] | 9ca6bd2 | 2014-06-18 23:48:59 | [diff] [blame] | 69 | DISALLOW_COPY_AND_ASSIGN(BluetoothGattConnection); |
| 70 | }; |
| 71 | |
| 72 | } // namespace device |
| 73 | |
| 74 | #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_ |