Home | History | Annotate | Download | only in proximity_auth
      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 COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
      6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
      7 
      8 #include <string>
      9 
     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"
     17 
     18 namespace net {
     19 class IOBuffer;
     20 }
     21 
     22 namespace proximity_auth {
     23 
     24 struct RemoteDevice;
     25 
     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 {
     30  public:
     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
     33   // Bluetooth daemon.
     34   BluetoothConnection(const RemoteDevice& remote_device,
     35                       const device::BluetoothUUID& uuid);
     36   virtual ~BluetoothConnection();
     37 
     38  protected:
     39   // Connection:
     40   virtual void Connect() OVERRIDE;
     41   virtual void Disconnect() OVERRIDE;
     42   virtual void SendMessageImpl(scoped_ptr<WireMessage> message) OVERRIDE;
     43 
     44   // BluetoothAdapter::Observer:
     45   virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
     46                              device::BluetoothDevice* device) OVERRIDE;
     47 
     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&
     54           error_callback);
     55 
     56  private:
     57   // Registers receive callbacks with the backing |socket_|.
     58   void StartReceive();
     59 
     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);
     69 
     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_;
     73 
     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_;
     77 
     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_;
     81 
     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_;
     85 
     86   base::WeakPtrFactory<BluetoothConnection> weak_ptr_factory_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(BluetoothConnection);
     89 };
     90 
     91 }  // namespace proximity_auth
     92 
     93 #endif  // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
     94