📄 config.c
字号:
PTB640_REQ_ADAPTER_OP_DETACH pReq;
PTB640_RSP_ADAPTER_OP_DETACH pRsp;
/*---------------------------------------------------------------------------------------------------------------------------
| Code section
*--------------------------------------------------------------------------------------------------------------------------*/
CODE
{
/* Initialize local variables */
hFilter = NULL;
hMsg = NULL;
TB640_ISDN_LOG (TRACE_LEVEL_4, "Detaching from adapter(s)\n");
/*-----------------------------------------------------------------------------------------------------------------------
| Detach from the adapters asynchronously
*----------------------------------------------------------------------------------------------------------------------*/
/* Create a new filter to receive response from adapters*/
un64UniqueID = g_AppContext->ahAdapterInfo [0].aun64UniqueId [0] + 0x100 + rand();
FilterParams.un32StructVersion = 1;
FilterParams.FilterMask = (TBX_FILTER_MSG_ID|TBX_FILTER_MSG_USER_CONTEXT1);
FilterParams.MsgId = TB640_MSG_ID_ADAPTER_OP_DETACH;
FilterParams.un64UserContext1 = un64UniqueID;
result = TBXCreateMsgFilter (g_AppContext->hTbxLib, 1, &FilterParams, &hFilter);
if (TBX_RESULT_FAILURE (result))
{
TBX_EXIT_ERROR(result, 0, "Unable to create event filter");
}
/* Detach from the adapters */
un32ExpectedNbResponse = 0;
for (un32Count=0; un32Count<g_AppContext->un32NbAdapter; un32Count++)
{
pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);
/* If the adapter was not attached, no need to send any message */
if (pAdapterInfo->fAttached == TBX_FALSE)
{
continue;
}
/* Format the "detach" request */
result = TBXGetMsg (g_AppContext->hTbxLib, sizeof(*pMsg), &hMsg);
if (TBX_RESULT_FAILURE (result))
{
TBX_EXIT_ERROR(result, 0, "Unable to allocate a message buffer");
}
/* Format the message header */
TBX_FORMAT_MSG_HEADER ( \
hMsg, \
TB640_MSG_ID_ADAPTER_OP_DETACH, \
TBX_MSG_TYPE_REQUEST, \
sizeof(*pMsg), \
pAdapterInfo->hAdapter, \
un64UniqueID, \
un32Count);
/* Format the message payload */
pMsg = (PTB640_MSG_ADAPTER_OP_DETACH)TBX_MSG_PAYLOAD_POINTER (hMsg);
pReq = &(pMsg->Request);
pReq->un32MsgVersion = 1;
/* Send the message */
TBXSendMsg (g_AppContext->hTbxLib, hMsg, NULL);
hMsg = NULL;
un32ExpectedNbResponse++;
}
/* Wait the response */
while (un32ExpectedNbResponse > 0)
{
/* Wait for responses */
result = TBXReceiveMsg (g_AppContext->hTbxLib, hFilter, TB640_ISDN_ADAPTER_ATTACH_TIMEOUT_MSEC, &hMsg);
if ((result != TBX_RESULT_API_TIMEOUT) && TBX_RESULT_FAILURE (result))
{
TBX_EXIT_ERROR(result, 0, "Unable to retrieve message");
}
else if (result == TBX_RESULT_API_TIMEOUT)
{
TB640_ISDN_LOG (TRACE_LEVEL_ALWAYS, "Unable to detach from %s\n", pAdapterInfo->szAdapterName);
TBX_EXIT_ERROR(result, 0, "Unable to detach from adapter");
}
/* Retrieve the adapter number */
un32Count = (TBX_UINT32)TBX_MSG_USER_CONTEXT2_GET(hMsg);
pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);
pMsg = (PTB640_MSG_ADAPTER_OP_DETACH)TBX_MSG_PAYLOAD_POINTER (hMsg);
pRsp = &(pMsg->Response);
/* We are now detached */
un32ExpectedNbResponse--;
pAdapterInfo->fAttached = TBX_FALSE;
pAdapterInfo->hAdapter = (TBX_ADAPTER_HANDLE)NULL;
/* Free the message */
TBXReleaseMsg (g_AppContext->hTbxLib, hMsg);
hMsg = NULL;
}
/* End of the code (skip to cleanup) */
TBX_EXIT_SUCCESS (TBX_RESULT_OK);
}
/*---------------------------------------------------------------------------------------------------------------------------
| Error handling section
*--------------------------------------------------------------------------------------------------------------------------*/
ERROR_HANDLING
{
/* Print error message */
TB640_ISDN_LOG (TRACE_LEVEL_ALWAYS, "%s (Result = 0x%08X, %s, line %d)\n", TBX_ERROR_DESCRIPTION, TBX_ERROR_RESULT, __FILE__, TBX_ERROR_LINE);
}
/*---------------------------------------------------------------------------------------------------------------------------
| Cleanup section
*--------------------------------------------------------------------------------------------------------------------------*/
CLEANUP
{
/* Free any received message */
if (hMsg != (TBX_MSG_HANDLE)NULL)
{
TBXReleaseMsg (g_AppContext->hTbxLib, hMsg);
hMsg = NULL;
}
/* Free the filter */
if (hFilter != NULL)
{
TBXDestroyMsgFilter (g_AppContext->hTbxLib, hFilter);
hFilter = NULL;
}
}
RETURN;
}
/*-------------------------------------------------------------------------------------------------------------------------------
|
| TB640IsdnCtBusResourcesGetKey: This function is called by the hash library to retrieve the key from a ctbus resource
| element
|
| in_pUserContext : User context (not used)
| in_pElement : Pointer to the element
|
| Note : ~
|
| Return : TBX_RESULT_OK
| TBX_RESULT_FAIL
|
*------------------------------------------------------------------------------------------------------------------------------*/
TBX_HASH_KEY TB640IsdnCtBusResourcesGetKey (
IN PTBX_VOID in_pUserContext,
IN PTBX_VOID in_pElement)
{
PTB640_ISDN_CTBUS_INFO pCtBusInfo = (PTB640_ISDN_CTBUS_INFO)in_pElement;
return (TBX_HASH_KEY)(pCtBusInfo->hCtbusResource);
}
/*-------------------------------------------------------------------------------------------------------------------------------
|
| TB640IsdnAllocateCtBusResources: This function allocates CTbus resources on all boards
|
| in_un32AdapterStartIdx: Index of adapter where to start
| in_un32AdapterEndIdx: Index of adapter where to end
|
| Note : ~
|
| Return : TBX_RESULT_OK
| TBX_RESULT_FAIL
|
*------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT
TB640IsdnAllocateCtBusResources (
IN TBX_UINT32 in_un32AdapterStartIdx,
IN TBX_UINT32 in_un32AdapterEndIdx)
{
TBX_RESULT result;
TBX_FILTER_PARAMS FilterParams;
TBX_UINT64 un64UniqueID;
TBX_FILTER_HANDLE hFilter;
TBX_UINT32 un32Count;
TBX_UINT32 un32Count2;
TBX_UINT32 un32Count3;
TBX_UINT32 un32CtbusResourcesRequired;
TBX_UINT32 un32ExpectedNbResponse;
TBX_UINT32 un32FailedAllocation;
TBX_UINT32 un32CtbusIdx;
TBX_MSG_HANDLE hMsg;
PTB640_ISDN_CTBUS_INFO pCtBusInfo;
PTB640_ISDN_ADAPTER_INFO pAdapterInfo;
PTB640_ISDN_TRUNK_INFO pTrunkInfo;
PTB640_ISDN_TRUNK_RESOURCE_INFO pTrunkResInfo;
PTB640_MSG_CTBUS_RES_ALLOC pMsg;
PTB640_REQ_CTBUS_RES_ALLOC pReq;
PTB640_RSP_CTBUS_RES_ALLOC pRsp;
/*---------------------------------------------------------------------------------------------------------------------------
| Code section
*--------------------------------------------------------------------------------------------------------------------------*/
CODE
{
/* Initialize local variables */
hFilter = NULL;
hMsg = NULL;
un32FailedAllocation = 0;
pAdapterInfo = NULL;
TB640_ISDN_LOG (TRACE_LEVEL_4, "Allocating CTbus resources\n");
/* Create a new filter to receive response from adapters*/
un64UniqueID = g_AppContext->ahAdapterInfo [in_un32AdapterStartIdx].aun64UniqueId [0] + 0x100 + rand();
FilterParams.un32StructVersion = 1;
FilterParams.FilterMask = (TBX_FILTER_MSG_ID|TBX_FILTER_MSG_USER_CONTEXT1);
FilterParams.MsgId = TB640_MSG_ID_CTBUS_RES_ALLOC;
FilterParams.un64UserContext1 = un64UniqueID;
result = TBXCreateMsgFilter (g_AppContext->hTbxLib, 1, &FilterParams, &hFilter);
if (TBX_RESULT_FAILURE (result))
{
TBX_EXIT_ERROR(result, 0, "Unable to create event filter");
}
/* Loop for all attached adapters */
un32ExpectedNbResponse = 0;
for (un32Count=in_un32AdapterStartIdx; un32Count<in_un32AdapterEndIdx; un32Count++)
{
pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);
/* Calculate the number of Ctbus resources required (max of 1 for every timeslot) */
un32CtbusResourcesRequired = 0;
for (un32Count2=0; un32Count2<TB640_ISDN_MAX_SUPPORTED_TRUNKS_PER_ADAPTER; un32Count2++)
{
pTrunkInfo = &(pAdapterInfo->aTrunkInfo [un32Count2]);
if (pTrunkInfo->TrunkConfiguration.Type != (TB640_TRUNK_TYPE)NULL)
{
if (pTrunkInfo->TrunkConfiguration.Type == TB640_TRUNK_TYPE_E1)
{
/* E1 trunks have 30 timeslot that can receive calls */
un32CtbusResourcesRequired += 30;
}
else
{
/* T1/J1 trunks have 23 timeslot that can receive calls */
un32CtbusResourcesRequired += 23;
}
}
}
/* Allocate the hash table for fast reverse lookup handle->stream/timeslot */
if (pAdapterInfo->hCtBusHash == NULL)
{
pAdapterInfo->hCtBusHash = TBXHashCreate (
un32CtbusResourcesRequired,
TB640IsdnCtBusResourcesGetKey,
NULL);
if (pAdapterInfo->hCtBusHash == NULL)
{
TBX_EXIT_ERROR(TBX_RESULT_OUT_OF_MEMORY, 0, "Unable to allocate hash table");
}
}
/* Send a request for every Ctbus resource */
for (un32Count2=0; un32Count2<TB640_ISDN_MAX_SUPPORTED_TRUNKS_PER_ADAPTER; un32Count2++)
{
pTrunkInfo = &(pAdapterInfo->aTrunkInfo [un32Count2]);
for (un32Count3=0; un32Count3<TB640_ISDN_MAX_TIMESLOT_IN_TRUNK; un32Count3++)
{
pTrunkResInfo = &(pTrunkInfo->aResourceInfo [un32Count3]);
pCtBusInfo = &(pTrunkResInfo->CtbusInfo);
/* Skip unnecessary timeslots */
if (pTrunkInfo->TrunkConfiguration.Type == TB640_TRUNK_TYPE_E1)
{
if ((un32Count3 == 0) || (un32Count3 == 16))
{
continue;
}
}
else
{
if (un32Count3 >= 23)
{
continue;
}
}
/* Check if the resource is already allocated */
if (pCtBusInfo->hCtbusResource == (TB640_RESOURCE_HANDLE)NULL)
{
/* Configure the static values */
un32CtbusIdx = ((un32Count2 * TB640_ISDN_MAX_TIMESLOT_IN_TRUNK) + un32Count3) * 2;
pCtBusInfo->un8Trunk = (TBX_UINT8)un32Count2;
pCtBusInfo->un8Timeslot = (TBX_UINT8)un32Count3;
pCtBusInfo->un8CtbusStream = (TBX_UINT8)(un32CtbusIdx / TB640_ISDN_MAX_TIMESLOT_PER_CTBUS_STREAM);
pCtBusInfo->un8CtbusTimeslot = (TBX_UINT8)(un32CtbusIdx % TB640_ISDN_MAX_TIMESLOT_PER_CTBUS_STREAM);
/* Format the "alloc" request */
result = TBXGetMsg (g_AppContext->hTbxLib, sizeof(*pMsg), &hMsg);
if (TBX_RESULT_FAILURE (result))
{
TBX_EXIT_ERROR(result, 0, "Unable to allocate a message buffer");
}
/* Format the message header */
TBX_FORMAT_MSG_HEADER ( \
hMsg, \
TB640_MSG_ID_CTBUS_RES_ALLOC, \
TBX_MSG_TYPE_REQUEST, \
sizeof(*pMsg), \
pAdapterInfo->hAdapter, \
un64UniqueID, \
((((un32Count3 << 24) & 0xFF000000) | ((un32Count2 << 16) & 0x00FF0000)) | (un32Count & 0x0000FFFF)));
/* Format the message payload */
pMsg = (PTB640_MSG_CTBUS_RES_ALLOC)TBX_MSG_PAYLOAD_POINTER (hMsg);
pReq = &(pMsg->Request);
pReq->un32MsgVersion = 1;
pReq->CTBusResParams.un32StructVersion = 1;
pReq->CTBusResParams.aun32Stream [0] = pCtBusInfo->un8CtbusStream;
pReq->CTBusResParams.aun32TimeSlot [0] = pCtBusInfo->un8CtbusTimeslot;
pReq->CTBusResParams.aun32Stream [1] = pCtBusInfo->un8CtbusStream;
pReq->CTBusResParams.aun32TimeSlot [1] = pCtBusInfo->un8CtbusTimeslot + 1;
pReq->CTBusResParams.ChannelRate = TB640_RESOURCE_CHANNEL_RATE_64KBPS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -