From c1e26fa20b1389dd75d7fb03ae7c2c13c4f82d8d Mon Sep 17 00:00:00 2001 From: Gatsby Van Date: Fri, 22 Dec 2023 01:25:00 +0800 Subject: [PATCH] miscellaneous update (enhance code coherence) --- datalink814_ge/src/linklayer/node_list.c | 36 +++++++++++++++++++----- datalink814_ge/src/linklayer/node_list.h | 2 +- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/datalink814_ge/src/linklayer/node_list.c b/datalink814_ge/src/linklayer/node_list.c index 289c530..f0f9151 100644 --- a/datalink814_ge/src/linklayer/node_list.c +++ b/datalink814_ge/src/linklayer/node_list.c @@ -16,6 +16,7 @@ /***************************** Include Files *********************************/ #include +#include #include #include #include "linklayer814_types.h" @@ -34,10 +35,16 @@ inline uint32_t L2_GetNodeListLen(NODE_LIST *list) { /************************** Variable Definitions *****************************/ static NODE_ENTRY util_entry; /************************** Function Prototypes ******************************/ +/** + * @brief helper function to free node entry. + */ static void entry_free(void* val){ free(val); } +/** + * @brief helper function to match node entries by nodeIds + */ static bool entry_match(void* a, void* b){ ASSERT(a != NULL); ASSERT(b != NULL); @@ -64,11 +71,28 @@ int32_t L2_NodeList_Init(NODE_LIST **list) { return SUCCESS; } +/*****************************************************************************/ +/** + * @brief Clear all nodes in the node list, free allocated memory. + * + * @param list + *NONE + * + ******************************************************************************/ void L2_NodeList_Clear(NODE_LIST *list){ ASSERT(list != NULL); list_clear(list); } +/*****************************************************************************/ +/** + * @brief Completely destroy node list. + * + * @param list + * + * @return NONE + * + ******************************************************************************/ void L2_NodeList_Destroy(NODE_LIST *list){ list_destroy(list); } @@ -153,7 +177,7 @@ NODE_ENTRY* L2_AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType * Other Node not exist. * ******************************************************************************/ -int32_t L2_DeleteNode(NODE_LIST *list, uint8_t nodeId) { +int32_t L2_DeleteNodeById(NODE_LIST *list, uint8_t nodeId) { ASSERT(list != NULL); util_entry.nodeInfo.nodeId = nodeId; list_node_t* node = list_find(list, &util_entry); @@ -177,7 +201,7 @@ int32_t L2_DeleteNode(NODE_LIST *list, uint8_t nodeId) { * false node not in the list * ******************************************************************************/ -bool L2_IsNodeInList(NODE_LIST *list, uint8_t nodeId) { +bool L2_isNodeInList(NODE_LIST *list, uint8_t nodeId) { ASSERT(list != NULL); return (L2_GetNodeById(list, nodeId) != NULL); } @@ -419,11 +443,11 @@ int32_t L2_IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation) DBG_ERROR("Failed to initialize list iteration process.\n"); return EALLOC; } - while (iter->next != NULL) + while ((node = list_iterator_next(iter))) { - node = list_iterator_next(iter); Status = operation((NODE_ENTRY*)node->val, param); if(Status != SUCCESS){ + DBG_ERROR("Error occurred when iteratively edit node entry.\n"); break; } } @@ -450,13 +474,11 @@ int32_t L2_IterDeleteNode(NODE_LIST *list, bool (*condition)(NODE_ENTRY *)) { DBG_ERROR("Failed to initialize list iteration process.\n"); return EALLOC; } - while (iter->next != NULL) + while ((node = list_iterator_next(iter))) { - node = list_iterator_next(iter); if (condition((NODE_ENTRY*) node->val) == false) { list_remove(list, node); } - node = list_iterator_next(iter); } list_iterator_destroy(iter); return SUCCESS; diff --git a/datalink814_ge/src/linklayer/node_list.h b/datalink814_ge/src/linklayer/node_list.h index 1b4229f..78288e1 100644 --- a/datalink814_ge/src/linklayer/node_list.h +++ b/datalink814_ge/src/linklayer/node_list.h @@ -46,7 +46,7 @@ void L2_NodeList_Destroy(NODE_LIST *list); uint32_t L2_GetNodeListLen(NODE_LIST *list); NODE_ENTRY* L2_GetNodeById(NODE_LIST *list, uint8_t nodeId); NODE_ENTRY* L2_AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType); -int32_t L2_DeleteNode(NODE_LIST *list, uint8_t nodeId); +int32_t L2_DeleteNodeById(NODE_LIST *list, uint8_t nodeId); int32_t L2_UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos); bool L2_isNodeInList(NODE_LIST *list, uint8_t nodeId); int32_t L2_GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *newPos);