/*
 * 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_service34.h"
#include "uds_service36.h"
#include "bootloader.h"


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

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

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

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

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

uint32_t Uds_GetMaxfBlockSize(void)
{
    /* user add code to get max num of block length */
    uint32_t blockSize = 0;
    /* user add code to get max num of block length */
    blockSize = Bootloader_GetPgmBlockSize() + 2u;

    return blockSize;
}

uint8_t Uds_GetValidBytesNumOfData(uint32_t data)
{
    uint8_t index = 0;
    uint8_t bytesNum = 0;

    for(index = 1; index <= 4; index++)
    {
        if(data >> ((4 - index) * 8))
        {
            bytesNum = 4 - index + 1;
            break;
        }
    }

    return bytesNum;
}

void UdsService34_RequestDownload(UdsType *obj, const uint8_t msgBuf[], uint16_t msgLen)
{
    uint32_t memAddress      = 0;
    uint8_t  memAddrLen      = 0;
    uint32_t memSize         = 0;
    uint8_t  memSizeLen      = 0;
    uint8_t  maxBlockSizeLen = 0;
    uint32_t maxBlockSize    = 0;
    uint8_t  index           = 0;
    uint16_t rspLen          = 0;
    uint8_t  rspBuffer[8]    = {0};

    if(obj->session == UDS_SESSION_DEFAULT)
    {
        Uds_NegativeResponse(obj, 0x34, NRC_CONDITIONS_NOT_CORRECT);
        return;
    }

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

    memAddrLen = msgBuf[2] & 0x0F;
    memSizeLen = msgBuf[2] >> 4;

    if((memAddrLen == 0) || (memAddrLen > sizeof(memAddress)) || (memSizeLen == 0) || (memSizeLen > sizeof(memSize)))
    {
        Uds_NegativeResponse(obj, 0x34, NRC_REQUEST_OUT_OF_RANGE);
        return;
    }

    for(index = 0; index < memAddrLen; index++)
    {
        memAddress <<= 8;
        memAddress += msgBuf[3 + index];
    }

    for(index = 0; index < memSizeLen; index++)
    {
        memSize <<= 8;
        memSize += msgBuf[3 + memAddrLen + index];
    }

    if(memSize == 0)
    {
        Uds_NegativeResponse(obj, 0x34, NRC_CONDITIONS_NOT_CORRECT);
        return;
    }

    Uds_InitDownload();

    Uds_SetMemAddr(memAddress);
    Uds_SetMemSize(memSize);

    Uds_SetDataTransferDirect(UDS_TRANSFER_DIR_DOWNLOAD);

    maxBlockSize    = Uds_GetMaxfBlockSize();
    maxBlockSizeLen = Uds_GetValidBytesNumOfData(maxBlockSize);

    rspBuffer[rspLen++] = UDS_GET_POSITIVE_RSP(0x34);
    rspBuffer[rspLen++] = maxBlockSizeLen << 4;
    for(index = 1; index <= maxBlockSizeLen; index++)
    {
        rspBuffer[rspLen++] = maxBlockSize >> (8 * (maxBlockSizeLen - index));
    }

    Bootloader_EventPgmReady(memAddress, memSize);

    Uds_PositiveResponse(obj, rspBuffer, rspLen);
}



