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

📄 voip_adapter.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |	Project:    	VOIP sample
 |
 |	Filename:   	voip_adapter.c
 |
 |	Copyright:  	TelcoBridges 2002-2004, All Rights Reserved
 |
 |	Description:	This file contains functions to manage VoIP adapters
 |
 |	Notes:      	Tabs = 4
 |
 *-------------------------------------------------------------------------------------------------------------------------------
 |
 |	Revision:   	$Revision: 1.9 $
 |
 *------------------------------------------------------------------------------------------------------------------------------*/


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

#include "voip_includes.h"



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


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


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


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

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


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


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


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


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

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipHandleAdapterAdded	:	Handle a new adapter discovered
 |
 |  in_hMsg					:	Message received
 |
 |  Note					:	~
 |
 |  Return					:	TBX_RESULT_OK if the function succeeded
 |								Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipHandleAdapterAdded(
	IN		TBX_MSG_HANDLE	in_hMsg)
{
	TBX_RESULT							Result;
	PVOIP_CLI_CONTEXT					pCliContext = &g_pContext->CliContext;
	PTBX_EVT_API_NOTIF_ADAPTER_ADDED	pAdapterAddedMsg;
	PVOIP_ADAPTER_CONTEXT				pAdapterContext;
	PVOIP_ADAPTER_CONTEXT_NODE			pAdapterContextNode;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result				= TBX_RESULT_OK;
		pAdapterContextNode	= NULL;
		pAdapterContext		= NULL;
		pAdapterAddedMsg	= TBX_MSG_PAYLOAD_POINTER( in_hMsg );

		/* Adapter ready. Check if it is one of the adapters we are using...  */
		pAdapterContextNode = TBXPoolOfBuffersFind
		(
			g_pContext->hPoolOfAdapters,
			(TBX_HASH_KEY)pAdapterAddedMsg->Info.szAdapterName
		);
		if( pAdapterContextNode )
		{
			pAdapterContext = pAdapterContextNode->pAdapterContext;
			
			/* Update adapter info */
			pAdapterContext->AdapterInfo				= pAdapterAddedMsg->Info;
			pAdapterContext->fPresent					= TBX_TRUE;
			pAdapterContext->fReadyToConfigure			= TBX_FALSE;
			pAdapterContext->fConfigured				= TBX_FALSE;
			pAdapterContext->fSyncCompleted				= TBX_FALSE;
			pAdapterContext->fAttached					= TBX_FALSE;

			Result = TBXHashFind
			(
				g_pContext->hAdapterHandleHash,
				(TBX_HASH_KEY)pAdapterAddedMsg->Info.hAdapter,
				NULL
			);
			if( TBX_RESULT_FAILURE( Result ) )
			{
				Result = TBXHashInsert
				(
					g_pContext->hAdapterHandleHash,
					(TBX_HASH_KEY)pAdapterAddedMsg->Info.hAdapter,
					pAdapterContext
				);
				if( TBX_RESULT_FAILURE( Result ) )
				{
					TbxCliToolsLogPrint
					(
						pCliContext->hCliTools,
						TRACE_LEVEL_ERROR, NULL,
						"VoipStateMachineMain: Failed to insert adapter %s in hash table of adapter handles (0x%08X)\n",
						pAdapterAddedMsg->Info.szSerialNumber,
						(int)Result
					);
				}
				else
				{
					/* Count this connected peer */
					g_pContext->un32NumberConnectedPeers++;
				}
			}

			/* Update our adapter info structure for this adapter */
			memcpy
			(
				&pAdapterContext->AdapterInfo,
				&pAdapterAddedMsg->Info,
				sizeof(pAdapterContext->AdapterInfo)
			);

			/* Forward this event to the state machine for this adapter */
			if( pAdapterContext->pFctStateHandler )
			{
				TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_3, NULL, "Adapter connected\n" );
				pAdapterContext->pFctStateHandler( pAdapterContext, in_hMsg );
			}
		}

		/* Configure network redundancy for this adapter */
		VoipConfigureNetworkRedundancy
		(
			pAdapterAddedMsg->Info.hAdapter,
			TBX_TRUE
		);

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

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
	}

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipHandleAdapterRemoved	:	Handle a disconnection with an adapter
 |
 |  in_pMsg						:	Adapter removed message received
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the function succeeded
 |									Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipHandleAdapterRemoved(
	IN		PTBX_EVT_API_NOTIF_ADAPTER_REMOVED	in_pMsg)
{
	TBX_RESULT				Result;
	PVOIP_CLI_CONTEXT		pCliContext = &g_pContext->CliContext;
	PVOIP_ADAPTER_CONTEXT	pAdapterContext;

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

		/* Search/remove this adapter in our list of adapters monitored */
		TBXHashRemove
		(
			g_pContext->hAdapterHandleHash,
			(TBX_HASH_KEY)in_pMsg->hAdapter,
			(PTBX_VOID*)&pAdapterContext
		);
		if( !pAdapterContext )
		{
			/* Ignore this adapter. Not in our list of used adapters */
			TBX_EXIT_SUCCESS (TBX_RESULT_OK);
		}

		/* Count this disconnected peer */
		if( g_pContext->un32NumberConnectedPeers )
		{
			g_pContext->un32NumberConnectedPeers--;
		}

		TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_3, NULL, "Disconnected\n" );

		pAdapterContext->fPresent			= TBX_FALSE;
		pAdapterContext->fReadyToConfigure	= TBX_FALSE;
		pAdapterContext->fConfigured		= TBX_FALSE;
		pAdapterContext->fSyncCompleted		= TBX_FALSE;
		pAdapterContext->fAttached			= TBX_FALSE;

		/* Clear this adapter's current config. */
		VoipAdapterClearConfig( &pAdapterContext->CurrentConfig );

		/* Clear this adapter's current status */
		memset( &pAdapterContext->Status, 0, sizeof(pAdapterContext->Status) );

		if( pAdapterContext->State == VOIP_ADAPTER_STATE_NOT_USED )
		{
			/* Stay in "not used" state */
		}
		else
		{
			if( pAdapterContext->fStopUsingPending )
			{
				/* We were trying to stop using this adapter. It is no more available. Go to "not used" instead of "not ready" */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_NOT_USED );
			}
			else
			{
				/* Immediately switch this state machine's state to "not ready" state */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_NOT_READY );
			}
		}

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

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

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterStopUsing	:	Stop using an adapter...
 |								The state machine will enter the "clear" state, then
 |								continue to "not used" state.
 |
 |  in_pAdapterContext		:	Context of the adapter to clean
 |
 |  Note					:	~
 |
 |  Return					:	TBX_RESULT_OK if the function succeeded
 |								Other error code if the function could not complete properly.
 |	
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipAdapterStopUsing(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext)
{
	TBX_RESULT	Result;

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

		if( in_pAdapterContext->State == VOIP_ADAPTER_STATE_NOT_USED )
		{
			/* This adapter is not used by this VOIP system. Ignore */
			TBX_EXIT_SUCCESS (TBX_RESULT_OK);
		}

		if( in_pAdapterContext->pFctStateHandler )
		{
			TBX_MSG_HANDLE		hMsg;

			/* Force the state machine to enter the "clear" state */
			hMsg = VoipFormatPrivateMsg
			(
				VOIP_MSG_ID_NOTIF_STOP_USING,
				sizeof( TBX_MSG_HEADER ),
				in_pAdapterContext->AdapterInfo.hAdapter,
				0
			);
			if( hMsg != (TBX_MSG_HANDLE)TBX_HANDLE_INVALID )
			{
				in_pAdapterContext->pFctStateHandler( in_pAdapterContext, hMsg );
				TBXReleaseMsg (g_pContext->hTbxLib, hMsg);
			}
		}

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

	/*---------------------------------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

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