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" 11 #include "base/macros.h" 12 #include "device/bluetooth/bluetooth_export.h" 13 14 namespace device { 15 16 class BluetoothAdapter; 17 class BluetoothDevice; 18 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). 25 class DEVICE_BLUETOOTH_EXPORT BluetoothGattConnection { 26 public: 27 BluetoothGattConnection(scoped_refptr<device::BluetoothAdapter> adapter, 28 const std::string& device_address); 29 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. 39 const std::string& GetDeviceAddress() const; 40 41 // Returns true if this GATT connection is open. 42 virtual bool IsConnected(); 43 44 // Disconnects this GATT connection. The device may still remain connected due 45 // to other GATT connections. When all BluetoothGattConnection objects are 46 // disconnected the BluetoothDevice object will disconnect GATT. 47 virtual void Disconnect(); 48 49 protected: 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; 65 66 private: 67 bool owns_reference_for_connection_ = false; 68 69 DISALLOW_COPY_AND_ASSIGN(BluetoothGattConnection); 70 }; 71 72 } // namespace device 73 74 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_ 75