From 3a90bc329b1cadeb8ffd8abf76db16657bc89f84 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Tue, 19 Aug 2025 10:23:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BD=8D?= =?UTF-8?q?=E6=9C=BA=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 16 ++- src/networking/link_layer/link_layer.c | 16 +-- src/networking/link_layer/link_layer.h | 10 +- src/networking/link_layer/link_layer_types.h | 5 +- src/networking/link_layer/routine_table.c | 8 +- src/networking/link_layer/scan.c | 24 ++-- src/networking/link_layer/service.c | 15 ++- src/systembus/systembus.c | 109 ++++++++----------- src/systembus/systembus.h | 2 +- 9 files changed, 106 insertions(+), 99 deletions(-) diff --git a/src/main.c b/src/main.c index b98a685..bfb7d11 100644 --- a/src/main.c +++ b/src/main.c @@ -77,8 +77,10 @@ struct callback_desc apptimer_callback_desc = { // .myType = L2_NODE_HEAD, // .clustId = 1U, // .myId = 1U, -// .headId = 1U, -// .netScale = 10U, +// .headId = L2_HEAD_ID, +// .netScale = L2_NET_SCALE, +// .initTable = initRoutTable, +// .normalTable = normalRoutTable0, // .serviceRate = L2_SVC_512K, // .phyInstPtr = NULL, // physical layer reference // .tmcInstPtr = NULL @@ -88,8 +90,10 @@ L2_CONFIG linkConfig = { .myType = L2_NODE_MEMBER, .clustId = 1U, .myId = 2U, - .headId = 1U, - .netScale = 10U, + .headId = L2_HEAD_ID, + .netScale = L2_NET_SCALE, + .initTable = initRoutTable, + .normalTable = normalRoutTable0, .serviceRate = L2_SVC_512K, .phyInstPtr = NULL, // physical layer reference .tmcInstPtr = NULL @@ -128,8 +132,8 @@ static SYSBUS_CONFIG sysbusConfig = { }; static SYSMON_CONFIG sysmonConfig = { - .sendQueueMax = 32U, - .recvQueueMax = 32U + .sendQueueMax = 16U, + .recvQueueMax = 16U }; /*********************** Driver Instance Definitions **************************/ diff --git a/src/networking/link_layer/link_layer.c b/src/networking/link_layer/link_layer.c index f06b0e0..734c97e 100644 --- a/src/networking/link_layer/link_layer.c +++ b/src/networking/link_layer/link_layer.c @@ -601,8 +601,8 @@ int32_t Linklayer_Init(L2_INST **instPtr, const L2_CONFIG *config) (*instPtr)->tor.last = L2_TOR_SUSPEND; (*instPtr)->tor.curr = L2_TOR_SUSPEND; (*instPtr)->tor.next = L2_TOR_SUSPEND; - (*instPtr)->rtneConfig.rtneTable = initTable; - (*instPtr)->rtneConfig.rtnePrd = ARRAY_SIZE(initTable); + (*instPtr)->rtneConfig.rtneTable = (*instPtr)->config.initTable; + (*instPtr)->rtneConfig.rtnePrd = ARRAY_SIZE(initRoutTable); (*instPtr)->mainState = L2_INIT_STATE; (*instPtr)->nextState = L2_INIT_STATE; (*instPtr)->myInfo.nodeId = config->myId; @@ -698,8 +698,8 @@ int32_t Linklayer_Reconfig(L2_INST *instPtr, const L2_CONFIG *config) instPtr->tor.last = L2_TOR_SUSPEND; instPtr->tor.curr = L2_TOR_SUSPEND; instPtr->tor.next = L2_TOR_SUSPEND; - instPtr->rtneConfig.rtneTable = initTable; - instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(initTable); + instPtr->rtneConfig.rtneTable = instPtr->config.initTable; + instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(initRoutTable); instPtr->mainState = L2_INIT_STATE; instPtr->nextState = L2_INIT_STATE; instPtr->myInfo.nodeId = config->myId; @@ -883,14 +883,14 @@ int32_t Linklayer_EventHandler(L2_INST *instPtr, const L2_EVENT *l2Event) // Reset scan net FMS for next scan process. L2_Scan_Reset(instPtr->scanFSM); UpdateMainSate(instPtr, L2_INIT_STATE); -// instPtr->rtneConfig.rtneTable = initTable; -// instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(initTable); + instPtr->rtneConfig.rtneTable = instPtr->config.initTable; + instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(initRoutTable); } else if (instPtr->nextState == L2_NORMAL_STATE) { UpdateMainSate(instPtr, L2_NORMAL_STATE); -// instPtr->rtneConfig.rtneTable = normalTable0; -// instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(normalTable0); + instPtr->rtneConfig.rtneTable = instPtr->config.normalTable; + instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(normalRoutTable0); } else { diff --git a/src/networking/link_layer/link_layer.h b/src/networking/link_layer/link_layer.h index 9c1de24..3911127 100644 --- a/src/networking/link_layer/link_layer.h +++ b/src/networking/link_layer/link_layer.h @@ -45,6 +45,8 @@ typedef struct uint8_t headId; ///< head node id uint8_t netScale; ///< net scale L2_SVC_RATE serviceRate; ///< service data rate + L2_RTNE_TABLE_ENTRY **initTable; + L2_RTNE_TABLE_ENTRY **normalTable; PHY_INST *phyInstPtr; ///< pointer to system physical layer instance TMC_INST *tmcInstPtr; ///< pointer to system timer controller layer instance } L2_CONFIG; @@ -129,10 +131,10 @@ typedef struct } L2_INST; /************************** Variable Definitions *****************************/ -extern L2_RTNE_TABLE_ENTRY *initTable[L2_INIT_RTNE_PERIOD]; -extern L2_RTNE_TABLE_ENTRY *normalTable0[L2_INIT_RTNE_PERIOD]; -extern L2_RTNE_TABLE_ENTRY *normalTable1[L2_INIT_RTNE_PERIOD]; -extern L2_RTNE_TABLE_ENTRY *normalTable2[L2_INIT_RTNE_PERIOD]; +extern L2_RTNE_TABLE_ENTRY *initRoutTable[L2_INIT_RTNE_PERIOD]; +extern L2_RTNE_TABLE_ENTRY *normalRoutTable0[L2_INIT_RTNE_PERIOD]; +extern L2_RTNE_TABLE_ENTRY *normalRoutTable1[L2_INIT_RTNE_PERIOD]; +extern L2_RTNE_TABLE_ENTRY *normalRoutTable2[L2_INIT_RTNE_PERIOD]; /************************** Function Prototypes ******************************/ /** * @defgroup LinkLayer_Main diff --git a/src/networking/link_layer/link_layer_types.h b/src/networking/link_layer/link_layer_types.h index 4872be7..feb545b 100644 --- a/src/networking/link_layer/link_layer_types.h +++ b/src/networking/link_layer/link_layer_types.h @@ -37,8 +37,9 @@ #define L2_BROADCAST_ID2 0xF2 #define L2_BROADCAST_ID3 0xF4 #define L2_UNICAST_ID 0xF0 -#define L2_NONE_ID 0 -#define L2_HEAD_ID 0x01 +#define L2_NONE_ID 0U +#define L2_HEAD_ID 1U +#define L2_NET_SCALE 10U // Routine configure #define L2_INIT_RTNE_PERIOD 1 diff --git a/src/networking/link_layer/routine_table.c b/src/networking/link_layer/routine_table.c index 645cec2..0a3b000 100644 --- a/src/networking/link_layer/routine_table.c +++ b/src/networking/link_layer/routine_table.c @@ -853,22 +853,22 @@ L2_RTNE_TABLE_ENTRY tab3Mng3Svc[L2_SLOTS_PER_FRAME] = {L2_SLOT_IDLE, 0x00, 0x00, 0x00} }; -L2_RTNE_TABLE_ENTRY *initTable[L2_INIT_RTNE_PERIOD] = +L2_RTNE_TABLE_ENTRY *initRoutTable[L2_INIT_RTNE_PERIOD] = { tab10Mng10Svc }; -L2_RTNE_TABLE_ENTRY *normalTable0[L2_INIT_RTNE_PERIOD] = +L2_RTNE_TABLE_ENTRY *normalRoutTable0[L2_INIT_RTNE_PERIOD] = { tab10Mng10Svc }; -L2_RTNE_TABLE_ENTRY *normalTable1[L2_INIT_RTNE_PERIOD] = +L2_RTNE_TABLE_ENTRY *normalRoutTable1[L2_INIT_RTNE_PERIOD] = { tab10Mng3Svc }; -L2_RTNE_TABLE_ENTRY *normalTable2[L2_INIT_RTNE_PERIOD] = +L2_RTNE_TABLE_ENTRY *normalRoutTable2[L2_INIT_RTNE_PERIOD] = { tab3Mng3Svc }; diff --git a/src/networking/link_layer/scan.c b/src/networking/link_layer/scan.c index 2b6dc0b..e8c8088 100644 --- a/src/networking/link_layer/scan.c +++ b/src/networking/link_layer/scan.c @@ -256,6 +256,7 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) recvPayload->dataLen = message->dataLen; memcpy(recvPayload->data, message->data, message->dataLen); SysMonitor_GenRecvRsp(recvPayload); + free(recvPayload); } // Logging... @@ -276,7 +277,7 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) L2_MSG_ACK *message = (L2_MSG_ACK *)msg; size_t msgLen = sizeof(L2_MSG_ACK); - size_t dataLen = L2_PAYLOAD_LEN_ACK; + size_t maxDataLen = L2_PAYLOAD_LEN_ACK; // Update node status. uint8_t nodeId = message->common.src; @@ -326,12 +327,21 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) } // Get data payload & upload - SYSBUS_SVC_PLD recvPayload; - recvPayload.dataId = message->dataId; - recvPayload.nodeId = message->common.src; - recvPayload.dataLen = message->dataLen; - memcpy(recvPayload.data, message->data, dataLen); - SysMonitor_GenRecvRsp(&recvPayload); + if (message->dataId != DATA_ID_NONE) + { + if (message->dataLen > maxDataLen) + { + DBG_ERROR("Payload length" PRIu16 " out of range.\n", message->dataLen); + return EIGNORE; + } + SYSBUS_SVC_PLD *recvPayload = calloc(1, sizeof(SYSBUS_SVC_PLD)); + recvPayload->dataId = message->dataId; + recvPayload->nodeId = message->common.src; + recvPayload->dataLen = message->dataLen; + memcpy(recvPayload->data, message->data, message->dataLen); + SysMonitor_GenRecvRsp(recvPayload); + free(recvPayload); + } // Logging... instPtr->log.rxLog.dataId = message->dataId; diff --git a/src/networking/link_layer/service.c b/src/networking/link_layer/service.c index 5516b06..4596cdc 100644 --- a/src/networking/link_layer/service.c +++ b/src/networking/link_layer/service.c @@ -201,7 +201,7 @@ static int32_t Service_Packing128K(L2_INST *instPtr, L2_FRAME_TYPE type, uint8_t { // Packing extra payload SYSBUS_SVC_PLD *sendPayload; - SysMonitor_GetSendPayload(&sendPayload); + SysMonitor_GetSendPayload(&sendPayload, dstId); if(sendPayload == NULL) { message->common.dst = L2_NONE_ID; @@ -211,7 +211,7 @@ static int32_t Service_Packing128K(L2_INST *instPtr, L2_FRAME_TYPE type, uint8_t } else { -// message->common.dst = sendPayload->nodeId; + // message->common.dst = sendPayload->nodeId; message->dataId = sendPayload->dataId; message->dataLen = sendPayload->dataLen; memset(message->data, 0xAA, maxDataLen); @@ -279,7 +279,7 @@ static int32_t Service_Packing512K(L2_INST *instPtr, L2_FRAME_TYPE type, uint8_t // Pack extra payload SYSBUS_SVC_PLD *sendPayload; - SysMonitor_GetSendPayload(&sendPayload); + SysMonitor_GetSendPayload(&sendPayload, dstId); if(sendPayload == NULL) { message->common.dst = L2_NONE_ID; @@ -289,7 +289,7 @@ static int32_t Service_Packing512K(L2_INST *instPtr, L2_FRAME_TYPE type, uint8_t } else { -// message->common.dst = sendPayload->nodeId; + // message->common.dst = sendPayload->nodeId; message->dataId = sendPayload->dataId; message->dataLen = sendPayload->dataLen; memset(message->data, 0xAA, maxDataLen); @@ -349,7 +349,7 @@ static int32_t Service_Packing2M(L2_INST *instPtr, L2_FRAME_TYPE type, uint8_t d // Packing extra payload SYSBUS_SVC_PLD *sendPayload; - SysMonitor_GetSendPayload(&sendPayload); + SysMonitor_GetSendPayload(&sendPayload, dstId); if(sendPayload == NULL) { message->common.dst = L2_NONE_ID; @@ -359,7 +359,7 @@ static int32_t Service_Packing2M(L2_INST *instPtr, L2_FRAME_TYPE type, uint8_t d } else { -// message->common.dst = sendPayload->nodeId; + // message->common.dst = sendPayload->nodeId; message->dataId = sendPayload->dataId; message->dataLen = sendPayload->dataLen; memset(message->data, 0xAA, maxDataLen); @@ -541,6 +541,7 @@ static int32_t Service_PktHandler128K(L2_INST *instPtr, uint8_t *msg) recvPayload->dataLen = message->dataLen; memcpy(recvPayload->data, message->data, message->dataLen); SysMonitor_GenRecvRsp(recvPayload); + free(recvPayload); } // Logging... @@ -629,6 +630,7 @@ static int32_t Service_PktHandler512K(L2_INST *instPtr, uint8_t *msg) recvPayload->dataLen = message->dataLen; memcpy(recvPayload->data, message->data, message->dataLen); SysMonitor_GenRecvRsp(recvPayload); + free(recvPayload); } // Logging... @@ -717,6 +719,7 @@ static int32_t Service_PktHandler2M(L2_INST *instPtr, uint8_t *msg) recvPayload->dataLen = message->dataLen; memcpy(recvPayload->data, message->data, message->dataLen); SysMonitor_GenRecvRsp(recvPayload); + free(recvPayload); } // Logging... diff --git a/src/systembus/systembus.c b/src/systembus/systembus.c index f558b59..103d099 100644 --- a/src/systembus/systembus.c +++ b/src/systembus/systembus.c @@ -27,6 +27,9 @@ #include "common.h" #include "error.h" #include "circ_queue.h" +// TODO: better approach? +#include "../networking/link_layer/link_layer.h" +#include "../networking/phy_layer/phy_layer.h" /** * @addtogroup SystemBus_Protocol_Routine @@ -175,8 +178,8 @@ typedef struct typedef struct { SYSMON_CONFIG config; ///< system monitor initialization parameters - CIRC_QUEUE *sendQueue; ///< send queue - CIRC_QUEUE *recvQueue; ///< receive queue + CIRC_QUEUE *sendQueue[L2_NET_SCALE]; ///< send queue + CIRC_QUEUE *recvQueue[L2_NET_SCALE]; ///< receive queue PINGPONG sysInfoCtrl; SYSINFO sysInfo[2]; PINGPONG linkLogCtrl; @@ -196,8 +199,6 @@ static int32_t SystemBus_GenRemoteTestRsp(SYSBUS *instPtr, uint8_t *msg); /**************************** Type Definitions *******************************/ /************************** Variable Definitions *****************************/ // TODO: better approach? -#include "../networking/link_layer/link_layer.h" -#include "../networking/phy_layer/phy_layer.h" extern L2_INST *linkInstPtr; extern L2_CONFIG linkConfig; extern SYSBUS *sysbusDescPtr; @@ -339,6 +340,39 @@ static int32_t SystemBus_SystemConfig(SYSBUS *instPtr, uint8_t *msg) } } + // Routing table selection + if (msg[WD_RTNE_CMD] != instPtr->sysbusLog.lastConfig[3]) + { + switch (msg[WD_RTNE_CMD]) + { + case 0: + { + reconfig |= (linkConfig.normalTable != normalRoutTable0); + linkConfig.normalTable = normalRoutTable0; + break; + } + case 1: + { + reconfig |= (linkConfig.normalTable != normalRoutTable1); + linkConfig.normalTable = normalRoutTable1; + break; + } + case 2: + { + reconfig |= (linkConfig.normalTable != normalRoutTable2); + linkConfig.normalTable = normalRoutTable2; + break; + } + default: + { + // Ignore current command + DBG_ERROR("Unexpected config command 0x%02X:0x%02X", + WD_RTNE_CMD, msg[WD_RTNE_CMD]); + return EIGNORE; + } + } + } + // Enable control if (msg[WD_EN_CMD] == EN_CMD_ENABLE) { @@ -368,58 +402,6 @@ static int32_t SystemBus_SystemConfig(SYSBUS *instPtr, uint8_t *msg) return EIGNORE; } - // Routing table selection - if (msg[WD_RTNE_CMD] != instPtr->sysbusLog.lastConfig[3]) - { - L2_RTNE_CONFIG newRtneConfig; - switch (msg[WD_RTNE_CMD]) - { - case 0: - { - newRtneConfig.rtneTable = normalTable0; - newRtneConfig.rtnePrd = ARRAY_SIZE(normalTable0); - L2_EVENT l2Event = - { - .event = L2_ROUTINE_UPDATE, - .msg = &newRtneConfig - }; - AssertL2Event(linkInstPtr, &l2Event); - break; - } - case 1: - { - newRtneConfig.rtneTable = normalTable1; - newRtneConfig.rtnePrd = ARRAY_SIZE(normalTable1); - L2_EVENT l2Event = - { - .event = L2_ROUTINE_UPDATE, - .msg = &newRtneConfig - }; - AssertL2Event(linkInstPtr, &l2Event); - break; - } - case 2: - { - newRtneConfig.rtneTable = normalTable2; - newRtneConfig.rtnePrd = ARRAY_SIZE(normalTable2); - L2_EVENT l2Event = - { - .event = L2_ROUTINE_UPDATE, - .msg = &newRtneConfig - }; - AssertL2Event(linkInstPtr, &l2Event); - break; - } - default: - { - // Ignore current command - DBG_ERROR("Unexpected config command 0x%02X:0x%02X", - WD_RTNE_CMD, msg[WD_RTNE_CMD]); - return EIGNORE; - } - } - } - instPtr->sysbusLog.lastConfig[0] = msg[WD_EN_CMD]; instPtr->sysbusLog.lastConfig[1] = msg[WD_ID_CMD]; instPtr->sysbusLog.lastConfig[2] = msg[WD_RATE_CMD]; @@ -780,8 +762,8 @@ int32_t SysMonitor_Init(SYSMON_CONFIG *config) } memcpy(&(sysMonitorInst->config), config, sizeof(SYSMON_CONFIG)); - sysMonitorInst->sendQueue = NULL; - sysMonitorInst->recvQueue = NULL; + sysMonitorInst->sendQueue[1] = NULL; + sysMonitorInst->recvQueue[1] = NULL; memset(&(sysMonitorInst->sysInfoCtrl), BUFF_NONE, sizeof(PINGPONG)); memset(&(sysMonitorInst->linkLogCtrl), BUFF_NONE, sizeof(PINGPONG)); memset(&(sysMonitorInst->phyLogCtrl), BUFF_NONE, sizeof(PINGPONG)); @@ -874,6 +856,11 @@ int32_t SysMonitor_UpdateSendPayload(SYSBUS_SVC_PLD *payload) DBG_ERROR("Payload length %d exceeds valid range.", payload->dataLen); return EPARAM; } + if (payload->nodeId < L2_NODE_NONE || payload->nodeId > L2_NET_SCALE) + { + DBG_ERROR("Node ID %d exceeds valid range.", payload->nodeId); + return EPARAM; + } // push message into send queue SYSBUS_SVC_PLD *payloadCopy = malloc(sizeof(SYSBUS_SVC_PLD)); @@ -883,7 +870,7 @@ int32_t SysMonitor_UpdateSendPayload(SYSBUS_SVC_PLD *payload) return ENOMEM; } memcpy(payloadCopy, payload, sizeof(SYSBUS_SVC_PLD)); - if (CircQueuePush(sysMonitorInst->sendQueue, (void *)payloadCopy) == false) + if (CircQueuePush(sysMonitorInst->sendQueue[payload->nodeId], (void *)payloadCopy) == false) { DBG_WARNING("SystemMonitor::SEND Queue overflowed!\n"); free(payloadCopy); @@ -898,12 +885,12 @@ int32_t SysMonitor_UpdateSendPayload(SYSBUS_SVC_PLD *payload) * * @param payload */ -void SysMonitor_GetSendPayload(SYSBUS_SVC_PLD **payload) +void SysMonitor_GetSendPayload(SYSBUS_SVC_PLD **payload, uint8_t dstId) { ASSERT(sysMonitorInst != NULL); ASSERT(payload != NULL); - if (CircQueuePop(sysMonitorInst->sendQueue, (void **)payload) == false) + if (CircQueuePop(sysMonitorInst->sendQueue[dstId], (void **)payload) == false) { *payload = NULL; DBG_INFO("SystemMonitor::Empty sendQueue, nothing need sent.\n"); diff --git a/src/systembus/systembus.h b/src/systembus/systembus.h index 4ebdeb6..c12dc10 100644 --- a/src/systembus/systembus.h +++ b/src/systembus/systembus.h @@ -313,7 +313,7 @@ void SysMonitor_UpdateSysinfo(const SYSINFO *info); void SysMonitor_GetSysinfo(SYSINFO *info); int32_t SysMonitor_UpdateSendPayload(SYSBUS_SVC_PLD *payload); -void SysMonitor_GetSendPayload(SYSBUS_SVC_PLD **payload); +void SysMonitor_GetSendPayload(SYSBUS_SVC_PLD **payload, uint8_t dstId); int32_t SysMonitor_GenRecvRsp(SYSBUS_SVC_PLD *payload); void SysMonitor_UpdateLinklayerLog(const LINKLOG *log);