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

📄 config.c

📁 telcobridges cas develop
💻 C
📖 第 1 页 / 共 5 页
字号:

			/* Keep the adapter name */
			strcpy (aszAdapterName [un32Count], Info.szAdapterName);

			/* Convert in caps */
			TB640CasToUpper (aszAdapterName [un32Count]);
		}

		/*-----------------------------------------------------------------------------------------------------------------------
		 | Wait for other adapters to register dynamically
		 *----------------------------------------------------------------------------------------------------------------------*/

		/* Start the waiting loop */
		un32AdapterLeftToDiscover = g_AppContext->un32NbAdapter;
		un32TimeMsec = 0;
		while ((un32AdapterLeftToDiscover > 0) && (un32TimeMsec <= TB640_CAS_ADAPTER_DISCOVERY_TIMEOUT_MSEC))
		{
			/* Cycle through all adapter configurations */
			for (un32Count=0; un32Count<g_AppContext->un32NbAdapter; un32Count++)
			{
				/* Was this adapter already found ? */
				pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);
				if (pAdapterInfo->hAdapter != (TBX_ADAPTER_HANDLE)NULL)
				{
					continue;
				}

				/*--------------------------------------------------------------------------------------------------------------
				 | Check for specific named adapters
				 *-------------------------------------------------------------------------------------------------------------*/

				/* Check for a specific name ? */
				if (strcmp (pAdapterInfo->szAdapterName, TB640_CAS_ANY_ADAPTER_STRING) != 0)
				{
					/* Search for that specific adapter */
					for (un32Count2=0; un32Count2<(TBX_UINT32)nAdapters; un32Count2++)
					{
						/* Compare the names */
						if (strcmp (aszAdapterName [un32Count2], pAdapterInfo->szAdapterName) == 0)
						{
							/* We found the adapter */
							un32AdapterLeftToDiscover--;

							/* Store the adapter handle */
							pAdapterInfo->hAdapter = aAdapters [un32Count2];
							afAdapterUsed [un32Count2] = TBX_TRUE;
							TB640_CAS_LOG (TRACE_LEVEL_4, "Adapter %s will be used as adapter %d/%d\n", \
							  pAdapterInfo->szAdapterName, (un32Count+1), g_AppContext->un32NbAdapter);

							break;
						}
					}
				}

				/*--------------------------------------------------------------------------------------------------------------
				 | Check for unspecific adapters
				 *-------------------------------------------------------------------------------------------------------------*/

				/* Take any adapter in the list */
				else
				{
					/* Search for any adapter that is not yet assigned */
					for (un32Count2=0; un32Count2<(TBX_UINT32)nAdapters; un32Count2++)
					{
						TBX_ADAPTER_INFO	Info;

						/* Is this a TB640? */
						result = TBXGetAdapterInfo (g_AppContext->hTbxLib, aAdapters [un32Count2], &Info);
						if (TBX_RESULT_FAILURE (result))
						{
							TBX_EXIT_ERROR(result, 0, "Unable to retrieve adapter information");
						}
						if( Info.AdapterType == TBX_ADAPTER_TYPE_TB640 )
						{
							/* Compare the names */
							if (afAdapterUsed [un32Count2] == TBX_FALSE)
							{
								/* We found the adapter */
								un32AdapterLeftToDiscover--;

								/* Store the adapter handle */
								pAdapterInfo->hAdapter = aAdapters [un32Count2];
								afAdapterUsed [un32Count2] = TBX_TRUE;

								/* Copy the adapter name to our internal structure */
								strcpy (pAdapterInfo->szAdapterName, aszAdapterName [un32Count2]);
								TB640_CAS_LOG (TRACE_LEVEL_0, "Adapter %s will be used as adapter %d/%d\n", \
								  pAdapterInfo->szAdapterName, (un32Count+1), g_AppContext->un32NbAdapter);

								break;
							}
						}
					}
				}

				/*--------------------------------------------------------------------------------------------------------------
				 | Any events for new adapters ?
				 *-------------------------------------------------------------------------------------------------------------*/

				/* Check if we received any events of card discovery */
				result = TBXReceiveMsg (g_AppContext->hTbxLib, hFilter, TB640_CAS_ADAPTER_LOOP_MSG_RECEIVE_TIMEOUT_MSEC, &hMsg);
				if ((result != TBX_RESULT_API_TIMEOUT) && TBX_RESULT_FAILURE (result))
				{
					TBX_EXIT_ERROR(TBX_RESULT_FAIL, 0, "Unable to retrieve message");
				}

				/* Have we received a new board ? */
				if (hMsg != (TBX_MSG_HANDLE)NULL)
				{
					pEvent = (PTBX_EVT_API_NOTIF_ADAPTER_ADDED)TBX_MSG_PAYLOAD_POINTER(hMsg);

					/* Convert in caps */
					TB640CasToUpper (pEvent->Info.szAdapterName);

					/* Make sure we didn't already receive that board identification */
					for (un32Count=0; un32Count<nAdapters; un32Count++)
					{
						/* This adapter is already registered */
						if (strcmp(aszAdapterName [un32Count], pEvent->Info.szAdapterName) == 0)
						{
							break;
						}
					}

					/* Was it already known to us ? */
					if (un32Count >= nAdapters)
					{
						/* No, then register it */
						strcpy (aszAdapterName [un32Count], pEvent->Info.szAdapterName);
						aAdapters [un32Count] = pEvent->Info.hAdapter;
						nAdapters++;
					}

					/* Free the message */
					TBXReleaseMsg (g_AppContext->hTbxLib, hMsg);
					hMsg = NULL;
				}

				/* Wait 20 msec to avoid taking all the CPU waiting for boards */
				TBX_SLEEP_MS (20);
				un32TimeMsec += 20;
			}
		}

		/*-----------------------------------------------------------------------------------------------------------------------
		 | Check if we have all required adapters to continue
		 *----------------------------------------------------------------------------------------------------------------------*/

		/* Have we found every adapter we wanted ? */
		if (un32AdapterLeftToDiscover != 0)
		{
			un32Count2 = 0;
			for (un32Count=0; un32Count<g_AppContext->un32NbAdapter; un32Count++)
			{
				pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);
				if (pAdapterInfo->hAdapter == (TBX_ADAPTER_HANDLE)NULL)
				{
					if (strcmp(pAdapterInfo->szAdapterName, TB640_CAS_ANY_ADAPTER_STRING) == 0)
					{
						un32Count2++;
					}
					else
					{
						TB640_CAS_LOG (TRACE_LEVEL_ALWAYS, "Adapter %s wasn't discovered\n", pAdapterInfo->szAdapterName);
					}
				}
			}

			/* Display indication if "ANY" adapter was used in the configuration file */
			if (un32Count2 > 0)
			{
				TB640_CAS_LOG (TRACE_LEVEL_ALWAYS, "Missing %d unidentified adapter(s) to start\n", un32Count2);
			}

			TB640_CAS_LOG (TRACE_LEVEL_ALWAYS, "Not able to find all required adapter in configuration file\n");
			TBX_EXIT_SUCCESS (TBX_RESULT_NOT_FOUND);
		}

		/* Destroy the filter */
		result = TBXDestroyMsgFilter (g_AppContext->hTbxLib, hFilter);
		if (TBX_RESULT_FAILURE (result))
		{
			TBX_EXIT_ERROR(TBX_RESULT_FAIL, 0, "Unable to destroy filter");
		}
		hFilter = NULL;


	    /*-----------------------------------------------------------------------------------------------------------------------
		 | Check if features list of found adapters match feature requirements for the test.
		 *----------------------------------------------------------------------------------------------------------------------*/

		for (un32Count=0; un32Count<g_AppContext->un32NbAdapter; un32Count++)
		{
			pAdapterInfo = &(g_AppContext->ahAdapterInfo[un32Count]);
			result = TB640CasCheckAdapterFeatures(pAdapterInfo);
            if ( result == TBX_RESULT_NOT_SUPPORTED_BY_LICENSE )
            {
                TBX_ERROR_RESULT = result;
    		}
    		else if (TBX_RESULT_FAILURE(result))
    		{
    			TBX_EXIT_ERROR(result, 0, "Unable to retrieve adapter features information");
    		}
		}

		if( result == TBX_RESULT_NOT_SUPPORTED_BY_LICENSE )
		{
		    TBX_EXIT_ERROR(result, 0, "Adapter features mismatch");
		}


		/*-----------------------------------------------------------------------------------------------------------------------
		 | Attach to 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_ATTACH;
		FilterParams.un64UserContext1 = un64UniqueID;
		result = TBXCreateMsgFilter (g_AppContext->hTbxLib, 1, &FilterParams, &hFilter);
		if (TBX_RESULT_FAILURE (result))
		{
			TBX_EXIT_ERROR(TBX_RESULT_FAIL, 0, "Unable to create event filter");
		}

		/* Attach to the adapters */
		un32ExpectedNbResponse = 0;
		for (un32Count=0; un32Count<g_AppContext->un32NbAdapter; un32Count++)
		{
			pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);

			/* Format the "attach" request */
			result = TBXGetMsg (g_AppContext->hTbxLib, sizeof(*pMsg), &hMsg);
			if (TBX_RESULT_FAILURE (result))
			{
				TBX_EXIT_ERROR(TBX_RESULT_OUT_OF_MEMORY, 0, "Unable to allocate a message buffer");
			}

			/* Format the message header */
			TBX_FORMAT_MSG_HEADER (						\
			  hMsg,										\
			  TB640_MSG_ID_ADAPTER_OP_ATTACH,			\
			  TBX_MSG_TYPE_REQUEST,						\
			  sizeof(*pMsg),							\
			  pAdapterInfo->hAdapter,					\
			  un64UniqueID,								\
			  un32Count);

			/* Format the message payload */
			pMsg = (PTB640_MSG_ADAPTER_OP_ATTACH)TBX_MSG_PAYLOAD_POINTER (hMsg);
			pReq = &(pMsg->Request);
			pReq->un32MsgVersion = 1;
			pReq->fEnableAutoReattach = TBX_TRUE;

			/* Send the message */
			TBXSendMsg (g_AppContext->hTbxLib, hMsg, NULL);
			hMsg = TBX_HANDLE_INVALID;
			un32ExpectedNbResponse++;
		}


		/* Wait the response */
		while (un32ExpectedNbResponse > 0)
		{
			/* Wait for responses */
			result = TBXReceiveMsg (g_AppContext->hTbxLib, hFilter, TB640_CAS_ADAPTER_ATTACH_TIMEOUT_MSEC, &hMsg);
			if ((result != TBX_RESULT_API_TIMEOUT) && TBX_RESULT_FAILURE (result))
			{
				TBX_EXIT_ERROR(TBX_RESULT_FAIL, 0, "Unable to retrieve message");
			}
			else if (result == TBX_RESULT_API_TIMEOUT)
			{
				TB640_CAS_LOG (TRACE_LEVEL_ALWAYS, "Unable to attach to %s\n", pAdapterInfo->szAdapterName);
				TBX_EXIT_ERROR(TBX_RESULT_FAIL, 0, "Unable to attach to adapter");
			}

			/* Retrieve the adapter number */
			un32Count = (TBX_UINT32)TBX_MSG_USER_CONTEXT2_GET(hMsg);
			pAdapterInfo = &(g_AppContext->ahAdapterInfo [un32Count]);
			pMsg = (PTB640_MSG_ADAPTER_OP_ATTACH)TBX_MSG_PAYLOAD_POINTER (hMsg);
			pRsp = &(pMsg->Response);

			/* We are now attached */
			un32ExpectedNbResponse--;
			pAdapterInfo->fAttached = TBX_TRUE;

			/* 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_CAS_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;
}

⌨️ 快捷键说明

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