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

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

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

/*! \brief The uds communication type definition of UDS
 */
typedef enum _UdsComType_
{
    UDS_CC_TYPE_NONE = 0,
    UDS_CC_TYPE_NORMAL, /* normal message */
    UDS_CC_TYPE_NM,     /* network management */
    UDS_CC_TYPE_NOR_NM, /* normal and network management message */
} UdsComType;

/*! \brief The uds communication control mode definition of UDS
 */
typedef enum _UdsComCtrlMode_
{
    UDS_CC_MODE_RX_TX = 0,
    UDS_CC_MODE_RX_NO,
    UDS_CC_MODE_NO_TX,
    UDS_CC_MODE_NO_NO
} UdsComCtrMode;

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

static uint8_t udsComCtrlType = 0;

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

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

uint8_t Uds_GetCommunicationControlMode(void)
{
    return udsComCtrlType;
}

void UdsService28_CommunicationControl(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 member2~7 is not written" */
    uint8_t ccType       = 0;

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

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

    subFunction = UDS_GET_SUB_FUNCTION(msgBuf[1]);
    ccType      = msgBuf[2];

    rspBuffer[0] = UDS_GET_POSITIVE_RSP(0x28);
    rspBuffer[1] = subFunction;
    switch(subFunction)
    {
    case UDS_CC_MODE_RX_TX:
        if(ccType == UDS_CC_TYPE_NORMAL || ccType == UDS_CC_TYPE_NM || ccType == UDS_CC_TYPE_NOR_NM)
        {
            udsComCtrlType = UDS_CC_MODE_RX_TX;

            Uds_PositiveResponse(obj, rspBuffer, 2);
        }
        else
        {
            Uds_NegativeResponse(obj, 0x28, NRC_REQUEST_OUT_OF_RANGE);
        }
        break;
    case UDS_CC_MODE_RX_NO:
        if(ccType == UDS_CC_TYPE_NORMAL || ccType == UDS_CC_TYPE_NM || ccType == UDS_CC_TYPE_NOR_NM)
        {
            udsComCtrlType = UDS_CC_MODE_RX_NO;

            Uds_PositiveResponse(obj, rspBuffer, 2);
        }
        else
        {
            Uds_NegativeResponse(obj, 0x28, NRC_REQUEST_OUT_OF_RANGE);
        }
        break;
    case UDS_CC_MODE_NO_TX:
        if(ccType == UDS_CC_TYPE_NORMAL || ccType == UDS_CC_TYPE_NM || ccType == UDS_CC_TYPE_NOR_NM)
        {
            udsComCtrlType = UDS_CC_MODE_NO_TX;

            Uds_PositiveResponse(obj, rspBuffer, 2);
        }
        else
        {
            Uds_NegativeResponse(obj, 0x28, NRC_REQUEST_OUT_OF_RANGE);
        }
        break;
    case UDS_CC_MODE_NO_NO:
        if(ccType == UDS_CC_TYPE_NORMAL || ccType == UDS_CC_TYPE_NM || ccType == UDS_CC_TYPE_NOR_NM)
        {
            udsComCtrlType = UDS_CC_MODE_NO_NO;

            Uds_PositiveResponse(obj, rspBuffer, 2);
        }
        else
        {
            Uds_NegativeResponse(obj, 0x28, NRC_REQUEST_OUT_OF_RANGE);
        }
        break;
    default:
        Uds_NegativeResponse(obj, 0x28, NRC_SUBFUNCTION_NOT_SUPPORTED);
        break;
    }
}
