]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Demos/Host/LowLevel/KeyboardHostWithParser/HIDReport.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Demos / Host / LowLevel / KeyboardHostWithParser / HIDReport.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28   this software.
29 */
30
31 #include "HIDReport.h"
32
33 /** Size in bytes of the attached device's HID report descriptor */
34 uint16_t         HIDReportSize;
35
36 /** Processed HID report descriptor items structure, containing information on each HID report element */
37 HID_ReportInfo_t HIDReportInfo;
38
39
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.
42  *
43  *  \return A value from the \ref KeyboardHostWithParser_GetHIDReportDataCodes_t enum
44  */
45 uint8_t GetHIDReportData(void)
46 {
47         /* Create a buffer big enough to hold the entire returned HID report */
48         uint8_t HIDReportData[HIDReportSize];
49
50         USB_ControlRequest = (USB_Request_Header_t)
51                 {
52                         .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
53                         .bRequest      = REQ_GetDescriptor,
54                         .wValue        = (HID_DTYPE_Report << 8),
55                         .wIndex        = 0,
56                         .wLength       = HIDReportSize,
57                 };
58
59         /* Select the control pipe for the request transfer */
60         Pipe_SelectPipe(PIPE_CONTROLPIPE);
61
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;
65
66         /* Send the HID report to the parser for processing */
67         if (USB_ProcessHIDReport(HIDReportData, HIDReportSize, &HIDReportInfo) != HID_PARSE_Successful)
68           return ParseError;
69
70         return ParseSuccessful;
71 }
72
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
76  *  have occupied).
77  *
78  *  \param[in] CurrentItem  Pointer to the item the HID report parser is currently working with
79  *
80  *  \return Boolean \c true if the item should be stored into the HID report structure, \c false if it should be discarded
81  */
82 bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_t* const CurrentItem)
83 {
84         /* Check the attributes of the current item - see if we are interested in it or not;
85          * only store KEYBOARD usage page items into the Processed HID Report structure to
86          * save RAM and ignore the rest
87          */
88         return (CurrentItem->Attributes.Usage.Page == USAGE_PAGE_KEYBOARD);
89 }
90