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