Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HIDReport.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #include "HIDReport.h"
  27. /** Size in bytes of the attached device's HID report descriptor */
  28. uint16_t HIDReportSize;
  29. /** Processed HID report descriptor items structure, containing information on each HID report element */
  30. HID_ReportInfo_t HIDReportInfo;
  31. /** Function to read in the HID report descriptor from the attached device, and process it into easy-to-read
  32. * structures via the HID parser routines in the LUFA library.
  33. *
  34. * \return A value from the \ref MouseHostWithParser_GetHIDReportDataCodes_t enum
  35. */
  36. uint8_t GetHIDReportData(void)
  37. {
  38. /* Create a buffer big enough to hold the entire returned HID report */
  39. uint8_t HIDReportData[HIDReportSize];
  40. USB_ControlRequest = (USB_Request_Header_t)
  41. {
  42. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
  43. .bRequest = REQ_GetDescriptor,
  44. .wValue = (HID_DTYPE_Report << 8),
  45. .wIndex = 0,
  46. .wLength = HIDReportSize,
  47. };
  48. /* Select the control pipe for the request transfer */
  49. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  50. /* Send control request to retrieve the HID report from the attached device */
  51. if (USB_Host_SendControlRequest(HIDReportData) != HOST_SENDCONTROL_Successful)
  52. return ParseControlError;
  53. /* Send the HID report to the parser for processing */
  54. if (USB_ProcessHIDReport(HIDReportData, HIDReportSize, &HIDReportInfo) != HID_PARSE_Successful)
  55. return ParseError;
  56. return ParseSuccessful;
  57. }
  58. /** Callback for the HID Report Parser. This function is called each time the HID report parser is about to store
  59. * an IN, OUT or FEATURE item into the HIDReportInfo structure. To save on RAM, we are able to filter out items
  60. * we aren't interested in (preventing us from being able to extract them later on, but saving on the RAM they would
  61. * have occupied).
  62. *
  63. * \param[in] CurrentItem Pointer to the item the HID report parser is currently working with
  64. *
  65. * \return Boolean \c true if the item should be stored into the HID report structure, \c false if it should be discarded
  66. */
  67. bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_t* const CurrentItem)
  68. {
  69. bool IsMouse = false;
  70. /* Iterate through the item's collection path, until either the root collection node or a collection with the
  71. * Mouse Usage is found - this prevents Joysticks, which use identical descriptors except for the Joystick usage
  72. * parent node, from being erroneously treated as a mouse
  73. */
  74. for (HID_CollectionPath_t* CurrPath = CurrentItem->CollectionPath; CurrPath != NULL; CurrPath = CurrPath->Parent)
  75. {
  76. if ((CurrPath->Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
  77. (CurrPath->Usage.Usage == USAGE_MOUSE))
  78. {
  79. IsMouse = true;
  80. break;
  81. }
  82. }
  83. /* If a collection with the mouse usage was not found, indicate that we are not interested in this item */
  84. if (!IsMouse)
  85. return false;
  86. /* Check the attributes of the current mouse item - see if we are interested in it or not;
  87. * only store BUTTON and GENERIC_DESKTOP_CONTROL items into the Processed HID Report
  88. * structure to save RAM and ignore the rest
  89. */
  90. return ((CurrentItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) ||
  91. (CurrentItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL));
  92. }