update to coap rfc

This commit is contained in:
Harald Pichler 2014-11-19 22:06:38 +01:00
parent a86f137cf5
commit d8e0dd7005
40 changed files with 3644 additions and 2310 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Matthias Kovatsch
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -31,7 +31,7 @@
/**
* \file
* Erbium (Er) CoAP client example
* Erbium (Er) CoAP client example.
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
@ -39,55 +39,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
#include "er-coap-engine.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#if WITH_COAP == 3
#include "er-coap-03-engine.h"
#elif WITH_COAP == 6
#include "er-coap-06-engine.h"
#elif WITH_COAP == 7
#include "er-coap-07-engine.h"
#elif WITH_COAP == 12
#include "er-coap-12-engine.h"
#elif WITH_COAP == 13
#include "er-coap-13-engine.h"
#else
#error "CoAP version defined by WITH_COAP not implemented"
#endif
#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])
#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
/* TODO: This server address is hard-coded for Cooja. */
#define SERVER_NODE(ipaddr) uip_ip6addr(ipaddr, 0xfe80, 0, 0, 0, 0x0221, 0x2eff, 0xff00, 0x26e6) /* cooja2 */
/* FIXME: This server address is hard-coded for Cooja and link-local for unconnected border router. */
#define SERVER_NODE(ipaddr) uip_ip6addr(ipaddr, 0xfe80, 0, 0, 0, 0x0212, 0x7402, 0x0002, 0x0202) /* cooja2 */
/* #define SERVER_NODE(ipaddr) uip_ip6addr(ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x1) */
#define LOCAL_PORT UIP_HTONS(COAP_DEFAULT_PORT+1)
#define LOCAL_PORT UIP_HTONS(COAP_DEFAULT_PORT + 1)
#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT)
PROCESS(coap_client_example, "COAP Client Example");
AUTOSTART_PROCESSES(&coap_client_example);
#define TOGGLE_INTERVAL 10
PROCESS(er_example_client, "Erbium Example Client");
AUTOSTART_PROCESSES(&er_example_client);
uip_ipaddr_t server_ipaddr;
static struct etimer et;
/* Example URIs that can be queried. */
#define NUMBER_OF_URLS 4
/* leading and ending slashes only for demo purposes, get cropped automatically when setting the Uri-Path */
char* service_urls[NUMBER_OF_URLS] = {".well-known/core", "/actuators/toggle", "battery/", "error/in//path"};
char *service_urls[NUMBER_OF_URLS] =
{ ".well-known/core", "/actuators/toggle", "battery/", "error/in//path" };
#if PLATFORM_HAS_BUTTON
static int uri_switch = 0;
#endif
/* This function is will be passed to COAP_BLOCKING_REQUEST() to handle responses. */
void
@ -96,53 +87,72 @@ client_chunk_handler(void *response)
const uint8_t *chunk;
int len = coap_get_payload(response, &chunk);
printf("|%.*s", len, (char *)chunk);
}
PROCESS_THREAD(coap_client_example, ev, data)
PROCESS_THREAD(er_example_client, ev, data)
{
PROCESS_BEGIN();
leds_off(LEDS_RED);
static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */
static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */
SERVER_NODE(&server_ipaddr);
/* receives all CoAP messages */
coap_receiver_init();
coap_init_engine();
etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND);
#if PLATFORM_HAS_BUTTON
SENSORS_ACTIVATE(button_sensor);
PRINTF("Press a button to request %s\n", service_urls[1]);
printf("Press a button to request %s\n", service_urls[uri_switch]);
#endif
while(1) {
PROCESS_YIELD();
#if PLATFORM_HAS_BUTTON
if (ev == sensors_event && data == &button_sensor) {
if(etimer_expired(&et)) {
printf("--Toggle timer--\n");
/* send a request to notify the end of the process */
PRINTF("--Toggle --\n");
leds_toggle(LEDS_RED);
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0 );
coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0);
coap_set_header_uri_path(request, service_urls[1]);
const char msg[] = "Toggle!";
coap_set_payload(request, (uint8_t *)msg, sizeof(msg)-1);
coap_set_payload(request, (uint8_t *)msg, sizeof(msg) - 1);
PRINT6ADDR(&server_ipaddr);
PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));
COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request, client_chunk_handler);
COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
client_chunk_handler);
PRINTF("\n--Done--\n");
}
printf("\n--Done--\n");
etimer_reset(&et);
#if PLATFORM_HAS_BUTTON
} else if(ev == sensors_event && data == &button_sensor) {
/* send a request to notify the end of the process */
coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
coap_set_header_uri_path(request, service_urls[uri_switch]);
printf("--Requesting %s--\n", service_urls[uri_switch]);
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");
uri_switch = (uri_switch + 1) % NUMBER_OF_URLS;
#endif
}
}
PROCESS_END();