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

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

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

/*! \brief The Data-by-ID definition of UDS
 */
typedef struct _Uds_DidBuffer_
{
    uint8_t buffer_F113[4];
} Uds_DidBuffer;

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

Uds_DidBuffer udsDidBuf;

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

static const Uds_ServiceType udsServiceTable[UDS_SERVICE_NUM] = {
    {0x10, 2, true,  UdsService10_SessionControl       },
    {0x11, 2, true,  UdsService11_ResetEcu             },
    {0x27, 2, true,  UdsService27_SecurityAccess       },
    {0x28, 3, true,  UdsService28_CommunicationControl },
    {0x3E, 2, true,  UdsService3E_TesterPresent        },
    {0x85, 2, true,  UdsService85_ControlDtcSetting    },
    {0x22, 3, false, UdsService22_ReadDataByIdentifier },
    {0x2E, 4, false, UdsService2E_WriteDataByIdentifier},
    {0x14, 4, false, UdsService14_ClearDiagInfo        },
    {0x19, 3, true,  UdsService19_ReadDtcInformation   },
    {0x2F, 3, false, UdsService2F_InputOutputCtrlById  },
    {0x31, 4, true,  UdsService31_RoutineControl       },
    {0x34, 5, false, UdsService34_RequestDownload      },
    {0x36, 2, false, UdsService36_TransferData         },
    {0x37, 1, false, UdsService37_RequestTransferExit  },
};

static const bool udsSecurityTable[UDS_SERVICE_NUM][UDS_SA_LEVEL_NUM] = {
    {false, false, false}, /* 0x10 */
    {false, false, false}, /* 0x11 */
    {false, false, false}, /* 0x27 */
    {false, false, false}, /* 0x28 */
    {false, false, false}, /* 0x3E */
    {false, false, false}, /* 0x85 */
    {false, false, false}, /* 0x22 */
    {false, false, false}, /* 0x2E */
    {false, false, false}, /* 0x14 */
    {false, false, false}, /* 0x19 */
    {false, false, false}, /* 0x2F */
    {false, false, false}, /* 0x31 */
    {false, false, false}, /* 0x34 */
    {false, false, false}, /* 0x36 */
    {false, false, false}, /* 0x37 */
};

Uds_DidType udsDidTable[] = {
    {0xF113, UDS_DID_TYPE_RAM_RW, UDS_SESSION_DEFAULT,  UDS_SA_NONE,     udsDidBuf.buffer_F113, BYTES_OF(udsDidBuf.buffer_F113)},
};

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

void Uds_UserInit(UdsType *obj, const Uds_ParamsType *pParams)
{
    Uds_Init(obj, pParams);

    obj->seviceTable   = udsServiceTable;
    obj->securityTable = &udsSecurityTable[0][0];
    obj->serviceNum    = UDS_SERVICE_NUM;
    obj->didTable      = udsDidTable;
    obj->didNum        = sizeof(udsDidTable) / sizeof(Uds_DidType);
}
