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