/*****************************************************************************/
/**
 * @file link_layer_types.h
 * @author FanHao (hao_fan_bit_ee@163.com)
 * @brief DF Linklayer types definition.
 * @version 0.1
 * @date 2023-05-06
 *
 * @copyright Copyright(c) 2024 Beijing Institute of Technology.\n
 * Lab of Communication and Network
 *
 ******************************************************************************/
/**
 * @defgroup LinkLayer
 * @{
 */

#ifndef __LINK_LAYER_DEF_H_
#define __LINK_LAYER_DEF_H_

/***************************** Include Files *********************************/
#include <stdbool.h>
#include <stdint.h>
#include "geometry/coord.h"
#include "geometry/quaternion.h"
#include "../phy_layer/phy_layer.h"
#ifndef EMULATION
#include "../config/app_config.h"
#endif
/************************** Constant Definitions *****************************/
/**
 * @name Linklayer_Common_Constants
 * @{
 */
// Common node ID definition
#define L2_BROADCAST_ID 0xFF
#define L2_NONE_ID 0

// Routine configure
#define L2_INIT_RTNE_PERIOD 1
#define L2_NORMAL_RTNE_PERIOD 1

// Common slot number definition
#define L2_SLOTNO_NONE 0
#define L2_FRAMENO_NONE 0

// Common number of slots definition
#define L2_SLOTS_PER_FRAME 100
#define L2_SLOTS_PER_SUBFRAME 20

#define L2_NUM_SLOTS_NONE UINT32_MAX
#define L2_NUM_SLOTS_100MS 20
#define L2_NUM_SLOTS_20MS 4
#define L2_NUM_SLOTS_10MS 2
#define L2_NUM_SLOTS_5MS 1
#define L2_NUM_SLOTS_SKIP 0
/** @} Linklayer_Common_Constants*/

/**************************** Type Definitions *******************************/

/**
 * @struct L2_RCD
 * @brief Linklayer recode buffer
 */
typedef struct
{
	uint32_t last;
	uint32_t curr;
	uint32_t next;
} L2_RCD;

/**
 * @enum L2_TOR_TYPE
 */
typedef enum
{
	L2_TOR_SUSPEND = 0,
	L2_TOR_TX = 1,
	L2_TOR_RX = 2
} L2_TOR_TYPE;

/**
 * @enum L2_NODE_TYPE
 */
typedef enum
{
	L2_NODE_NONE = 0,
	L2_NODE_HEAD = 1,
	L2_NODE_MEMBER = 2,
	L2_NODE_BACKUP = 3
} L2_NODE_TYPE;

/**
 * @enum L2_NODE_STATE
 */
typedef enum
{
	L2_NONE_STATE = 0,
	L2_INIT_STATE = 1,
	L2_NORMAL_STATE = 2,
	L2_INTRA_SEEK_STATE = 3,
	L2_INTER_SEEK_STATE = 4,
	L2_SEEK_STATE = 5
} L2_NODE_STATE;

/**
 * @struct L2_NODE_INFO
 * @brief Node information struct contains the detail node info which needed by the link layer.
 */
typedef struct
{
	uint8_t nodeId;		   ///< node ID
	uint8_t clustId;	   ///< node cluster ID
	uint8_t headId;		   ///< cluster head ID
	L2_NODE_TYPE nodeType; ///< node type
	uint8_t beamNo;		   ///< beam number to this node
	COORD pos;			   ///< node position
	COORD speed;		   ///< node speed
	QUATERNION attitude;   ///< node attitude
} L2_NODE_INFO;

/**
 * @enum L2_EVENT_TYPE
 */
typedef enum
{
	L2_EV_NONE = 0,
	L2_EV_SCAN_PROC_DONE = 1,
	L2_EV_NET_ADV_UPDATE = 2,
	L2_EV_NET_SETUP_DONE = 3,
	L2_EV_TIME_SYNC = 4,
	L2_EV_LOST_CXN = 5,
	L2_EV_LOST_NODE = 6,
	L2_EV_SET_FRAME_NUM = 7,
	L2_EV_SET_SLOT_NUM = 8,
	L2_EV_MAIN_STATE_UPDATE = 9,
	L2_ROUTINE_FINISH = 10,
	L2_ROUTINE_UPDATE = 11
} L2_EVENT_TYPE;

/**
 * @struct L2_EVENT
 * @brief Linklayer event instance
 */
typedef struct
{
	L2_EVENT_TYPE event; ///< event type
	void *msg;			 ///< event message
} L2_EVENT;

/**
 * @enum L2_SLOT_TYPE
 */
typedef enum
{
	L2_SLOT_NONE = 0,
	L2_SLOT_IDLE = 1,
	L2_SLOT_SCAN = 2,
	L2_SLOT_ADVERTISE = 3,
	L2_SLOT_CONFIRM = 4,
	L2_SLOT_SERVICE = 5,
} L2_SLOT_TYPE;

/**
 * @enum L2_FRAME_TYPE
 */
typedef enum
{
	L2_NONE_FRAME = 0,
	L2_SCAN_FRAME = 1,
	L2_ADV_FRAME = 2,
	L2_CFM_FRAME = 3,
	L2_SVC_FRAME = 4
} L2_FRAME_TYPE;

/**
 * @enum L2_CRC_TYPE
 */
typedef enum
{
	L2_CRC_NONE = 0,
	L2_CRC16_1021 = 1,
	L2_CRC16_8005 = 2,
	L2_CRC8_31 = 3
} L2_CRC_TYPE;

/**
 * @struct L2_RTNE_TABLE_ENTRY
 * @brief Linklayer routine table entry
 */
typedef struct
{
	L2_SLOT_TYPE slotType : 8; ///< slot type
	uint8_t slotSeq;		   ///< slot sequence number
	uint8_t src;			   ///< source node ID
	uint8_t dst;			   ///< destination node ID
} L2_RTNE_TABLE_ENTRY;

/**
 * @enum SCAN_STAGE
 */
typedef enum
{
	L2_SCAN_NONE_STAGE = 0,
	L2_SCAN_INIT_STAGE = 1,
	L2_SCAN_ADV_STAGE = 2,
	L2_SCAN_CFM_STAGE = 3
} SCAN_STAGE;

/**
 * @struct L2_SCAN_FSM
 * @brief Linklayer scan process FSM instance
 */
typedef struct
{
	bool isLocked;		  ///< indicates if init acq done(beam locked).
	bool isAdvRecvd;	  ///< indicates if advertisement frame done(beam locked).
	bool isAckPending;	  ///< indicates if confirm message acked
	uint8_t scanCnt;	  ///< scan count
	uint8_t advTimeout;	  ///< adv receive try count
	uint8_t cfmTimeout;	  ///< cmf receive try count
	SCAN_STAGE scanStage; ///< scan stage \n
						  /// only for member node
	uint8_t beamNo;		  ///< locked beam number
	uint8_t tableNo;	  ///< scan table index
} L2_SCAN_FSM;

/**
 * @struct L2_SVC_FSM
 * @brief Linklayer service process FSM
 */
typedef struct
{
	uint32_t txCnt;	 ///< tx frame count
	uint32_t rxCnt;	 ///< rx frame count
	uint32_t rxdCnt; ///< received frame count
#ifdef DATALINK_ENABLE_BERTEST
	uint32_t bitTotal; ///< received bits count
	uint32_t bitError; ///< error bits count
	double lossRate;   ///< packet loss rate
	double errorRate;  ///< bit error rate
#endif
} L2_SVC_FSM;

/**
 * @struct L2_RTNE_CONFIG
 * @brief Linklayer routine table configuration
 */
typedef struct
{
	L2_RTNE_TABLE_ENTRY **rtneTable; ///< routine table
	uint32_t rtnePrd;				 ///< period of routine table
	L2_RTNE_TABLE_ENTRY *last;		 ///< last routine entry
	L2_RTNE_TABLE_ENTRY *curr;		 ///< curent routine entry
	L2_RTNE_TABLE_ENTRY *next;		 ///< next routine entry
} L2_RTNE_CONFIG;

/************************** Variable Definitions *****************************/

#endif /* __LINK_LAYER_DEF_H_ */

/** @} LinkLayer*/