/*
********************************************************************************
** SAE J1699-3 Test Source Code
**
**  Copyright (C) 2002 Drew Technologies. http://j1699-3.sourceforge.net/
**
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**
** This source code, when compiled and used with an SAE J2534-compatible pass
** thru device, is intended to run the tests described in the SAE J1699-3
** document in an automated manner.
**
** This computer program is based upon SAE Technical Report J1699,
** which is provided "AS IS"
**
** See j1699.c for details of how to build and run this test.
**
********************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "j2534.h"
#include "j1699.h"


/*
*******************************************************************************
** IsDTCPending - Function to check if SID7 reports a DTC pending
*******************************************************************************
*/
STATUS IsDTCPending(unsigned long Flags)
{
	SID_REQ        SidReq;
	STATUS         RetCode;
	unsigned long  EcuIndex;

	/* Request SID 7 data */
	SidReq.SID = 7;
	SidReq.NumIds = 0;

	RetCode = SidRequest(&SidReq, Flags);

	if (RetCode != PASS)
	{
		/* Indicate request failed */
		return(ERRORS);
	}


	RetCode = FAIL;
	/* Check if SID 7 reports DTCs pending */
	for (EcuIndex = 0; EcuIndex < gOBDNumEcus; EcuIndex++)
	{
		if (gOBDResponse[EcuIndex].Sid7Size == 0)
		{
			/* If no data, ignore */
		}
		/* Check if there is at least one DTC */
		else if ((gOBDResponse[EcuIndex].Sid7[0] != 0) || (gOBDResponse[EcuIndex].Sid7[1] != 0))
		{
			IsDTCIgnored ( EcuIndex, gOBDResponse[EcuIndex].Sid7Size, &gOBDResponse[EcuIndex].Sid7[0] );
			RetCode = PASS;
		}
	}

	return(RetCode);
}


/*
*******************************************************************************
** IsDTCIgnored - Function to check if the DTC(s) are in the ignore list
** FAIL if any DTC is not on the ignore list
*******************************************************************************
*/
STATUS IsDTCIgnored ( unsigned long  EcuIndex, unsigned short Size, unsigned char * DTC_Array )
{
	STATUS         RetCode = PASS;
	unsigned short ArrayIndex;
	unsigned short ListIndex;
	unsigned short DTC;
	char           DTC_Type;

	// check if it's in the ignore list
	for ( ArrayIndex = 0; ArrayIndex < Size; ArrayIndex += 2 )
	{
		DTC = (unsigned short)( ((DTC_Array[ArrayIndex])<<8) + DTC_Array[ArrayIndex+1] );
		// if the DTC is not in the ignore list, fail
		/* Check if SID 3 reports DTCs stored */
		for ( ListIndex = 0; ListIndex < gDTC_Ignore_List_Count; ListIndex++)
		{
			if ( DTC == gDTC_Ignore_List[ListIndex] )
			{
				// this DTC was found, don't search list anymore
				break;
			}
		}

		/* Process based on the type of DTC */
		switch (DTC_Array[ArrayIndex] & 0xC0)
		{
			case 0x00:
			{
				DTC_Type = 'P';
			}
			break;
			case 0x40:
			{
				DTC_Type = 'C';
			}
			break;
			case 0x80:
			{
				DTC_Type = 'B';
			}
			break;
			case 0xC0:
			{
				DTC_Type = 'U';
			}
			break;
		}
		// If reached the end of the ignore list, the DTC isn't ignored
		if ( ListIndex == gDTC_Ignore_List_Count ||
		     (TestPhase != eTestFaultRepaired &&
		      TestPhase != eTestNoFault3DriveCycle) )
		{
			Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
			     "ECU %X  DTC %c%02X%02X detected\n",
			     GetEcuId (EcuIndex),
			     DTC_Type,
			     DTC_Array[ArrayIndex] & 0x3F,
			     DTC_Array[ArrayIndex + 1]);
			RetCode = FAIL;
		}
		else
		{
			Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
			     "ECU %X  DTC %c%02X%02X detected but ignored\n",
			     GetEcuId (EcuIndex),
			     DTC_Type,
			     DTC_Array[ArrayIndex] & 0x3F,
			     DTC_Array[ArrayIndex + 1]);
		}
	}

	return(RetCode);
}
