/*****************************************************************************/ /** * Description: Header file for 814 linklayer functions * * Date: 2021-11-13 13:33 * LastEditors: Fan Hao * LastEditTime: 2022-07-16 10:12 * Revisions: Rev 0.1 * History: * [2022-07-16]: New branch for DiJian. * [2021-11-23]: Initial version. * * Copyright(c) 2021 Beijing Institute of Technology. * Lab of Communication and Networking * ******************************************************************************/ #ifndef LINKLAYER814_H_ #define LINKLAYER814_H_ /***************************** Include Files *********************************/ #include #include "linklayer814_types.h" #include "node_list.h" #include "schdevent.h" #include "rtt.h" #include "../bit_array/bit_array.h" #include "../phy_ctrl/phy_ctrl.h" #include "../time_ctrl/time_ctrl.h" /************************** Constant Definitions *****************************/ #define DATA_STORE_INTERVAL 100 // 250ms #define NET_INI_STAGE 0 #define NET_NORMAL_STAGE 1 #define NET_INTER_SEEK_STAGE 2 #define NET_INTRA_SEEK_STAGE 3 #define NET_SEEK_STAGE 4 #define NET_ERROR_STAGE -1 #define L2_PACKET_LEN_MAX 1024 // bit /***************** Macros (Inline Functions) Definitions *********************/ #define AssertL2Event(instPtr, event) Linklayer_EventHandler(instPtr, event) /**************************** Type Definitions *******************************/ typedef struct { L2_NODE_TYPE myType; bool isInterHead; uint8_t clustId; uint8_t myId; uint8_t headId; uint8_t bkpHeadId; uint8_t netScale; PHY_INST *phyInstPtr; TMC_INST *tmcInstPtr; } L2_CONFIG; typedef struct { uint32_t frameRx; uint32_t frameTx; uint32_t frameRecv; uint32_t frameCorrect; uint32_t bmRxCnt; uint32_t bmTxCnt; uint32_t bmRecvCnt; uint32_t bmCorrectCnt; uint32_t w2acqRxCnt; uint32_t w2acqTxCnt; uint32_t w2acqRecvCnt; uint32_t w2acqCorrectCnt; uint32_t mgmtRxCnt; uint32_t mgmtTxCnt; uint32_t mgmtRecvCnt; uint32_t mgmtCorrectCnt; uint32_t tsyncRxCnt; uint32_t tsyncTxCnt; uint32_t tsyncRecvCnt; uint32_t tsyncCorrectCnt; uint32_t svcRxCnt; uint32_t svcTxCnt; uint32_t svcRecvCnt; uint32_t svcCorrectCnt; } L2_LOG; typedef struct { L2_CONFIG config; L2_NODE_INFO myInfo; NODE_LIST *nodeList; L2_SCHD_POOL *schdEventPool; uint32_t netStartTime; bool isEnable; // if the node is connected to the network bool dataAvail; // if there is a valid received frame data in rx buffer bool cxn2Net; // if the node is connected to the network L2_RCD frameNum; // stores the last current and next frame number. L2_RCD slotNum; // stores the last current and next slot number. L2_RCD frame; // stores the last current and next link layer frame type. L2_RCD tor; // stores the last current and next link layer tx/rx state. L2_NODE_STATE mainState; // the main state of link layer state L2_NODE_STATE nextState; // next state of link layer state, which will be applied to mainState every second. L2_RTNE_CONFIG rtneConfig; // current routine table configuration and status. L2_SCAN_FSM *scanFSM; // FSM of scan net (link set up) L2_MGMT_FSM *mgmtFSM; // FSM of management net (link management and time sync) L2_SVC_FSM *serviceFSM; // FSM of service net (data service) L2_RTT_FSM *rttFSM; // FSM for RTT (time sync algorithm) uint8_t *sendPktData; uint8_t *recvPktData; L2_LOG log; // for MPC uint8_t buffSwitch; uint8_t mgmtMsgRecvCnt0; uint8_t svcMsgRecvCnt0; uint8_t *mgmtMsgRecvBuff0; // mgmtMsg receive data buffer uint8_t *svcMsgRecvBuff0; // svc receive data buffer uint8_t mgmtMsgRecvCnt1; uint8_t svcMsgRecvCnt1; uint8_t *mgmtMsgRecvBuff1; // mgmtMsg receive data buffer uint8_t *svcMsgRecvBuff1; // svc receive data buffer } L2_INST; /************************** Variable Definitions *****************************/ extern L2_RTNE_TABLE_ENTRY *initTable[L2_INIT_RTNE_PERIOD]; extern L2_RTNE_TABLE_ENTRY *seekTable[L2_SEEK_RTNE_PERIOD]; extern L2_RTNE_TABLE_ENTRY *interSeekTable[L2_NORMAL_RTNE_PERIOD]; extern L2_RTNE_TABLE_ENTRY *intraSeekTable[L2_NORMAL_RTNE_PERIOD]; extern L2_RTNE_TABLE_ENTRY *normalTable[L2_NORMAL_RTNE_PERIOD]; /************************** Function Prototypes ******************************/ // Main int32_t Linklayer_Init(L2_INST **instPtr, const L2_CONFIG *config); int32_t Linklayer_Reconfig(L2_INST *instPtr, const L2_CONFIG *config); void Linklayer_Destroy(L2_INST *instPtr); int32_t Linklayer_Start(L2_INST *instPtr); int32_t Linklayer_Halt(L2_INST *instPtr); int32_t Linklayer_Proc(L2_INST *instPtr); int32_t Linklayer_EventHandler(L2_INST *instPtr, const L2_EVENT *l2Event); int32_t Linklayer_PktHandler(L2_INST *instPtr, L2_FRAME_TYPE frameType); int32_t L2_SetupNextSlot(L2_INST *instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE frameType); // Scheduling Event void L2_EventPoolHandler(L2_INST *instPtr); int32_t L2_ScheduleEventAt(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t frameNo, uint32_t slotNo); int32_t L2_ScheduleEventAfter(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t numSlots); int32_t L2_RescheduleEventAt(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t frameNo, uint32_t slotNo); int32_t L2_RescheduleEventAfter(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t numSlots); // Framing void L2_FrameStateUpdate(L2_INST *instPtr); L2_FRAME_TYPE L2_GetLastRcvFrameType(L2_INST *instPtr); L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame); void FrameGetChunk(BIT_ARRAY *bitarr, uint32_t offset, uint32_t numBit, uint64_t *chunk); void FrameInsertChunk(BIT_ARRAY *bitarr, uint32_t offset, uint32_t numBit, uint64_t chunck); int32_t L2_CrcAppend(uint8_t *dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TYPE crcType); bool L2_CrcCheck(uint8_t *dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TYPE crcType); // Scan Net int32_t L2_Scan_Init(L2_SCAN_FSM **scanFSMPtr); void L2_Scan_Reset(L2_SCAN_FSM *scanFSMPtr); void L2_Scan_Destroy(L2_SCAN_FSM *scanFSMPtr); int32_t L2_Scan_Proc(L2_INST *instPtr); int32_t L2_Scan_PktHandler(L2_INST *instPtr); int32_t L2_Scan_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry); // Management Net int32_t L2_Mgmt_Init(L2_MGMT_FSM **mgmtFSM); void L2_Mgmt_Reset(L2_MGMT_FSM *mgmtFSM); void L2_Mgmt_Destroy(L2_MGMT_FSM *mgmtFSM); int32_t L2_Mgmt_PktHandler(L2_INST *instPtr); int32_t L2_Mgmt_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry); // Service net int32_t L2_Service_Init(L2_SVC_FSM **serviceFSM); void L2_Service_Reset(L2_SVC_FSM *serviceFSM); void L2_Service_Destroy(L2_SVC_FSM *serviceFSM); int32_t L2_Service_PktHandler(L2_INST *instPtr); int32_t L2_Service_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *schedEntry); // Miscellaneous uint8_t L2_GetNodeId(L2_INST *instPtr); uint8_t L2_GetClusterId(L2_INST *instPtr); uint8_t L2_GetHeadId(L2_INST *instPtr); L2_NODE_TYPE L2_GetNodeType(L2_INST *instPtr); L2_NODE_STATE L2_GetMainState(L2_INST *instPtr); L2_RTT_FSM *L2_GetRttFsm(L2_INST *instPtr); bool L2_isDataAvail(L2_INST *instPtr); bool L2_isCxn2Net(L2_INST *instPtr); uint32_t L2_GetFrameLen(L2_FRAME_TYPE l2FrameType); uint32_t L2_GetLastFrameNum(L2_INST *instPtr); uint32_t L2_GetLastSlotNum(L2_INST *instPtr); L2_FRAME_TYPE L2_GetLastFrameType(L2_INST *instPtr); L2_TOR_TYPE L2_GetLastTor(L2_INST *instPtr); uint32_t L2_GetCurrentFrameNum(L2_INST *instPtr); uint32_t L2_GetCurrentSlotNum(L2_INST *instPtr); L2_FRAME_TYPE L2_GetCurrentFrameType(L2_INST *instPtr); L2_TOR_TYPE L2_GetCurrentTor(L2_INST *instPtr); uint32_t L2_GetNextFrameNum(L2_INST *instPtr); uint32_t L2_GetNextSlotNum(L2_INST *instPtr); L2_FRAME_TYPE L2_GetNextFrameType(L2_INST *instPtr); L2_TOR_TYPE L2_GetNextTor(L2_INST *instPtr); // Time syncronization int32_t L2_TimeAlign(L2_INST *instPtr, uint32_t frameNum, uint32_t slotNum, bool relative); // RTT int32_t RTT_Init(L2_RTT_FSM **rttFsmPtr); void RTT_Reset(L2_RTT_FSM *rttFsmPtr); void RTT_Destroy(L2_RTT_FSM *rttFsmPtr); int32_t RTT_FsmUpdate(L2_INST *instPtr, L2_TOR_TYPE torType); #endif /* LINKLAYER814_H_ */