
#include "Std_Types.h"
#include "ComStack_Cfg.h"
#include "CanIf.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_CheckBootLoaderSignature.h"
#include "Ios_BootLoaderVersion.h"
#include "Ios_RunTimeInstance.h"
#include "FBL_PortUserCode.h"

#define MASTER_ECU	0
#define SLAVE_ECU	1

#define MASTER_BOOTLOADER_INFO_MESSAGE_ID		0x743
#define MASTER_DIAGNOSTIC_REQUEST_MESSAGE_ID	0x040

#define SLAVE_BOOTLOADER_INFO_MESSAGE_ID		0x747
#define SALVE_DIAGNOSTIC_REQUEST_MESSAGE_ID		0x044

#define MASTER_BOOTLOADER_INFO_PDU_ID		CanIfConf_CanIfTxPduCfg_FBL_Tx_MasterEcu_Can_Network_CANNODE_0
#define SLAVE_BOOTLOADER_INFO_PDU_ID		CanIfConf_CanIfTxPduCfg_FBL_Tx_SlaveEcu_Can_Network_CANNODE_0

static uint8 BootloaderInfo_SduData[8] = {0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};

static boolean Fbl_Port_BootloaderInfoPublished = FALSE; 	/* Hold the status of publishing BootloaderInfo*/

const PduInfoType BootloaderInfo_PduInfo = {BootloaderInfo_SduData, NULL_PTR, 8};

/* [$Satisfies $SRS_WAE_FBL 014] */
/* Function to check Rx message belong to ECU variant */
boolean FBL_Port_CheckRxCanIdOfEcuVariant(Can_IdType CanId) {

	if ( ( (Fbl_Port_Variant_ECU == MASTER_ECU) & (CanId == MASTER_DIAGNOSTIC_REQUEST_MESSAGE_ID) ) |
		 ( (Fbl_Port_Variant_ECU == SLAVE_ECU)  & (CanId == SALVE_DIAGNOSTIC_REQUEST_MESSAGE_ID)  ) ) {
		return TRUE;
	}
	else {
		return FALSE;
	}
}

/* [$Satisfies $SRS_WAE_FBL 014] [$Satisfies $SRS_WAE_FBL 016] */
/* Function to send Bootloader CAN Info Message */
void FBL_Port_SendBootloaderInfo(void)
{
    Std_ReturnType lRetVal_en = E_NOT_OK;
    uint32 ulBootloaderCRC = (uint32)(fnIosGetBootLoaderSignatureValue());

    BootloaderInfo_SduData[0] = BOOT_VERSION_MAJOR;
    BootloaderInfo_SduData[1] = BOOT_VERSION_MINOR;
    BootloaderInfo_SduData[2] = BOOT_VERSION_REVISION;
    BootloaderInfo_SduData[3] = BOOT_VERSION_BUILD_ID;
    BootloaderInfo_SduData[4] = (uint8)(ulBootloaderCRC);
    BootloaderInfo_SduData[5] = (uint8)(ulBootloaderCRC >> 8);
    BootloaderInfo_SduData[6] = (uint8)(ulBootloaderCRC >> 16);
    BootloaderInfo_SduData[7] = (uint8)(ulBootloaderCRC >> 24);

    if (MASTER_ECU == Fbl_Port_Variant_ECU) {

        lRetVal_en = CanIf_Transmit(MASTER_BOOTLOADER_INFO_PDU_ID, &BootloaderInfo_PduInfo);
    }
    else {

		if (SLAVE_ECU == Fbl_Port_Variant_ECU) {

			lRetVal_en = CanIf_Transmit(SLAVE_BOOTLOADER_INFO_PDU_ID, &BootloaderInfo_PduInfo);
		}
	}

	/* Update status of publishing of BootloaderInfo */
	if (lRetVal_en == E_OK) {

		Fbl_Port_BootloaderInfoPublished = TRUE;
	}
	else {
		/* Do nothing */
	}
}
