bluetooth_socket_chromeos.h revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2013 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_SOCKET_CHROMEOS_H_
6#define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_
7
8#include <queue>
9#include <string>
10
11#include "base/memory/linked_ptr.h"
12#include "base/memory/scoped_ptr.h"
13#include "chromeos/chromeos_export.h"
14#include "chromeos/dbus/bluetooth_profile_manager_client.h"
15#include "chromeos/dbus/bluetooth_profile_service_provider.h"
16#include "dbus/object_path.h"
17#include "device/bluetooth/bluetooth_adapter.h"
18#include "device/bluetooth/bluetooth_socket.h"
19#include "device/bluetooth/bluetooth_socket_net.h"
20#include "device/bluetooth/bluetooth_uuid.h"
21
22namespace dbus {
23class FileDescriptor;
24}  // namespace dbus
25
26namespace chromeos {
27
28class BluetoothDeviceChromeOS;
29
30// The BluetoothSocketChromeOS class implements BluetoothSocket for the
31// Chrome OS platform.
32class CHROMEOS_EXPORT BluetoothSocketChromeOS
33    : public device::BluetoothSocketNet,
34      public device::BluetoothAdapter::Observer,
35      public BluetoothProfileServiceProvider::Delegate {
36 public:
37  static scoped_refptr<BluetoothSocketChromeOS> CreateBluetoothSocket(
38      scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
39      scoped_refptr<device::BluetoothSocketThread> socket_thread,
40      net::NetLog* net_log,
41      const net::NetLog::Source& source);
42
43  // Connects this socket to the service on |device| published as UUID |uuid|,
44  // the underlying protocol and PSM or Channel is obtained through service
45  // discovery. On a successful connection the socket properties will be updated
46  // and |success_callback| called. On failure |error_callback| will be called
47  // with a message explaining the cause of the failure.
48  virtual void Connect(const BluetoothDeviceChromeOS* device,
49                       const device::BluetoothUUID& uuid,
50                       const base::Closure& success_callback,
51                       const ErrorCompletionCallback& error_callback);
52
53  // Listens using this socket using a service published on |adapter|. The
54  // service is either RFCOMM or L2CAP depending on |socket_type| and published
55  // as UUID |uuid|. The |service_options| argument is interpreted according to
56  // |socket_type|. |success_callback| will be called if the service is
57  // successfully registered, |error_callback| on failure with a message
58  // explaining the cause.
59  enum SocketType { kRfcomm, kL2cap };
60  virtual void Listen(
61      scoped_refptr<device::BluetoothAdapter> adapter,
62      SocketType socket_type,
63      const device::BluetoothUUID& uuid,
64      const device::BluetoothAdapter::ServiceOptions& service_options,
65      const base::Closure& success_callback,
66      const ErrorCompletionCallback& error_callback);
67
68  // BluetoothSocket:
69  virtual void Close() OVERRIDE;
70  virtual void Disconnect(const base::Closure& callback) OVERRIDE;
71  virtual void Accept(const AcceptCompletionCallback& success_callback,
72                      const ErrorCompletionCallback& error_callback) OVERRIDE;
73
74  // Returns the object path of the socket.
75  const dbus::ObjectPath& object_path() const { return object_path_; }
76
77 protected:
78  virtual ~BluetoothSocketChromeOS();
79
80 private:
81  BluetoothSocketChromeOS(
82      scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
83      scoped_refptr<device::BluetoothSocketThread> socket_thread,
84      net::NetLog* net_log,
85      const net::NetLog::Source& source);
86
87  // Register the underlying profile client object with the Bluetooth Daemon.
88  void RegisterProfile(const base::Closure& success_callback,
89                       const ErrorCompletionCallback& error_callback);
90  void OnRegisterProfile(const base::Closure& success_callback,
91                         const ErrorCompletionCallback& error_callback);
92  void OnRegisterProfileError(const ErrorCompletionCallback& error_callback,
93                              const std::string& error_name,
94                              const std::string& error_message);
95
96  // Called by dbus:: on completion of the ConnectProfile() method.
97  void OnConnectProfile(const base::Closure& success_callback);
98  void OnConnectProfileError(const ErrorCompletionCallback& error_callback,
99                             const std::string& error_name,
100                             const std::string& error_message);
101
102  // BluetoothAdapter::Observer:
103  virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
104                                     bool present) OVERRIDE;
105
106  // Called by dbus:: on completion of the RegisterProfile() method call
107  // triggered as a result of the adapter becoming present again.
108  void OnInternalRegisterProfile();
109  void OnInternalRegisterProfileError(const std::string& error_name,
110                                      const std::string& error_message);
111
112  // BluetoothProfileServiceProvider::Delegate:
113  virtual void Released() OVERRIDE;
114  virtual void NewConnection(
115      const dbus::ObjectPath& device_path,
116      scoped_ptr<dbus::FileDescriptor> fd,
117      const BluetoothProfileServiceProvider::Delegate::Options& options,
118      const ConfirmationCallback& callback) OVERRIDE;
119  virtual void RequestDisconnection(
120      const dbus::ObjectPath& device_path,
121      const ConfirmationCallback& callback) OVERRIDE;
122  virtual void Cancel() OVERRIDE;
123
124  // Method run to accept a single incoming connection.
125  void AcceptConnectionRequest();
126
127  // Method run on the socket thread to validate the file descriptor of a new
128  // connection and set up the underlying net::TCPSocket() for it.
129  void DoNewConnection(
130      const dbus::ObjectPath& device_path,
131      scoped_ptr<dbus::FileDescriptor> fd,
132      const BluetoothProfileServiceProvider::Delegate::Options& options,
133      const ConfirmationCallback& callback);
134
135  // Method run on the UI thread after a new connection has been accepted and
136  // a socket allocated in |socket|. Takes care of calling the Accept()
137  // callback and |callback| with the right arguments based on |status|.
138  void OnNewConnection(scoped_refptr<BluetoothSocket> socket,
139                       const ConfirmationCallback& callback,
140                       Status status);
141
142  // Method run on the socket thread with a valid file descriptor |fd|, once
143  // complete calls |callback| on the UI thread with an appropriate argument
144  // indicating success or failure.
145  void DoConnect(scoped_ptr<dbus::FileDescriptor> fd,
146                 const ConfirmationCallback& callback);
147
148  // Method run to clean-up a listening socket.
149  void DoCloseListening();
150
151  // Unregister the underlying profile client object from the Bluetooth Daemon.
152  void UnregisterProfile();
153  void OnUnregisterProfile(const dbus::ObjectPath& object_path);
154  void OnUnregisterProfileError(const dbus::ObjectPath& object_path,
155                                const std::string& error_name,
156                                const std::string& error_message);
157
158  // Adapter the profile is registered against; this is only present when the
159  // socket is listening.
160  scoped_refptr<device::BluetoothAdapter> adapter_;
161
162  // Address and D-Bus object path of the device being connected to, empty and
163  // ignored if the socket is listening.
164  std::string device_address_;
165  dbus::ObjectPath device_path_;
166
167  // UUID of the profile being connected to, or listening on.
168  device::BluetoothUUID uuid_;
169
170  // Copy of the profile options used for registering the profile.
171  scoped_ptr<BluetoothProfileManagerClient::Options> options_;
172
173  // Object path of the local profile D-Bus object.
174  dbus::ObjectPath object_path_;
175
176  // Local profile D-Bus object used for receiving profile delegate methods
177  // from BlueZ.
178  scoped_ptr<BluetoothProfileServiceProvider> profile_;
179
180  // Pending request to an Accept() call.
181  struct AcceptRequest {
182    AcceptRequest();
183    ~AcceptRequest();
184
185    AcceptCompletionCallback success_callback;
186    ErrorCompletionCallback error_callback;
187  };
188  scoped_ptr<AcceptRequest> accept_request_;
189
190  // Queue of incoming connection requests.
191  struct ConnectionRequest {
192    ConnectionRequest();
193    ~ConnectionRequest();
194
195    dbus::ObjectPath device_path;
196    scoped_ptr<dbus::FileDescriptor> fd;
197    BluetoothProfileServiceProvider::Delegate::Options options;
198    ConfirmationCallback callback;
199    bool accepting;
200    bool cancelled;
201  };
202  std::queue<linked_ptr<ConnectionRequest> > connection_request_queue_;
203
204  DISALLOW_COPY_AND_ASSIGN(BluetoothSocketChromeOS);
205};
206
207}  // namespace chromeos
208
209#endif  // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_
210