diff --git a/datalink814_ge/src/linklayer/node_list.c b/datalink814_ge/src/linklayer/node_list.c index 1776b72..cea1215 100644 --- a/datalink814_ge/src/linklayer/node_list.c +++ b/datalink814_ge/src/linklayer/node_list.c @@ -16,23 +16,34 @@ /***************************** Include Files *********************************/ #include -#include +#include +#include #include "linklayer814_types.h" #include "node_list.h" +#include "../util/list.h" #include "../common.h" #include "../error.h" /***************** Macros (Inline Functions) Definitions *********************/ -inline int32_t GetNodeNum(NODE_LIST *list) { +inline uint32_t L2_GetNodeListLen(NODE_LIST *list) { ASSERT(list != NULL); - return list->nodeNum; + return list->len; } /**************************** Type Definitions *******************************/ /************************** Variable Definitions *****************************/ - +static NODE_ENTRY util_entry; /************************** Function Prototypes ******************************/ +static void entry_free(void* val){ + free(val); +} +static bool entry_match(void* a, void* b){ + ASSERT(a == NULL); + ASSERT(b != NULL); + return (((NODE_ENTRY*)a)->nodeInfo.nodeId + == ((NODE_ENTRY*)b)->nodeInfo.nodeId); +} /*****************************************************************************/ /** * @brief Initialize node list @@ -43,47 +54,23 @@ inline int32_t GetNodeNum(NODE_LIST *list) { * ENOMEM - not enough memory. * ******************************************************************************/ -int32_t NodeList_Init(NODE_LIST **list) { - NODE_ENTRY *entry; - (*list) = malloc(sizeof(NODE_LIST)); +int32_t L2_NodeList_Init(NODE_LIST **list) { + (*list) = list_new(); if (*list == NULL) { return ENOMEM; } - entry = malloc(sizeof(NODE_ENTRY)); - if(entry == NULL) { - free(*list); - return ENOMEM; - } - entry->nodeInfo.nodeId = 0; - entry->nextNode = entry; - entry->preNode = entry; - (*list)->head = entry; - (*list)->nodeNum = 0; + (*list)->free = entry_free; + (*list)->match = entry_match; return SUCCESS; } -void NodeList_Clear(NODE_LIST *list){ +void L2_NodeList_Clear(NODE_LIST *list){ ASSERT(list != NULL); - NODE_ENTRY *nodeEntry, *deletEntry; - nodeEntry = list->head->nextNode; - while (nodeEntry != list->head) { - deletEntry = nodeEntry; - nodeEntry = nodeEntry->nextNode; - deletEntry->preNode->nextNode = deletEntry->nextNode; - deletEntry->nextNode->preNode = deletEntry->preNode; - DBG_INFO("Deleting node [%d].\n", deletEntry->nodeInfo.nodeId); - free(deletEntry); - list->nodeNum--; - } + list_clear(list); } -void NodeList_Destroy(NODE_LIST *list){ - if(list == NULL){ - return; - } - NodeList_Clear(list); - free(list->head); - free(list); +void L2_NodeList_Destroy(NODE_LIST *list){ + list_destroy(list); } /*****************************************************************************/ /** @@ -96,40 +83,19 @@ void NodeList_Destroy(NODE_LIST *list){ * NULL if node dose not exist. * ******************************************************************************/ -NODE_ENTRY* GetNode(NODE_LIST *list, uint32_t nodeId) { +NODE_ENTRY* L2_GetNodeById(NODE_LIST *list, uint8_t nodeId) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = list->head->nextNode; - while (nodeEntry != list->head) { - if (nodeEntry->nodeInfo.nodeId == nodeId) { - break; - } - else { - nodeEntry = nodeEntry->nextNode; - } - } - if (nodeEntry == list->head) { + util_entry.nodeInfo.nodeId = nodeId; + list_node_t* node = list_find(list, &util_entry); + if(node == NULL) { return NULL; } else { - return nodeEntry; + return ((NODE_ENTRY*) node->val); } } -/*****************************************************************************/ -/** - * @brief Check whether the node is already in the node list. - * - * @param list - * @param nodeId - * - * @return true node in the list - * false node not in the list - * - ******************************************************************************/ -inline bool IsNodeInList(NODE_LIST *list, uint8_t nodeId) { - ASSERT(list != NULL); - return (GetNode(list, nodeId) != NULL); -} + /*****************************************************************************/ /** @@ -143,20 +109,27 @@ inline bool IsNodeInList(NODE_LIST *list, uint8_t nodeId) { * Other Error occurred.(Node already exist, memory alloc error ect.) * ******************************************************************************/ -int32_t AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType) +NODE_ENTRY* L2_AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType) { ASSERT(list != NULL); - if(IsNodeInList(list, nodeId)){ + if(L2_isNodeInList(list, nodeId)){ DBG_WARNING("Node [%d] already exist.\n", nodeId); return FAILURE; } DBG_INFO("Link layer appending node [%d] to node list.\n", nodeId); NODE_ENTRY* nodeEntry = (NODE_ENTRY*) malloc(sizeof(NODE_ENTRY)); if (nodeEntry == NULL) { + DBG_ERROR("Failed to allocated memory for nodeEntry.\n"); + return ENOMEM; + } + list_node_t* node = list_node_new(nodeEntry); + if(node == NULL) { + DBG_ERROR("Failed to allocated memory for list node.\n"); + free(nodeEntry); return ENOMEM; } nodeEntry->nodeInfo.nodeId = nodeId; - nodeEntry->nodeInfo.nodeType = (uint8_t) nodeType; + nodeEntry->nodeInfo.nodeType = (uint8_t)nodeType; nodeEntry->nodeInfo.pos.x = 0; nodeEntry->nodeInfo.pos.y = 0; nodeEntry->nodeInfo.pos.z = 0; @@ -165,12 +138,6 @@ int32_t AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType) nodeEntry->nodeInfo.speed.z = 0; nodeEntry->nodeInfo.beamNo = 0; nodeEntry->ttlCnt = 0; - - nodeEntry->nextNode = list->head; - nodeEntry->preNode = list->head->nextNode; - list->head->nextNode = nodeEntry; - - list->nodeNum++; return SUCCESS; } @@ -185,21 +152,34 @@ int32_t AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType) * Other Node not exist. * ******************************************************************************/ -int32_t DeletNode(NODE_LIST *list, uint8_t nodeId) { +int32_t L2_DeleteNode(NODE_LIST *list, uint8_t nodeId) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); - if(nodeEntry == NULL){ + util_entry.nodeInfo.nodeId = nodeId; + list_node_t* node = list_find(list, &util_entry); + if(node == NULL) { DBG_WARNING("Node [%d] in not in node list, skip deleting process.\n", nodeId); return SUCCESS; } - nodeEntry->preNode->nextNode = nodeEntry->nextNode; - nodeEntry->nextNode->preNode = nodeEntry->preNode; DBG_INFO("Deleting node [%d].\n", nodeId); - free(nodeEntry); - list->nodeNum--; + list_remove(list, node); return SUCCESS; } +/*****************************************************************************/ +/** + * @brief Check whether the node is already in the node list. + * + * @param list + * @param nodeId + * + * @return true node in the list + * false node not in the list + * + ******************************************************************************/ +bool L2_IsNodeInList(NODE_LIST *list, uint8_t nodeId) { + ASSERT(list != NULL); + return (L2_GetNodeById(list, nodeId) != NULL); +} /*****************************************************************************/ /** @@ -214,9 +194,9 @@ int32_t DeletNode(NODE_LIST *list, uint8_t nodeId) { * ******************************************************************************/ -int32_t GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double* dst) { +int32_t L2_GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double* dst) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -238,9 +218,9 @@ int32_t GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double* dst) * FAILURE node not exist. * ******************************************************************************/ -int32_t UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos) { +int32_t L2_UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -263,9 +243,9 @@ int32_t UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos) { * FAILURE node not exist. * ******************************************************************************/ -int32_t GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *pos) { +int32_t L2_GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *pos) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -287,9 +267,9 @@ int32_t GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *pos) { * FAILURE node not exist. * ******************************************************************************/ -int32_t UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpeed) { +int32_t L2_UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpeed) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -312,9 +292,9 @@ int32_t UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpeed) * FAILURE node not exist. * ******************************************************************************/ -int32_t GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed) { +int32_t L2_GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -335,9 +315,9 @@ int32_t GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed) { * FAILURE Node not exist. * ******************************************************************************/ -int32_t UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi) { +int32_t L2_UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -358,9 +338,9 @@ int32_t UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi) { * @return NONE * ******************************************************************************/ -int32_t IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc) { +int32_t L2_IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -381,9 +361,9 @@ int32_t IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc) { * FAILURE Node not exist. * ******************************************************************************/ -int32_t RstNodeTTL(NODE_LIST *list, uint8_t nodeId) { +int32_t L2_RstNodeTTL(NODE_LIST *list, uint8_t nodeId) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -404,9 +384,9 @@ int32_t RstNodeTTL(NODE_LIST *list, uint8_t nodeId) { * @return NONE * ******************************************************************************/ -int32_t GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl) { +int32_t L2_GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl) { ASSERT(list != NULL); - NODE_ENTRY* nodeEntry = GetNode(list, nodeId); + NODE_ENTRY* nodeEntry = L2_GetNodeById(list, nodeId); if (nodeEntry == NULL) { return FAILURE; } @@ -429,17 +409,24 @@ int32_t GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl) { * Other Error occurred. * ******************************************************************************/ -int32_t IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)(NODE_ENTRY *, const void *)) { +int32_t L2_IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)(NODE_ENTRY *, const void *)) { ASSERT(list != NULL); - int32_t Status = SUCCESS; - NODE_ENTRY *nodeEntry = list->head->nextNode; - while (nodeEntry != list->head) { - Status = operation(nodeEntry, param); + int32_t Status; + list_node_t* node; + list_iterator_t* iter = list_iterator_new(list, LIST_HEAD); + if(iter == NULL) { + DBG_ERROR("Failed to initialize list iteration process.\n"); + return EALLOC; + } + while (iter->next != NULL) + { + node = list_iterator_next(iter); + Status = operation((NODE_ENTRY*)node->val, param); if(Status != SUCCESS){ break; } - nodeEntry = nodeEntry->nextNode; } + list_iterator_destroy(iter); return Status; } @@ -454,24 +441,23 @@ int32_t IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)(NO * @return SUCCESS * ******************************************************************************/ -int32_t IterDeleteNode(NODE_LIST *list, bool (*condition)(NODE_ENTRY *)) { +int32_t L2_IterDeleteNode(NODE_LIST *list, bool (*condition)(NODE_ENTRY *)) { ASSERT(list != NULL); - NODE_ENTRY *nodeEntry, *deletEntry; - nodeEntry = list->head->nextNode; - while (nodeEntry != list->head) { - if (condition(nodeEntry) == false) { - deletEntry = nodeEntry; - nodeEntry = nodeEntry->nextNode; - deletEntry->preNode->nextNode = deletEntry->nextNode; - deletEntry->nextNode->preNode = deletEntry->preNode; - DBG_INFO("Deleting node [%d].\n", deletEntry->nodeInfo.nodeId); - free(deletEntry); - list->nodeNum--; - } - else { - nodeEntry = nodeEntry->nextNode; - } + list_node_t* node; + list_iterator_t* iter = list_iterator_new(list, LIST_HEAD); + if(iter == NULL) { + DBG_ERROR("Failed to initialize list iteration process.\n"); + return EALLOC; } + while (iter->next != NULL) + { + 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 fd27d24..1b4229f 100644 --- a/datalink814_ge/src/linklayer/node_list.h +++ b/datalink814_ge/src/linklayer/node_list.h @@ -48,16 +48,15 @@ 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_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); int32_t L2_UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpeed); int32_t L2_GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed); int32_t L2_UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi); int32_t L2_GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double* dst); -int32_t L2_GetNodeNum(NODE_LIST *list); int32_t L2_RstNodeTTL(NODE_LIST *list, uint8_t nodeId); int32_t L2_IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc); int32_t L2_GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl); -bool L2_IsNodeInList(NODE_LIST *list, uint8_t nodeId); int32_t L2_IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)(NODE_ENTRY*, const void*)); int32_t L2_IterDeleteNode(NODE_LIST *list, bool (*condition)(NODE_ENTRY *));