netreg.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include <inttypes.h>
22 
23 #include "sched.h"
24 #include "net/gnrc/nettype.h"
25 #include "net/gnrc/pkt.h"
26 
27 #ifdef MODULE_GNRC_NETAPI_MBOX
28 #include "mbox.h"
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS) || \
36  defined(DOXYGEN)
42 typedef enum {
51 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
59 #endif
60 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
68 #endif
70 #endif
71 
77 #define GNRC_NETREG_DEMUX_CTX_ALL (0xffff0000)
78 
93 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS)
94 #define GNRC_NETREG_ENTRY_INIT_PID(demux_ctx, pid) { NULL, demux_ctx, \
95  GNRC_NETREG_TYPE_DEFAULT, \
96  { pid } }
97 #else
98 #define GNRC_NETREG_ENTRY_INIT_PID(demux_ctx, pid) { NULL, demux_ctx, { pid } }
99 #endif
100 
101 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
113 #define GNRC_NETREG_ENTRY_INIT_MBOX(demux_ctx, _mbox) { NULL, demux_ctx, \
114  GNRC_NETREG_TYPE_MBOX, \
115  { .mbox = _mbox } }
116 #endif
117 
118 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
130 #define GNRC_NETREG_ENTRY_INIT_CB(demux_ctx, _cbd) { NULL, demux_ctx, \
131  GNRC_NETREG_TYPE_CB, \
132  { .cbd = _cbd } }
148 typedef void (*gnrc_netreg_entry_cb_t)(uint16_t cmd, gnrc_pktsnip_t *pkt,
149  void *ctx);
150 
155 typedef struct {
157  void *ctx;
159 #endif
160 
164 typedef struct gnrc_netreg_entry {
171 
179  uint32_t demux_ctx;
180 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS) || \
181  defined(DOXYGEN)
189 #endif
190  union {
192 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
199 #endif
200 
201 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
208 #endif
209  } target;
211 
247 
253 void gnrc_netreg_init(void);
254 
270  uint32_t demux_ctx,
271  kernel_pid_t pid)
272 {
273  entry->next = NULL;
274  entry->demux_ctx = demux_ctx;
275 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS)
277 #endif
278  entry->target.pid = pid;
279 }
280 
281 #if defined(MODULE_GNRC_NETAPI_MBOX) || defined(DOXYGEN)
293  uint32_t demux_ctx,
294  mbox_t *mbox)
295 {
296  entry->next = NULL;
297  entry->demux_ctx = demux_ctx;
298  entry->type = GNRC_NETREG_TYPE_MBOX;
299  entry->target.mbox = mbox;
300 }
301 #endif
302 
303 #if defined(MODULE_GNRC_NETAPI_CALLBACKS) || defined(DOXYGEN)
315  uint32_t demux_ctx,
317 {
318  entry->next = NULL;
319  entry->demux_ctx = demux_ctx;
320  entry->type = GNRC_NETREG_TYPE_CB;
321  entry->target.cbd = cbd;
322 }
323 #endif
351 
359 
376 
395 int gnrc_netreg_num(gnrc_nettype_t type, uint32_t demux_ctx);
396 
414 
430 
431 #ifdef __cplusplus
432 }
433 #endif
434 
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
void gnrc_netreg_acquire_shared(void)
The global locking of netregs.
int gnrc_netreg_calc_csum(gnrc_pktsnip_t *hdr, gnrc_pktsnip_t *pseudo_hdr)
Calculates the checksum for a header.
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:292
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.
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:148
gnrc_netreg_type_t
The type of the netreg entry.
Definition: netreg.h:42
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:269
void gnrc_netreg_release_shared(void)
Release a shared lock on the GNRC netreg.
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...
struct gnrc_netreg_entry gnrc_netreg_entry_t
Entry to the Network protocol registry.
void gnrc_netreg_init(void)
Initializes module.
void gnrc_netreg_unregister(gnrc_nettype_t type, gnrc_netreg_entry_t *entry)
Removes a thread from the registry.
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:314
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.
int gnrc_netreg_register(gnrc_nettype_t type, gnrc_netreg_entry_t *entry)
Registers a thread to the registry.
@ GNRC_NETREG_TYPE_DEFAULT
Use default IPC for netapi operations.
Definition: netreg.h:50
@ GNRC_NETREG_TYPE_MBOX
Use centralized IPC for netapi operations.
Definition: netreg.h:58
@ GNRC_NETREG_TYPE_CB
Use function callbacks for netapi operations.
Definition: netreg.h:67
gnrc_nettype_t
Definition of protocol types in the network stack.
Definition: nettype.h:48
Adds include for missing inttype definitions.
Mailbox API.
General definitions for network packets and their helper functions.
Protocol type definitions.
Scheduler API definition.
Callback + Context descriptor.
Definition: netreg.h:155
gnrc_netreg_entry_cb_t cb
the callback
Definition: netreg.h:156
void * ctx
application context for the callback
Definition: netreg.h:157
Entry to the Network protocol registry.
Definition: netreg.h:164
uint32_t demux_ctx
The demultiplexing context for the registering thread.
Definition: netreg.h:179
struct gnrc_netreg_entry * next
next element in list
Definition: netreg.h:170
union gnrc_netreg_entry::@375 target
Target for the registry entry.
mbox_t * mbox
Target mailbox for the registry entry.
Definition: netreg.h:198
kernel_pid_t pid
The PID of the registering thread.
Definition: netreg.h:191
gnrc_netreg_entry_cbd_t * cbd
Target callback for the registry entry.
Definition: netreg.h:207
gnrc_netreg_type_t type
Type of the registry entry.
Definition: netreg.h:188
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:105
Mailbox struct definition.
Definition: mbox.h:37