/***************************************************************************//**
* \file CDC_cls.c
* \version 3.20
*
* \brief
*  This file contains the USB Class request handler.
*
********************************************************************************
* \copyright
* Copyright 2008-2016, Cypress Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/

#include "CDC_pvt.h"
#include "cyapicallbacks.h"

#if(CDC_EXTERN_CLS == CDC_FALSE)

/***************************************
* User Implemented Class Driver Declarations.
***************************************/
/* `#START USER_DEFINED_CLASS_DECLARATIONS` Place your declaration here */

/* `#END` */


/*******************************************************************************
* Function Name: CDC_DispatchClassRqst
****************************************************************************//**
*  This routine dispatches class specific requests depend on interface class.
*
* \return
*  requestHandled.
*
* \reentrant
*  No.
*
*******************************************************************************/
uint8 CDC_DispatchClassRqst(void) 
{
    uint8 interfaceNumber;
    uint8 requestHandled = CDC_FALSE;

    /* Get interface to which request is intended. */
    switch (CDC_bmRequestTypeReg & CDC_RQST_RCPT_MASK)
    {
        case CDC_RQST_RCPT_IFC:
            /* Class-specific request directed to interface: wIndexLoReg 
            * contains interface number.
            */
            interfaceNumber = (uint8) CDC_wIndexLoReg;
            break;
        
        case CDC_RQST_RCPT_EP:
            /* Class-specific request directed to endpoint: wIndexLoReg contains 
            * endpoint number. Find interface related to endpoint. 
            */
            interfaceNumber = CDC_EP[CDC_wIndexLoReg & CDC_DIR_UNUSED].interface;
            break;
            
        default:
            /* Default interface is zero. */
            interfaceNumber = 0u;
            break;
    }
    
    /* Check that interface is within acceptable range */
    if (interfaceNumber <= CDC_MAX_INTERFACES_NUMBER)
    {
    #if (defined(CDC_ENABLE_HID_CLASS)   || \
         defined(CDC_ENABLE_AUDIO_CLASS) || \
         defined(CDC_ENABLE_CDC_CLASS)   || \
         CDC_ENABLE_MSC_CLASS)

        /* Handle class request depends on interface type. */
        switch (CDC_interfaceClass[interfaceNumber])
        {
        #if defined(CDC_ENABLE_HID_CLASS)
            case CDC_CLASS_HID:
                requestHandled = CDC_DispatchHIDClassRqst();
                break;
        #endif /* (CDC_ENABLE_HID_CLASS) */
                
        #if defined(CDC_ENABLE_AUDIO_CLASS)
            case CDC_CLASS_AUDIO:
                requestHandled = CDC_DispatchAUDIOClassRqst();
                break;
        #endif /* (CDC_CLASS_AUDIO) */
                
        #if defined(CDC_ENABLE_CDC_CLASS)
            case CDC_CLASS_CDC:
                requestHandled = CDC_DispatchCDCClassRqst();
                break;
        #endif /* (CDC_ENABLE_CDC_CLASS) */
            
        #if (CDC_ENABLE_MSC_CLASS)
            case CDC_CLASS_MSD:
            #if (CDC_HANDLE_MSC_REQUESTS)
                /* MSC requests are handled by the component. */
                requestHandled = CDC_DispatchMSCClassRqst();
            #elif defined(CDC_DISPATCH_MSC_CLASS_RQST_CALLBACK)
                /* MSC requests are handled by user defined callbcak. */
                requestHandled = CDC_DispatchMSCClassRqst_Callback();
            #else
                /* MSC requests are not handled. */
                requestHandled = CDC_FALSE;
            #endif /* (CDC_HANDLE_MSC_REQUESTS) */
                break;
        #endif /* (CDC_ENABLE_MSC_CLASS) */
            
            default:
                /* Request is not handled: unknown class request type. */
                requestHandled = CDC_FALSE;
                break;
        }
    #endif /* Class support is enabled */
    }
    
    /* `#START USER_DEFINED_CLASS_CODE` Place your Class request here */

    /* `#END` */

#ifdef CDC_DISPATCH_CLASS_RQST_CALLBACK
    if (CDC_FALSE == requestHandled)
    {
        requestHandled = CDC_DispatchClassRqst_Callback(interfaceNumber);
    }
#endif /* (CDC_DISPATCH_CLASS_RQST_CALLBACK) */

    return (requestHandled);
}


/*******************************************************************************
* Additional user functions supporting Class Specific Requests
********************************************************************************/

/* `#START CLASS_SPECIFIC_FUNCTIONS` Place any additional functions here */

/* `#END` */

#endif /* CDC_EXTERN_CLS */


/* [] END OF FILE */
