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