]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Class/Common/HIDParser.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Class / Common / HIDParser.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief USB Human Interface Device (HID) Class report descriptor parser.\r
33  *\r
34  *  This file allows for the easy parsing of complex HID report descriptors, which describes the data that\r
35  *  a HID device transmits to the host. It also provides an easy API for extracting and processing the data\r
36  *  elements inside a HID report sent from an attached HID device.\r
37  */\r
38 \r
39 /** \ingroup Group_USB\r
40  *  \defgroup Group_HIDParser HID Report Parser\r
41  *  \brief USB Human Interface Device (HID) Class report descriptor parser.\r
42  *\r
43  *  \section Sec_Dependencies Module Source Dependencies\r
44  *  The following files must be built with any user project that uses this module:\r
45  *    - LUFA/Drivers/USB/Class/Host/HIDParser.c <i>(Makefile source module name: LUFA_SRC_USB)</i>\r
46  *\r
47  *  \section Sec_ModDescription Module Description\r
48  *  Human Interface Device (HID) class report descriptor parser. This module implements a parser than is\r
49  *  capable of processing a complete HID report descriptor, and outputting a flat structure containing the\r
50  *  contents of the report in an a more friendly format. The parsed data may then be further processed and used\r
51  *  within an application to process sent and received HID reports to and from an attached HID device.\r
52  *\r
53  *  A HID report descriptor consists of a set of HID report items, which describe the function and layout\r
54  *  of data exchanged between a HID device and a host, including both the physical encoding of each item\r
55  *  (such as a button, key press or joystick axis) in the sent and received data packets - known as "reports" -\r
56  *  as well as other information about each item such as the usages, data range, physical location and other\r
57  *  characteristics. In this way a HID device can retain a high degree of flexibility in its capabilities, as it\r
58  *  is not forced to comply with a given report layout or feature-set.\r
59  *\r
60  *  This module also contains routines for the processing of data in an actual HID report, using the parsed report\r
61  *  descriptor data as a guide for the encoding.\r
62  *\r
63  *  @{\r
64  */\r
65 \r
66 #ifndef __HIDPARSER_H__\r
67 #define __HIDPARSER_H__\r
68 \r
69         /* Includes: */\r
70                 #include "../../../../Common/Common.h"\r
71 \r
72                 #include "HIDReportData.h"\r
73                 #include "HIDClassCommon.h"\r
74 \r
75         /* Enable C linkage for C++ Compilers: */\r
76                 #if defined(__cplusplus)\r
77                         extern "C" {\r
78                 #endif\r
79 \r
80         /* Macros: */\r
81                 #if !defined(HID_STATETABLE_STACK_DEPTH) || defined(__DOXYGEN__)\r
82                         /** Constant indicating the maximum stack depth of the state table. A larger state table\r
83                          *  allows for more PUSH/POP report items to be nested, but consumes more memory. By default\r
84                          *  this is set to 2 levels (allowing non-nested PUSH items) but this can be overridden by\r
85                          *  defining \c HID_STATETABLE_STACK_DEPTH to another value in the user project makefile, passing the\r
86                          *  define to the compiler using the -D compiler switch.\r
87                          */\r
88                         #define HID_STATETABLE_STACK_DEPTH    2\r
89                 #endif\r
90 \r
91                 #if !defined(HID_USAGE_STACK_DEPTH) || defined(__DOXYGEN__)\r
92                         /** Constant indicating the maximum stack depth of the usage table. A larger usage table\r
93                          *  allows for more USAGE items to be indicated sequentially for REPORT COUNT entries of more than\r
94                          *  one, but requires more stack space. By default this is set to 8 levels (allowing for a report\r
95                          *  item with a count of 8) but this can be overridden by defining \c HID_USAGE_STACK_DEPTH to another\r
96                          *  value in the user project makefile, passing the define to the compiler using the -D compiler\r
97                          *  switch.\r
98                          */\r
99                         #define HID_USAGE_STACK_DEPTH         8\r
100                 #endif\r
101 \r
102                 #if !defined(HID_MAX_COLLECTIONS) || defined(__DOXYGEN__)\r
103                         /** Constant indicating the maximum number of COLLECTION items (nested or unnested) that can be\r
104                          *  processed in the report item descriptor. A large value allows for more COLLECTION items to be\r
105                          *  processed, but consumes more memory. By default this is set to 10 collections, but this can be\r
106                          *  overridden by defining \c HID_MAX_COLLECTIONS to another value in the user project makefile, passing\r
107                          *  the define to the compiler using the -D compiler switch.\r
108                          */\r
109                         #define HID_MAX_COLLECTIONS           10\r
110                 #endif\r
111 \r
112                 #if !defined(HID_MAX_REPORTITEMS) || defined(__DOXYGEN__)\r
113                         /** Constant indicating the maximum number of report items (IN, OUT or FEATURE) that can be processed\r
114                          *  in the report item descriptor and stored in the user HID Report Info structure. A large value allows\r
115                          *  for more report items to be stored, but consumes more memory. By default this is set to 20 items,\r
116                          *  but this can be overridden by defining \c HID_MAX_REPORTITEMS to another value in the user project\r
117                          *  makefile, and passing the define to the compiler using the -D compiler switch.\r
118                          */\r
119                         #define HID_MAX_REPORTITEMS           20\r
120                 #endif\r
121 \r
122                 #if !defined(HID_MAX_REPORT_IDS) || defined(__DOXYGEN__)\r
123                         /** Constant indicating the maximum number of unique report IDs that can be processed in the report item\r
124                          *  descriptor for the report size information array in the user HID Report Info structure. A large value\r
125                          *  allows for more report ID report sizes to be stored, but consumes more memory. By default this is set\r
126                          *  to 10 items, but this can be overridden by defining \c HID_MAX_REPORT_IDS to another value in the user project\r
127                          *  makefile, and passing the define to the compiler using the -D compiler switch. Note that IN, OUT and FEATURE\r
128                          *  items sharing the same report ID consume only one size item in the array.\r
129                          */\r
130                         #define HID_MAX_REPORT_IDS            10\r
131                 #endif\r
132 \r
133                 /** Returns the value a given HID report item (once its value has been fetched via \ref USB_GetHIDReportItemInfo())\r
134                  *  left-aligned to the given data type. This allows for signed data to be interpreted correctly, by shifting the data\r
135                  *  leftwards until the data's sign bit is in the correct position.\r
136                  *\r
137                  *  \param[in] ReportItem  HID Report Item whose retrieved value is to be aligned.\r
138                  *  \param[in] Type        Data type to align the HID report item's value to.\r
139                  *\r
140                  *  \return Left-aligned data of the given report item's pre-retrieved value for the given datatype.\r
141                  */\r
142                 #define HID_ALIGN_DATA(ReportItem, Type) ((Type)(ReportItem->Value << ((8 * sizeof(Type)) - ReportItem->Attributes.BitSize)))\r
143 \r
144         /* Public Interface - May be used in end-application: */\r
145                 /* Enums: */\r
146                         /** Enum for the possible error codes in the return value of the \ref USB_ProcessHIDReport() function. */\r
147                         enum HID_Parse_ErrorCodes_t\r
148                         {\r
149                                 HID_PARSE_Successful                  = 0, /**< Successful parse of the HID report descriptor, no error. */\r
150                                 HID_PARSE_HIDStackOverflow            = 1, /**< More than \ref HID_STATETABLE_STACK_DEPTH nested PUSHes in the report. */\r
151                                 HID_PARSE_HIDStackUnderflow           = 2, /**< A POP was found when the state table stack was empty. */\r
152                                 HID_PARSE_InsufficientReportItems     = 3, /**< More than \ref HID_MAX_REPORTITEMS report items in the report. */\r
153                                 HID_PARSE_UnexpectedEndCollection     = 4, /**< An END COLLECTION item found without matching COLLECTION item. */\r
154                                 HID_PARSE_InsufficientCollectionPaths = 5, /**< More than \ref HID_MAX_COLLECTIONS collections in the report. */\r
155                                 HID_PARSE_UsageListOverflow           = 6, /**< More than \ref HID_USAGE_STACK_DEPTH usages listed in a row. */\r
156                                 HID_PARSE_InsufficientReportIDItems   = 7, /**< More than \ref HID_MAX_REPORT_IDS report IDs in the device. */\r
157                                 HID_PARSE_NoUnfilteredReportItems     = 8, /**< All report items from the device were filtered by the filtering callback routine. */\r
158                         };\r
159 \r
160                 /* Type Defines: */\r
161                         /** \brief HID Parser Report Item Min/Max Structure.\r
162                          *\r
163                          *  Type define for an attribute with both minimum and maximum values (e.g. Logical Min/Max).\r
164                          */\r
165                         typedef struct\r
166                         {\r
167                                 uint32_t Minimum; /**< Minimum value for the attribute. */\r
168                                 uint32_t Maximum; /**< Maximum value for the attribute. */\r
169                         } HID_MinMax_t;\r
170 \r
171                         /** \brief HID Parser Report Item Unit Structure.\r
172                          *\r
173                          *  Type define for the Unit attributes of a report item.\r
174                          */\r
175                         typedef struct\r
176                         {\r
177                                 uint32_t Type;     /**< Unit type (refer to HID specifications for details). */\r
178                                 uint8_t  Exponent; /**< Unit exponent (refer to HID specifications for details). */\r
179                         } HID_Unit_t;\r
180 \r
181                         /** \brief HID Parser Report Item Usage Structure.\r
182                          *\r
183                          *  Type define for the Usage attributes of a report item.\r
184                          */\r
185                         typedef struct\r
186                         {\r
187                                 uint16_t Page;  /**< Usage page of the report item. */\r
188                                 uint16_t Usage; /**< Usage of the report item. */\r
189                         } HID_Usage_t;\r
190 \r
191                         /** \brief HID Parser Report Item Collection Path Structure.\r
192                          *\r
193                          *  Type define for a COLLECTION object. Contains the collection attributes and a reference to the\r
194                          *  parent collection if any.\r
195                          */\r
196                         typedef struct HID_CollectionPath\r
197                         {\r
198                                 uint8_t                    Type;   /**< Collection type (e.g. "Generic Desktop"). */\r
199                                 HID_Usage_t                Usage;  /**< Collection usage. */\r
200                                 struct HID_CollectionPath* Parent; /**< Reference to parent collection, or \c NULL if root collection. */\r
201                         } HID_CollectionPath_t;\r
202 \r
203                         /** \brief HID Parser Report Item Attributes Structure.\r
204                          *\r
205                          *  Type define for all the data attributes of a report item, except flags.\r
206                          */\r
207                         typedef struct\r
208                         {\r
209                                 uint8_t      BitSize;  /**< Size in bits of the report item's data. */\r
210 \r
211                                 HID_Usage_t  Usage;    /**< Usage of the report item. */\r
212                                 HID_Unit_t   Unit;     /**< Unit type and exponent of the report item. */\r
213                                 HID_MinMax_t Logical;  /**< Logical minimum and maximum of the report item. */\r
214                                 HID_MinMax_t Physical; /**< Physical minimum and maximum of the report item. */\r
215                         } HID_ReportItem_Attributes_t;\r
216 \r
217                         /** \brief HID Parser Report Item Details Structure.\r
218                          *\r
219                          *  Type define for a report item (IN, OUT or FEATURE) layout attributes and other details.\r
220                          */\r
221                         typedef struct\r
222                         {\r
223                                 uint16_t                    BitOffset;      /**< Bit offset in the IN, OUT or FEATURE report of the item. */\r
224                                 uint8_t                     ItemType;       /**< Report item type, a value in \ref HID_ReportItemTypes_t. */\r
225                                 uint16_t                    ItemFlags;      /**< Item data flags, a mask of \c HID_IOF_* constants. */\r
226                                 uint8_t                     ReportID;       /**< Report ID this item belongs to, or 0x00 if device has only one report */\r
227                                 HID_CollectionPath_t*       CollectionPath; /**< Collection path of the item. */\r
228 \r
229                                 HID_ReportItem_Attributes_t Attributes;     /**< Report item attributes. */\r
230 \r
231                                 uint32_t                    Value;          /**< Current value of the report item - use \ref HID_ALIGN_DATA() when processing\r
232                                                                              *   a retrieved value so that it is aligned to a specific type.\r
233                                                                              */\r
234                                 uint32_t                    PreviousValue;  /**< Previous value of the report item. */\r
235                         } HID_ReportItem_t;\r
236 \r
237                         /** \brief HID Parser Report Size Structure.\r
238                          *\r
239                          *  Type define for a report item size information structure, to retain the size of a device's reports by ID.\r
240                          */\r
241                         typedef struct\r
242                         {\r
243                                 uint8_t  ReportID; /**< Report ID of the report within the HID interface. */\r
244                                 uint16_t ReportSizeBits[3]; /**< Total number of bits in each report type for the given Report ID,\r
245                                                              *   indexed by the \ref HID_ReportItemTypes_t enum.\r
246                                                              */\r
247                         } HID_ReportSizeInfo_t;\r
248 \r
249                         /** \brief HID Parser State Structure.\r
250                          *\r
251                          *  Type define for a complete processed HID report, including all report item data and collections.\r
252                          */\r
253                         typedef struct\r
254                         {\r
255                                 uint8_t              TotalReportItems; /**< Total number of report items stored in the \c ReportItems array. */\r
256                                 HID_ReportItem_t     ReportItems[HID_MAX_REPORTITEMS]; /**< Report items array, including all IN, OUT\r
257                                                                                     *   and FEATURE items.\r
258                                                                                         */\r
259                                 HID_CollectionPath_t CollectionPaths[HID_MAX_COLLECTIONS]; /**< All collection items, referenced\r
260                                                                                             *   by the report items.\r
261                                                                                             */\r
262                                 uint8_t              TotalDeviceReports; /**< Number of reports within the HID interface */\r
263                                 HID_ReportSizeInfo_t ReportIDSizes[HID_MAX_REPORT_IDS]; /**< Report sizes for each report in the interface */\r
264                                 uint16_t             LargestReportSizeBits; /**< Largest report that the attached device will generate, in bits */\r
265                                 bool                 UsingReportIDs; /**< Indicates if the device has at least one REPORT ID\r
266                                                                       *   element in its HID report descriptor.\r
267                                                                       */\r
268                         } HID_ReportInfo_t;\r
269 \r
270                 /* Function Prototypes: */\r
271                         /** Function to process a given HID report returned from an attached device, and store it into a given\r
272                          *  \ref HID_ReportInfo_t structure.\r
273                          *\r
274                          *  \param[in]  ReportData  Buffer containing the device's HID report table.\r
275                          *  \param[in]  ReportSize  Size in bytes of the HID report table.\r
276                          *  \param[out] ParserData  Pointer to a \ref HID_ReportInfo_t instance for the parser output.\r
277                          *\r
278                          *  \return A value in the \ref HID_Parse_ErrorCodes_t enum.\r
279                          */\r
280                         uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,\r
281                                                      uint16_t ReportSize,\r
282                                                      HID_ReportInfo_t* const ParserData) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);\r
283 \r
284                         /** Extracts the given report item's value out of the given HID report and places it into the Value\r
285                          *  member of the report item's \ref HID_ReportItem_t structure.\r
286                          *\r
287                          *  When called on a report with an item that exists in that report, this copies the report item's \c Value\r
288                          *  to its \c PreviousValue element for easy checking to see if an item's value has changed before processing\r
289                          *  a report. If the given item does not exist in the report, the function does not modify the report item's\r
290                          *  data.\r
291                          *\r
292                          *  \param[in]     ReportData  Buffer containing an IN or FEATURE report from an attached device.\r
293                          *  \param[in,out] ReportItem  Pointer to the report item of interest in a \ref HID_ReportInfo_t ReportItem array.\r
294                          *\r
295                          *  \returns Boolean \c true if the item to retrieve was located in the given report, \c false otherwise.\r
296                          */\r
297                         bool USB_GetHIDReportItemInfo(const uint8_t* ReportData,\r
298                                                       HID_ReportItem_t* const ReportItem) ATTR_NON_NULL_PTR_ARG(1);\r
299 \r
300                         /** Retrieves the given report item's value out of the \c Value member of the report item's\r
301                          *  \ref HID_ReportItem_t structure and places it into the correct position in the HID report\r
302                          *  buffer. The report buffer is assumed to have the appropriate bits cleared before calling\r
303                          *  this function (i.e., the buffer should be explicitly cleared before report values are added).\r
304                          *\r
305                          *  When called, this copies the report item's \c Value element to its \c PreviousValue element for easy\r
306                          *  checking to see if an item's value has changed before sending a report.\r
307                          *\r
308                          *  If the device has multiple HID reports, the first byte in the report is set to the report ID of the given item.\r
309                          *\r
310                          *  \param[out] ReportData  Buffer holding the current OUT or FEATURE report data.\r
311                          *  \param[in]  ReportItem  Pointer to the report item of interest in a \ref HID_ReportInfo_t ReportItem array.\r
312                          */\r
313                         void USB_SetHIDReportItemInfo(uint8_t* ReportData,\r
314                                                       HID_ReportItem_t* const ReportItem) ATTR_NON_NULL_PTR_ARG(1);\r
315 \r
316                         /** Retrieves the size of a given HID report in bytes from its Report ID.\r
317                          *\r
318                          *  \param[in] ParserData  Pointer to a \ref HID_ReportInfo_t instance containing the parser output.\r
319                          *  \param[in] ReportID    Report ID of the report whose size is to be determined.\r
320                          *  \param[in] ReportType  Type of the report whose size is to be determined, a value from the\r
321                          *                         \ref HID_ReportItemTypes_t enum.\r
322                          *\r
323                          *  \return Size of the report in bytes, or \c 0 if the report does not exist.\r
324                          */\r
325                         uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData,\r
326                                                       const uint8_t ReportID,\r
327                                                       const uint8_t ReportType) ATTR_CONST ATTR_NON_NULL_PTR_ARG(1);\r
328 \r
329                         /** Callback routine for the HID Report Parser. This callback <b>must</b> be implemented by the user code when\r
330                          *  the parser is used, to determine what report IN, OUT and FEATURE item's information is stored into the user\r
331                          *  \ref HID_ReportInfo_t structure. This can be used to filter only those items the application will be using, so that\r
332                          *  no RAM is wasted storing the attributes for report items which will never be referenced by the application.\r
333                          *\r
334                          *  Report item pointers passed to this callback function may be cached by the user application for later use\r
335                          *  when processing report items. This provides faster report processing in the user application than would\r
336                          *  a search of the entire parsed report item table for each received or sent report.\r
337                          *\r
338                          *  \param[in] CurrentItem  Pointer to the current report item for user checking.\r
339                          *\r
340                          *  \return Boolean \c true if the item should be stored into the \ref HID_ReportInfo_t structure, \c false if\r
341                          *          it should be ignored.\r
342                          */\r
343                         bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_t* const CurrentItem);\r
344 \r
345         /* Private Interface - For use in library only: */\r
346         #if !defined(__DOXYGEN__)\r
347                 /* Type Defines: */\r
348                         typedef struct\r
349                         {\r
350                                  HID_ReportItem_Attributes_t Attributes;\r
351                                  uint8_t                     ReportCount;\r
352                                  uint8_t                     ReportID;\r
353                         } HID_StateTable_t;\r
354         #endif\r
355 \r
356         /* Disable C linkage for C++ Compilers: */\r
357                 #if defined(__cplusplus)\r
358                         }\r
359                 #endif\r
360 \r
361 #endif\r
362 \r
363 /** @} */\r
364 \r