bluetooth_socket.h 3.8 KB

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