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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "components/proximity_auth/connection.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_device.h"
15 #include "device/bluetooth/bluetooth_socket.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
22 namespace proximity_auth
{
26 // Represents a Bluetooth connection with a remote device. The connection is a
27 // persistent bidirectional channel for sending and receiving wire messages.
28 class BluetoothConnection
: public Connection
,
29 public device::BluetoothAdapter::Observer
{
31 // Constructs a Bluetooth connection to the service with |uuid| on the
32 // |remote_device|. The |remote_device| must already be known to the system
34 BluetoothConnection(const RemoteDevice
& remote_device
,
35 const device::BluetoothUUID
& uuid
);
36 ~BluetoothConnection() override
;
39 void Connect() override
;
40 void Disconnect() override
;
44 void SendMessageImpl(scoped_ptr
<WireMessage
> message
) override
;
46 // BluetoothAdapter::Observer:
47 void DeviceRemoved(device::BluetoothAdapter
* adapter
,
48 device::BluetoothDevice
* device
) override
;
51 // Registers receive callbacks with the backing |socket_|.
54 // Callbacks for asynchronous Bluetooth operations.
55 void OnAdapterInitialized(scoped_refptr
<device::BluetoothAdapter
> adapter
);
56 void OnConnected(scoped_refptr
<device::BluetoothSocket
> socket
);
57 void OnConnectionError(const std::string
& error_message
);
58 void OnSend(int bytes_sent
);
59 void OnSendError(const std::string
& error_message
);
60 void OnReceive(int bytes_received
, scoped_refptr
<net::IOBuffer
> buffer
);
61 void OnReceiveError(device::BluetoothSocket::ErrorReason error_reason
,
62 const std::string
& error_message
);
64 // The UUID (universally unique identifier) of the Bluetooth service on the
65 // remote device that |this| connection should connect to.
66 const device::BluetoothUUID uuid_
;
68 // The Bluetooth adapter over which this connection is made. Non-null iff
69 // |this| connection is registered as an observer of the |adapter_|.
70 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
72 // The Bluetooth socket that backs this connection. NULL iff the connection is
73 // not in a connected state.
74 scoped_refptr
<device::BluetoothSocket
> socket_
;
76 // The message that was sent over the backing |socket_|. NULL iff there is no
77 // send operation in progress.
78 scoped_ptr
<WireMessage
> pending_message_
;
80 base::WeakPtrFactory
<BluetoothConnection
> weak_ptr_factory_
;
82 DISALLOW_COPY_AND_ASSIGN(BluetoothConnection
);
85 } // namespace proximity_auth
87 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H