/*
 * 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_service19.h"
#include "uds_dtc.h"



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

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

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

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

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

void UdsService19_ReadDtcInformation(UdsType *obj, const uint8_t msgBuf[], uint16_t msgLen)
{
    uint8_t subFunction = 0;
    uint8_t dtcStatusMask = 0;
    uint16_t dtcCount = 0;
    uint16_t dtcDlc = 0;
    uint8_t rspBuffer[UDS_RSP_LEN_MAX] = {0};

    if(msgLen < obj->seviceTable[obj->curServiceIdx].minLen)
    {
        Uds_NegativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
        return;
    }    
   
    subFunction = UDS_GET_SUB_FUNCTION (msgBuf[1]);
    rspBuffer[0] = UDS_GET_POSITIVE_RSP(0x19);
    rspBuffer[1] = subFunction;
    switch(subFunction)
    {
    case REPORT_DTC_NUMBER_BY_STATUS_MASK:
        if(msgLen != 3) 
        {
            Uds_NegativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    

        dtcStatusMask = msgBuf[2];
        dtcCount = Uds_GetDtcNumberByStatusMask(dtcStatusMask);
        rspBuffer[2] = DTC_AVAILABILITY_STATUS_MASK;
        rspBuffer[3] = DTC_FORMAT_14229;
        rspBuffer[4] = (dtcCount >> 8) & 0xFF;
        rspBuffer[5] = (dtcCount >> 0) & 0xFF;
        Uds_PositiveResponse(obj, rspBuffer, 6);
        break;
    case REPORT_DTC_BY_STATUS_MASK:
        if(msgLen != 3) 
        {
            Uds_NegativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    
        dtcStatusMask = msgBuf[2];
        rspBuffer[2] = DTC_AVAILABILITY_STATUS_MASK;
        dtcDlc = Uds_GetDtcByStatusMask(&rspBuffer[3], UDS_RSP_LEN_MAX - 3, dtcStatusMask);
        Uds_PositiveResponse(obj, rspBuffer, dtcDlc + 3);
        break;
#if 0
    case REPORT_DTC_SNOPSHOT_IDENTIFICATION:
        if(msgLen != 6) 
        {
            UDS_negativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }   
        /* add user code */
        
        break;
    case REPORT_DTC_SNOPSHOT_RECORD_BY_DTC_NUMBER:
        if(msgLen != 6) 
        {
            UDS_negativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    
        /* add user code */
        
        break;
    case REPORT_DTC_STORE_DATA_BY_RECORD_NUMBER:
        if(msgLen != 3) 
        {
            UDS_negativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    
        /* add user code */
        
        break;
    case REPORT_DTC_EXT_DATA_RECORD_BY_DTC_NUMBER:
        if(msgLen != 6) 
        {
            UDS_negativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    
        /* add user code */
        
        break;
    case REPORT_DTC_NUMBER_BY_SEVERITY_MASK_RECORD:
        if(msgLen != 4) 
        {
            UDS_negativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    
        /* add user code */
        
        break;
    case REPORT_DTC_BY_SEVERITY_MASK_RECORD:
        if(msgLen != 4) 
        {
            UDS_negativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    
        /* add user code */
        
        break;
#endif
    case REPORT_SUPPORTED_DTC:
        if(msgLen != 2) 
        {
            Uds_NegativeResponse(obj, 0x19, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
            return;
        }    

        dtcDlc = Uds_GetSupportedDtc(&rspBuffer[3], UDS_RSP_LEN_MAX - 3);
        Uds_PositiveResponse(obj, rspBuffer, dtcDlc + 3);       
        break;
    default:
        Uds_NegativeResponse(obj, 0x19, NRC_SUBFUNCTION_NOT_SUPPORTED);
        break;
    }
}


