From f32fe172d42f954cac67b389873bf3e3a22908b2 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 28 Mar 2017 10:47:25 -0600 Subject: [PATCH] 6loWPAN: Add HC06 initialization --- include/nuttx/net/sixlowpan.h | 16 +-- net/sixlowpan/Kconfig | 70 +++++++++++ net/sixlowpan/Make.defs | 4 + net/sixlowpan/sixlowpan.h | 23 ++++ net/sixlowpan/sixlowpan_hc06.c | 169 +++++++++++++++++++++++++++ net/sixlowpan/sixlowpan_initialize.c | 8 +- 6 files changed, 274 insertions(+), 16 deletions(-) create mode 100644 net/sixlowpan/sixlowpan_hc06.c diff --git a/include/nuttx/net/sixlowpan.h b/include/nuttx/net/sixlowpan.h index 5c94bb02b9..8901b453d1 100644 --- a/include/nuttx/net/sixlowpan.h +++ b/include/nuttx/net/sixlowpan.h @@ -247,7 +247,7 @@ struct sixlowpan_frag_hdr * sixlowpan_hc1_hc_udp structure */ -struct sixlowpan_hc1_hdr +struct sixlowpan_hc1hdr_s { uint8_t dispatch; uint8_t encoding; @@ -256,7 +256,7 @@ struct sixlowpan_hc1_hdr /* HC1 followed by HC_UDP */ -struct sixlowpan_hc1_hc_udp_hdr +struct sixlowpan_hc1_hcudp_hdr_s { uint8_t dispatch; uint8_t hc1_encoding; @@ -266,18 +266,6 @@ struct sixlowpan_hc1_hc_udp_hdr uint16_t udpchksum; }; -/* An address context for IPHC address compression each context can have up - * to 8 bytes - */ - -struct sixlowpan_addr_context -{ - uint8_t used; /* Possibly use as prefix-length */ - uint8_t number; - uint8_t prefix[8]; -}; - - /* The structure of a next header compressor. This compressor is provided * by architecture-specific logic outside of the network stack. * diff --git a/net/sixlowpan/Kconfig b/net/sixlowpan/Kconfig index cf29ed9abf..b89d1345d9 100644 --- a/net/sixlowpan/Kconfig +++ b/net/sixlowpan/Kconfig @@ -43,12 +43,82 @@ config NET_6LOWPAN_COMPRESSION_HC06 endchoice # 6loWPAN Compression +if NET_6LOWPAN_COMPRESSION_HC06 + config NET_6LOWPAN_MAXADDRCONTEXT int "Maximum address contexts" default 1 ---help--- If we use IPHC compression, how many address contexts do we support? +config NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_0_0 + hex "Address context 0 Prefix 0" + default 0xaa + ---help--- + Prefix 0 for address context ze0ro (assumes CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 0) + +config NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_0_1 + hex "Address context 0 Prefix 1" + default 0xaa + ---help--- + Prefix 1 for address context 0 (assumes CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 0) + +config NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_1 + bool "Pre-initialize address context 1" + default n + ---help--- + Preinitialize address context 1 for better header compression + (Saves up to 13 bytes per 6lowpan packet). Assumes + CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 1) + +if NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_1 + +config NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_1_0 + hex "Address context 1 Prefix 0" + default 0xaa + ---help--- + Prefix 0 for address context 1 (assumes CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 1) + +config NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_1_1 + hex "Address context 1 Prefix 1" + default 0xaa + ---help--- + Prefix 1 for address context 1 (assumes CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 1) + +endif # NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_1 + +config NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_2 + bool "Pre-initialize address context 2" + default n + depends on NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_1 + ---help--- + Preinitialize any address contexts for better header compression + (Saves up to 13 bytes per 6lowpan packet). Assumes + CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 2) + +if NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_2 + +config NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_2_0 + hex "Address context 2 Prefix 0" + default 0xaa + ---help--- + Prefix 0 for address context 2 (assumes CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 2) + +config NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_2_1 + hex "Address context 2 Prefix 1" + default 0xaa + ---help--- + Prefix 1 for address context 2 (assumes CONFIG_NET_6LOWPAN_MAXADDRCONTEXT >= 2) + +endif # NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_0 +endif # NET_6LOWPAN_COMPRESSION_HC06 + +config NET_SIXLOWPAN_MAXAGE + int "Packet reassembly timeout" + default 20 + ---help--- + Timeout for packet reassembly at the 6lowpan layer (should be < 60s) + config NET_6LOWPAN_MTU int "6LoWPAN packet buffer size (MTU)" default 1294 diff --git a/net/sixlowpan/Make.defs b/net/sixlowpan/Make.defs index ffe1351f94..0ddebc0b20 100644 --- a/net/sixlowpan/Make.defs +++ b/net/sixlowpan/Make.defs @@ -42,6 +42,10 @@ ifeq ($(CONFIG_NET_6LOWPAN),y) NET_CSRCS += sixlowpan_initialize.c sixlowpan_globals.c NET_CSRCS += sixlowpan_compressor.c sixlowpan_sniffer.c +ifeq ($(CONFIG_NET_6LOWPAN_COMPRESSION_HC06),y) +NET_CSRCS += sixlowpan_hc06.c +endif + # Include the sixlowpan directory in the build DEPPATH += --dep-path sixlowpan diff --git a/net/sixlowpan/sixlowpan.h b/net/sixlowpan/sixlowpan.h index 37d8bb597c..31a858205d 100644 --- a/net/sixlowpan/sixlowpan.h +++ b/net/sixlowpan/sixlowpan.h @@ -88,5 +88,28 @@ extern FAR struct sixlowpan_rime_sniffer_s *g_sixlowpan_sniffer; void sixlowpan_initialize(void); +/**************************************************************************** + * Name: sixlowpan_hc06_initialize + * + * Description: + * sixlowpan_hc06_initialize() is called during OS initialization at power-up + * reset. It is called from the common sixlowpan_initialize() function. + * sixlowpan_hc06_initialize() configures HC06 networking data structures. + * It is called prior to platform-specific driver initialization so that + * the 6loWPAN networking subsystem is prepared to deal with network + * driver initialization actions. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +#ifdef CONFIG_NET_6LOWPAN_COMPRESSION_HC06 +void sixlowpan_hc06_initialize(void); +#endif + #endif /* CONFIG_NET_6LOWPAN */ #endif /* _NET_SIXLOWPAN_SIXLOWPAN_H */ diff --git a/net/sixlowpan/sixlowpan_hc06.c b/net/sixlowpan/sixlowpan_hc06.c new file mode 100644 index 0000000000..f992676ef9 --- /dev/null +++ b/net/sixlowpan/sixlowpan_hc06.c @@ -0,0 +1,169 @@ +/**************************************************************************** + * net/sixlowpan/sixlowpan_hc06.c + * 6lowpan HC06 implementation (draft-ietf-6lowpan-hc-06) + * + * Copyright (C) 2017, Gregory Nutt, all rights reserved + * Author: Gregory Nutt + * + * Derives from Contiki: + * + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * Authors: Adam Dunkels + * Nicolas Tsiftes + * Niclas Finne + * Mathilde Durvy + * Julien Abeille + * Joakim Eriksson + * Joel Hoglund + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + ****************************************************************************/ + +/* FOR HC-06 COMPLIANCE TODO: + * + * -Add compression options to UDP, currently only supports + * both ports compressed or both ports elided + * -Verify TC/FL compression works + * -Add stateless multicast option + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include "sixlowpan/sixlowpan.h" + +#ifdef CONFIG_NET_6LOWPAN_COMPRESSION_HC06 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* An address context for IPHC address compression each context can have up + * to 8 bytes + */ + +struct sixlowpan_addrcontext_s +{ + uint8_t used; /* Possibly use as prefix-length */ + uint8_t number; + uint8_t prefix[8]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* HC06 specific variables */ + +#if CONFIG_NET_6LOWPAN_MAXADDRCONTEXT > 0 +/* Addresses contexts for IPHC. */ + +static struct sixlowpan_addrcontext_s + g_hc06_addrcontexts[CONFIG_NET_6LOWPAN_MAXADDRCONTEXT]; +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: sixlowpan_hc06_initialize + * + * Description: + * sixlowpan_hc06_initialize() is called during OS initialization at power-up + * reset. It is called from the common sixlowpan_initialize() function. + * sixlowpan_hc06_initialize() configures HC06 networking data structures. + * It is called prior to platform-specific driver initialization so that + * the 6loWPAN networking subsystem is prepared to deal with network + * driver initialization actions. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void sixlowpan_hc06_initialize(void) +{ +#if CONFIG_NET_6LOWPAN_MAXADDRCONTEXT > 0 +#if CONFIG_NET_6LOWPAN_MAXADDRCONTEXT > 1 + int i; +#endif + + /* Preinitialize any address contexts for better header compression + * (Saves up to 13 bytes per 6lowpan packet). + */ + + g_hc06_addrcontexts[0].used = 1; + g_hc06_addrcontexts[0].number = 0; + + g_hc06_addrcontexts[0].prefix[0] = CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_0_0; + g_hc06_addrcontexts[0].prefix[1] = CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_0_1; + +#if CONFIG_NET_6LOWPAN_MAXADDRCONTEXT > 1 + for (i = 1; i < CONFIG_NET_6LOWPAN_MAXADDRCONTEXT; i++) + { +#ifdef CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_1 + if (i == 1) + { + g_hc06_addrcontexts[1].used = 1; + g_hc06_addrcontexts[1].number = 1; + + g_hc06_addrcontexts[1].prefix[0] = CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_1_0; + g_hc06_addrcontexts[1].prefix[1] = CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_1_1; + } + else +#ifdef CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREINIT_2 + if (i == 2) + { + g_hc06_addrcontexts[2].used = 1; + g_hc06_addrcontexts[2].number = 2; + + g_hc06_addrcontexts[2].prefix[0] = CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_1_0; + g_hc06_addrcontexts[2].prefix[1] = CONFIG_NET_6LOWPAN_MAXADDRCONTEXT_PREFIX_1_1; + } + else +#endif /* SIXLOWPAN_CONF_ADDR_CONTEXT_2 */ + { + g_hc06_addrcontexts[i].used = 0; + } +#else + g_hc06_addrcontexts[i].used = 0; +#endif /* SIXLOWPAN_CONF_ADDR_CONTEXT_1 */ + } +#endif /* CONFIG_NET_6LOWPAN_MAXADDRCONTEXT > 1 */ +#endif /* CONFIG_NET_6LOWPAN_MAXADDRCONTEXT > 0 */ +} + +#endif /* CONFIG_NET_6LOWPAN_COMPRESSION_HC06 */ diff --git a/net/sixlowpan/sixlowpan_initialize.c b/net/sixlowpan/sixlowpan_initialize.c index 53180f0a90..ccf0cefed4 100644 --- a/net/sixlowpan/sixlowpan_initialize.c +++ b/net/sixlowpan/sixlowpan_initialize.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/net_sockets.c + * net/sixlowpan/sixlowpan_initialize.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -68,7 +68,11 @@ void sixlowpan_initialize(void) { - /* REVIST: To be provided */ +#ifdef CONFIG_NET_6LOWPAN_COMPRESSION_HC06 + /* Initialize HC06 data data structures */ + + sixlowpan_hc06_initialize(); +#endif } #endif /* CONFIG_NET_6LOWPAN */