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