/*
 * 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 <string.h>
#include "uds_service2E.h"


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

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

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

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

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

void UdsService2E_WriteDataByIdentifier(UdsType *obj, const uint8_t msgBuf[], uint16_t msgLen)
{
    uint16_t didValue = 0;
    uint16_t didIdx = 0;
    uint8_t rspBuffer[8] = {0};

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

    didValue = ((uint16_t)msgBuf[1]) << 8;
    didValue |= msgBuf[2];
    
    rspBuffer[0] = UDS_GET_POSITIVE_RSP(0x2E);
    rspBuffer[1] = msgBuf[1];
    rspBuffer[2] = msgBuf[2];
    for(didIdx = 0; didIdx < obj->didNum; didIdx++)
    {
         if(obj->didTable[didIdx].did == didValue)
         {
            if((obj->didTable[didIdx].securityLevel != UDS_SA_NONE) && (obj->securityLevel != obj->didTable[didIdx].securityLevel))
            {
                Uds_NegativeResponse(obj, 0x22, NRC_SECURITY_ACCESS_DENIED);
                return;
            }
            else if((msgLen - 3) != obj->didTable[didIdx].length)
            {
                Uds_NegativeResponse(obj, 0x2E, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
                return;
            }

            if((UDS_DID_TYPE_NVM_RO == obj->didTable[didIdx].type) || (obj->session < obj->didTable[didIdx].sessionLevel))
            {
                Uds_NegativeResponse(obj, 0x2E, NRC_CONDITIONS_NOT_CORRECT);
                return;
            }

            memcpy(obj->didTable[didIdx].pBytes, &msgBuf[3], obj->didTable[didIdx].length);
            /* add user code to save data */

            /* check save data success */
            /* polyspace-begin DEFECT:DEAD_CODE [No action planned:Low] "Still reserve though it's maybe unreachable" */
            if(1)    
            {
                Uds_PositiveResponse(obj, rspBuffer, 3);
                return;
            }
            else
            {
                 Uds_NegativeResponse(obj, 0x2E, NRC_GENERAL_PROGRAMMING_FAILURE);
                 return;
            }
            /* polyspace-end DEFECT:DEAD_CODE [No action planned:Low] "Still reserve though it's maybe unreachable" */
         }
    }

    Uds_NegativeResponse(obj, 0x2E, NRC_REQUEST_OUT_OF_RANGE);
}



