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

📄 voip_adapter.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 2 页
字号:
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		VoipCliAdapterStatePrint(
			in_pAdapterContext,
			TRACE_LEVEL_ERROR,
			"VoipAdapterStopUsing: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterChangeState		:	Called everytime a state change is required.
 |									This function performs all necessary actions to enter the new state.
 |
 |  io_pAdapterContext			:	Context of the adapter
 |	in_NewState					:	New state to switch to
 |
 |  Note						:	~
 |
 |  Return						:	~
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT VoipAdapterChangeState(
  IN_OUT	PTBX_VOID			io_pAdapterContext,
  IN		VOIP_ADAPTER_STATE	in_NewState)
{
	TBX_RESULT				Result;
	PVOIP_CLI_CONTEXT		pCliContext = &g_pContext->CliContext;
	PVOIP_ADAPTER_CONTEXT	pAdapterContext = io_pAdapterContext;
	TBX_MSG_HANDLE			hMsg;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result = TBX_RESULT_OK;

		pAdapterContext->pFctStateHandler	= VoipAdapterStateUndefined;	/* Should be set below in this function */
		pAdapterContext->State				= in_NewState;

		/* Print a trace that indicate we are entering this state */
		VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_3, "Enter\n" );

		switch( in_NewState )
		{
			case VOIP_ADAPTER_STATE_NOT_USED:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateNotUsed;
				pAdapterContext->State				= in_NewState;

				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_NOT_USED_TIMEOUT_MS );
			} break;

			case VOIP_ADAPTER_STATE_NOT_READY:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateNotReady;
				pAdapterContext->State				= in_NewState;

				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_NOT_READY_TIMEOUT_MS );
			} break;

			case VOIP_ADAPTER_STATE_SYNC:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateSync;
				pAdapterContext->State				= in_NewState;

				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_SYNC_TIMEOUT_MS );
			} break;

			case VOIP_ADAPTER_STATE_CONFIGURING_CLEAR:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateConfiguringClear;
				pAdapterContext->State				= in_NewState;

				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_CONFIGURING_CLEAR_TIMEOUT_MS );
			} break;

			case VOIP_ADAPTER_STATE_CONFIGURING_ALLOC:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateConfiguringAlloc;
				pAdapterContext->State				= in_NewState;

				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_CONFIGURING_ALLOC_TIMEOUT_MS );
			} break;


			case VOIP_ADAPTER_STATE_READY:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateReady;
				pAdapterContext->State				= in_NewState;
				
				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_READY_TIMEOUT_MS );
			} break;
				
			case VOIP_ADAPTER_STATE_CLEAR:
			{
				/* Set appropriate function handler for this state */
				pAdapterContext->pFctStateHandler	= VoipAdapterStateClear;
				pAdapterContext->State				= in_NewState;

				/* Set appropriate timeout for this state */
				VoipAdapterSetStateTimeout( pAdapterContext, VOIP_ADAPTER_STATE_CLEAR_TIMEOUT_MS );
			} break;
		}

		/* Send the "enter" message to this state */
		hMsg = VoipFormatPrivateMsg
		(
			VOIP_MSG_ID_NOTIF_STATE_ENTER,
			sizeof( TBX_MSG_HEADER ),
			pAdapterContext->AdapterInfo.hAdapter,
			0
		);
		if( hMsg != (TBX_MSG_HANDLE)TBX_HANDLE_INVALID )
		{
			pAdapterContext->pFctStateHandler( pAdapterContext, hMsg );
			TBXReleaseMsg (g_pContext->hTbxLib, hMsg);
		}

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

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

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterPollStateTimeout	:	Poll timer class for expired timeouts.
 |
 |  ~							:	No parameters
 |										
 |  Note						:	~
 |										
 |  Return						:	~
 |								
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT VoipAdapterPollStateTimeout()
{
	TBX_RESULT				Result;
	PVOIP_CLI_CONTEXT		pCliContext = &g_pContext->CliContext;
	TBX_UINT32				un32NbExpiredTimers;
	TBX_UINT32				un32Index;
	PVOIP_ADAPTER_CONTEXT	apAdapterContexts[ VOIP_MAX_ADAPTERS ];

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result				= TBX_RESULT_OK;
		un32NbExpiredTimers	= 0;

		/* Get the number of expired timers */
		Result = TBXTimerPoll
		(
			g_pContext->hAdapterTimers,
			g_pContext->un32TimerCurrentTime,
			&un32NbExpiredTimers,
			NULL
		);
		if( TBX_RESULT_FAILURE( Result ) )
		{
			TBX_EXIT_ERROR (Result, 0, "Failed to poll adapter timers.");
		}

		if( un32NbExpiredTimers )
		{
			/* Get the list of expired timers */
			un32NbExpiredTimers = VOIP_MAX_ADAPTERS;
			Result = TBXTimerPoll
			(
				g_pContext->hAdapterTimers,
				g_pContext->un32TimerCurrentTime,
				&un32NbExpiredTimers,
				(PTBX_VOID*)apAdapterContexts
			);
			if( TBX_RESULT_FAILURE( Result ) )
			{
				TBX_EXIT_ERROR (Result, 0, "Failed to poll adapter timers.");
			}

			/* Call state machine for every expired timer */
			for( un32Index = 0; un32Index < un32NbExpiredTimers; un32Index++ )
			{
				/* Reinsert in timer list */
				Result = TBXTimerInsert
				(
					g_pContext->hAdapterTimers,
					g_pContext->un32TimerCurrentTime + (apAdapterContexts[ un32Index ]->un32TimeoutMs / TBX_MSEC_PER_TICKS),
					apAdapterContexts[ un32Index ]
				);
				if( TBX_RESULT_FAILURE( Result ) )
				{
					VoipCliAdapterStatePrint(  apAdapterContexts[ un32Index ], TRACE_LEVEL_ERROR, "VoipAdapterPollStateTimeout: Failed to reinsert in timer list!\n" );
				}

				if( apAdapterContexts[ un32Index ]->pFctStateHandler )
				{
					TBX_MSG_HANDLE		hMsg;

					/* Allocated the "timeout" message */
					hMsg = VoipFormatPrivateMsg
					(
						VOIP_MSG_ID_NOTIF_STATE_TIMEOUT,
						sizeof( TBX_MSG_HEADER ),
						apAdapterContexts[ un32Index ]->AdapterInfo.hAdapter,
						0
					);
					if( hMsg != (TBX_MSG_HANDLE)TBX_HANDLE_INVALID )
					{
						apAdapterContexts[ un32Index ]->pFctStateHandler( apAdapterContexts[ un32Index ], hMsg );
						TBXReleaseMsg (g_pContext->hTbxLib, hMsg);
					}
				}
			}
		}

		/* 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,
			"VoipAdapterPollStateTimeout: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterSetStateTimeout	:	Changes the timeout for current state machine's state.
 |
 |  io_pAdapterContext			:	Context of the adapter
 |	in_un32TimeoutMs			:	Timeout to set
 |
 |  Note						:	~
 |
 |  Return						:	~
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT VoipAdapterSetStateTimeout(
  IN_OUT	PVOIP_ADAPTER_CONTEXT	io_pAdapterContext,
  IN		TBX_UINT32				in_un32TimeoutMs)
{
	TBX_RESULT	Result = TBX_RESULT_OK;

	/* Make sure context is no more in timer list */
	TBXTimerRemove
	(
		g_pContext->hAdapterTimers,
		io_pAdapterContext
	);

	/* Update timeout */
	io_pAdapterContext->un32TimeoutMs = in_un32TimeoutMs;

	if( in_un32TimeoutMs == 0xFFFFFFFF )
	{
		/* Infinite timeout. Don't insert in timer list. */
	}
	else
	{
		/* If timeout is higher than maximum supported, truncate */
		io_pAdapterContext->un32TimeoutMs = in_un32TimeoutMs;
		if( io_pAdapterContext->un32TimeoutMs > VOIP_TIMER_MAX_TIME_MS )
		{
			io_pAdapterContext->un32TimeoutMs = VOIP_TIMER_MAX_TIME_MS;
		}

		/* Insert in timeout list */
		Result = TBXTimerInsert
		(
			g_pContext->hAdapterTimers,
			g_pContext->un32TimerCurrentTime + (io_pAdapterContext->un32TimeoutMs / TBX_MSEC_PER_TICKS),
			io_pAdapterContext
		);
	}

	return Result;
}

⌨️ 快捷键说明

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