/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Device CCID Class                                                   */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_ccid.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_ccid_response                      PORTABLE C      */
/*                                                           6.2.1        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends bulk IN response of the USB CCID device.        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ccid                                  Pointer to ccid instance      */
/*    buffer                                Pointer to data buffer        */
/*    length                                Data length                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    USBX Source Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _ux_device_class_ccid_response(UX_DEVICE_CLASS_CCID *ccid, UCHAR *buffer, ULONG length)
{

UX_SLAVE_ENDPOINT               *endpoint;
UX_SLAVE_TRANSFER               *transfer;
UINT                            status;

    /* Get bulk IN endpoint.  */
    endpoint = ccid -> ux_device_class_ccid_endpoint_in;

    /* Get transfer request.  */
    transfer = &endpoint -> ux_slave_endpoint_transfer_request;

    /* Lock bulk IN.  */
    _ux_device_mutex_on(&ccid -> ux_device_class_ccid_response_mutex);

    /* Prepare data to transfer.  */
    if (length > 0 && buffer != UX_NULL &&
        transfer -> ux_slave_transfer_request_data_pointer != buffer)
    {
        _ux_utility_memory_copy(transfer -> ux_slave_transfer_request_data_pointer,
                                buffer, length); /* Use case of memcpy is verified. */
    }

#if defined(UX_DEVICE_STANDALONE)

    /* Setup transfer struct for running.  */
    UX_SLAVE_TRANSFER_STATE_RESET(transfer);
    transfer -> ux_slave_transfer_request_requested_length = length;
    status = UX_SUCCESS;

    ccid -> ux_device_class_ccid_rsp_state = UX_DEVICE_CLASS_CCID_RSP_START;
#else

    /* Transfer data.  */
    status = _ux_device_stack_transfer_request(transfer, length, length);
#endif

    /* Unlock bulk IN.  */
    _ux_device_mutex_off(&ccid -> ux_device_class_ccid_response_mutex);

    /* Return transfer status.  */
    return(status);
}
