⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 voip_state.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 4 页
字号:
/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |	Project:    	VOIP sample
 |
 |	Filename:   	voip_state.c
 |
 |	Copyright:  	TelcoBridges 2002-2004, All Rights Reserved
 |
 |	Description:	This file contains the entry point of the state machine of this application
 |
 |	Notes:      	Tabs = 4
 |
 *-------------------------------------------------------------------------------------------------------------------------------
 |
 |	Revision:   	$Revision: 1.38 $
 |
 *------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------
 |  Includes
 *------------------------------------------------------------------------------------------------------------------------------*/

#include "voip_includes.h"


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Forward declarations
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Defines
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Types
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Versioning
 *------------------------------------------------------------------------------------------------------------------------------*/

#ifdef WIN32
/*@unused@*/ static char g_szFileVersion [] = "$Revision: 1.38 $";
#endif


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Global variables
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Macros
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Function Prototypes
 *------------------------------------------------------------------------------------------------------------------------------*/

static TBX_VOID VoipStateTestQuit( TBX_VOID );

TBX_RESULT	VoipHandleHostAdded
(
	IN		TBX_MSG_HANDLE					in_hMsg
);

/*--------------------------------------------------------------------------------------------------------------------------------
 |  Implementation
 *------------------------------------------------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipCreateTbxLibFilter	:	Create the main responses/events filter for this application
 |
 |  ~						:	No parameters
 |
 |  Note					:	~
 |
 |  Return					:	TBX_RESULT_OK if the function succeeded
 |								Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipCreateTbxLibFilter( TBX_VOID )
{
	TBX_RESULT			Result;
	PVOIP_CLI_CONTEXT	pCliContext;
	TBX_FILTER_PARAMS	aFilterParams[16];
	TBX_UINT32			un32FilterCount;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result			= TBX_RESULT_OK;
		pCliContext		= &g_pContext->CliContext;
		un32FilterCount	= 0;

		/* Initialize filter parameters to receive messages from the TBX library */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_GROUP;
		aFilterParams[un32FilterCount].MsgGroup				= TBX_ID_CLASS_TBX_API;
		un32FilterCount++;

		/* Initialize filter parameters to receive all responses from our requests */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_TYPE;
		aFilterParams[un32FilterCount].MsgTypeMask			= TBX_MSG_TYPE_RESPONSE;
		un32FilterCount++;

		/* Initialize filter parameters to receive CPU monitor reports */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_TYPE | TBX_FILTER_MSG_ID;
		aFilterParams[un32FilterCount].MsgTypeMask			= TBX_MSG_TYPE_EVENT;
		aFilterParams[un32FilterCount].MsgId				= TB640_MSG_ID_ADAPTER_NOTIF_CPU_REPORT;
		un32FilterCount++;

		/* Initialize filter parameters to receive clock status events */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_ID;
		aFilterParams[un32FilterCount].MsgId				= TB640_MSG_ID_CLK_SYNC_NOTIF_STATE_CHANGE;
		un32FilterCount++;

		/* Create the TBX library filters to receive messages from the CLI state machine thread */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_GROUP;
		aFilterParams[un32FilterCount].MsgGroup				= TBX_ID_CLASS_TBX_APPS_VOIP_PRIVATE;
		un32FilterCount++;

		/* Create the TBX library filters to receive event from stream resources */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_TYPE | TBX_FILTER_MSG_ID;;
		aFilterParams[un32FilterCount].MsgTypeMask			= TBX_MSG_TYPE_EVENT;
		aFilterParams[un32FilterCount].MsgId				= TB640_MSG_ID_STREAM_NOTIF_STATE_CHANGE;
		un32FilterCount++;

		/* Initialize filter parameters to receive auto test lib messages */
		aFilterParams[un32FilterCount].un32StructVersion	= 1;
		aFilterParams[un32FilterCount].FilterMask			= TBX_FILTER_MSG_GROUP;
		aFilterParams[un32FilterCount].MsgGroup				= TBX_TESTLIB_MSG_GROUP;
		un32FilterCount++;

		Result = TBXCreateMsgFilter
		(
			g_pContext->hTbxLib,
			un32FilterCount,
			aFilterParams,
			&g_pContext->hFilter
		);
		if( TBX_RESULT_FAILURE( Result ) )
		{
			TBX_EXIT_ERROR (Result, 0, "Failed to create host library filter.");
		}

		/* Now that our filter is created, we are ready to enable detection of remote adapters */
		g_pContext->LibParam.fIgnoreAdapters = TBX_FALSE;

		Result = TBXModifLibParams (g_pContext->hTbxLib, &g_pContext->LibParam);
		if( TBX_RESULT_FAILURE( Result ) )
		{
			TBX_EXIT_ERROR (Result, 0, "Failed to query TBX library parameters.");
		}

		/* End of the code (skip to cleanup) */
		TBX_EXIT_SUCCESS (TBX_RESULT_OK);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR,
			NULL,
			"VoipCreateTbxLibFilter: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CLEANUP
	{
	}

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipStateMachineMain	:	The main state machine of this application
 |
 |  ~						:	No parameters
 |
 |  Note					:	~
 |
 |  Return					:	TBX_RESULT_OK if the function succeeded
 |								Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipStateMachineMain( TBX_VOID )
{
	TBX_RESULT					Result;
	PVOIP_CLI_CONTEXT			pCliContext;
	TBX_MSG_HANDLE				hMsg;
	PVOIP_ADAPTER_CONTEXT		pAdapterContext;
	PVOIP_ADAPTER_CONTEXT_NODE	pAdapterContextNode;
	TBX_BOOL					fQuitSequenceEntered;
	TBX_UINT32					un32ElapsedMs;
	TBX_UINT32					un32LastPollTicks;
	TBX_UINT32					un32LastStatesUpdateTicks;
	TBX_UINT32					un32LastStatsUpdateTicks;
	PVOIP_CONFIG				pConfig;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result						= TBX_RESULT_OK;
		pCliContext					= &g_pContext->CliContext;
		fQuitSequenceEntered		= TBX_FALSE;
		pAdapterContext				= NULL;
		pAdapterContextNode			= NULL;
		un32LastPollTicks			= g_pContext->un32TimerCurrentTime;
		un32LastStatesUpdateTicks	= g_pContext->un32TimerCurrentTime;
		un32LastStatsUpdateTicks	= g_pContext->un32TimerCurrentTime;
		hMsg						= (TBX_MSG_HANDLE)TBX_HANDLE_INVALID;
		pConfig						= &g_pContext->CurrentConfig;

		/* Lock the thread semaphore */
		TBX_SEM_GET (g_pContext->hThreadSem, TBX_SEM_WAIT_FOREVER);

		/* Loop as long as the application is not being cancelled */
		while( g_fRunStateMachine )
		{
			TBX_UINT32	un32TimeoutToUseMs;

			if( g_pContext->fStressTestRunning == TBX_TRUE )
			{
				un32TimeoutToUseMs = VOIP_STRESS_TEST_LOOP_DELAY_MS;
			}
			else if( g_pContext->fDemoRunning == TBX_TRUE )
			{
				un32TimeoutToUseMs = VOIP_DEMO_LOOP_DELAY_MS;
			}
			else
			{
				un32TimeoutToUseMs = VOIP_HOST_LIB_RECEIVE_TIMEOUT_MS;
			}

			TBX_SEM_GIV (g_pContext->hThreadSem);
			{
				/* Wait for the next API message */
				hMsg = (TBX_MSG_HANDLE)TBX_HANDLE_INVALID;
				Result = TBXReceiveMsg
				(
					g_pContext->hTbxLib,
					g_pContext->hFilter,
					un32TimeoutToUseMs,
					&hMsg
				);
			}
			TBX_SEM_GET (g_pContext->hThreadSem, TBX_SEM_WAIT_FOREVER);

			if( !g_fRunStateMachine )
				break;

			g_pContext->un32TimerCurrentTime = TBX_GET_TICK();

			/* Poll timers... */
			{
				un32ElapsedMs = (g_pContext->un32TimerCurrentTime - un32LastPollTicks) * TBX_MSEC_PER_TICKS;
				if( un32ElapsedMs >= VOIP_TIMER_POLL_PERIOD_MS )
				{
					un32LastPollTicks = g_pContext->un32TimerCurrentTime;

					VoipAdapterPollStateTimeout();
				}
			}


			/* Update global stats */
			{
				TBX_UINT32	un32ElapsedMs;
				TBX_RESULT		LocalResult;

				un32ElapsedMs = (g_pContext->un32TimerCurrentTime - un32LastStatsUpdateTicks) * TBX_MSEC_PER_TICKS;
				if( un32ElapsedMs >= (VOIP_STATS_UPDATE_DELAY_SECOND * 1000) )
				{
					un32LastStatsUpdateTicks = g_pContext->un32TimerCurrentTime;

					LocalResult = TBXStreamGetStats
					(
						g_pContext->hStreamLib,
						&g_pContext->Stats.StreamLibStats
					);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -