/*******************************************************************************
* Function Name: USBUART_1_HandleVendorRqst
********************************************************************************
*
* Summary:
*  This routine provide users with a method to implement vendor specific
*  requests.
*
*  To implement vendor specific requests, add your code in this function to
*  decode and disposition the request.  If the request is handled, your code
*  must set the variable "requestHandled" to TRUE, indicating that the
*  request has been handled.
*
* Parameters:
*  None.
*
* Return:
*  requestHandled.
*
* Reentrant:
*  No.
*
*******************************************************************************/
uint8 USBUART_1_HandleVendorRqst(void)
{
    uint8 requestHandled = USBUART_1_FALSE;

    if ((CY_GET_REG8(USBUART_1_bmRequestType) & USBUART_1_RQST_DIR_MASK) == USBUART_1_RQST_DIR_D2H)
    {
        /* Control Read */
        switch (CY_GET_REG8(USBUART_1_bRequest))
        {
        case USBUART_1_GET_EXTENDED_CONFIG_DESCRIPTOR:
#if defined(USBUART_1_ENABLE_MSOS_STRING)
            USBUART_1_currentTD.pData = (volatile uint8 *)&USBUART_1_MSOS_CONFIGURATION_DESCR[0u];
            USBUART_1_currentTD.count = USBUART_1_MSOS_CONFIGURATION_DESCR[0u];
            requestHandled  = USBUART_1_InitControlRead();
#endif /*  USBUART_1_ENABLE_MSOS_STRING */
            break;
        default:
            break;
        }
    }

    /* `#START VENDOR_SPECIFIC_CODE` Place your vendor specific request here */

    /* `#END` */

#ifdef USBUART_1_HANDLE_VENDOR_RQST_CALLBACK
    USBUART_1_HandleVendorRqst_Callback();
#endif /* USBUART_1_HANDLE_VENDOR_RQST_CALLBACK */

    return(requestHandled);
}
/*******************************************************************************
* Function Name: USBUART_1_DispatchCDCClassRqst
********************************************************************************
*
* Summary:
*  This routine dispatches CDC class requests.
*
* Parameters:
*  None.
*
* Return:
*  requestHandled
*
* Global variables:
*   USBUART_1_lineCoding: Contains the current line coding structure.
*     It is set by the Host using SET_LINE_CODING request and returned to the
*     user code by the USBFS_GetDTERate(), USBFS_GetCharFormat(),
*     USBFS_GetParityType(), USBFS_GetDataBits() APIs.
*   USBUART_1_lineControlBitmap: Contains the current control signal
*     bitmap. It is set by the Host using SET_CONTROL_LINE request and returned
*     to the user code by the USBFS_GetLineControl() API.
*   USBUART_1_lineChanged: This variable is used as a flag for the
*     USBFS_IsLineChanged() API, to be aware that Host has been sent request
*     for changing Line Coding or Control Bitmap.
*
* Reentrant:
*  No.
*
*******************************************************************************/
uint8 USBUART_1_DispatchCDCClassRqst(void) 
{
    uint8 requestHandled = USBUART_1_FALSE;

    if ((CY_GET_REG8(USBUART_1_bmRequestType) & USBUART_1_RQST_DIR_MASK) == USBUART_1_RQST_DIR_D2H)
    {   /* Control Read */
        switch (CY_GET_REG8(USBUART_1_bRequest))
        {
            case USBUART_1_CDC_GET_LINE_CODING:
                USBUART_1_currentTD.count = USBUART_1_LINE_CODING_SIZE;
                USBUART_1_currentTD.pData = USBUART_1_lineCoding;
                requestHandled  = USBUART_1_InitControlRead();
                break;

            /* `#START CDC_READ_REQUESTS` Place other request handler here */

            /* `#END` */

            default:    /* requestHandled is initialized as FALSE by default */
                break;
        }
    }
    else if ((CY_GET_REG8(USBUART_1_bmRequestType) & USBUART_1_RQST_DIR_MASK) == \
                                                                            USBUART_1_RQST_DIR_H2D)
    {   /* Control Write */
        switch (CY_GET_REG8(USBUART_1_bRequest))
        {
            case USBUART_1_CDC_SET_LINE_CODING:
                USBUART_1_currentTD.count = USBUART_1_LINE_CODING_SIZE;
                USBUART_1_currentTD.pData = USBUART_1_lineCoding;
                USBUART_1_lineChanged |= USBUART_1_LINE_CODING_CHANGED;
                requestHandled = USBUART_1_InitControlWrite();
                break;

            case USBUART_1_CDC_SET_CONTROL_LINE_STATE:
                USBUART_1_lineControlBitmap = CY_GET_REG8(USBUART_1_wValueLo);
                USBUART_1_lineChanged |= USBUART_1_LINE_CONTROL_CHANGED;
                requestHandled = USBUART_1_InitNoDataControlTransfer();
                break;

            /* `#START CDC_WRITE_REQUESTS` Place other request handler here */

            /* `#END` */

            default:    /* requestHandled is initialized as FALSE by default */
                break;
        }
    }
    else
    {   /* requestHandled is initialized as FALSE by default */
    }

    return(requestHandled);
}
/*******************************************************************************
* Function Name: USBUART_1_DispatchHIDClassRqst
********************************************************************************
*
* Summary:
*  This routine dispatches class requests
*
* Parameters:
*  None.
*
* Return:
*  requestHandled
*
* Reentrant:
*  No.
*
*******************************************************************************/
uint8 USBUART_1_DispatchHIDClassRqst(void) 
{
    uint8 requestHandled = USBUART_1_FALSE;
    uint8 interfaceNumber;

    interfaceNumber = CY_GET_REG8(USBUART_1_wIndexLo);
    if ((CY_GET_REG8(USBUART_1_bmRequestType) & USBUART_1_RQST_DIR_MASK) == USBUART_1_RQST_DIR_D2H)
    {   /* Control Read */
        switch (CY_GET_REG8(USBUART_1_bRequest))
        {
            case USBUART_1_GET_DESCRIPTOR:
                if (CY_GET_REG8(USBUART_1_wValueHi) == USBUART_1_DESCR_HID_CLASS)
                {
                    USBUART_1_FindHidClassDecriptor();
                    if (USBUART_1_currentTD.count != 0u)
                    {
                        requestHandled = USBUART_1_InitControlRead();
                    }
                }
                else if (CY_GET_REG8(USBUART_1_wValueHi) == USBUART_1_DESCR_HID_REPORT)
                {
                    USBUART_1_FindReportDescriptor();
                    if (USBUART_1_currentTD.count != 0u)
                    {
                        requestHandled = USBUART_1_InitControlRead();
                    }
                }
                else
                {   /* requestHandled is initialezed as FALSE by default */
                }
                break;
            case USBUART_1_HID_GET_REPORT:
                USBUART_1_FindReport();
                if (USBUART_1_currentTD.count != 0u)
                {
                    requestHandled = USBUART_1_InitControlRead();
                }
                break;

            case USBUART_1_HID_GET_IDLE:
                /* This function does not support multiple reports per interface*/
                /* Validate interfaceNumber and Report ID (should be 0) */
                if( (interfaceNumber < USBUART_1_MAX_INTERFACES_NUMBER) &&
                    (CY_GET_REG8(USBUART_1_wValueLo) == 0u ) ) /* Do not support Idle per Report ID */
                {
                    USBUART_1_currentTD.count = 1u;
                    USBUART_1_currentTD.pData = &USBUART_1_hidIdleRate[interfaceNumber];
                    requestHandled  = USBUART_1_InitControlRead();
                }
                break;
            case USBUART_1_HID_GET_PROTOCOL:
                /* Validate interfaceNumber */
                if( interfaceNumber < USBUART_1_MAX_INTERFACES_NUMBER)
                {
                    USBUART_1_currentTD.count = 1u;
                    USBUART_1_currentTD.pData = &USBUART_1_hidProtocol[interfaceNumber];
                    requestHandled  = USBUART_1_InitControlRead();
                }
                break;
            default:    /* requestHandled is initialized as FALSE by default */
                break;
        }
    }
    else if ((CY_GET_REG8(USBUART_1_bmRequestType) & USBUART_1_RQST_DIR_MASK) ==
                                                                            USBUART_1_RQST_DIR_H2D)
    {   /* Control Write */
        switch (CY_GET_REG8(USBUART_1_bRequest))
        {
            case USBUART_1_HID_SET_REPORT:
                USBUART_1_FindReport();
                if (USBUART_1_currentTD.count != 0u)
                {
                    requestHandled = USBUART_1_InitControlWrite();
                }
                break;
            case USBUART_1_HID_SET_IDLE:
                /* This function does not support multiple reports per interface */
                /* Validate interfaceNumber and Report ID (should be 0) */
                if( (interfaceNumber < USBUART_1_MAX_INTERFACES_NUMBER) &&
                    (CY_GET_REG8(USBUART_1_wValueLo) == 0u ) ) /* Do not support Idle per Report ID */
                {
                    USBUART_1_hidIdleRate[interfaceNumber] = CY_GET_REG8(USBUART_1_wValueHi);
                    /* With regards to HID spec: "7.2.4 Set_Idle Request"
                    *  Latency. If the current period has gone past the
                    *  newly proscribed time duration, then a report
                    *  will be generated immediately.
                    */
                    if(USBUART_1_hidIdleRate[interfaceNumber] <
                       USBUART_1_hidIdleTimer[interfaceNumber])
                    {
                        /* Set the timer to zero and let the UpdateHIDTimer() API return IDLE_TIMER_EXPIRED status*/
                        USBUART_1_hidIdleTimer[interfaceNumber] = 0u;
                    }
                    /* If the new request is received within 4 milliseconds
                    *  (1 count) of the end of the current period, then the
                    *  new request will have no effect until after the report.
                    */
                    else if(USBUART_1_hidIdleTimer[interfaceNumber] <= 1u)
                    {
                        /* Do nothing.
                        *  Let the UpdateHIDTimer() API continue to work and
                        *  return IDLE_TIMER_EXPIRED status
                        */
                    }
                    else
                    {   /* Reload the timer*/
                        USBUART_1_hidIdleTimer[interfaceNumber] =
                        USBUART_1_hidIdleRate[interfaceNumber];
                    }
                    requestHandled = USBUART_1_InitNoDataControlTransfer();
                }
                break;

            case USBUART_1_HID_SET_PROTOCOL:
                /* Validate interfaceNumber and protocol (must be 0 or 1) */
                if( (interfaceNumber < USBUART_1_MAX_INTERFACES_NUMBER) &&
                    (CY_GET_REG8(USBUART_1_wValueLo) <= 1u) )
                {
                    USBUART_1_hidProtocol[interfaceNumber] = CY_GET_REG8(USBUART_1_wValueLo);
                    requestHandled = USBUART_1_InitNoDataControlTransfer();
                }
                break;
            default:    /* requestHandled is initialized as FALSE by default */
                break;
        }
    }
    else
    {   /* requestHandled is initialized as FALSE by default */
    }

    return(requestHandled);
}