/*
//////////////////////////////////////////////////////////////////////
AI Debug trace file
//////////////////////////////////////////////////////////////////////
File Name : AID_Trac.c
Creation Date : September 29, 1997
Author : David Reizer
//////////////////////////////////////////////////////////////////////
Puropose :
This file implements structures and prototypes for AI engine debug trace
//////////////////////////////////////////////////////////////////////
Modification :
Date :
Author :
//////////////////////////////////////////////////////////////////////
*/
 
#include "AIUseCpa.h"
#include "specif/AIOption.h"
#include "AI_Struc.h"
 
#include "AID_Mmg.h"
#include "AID_Erm.h"
 
#include "Action.h"
#include "DsgMem.h"
#include "specif/ActTable.h"
#include "StrIntel.h"
#include "GetSet.h"
#include "specif/AID_Trac.h"
#include "AID_Main.h"
 
#if defined(ACTIVE_AIDEBUG)
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_vInitTrace
Initialize the trace information for a 'Mind'
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
////////////////////////////////////////////////////////////////////////////////
Output :
None
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind is NULL
E_uwAIDebugWarningInitFailureMem if p_stTrace is not allocated
////////////////////////////////////////////////////////////////////////////////
Creation date : September 30, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
Note :
The buffer is allocated later to avoid the use of memory for 
an object with a Mind but neither reflex nor ai
////////////////////////////////////////////////////////////////////////////////
*/
void AIDebug_fn_vInitTrace( AI_tdstMind *_p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
 
	/* check the validity of the parameter */
	if (!_p_stMind)
	{
       Erm_M_UpdateLastError
			(
             AIDebug, /* Module 's name*/
             C_ucErmDefaultChannel, /*mutli-thread channel*/
             E_uwAIDebugWarningInvalidParameter, /* error code */
             C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
       return;
    }
 
	/* allocates the trace */
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
  AIDebug_M_xAlloc
		(
		 AI_M_p_stGetTrace(_p_stMind),
		 AIDebug_tdstTrace *,
		 sizeof(AIDebug_tdstTrace),
         E_uwAIDebugWarningInitFailureMem,
		{return ;}
		);
  p_stTrace = AI_M_p_stGetTrace(_p_stMind);
 
	/* initialize the different fields */
	AIDebug_M_d_stGetBuffer(p_stTrace) = NULL;
	AIDebug_M_uwGetSizeOfBuffer(p_stTrace) = AIDebug_C_uwDefaultSizeOfBuffer;
  AIDebug_M_uwGetCurrentIndex(p_stTrace) = 0;
  AIDebug_M_bBufferIsFull(p_stTrace) = 0;
	/*AIDebug_M_uwGetCurrentEngineLoop(p_stTrace) = 0;*/
	AIDebug_M_bIsEnable(p_stTrace) = 0;
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_vReinitTrace
Initialize the trace information for a 'Mind' using the same buffer
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
////////////////////////////////////////////////////////////////////////////////
Output :
None
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind is NULL
E_uwAIDebugWarningInitFailureMem if p_stTrace is not allocated
////////////////////////////////////////////////////////////////////////////////
Creation date : October 13, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
void AIDebug_fn_vReinitTrace( AI_tdstMind *_p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
 
	/* check the validity of the parameter */
	if (   (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
       Erm_M_UpdateLastError
			(
             AIDebug, /* Module 's name*/
             C_ucErmDefaultChannel, /*mutli-thread channel*/
             E_uwAIDebugWarningInvalidParameter, /* error code */
             C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
       return;
    }
  
    /* initialize the different fields */
    AIDebug_M_uwGetCurrentIndex(p_stTrace) = 0;
    AIDebug_M_bBufferIsFull(p_stTrace) = 0;
	  /*AIDebug_M_uwGetCurrentEngineLoop(p_stTrace) = 0;*/
}
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_vInitBuffer
Initialize the buffer of the trace information for a 'Mind'
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
////////////////////////////////////////////////////////////////////////////////
Output :
None
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind or p_stTrace is NULL
E_uwAIDebugWarningInitFailureMem if the buffer is not allocated
////////////////////////////////////////////////////////////////////////////////
Creation date : September 30, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
void AIDebug_fn_vInitBuffer( AI_tdstMind *_p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
 
	if (   (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	  )
	{
       Erm_M_UpdateLastError
			(
             AIDebug,
             C_ucErmDefaultChannel,
             E_uwAIDebugWarningInvalidParameter,
             C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
       return;
    }
 
	if ( !AIDebug_M_bIsEnable(p_stTrace) ) return;
    
    /* allocates memory for tracing information :*/
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
    AIDebug_M_xAlloc
		(
		 AIDebug_M_d_stGetBuffer(p_stTrace),
		 AIDebug_tdstSectionEntry *,
		 (AIDebug_M_uwGetSizeOfBuffer(p_stTrace) * sizeof(AIDebug_tdstSectionEntry)),
         E_uwAIDebugWarningInitFailureMem, {return ;}
	  );
}
 
 
void AIDebug_fn_vFreeBuffer( AI_tdstMind *_p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
 
	if (   (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	  )
	{
       Erm_M_UpdateLastError
			(
             AIDebug,
             C_ucErmDefaultChannel,
             E_uwAIDebugWarningInvalidParameter,
             C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
       return;
   }
 
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
	AIDebug_M_xFree
		(
		 AIDebug_M_d_stGetBuffer(p_stTrace),
		 AIDebug_tdstSectionEntry *,
     E_uwAIDebugWarningInitFailureMem, {return ;}
	  );
 
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_vResizeTrace
Resize the buffer of the trace information for a 'Mind'
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
The new size
////////////////////////////////////////////////////////////////////////////////
Output :
None
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind or p_stTrace is NULL
E_uwAIDebugWarningInitFailureMem if the buffer is not allocated
////////////////////////////////////////////////////////////////////////////////
Creation date : October 13, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
void AIDebug_fn_vResizeTrace( AI_tdstMind *_p_stMind, unsigned short _uwSize )
{
	AIDebug_tdstTrace *p_stTrace;
	
	if (   (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	  )
	{
       Erm_M_UpdateLastError
			(
             AIDebug,
             C_ucErmDefaultChannel,
             E_uwAIDebugWarningInvalidParameter,
             C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
       return;
    }
 
	if ( !AIDebug_M_bIsEnable(p_stTrace) ) return;
 
	AIDebug_M_xReAlloc
		(
		 AIDebug_M_d_stGetBuffer(p_stTrace),
		 AIDebug_tdstSectionEntry *,
		 (_uwSize * sizeof(AIDebug_tdstSectionEntry)),
         E_uwAIDebugWarningInitFailureMem, {return ;}
	    );
	
	/* if the size is reduced the index restart from 0 */
	if ( _uwSize < AIDebug_M_uwGetSizeOfBuffer(p_stTrace) )
	{
		AIDebug_M_uwGetCurrentIndex(p_stTrace) = 0;
		AIDebug_M_bBufferIsFull(p_stTrace) = 0;
	}
 
	AIDebug_M_uwGetSizeOfBuffer(p_stTrace) = _uwSize;
}
 
 
unsigned short AIDebug_fn_uwGetSizeOfTrace( AI_tdstMind *_p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
 
	if (   (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	  )
	{
       Erm_M_UpdateLastError
			(
             AIDebug,
             C_ucErmDefaultChannel,
             E_uwAIDebugWarningInvalidParameter,
             C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
       return AIDebug_C_uwInvalidPosition;
    }
 
	return AIDebug_M_uwGetSizeOfBuffer( p_stTrace );
}
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_uwAddSectionEntry
Insert a SectionEntry in the buffer of a mind
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
The type of the SectionEntry to add
The GetSetParam to add
////////////////////////////////////////////////////////////////////////////////
Output :
the position of the next Entry
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if 
	_p_stMind is NULL
	p_stTrace is NULL
	the buffer is NULL
	the SectionType is not valid
////////////////////////////////////////////////////////////////////////////////
Creation date : September 30, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
unsigned short AIDebug_fn_uwAddSectionEntry
	(
	 AI_tdstMind *_p_stMind,
	 AIDebug_tdeSectionType _eSectionType,
	 tdstGetSetParam * _p_stGetSetParam,
	 tdstNodeInterpret * _p_stNode
	)
{
 
	AIDebug_tdstTrace *p_stTrace;
	unsigned short uwIndex;
 
	/* check the validity of parameters */
	if (   ( !_p_stMind )
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
  }
 
	if ( !AIDebug_M_bIsEnable(p_stTrace) ) return AIDebug_C_uwInvalidPosition;
 
	if (   ( !AIDebug_M_d_stGetBuffer(p_stTrace) )
		|| ( !AIDebug_M_bIsValidSectionType(_eSectionType) )
		/* maybe test the validity of _p_stGetSetParam*/
	   )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
    }
 
	uwIndex = AIDebug_M_uwGetCurrentIndex( p_stTrace );
 
	/* if an EngineLoop SectionEntry is overwrite then the first engine*/
	/*   loop is now the next one (clear?)*/
	/*if (   AIDebug_M_bBufferIsFull( p_stTrace ) */
	/*	&& ( AIDebug_M_eGetNSectionType( p_stTrace, uwIndex ) == AIDebug_E_SecTyp_EngineLoop )*/
	/*   )*/
	/*	AIDebug_M_uwGetFirstEngineLoop( p_stTrace )++;*/
 
	/* copy the section entry */
	AIDebug_M_eGetNSectionType( p_stTrace, uwIndex ) = _eSectionType;
	AIDebug_M_stGetNGetSetParam( p_stTrace, uwIndex ) = *(_p_stGetSetParam);
	AIDebug_M_p_stGetNNode( p_stTrace, uwIndex ) = _p_stNode;
 
	/* is it the end of the buffer ? */
	if ( ++uwIndex < AIDebug_M_uwGetSizeOfBuffer(p_stTrace) )
	{
		AIDebug_M_uwGetCurrentIndex( p_stTrace ) = uwIndex;
	}
	else
	{
		AIDebug_M_uwGetCurrentIndex( p_stTrace ) = 0;
		AIDebug_M_bBufferIsFull( p_stTrace ) = 1;
	}
 
	return uwIndex;
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_uwPeekSectionEntry
Get a SectionEntry from the buffer of a mind
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
The position of the entry to keep
A pointer to the type of the SectionEntry
A pointer to the GetSetParam
////////////////////////////////////////////////////////////////////////////////
Output :
the position of the next Entry
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if 
	_p_stMind is NULL
	p_stTrace is NULL
	the buffer is NULL
	the SectionType is not valid
////////////////////////////////////////////////////////////////////////////////
Creation date : October 1, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
/*unsigned short AIDebug_fn_uwPeekSectionEntry
	(
	 AI_tdstMind * _p_stMind,
	 unsigned short _uwIndex,
	 AIDebug_tdeSectionType *_p_eSectionType,
	 tdstGetSetParam * _p_stGetSetParam
	)
{
	AIDebug_tdstTrace *p_stTrace;
 
 
	if (   ( !_p_stMind )
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
		|| ( !AIDebug_M_d_stGetBuffer(p_stTrace) )
	   )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
    }
 
	*(_p_eSectionType)  = AIDebug_M_eGetNSectionType( p_stTrace, _uwIndex );
	*(_p_stGetSetParam) = AIDebug_M_stGetNGetSetParam( p_stTrace, _uwIndex );
 
	return ( ++_uwIndex );
}*/
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_uwAddSectionEntryUsingLong
Add a SectionEntry, using a long parameter to create a GetSetParam of type Integer
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure tdstMind
The Type of the SectionEntry
The Long to insert in the GetSetParam field of the SectionEntry
////////////////////////////////////////////////////////////////////////////////
Output :
the position of the next Entry
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind is NULL
E_uwAIDebugWarningInitFailureMem if p_stGetSetParam is not allocated
E_uwAIDebugWarningDesInitFailureMem if p_stGetSetParam is not correctly desallocated
////////////////////////////////////////////////////////////////////////////////
Creation date : September 30, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
unsigned short AIDebug_fn_uwAddSectionEntryUsingLong
	(
	 AI_tdstMind *_p_stMind,
	 AIDebug_tdeSectionType _eSectionType,
	 long _lValue,
	 tdstNodeInterpret *_p_stNode
	)
{
	tdstGetSetParam *p_stGetSetParam = NULL;
	unsigned char uwReturnedValue;
 
	if ( !_p_stMind )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
    }
 
	/* allocates the temporary GetSetParam used */
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
	AIDebug_M_xAlloc
		(
		 p_stGetSetParam,
		 tdstGetSetParam *,
		 sizeof(tdstGetSetParam),
         E_uwAIDebugWarningInitFailureMem,
		{return AIDebug_C_uwInvalidPosition;}
	    );
	M_Full_GetSetParam_Integer( p_stGetSetParam, _lValue );
 
	uwReturnedValue = AIDebug_fn_uwAddSectionEntry
		(
		 _p_stMind,
		 _eSectionType,
		 p_stGetSetParam,
		 _p_stNode
		);
	/* desallocation */
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
	AIDebug_M_xFree
		(
		 p_stGetSetParam, 
		 tdstGetSetParam*,
		 E_uwAIDebugWarningDesInitFailureMem, {}
		);
 
	return uwReturnedValue;
}
 
 
unsigned short AIDebug_fn_uwEnterEngineLoop( AI_tdstMind *_p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
	unsigned long ulLoop;
	/* BEGIN ROMTEAM Cristi Petrescu 98-06-*/
	unsigned short usResult;
	unsigned char ucVarId;
	tdstGetSetParam stGetSetParam;
	/* END ROMTEAM Cristi Petrescu 98-06-*/
 
	if (   ( !_p_stMind )
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel, /*mutli-thread channel*/
			 E_uwAIDebugWarningInvalidParameter, /* error code */
		     C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
    }
 
	ulLoop = g_stEngineStructure.stEngineTimer.ulTrameNumber;
	/* BEGIN ROMTEAM Cristi Petrescu 98-06-*/
	usResult = AIDebug_fn_uwAddSectionEntryUsingLong( _p_stMind, AIDebug_E_SecTyp_EngineLoop, ulLoop, 0 );
 
	if (usResult == AIDebug_C_uwInvalidPosition)
		return usResult;
 
	if (AI_M_p_stGetAIModel (_p_stMind) && AI_M_p_stGetDsgVar (_p_stMind))
	{
		for (ucVarId = 0; ucVarId < M_GetNbDsgVar (_p_stMind); ucVarId ++)
		{	
			/* save the variables*/
			fn_ucGetDsgVar (ucVarId, 0, _p_stMind, & stGetSetParam);
			usResult = AIDebug_M_SetDsgVar (_p_stMind, & stGetSetParam, NULL);
		}
	}
 
	return usResult;
	/* END ROMTEAM Cristi Petrescu 98-06-*/
}
 
 
unsigned short AIDebug_fn_uwSetCurrentComport( AI_tdstMind *_p_stMind, tdstComport * _p_stComport )
{
	tdstGetSetParam *p_stGetSetParam = NULL;
	unsigned short uwReturnedValue;
 
	if ( !_p_stMind )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel, /*mutli-thread channel*/
			 E_uwAIDebugWarningInvalidParameter, /* error code */
		     C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
    }
 
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
	AIDebug_M_xAlloc
		(
		 p_stGetSetParam,
		 tdstGetSetParam *,
		 sizeof(tdstGetSetParam),
         E_uwAIDebugWarningInitFailureMem,
		{return AIDebug_C_uwInvalidPosition;}
	    );
	M_Full_GetSetParam_pComport( p_stGetSetParam, _p_stComport );
 
	uwReturnedValue = AIDebug_fn_uwAddSectionEntry
		(
		 _p_stMind,
		 AIDebug_E_SecTyp_CurrentComport,
		 p_stGetSetParam,
		 0
		);
	/* desallocation */
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeAI , MMG_C_lSubTypeAIDEBUG , 0 );
	AIDebug_M_xFree
		(
		 p_stGetSetParam, 
		 tdstGetSetParam*,
		 E_uwAIDebugWarningDesInitFailureMem, {}
		);
 
	return uwReturnedValue;
 
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_uwFindNextSection
Find the next SectionEntry with the corresponding type
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AIDebug_tdstTrace
An index where to begin the research
The Type of the SectionEntry to find
////////////////////////////////////////////////////////////////////////////////
Output :
the position of the first corresponding Entry found
////////////////////////////////////////////////////////////////////////////////
Error raised:
////////////////////////////////////////////////////////////////////////////////
Creation date : September 30, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
unsigned short AIDebug_fn_uwFindNextSection
	(
	 AIDebug_tdstTrace * _p_stTrace,
	 unsigned short _uwBeginSearch,
	 AIDebug_tdeSectionType _eSectionType
	)
{
	unsigned short uwBeginIndex, uwIndex;
 
	if (   ( _p_stTrace == 0 )
		/*|| ( !AIDebug_M_d_stGetBuffer(_p_stTrace) )*/
		|| ( !AIDebug_M_bIsValidSectionType(_eSectionType) )
		|| ( !AIDebug_M_bIsValidIndex(_p_stTrace,_uwBeginSearch) )
	   )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return AIDebug_C_uwInvalidPosition;
    }
 
	if( AIDebug_M_d_stGetBuffer(_p_stTrace) == NULL ) return AIDebug_C_uwInvalidPosition;
 
	if (   ( _uwBeginSearch >= AIDebug_M_uwGetCurrentIndex(_p_stTrace) )
		&& ( AIDebug_M_bBufferIsFull(_p_stTrace) )
	   )
	{
		for ( uwIndex = _uwBeginSearch; uwIndex < AIDebug_M_uwGetSizeOfBuffer(_p_stTrace); uwIndex++ )
			if ( AIDebug_M_eGetNSectionType(_p_stTrace,uwIndex) == _eSectionType )
				return uwIndex;
	}
 
	uwBeginIndex = ( _uwBeginSearch >= AIDebug_M_uwGetCurrentIndex(_p_stTrace) ? 0 : _uwBeginSearch );
 
	for ( uwIndex = uwBeginIndex; uwIndex < AIDebug_M_uwGetCurrentIndex(_p_stTrace); uwIndex++ )
		if ( AIDebug_M_eGetNSectionType(_p_stTrace,uwIndex) == _eSectionType )
			return uwIndex;
 
	return AIDebug_C_uwInvalidPosition;
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_vUpdateGetSetParam
Update the GetSeParam field of the Entry number uwIndex-1
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a Mind
An index
The new GetSetParam to saved
////////////////////////////////////////////////////////////////////////////////
Output :
none
////////////////////////////////////////////////////////////////////////////////
Error raised:
////////////////////////////////////////////////////////////////////////////////
Creation date : October 10, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
/*** update the entry number (_uwIndex - 1)***/
void AIDebug_fn_vUpdateGetSetParam
	(
	 AI_tdstMind *_p_stMind,
	 unsigned short _uwIndex,
	 tdstGetSetParam * _p_stGetSetParam
	)
{
	AIDebug_tdstTrace *p_stTrace;
	
	/* check the validity of parameters */
	if (   ( !_p_stMind )
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return;
    }
 
	if ( !AIDebug_M_bIsEnable(p_stTrace) ) return;
 
	if ( !AIDebug_M_d_stGetBuffer(p_stTrace) )
	{
		Erm_M_UpdateLastError
			(
			 AIDebug,
		 	 C_ucErmDefaultChannel,
			 E_uwAIDebugWarningInvalidParameter,
		     C_lErmNoDebugData,
             C_ucErmOpenInfoWindow,
             C_ucAllowStopForDebug,
             NULL
            );
        return;
    }
 
	AIDebug_M_stGetNGetSetParam( p_stTrace, AIDebug_M_uwGetPrevIndex(p_stTrace,_uwIndex) ) = *(_p_stGetSetParam);
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_uwInitExploitation
Return the first SectionEntry from the trace of a Mind
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
The Pointers to fill
////////////////////////////////////////////////////////////////////////////////
Output :
TRUE if the entry filled is valid
FALSE else
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind or p_stTrace is NULL
////////////////////////////////////////////////////////////////////////////////
Creation date : October 03, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
unsigned short AIDebug_fn_bInitExploitation
	(
	 AI_tdstMind* _p_stMind,
	 AIDebug_tdeSectionType *_p_eSectionType,
	 long *_p_lId,
	 tdstGetSetParam *_p_stGetSetParam,
	 unsigned char *_p_ucDepth,
	 tdstNodeInterpret **_pp_stNode
	)
{
	AIDebug_tdstTrace *p_stTrace;
	unsigned short uwBeginSearch, uwIndex;	
	
	/* check the validity of the parameter */
	if (  (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
       Erm_M_UpdateLastError
			(
             AIDebug, /* Module 's name*/
             C_ucErmDefaultChannel, /*mutli-thread channel*/
             E_uwAIDebugWarningInvalidParameter, /* error code */
             C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
       return 0;
    }
 
	/* if the buffer is empty there is no information available */
	if ( AIDebug_M_bBufferIsEmpty(p_stTrace) ) return 0;
 
	/* searching for the first EngineLoop information */
	uwBeginSearch = AIDebug_M_uwGetBeginData( p_stTrace );
	uwIndex = AIDebug_fn_uwFindNextSection ( p_stTrace, uwBeginSearch, AIDebug_E_SecTyp_EngineLoop );
	if ( uwIndex == AIDebug_C_uwInvalidPosition ) return 0;
	
	*_p_eSectionType = AIDebug_M_eGetNSectionType(p_stTrace,uwIndex);
	*_p_stGetSetParam = AIDebug_M_stGetNGetSetParam(p_stTrace,uwIndex);
	*_pp_stNode = AIDebug_M_p_stGetNNode(p_stTrace,uwIndex);
 
	switch( *_p_eSectionType )
	{
	case AIDebug_E_SecTyp_EngineLoop :
		*_p_ucDepth = 0;
		break;
	case AIDebug_E_SecTyp_ReflexOrAI :
		*_p_ucDepth = 1;
		break;
	case AIDebug_E_SecTyp_CurrentComport :
		*_p_ucDepth = 2;
		break;
	/* BEGIN ROMTEAM Cristi Petrescu 98-06-*/
	case AIDebug_E_SecTyp_DsgVar:
		if (! * _pp_stNode)
		{
			* _p_ucDepth = 0;
			break;
		}
		else
			;
	/* END ROMTEAM Cristi Petrescu 98-06-*/
	default : 	
		*_p_ucDepth = M_DepthInterpret(*_pp_stNode) + 1;
	}
 
	if ( ! *_pp_stNode ) *_p_lId = 0;
	else
		{
			switch( M_GetTypeInterpret(*_pp_stNode) )
			{
			case E_ti_Field: 
				*_p_lId = (long)M_eFieldIdInterpret(*_pp_stNode);
				break;
			case E_ti_DsgVar:
				*_p_lId = (long)M_ucVarIdInterpret(*_pp_stNode);
				break;
			case E_ti_Button:
				*_p_lId = (long)M_hInputHandleIdInterpret(*_pp_stNode);
				break;
			case E_ti_KeyWord:
				*_p_lId = (long)M_eKeyWordIdInterpret(*_pp_stNode);
				break;
			case E_ti_Condition:
				*_p_lId = (long)M_eCondIdInterpret(*_pp_stNode);
				break;
			case E_ti_Function:
				*_p_lId = (long)M_eFuncIdInterpret(*_pp_stNode);
				break;
			case E_ti_Operator:
				*_p_lId = (long)M_eOperatorIdInterpret(*_pp_stNode);
				break;
			case E_ti_Procedure:
				*_p_lId = (long)M_eProcedureIdInterpret(*_pp_stNode);
				break;
			default: 
			*_p_lId = (long)0;
		  break;
			}
		}
 
	AIDebug_M_uwGetExplIndex( p_stTrace ) = ( ++uwIndex == AIDebug_M_uwGetSizeOfBuffer(p_stTrace) ? 0 : uwIndex );
	return 1;
}
 
 
/*
////////////////////////////////////////////////////////////////////////////////
Description : AIDebug_fn_bGetNextEntry
Get an Entry from the buffer : 
////////////////////////////////////////////////////////////////////////////////
Input :
A pointer to a structure AI_tdstMind
A pointer to a AIDebug_tdeSectionType
A pointer to a long (Id)
A pointer to a tdstGetSetParam
A pointer to an unsigned char (Depth)
////////////////////////////////////////////////////////////////////////////////
Output :
FALSE if the get entry is the last valid
TRUE  else
////////////////////////////////////////////////////////////////////////////////
Error raised:
E_uwAIDebugWarningInvalidParameter if _p_stMind or p_stTrace is NULL
////////////////////////////////////////////////////////////////////////////////
Creation date : October 03, 97
Author : David Reizer
////////////////////////////////////////////////////////////////////////////////
*/
unsigned short AIDebug_fn_bGetNextEntry
	(
	 AI_tdstMind* _p_stMind,
	 AIDebug_tdeSectionType *_p_eSectionType,
	 long *_p_lId,
	 tdstGetSetParam *_p_stGetSetParam,
	 unsigned char *_p_ucDepth,
	 tdstNodeInterpret **_pp_stNode
	)
{
	AIDebug_tdstTrace *p_stTrace;
	unsigned short uwIndex;
	
	/* check the validity of the parameter */
	if (  (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
       Erm_M_UpdateLastError
			(
             AIDebug, /* Module 's name*/
             C_ucErmDefaultChannel, /*mutli-thread channel*/
             E_uwAIDebugWarningInvalidParameter, /* error code */
             C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
       return 0;
    }
	
	uwIndex = AIDebug_M_uwGetExplIndex( p_stTrace );
	if ( uwIndex == AIDebug_M_uwGetCurrentIndex(p_stTrace) ) return 0;
 
	*_p_eSectionType = AIDebug_M_eGetNSectionType(p_stTrace,uwIndex);
	*_p_stGetSetParam = AIDebug_M_stGetNGetSetParam(p_stTrace,uwIndex);
	*_pp_stNode = AIDebug_M_p_stGetNNode(p_stTrace,uwIndex);
 
	switch( *_p_eSectionType )
	{
	case AIDebug_E_SecTyp_EngineLoop :
		*_p_ucDepth = 0;
		break;
	case AIDebug_E_SecTyp_ReflexOrAI :
		*_p_ucDepth = 1;
		break;
	case AIDebug_E_SecTyp_CurrentComport :
		*_p_ucDepth = 2;
		break;
	/* BEGIN ROMTEAM Cristi Petrescu 98-06-*/
	case AIDebug_E_SecTyp_DsgVar:
		if (! * _pp_stNode)
		{
			* _p_ucDepth = 0;
			break;
		}
		else
			;
	/* END ROMTEAM Cristi Petrescu 98-06-*/
	default : 	
		*_p_ucDepth = M_DepthInterpret(*_pp_stNode) + 1;
	}
 
	if ( ! *_pp_stNode ) *_p_lId = 0;
	else
		{
			switch( M_GetTypeInterpret(*_pp_stNode) )
			{
			case E_ti_Field: 
				*_p_lId = (long)M_eFieldIdInterpret(*_pp_stNode);
				break;
			case E_ti_DsgVar:
			/* BEGIN ROMTEAM Cristi Petrescu 98-08-*/
			case E_ti_DsgVarRef:
			/* END ROMTEAM Cristi Petrescu 98-08-*/
				*_p_lId = (long)M_ucVarIdInterpret(*_pp_stNode);
				break;
			case E_ti_Button:
				*_p_lId = (long)M_hInputHandleIdInterpret(*_pp_stNode);
				break;
			case E_ti_KeyWord:
				*_p_lId = (long)M_eKeyWordIdInterpret(*_pp_stNode);
				break;
			case E_ti_Condition:
				*_p_lId = (long)M_eCondIdInterpret(*_pp_stNode);
				break;
			case E_ti_Function:
				*_p_lId = (long)M_eFuncIdInterpret(*_pp_stNode);
				break;
			case E_ti_Operator:
				*_p_lId = (long)M_eOperatorIdInterpret(*_pp_stNode);
				break;
			case E_ti_Procedure:
				*_p_lId = (long)M_eProcedureIdInterpret(*_pp_stNode);
				break;
			case E_ti_MetaAction:
				*_p_lId = (long)M_eMetaActionIdInterpret(*_pp_stNode);
				break;
			default: 
			*_p_lId = (long)0;
		  break;
			}
		}
	
	AIDebug_M_uwGetExplIndex( p_stTrace ) = ( ++uwIndex == AIDebug_M_uwGetSizeOfBuffer(p_stTrace) ? 0 : uwIndex );
	return 1;
}
 
 
unsigned short AIDebug_fn_bGetEnable( AI_tdstMind * _p_stMind )
{
	AIDebug_tdstTrace *p_stTrace;
	/* check the validity of the parameter */
	if (  (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
       Erm_M_UpdateLastError
			(
             AIDebug, /* Module 's name*/
             C_ucErmDefaultChannel, /*mutli-thread channel*/
             E_uwAIDebugWarningInvalidParameter, /* error code */
             C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
       return 0;
    }
 
	return( AIDebug_M_bIsEnable(p_stTrace) );
}
 
 
void AIDebug_fn_vSetEnable( AI_tdstMind * _p_stMind, unsigned short _uwFlag )
{
	AIDebug_tdstTrace *p_stTrace;
	/* check the validity of the parameter */
	if (  (!_p_stMind)
		|| ( (p_stTrace = AI_M_p_stGetTrace(_p_stMind)) == 0 )
	   )
	{
       Erm_M_UpdateLastError
			(
             AIDebug, /* Module 's name*/
             C_ucErmDefaultChannel, /*mutli-thread channel*/
             E_uwAIDebugWarningInvalidParameter, /* error code */
             C_lErmNoDebugData, /* long for debugging data */
             C_ucErmOpenInfoWindow, /* open window ?*/
             C_ucAllowStopForDebug,/* wether to stop or not for debugging */
             NULL
            );
       return;
    }
 
	AIDebug_M_bIsEnable( p_stTrace ) = _uwFlag;
	if ( _uwFlag ) AIDebug_fn_vInitBuffer( _p_stMind );
	else AIDebug_fn_vFreeBuffer( _p_stMind );
}
 
#endif /*ACTIVE_AIDEBUG*/
