Add user-filtering to the HID report parser, so that the user code can decide which items are to be stored into the HID_ReportInfo_t structure and which should be discarded to save on RAM usage.

pull/1469/head
Dean Camera 16 years ago
parent ecf7c18cf2
commit 205b35d131

@ -70,3 +70,25 @@ uint8_t GetHIDReportData(void)
return ParseSuccessful; return ParseSuccessful;
} }
/** Callback for the HID Report Parser. This function is called each time the HID report parser is about to store
* an IN, OUT or FEATURE item into the HIDReportInfo structure. To save on RAM, we are able to filter out items
* we aren't interested in (preventing us from being able to extract them later on, but saving on the RAM they would
* have occupied).
*
* \param CurrentItemAttributes Pointer to the attrbutes of the item the HID report parser is currently working with
*
* \return Boolean true if the item should be stored into the HID report structure, false if it should be discarded
*/
bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_Attributes_t* CurrentItemAttributes)
{
/* Check the attributes of the current item - see if we are interested in it or not */
if (CurrentItemAttributes->Usage.Page == USAGE_PAGE_KEYBOARD)
{
/* Only store KEYBOARD usage page items into the Processed HID Report structure to save RAM */
return true;
}
else
{
return false;
}
}

@ -77,4 +77,6 @@
/* Function Prototypes: */ /* Function Prototypes: */
uint8_t GetHIDReportData(void); uint8_t GetHIDReportData(void);
bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_Attributes_t* CurrentItemAttributes);
#endif #endif

@ -69,3 +69,27 @@ uint8_t GetHIDReportData(void)
return ParseSuccessful; return ParseSuccessful;
} }
/** Callback for the HID Report Parser. This function is called each time the HID report parser is about to store
* an IN, OUT or FEATURE item into the HIDReportInfo structure. To save on RAM, we are able to filter out items
* we aren't interested in (preventing us from being able to extract them later on, but saving on the RAM they would
* have occupied).
*
* \param CurrentItemAttributes Pointer to the attrbutes of the item the HID report parser is currently working with
*
* \return Boolean true if the item should be stored into the HID report structure, false if it should be discarded
*/
bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_Attributes_t* CurrentItemAttributes)
{
/* Check the attributes of the current item - see if we are interested in it or not */
if ((CurrentItemAttributes->Usage.Page == USAGE_PAGE_BUTTON) ||
(CurrentItemAttributes->Usage.Page == USAGE_PAGE_GENERIC_DCTRL))
{
/* Only store BUTTON and GENERIC_DESKTOP_CONTROL items into the Processed HID Report structure to save RAM */
return true;
}
else
{
return false;
}
}

@ -86,4 +86,6 @@
/* Function Prototypes: */ /* Function Prototypes: */
uint8_t GetHIDReportData(void); uint8_t GetHIDReportData(void);
bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_Attributes_t* CurrentItemAttributes);
#endif #endif

@ -37,14 +37,12 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
{ {
HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH]; HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
HID_StateTable_t* CurrStateTable = &StateTable[0]; HID_StateTable_t* CurrStateTable = &StateTable[0];
HID_CollectionPath_t* CurrCollectionPath = NULL;
uint16_t UsageStack[HID_USAGE_STACK_DEPTH]; uint16_t UsageStack[HID_USAGE_STACK_DEPTH];
uint8_t UsageStackSize = 0; uint8_t UsageStackSize = 0;
uint16_t BitOffsetIn = 0; uint16_t BitOffsetIn = 0;
uint16_t BitOffsetOut = 0; uint16_t BitOffsetOut = 0;
#if defined(HID_ENABLE_FEATURE_PROCESSING)
uint16_t BitOffsetFeature = 0; uint16_t BitOffsetFeature = 0;
#endif
HID_CollectionPath_t* CurrCollectionPath = NULL;
ParserData->TotalReportItems = 0; ParserData->TotalReportItems = 0;
ParserData->UsingMultipleReports = false; ParserData->UsingMultipleReports = false;
@ -131,10 +129,7 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
ParserData->UsingMultipleReports = true; ParserData->UsingMultipleReports = true;
BitOffsetIn = 0; BitOffsetIn = 0;
BitOffsetOut = 0; BitOffsetOut = 0;
#if defined(HID_ENABLE_FEATURE_PROCESSING)
BitOffsetFeature = 0; BitOffsetFeature = 0;
#endif
break; break;
case (TYPE_LOCAL | TAG_LOCAL_USAGE): case (TYPE_LOCAL | TAG_LOCAL_USAGE):
if (UsageStackSize == HID_USAGE_STACK_DEPTH) if (UsageStackSize == HID_USAGE_STACK_DEPTH)
@ -197,27 +192,22 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
break; break;
case (TYPE_MAIN | TAG_MAIN_INPUT): case (TYPE_MAIN | TAG_MAIN_INPUT):
case (TYPE_MAIN | TAG_MAIN_OUTPUT): case (TYPE_MAIN | TAG_MAIN_OUTPUT):
#if defined(HID_ENABLE_FEATURE_PROCESSING)
case (TYPE_MAIN | TAG_MAIN_FEATURE): case (TYPE_MAIN | TAG_MAIN_FEATURE):
#endif
for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++) for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
{ {
HID_ReportItem_t* CurrReportItem = &ParserData->ReportItems[ParserData->TotalReportItems]; HID_ReportItem_t NewReportItem;
if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS) memcpy(&NewReportItem.Attributes,
return HID_PARSE_InsufficientReportItems;
memcpy(&CurrReportItem->Attributes,
&CurrStateTable->Attributes, &CurrStateTable->Attributes,
sizeof(HID_ReportItem_Attributes_t)); sizeof(HID_ReportItem_Attributes_t));
CurrReportItem->ItemFlags = ReportItemData; NewReportItem.ItemFlags = ReportItemData;
CurrReportItem->CollectionPath = CurrCollectionPath; NewReportItem.CollectionPath = CurrCollectionPath;
CurrReportItem->ReportID = CurrStateTable->ReportID; NewReportItem.ReportID = CurrStateTable->ReportID;
if (UsageStackSize) if (UsageStackSize)
{ {
CurrReportItem->Attributes.Usage.Usage = UsageStack[0]; NewReportItem.Attributes.Usage.Usage = UsageStack[0];
for (uint8_t i = 0; i < UsageStackSize; i++) for (uint8_t i = 0; i < UsageStackSize; i++)
UsageStack[i] = UsageStack[i + 1]; UsageStack[i] = UsageStack[i + 1];
@ -226,42 +216,41 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
} }
else else
{ {
CurrReportItem->Attributes.Usage.Usage = 0; NewReportItem.Attributes.Usage.Usage = 0;
} }
switch (HIDReportItem & TAG_MASK) switch (HIDReportItem & TAG_MASK)
{ {
case TAG_MAIN_INPUT: case TAG_MAIN_INPUT:
CurrReportItem->ItemType = REPORT_ITEM_TYPE_In; NewReportItem.ItemType = REPORT_ITEM_TYPE_In;
CurrReportItem->BitOffset = BitOffsetIn; NewReportItem.BitOffset = BitOffsetIn;
BitOffsetIn += CurrStateTable->Attributes.BitSize; BitOffsetIn += CurrStateTable->Attributes.BitSize;
break; break;
case TAG_MAIN_OUTPUT: case TAG_MAIN_OUTPUT:
CurrReportItem->ItemType = REPORT_ITEM_TYPE_Out; NewReportItem.ItemType = REPORT_ITEM_TYPE_Out;
CurrReportItem->BitOffset = BitOffsetOut; NewReportItem.BitOffset = BitOffsetOut;
BitOffsetOut += CurrStateTable->Attributes.BitSize; BitOffsetOut += CurrStateTable->Attributes.BitSize;
break; break;
#if defined(HID_ENABLE_FEATURE_PROCESSING)
case TAG_MAIN_FEATURE: case TAG_MAIN_FEATURE:
CurrReportItem->ItemType = REPORT_ITEM_TYPE_Feature; NewReportItem.ItemType = REPORT_ITEM_TYPE_Feature;
CurrReportItem->BitOffset = BitOffsetFeature; NewReportItem.BitOffset = BitOffsetFeature;
BitOffsetFeature += CurrStateTable->Attributes.BitSize; BitOffsetFeature += CurrStateTable->Attributes.BitSize;
break; break;
#endif
} }
#if defined(HID_INCLUDE_CONSTANT_DATA_ITEMS) if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&CurrStateTable->Attributes))
ParserData->TotalReportItems++; {
#else if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
if (!(ReportItemData & IOF_CONSTANT)) return HID_PARSE_InsufficientReportItems;
memcpy(&ParserData->ReportItems[ParserData->TotalReportItems],
&NewReportItem, sizeof(HID_ReportItem_t));
ParserData->TotalReportItems++; ParserData->TotalReportItems++;
#endif }
} }
UsageStackSize = 0; UsageStackSize = 0;

@ -46,16 +46,11 @@
* Functions, macros, variables, enums and types related to the parsing of HID class device report descriptors. * Functions, macros, variables, enums and types related to the parsing of HID class device report descriptors.
* *
* The processed HID report is presented back to the user application as a flat structure containing each report * The processed HID report is presented back to the user application as a flat structure containing each report
* item's IN, OUT and FEATURE (if desired) items along with each item's attributes. * item's IN, OUT and FEATURE items along with each item's attributes.
* *
* This library portion also allows for easy setting and retrieval of data from a HID report, including devices * This library portion also allows for easy setting and retrieval of data from a HID report, including devices
* with multiple reports on the one HID interface. * with multiple reports on the one HID interface.
* *
* By default, FEATURE reports and IN/OUT reports with constant data are ignored in the HID report when processed
* to save on memory. This can be overridden by defining the HID_ENABLE_FEATURE_PROCESSING or
* HID_INCLUDE_CONSTANT_DATA_ITEMS tokens in the user project makefile, passing them to the compiler via the -D
* switch.
*
* @{ * @{
*/ */
@ -108,13 +103,13 @@
#endif #endif
#if !defined(HID_MAX_REPORTITEMS) || defined(__DOXYGEN__) #if !defined(HID_MAX_REPORTITEMS) || defined(__DOXYGEN__)
/** Constant indicating the maximum number of report items (IN, OUT or FEATURE if enabled) that can be /** Constant indicating the maximum number of report items (IN, OUT or FEATURE) that can be processed
* processed in the report item descriptor. A large value allows for more report items to be * in the report item descriptor and stored in the user HID Report Info structure. A large value allows
* processed, but consumes more memory. By default this is set to 30 items, but this can be * for more report items to be stored, but consumes more memory. By default this is set to 20 items,
* overridden by defining HID_MAX_REPORTITEMS to another value in the user project makefile, passing * but this can be overridden by defining HID_MAX_REPORTITEMS to another value in the user project
* the define to the compiler using the -D compiler switch. * makefile, passing the define to the compiler using the -D compiler switch.
*/ */
#define HID_MAX_REPORTITEMS 30 #define HID_MAX_REPORTITEMS 20
#endif #endif
/* Public Interface - May be used in end-application: */ /* Public Interface - May be used in end-application: */
@ -251,6 +246,17 @@
void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem) void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)
ATTR_NON_NULL_PTR_ARG(1, 2); ATTR_NON_NULL_PTR_ARG(1, 2);
/** Callback routine for the HID Report Parser. This callback <b>must</b> be implemented by the user code when
* the parser is used, to determine what report IN, OUT and FEATURE item's information is stored into the user
* HID_ReportInfo_t structure. This can be used to filter only those items the application will be using, so that
* no RAM is wasted storing the attributes for report items which will never be referenced by the application.
*
* \param CurrentItemAttributes Pointer to the current report item attributes for user checking
*
* \return Boolean true if the item should be stored into the HID_ReportInfo_t structure, false if it should be ignored
*/
bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_Attributes_t* CurrentItemAttributes);
/* Private Interface - For use in library only: */ /* Private Interface - For use in library only: */
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)
/* Type Defines: */ /* Type Defines: */

@ -18,6 +18,8 @@
* - Added ShutDown() functions for all hardware peripheral drivers, so that peripherals can be turned off after use * - Added ShutDown() functions for all hardware peripheral drivers, so that peripherals can be turned off after use
* - Added new CDC_Device_Flush() command to the device mode CDC Class driver to flush Device->Host data * - Added new CDC_Device_Flush() command to the device mode CDC Class driver to flush Device->Host data
* - Added extra masks to the SPI driver, changed SPI_Init() so that the clock polarity and sample modes can be set * - Added extra masks to the SPI driver, changed SPI_Init() so that the clock polarity and sample modes can be set
* - Added new callback to the HID report parser, so that the user application can filter only the items it is interested
* in to be stored into the HIDReportInfo structure to save RAM
* *
* <b>Changed:</b> * <b>Changed:</b>
* - SetIdle requests to the HID device driver with a 0 idle period (send changes only) now only affect the requested * - SetIdle requests to the HID device driver with a 0 idle period (send changes only) now only affect the requested
@ -32,6 +34,8 @@
* - Changed the parameters and behaviour of the USB_GetDeviceConfigDescriptor() function so that it now performs size checks * - Changed the parameters and behaviour of the USB_GetDeviceConfigDescriptor() function so that it now performs size checks
* and data validations internally, to simplify user code * and data validations internally, to simplify user code
* - Changed HIDParser to only zero out important values in the Parsed HID Report Item Information structure to save cycles * - Changed HIDParser to only zero out important values in the Parsed HID Report Item Information structure to save cycles
* - The HID report parser now always processed FEATURE items - HID_ENABLE_FEATURE_PROCESSING token now has no effect
* - The HID report parser now always ignores constant-data items, HID_INCLUDE_CONSTANT_DATA_ITEMS token now has no effect
* *
* <b>Fixed:</b> * <b>Fixed:</b>
* - Fixed possible lockup in the CDC device class driver, when the host sends data that is a multiple of the * - Fixed possible lockup in the CDC device class driver, when the host sends data that is a multiple of the

@ -23,17 +23,6 @@
* \section Sec_SummaryUSBClassTokens USB Class Driver Related Tokens * \section Sec_SummaryUSBClassTokens USB Class Driver Related Tokens
* This section describes compile tokens which affect USB class-specific drivers in the LUFA library. * This section describes compile tokens which affect USB class-specific drivers in the LUFA library.
* *
* <b>HID_ENABLE_FEATURE_PROCESSING</b> - ( \ref Group_HIDParser ) \n
* Define this token to enable the processing of FEATURE HID report items, if any, into the processed HID structure.
* By default FEATURE items (which are device features settable by the host but not directly visible by the user) are
* skipped when processing a device HID report.
*
* <b>HID_INCLUDE_CONSTANT_DATA_ITEMS</b> - ( \ref Group_HIDParser ) \n
* By default, constant data items (usually used as spacers to align separate report items to a byte or word boundary)
* in the HID report are skipped during report processing. It is highly unusual for an application to make any use of
* constant data items (as they do not carry any useful data and only occupy limited RAM) however if required defining
* this switch will put constant data items into the processed HID report structure.
*
* <b>HID_STATETABLE_STACK_DEPTH</b> - ( \ref Group_HIDParser ) \n * <b>HID_STATETABLE_STACK_DEPTH</b> - ( \ref Group_HIDParser ) \n
* HID reports may contain PUSH and POP elements, to store and retrieve the current HID state table onto a stack. This * HID reports may contain PUSH and POP elements, to store and retrieve the current HID state table onto a stack. This
* allows for reports to save the state table before modifying it slightly for a data item, and then restore the previous * allows for reports to save the state table before modifying it slightly for a data item, and then restore the previous

@ -24,6 +24,10 @@
* preallocate the largest allowable buffer, and pass the size of the buffer to the function. This allows for a single * preallocate the largest allowable buffer, and pass the size of the buffer to the function. This allows for a single
* call to the function to retrieve, size check and validate the Configuration Descriptor rather than having the user * call to the function to retrieve, size check and validate the Configuration Descriptor rather than having the user
* application perform these intermediatary steps. * application perform these intermediatary steps.
* - The HID report parser now requires a mandatory callback in the user code, to filter only the items the application
* is interested in into the processed HID report item structure to save RAM. See \ref CALLBACK_HIDParser_FilterHIDReportItem().
* - The HID report parser now always parses FEATURE and always ignores constant-data items - the HID_ENABLE_FEATURE_PROCESSING
* and HID_INCLUDE_CONSTANT_DATA_ITEMS compile time tokens now have no effect.
* *
* \section Sec_Migration090810 Migrating from 090605 to 090810 * \section Sec_Migration090810 Migrating from 090605 to 090810
* *

Loading…
Cancel
Save