mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
Merge pull request #1640 from authmillenon/packetqueue
net: Initial import of a packet wrapper for priority_queue
This commit is contained in:
commit
36f8e3c5c3
@ -1,2 +1,3 @@
|
||||
PSEUDOMODULES += defaulttransceiver
|
||||
PSEUDOMODULES += transport_layer
|
||||
PSEUDOMODULES += pktqueue
|
||||
|
@ -18,6 +18,9 @@ endif
|
||||
ifneq (,$(filter net_if,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/include
|
||||
endif
|
||||
ifneq (,$(filter pktqueue,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/include
|
||||
endif
|
||||
ifneq (,$(filter protocol_multiplex,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/include
|
||||
endif
|
||||
|
134
sys/net/include/pktqueue.h
Normal file
134
sys/net/include/pktqueue.h
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Martin Lenders <mlenders@inf.fu-berlin.de>
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for
|
||||
* more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup pktqueue
|
||||
* @addtogroup net
|
||||
* @{
|
||||
*
|
||||
* @file pktqueue.h
|
||||
* @brief Pointer-centric wrapper for @ref priority_queue
|
||||
*
|
||||
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __PKTQUEUE_H_
|
||||
#define __PKTQUEUE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "priority_queue.h"
|
||||
|
||||
/**
|
||||
* @brief data type for packet queue nodes
|
||||
*
|
||||
* @extends priority_queue_node_t
|
||||
*/
|
||||
typedef struct pktqueue_node_t {
|
||||
struct pktqueue_node_t *next; /**< next node in queue */
|
||||
void *data; /**< pointer to the data */
|
||||
uint32_t priority; /**< priority of the node */
|
||||
} pktqueue_node_t;
|
||||
|
||||
/**
|
||||
* @brief data type for packet queues
|
||||
*
|
||||
* @extends prioriry_queue_t
|
||||
*/
|
||||
typedef struct {
|
||||
pktqueue_node_t *first; /**< first node in the queue */
|
||||
} pktqueue_t;
|
||||
|
||||
/**
|
||||
* @brief Static initializer for pktqueue_node_t
|
||||
*/
|
||||
#define PKTQUEUE_NODE_INIT { NULL, NULL, 0 }
|
||||
|
||||
/**
|
||||
* @brief Initializes a packet queue node.
|
||||
* @details For initialization of variables use PKTQUEUE_NODE_INIT instead.
|
||||
* Only use this function for dynamically allocated packet queue nodes.
|
||||
*
|
||||
* @param[out] node pre-allocated pktqueue_node_t object. must not be NULL.
|
||||
*/
|
||||
static inline void pktqueue_node_init(pktqueue_node_t *node)
|
||||
{
|
||||
priority_queue_node_init((priority_queue_node_t *)node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Static initializer for pktqueue_t.
|
||||
*/
|
||||
#define PKTQUEUE_INIT PRIORITY_QUEUE_INIT
|
||||
|
||||
/**
|
||||
* @brief Initialize a packet queue object.
|
||||
* @details For initialization of variables use PKTQUEUE_INIT
|
||||
* instead. Only use this function for dynamically allocated
|
||||
* packet queues.
|
||||
* @param[out] queue pre-allocated pktqueue_t object, must not be NULL.
|
||||
*/
|
||||
static inline void pktqueue_init(pktqueue_t *queue)
|
||||
{
|
||||
priority_queue_init((priority_queue_t *)queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get the packet queue's head without removing it
|
||||
*
|
||||
* @param[out] root the queue
|
||||
*
|
||||
* @return the head
|
||||
*/
|
||||
static inline pktqueue_node_t *pktqueue_get_head(pktqueue_t *queue)
|
||||
{
|
||||
return queue->first;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief remove the packet queue's head
|
||||
*
|
||||
* @param[in] root the queue
|
||||
*
|
||||
* @return the old head
|
||||
*/
|
||||
static inline pktqueue_node_t *pktqueue_remove_head(pktqueue_t *queue)
|
||||
{
|
||||
return (pktqueue_node_t *)priority_queue_remove_head((priority_queue_t *)queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief add *node* into *queue* based on its priority
|
||||
*
|
||||
* @details The new node will be appended after objects with the same
|
||||
* priority.
|
||||
*
|
||||
* @param[in] queue the queue
|
||||
* @param[in] node the node to add
|
||||
*/
|
||||
static inline void pktqueue_add(pktqueue_t *queue, pktqueue_node_t *node)
|
||||
{
|
||||
priority_queue_add((priority_queue_t *)queue, (priority_queue_node_t *) node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief remove *node* from *queue*
|
||||
*
|
||||
* @param[in] queue the queue
|
||||
* @param[in] node the node to remove
|
||||
*/
|
||||
static inline void pktqueue_remove(pktqueue_t *queue, pktqueue_node_t *node)
|
||||
{
|
||||
priority_queue_remove((priority_queue_t *)queue, (priority_queue_node_t *) node);
|
||||
}
|
||||
|
||||
#endif /* __PKTQUEUE_H_ */
|
||||
/**
|
||||
* @}
|
||||
*/
|
3
tests/unittests/tests-pktqueue/Makefile
Normal file
3
tests/unittests/tests-pktqueue/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
MODULE = tests-pktqueue
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
1
tests/unittests/tests-pktqueue/Makefile.include
Normal file
1
tests/unittests/tests-pktqueue/Makefile.include
Normal file
@ -0,0 +1 @@
|
||||
USEMODULE += pktqueue
|
167
tests/unittests/tests-pktqueue/tests-pktqueue.c
Normal file
167
tests/unittests/tests-pktqueue/tests-pktqueue.c
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Martin Lenders
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @{
|
||||
*
|
||||
* @file tests-pktqueue.c
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "embUnit/embUnit.h"
|
||||
|
||||
#include "pktqueue.h"
|
||||
|
||||
#include "tests-pktqueue.h"
|
||||
|
||||
#define Q_LEN (4)
|
||||
|
||||
static pktqueue_t q = PKTQUEUE_INIT;
|
||||
static pktqueue_node_t qe[Q_LEN];
|
||||
|
||||
static void set_up(void)
|
||||
{
|
||||
pktqueue_init(&q);
|
||||
|
||||
for (unsigned i = 0; i < Q_LEN; ++i) {
|
||||
pktqueue_node_init(&(qe[i]));
|
||||
}
|
||||
}
|
||||
|
||||
static void test_pktqueue_remove_head_empty(void)
|
||||
{
|
||||
pktqueue_t *root = &q;
|
||||
pktqueue_node_t *res;
|
||||
|
||||
res = pktqueue_remove_head(root);
|
||||
|
||||
TEST_ASSERT_NULL(res);
|
||||
}
|
||||
|
||||
static void test_pktqueue_remove_head_one(void)
|
||||
{
|
||||
pktqueue_t *root = &q;
|
||||
pktqueue_node_t *elem = &(qe[1]), *res;
|
||||
|
||||
elem->data = (void *)62801;
|
||||
|
||||
pktqueue_add(root, elem);
|
||||
|
||||
res = pktqueue_remove_head(root);
|
||||
|
||||
TEST_ASSERT(res == elem);
|
||||
TEST_ASSERT(((void *)62801) == res->data);
|
||||
|
||||
res = pktqueue_remove_head(root);
|
||||
|
||||
TEST_ASSERT_NULL(res);
|
||||
}
|
||||
|
||||
static void test_pktqueue_add_one(void)
|
||||
{
|
||||
pktqueue_t *root = &q;
|
||||
pktqueue_node_t *elem = &(qe[1]);
|
||||
|
||||
elem->data = (void *)7317;
|
||||
elem->priority = 713643658;
|
||||
|
||||
pktqueue_add(root, elem);
|
||||
|
||||
TEST_ASSERT(root->first == elem);
|
||||
TEST_ASSERT(((void *)7317) == root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(713643658, root->first->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next);
|
||||
}
|
||||
|
||||
static void test_pktqueue_add_two_equal(void)
|
||||
{
|
||||
pktqueue_t *root = &q;
|
||||
pktqueue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
|
||||
|
||||
elem1->data = (void *)27088;
|
||||
elem1->priority = 14202;
|
||||
|
||||
elem2->data = (void *)4356;
|
||||
elem2->priority = 14202;
|
||||
|
||||
pktqueue_add(root, elem1);
|
||||
pktqueue_add(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem2);
|
||||
TEST_ASSERT(((void *)4356) == root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(14202, root->first->priority);
|
||||
|
||||
TEST_ASSERT(root->first->next == elem1);
|
||||
TEST_ASSERT(((void *)27088) == root->first->next->data);
|
||||
TEST_ASSERT_EQUAL_INT(14202, root->first->next->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
static void test_pktqueue_add_two_distinct(void)
|
||||
{
|
||||
pktqueue_t *root = &q;
|
||||
pktqueue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
|
||||
|
||||
elem1->data = (void *)46421;
|
||||
elem1->priority = 4567;
|
||||
|
||||
elem2->data = (void *)43088;
|
||||
elem2->priority = 1234;
|
||||
|
||||
pktqueue_add(root, elem1);
|
||||
pktqueue_add(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem2);
|
||||
TEST_ASSERT(((void *)43088) == root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(1234, root->first->priority);
|
||||
|
||||
TEST_ASSERT(root->first->next == elem1);
|
||||
TEST_ASSERT(((void *)46421) == root->first->next->data);
|
||||
TEST_ASSERT_EQUAL_INT(4567, root->first->next->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
static void test_pktqueue_remove_one(void)
|
||||
{
|
||||
pktqueue_t *root = &q;
|
||||
pktqueue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]), *elem3 = &(qe[3]);
|
||||
|
||||
pktqueue_add(root, elem1);
|
||||
pktqueue_add(root, elem2);
|
||||
pktqueue_add(root, elem3);
|
||||
pktqueue_remove(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem1);
|
||||
TEST_ASSERT(root->first->next == elem3);
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
Test *tests_pktqueue_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_pktqueue_remove_head_empty),
|
||||
new_TestFixture(test_pktqueue_remove_head_one),
|
||||
new_TestFixture(test_pktqueue_add_one),
|
||||
new_TestFixture(test_pktqueue_add_two_equal),
|
||||
new_TestFixture(test_pktqueue_add_two_distinct),
|
||||
new_TestFixture(test_pktqueue_remove_one),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(pktqueue_tests, set_up, NULL, fixtures);
|
||||
|
||||
return (Test *)&pktqueue_tests;
|
||||
}
|
||||
|
||||
void tests_pktqueue(void)
|
||||
{
|
||||
TESTS_RUN(tests_pktqueue_tests());
|
||||
}
|
||||
/** @} */
|
29
tests/unittests/tests-pktqueue/tests-pktqueue.h
Normal file
29
tests/unittests/tests-pktqueue/tests-pktqueue.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Martin Lenders
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup unittests
|
||||
* @{
|
||||
*
|
||||
* @file tests-pktqueue.h
|
||||
* @brief Unittests for the ``pktqueue`` module
|
||||
*
|
||||
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
|
||||
*/
|
||||
#ifndef __TESTS_PKTQUEUE_H_
|
||||
#define __TESTS_PKTQUEUE_H_
|
||||
|
||||
#include "../unittests.h"
|
||||
|
||||
/**
|
||||
* @brief The entry point of this test suite.
|
||||
*/
|
||||
void tests_pktqueue(void);
|
||||
|
||||
#endif /* __TESTS_PKTQUEUE_H_ */
|
||||
/** @} */
|
Loading…
Reference in New Issue
Block a user