Add Get Report Item function and add Usage Page parameter to Set Report Item function

This commit is contained in:
mbanth
2021-07-06 16:47:31 +01:00
parent e3068ad245
commit f25a765e82
2 changed files with 91 additions and 9 deletions

View File

@@ -72,6 +72,17 @@ static unsigned hidGetItemTag( const unsigned char header );
*/ */
static unsigned hidGetItemType( 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 * @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; 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 hidGetReportLength( void )
{ {
size_t retVal = ( hidReportDescriptorPrepared ) ? HID_REPORT_LENGTH : 0; size_t retVal = ( hidReportDescriptorPrepared ) ? HID_REPORT_LENGTH : 0;
return retVal; return retVal;
} }
static unsigned hidGetUsagePage( const unsigned byte )
{
unsigned retVal = ( byte < HID_REPORT_LENGTH ) ? hidUsagePages[ byte ]->data[ 0 ] : 0;
return retVal;
}
void hidPrepareReportDescriptor( void ) void hidPrepareReportDescriptor( void )
{ {
if( !hidReportDescriptorPrepared ) { if( !hidReportDescriptorPrepared ) {
@@ -158,8 +205,13 @@ void hidResetReportDescriptor( void )
hidReportDescriptorPrepared = 0; hidReportDescriptorPrepared = 0;
} }
#define HID_CONFIGURABLE_ITEM_COUNT ( sizeof hidConfigurableItems / sizeof ( USB_HID_Short_Item_t* )) unsigned hidSetReportItem(
unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsigned char header, const unsigned char data[] ) const unsigned byte,
const unsigned bit,
const unsigned char page,
const unsigned char header,
const unsigned char data[]
)
{ {
unsigned retVal = HID_STATUS_IN_USE; unsigned retVal = HID_STATUS_IN_USE;
@@ -180,6 +232,9 @@ unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsign
unsigned bByte = hidGetItemByteLocation( item.location ); unsigned bByte = hidGetItemByteLocation( item.location );
if(( bit == bBit ) && ( byte == bByte )) { if(( bit == bBit ) && ( byte == bByte )) {
unsigned pg = hidGetUsagePage( byte );
if( page == pg ) {
item.header = header; item.header = header;
for( unsigned dataIdx = 0; dataIdx < bSize; ++dataIdx ) { for( unsigned dataIdx = 0; dataIdx < bSize; ++dataIdx ) {
@@ -188,6 +243,11 @@ unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsign
*hidConfigurableItems[ itemIdx ] = item; *hidConfigurableItems[ itemIdx ] = item;
retVal = HID_STATUS_GOOD; retVal = HID_STATUS_GOOD;
} else {
retVal = HID_STATUS_BAD_PAGE;
}
break;
} }
} }
} }

View File

@@ -41,7 +41,8 @@
#define HID_STATUS_GOOD ( 0 ) #define HID_STATUS_GOOD ( 0 )
#define HID_STATUS_BAD_HEADER ( 1 ) #define HID_STATUS_BAD_HEADER ( 1 )
#define HID_STATUS_BAD_LOCATION ( 2 ) #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 * @brief USB HID Report Descriptor. Short Item
@@ -94,6 +95,24 @@ unsigned char* hidGetReportDescriptor( void );
*/ */
size_t hidGetReportDescriptorLength( 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 * @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] 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] 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] 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 * @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 * 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 * @retval \c HID_STATUS_BAD_LOCATION The \a bit or \a byte arguments specify a location outside
* of the HID Report * 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 * @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_ #endif // _HID_REPORT_DESCRIPTOR_