1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Merge pull request #2400 from haukepetersen/ng_netapi

ng_net: add new version of NETAPI
This commit is contained in:
Martine Lenders 2015-02-10 14:03:27 +01:00
commit 09ebb0e89c
4 changed files with 207 additions and 0 deletions

View File

@ -74,6 +74,9 @@ endif
ifneq (,$(filter trickle,$(USEMODULE)))
DIRS += trickle
endif
ifneq (,$(filter ng_netapi,$(USEMODULE)))
DIRS += net/crosslayer/ng_netapi
endif
DIRS += $(dir $(wildcard $(addsuffix /Makefile, ${USEMODULE})))

124
sys/include/net/ng_netapi.h Normal file
View File

@ -0,0 +1,124 @@
/*
* Copyright (C) 2015 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup net_netapi Generic network module interface
* @ingroup net
* @brief Generic interface for IPC communication between network modules
*
* @details The idea of this interface is that it provides every network
* module with a basic set of commands to communicate with its
* neighboring modules. In this model every module runs in its own
* thread and communication is done using netapi.
*
* @{
*
* @file
* @brief Generic interface to communicate with network modules
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/
#ifndef NG_NETAPI_H_
#define NG_NETAPI_H_
#include "kernel.h"
#include "thread.h"
#include "net/ng_netconf.h"
#include "net/ng_pktsnip.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Message type for passing data up the network stack
*/
#define NG_NETAPI_MSG_TYPE_RCV (0x0201)
/**
* @brief Message type for passing data down the network stack
*/
#define NG_NETAPI_MSG_TYPE_SND (0x0202)
/**
* @brief Message type for setting options of network modules
*/
#define NG_NETAPI_MSG_TYPE_SETOPT (0x0203)
/**
* @brief Message type for getting options from network modules
*/
#define NG_NETAPI_MSG_TYPE_GETOPT (0x0204)
/**
* @brief Message type for replying to get and set option messages
*/
#define NG_NETAPI_MSG_TYPE_ACK (0x0205)
/**
* @brief Data structure to be send for setting and getting options
*/
typedef struct {
ng_netconf_opt_t opt; /**< the option to get/set */
uint16_t context; /**< (optional) context for that option */
void *data; /**< data to set or buffer to read into */
uint16_t data_len; /**< size of the data / the buffer */
} ng_netapi_opt_t;
/**
* @brief Shortcut function for sending *NETAPI_CMD_SND* messages
*
* @param[in] pid PID of the targeted network module
* @param[in] pkt pointer into the packet buffer holding the data to send
*
* @return 1 if packet was successfully delivered
* @return -1 on error (invalid PID or no space in queue)
*/
int ng_netapi_send(kernel_pid_t pid, ng_pktsnip_t *pkt);
/**
* @brief Shortcut function for send *NETAPI_CMD_GETOPT* messages and parsing
* the returned *NETAPI_CMD_ACK* message
*
* @param[in] pid PID of the targeted network module
* @param[in] opt option to get
* @param[in] context (optional) context to the given option
* @param[in] data pointer to buffer for reading the option's value
* @param[in] data_len size of the given buffer
*
* @return value returned by the ACK message
*/
int ng_netapi_get(kernel_pid_t pid, nt_netconf_opt_t opt, uint16_t context,
void *data, size_t data_len);
/**
* @brief Set an option of a protocol layer identified by *pid*.
*
* @note Wraps IPC call of NETAPI_CMD_SET.
*
* @param[in] pid PID of the targeted network module
* @param[in] opt option to set
* @param[in] context (optional) context to the given option
* @param[in] data data to set the given option to
* @param[in] data_len size of *data*
*
* @return value returned by the ACK message
*/
int ng_netapi_set(kernel_pid_t pid, ng_netconf_opt_t opt, uint16_t context,
void *data, size_t data_len);
#ifdef __cplusplus
}
#endif
#endif /* NG_NETAPI_H_ */
/**
* @}^
*/

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2015 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
* @ingroup net_netapi
* @file
* @brief This file contains a number of helper functions that provide
* some shortcuts for some always repeating code snippets when
* servicing the netapi interface
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @}
*/
#include "kernel.h"
#include "msg.h"
#include "net/ng_netapi.h"
/**
* @brief Unified function for getting and setting netapi options
*
* @param[in] pid PID of the targeted thread
* @param[in] type specify if option is to be set or get
* @param[in] opt option to set or get
* @param[in] data data to set or pointer to buffer for reading options
* @param[in] data_len size of the given buffer
*
* @return the value from the received ACK message
*/
static inline int _get_set(kernel_pid_t pid, uint16_t type,
ng_netconf_opt_t opt, uint16_t context,
void *data, size_t data_len)
{
msg_t cmd;
msg_t ack;
ng_netapi_opt_t opt;
/* set ńetapi's option struct */
opt.type = opt;
opt.context = context;
opt.data = data;
opt.data_len = data_len;
/* set outgoing message's fields */
cmd.type = type;
cmd.content.ptr = (void *)&opt;
/* trigger the netapi */
msg_send_receive(&cmd, &ack, pid);
/* return the ACK message's value */
return (int)ack.content.value;
}
int ng_netapi_send(kernel_pid_t pid, ng_pktsnip_t *pkt)
{
msg_t msg;
/* set the outgoing message's fields */
msg.type = NETAPI_MSG_TYPE_SND;
msg.content.ptr = (void *)pkt;
/* send data using netapi */
return msg_send(&msg, pid);
}
int ng_netapi_get(kernel_pid_t pid, ng_netconf_opt_t opt, uint16_t context,
void *data, size_t data_len)
{
return _get_set(pid, NG_NETAPI_MSG_TYPE_GETOPT, opt, context,
data, data_len);
}
int ng_netapi_set(kernel_pid_t pid, ng_netconf_opt_t opt, uint16_t context,
void *data, size_t data_len)
{
return _get_set(pid, NG_NETAPI_MSG_TYPE_SETOPT, opt, context,
data, data_len);
}