hid_connection.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2014 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 SERVICES_DEVICE_HID_HID_CONNECTION_H_
  5. #define SERVICES_DEVICE_HID_HID_CONNECTION_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <tuple>
  9. #include "base/callback_forward.h"
  10. #include "base/containers/queue.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/sequence_checker.h"
  14. #include "services/device/hid/hid_device_info.h"
  15. namespace base {
  16. class RefCountedBytes;
  17. }
  18. namespace device {
  19. class HidConnection : public base::RefCountedThreadSafe<HidConnection> {
  20. public:
  21. enum SpecialReportIds {
  22. kNullReportId = 0x00,
  23. kAnyReportId = 0xFF,
  24. };
  25. using ReadCallback =
  26. base::OnceCallback<void(bool success,
  27. scoped_refptr<base::RefCountedBytes> buffer,
  28. size_t size)>;
  29. using WriteCallback = base::OnceCallback<void(bool success)>;
  30. class Client {
  31. public:
  32. // Notify the client when an input report is received from the connected
  33. // device. |buffer| contains the report data, and |size| is the size of the
  34. // received report. The buffer is sized to fit the largest input report
  35. // supported by the device, which may be larger than |size|.
  36. virtual void OnInputReport(scoped_refptr<base::RefCountedBytes> buffer,
  37. size_t size) = 0;
  38. };
  39. HidConnection(HidConnection&) = delete;
  40. HidConnection& operator=(HidConnection&) = delete;
  41. void SetClient(Client* client);
  42. scoped_refptr<HidDeviceInfo> device_info() const { return device_info_; }
  43. bool has_always_protected_collection() const {
  44. return has_always_protected_collection_;
  45. }
  46. bool closed() const { return closed_; }
  47. // Closes the connection. This must be called before the object is freed.
  48. void Close();
  49. // The report ID (or 0 if report IDs are not supported by the device) is
  50. // always returned in the first byte of the buffer.
  51. void Read(ReadCallback callback);
  52. // The report ID (or 0 if report IDs are not supported by the device) is
  53. // always expected in the first byte of the buffer.
  54. void Write(scoped_refptr<base::RefCountedBytes> buffer,
  55. WriteCallback callback);
  56. // The buffer will contain whatever report data was received from the device.
  57. // This may include the report ID. The report ID is not stripped because a
  58. // device may respond with other data in place of the report ID.
  59. void GetFeatureReport(uint8_t report_id, ReadCallback callback);
  60. // The report ID (or 0 if report IDs are not supported by the device) is
  61. // always expected in the first byte of the buffer.
  62. void SendFeatureReport(scoped_refptr<base::RefCountedBytes> buffer,
  63. WriteCallback callback);
  64. protected:
  65. friend class base::RefCountedThreadSafe<HidConnection>;
  66. HidConnection(scoped_refptr<HidDeviceInfo> device_info,
  67. bool allow_protected_reports,
  68. bool allow_fido_reports);
  69. virtual ~HidConnection();
  70. virtual void PlatformClose() = 0;
  71. virtual void PlatformWrite(scoped_refptr<base::RefCountedBytes> buffer,
  72. WriteCallback callback) = 0;
  73. virtual void PlatformGetFeatureReport(uint8_t report_id,
  74. ReadCallback callback) = 0;
  75. virtual void PlatformSendFeatureReport(
  76. scoped_refptr<base::RefCountedBytes> buffer,
  77. WriteCallback callback) = 0;
  78. bool IsReportIdProtected(uint8_t report_id, HidReportType report_type);
  79. void ProcessInputReport(scoped_refptr<base::RefCountedBytes> buffer,
  80. size_t size);
  81. void ProcessReadQueue();
  82. private:
  83. scoped_refptr<HidDeviceInfo> device_info_;
  84. const bool allow_protected_reports_;
  85. const bool allow_fido_reports_;
  86. raw_ptr<Client> client_ = nullptr;
  87. bool has_always_protected_collection_;
  88. bool closed_;
  89. base::queue<std::tuple<scoped_refptr<base::RefCountedBytes>, size_t>>
  90. pending_reports_;
  91. base::queue<ReadCallback> pending_reads_;
  92. SEQUENCE_CHECKER(sequence_checker_);
  93. };
  94. } // namespace device
  95. #endif // SERVICES_DEVICE_HID_HID_CONNECTION_H_