blob: e616a115211dc9c5424a713bc805090fe42b0fe0 [file] [log] [blame]
[email protected]9ca6bd22014-06-18 23:48:591// 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"
avi176e2692015-12-22 19:26:5211#include "base/macros.h"
scheib7c0d2772014-12-06 16:02:0412#include "device/bluetooth/bluetooth_export.h"
[email protected]9ca6bd22014-06-18 23:48:5913
14namespace device {
15
scheib95560872015-09-21 05:09:3316class BluetoothAdapter;
17class BluetoothDevice;
18
[email protected]9ca6bd22014-06-18 23:48:5919// 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).
scheib7c0d2772014-12-06 16:02:0425class DEVICE_BLUETOOTH_EXPORT BluetoothGattConnection {
[email protected]9ca6bd22014-06-18 23:48:5926 public:
scheib95560872015-09-21 05:09:3327 BluetoothGattConnection(scoped_refptr<device::BluetoothAdapter> adapter,
28 const std::string& device_address);
29
[email protected]9ca6bd22014-06-18 23:48:5930 // 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.
scheib95560872015-09-21 05:09:3339 const std::string& GetDeviceAddress() const;
[email protected]9ca6bd22014-06-18 23:48:5940
scheib95560872015-09-21 05:09:3341 // Returns true if this GATT connection is open.
42 virtual bool IsConnected();
[email protected]9ca6bd22014-06-18 23:48:5943
scheibc89c8982015-09-16 21:26:1744 // Disconnects this GATT connection. The device may still remain connected due
scheib95560872015-09-21 05:09:3345 // to other GATT connections. When all BluetoothGattConnection objects are
46 // disconnected the BluetoothDevice object will disconnect GATT.
47 virtual void Disconnect();
[email protected]9ca6bd22014-06-18 23:48:5948
49 protected:
scheib95560872015-09-21 05:09:3350 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]9ca6bd22014-06-18 23:48:5965
66 private:
scheib95560872015-09-21 05:09:3367 bool owns_reference_for_connection_ = false;
68
[email protected]9ca6bd22014-06-18 23:48:5969 DISALLOW_COPY_AND_ASSIGN(BluetoothGattConnection);
70};
71
72} // namespace device
73
74#endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_