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

📄 voip_adapter_state.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 5 页
字号:
			 * Response from our "Stream free" messages
			 */
			case TB640_MSG_ID_STREAM_RES_FREE:
			{
				/* Handle this response */
				Result = VoipHandleAdapterStreamResClearResponse( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			/*
			 * Response from our "voice processing free" messages
			 */
			case TB640_MSG_ID_VP_GROUP_FREE:
			{
				/* Handle this response */
				Result = VoipHandleAdapterVpGroupClearResponse( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			/*
			 * Response from our "connection free" messages
			 */
			case TB640_MSG_ID_CONN_OP_DESTROY:
			{
				/* Handle this response */
				Result = VoipHandleAdapterConnectionClearResponse( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			/*
			 * Stream lib related events
			 */
			case VOIP_MSG_ID_OP_STREAM_GET_DATA_ADDR:
			{
				Result = VoipHandleStreamGetDataAddr( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			case VOIP_MSG_ID_OP_STREAM_STOP_CONFIRMED:
			{
				Result = VoipHandleStreamStopConfirmed( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			default:
			{
				/* Not supposed to happen! */
				if( TBX_MSG_TYPE_GET( in_hMsg ) == TBX_MSG_TYPE_RESPONSE )
				{
					if( pAdapterContext->un32NbResponsesExpected )
						pAdapterContext->un32NbResponsesExpected--;
				}
				sprintf( szErrorMsg, "Unexpected message id 0x%X for this state\n", (int)TBX_MSG_ID_GET( in_hMsg ) );
				TBX_EXIT_ERROR (TBX_RESULT_NOT_FOUND, 0, szErrorMsg);
			} break;
		}

		if( TBX_RESULT_SUCCESS( pAdapterContext->StateResult ) )
		{
			/* Proceed next state machine sub-state, if appropriate */
			while( pAdapterContext->un32NbResponsesExpected == 0 )
			{
				if( pAdapterContext->ClearSubState == VOIP_ADAPTER_SUB_STATE_2 )
				{
					/* Send the "clear" messages for all resources that need to be freed for this adapter */
					Result = VoipAdapterClearSubState2( pAdapterContext, TBX_FALSE );
					if( TBX_RESULT_FAILURE( Result ) )
					{
						pAdapterContext->StateResult = Result;
						TBX_EXIT_ERROR (Result, 0, "Failed to clear resources on the adapter.");
					}

					/* Continue clearing with trunk */
					pAdapterContext->ClearSubState = VOIP_ADAPTER_SUB_STATE_3;
				}
				else if( pAdapterContext->ClearSubState == VOIP_ADAPTER_SUB_STATE_3 )
				{
					/* Send the "clear" messages for all resources that need to be freed for this adapter */
					Result = VoipAdapterClearSubState3( pAdapterContext, TBX_FALSE );
					if( TBX_RESULT_FAILURE( Result ) )
					{
						pAdapterContext->StateResult = Result;
						TBX_EXIT_ERROR (Result, 0, "Failed to clear trunks on the adapter.");
					}

					/* Continue clearing with trunk */
					pAdapterContext->ClearSubState = VOIP_ADAPTER_SUB_STATE_DONE;
				}
				else if( pAdapterContext->ClearSubState == VOIP_ADAPTER_SUB_STATE_DONE )
				{
					/* Done clearing */
					pAdapterContext->fClearComplete = TBX_TRUE;
					break;
				}
			}
		}

		/* Test if it's time to detach from adapter events */
		if( pAdapterContext->fAttached )
		{
			if( pAdapterContext->un32NbResponsesExpected == 0 )
			{
				if( ( pAdapterContext->fStopUsingPending ) &&
					!pAdapterContext->fDetachSent )
				{
					VoipSendAdapterDetach( pAdapterContext );
					pAdapterContext->fDetachSent = TBX_TRUE;
				}
			}
		}

		/* Test if continue clear is required */
		if( pAdapterContext->un32NbResponsesExpected == 0 )
		{
			if( TBX_RESULT_SUCCESS( pAdapterContext->StateResult ) )
			{
				if( pAdapterContext->fClearComplete )
				{
					/* Clear is complete. */
				}
				else
				{
					/* Clear is not complete. Continue clearing. */
					Result = VoipAdapterClearSubState1( pAdapterContext, TBX_FALSE );
					if( TBX_RESULT_FAILURE( Result ) )
					{
						pAdapterContext->StateResult = Result;
						TBX_EXIT_ERROR (Result, 0, "Failed to clear resources on the adapter.");
					}
				}
			}
		}

		/* Test if we received all expected responses */
		if( pAdapterContext->un32NbResponsesExpected == 0 && pAdapterContext->fClearComplete )
		{
			/* This adapter is ready to configure adapter */
			pAdapterContext->fReadyToConfigure = TBX_TRUE;

			/* No more waiting for responses. Continue to next state */
			if( pAdapterContext->fStopUsingPending )
			{
				VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_2, "Stop using adapter -> No more pending responses\n" );

				/* We wish to stop using this adapter. Go to "clear" state instead of "Ready" state. */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_CLEAR );
			}
			else if( pAdapterContext->fNewConfigPending )
			{
				VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_2, "Apply new config -> No more pending responses\n" );

				/* We must apply a new config. Go back to 'sync' state */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_SYNC );
			}
			else if( pAdapterContext->fStateTimedout )
			{
				/* Adapter is still ready... but configure timed-out. Maybe we can try sync/configure again... */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_SYNC );
			}
			else if( pAdapterContext->fReadyToClear == TBX_FALSE )
			{
				VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_2, "Not ready to config clear anymore\n" );
				
				/* Go back to sync state and wait till fReadyToClear flags goes back up  */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_SYNC );
			}
			else if( pAdapterContext->fReadyToConfigure == TBX_TRUE )
			{
				VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_2, "Configuration clear done\n" );
				
				/* Finished configuring. Continue to "configure set" state */
				VoipAdapterChangeState( pAdapterContext, VOIP_ADAPTER_STATE_CONFIGURING_ALLOC );
			}
			else
			{
				if( TBX_RESULT_FAILURE( pAdapterContext->StateResult ) )
				{
					/* Synchronization failed. */
					VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_ERROR, "Clear failed. Waiting timeout for retry\n" );
				}
			}
		}

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

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

	return;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterStateConfiguringAlloc	:	Handles adapter state: VOIP_ADAPTER_STATE_CONFIGURING_ALLOC.
 |											This function is called when receiving response/event
 |											from an adapter while configuring that adapter.
 |
 |  io_pAdapterContext					:	Context of the adapter
 |	in_hMsg								:	Message received for this adapter
 |
 |  Note								:	~
 |
 |  Return								:	~
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_VOID VoipAdapterStateConfiguringAlloc(
  IN		PTBX_VOID		io_pAdapterContext,
  IN		TBX_MSG_HANDLE	in_hMsg)
{
	TBX_RESULT				Result;
	PVOIP_ADAPTER_CONTEXT	pAdapterContext = io_pAdapterContext;
	TBX_CHAR				szErrorMsg[ 256 ];

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

		switch( TBX_MSG_ID_GET( in_hMsg ) )
		{
			VOIP_HANDLE_COMMON_EVENTS( pAdapterContext, in_hMsg, TBX_FALSE )

			case VOIP_MSG_ID_NOTIF_STATE_ENTER:
			{
				/* Sanity check */
				if( pAdapterContext->un32NbResponsesExpected != 0 )
				{
					VoipCliAdapterStatePrint
					(
						pAdapterContext,
						TRACE_LEVEL_ERROR,
						"BUG: State entered while %d responses still awaited from previous state!\n",
						(int)pAdapterContext->un32NbResponsesExpected
					);
					pAdapterContext->un32NbResponsesExpected = 0;
				}

				/* Set initial state result */
				pAdapterContext->StateResult		= TBX_RESULT_OK;
				pAdapterContext->fStateTimedout		= TBX_FALSE;
				pAdapterContext->fDetachSent		= TBX_FALSE;

				if( ((pAdapterContext->CurrentConfig.fVpGroup0Available == TBX_FALSE) || (pAdapterContext->Status.fVpGroup0Ready == TBX_TRUE)) &&
					((pAdapterContext->CurrentConfig.fVpGroup1Available == TBX_FALSE) || (pAdapterContext->Status.fVpGroup1Ready == TBX_TRUE)) )
				{
					pAdapterContext->ConfigSubState		= VOIP_ADAPTER_SUB_STATE_2;

					/* Send the "alloc" or "config set" messages for all trunks/clock that need to be configured for this adapter */
					Result = VoipAdapterAllocSubState1( pAdapterContext );
					if( TBX_RESULT_FAILURE( Result ) )
					{
						pAdapterContext->StateResult = Result;
						TBX_EXIT_ERROR (Result, 0, "Failed to configure/allocate trunks/clock on the adapter.");
					}
				}
				else
				{
					pAdapterContext->ConfigSubState = VOIP_ADAPTER_SUB_STATE_1;

					VoipCliAdapterStatePrint
					(
						pAdapterContext,
						TRACE_LEVEL_2,
						"VP group not ready yet...\n"
					);

					/* Get voice processing ready state */
					VoipSendAdapterGetVpGroupReadyState( pAdapterContext );
				}
			} break;

			case VOIP_MSG_ID_NOTIF_ADAPTER_ACTIVATED_CHANGED:
			{
				/* Nothing to do here */
			} break;

			case VOIP_MSG_ID_NOTIF_STOP_USING:
			{
				VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_3, "Stop using adapter -> waiting for pending responses\n" );

				/* We are actually configuring the adapter. Stop configuring, wait for missing responses,
				   and continue to "not used" state. */
				pAdapterContext->fStopUsingPending	= TBX_TRUE;
			} break;

			case VOIP_MSG_ID_NOTIF_APPLY_NEW_CONFIG:
			{
				VoipCliAdapterStatePrint( pAdapterContext, TRACE_LEVEL_2, "Applying new config -> waiting for pending responses\n" );

				/* We were clearing things, but now we have a new config to apply. We will go back to
				   the beginning of the 'clear' state as soon as we received pending responses for current state. */
				pAdapterContext->fNewConfigPending = TBX_TRUE;
			} break;

			case VOIP_MSG_ID_NOTIF_RECHECK_STATES:
			{
				/* Nothing to do */
			} break;

			case VOIP_MSG_ID_NOTIF_UPDATE_ALL_STATES:
			{
				/* Nothing to do */
			} break;

			case VOIP_MSG_ID_NOTIF_STATE_TIMEOUT:
			{
				/* Allocate timeout */
				VoipCliAdapterStatePrint(  pAdapterContext, TRACE_LEVEL_ERROR, "Timeout\n");

				pAdapterContext->fStateTimedout				= TBX_TRUE;
				pAdapterContext->un32NbResponsesExpected	= 0;	/* Assume we will not receive the missing responses. */
			} break;

			/*
			 * Response from adapter detach
			 */
			case TB640_MSG_ID_ADAPTER_OP_DETACH:
			{
				/* Handle this response */
				Result = VoipHandleAdapterDetachResponse( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			/*
			 * Response from our "VP group get ready state" message
			 */
			case TB640_MSG_ID_VP_GROUP_GET_READY_STATE:
			{
				/* Handle this response */
				Result = VoipHandleAdapterGetVpGroupReadyStateResponse( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			/*
			 * Response from our "clock set" messages
			 */
			case TB640_MSG_ID_CLK_SYNC_CFG_SET:
			{
				/* Handle this response */
				Result = VoipHandleAdapterSetClockConfigResponse( pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) ) pAdapterContext->StateResult = Result;
			} break;

			/*
			 * Response from our "BERT alloc" messages
			 */
			case TB640_MSG_ID_BERT_RES_ALLOC:
			{
				/* Handle this response */

⌨️ 快捷键说明

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