/*
 * 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 "bootloader.h"


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

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

/*******************************************************************************
 * the globals
 ******************************************************************************/
 
static uint32_t s_memAddr = 0;
static uint32_t s_memSize = 0;
static UdsTransDirType s_dataTransDir = UDS_TRANSFER_DIR_NONE;

static uint8_t s_curRcvSequence = 0;
static uint8_t s_lastRcvSqunce = 0;
static uint32_t s_rcvDataTotalLen = 0;

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

/*******************************************************************************
 * the functions
 ******************************************************************************/
void Uds_InitDownload(void)
{
    s_curRcvSequence  = 0;
    s_lastRcvSqunce   = 0;
    s_rcvDataTotalLen = 0;
    s_memAddr         = 0;
    s_memSize         = 0;
}

void Uds_SetMemAddr(uint32_t addr)
{
    s_memAddr = addr;
}

uint32_t Uds_GetMemAddr(void)
{
    return s_memAddr;
}

void Uds_SetMemSize(uint32_t size)
{
    s_memSize = size;
}

uint32_t Uds_GetMemSize(void)
{
    return s_memSize;
}

void Uds_SetDataTransferDirect(UdsTransDirType dirType)
{
    s_dataTransDir = dirType;
}

UdsTransDirType Uds_GetDataTransferDirect(void)
{
    return s_dataTransDir;
}

uint32_t Uds_GetRcvDataTotalLen(void)
{
    return s_rcvDataTotalLen;
}

void UdsService36_TransferData(UdsType *obj, const uint8_t msgBuf[], uint16_t msgLen)
{
    uint8_t rspBuffer[10] = {0};
    if(obj->session == UDS_SESSION_DEFAULT)
    {
        Uds_NegativeResponse(obj, 0x36, NRC_CONDITIONS_NOT_CORRECT);
        return;
    }

    if(msgLen < obj->seviceTable[obj->curServiceIdx].minLen)
    {
        Uds_NegativeResponse(obj, 0x36, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
        return;
    }

    switch(s_dataTransDir)
    {
    case UDS_TRANSFER_DIR_DOWNLOAD:
        s_curRcvSequence = msgBuf[1];

        if((s_curRcvSequence != s_lastRcvSqunce) && (s_curRcvSequence != ((uint8_t)(s_lastRcvSqunce + 1u))))
        {
            Uds_NegativeResponse(obj, 0x36, NRC_WRONG_BLOCK_SEQUENCE_COUNTER);
            return;
        }

        if((s_rcvDataTotalLen + msgLen - 2u) > s_memSize)
        {
            Uds_NegativeResponse(obj, 0x36, NRC_TRANSFER_DATA_SUSPENDED);
            return;
        }

        if(s_curRcvSequence == ((uint8_t)(s_lastRcvSqunce + 1u)))
        {
            if(Bootloader_EventPgmData(&msgBuf[2], msgLen - 2u, s_curRcvSequence) == true)
            {
                s_rcvDataTotalLen += (msgLen - 2u);
            }

            s_lastRcvSqunce = s_curRcvSequence;

            rspBuffer[0] = UDS_GET_POSITIVE_RSP(0x36);
            rspBuffer[1] = s_curRcvSequence;
            //Uds_PositiveResponse(obj, rspBuffer, 2);
        }
        break;
    case UDS_TRANSFER_DIR_UPLOAD:
        Uds_NegativeResponse(obj, 0x36, NRC_REQUEST_OUT_OF_RANGE);
        break;
    default:
        Uds_NegativeResponse(obj, 0x36, NRC_REQUEST_SEQUENCE_ERROR);
        break;
    }
}



