netreg.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef NETREG_H
22 #define NETREG_H
23 
24 #include <inttypes.h>
25 
26 #include "kernel_types.h"
27 #include "net/gnrc/nettype.h"
28 #include "net/gnrc/pkt.h"
29 
30 #ifdef MODULE_GNRC_NETAPI_MBOX
31 #include "mbox.h"
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS) || \
39  defined(DOXYGEN)
40 
45 typedef enum {
46  GNRC_NETREG_TYPE_DEFAULT = 0,
47 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
48  GNRC_NETREG_TYPE_MBOX,
49 #endif
50 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
51  GNRC_NETREG_TYPE_CB,
52 #endif
54 #endif
55 
61 #define GNRC_NETREG_DEMUX_CTX_ALL (0xffff0000)
62 
72 #ifdef MODULE_GNRC_NETAPI_MBOX
73 #define GNRC_NETREG_ENTRY_INIT_PID(demux_ctx, pid) { NULL, demux_ctx, \
74  GNRC_NETREG_TYPE_DEFAULT, \
75  { pid } }
76 #else
77 #define GNRC_NETREG_ENTRY_INIT_PID(demux_ctx, pid) { NULL, demux_ctx, { pid } }
78 #endif
79 
80 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
81 
92 #define GNRC_NETREG_ENTRY_INIT_MBOX(demux_ctx, mbox) { NULL, demux_ctx, \
93  GNRC_NETREG_TYPE_MBOX, \
94  { .mbox = mbox } }
95 #endif
96 
97 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
98 
109 #define GNRC_NETREG_ENTRY_INIT_CB(demux_ctx, cbd) { NULL, demux_ctx, \
110  GNRC_NETREG_TYPE_CB, \
111  { .cbd = cbd } }
112 
126 typedef void (*gnrc_netreg_entry_cb_t)(uint16_t cmd, gnrc_pktsnip_t *pkt,
127  void *ctx);
128 
133 typedef struct {
135  void *ctx;
137 #endif
138 
142 typedef struct gnrc_netreg_entry {
149 
157  uint32_t demux_ctx;
158 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS) || \
159  defined(DOXYGEN)
160 
167 #endif
168  union {
170 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
171 
177 #endif
178 
179 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
180 
186 #endif
187  } target;
189 
193 void gnrc_netreg_init(void);
194 
205  uint32_t demux_ctx,
206  kernel_pid_t pid)
207 {
208  entry->next = NULL;
209  entry->demux_ctx = demux_ctx;
210 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS)
211  entry->type = GNRC_NETREG_TYPE_DEFAULT;
212 #endif
213  entry->target.pid = pid;
214 }
215 
216 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
217 
228  uint32_t demux_ctx,
229  mbox_t *mbox)
230 {
231  entry->next = NULL;
232  entry->demux_ctx = demux_ctx;
233  entry->type = GNRC_NETREG_TYPE_MBOX;
234  entry->target.mbox = mbox;
235 }
236 #endif
237 
238 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
239 
250  uint32_t demux_ctx,
252 {
253  entry->next = NULL;
254  entry->demux_ctx = demux_ctx;
255  entry->type = GNRC_NETREG_TYPE_CB;
256  entry->target.cbd = cbd;
257 }
258 #endif
259 
280 
288 
300 gnrc_netreg_entry_t *gnrc_netreg_lookup(gnrc_nettype_t type, uint32_t demux_ctx);
301 
313 int gnrc_netreg_num(gnrc_nettype_t type, uint32_t demux_ctx);
314 
327 
343 
344 #ifdef __cplusplus
345 }
346 #endif
347 
348 #endif /* NETREG_H */
349 
Types used by the kernel.
void(* gnrc_netreg_entry_cb_t)(uint16_t cmd, gnrc_pktsnip_t *pkt, void *ctx)
Packet handler callback for netreg entries with callback.
Definition: netreg.h:126
Entry to the Network protocol registry.
Definition: netreg.h:142
mbox_t * mbox
Target mailbox for the registry entry.
Definition: netreg.h:176
Mailbox struct definition.
Definition: mbox.h:39
Mailbox API.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
void gnrc_netreg_unregister(gnrc_nettype_t type, gnrc_netreg_entry_t *entry)
Removes a thread from the registry.
General definitions for network packets and their helper functions.
gnrc_netreg_entry_t * gnrc_netreg_lookup(gnrc_nettype_t type, uint32_t demux_ctx)
Searches for entries with given parameters in the registry and returns the first found.
uint32_t demux_ctx
The demultiplexing context for the registering thread.
Definition: netreg.h:157
gnrc_netreg_entry_cb_t cb
the callback
Definition: netreg.h:134
static void gnrc_netreg_entry_init_cb(gnrc_netreg_entry_t *entry, uint32_t demux_ctx, gnrc_netreg_entry_cbd_t *cbd)
Initializes a netreg entry dynamically with callback.
Definition: netreg.h:249
gnrc_netreg_type_t type
Type of the registry entry.
Definition: netreg.h:166
Callback + Context descriptor.
Definition: netreg.h:133
struct gnrc_netreg_entry gnrc_netreg_entry_t
Entry to the Network protocol registry.
void * ctx
application context for the callback
Definition: netreg.h:135
struct gnrc_netreg_entry * next
next element in list
Definition: netreg.h:148
static void gnrc_netreg_entry_init_pid(gnrc_netreg_entry_t *entry, uint32_t demux_ctx, kernel_pid_t pid)
Initializes a netreg entry dynamically with PID.
Definition: netreg.h:204
gnrc_netreg_type_t
The type of the netreg entry.
Definition: netreg.h:45
gnrc_netreg_entry_t * gnrc_netreg_getnext(gnrc_netreg_entry_t *entry)
Returns the next entry after entry with the same gnrc_netreg_entry_t::type and gnrc_netreg_entry_t::d...
int gnrc_netreg_register(gnrc_nettype_t type, gnrc_netreg_entry_t *entry)
Registers a thread to the registry.
static void gnrc_netreg_entry_init_mbox(gnrc_netreg_entry_t *entry, uint32_t demux_ctx, mbox_t *mbox)
Initializes a netreg entry dynamically with mbox.
Definition: netreg.h:227
Adds include for missing inttype definitions.
union gnrc_netreg_entry::@159 target
Target for the registry entry.
int gnrc_netreg_calc_csum(gnrc_pktsnip_t *hdr, gnrc_pktsnip_t *pseudo_hdr)
Calculates the checksum for a header.
void gnrc_netreg_init(void)
Initializes module.
Type to represent parts (either headers or payload) of a packet, called snips.
gnrc_nettype_t
Definition of protocol types in the network stack.
Definition: nettype.h:42
kernel_pid_t pid
The PID of the registering thread.
Definition: netreg.h:169
int gnrc_netreg_num(gnrc_nettype_t type, uint32_t demux_ctx)
Returns number of entries with the same gnrc_netreg_entry_t::type and gnrc_netreg_entry_t::demux_ctx...
gnrc_netreg_entry_cbd_t * cbd
Target callback for the registry entry.
Definition: netreg.h:185
Protocol type definitions.