/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/
#include "uds_service36.h"
#include "uds_service37.h"


/*******************************************************************************
 * the defines
 ******************************************************************************/

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the constants
 ******************************************************************************/

/*******************************************************************************
 * the functions
 ******************************************************************************/

void UdsService37_RequestTransferExit(UdsType *obj, const uint8_t msgBuf[], uint16_t msgLen)
{
    uint16_t rspLen = 0;
    uint8_t rspBuffer[8] ={0};      /* polyspace DEFECT:PARTIALLY_ACCESSED_ARRAY [No action planned:Low] "Still reserve though member1~7 is not written" */

    if(obj->session == UDS_SESSION_DEFAULT)
    {
        Uds_NegativeResponse(obj, 0x37, NRC_CONDITIONS_NOT_CORRECT);
        return;
    }
    
    if(msgLen < obj->seviceTable[obj->curServiceIdx].minLen)
    {
        Uds_NegativeResponse(obj, 0x37, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
        return;
    }    

    rspBuffer[rspLen++] = UDS_GET_POSITIVE_RSP(0x37);
    switch(Uds_GetDataTransferDirect())
    {
    case UDS_TRANSFER_DIR_DOWNLOAD:
        if(Uds_GetRcvDataTotalLen() != Uds_GetMemSize())
        {
            Uds_NegativeResponse(obj, 0x37, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            break;
        }

        /* user add process code, example data CRC */
        /* polyspace-begin DEFECT:DEAD_CODE [No action planned:Low] "Still reserve though it's maybe unreachable" */
        if(1) /* result check correct */
        {
            Uds_PositiveResponse(obj, rspBuffer, rspLen);   
        }
        else
        {
            Uds_NegativeResponse(obj, 0x37, NRC_GENERAL_PROGRAMMING_FAILURE);
        }
        /* polyspace-end DEFECT:DEAD_CODE [No action planned:Low] "Still reserve though it's maybe unreachable" */
        break;
    case UDS_TRANSFER_DIR_UPLOAD:
        Uds_NegativeResponse(obj, 0x37, NRC_REQUEST_OUT_OF_RANGE);
        break;
    default:
        Uds_NegativeResponse(obj, 0x37, NRC_REQUEST_SEQUENCE_ERROR);
        break;
    }
    
    Uds_SetDataTransferDirect(UDS_TRANSFER_DIR_NONE);   
}



