]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Host/LowLevel/MouseHostWithParser/HIDReport.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Host / LowLevel / MouseHostWithParser / HIDReport.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 MouseHostWithParser_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 bool IsMouse = false;
85
86 /* Iterate through the item's collection path, until either the root collection node or a collection with the
87 * Mouse Usage is found - this prevents Joysticks, which use identical descriptors except for the Joystick usage
88 * parent node, from being erroneously treated as a mouse
89 */
90 for (HID_CollectionPath_t* CurrPath = CurrentItem->CollectionPath; CurrPath != NULL; CurrPath = CurrPath->Parent)
91 {
92 if ((CurrPath->Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
93 (CurrPath->Usage.Usage == USAGE_MOUSE))
94 {
95 IsMouse = true;
96 break;
97 }
98 }
99
100 /* If a collection with the mouse usage was not found, indicate that we are not interested in this item */
101 if (!IsMouse)
102 return false;
103
104 /* Check the attributes of the current mouse 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
107 */
108 return ((CurrentItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) ||
109 (CurrentItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL));
110 }
111
Imprint / Impressum