cleanup code, add coap13 support

This commit is contained in:
Harald Pichler 2013-12-02 15:53:10 +01:00
parent a9b36497f6
commit fd57fcef72
5 changed files with 87 additions and 304 deletions

View file

@ -1,44 +1,50 @@
all: er-example-server
# Use this target explicitly if requried: er-plugtest-server
# use this target explicitly if requried: er-plugtest-server
# variable for this Makefile
# configure CoAP implementation (3|7|12|13) (er-coap-07 also supports CoAP draft 08)
WITH_COAP=13
# for some platforms
UIP_CONF_IPV6=1
# IPv6 make config disappeared completely
CFLAGS += -DUIP_CONF_IPV6=1
CONTIKI=../../..
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
# for static routing, if enabled
# variable for Makefile.include
ifneq ($(TARGET), minimal-net)
ifneq ($(TARGET), native)
ifneq ($(findstring avr,$(TARGET)), avr)
PROJECT_SOURCEFILES += static-routing.c
endif
endif
endif
# variable for root Makefile.include
WITH_UIP6=1
# for some platforms
UIP_CONF_IPV6=1
# variable for this Makefile
# configure CoAP implementation (3|7) (er-coap-07 also supports CoAP draft 08)
WITH_COAP=7
# new variable since slip-radio
ifneq ($(TARGET), minimal-net)
UIP_CONF_RPL=1
CFLAGS += -DUIP_CONF_IPV6_RPL=1
else
# minimal-net does not support RPL under Linux and is mostly used to test CoAP only
${info INFO: compiling without RPL}
UIP_CONF_RPL=0
CFLAGS += -DUIP_CONF_ND6_DEF_MAXDADNS=0
CFLAGS += -DUIP_CONF_IPV6_RPL=0
CFLAGS += -DHARD_CODED_ADDRESS=\"fdfd::10\"
CFLAGS += -DUIP_CONF_BUFFER_SIZE=1280
${info INFO: compiling with large buffers}
CFLAGS += -DUIP_CONF_BUFFER_SIZE=2048
CFLAGS += -DREST_MAX_CHUNK_SIZE=1024
CFLAGS += -DCOAP_MAX_HEADER_SIZE=640
endif
# linker optimizations
SMALL=1
# REST framework, requires WITH_COAP
ifeq ($(WITH_COAP), 7)
ifeq ($(WITH_COAP), 13)
${info INFO: compiling with CoAP-13}
CFLAGS += -DWITH_COAP=13
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-13
else ifeq ($(WITH_COAP), 12)
${info INFO: compiling with CoAP-12}
CFLAGS += -DWITH_COAP=12
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-12
else ifeq ($(WITH_COAP), 7)
${info INFO: compiling with CoAP-08}
CFLAGS += -DWITH_COAP=7
CFLAGS += -DREST=coap_rest_implementation
@ -84,5 +90,5 @@ connect-router: $(CONTIKI)/tools/tunslip6
connect-router-cooja: $(CONTIKI)/tools/tunslip6
sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 aaaa::1/64
tap0up:
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0

View file

@ -46,22 +46,13 @@
/* Define which resources to include to meet memory constraints. */
#define REST_RES_INFO 1
#define REST_RES_SERVO 0
#define REST_RES_T4_SERVO 1
#define REST_RES_TEMPERATURE 0
#define REST_RES_EVENT 0
#define REST_RES_LEDS 0
#define REST_RES_TOGGLE 0
#define REST_RES_BATTERY 1
#if !UIP_CONF_IPV6_RPL && !defined (CONTIKI_TARGET_MINIMAL_NET) && !defined (CONTIKI_TARGET_NATIVE)
#warning "Compiling with static routing!"
#include "static-routing.h"
#endif
#include "erbium.h"
#include "dev/led.h"
#if defined (PLATFORM_HAS_BUTTON)
#include "dev/button-sensor.h"
#endif
@ -87,6 +78,10 @@
#include "er-coap-03.h"
#elif WITH_COAP == 7
#include "er-coap-07.h"
#elif WITH_COAP == 12
#include "er-coap-12.h"
#elif WITH_COAP == 13
#include "er-coap-13.h"
#else
#warning "Erbium example without CoAP-specifc functionality"
#endif /* CoAP-specific example */
@ -272,72 +267,6 @@ t4_servo_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
}
#endif
/*A simple actuator example, post variable mode, relay is activated or deactivated*/
RESOURCE(led1, METHOD_GET | METHOD_PUT , "aktors/led1", "title=\"Led1\";rt=\"led\"");
void
led1_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
char mode[10];
static uint8_t led1 = 0;
static char name[17]="led1";
int success = 1;
char temp[100];
int index = 0;
size_t len = 0;
const char *pmode = NULL;
const char *pname = NULL;
switch(REST.get_method_type(request)){
case METHOD_GET:
// jSON Format
index += sprintf(temp + index,"{\n \"name\" : \"%s\",\n",name);
if(led1 == 0)
index += sprintf(temp + index," \"mode\" : \"off\"\n");
if(led1 == 1)
index += sprintf(temp + index," \"mode\" : \"on\"\n");
index += sprintf(temp + index,"}\n");
len = strlen(temp);
memcpy(buffer, temp,len );
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
REST.set_response_payload(response, buffer, len);
break;
case METHOD_POST:
success = 0;
break;
case METHOD_PUT:
if (success && (len=REST.get_post_variable(request, "mode", &pmode))) {
PRINTF("name %s\n", mode);
memcpy(mode, pmode,len);
mode[len]=0;
if (!strcmp(mode, "on")) {
led1_on();
led1 = 1;
} else if (!strcmp(mode, "off")) {
led1_off();
led1 = 0;
} else {
success = 0;
}
} else if (success && (len=REST.get_post_variable(request, "name", &pname))) {
PRINTF("name %s\n", name);
memcpy(name, pname,len);
name[len]=0;
} else {
success = 0;
}
break;
default:
success = 0;
}
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
/******************************************************************************/
#if defined (PLATFORM_HAS_LEDS)
@ -468,7 +397,7 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
}
else
{
REST.set_response_status(response, REST.status.UNSUPPORTED_MADIA_TYPE);
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
@ -479,7 +408,9 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
void
hw_init()
{
led1_off();
#if defined (PLATFORM_HAS_LEDS)
leds_off(LEDS_RED);
#endif
}
PROCESS(rest_server_example, "Erbium Example Server");
@ -515,7 +446,6 @@ PROCESS_THREAD(rest_server_example, ev, data)
rest_init_engine();
/* Activate the application-specific resources. */
rest_activate_resource(&resource_led1);
#if REST_RES_INFO
rest_activate_resource(&resource_info);
#endif
@ -535,10 +465,6 @@ PROCESS_THREAD(rest_server_example, ev, data)
SENSORS_ACTIVATE(battery_sensor);
rest_activate_resource(&resource_battery);
#endif
#if defined (PLATFORM_HAS_SERVO) && REST_RES_SERVO
SENSORS_ACTIVATE(servo_sensor);
rest_activate_resource(&resource_servo);
#endif
#if defined (PLATFORM_HAS_T4_SERVO) && REST_RES_T4_SERVO
SENSORS_ACTIVATE(t4_servo_sensor);
rest_activate_resource(&resource_t4_servo);

View file

@ -34,9 +34,7 @@
//#define PLATFORM_HAS_LEDS 1
//#define PLATFORM_HAS_BUTTON 1
//#define PLATFORM_HAS_SERVO 1
#define PLATFORM_HAS_T4_SERVO 1
//#define PLATFORM_HAS_TEMPERATURE 1
#define PLATFORM_HAS_BATTERY 1
#define SICSLOWPAN_CONF_FRAG 1
@ -45,34 +43,62 @@
#undef RDC_CONF_MCU_SLEEP
#define RDC_CONF_MCU_SLEEP 0
/* Some platforms have weird includes. */
#undef IEEE802154_CONF_PANID
/* Disabling RDC for demo purposes. Core updates often require more memory. */
/* For projects, optimize memory and enable RDC again. */
//#undef NETSTACK_CONF_RDC
// #undef NETSTACK_CONF_RDC
//#define NETSTACK_CONF_RDC nullrdc_driver
/* Save some memory for the sky platform. */
#undef UIP_CONF_DS6_NBR_NBU
#define UIP_CONF_DS6_NBR_NBU 10
#undef UIP_CONF_DS6_ROUTE_NBU
#define UIP_CONF_DS6_ROUTE_NBU 10
/* Increase rpl-border-router IP-buffer when using 128. */
#ifndef REST_MAX_CHUNK_SIZE
/* Increase rpl-border-router IP-buffer when using more than 64. */
#undef REST_MAX_CHUNK_SIZE
#define REST_MAX_CHUNK_SIZE 64
#endif
/* Estimate your header size, especially when using Proxy-Uri. */
/*
#undef COAP_MAX_HEADER_SIZE
#define COAP_MAX_HEADER_SIZE 70
*/
/* The IP buffer size must fit all other hops, in particular the border router. */
#undef UIP_CONF_BUFFER_SIZE
#define UIP_CONF_BUFFER_SIZE 256
/* Multiplies with chunk size, be aware of memory constraints. */
#ifndef COAP_MAX_OPEN_TRANSACTIONS
#define COAP_MAX_OPEN_TRANSACTIONS 2
#endif
#undef COAP_MAX_OPEN_TRANSACTIONS
#define COAP_MAX_OPEN_TRANSACTIONS 4
/* Must be <= open transaction number. */
#ifndef COAP_MAX_OBSERVERS
#define COAP_MAX_OBSERVERS COAP_MAX_OPEN_TRANSACTIONS-1
#endif
/* Must be <= open transaction number, 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
*/
/* Save some memory for the sky platform. */
/*
#undef NBR_TABLE_CONF_MAX_NEIGHBORS
#define NBR_TABLE_CONF_MAX_NEIGHBORS 10
#undef UIP_CONF_MAX_ROUTES
#define UIP_CONF_MAX_ROUTES 10
*/
/* Reduce 802.15.4 frame queue to save RAM. */
/*
#undef QUEUEBUF_CONF_NUM
#define QUEUEBUF_CONF_NUM 4
#define QUEUEBUF_CONF_NUM 4
*/
#endif /* __PROJECT_RPL_WEB_CONF_H__ */
/*
#undef SICSLOWPAN_CONF_FRAG
#define SICSLOWPAN_CONF_FRAG 1
*/
#endif /* PROJECT_ERBIUM_CONF_H_ */

View file

@ -1,155 +0,0 @@
/*
* static-routing.c
*
* Created on: Oct 12, 2010
* Author: simonduq
*/
#include <stdio.h>
#include "static-routing.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
#include "contiki-net.h"
#include "node-id.h"
int node_rank;
struct id_to_addrs {
int id;
uint32_t addr;
};
const struct id_to_addrs motes_addrs[] = {
/*
* Static routing requires a map nodeid => address.
* The nodeid can be programmed with the sky-shell.
* The addresses should also be added to /etc/hosts.
*
* aaaa::212:7400:1160:f62d sky1
* aaaa::212:7400:0da0:d748 sky2
* aaaa::212:7400:116e:c325 sky3
* aaaa::212:7400:116e:c444 sky4
* aaaa::212:7400:115e:b717 sky5
*
* Add the nodeid and last 4 bytes of the address to the map.
*/
{1, 0x1160f62d},
{2, 0x0da0d748},
{3, 0x116ec325},
{4, 0x116ec444},
{5, 0x115eb717},
};
/* Define the size of the map. */
#define NODES_IN_MAP 5
uint32_t get_mote_suffix(int rank) {
if(--rank >=0 && rank<(sizeof(motes_addrs)/sizeof(struct id_to_addrs))) {
return motes_addrs[rank].addr;
}
return 0;
}
int get_mote_id(uint32_t suffix) {
#if IN_COOJA
return suffix & 0xff;
#else
int i;
for(i=0; i<(sizeof(motes_addrs)/sizeof(struct id_to_addrs)); i++) {
if(suffix == motes_addrs[i].addr) {
return motes_addrs[i].id;
}
}
return 0;
#endif
}
void set_global_address(void) {
uip_ipaddr_t ipaddr;
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
}
static void add_route_ext(int dest, int next) {
PRINTF("add route ext %d %d\n", dest, next);
uip_ipaddr_t ipaddr_dest, ipaddr_next;
uip_ip6addr(&ipaddr_dest, 0xaaaa, 0, 0, 0, 0, 0, 0, dest);
#if IN_COOJA
uip_ip6addr(&ipaddr_next, 0xfe80, 0, 0, 0, 0x0212, 0x7400 | next, next, next<<8 | next);
#else
uint32_t next_suffix = get_mote_suffix(next);
uip_ip6addr(&ipaddr_next, 0xfe80, 0, 0, 0, 0x0212, 0x7400, (next_suffix >> 16) & 0xffff, next_suffix & 0xffff);
#endif
uip_ds6_route_add(&ipaddr_dest, 128, &ipaddr_next, 0);
}
void add_route(int dest, int next) {
PRINTF("add route %d %d\n", dest, next);
uip_ipaddr_t ipaddr_dest, ipaddr_next;
#if IN_COOJA
uip_ip6addr(&ipaddr_dest, 0xaaaa, 0, 0, 0, 0x0212, 0x7400 | dest, dest, dest<<8 | dest);
uip_ip6addr(&ipaddr_next, 0xfe80, 0, 0, 0, 0x0212, 0x7400 | next, next, next<<8 | next);
#else
uint32_t dest_suffix = get_mote_suffix(dest);
uint32_t next_suffix = get_mote_suffix(next);
uip_ip6addr(&ipaddr_dest, 0xaaaa, 0, 0, 0, 0x0212, 0x7400, (dest_suffix >> 16) & 0xffff, dest_suffix & 0xffff);
uip_ip6addr(&ipaddr_next, 0xfe80, 0, 0, 0, 0x0212, 0x7400, (next_suffix >> 16) & 0xffff, next_suffix & 0xffff);
#endif
uip_ds6_route_add(&ipaddr_dest, 128, &ipaddr_next, 0);
}
void configure_routing(void) {
int i;
#if IN_COOJA
node_rank = node_id;
#else
node_rank = -1;
for(i=0; i<(sizeof(motes_addrs)/sizeof(struct id_to_addrs)); ++i) {
if(node_id == motes_addrs[i].id) {
node_rank = i+1;
break;
}
}
if(node_rank == -1) {
printf("unable to configure routing, node_id=%d\n", node_id);
return;
}
#endif
printf("configure_routing, node_id=%d, node_rank %d\n", node_id, node_rank);
if (node_rank == 1) { /* border router #1 */
add_route_ext(2, 2);
for(i=2; i<=NODES_IN_MAP; ++i) {
add_route(i, 2);
}
} else if (node_rank < NODES_IN_MAP) { /* other node */
add_route_ext(1, node_rank-1);
add_route_ext(2, node_rank+1);
for(i=1; i<=NODES_IN_MAP; ++i) {
if(i<node_rank) {
add_route(i, node_rank-1);
} else if(i>node_rank) {
add_route(i, node_rank+1);
}
}
} else if (node_rank == NODES_IN_MAP) { /* 2nd border router */
add_route_ext(1, NODES_IN_MAP-1);
for(i=1; i<=NODES_IN_MAP-1; ++i) {
add_route(i, NODES_IN_MAP-1);
}
}
}

View file

@ -1,20 +0,0 @@
/*
* static-routing.h
*
* Created on: Oct 12, 2010
* Author: simonduq
*/
#ifndef STATICROUTING_H_
#define STATICROUTING_H_
#include "contiki.h"
extern int node_rank;
extern uint32_t get_mote_suffix(int id);
extern int get_mote_id(uint32_t suffix);
extern void add_route(int dest, int next);
extern void set_global_address(void);
extern void configure_routing(void);
#endif /* STATICROUTING_H_ */