/*******************************************************************************
********************************************************************************
**
**  Copyright(c) 2022, Alliance for Automotive Innovation
**  Used only under license from the Alliance for Automotive Innovation. All Rights Reserved.
**
**  Project:  J1699-3
**  FileName: VerifyO2TestResults.c
**  Author:   EnGenius
**  Date:     2/25/2022
**  Email:    <support@autosinnovate.org>
**
**  Purpose:  SAE J1699-3 Vehicle OBD II Compliance Test Cases Source Code.
**            This source code is intended to run the tests described in
**            the SAE J1699-3 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:  12/07/2022  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


/*******************************************************************************
**
**  Function:  VerifyO2TestResults
**
**  Purpose:   Verify SID5 O2 test results
**
*******************************************************************************/
STATUS VerifyO2TestResults ( void )
{
	REQ_MSG       stReqMsg;
	BYTE          EcuIdx;
	unsigned long SidRequestFlags = REQ_MSG_ALLOW_NO_RESPONSE;


	// Bypass the no response warning if ISO15765
	if ( gstProtocolList[gProtocolIdx].eProtocol == ISO15765 )
	{
		SidRequestFlags |= REQ_MSG_IGNORE_NO_RESPONSE;
	}

	// Request SID 5 TID 0
	stReqMsg.SID    = 0x05;
	stReqMsg.NumIds = 2;
	stReqMsg.ID[0]  = 0x00;
	stReqMsg.ID[1]  = 0x02; // O2 sensor number #2
	if ( RequestSid ( &stReqMsg, SidRequestFlags ) != PASS )
	{
		// Response is optional
		return FAIL;
	}

	// Verify that no response received for CAN protocols
	if ( gstProtocolList[gProtocolIdx].eProtocol == ISO15765 &&
	     gNumOfECUsResp != 0 )
	{
		for ( EcuIdx = 0;
		      EcuIdx < gNumOfECUs;
		      EcuIdx++ )
		{
			if ( gstResponse[EcuIdx].O2TidSize != 0 )
			{
				Log ( FAILURE, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
				      "ECU %X  Response received for SID $05 (Not allowed for ISO15765 protocol)!\n",
				      GetEcuId ( EcuIdx ) );
			}
		}
		return FAIL;
	}

	return PASS;
}
