USB Libraries Help > USB Device Libraries > USB Device Layer Library > Library Interface > b) Client Core Functions > USB_DEVICE_EventHandlerSet Function
MPLAB Harmony USB Stack
USB_DEVICE_EventHandlerSet Function

This is the USB Device Layer Event Handler Callback Set function. A client can receive USB Device Layer event by using this function to register and event handler callback function. The client can additionally specify a specific context which will returned with the event handler callback function.

C
void USB_DEVICE_EventHandlerSet(
    USB_DEVICE_HANDLE usbDeviceHandle, 
    const USB_DEVICE_EVENT_HANDLER callBackFunc, 
    uintptr_t context
);
Preconditions

The device layer must have been initialized by calling USB_DEVICE_Initialize and a valid handle to the instance must have been obtained by calling USB_DEVICE_Open.

Parameters
Parameters 
Description 
usbDeviceHandle 
Pointer to the device layer handle that is returned from USB_DEVICE_Open
 
callBackFunc 
Pointer to the call back function. The device layer calls notifies the client about bus event by calling this function.
 
context 
Client specific context 
Returns

None.

Remarks

None.

Example
// This code example shows how the application can set 
// a Device Layer Event Handler. 

// Application states
typedef enum
{
    //Application's state machine's initial state.
    APP_STATE_INIT=0,
    APP_STATE_SERVICE_TASKS,
    APP_STATE_WAIT_FOR_CONFIGURATION, 
} APP_STATES;

USB_DEVICE_HANDLE usbDeviceHandle;
APP_STATES appState; 

// This is the application device layer event handler function.

USB_DEVICE_EVENT_RESPONSE APP_USBDeviceEventHandler
(
    USB_DEVICE_EVENT event,
    void * pData, 
    uintptr_t context
)
{
    USB_SETUP_PACKET * setupPacket;
    switch(event)
    {
        case USB_DEVICE_EVENT_POWER_DETECTED:
            // This event in generated when VBUS is detected. Attach the device 
            USB_DEVICE_Attach(usbDeviceHandle);
            break;
            
        case USB_DEVICE_EVENT_POWER_REMOVED:
            // This event is generated when VBUS is removed. Detach the device
            USB_DEVICE_Detach (usbDeviceHandle);
            break; 
            
        case USB_DEVICE_EVENT_CONFIGURED:
            // This event indicates that Host has set Configuration in the Device. 
            break;
            
        case USB_DEVICE_EVENT_CONTROL_TRANSFER_SETUP_REQUEST:
            // This event indicates a Control transfer setup stage has been completed. 
            setupPacket = (USB_SETUP_PACKET *)pData;
            
            // Parse the setup packet and respond with a USB_DEVICE_ControlSend(), 
            // USB_DEVICE_ControlReceive or USB_DEVICE_ControlStatus(). 
            
            break; 
            
        case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_SENT:
            // This event indicates that a Control transfer Data has been sent to Host.   
            break; 
            
        case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_RECEIVED:
            // This event indicates that a Control transfer Data has been received from Host.
            break; 
            
        case USB_DEVICE_EVENT_CONTROL_TRANSFER_ABORTED:
            // This event indicates a control transfer was aborted. 
            break; 
            
        case USB_DEVICE_EVENT_SUSPENDED:
            break;
            
        case USB_DEVICE_EVENT_RESUMED:
            break;
            
        case USB_DEVICE_EVENT_ERROR:
            break;
            
        case USB_DEVICE_EVENT_RESET:
            break;
            
        case USB_DEVICE_EVENT_SOF:
            // This event indicates an SOF is detected on the bus. The  USB_DEVICE_SOF_EVENT_ENABLE
            // macro should be defined to get this event. 
            break;
        default:
            break;
    }
}


void APP_Tasks ( void )
{
    // Check the application's current state.
    switch ( appState )
    {
        // Application's initial state. 
        case APP_STATE_INIT:
            // Open the device layer 
            usbDeviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,
                DRV_IO_INTENT_READWRITE );

            if(usbDeviceHandle != USB_DEVICE_HANDLE_INVALID)
            {
                // Register a callback with device layer to get event notification 
                USB_DEVICE_EventHandlerSet(usbDeviceHandle,
                    APP_USBDeviceEventHandler, 0);
                appState = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                // The Device Layer is not ready to be opened. We should try
                // gain later. 
            }
            break; 

        case APP_STATE_SERVICE_TASKS:
            break; 

            // The default state should never be executed. 
        default:
            break; 
    }
}