dmitrystu/libusb_stm32

Get data for USB HID Keyboard's LED via Class Request 0x09 (Set Report)?

GrantMTG opened this issue · 3 comments

This is a standard feature of HID Keyboards, they receive the LED information (Caps Lock, etc) via a SETUP then a one-byte EP0 OUT data packet. The setup looks normally like this:

21 09 00 02 00 00 01 00

I can set the usbd_process_eprx() function to receive the data (1 byte) using the following, but I don't know the correct way to direct that byte to my own buffer. Alternatively I would need notification or a callback to get it out of the default(?) one. This code currently resides in cdc_control() in the demo and is similar to #32 (comment).

if((req->bmRequestType & (USB_REQ_TYPE|USB_REQ_RECIPIENT)) == (USB_REQ_CLASS|USB_REQ_INTERFACE))
   {
   switch(req->bRequest)
      {
      case USB_HID_SETREPORT:          // 0x09
           ReportType = req->wValue >> 8;         // 2=Output report, 3=Feature report
           ReportID   = req->wValue &0xFF;
           // Configure the data stage:
           dev->status.control_state = usbd_ctl_rxdata;
           dev->status.data_count = 1;
// Not sure how to get the results from the data stage afterward...
//           dev->status.data_buf = &HidKbLEDs;
//           dev->status.data_ptr = &HidKbLEDs; 
           return usbd_ack;                       // We are expecting an OUT data stage with the info...

How can I get the data and/or be notified that is has been received?

Curiously the data stage is already handled before

static usbd_respond cdc_control(usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback)

is even called! I did not expect that. So, my code is much simpler even:

if((req->bmRequestType & (USB_REQ_TYPE|USB_REQ_RECIPIENT)) == (USB_REQ_CLASS|USB_REQ_INTERFACE))
   {
   switch(req->bRequest)
      {
      case USB_HID_SETREPORT:          // 0x09
           ReportType = req->wValue >> 8;         // 2=Output report, 3=Feature report
           ReportID   = req->wValue &0xFF;
           // ToDo: check the above two values first...
           HidKbLEDs = ((uint8_t *)dev->status.data_ptr)[0];
           return usbd_ack; 

Let me know if this is indeed correct.

Hi
Yes, that's correct.
Main logic