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.

HIDParser.h 19KB

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