1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/net/gnrc/network_layer/sixlowpan/gnrc_sixlowpan.c

330 lines
9.3 KiB
C
Raw Normal View History

2015-03-16 17:52:19 +01:00
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* 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.
*/
/**
* @{
*
* @file
*/
#include "kernel_types.h"
2015-08-10 02:41:08 +02:00
#include "net/gnrc.h"
2015-03-16 17:52:19 +01:00
#include "thread.h"
#include "utlist.h"
#include "net/gnrc/ipv6/hdr.h"
#include "net/gnrc/sixlowpan.h"
#include "net/gnrc/sixlowpan/frag.h"
#include "net/gnrc/sixlowpan/iphc.h"
#include "net/gnrc/sixlowpan/netif.h"
#include "net/sixlowpan.h"
2015-03-16 17:52:19 +01:00
#define ENABLE_DEBUG (0)
#include "debug.h"
#if ENABLE_DEBUG
/* For PRIu16 etc. */
#include <inttypes.h>
#endif
2015-03-16 17:52:19 +01:00
static kernel_pid_t _pid = KERNEL_PID_UNDEF;
#if ENABLE_DEBUG
static char _stack[GNRC_SIXLOWPAN_STACK_SIZE + THREAD_EXTRA_STACKSIZE_PRINTF];
#else
static char _stack[GNRC_SIXLOWPAN_STACK_SIZE];
#endif
2015-03-16 17:52:19 +01:00
/* handles GNRC_NETAPI_MSG_TYPE_RCV commands */
static void _receive(gnrc_pktsnip_t *pkt);
/* handles GNRC_NETAPI_MSG_TYPE_SND commands */
static void _send(gnrc_pktsnip_t *pkt);
2015-03-16 17:52:19 +01:00
/* Main event loop for 6LoWPAN */
static void *_event_loop(void *args);
kernel_pid_t gnrc_sixlowpan_init(void)
2015-03-16 17:52:19 +01:00
{
if (_pid > KERNEL_PID_UNDEF) {
return _pid;
}
_pid = thread_create(_stack, sizeof(_stack), GNRC_SIXLOWPAN_PRIO,
2015-03-16 17:52:19 +01:00
CREATE_STACKTEST, _event_loop, NULL, "6lo");
return _pid;
}
static void _receive(gnrc_pktsnip_t *pkt)
2015-03-16 17:52:19 +01:00
{
gnrc_pktsnip_t *payload;
2015-03-16 17:52:19 +01:00
uint8_t *dispatch;
2015-06-19 14:52:51 +02:00
/* seize payload as a temporary variable */
payload = gnrc_pktbuf_start_write(pkt); /* need to duplicate since pkt->next
2015-06-19 14:52:51 +02:00
* might get replaced */
if (payload == NULL) {
DEBUG("6lo: can not get write access on received packet\n");
#if defined(DEVELHELP) && ENABLE_DEBUG
gnrc_pktbuf_stats();
2015-06-19 14:52:51 +02:00
#endif
gnrc_pktbuf_release(pkt);
2015-06-19 14:52:51 +02:00
return;
}
pkt = payload; /* reset pkt from temporary variable */
LL_SEARCH_SCALAR(pkt, payload, type, GNRC_NETTYPE_SIXLOWPAN);
2015-03-16 17:52:19 +01:00
if ((payload == NULL) || (payload->size < 1)) {
DEBUG("6lo: Received packet has no 6LoWPAN payload\n");
gnrc_pktbuf_release(pkt);
2015-06-19 17:13:10 +02:00
return;
2015-03-16 17:52:19 +01:00
}
dispatch = payload->data;
if (dispatch[0] == SIXLOWPAN_UNCOMP) {
gnrc_pktsnip_t *sixlowpan;
2015-03-16 17:52:19 +01:00
DEBUG("6lo: received uncompressed IPv6 packet\n");
payload = gnrc_pktbuf_start_write(payload);
2015-03-16 17:52:19 +01:00
if (payload == NULL) {
DEBUG("6lo: can not get write access on received packet\n");
#if defined(DEVELHELP) && ENABLE_DEBUG
gnrc_pktbuf_stats();
2015-03-16 17:52:19 +01:00
#endif
gnrc_pktbuf_release(pkt);
2015-03-16 17:52:19 +01:00
return;
}
/* packet is uncompressed: just mark and remove the dispatch */
sixlowpan = gnrc_pktbuf_mark(payload, sizeof(uint8_t), GNRC_NETTYPE_SIXLOWPAN);
if (sixlowpan == NULL) {
DEBUG("6lo: can not mark 6LoWPAN dispatch\n");
gnrc_pktbuf_release(pkt);
return;
}
pkt = gnrc_pktbuf_remove_snip(pkt, sixlowpan);
2015-03-16 17:52:19 +01:00
}
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG
else if (sixlowpan_frag_is((sixlowpan_frag_t *)dispatch)) {
DEBUG("6lo: received 6LoWPAN fragment\n");
gnrc_sixlowpan_frag_handle_pkt(pkt);
2015-04-28 13:44:20 +02:00
return;
}
#endif
#ifdef MODULE_GNRC_SIXLOWPAN_IPHC
else if (sixlowpan_iphc_is(dispatch)) {
2015-08-11 23:24:01 +02:00
size_t dispatch_size;
gnrc_pktsnip_t *sixlowpan;
gnrc_pktsnip_t *ipv6 = gnrc_pktbuf_add(NULL, NULL, sizeof(ipv6_hdr_t),
GNRC_NETTYPE_IPV6);
2015-08-11 23:24:01 +02:00
if ((ipv6 == NULL) ||
(dispatch_size = gnrc_sixlowpan_iphc_decode(ipv6, pkt, 0)) == 0) {
DEBUG("6lo: error on IPHC decoding\n");
2015-08-11 23:24:01 +02:00
if (ipv6 != NULL) {
gnrc_pktbuf_release(ipv6);
2015-08-11 23:24:01 +02:00
}
gnrc_pktbuf_release(pkt);
2015-08-11 23:24:01 +02:00
return;
}
sixlowpan = gnrc_pktbuf_mark(pkt, dispatch_size, GNRC_NETTYPE_SIXLOWPAN);
2015-08-11 23:24:01 +02:00
if (sixlowpan == NULL) {
DEBUG("6lo: error on marking IPHC dispatch\n");
gnrc_pktbuf_release(ipv6);
gnrc_pktbuf_release(pkt);
return;
}
2015-08-11 23:24:01 +02:00
/* Remove IPHC dispatch */
gnrc_pktbuf_remove_snip(pkt, sixlowpan);
2015-08-11 23:24:01 +02:00
/* Insert IPv6 header instead */
ipv6->next = pkt->next;
pkt->next = ipv6;
}
#endif
2015-07-15 12:46:46 +02:00
else {
DEBUG("6lo: dispatch %02" PRIx8 " ... is not supported\n",
dispatch[0]);
gnrc_pktbuf_release(pkt);
2015-07-15 12:46:46 +02:00
return;
}
payload->type = GNRC_NETTYPE_IPV6;
2015-03-16 17:52:19 +01:00
if (!gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, GNRC_NETREG_DEMUX_CTX_ALL, pkt)) {
2015-07-23 16:56:14 +02:00
DEBUG("6lo: No receivers for this packet found\n");
gnrc_pktbuf_release(pkt);
2015-03-16 17:52:19 +01:00
}
}
static inline bool _add_uncompr_disp(gnrc_pktsnip_t *pkt)
2015-07-22 20:56:17 +02:00
{
gnrc_pktsnip_t *sixlowpan;
2015-07-22 20:56:17 +02:00
uint8_t *disp;
DEBUG("6lo: Send uncompressed\n");
sixlowpan = gnrc_pktbuf_add(NULL, NULL, sizeof(uint8_t), GNRC_NETTYPE_SIXLOWPAN);
2015-07-22 20:56:17 +02:00
if (sixlowpan == NULL) {
return false;
}
sixlowpan->next = pkt->next;
pkt->next = sixlowpan;
disp = sixlowpan->data;
disp[0] = SIXLOWPAN_UNCOMP;
2015-07-22 20:56:17 +02:00
return true;
}
static void _send(gnrc_pktsnip_t *pkt)
2015-03-16 17:52:19 +01:00
{
gnrc_netif_hdr_t *hdr;
gnrc_pktsnip_t *pkt2;
gnrc_sixlowpan_netif_t *iface;
2015-08-11 23:24:01 +02:00
/* datagram_size: pure IPv6 packet without 6LoWPAN dispatches or compression */
size_t datagram_size;
2015-03-16 17:52:19 +01:00
if ((pkt == NULL) || (pkt->size < sizeof(gnrc_netif_hdr_t))) {
2015-03-16 17:52:19 +01:00
DEBUG("6lo: Sending packet has no netif header\n");
gnrc_pktbuf_release(pkt);
2015-03-16 17:52:19 +01:00
return;
}
if ((pkt->next == NULL) || (pkt->next->type != GNRC_NETTYPE_IPV6)) {
2015-03-16 17:52:19 +01:00
DEBUG("6lo: Sending packet has no IPv6 header\n");
gnrc_pktbuf_release(pkt);
2015-03-16 17:52:19 +01:00
return;
}
pkt2 = gnrc_pktbuf_start_write(pkt);
2015-03-16 17:52:19 +01:00
2015-07-22 20:56:17 +02:00
if (pkt2 == NULL) {
2015-03-16 17:52:19 +01:00
DEBUG("6lo: no space left in packet buffer\n");
gnrc_pktbuf_release(pkt);
2015-03-16 17:52:19 +01:00
return;
}
2015-07-22 20:56:17 +02:00
hdr = pkt2->data;
iface = gnrc_sixlowpan_netif_get(hdr->if_pid);
datagram_size = gnrc_pkt_len(pkt2->next);
2015-03-16 17:52:19 +01:00
if (iface == NULL) {
DEBUG("6lo: Can not get 6LoWPAN specific interface information.\n");
gnrc_pktbuf_release(pkt);
return;
2015-03-16 17:52:19 +01:00
}
#ifdef MODULE_GNRC_SIXLOWPAN_IPHC
if (iface->iphc_enabled) {
if (!gnrc_sixlowpan_iphc_encode(pkt2)) {
DEBUG("6lo: error on IPHC encoding\n");
gnrc_pktbuf_release(pkt2);
return;
}
2015-07-22 20:56:17 +02:00
/* IPHC dispatch does not count on dispatch length since it _shortens_
* the datagram */
}
else {
2015-07-22 20:56:17 +02:00
if (!_add_uncompr_disp(pkt2)) {
/* adding uncompressed dispatch failed */
DEBUG("6lo: no space left in packet buffer\n");
gnrc_pktbuf_release(pkt2);
return;
}
}
#else
/* suppress clang-analyzer report about iface being not read */
(void) iface;
2015-07-22 20:56:17 +02:00
if (!_add_uncompr_disp(pkt2)) {
/* adding uncompressed dispatch failed */
DEBUG("6lo: no space left in packet buffer\n");
gnrc_pktbuf_release(pkt2);
return;
}
#endif
2015-07-22 20:56:17 +02:00
DEBUG("6lo: iface->max_frag_size = %" PRIu16 " for interface %"
PRIkernel_pid "\n", iface->max_frag_size, hdr->if_pid);
2015-03-16 17:52:19 +01:00
/* IP should not send anything here if it is not a 6LoWPAN interface,
* so we don't need to check for NULL pointers */
if (gnrc_pkt_len(pkt2->next) <= iface->max_frag_size) {
2015-03-16 17:52:19 +01:00
DEBUG("6lo: Send SND command for %p to %" PRIu16 "\n",
2015-07-22 20:56:17 +02:00
(void *)pkt2, hdr->if_pid);
gnrc_netapi_send(hdr->if_pid, pkt2);
2015-03-16 17:52:19 +01:00
return;
}
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG
else {
DEBUG("6lo: Send fragmented (%u > %" PRIu16 ")\n",
(unsigned int)datagram_size, iface->max_frag_size);
gnrc_sixlowpan_frag_send(hdr->if_pid, pkt2, datagram_size);
}
#else
(void)datagram_size;
2015-03-16 17:52:19 +01:00
DEBUG("6lo: packet too big (%u> %" PRIu16 ")\n",
(unsigned int)datagram_size, iface->max_frag_size);
#endif
2015-03-16 17:52:19 +01:00
}
static void *_event_loop(void *args)
{
msg_t msg, reply, msg_q[GNRC_SIXLOWPAN_MSG_QUEUE_SIZE];
gnrc_netreg_entry_t me_reg;
2015-03-16 17:52:19 +01:00
(void)args;
msg_init_queue(msg_q, GNRC_SIXLOWPAN_MSG_QUEUE_SIZE);
2015-03-16 17:52:19 +01:00
me_reg.demux_ctx = GNRC_NETREG_DEMUX_CTX_ALL;
2015-03-16 17:52:19 +01:00
me_reg.pid = thread_getpid();
/* register interest in all 6LoWPAN packets */
gnrc_netreg_register(GNRC_NETTYPE_SIXLOWPAN, &me_reg);
2015-03-16 17:52:19 +01:00
/* preinitialize ACK */
reply.type = GNRC_NETAPI_MSG_TYPE_ACK;
2015-03-16 17:52:19 +01:00
/* start event loop */
while (1) {
DEBUG("6lo: waiting for incoming message.\n");
msg_receive(&msg);
switch (msg.type) {
case GNRC_NETAPI_MSG_TYPE_RCV:
DEBUG("6lo: GNRC_NETDEV_MSG_TYPE_RCV received\n");
_receive((gnrc_pktsnip_t *)msg.content.ptr);
2015-03-16 17:52:19 +01:00
break;
case GNRC_NETAPI_MSG_TYPE_SND:
DEBUG("6lo: GNRC_NETDEV_MSG_TYPE_SND received\n");
_send((gnrc_pktsnip_t *)msg.content.ptr);
2015-03-16 17:52:19 +01:00
break;
case GNRC_NETAPI_MSG_TYPE_GET:
case GNRC_NETAPI_MSG_TYPE_SET:
2015-03-16 17:52:19 +01:00
DEBUG("6lo: reply to unsupported get/set\n");
reply.content.value = -ENOTSUP;
msg_reply(&msg, &reply);
break;
default:
DEBUG("6lo: operation not supported\n");
break;
}
}
return NULL;
}
/** @} */