/*******************************************************************************
********************************************************************************
**
**  Copyright(c) 2022, Alliance for Automotive Innovation
**  Used only under license from the Alliance for Automotive Innovation. All Rights Reserved.
**
**  Project:  J1699-5
**  FileName: VerifyRIDSupportAndData.c
**  Author:   EnGenius
**  Date:     2/25/2022
**  Email:    <support@autosinnovate.org>
**
**  Purpose:  SAE J1699-5 Vehicle OBD II Compliance Test Cases Source Code.
**            This source code is intended to run the tests described in
**            the SAE J1699-5 document in an automated manner, when compiled
**            and used with an SAE J2534-compatible pass-thru device.
**
**            File j1699.c contains information on building and running this test.
**
**  Description:
**
**  Modifications:  03/13/2023  Initial Version
**
********************************************************************************
*******************************************************************************/

#include <stdio.h>    // C Library input and output declarations
#include <stdlib.h>   // C Library general function declarations
#include <time.h>     // C Library time and date declarations
#include <windows.h>  // Windows API declarations
#include "j2534.h"    // j1699 project j2534 declarations
#include "j1699.h"    // j1699 project general declarations


STATUS RequestSID8SupportData ( void );
void   FormatSID8SupReq ( unsigned int IdIdx, REQ_MSG *stReqMsg );
STATUS VerifySID8PIDSupportData ( void );


/*******************************************************************************
**
**  Function:  VerifyRIDSupportAndData -
**
**  Purpose:   Verify Routine Identifiers support and data
**
*******************************************************************************/
STATUS VerifyRIDSupportAndData ( void )
{
	REQ_MSG  stReqMsg;
	BYTE     EcuIdx;
	BOOL bTestFailed = FALSE;


	stReqMsg.SID = 0x31;       // SID $31 Routine Control
	stReqMsg.NumIds = 3;
	stReqMsg.u.ID[0] = 0x01;  // Routine Control Type - Start Routine
	stReqMsg.u.ID[1] = 0xE0;  // Routine ID - High Byte - $E0
	stReqMsg.u.ID[2] = 0x00;  // Routine ID - Low  Byte - $00
	if ( RequestSID ( &stReqMsg, REQ_MSG_NORMAL | REQ_MSG_ALLOW_NO_RESPONSE ) == FAIL )
	{
		Log ( WARNING, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
		      "No response to SID $31 Routine Control $01 (StartRoutine) request\n" );
		return FAIL;
	}

	for ( EcuIdx = 0;
	      EcuIdx < gNumOfECUs;
	      EcuIdx++ )
	{
		if ( gstResponse[EcuIdx].RIDSupportSize != 0 )
		{
			// log ECU ID
			Log ( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
			      "ECU %X SID $31 $01 (StartRoutine) $E000 response data:\n"
			      "                   RP_INF = $%02X\n"
			      "                   DATA_A = $%02X\n"
			      "                   DATA_B = $%02X\n"
			      "                   DATA_C = $%02X\n"
			      "                   DATA_D = $%02X\n",
			      GetEcuId ( EcuIdx ),
			      gstResponse[EcuIdx].RIDSupport[2],
			      gstResponse[EcuIdx].RIDSupport[3],
			      gstResponse[EcuIdx].RIDSupport[4],
			      gstResponse[EcuIdx].RIDSupport[5],
			      gstResponse[EcuIdx].RIDSupport[6] );

			// if EVAP supported but $E001 not supported, fail
			if ( gstUserInput.eComplianceType == US_OBDII &&
			     gstUserInput.eVehicleType == LD &&
			     (gstResponse[EcuIdx].PIDF501.Data[3] & 0x01) == 0x01 &&
			     (gstResponse[EcuIdx].RIDSupport[3] & 0x80) == 0x00 )
			{
				Log ( FAILURE, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
				      "ECU %X  $E001 not supported (Must be supported for OBDII LD vehicles which support EVAP)\n",
				      GetEcuId ( EcuIdx ) );
				bTestFailed = TRUE;
			}
		}
	}

	if ( bTestFailed == TRUE )
	{
		return FAIL;
	}
	else
	{
		return PASS;
	}
}
