OLD | NEW |
| (Empty) |
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_BLUEZ_H_ | |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_BLUEZ_H_ | |
7 | |
8 #include <memory> | |
9 #include <queue> | |
10 #include <string> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/linked_ptr.h" | |
14 #include "dbus/object_path.h" | |
15 #include "device/bluetooth/bluetooth_adapter.h" | |
16 #include "device/bluetooth/bluetooth_export.h" | |
17 #include "device/bluetooth/bluetooth_socket.h" | |
18 #include "device/bluetooth/bluetooth_socket_net.h" | |
19 #include "device/bluetooth/bluetooth_uuid.h" | |
20 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" | |
21 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" | |
22 | |
23 namespace dbus { | |
24 class FileDescriptor; | |
25 } // namespace dbus | |
26 | |
27 namespace bluez { | |
28 | |
29 class BluetoothDeviceBlueZ; | |
30 class BluetoothAdapterBlueZ; | |
31 class BluetoothAdapterProfileBlueZ; | |
32 | |
33 // The BluetoothSocketBlueZ class implements BluetoothSocket for platforms that | |
34 // use BlueZ. | |
35 // | |
36 // This class is not thread-safe, but is only called from the UI thread. | |
37 class DEVICE_BLUETOOTH_EXPORT BluetoothSocketBlueZ | |
38 : public device::BluetoothSocketNet, | |
39 public device::BluetoothAdapter::Observer, | |
40 public bluez::BluetoothProfileServiceProvider::Delegate { | |
41 public: | |
42 enum SecurityLevel { SECURITY_LEVEL_LOW, SECURITY_LEVEL_MEDIUM }; | |
43 | |
44 static scoped_refptr<BluetoothSocketBlueZ> CreateBluetoothSocket( | |
45 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
46 scoped_refptr<device::BluetoothSocketThread> socket_thread); | |
47 | |
48 // Connects this socket to the service on |device| published as UUID |uuid|, | |
49 // the underlying protocol and PSM or Channel is obtained through service | |
50 // discovery. On a successful connection the socket properties will be updated | |
51 // and |success_callback| called. On failure |error_callback| will be called | |
52 // with a message explaining the cause of the failure. | |
53 virtual void Connect(const BluetoothDeviceBlueZ* device, | |
54 const device::BluetoothUUID& uuid, | |
55 SecurityLevel security_level, | |
56 const base::Closure& success_callback, | |
57 const ErrorCompletionCallback& error_callback); | |
58 | |
59 // Listens using this socket using a service published on |adapter|. The | |
60 // service is either RFCOMM or L2CAP depending on |socket_type| and published | |
61 // as UUID |uuid|. The |service_options| argument is interpreted according to | |
62 // |socket_type|. |success_callback| will be called if the service is | |
63 // successfully registered, |error_callback| on failure with a message | |
64 // explaining the cause. | |
65 enum SocketType { kRfcomm, kL2cap }; | |
66 virtual void Listen( | |
67 scoped_refptr<device::BluetoothAdapter> adapter, | |
68 SocketType socket_type, | |
69 const device::BluetoothUUID& uuid, | |
70 const device::BluetoothAdapter::ServiceOptions& service_options, | |
71 const base::Closure& success_callback, | |
72 const ErrorCompletionCallback& error_callback); | |
73 | |
74 // BluetoothSocket: | |
75 void Close() override; | |
76 void Disconnect(const base::Closure& callback) override; | |
77 void Accept(const AcceptCompletionCallback& success_callback, | |
78 const ErrorCompletionCallback& error_callback) override; | |
79 | |
80 protected: | |
81 ~BluetoothSocketBlueZ() override; | |
82 | |
83 private: | |
84 BluetoothSocketBlueZ( | |
85 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
86 scoped_refptr<device::BluetoothSocketThread> socket_thread); | |
87 | |
88 // Register the underlying profile client object with the Bluetooth Daemon. | |
89 void RegisterProfile(BluetoothAdapterBlueZ* adapter, | |
90 const base::Closure& success_callback, | |
91 const ErrorCompletionCallback& error_callback); | |
92 void OnRegisterProfile(const base::Closure& success_callback, | |
93 const ErrorCompletionCallback& error_callback, | |
94 BluetoothAdapterProfileBlueZ* profile); | |
95 void OnRegisterProfileError(const ErrorCompletionCallback& error_callback, | |
96 const std::string& error_message); | |
97 | |
98 // Called by dbus:: on completion of the ConnectProfile() method. | |
99 void OnConnectProfile(const base::Closure& success_callback); | |
100 void OnConnectProfileError(const ErrorCompletionCallback& error_callback, | |
101 const std::string& error_name, | |
102 const std::string& error_message); | |
103 | |
104 // BluetoothAdapter::Observer: | |
105 void AdapterPresentChanged(device::BluetoothAdapter* adapter, | |
106 bool present) override; | |
107 | |
108 // Called by dbus:: on completion of the RegisterProfile() method call | |
109 // triggered as a result of the adapter becoming present again. | |
110 void OnInternalRegisterProfile(BluetoothAdapterProfileBlueZ* profile); | |
111 void OnInternalRegisterProfileError(const std::string& error_message); | |
112 | |
113 // bluez::BluetoothProfileServiceProvider::Delegate: | |
114 void Released() override; | |
115 void NewConnection( | |
116 const dbus::ObjectPath& device_path, | |
117 std::unique_ptr<dbus::FileDescriptor> fd, | |
118 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | |
119 const ConfirmationCallback& callback) override; | |
120 void RequestDisconnection(const dbus::ObjectPath& device_path, | |
121 const ConfirmationCallback& callback) override; | |
122 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 std::unique_ptr<dbus::FileDescriptor> fd, | |
132 const bluez::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(std::unique_ptr<dbus::FileDescriptor> fd, | |
146 const ConfirmationCallback& callback); | |
147 | |
148 // Method run to clean-up a listening socket. | |
149 void DoCloseListening(); | |
150 | |
151 // Unregisters this socket's usage of the Bluetooth profile which cleans up | |
152 // the profile if no one is using it. | |
153 void UnregisterProfile(); | |
154 | |
155 // Adapter the profile is registered against | |
156 scoped_refptr<device::BluetoothAdapter> adapter_; | |
157 | |
158 // Address and D-Bus object path of the device being connected to, empty and | |
159 // ignored if the socket is listening. | |
160 std::string device_address_; | |
161 dbus::ObjectPath device_path_; | |
162 | |
163 // UUID of the profile being connected to, or listening on. | |
164 device::BluetoothUUID uuid_; | |
165 | |
166 // Copy of the profile options used for registering the profile. | |
167 std::unique_ptr<bluez::BluetoothProfileManagerClient::Options> options_; | |
168 | |
169 // The profile registered with the adapter for this socket. | |
170 BluetoothAdapterProfileBlueZ* profile_; | |
171 | |
172 // Pending request to an Accept() call. | |
173 struct AcceptRequest { | |
174 AcceptRequest(); | |
175 ~AcceptRequest(); | |
176 | |
177 AcceptCompletionCallback success_callback; | |
178 ErrorCompletionCallback error_callback; | |
179 }; | |
180 std::unique_ptr<AcceptRequest> accept_request_; | |
181 | |
182 // Queue of incoming connection requests. | |
183 struct ConnectionRequest { | |
184 ConnectionRequest(); | |
185 ~ConnectionRequest(); | |
186 | |
187 dbus::ObjectPath device_path; | |
188 std::unique_ptr<dbus::FileDescriptor> fd; | |
189 bluez::BluetoothProfileServiceProvider::Delegate::Options options; | |
190 ConfirmationCallback callback; | |
191 bool accepting; | |
192 bool cancelled; | |
193 }; | |
194 std::queue<linked_ptr<ConnectionRequest>> connection_request_queue_; | |
195 | |
196 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketBlueZ); | |
197 }; | |
198 | |
199 } // namespace bluez | |
200 | |
201 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_BLUEZ_H_ | |
OLD | NEW |