2015-02-06 20:03:27 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
2015-08-17 15:41:29 +02:00
|
|
|
* @ingroup net_gnrc_netapi
|
2015-02-06 20:03:27 +01:00
|
|
|
* @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"
|
2015-08-17 15:41:29 +02:00
|
|
|
#include "net/gnrc/netreg.h"
|
|
|
|
#include "net/gnrc/pktbuf.h"
|
|
|
|
#include "net/gnrc/netapi.h"
|
2015-02-06 20:03:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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,
|
2015-08-06 15:37:11 +02:00
|
|
|
netopt_t opt, uint16_t context,
|
2015-02-06 20:03:27 +01:00
|
|
|
void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
msg_t cmd;
|
|
|
|
msg_t ack;
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netapi_opt_t o;
|
2015-02-06 20:03:27 +01:00
|
|
|
/* set ńetapi's option struct */
|
2015-03-02 18:40:09 +01:00
|
|
|
o.opt = opt;
|
|
|
|
o.context = context;
|
|
|
|
o.data = data;
|
|
|
|
o.data_len = data_len;
|
2015-02-06 20:03:27 +01:00
|
|
|
/* set outgoing message's fields */
|
|
|
|
cmd.type = type;
|
2015-03-02 18:40:09 +01:00
|
|
|
cmd.content.ptr = (void *)&o;
|
2015-02-06 20:03:27 +01:00
|
|
|
/* trigger the netapi */
|
|
|
|
msg_send_receive(&cmd, &ack, pid);
|
|
|
|
/* return the ACK message's value */
|
|
|
|
return (int)ack.content.value;
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
static inline int _snd_rcv(kernel_pid_t pid, uint16_t type, gnrc_pktsnip_t *pkt)
|
2015-02-06 20:03:27 +01:00
|
|
|
{
|
|
|
|
msg_t msg;
|
|
|
|
/* set the outgoing message's fields */
|
2015-03-20 19:21:44 +01:00
|
|
|
msg.type = type;
|
2015-02-06 20:03:27 +01:00
|
|
|
msg.content.ptr = (void *)pkt;
|
2015-03-20 19:21:44 +01:00
|
|
|
/* send message */
|
2015-02-06 20:03:27 +01:00
|
|
|
return msg_send(&msg, pid);
|
|
|
|
}
|
|
|
|
|
2015-08-28 13:25:20 +02:00
|
|
|
int gnrc_netapi_dispatch(gnrc_nettype_t type, uint32_t demux_ctx,
|
|
|
|
uint16_t cmd, gnrc_pktsnip_t *pkt)
|
2015-04-29 22:32:06 +02:00
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
int numof = gnrc_netreg_num(type, demux_ctx);
|
2015-04-29 22:32:06 +02:00
|
|
|
|
|
|
|
if (numof != 0) {
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netreg_entry_t *sendto = gnrc_netreg_lookup(type, demux_ctx);
|
2015-04-29 22:32:06 +02:00
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_pktbuf_hold(pkt, numof - 1);
|
2015-04-29 22:32:06 +02:00
|
|
|
|
|
|
|
while (sendto) {
|
2015-09-29 16:58:08 +02:00
|
|
|
if (_snd_rcv(sendto->pid, cmd, pkt) < 1) {
|
|
|
|
/* unable to dispatch packet */
|
|
|
|
gnrc_pktbuf_release(pkt);
|
|
|
|
}
|
2015-08-17 15:41:29 +02:00
|
|
|
sendto = gnrc_netreg_getnext(sendto);
|
2015-04-29 22:32:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numof;
|
|
|
|
}
|
2015-08-28 13:25:20 +02:00
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
int gnrc_netapi_send(kernel_pid_t pid, gnrc_pktsnip_t *pkt)
|
2015-03-20 19:21:44 +01:00
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
return _snd_rcv(pid, GNRC_NETAPI_MSG_TYPE_SND, pkt);
|
2015-03-20 19:21:44 +01:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
int gnrc_netapi_receive(kernel_pid_t pid, gnrc_pktsnip_t *pkt)
|
2015-03-20 19:21:44 +01:00
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
return _snd_rcv(pid, GNRC_NETAPI_MSG_TYPE_RCV, pkt);
|
2015-03-20 19:21:44 +01:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
int gnrc_netapi_get(kernel_pid_t pid, netopt_t opt, uint16_t context,
|
|
|
|
void *data, size_t data_len)
|
2015-02-06 20:03:27 +01:00
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
return _get_set(pid, GNRC_NETAPI_MSG_TYPE_GET, opt, context,
|
2015-02-06 20:03:27 +01:00
|
|
|
data, data_len);
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
int gnrc_netapi_set(kernel_pid_t pid, netopt_t opt, uint16_t context,
|
|
|
|
void *data, size_t data_len)
|
2015-02-06 20:03:27 +01:00
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
return _get_set(pid, GNRC_NETAPI_MSG_TYPE_SET, opt, context,
|
2015-02-06 20:03:27 +01:00
|
|
|
data, data_len);
|
|
|
|
}
|