From d491e827fb80538cea926dee8ff56a236acaa278 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Mon, 12 Oct 2020 15:21:22 +0200 Subject: [PATCH] gnrc_pkt: introduce packet list operations Up until now `utlist.h` is used to manipulate the packet list within a GNRC pkt snip. Since I always wanted to see if there would be an advantage when using `core`'s `list.h`, I provide this `gnrc_pktsnip_t`-specific API to manipulate the packet list. It has the additional advantage of type safety and that future steps could encapsulate those operations. --- sys/include/net/gnrc/pkt.h | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/sys/include/net/gnrc/pkt.h b/sys/include/net/gnrc/pkt.h index efa363003c..ffa57fbf24 100644 --- a/sys/include/net/gnrc/pkt.h +++ b/sys/include/net/gnrc/pkt.h @@ -27,6 +27,7 @@ #include "kernel_types.h" #include "net/gnrc/nettype.h" +#include "utlist.h" #ifdef __cplusplus extern "C" { @@ -122,6 +123,23 @@ typedef struct gnrc_pktsnip { #endif } gnrc_pktsnip_t; +/** + * @brief Returns the snip before a given snip in a packet + * + * @param[in] pkt A packet. + * @param[in] snip The snip for which the predecessor in @p pkt is searched for. + * + * @return The snip before @p snip in @p pkt if @p snip is in @p pkt. + * @return `NULL`, if @p snip is not in @p pkt. + */ +static inline gnrc_pktsnip_t *gnrc_pkt_prev_snip(gnrc_pktsnip_t *pkt, + gnrc_pktsnip_t *snip) +{ + gnrc_pktsnip_t *prev; + LL_SEARCH_SCALAR(pkt, prev, next, snip); + return prev; +} + /** * @brief Calculates length of a packet in byte. * @@ -141,6 +159,52 @@ static inline size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt) return len; } +/** + * @brief Appends a snip to a packet. + * + * @param[in] pkt A packet. + * @param[in] snip A snip. + * + * @return The new head of @p pkt. + */ +static inline gnrc_pktsnip_t *gnrc_pkt_append(gnrc_pktsnip_t *pkt, + gnrc_pktsnip_t *snip) +{ + LL_APPEND(pkt, snip); + return pkt; +} + +/** + * @brief Prepends a snip to a packet. + * + * @param[in] pkt A packet. + * @param[in] snip A snip. + * + * @return The new head of @p pkt. + */ +static inline gnrc_pktsnip_t *gnrc_pkt_prepend(gnrc_pktsnip_t *pkt, + gnrc_pktsnip_t *snip) +{ + LL_PREPEND(pkt, snip); + return pkt; +} + +/** + * @brief Deletes a snip from a packet. + * + * @param[in] pkt A packet. + * @param[in] snip A snip. + * + * @return The new head of @p pkt. + */ +static inline gnrc_pktsnip_t *gnrc_pkt_delete(gnrc_pktsnip_t *pkt, + gnrc_pktsnip_t *snip) +{ + LL_DELETE(pkt, snip); + return pkt; +} + + /** * @brief Calculates length of a packet in byte up to (including) a snip with the given type. *