more hid host API rework

This commit is contained in:
hathach 2021-05-13 13:42:52 +07:00
parent 7305fec4db
commit 9324fd8f2e
3 changed files with 56 additions and 114 deletions

View File

@ -160,7 +160,7 @@ void tuh_hid_mounted_cb(uint8_t dev_addr, uint8_t instance)
// printf("A Keyboard device (address %d) is mounted\r\n", dev_addr); // printf("A Keyboard device (address %d) is mounted\r\n", dev_addr);
if (instance == 0) { if (instance == 0) {
tuh_hid_keyboard_get_report(dev_addr, &usb_keyboard_report); tuh_n_hid_n_get_report(dev_addr, instance, &usb_keyboard_report, sizeof(usb_keyboard_report));
} }
} }
@ -243,28 +243,30 @@ void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr)
void hid_task(void) void hid_task(void)
{ {
uint8_t const addr = 1; uint8_t const daddr = 1;
uint8_t const instance = 0;
#if CFG_TUH_HID_KEYBOARD #if CFG_TUH_HID_KEYBOARD
if ( tuh_n_hid_n_keyboard_mounted(addr, 0) ) if ( tuh_n_hid_n_keyboard_mounted(daddr, instance) )
{ {
if ( !tuh_hid_keyboard_is_busy(addr) ) if ( tuh_n_hid_n_ready(daddr, instance) )
{ {
process_kbd_report(&usb_keyboard_report); process_kbd_report(&usb_keyboard_report);
tuh_hid_keyboard_get_report(addr, &usb_keyboard_report); tuh_n_hid_n_get_report(daddr, instance, &usb_keyboard_report, sizeof(usb_keyboard_report));
} }
} }
#endif #endif
#if CFG_TUH_HID_MOUSE #if CFG_TUH_HID_MOUSE
if ( tuh_n_hid_n_mouse_mounted(addr, 0) ) (void) usb_mouse_report;
{ // if ( tuh_n_hid_n_mouse_mounted(daddr, instance) )
if ( !tuh_hid_mouse_is_busy(addr) ) // {
{ // if ( tuh_n_hid_n_ready(daddr, instance) )
process_mouse_report(&usb_mouse_report); // {
tuh_hid_mouse_get_report(addr, &usb_mouse_report); // process_mouse_report(&usb_mouse_report);
} // tuh_n_hid_n_get_report(daddr, instance, &usb_mouse_report, sizeof(usb_mouse_report));
} // }
// }
#endif #endif
} }

View File

@ -52,19 +52,19 @@ typedef struct
uint8_t report_desc_type; uint8_t report_desc_type;
uint16_t report_desc_len; uint16_t report_desc_len;
uint16_t ep_size;
bool valid;
uint16_t report_size; // TODO remove later
uint8_t boot_protocol; // None, Keyboard, Mouse uint8_t boot_protocol; // None, Keyboard, Mouse
bool boot_mode; // Boot or Report protocol bool boot_mode; // Boot or Report protocol
uint8_t report_count; // Number of reports
uint8_t report_count; // Number of reports
struct { struct {
uint8_t in_len; // length of IN report
uint8_t out_len; // length of OUT report
uint8_t usage_page; uint8_t usage_page;
uint8_t usage; uint8_t usage;
// TODO just use the endpint size for now
uint8_t in_len; // length of IN report
uint8_t out_len; // length of OUT report
}reports[CFG_TUH_HID_REPORT_MAX]; }reports[CFG_TUH_HID_REPORT_MAX];
// Parsed Report ID for convenient API // Parsed Report ID for convenient API
@ -99,59 +99,43 @@ uint8_t tuh_n_hid_instance_count(uint8_t daddr)
// HID Interface common functions // HID Interface common functions
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// called from public API need to validate parameters bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance)
tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_interface_t *p_hid)
{ {
//------------- parameters validation -------------// hidh_interface_t* hid_itf = get_instance(daddr, instance);
// TODO change to use is configured function return (hid_itf->ep_in != 0) || (hid_itf->ep_out != 0);
TU_ASSERT(tuh_device_configured(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
TU_VERIFY(report, TUSB_ERROR_INVALID_PARA);
TU_VERIFY(!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY);
TU_ASSERT( usbh_edpt_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size) ) ;
return TUSB_ERROR_NONE;
} }
//bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance) bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance)
//{ {
// TU_VERIFY(tuh_n_hid_n_mounted(dev_addr, instance));
//}
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
return !hcd_edpt_busy(dev_addr, hid_itf->ep_in);
}
bool tuh_n_hid_n_get_report(uint8_t daddr, uint8_t instance, void* report, uint16_t len)
{
TU_VERIFY( tuh_device_ready(daddr) && report && len);
hidh_interface_t* hid_itf = get_instance(daddr, instance);
// TODO change to claim endpoint
TU_VERIFY( !hcd_edpt_busy(daddr, hid_itf->ep_in) );
len = tu_min16(len, hid_itf->ep_size);
return usbh_edpt_xfer(daddr, hid_itf->ep_in, report, len);
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// KEYBOARD // KEYBOARD
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance)
{
hidh_interface_t* hid_itf = get_instance(daddr, instance);
// TODO check rid_keyboard
return tuh_device_configured(daddr) && (hid_itf->ep_in != 0);
}
bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance) bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance)
{ {
hidh_interface_t* hid_itf = get_instance(daddr, instance); hidh_interface_t* hid_itf = get_instance(daddr, instance);
// TODO check rid_keyboard // TODO check rid_keyboard
return tuh_device_configured(daddr) && (hid_itf->ep_in != 0); return tuh_device_ready(daddr) && (hid_itf->ep_in != 0);
}
tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void* buffer)
{
uint8_t inst = 0;
hidh_interface_t* hid_itf = get_instance(dev_addr, inst);
return hidh_interface_get_report(dev_addr, buffer, hid_itf);
}
bool tuh_hid_keyboard_is_busy(uint8_t dev_addr)
{
uint8_t inst = 0;
hidh_interface_t* hid_itf = get_instance(dev_addr, inst);
return tuh_n_hid_n_keyboard_mounted(dev_addr, inst) && hcd_edpt_busy(dev_addr, hid_itf->ep_in);
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -165,26 +149,11 @@ static hidh_interface_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have
bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance) bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance)
{ {
// hidh_interface_t* hid_itf = get_instance(dev_addr, instance); // hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
return tuh_device_configured(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0); return tuh_device_ready(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0);
}
bool tuh_hid_mouse_is_busy(uint8_t dev_addr)
{
return tuh_n_hid_n_mouse_mounted(dev_addr, 0) && hcd_edpt_busy(dev_addr, mouseh_data[dev_addr-1].ep_in);
}
tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void * report)
{
return hidh_interface_get_report(dev_addr, report, &mouseh_data[dev_addr-1]);
} }
#endif #endif
//--------------------------------------------------------------------+
// GENERIC
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// USBH API // USBH API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -264,10 +233,9 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count); hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
hid_dev->inst_count++; hid_dev->inst_count++;
hid_itf->itf_num = desc_itf->bInterfaceNumber; hid_itf->itf_num = desc_itf->bInterfaceNumber;
hid_itf->ep_in = desc_ep->bEndpointAddress; hid_itf->ep_in = desc_ep->bEndpointAddress;
hid_itf->report_size = desc_ep->wMaxPacketSize.size; // TODO get size from report descriptor hid_itf->ep_size = desc_ep->wMaxPacketSize.size;
hid_itf->valid = true;
// Assume bNumDescriptors = 1 // Assume bNumDescriptors = 1
hid_itf->report_desc_type = desc_hid->bReportType; hid_itf->report_desc_type = desc_hid->bReportType;

View File

@ -61,7 +61,12 @@
uint8_t tuh_n_hid_instance_count(uint8_t daddr); uint8_t tuh_n_hid_instance_count(uint8_t daddr);
// Check if HID instance is mounted // Check if HID instance is mounted
//bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance); bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance);
// Check if the interface is ready to use
bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance);
bool tuh_n_hid_n_get_report(uint8_t daddr, uint8_t instance, void* report, uint16_t len);
// Check if HID instance with Keyboard is mounted // Check if HID instance with Keyboard is mounted
bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance); bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance);
@ -69,6 +74,7 @@ bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance);
// Check if HID instance with Mouse is mounted // Check if HID instance with Mouse is mounted
bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance); bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance);
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Application API (Single device) // Application API (Single device)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -119,19 +125,6 @@ bool tuh_hid_keyboard_mounted(uint8_t dev_addr);
* \note This function is primarily used for polling/waiting result after \ref tuh_hid_keyboard_get_report. * \note This function is primarily used for polling/waiting result after \ref tuh_hid_keyboard_get_report.
* Alternatively, asynchronous event API can be used * Alternatively, asynchronous event API can be used
*/ */
bool tuh_hid_keyboard_is_busy(uint8_t dev_addr);
/** \brief Perform a get report from Keyboard interface
* \param[in] dev_addr device address
* \param[in,out] p_report address that is used to store data from device. Must be accessible by usb controller (see \ref CFG_TUSB_MEM_SECTION)
* \returns \ref tusb_error_t type to indicate success or error condition.
* \retval TUSB_ERROR_NONE on success
* \retval TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
* \retval TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
* \retval TUSB_ERROR_INVALID_PARA if input parameters are not correct
* \note This function is non-blocking and returns immediately. The result of usb transfer will be reported by the interface's callback function
*/
tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void * p_report);
//------------- Application Callback -------------// //------------- Application Callback -------------//
/** \brief Callback function that is invoked when an transferring event occurred /** \brief Callback function that is invoked when an transferring event occurred
@ -170,27 +163,6 @@ void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr);
* The interface API includes status checking function, data transferring function and callback functions * The interface API includes status checking function, data transferring function and callback functions
* @{ */ * @{ */
/** \brief Check if the interface is currently busy or not
* \param[in] dev_addr device address
* \retval true if the interface is busy meaning the stack is still transferring/waiting data from/to device
* \retval false if the interface is not busy meaning the stack successfully transferred data from/to device
* \note This function is primarily used for polling/waiting result after \ref tuh_hid_mouse_get_report.
* Alternatively, asynchronous event API can be used
*/
bool tuh_hid_mouse_is_busy(uint8_t dev_addr);
/** \brief Perform a get report from Mouse interface
* \param[in] dev_addr device address
* \param[in,out] p_report address that is used to store data from device. Must be accessible by usb controller (see \ref CFG_TUSB_MEM_SECTION)
* \returns \ref tusb_error_t type to indicate success or error condition.
* \retval TUSB_ERROR_NONE on success
* \retval TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
* \retval TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
* \retval TUSB_ERROR_INVALID_PARA if input parameters are not correct
* \note This function is non-blocking and returns immediately. The result of usb transfer will be reported by the interface's callback function
*/
tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void* p_report);
//------------- Application Callback -------------// //------------- Application Callback -------------//
/** \brief Callback function that is invoked when an transferring event occurred /** \brief Callback function that is invoked when an transferring event occurred
* \param[in] dev_addr Address of device * \param[in] dev_addr Address of device