Skip to content

Commit

Permalink
Merge branch 'gnrc_netreg/api/mbox-extension' (RIOT-OS#5526) into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Aug 26, 2016
2 parents fbe84b2 + b6c0c80 commit 7c55576
Show file tree
Hide file tree
Showing 26 changed files with 305 additions and 71 deletions.
4 changes: 4 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ ifneq (,$(filter gnrc_conn_udp,$(USEMODULE)))
USEMODULE += gnrc_udp
endif

ifneq (,$(filter gnrc_netreg_extra,$(USEMODULE)))
USEMODULE += core_mbox
endif

ifneq (,$(filter netdev2_tap,$(USEMODULE)))
USEMODULE += netif
USEMODULE += netdev2_eth
Expand Down
1 change: 1 addition & 0 deletions Makefile.pseudomodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PSEUDOMODULES += gnrc_ipv6_router
PSEUDOMODULES += gnrc_ipv6_router_default
PSEUDOMODULES += gnrc_netdev_default
PSEUDOMODULES += gnrc_neterr
PSEUDOMODULES += gnrc_netreg_extra
PSEUDOMODULES += gnrc_pktbuf
PSEUDOMODULES += gnrc_sixlowpan_border_router_default
PSEUDOMODULES += gnrc_sixlowpan_default
Expand Down
6 changes: 2 additions & 4 deletions examples/default/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ int main(void)
#endif

#ifdef MODULE_NETIF
gnrc_netreg_entry_t dump;

dump.pid = gnrc_pktdump_pid;
dump.demux_ctx = GNRC_NETREG_DEMUX_CTX_ALL;
gnrc_netreg_entry_t dump = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
gnrc_pktdump_pid);
gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &dump);
#endif

Expand Down
11 changes: 6 additions & 5 deletions examples/gnrc_networking/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#include "timex.h"
#include "xtimer.h"

static gnrc_netreg_entry_t server = { NULL, GNRC_NETREG_DEMUX_CTX_ALL, KERNEL_PID_UNDEF };
static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
KERNEL_PID_UNDEF);


static void send(char *addr_str, char *port_str, char *data, unsigned int num,
Expand Down Expand Up @@ -93,7 +94,7 @@ static void start_server(char *port_str)
uint16_t port;

/* check if server is already running */
if (server.pid != KERNEL_PID_UNDEF) {
if (server.target.pid != KERNEL_PID_UNDEF) {
printf("Error: server already running on port %" PRIu32 "\n",
server.demux_ctx);
return;
Expand All @@ -105,7 +106,7 @@ static void start_server(char *port_str)
return;
}
/* start server (which means registering pktdump for the chosen port) */
server.pid = gnrc_pktdump_pid;
server.target.pid = gnrc_pktdump_pid;
server.demux_ctx = (uint32_t)port;
gnrc_netreg_register(GNRC_NETTYPE_UDP, &server);
printf("Success: started UDP server on port %" PRIu16 "\n", port);
Expand All @@ -114,13 +115,13 @@ static void start_server(char *port_str)
static void stop_server(void)
{
/* check if server is running at all */
if (server.pid == KERNEL_PID_UNDEF) {
if (server.target.pid == KERNEL_PID_UNDEF) {
printf("Error: server was not running\n");
return;
}
/* stop server */
gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &server);
server.pid = KERNEL_PID_UNDEF;
server.target.pid = KERNEL_PID_UNDEF;
puts("Success: stopped UDP server");
}

Expand Down
7 changes: 3 additions & 4 deletions sys/include/net/gnrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@
* static msg_t _msg_q[Q_SZ];
* (void)arg;
* msg_init_queue(_msg_q, Q_SZ);
* gnrc_netreg_entry me_reg = {
* .demux_ctx = GNRC_NETREG_DEMUX_CTX_ALL,
* .pid = thread_getpid()
* };
* gnrc_netreg_entry me_reg = GNRC_NETREG_ENTRY_INIT_PID(
* GNRC_NETREG_DEMUX_CTX_ALL,
* sched_active_pid);
* gnrc_netreg_register(GNRC_NETTYPE_IPV6, &me_reg);
* while (1) {
* msg_receive(&msg);
Expand Down
3 changes: 1 addition & 2 deletions sys/include/net/gnrc/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ struct conn_udp {
static inline void gnrc_conn_reg(gnrc_netreg_entry_t *entry, gnrc_nettype_t type,
uint32_t demux_ctx)
{
entry->pid = sched_active_pid;
entry->demux_ctx = demux_ctx;
gnrc_netreg_entry_init_pid(entry, demux_ctx, sched_active_pid);
gnrc_netreg_register(type, entry);
}

Expand Down
190 changes: 189 additions & 1 deletion sys/include/net/gnrc/netreg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
* @brief Definitions to register network protocol PIDs to use with
* @ref net_gnrc_netapi.
*
* @defgroup net_gnrc_netreg_extra Extension for alternative handlers
* @ingroup net_gnrc_netreg
* @brief Registry extension for alternative handlers
* @{
* The submodule `gnrc_netreg_extra` provides an extension for alternative
* handlers (e.g. @ref core_mbox "Mailbox IPC").
*
* To use, add the module `gnrc_netreg_extra` to the `USEMODULE` macro in your
* application's Makefile:
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.mk}
* USEMODULE += gnrc_netreg_extra
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* @}
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NETREG_H_
Expand All @@ -27,17 +42,103 @@
#include "net/gnrc/nettype.h"
#include "net/gnrc/pkt.h"

#ifdef MODULE_GNRC_NETREG_EXTRA
#include "mbox.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifdef MODULE_GNRC_NETREG_EXTRA
typedef enum {
GNRC_NETREG_TYPE_DEFAULT = 0,
GNRC_NETREG_TYPE_MBOX,
GNRC_NETREG_TYPE_CB,
} gnrc_netreg_type_t;
#endif

/**
* @brief Demux context value to get all packets of a certain type.
*
* @see gnrc_netreg_entry_t::demux_ctx
*/
#define GNRC_NETREG_DEMUX_CTX_ALL (0xffff0000)

/**
* @brief Initializes a netreg entry statically with PID
*
* @param[in] demux_ctx The @ref gnrc_netreg_entry_t::demux_ctx "demux context"
* for the netreg entry
* @param[in] pid The PID of the registering thread
*
* @return An initialized netreg entry
*/
#ifdef MODULE_GNRC_NETREG_EXTRA
#define GNRC_NETREG_ENTRY_INIT_PID(demux_ctx, pid) { NULL, demux_ctx, \
GNRC_NETREG_TYPE_DEFAULT, \
{ pid } }
#else
#define GNRC_NETREG_ENTRY_INIT_PID(demux_ctx, pid) { NULL, demux_ctx, { pid } }
#endif

#if defined(MODULE_GNRC_NETREG_EXTRA) || defined(DOXYGEN)
/**
* @brief Initializes a netreg entry statically with mbox
*
* @param[in] demux_ctx The @ref gnrc_netreg_entry_t::demux_ctx "demux context"
* for the netreg entry
* @param[in] mbox Target @ref core_mbox "mailbox" for the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*
* @return An initialized netreg entry
*/
#define GNRC_NETREG_ENTRY_INIT_MBOX(demux_ctx, mbox) { NULL, demux_ctx, \
GNRC_NETREG_TYPE_MBOX, \
{ .mbox = mbox } }

/**
* @brief Initializes a netreg entry statically with callback
*
* @param[in] demux_ctx The @ref gnrc_netreg_entry_t::demux_ctx "demux context"
* for the netreg entry
* @param[in] cb Target callback for the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*
* @return An initialized netreg entry
*/
#define GNRC_NETREG_ENTRY_INIT_CB(demux_ctx, cbd) { NULL, demux_ctx, \
GNRC_NETREG_TYPE_CB, \
{ .cbd = cbd } }

/**
* @brief Packet handler callback for netreg entries with callback.
*
* @pre `cmd` $\in$ { @ref GNRC_NETAPI_MSG_TYPE_RCV, @ref GNRC_NETAPI_MSG_TYPE_SND }
*
* @note Only available with @ref net_gnrc_netreg_extra.
*
* @param[in] cmd @ref net_gnrc_netapi command type. Must be either
* @ref GNRC_NETAPI_MSG_TYPE_SND or
* @ref GNRC_NETAPI_MSG_TYPE_RCV
* @param[in] pkt The packet to handle.
* @param[in] ctx Application context.
*/
typedef void (*gnrc_netreg_entry_cb_t)(uint16_t cmd, gnrc_pktsnip_t *pkt,
void *ctx);

/**
* @brief Callback + Context descriptor
* @note Only available with @ref net_gnrc_netreg_extra.
*/
typedef struct {
gnrc_netreg_entry_cb_t cb; /**< the callback */
void *ctx; /**< application context for the callback */
} gnrc_netreg_entry_cbd_t;
#endif

/**
* @brief Entry to the @ref net_gnrc_netreg
*/
Expand All @@ -57,14 +158,101 @@ typedef struct gnrc_netreg_entry {
* ports in UDP/TCP, or similar.
*/
uint32_t demux_ctx;
kernel_pid_t pid; /**< The PID of the registering thread */
#if defined(MODULE_GNRC_NETREG_EXTRA) || defined(DOXYGEN)
/**
* @brief Type of the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*/
gnrc_netreg_type_t type;
#endif
union {
kernel_pid_t pid; /**< The PID of the registering thread */
#if defined(MODULE_GNRC_NETREG_EXTRA) || defined(DOXYGEN)
/**
* @brief Target @ref core_mbox "mailbox" for the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*/
mbox_t *mbox;

/**
* @brief Target callback for the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*/
gnrc_netreg_entry_cbd_t *cbd;
#endif
} target; /**< Target for the registry entry */
} gnrc_netreg_entry_t;

/**
* @brief Initializes module.
*/
void gnrc_netreg_init(void);

/**
* @brief Initializes a netreg entry dynamically with PID
*
* @param[out] entry A netreg entry
* @param[in] demux_ctx The @ref gnrc_netreg_entry_t::demux_ctx "demux context"
* for the netreg entry
* @param[in] pid The PID of the registering thread
*
*/
static inline void gnrc_netreg_entry_init_pid(gnrc_netreg_entry_t *entry,
uint32_t demux_ctx,
kernel_pid_t pid)
{
entry->next = NULL;
entry->demux_ctx = demux_ctx;
#ifdef MODULE_GNRC_NETREG_EXTRA
entry->type = GNRC_NETREG_TYPE_DEFAULT;
#endif
entry->target.pid = pid;
}

#if defined(MODULE_GNRC_NETREG_EXTRA) || defined(DOXYGEN)
/**
* @brief Initializes a netreg entry dynamically with mbox
*
* @param[out] entry A netreg entry
* @param[in] demux_ctx The @ref gnrc_netreg_entry_t::demux_ctx "demux context"
* for the netreg entry
* @param[in] mbox Target @ref core_mbox "mailbox" for the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*/
static inline void gnrc_netreg_entry_init_mbox(gnrc_netreg_entry_t *entry,
uint32_t demux_ctx,
mbox_t *mbox)
{
entry->next = NULL;
entry->demux_ctx = demux_ctx;
entry->type = GNRC_NETREG_TYPE_MBOX;
entry->target.mbox = mbox;
}
/**
* @brief Initializes a netreg entry dynamically with callback
*
* @param[out] entry A netreg entry
* @param[in] demux_ctx The @ref gnrc_netreg_entry_t::demux_ctx "demux context"
* for the netreg entry
* @param[in] mbox Target callback for the registry entry
*
* @note Only available with @ref net_gnrc_netreg_extra.
*/
static inline void gnrc_netreg_entry_init_cb(gnrc_netreg_entry_t *entry,
uint32_t demux_ctx,
gnrc_netreg_entry_cbd_t *cbd)
{
entry->next = NULL;
entry->demux_ctx = demux_ctx;
entry->type = GNRC_NETREG_TYPE_CB;
entry->target.cbd = cbd;
}
#endif

/**
* @brief Registers a thread to the registry.
*
Expand Down
12 changes: 6 additions & 6 deletions sys/net/gnrc/application_layer/tftp/gnrc_tftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ int _tftp_server(tftp_context_t *ctxt)
{
msg_t msg;
bool active = true;
gnrc_netreg_entry_t entry = { NULL, GNRC_TFTP_DEFAULT_DST_PORT,
thread_getpid() };
gnrc_netreg_entry_t entry = GNRC_NETREG_ENTRY_INIT_PID(GNRC_TFTP_DEFAULT_DST_PORT,
sched_active_pid);

while (active) {
int ret = TS_BUSY;
Expand Down Expand Up @@ -520,7 +520,8 @@ int _tftp_do_client_transfer(tftp_context_t *ctxt)
tftp_state ret = TS_BUSY;

/* register our DNS response listener */
gnrc_netreg_entry_t entry = { NULL, ctxt->src_port, thread_getpid() };
gnrc_netreg_entry_t entry = GNRC_NETREG_ENTRY_INIT_PID(ctxt->src_port,
sched_active_pid);

if (gnrc_netreg_register(GNRC_NETTYPE_UDP, &entry)) {
DEBUG("tftp: error starting server.");
Expand Down Expand Up @@ -636,9 +637,8 @@ tftp_state _tftp_state_processes(tftp_context_t *ctxt, msg_t *m)
}

/* register a listener for the UDP port */
ctxt->entry.next = NULL;
ctxt->entry.demux_ctx = ctxt->src_port;
ctxt->entry.pid = thread_getpid();
gnrc_netreg_entry_init_pid(&(ctxt->entry), ctxt->src_port,
sched_active_pid);
gnrc_netreg_register(GNRC_NETTYPE_UDP, &(ctxt->entry));

/* try to decode the options */
Expand Down
7 changes: 2 additions & 5 deletions sys/net/gnrc/application_layer/zep/gnrc_zep.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,11 @@ void *_event_loop(void *args)
msg_t msg, ack, msg_q[GNRC_ZEP_MSG_QUEUE_SIZE];
gnrc_netdev_t *dev = (gnrc_netdev_t *)args;
gnrc_netapi_opt_t *opt;
gnrc_netreg_entry_t my_reg = { NULL, ((gnrc_zep_t *)args)->src_port,
KERNEL_PID_UNDEF
};
gnrc_netreg_entry_t my_reg = GNRC_NETREG_ENTRY_INIT_PID(((gnrc_zep_t *)args)->src_port,
sched_active_pid);

msg_init_queue(msg_q, GNRC_ZEP_MSG_QUEUE_SIZE);

my_reg.pid = thread_getpid();

gnrc_netreg_register(GNRC_NETTYPE_UDP, &my_reg);

while (1) {
Expand Down
2 changes: 1 addition & 1 deletion sys/net/gnrc/conn/ip/gnrc_conn_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int conn_ip_create(conn_ip_t *conn, const void *addr, size_t addr_len, int famil
void conn_ip_close(conn_ip_t *conn)
{
assert(conn->l4_type == GNRC_NETTYPE_UNDEF);
if (conn->netreg_entry.pid != KERNEL_PID_UNDEF) {
if (conn->netreg_entry.target.pid != KERNEL_PID_UNDEF) {
gnrc_netreg_unregister(conn->l3_type, &conn->netreg_entry);
}
}
Expand Down
4 changes: 2 additions & 2 deletions sys/net/gnrc/conn/udp/gnrc_conn_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ int conn_udp_create(conn_udp_t *conn, const void *addr, size_t addr_len,
void conn_udp_close(conn_udp_t *conn)
{
assert(conn->l4_type == GNRC_NETTYPE_UDP);
if (conn->netreg_entry.pid != KERNEL_PID_UNDEF) {
if (conn->netreg_entry.target.pid != KERNEL_PID_UNDEF) {
gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &conn->netreg_entry);
conn->netreg_entry.pid = KERNEL_PID_UNDEF;
conn->netreg_entry.target.pid = KERNEL_PID_UNDEF;
}
}

Expand Down
Loading

0 comments on commit 7c55576

Please sign in to comment.