#include "NDTSCommonHALErrors.h"
#include "..\error\NDTSError.h"

typedef struct 
{
	NDTS_HAL_ReturnCode code ;
	char *pCodeString ;
} NDTS_HAL_Error ;

NDTS_HAL_Error NDTS_HAL_ApiErrors[] = 
{
	{NDTS_HAL_OK,																"NDTS_HAL_OK"},
	{NDTS_HAL_GetHALInfo_MissingHAL,											"NDTS_HAL_GetHALInfo fail: missing HAL implementation."},
	{NDTS_HAL_GetCardInternalIndex_MissingCard,									"NDTS_HAL_GetCardInternalIndex fail: missing card."},
	{NDTS_HAL_EnumerateHALs_InsufficientHALInputBufferSize,						"NDTS_HAL_EnumerateHALs fail: input buffer is too small."},
	{NDTS_HAL_InitialiseHAL_MissingFunctionPointers,							"NDTS_HAL_InitialiseHAL fail: missing HAL implementation function pointers."},
	{NDTS_HAL_RegisterBoard_DifferentCardTypeAlreadyAssignedToHAL,				"NDTS_HAL_RegisterBoard fail: HAL has already been assigned a different card type."},
	{NDTS_HAL_RegisterBoard_CardTypeAlreadyAssignedToDifferentHAL,				"NDTS_HAL_RegisterBoard fail: card type has already been assigned to a different HAL implementation."},
	{NDTS_HAL_InitialiseCardType_NoAvailableHALs,								"NDTS_HAL_InitialiseCardType fail: no available HAL implementations for card type."},
	{NDTS_HAL_InitialiseCardType_NotInitialised,								"NDTS_HAL_InitialiseCardType fail: NDTSCommonHAL is not initialised."},
	{NDTS_HAL_GetBoardIDs_FindDeviceFail,										"NDTS_HAL_GetBoardIDs fail: find device fail."},
	{NDTS_HAL_GetBoardIDs_OpenDeviceFail,										"NDTS_HAL_GetBoardIDs fail: open device fail."},
	{NDTS_HAL_GetBoardIDs_CloseDeviceFail,										"NDTS_HAL_GetBoardIDs fail: close device fail."},
	{NDTS_HAL_GetBoardIDs_InsufficientInputBoardIDBufferSize,					"NDTS_HAL_GetBoardIDs fail: insufficient size for input board ID buffer."},
	{NDTS_HAL_InitialiseHAL_HALIsNotAvailable,									"NDTS_HAL_InitialiseHAL fail: HAL is not available."},
	{NDTS_HAL_InitialiseHAL_CannotFindDLL,										"NDTS_HAL_InitialiseHAL fail: cannot find DLL."},
	{NDTS_HAL_InitialiseHAL_CannotFindDLLFunctionPointer,						"NDTS_HAL_InitialiseHAL fail: cannot find at least one function in the DLL."},
	{NDTS_HAL_AddBoard_HALIsNotInitialised,										"NDTS_HAL_AddBoard fail: HAL is not initialised."},
	{NDTS_HAL_AddBoard_FindDeviceFail,											"NDTS_HAL_AddBoard fail: find device fail."},
	{NDTS_HAL_AddBoard_OpenDeviceFail,											"NDTS_HAL_AddBoard fail: open device fail."},
	{NDTS_HAL_AddBoard_CloseDeviceFail,											"NDTS_HAL_AddBoard fail: close device fail."},
	{NDTS_HAL_AddBoard_SerialEepromPresentFail,									"NDTS_HAL_AddBoard fail: SerialEepromPresentFail fail."},
	{NDTS_HAL_AddBoard_CardNotFound,											"NDTS_HAL_AddBoard fail: requested card cannot be found."},
	{NDTS_HAL_AddBoard_BarMapFail,												"NDTS_HAL_AddBoard fail: bar map fail."},
	{NDTS_HAL_AddBoard_GetCommonBufferPropertiesFail,							"NDTS_HAL_AddBoard fail: get common buffer properties fail."},
	{NDTS_HAL_AddBoard_CommonBufferMapFail,										"NDTS_HAL_AddBoard fail: common buffer map fail."},
	{NDTS_HAL_RemoveBoard_BarUnmapFail,											"NDTS_HAL_RemoveBoard fail: bar unmap fail."},
	{NDTS_HAL_RemoveBoard_CloseDeviceFail,										"NDTS_HAL_RemoveBoard fail: device close fial."},
	{NDTS_HAL_OpenDMAChannel_DMABlockChannelOpenFail,							"NDTS_HAL_OpenDMAChannel fail: cannot open DMA channel."},
	{NDTS_HAL_CloseDMAChannel_DMABlockChannelCloseFail,							"NDTS_HAL_CloseDMAChannel fail: cannot close DMA channel."},
	{NDTS_HAL_PerformDMARead_DMABlockTransferFail,								"NDTS_HAL_PerformDMARead fail: DMA transfer fail."},
	{NDTS_HAL_PerformDMARead_DMAChannelNotOpen,									"NDTS_HAL_PerformDMARead fail: DMA channel not open."},
	{NDTS_HAL_PerformDMAWrite_DMABlockTransferFail,								"NDTS_HAL_PerformDMAWrite fail: DMA transfer fail."},
	{NDTS_HAL_PerformDMAWrite_DMAChannelNotOpen,								"NDTS_HAL_PerformDMAWrite fail: DMA channel not open."},
	{NDTS_HAL_CheckCardIndexState_HALIsNotInitialised,							"NDTS_HAL_CheckCardIndexState fail: HAL is not initialised."},
	{NDTS_HAL_CheckCardIndexState_InvalidCardIndex,								"NDTS_HAL_CheckCardIndexState fail: invalid card index."},
	{NDTS_HAL_CheckCardIndexState_MissingState,									"NDTS_HAL_CheckCardIndexState fail: missing state."},

	{NDTS_HAL_LastError,														"Unknown"}
} ;


NDTSHAL_API const char * CALL_CONV NDTS_HAL_GetCodeString(int errorCode)
{
	int i = 0 ;

	while (NDTS_HAL_ApiErrors[i].code != errorCode && NDTS_HAL_ApiErrors[i].code != NDTS_HAL_LastError)
		i++ ;

	return NDTS_HAL_ApiErrors[i].pCodeString ;
}

void NDTS_HAL_HandleHALError(NDTS_HAL_ReturnCode errorCode) 
{
	NDTS_HandleError(errorCode) ;
}
