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
;
40 void Connect() override
;
41 void Disconnect() override
;
42 void SendMessageImpl(scoped_ptr
<WireMessage
> message
) override
;
44 // BluetoothAdapter::Observer:
45 void DeviceRemoved(device::BluetoothAdapter
* adapter
,
46 device::BluetoothDevice
* device
) override
;
49 // Registers receive callbacks with the backing |socket_|.
52 // Callbacks for asynchronous Bluetooth operations.
53 void OnAdapterInitialized(scoped_refptr
<device::BluetoothAdapter
> adapter
);
54 void OnConnected(scoped_refptr
<device::BluetoothSocket
> socket
);
55 void OnConnectionError(const std::string
& error_message
);
56 void OnSend(int bytes_sent
);
57 void OnSendError(const std::string
& error_message
);
58 void OnReceive(int bytes_received
, scoped_refptr
<net::IOBuffer
> buffer
);
59 void OnReceiveError(device::BluetoothSocket::ErrorReason error_reason
,
60 const std::string
& error_message
);
62 // The UUID (universally unique identifier) of the Bluetooth service on the
63 // remote device that |this| connection should connect to.
64 const device::BluetoothUUID uuid_
;
66 // The Bluetooth adapter over which this connection is made. Non-null iff
67 // |this| connection is registered as an observer of the |adapter_|.
68 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
70 // The Bluetooth socket that backs this connection. NULL iff the connection is
71 // not in a connected state.
72 scoped_refptr
<device::BluetoothSocket
> socket_
;
74 // The message that was sent over the backing |socket_|. NULL iff there is no
75 // send operation in progress.
76 scoped_ptr
<WireMessage
> pending_message_
;
78 base::WeakPtrFactory
<BluetoothConnection
> weak_ptr_factory_
;
80 DISALLOW_COPY_AND_ASSIGN(BluetoothConnection
);
83 } // namespace proximity_auth
85 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H