/*****************************************************************************/
/**
 * @file time_sync.c
 * @author FanHao (hao_fan_bit_ee@163.com)
 * @brief DF Linklayer time sync implementation file.
 * @version 0.0
 * @date 2023-05-06
 *
 * @copyright Copyright(c) 2024 Beijing Institute of Technology.\n
 * Lab of Communication and Network
 *
 ******************************************************************************/
/**
 * @addtogroup LinkLayer
 * @{
 */

/**
 * @addtogroup LinkLayer_TimeSync
 * @{
 */
/***************************** Include Files *********************************/
#include <string.h>
#include <stdlib.h>
#include "link_layer.h"
#include "../phy_layer/phy_layer.h"
#include "error.h"
#include "common.h"

/************************** Constant Definitions *****************************/
/***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/
/************************** Variable Definitions *****************************/
/************************** Function Prototypes ******************************/

/*****************************************************************************/
/**
 * @brief    Get time deviation to the specified frame and slot number.
 *
 * @param   instPtr	a pointer to linklayer layer instance
 * @param   frameNum
 * @param   slotNum
 * @param   relative
 * @param   timeDev
 *
 * @return  SUCCESS
 * 			EIGNORE there is no received frame
 *
 ******************************************************************************/
int32_t L2_GetTimeDeviation(L2_INST *instPtr, uint32_t frameNum, uint32_t slotNum, bool relative, int64_t *timeDev)
{
	ASSERT(instPtr != NULL);
	ASSERT(timeDev != NULL);
	L1_FRAME_TYPE lastRecvdFrame;
	PHY_INST *phyRef = instPtr->config.phyInstPtr;
	lastRecvdFrame = Phy_GetLastRecvFrameType(phyRef);
	if (lastRecvdFrame == L1_NONE_FRAME)
	{
		DBG_ERROR("Invalid TimeAlign operation, no frame was received.");
		return EIGNORE;
	}

	DBG_INFO("Starting TimeAlign operation...\n");
	// read nextSendTime from physical layer
	uint64_t currTime, expectTime;
	TIME_WORD timeStamp;
	expectTime = ((uint64_t)(frameNum * L2_SLOTS_PER_FRAME + slotNum)) * TIME_FRAC_MAX;
	expectTime += Phy_GetTimeComp(lastRecvdFrame);

	if (relative)
	{
		expectTime += (uint64_t)instPtr->netStartTime * L2_SLOTS_PER_FRAME * TIME_FRAC_MAX;
	}

	Phy_GetLastReceivedFrameSyncTimeStamp(phyRef, &timeStamp);
	currTime = (uint64_t)timeStamp.frac_part;
	currTime += ((uint64_t)timeStamp.int_part) * TIME_FRAC_MAX;

	*timeDev = (int64_t)(expectTime - currTime);
	return SUCCESS;
}

/*****************************************************************************/
/**
 * @brief    Implement time adjustment by applying time deviation.
 *
 * @param   instPtr	a pointer to linklayer layer instance
 * @param   timeDev
 *
 * @return  NONE
 *
 ******************************************************************************/
void L2_TimeAdjust(L2_INST *instPtr, const int64_t *timeDev)
{
	ASSERT(instPtr != NULL);
	ASSERT(timeDev != NULL);
	TIME_WORD slotTimerAdjmet;
	slotTimerAdjmet.int_part = (uint32_t)(*timeDev / TIME_FRAC_MAX);
	slotTimerAdjmet.frac_part = (uint32_t)(*timeDev % TIME_FRAC_MAX);
	TimeCtrl_TimeAdjust(instPtr->config.tmcInstPtr, &slotTimerAdjmet, TM_ADJ_ADD);
}

/** @} LinkLayer_TimeSync*/

/** @} LinkLayer*/