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


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

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

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

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

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

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

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

    subFunction = UDS_GET_SUB_FUNCTION (msgBuf[1]);

    rspBuffer[0] = UDS_GET_POSITIVE_RSP(0x10);
    rspBuffer[1] = subFunction;
    rspBuffer[2] = (uint8_t)(obj->p2Server_ms >> 8);
    rspBuffer[3] = (uint8_t)(obj->p2Server_ms & 0x00ff);
    rspBuffer[4] = (uint8_t)(obj->p2xServer_10ms >> 8);
    rspBuffer[5] = (uint8_t)(obj->p2xServer_10ms & 0x00ff);
    switch(subFunction)
    {
    case UDS_SESSION_DEFAULT:
        obj->securityLevel = UDS_SA_NONE;
        obj->session = subFunction;
        Uds_PositiveResponse(obj, rspBuffer, 6);
        break;
    case UDS_SESSION_EXTENDED:
        obj->securityLevel = UDS_SA_NONE;
        obj->session = subFunction;
        Uds_PositiveResponse(obj, rspBuffer, 6);
        break;
    case UDS_SESSION_PROGRAMMING:
        obj->securityLevel = UDS_SA_NONE;
        obj->session = subFunction;
        Uds_PositiveResponse(obj, rspBuffer, 6);
        break;
    case UDS_SESSION_SAFTY:
        obj->securityLevel = UDS_SA_NONE;
        obj->session = subFunction;
        Uds_PositiveResponse(obj, rspBuffer, 6);
        break;
    default:
        Uds_NegativeResponse(obj, 0x10, NRC_SUBFUNCTION_NOT_SUPPORTED);
        break;
    }
}

