HIDReportViewer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 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. *
  28. * Main source file for the HIDReportViewer project. This file contains the main tasks of
  29. * the project and is responsible for the initial application hardware configuration.
  30. */
  31. #include "HIDReportViewer.h"
  32. /** Processed HID report descriptor items structure, containing information on each HID report element */
  33. static HID_ReportInfo_t HIDReportInfo;
  34. /** LUFA HID Class driver interface configuration and state information. This structure is
  35. * passed to all HID Class driver functions, so that multiple instances of the same class
  36. * within a device can be differentiated from one another.
  37. */
  38. USB_ClassInfo_HID_Host_t Device_HID_Interface =
  39. {
  40. .Config =
  41. {
  42. .DataINPipe =
  43. {
  44. .Address = (PIPE_DIR_IN | 1),
  45. .Banks = 1,
  46. },
  47. .DataOUTPipe =
  48. {
  49. .Address = (PIPE_DIR_OUT | 2),
  50. .Banks = 1,
  51. },
  52. .HIDInterfaceProtocol = HID_CSCP_NonBootProtocol,
  53. .HIDParserData = &HIDReportInfo
  54. },
  55. };
  56. /** Main program entry point. This routine configures the hardware required by the application, then
  57. * enters a loop to run the application tasks in sequence.
  58. */
  59. int main(void)
  60. {
  61. SetupHardware();
  62. puts_P(PSTR(ESC_FG_CYAN "HID Device Report Viewer Running.\r\n" ESC_FG_WHITE));
  63. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  64. GlobalInterruptEnable();
  65. for (;;)
  66. {
  67. RetrieveDeviceData();
  68. HID_Host_USBTask(&Device_HID_Interface);
  69. USB_USBTask();
  70. }
  71. }
  72. /** Task to retrieve the HID device information from an attached device, and output
  73. * the relevant data to the serial port for analysis.
  74. */
  75. void RetrieveDeviceData(void)
  76. {
  77. if (USB_HostState != HOST_STATE_Configured)
  78. return;
  79. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  80. OutputReportSizes();
  81. OutputParsedReportItems();
  82. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  83. USB_Host_SetDeviceConfiguration(0);
  84. }
  85. /** Prints a summary of the device's HID report sizes from the HID parser output to the serial port
  86. * for display to the user.
  87. */
  88. void OutputReportSizes(void)
  89. {
  90. printf_P(PSTR("\r\n\r\nTotal Device Reports: %" PRId8 "\r\n"), HIDReportInfo.TotalDeviceReports);
  91. for (uint8_t ReportIndex = 0; ReportIndex < HIDReportInfo.TotalDeviceReports; ReportIndex++)
  92. {
  93. const HID_ReportSizeInfo_t* CurrReportIDInfo = &HIDReportInfo.ReportIDSizes[ReportIndex];
  94. uint8_t ReportSizeInBits = CurrReportIDInfo->ReportSizeBits[HID_REPORT_ITEM_In];
  95. uint8_t ReportSizeOutBits = CurrReportIDInfo->ReportSizeBits[HID_REPORT_ITEM_Out];
  96. uint8_t ReportSizeFeatureBits = CurrReportIDInfo->ReportSizeBits[HID_REPORT_ITEM_Feature];
  97. /* Print out the byte sizes of each report within the device */
  98. printf_P(PSTR(" + Report ID 0x%02" PRIX8 "\r\n"
  99. " - Input Data: %" PRId8 " bits (%" PRId8 " bytes)\r\n"
  100. " - Output Data: %" PRId8 " bits (%" PRId8 " bytes)\r\n"
  101. " - Feature Data: %" PRId8 " bits (%" PRId8 " bytes)\r\n"),
  102. CurrReportIDInfo->ReportID,
  103. ReportSizeInBits,
  104. ((ReportSizeInBits >> 3) + ((ReportSizeInBits & 0x07) != 0)),
  105. ReportSizeOutBits,
  106. ((ReportSizeOutBits >> 3) + ((ReportSizeOutBits & 0x07) != 0)),
  107. ReportSizeFeatureBits,
  108. ((ReportSizeFeatureBits >> 3) + ((ReportSizeFeatureBits & 0x07) != 0)));
  109. }
  110. }
  111. /** Prints a summary of the device's parsed and stored report items along with their attributes
  112. * to the serial port for display to the user.
  113. */
  114. void OutputParsedReportItems(void)
  115. {
  116. printf_P(PSTR("\r\nReport Items (%" PRId8 " in Table):\r\n"), HIDReportInfo.TotalReportItems);
  117. for (uint8_t ItemIndex = 0; ItemIndex < HIDReportInfo.TotalReportItems; ItemIndex++)
  118. {
  119. const HID_ReportItem_t* RItem = &HIDReportInfo.ReportItems[ItemIndex];
  120. printf_P(PSTR(" + Item %" PRId8 ":\r\n"
  121. " - Report ID: 0x%02" PRIX8 "\r\n"
  122. " - Data Direction: %s\r\n"
  123. " - Item Flags: 0x%02" PRIX8 "\r\n"
  124. " - Item Offset (Bits): 0x%02" PRIX8 "\r\n"
  125. " - Item Size (Bits): 0x%02" PRIX8 "\r\n"
  126. " - Usage Page: 0x%04" PRIX16 "\r\n"
  127. " - Usage: 0x%04" PRIX16 "\r\n"
  128. " - Unit Type: 0x%08" PRIX32 "\r\n"
  129. " - Unit Exponent: 0x%02" PRIX8 "\r\n"
  130. " - Logical Minimum: 0x%08" PRIX32 "\r\n"
  131. " - Logical Maximum: 0x%08" PRIX32 "\r\n"
  132. " - Physical Minimum: 0x%08" PRIX32 "\r\n"
  133. " - Physical Maximum: 0x%08" PRIX32 "\r\n"
  134. " - Collection Path:\r\n"),
  135. ItemIndex,
  136. RItem->ReportID,
  137. ((RItem->ItemType == HID_REPORT_ITEM_In) ? "IN" : ((RItem->ItemType == HID_REPORT_ITEM_Out) ? "OUT" : "FEATURE")),
  138. RItem->ItemFlags,
  139. RItem->BitOffset,
  140. RItem->Attributes.BitSize,
  141. RItem->Attributes.Usage.Page,
  142. RItem->Attributes.Usage.Usage,
  143. RItem->Attributes.Unit.Type,
  144. RItem->Attributes.Unit.Exponent,
  145. RItem->Attributes.Logical.Minimum,
  146. RItem->Attributes.Logical.Maximum,
  147. RItem->Attributes.Physical.Minimum,
  148. RItem->Attributes.Physical.Maximum);
  149. OutputCollectionPath(RItem->CollectionPath);
  150. }
  151. }
  152. /** Prints the HID Collection path (along with each node's attributes) to the serial port
  153. * for display to the user, from the given starting node to the root node.
  154. *
  155. * \param[in] CollectionPath Starting HID Collection node to print
  156. */
  157. void OutputCollectionPath(const HID_CollectionPath_t* const CollectionPath)
  158. {
  159. const HID_CollectionPath_t* CurrentNode = CollectionPath;
  160. while (CurrentNode != NULL)
  161. {
  162. printf_P(PSTR(" |\r\n"
  163. " - Type: 0x%02" PRIX8 "\r\n"
  164. " - Usage: 0x%02" PRIX8 "\r\n"),
  165. CurrentNode->Type, CurrentNode->Usage);
  166. CurrentNode = CurrentNode->Parent;
  167. }
  168. printf_P(PSTR(" |\r\n"
  169. " END\r\n"));
  170. }
  171. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  172. void SetupHardware(void)
  173. {
  174. #if (ARCH == ARCH_AVR8)
  175. /* Disable watchdog if enabled by bootloader/fuses */
  176. MCUSR &= ~(1 << WDRF);
  177. wdt_disable();
  178. /* Disable clock division */
  179. clock_prescale_set(clock_div_1);
  180. #endif
  181. /* Hardware Initialization */
  182. Serial_Init(9600, false);
  183. LEDs_Init();
  184. USB_Init();
  185. /* Create a stdio stream for the serial port for stdin and stdout */
  186. Serial_CreateStream(NULL);
  187. }
  188. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  189. * starts the library USB task to begin the enumeration and USB management process.
  190. */
  191. void EVENT_USB_Host_DeviceAttached(void)
  192. {
  193. puts_P(PSTR("Device Attached.\r\n"));
  194. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  195. }
  196. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  197. * stops the library USB task management process.
  198. */
  199. void EVENT_USB_Host_DeviceUnattached(void)
  200. {
  201. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  202. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  203. }
  204. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  205. * enumerated by the host and is now ready to be used by the application.
  206. */
  207. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  208. {
  209. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  210. uint16_t ConfigDescriptorSize;
  211. uint8_t ConfigDescriptorData[512];
  212. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  213. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  214. {
  215. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  216. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  217. return;
  218. }
  219. if (HID_Host_ConfigurePipes(&Device_HID_Interface,
  220. ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)
  221. {
  222. puts_P(PSTR("Attached Device Not a Valid HID Device.\r\n"));
  223. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  224. return;
  225. }
  226. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  227. {
  228. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  229. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  230. return;
  231. }
  232. if (HID_Host_SetReportProtocol(&Device_HID_Interface) != 0)
  233. {
  234. puts_P(PSTR("Error Setting Report Protocol Mode.\r\n"));
  235. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  236. USB_Host_SetDeviceConfiguration(0);
  237. return;
  238. }
  239. puts_P(PSTR("HID Device Enumerated.\r\n"));
  240. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  241. }
  242. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  243. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  244. {
  245. USB_Disable();
  246. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  247. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  248. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  249. for(;;);
  250. }
  251. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  252. * enumerating an attached USB device.
  253. */
  254. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  255. const uint8_t SubErrorCode)
  256. {
  257. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  258. " -- Error Code %d\r\n"
  259. " -- Sub Error Code %d\r\n"
  260. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  261. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  262. }
  263. /** Callback for the HID Report Parser. This function is called each time the HID report parser is about to store
  264. * an IN, OUT or FEATURE item into the HIDReportInfo structure. To save on RAM, we are able to filter out items
  265. * we aren't interested in (preventing us from being able to extract them later on, but saving on the RAM they would
  266. * have occupied).
  267. *
  268. * \param[in] CurrentItem Pointer to the item the HID report parser is currently working with
  269. *
  270. * \return Boolean \c true if the item should be stored into the HID report structure, \c false if it should be discarded
  271. */
  272. bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_t* const CurrentItem)
  273. {
  274. return true;
  275. }