From 5640293e8fd0e30b1b7c8932a6f58249947c3171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 7 Dec 2016 17:59:30 +0100 Subject: [PATCH 1/8] er-coap: separate the communication layer This code was copied and adapted from: https://github.com/cetic/6lbr Licensed under the same license as Contiki. --- apps/er-coap/Makefile.er-coap | 2 ++ apps/er-coap/er-coap-communication.h | 15 ++++++++++ apps/er-coap/er-coap-engine.c | 7 +++-- apps/er-coap/er-coap-engine.h | 1 + apps/er-coap/er-coap-udp.c | 42 ++++++++++++++++++++++++++++ apps/er-coap/er-coap.c | 23 ++------------- 6 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 apps/er-coap/er-coap-communication.h create mode 100644 apps/er-coap/er-coap-udp.c diff --git a/apps/er-coap/Makefile.er-coap b/apps/er-coap/Makefile.er-coap index 23b70613e..6a2ef7108 100755 --- a/apps/er-coap/Makefile.er-coap +++ b/apps/er-coap/Makefile.er-coap @@ -4,3 +4,5 @@ er-coap_src = er-coap.c er-coap-engine.c er-coap-transactions.c \ # Erbium will implement the REST Engine CFLAGS += -DREST=coap_rest_implementation + +er-coap_src += er-coap-udp.c diff --git a/apps/er-coap/er-coap-communication.h b/apps/er-coap/er-coap-communication.h new file mode 100644 index 000000000..bae0639da --- /dev/null +++ b/apps/er-coap/er-coap-communication.h @@ -0,0 +1,15 @@ +#ifndef _ER_COAP_COMMUNICATION_H_ +#define _ER_COAP_COMMUNICATION_H_ + +#include "contiki.h" + +void +coap_init_communication_layer(uint16_t port); + +void +coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length); + +void +coap_handle_receive(void); + +#endif diff --git a/apps/er-coap/er-coap-engine.c b/apps/er-coap/er-coap-engine.c index 872de013a..786eb092e 100644 --- a/apps/er-coap/er-coap-engine.c +++ b/apps/er-coap/er-coap-engine.c @@ -41,6 +41,7 @@ #include #include #include "er-coap-engine.h" +#include "er-coap-communication.h" #define DEBUG 0 #if DEBUG @@ -64,8 +65,8 @@ static service_callback_t service_cbk = NULL; /*---------------------------------------------------------------------------*/ /*- Internal API ------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -static int -coap_receive(void) +int +coap_receive() { erbium_status_code = NO_ERROR; @@ -346,7 +347,7 @@ PROCESS_THREAD(coap_engine, ev, data) PROCESS_YIELD(); if(ev == tcpip_event) { - coap_receive(); + coap_handle_receive(); } else if(ev == PROCESS_EVENT_TIMER) { /* retransmissions are handled here */ coap_check_transactions(); diff --git a/apps/er-coap/er-coap-engine.h b/apps/er-coap/er-coap-engine.h index c6c6ae676..5d40d1511 100644 --- a/apps/er-coap/er-coap-engine.h +++ b/apps/er-coap/er-coap-engine.h @@ -52,6 +52,7 @@ typedef coap_packet_t rest_request_t; typedef coap_packet_t rest_response_t; void coap_init_engine(void); +int coap_receive(); /*---------------------------------------------------------------------------*/ /*- Client Part -------------------------------------------------------------*/ diff --git a/apps/er-coap/er-coap-udp.c b/apps/er-coap/er-coap-udp.c new file mode 100644 index 000000000..6476d10c3 --- /dev/null +++ b/apps/er-coap/er-coap-udp.c @@ -0,0 +1,42 @@ +#include "contiki.h" +#include "contiki-net.h" +#include "er-coap-engine.h" +#include "er-coap-communication.h" + +#include + +#define DEBUG DEBUG_NONE +#include "uip-debug.h" + +static struct uip_udp_conn *udp_conn = NULL; + +/*-----------------------------------------------------------------------------------*/ +void +coap_init_communication_layer(uint16_t port) +{ + /* new connection with remote host */ + udp_conn = udp_new(NULL, 0, NULL); + udp_bind(udp_conn, port); + PRINTF("Listening on port %u\n", uip_ntohs(udp_conn->lport)); +} +/*-----------------------------------------------------------------------------------*/ +void +coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length) +{ + /* Configure connection to reply to client */ + uip_ipaddr_copy(&udp_conn->ripaddr, addr); + udp_conn->rport = port; + + uip_udp_packet_send(udp_conn, data, length); + PRINTF("-sent UDP datagram (%u)-\n", length); + + /* Restore server connection to allow data from any node */ + memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); + udp_conn->rport = 0; +} +/*-----------------------------------------------------------------------------------*/ +void +coap_handle_receive() +{ + coap_receive(); +} diff --git a/apps/er-coap/er-coap.c b/apps/er-coap/er-coap.c index 3c71ff6f9..c544f333c 100644 --- a/apps/er-coap/er-coap.c +++ b/apps/er-coap/er-coap.c @@ -44,6 +44,7 @@ #include "er-coap.h" #include "er-coap-transactions.h" +#include "er-coap-communication.h" #define DEBUG 0 #if DEBUG @@ -60,7 +61,6 @@ /*---------------------------------------------------------------------------*/ /*- Variables ---------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -static struct uip_udp_conn *udp_conn = NULL; static uint16_t current_mid = 0; coap_status_t erbium_status_code = NO_ERROR; @@ -279,9 +279,7 @@ void coap_init_connection(uint16_t port) { /* new connection with remote host */ - udp_conn = udp_new(NULL, 0, NULL); - udp_bind(udp_conn, port); - PRINTF("Listening on port %u\n", uip_ntohs(udp_conn->lport)); + coap_init_communication_layer(port); /* initialize transaction ID */ current_mid = random_rand(); @@ -422,23 +420,6 @@ coap_serialize_message(void *packet, uint8_t *buffer) return (option - buffer) + coap_pkt->payload_len; /* packet length */ } /*---------------------------------------------------------------------------*/ -void -coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, - uint16_t length) -{ - /* configure connection to reply to client */ - uip_ipaddr_copy(&udp_conn->ripaddr, addr); - udp_conn->rport = port; - - uip_udp_packet_send(udp_conn, data, length); - - PRINTF("-sent UDP datagram (%u)-\n", length); - - /* restore server socket to allow data from any node */ - memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); - udp_conn->rport = 0; -} -/*---------------------------------------------------------------------------*/ coap_status_t coap_parse_message(void *packet, uint8_t *data, uint16_t data_len) { From d94efe528a3d689a237c8ad52597594104df2086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 14 Dec 2016 13:02:18 +0100 Subject: [PATCH 2/8] tinydtls: add tinydtls as an app submodule Currently, I'm providing my version for the submodule, but patches have been proposed upstream to make tinydtls work out of the box. --- .gitmodules | 4 +++- apps/tinydtls | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) create mode 160000 apps/tinydtls diff --git a/.gitmodules b/.gitmodules index 308d06c43..da141ca5f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,7 +13,9 @@ [submodule "platform/stm32nucleo-spirit1/stm32cube-lib"] path = platform/stm32nucleo-spirit1/stm32cube-lib url = https://github.com/STclab/stm32nucleo-spirit1-lib - [submodule "tools/sensniff"] path = tools/sensniff url = https://github.com/g-oikonomou/sensniff.git +[submodule "apps/tinydtls"] + path = apps/tinydtls + url = https://github.com/iot-lab/armour-tinydtls.git diff --git a/apps/tinydtls b/apps/tinydtls new file mode 160000 index 000000000..e95b02584 --- /dev/null +++ b/apps/tinydtls @@ -0,0 +1 @@ +Subproject commit e95b02584a0041817da67c8c01f2a197d0c26915 From 612cb2375989aa000d4683dc52ea59fb5e7a6893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 7 Dec 2016 18:09:28 +0100 Subject: [PATCH 3/8] er-coap: COPIED FROM CETIC 6LBR add cetic tinydtls 'er-coap' communication layer I would rather have a different way of configuring the communication layer. A way which would not require modifiying the er-coap application. Maybe more like a "disable udp" communication layer thing. And overwrite with something else. --- apps/er-coap/Makefile.er-coap | 4 + apps/er-coap/er-coap-dtls.c | 190 ++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 apps/er-coap/er-coap-dtls.c diff --git a/apps/er-coap/Makefile.er-coap b/apps/er-coap/Makefile.er-coap index 6a2ef7108..5a13792de 100755 --- a/apps/er-coap/Makefile.er-coap +++ b/apps/er-coap/Makefile.er-coap @@ -5,4 +5,8 @@ er-coap_src = er-coap.c er-coap-engine.c er-coap-transactions.c \ # Erbium will implement the REST Engine CFLAGS += -DREST=coap_rest_implementation +ifeq ($(WITH_DTLS_COAP),1) +er-coap_src += er-coap-dtls.c +else er-coap_src += er-coap-udp.c +endif diff --git a/apps/er-coap/er-coap-dtls.c b/apps/er-coap/er-coap-dtls.c new file mode 100644 index 000000000..fe6dd85da --- /dev/null +++ b/apps/er-coap/er-coap-dtls.c @@ -0,0 +1,190 @@ +#include "contiki.h" +#include "contiki-net.h" +#include "er-coap.h" +#include "er-coap-engine.h" + +#include "dtls.h" + +#include + +#define DEBUG DEBUG_NONE +#include "dtls_debug.h" + +/*---------------------------------------------------------------------------*/ + +#if defined DTLS_CONF_IDENTITY_HINT && defined DTLS_CONF_IDENTITY_HINT_LENGTH +#define DTLS_IDENTITY_HINT DTLS_CONF_IDENTITY_HINT +#define DTLS_IDENTITY_HINT_LENGTH DTLS_CONF_IDENTITY_HINT_LENGTH +#else +#define DTLS_IDENTITY_HINT "Client_identity" +#define DTLS_IDENTITY_HINT_LENGTH 15 +#endif + +#if defined DTLS_CONF_PSK_KEY && defined DTLS_CONF_PSK_KEY_LENGTH +#define DTLS_PSK_KEY_VALUE DTLS_CONF_PSK_KEY +#define DTLS_PSK_KEY_VALUE_LENGTH DTLS_CONF_PSK_KEY_LENGTH +#else +#warning "DTLS: Using default secret key !" +#define DTLS_PSK_KEY_VALUE "secretPSK" +#define DTLS_PSK_KEY_VALUE_LENGTH 9 +#endif + +/*---------------------------------------------------------------------------*/ + +static struct dtls_context_t *dtls_ctx = NULL; + +static int +send_to_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len); + +static int +read_from_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len); +static int +get_psk_info(struct dtls_context_t *ctx, const session_t *session, + dtls_credentials_type_t type, + const unsigned char *id, size_t id_len, + unsigned char *result, size_t result_length); + +static dtls_handler_t dtls_cb = { + .write = send_to_peer, + .read = read_from_peer, + .event = NULL, +#ifdef DTLS_PSK + .get_psk_info = get_psk_info, +#endif +#ifdef DTLS_ECC + .get_ecdsa_key = NULL, + .verify_ecdsa_key = NULL, +#endif +}; + +/*-----------------------------------------------------------------------------------*/ +void +coap_init_communication_layer(uint16_t port) +{ + struct uip_udp_conn *udp_conn = NULL; + + dtls_init(); + dtls_set_log_level(DTLS_LOG_DEBUG); + + udp_conn = udp_new(NULL, 0, NULL); + udp_bind(udp_conn, port); + + dtls_ctx = dtls_new_context(udp_conn); + if(dtls_ctx) { + dtls_set_handler(dtls_ctx, &dtls_cb); + } + /* new connection with remote host */ + printf("COAP-DTLS listening on port %u\n", uip_ntohs(udp_conn->lport)); +} +/*-----------------------------------------------------------------------------------*/ +void +coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length) +{ + session_t session; + + dtls_session_init(&session); + uip_ipaddr_copy(&session.addr, addr); + session.port = port; + + dtls_write(dtls_ctx, &session, data, length); +} +/*-----------------------------------------------------------------------------------*/ +void +coap_handle_receive() +{ + session_t session; + + if(uip_newdata()) { + dtls_session_init(&session); + uip_ipaddr_copy(&session.addr, &UIP_IP_BUF->srcipaddr); + session.port = UIP_UDP_BUF->srcport; + + dtls_handle_message(dtls_ctx, &session, uip_appdata, uip_datalen()); + } +} +/* DTLS Specific functions */ +/*-----------------------------------------------------------------------------------*/ +#ifdef DTLS_PSK +/* This function is the "key store" for tinyDTLS. It is called to + * retrieve a key for the given identiy within this particular + * session. */ +static int +get_psk_info(struct dtls_context_t *ctx, const session_t *session, + dtls_credentials_type_t type, + const unsigned char *id, size_t id_len, + unsigned char *result, size_t result_length) +{ + + struct keymap_t { + unsigned char *id; + size_t id_length; + unsigned char *key; + size_t key_length; + } psk[1] = { + { (unsigned char *)DTLS_IDENTITY_HINT, DTLS_IDENTITY_HINT_LENGTH, (unsigned char *)DTLS_PSK_KEY_VALUE, DTLS_PSK_KEY_VALUE_LENGTH }, + }; + if(type == DTLS_PSK_IDENTITY) { + if(id_len) { + dtls_debug("got psk_identity_hint: '%.*s'\n", id_len, id); + } + + if(result_length < psk[0].id_length) { + dtls_warn("cannot set psk_identity -- buffer too small\n"); + return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); + } + + memcpy(result, psk[0].id, psk[0].id_length); + return psk[0].id_length; + } else if(type == DTLS_PSK_KEY) { + if(id) { + int i; + for(i = 0; i < sizeof(psk) / sizeof(struct keymap_t); i++) { + if(id_len == psk[i].id_length && memcmp(id, psk[i].id, id_len) == 0) { + if(result_length < psk[i].key_length) { + dtls_warn("buffer too small for PSK"); + return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); + } + + memcpy(result, psk[i].key, psk[i].key_length); + return psk[i].key_length; + } + } + } + } else { + return 0; + } + + return dtls_alert_fatal_create(DTLS_ALERT_DECRYPT_ERROR); +} +#endif +/*-----------------------------------------------------------------------------------*/ +static int +send_to_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len) +{ + + struct uip_udp_conn *conn = (struct uip_udp_conn *)dtls_get_app_data(ctx); + + uip_ipaddr_copy(&conn->ripaddr, &session->addr); + conn->rport = session->port; + + uip_udp_packet_send(conn, data, len); + + /* Restore server connection to allow data from any node */ + memset(&conn->ripaddr, 0, sizeof(conn->ripaddr)); + memset(&conn->rport, 0, sizeof(conn->rport)); + + return len; +} +/*-----------------------------------------------------------------------------------*/ +static int +read_from_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len) +{ + uip_len = len; + memmove(uip_appdata, data, len); + coap_receive(ctx); + return 0; +} From 59dc4413bbf269ca247de593925a38bf794bcb19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Tue, 13 Dec 2016 17:04:53 +0100 Subject: [PATCH 4/8] er-coap-dtls: rename wrongly name IDENTITY_"HINT" It's not IDENTITY_HINT but only IDENTITY here. IDENTITY_HINT is not handled in any ways. --- apps/er-coap/er-coap-dtls.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/er-coap/er-coap-dtls.c b/apps/er-coap/er-coap-dtls.c index fe6dd85da..03c26209a 100644 --- a/apps/er-coap/er-coap-dtls.c +++ b/apps/er-coap/er-coap-dtls.c @@ -12,12 +12,12 @@ /*---------------------------------------------------------------------------*/ -#if defined DTLS_CONF_IDENTITY_HINT && defined DTLS_CONF_IDENTITY_HINT_LENGTH -#define DTLS_IDENTITY_HINT DTLS_CONF_IDENTITY_HINT -#define DTLS_IDENTITY_HINT_LENGTH DTLS_CONF_IDENTITY_HINT_LENGTH +#if defined DTLS_CONF_IDENTITY && defined DTLS_CONF_IDENTITY_LENGTH +#define DTLS_IDENTITY DTLS_CONF_IDENTITY +#define DTLS_IDENTITY_LENGTH DTLS_CONF_IDENTITY_LENGTH #else -#define DTLS_IDENTITY_HINT "Client_identity" -#define DTLS_IDENTITY_HINT_LENGTH 15 +#define DTLS_IDENTITY "Client_identity" +#define DTLS_IDENTITY_LENGTH 15 #endif #if defined DTLS_CONF_PSK_KEY && defined DTLS_CONF_PSK_KEY_LENGTH @@ -123,7 +123,7 @@ get_psk_info(struct dtls_context_t *ctx, const session_t *session, unsigned char *key; size_t key_length; } psk[1] = { - { (unsigned char *)DTLS_IDENTITY_HINT, DTLS_IDENTITY_HINT_LENGTH, (unsigned char *)DTLS_PSK_KEY_VALUE, DTLS_PSK_KEY_VALUE_LENGTH }, + { (unsigned char *)DTLS_IDENTITY, DTLS_IDENTITY_LENGTH, (unsigned char *)DTLS_PSK_KEY_VALUE, DTLS_PSK_KEY_VALUE_LENGTH }, }; if(type == DTLS_PSK_IDENTITY) { if(id_len) { From fcc4f6d39f3a88caa7d1664f41d8c18a76712620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 14 Dec 2016 11:51:49 +0100 Subject: [PATCH 5/8] er-coap-dtls: use tinydtls app if "WITH_DTLS_COAP" is set --- apps/er-coap/Makefile.er-coap | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/er-coap/Makefile.er-coap b/apps/er-coap/Makefile.er-coap index 5a13792de..c49e6d26e 100755 --- a/apps/er-coap/Makefile.er-coap +++ b/apps/er-coap/Makefile.er-coap @@ -7,6 +7,15 @@ CFLAGS += -DREST=coap_rest_implementation ifeq ($(WITH_DTLS_COAP),1) er-coap_src += er-coap-dtls.c + +# Load tinydtls +APPS += tinydtls +$(CONTIKI)/apps/tinydtls/Makefile.tinydtls: + @echo " You should run 'git submodule update --init' to clone 'app/tinydtls'" + @exit 1 +include $(CONTIKI)/apps/tinydtls/Makefile.tinydtls +PROJECTDIRS+=$(CONTIKI)/apps/tinydtls/aes $(CONTIKI)/apps/tinydtls/sha2 $(CONTIKI)/apps/tinydtls/ecc + else er-coap_src += er-coap-udp.c endif From e24f500e57488914cb40ba3764f55095354f67d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 14 Dec 2016 12:01:19 +0100 Subject: [PATCH 6/8] er-rest-dtls: add rest dtls examples --- examples/er-rest-dtls/Makefile | 25 +++++ examples/er-rest-dtls/er-example-client.c | 111 ++++++++++++++++++++++ examples/er-rest-dtls/er-example-server.c | 98 +++++++++++++++++++ examples/er-rest-dtls/project-conf.h | 77 +++++++++++++++ 4 files changed, 311 insertions(+) create mode 100644 examples/er-rest-dtls/Makefile create mode 100644 examples/er-rest-dtls/er-example-client.c create mode 100644 examples/er-rest-dtls/er-example-server.c create mode 100644 examples/er-rest-dtls/project-conf.h diff --git a/examples/er-rest-dtls/Makefile b/examples/er-rest-dtls/Makefile new file mode 100644 index 000000000..f6ef24789 --- /dev/null +++ b/examples/er-rest-dtls/Makefile @@ -0,0 +1,25 @@ +all: er-example-server er-example-client + +CONTIKI=../.. + +CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\" + +WITH_DTLS_COAP ?= 1 + +# automatically build RESTful resources +ER_EXAMPLE = $(CONTIKI)/examples/er-rest-example +REST_RESOURCES_DIR = $(ER_EXAMPLE)/resources +REST_RESOURCES_FILES += res-push.c +REST_RESOURCES_FILES += res-hello.c +PROJECTDIRS += $(REST_RESOURCES_DIR) +PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES) + +# linker optimizations +SMALL=1 + +# REST Engine shall use Erbium CoAP implementation +APPS += er-coap +APPS += rest-engine + +CONTIKI_WITH_IPV6 = 1 +include $(CONTIKI)/Makefile.include diff --git a/examples/er-rest-dtls/er-example-client.c b/examples/er-rest-dtls/er-example-client.c new file mode 100644 index 000000000..07caec573 --- /dev/null +++ b/examples/er-rest-dtls/er-example-client.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich + * All rights reserved. + * + * 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. + * + * This file is part of the Contiki operating system. + */ + +/** + * \file + * Erbium (Er) CoAP client example. + * \author + * Matthias Kovatsch + * Gaëtan Harter + */ + +#include +#include +#include +#include "contiki.h" +#include "contiki-net.h" +#include "er-coap-engine.h" + +#define DEBUG 0 +#include "uip-debug.h" + +#ifndef ER_COAP_SERVER_ADDR +/* cooja2 */ +#define ER_COAP_SERVER_ADDR "fe80::0212:7402:0002:0x0202" +#endif /* ER_COAP_SERVER_ADDR */ + +#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT) + +#define TOGGLE_INTERVAL 10 + +PROCESS(er_example_client, "Erbium Example Client"); +AUTOSTART_PROCESSES(&er_example_client); + + +/* This function is will be passed to COAP_BLOCKING_REQUEST() to handle responses. */ +void +client_chunk_handler(void *response) +{ + const uint8_t *chunk; + + int len = coap_get_payload(response, &chunk); + + printf("|%.*s", len, (char *)chunk); +} +PROCESS_THREAD(er_example_client, ev, data) +{ + PROCESS_BEGIN(); + + static struct etimer et; + static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */ + static uip_ipaddr_t server_ipaddr; + + uiplib_ipaddrconv(ER_COAP_SERVER_ADDR, &server_ipaddr); + + /* receives all CoAP messages */ + coap_init_engine(); + + etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); + + while(1) { + PROCESS_YIELD(); + + if(etimer_expired(&et)) { + printf("--Toggle timer--\n"); + + /* prepare request, TID is set by COAP_BLOCKING_REQUEST() */ + coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0); + coap_set_header_uri_path(request, "test/hello"); + + PRINT6ADDR(&server_ipaddr); + PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT)); + + COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request, + client_chunk_handler); + + printf("\n--Done--\n"); + + etimer_reset(&et); + } + } + + PROCESS_END(); +} diff --git a/examples/er-rest-dtls/er-example-server.c b/examples/er-rest-dtls/er-example-server.c new file mode 100644 index 000000000..96e38fbde --- /dev/null +++ b/examples/er-rest-dtls/er-example-server.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich + * All rights reserved. + * + * 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. + * + * This file is part of the Contiki operating system. + */ + +/** + * \file + * Erbium (Er) REST Engine example. + * \author + * Matthias Kovatsch + * Gaëtan Harter + */ + +#include +#include +#include +#include "contiki.h" +#include "contiki-net.h" +#include "rest-engine.h" + +#define DEBUG 0 +#include "uip-debug.h" + +/* + * Resources to be activated need to be imported through the extern keyword. + * The build system automatically compiles the resources in the corresponding sub-directory. + */ +extern resource_t + res_hello, + res_push; + +PROCESS(er_example_server, "Erbium Example Server"); +AUTOSTART_PROCESSES(&er_example_server); + +PROCESS_THREAD(er_example_server, ev, data) +{ + PROCESS_BEGIN(); + + PROCESS_PAUSE(); + + PRINTF("Starting Erbium Example Server\n"); + +#ifdef RF_CHANNEL + PRINTF("RF channel: %u\n", RF_CHANNEL); +#endif +#ifdef IEEE802154_PANID + PRINTF("PAN ID: 0x%04X\n", IEEE802154_PANID); +#endif + + PRINTF("uIP buffer: %u\n", UIP_BUFSIZE); + PRINTF("LL header: %u\n", UIP_LLH_LEN); + PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN); + PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE); + + /* Initialize the REST engine. */ + rest_init_engine(); + + /* + * Bind the resources to their Uri-Path. + * WARNING: Activating twice only means alternate path, not two instances! + * All static variables are the same for each URI path. + */ + rest_activate_resource(&res_hello, "test/hello"); + rest_activate_resource(&res_push, "test/push"); + + /* Define application-specific events here. */ + while(1) { + PROCESS_WAIT_EVENT(); + } /* while (1) */ + + PROCESS_END(); +} diff --git a/examples/er-rest-dtls/project-conf.h b/examples/er-rest-dtls/project-conf.h new file mode 100644 index 000000000..ebd3256cb --- /dev/null +++ b/examples/er-rest-dtls/project-conf.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich + * All rights reserved. + * + * 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. + * + * This file is part of the Contiki operating system. + */ + +/** + * \file + * Erbium (Er) example project configuration. + * \author + * Matthias Kovatsch + */ + +#ifndef __PROJECT_ERBIUM_CONF_H__ +#define __PROJECT_ERBIUM_CONF_H__ + +/* Custom channel and PAN ID configuration for your project. */ + +/* IP buffer size must match all other hops, in particular the border router. */ +#define UIP_CONF_BUFFER_SIZE 1500 + +/* Increase rpl-border-router IP-buffer when using more than 64. */ +#undef REST_MAX_CHUNK_SIZE +#define REST_MAX_CHUNK_SIZE 48 + +/* Estimate your header size, especially when using Proxy-Uri. */ +/* + #undef COAP_MAX_HEADER_SIZE + #define COAP_MAX_HEADER_SIZE 70 + */ + +/* Multiplies with chunk size, be aware of memory constraints. */ +#undef COAP_MAX_OPEN_TRANSACTIONS +#define COAP_MAX_OPEN_TRANSACTIONS 4 + +/* Must be <= open transactions, default is COAP_MAX_OPEN_TRANSACTIONS-1. */ +/* + #undef COAP_MAX_OBSERVERS + #define COAP_MAX_OBSERVERS 2 + */ + +/* Filtering .well-known/core per query can be disabled to save space. */ +#undef COAP_LINK_FORMAT_FILTERING +#define COAP_LINK_FORMAT_FILTERING 0 +#undef COAP_PROXY_OPTION_PROCESSING +#define COAP_PROXY_OPTION_PROCESSING 0 + + +/* Enable client-side support for COAP observe */ +#define COAP_OBSERVE_CLIENT 1 + +#endif /* __PROJECT_ERBIUM_CONF_H__ */ From 1addd8da3a18e6ea41ebfbe1829a2193451865dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Thu, 23 Mar 2017 16:24:37 +0100 Subject: [PATCH 7/8] apps/tinydtls: disable doxygen generation --- doc/Doxyfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/Doxyfile b/doc/Doxyfile index 056018d13..f7cc7455f 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -809,7 +809,8 @@ EXCLUDE_SYMLINKS = NO EXCLUDE_PATTERNS = */cpu/cc26xx-cc13xx/lib/* \ */cpu/cc26xx-cc13xx/rf-core/api/* \ - */platform/stm32nucleo-spirit1/stm32cube-lib/* + */platform/stm32nucleo-spirit1/stm32cube-lib/* \ + */apps/tinydtls/* # The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names # (namespaces, classes, functions, etc.) that should be excluded from the From c7de205029411d8b54d6c4a7938ddef0605575c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Thu, 23 Mar 2017 17:23:22 +0100 Subject: [PATCH 8/8] er-coap/dtls: allow overwriting the default tinydtls specific functions --- apps/er-coap/er-coap-dtls.c | 65 ++++++++++--------------------------- apps/er-coap/er-coap-dtls.h | 47 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 apps/er-coap/er-coap-dtls.h diff --git a/apps/er-coap/er-coap-dtls.c b/apps/er-coap/er-coap-dtls.c index 03c26209a..d83dce478 100644 --- a/apps/er-coap/er-coap-dtls.c +++ b/apps/er-coap/er-coap-dtls.c @@ -12,46 +12,16 @@ /*---------------------------------------------------------------------------*/ -#if defined DTLS_CONF_IDENTITY && defined DTLS_CONF_IDENTITY_LENGTH -#define DTLS_IDENTITY DTLS_CONF_IDENTITY -#define DTLS_IDENTITY_LENGTH DTLS_CONF_IDENTITY_LENGTH -#else -#define DTLS_IDENTITY "Client_identity" -#define DTLS_IDENTITY_LENGTH 15 -#endif - -#if defined DTLS_CONF_PSK_KEY && defined DTLS_CONF_PSK_KEY_LENGTH -#define DTLS_PSK_KEY_VALUE DTLS_CONF_PSK_KEY -#define DTLS_PSK_KEY_VALUE_LENGTH DTLS_CONF_PSK_KEY_LENGTH -#else -#warning "DTLS: Using default secret key !" -#define DTLS_PSK_KEY_VALUE "secretPSK" -#define DTLS_PSK_KEY_VALUE_LENGTH 9 -#endif - /*---------------------------------------------------------------------------*/ static struct dtls_context_t *dtls_ctx = NULL; -static int -send_to_peer(struct dtls_context_t *ctx, - session_t *session, uint8 *data, size_t len); - -static int -read_from_peer(struct dtls_context_t *ctx, - session_t *session, uint8 *data, size_t len); -static int -get_psk_info(struct dtls_context_t *ctx, const session_t *session, - dtls_credentials_type_t type, - const unsigned char *id, size_t id_len, - unsigned char *result, size_t result_length); - -static dtls_handler_t dtls_cb = { - .write = send_to_peer, - .read = read_from_peer, +static dtls_handler_t coap_dtls_callback = { + .write = coap_dtls_send_to_peer, + .read = coap_dtls_read_from_peer, .event = NULL, #ifdef DTLS_PSK - .get_psk_info = get_psk_info, + .get_psk_info = coap_dtls_get_psk_info, #endif #ifdef DTLS_ECC .get_ecdsa_key = NULL, @@ -73,14 +43,15 @@ coap_init_communication_layer(uint16_t port) dtls_ctx = dtls_new_context(udp_conn); if(dtls_ctx) { - dtls_set_handler(dtls_ctx, &dtls_cb); + dtls_set_handler(dtls_ctx, &COAP_DTLS_CALLBACK); } /* new connection with remote host */ printf("COAP-DTLS listening on port %u\n", uip_ntohs(udp_conn->lport)); } /*-----------------------------------------------------------------------------------*/ void -coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length) +coap_send_message(uip_ipaddr_t *addr, uint16_t port, + uint8_t *data, uint16_t length) { session_t session; @@ -110,11 +81,11 @@ coap_handle_receive() /* This function is the "key store" for tinyDTLS. It is called to * retrieve a key for the given identiy within this particular * session. */ -static int -get_psk_info(struct dtls_context_t *ctx, const session_t *session, - dtls_credentials_type_t type, - const unsigned char *id, size_t id_len, - unsigned char *result, size_t result_length) +int +coap_dtls_get_psk_info(struct dtls_context_t *ctx, const session_t *session, + dtls_credentials_type_t type, + const unsigned char *id, size_t id_len, + unsigned char *result, size_t result_length) { struct keymap_t { @@ -160,9 +131,9 @@ get_psk_info(struct dtls_context_t *ctx, const session_t *session, } #endif /*-----------------------------------------------------------------------------------*/ -static int -send_to_peer(struct dtls_context_t *ctx, - session_t *session, uint8 *data, size_t len) +int +coap_dtls_send_to_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len) { struct uip_udp_conn *conn = (struct uip_udp_conn *)dtls_get_app_data(ctx); @@ -179,9 +150,9 @@ send_to_peer(struct dtls_context_t *ctx, return len; } /*-----------------------------------------------------------------------------------*/ -static int -read_from_peer(struct dtls_context_t *ctx, - session_t *session, uint8 *data, size_t len) +int +coap_dtls_read_from_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len) { uip_len = len; memmove(uip_appdata, data, len); diff --git a/apps/er-coap/er-coap-dtls.h b/apps/er-coap/er-coap-dtls.h new file mode 100644 index 000000000..ac87b1261 --- /dev/null +++ b/apps/er-coap/er-coap-dtls.h @@ -0,0 +1,47 @@ +#ifndef COAP_DTLS_H_ +#define COAP_DTLS_H_ + +/* Internal configuration of tinydtls for er-coap-dtls */ + +#if defined DTLS_CONF_IDENTITY && defined DTLS_CONF_IDENTITY_LENGTH +#define DTLS_IDENTITY DTLS_CONF_IDENTITY +#define DTLS_IDENTITY_LENGTH DTLS_CONF_IDENTITY_LENGTH +#else +#define DTLS_IDENTITY "Client_identity" +#define DTLS_IDENTITY_LENGTH 15 +#endif + +#if defined DTLS_CONF_PSK_KEY && defined DTLS_CONF_PSK_KEY_LENGTH +#define DTLS_PSK_KEY_VALUE DTLS_CONF_PSK_KEY +#define DTLS_PSK_KEY_VALUE_LENGTH DTLS_CONF_PSK_KEY_LENGTH +#else +#warning "DTLS: Using default secret key !" +#define DTLS_PSK_KEY_VALUE "secretPSK" +#define DTLS_PSK_KEY_VALUE_LENGTH 9 +#endif + +/* Structure that hold tinydtls callbacks, has type 'dtls_handler_t'. */ +#ifndef COAP_DTLS_CALLBACK +#ifdef COAP_DTLS_CONF_CALLBACK +#define COAP_DTLS_CALLBACK COAP_DTLS_CONF_CALLBACK +#else /* COAP_DTLS_CONF_CALLBACK */ +#define COAP_DTLS_CALLBACK coap_dtls_callback +#endif /* COAP_DTLS_CALLBACK */ + +/* Send 'data' to peer defined by session */ +int coap_dtls_send_to_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len); + +/* Read 'data' from peer */ +int coap_dtls_read_from_peer(struct dtls_context_t *ctx, + session_t *session, uint8 *data, size_t len); +#ifdef DTLS_PSK +/* Retrieve the key for given identity withing this session */ +int coap_dtls_get_psk_info(struct dtls_context_t *ctx, + const session_t *session, + dtls_credentials_type_t type, + const unsigned char *id, size_t id_len, + unsigned char *result, size_t result_length); +#endif + +#endif /* COAP_DTLS_H_ */