1 // Copyright (c) 2012 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 DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "device/bluetooth/bluetooth_export.h"
20 class BluetoothDevice
;
23 // BluetoothSocket represents a socket to a specific service on a
24 // BluetoothDevice. BluetoothSocket objects are ref counted and may outlive
25 // both the BluetoothDevice and BluetoothAdapter that were involved in their
26 // creation. In terms of threading, platform specific implementations may
27 // differ slightly, but platform independent consumers must guarantee calling
28 // various instance methods on the same thread as the thread used at
29 // construction time -- platform specific implementation are responsible for
30 // marshalling calls to a different thread if required.
31 class DEVICE_BLUETOOTH_EXPORT BluetoothSocket
32 : public base::RefCountedThreadSafe
<BluetoothSocket
> {
34 enum ErrorReason
{ kSystemError
, kIOPending
, kDisconnected
};
36 typedef base::Callback
<void(int)> SendCompletionCallback
;
37 typedef base::Callback
<void(int, scoped_refptr
<net::IOBuffer
> io_buffer
)>
38 ReceiveCompletionCallback
;
39 typedef base::Callback
<void(const BluetoothDevice
* device
,
40 scoped_refptr
<BluetoothSocket
> socket
)>
41 AcceptCompletionCallback
;
42 typedef base::Callback
<void(const std::string
& error_message
)>
43 ErrorCompletionCallback
;
44 typedef base::Callback
<void(ErrorReason
, const std::string
& error_message
)>
45 ReceiveErrorCompletionCallback
;
47 // Destroys resources associated with the socket. After calling this method,
48 // it is illegal to call any method on this socket instance (except for the
49 // destructor via Release).
50 virtual void Close() = 0;
52 // Gracefully disconnects the socket and calls |callback| upon completion.
53 // There is no failure case, as this is a best effort operation.
54 virtual void Disconnect(const base::Closure
& success_callback
) = 0;
56 // Receives data from the socket and calls |success_callback| when data is
57 // available. |buffer_size| specifies the maximum number of bytes that can be
58 // received. If an error occurs, calls |error_callback| with a reason and an
62 const ReceiveCompletionCallback
& success_callback
,
63 const ReceiveErrorCompletionCallback
& error_callback
) = 0;
65 // Sends |buffer| to the socket and calls |success_callback| when data has
66 // been successfully sent. |buffer_size| is the number of bytes contained in
67 // |buffer|. If an error occurs, calls |error_callback| with an error message.
68 virtual void Send(scoped_refptr
<net::IOBuffer
> buffer
,
70 const SendCompletionCallback
& success_callback
,
71 const ErrorCompletionCallback
& error_callback
) = 0;
73 // Accepts a pending client connection from the socket and calls
74 // |success_callback| on completion, passing a new BluetoothSocket instance
75 // for the new client. If an error occurs, calls |error_callback| with a
76 // reason and an error message.
77 virtual void Accept(const AcceptCompletionCallback
& success_callback
,
78 const ErrorCompletionCallback
& error_callback
) = 0;
81 friend class base::RefCountedThreadSafe
<BluetoothSocket
>;
82 virtual ~BluetoothSocket();
87 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_