PORTS Forum

Ports and Interfaces => USB => Topic started by: Babu on March 23, 2015, 01:38:14 pm

Title: Device Notify Host App
Post by: Babu on March 23, 2015, 01:38:14 pm
I'm very new to the driver development.

I'm developing a HID device which has say only one switch. And a host application for the same device.

I can continuously poll for the state of the device using HID_GetInputReport.

Instead how can I make whenever there is a change in the device ( switch on/off ), the driver should inform my host application.

How can my host application register for the notifications?

How to make my driver to notify the host application ( like sending the notification along with the data )?

Can we use RAWINPUT mechanism in windows? Does it work only for mice,keyboard or for any device?

If you can provide some link for the sample apps it will be great.
Title: Re: Device Notify Host App
Post by: Jan Axelson on March 23, 2015, 04:59:58 pm
My HID page has example code that uses a different thread to poll for received data and notify the main application when new data is available:

http://janaxelson.com/hidpage.htm#MyExampleCode
Title: Re: Device Notify Host App
Post by: Babu on March 24, 2015, 05:17:11 pm
Hi Jan
Thanks for your reply.
Can you let me know which sample code should I look for? Is it generic_hid_cs_62
I would like to know how are you broadcasting the data from the device to OS which in turn can notify the host app using WM_INPUT messages.
Title: Re: Device Notify Host App
Post by: Jan Axelson on March 24, 2015, 08:33:17 pm
Yes. The device sends data in interrupt IN transfers.

In my example, cmdGetInputReportInterrupt_Click calls the async routine RequestToGetInputReport, which waits until data is available or a timeout. You don't have to use a click event to request the data. Any routine could call RequestToGetInputReport. Because RequestToGetInputReport is async, the application can do other work while waiting.

Title: Re: Device Notify Host App
Post by: Babu on March 25, 2015, 10:30:39 am
Hi Jan
Thanks for your reply.
cmdGetInputReportInterrupt_Click is the call which host initiates whenever "Get Input Report" button is clicked.

What I'm looking for is say:
1. Whenever the button in the device is toggled, how can I make the device to inform it to the host? I mean to receive the WM_INPUT message in the host, what the device firmware should do? How the device firmware has to inform the OS about there is a change in the button state.I don't want my host to poll for the report continuously and listen for the states. Whenever there is a change in the device, how the device firmware can notify the host about the changes to host.

I have registered for WM_INPUT message in my host app like
RAWINPUTDEVICE rid;
   rid.usUsagePage = 65280;
   rid.usUsage     = 1;
   rid.dwFlags     = 0;
   rid.hwndTarget  = this->GetSafeHwnd();
   BOOL bFlag = ::RegisterRawInputDevices( &rid,1,sizeof(RAWINPUTDEVICE) );
But still I'm not getting the WM_INPUT message in my host app when there is button toggle in the device.
So I would like to know to receive this message in the host, what should be done from the device side?
Am I Clear?
Title: Re: Device Notify Host App
Post by: Jan Axelson on March 25, 2015, 10:48:35 am
I don't have a raw input example. If you haven't seen it, check out:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645546%28v=vs.85%29.aspx

Be sure RegisterRawInputDevices succeeds. (See example above.)

A protocol analyzer will show whether the data is transferring on the bus.

The low-level HID driver retrieves data continuously by polling the device's interrupt IN endpoint according to the bInterval value in the endpoint descriptors. The driver stores the retrieved data, which applications can then access.

HID_GetInputReport bypasses the HID buffer and retrieves Input reports using control transfers.

Be sure your device is also sending data in response to token packets on the interrupt IN endpoint.
Title: Re: Device Notify Host App
Post by: Babu on March 25, 2015, 11:12:55 am
RegisterRawInputDevices call return true. So I dont see any problem.
But I'm not getting WM_INPUT message still.
Do you mean to check whether data transfer is happening when the button is pressed using protocol analyzer?
I was looking something like whenever the mouse is registered, the host gets all the state changes [ button clicks,mouse move etc ] via WM_INPUT message.
The same thing I want to have it with my HID device.
From the host side,everything looks fine [ The only thing to do from host side is to register the device using RegisterRawInputDevices ].
Should I to do something from the device side?
Title: Re: Device Notify Host App
Post by: Jan Axelson on March 25, 2015, 11:31:49 am
You mentioned retrieving data using HID_GetInputReport. This function uses control transfers. To use raw input, I believe the data must be sent using interrupt transfers. Be sure your device is doing that.
Title: Re: Device Notify Host App
Post by: Babu on March 25, 2015, 12:31:25 pm
Ok I'll check.
If I'm not wrong, if the device sends the data through interrupt transfer, my host app should receive it as WM_INPUT messages.
Am I correct on this?
Title: Re: Device Notify Host App
Post by: Jan Axelson on March 25, 2015, 12:54:48 pm
yes

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645590%28v=vs.85%29.aspx
Title: Re: Device Notify Host App
Post by: Babu on April 01, 2015, 02:24:58 pm
When I called HidD_GetInputReport it failed and GetLastError() returned an error saying "device attached to the system is not functioning"
What could be the problem?
I'm able to use the ReadFile but not HidD_GetInputReport?
Should I check something with the end desciptors of the device?
Title: Re: Device Notify Host App
Post by: Jan Axelson on April 01, 2015, 05:34:20 pm
Does your device firmware detect and respond to the HID-class Get_Report request?
Title: Re: Device Notify Host App
Post by: Babu on April 01, 2015, 05:38:54 pm
I'm not very clear on your question..
Title: Re: Device Notify Host App
Post by: Babu on April 01, 2015, 05:40:08 pm
I dont have the firmware code with me
Title: Re: Device Notify Host App
Post by: Jan Axelson on April 01, 2015, 06:11:38 pm
ReadFile retrieves reports received on the interrupt IN endpoint and stored in the buffer maintained by the host's HID driver.

HidD_GetInputReport bypasses the HID buffer and sends a control request with a HID-class Get Report request.

If your device doesn't support Get Report, it won't send a report in response to the request.

Title: Re: Device Notify Host App
Post by: Babu on April 02, 2015, 12:52:36 pm
How can i check whether the device supports Get Report?
Title: Re: Device Notify Host App
Post by: Babu on April 02, 2015, 01:03:17 pm
My firmware and host code looks something like this: [ I have not copied the complete code ]



//*******************************************************************************************************
//               DEVICE CODE
//*******************************************************************************************************

unsigned char ReceivedDataBuffer[64];
unsigned char ToSendDataBuffer[64];


// Main program entry point
void main(void)
{   

    initialisePic();

   // If we are running in interrupt mode attempt to attach the USB device
    #if defined(USB_INTERRUPT)
        USBDeviceAttach();
    #endif
   

   
    //ON the LED
    mStatusLED0_on();
   


    while(1)
    {
        #if defined(USB_POLLING)
         // If we are in polling mode the USB device tasks must be processed here
         // (otherwise the interrupt is performing this task)
           USBDeviceTasks();
        #endif
       
       // Process USB Commands
        processUsbCommands(); 
       
        // Note: Other application specific actions can be placed here     
    }
}

void processUsbCommands(void)
{   


    if(!HIDRxHandleBusy(USBOutHandle))
    {   
           
             switch(ReceivedDataBuffer[0])
             {
           // Toggle the LED   
               case 0x80:
 
            // Toggle the LED0
            mStatusLED0_Toggle();
               break;
           
           // Read the push switch status
               case 0x81:
            ToSendDataBuffer[0] = sw0;

            // Transmit the response to the host
                      if(!HIDTxHandleBusy(USBInHandle))
            {
               USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
            }
               break;
       }
    }
}


//*******************************************************************************************************
//               HOST CODE
//*******************************************************************************************************

void CHID_Window_NewDlg::UpdateSwitchState()
{
   //Read the data from the device.
   //First send the command to the control end point
   BYTE commandBuffer[65];

   //First byte should be 0.It is reserved for windows
   commandBuffer[0] = 0;

   //Second byte should be the command id which reads the LED status
   commandBuffer[1] = 0x81;

   //Write this command to the control end point
   WriteData(commandBuffer);

   //Now read the status in the input buffer
   BYTE* pbInBuffer = NULL;

   //Read the data from the device
   BOOL bResult = ReadData( pbInBuffer );

   //First byte contains the state of the LED
   if( pbInBuffer[1] )
   {
      m_butSwitchState.SetBitmap(m_bmpON);
   }
   else
   {
      m_butSwitchState.SetBitmap(m_bmpOFF);
   }
}
Title: Re: Device Notify Host App
Post by: Jan Axelson on April 02, 2015, 03:29:46 pm
In your original question, you said:

I can continuously poll for the state of the device using HID_GetInputReport.

if that works, your device supports sending reports using control transfers.

You also mentioned using raw input, and that uses reports transferred using interrupt transfers.
Title: Re: Device Notify Host App
Post by: Babu on April 02, 2015, 04:28:36 pm
Sorry about that.
It was a different code I tried.
Now the code what I have shown you is the firmware code I use..
Title: Re: Device Notify Host App
Post by: Jan Axelson on April 02, 2015, 04:46:21 pm
If you can get reports using ReadFile, your device supports sending reports using interrupt transfers.