From 59f62e536674e824a16ec5ff0a24a08eb140b33e Mon Sep 17 00:00:00 2001
From: Martine Lenders <mlenders@inf.fu-berlin.de>
Date: Wed, 29 Apr 2015 22:32:06 +0200
Subject: [PATCH] ng_netapi: centralize packet dispatchment for RCV and SND

---
 sys/include/net/ng_netapi.h              | 27 +++++++++++++++++++
 sys/net/crosslayer/ng_netapi/ng_netapi.c | 33 ++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/sys/include/net/ng_netapi.h b/sys/include/net/ng_netapi.h
index 3116a8e702..9bf4b772b5 100644
--- a/sys/include/net/ng_netapi.h
+++ b/sys/include/net/ng_netapi.h
@@ -31,6 +31,7 @@
 #include "kernel.h"
 #include "thread.h"
 #include "net/ng_netconf.h"
+#include "net/ng_nettype.h"
 #include "net/ng_pkt.h"
 
 #ifdef __cplusplus
@@ -83,6 +84,19 @@ typedef struct {
  */
 int ng_netapi_send(kernel_pid_t pid, ng_pktsnip_t *pkt);
 
+/**
+ * @brief   Sends a @ref NG_NETAPI_MSG_TYPE_SND command to all subscribers to
+ *          (@p type, @p demux_ctx).
+ *
+ * @param[in] type      type of the targeted network module.
+ * @param[in] demux_ctx demultiplexing context for @p type.
+ * @param[in] pkt       pointer into the packet buffer holding the data to send
+ *
+ * @return Number of subscribers to (@p type, @p demux_ctx).
+ */
+int ng_netapi_dispatch_send(ng_nettype_t type, uint32_t demux_ctx,
+                            ng_pktsnip_t *pkt);
+
 /**
  * @brief   Shortcut function for sending @ref NG_NETAPI_MSG_TYPE_RCV messages
  *
@@ -94,6 +108,19 @@ int ng_netapi_send(kernel_pid_t pid, ng_pktsnip_t *pkt);
  */
 int ng_netapi_receive(kernel_pid_t pid, ng_pktsnip_t *pkt);
 
+/**
+ * @brief   Sends a @ref NG_NETAPI_MSG_TYPE_RCV command to all subscribers to
+ *          (@p type, @p demux_ctx).
+ *
+ * @param[in] type      type of the targeted network module.
+ * @param[in] demux_ctx demultiplexing context for @p type.
+ * @param[in] pkt       pointer into the packet buffer holding the data to send
+ *
+ * @return Number of subscribers to (@p type, @p demux_ctx).
+ */
+int ng_netapi_dispatch_receive(ng_nettype_t type, uint32_t demux_ctx,
+                               ng_pktsnip_t *pkt);
+
 /**
  * @brief   Shortcut function for sending @ref NG_NETAPI_MSG_TYPE_GET messages and
  *          parsing the returned @ref NG_NETAPI_MSG_TYPE_ACK message
diff --git a/sys/net/crosslayer/ng_netapi/ng_netapi.c b/sys/net/crosslayer/ng_netapi/ng_netapi.c
index 97894dd563..c2a3593104 100644
--- a/sys/net/crosslayer/ng_netapi/ng_netapi.c
+++ b/sys/net/crosslayer/ng_netapi/ng_netapi.c
@@ -20,6 +20,8 @@
 
 #include "kernel.h"
 #include "msg.h"
+#include "net/ng_netreg.h"
+#include "net/ng_pktbuf.h"
 #include "net/ng_netapi.h"
 
 /**
@@ -64,16 +66,47 @@ static inline int _snd_rcv(kernel_pid_t pid, uint16_t type, ng_pktsnip_t *pkt)
     return msg_send(&msg, pid);
 }
 
+static inline int _snd_rcv_dispatch(ng_nettype_t type, uint32_t demux_ctx,
+                                    uint16_t cmd, ng_pktsnip_t *pkt)
+{
+    int numof = ng_netreg_num(type, demux_ctx);
+    ng_netreg_entry_t *sendto;
+
+    if (numof != 0) {
+        sendto = ng_netreg_lookup(type, demux_ctx);
+
+        ng_pktbuf_hold(pkt, numof - 1);
+
+        while (sendto) {
+            _snd_rcv(sendto->pid, cmd, pkt);
+            sendto = ng_netreg_getnext(sendto);
+        }
+    }
+
+    return numof;
+}
 int ng_netapi_send(kernel_pid_t pid, ng_pktsnip_t *pkt)
 {
     return _snd_rcv(pid, NG_NETAPI_MSG_TYPE_SND, pkt);
 }
 
+int ng_netapi_dispatch_send(ng_nettype_t type, uint32_t demux_ctx,
+                            ng_pktsnip_t *pkt)
+{
+    return _snd_rcv_dispatch(type, demux_ctx, NG_NETAPI_MSG_TYPE_SND, pkt);
+}
+
 int ng_netapi_receive(kernel_pid_t pid, ng_pktsnip_t *pkt)
 {
     return _snd_rcv(pid, NG_NETAPI_MSG_TYPE_RCV, pkt);
 }
 
+int ng_netapi_dispatch_receive(ng_nettype_t type, uint32_t demux_ctx,
+                               ng_pktsnip_t *pkt)
+{
+    return _snd_rcv_dispatch(type, demux_ctx, NG_NETAPI_MSG_TYPE_RCV, pkt);
+}
+
 int ng_netapi_get(kernel_pid_t pid, ng_netconf_opt_t opt, uint16_t context,
                   void *data, size_t data_len)
 {
-- 
GitLab