diff --git a/lib_xua/src/hid/hid_report_descriptor.c b/lib_xua/src/hid/hid_report_descriptor.c index 022085ed..a9cb3bab 100644 --- a/lib_xua/src/hid/hid_report_descriptor.c +++ b/lib_xua/src/hid/hid_report_descriptor.c @@ -72,6 +72,17 @@ static unsigned hidGetItemTag( const unsigned char header ); */ static unsigned hidGetItemType( const unsigned char header ); +/** + * @brief Get the Usage Page number for a given byte in the HID Report + * + * Parameters: + * + * @param[in] byte The byte location in the HID Report + * + * @return The USB HID Usage Page code or zero if the \a byte parameter is out-of-range + */ +static unsigned hidGetUsagePage( const unsigned byte ); + /** * @brief Translate an Item from the \c USB_HID_Short_Item format to raw bytes * @@ -134,12 +145,48 @@ size_t hidGetReportDescriptorLength( void ) return retVal; } +#define HID_CONFIGURABLE_ITEM_COUNT ( sizeof hidConfigurableItems / sizeof ( USB_HID_Short_Item_t* )) +unsigned hidGetReportItem( + const unsigned byte, + const unsigned bit, + unsigned char* const page, + unsigned char* const header, + unsigned char data[] +) +{ + unsigned retVal = HID_STATUS_BAD_LOCATION; + for( unsigned itemIdx = 0; itemIdx < HID_CONFIGURABLE_ITEM_COUNT; ++itemIdx ) { + USB_HID_Short_Item_t item = *hidConfigurableItems[ itemIdx ]; + unsigned bBit = hidGetItemBitLocation( item.location ); + unsigned bByte = hidGetItemByteLocation( item.location ); + + if(( bit == bBit ) && ( byte == bByte )) { + *page = hidGetUsagePage( byte ); + *header = item.header; + + for( unsigned dataIdx = 0; dataIdx < HID_REPORT_ITEM_MAX_SIZE; ++data, ++dataIdx ) { + *data = item.data[ dataIdx ]; + } + + retVal = HID_STATUS_GOOD; + break; + } + } + return retVal; +} + size_t hidGetReportLength( void ) { size_t retVal = ( hidReportDescriptorPrepared ) ? HID_REPORT_LENGTH : 0; return retVal; } +static unsigned hidGetUsagePage( const unsigned byte ) +{ + unsigned retVal = ( byte < HID_REPORT_LENGTH ) ? hidUsagePages[ byte ]->data[ 0 ] : 0; + return retVal; +} + void hidPrepareReportDescriptor( void ) { if( !hidReportDescriptorPrepared ) { @@ -158,8 +205,13 @@ void hidResetReportDescriptor( void ) hidReportDescriptorPrepared = 0; } -#define HID_CONFIGURABLE_ITEM_COUNT ( sizeof hidConfigurableItems / sizeof ( USB_HID_Short_Item_t* )) -unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsigned char header, const unsigned char data[] ) +unsigned hidSetReportItem( + const unsigned byte, + const unsigned bit, + const unsigned char page, + const unsigned char header, + const unsigned char data[] +) { unsigned retVal = HID_STATUS_IN_USE; @@ -180,14 +232,22 @@ unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsign unsigned bByte = hidGetItemByteLocation( item.location ); if(( bit == bBit ) && ( byte == bByte )) { - item.header = header; + unsigned pg = hidGetUsagePage( byte ); - for( unsigned dataIdx = 0; dataIdx < bSize; ++dataIdx ) { - item.data[ dataIdx ] = data[ dataIdx ]; + if( page == pg ) { + item.header = header; + + for( unsigned dataIdx = 0; dataIdx < bSize; ++dataIdx ) { + item.data[ dataIdx ] = data[ dataIdx ]; + } + + *hidConfigurableItems[ itemIdx ] = item; + retVal = HID_STATUS_GOOD; + } else { + retVal = HID_STATUS_BAD_PAGE; } - *hidConfigurableItems[ itemIdx ] = item; - retVal = HID_STATUS_GOOD; + break; } } } diff --git a/lib_xua/src/hid/xua_hid_report_descriptor.h b/lib_xua/src/hid/xua_hid_report_descriptor.h index c78202a7..34d391a3 100644 --- a/lib_xua/src/hid/xua_hid_report_descriptor.h +++ b/lib_xua/src/hid/xua_hid_report_descriptor.h @@ -41,7 +41,8 @@ #define HID_STATUS_GOOD ( 0 ) #define HID_STATUS_BAD_HEADER ( 1 ) #define HID_STATUS_BAD_LOCATION ( 2 ) -#define HID_STATUS_IN_USE ( 3 ) +#define HID_STATUS_BAD_PAGE ( 3 ) +#define HID_STATUS_IN_USE ( 4 ) /** * @brief USB HID Report Descriptor. Short Item @@ -94,6 +95,24 @@ unsigned char* hidGetReportDescriptor( void ); */ size_t hidGetReportDescriptorLength( void ); +/** + * @brief Get a HID Report descriptor item + * + * Parameters: + * + * @param[in] byte The byte position of the control within the HID Report + * @param[in] bit The bit position of the control within the \a byte + * @param[out] page The USB HID Usage Page code for the Item (see 5.5) + * @param[out] header The LSB of the Item containing the bSize, bType and bTag fields (see 6.2.2.2) + * @param[out] data A two element array containing data bytes for the Item + * + * @return A status value + * @retval \c HID_STATUS_GOOD Item successfully returned + * @retval \c HID_STATUS_BAD_LOCATION The \a bit or \a byte arguments specify a location outside + * of the HID Report + */ +unsigned hidGetReportItem( const unsigned byte, const unsigned bit, unsigned char* const page, unsigned char* const header, unsigned char data[]); + /** * @brief Get the length of the HID Report * @@ -133,6 +152,7 @@ void hidResetReportDescriptor( void ); * * @param[in] byte The byte position of the control within the HID Report * @param[in] bit The bit position of the control within the \a byte + * @param[in] page The USB HID Usage Page code for the Item (see 5.5) * @param[in] header The LSB of the Item containing the bSize, bType and bTag fields (see 6.2.2.2) * @param[in] data An array containing data bytes or NULL for an Item with no data * @@ -142,8 +162,10 @@ void hidResetReportDescriptor( void ); * a Tag or Type inconsistent with a Usage Item * @retval \c HID_STATUS_BAD_LOCATION The \a bit or \a byte arguments specify a location outside * of the HID Report + * @retval \c HID_STATUS_BAD_PAGE The \a byte argument specifies a location for controls from + * a Usage Page other than the one given by the \a page parameter * @retval \c HID_STATUS_IN_USE The Report descriptor is in use */ -unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsigned char header, const unsigned char data[] ); +unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsigned char page, const unsigned char header, const unsigned char data[]); #endif // _HID_REPORT_DESCRIPTOR_