3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  21   The author disclaim 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 
  33  *  Main source file for the HIDReportViewer project. This file contains the main tasks of 
  34  *  the project and is responsible for the initial application hardware configuration. 
  37 #include "HIDReportViewer.h" 
  39 /** Processed HID report descriptor items structure, containing information on each HID report element */ 
  40 static HID_ReportInfo_t HIDReportInfo
; 
  42 /** LUFA HID Class driver interface configuration and state information. This structure is 
  43  *  passed to all HID Class driver functions, so that multiple instances of the same class 
  44  *  within a device can be differentiated from one another. 
  46 USB_ClassInfo_HID_Host_t Device_HID_Interface 
= 
  50                                 .DataINPipeNumber       
= 1, 
  51                                 .DataINPipeDoubleBank   
= false, 
  53                                 .DataOUTPipeNumber      
= 2, 
  54                                 .DataOUTPipeDoubleBank  
= false, 
  56                                 .HIDInterfaceProtocol   
= HID_CSCP_NonBootProtocol
, 
  58                                 .HIDParserData          
= &HIDReportInfo
 
  63 /** Main program entry point. This routine configures the hardware required by the application, then 
  64  *  enters a loop to run the application tasks in sequence. 
  70         puts_P(PSTR(ESC_FG_CYAN 
"HID Device Report Viewer Running.\r\n" ESC_FG_WHITE
)); 
  72         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  79                 HID_Host_USBTask(&Device_HID_Interface
); 
  84 /** Task to retrieve the HID device information from an attached device, and output 
  85  *  the relevant data to the serial port for analysis. 
  87 void RetrieveDeviceData(void) 
  89         if (USB_CurrentMode 
!= USB_MODE_Host
) 
  92         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
  95         OutputParsedReportItems(); 
  97         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
  98         USB_Host_SetDeviceConfiguration(0); 
 101 /** Prints a summary of the device's HID report sizes from the HID parser output to the serial port 
 102  *  for display to the user. 
 104 void OutputReportSizes(void) 
 106         printf_P(PSTR("\r\n\r\nTotal Device Reports: %" PRId8 
"\r\n"), HIDReportInfo
.TotalDeviceReports
); 
 108         for (uint8_t ReportIndex 
= 0; ReportIndex 
< HIDReportInfo
.TotalDeviceReports
; ReportIndex
++) 
 110                 const HID_ReportSizeInfo_t
* CurrReportIDInfo 
= &HIDReportInfo
.ReportIDSizes
[ReportIndex
]; 
 112                 uint8_t ReportSizeInBits      
= CurrReportIDInfo
->ReportSizeBits
[HID_REPORT_ITEM_In
]; 
 113                 uint8_t ReportSizeOutBits     
= CurrReportIDInfo
->ReportSizeBits
[HID_REPORT_ITEM_Out
]; 
 114                 uint8_t ReportSizeFeatureBits 
= CurrReportIDInfo
->ReportSizeBits
[HID_REPORT_ITEM_Feature
]; 
 116                 /* Print out the byte sizes of each report within the device */ 
 117                 printf_P(PSTR("  + Report ID 0x%02" PRIX8 
"\r\n" 
 118                                           "    - Input Data:   %" PRId8 
" bits (%" PRId8 
" bytes)\r\n" 
 119                                           "    - Output Data:  %" PRId8 
" bits (%" PRId8 
" bytes)\r\n" 
 120                                           "    - Feature Data: %" PRId8 
" bits (%" PRId8 
" bytes)\r\n"), 
 121                                  CurrReportIDInfo
->ReportID
, 
 123                                  ((ReportSizeInBits      
>> 3) + ((ReportSizeInBits      
& 0x07) != 0)), 
 125                                  ((ReportSizeOutBits     
>> 3) + ((ReportSizeOutBits     
& 0x07) != 0)), 
 126                                  ReportSizeFeatureBits
, 
 127                                  ((ReportSizeFeatureBits 
>> 3) + ((ReportSizeFeatureBits 
& 0x07) != 0))); 
 131 /** Prints a summary of the device's parsed and stored report items along with their attributes 
 132  *  to the serial port for display to the user. 
 134 void OutputParsedReportItems(void) 
 136         printf_P(PSTR("\r\nReport Items (%" PRId8 
" in Table):\r\n"), HIDReportInfo
.TotalReportItems
); 
 138         for (uint8_t ItemIndex 
= 0; ItemIndex 
< HIDReportInfo
.TotalReportItems
; ItemIndex
++) 
 140                 const HID_ReportItem_t
* RItem 
= &HIDReportInfo
.ReportItems
[ItemIndex
]; 
 142                 printf_P(PSTR("  + Item %" PRId8 
":\r\n" 
 143                                           "    - Report ID:          0x%02" PRIX8  
"\r\n" 
 144                                           "    - Data Direction:     %s\r\n" 
 145                                           "    - Item Flags:         0x%02" PRIX8  
"\r\n" 
 146                                           "    - Item Offset (Bits): 0x%02" PRIX8  
"\r\n" 
 147                                           "    - Item Size (Bits):   0x%02" PRIX8  
"\r\n" 
 148                                           "    - Usage Page:         0x%04" PRIX16 
"\r\n" 
 149                                           "    - Usage:              0x%04" PRIX16 
"\r\n" 
 150                                           "    - Unit Type:          0x%08" PRIX32 
"\r\n" 
 151                                           "    - Unit Exponent:      0x%02" PRIX8  
"\r\n" 
 152                                           "    - Logical Minimum:    0x%08" PRIX32 
"\r\n" 
 153                                           "    - Logical Maximum:    0x%08" PRIX32 
"\r\n" 
 154                                           "    - Physical Minimum:   0x%08" PRIX32 
"\r\n" 
 155                                           "    - Physical Maximum:   0x%08" PRIX32 
"\r\n" 
 156                                           "    - Collection Path:\r\n"), 
 159                                  ((RItem
->ItemType 
== HID_REPORT_ITEM_In
) ? 
"IN" : ((RItem
->ItemType 
== HID_REPORT_ITEM_Out
) ? 
"OUT" : "FEATURE")), 
 162                                  RItem
->Attributes
.BitSize
, 
 163                                  RItem
->Attributes
.Usage
.Page
, 
 164                                  RItem
->Attributes
.Usage
.Usage
, 
 165                                  RItem
->Attributes
.Unit
.Type
, 
 166                                  RItem
->Attributes
.Unit
.Exponent
, 
 167                                  RItem
->Attributes
.Logical
.Minimum
, 
 168                                  RItem
->Attributes
.Logical
.Maximum
, 
 169                                  RItem
->Attributes
.Physical
.Minimum
, 
 170                                  RItem
->Attributes
.Physical
.Maximum
); 
 172                 OutputCollectionPath(RItem
->CollectionPath
); 
 176 /** Prints the HID Collection path (along with each node's attributes) to the serial port 
 177  *  for display to the user, from the given starting node to the root node. 
 179  *  \param[in] CollectionPath  Starting HID Collection node to print 
 181 void OutputCollectionPath(const HID_CollectionPath_t
* const CollectionPath
) 
 183         const HID_CollectionPath_t
* CurrentNode 
= CollectionPath
; 
 185         while (CurrentNode 
!= NULL
) 
 187                 printf_P(PSTR("      |\r\n" 
 188                                           "      - Type:  0x%02" PRIX8 
"\r\n" 
 189                                           "      - Usage: 0x%02" PRIX8 
"\r\n"), 
 190                                           CurrentNode
->Type
, CurrentNode
->Usage
); 
 192                 CurrentNode 
= CurrentNode
->Parent
; 
 195         printf_P(PSTR("      |\r\n" 
 199 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 200 void SetupHardware(void) 
 202         /* Disable watchdog if enabled by bootloader/fuses */ 
 203         MCUSR 
&= ~(1 << WDRF
); 
 206         /* Disable clock division */ 
 207         clock_prescale_set(clock_div_1
); 
 209         /* Hardware Initialization */ 
 210         Serial_Init(9600, false); 
 214         /* Create a stdio stream for the serial port for stdin and stdout */ 
 215         Serial_CreateStream(NULL
); 
 218 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and 
 219  *  starts the library USB task to begin the enumeration and USB management process. 
 221 void EVENT_USB_Host_DeviceAttached(void) 
 223         puts_P(PSTR("Device Attached.\r\n")); 
 224         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 227 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and 
 228  *  stops the library USB task management process. 
 230 void EVENT_USB_Host_DeviceUnattached(void) 
 232         puts_P(PSTR("\r\nDevice Unattached.\r\n")); 
 233         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 236 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully 
 237  *  enumerated by the host and is now ready to be used by the application. 
 239 void EVENT_USB_Host_DeviceEnumerationComplete(void) 
 241         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 243         uint16_t ConfigDescriptorSize
; 
 244         uint8_t  ConfigDescriptorData
[512]; 
 246         if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
, 
 247                                                sizeof(ConfigDescriptorData
)) != HOST_GETCONFIG_Successful
) 
 249                 puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n")); 
 250                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 254         if (HID_Host_ConfigurePipes(&Device_HID_Interface
, 
 255                                     ConfigDescriptorSize
, ConfigDescriptorData
) != HID_ENUMERROR_NoError
) 
 257                 puts_P(PSTR("Attached Device Not a Valid HID Device.\r\n")); 
 258                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 262         if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
) 
 264                 puts_P(PSTR("Error Setting Device Configuration.\r\n")); 
 265                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 269         if (HID_Host_SetReportProtocol(&Device_HID_Interface
) != 0) 
 271                 puts_P(PSTR("Error Setting Report Protocol Mode.\r\n")); 
 272                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 273                 USB_Host_SetDeviceConfiguration(0); 
 277         puts_P(PSTR("HID Device Enumerated.\r\n")); 
 278         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 281 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ 
 282 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) 
 286         printf_P(PSTR(ESC_FG_RED 
"Host Mode Error\r\n" 
 287                                  " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 289         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 293 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while 
 294  *  enumerating an attached USB device. 
 296 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, 
 297                                             const uint8_t SubErrorCode
) 
 299         printf_P(PSTR(ESC_FG_RED 
"Dev Enum Error\r\n" 
 300                                  " -- Error Code %d\r\n" 
 301                                  " -- Sub Error Code %d\r\n" 
 302                                  " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
); 
 304         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 307 /** Callback for the HID Report Parser. This function is called each time the HID report parser is about to store 
 308  *  an IN, OUT or FEATURE item into the HIDReportInfo structure. To save on RAM, we are able to filter out items 
 309  *  we aren't interested in (preventing us from being able to extract them later on, but saving on the RAM they would 
 312  *  \param[in] CurrentItem  Pointer to the item the HID report parser is currently working with 
 314  *  \return Boolean true if the item should be stored into the HID report structure, false if it should be discarded 
 316 bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_t
* const CurrentItem
)