hid_parser.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /****************************************************************************
  2. * include/nuttx/usb/hid_parser.h
  3. *
  4. * Copyright (C) 2011, 2015 Gregory Nutt. All rights reserved.
  5. *
  6. * Adapted from the LUFA Library:
  7. *
  8. * Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. * dean [at] fourwalledcubicle [dot] com, www.lufa-lib.org
  10. *
  11. * Permission to use, copy, modify, distribute, and sell this
  12. * software and its documentation for any purpose is hereby granted
  13. * without fee, provided that the above copyright notice appear in
  14. * all copies and that both that the copyright notice and this
  15. * permission notice and warranty disclaimer appear in supporting
  16. * documentation, and that the name of the author not be used in
  17. * advertising or publicity pertaining to distribution of the
  18. * software without specific, written prior permission.
  19. *
  20. * The author disclaim all warranties with regard to this
  21. * software, including all implied warranties of merchantability
  22. * and fitness. In no event shall the author be liable for any
  23. * special, indirect or consequential damages or any damages
  24. * whatsoever resulting from loss of use, data or profits, whether
  25. * in an action of contract, negligence or other tortious action,
  26. * arising out of or in connection with the use or performance of
  27. * this software.
  28. *
  29. ****************************************************************************/
  30. #ifndef __INCLUDE_NUTTX_USB_HID_PARSER_H
  31. #define __INCLUDE_NUTTX_USB_HID_PARSER_H
  32. /****************************************************************************
  33. * Included Files
  34. ****************************************************************************/
  35. #include <string.h>
  36. #include <stdbool.h>
  37. /****************************************************************************
  38. * Pre-processor Definitions
  39. ****************************************************************************/
  40. /* Configuration ************************************************************/
  41. /* CONFIG_HID_STATEDEPTH
  42. * Constant indicating the maximum stack depth of the state table. A larger
  43. * state table allows for more PUSH/POP report items to be nested, but
  44. * consumes more memory. By default this is set to 2 levels (allowing non-
  45. * nested PUSH items) but this can be overridden by defining
  46. * CONFIG_HID_STATEDEPTH in the Nuttx config file.
  47. *
  48. * CONFIG_HID_USAGEDEPTH
  49. * Constant indicating the maximum stack depth of the usage table. A larger
  50. * usage table allows for more USAGE items to be indicated sequentially for
  51. * REPORT COUNT entries of more than one, but requires more stack space. By
  52. * default this is set to 8 levels (allowing for a report item with a count
  53. * of 8) but this can be overridden by defining CONFIG_HID_USAGEDEPTH to
  54. * in the Nuttx config file.
  55. *
  56. * CONFIG_HID_MAXCOLLECTIONS
  57. * Constant indicating the maximum number of COLLECTION items (nested or
  58. * unnested) that can be processed in the report item descriptor. A large
  59. * value allows for more COLLECTION items to be processed, but consumes
  60. * more memory. By default this is set to 10 collections, but this can be
  61. * overridden by defining CONFIG_HID_MAXCOLLECTIONS in the Nuttx config file.
  62. *
  63. * CONFIG_HID_MAXITEMS
  64. * Constant indicating the maximum number of report items (IN, OUT or
  65. * FEATURE) that can be processed in the report item descriptor and stored
  66. * in the user HID Report Info structure. A large value allows
  67. * for more report items to be stored, but consumes more memory. By default
  68. * this is set to 20 items, but this can be overridden by defining
  69. * CONFIG_HID_MAXITEMS in the Nuttx config file.
  70. *
  71. * CONFIG_HID_MAXIDS
  72. * Constant indicating the maximum number of unique report IDs that can be
  73. * processed in the report item descriptor for the report size information
  74. * array in the user HID Report Info structure. A large value allows for
  75. * more report ID report sizes to be stored, but consumes more memory. By
  76. * default this is set to 10 items, but this can be overridden by defining
  77. * CONFIG_HID_MAXIDS in the Nuttx config file. Note that IN, OUT and FEATURE
  78. * items sharing the same report ID consume only one size item in the array.
  79. */
  80. #ifndef CONFIG_HID_STATEDEPTH
  81. # define CONFIG_HID_STATEDEPTH 2
  82. #endif
  83. #ifndef CONFIG_HID_USAGEDEPTH
  84. # define CONFIG_HID_USAGEDEPTH 8
  85. #endif
  86. #ifndef CONFIG_HID_MAXCOLLECTIONS
  87. # define CONFIG_HID_MAXCOLLECTIONS 10
  88. #endif
  89. #ifndef CONFIG_HID_MAXITEMS
  90. # define CONFIG_HID_MAXITEMS 20
  91. #endif
  92. #ifndef CONFIG_HID_MAXIDS
  93. # define CONFIG_HID_MAXIDS 10
  94. #endif
  95. /* HID report type indices */
  96. #define HID_REPORT_ITEM_IN 0
  97. #define HID_REPORT_ITEM_OUT 1
  98. #define HID_REPORT_ITEM_FEATURE 2
  99. /****************************************************************************
  100. * Public Types
  101. ****************************************************************************/
  102. /* HID Parser Report Item Min/Max Structure. Type define for an attribute
  103. * with both minimum and maximum values (e.g. Logical Min/Max).
  104. */
  105. struct hid_range_s
  106. {
  107. uint32_t min; /* Minimum value for the attribute */
  108. uint32_t max; /* Maximum value for the attribute */
  109. };
  110. /* HID Parser Report Item Unit Structure. Type define for the Unit attributes
  111. * of a report item.
  112. */
  113. struct hid_unit_t
  114. {
  115. uint32_t type; /* Unit type (refer to HID spec for details) */
  116. uint8_t exponent; /* Unit exponent (refer to HID spec for details) */
  117. };
  118. /* HID Parser Report Item Usage Structure. Type define for the Usage
  119. * attributes of a report item.
  120. */
  121. struct hid_usage_t
  122. {
  123. uint16_t page; /* Usage page of the report item */
  124. uint16_t usage; /* Usage of the report item */
  125. };
  126. /* HID Parser Report Item Collection Path Structure. Type define for a
  127. * COLLECTION object. Contains the collection attributes and a reference to
  128. * the parent collection if any.
  129. */
  130. struct hid_collectionpath_s
  131. {
  132. uint8_t type; /* Collection type (e.g. "Generic Desktop") */
  133. struct hid_usage_t usage; /* Collection usage */
  134. struct hid_collectionpath_s *parent; /* Reference to parent collection (NULL if root) */
  135. };
  136. /* HID Parser Report Item Attributes Structure. Type define for all the data
  137. * attributes of a report item, except flags.
  138. */
  139. struct hid_rptitem_attributes_s
  140. {
  141. uint8_t bitsize; /* Size in bits of the report item's data */
  142. struct hid_usage_t usage; /* Usage of the report item */
  143. struct hid_unit_t unit; /* Unit type and exponent of the report item */
  144. struct hid_range_s logical; /* Logical minimum and maximum of the report item */
  145. struct hid_range_s physical; /* Physical minimum and maximum of the report item */
  146. };
  147. /* HID Parser Report Item Details Structure. Type define for a report item
  148. * (IN, OUT or FEATURE) layout attributes and other details.
  149. */
  150. struct hid_rptitem_s
  151. {
  152. uint16_t bitoffset; /* Bit offset in IN, OUT or FEATURE report of the item */
  153. uint8_t type; /* Report item type */
  154. uint16_t flags; /* Item data flags */
  155. uint8_t id; /* Report ID this item belongs to (0 if only one report) */
  156. struct hid_collectionpath_s *collectionpath; /* Collection path of the item */
  157. struct hid_rptitem_attributes_s attrib; /* Report item attributes */
  158. uint32_t value; /* Current value of the report item */
  159. uint32_t previous; /* Previous value of the report item */
  160. };
  161. /* HID Parser Report Size Structure. Type define for a report item size
  162. * information structure, to retain the size of a device's reports by ID.
  163. */
  164. struct hid_rptsizeinfo_s
  165. {
  166. uint8_t id; /* Report ID of the report within the HID interface */
  167. uint16_t size[3]; /* Number of bits in report type for the Report ID */
  168. };
  169. /* HID Parser State Structure. Type define for a complete processed HID
  170. * report, including all report item data and collections.
  171. */
  172. struct hid_rptinfo_s
  173. {
  174. /* nitems is the number of report items stored in the report items array (rptitems[]). */
  175. uint8_t nitems;
  176. struct hid_rptitem_s items[CONFIG_HID_MAXITEMS];
  177. /* All collection items, referenced by the report items. */
  178. struct hid_collectionpath_s collectionpaths[CONFIG_HID_MAXCOLLECTIONS];
  179. uint8_t nreports; /* Number of reports within the HID interface */
  180. struct hid_rptsizeinfo_s rptsize[CONFIG_HID_MAXIDS]; /* Report sizes for each report in the interface */
  181. uint16_t maxrptsize; /* Largest report that the attached device will generate, in bits */
  182. bool haverptid; /* Device has at least one REPORT ID in its HID report */
  183. };
  184. /* Callback routine for the HID Report Parser. This callback must be
  185. * implemented by the user code when the parser is used, to determine what
  186. * report IN, OUT and FEATURE item's information is stored into the user
  187. * struct hid_rptinfo_s structure. This can be used to filter only those
  188. * items the application will be using, so that no RAM is wasted storing
  189. * the attributes for report items which will never be referenced by the
  190. * application.
  191. *
  192. * Input Parameters:
  193. * item Pointer to the current report item for user checking.
  194. *
  195. * Returned Value:
  196. * Boolean true if the item should be stored into the struct hid_rptinfo_s
  197. * structure, false if it should be ignored.
  198. */
  199. typedef CODE bool (*hid_rptfilter_t)(FAR struct hid_rptitem_s *item);
  200. /****************************************************************************
  201. * Public Function Prototypes
  202. ****************************************************************************/
  203. #undef EXTERN
  204. #if defined(__cplusplus)
  205. # define EXTERN extern "C"
  206. extern "C"
  207. {
  208. #else
  209. # define EXTERN extern
  210. #endif
  211. /****************************************************************************
  212. * Name: hid_parsereport
  213. *
  214. * Description:
  215. * Function to process a given HID report returned from an attached device,
  216. * and store it into a given struct hid_rptinfo_s structure.
  217. *
  218. * Input Parameters:
  219. * report Buffer containing the device's HID report table.
  220. * rptlen Size in bytes of the HID report table.
  221. * filter Callback function to decide if an item should be retained
  222. * rptinfo Pointer to a struct hid_rptinfo_s instance for the parser
  223. * output.
  224. *
  225. * Returned Value:
  226. * Zero on success, otherwise a negated errno value.
  227. ****************************************************************************/
  228. int hid_parsereport(FAR const uint8_t *report, int rptlen,
  229. hid_rptfilter_t filter,
  230. FAR struct hid_rptinfo_s *rptinfo);
  231. /****************************************************************************
  232. * Name: hid_getitem
  233. *
  234. * Description:
  235. * Extracts the given report item's value out of the given HID report and
  236. * places it into the value member of the report item's struct
  237. * hid_rptitem_s structure.
  238. *
  239. * When called on a report with an item that exists in that report, this
  240. * copies the report item's Value to it's previous element for easy
  241. * checking to see if an item's value has changed before processing a
  242. * report. If the given item does not exist in the report, the function
  243. * does not modify the report item's data.
  244. *
  245. * Input Parameters:
  246. * report Buffer containing an IN or FEATURE report from an attached
  247. * device.
  248. * item Pointer to the report item of interest in a struct hid_rptinfo_s
  249. * item array.
  250. *
  251. * Returned Value:
  252. * Zero on success, otherwise a negated errno value.
  253. *
  254. ****************************************************************************/
  255. int hid_getitem(FAR const uint8_t *report, FAR struct hid_rptitem_s *item);
  256. /****************************************************************************
  257. * Name: hid_putitem
  258. *
  259. * Description:
  260. * Retrieves the given report item's value out of the value member of the
  261. * report item's struct hid_rptitem_s structure and places it into the
  262. * correct position in the HID report buffer. The report buffer is assumed
  263. * to have the appropriate bits cleared before calling this function (i.e.,
  264. * the buffer should be explicitly cleared before report values are added).
  265. *
  266. * When called, this copies the report item's Value element to it's
  267. * previous element for easy checking to see if an item's value has
  268. * changed before sending a report.
  269. *
  270. * If the device has multiple HID reports, the first byte in the report is
  271. * set to the report ID of the given item.
  272. *
  273. * Input Parameters:
  274. * report Buffer holding the current OUT or FEATURE report data.
  275. * item Pointer to the report item of interest in a struct
  276. * hid_rptinfo_s item array.
  277. *
  278. ****************************************************************************/
  279. #if 0 /* Not needed by host */
  280. void hid_putitem(FAR uint8_t *report, FAR struct hid_rptitem_s *item);
  281. #endif
  282. /****************************************************************************
  283. * Name: hid_reportsize
  284. *
  285. * Description:
  286. * Retrieves the size of a given HID report in bytes from it's Report ID.
  287. *
  288. * Input Parameters:
  289. * rptinfo Pointer to a struct hid_rptinfo_s instance containing the parser
  290. * output.
  291. * id Report ID of the report whose size is to be retrieved.
  292. * rpttype Type of the report whose size is to be determined, a valued from
  293. * the HID_ReportItemTypes_t enum.
  294. *
  295. * Size of the report in bytes, or 0 if the report does not exist.
  296. *
  297. ****************************************************************************/
  298. size_t hid_reportsize(FAR struct hid_rptinfo_s *rptinfo, uint8_t id,
  299. uint8_t rpttype);
  300. #undef EXTERN
  301. #if defined(__cplusplus)
  302. }
  303. #endif
  304. #endif /* __INCLUDE_NUTTX_USB_HID_PARSER_H */