/*********************************************************************************************************************
*
* Description:     BLSM module implementation for Port FBL
* FileName:        BLSM_Main.c
* Company:         ETAS GmbH (www.etas.com)
* Generated        28/12/2018 at 11:22
*
**********************************************************************************************************************
*
* COPYRIGHT RESERVED, Copyright ETAS GmbH 2018. All rights reserved, also regarding any disposal,
* exploitation, reproduction, editing, distribution, as well as in the event of applications
* for industrial property rights.
*
**********************************************************************************************************************
* History:
**********************************************************************************************************************
* Author        Date            Comment
**********************************************************************************************************************
BGA1YOK         23/02/2018      First Version    
**********************************************************************************************************************/


/*********************************************************************************************************************
* Includes
*********************************************************************************************************************/
#include "BLSM.h"
#include "BLSM_Callouts.h"
#include "CanSM_ComM.h"
#include "Det.h"
#include "NvM.h"
#include "Dcm_Cbk.h"
#include "Os.h"
#include "CanSM_BswM.h"
#include "FBL_BootM.h"
#include "FBL_DataM.h"
#include "FBL_Priv.h"
#include "FBL_Port.h"
#include "Os_TimeServices.h"

// Removing of compiler warnings and errors because of type re-declaration between hal_stdtypes.h and Std_Types.h
#define _BOOLEAN_DECLARED
#define _UINT8_DECLARED
#define _UINT16_DECLARED
#define _UINT32_DECLARED
#define _UINT64_DECLARED
#define _SINT8_DECLARED
#define _SINT16_DECLARED
#define _SINT32_DECLARED
#define _SINT64_DECLARED
#define _FLOAT32_DECLARED
#define _FLOAT64_DECLARED
#define _STD_RETURN_TYPE_DECLARED
#define _STDVERSION_DECLARED

#include "Ios_Terminal.h"
#include "Ios_Std.h"

/*********************************************************************************************************************
 * Extern Defines
 *********************************************************************************************************************/
extern void FBL_Port_SendBootloaderInfo();

/*********************************************************************************************************************
* Local Defines
*********************************************************************************************************************/

/*********************************************************************************************************************
* Local Macros
*********************************************************************************************************************/

/*********************************************************************************************************************
* Local Types
*********************************************************************************************************************/
/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 1] */
typedef enum
{
  FBL_PORT_BLSM_CMPSTATE_INIT,
  FBL_PORT_BLSM_CMPSTATE_STARTUP,
  FBL_PORT_BLSM_CMPSTATE_RUN
}Fbl_Port_BLSM_CmpStateType;

/*********************************************************************************************************************
* Local Variables
*********************************************************************************************************************/
/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 2] */
static Fbl_Port_BLSM_CmpStateType Fbl_Port_BLSM_CurrentState = FBL_PORT_BLSM_CMPSTATE_INIT;
/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 3] */
static Fbl_Port_BLSM_CmpStateType Fbl_Port_BLSM_PrevState    = FBL_PORT_BLSM_CMPSTATE_INIT;
/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 4] */
/* PRQA S 4304 EOF */ /* <Deviation: rule_10.5_4304_boolean */
static boolean FBL_Port_BLSM_NvmReadAllEnd  = (boolean)FALSE;
/*********************************************************************************************************************
* Local Functions Prototypes
*********************************************************************************************************************/

/*********************************************************************************************************************
* Local Functions Implementation
*********************************************************************************************************************/

/*********************************************************************************************************************
* Exported Variables 
*********************************************************************************************************************/
//Boot timer
uint32 Fbl_BLSM_BootTimer;
//Message received flag
uint32 Fbl_BLSM_BootTimeout_Value;

/*********************************************************************************************************************
* Exported Functions Implementation
*********************************************************************************************************************/
/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 9] */
void Fbl_Port_BLSM_Init (void)
{
  Fbl_Port_BLSM_CurrentState = FBL_PORT_BLSM_CMPSTATE_STARTUP;
  Fbl_Port_BLSM_PrevState    = FBL_PORT_BLSM_CMPSTATE_INIT;
  //Set initially to false
  Fbl_BLSM_BootTimeout_Value = FBL_BLSM_BOOT_TIME_MS;

  /* Driver init list One */
  Fbl_Port_BLSM_DriverInitOne();

  /* Driver init list Two */  
  Fbl_Port_BLSM_DriverInitTwo();  

  if(TRUE == Fbl_IsReprogammingRequested())
  {
      wcu_printf("Reprog request detected - Extending boot time.\n");
      Fbl_BLSM_BootTimeout_Value = FBL_BLSM_EXT_BOOT_TIME_MS;
  }
//  /* Send Boot info */
//  FBL_Port_SendBootloaderInfo();

  /* Init OS timer after DriverInitTwo as RTI is initialized from NVM */
  OS_Timer32_Start(&Fbl_BLSM_BootTimer);
  /* Start Os */
  Os_Start();

  /* This function never returns */
  Os_MainFunction();
}

/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 10] */
void Fbl_Port_BLSM_MainFunction(void)
{
    Dcm_SesCtrlType currSession;
    //uint8 bDummyRAMBlock[8] = {0x55, 0xAA, 0x00, 0xFF, 0x11, 0x22, 0x33, 0x44 };
  switch (Fbl_Port_BLSM_CurrentState)
  {
    case FBL_PORT_BLSM_CMPSTATE_STARTUP:
      /* First call after calling init */
      if (FBL_PORT_BLSM_CMPSTATE_INIT == Fbl_Port_BLSM_PrevState)
      {
        /* Update previous state state */
        Fbl_Port_BLSM_PrevState = FBL_PORT_BLSM_CMPSTATE_STARTUP;
        /* Initialise the bootloader */
        Fbl_Port_BLSM_Initialize_App();
      } else {/* do nothing */}
      if ((boolean)TRUE == FBL_Port_BLSM_NvmReadAllEnd)
      {
        /* Once the NvM has been all read, we can initialise
        the full communication and the DCM and go into the running
        state */
        /* Set CANSM channels to FULLCom */
        /* MISRA DEVIATION: Don't need return value because there is nothing we can do with it anyway. */  
        if (E_OK == CanSM_RequestComMode(ComMConf_ComMChannel_ComMChannel_Can_Network_Channel, COMM_FULL_COMMUNICATION))
        {
            Fbl_Port_BLSM_DriverInitThree();
            Fbl_Port_BLSM_CurrentState = FBL_PORT_BLSM_CMPSTATE_RUN;
        }

      } else {/* do nothing */}
      break;
    case FBL_PORT_BLSM_CMPSTATE_RUN:

      if (Fbl_Port_BLSM_PrevState != FBL_PORT_BLSM_CMPSTATE_RUN)
      {
        /* Change previous state */
        Fbl_Port_BLSM_PrevState = FBL_PORT_BLSM_CMPSTATE_RUN;
      } else {/* do nothing */}

      //Check if Boot timer has elapsed
        if ((boolean)TRUE == OS_Timer32_IsElapsed(Fbl_BLSM_BootTimer, Fbl_BLSM_BootTimeout_Value))
        {
            //wcu_printf("Boot Timer elapsed\n");
            (void)Dcm_GetSesCtrlType(&currSession);
            if (((uint8)DCM_DEFAULT_SESSION == currSession) && (GetTesterPresentNotification() == FALSE))
            {
                //Fbl_DataM_NvmSecretKeyUpdateCounterWriteFunc(bDummyRAMBlock);
                //wcu_printf("No tester detected\n");
                if((boolean)TRUE == Fbl_Port_Bootloader_Application_Boot_Conditions())
                {
                    Fbl_BootM_CheckEcuState();
                }
            }
        }


      break;
    default:
      /* try to recover */
      Fbl_Port_BLSM_CurrentState = FBL_PORT_BLSM_CMPSTATE_STARTUP;
      Fbl_Port_BLSM_PrevState = FBL_PORT_BLSM_CMPSTATE_INIT;
      break;
  }
}

/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 11] */
void Fbl_Port_BLSM_NfyDataMJobEnd (uint8 ServiceId, NvM_RequestResultType JobResult)
{
  if ( NVM_SERVICE_ID_READ_ALL == ServiceId)
  {
    FBL_Port_BLSM_NvmReadAllEnd = (boolean)TRUE;
  }
  else
  {
    /* Do nothing */
  }
}

/* [$Satisfies $RTA_FBL_SDK_DD_BLSM 12] */
/* PRQA S 3673 1 */ /* <Deviation: rule_8.13_3673_pointer to const. This form api generated from RTA-BSW 5.2 */
void ComM_BusSM_ModeIndication (NetworkHandleType Channel, ComM_ModeType *ComMode)
{
  if (*ComMode == COMM_FULL_COMMUNICATION)
  {
    Dcm_ComM_FullComModeEntered(Channel);
  }
  else if (*ComMode == COMM_SILENT_COMMUNICATION)
  {
    Dcm_ComM_SilentComModeEntered(Channel);
  }
  else if (*ComMode == COMM_NO_COMMUNICATION)
  {
    Dcm_ComM_NoComModeEntered(Channel);
  }
  else
  {
    Det_ReportError(0,0,0,0);
  }
}
