Merge pull request #2 from iot-lab/pr/er-coap-dtls/tinydtls_and_er_coap_integration
Pr/er coap dtls/tinydtls and er coap integration
This commit is contained in:
commit
1f53447813
4
.gitmodules
vendored
4
.gitmodules
vendored
|
@ -10,7 +10,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
|
||||
|
|
|
@ -4,3 +4,18 @@ 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
|
||||
|
||||
# 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
|
||||
|
|
15
apps/er-coap/er-coap-communication.h
Normal file
15
apps/er-coap/er-coap-communication.h
Normal file
|
@ -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
|
161
apps/er-coap/er-coap-dtls.c
Normal file
161
apps/er-coap/er-coap-dtls.c
Normal file
|
@ -0,0 +1,161 @@
|
|||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "er-coap.h"
|
||||
#include "er-coap-engine.h"
|
||||
|
||||
#include "dtls.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG DEBUG_NONE
|
||||
#include "dtls_debug.h"
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
static struct dtls_context_t *dtls_ctx = NULL;
|
||||
|
||||
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 = coap_dtls_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, &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)
|
||||
{
|
||||
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. */
|
||||
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 {
|
||||
unsigned char *id;
|
||||
size_t id_length;
|
||||
unsigned char *key;
|
||||
size_t key_length;
|
||||
} psk[1] = {
|
||||
{ (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) {
|
||||
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
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
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);
|
||||
coap_receive(ctx);
|
||||
return 0;
|
||||
}
|
47
apps/er-coap/er-coap-dtls.h
Normal file
47
apps/er-coap/er-coap-dtls.h
Normal file
|
@ -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_ */
|
|
@ -41,6 +41,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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();
|
||||
|
|
|
@ -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 -------------------------------------------------------------*/
|
||||
|
|
42
apps/er-coap/er-coap-udp.c
Normal file
42
apps/er-coap/er-coap-udp.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "er-coap-engine.h"
|
||||
#include "er-coap-communication.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#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();
|
||||
}
|
|
@ -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)
|
||||
{
|
||||
|
|
1
apps/tinydtls
Submodule
1
apps/tinydtls
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit e95b02584a0041817da67c8c01f2a197d0c26915
|
|
@ -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
|
||||
|
|
25
examples/er-rest-dtls/Makefile
Normal file
25
examples/er-rest-dtls/Makefile
Normal file
|
@ -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
|
111
examples/er-rest-dtls/er-example-client.c
Normal file
111
examples/er-rest-dtls/er-example-client.c
Normal file
|
@ -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 <kovatsch@inf.ethz.ch>
|
||||
* Gaëtan Harter <gaetan.harter@inria.fr>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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();
|
||||
}
|
98
examples/er-rest-dtls/er-example-server.c
Normal file
98
examples/er-rest-dtls/er-example-server.c
Normal file
|
@ -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 <kovatsch@inf.ethz.ch>
|
||||
* Gaëtan Harter <gaetan.harter@inria.fr>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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();
|
||||
}
|
77
examples/er-rest-dtls/project-conf.h
Normal file
77
examples/er-rest-dtls/project-conf.h
Normal file
|
@ -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 <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#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__ */
|
Loading…
Reference in a new issue