/*******************************************************************************
********************************************************************************
** COPYRIGHT NOTIFICATION (c) 2013 HMS Industrial Networks AB                 **
**                                                                            **
** This code is the property of HMS Industrial Networks AB.                   **
** The source code may not be reproduced, distributed, or used without        **
** permission. When used together with a product from HMS, permission is      **
** granted to modify, reproduce and distribute the code in binary form        **
** without any restrictions.                                                  **
**                                                                            **
** THE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. HMS DOES NOT    **
** WARRANT THAT THE FUNCTIONS OF THE CODE WILL MEET YOUR REQUIREMENTS, OR     **
** THAT THE OPERATION OF THE CODE WILL BE UNINTERRUPTED OR ERROR-FREE, OR     **
** THAT DEFECTS IN IT CAN BE CORRECTED.                                       **
********************************************************************************
********************************************************************************
** File Description:
** Default implementation of ABCC driver callback functions.
********************************************************************************
********************************************************************************
*/
#include <stdio.h>
#include "abcc_td.h"
#include "abp.h"
#include "abcc.h"
#include "abcc_sys_adapt.h"
#include "user_setup.h"

// mandatory host application objects.
#include "ad_obj.h"        /* Application data object:   254                 */     
#include "app_obj.h"       /* Application object:        255                 */

// optional host application objects.
#include "dpv1.h"

/*******************************************************************************
** Constants
********************************************************************************
*/


/*******************************************************************************
** Typedefs
********************************************************************************
*/


/*******************************************************************************
** Public Globals
********************************************************************************
*/


/*******************************************************************************
** Private Globals
********************************************************************************
*/


/*******************************************************************************
** Private Services
********************************************************************************
*/


/*******************************************************************************
** Public Services
********************************************************************************
*/
void ABCC_CbfUserInitReq( void )
{

   /*
   ** Triggers the user initiation sequence.
   **
   ** The function will indicate to ABCC_Driver that the initiation sequence has 
   ** ended by calling ABCC_UserInitComplete(), which in turn will trigger
   ** SETUP_COMPLETE to be sent to the ABCC.
   */
   US_Init();
   US_RunUserInitStateMachine();

}

void ABCC_CbfAdiMappingReq( const AD_AdiEntryType**   ppsAdiEntry,
                            const AD_DefaultMapType** ppsDefaultMap )
{
   
  AD_AdiMappingReq( ppsAdiEntry, ppsDefaultMap );

}

#ifdef ABCC_USER_REMAP_SUPPORT_ENABLED
void ABCC_CbfRemapDone( void )
{
   AD_RemapDone();
}
#endif

BOOL ABCC_CbfUpdateWriteProcessData( UINT8* pxWritePd )
{
  return AD_UpdatePdWriteData( pxWritePd );
}


void ABCC_CbfNewReadPd( UINT8* pxReadPd )
{
   AD_UpdatePdReadData( pxReadPd );
}


void ABCC_CbfDriverError( ABCC_SeverityType eSeverity, ABCC_ErrorCodeType iErrorCode, UINT32 lAddInfo )
{
   //static UINT16 iErrcount = 0;
   printf("ERROR: Severity:%d Error code:%d, AddInfo:%d\n",eSeverity, iErrorCode, lAddInfo);

//   while ( 1 );
}


void ABCC_CbfReceiveMsg( ABP_MsgType* psReceivedMsg )
{
   switch( psReceivedMsg->sHeader.bDestObj )
   {
   /*
   ** mandatory host application objects
   */
   case ABP_OBJ_NUM_APPD:      
      /*
      ** We have received a command to the Application Data object.
      */
      AD_ProcObjectRequest( psReceivedMsg );
      break;      
   case ABP_OBJ_NUM_APP:      
      /*
      ** We have received a command to the Application
      */
      APP_ProcessCmdMsg( psReceivedMsg );      
      break;
   /*
   ** optional host application objects.
   */
   case ABP_OBJ_NUM_DPV1:
      /*
      ** We have received a command to the Profibus DP-V1 object.
      */
      DPV1_ProcessCmdMsg( psReceivedMsg );
      break;

   default:
      /*
      ** We have received a command to an unsupported object.
      */
      ABP_SetMsgErrorResponse( psReceivedMsg, 1, ABP_ERR_UNSUP_OBJ );
      ABCC_SendRespMsg( psReceivedMsg );
      break;
   }
}

void ABCC_CbfWdTimeout( void )
{
    ABCC_USER_DEBUG_PRINT((" ABCC watchdog timeout "));
}

void ABCC_CbfWdTimeoutRecovered( void )
{
    ABCC_USER_DEBUG_PRINT((" ABCC watchdog recovered "));
}


void ABCC_CbfEvent( UINT16 iEvents)
{
   if( iEvents & ABP_INTSTATUS_STATUSI  )
   {
      ABCC_CheckAnbStatus();
   }

   if( iEvents & ABP_INTSTATUS_RDPDI )
   {
      ABCC_RdPdEvent();
   }

   if( iEvents & ABP_INTSTATUS_RDMSGI )
   {
      ABCC_RdMsgEvent();
   }

   if( iEvents & ( ABP_INTSTATUS_WRMSGI | ABP_INTSTATUS_ANBRI ) )
   {
      /*
      ** Check if there is something in the queue to be sent.
      */
      ABCC_CheckSendMessage();
   }
}

void ABCC_CbfAnbStatusChanged( void )
{
    static const char* AnbStateString[ 8 ] = {"ABP_ANB_STATE_SETUP",
                                             "ABP_ANB_STATE_NW_INIT",
                                             "ABP_ANB_STATE_WAIT_PROCESS",
                                             "ABP_ANB_STATE_IDLE",
                                             "ABP_ANB_STATE_PROCESS_ACTIVE",
                                             "ABP_ANB_STATE_ERROR",
                                             "",
                                             "ABP_ANB_STATE_EXCEPTION"};


   ABCC_USER_DEBUG_PRINT(( "ANB_STATUS: %s \n" , AnbStateString[ ABCC_AnbState() ] ));
}



/*******************************************************************************
** End of abcc_cbf.c
********************************************************************************
*/
