hid_service_win.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_SERVICE_WIN_H_
  5. #define SERVICES_DEVICE_HID_HID_SERVICE_WIN_H_
  6. #include <windows.h>
  7. // Must be after windows.h.
  8. #include <hidclass.h>
  9. // NOTE: <hidsdi.h> must be included before <hidpi.h>. clang-format will want to
  10. // reorder them.
  11. // clang-format off
  12. extern "C" {
  13. #include <hidsdi.h>
  14. #include <hidpi.h>
  15. }
  16. // clang-format on
  17. #include <string>
  18. #include <string>
  19. #include <vector>
  20. #include "base/memory/weak_ptr.h"
  21. #include "base/scoped_observation.h"
  22. #include "base/win/scoped_handle.h"
  23. #include "device/base/device_monitor_win.h"
  24. #include "services/device/hid/hid_service.h"
  25. namespace base {
  26. class SequencedTaskRunner;
  27. }
  28. namespace device {
  29. class HidServiceWin : public HidService, public DeviceMonitorWin::Observer {
  30. public:
  31. // Interface for accessing information contained in the opaque
  32. // HIDP_PREPARSED_DATA object. A PreparsedData instance represents a single
  33. // HID top-level collection.
  34. class PreparsedData {
  35. public:
  36. struct ReportItem {
  37. // The report ID, or zero if the device does not use report IDs.
  38. uint8_t report_id;
  39. // The bit field for the corresponding main item in the HID report. This
  40. // bit field is defined in the Device Class Definition for HID v1.11
  41. // section 6.2.2.5.
  42. // https://www.usb.org/document-library/device-class-definition-hid-111
  43. uint32_t bit_field;
  44. // The size of one field defined by this item, in bits.
  45. uint16_t report_size;
  46. // The number of report fields defined by this item.
  47. uint16_t report_count;
  48. // The usage page for this item.
  49. uint16_t usage_page;
  50. // The usage range for this item. If the item has a single usage instead
  51. // of a range, |usage_min| and |usage_max| are set to the same usage ID.
  52. // Both usage IDs must be from the same |usage_page|.
  53. uint16_t usage_minimum;
  54. uint16_t usage_maximum;
  55. // The designator index range for this item. If the item does not have any
  56. // designators, both |designator_min| and |designator_max| are set to
  57. // zero.
  58. uint16_t designator_minimum;
  59. uint16_t designator_maximum;
  60. // The string descriptor index range for this item. If the item does not
  61. // have any associated string descriptors, both |string_min| and
  62. // |string_max| are set to zero.
  63. uint16_t string_minimum;
  64. uint16_t string_maximum;
  65. // The range for report fields defined by this item in logical units.
  66. int32_t logical_minimum;
  67. int32_t logical_maximum;
  68. // The range for report fields defined by this item in physical units. May
  69. // be zero if the item does not define physical units.
  70. int32_t physical_minimum;
  71. int32_t physical_maximum;
  72. // The unit definition for this item. The format for this definition is
  73. // described in the Device Class Definition for HID v1.11 section 6.2.2.7.
  74. // https://www.usb.org/document-library/device-class-definition-hid-111
  75. uint32_t unit;
  76. uint32_t unit_exponent;
  77. // The index of the first bit of this item within the containing report,
  78. // omitting the report ID byte. The bit index follows HID report packing
  79. // order. (Increasing byte index, and least-signficiant bit to
  80. // most-significant bit within each byte.)
  81. size_t bit_index;
  82. };
  83. virtual ~PreparsedData() = default;
  84. // Creates a new mojom::HidCollectionInfoPtr representing the top-level HID
  85. // collection described by this PreparsedData.
  86. mojom::HidCollectionInfoPtr CreateHidCollectionInfo() const;
  87. // Returns the maximum length in bytes of reports of type |report_type|.
  88. // The returned length does not include the report ID byte.
  89. uint16_t GetReportByteLength(HIDP_REPORT_TYPE report_type) const;
  90. // Returns information about the top-level collection described by this
  91. // PreparsedData.
  92. //
  93. // See the HIDP_CAPS documentation for more information.
  94. // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/hidpi/ns-hidpi-_hidp_caps
  95. virtual const HIDP_CAPS& GetCaps() const = 0;
  96. // Returns a vector of ReportItems describing the fields that make up
  97. // reports of type |report_type|.
  98. virtual std::vector<ReportItem> GetReportItems(
  99. HIDP_REPORT_TYPE report_type) const = 0;
  100. };
  101. HidServiceWin();
  102. HidServiceWin(const HidServiceWin&) = delete;
  103. HidServiceWin& operator=(const HidServiceWin&) = delete;
  104. ~HidServiceWin() override;
  105. void Connect(const std::string& device_id,
  106. bool allow_protected_reports,
  107. bool allow_fido_reports,
  108. ConnectCallback callback) override;
  109. base::WeakPtr<HidService> GetWeakPtr() override;
  110. private:
  111. static void EnumerateBlocking(
  112. base::WeakPtr<HidServiceWin> service,
  113. scoped_refptr<base::SequencedTaskRunner> task_runner);
  114. static void AddDeviceBlocking(
  115. base::WeakPtr<HidServiceWin> service,
  116. scoped_refptr<base::SequencedTaskRunner> task_runner,
  117. const std::wstring& device_path,
  118. const std::string& physical_device_id,
  119. const std::wstring& interface_id);
  120. // DeviceMonitorWin::Observer implementation:
  121. void OnDeviceAdded(const GUID& class_guid,
  122. const std::wstring& device_path) override;
  123. void OnDeviceRemoved(const GUID& class_guid,
  124. const std::wstring& device_path) override;
  125. // Tries to open the device read-write and falls back to read-only.
  126. static base::win::ScopedHandle OpenDevice(const std::wstring& device_path);
  127. const scoped_refptr<base::SequencedTaskRunner> task_runner_;
  128. const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  129. base::ScopedObservation<DeviceMonitorWin, DeviceMonitorWin::Observer>
  130. device_observation_{this};
  131. base::WeakPtrFactory<HidServiceWin> weak_factory_{this};
  132. };
  133. } // namespace device
  134. #endif // SERVICES_DEVICE_HID_HID_SERVICE_WIN_H_