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

📄 voip_adapter_alloc.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 5 页
字号:
			in_un32TimeSlot
		);

		/* Store the trunk number we are configuring in the user context, to retrieve our context upon response reception */
		TBX_MSG_USER_CONTEXT1_SET( hMsg, (TBX_UINT64)in_pTargetTrunkConfig->un32TrunkNumber );

		/* Store the trunk number we are configuring in the user context, to retrieve our context upon response reception */
		TBX_MSG_USER_CONTEXT2_SET( hMsg, (TBX_UINT64)in_un32TimeSlot );

		/* Fill request parameters */
		pMsg->Request.TrunkResParams.un32StructVersion	= 1;
		pMsg->Request.TrunkResParams.ResType			= TB640_RESOURCE_TYPE_FD;
		pMsg->Request.TrunkResParams.hTrunk				= pCurrentTrunkConfig->hTrunk;
		pMsg->Request.TrunkResParams.un32TimeSlot		= in_un32TimeSlot;
		pMsg->Request.TrunkResParams.ChannelRate		= TB640_RESOURCE_CHANNEL_RATE_64KBPS;
		pMsg->Request.TrunkResParams.un32Channel		= 0;
	}
	/* Send request macro part 2: Send the request, return Result code */
	VOIP_SEND_REQUEST_BODY_PART2("VoipSendAdapterTrunkResAlloc")
}

/* Function to handle the response of the message above */
TBX_RESULT	VoipHandleAdapterTrunkResAllocResponse(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext,
	IN		TBX_MSG_HANDLE			in_hMsg)
{
	TBX_RESULT					Result;
	PTB640_RSP_TRUNK_RES_ALLOC	pResponse;
	TBX_UINT32					un32TrunkNb;
	TBX_UINT32					un32TimeSlot;
	PVOIP_TRUNK_CONFIG			pCurrentTrunkConfig;
	PVOIP_TRUNK_RES				pCurrentTrunkRes;
	PVOIP_TRUNK_CONFIG			pTargetTrunkConfig;
	PVOIP_TRUNK_RES				pTargetTrunkRes;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		pResponse			= TBX_MSG_PAYLOAD_POINTER( in_hMsg );
		un32TrunkNb			= (TBX_UINT32)TBX_MSG_USER_CONTEXT1_GET( in_hMsg );
		un32TimeSlot		= (TBX_UINT32)TBX_MSG_USER_CONTEXT2_GET( in_hMsg );
		pCurrentTrunkConfig	= &in_pAdapterContext->CurrentConfig.aTrunk[ un32TrunkNb ];
		pTargetTrunkConfig	= &in_pAdapterContext->pTargetConfig->aTrunk[ un32TrunkNb ];
		pCurrentTrunkRes	= &pCurrentTrunkConfig->aRes[ un32TimeSlot ];
		pTargetTrunkRes		= &pTargetTrunkConfig->aRes[ un32TimeSlot ];

		/* Count this received response */
		if( in_pAdapterContext->un32NbResponsesExpected )
		{
			in_pAdapterContext->un32NbResponsesExpected--;
		}

		VoipCliAdapterStatePrint
		(
			in_pAdapterContext,
			TRACE_LEVEL_1,
			"Allocate trunk %d timeslot %d result is 0x%08X\n",
			(int)un32TrunkNb,
			(int)un32TimeSlot,
			(int)pResponse->Result
		);

		if( TBX_RESULT_FAILURE( pResponse->Result ) )
		{
			/* Request failed. */
			TBX_EXIT_ERROR (pResponse->Result, 0, "Failed to alloc trunk resource!");
		}

		/* Insert trunk resource handle in resources handle hash table */
		Result = TBXHashInsert
		(
			in_pAdapterContext->CurrentConfig.hResHandleHash,
			(TBX_HASH_KEY)pResponse->hTrunkRes,
			pCurrentTrunkRes
		);
		if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
		{
			TBX_EXIT_ERROR (Result, 0, "Failed to insert element in resource handle hash table.");
		}

		/* Allocate and initialize our "current" context. (config will be copied later in file "voip_adapter_set.c") */
		pCurrentTrunkRes->Common.hRes				= pResponse->hTrunkRes;
		pCurrentTrunkRes->Common.fAllocated			= TBX_TRUE;
		pCurrentTrunkRes->Common.fMustClear			= TBX_FALSE;
		pCurrentTrunkRes->Common.fUsed				= TBX_FALSE;
		pCurrentTrunkRes->Common.un32ConnectionId	= 0;
		pCurrentTrunkRes->ResType					= pTargetTrunkRes->ResType;
		pCurrentTrunkRes->un32TimeSlot				= pTargetTrunkRes->un32TimeSlot;
		pCurrentTrunkRes->ChannelRate				= pTargetTrunkRes->ChannelRate;
		pCurrentTrunkRes->un32Channel				= pTargetTrunkRes->un32Channel;

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

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		VoipCliAdapterStatePrint(
			in_pAdapterContext,
			TRACE_LEVEL_ERROR,
			"VoipHandleAdapterTrunkResAllocResponse: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			(int)TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipSendAdapterTrunkAlloc	:	Alloc a trunk on the adapter.
 |
 |	in_pAdapterContext			:	Adapter configuration we are configuring
 |	in_pTargetTrunkConfig		:	Target trunk to Alloc
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the function succeeded
 |									Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipSendAdapterTrunkAlloc(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext,
	IN		PVOIP_TRUNK_CONFIG		in_pTargetTrunkConfig)
{
	VOIP_SEND_REQUEST_BODY_PART1( TRUNK_OP_ALLOC, 0 )
	{
		if( in_pAdapterContext->fStopUsingPending )
		{
			/* We must not use this adapter anymore (stop using). Don't send request. */
			TBX_EXIT_SUCCESS (Result);
		}
		if( in_pAdapterContext->fNewConfigPending )
		{
			/* Config has been changed. Don't loose too much time trying to allocate things that may not be
			   required in the new config... */
			TBX_EXIT_SUCCESS (Result);
		}

		/* Store the trunk number we are configuring in the user context, to retrieve our context upon response reception */
		TBX_MSG_USER_CONTEXT1_SET( hMsg, (TBX_UINT64)in_pTargetTrunkConfig->un32TrunkNumber );

		/* Fill request parameters */
		pMsg->Request.un32Trunk			= in_pTargetTrunkConfig->un32TrunkNumber;
		pMsg->Request.un64UserContext1	= 0;	/* Don't care */
		pMsg->Request.un64UserContext2	= 0;	/* Don't care */
		memcpy
		(
			&pMsg->Request.TrunkCfg,
			&( in_pAdapterContext->pTargetConfig->aTrunk[ in_pTargetTrunkConfig->un32TrunkNumber ].TrunkCfg ),
			sizeof( pMsg->Request.TrunkCfg )
		);

		VoipCliAdapterStatePrint
		(
			in_pAdapterContext,
			TRACE_LEVEL_1,
			"Allocating trunk %d\n",
			(int)in_pTargetTrunkConfig->un32TrunkNumber
		);
	}
	/* Send request macro part 2: Send the request, return Result code */
	VOIP_SEND_REQUEST_BODY_PART2("VoipSendAdapterTrunkAlloc")
}

/* Function to handle the response of the message above */
TBX_RESULT	VoipHandleAdapterTrunkAllocResponse(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext,
	IN		TBX_MSG_HANDLE			in_hMsg)
{
	TBX_RESULT					Result;
	PTB640_RSP_TRUNK_OP_ALLOC	pResponse;
	TBX_UINT32					un32TrunkNb;
	PVOIP_TRUNK_CONFIG			pCurrentTrunkConfig;
	PVOIP_TRUNK_CONFIG			pTargetTrunkConfig;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		pResponse			= TBX_MSG_PAYLOAD_POINTER( in_hMsg );
		un32TrunkNb			= (TBX_UINT32)TBX_MSG_USER_CONTEXT1_GET( in_hMsg );
		pCurrentTrunkConfig	= &in_pAdapterContext->CurrentConfig.aTrunk[ un32TrunkNb ];
		pTargetTrunkConfig	= &in_pAdapterContext->pTargetConfig->aTrunk[ un32TrunkNb ];

		/* Count this received response */
		if( in_pAdapterContext->un32NbResponsesExpected )
		{
			in_pAdapterContext->un32NbResponsesExpected--;
		}

		if( TBX_RESULT_FAILURE( pResponse->Result ) )
		{
			/* Request failed. */
			TBX_EXIT_ERROR (pResponse->Result, 0, "Failed to allocate trunk!");
		}

		/* Allocate and initialize our "current" context. (config will be copied later in file "voip_adapter_set.c") */
		pCurrentTrunkConfig->fAllocated			= TBX_TRUE;
		pCurrentTrunkConfig->fActivated			= TBX_FALSE;
		pCurrentTrunkConfig->hTrunk				= pResponse->hTrunk;
		pCurrentTrunkConfig->un32TrunkNumber	= un32TrunkNb;
		strncpy( pCurrentTrunkConfig->szTrunkName, pTargetTrunkConfig->szTrunkName, sizeof(pCurrentTrunkConfig->szTrunkName) );
		pCurrentTrunkConfig->szTrunkName[ sizeof(pCurrentTrunkConfig->szTrunkName) - 1] = '\0';
		memcpy( &pCurrentTrunkConfig->TrunkCfg, &pTargetTrunkConfig->TrunkCfg, sizeof(pCurrentTrunkConfig->TrunkCfg) );
		pCurrentTrunkConfig->un32MaxTimeSlot	= pTargetTrunkConfig->un32MaxTimeSlot;

		/* Activate that trunk... */
		Result = VoipSendAdapterTrunkActivate
		(
			in_pAdapterContext,
			&in_pAdapterContext->pTargetConfig->aTrunk[ un32TrunkNb ]
		);

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

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		VoipCliAdapterStatePrint(
			in_pAdapterContext,
			TRACE_LEVEL_ERROR,
			"VoipHandleAdapterTrunkAllocResponse: Trunk %d: %s (Result 0x%08X, %s, line %d)\n",
			(int)un32TrunkNb,
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

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

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipSendAdapterTrunkActivate	:	Activate a trunk on the adapter.
 |
 |	in_pAdapterContext				:	Adapter configuration we are configuring
 |	in_pTargetTrunkConfig			:	Target trunk to activate
 |
 |  Note							:	~
 |
 |  Return							:	TBX_RESULT_OK if the function succeeded
 |										Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipSendAdapterTrunkActivate(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext,
	IN		PVOIP_TRUNK_CONFIG		in_pTargetTrunkConfig)
{
	PVOIP_TRUNK_CONFIG	pCurrentTrunkConfig;

	VOIP_SEND_REQUEST_BODY_PART1( TRUNK_OP_ACTIVE, 0 )
	{
		/* Initialize local variables */
		pCurrentTrunkConfig = NULL;

		if( in_pAdapterContext->fStopUsingPending )
		{
			/* We must not use this adapter anymore (stop using). Don't send request. */
			TBX_EXIT_SUCCESS (Result);
		}
		if( in_pAdapterContext->fNewConfigPending )
		{
			/* Config has been changed. Don't loose too much time trying to allocate things that may not be
			   required in the new config... */
			TBX_EXIT_SUCCESS (Result);
		}

		/* Retrieve the corresponding current trunk context */
		pCurrentTrunkConfig = &in_pAdapterContext->CurrentConfig.aTrunk[ in_pTargetTrunkConfig->un32TrunkNumber ];

		/* Store the trunk number we are configuring in the user context, to retrieve our context upon response reception */
		TBX_MSG_USER_CONTEXT1_SET( hMsg, (TBX_UINT64)in_pTargetTrunkConfig->un32TrunkNumber );

		/* Fill request parameters */
		pMsg->Request.hTrunk			= pCurrentTrunkConfig->hTrunk;

		VoipCliAdapterStatePrint
		(
			in_pAdapterContext,
			TRACE_LEVEL_1,
			"Activating trunk %d\n",
			(int)in_pTargetTrunkConfig->un32TrunkNumber
		);
	}
	/* Send request macro part 2: Send the request, return Result code */
	VOIP_SEND_REQUEST_BODY_PART2("VoipSendAdapterTrunkActivate")
}

/* Function to handle the response of the message above */
TBX_RESULT	VoipHandleAdapterTrunkActivateResponse(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext,
	IN		TBX_MSG_HANDLE			in_hMsg)
{
	TBX_RESULT						Result;
	PTB640_RSP_TRUNK_OP_ACTIVE		pResponse;
	PTB640_RSP_TRUNK_OP_SET_PARAMS	pSetResponse;
	TBX_UINT32						un32TrunkNb;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		pResponse		= TBX_MSG_PAYLOAD_POINTER( in_hMsg );
		pSetResponse	= TBX_MSG_PAYLOAD_POINTER( in_hMsg );
		un32TrunkNb		= (TBX_UINT32)TBX_MSG_USER_CONTEXT1_GET( in_hMsg );

		/* Count this received response */
		if( in_pAdapterContext->un32NbResponsesExpected )
		{
			in_pAdapterContext->un32NbResponsesExpected--;
		}

		if( TBX_RESULT_FAILURE( pResponse->Result ) )
		{
			/* Request failed. */
			TBX_EXIT_ERROR (pResponse->Result, 0, "Failed to activate trunk!");
		}

		/* Mark the trunk as activated */
		in_pAdapterContext->CurrentConfig.aTrunk[ un32TrunkNb ].fActivated = TBX_TRUE;

		/* Call the "set" state machine which is responsible to continue the allocation tree. */
		Result							= pResponse->Result;
		pResponse						= NULL;
		pSetResponse->un32MsgVersion	= 1;
		pSetResponse->Result			= Result;

		/* Trick the number of expected responses before calling VoipHandleAdapterTrunkSetResponse */
		in_pAdapterContext->un32NbResponsesExpected++;
		Result = VoipHandleAdapterTrunkSetResponse

⌨️ 快捷键说明

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