Merge remote-tracking branch 'sf/master' into rpl-patch
|
@ -55,7 +55,7 @@ NET = netstack.c uip-debug.c packetbuf.c queuebuf.c packetqueue.c
|
||||||
ifdef UIP_CONF_IPV6
|
ifdef UIP_CONF_IPV6
|
||||||
CFLAGS += -DUIP_CONF_IPV6=1
|
CFLAGS += -DUIP_CONF_IPV6=1
|
||||||
UIP = uip6.c tcpip.c psock.c uip-udp-packet.c uip-split.c \
|
UIP = uip6.c tcpip.c psock.c uip-udp-packet.c uip-split.c \
|
||||||
resolv.c tcpdump.c uiplib.c
|
resolv.c tcpdump.c uiplib.c simple-udp.c
|
||||||
NET += $(UIP) uip-icmp6.c uip-nd6.c uip-packetqueue.c \
|
NET += $(UIP) uip-icmp6.c uip-nd6.c uip-packetqueue.c \
|
||||||
sicslowpan.c neighbor-attr.c neighbor-info.c uip-ds6.c
|
sicslowpan.c neighbor-attr.c neighbor-info.c uip-ds6.c
|
||||||
include $(CONTIKI)/core/net/rpl/Makefile.rpl
|
include $(CONTIKI)/core/net/rpl/Makefile.rpl
|
||||||
|
@ -136,6 +136,14 @@ vpath %.S $(SOURCEDIRS)
|
||||||
|
|
||||||
CFLAGS += ${addprefix -I,$(SOURCEDIRS)}
|
CFLAGS += ${addprefix -I,$(SOURCEDIRS)}
|
||||||
|
|
||||||
|
### Check for a git repo and pass version if found
|
||||||
|
### git.exe in Windows cmd shells may require no stderr redirection
|
||||||
|
#RELSTR=${shell git describe --tags}
|
||||||
|
RELSTR=${shell git describe --tags 2>/dev/null}
|
||||||
|
ifneq ($(RELSTR),)
|
||||||
|
CFLAGS += -DCONTIKI_VERSION_STRING=\"Contiki-$(RELSTR)\"
|
||||||
|
endif
|
||||||
|
|
||||||
### Automatic dependency generation
|
### Automatic dependency generation
|
||||||
|
|
||||||
ifneq ($(MAKECMDGOALS),clean)
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
|
@ -152,7 +160,7 @@ sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
|
||||||
rm -f $(@:.o=.$$$$)
|
rm -f $(@:.o=.$$$$)
|
||||||
endef
|
endef
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *~ *core core *.srec \
|
rm -f *~ *core core *.srec \
|
||||||
*.lst *.map \
|
*.lst *.map \
|
||||||
*.cprg *.bin *.data contiki*.a *.firmware core-labels.S *.ihex *.ini \
|
*.cprg *.bin *.data contiki*.a *.firmware core-labels.S *.ihex *.ini \
|
||||||
|
|
|
@ -3,5 +3,9 @@ collect-view_src = collect-view.c
|
||||||
ifeq ($(TARGET), sky)
|
ifeq ($(TARGET), sky)
|
||||||
collect-view_src += collect-view-sky.c
|
collect-view_src += collect-view-sky.c
|
||||||
else
|
else
|
||||||
|
ifeq ($(TARGET), z1)
|
||||||
|
collect-view_src += collect-view-z1.c
|
||||||
|
else
|
||||||
collect-view_src += collect-view-template.c
|
collect-view_src += collect-view-template.c
|
||||||
endif
|
endif
|
||||||
|
endif
|
||||||
|
|
34
apps/collect-view/collect-view-z1.c
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#include "collect-view.h"
|
||||||
|
#include "dev/cc2420.h"
|
||||||
|
#include "dev/leds.h"
|
||||||
|
#include "dev/i2cmaster.h"
|
||||||
|
#include "dev/tmp102.h"
|
||||||
|
|
||||||
|
#include "collect-view-z1.h"
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static uint16_t
|
||||||
|
get_temp()
|
||||||
|
{
|
||||||
|
/* XXX Fix me: check /examples/z1/test-tmp102.c for correct conversion */
|
||||||
|
return (uint16_t)tmp102_read_temp_raw();
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
collect_view_arch_read_sensors(struct collect_view_data_msg *msg)
|
||||||
|
{
|
||||||
|
static int initialized = 0;
|
||||||
|
|
||||||
|
if(!initialized) {
|
||||||
|
tmp102_init();
|
||||||
|
initialized = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
msg->sensors[BATTERY_VOLTAGE_SENSOR] = 0;
|
||||||
|
msg->sensors[BATTERY_INDICATOR] = 0;
|
||||||
|
msg->sensors[LIGHT1_SENSOR] = 0;
|
||||||
|
msg->sensors[LIGHT2_SENSOR] = 0;
|
||||||
|
msg->sensors[TEMP_SENSOR] = get_temp();
|
||||||
|
msg->sensors[HUMIDITY_SENSOR] = 0;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
21
apps/collect-view/collect-view-z1.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#ifndef COLLECT_VIEW_Z1_H
|
||||||
|
#define COLLECT_VIEW_Z1_H
|
||||||
|
|
||||||
|
#include "collect-view.h"
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BATTERY_VOLTAGE_SENSOR,
|
||||||
|
BATTERY_INDICATOR,
|
||||||
|
LIGHT1_SENSOR,
|
||||||
|
LIGHT2_SENSOR,
|
||||||
|
TEMP_SENSOR,
|
||||||
|
HUMIDITY_SENSOR,
|
||||||
|
RSSI_SENSOR,
|
||||||
|
ETX1_SENSOR,
|
||||||
|
ETX2_SENSOR,
|
||||||
|
ETX3_SENSOR,
|
||||||
|
ETX4_SENSOR,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* COLLECT_VIEW_Z1_H */
|
1
apps/er-coap-03/Makefile.er-coap-03
Normal file
|
@ -0,0 +1 @@
|
||||||
|
er-coap-03_src = er-coap-03-engine.c er-coap-03.c er-coap-03-transactions.c er-coap-03-observing.c
|
559
apps/er-coap-03/er-coap-03-engine.c
Normal file
|
@ -0,0 +1,559 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP implementation of the REST Engine
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "er-coap-03-engine.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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 PRINTBITS(buf,len) { \
|
||||||
|
int i,j=0; \
|
||||||
|
for (i=0; i<len; ++i) { \
|
||||||
|
for (j=7; j>=0; --j) { \
|
||||||
|
PRINTF("%c", (((char *)buf)[i] & 1<<j) ? '1' : '0'); \
|
||||||
|
} \
|
||||||
|
PRINTF(" "); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define PRINTF(...)
|
||||||
|
#define PRINT6ADDR(addr)
|
||||||
|
#define PRINTLLADDR(addr)
|
||||||
|
#define PRINTBITS(buf,len)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
PROCESS(coap_receiver, "CoAP Receiver");
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Constants -----------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
const char* error_messages[] = {
|
||||||
|
"",
|
||||||
|
|
||||||
|
/* Memory errors */
|
||||||
|
"Transaction buffer allocation failed",
|
||||||
|
"Memory boundary exceeded",
|
||||||
|
|
||||||
|
/* CoAP errors */
|
||||||
|
"Request has unknown critical option", /*FIXME which one? */
|
||||||
|
"Packet could not be serialized"
|
||||||
|
};
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Variables -----------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static service_callback_t service_cbk = NULL;
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
int
|
||||||
|
handle_incoming_data(void)
|
||||||
|
{
|
||||||
|
int error = NO_ERROR;
|
||||||
|
|
||||||
|
PRINTF("handle_incoming_data(): received uip_datalen=%u \n",(uint16_t)uip_datalen());
|
||||||
|
|
||||||
|
uint8_t *data = uip_appdata + uip_ext_len;
|
||||||
|
uint16_t data_len = uip_datalen() - uip_ext_len;
|
||||||
|
|
||||||
|
if (uip_newdata()) {
|
||||||
|
|
||||||
|
PRINTF("receiving UDP datagram from: ");
|
||||||
|
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
|
||||||
|
PRINTF(":%u\n Length: %u\n Data: ", uip_ntohs(UIP_UDP_BUF->srcport), data_len );
|
||||||
|
PRINTBITS(data, data_len);
|
||||||
|
PRINTF("\n");
|
||||||
|
|
||||||
|
coap_packet_t message[1];
|
||||||
|
coap_transaction_t *transaction = NULL;
|
||||||
|
|
||||||
|
error = coap_parse_message(message, data, data_len);
|
||||||
|
|
||||||
|
if (error==NO_ERROR)
|
||||||
|
{
|
||||||
|
|
||||||
|
/*TODO duplicates suppression, if required */
|
||||||
|
|
||||||
|
PRINTF(" Parsed: v %u, t %u, oc %u, c %u, tid %u\n", message->version, message->type, message->option_count, message->code, message->tid);
|
||||||
|
PRINTF(" URL: %.*s\n", message->uri_path_len, message->uri_path);
|
||||||
|
PRINTF(" Payload: %.*s\n", message->payload_len, message->payload);
|
||||||
|
|
||||||
|
/* Handle requests. */
|
||||||
|
if (message->code >= COAP_GET && message->code <= COAP_DELETE)
|
||||||
|
{
|
||||||
|
/* Use transaction buffer for response to confirmable request. */
|
||||||
|
if ( (transaction = coap_new_transaction(message->tid, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport)) )
|
||||||
|
{
|
||||||
|
uint32_t block_num = 0;
|
||||||
|
uint16_t block_size = REST_MAX_CHUNK_SIZE;
|
||||||
|
uint32_t block_offset = 0;
|
||||||
|
int32_t new_offset = 0;
|
||||||
|
|
||||||
|
/* prepare response */
|
||||||
|
coap_packet_t response[1]; /* This way the packet can be treated as pointer as usual. */
|
||||||
|
if (message->type==COAP_TYPE_CON)
|
||||||
|
{
|
||||||
|
/* Reliable CON requests are answered with an ACK. */
|
||||||
|
coap_init_message(response, COAP_TYPE_ACK, OK_200, message->tid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Unreliable NON requests are answered with a NON as well. */
|
||||||
|
coap_init_message(response, COAP_TYPE_NON, OK_200, coap_get_tid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* resource handlers must take care of different handling (e.g., TOKEN_OPTION_REQUIRED_240) */
|
||||||
|
if (IS_OPTION(message, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
coap_set_header_token(response, message->token, message->token_len);
|
||||||
|
SET_OPTION(response, COAP_OPTION_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* get offset for blockwise transfers */
|
||||||
|
if (coap_get_header_block(message, &block_num, NULL, &block_size, &block_offset))
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: block request %lu (%u/%u) @ %lu bytes\n", block_num, block_size, REST_MAX_CHUNK_SIZE, block_offset);
|
||||||
|
block_size = MIN(block_size, REST_MAX_CHUNK_SIZE);
|
||||||
|
new_offset = block_offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*------------------------------------------*/
|
||||||
|
/* call application-specific handler */
|
||||||
|
/*------------------------------------------*/
|
||||||
|
if (service_cbk) {
|
||||||
|
service_cbk(message, response, transaction->packet+COAP_MAX_HEADER_SIZE, block_size, &new_offset);
|
||||||
|
}
|
||||||
|
/*------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
|
/* apply blockwise transfers */
|
||||||
|
if ( IS_OPTION(message, COAP_OPTION_BLOCK) )
|
||||||
|
{
|
||||||
|
/* unchanged new_offset indicates that resource is unaware of blockwise transfer */
|
||||||
|
if (new_offset==block_offset)
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: unaware resource with payload length %u/%u\n", response->payload_len, block_size);
|
||||||
|
if (block_offset >= response->payload_len)
|
||||||
|
{
|
||||||
|
response->code = BAD_REQUEST_400;
|
||||||
|
coap_set_payload(response, (uint8_t*)"Block out of scope", 18);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_set_header_block(response, block_num, response->payload_len - block_offset > block_size, block_size);
|
||||||
|
coap_set_payload(response, response->payload+block_offset, MIN(response->payload_len - block_offset, block_size));
|
||||||
|
} /* if (valid offset) */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* resource provides chunk-wise data */
|
||||||
|
PRINTF("Blockwise: blockwise resource, new offset %ld\n", new_offset);
|
||||||
|
coap_set_header_block(response, block_num, new_offset!=-1 || response->payload_len > block_size, block_size);
|
||||||
|
if (response->payload_len > block_size) coap_set_payload(response, response->payload, block_size);
|
||||||
|
} /* if (resource aware of blockwise) */
|
||||||
|
}
|
||||||
|
else if (new_offset!=0)
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: no block option for blockwise resource, using block size %u\n", REST_MAX_CHUNK_SIZE);
|
||||||
|
|
||||||
|
coap_set_header_block(response, 0, new_offset!=-1, REST_MAX_CHUNK_SIZE);
|
||||||
|
coap_set_payload(response, response->payload, MIN(response->payload_len, REST_MAX_CHUNK_SIZE));
|
||||||
|
} /* if (blockwise request) */
|
||||||
|
|
||||||
|
if ((transaction->packet_len = coap_serialize_message(response, transaction->packet))==0)
|
||||||
|
{
|
||||||
|
error = PACKET_SERIALIZATION_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
error = MEMORY_ALLOC_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Responses */
|
||||||
|
coap_transaction_t *t;
|
||||||
|
|
||||||
|
if (message->type==COAP_TYPE_ACK)
|
||||||
|
{
|
||||||
|
PRINTF("Received ACK\n");
|
||||||
|
}
|
||||||
|
else if (message->type==COAP_TYPE_RST)
|
||||||
|
{
|
||||||
|
PRINTF("Received RST\n");
|
||||||
|
/* Cancel possible subscriptions. */
|
||||||
|
if (IS_OPTION(message, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
PRINTF(" Token 0x%02X%02X\n", message->token[0], message->token[1]);
|
||||||
|
coap_remove_observer_by_token(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, message->token, message->token_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (t = coap_get_transaction_by_tid(message->tid)) )
|
||||||
|
{
|
||||||
|
/* Free transaction memory before callback, as it may create a new transaction. */
|
||||||
|
restful_response_handler callback = t->callback;
|
||||||
|
void *callback_data = t->callback_data;
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
|
||||||
|
/* Check if someone registered for the response */
|
||||||
|
if (callback) {
|
||||||
|
callback(callback_data, message);
|
||||||
|
}
|
||||||
|
} /* if (transaction) */
|
||||||
|
}
|
||||||
|
} /* if (parsed correctly) */
|
||||||
|
|
||||||
|
if (error==NO_ERROR) {
|
||||||
|
if (transaction) coap_send_transaction(transaction);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("ERROR %u: %s\n", error, error_messages[error]);
|
||||||
|
|
||||||
|
/* reuse input buffer */
|
||||||
|
coap_init_message(message, COAP_TYPE_ACK, INTERNAL_SERVER_ERROR_500, message->tid);
|
||||||
|
coap_set_payload(message, (uint8_t *) error_messages[error], strlen(error_messages[error]));
|
||||||
|
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, data, coap_serialize_message(message, data));
|
||||||
|
}
|
||||||
|
} /* if (new data) */
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_receiver_init()
|
||||||
|
{
|
||||||
|
process_start(&coap_receiver, NULL);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_set_service_callback(service_callback_t callback)
|
||||||
|
{
|
||||||
|
service_cbk = callback;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
rest_resource_flags_t
|
||||||
|
coap_get_rest_method(void *packet)
|
||||||
|
{
|
||||||
|
return (rest_resource_flags_t)(1 << (((coap_packet_t *)packet)->code - 1));
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_set_rest_status(void *packet, unsigned int code)
|
||||||
|
{
|
||||||
|
if (code <= 0xFF)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->code = (uint8_t) code;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Server part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* The discover resource should be included when using CoAP. */
|
||||||
|
RESOURCE(well_known_core, METHOD_GET, ".well-known/core", "");
|
||||||
|
void
|
||||||
|
well_known_core_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
|
{
|
||||||
|
/* Response might be NULL for non-confirmable requests. */
|
||||||
|
if (response)
|
||||||
|
{
|
||||||
|
size_t strpos = 0;
|
||||||
|
size_t bufpos = 0;
|
||||||
|
resource_t* resource = NULL;
|
||||||
|
|
||||||
|
for (resource = (resource_t*)list_head(rest_get_resources()); resource; resource = resource->next)
|
||||||
|
{
|
||||||
|
strpos += snprintf((char *) buffer + bufpos, REST_MAX_CHUNK_SIZE - bufpos + 1,
|
||||||
|
"</%s>%s%s%s",
|
||||||
|
resource->url,
|
||||||
|
resource->attributes[0] ? ";" : "",
|
||||||
|
resource->attributes,
|
||||||
|
resource->next ? "," : "" );
|
||||||
|
|
||||||
|
PRINTF("discover: %s\n", resource->url);
|
||||||
|
|
||||||
|
if (strpos <= *offset)
|
||||||
|
{
|
||||||
|
/* Discard output before current block */
|
||||||
|
PRINTF(" if %d <= %ld B\n", strpos, *offset);
|
||||||
|
PRINTF(" %s\n", buffer);
|
||||||
|
bufpos = 0;
|
||||||
|
}
|
||||||
|
else /* (strpos > *offset) */
|
||||||
|
{
|
||||||
|
/* output partly in block */
|
||||||
|
size_t len = MIN(strpos - *offset, preferred_size);
|
||||||
|
|
||||||
|
PRINTF(" el %d/%d @ %ld B\n", len, preferred_size, *offset);
|
||||||
|
|
||||||
|
/* Block might start in the middle of the output; align with buffer start. */
|
||||||
|
if (bufpos == 0)
|
||||||
|
{
|
||||||
|
memmove(buffer, buffer+strlen((char *)buffer)-strpos+*offset, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
bufpos = len;
|
||||||
|
PRINTF(" %s\n", buffer);
|
||||||
|
|
||||||
|
if (bufpos >= preferred_size)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bufpos>0) {
|
||||||
|
coap_set_payload(response, buffer, bufpos );
|
||||||
|
coap_set_header_content_type(response, APPLICATION_LINK_FORMAT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_set_rest_status(response, BAD_REQUEST_400);
|
||||||
|
coap_set_payload(response, (uint8_t*)"Block out of scope", 18);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource==NULL) {
|
||||||
|
*offset = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*offset += bufpos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PROCESS_THREAD(coap_receiver, ev, data)
|
||||||
|
{
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
PRINTF("Starting CoAP-03 receiver...\n");
|
||||||
|
|
||||||
|
rest_activate_resource(&resource_well_known_core);
|
||||||
|
|
||||||
|
coap_register_as_transaction_handler();
|
||||||
|
coap_init_connection(SERVER_LISTEN_PORT);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PROCESS_YIELD();
|
||||||
|
|
||||||
|
if(ev == tcpip_event) {
|
||||||
|
handle_incoming_data();
|
||||||
|
} else if (ev == PROCESS_EVENT_TIMER) {
|
||||||
|
/* retransmissions are handled here */
|
||||||
|
coap_check_transactions();
|
||||||
|
}
|
||||||
|
} /* while (1) */
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Client part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void blocking_request_callback(void *callback_data, void *response) {
|
||||||
|
struct request_state_t *state = (struct request_state_t *) callback_data;
|
||||||
|
state->response = (coap_packet_t*) response;
|
||||||
|
process_poll(state->process);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
|
||||||
|
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
|
||||||
|
coap_packet_t *request,
|
||||||
|
blocking_response_handler request_callback)) {
|
||||||
|
PT_BEGIN(&state->pt);
|
||||||
|
|
||||||
|
static uint8_t more;
|
||||||
|
static uint32_t res_block;
|
||||||
|
static uint8_t block_error;
|
||||||
|
|
||||||
|
state->block_num = 0;
|
||||||
|
state->response = NULL;
|
||||||
|
state->process = PROCESS_CURRENT();
|
||||||
|
|
||||||
|
more = 0;
|
||||||
|
res_block = 0;
|
||||||
|
block_error = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
request->tid = coap_get_tid();
|
||||||
|
if ((state->transaction = coap_new_transaction(request->tid, remote_ipaddr, remote_port)))
|
||||||
|
{
|
||||||
|
state->transaction->callback = blocking_request_callback;
|
||||||
|
state->transaction->callback_data = state;
|
||||||
|
|
||||||
|
if (state->block_num>0)
|
||||||
|
{
|
||||||
|
coap_set_header_block(request, state->block_num, 0, REST_MAX_CHUNK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
state->transaction->packet_len = coap_serialize_message(request, state->transaction->packet);
|
||||||
|
|
||||||
|
coap_send_transaction(state->transaction);
|
||||||
|
PRINTF("Requested #%lu (TID %u)\n", state->block_num, request->tid);
|
||||||
|
|
||||||
|
PT_YIELD_UNTIL(&state->pt, ev == PROCESS_EVENT_POLL);
|
||||||
|
|
||||||
|
if (!state->response)
|
||||||
|
{
|
||||||
|
PRINTF("Server not responding\n");
|
||||||
|
PT_EXIT(&state->pt);
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_get_header_block(state->response, &res_block, &more, NULL, NULL);
|
||||||
|
|
||||||
|
PRINTF("Received #%lu%s (%u bytes)\n", res_block, more ? "+" : "", state->response->payload_len);
|
||||||
|
|
||||||
|
if (res_block==state->block_num)
|
||||||
|
{
|
||||||
|
request_callback(state->response);
|
||||||
|
++(state->block_num);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("WRONG BLOCK %lu/%lu\n", res_block, state->block_num);
|
||||||
|
++block_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("Could not allocate transaction buffer");
|
||||||
|
PT_EXIT(&state->pt);
|
||||||
|
}
|
||||||
|
} while (more && block_error<COAP_MAX_ATTEMPTS);
|
||||||
|
|
||||||
|
PT_END(&state->pt);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Engine Interface ----------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
const struct rest_implementation coap_rest_implementation = {
|
||||||
|
"CoAP-03",
|
||||||
|
|
||||||
|
coap_receiver_init,
|
||||||
|
coap_set_service_callback,
|
||||||
|
|
||||||
|
coap_get_header_uri_path,
|
||||||
|
coap_set_header_uri_path,
|
||||||
|
coap_get_rest_method,
|
||||||
|
coap_set_rest_status,
|
||||||
|
|
||||||
|
coap_get_header_content_type,
|
||||||
|
coap_set_header_content_type,
|
||||||
|
NULL,
|
||||||
|
coap_get_header_max_age,
|
||||||
|
coap_set_header_max_age,
|
||||||
|
coap_set_header_etag,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
coap_get_header_uri_host,
|
||||||
|
coap_set_header_location,
|
||||||
|
|
||||||
|
coap_get_payload,
|
||||||
|
coap_set_payload,
|
||||||
|
|
||||||
|
coap_get_header_uri_query,
|
||||||
|
coap_get_query_variable,
|
||||||
|
coap_get_post_variable,
|
||||||
|
|
||||||
|
coap_notify_observers,
|
||||||
|
(restful_post_handler) coap_observe_handler,
|
||||||
|
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
|
||||||
|
{
|
||||||
|
OK_200,
|
||||||
|
CREATED_201,
|
||||||
|
OK_200,
|
||||||
|
OK_200,
|
||||||
|
NOT_MODIFIED_304,
|
||||||
|
|
||||||
|
BAD_REQUEST_400,
|
||||||
|
METHOD_NOT_ALLOWED_405,
|
||||||
|
BAD_REQUEST_400,
|
||||||
|
METHOD_NOT_ALLOWED_405,
|
||||||
|
NOT_FOUND_404,
|
||||||
|
METHOD_NOT_ALLOWED_405,
|
||||||
|
BAD_REQUEST_400,
|
||||||
|
UNSUPPORTED_MADIA_TYPE_415,
|
||||||
|
|
||||||
|
INTERNAL_SERVER_ERROR_500,
|
||||||
|
CRITICAL_OPTION_NOT_SUPPORTED,
|
||||||
|
BAD_GATEWAY_502,
|
||||||
|
SERVICE_UNAVAILABLE_503,
|
||||||
|
GATEWAY_TIMEOUT_504,
|
||||||
|
INTERNAL_SERVER_ERROR_500
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
TEXT_PLAIN,
|
||||||
|
TEXT_XML,
|
||||||
|
TEXT_CSV,
|
||||||
|
TEXT_HTML,
|
||||||
|
IMAGE_GIF,
|
||||||
|
IMAGE_JPEG,
|
||||||
|
IMAGE_PNG,
|
||||||
|
IMAGE_TIFF,
|
||||||
|
AUDIO_RAW,
|
||||||
|
VIDEO_RAW,
|
||||||
|
APPLICATION_LINK_FORMAT,
|
||||||
|
APPLICATION_XML,
|
||||||
|
APPLICATION_OCTET_STREAM,
|
||||||
|
APPLICATION_RDF_XML,
|
||||||
|
APPLICATION_SOAP_XML,
|
||||||
|
APPLICATION_ATOM_XML,
|
||||||
|
APPLICATION_XMPP_XML,
|
||||||
|
APPLICATION_EXI,
|
||||||
|
APPLICATION_FASTINFOSET,
|
||||||
|
APPLICATION_SOAP_FASTINFOSET,
|
||||||
|
APPLICATION_JSON,
|
||||||
|
APPLICATION_OCTET_STREAM
|
||||||
|
}
|
||||||
|
};
|
91
apps/er-coap-03/er-coap-03-engine.h
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP implementation of the REST Engine
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_SERVER_H_
|
||||||
|
#define COAP_SERVER_H_
|
||||||
|
|
||||||
|
#if !defined(REST)
|
||||||
|
#error "Define REST to \"coap_rest_implementation\""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "er-coap-03.h"
|
||||||
|
#include "er-coap-03-transactions.h"
|
||||||
|
#include "er-coap-03-observing.h"
|
||||||
|
|
||||||
|
#include "pt.h"
|
||||||
|
|
||||||
|
/* Declare server process */
|
||||||
|
PROCESS_NAME(coap_receiver);
|
||||||
|
|
||||||
|
#define SERVER_LISTEN_PORT UIP_HTONS(COAP_SERVER_PORT)
|
||||||
|
|
||||||
|
typedef coap_packet_t rest_request_t;
|
||||||
|
typedef coap_packet_t rest_response_t;
|
||||||
|
|
||||||
|
extern const struct rest_implementation coap_rest_implementation;
|
||||||
|
|
||||||
|
void coap_receiver_init(void);
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Client part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
struct request_state_t {
|
||||||
|
struct pt pt;
|
||||||
|
struct process *process;
|
||||||
|
coap_transaction_t *transaction;
|
||||||
|
coap_packet_t *response;
|
||||||
|
uint32_t block_num;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*blocking_response_handler) (void* response);
|
||||||
|
|
||||||
|
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
|
||||||
|
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
|
||||||
|
coap_packet_t *request,
|
||||||
|
blocking_response_handler request_callback));
|
||||||
|
|
||||||
|
#define COAP_BLOCKING_REQUEST(server_addr, server_port, request, chunk_handler) \
|
||||||
|
static struct request_state_t request_state; \
|
||||||
|
PT_SPAWN(process_pt, &request_state.pt, \
|
||||||
|
coap_blocking_request(&request_state, ev, \
|
||||||
|
server_addr, server_port, \
|
||||||
|
request, chunk_handler) \
|
||||||
|
);
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#endif /* COAP_SERVER_H_ */
|
199
apps/er-coap-03/er-coap-03-observing.c
Normal file
|
@ -0,0 +1,199 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for observing resources
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "er-coap-03-observing.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
|
||||||
|
LIST(observers_list);
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
coap_observer_t *
|
||||||
|
coap_add_observer(const char *url, uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
coap_observer_t *o = memb_alloc(&observers_memb);
|
||||||
|
|
||||||
|
if (o)
|
||||||
|
{
|
||||||
|
o->url = url;
|
||||||
|
uip_ipaddr_copy(&o->addr, addr);
|
||||||
|
o->port = port;
|
||||||
|
o->token_len = token_len;
|
||||||
|
memcpy(o->token, token, token_len);
|
||||||
|
|
||||||
|
stimer_set(&o->refresh_timer, COAP_OBSERVING_REFRESH_INTERVAL);
|
||||||
|
|
||||||
|
PRINTF("Adding observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
||||||
|
list_add(observers_list, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_remove_observer(coap_observer_t *o)
|
||||||
|
{
|
||||||
|
PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
||||||
|
|
||||||
|
memb_free(&observers_memb, o);
|
||||||
|
list_remove(observers_list, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check Port %u\n", port);
|
||||||
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]);
|
||||||
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port && memcmp(obs->token, token, token_len)==0)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
if (obs->url==url) /* using RESOURCE url string as handle */
|
||||||
|
{
|
||||||
|
coap_transaction_t *transaction = NULL;
|
||||||
|
|
||||||
|
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */
|
||||||
|
|
||||||
|
if ( (transaction = coap_new_transaction(coap_get_tid(), &obs->addr, obs->port)) )
|
||||||
|
{
|
||||||
|
/* Use CON to check whether client is still there/interested after COAP_OBSERVING_REFRESH_INTERVAL. */
|
||||||
|
if (stimer_expired(&obs->refresh_timer))
|
||||||
|
{
|
||||||
|
PRINTF("Observing: Refresh client with CON\n");
|
||||||
|
type = COAP_TYPE_CON;
|
||||||
|
stimer_restart(&obs->refresh_timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prepare response */
|
||||||
|
coap_packet_t push[1]; /* This way the packet can be treated as pointer as usual. */
|
||||||
|
coap_init_message(push, (coap_message_type_t)type, OK_200, transaction->tid );
|
||||||
|
coap_set_header_observe(push, observe);
|
||||||
|
coap_set_header_token(push, obs->token, obs->token_len);
|
||||||
|
coap_set_payload(push, payload, payload_len);
|
||||||
|
transaction->packet_len = coap_serialize_message(push, transaction->packet);
|
||||||
|
|
||||||
|
PRINTF("Observing: Notify from /%s for ", url);
|
||||||
|
PRINT6ADDR(&obs->addr);
|
||||||
|
PRINTF(":%u\n", obs->port);
|
||||||
|
PRINTF(" %.*s\n", payload_len, payload);
|
||||||
|
|
||||||
|
coap_send_transaction(transaction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_observe_handler(resource_t *resource, void *request, void *response)
|
||||||
|
{
|
||||||
|
static char content[26];
|
||||||
|
|
||||||
|
if (response && ((coap_packet_t *)response)->code<128) /* response without error code */
|
||||||
|
{
|
||||||
|
if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_OBSERVE))
|
||||||
|
{
|
||||||
|
if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
if (coap_add_observer(resource->url, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, ((coap_packet_t *)request)->token, ((coap_packet_t *)request)->token_len))
|
||||||
|
{
|
||||||
|
coap_set_header_observe(response, 0);
|
||||||
|
coap_set_payload(response, (uint8_t *)content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((coap_packet_t *)response)->code = SERVICE_UNAVAILABLE_503;
|
||||||
|
coap_set_payload(response, (uint8_t *)"Too many observers", 18);
|
||||||
|
} /* if (added observer) */
|
||||||
|
}
|
||||||
|
else /* if (token) */
|
||||||
|
{
|
||||||
|
((coap_packet_t *)response)->code = TOKEN_OPTION_REQUIRED;
|
||||||
|
coap_set_payload(response, (uint8_t *)"Observing requires token", 24);
|
||||||
|
} /* if (token) */
|
||||||
|
}
|
||||||
|
else /* if (observe) */
|
||||||
|
{
|
||||||
|
/* Remove client if it is currently observing. */
|
||||||
|
coap_remove_observer_by_client(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport);
|
||||||
|
} /* if (observe) */
|
||||||
|
}
|
||||||
|
}
|
76
apps/er-coap-03/er-coap-03-observing.h
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for observing resources
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_OBSERVING_H_
|
||||||
|
#define COAP_OBSERVING_H_
|
||||||
|
|
||||||
|
#include "er-coap-03.h"
|
||||||
|
#include "er-coap-03-transactions.h"
|
||||||
|
|
||||||
|
#ifndef COAP_MAX_OBSERVERS
|
||||||
|
#define COAP_MAX_OBSERVERS 4
|
||||||
|
#endif /* COAP_MAX_OBSERVERS */
|
||||||
|
|
||||||
|
/* Interval in seconds in which NON notifies are changed to CON notifies to check client. */
|
||||||
|
#define COAP_OBSERVING_REFRESH_INTERVAL 60
|
||||||
|
|
||||||
|
#if COAP_MAX_OPEN_TRANSACTIONS<COAP_MAX_OBSERVERS
|
||||||
|
#warning "COAP_MAX_OPEN_TRANSACTIONS smaller than COAP_MAX_OBSERVERS: cannot handle CON notifications"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct coap_observer {
|
||||||
|
struct coap_observer *next; /* for LIST */
|
||||||
|
|
||||||
|
const char *url;
|
||||||
|
uip_ipaddr_t addr;
|
||||||
|
uint16_t port;
|
||||||
|
uint8_t token_len;
|
||||||
|
uint8_t token[COAP_TOKEN_LEN];
|
||||||
|
struct stimer refresh_timer;
|
||||||
|
} coap_observer_t;
|
||||||
|
|
||||||
|
list_t coap_get_observers(void);
|
||||||
|
coap_observer_t *coap_add_observer(const char *url, uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len);
|
||||||
|
void coap_remove_observer(coap_observer_t *o);
|
||||||
|
int coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port);
|
||||||
|
int coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len);
|
||||||
|
void coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len);
|
||||||
|
|
||||||
|
void coap_observe_handler(resource_t *resource, void *request, void *response);
|
||||||
|
|
||||||
|
#endif /* COAP_OBSERVING_H_ */
|
177
apps/er-coap-03/er-coap-03-transactions.c
Normal file
|
@ -0,0 +1,177 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for reliable transport
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "er-coap-03-transactions.h"
|
||||||
|
#include "er-coap-03-observing.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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
|
||||||
|
LIST(transactions_list);
|
||||||
|
|
||||||
|
|
||||||
|
static struct process *transaction_handler_process = NULL;
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_register_as_transaction_handler()
|
||||||
|
{
|
||||||
|
transaction_handler_process = PROCESS_CURRENT();
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_transaction_t *
|
||||||
|
coap_new_transaction(uint16_t tid, uip_ipaddr_t *addr, uint16_t port)
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = memb_alloc(&transactions_memb);
|
||||||
|
|
||||||
|
if (t)
|
||||||
|
{
|
||||||
|
t->tid = tid;
|
||||||
|
t->retrans_counter = 0;
|
||||||
|
|
||||||
|
/* save client address */
|
||||||
|
uip_ipaddr_copy(&t->addr, addr);
|
||||||
|
t->port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_send_transaction(coap_transaction_t *t)
|
||||||
|
{
|
||||||
|
PRINTF("Sending transaction %u\n", t->tid);
|
||||||
|
|
||||||
|
coap_send_message(&t->addr, t->port, t->packet, t->packet_len);
|
||||||
|
|
||||||
|
if (COAP_TYPE_CON==((COAP_HEADER_TYPE_MASK & t->packet[0])>>COAP_HEADER_TYPE_POSITION))
|
||||||
|
{
|
||||||
|
if (t->retrans_counter<COAP_MAX_RETRANSMIT)
|
||||||
|
{
|
||||||
|
PRINTF("Keeping transaction %u\n", t->tid);
|
||||||
|
|
||||||
|
/*FIXME hack, maybe there is a better way, but avoid posting everything to the process */
|
||||||
|
struct process *process_actual = PROCESS_CURRENT();
|
||||||
|
process_current = transaction_handler_process;
|
||||||
|
etimer_set(&t->retrans_timer, CLOCK_SECOND * COAP_RESPONSE_TIMEOUT * (1<<(t->retrans_counter)));
|
||||||
|
process_current = process_actual;
|
||||||
|
|
||||||
|
list_add(transactions_list, t); /* list itself makes sure same element is not added twice */
|
||||||
|
|
||||||
|
t = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* timeout */
|
||||||
|
PRINTF("Timeout\n");
|
||||||
|
restful_response_handler callback = t->callback;
|
||||||
|
void *callback_data = t->callback_data;
|
||||||
|
|
||||||
|
/* handle observers */
|
||||||
|
coap_remove_observer_by_client(&t->addr, t->port);
|
||||||
|
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback(callback_data, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_clear_transaction(coap_transaction_t *t)
|
||||||
|
{
|
||||||
|
if (t)
|
||||||
|
{
|
||||||
|
PRINTF("Freeing transaction %u: %p\n", t->tid, t);
|
||||||
|
|
||||||
|
etimer_stop(&t->retrans_timer);
|
||||||
|
list_remove(transactions_list, t);
|
||||||
|
memb_free(&transactions_memb, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_transaction_t *
|
||||||
|
coap_get_transaction_by_tid(uint16_t tid)
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = NULL;
|
||||||
|
|
||||||
|
for (t = (coap_transaction_t*)list_head(transactions_list); t; t = t->next)
|
||||||
|
{
|
||||||
|
if (t->tid==tid)
|
||||||
|
{
|
||||||
|
PRINTF("Found transaction for TID %u: %p\n", t->tid, t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_check_transactions()
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = NULL;
|
||||||
|
|
||||||
|
for (t = (coap_transaction_t*)list_head(transactions_list); t; t = t->next)
|
||||||
|
{
|
||||||
|
if (etimer_expired(&t->retrans_timer))
|
||||||
|
{
|
||||||
|
++(t->retrans_counter);
|
||||||
|
PRINTF("Retransmitting %u (%u)\n", t->tid, t->retrans_counter);
|
||||||
|
coap_send_transaction(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
78
apps/er-coap-03/er-coap-03-transactions.h
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for reliable transport
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_TRANSACTIONS_H_
|
||||||
|
#define COAP_TRANSACTIONS_H_
|
||||||
|
|
||||||
|
#include "er-coap-03.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The number of concurrent messages that can be stored for retransmission in the transaction layer.
|
||||||
|
*/
|
||||||
|
#ifndef COAP_MAX_OPEN_TRANSACTIONS
|
||||||
|
#define COAP_MAX_OPEN_TRANSACTIONS 4
|
||||||
|
#endif /* COAP_MAX_OPEN_TRANSACTIONS */
|
||||||
|
|
||||||
|
/* container for transactions with message buffer and retransmission info */
|
||||||
|
typedef struct coap_transaction {
|
||||||
|
struct coap_transaction *next; /* for LIST */
|
||||||
|
|
||||||
|
uint16_t tid;
|
||||||
|
struct etimer retrans_timer;
|
||||||
|
uint8_t retrans_counter;
|
||||||
|
|
||||||
|
uip_ipaddr_t addr;
|
||||||
|
uint16_t port;
|
||||||
|
|
||||||
|
restful_response_handler callback;
|
||||||
|
void *callback_data;
|
||||||
|
|
||||||
|
uint16_t packet_len;
|
||||||
|
uint8_t packet[COAP_MAX_PACKET_SIZE+1]; /* +1 for the terminating '\0' to simply and savely use snprintf(buf, len+1, "", ...) in the resource handler. */
|
||||||
|
} coap_transaction_t;
|
||||||
|
|
||||||
|
void coap_register_as_transaction_handler();
|
||||||
|
|
||||||
|
coap_transaction_t *coap_new_transaction(uint16_t tid, uip_ipaddr_t *addr, uint16_t port);
|
||||||
|
void coap_send_transaction(coap_transaction_t *t);
|
||||||
|
void coap_clear_transaction(coap_transaction_t *t);
|
||||||
|
coap_transaction_t *coap_get_transaction_by_tid(uint16_t tid);
|
||||||
|
|
||||||
|
void coap_check_transactions();
|
||||||
|
|
||||||
|
#endif /* COAP_TRANSACTIONS_H_ */
|
769
apps/er-coap-03/er-coap-03.c
Normal file
|
@ -0,0 +1,769 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An implementation of the Constrained Application Protocol (draft 03)
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONTIKI_TARGET_NETSIM
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#else
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "er-coap-03.h"
|
||||||
|
#include "er-coap-03-transactions.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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
static struct uip_udp_conn *udp_conn = NULL;
|
||||||
|
static uint16_t current_tid = 0;
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- LOCAL HELP FUNCTIONS ------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
uint16_t
|
||||||
|
log_2(uint16_t value)
|
||||||
|
{
|
||||||
|
uint16_t result = 0;
|
||||||
|
do {
|
||||||
|
value = value >> 1;
|
||||||
|
result++;
|
||||||
|
} while (value);
|
||||||
|
|
||||||
|
return result ? result - 1 : result;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
uint32_t
|
||||||
|
bytes_2_uint32(uint8_t *bytes, uint16_t length)
|
||||||
|
{
|
||||||
|
uint32_t var = 0;
|
||||||
|
int i = 0;
|
||||||
|
while (i<length)
|
||||||
|
{
|
||||||
|
var <<= 8;
|
||||||
|
var |= 0xFF & bytes[i++];
|
||||||
|
}
|
||||||
|
return var;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* Unused in coap-03.
|
||||||
|
static
|
||||||
|
int
|
||||||
|
uint16_2_bytes(uint8_t *bytes, uint16_t var)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
if (0xFF00 & var) bytes[i++] = var>>8;
|
||||||
|
bytes[i++] = 0xFF & var;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
int
|
||||||
|
uint32_2_bytes(uint8_t *bytes, uint32_t var)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
if (0xFF000000 & var) bytes[i++] = var>>24;
|
||||||
|
if (0xFF0000 & var) bytes[i++] = var>>16;
|
||||||
|
if (0xFF00 & var) bytes[i++] = var>>8;
|
||||||
|
bytes[i++] = 0xFF & var;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
int
|
||||||
|
coap_get_variable(const char *buffer, size_t length, const char *name, const char **output)
|
||||||
|
{
|
||||||
|
const char *start = NULL;
|
||||||
|
const char *end = NULL;
|
||||||
|
const char *value_end = NULL;
|
||||||
|
size_t name_len = 0;
|
||||||
|
|
||||||
|
/*initialize the output buffer first*/
|
||||||
|
*output = 0;
|
||||||
|
|
||||||
|
name_len = strlen(name);
|
||||||
|
end = buffer + length;
|
||||||
|
|
||||||
|
for (start = buffer; start + name_len < end; ++start){
|
||||||
|
if ((start == buffer || start[-1] == '&') && start[name_len] == '=' &&
|
||||||
|
strncmp(name, start, name_len)==0) {
|
||||||
|
|
||||||
|
/* Point start to variable value */
|
||||||
|
start += name_len + 1;
|
||||||
|
|
||||||
|
/* Point end to the end of the value */
|
||||||
|
value_end = (const char *) memchr(start, '&', end - start);
|
||||||
|
if (value_end == NULL) {
|
||||||
|
value_end = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
*output = start;
|
||||||
|
|
||||||
|
return (value_end - start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- MEASSAGE SENDING ----------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
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));
|
||||||
|
|
||||||
|
/* Initialize transaction ID. */
|
||||||
|
current_tid = random_rand();
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
uint16_t
|
||||||
|
coap_get_tid()
|
||||||
|
{
|
||||||
|
++current_tid;
|
||||||
|
PRINTF("Get TID %u\n", current_tid);
|
||||||
|
return current_tid;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
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------\n Length: %u\n -----------------------\n", length);
|
||||||
|
|
||||||
|
/* Restore server connection to allow data from any node */
|
||||||
|
memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
|
||||||
|
udp_conn->rport = 0;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- MEASSAGE PROCESSING -------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid)
|
||||||
|
{
|
||||||
|
memset(packet, 0, sizeof(coap_packet_t));
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->type = type;
|
||||||
|
((coap_packet_t *)packet)->code = code;
|
||||||
|
((coap_packet_t *)packet)->tid = tid;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_serialize_message(void *packet, uint8_t *buffer)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->buffer = buffer;
|
||||||
|
((coap_packet_t *)packet)->version = 1;
|
||||||
|
((coap_packet_t *)packet)->option_count = 0;
|
||||||
|
|
||||||
|
/* serialize options */
|
||||||
|
uint8_t *option = ((coap_packet_t *)packet)->buffer + COAP_HEADER_LEN;
|
||||||
|
size_t option_len = 0;
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_CONTENT_TYPE)) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = 1;
|
||||||
|
((coap_header_option_t *)option)->s.length = 1;
|
||||||
|
*(++option) = ((coap_packet_t *)packet)->content_type;
|
||||||
|
PRINTF("OPTION %u (type %u, len 1, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_CONTENT_TYPE, COAP_OPTION_CONTENT_TYPE - index);
|
||||||
|
PRINTF("Content-Type [%u]\n", ((coap_packet_t *)packet)->content_type);
|
||||||
|
index = COAP_OPTION_CONTENT_TYPE;
|
||||||
|
option += 1;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_MAX_AGE)) {
|
||||||
|
option_len = uint32_2_bytes(option+1, ((coap_packet_t *)packet)->max_age);
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_MAX_AGE - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = option_len;
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_MAX_AGE, option_len, COAP_OPTION_MAX_AGE - index);
|
||||||
|
PRINTF("Max-Age [%lu]\n", ((coap_packet_t *)packet)->max_age);
|
||||||
|
index = COAP_OPTION_MAX_AGE;
|
||||||
|
option += 1 + option_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_ETAG)) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_ETAG - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = ((coap_packet_t *)packet)->etag_len;
|
||||||
|
memcpy(++option, ((coap_packet_t *)packet)->etag, ((coap_packet_t *)packet)->etag_len);
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_ETAG, ((coap_packet_t *)packet)->etag_len, COAP_OPTION_ETAG - index);
|
||||||
|
PRINTF("ETag %u [0x%02X", ((coap_packet_t *)packet)->etag_len, ((coap_packet_t *)packet)->etag[0]); /*FIXME always prints 4 bytes */
|
||||||
|
PRINTF("%02X", ((coap_packet_t *)packet)->etag[1]);
|
||||||
|
PRINTF("%02X", ((coap_packet_t *)packet)->etag[2]);
|
||||||
|
PRINTF("%02X", ((coap_packet_t *)packet)->etag[3]);
|
||||||
|
PRINTF("]\n");
|
||||||
|
index = COAP_OPTION_ETAG;
|
||||||
|
option += ((coap_packet_t *)packet)->etag_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST)) {
|
||||||
|
if (((coap_packet_t *)packet)->uri_host_len<15) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_URI_HOST - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = ((coap_packet_t *)packet)->uri_host_len;
|
||||||
|
option += 1;
|
||||||
|
} else {
|
||||||
|
((coap_header_option_t *)option)->l.delta = COAP_OPTION_URI_HOST - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = 15;
|
||||||
|
((coap_header_option_t *)option)->l.length = ((coap_packet_t *)packet)->uri_host_len - 15;
|
||||||
|
option += 2;
|
||||||
|
}
|
||||||
|
memcpy(option, ((coap_packet_t *)packet)->uri_host, ((coap_packet_t *)packet)->uri_host_len);
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_URI_HOST, ((coap_packet_t *)packet)->uri_host_len, COAP_OPTION_URI_HOST - index);
|
||||||
|
PRINTF("Uri-Auth [%.*s]\n", ((coap_packet_t *)packet)->uri_host_len, ((coap_packet_t *)packet)->uri_host);
|
||||||
|
index = COAP_OPTION_URI_HOST;
|
||||||
|
option += ((coap_packet_t *)packet)->uri_host_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH)) {
|
||||||
|
if (((coap_packet_t *)packet)->location_path_len<15) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_LOCATION_PATH - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = ((coap_packet_t *)packet)->location_path_len;
|
||||||
|
option += 1;
|
||||||
|
} else {
|
||||||
|
((coap_header_option_t *)option)->l.delta = COAP_OPTION_LOCATION_PATH - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = 15;
|
||||||
|
((coap_header_option_t *)option)->l.length = ((coap_packet_t *)packet)->location_path_len - 15;
|
||||||
|
option += 2;
|
||||||
|
}
|
||||||
|
memcpy(option, ((coap_packet_t *)packet)->location_path, ((coap_packet_t *)packet)->location_path_len);
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_LOCATION_PATH, ((coap_packet_t *)packet)->location_path_len, COAP_OPTION_LOCATION_PATH - index);
|
||||||
|
PRINTF("Location [%.*s]\n", ((coap_packet_t *)packet)->location_path_len, ((coap_packet_t *)packet)->location_path);
|
||||||
|
index = COAP_OPTION_LOCATION_PATH;
|
||||||
|
option += ((coap_packet_t *)packet)->location_path_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH)) {
|
||||||
|
if (((coap_packet_t *)packet)->uri_path_len<15) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_URI_PATH - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = ((coap_packet_t *)packet)->uri_path_len;
|
||||||
|
option += 1;
|
||||||
|
} else {
|
||||||
|
((coap_header_option_t *)option)->l.delta = COAP_OPTION_URI_PATH - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = 15;
|
||||||
|
((coap_header_option_t *)option)->l.length = ((coap_packet_t *)packet)->uri_path_len - 15;
|
||||||
|
option += 2;
|
||||||
|
}
|
||||||
|
memcpy(option, ((coap_packet_t *)packet)->uri_path, ((coap_packet_t *)packet)->uri_path_len);
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_URI_PATH, ((coap_packet_t *)packet)->uri_path_len, COAP_OPTION_URI_PATH - index);
|
||||||
|
PRINTF("Uri-Path [%.*s]\n", ((coap_packet_t *)packet)->uri_path_len, ((coap_packet_t *)packet)->uri_path);
|
||||||
|
index = COAP_OPTION_URI_PATH;
|
||||||
|
option += ((coap_packet_t *)packet)->uri_path_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_OBSERVE)) {
|
||||||
|
option_len = uint32_2_bytes(option+1, ((coap_packet_t *)packet)->observe);
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_OBSERVE - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = option_len;
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_OBSERVE, option_len, COAP_OPTION_OBSERVE - index);
|
||||||
|
PRINTF("Observe [%lu]\n", ((coap_packet_t *)packet)->observe);
|
||||||
|
index = COAP_OPTION_OBSERVE;
|
||||||
|
option += 1 + option_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_TOKEN)) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_TOKEN - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = ((coap_packet_t *)packet)->token_len;
|
||||||
|
memcpy(++option, ((coap_packet_t *)packet)->token, ((coap_packet_t *)packet)->token_len);
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_TOKEN, ((coap_packet_t *)packet)->token_len, COAP_OPTION_TOKEN - index);
|
||||||
|
PRINTF("Token %u [0x%02X%02X]\n", ((coap_packet_t *)packet)->token_len, ((coap_packet_t *)packet)->token[0], ((coap_packet_t *)packet)->token[1]); /*FIXME always prints 2 bytes */
|
||||||
|
index = COAP_OPTION_TOKEN;
|
||||||
|
option += ((coap_packet_t *)packet)->token_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK)) {
|
||||||
|
uint32_t block = ((coap_packet_t *)packet)->block_num << 4;
|
||||||
|
if (((coap_packet_t *)packet)->block_more) block |= 0x8;
|
||||||
|
block |= 0xF & log_2(((coap_packet_t *)packet)->block_size/16);
|
||||||
|
option_len = uint32_2_bytes(option+1, block);
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_BLOCK - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = option_len;
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_BLOCK, option_len, COAP_OPTION_BLOCK - index);
|
||||||
|
PRINTF("Block [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block_num, ((coap_packet_t *)packet)->block_more ? "+" : "", ((coap_packet_t *)packet)->block_size);
|
||||||
|
index = COAP_OPTION_BLOCK;
|
||||||
|
option += 1 + option_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY)) {
|
||||||
|
if (((coap_packet_t *)packet)->uri_query_len<15) {
|
||||||
|
((coap_header_option_t *)option)->s.delta = COAP_OPTION_URI_QUERY - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = ((coap_packet_t *)packet)->uri_query_len;
|
||||||
|
option += 1;
|
||||||
|
} else {
|
||||||
|
((coap_header_option_t *)option)->l.delta = COAP_OPTION_URI_QUERY - index;
|
||||||
|
((coap_header_option_t *)option)->s.length = 15;
|
||||||
|
((coap_header_option_t *)option)->l.length = ((coap_packet_t *)packet)->uri_query_len - 15;
|
||||||
|
option += 2;
|
||||||
|
}
|
||||||
|
memcpy(option, ((coap_packet_t *)packet)->uri_query, ((coap_packet_t *)packet)->uri_query_len);
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", ((coap_packet_t *)packet)->option_count, COAP_OPTION_URI_QUERY, ((coap_packet_t *)packet)->uri_query_len, COAP_OPTION_URI_QUERY - index);
|
||||||
|
PRINTF("Uri-Query [%.*s]\n", ((coap_packet_t *)packet)->uri_query_len, ((coap_packet_t *)packet)->uri_query);
|
||||||
|
index = COAP_OPTION_URI_QUERY;
|
||||||
|
option += ((coap_packet_t *)packet)->uri_query_len;
|
||||||
|
++(((coap_packet_t *)packet)->option_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pack payload */
|
||||||
|
if ((option - ((coap_packet_t *)packet)->buffer)<=COAP_MAX_HEADER_SIZE)
|
||||||
|
{
|
||||||
|
memmove(option, ((coap_packet_t *)packet)->payload, ((coap_packet_t *)packet)->payload_len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* An error occured. Caller must check for !=0. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set header fields */
|
||||||
|
((coap_packet_t *)packet)->buffer[0] = 0x00;
|
||||||
|
((coap_packet_t *)packet)->buffer[0] |= COAP_HEADER_VERSION_MASK & (((coap_packet_t *)packet)->version)<<COAP_HEADER_VERSION_POSITION;
|
||||||
|
((coap_packet_t *)packet)->buffer[0] |= COAP_HEADER_TYPE_MASK & (((coap_packet_t *)packet)->type)<<COAP_HEADER_TYPE_POSITION;
|
||||||
|
((coap_packet_t *)packet)->buffer[0] |= COAP_HEADER_OPTION_COUNT_MASK & (((coap_packet_t *)packet)->option_count)<<COAP_HEADER_OPTION_COUNT_POSITION;
|
||||||
|
((coap_packet_t *)packet)->buffer[1] = ((coap_packet_t *)packet)->code;
|
||||||
|
((coap_packet_t *)packet)->buffer[2] = 0xFF & (((coap_packet_t *)packet)->tid)>>8;
|
||||||
|
((coap_packet_t *)packet)->buffer[3] = 0xFF & ((coap_packet_t *)packet)->tid;
|
||||||
|
|
||||||
|
PRINTF("Serialized %u options, header len %u, payload len %u\n", ((coap_packet_t *)packet)->option_count, option - ((coap_packet_t *)packet)->buffer, ((coap_packet_t *)packet)->payload_len);
|
||||||
|
|
||||||
|
return (option - ((coap_packet_t *)packet)->buffer) + ((coap_packet_t *)packet)->payload_len; /* packet length */
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
error_t
|
||||||
|
coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
|
||||||
|
{
|
||||||
|
/* Initialize packet */
|
||||||
|
memset(packet, 0, sizeof(coap_packet_t));
|
||||||
|
|
||||||
|
/* pointer to packet bytes */
|
||||||
|
((coap_packet_t *)packet)->buffer = data;
|
||||||
|
|
||||||
|
/* parse header fields */
|
||||||
|
((coap_packet_t *)packet)->version = (COAP_HEADER_VERSION_MASK & ((coap_packet_t *)packet)->buffer[0])>>COAP_HEADER_VERSION_POSITION;
|
||||||
|
((coap_packet_t *)packet)->type = (COAP_HEADER_TYPE_MASK & ((coap_packet_t *)packet)->buffer[0])>>COAP_HEADER_TYPE_POSITION;
|
||||||
|
((coap_packet_t *)packet)->option_count = (COAP_HEADER_OPTION_COUNT_MASK & ((coap_packet_t *)packet)->buffer[0])>>COAP_HEADER_OPTION_COUNT_POSITION;
|
||||||
|
((coap_packet_t *)packet)->code = ((coap_packet_t *)packet)->buffer[1];
|
||||||
|
((coap_packet_t *)packet)->tid = ((coap_packet_t *)packet)->buffer[2]<<8 | ((coap_packet_t *)packet)->buffer[3];
|
||||||
|
|
||||||
|
/* parse options */
|
||||||
|
((coap_packet_t *)packet)->options = 0x0000;
|
||||||
|
coap_header_option_t *current_option = (coap_header_option_t *) (data + COAP_HEADER_LEN);
|
||||||
|
|
||||||
|
if (((coap_packet_t *)packet)->option_count) {
|
||||||
|
uint8_t option_index = 0;
|
||||||
|
uint8_t option_type = 0;
|
||||||
|
|
||||||
|
uint16_t option_len = 0;
|
||||||
|
uint8_t *option_data = NULL;
|
||||||
|
|
||||||
|
uint8_t *last_option = NULL;
|
||||||
|
|
||||||
|
for (option_index=0; option_index < ((coap_packet_t *)packet)->option_count; ++option_index) {
|
||||||
|
|
||||||
|
option_type += current_option->s.delta;
|
||||||
|
|
||||||
|
if (current_option->s.length<15) {
|
||||||
|
option_len = current_option->s.length;
|
||||||
|
option_data = ((uint8_t *) current_option) + 1;
|
||||||
|
} else {
|
||||||
|
option_len = current_option->l.length + 15;
|
||||||
|
option_data = ((uint8_t *) current_option) + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
PRINTF("OPTION %u (type %u, len %u, delta %u): ", option_index, option_type, option_len, current_option->s.delta);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, option_type);
|
||||||
|
|
||||||
|
switch (option_type) {
|
||||||
|
case COAP_OPTION_CONTENT_TYPE:
|
||||||
|
((coap_packet_t *)packet)->content_type = option_data[0];
|
||||||
|
PRINTF("Content-Type [%u]\n", ((coap_packet_t *)packet)->content_type);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_MAX_AGE:
|
||||||
|
((coap_packet_t *)packet)->max_age = bytes_2_uint32(option_data, option_len);
|
||||||
|
PRINTF("Max-Age [%lu]\n", ((coap_packet_t *)packet)->max_age);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_ETAG:
|
||||||
|
((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, option_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->etag, option_data, ((coap_packet_t *)packet)->etag_len);
|
||||||
|
PRINTF("ETag %u [0x%02X", ((coap_packet_t *)packet)->etag_len, ((coap_packet_t *)packet)->etag[0]); /*FIXME always prints 4 bytes */
|
||||||
|
PRINTF("%02X", ((coap_packet_t *)packet)->etag[1]);
|
||||||
|
PRINTF("%02X", ((coap_packet_t *)packet)->etag[2]);
|
||||||
|
PRINTF("%02X", ((coap_packet_t *)packet)->etag[3]);
|
||||||
|
PRINTF("]\n");
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_HOST:
|
||||||
|
((coap_packet_t *)packet)->uri_host = (char *) option_data;
|
||||||
|
((coap_packet_t *)packet)->uri_host_len = option_len;
|
||||||
|
PRINTF("Uri-Auth [%.*s]\n", ((coap_packet_t *)packet)->uri_host_len, ((coap_packet_t *)packet)->uri_host);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_LOCATION_PATH:
|
||||||
|
((coap_packet_t *)packet)->location_path = (char *) option_data;
|
||||||
|
((coap_packet_t *)packet)->location_path_len = option_len;
|
||||||
|
PRINTF("Location [%.*s]\n", ((coap_packet_t *)packet)->location_path_len, ((coap_packet_t *)packet)->location_path);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_PATH:
|
||||||
|
((coap_packet_t *)packet)->uri_path = (char *) option_data;
|
||||||
|
((coap_packet_t *)packet)->uri_path_len = option_len;
|
||||||
|
PRINTF("Uri-Path [%.*s]\n", ((coap_packet_t *)packet)->uri_path_len, ((coap_packet_t *)packet)->uri_path);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_OBSERVE:
|
||||||
|
((coap_packet_t *)packet)->observe = bytes_2_uint32(option_data, option_len);
|
||||||
|
PRINTF("Observe [%lu]\n", ((coap_packet_t *)packet)->observe);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_TOKEN:
|
||||||
|
((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, option_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->token, option_data, ((coap_packet_t *)packet)->token_len);
|
||||||
|
PRINTF("Token %u [0x%02X%02X]\n", ((coap_packet_t *)packet)->token_len, ((coap_packet_t *)packet)->token[0], ((coap_packet_t *)packet)->token[1]); /*FIXME always prints 2 bytes */
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_BLOCK:
|
||||||
|
((coap_packet_t *)packet)->block_num = bytes_2_uint32(option_data, option_len);
|
||||||
|
((coap_packet_t *)packet)->block_more = (((coap_packet_t *)packet)->block_num & 0x08)>>3;
|
||||||
|
((coap_packet_t *)packet)->block_size = 16 << (((coap_packet_t *)packet)->block_num & 0x07);
|
||||||
|
((coap_packet_t *)packet)->block_offset = (((coap_packet_t *)packet)->block_num & ~0x0F)<<(((coap_packet_t *)packet)->block_num & 0x07);
|
||||||
|
((coap_packet_t *)packet)->block_num >>= 4;
|
||||||
|
PRINTF("Block [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block_num, ((coap_packet_t *)packet)->block_more ? "+" : "", ((coap_packet_t *)packet)->block_size);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_NOOP:
|
||||||
|
PRINTF("Noop-Fencepost\n");
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_QUERY:
|
||||||
|
((coap_packet_t *)packet)->uri_query = (char *) option_data;
|
||||||
|
((coap_packet_t *)packet)->uri_query_len = option_len;
|
||||||
|
PRINTF("Uri-Query [%.*s]\n", ((coap_packet_t *)packet)->uri_query_len, ((coap_packet_t *)packet)->uri_query);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PRINTF("unknown (%u)\n", option_type);
|
||||||
|
if (option_type & 1)
|
||||||
|
{
|
||||||
|
return UNKNOWN_CRITICAL_OPTION;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* terminate strings where possible */
|
||||||
|
if (last_option) {
|
||||||
|
last_option[0] = 0x00;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_option = (uint8_t *) current_option;
|
||||||
|
current_option = (coap_header_option_t *) (option_data+option_len);
|
||||||
|
} /* for () */
|
||||||
|
} /* if (oc) */
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->payload = (uint8_t *) current_option;
|
||||||
|
((coap_packet_t *)packet)->payload_len = data_len - (((coap_packet_t *)packet)->payload - data);
|
||||||
|
|
||||||
|
return NO_ERROR;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- REST FRAMEWORK FUNCTIONS --------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_query_variable(void *packet, const char *name, const char **output)
|
||||||
|
{
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY)) {
|
||||||
|
return coap_get_variable(((coap_packet_t *)packet)->uri_query, ((coap_packet_t *)packet)->uri_query_len, name, output);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_get_post_variable(void *packet, const char *name, const char **output)
|
||||||
|
{
|
||||||
|
if (((coap_packet_t *)packet)->payload_len) {
|
||||||
|
return coap_get_variable((const char *)((coap_packet_t *)packet)->payload, ((coap_packet_t *)packet)->payload_len, name, output);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- HEADER OPTION GETTERS AND SETTERS -----------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
unsigned int
|
||||||
|
coap_get_header_content_type(void *packet)
|
||||||
|
{
|
||||||
|
return ((coap_packet_t *)packet)->content_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_content_type(void *packet, unsigned int content_type)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->content_type = (coap_content_type_t) content_type;
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_CONTENT_TYPE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_max_age(void *packet, uint32_t *age)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_MAX_AGE)) {
|
||||||
|
*age = COAP_DEFAULT_MAX_AGE;
|
||||||
|
} else {
|
||||||
|
*age = ((coap_packet_t *)packet)->max_age;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_max_age(void *packet, uint32_t age)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->max_age = age;
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_MAX_AGE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_etag(void *packet, const uint8_t **etag)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_ETAG)) return 0;
|
||||||
|
|
||||||
|
*etag = ((coap_packet_t *)packet)->etag;
|
||||||
|
return ((coap_packet_t *)packet)->etag_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, etag_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->etag, etag, ((coap_packet_t *)packet)->etag_len);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_ETAG);
|
||||||
|
return ((coap_packet_t *)packet)->etag_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_uri_host(void *packet, const char **host)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST)) return 0;
|
||||||
|
|
||||||
|
*host = ((coap_packet_t *)packet)->uri_host;
|
||||||
|
return ((coap_packet_t *)packet)->uri_host_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_uri_host(void *packet, char *host)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->uri_host = (char *) host;
|
||||||
|
((coap_packet_t *)packet)->uri_host_len = strlen(host);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST);
|
||||||
|
return ((coap_packet_t *)packet)->uri_host_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_location(void *packet, const char **uri)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH)) return 0;
|
||||||
|
|
||||||
|
*uri = ((coap_packet_t *)packet)->location_path;
|
||||||
|
return ((coap_packet_t *)packet)->location_path_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_location(void *packet, char *location)
|
||||||
|
{
|
||||||
|
while (location[0]=='/') ++location;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->location_path = (char *) location;
|
||||||
|
((coap_packet_t *)packet)->location_path_len = strlen(location);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH);
|
||||||
|
return ((coap_packet_t *)packet)->location_path_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_uri_path(void *packet, const char **path)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH)) return 0;
|
||||||
|
|
||||||
|
*path = ((coap_packet_t *)packet)->uri_path;
|
||||||
|
return ((coap_packet_t *)packet)->uri_path_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_uri_path(void *packet, char *path)
|
||||||
|
{
|
||||||
|
while (path[0]=='/') ++path;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->uri_path = (char *) path;
|
||||||
|
((coap_packet_t *)packet)->uri_path_len = strlen(path);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH);
|
||||||
|
return ((coap_packet_t *)packet)->uri_path_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_observe(void *packet, uint32_t *observe)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_OBSERVE)) return 0;
|
||||||
|
|
||||||
|
*observe = ((coap_packet_t *)packet)->observe;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_observe(void *packet, uint32_t observe)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->observe = observe;
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_OBSERVE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_token(void *packet, const uint8_t **token)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_TOKEN)) return 0;
|
||||||
|
|
||||||
|
*token = ((coap_packet_t *)packet)->token;
|
||||||
|
return ((coap_packet_t *)packet)->token_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_token(void *packet, uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, token_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->token, token, ((coap_packet_t *)packet)->token_len);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_TOKEN);
|
||||||
|
return ((coap_packet_t *)packet)->token_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_block(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK)) return 0;
|
||||||
|
|
||||||
|
/* pointers may be NULL to get only specific block parameters */
|
||||||
|
if (num!=NULL) *num = ((coap_packet_t *)packet)->block_num;
|
||||||
|
if (more!=NULL) *more = ((coap_packet_t *)packet)->block_more;
|
||||||
|
if (size!=NULL) *size = ((coap_packet_t *)packet)->block_size;
|
||||||
|
if (offset!=NULL) *offset = ((coap_packet_t *)packet)->block_offset;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_block(void *packet, uint32_t num, uint8_t more, uint16_t size)
|
||||||
|
{
|
||||||
|
if (size<16) return 0;
|
||||||
|
if (size>2048) return 0;
|
||||||
|
if (num>0x0FFFFF) return 0;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->block_num = num;
|
||||||
|
((coap_packet_t *)packet)->block_more = more;
|
||||||
|
((coap_packet_t *)packet)->block_size = size;
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_uri_query(void *packet, const char **query)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY)) return 0;
|
||||||
|
|
||||||
|
*query = ((coap_packet_t *)packet)->uri_query;
|
||||||
|
return ((coap_packet_t *)packet)->uri_query_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_uri_query(void *packet, char *query)
|
||||||
|
{
|
||||||
|
while (query[0]=='?') ++query;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->uri_query = query;
|
||||||
|
((coap_packet_t *)packet)->uri_query_len = strlen(query);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY);
|
||||||
|
return ((coap_packet_t *)packet)->uri_query_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- PAYLOAD -------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_payload(void *packet, const uint8_t **payload)
|
||||||
|
{
|
||||||
|
if (((coap_packet_t *)packet)->payload) {
|
||||||
|
*payload = ((coap_packet_t *)packet)->payload;
|
||||||
|
return ((coap_packet_t *)packet)->payload_len;
|
||||||
|
} else {
|
||||||
|
*payload = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_payload(void *packet, uint8_t *payload, size_t length)
|
||||||
|
{
|
||||||
|
PRINTF("setting payload (%u/%u)\n", length, REST_MAX_CHUNK_SIZE);
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->payload = payload;
|
||||||
|
((coap_packet_t *)packet)->payload_len = MIN(REST_MAX_CHUNK_SIZE, length);
|
||||||
|
|
||||||
|
return ((coap_packet_t *)packet)->payload_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
284
apps/er-coap-03/er-coap-03.h
Normal file
|
@ -0,0 +1,284 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An implementation of the Constrained Application Protocol (draft 03)
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_03_H_
|
||||||
|
#define COAP_03_H_
|
||||||
|
|
||||||
|
#include <stddef.h> /* for size_t */
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "erbium.h"
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_PORT 61616
|
||||||
|
|
||||||
|
#ifndef COAP_SERVER_PORT
|
||||||
|
#define COAP_SERVER_PORT COAP_DEFAULT_PORT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_MAX_AGE 60
|
||||||
|
#define COAP_RESPONSE_TIMEOUT 1
|
||||||
|
#define COAP_MAX_RETRANSMIT 5
|
||||||
|
|
||||||
|
#define COAP_HEADER_LEN 4 /* | oc:0xF0 type:0x0C version:0x03 | code | tid:0x00FF | tid:0xFF00 | */
|
||||||
|
#define COAP_ETAG_LEN 4 /* The maximum number of bytes for the ETag, which is 4 for coap-03 */
|
||||||
|
#define COAP_TOKEN_LEN 2 /* The maximum number of bytes for the ETag, which is 4 for coap-03 */
|
||||||
|
|
||||||
|
#define COAP_HEADER_VERSION_MASK 0xC0
|
||||||
|
#define COAP_HEADER_VERSION_POSITION 6
|
||||||
|
#define COAP_HEADER_TYPE_MASK 0x30
|
||||||
|
#define COAP_HEADER_TYPE_POSITION 4
|
||||||
|
#define COAP_HEADER_OPTION_COUNT_MASK 0x0F
|
||||||
|
#define COAP_HEADER_OPTION_COUNT_POSITION 0
|
||||||
|
|
||||||
|
#define COAP_HEADER_OPTION_DELTA_MASK 0xF0
|
||||||
|
#define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Conservative size limit, as not all options have to be set at the same time.
|
||||||
|
*/
|
||||||
|
/* Hdr CoT Age Tag Obs Tok Blo strings */
|
||||||
|
#define COAP_MAX_HEADER_SIZE (4 + 2 + 5 + 5 + 5 + 5 + 4 + 0)
|
||||||
|
#define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
|
||||||
|
/* 0/14 48 for IPv6 (28 for IPv4) */
|
||||||
|
#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN)
|
||||||
|
#error "UIP_CONF_BUFFER_SIZE too small for REST_MAX_CHUNK_SIZE"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Maximum number of failed request attempts before action
|
||||||
|
*/
|
||||||
|
#ifndef COAP_MAX_ATTEMPTS
|
||||||
|
#define COAP_MAX_ATTEMPTS 4
|
||||||
|
#endif /* COAP_MAX_ATTEMPTS */
|
||||||
|
|
||||||
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||||
|
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
|
||||||
|
|
||||||
|
#define SET_OPTION(packet, opt) ((packet)->options |= 1<<opt)
|
||||||
|
#define IS_OPTION(packet, opt) ((packet)->options & 1<<opt)
|
||||||
|
|
||||||
|
#ifndef MIN
|
||||||
|
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
||||||
|
#endif /* MIN */
|
||||||
|
|
||||||
|
/* CoAP message types */
|
||||||
|
typedef enum {
|
||||||
|
COAP_TYPE_CON, /* confirmables */
|
||||||
|
COAP_TYPE_NON, /* non-confirmables */
|
||||||
|
COAP_TYPE_ACK, /* acknowledgements */
|
||||||
|
COAP_TYPE_RST /* reset */
|
||||||
|
} coap_message_type_t;
|
||||||
|
|
||||||
|
/* CoAP request method codes */
|
||||||
|
typedef enum {
|
||||||
|
COAP_GET = 1,
|
||||||
|
COAP_POST,
|
||||||
|
COAP_PUT,
|
||||||
|
COAP_DELETE
|
||||||
|
} coap_method_t;
|
||||||
|
|
||||||
|
/* CoAP response codes */
|
||||||
|
typedef enum {
|
||||||
|
CONTINUE_100 = 40,
|
||||||
|
OK_200 = 80,
|
||||||
|
CREATED_201 = 81,
|
||||||
|
NOT_MODIFIED_304 = 124,
|
||||||
|
BAD_REQUEST_400 = 160,
|
||||||
|
NOT_FOUND_404 = 164,
|
||||||
|
METHOD_NOT_ALLOWED_405 = 165,
|
||||||
|
UNSUPPORTED_MADIA_TYPE_415 = 175,
|
||||||
|
INTERNAL_SERVER_ERROR_500 = 200,
|
||||||
|
BAD_GATEWAY_502 = 202,
|
||||||
|
SERVICE_UNAVAILABLE_503 = 203,
|
||||||
|
GATEWAY_TIMEOUT_504 = 204,
|
||||||
|
TOKEN_OPTION_REQUIRED = 240,
|
||||||
|
HOST_REQUIRED = 241,
|
||||||
|
CRITICAL_OPTION_NOT_SUPPORTED = 242
|
||||||
|
} coap_status_t;
|
||||||
|
|
||||||
|
/* CoAP header options */
|
||||||
|
typedef enum {
|
||||||
|
COAP_OPTION_CONTENT_TYPE = 1, /* 1 B */
|
||||||
|
COAP_OPTION_MAX_AGE = 2, /* 1-4 B */
|
||||||
|
COAP_OPTION_ETAG = 4, /* 1-4 B */
|
||||||
|
COAP_OPTION_URI_HOST = 5, /* 1-270 B */
|
||||||
|
COAP_OPTION_LOCATION_PATH = 6, /* 1-270 B */
|
||||||
|
COAP_OPTION_URI_PATH = 9, /* 1-270 B */
|
||||||
|
COAP_OPTION_OBSERVE = 10, /* 0-4 B */
|
||||||
|
COAP_OPTION_TOKEN = 11, /* 1-2 B */
|
||||||
|
COAP_OPTION_BLOCK = 13, /* 1-3 B */
|
||||||
|
COAP_OPTION_NOOP = 14, /* 0 B */
|
||||||
|
COAP_OPTION_URI_QUERY = 15 /* 1-270 B */
|
||||||
|
} coap_option_t;
|
||||||
|
|
||||||
|
/* CoAP content-types */
|
||||||
|
typedef enum {
|
||||||
|
TEXT_PLAIN = 0,
|
||||||
|
TEXT_XML = 1,
|
||||||
|
TEXT_CSV = 2,
|
||||||
|
TEXT_HTML = 3,
|
||||||
|
IMAGE_GIF = 21,
|
||||||
|
IMAGE_JPEG = 22,
|
||||||
|
IMAGE_PNG = 23,
|
||||||
|
IMAGE_TIFF = 24,
|
||||||
|
AUDIO_RAW = 25,
|
||||||
|
VIDEO_RAW = 26,
|
||||||
|
APPLICATION_LINK_FORMAT = 40,
|
||||||
|
APPLICATION_XML = 41,
|
||||||
|
APPLICATION_OCTET_STREAM = 42,
|
||||||
|
APPLICATION_RDF_XML = 43,
|
||||||
|
APPLICATION_SOAP_XML = 44,
|
||||||
|
APPLICATION_ATOM_XML = 45,
|
||||||
|
APPLICATION_XMPP_XML = 46,
|
||||||
|
APPLICATION_EXI = 47,
|
||||||
|
APPLICATION_X_BXML = 48,
|
||||||
|
APPLICATION_FASTINFOSET = 49,
|
||||||
|
APPLICATION_SOAP_FASTINFOSET = 50,
|
||||||
|
APPLICATION_JSON = 51
|
||||||
|
} coap_content_type_t;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
struct { /* 0--14 bytes options */
|
||||||
|
uint8_t length:4; /* option length in bytes (15 indicates long option format) */
|
||||||
|
uint8_t delta:4; /* option delta */
|
||||||
|
/* 0--14 bytes options */
|
||||||
|
} s;
|
||||||
|
struct { /* 15-270 bytes options */
|
||||||
|
uint16_t flag:4; /* must be 15 */
|
||||||
|
uint16_t delta:4; /* option delta */
|
||||||
|
uint16_t length:8; /* length - 15 */
|
||||||
|
} l;
|
||||||
|
} coap_header_option_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
|
||||||
|
|
||||||
|
uint8_t version;
|
||||||
|
coap_message_type_t type;
|
||||||
|
uint8_t option_count;
|
||||||
|
uint8_t code;
|
||||||
|
uint16_t tid;
|
||||||
|
|
||||||
|
uint16_t options; /* Bitmap to check if option is set */
|
||||||
|
|
||||||
|
coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */
|
||||||
|
uint32_t max_age;
|
||||||
|
uint8_t etag_len;
|
||||||
|
uint8_t etag[COAP_ETAG_LEN];
|
||||||
|
uint8_t uri_host_len;
|
||||||
|
char *uri_host;
|
||||||
|
uint8_t location_path_len;
|
||||||
|
char *location_path;
|
||||||
|
uint8_t uri_path_len;
|
||||||
|
char *uri_path;
|
||||||
|
uint32_t observe; /* 0-4 bytes for coap-03 */
|
||||||
|
uint8_t token_len;
|
||||||
|
uint8_t token[COAP_TOKEN_LEN];
|
||||||
|
uint32_t block_num;
|
||||||
|
uint8_t block_more;
|
||||||
|
uint16_t block_size;
|
||||||
|
uint32_t block_offset;
|
||||||
|
uint8_t uri_query_len;
|
||||||
|
char *uri_query;
|
||||||
|
|
||||||
|
uint16_t payload_len;
|
||||||
|
uint8_t *payload;
|
||||||
|
|
||||||
|
} coap_packet_t;
|
||||||
|
|
||||||
|
/*error definitions*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
NO_ERROR,
|
||||||
|
|
||||||
|
/* Memory errors */
|
||||||
|
MEMORY_ALLOC_ERR,
|
||||||
|
MEMORY_BOUNDARY_EXCEEDED,
|
||||||
|
|
||||||
|
/* CoAP errors */
|
||||||
|
UNKNOWN_CRITICAL_OPTION,
|
||||||
|
PACKET_SERIALIZATION_ERROR
|
||||||
|
} error_t;
|
||||||
|
|
||||||
|
void coap_init_connection(uint16_t port);
|
||||||
|
uint16_t coap_get_tid(void);
|
||||||
|
void coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length);
|
||||||
|
|
||||||
|
void coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid);
|
||||||
|
int coap_serialize_message(void *packet, uint8_t *buffer);
|
||||||
|
error_t coap_parse_message(void *request, uint8_t *data, uint16_t data_len);
|
||||||
|
|
||||||
|
coap_method_t coap_get_method(void *packet);
|
||||||
|
void coap_set_status(void *packet, unsigned int code);
|
||||||
|
|
||||||
|
int coap_get_query_variable(void *packet, const char *name, const char **output);
|
||||||
|
int coap_get_post_variable(void *packet, const char *name, const char **output);
|
||||||
|
|
||||||
|
unsigned int coap_get_header_content_type(void *packet);
|
||||||
|
int coap_set_header_content_type(void *packet, unsigned int content_type);
|
||||||
|
|
||||||
|
int coap_get_header_max_age(void *packet, uint32_t *age);
|
||||||
|
int coap_set_header_max_age(void *packet, uint32_t age);
|
||||||
|
|
||||||
|
int coap_get_header_etag(void *packet, const uint8_t **etag);
|
||||||
|
int coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len);
|
||||||
|
|
||||||
|
int coap_get_header_uri_host(void *packet, const char **host); /*CAUTION in-place string might not be 0-terminated */
|
||||||
|
int coap_set_header_uri_host(void *packet, char *host);
|
||||||
|
|
||||||
|
int coap_get_header_location(void *packet, const char **uri); /*CAUTION in-place string might not be 0-terminated */
|
||||||
|
int coap_set_header_location(void *packet, char *uri);
|
||||||
|
|
||||||
|
int coap_get_header_uri_path(void *packet, const char **uri); /*CAUTION in-place string might not be 0-terminated */
|
||||||
|
int coap_set_header_uri_path(void *packet, char *uri);
|
||||||
|
|
||||||
|
int coap_get_header_observe(void *packet, uint32_t *observe);
|
||||||
|
int coap_set_header_observe(void *packet, uint32_t observe);
|
||||||
|
|
||||||
|
int coap_get_header_token(void *packet, const uint8_t **token);
|
||||||
|
int coap_set_header_token(void *packet, uint8_t *token, size_t token_len);
|
||||||
|
|
||||||
|
int coap_get_header_block(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
|
||||||
|
int coap_set_header_block(void *packet, uint32_t num, uint8_t more, uint16_t size);
|
||||||
|
|
||||||
|
int coap_get_header_uri_query(void *packet, const char **query); /*CAUTION in-place string might not be 0-terminated */
|
||||||
|
int coap_set_header_uri_query(void *packet, char *query);
|
||||||
|
|
||||||
|
int coap_get_payload(void *packet, const uint8_t **payload);
|
||||||
|
int coap_set_payload(void *packet, uint8_t *payload, size_t length);
|
||||||
|
|
||||||
|
#endif /* COAP_03_H_ */
|
1
apps/er-coap-06/Makefile.er-coap-06
Normal file
|
@ -0,0 +1 @@
|
||||||
|
er-coap-06_src = er-coap-06-engine.c er-coap-06.c er-coap-06-transactions.c er-coap-06-observing.c er-coap-06-separate.c
|
564
apps/er-coap-06/er-coap-06-engine.c
Normal file
|
@ -0,0 +1,564 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP implementation of the REST Engine
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "er-coap-06-engine.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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 PRINTBITS(buf,len) { \
|
||||||
|
int i,j=0; \
|
||||||
|
for (i=0; i<len; ++i) { \
|
||||||
|
for (j=7; j>=0; --j) { \
|
||||||
|
PRINTF("%c", (((char *)buf)[i] & 1<<j) ? '1' : '0'); \
|
||||||
|
} \
|
||||||
|
PRINTF(" "); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define PRINTF(...)
|
||||||
|
#define PRINT6ADDR(addr)
|
||||||
|
#define PRINTLLADDR(addr)
|
||||||
|
#define PRINTBITS(buf,len)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
PROCESS(coap_receiver, "CoAP Receiver");
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Variables -----------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static service_callback_t service_cbk = NULL;
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
int
|
||||||
|
handle_incoming_data(void)
|
||||||
|
{
|
||||||
|
coap_error_code = NO_ERROR;
|
||||||
|
|
||||||
|
PRINTF("handle_incoming_data(): received uip_datalen=%u \n",(uint16_t)uip_datalen());
|
||||||
|
|
||||||
|
uint8_t *data = uip_appdata + uip_ext_len;
|
||||||
|
uint16_t data_len = uip_datalen() - uip_ext_len;
|
||||||
|
|
||||||
|
/* Static declaration reduces stack peaks and program code size. */
|
||||||
|
static coap_packet_t message[1]; /* This way the packet can be treated as pointer as usual. */
|
||||||
|
static coap_packet_t response[1];
|
||||||
|
static coap_transaction_t *transaction = NULL;
|
||||||
|
|
||||||
|
if (uip_newdata()) {
|
||||||
|
|
||||||
|
PRINTF("receiving UDP datagram from: ");
|
||||||
|
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
|
||||||
|
PRINTF(":%u\n Length: %u\n Data: ", uip_ntohs(UIP_UDP_BUF->srcport), data_len );
|
||||||
|
PRINTBITS(data, data_len);
|
||||||
|
PRINTF("\n");
|
||||||
|
|
||||||
|
coap_error_code = coap_parse_message(message, data, data_len);
|
||||||
|
|
||||||
|
if (coap_error_code==NO_ERROR)
|
||||||
|
{
|
||||||
|
|
||||||
|
/*TODO duplicates suppression, if required */
|
||||||
|
|
||||||
|
PRINTF(" Parsed: v %u, t %u, oc %u, c %u, tid %u\n", message->version, message->type, message->option_count, message->code, message->tid);
|
||||||
|
PRINTF(" URL: %.*s\n", message->uri_path_len, message->uri_path);
|
||||||
|
PRINTF(" Payload: %.*s\n", message->payload_len, message->payload);
|
||||||
|
|
||||||
|
/* Handle requests. */
|
||||||
|
if (message->code >= COAP_GET && message->code <= COAP_DELETE)
|
||||||
|
{
|
||||||
|
/* Use transaction buffer for response to confirmable request. */
|
||||||
|
if ( (transaction = coap_new_transaction(message->tid, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport)) )
|
||||||
|
{
|
||||||
|
static uint32_t block_num = 0;
|
||||||
|
static uint16_t block_size = REST_MAX_CHUNK_SIZE;
|
||||||
|
static uint32_t block_offset = 0;
|
||||||
|
static int32_t new_offset = 0;
|
||||||
|
|
||||||
|
/* prepare response */
|
||||||
|
if (message->type==COAP_TYPE_CON)
|
||||||
|
{
|
||||||
|
/* Reliable CON requests are answered with an ACK. */
|
||||||
|
coap_init_message(response, COAP_TYPE_ACK, CONTENT_2_05, message->tid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Unreliable NON requests are answered with a NON as well. */
|
||||||
|
coap_init_message(response, COAP_TYPE_NON, CONTENT_2_05, coap_get_tid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* resource handlers must take care of different handling (e.g., TOKEN_OPTION_REQUIRED_240) */
|
||||||
|
if (IS_OPTION(message, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
coap_set_header_token(response, message->token, message->token_len);
|
||||||
|
SET_OPTION(response, COAP_OPTION_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* get offset for blockwise transfers */
|
||||||
|
if (coap_get_header_block2(message, &block_num, NULL, &block_size, &block_offset))
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: block request %lu (%u/%u) @ %lu bytes\n", block_num, block_size, REST_MAX_CHUNK_SIZE, block_offset);
|
||||||
|
block_size = MIN(block_size, REST_MAX_CHUNK_SIZE);
|
||||||
|
new_offset = block_offset;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Invoke resource handler. */
|
||||||
|
if (service_cbk)
|
||||||
|
{
|
||||||
|
/* Call REST framework and check if found and allowed. */
|
||||||
|
if (service_cbk(message, response, transaction->packet+COAP_MAX_HEADER_SIZE, block_size, &new_offset))
|
||||||
|
{
|
||||||
|
/* Apply blockwise transfers. */
|
||||||
|
if ( IS_OPTION(message, COAP_OPTION_BLOCK2) )
|
||||||
|
{
|
||||||
|
/* unchanged new_offset indicates that resource is unaware of blockwise transfer */
|
||||||
|
if (new_offset==block_offset)
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: unaware resource with payload length %u/%u\n", response->payload_len, block_size);
|
||||||
|
if (block_offset >= response->payload_len)
|
||||||
|
{
|
||||||
|
PRINTF("handle_incoming_data(): block_offset >= response->payload_len\n");
|
||||||
|
|
||||||
|
response->code = BAD_OPTION_4_02;
|
||||||
|
coap_set_payload(response, (uint8_t*)"Block out of scope", 18);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_set_header_block2(response, block_num, response->payload_len - block_offset > block_size, block_size);
|
||||||
|
coap_set_payload(response, response->payload+block_offset, MIN(response->payload_len - block_offset, block_size));
|
||||||
|
} /* if (valid offset) */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* resource provides chunk-wise data */
|
||||||
|
PRINTF("Blockwise: blockwise resource, new offset %ld\n", new_offset);
|
||||||
|
coap_set_header_block2(response, block_num, new_offset!=-1 || response->payload_len > block_size, block_size);
|
||||||
|
if (response->payload_len > block_size) coap_set_payload(response, response->payload, block_size);
|
||||||
|
} /* if (resource aware of blockwise) */
|
||||||
|
}
|
||||||
|
else if (new_offset!=0)
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: no block option for blockwise resource, using block size %u\n", REST_MAX_CHUNK_SIZE);
|
||||||
|
|
||||||
|
coap_set_header_block2(response, 0, new_offset!=-1, REST_MAX_CHUNK_SIZE);
|
||||||
|
coap_set_payload(response, response->payload, MIN(response->payload_len, REST_MAX_CHUNK_SIZE));
|
||||||
|
} /* if (blockwise request) */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_error_code = INTERNAL_SERVER_ERROR_5_00;
|
||||||
|
coap_error_message = "Service callback undefined";
|
||||||
|
} /* if (service callback) */
|
||||||
|
|
||||||
|
/* serialize Response. */
|
||||||
|
if ((transaction->packet_len = coap_serialize_message(response, transaction->packet))==0)
|
||||||
|
{
|
||||||
|
coap_error_code = PACKET_SERIALIZATION_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
coap_error_code = MEMORY_ALLOC_ERR;
|
||||||
|
coap_error_message = "Transaction buffer allocation failed";
|
||||||
|
} /* if (transaction buffer) */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Responses */
|
||||||
|
|
||||||
|
if (message->type==COAP_TYPE_ACK)
|
||||||
|
{
|
||||||
|
PRINTF("Received ACK\n");
|
||||||
|
}
|
||||||
|
else if (message->type==COAP_TYPE_RST)
|
||||||
|
{
|
||||||
|
PRINTF("Received RST\n");
|
||||||
|
/* Cancel possible subscriptions. */
|
||||||
|
if (IS_OPTION(message, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
PRINTF(" Token 0x%02X%02X\n", message->token[0], message->token[1]);
|
||||||
|
coap_remove_observer_by_token(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, message->token, message->token_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (transaction = coap_get_transaction_by_tid(message->tid)) )
|
||||||
|
{
|
||||||
|
/* Free transaction memory before callback, as it may create a new transaction. */
|
||||||
|
restful_response_handler callback = transaction->callback;
|
||||||
|
void *callback_data = transaction->callback_data;
|
||||||
|
coap_clear_transaction(transaction);
|
||||||
|
|
||||||
|
/* Check if someone registered for the response */
|
||||||
|
if (callback) {
|
||||||
|
callback(callback_data, message);
|
||||||
|
}
|
||||||
|
} /* if (ACKed transaction) */
|
||||||
|
transaction = NULL;
|
||||||
|
}
|
||||||
|
} /* if (parsed correctly) */
|
||||||
|
|
||||||
|
if (coap_error_code==NO_ERROR) {
|
||||||
|
if (transaction) coap_send_transaction(transaction);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("ERROR %u: %s\n", coap_error_code, coap_error_message);
|
||||||
|
coap_clear_transaction(transaction);
|
||||||
|
|
||||||
|
/* Set to sendable error code. */
|
||||||
|
if (coap_error_code >= 192)
|
||||||
|
{
|
||||||
|
coap_error_code = INTERNAL_SERVER_ERROR_5_00;
|
||||||
|
}
|
||||||
|
/* Reuse input buffer for error message. */
|
||||||
|
coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->tid);
|
||||||
|
coap_set_payload(message, (uint8_t *) coap_error_message, strlen(coap_error_message));
|
||||||
|
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, data, coap_serialize_message(message, data));
|
||||||
|
}
|
||||||
|
} /* if (new data) */
|
||||||
|
|
||||||
|
return coap_error_code;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_receiver_init()
|
||||||
|
{
|
||||||
|
process_start(&coap_receiver, NULL);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_set_service_callback(service_callback_t callback)
|
||||||
|
{
|
||||||
|
service_cbk = callback;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
rest_resource_flags_t
|
||||||
|
coap_get_rest_method(void *packet)
|
||||||
|
{
|
||||||
|
return (rest_resource_flags_t)(1 << (((coap_packet_t *)packet)->code - 1));
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_set_rest_status(void *packet, unsigned int code)
|
||||||
|
{
|
||||||
|
if (code <= 0xFF)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->code = (uint8_t) code;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Server part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* The discover resource is automatically included for CoAP. */
|
||||||
|
RESOURCE(well_known_core, METHOD_GET, ".well-known/core", "");
|
||||||
|
void
|
||||||
|
well_known_core_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
|
{
|
||||||
|
/* Response might be NULL for non-confirmable requests. */
|
||||||
|
if (response)
|
||||||
|
{
|
||||||
|
size_t strpos = 0;
|
||||||
|
size_t bufpos = 0;
|
||||||
|
resource_t* resource = NULL;
|
||||||
|
|
||||||
|
for (resource = (resource_t*)list_head(rest_get_resources()); resource; resource = resource->next)
|
||||||
|
{
|
||||||
|
strpos += snprintf((char *) buffer + bufpos, REST_MAX_CHUNK_SIZE - bufpos + 1,
|
||||||
|
"</%s>%s%s%s",
|
||||||
|
resource->url,
|
||||||
|
resource->attributes[0] ? ";" : "",
|
||||||
|
resource->attributes,
|
||||||
|
resource->next ? "," : "" );
|
||||||
|
|
||||||
|
PRINTF("discover: %s\n", resource->url);
|
||||||
|
|
||||||
|
if (strpos <= *offset)
|
||||||
|
{
|
||||||
|
/* Discard output before current block */
|
||||||
|
PRINTF(" if %d <= %ld B\n", strpos, *offset);
|
||||||
|
PRINTF(" %s\n", buffer);
|
||||||
|
bufpos = 0;
|
||||||
|
}
|
||||||
|
else /* (strpos > *offset) */
|
||||||
|
{
|
||||||
|
/* output partly in block */
|
||||||
|
size_t len = MIN(strpos - *offset, preferred_size);
|
||||||
|
|
||||||
|
PRINTF(" el %d/%d @ %ld B\n", len, preferred_size, *offset);
|
||||||
|
|
||||||
|
/* Block might start in the middle of the output; align with buffer start. */
|
||||||
|
if (bufpos == 0)
|
||||||
|
{
|
||||||
|
memmove(buffer, buffer+strlen((char *)buffer)-strpos+*offset, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
bufpos = len;
|
||||||
|
PRINTF(" %s\n", buffer);
|
||||||
|
|
||||||
|
if (bufpos >= preferred_size)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bufpos>0) {
|
||||||
|
coap_set_payload(response, buffer, bufpos );
|
||||||
|
coap_set_header_content_type(response, APPLICATION_LINK_FORMAT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("well_known_core_handler(): bufpos<=0\n");
|
||||||
|
|
||||||
|
coap_set_rest_status(response, BAD_OPTION_4_02);
|
||||||
|
coap_set_payload(response, (uint8_t*)"Block out of scope", 18);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource==NULL) {
|
||||||
|
*offset = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*offset += bufpos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PROCESS_THREAD(coap_receiver, ev, data)
|
||||||
|
{
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
PRINTF("Starting CoAP-06 receiver...\n");
|
||||||
|
|
||||||
|
rest_activate_resource(&resource_well_known_core);
|
||||||
|
|
||||||
|
coap_register_as_transaction_handler();
|
||||||
|
coap_init_connection(SERVER_LISTEN_PORT);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PROCESS_YIELD();
|
||||||
|
|
||||||
|
if(ev == tcpip_event) {
|
||||||
|
handle_incoming_data();
|
||||||
|
} else if (ev == PROCESS_EVENT_TIMER) {
|
||||||
|
/* retransmissions are handled here */
|
||||||
|
coap_check_transactions();
|
||||||
|
}
|
||||||
|
} /* while (1) */
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Client part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void blocking_request_callback(void *callback_data, void *response) {
|
||||||
|
struct request_state_t *state = (struct request_state_t *) callback_data;
|
||||||
|
state->response = (coap_packet_t*) response;
|
||||||
|
process_poll(state->process);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
|
||||||
|
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
|
||||||
|
coap_packet_t *request,
|
||||||
|
blocking_response_handler request_callback)) {
|
||||||
|
PT_BEGIN(&state->pt);
|
||||||
|
|
||||||
|
static uint8_t more;
|
||||||
|
static uint32_t res_block;
|
||||||
|
static uint8_t block_error;
|
||||||
|
|
||||||
|
state->block_num = 0;
|
||||||
|
state->response = NULL;
|
||||||
|
state->process = PROCESS_CURRENT();
|
||||||
|
|
||||||
|
more = 0;
|
||||||
|
res_block = 0;
|
||||||
|
block_error = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
request->tid = coap_get_tid();
|
||||||
|
if ((state->transaction = coap_new_transaction(request->tid, remote_ipaddr, remote_port)))
|
||||||
|
{
|
||||||
|
state->transaction->callback = blocking_request_callback;
|
||||||
|
state->transaction->callback_data = state;
|
||||||
|
|
||||||
|
if (state->block_num>0)
|
||||||
|
{
|
||||||
|
coap_set_header_block2(request, state->block_num, 0, REST_MAX_CHUNK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
state->transaction->packet_len = coap_serialize_message(request, state->transaction->packet);
|
||||||
|
|
||||||
|
coap_send_transaction(state->transaction);
|
||||||
|
PRINTF("Requested #%lu (TID %u)\n", state->block_num, request->tid);
|
||||||
|
|
||||||
|
PT_YIELD_UNTIL(&state->pt, ev == PROCESS_EVENT_POLL);
|
||||||
|
|
||||||
|
if (!state->response)
|
||||||
|
{
|
||||||
|
PRINTF("Server not responding\n");
|
||||||
|
PT_EXIT(&state->pt);
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
|
||||||
|
|
||||||
|
PRINTF("Received #%lu%s (%u bytes)\n", res_block, more ? "+" : "", state->response->payload_len);
|
||||||
|
|
||||||
|
if (res_block==state->block_num)
|
||||||
|
{
|
||||||
|
request_callback(state->response);
|
||||||
|
++(state->block_num);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("WRONG BLOCK %lu/%lu\n", res_block, state->block_num);
|
||||||
|
++block_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("Could not allocate transaction buffer");
|
||||||
|
PT_EXIT(&state->pt);
|
||||||
|
}
|
||||||
|
} while (more && block_error<COAP_MAX_ATTEMPTS);
|
||||||
|
|
||||||
|
PT_END(&state->pt);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Engine Interface ----------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
const struct rest_implementation coap_rest_implementation = {
|
||||||
|
"CoAP-06",
|
||||||
|
|
||||||
|
coap_receiver_init,
|
||||||
|
coap_set_service_callback,
|
||||||
|
|
||||||
|
coap_get_header_uri_path,
|
||||||
|
coap_set_header_uri_path,
|
||||||
|
coap_get_rest_method,
|
||||||
|
coap_set_rest_status,
|
||||||
|
|
||||||
|
coap_get_header_content_type,
|
||||||
|
coap_set_header_content_type,
|
||||||
|
NULL,
|
||||||
|
coap_get_header_max_age,
|
||||||
|
coap_set_header_max_age,
|
||||||
|
coap_set_header_etag,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
coap_get_header_uri_host,
|
||||||
|
coap_set_header_location_path,
|
||||||
|
|
||||||
|
coap_get_payload,
|
||||||
|
coap_set_payload,
|
||||||
|
|
||||||
|
coap_get_header_uri_query,
|
||||||
|
coap_get_query_variable,
|
||||||
|
coap_get_post_variable,
|
||||||
|
|
||||||
|
coap_notify_observers,
|
||||||
|
(restful_post_handler) coap_observe_handler,
|
||||||
|
|
||||||
|
NULL, /* default pre-handler (set separate handler after activation if needed) */
|
||||||
|
NULL, /* default post-handler for non-observable resources */
|
||||||
|
|
||||||
|
{
|
||||||
|
CONTENT_2_05,
|
||||||
|
CREATED_2_01,
|
||||||
|
CHANGED_2_04,
|
||||||
|
DELETED_2_02,
|
||||||
|
VALID_2_03,
|
||||||
|
BAD_REQUEST_4_00,
|
||||||
|
UNAUTHORIZED_4_01,
|
||||||
|
BAD_OPTION_4_02,
|
||||||
|
FORBIDDEN_4_03,
|
||||||
|
NOT_FOUND_4_04,
|
||||||
|
METHOD_NOT_ALLOWED_4_05,
|
||||||
|
REQUEST_ENTITY_TOO_LARGE_4_13,
|
||||||
|
UNSUPPORTED_MADIA_TYPE_4_15,
|
||||||
|
INTERNAL_SERVER_ERROR_5_00,
|
||||||
|
NOT_IMPLEMENTED_5_01,
|
||||||
|
BAD_GATEWAY_5_02,
|
||||||
|
SERVICE_UNAVAILABLE_5_03,
|
||||||
|
GATEWAY_TIMEOUT_5_04,
|
||||||
|
PROXYING_NOT_SUPPORTED_5_05
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
TEXT_PLAIN,
|
||||||
|
TEXT_XML,
|
||||||
|
TEXT_CSV,
|
||||||
|
TEXT_HTML,
|
||||||
|
IMAGE_GIF,
|
||||||
|
IMAGE_JPEG,
|
||||||
|
IMAGE_PNG,
|
||||||
|
IMAGE_TIFF,
|
||||||
|
AUDIO_RAW,
|
||||||
|
VIDEO_RAW,
|
||||||
|
APPLICATION_LINK_FORMAT,
|
||||||
|
APPLICATION_XML,
|
||||||
|
APPLICATION_OCTET_STREAM,
|
||||||
|
APPLICATION_RDF_XML,
|
||||||
|
APPLICATION_SOAP_XML,
|
||||||
|
APPLICATION_ATOM_XML,
|
||||||
|
APPLICATION_XMPP_XML,
|
||||||
|
APPLICATION_EXI,
|
||||||
|
APPLICATION_FASTINFOSET,
|
||||||
|
APPLICATION_SOAP_FASTINFOSET,
|
||||||
|
APPLICATION_JSON,
|
||||||
|
APPLICATION_X_OBIX_BINARY
|
||||||
|
}
|
||||||
|
};
|
92
apps/er-coap-06/er-coap-06-engine.h
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP implementation of the REST Engine
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_SERVER_H_
|
||||||
|
#define COAP_SERVER_H_
|
||||||
|
|
||||||
|
#if !defined(REST)
|
||||||
|
#error "Define REST to \"coap_rest_implementation\""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "er-coap-06.h"
|
||||||
|
#include "er-coap-06-transactions.h"
|
||||||
|
#include "er-coap-06-observing.h"
|
||||||
|
#include "er-coap-06-separate.h"
|
||||||
|
|
||||||
|
#include "pt.h"
|
||||||
|
|
||||||
|
/* Declare server process */
|
||||||
|
PROCESS_NAME(coap_receiver);
|
||||||
|
|
||||||
|
#define SERVER_LISTEN_PORT UIP_HTONS(COAP_SERVER_PORT)
|
||||||
|
|
||||||
|
typedef coap_packet_t rest_request_t;
|
||||||
|
typedef coap_packet_t rest_response_t;
|
||||||
|
|
||||||
|
extern const struct rest_implementation coap_rest_implementation;
|
||||||
|
|
||||||
|
void coap_receiver_init(void);
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Client part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
struct request_state_t {
|
||||||
|
struct pt pt;
|
||||||
|
struct process *process;
|
||||||
|
coap_transaction_t *transaction;
|
||||||
|
coap_packet_t *response;
|
||||||
|
uint32_t block_num;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*blocking_response_handler) (void* response);
|
||||||
|
|
||||||
|
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
|
||||||
|
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
|
||||||
|
coap_packet_t *request,
|
||||||
|
blocking_response_handler request_callback));
|
||||||
|
|
||||||
|
#define COAP_BLOCKING_REQUEST(server_addr, server_port, request, chunk_handler) \
|
||||||
|
static struct request_state_t request_state; \
|
||||||
|
PT_SPAWN(process_pt, &request_state.pt, \
|
||||||
|
coap_blocking_request(&request_state, ev, \
|
||||||
|
server_addr, server_port, \
|
||||||
|
request, chunk_handler) \
|
||||||
|
);
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#endif /* COAP_SERVER_H_ */
|
201
apps/er-coap-06/er-coap-06-observing.c
Normal file
|
@ -0,0 +1,201 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for observing resources
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "er-coap-06-observing.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
|
||||||
|
LIST(observers_list);
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
coap_observer_t *
|
||||||
|
coap_add_observer(const char *url, uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
coap_observer_t *o = memb_alloc(&observers_memb);
|
||||||
|
|
||||||
|
if (o)
|
||||||
|
{
|
||||||
|
o->url = url;
|
||||||
|
uip_ipaddr_copy(&o->addr, addr);
|
||||||
|
o->port = port;
|
||||||
|
o->token_len = token_len;
|
||||||
|
memcpy(o->token, token, token_len);
|
||||||
|
|
||||||
|
stimer_set(&o->refresh_timer, COAP_OBSERVING_REFRESH_INTERVAL);
|
||||||
|
|
||||||
|
PRINTF("Adding observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
||||||
|
list_add(observers_list, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_remove_observer(coap_observer_t *o)
|
||||||
|
{
|
||||||
|
PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
||||||
|
|
||||||
|
memb_free(&observers_memb, o);
|
||||||
|
list_remove(observers_list, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check client ");
|
||||||
|
PRINT6ADDR(addr);
|
||||||
|
PRINTF(":%u\n", port);
|
||||||
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]);
|
||||||
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port && memcmp(obs->token, token, token_len)==0)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
if (obs->url==url) /* using RESOURCE url pointer as handle */
|
||||||
|
{
|
||||||
|
coap_transaction_t *transaction = NULL;
|
||||||
|
|
||||||
|
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */
|
||||||
|
|
||||||
|
if ( (transaction = coap_new_transaction(coap_get_tid(), &obs->addr, obs->port)) )
|
||||||
|
{
|
||||||
|
/* Use CON to check whether client is still there/interested after COAP_OBSERVING_REFRESH_INTERVAL. */
|
||||||
|
if (stimer_expired(&obs->refresh_timer))
|
||||||
|
{
|
||||||
|
PRINTF("Observing: Refresh client with CON\n");
|
||||||
|
type = COAP_TYPE_CON;
|
||||||
|
stimer_restart(&obs->refresh_timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prepare response */
|
||||||
|
coap_packet_t push[1]; /* This way the packet can be treated as pointer as usual. */
|
||||||
|
coap_init_message(push, (coap_message_type_t)type, CONTENT_2_05, transaction->tid );
|
||||||
|
coap_set_header_observe(push, observe);
|
||||||
|
coap_set_header_token(push, obs->token, obs->token_len);
|
||||||
|
coap_set_payload(push, payload, payload_len);
|
||||||
|
transaction->packet_len = coap_serialize_message(push, transaction->packet);
|
||||||
|
|
||||||
|
PRINTF("Observing: Notify from /%s for ", url);
|
||||||
|
PRINT6ADDR(&obs->addr);
|
||||||
|
PRINTF(":%u\n", obs->port);
|
||||||
|
PRINTF(" %.*s\n", payload_len, payload);
|
||||||
|
|
||||||
|
coap_send_transaction(transaction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_observe_handler(resource_t *resource, void *request, void *response)
|
||||||
|
{
|
||||||
|
static char content[26];
|
||||||
|
|
||||||
|
if (response && ((coap_packet_t *)response)->code<128) /* response without error code */
|
||||||
|
{
|
||||||
|
if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_OBSERVE))
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)request, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
/* Set default token. */
|
||||||
|
coap_set_header_token(request, (uint8_t *)"", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (coap_add_observer(resource->url, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, ((coap_packet_t *)request)->token, ((coap_packet_t *)request)->token_len))
|
||||||
|
{
|
||||||
|
coap_set_header_observe(response, 0);
|
||||||
|
coap_set_payload(response, (uint8_t *)content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((coap_packet_t *)response)->code = SERVICE_UNAVAILABLE_5_03;
|
||||||
|
coap_set_payload(response, (uint8_t *)"Too many observers", 18);
|
||||||
|
} /* if (added observer) */
|
||||||
|
}
|
||||||
|
else /* if (observe) */
|
||||||
|
{
|
||||||
|
/* Remove client if it is currently observing. */
|
||||||
|
coap_remove_observer_by_client(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport);
|
||||||
|
} /* if (observe) */
|
||||||
|
}
|
||||||
|
}
|
76
apps/er-coap-06/er-coap-06-observing.h
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for observing resources
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_OBSERVING_H_
|
||||||
|
#define COAP_OBSERVING_H_
|
||||||
|
|
||||||
|
#include "er-coap-06.h"
|
||||||
|
#include "er-coap-06-transactions.h"
|
||||||
|
|
||||||
|
#ifndef COAP_MAX_OBSERVERS
|
||||||
|
#define COAP_MAX_OBSERVERS 4
|
||||||
|
#endif /* COAP_MAX_OBSERVERS */
|
||||||
|
|
||||||
|
/* Interval in seconds in which NON notifies are changed to CON notifies to check client. */
|
||||||
|
#define COAP_OBSERVING_REFRESH_INTERVAL 60
|
||||||
|
|
||||||
|
#if COAP_MAX_OPEN_TRANSACTIONS<COAP_MAX_OBSERVERS
|
||||||
|
#warning "COAP_MAX_OPEN_TRANSACTIONS smaller than COAP_MAX_OBSERVERS: cannot handle CON notifications"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct coap_observer {
|
||||||
|
struct coap_observer *next; /* for LIST */
|
||||||
|
|
||||||
|
const char *url;
|
||||||
|
uip_ipaddr_t addr;
|
||||||
|
uint16_t port;
|
||||||
|
uint8_t token_len;
|
||||||
|
uint8_t token[COAP_TOKEN_LEN];
|
||||||
|
struct stimer refresh_timer;
|
||||||
|
} coap_observer_t;
|
||||||
|
|
||||||
|
list_t coap_get_observers(void);
|
||||||
|
coap_observer_t *coap_add_observer(const char *url, uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len);
|
||||||
|
void coap_remove_observer(coap_observer_t *o);
|
||||||
|
int coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port);
|
||||||
|
int coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len);
|
||||||
|
void coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len);
|
||||||
|
|
||||||
|
void coap_observe_handler(resource_t *resource, void *request, void *response);
|
||||||
|
|
||||||
|
#endif /* COAP_OBSERVING_H_ */
|
72
apps/er-coap-06/er-coap-06-separate.c
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for separate responses
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "er-coap-06-separate.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void coap_separate_handler(resource_t *resource, void *request, void *response)
|
||||||
|
{
|
||||||
|
if (resource->benchmark > COAP_SEPARATE_THRESHOLD)
|
||||||
|
{
|
||||||
|
PRINTF("Separate response for /%s \n", resource->url);
|
||||||
|
/* send separate ACK. */
|
||||||
|
coap_packet_t ack[1];
|
||||||
|
/* ACK with empty code (0) */
|
||||||
|
coap_init_message(ack, COAP_TYPE_ACK, 0, ((coap_packet_t *)request)->tid);
|
||||||
|
/* Should only overwrite Header which is already parsed to request. */
|
||||||
|
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata + uip_ext_len), coap_serialize_message(ack, (uip_appdata + uip_ext_len)));
|
||||||
|
|
||||||
|
/* Change response to separate response. */
|
||||||
|
((coap_packet_t *)response)->type = COAP_TYPE_CON;
|
||||||
|
((coap_packet_t *)response)->tid = coap_get_tid();
|
||||||
|
}
|
||||||
|
}
|
50
apps/er-coap-06/er-coap-06-separate.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for separate responses
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_SEPARATE_H_
|
||||||
|
#define COAP_SEPARATE_H_
|
||||||
|
|
||||||
|
#include "er-coap-06.h"
|
||||||
|
|
||||||
|
#ifndef COAP_SEPARATE_THRESHOLD
|
||||||
|
#define COAP_SEPARATE_THRESHOLD 42
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void coap_separate_handler(resource_t *resource, void *request, void *response);
|
||||||
|
|
||||||
|
#endif /* COAP_SEPARATE_H_ */
|
194
apps/er-coap-06/er-coap-06-transactions.c
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for reliable transport
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "er-coap-06-transactions.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Modulo mask (+1 and +0.5 for rounding) for a random number to get the tick number for the random
|
||||||
|
* retransmission time between COAP_RESPONSE_TIMEOUT and COAP_RESPONSE_TIMEOUT*COAP_RESPONSE_RANDOM_FACTOR.
|
||||||
|
*/
|
||||||
|
#define COAP_RESPONSE_TIMEOUT_TICKS (CLOCK_SECOND * COAP_RESPONSE_TIMEOUT)
|
||||||
|
#define COAP_RESPONSE_TIMEOUT_BACKOFF_MASK ((CLOCK_SECOND * COAP_RESPONSE_TIMEOUT * (COAP_RESPONSE_RANDOM_FACTOR - 1)) + 1.5)
|
||||||
|
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
|
||||||
|
LIST(transactions_list);
|
||||||
|
|
||||||
|
|
||||||
|
static struct process *transaction_handler_process = NULL;
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_register_as_transaction_handler()
|
||||||
|
{
|
||||||
|
transaction_handler_process = PROCESS_CURRENT();
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_transaction_t *
|
||||||
|
coap_new_transaction(uint16_t tid, uip_ipaddr_t *addr, uint16_t port)
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = memb_alloc(&transactions_memb);
|
||||||
|
|
||||||
|
if (t)
|
||||||
|
{
|
||||||
|
t->tid = tid;
|
||||||
|
t->retrans_counter = 0;
|
||||||
|
|
||||||
|
/* save client address */
|
||||||
|
uip_ipaddr_copy(&t->addr, addr);
|
||||||
|
t->port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_send_transaction(coap_transaction_t *t)
|
||||||
|
{
|
||||||
|
PRINTF("Sending transaction %u\n", t->tid);
|
||||||
|
|
||||||
|
coap_send_message(&t->addr, t->port, t->packet, t->packet_len);
|
||||||
|
|
||||||
|
if (COAP_TYPE_CON==((COAP_HEADER_TYPE_MASK & t->packet[0])>>COAP_HEADER_TYPE_POSITION))
|
||||||
|
{
|
||||||
|
if (t->retrans_counter<COAP_MAX_RETRANSMIT)
|
||||||
|
{
|
||||||
|
PRINTF("Keeping transaction %u\n", t->tid);
|
||||||
|
|
||||||
|
if (t->retrans_counter==0)
|
||||||
|
{
|
||||||
|
t->retrans_timer.timer.interval = COAP_RESPONSE_TIMEOUT_TICKS + (random_rand() % (clock_time_t) COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
|
||||||
|
PRINTF("Initial interval %f\n", (float)t->retrans_timer.timer.interval/CLOCK_SECOND);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
t->retrans_timer.timer.interval <<= 1; /* double */
|
||||||
|
PRINTF("Doubled (%u) interval %f\n", t->retrans_counter, (float)t->retrans_timer.timer.interval/CLOCK_SECOND);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*FIXME hack, maybe there is a better way, but avoid posting everything to the process */
|
||||||
|
struct process *process_actual = PROCESS_CURRENT();
|
||||||
|
process_current = transaction_handler_process;
|
||||||
|
etimer_restart(&t->retrans_timer); /* interval updated above */
|
||||||
|
process_current = process_actual;
|
||||||
|
|
||||||
|
list_add(transactions_list, t); /* List itself makes sure same element is not added twice. */
|
||||||
|
|
||||||
|
t = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* timeout */
|
||||||
|
PRINTF("Timeout\n");
|
||||||
|
restful_response_handler callback = t->callback;
|
||||||
|
void *callback_data = t->callback_data;
|
||||||
|
|
||||||
|
/* handle observers */
|
||||||
|
coap_remove_observer_by_client(&t->addr, t->port);
|
||||||
|
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback(callback_data, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_clear_transaction(coap_transaction_t *t)
|
||||||
|
{
|
||||||
|
if (t)
|
||||||
|
{
|
||||||
|
PRINTF("Freeing transaction %u: %p\n", t->tid, t);
|
||||||
|
|
||||||
|
etimer_stop(&t->retrans_timer);
|
||||||
|
list_remove(transactions_list, t);
|
||||||
|
memb_free(&transactions_memb, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_transaction_t *
|
||||||
|
coap_get_transaction_by_tid(uint16_t tid)
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = NULL;
|
||||||
|
|
||||||
|
for (t = (coap_transaction_t*)list_head(transactions_list); t; t = t->next)
|
||||||
|
{
|
||||||
|
if (t->tid==tid)
|
||||||
|
{
|
||||||
|
PRINTF("Found transaction for TID %u: %p\n", t->tid, t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_check_transactions()
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = NULL;
|
||||||
|
|
||||||
|
for (t = (coap_transaction_t*)list_head(transactions_list); t; t = t->next)
|
||||||
|
{
|
||||||
|
if (etimer_expired(&t->retrans_timer))
|
||||||
|
{
|
||||||
|
++(t->retrans_counter);
|
||||||
|
PRINTF("Retransmitting %u (%u)\n", t->tid, t->retrans_counter);
|
||||||
|
coap_send_transaction(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
apps/er-coap-06/er-coap-06-transactions.h
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for reliable transport
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_TRANSACTIONS_H_
|
||||||
|
#define COAP_TRANSACTIONS_H_
|
||||||
|
|
||||||
|
#include "er-coap-06.h"
|
||||||
|
#include "er-coap-06-observing.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The number of concurrent messages that can be stored for retransmission in the transaction layer.
|
||||||
|
*/
|
||||||
|
#ifndef COAP_MAX_OPEN_TRANSACTIONS
|
||||||
|
#define COAP_MAX_OPEN_TRANSACTIONS 4
|
||||||
|
#endif /* COAP_MAX_OPEN_TRANSACTIONS */
|
||||||
|
|
||||||
|
/* container for transactions with message buffer and retransmission info */
|
||||||
|
typedef struct coap_transaction {
|
||||||
|
struct coap_transaction *next; /* for LIST */
|
||||||
|
|
||||||
|
uint16_t tid;
|
||||||
|
struct etimer retrans_timer;
|
||||||
|
uint8_t retrans_counter;
|
||||||
|
|
||||||
|
uip_ipaddr_t addr;
|
||||||
|
uint16_t port;
|
||||||
|
|
||||||
|
restful_response_handler callback;
|
||||||
|
void *callback_data;
|
||||||
|
|
||||||
|
uint16_t packet_len;
|
||||||
|
uint8_t packet[COAP_MAX_PACKET_SIZE+1]; /* +1 for the terminating '\0' to simply and savely use snprintf(buf, len+1, "", ...) in the resource handler. */
|
||||||
|
} coap_transaction_t;
|
||||||
|
|
||||||
|
void coap_register_as_transaction_handler();
|
||||||
|
|
||||||
|
coap_transaction_t *coap_new_transaction(uint16_t tid, uip_ipaddr_t *addr, uint16_t port);
|
||||||
|
void coap_send_transaction(coap_transaction_t *t);
|
||||||
|
void coap_clear_transaction(coap_transaction_t *t);
|
||||||
|
coap_transaction_t *coap_get_transaction_by_tid(uint16_t tid);
|
||||||
|
|
||||||
|
void coap_check_transactions();
|
||||||
|
|
||||||
|
#endif /* COAP_TRANSACTIONS_H_ */
|
986
apps/er-coap-06/er-coap-06.c
Normal file
|
@ -0,0 +1,986 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An implementation of the Constrained Application Protocol (draft 06)
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "er-coap-06.h"
|
||||||
|
#include "er-coap-06-transactions.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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Variables -----------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static struct uip_udp_conn *udp_conn = NULL;
|
||||||
|
static uint16_t current_tid = 0;
|
||||||
|
|
||||||
|
coap_status_t coap_error_code = NO_ERROR;
|
||||||
|
char *coap_error_message = "";
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- LOCAL HELP FUNCTIONS ------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
uint16_t
|
||||||
|
log_2(uint16_t value)
|
||||||
|
{
|
||||||
|
uint16_t result = 0;
|
||||||
|
do {
|
||||||
|
value = value >> 1;
|
||||||
|
result++;
|
||||||
|
} while (value);
|
||||||
|
|
||||||
|
return result ? result - 1 : result;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
uint32_t
|
||||||
|
parse_int_option(uint8_t *bytes, uint16_t length)
|
||||||
|
{
|
||||||
|
uint32_t var = 0;
|
||||||
|
int i = 0;
|
||||||
|
while (i<length)
|
||||||
|
{
|
||||||
|
var <<= 8;
|
||||||
|
var |= 0xFF & bytes[i++];
|
||||||
|
}
|
||||||
|
return var;
|
||||||
|
}
|
||||||
|
static
|
||||||
|
size_t
|
||||||
|
set_option_header(int delta, size_t length, uint8_t *buffer)
|
||||||
|
{
|
||||||
|
if (length<15)
|
||||||
|
{
|
||||||
|
buffer[0] = (0x0F & length) | (0xF0 & delta<<4);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer[0] = 0x0F | (0xF0 & delta<<4);
|
||||||
|
buffer[1] = 0xFF & (length - 15);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
size_t
|
||||||
|
insert_option_fence_posts(int number, int *current_number, uint8_t *buffer)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
while (number-*current_number > 15)
|
||||||
|
{
|
||||||
|
uint8_t delta = COAP_OPTION_FENCE_POST - (*current_number%COAP_OPTION_FENCE_POST);
|
||||||
|
set_option_header(delta, 0, &buffer[i++]);
|
||||||
|
*current_number += delta;
|
||||||
|
|
||||||
|
PRINTF("OPTION FENCE POST delta %u\n", delta);
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
size_t
|
||||||
|
serialize_int_option(int number, int current_number, uint8_t *buffer, uint32_t value)
|
||||||
|
{
|
||||||
|
/* Insert fence-posts for large deltas */
|
||||||
|
size_t i = insert_option_fence_posts(number, ¤t_number, buffer);
|
||||||
|
size_t start_i = i;
|
||||||
|
|
||||||
|
uint8_t *option = &buffer[i];
|
||||||
|
|
||||||
|
if (0xFF000000 & value) buffer[++i] = (uint8_t) (value>>24);
|
||||||
|
if (0x00FF0000 & value) buffer[++i] = (uint8_t) (value>>16);
|
||||||
|
if (0x0000FF00 & value) buffer[++i] = (uint8_t) (value>>8);
|
||||||
|
if (0x000000FF & value) buffer[++i] = (uint8_t) value;
|
||||||
|
|
||||||
|
i += set_option_header(number - current_number, i-start_i, option);
|
||||||
|
|
||||||
|
PRINTF("OPTION type %u, delta %u, len %u\n", number, number - current_number, i-start_i);
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
size_t
|
||||||
|
serialize_array_option(int number, int current_number, uint8_t *buffer, uint8_t *array, size_t length, uint8_t *split_option)
|
||||||
|
{
|
||||||
|
/* Insert fence-posts for large deltas */
|
||||||
|
size_t i = insert_option_fence_posts(number, ¤t_number, buffer);
|
||||||
|
|
||||||
|
if (split_option!=NULL)
|
||||||
|
{
|
||||||
|
int j;
|
||||||
|
uint8_t *part_start = array;
|
||||||
|
uint8_t *part_end = NULL;
|
||||||
|
size_t temp_length;
|
||||||
|
|
||||||
|
char split_char = *split_option;
|
||||||
|
*split_option = 0; /* Ensure reflecting the created option count */
|
||||||
|
|
||||||
|
for (j = 0; j<=length; ++j)
|
||||||
|
{
|
||||||
|
if (array[j]==split_char || j==length)
|
||||||
|
{
|
||||||
|
part_end = array + j;
|
||||||
|
temp_length = part_end-part_start;
|
||||||
|
|
||||||
|
i += set_option_header(number - current_number, temp_length, &buffer[i]);
|
||||||
|
memcpy(&buffer[i], part_start, temp_length);
|
||||||
|
i += temp_length;
|
||||||
|
|
||||||
|
PRINTF("OPTION type %u, delta %u, len %u, part [%.*s]\n", number, number - current_number, i, temp_length, part_start);
|
||||||
|
|
||||||
|
++(*split_option);
|
||||||
|
++j; /* skip the slash */
|
||||||
|
current_number = number;
|
||||||
|
while( array[j]=='/') ++j;
|
||||||
|
part_start = array + j;
|
||||||
|
}
|
||||||
|
} /* for */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i += set_option_header(number - current_number, length, &buffer[i]);
|
||||||
|
memcpy(&buffer[i], array, length);
|
||||||
|
i += length;
|
||||||
|
|
||||||
|
PRINTF("OPTION type %u, delta %u, len %u\n", number, number - current_number, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
void
|
||||||
|
coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, size_t option_len, char separator)
|
||||||
|
{
|
||||||
|
/* Merge multiple options. */
|
||||||
|
if (*dst_len > 0)
|
||||||
|
{
|
||||||
|
(*dst)[*dst_len] = separator;
|
||||||
|
*dst_len += 1;
|
||||||
|
|
||||||
|
/* memmove handles 2-byte option headers */
|
||||||
|
memmove((*dst)+(*dst_len), option, option_len);
|
||||||
|
|
||||||
|
*dst_len += option_len;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*dst = (char *) option; /* thus pointer pointer */
|
||||||
|
*dst_len = option_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
int
|
||||||
|
coap_get_variable(const char *buffer, size_t length, const char *name, const char **output)
|
||||||
|
{
|
||||||
|
const char *start = NULL;
|
||||||
|
const char *end = NULL;
|
||||||
|
const char *value_end = NULL;
|
||||||
|
size_t name_len = 0;
|
||||||
|
|
||||||
|
/*initialize the output buffer first*/
|
||||||
|
*output = 0;
|
||||||
|
|
||||||
|
name_len = strlen(name);
|
||||||
|
end = buffer + length;
|
||||||
|
|
||||||
|
for (start = buffer; start + name_len < end; ++start){
|
||||||
|
if ((start == buffer || start[-1] == '&') && start[name_len] == '=' &&
|
||||||
|
strncmp(name, start, name_len)==0) {
|
||||||
|
|
||||||
|
/* Point start to variable value */
|
||||||
|
start += name_len + 1;
|
||||||
|
|
||||||
|
/* Point end to the end of the value */
|
||||||
|
value_end = (const char *) memchr(start, '&', end - start);
|
||||||
|
if (value_end == NULL) {
|
||||||
|
value_end = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
*output = start;
|
||||||
|
|
||||||
|
return (value_end - start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- MEASSAGE SENDING ----------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
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));
|
||||||
|
|
||||||
|
/* Initialize transaction ID. */
|
||||||
|
current_tid = random_rand();
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
uint16_t
|
||||||
|
coap_get_tid()
|
||||||
|
{
|
||||||
|
return ++current_tid;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- MEASSAGE PROCESSING -------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid)
|
||||||
|
{
|
||||||
|
/* Important thing */
|
||||||
|
memset(packet, 0, sizeof(coap_packet_t));
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->type = type;
|
||||||
|
((coap_packet_t *)packet)->code = code;
|
||||||
|
((coap_packet_t *)packet)->tid = tid;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
size_t
|
||||||
|
coap_serialize_message(void *packet, uint8_t *buffer)
|
||||||
|
{
|
||||||
|
/* Initialize */
|
||||||
|
((coap_packet_t *)packet)->buffer = buffer;
|
||||||
|
((coap_packet_t *)packet)->version = 1;
|
||||||
|
((coap_packet_t *)packet)->option_count = 0;
|
||||||
|
|
||||||
|
/* serialize options */
|
||||||
|
uint8_t *option = ((coap_packet_t *)packet)->buffer + COAP_HEADER_LEN;
|
||||||
|
int current_number = 0;
|
||||||
|
|
||||||
|
PRINTF("-Serializing options-\n");
|
||||||
|
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_CONTENT_TYPE)) {
|
||||||
|
PRINTF("Content-Type [%u]\n", ((coap_packet_t *)packet)->content_type);
|
||||||
|
|
||||||
|
option += serialize_int_option(COAP_OPTION_CONTENT_TYPE, current_number, option, ((coap_packet_t *)packet)->content_type);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_CONTENT_TYPE;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_MAX_AGE)) {
|
||||||
|
PRINTF("Max-Age [%lu]\n", ((coap_packet_t *)packet)->max_age);
|
||||||
|
|
||||||
|
option += serialize_int_option(COAP_OPTION_MAX_AGE, current_number, option, ((coap_packet_t *)packet)->max_age);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_MAX_AGE;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_PROXY_URI)) {
|
||||||
|
PRINTF("Proxy-Uri [%.*s]\n", ((coap_packet_t *)packet)->proxy_uri_len, ((coap_packet_t *)packet)->proxy_uri);
|
||||||
|
|
||||||
|
int length = ((coap_packet_t *)packet)->proxy_uri_len;
|
||||||
|
int j = 0;
|
||||||
|
while (length>0)
|
||||||
|
{
|
||||||
|
option += serialize_array_option(COAP_OPTION_PROXY_URI, current_number, option, (uint8_t *) ((coap_packet_t *)packet)->proxy_uri + j*270, MIN(270, length), NULL);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_PROXY_URI;
|
||||||
|
|
||||||
|
++j;
|
||||||
|
length -= 270;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_ETAG)) {
|
||||||
|
PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", ((coap_packet_t *)packet)->etag_len,
|
||||||
|
((coap_packet_t *)packet)->etag[0],
|
||||||
|
((coap_packet_t *)packet)->etag[1],
|
||||||
|
((coap_packet_t *)packet)->etag[2],
|
||||||
|
((coap_packet_t *)packet)->etag[3],
|
||||||
|
((coap_packet_t *)packet)->etag[4],
|
||||||
|
((coap_packet_t *)packet)->etag[5],
|
||||||
|
((coap_packet_t *)packet)->etag[6],
|
||||||
|
((coap_packet_t *)packet)->etag[7]
|
||||||
|
); /*FIXME always prints 8 bytes */
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_ETAG, current_number, option, ((coap_packet_t *)packet)->etag, ((coap_packet_t *)packet)->etag_len, NULL);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_ETAG;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST)) {
|
||||||
|
PRINTF("Uri-Host [%.*s]\n", ((coap_packet_t *)packet)->uri_host_len, ((coap_packet_t *)packet)->uri_host);
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_URI_HOST, current_number, option, (uint8_t *) ((coap_packet_t *)packet)->uri_host, ((coap_packet_t *)packet)->uri_host_len, NULL);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_URI_HOST;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH)) {
|
||||||
|
PRINTF("Location [%.*s]\n", ((coap_packet_t *)packet)->location_path_len, ((coap_packet_t *)packet)->location_path);
|
||||||
|
|
||||||
|
uint8_t split_options = '/';
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_LOCATION_PATH, current_number, option, (uint8_t *) ((coap_packet_t *)packet)->location_path, ((coap_packet_t *)packet)->location_path_len, &split_options);
|
||||||
|
((coap_packet_t *)packet)->option_count += split_options;
|
||||||
|
current_number = COAP_OPTION_LOCATION_PATH;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PORT)) {
|
||||||
|
PRINTF("Uri-Port [%u]\n", ((coap_packet_t *)packet)->uri_port);
|
||||||
|
|
||||||
|
option += serialize_int_option(COAP_OPTION_URI_PORT, current_number, option, ((coap_packet_t *)packet)->uri_port);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_URI_PORT;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_QUERY)) {
|
||||||
|
PRINTF("Location-Query [%.*s]\n", ((coap_packet_t *)packet)->location_query_len, ((coap_packet_t *)packet)->location_query);
|
||||||
|
|
||||||
|
uint8_t split_options = '&';
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_LOCATION_QUERY, current_number, option, (uint8_t *) ((coap_packet_t *)packet)->location_query, ((coap_packet_t *)packet)->location_query_len, &split_options);
|
||||||
|
((coap_packet_t *)packet)->option_count += split_options;
|
||||||
|
current_number = COAP_OPTION_LOCATION_QUERY;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH)) {
|
||||||
|
PRINTF("Uri-Path [%.*s]\n", ((coap_packet_t *)packet)->uri_path_len, ((coap_packet_t *)packet)->uri_path);
|
||||||
|
|
||||||
|
uint8_t split_options = '/';
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_URI_PATH, current_number, option, (uint8_t *) ((coap_packet_t *)packet)->uri_path, ((coap_packet_t *)packet)->uri_path_len, &split_options);
|
||||||
|
((coap_packet_t *)packet)->option_count += split_options;
|
||||||
|
current_number = COAP_OPTION_URI_PATH;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_OBSERVE)) {
|
||||||
|
PRINTF("Observe [%u]\n", ((coap_packet_t *)packet)->observe);
|
||||||
|
|
||||||
|
option += serialize_int_option(COAP_OPTION_OBSERVE, current_number, option, ((coap_packet_t *)packet)->observe);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_OBSERVE;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_TOKEN)) {
|
||||||
|
PRINTF("Token %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", ((coap_packet_t *)packet)->token_len,
|
||||||
|
((coap_packet_t *)packet)->token[0],
|
||||||
|
((coap_packet_t *)packet)->token[1],
|
||||||
|
((coap_packet_t *)packet)->token[2],
|
||||||
|
((coap_packet_t *)packet)->token[3],
|
||||||
|
((coap_packet_t *)packet)->token[4],
|
||||||
|
((coap_packet_t *)packet)->token[5],
|
||||||
|
((coap_packet_t *)packet)->token[6],
|
||||||
|
((coap_packet_t *)packet)->token[7]
|
||||||
|
); /*FIXME always prints 8 bytes */
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_TOKEN, current_number, option, ((coap_packet_t *)packet)->token, ((coap_packet_t *)packet)->token_len, NULL);
|
||||||
|
((coap_packet_t *)packet)->option_count += 1;
|
||||||
|
current_number = COAP_OPTION_TOKEN;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY)) {
|
||||||
|
PRINTF("Uri-Query [%.*s]\n", ((coap_packet_t *)packet)->uri_query_len, ((coap_packet_t *)packet)->uri_query);
|
||||||
|
|
||||||
|
uint8_t split_options = '&';
|
||||||
|
|
||||||
|
option += serialize_array_option(COAP_OPTION_URI_QUERY, current_number, option, (uint8_t *) ((coap_packet_t *)packet)->uri_query, ((coap_packet_t *)packet)->uri_query_len, &split_options);
|
||||||
|
((coap_packet_t *)packet)->option_count += split_options + (COAP_OPTION_URI_QUERY-current_number)/COAP_OPTION_FENCE_POST;
|
||||||
|
current_number = COAP_OPTION_URI_QUERY;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK2))
|
||||||
|
{
|
||||||
|
PRINTF("Block2 [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block2_num, ((coap_packet_t *)packet)->block2_more ? "+" : "", ((coap_packet_t *)packet)->block2_size);
|
||||||
|
|
||||||
|
uint32_t block = ((coap_packet_t *)packet)->block2_num << 4;
|
||||||
|
if (((coap_packet_t *)packet)->block2_more) block |= 0x8;
|
||||||
|
block |= 0xF & log_2(((coap_packet_t *)packet)->block2_size/16);
|
||||||
|
|
||||||
|
PRINTF("Block2 encoded: 0x%lX\n", block);
|
||||||
|
|
||||||
|
option += serialize_int_option(COAP_OPTION_BLOCK2, current_number, option, block);
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->option_count += 1 + (COAP_OPTION_BLOCK2-current_number)/COAP_OPTION_FENCE_POST;
|
||||||
|
current_number = COAP_OPTION_BLOCK2;
|
||||||
|
}
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK1))
|
||||||
|
{
|
||||||
|
PRINTF("Block1 [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block1_num, ((coap_packet_t *)packet)->block1_more ? "+" : "", ((coap_packet_t *)packet)->block1_size);
|
||||||
|
|
||||||
|
uint32_t block = ((coap_packet_t *)packet)->block1_num << 4;
|
||||||
|
if (((coap_packet_t *)packet)->block1_more) block |= 0x8;
|
||||||
|
block |= 0xF & log_2(((coap_packet_t *)packet)->block1_size/16);
|
||||||
|
|
||||||
|
PRINTF("Block1 encoded: 0x%lX\n", block);
|
||||||
|
|
||||||
|
option += serialize_int_option(COAP_OPTION_BLOCK1, current_number, option, block);
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->option_count += 1 + (COAP_OPTION_BLOCK1-current_number)/COAP_OPTION_FENCE_POST;
|
||||||
|
current_number = COAP_OPTION_BLOCK1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pack payload */
|
||||||
|
if ((option - ((coap_packet_t *)packet)->buffer)<=COAP_MAX_HEADER_SIZE)
|
||||||
|
{
|
||||||
|
memmove(option, ((coap_packet_t *)packet)->payload, ((coap_packet_t *)packet)->payload_len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* An error occured. Caller must check for !=0. */
|
||||||
|
((coap_packet_t *)packet)->buffer = NULL;
|
||||||
|
coap_error_message = "Serialized header exceeds COAP_MAX_HEADER_SIZE";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set header fields */
|
||||||
|
((coap_packet_t *)packet)->buffer[0] = 0x00;
|
||||||
|
((coap_packet_t *)packet)->buffer[0] |= COAP_HEADER_VERSION_MASK & (((coap_packet_t *)packet)->version)<<COAP_HEADER_VERSION_POSITION;
|
||||||
|
((coap_packet_t *)packet)->buffer[0] |= COAP_HEADER_TYPE_MASK & (((coap_packet_t *)packet)->type)<<COAP_HEADER_TYPE_POSITION;
|
||||||
|
((coap_packet_t *)packet)->buffer[0] |= COAP_HEADER_OPTION_COUNT_MASK & (((coap_packet_t *)packet)->option_count)<<COAP_HEADER_OPTION_COUNT_POSITION;
|
||||||
|
((coap_packet_t *)packet)->buffer[1] = ((coap_packet_t *)packet)->code;
|
||||||
|
((coap_packet_t *)packet)->buffer[2] = 0xFF & (((coap_packet_t *)packet)->tid)>>8;
|
||||||
|
((coap_packet_t *)packet)->buffer[3] = 0xFF & ((coap_packet_t *)packet)->tid;
|
||||||
|
|
||||||
|
PRINTF("-Done %u options, header len %u, payload len %u-\n", ((coap_packet_t *)packet)->option_count, option - buffer, ((coap_packet_t *)packet)->payload_len);
|
||||||
|
|
||||||
|
return (option - buffer) + ((coap_packet_t *)packet)->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 connection 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)
|
||||||
|
{
|
||||||
|
/* Initialize packet */
|
||||||
|
memset(packet, 0, sizeof(coap_packet_t));
|
||||||
|
|
||||||
|
/* pointer to packet bytes */
|
||||||
|
((coap_packet_t *)packet)->buffer = data;
|
||||||
|
|
||||||
|
/* parse header fields */
|
||||||
|
((coap_packet_t *)packet)->version = (COAP_HEADER_VERSION_MASK & ((coap_packet_t *)packet)->buffer[0])>>COAP_HEADER_VERSION_POSITION;
|
||||||
|
((coap_packet_t *)packet)->type = (COAP_HEADER_TYPE_MASK & ((coap_packet_t *)packet)->buffer[0])>>COAP_HEADER_TYPE_POSITION;
|
||||||
|
((coap_packet_t *)packet)->option_count = (COAP_HEADER_OPTION_COUNT_MASK & ((coap_packet_t *)packet)->buffer[0])>>COAP_HEADER_OPTION_COUNT_POSITION;
|
||||||
|
((coap_packet_t *)packet)->code = ((coap_packet_t *)packet)->buffer[1];
|
||||||
|
((coap_packet_t *)packet)->tid = ((coap_packet_t *)packet)->buffer[2]<<8 | ((coap_packet_t *)packet)->buffer[3];
|
||||||
|
|
||||||
|
if (((coap_packet_t *)packet)->version != 1)
|
||||||
|
{
|
||||||
|
coap_error_message = "CoAP version must be 1";
|
||||||
|
return BAD_REQUEST_4_00;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* parse options */
|
||||||
|
((coap_packet_t *)packet)->options = 0x0000;
|
||||||
|
uint8_t *current_option = data + COAP_HEADER_LEN;
|
||||||
|
|
||||||
|
if (((coap_packet_t *)packet)->option_count)
|
||||||
|
{
|
||||||
|
uint8_t option_index = 0;
|
||||||
|
|
||||||
|
uint8_t current_number = 0;
|
||||||
|
size_t option_len = 0;
|
||||||
|
|
||||||
|
PRINTF("-Parsing %u options-\n", ((coap_packet_t *)packet)->option_count);
|
||||||
|
for (option_index=0; option_index < ((coap_packet_t *)packet)->option_count; ++option_index) {
|
||||||
|
|
||||||
|
current_number += current_option[0]>>4;
|
||||||
|
|
||||||
|
PRINTF("OPTION %u (type %u, delta %u, len %u): ", option_index, current_number, current_option[0]>>4, (0x0F & current_option[0]) < 15 ? (0x0F & current_option[0]) : current_option[1] + 15);
|
||||||
|
|
||||||
|
if ((0x0F & current_option[0]) < 15) {
|
||||||
|
option_len = 0x0F & current_option[0];
|
||||||
|
current_option += 1;
|
||||||
|
} else {
|
||||||
|
option_len = current_option[1] + 15;
|
||||||
|
current_option += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, current_number);
|
||||||
|
|
||||||
|
switch (current_number) {
|
||||||
|
case COAP_OPTION_CONTENT_TYPE:
|
||||||
|
((coap_packet_t *)packet)->content_type = parse_int_option(current_option, option_len);
|
||||||
|
PRINTF("Content-Type [%u]\n", ((coap_packet_t *)packet)->content_type);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_MAX_AGE:
|
||||||
|
((coap_packet_t *)packet)->max_age = parse_int_option(current_option, option_len);
|
||||||
|
PRINTF("Max-Age [%lu]\n", ((coap_packet_t *)packet)->max_age);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_PROXY_URI:
|
||||||
|
/*FIXME check for own end-point */
|
||||||
|
((coap_packet_t *)packet)->proxy_uri = (char *) current_option;
|
||||||
|
((coap_packet_t *)packet)->proxy_uri_len = option_len;
|
||||||
|
/*TODO length > 270 not implemented (actually not required) */
|
||||||
|
PRINTF("Proxy-Uri NOT IMPLEMENTED [%.*s]\n", ((coap_packet_t *)packet)->proxy_uri_len, ((coap_packet_t *)packet)->proxy_uri);
|
||||||
|
coap_error_message = "This is a constrained server (Contiki)";
|
||||||
|
return PROXYING_NOT_SUPPORTED_5_05;
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_ETAG:
|
||||||
|
((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, option_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->etag, current_option, ((coap_packet_t *)packet)->etag_len);
|
||||||
|
PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", ((coap_packet_t *)packet)->etag_len,
|
||||||
|
((coap_packet_t *)packet)->etag[0],
|
||||||
|
((coap_packet_t *)packet)->etag[1],
|
||||||
|
((coap_packet_t *)packet)->etag[2],
|
||||||
|
((coap_packet_t *)packet)->etag[3],
|
||||||
|
((coap_packet_t *)packet)->etag[4],
|
||||||
|
((coap_packet_t *)packet)->etag[5],
|
||||||
|
((coap_packet_t *)packet)->etag[6],
|
||||||
|
((coap_packet_t *)packet)->etag[7]
|
||||||
|
); /*FIXME always prints 8 bytes */
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_HOST:
|
||||||
|
((coap_packet_t *)packet)->uri_host = (char *) current_option;
|
||||||
|
((coap_packet_t *)packet)->uri_host_len = option_len;
|
||||||
|
PRINTF("Uri-Host [%.*s]\n", ((coap_packet_t *)packet)->uri_host_len, ((coap_packet_t *)packet)->uri_host);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_LOCATION_PATH:
|
||||||
|
coap_merge_multi_option(&(((coap_packet_t *)packet)->location_path), &(((coap_packet_t *)packet)->location_path_len), current_option, option_len, '/');
|
||||||
|
PRINTF("Location-Path [%.*s]\n", ((coap_packet_t *)packet)->location_path_len, ((coap_packet_t *)packet)->location_path);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_PORT:
|
||||||
|
((coap_packet_t *)packet)->uri_port = parse_int_option(current_option, option_len);
|
||||||
|
PRINTF("Uri-Port [%u]\n", ((coap_packet_t *)packet)->uri_port);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_LOCATION_QUERY:
|
||||||
|
coap_merge_multi_option(&(((coap_packet_t *)packet)->location_query), &(((coap_packet_t *)packet)->location_query_len), current_option, option_len, '&');
|
||||||
|
PRINTF("Location-Query [%.*s]\n", ((coap_packet_t *)packet)->location_query_len, ((coap_packet_t *)packet)->location_query);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_PATH:
|
||||||
|
coap_merge_multi_option(&(((coap_packet_t *)packet)->uri_path), &(((coap_packet_t *)packet)->uri_path_len), current_option, option_len, '/');
|
||||||
|
PRINTF("Uri-Path [%.*s]\n", ((coap_packet_t *)packet)->uri_path_len, ((coap_packet_t *)packet)->uri_path);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_OBSERVE:
|
||||||
|
((coap_packet_t *)packet)->observe = parse_int_option(current_option, option_len);
|
||||||
|
PRINTF("Observe [%u]\n", ((coap_packet_t *)packet)->observe);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_TOKEN:
|
||||||
|
((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, option_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->token, current_option, ((coap_packet_t *)packet)->token_len);
|
||||||
|
PRINTF("Token %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", ((coap_packet_t *)packet)->token_len,
|
||||||
|
((coap_packet_t *)packet)->token[0],
|
||||||
|
((coap_packet_t *)packet)->token[1],
|
||||||
|
((coap_packet_t *)packet)->token[2],
|
||||||
|
((coap_packet_t *)packet)->token[3],
|
||||||
|
((coap_packet_t *)packet)->token[4],
|
||||||
|
((coap_packet_t *)packet)->token[5],
|
||||||
|
((coap_packet_t *)packet)->token[6],
|
||||||
|
((coap_packet_t *)packet)->token[7]
|
||||||
|
); /*FIXME always prints 8 bytes */
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_FENCE_POST:
|
||||||
|
PRINTF("Fence-Post\n");
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_URI_QUERY:
|
||||||
|
coap_merge_multi_option(&(((coap_packet_t *)packet)->uri_query), &(((coap_packet_t *)packet)->uri_query_len), current_option, option_len, '&');
|
||||||
|
PRINTF("Uri-Query [%.*s]\n", ((coap_packet_t *)packet)->uri_query_len, ((coap_packet_t *)packet)->uri_query);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_BLOCK2:
|
||||||
|
((coap_packet_t *)packet)->block2_num = parse_int_option(current_option, option_len);
|
||||||
|
((coap_packet_t *)packet)->block2_more = (((coap_packet_t *)packet)->block2_num & 0x08)>>3;
|
||||||
|
((coap_packet_t *)packet)->block2_size = 16 << (((coap_packet_t *)packet)->block2_num & 0x07);
|
||||||
|
((coap_packet_t *)packet)->block2_offset = (((coap_packet_t *)packet)->block2_num & ~0x0F)<<(((coap_packet_t *)packet)->block2_num & 0x07);
|
||||||
|
((coap_packet_t *)packet)->block2_num >>= 4;
|
||||||
|
PRINTF("Block2 [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block2_num, ((coap_packet_t *)packet)->block2_more ? "+" : "", ((coap_packet_t *)packet)->block2_size);
|
||||||
|
break;
|
||||||
|
case COAP_OPTION_BLOCK1:
|
||||||
|
PRINTF("Block1 NOT IMPLEMENTED\n");
|
||||||
|
/*TODO implement */
|
||||||
|
coap_error_message = "Blockwise POST/PUT not supported";
|
||||||
|
return NOT_IMPLEMENTED_5_01;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PRINTF("unknown (%u)\n", current_number);
|
||||||
|
/* Check if critical (odd) */
|
||||||
|
if (current_number & 1)
|
||||||
|
{
|
||||||
|
coap_error_message = "Unsupported critical option";
|
||||||
|
return BAD_OPTION_4_02;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_option += option_len;
|
||||||
|
} /* for */
|
||||||
|
PRINTF("-Done parsing-------\n");
|
||||||
|
} /* if (oc) */
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->payload = current_option;
|
||||||
|
((coap_packet_t *)packet)->payload_len = data_len - (((coap_packet_t *)packet)->payload - data);
|
||||||
|
|
||||||
|
return NO_ERROR;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- REST FRAMEWORK FUNCTIONS --------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_query_variable(void *packet, const char *name, const char **output)
|
||||||
|
{
|
||||||
|
if (IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY)) {
|
||||||
|
return coap_get_variable(((coap_packet_t *)packet)->uri_query, ((coap_packet_t *)packet)->uri_query_len, name, output);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_get_post_variable(void *packet, const char *name, const char **output)
|
||||||
|
{
|
||||||
|
if (((coap_packet_t *)packet)->payload_len) {
|
||||||
|
return coap_get_variable((const char *)((coap_packet_t *)packet)->payload, ((coap_packet_t *)packet)->payload_len, name, output);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- HEADER OPTION GETTERS AND SETTERS -----------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
unsigned int
|
||||||
|
coap_get_header_content_type(void *packet)
|
||||||
|
{
|
||||||
|
return ((coap_packet_t *)packet)->content_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_content_type(void *packet, unsigned int content_type)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->content_type = (coap_content_type_t) content_type;
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_CONTENT_TYPE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_max_age(void *packet, uint32_t *age)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_MAX_AGE)) {
|
||||||
|
*age = COAP_DEFAULT_MAX_AGE;
|
||||||
|
} else {
|
||||||
|
*age = ((coap_packet_t *)packet)->max_age;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_max_age(void *packet, uint32_t age)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->max_age = age;
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_MAX_AGE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_etag(void *packet, const uint8_t **etag)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_ETAG)) return 0;
|
||||||
|
|
||||||
|
*etag = ((coap_packet_t *)packet)->etag;
|
||||||
|
return ((coap_packet_t *)packet)->etag_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, etag_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->etag, etag, ((coap_packet_t *)packet)->etag_len);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_ETAG);
|
||||||
|
return ((coap_packet_t *)packet)->etag_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_token(void *packet, const uint8_t **token)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_TOKEN)) return 0;
|
||||||
|
|
||||||
|
*token = ((coap_packet_t *)packet)->token;
|
||||||
|
return ((coap_packet_t *)packet)->token_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_token(void *packet, uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, token_len);
|
||||||
|
memcpy(((coap_packet_t *)packet)->token, token, ((coap_packet_t *)packet)->token_len);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_TOKEN);
|
||||||
|
return ((coap_packet_t *)packet)->token_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_proxy_uri(void *packet, const char **uri)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_PROXY_URI)) return 0;
|
||||||
|
|
||||||
|
*uri = ((coap_packet_t *)packet)->proxy_uri;
|
||||||
|
return ((coap_packet_t *)packet)->proxy_uri_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_proxy_uri(void *packet, char *uri)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->proxy_uri = uri;
|
||||||
|
((coap_packet_t *)packet)->proxy_uri_len = strlen(uri);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_PROXY_URI);
|
||||||
|
return ((coap_packet_t *)packet)->proxy_uri_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_uri_host(void *packet, const char **host)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST)) return 0;
|
||||||
|
|
||||||
|
*host = ((coap_packet_t *)packet)->uri_host;
|
||||||
|
return ((coap_packet_t *)packet)->uri_host_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_uri_host(void *packet, char *host)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->uri_host = host;
|
||||||
|
((coap_packet_t *)packet)->uri_host_len = strlen(host);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST);
|
||||||
|
return ((coap_packet_t *)packet)->uri_host_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_uri_path(void *packet, const char **path)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH)) return 0;
|
||||||
|
|
||||||
|
*path = ((coap_packet_t *)packet)->uri_path;
|
||||||
|
return ((coap_packet_t *)packet)->uri_path_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_uri_path(void *packet, char *path)
|
||||||
|
{
|
||||||
|
while (path[0]=='/') ++path;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->uri_path = path;
|
||||||
|
((coap_packet_t *)packet)->uri_path_len = strlen(path);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH);
|
||||||
|
return ((coap_packet_t *)packet)->uri_path_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_uri_query(void *packet, const char **query)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY)) return 0;
|
||||||
|
|
||||||
|
*query = ((coap_packet_t *)packet)->uri_query;
|
||||||
|
return ((coap_packet_t *)packet)->uri_query_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_uri_query(void *packet, char *query)
|
||||||
|
{
|
||||||
|
while (query[0]=='?') ++query;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->uri_query = query;
|
||||||
|
((coap_packet_t *)packet)->uri_query_len = strlen(query);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_QUERY);
|
||||||
|
return ((coap_packet_t *)packet)->uri_query_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_location_path(void *packet, const char **path)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH)) return 0;
|
||||||
|
|
||||||
|
*path = ((coap_packet_t *)packet)->location_path;
|
||||||
|
return ((coap_packet_t *)packet)->location_path_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_location_path(void *packet, char *path)
|
||||||
|
{
|
||||||
|
char *query;
|
||||||
|
|
||||||
|
while (path[0]=='/') ++path;
|
||||||
|
|
||||||
|
if ((query = strchr(path, '?')))
|
||||||
|
{
|
||||||
|
coap_set_header_location_query(packet, query+1);
|
||||||
|
((coap_packet_t *)packet)->location_path_len = query - path;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->location_path_len = strlen(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->location_path = path;
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH);
|
||||||
|
return ((coap_packet_t *)packet)->location_path_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_location_query(void *packet, const char **query)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_QUERY)) return 0;
|
||||||
|
|
||||||
|
*query = ((coap_packet_t *)packet)->location_query;
|
||||||
|
return ((coap_packet_t *)packet)->location_query_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_location_query(void *packet, char *query)
|
||||||
|
{
|
||||||
|
while (query[0]=='?') ++query;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->location_query = query;
|
||||||
|
((coap_packet_t *)packet)->location_query_len = strlen(query);
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_QUERY);
|
||||||
|
return ((coap_packet_t *)packet)->location_query_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_observe(void *packet, uint32_t *observe)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_OBSERVE)) return 0;
|
||||||
|
|
||||||
|
*observe = ((coap_packet_t *)packet)->observe;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_observe(void *packet, uint32_t observe)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->observe = observe;
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_OBSERVE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK2)) return 0;
|
||||||
|
|
||||||
|
/* pointers may be NULL to get only specific block parameters */
|
||||||
|
if (num!=NULL) *num = ((coap_packet_t *)packet)->block2_num;
|
||||||
|
if (more!=NULL) *more = ((coap_packet_t *)packet)->block2_more;
|
||||||
|
if (size!=NULL) *size = ((coap_packet_t *)packet)->block2_size;
|
||||||
|
if (offset!=NULL) *offset = ((coap_packet_t *)packet)->block2_offset;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size)
|
||||||
|
{
|
||||||
|
if (size<16) return 0;
|
||||||
|
if (size>2048) return 0;
|
||||||
|
if (num>0x0FFFFF) return 0;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->block2_num = num;
|
||||||
|
((coap_packet_t *)packet)->block2_more = more;
|
||||||
|
((coap_packet_t *)packet)->block2_size = size;
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK1)) return 0;
|
||||||
|
|
||||||
|
/* pointers may be NULL to get only specific block parameters */
|
||||||
|
if (num!=NULL) *num = ((coap_packet_t *)packet)->block1_num;
|
||||||
|
if (more!=NULL) *more = ((coap_packet_t *)packet)->block1_more;
|
||||||
|
if (size!=NULL) *size = ((coap_packet_t *)packet)->block1_size;
|
||||||
|
if (offset!=NULL) *offset = ((coap_packet_t *)packet)->block1_offset;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size)
|
||||||
|
{
|
||||||
|
if (size<16) return 0;
|
||||||
|
if (size>2048) return 0;
|
||||||
|
if (num>0x0FFFFF) return 0;
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->block1_num = num;
|
||||||
|
((coap_packet_t *)packet)->block1_more = more;
|
||||||
|
((coap_packet_t *)packet)->block1_size = size;
|
||||||
|
|
||||||
|
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- PAYLOAD -------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_get_payload(void *packet, const uint8_t **payload)
|
||||||
|
{
|
||||||
|
if (((coap_packet_t *)packet)->payload) {
|
||||||
|
*payload = ((coap_packet_t *)packet)->payload;
|
||||||
|
return ((coap_packet_t *)packet)->payload_len;
|
||||||
|
} else {
|
||||||
|
*payload = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_set_payload(void *packet, uint8_t *payload, size_t length)
|
||||||
|
{
|
||||||
|
PRINTF("setting payload (%u/%u)\n", length, REST_MAX_CHUNK_SIZE);
|
||||||
|
|
||||||
|
((coap_packet_t *)packet)->payload = payload;
|
||||||
|
((coap_packet_t *)packet)->payload_len = MIN(REST_MAX_CHUNK_SIZE, length);
|
||||||
|
|
||||||
|
return ((coap_packet_t *)packet)->payload_len;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
300
apps/er-coap-06/er-coap-06.h
Normal file
|
@ -0,0 +1,300 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An implementation of the Constrained Application Protocol (draft 06)
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_06_H_
|
||||||
|
#define COAP_06_H_
|
||||||
|
|
||||||
|
#include <stddef.h> /* for size_t */
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "erbium.h"
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_PORT 5683
|
||||||
|
|
||||||
|
#ifndef COAP_SERVER_PORT
|
||||||
|
#define COAP_SERVER_PORT COAP_DEFAULT_PORT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_MAX_AGE 60
|
||||||
|
#define COAP_RESPONSE_TIMEOUT 2
|
||||||
|
#define COAP_RESPONSE_RANDOM_FACTOR 1.5
|
||||||
|
#define COAP_MAX_RETRANSMIT 4
|
||||||
|
|
||||||
|
#define COAP_HEADER_LEN 4 /* | oc:0xF0 type:0x0C version:0x03 | code | tid:0x00FF | tid:0xFF00 | */
|
||||||
|
#define COAP_ETAG_LEN 8 /* The maximum number of bytes for the ETag */
|
||||||
|
#define COAP_TOKEN_LEN 8 /* The maximum number of bytes for the Token */
|
||||||
|
|
||||||
|
#define COAP_HEADER_VERSION_MASK 0xC0
|
||||||
|
#define COAP_HEADER_VERSION_POSITION 6
|
||||||
|
#define COAP_HEADER_TYPE_MASK 0x30
|
||||||
|
#define COAP_HEADER_TYPE_POSITION 4
|
||||||
|
#define COAP_HEADER_OPTION_COUNT_MASK 0x0F
|
||||||
|
#define COAP_HEADER_OPTION_COUNT_POSITION 0
|
||||||
|
|
||||||
|
#define COAP_HEADER_OPTION_DELTA_MASK 0xF0
|
||||||
|
#define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Conservative size limit, as not all options have to be set at the same time.
|
||||||
|
*/
|
||||||
|
/* Hdr CoT Age Tag Obs Tok Blo strings */
|
||||||
|
#define COAP_MAX_HEADER_SIZE (4 + 3 + 5 + 1+COAP_ETAG_LEN + 3 + 1+COAP_TOKEN_LEN + 4 + 10) /* 50 */
|
||||||
|
#define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
|
||||||
|
/* 0/14 48 for IPv6 (28 for IPv4) */
|
||||||
|
#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN)
|
||||||
|
#error "UIP_CONF_BUFFER_SIZE too small for REST_MAX_CHUNK_SIZE"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Maximum number of failed request attempts before action
|
||||||
|
*/
|
||||||
|
#ifndef COAP_MAX_ATTEMPTS
|
||||||
|
#define COAP_MAX_ATTEMPTS 4
|
||||||
|
#endif /* COAP_MAX_ATTEMPTS */
|
||||||
|
|
||||||
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||||
|
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
|
||||||
|
|
||||||
|
#define SET_OPTION(packet, opt) ((packet)->options |= 1L<<opt)
|
||||||
|
#define IS_OPTION(packet, opt) ((packet)->options & 1L<<opt)
|
||||||
|
|
||||||
|
#ifndef MIN
|
||||||
|
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
||||||
|
#endif /* MIN */
|
||||||
|
|
||||||
|
/* CoAP message types */
|
||||||
|
typedef enum {
|
||||||
|
COAP_TYPE_CON, /* confirmables */
|
||||||
|
COAP_TYPE_NON, /* non-confirmables */
|
||||||
|
COAP_TYPE_ACK, /* acknowledgements */
|
||||||
|
COAP_TYPE_RST /* reset */
|
||||||
|
} coap_message_type_t;
|
||||||
|
|
||||||
|
/* CoAP request method codes */
|
||||||
|
typedef enum {
|
||||||
|
COAP_GET = 1,
|
||||||
|
COAP_POST,
|
||||||
|
COAP_PUT,
|
||||||
|
COAP_DELETE
|
||||||
|
} coap_method_t;
|
||||||
|
|
||||||
|
/* CoAP response codes */
|
||||||
|
typedef enum {
|
||||||
|
NO_ERROR = 0,
|
||||||
|
|
||||||
|
CREATED_2_01 = 65, /* CREATED */
|
||||||
|
DELETED_2_02 = 66, /* DELETED */
|
||||||
|
VALID_2_03 = 67, /* NOT_MODIFIED */
|
||||||
|
CHANGED_2_04 = 68, /* CHANGED */
|
||||||
|
CONTENT_2_05 = 69, /* OK */
|
||||||
|
|
||||||
|
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
|
||||||
|
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
|
||||||
|
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
|
||||||
|
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
|
||||||
|
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
|
||||||
|
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
|
||||||
|
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
|
||||||
|
UNSUPPORTED_MADIA_TYPE_4_15 = 143, /* UNSUPPORTED_MADIA_TYPE */
|
||||||
|
|
||||||
|
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
|
||||||
|
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
|
||||||
|
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
|
||||||
|
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
|
||||||
|
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
|
||||||
|
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
|
||||||
|
|
||||||
|
/* Memory errors */
|
||||||
|
IMPLEMENTATION_ERROR = 192,
|
||||||
|
MEMORY_ALLOC_ERR = 193,
|
||||||
|
MEMORY_BOUNDARY_EXCEEDED = 194,
|
||||||
|
|
||||||
|
/* CoAP errors */
|
||||||
|
UNIMPLEMENTED_CRITICAL_OPTION,
|
||||||
|
UNKNOWN_CRITICAL_OPTION,
|
||||||
|
PACKET_SERIALIZATION_ERROR
|
||||||
|
} coap_status_t;
|
||||||
|
|
||||||
|
/* CoAP header options */
|
||||||
|
typedef enum {
|
||||||
|
COAP_OPTION_CONTENT_TYPE = 1, /* 1-2 B */
|
||||||
|
COAP_OPTION_MAX_AGE = 2, /* 0-4 B */
|
||||||
|
COAP_OPTION_PROXY_URI = 3, /* 1-270 B */
|
||||||
|
COAP_OPTION_ETAG = 4, /* 1-8 B */
|
||||||
|
COAP_OPTION_URI_HOST = 5, /* 1-270 B */
|
||||||
|
COAP_OPTION_LOCATION_PATH = 6, /* 1-270 B */
|
||||||
|
COAP_OPTION_URI_PORT = 7, /* 0-2 B */
|
||||||
|
COAP_OPTION_LOCATION_QUERY = 8, /* 1-270 B */
|
||||||
|
COAP_OPTION_URI_PATH = 9, /* 1-270 B */
|
||||||
|
COAP_OPTION_OBSERVE = 10, /* 0-2 B */
|
||||||
|
COAP_OPTION_TOKEN = 11, /* 1-8 B */
|
||||||
|
COAP_OPTION_FENCE_POST = 14, /* 0 B */
|
||||||
|
COAP_OPTION_URI_QUERY = 15, /* 1-270 B */
|
||||||
|
COAP_OPTION_BLOCK2 = 17, /* 1-3 B */
|
||||||
|
COAP_OPTION_BLOCK1 = 19, /* 1-3 B */
|
||||||
|
} coap_option_t;
|
||||||
|
|
||||||
|
/* CoAP content-types */
|
||||||
|
typedef enum {
|
||||||
|
TEXT_PLAIN = 0,
|
||||||
|
TEXT_XML = 1, /* Intented types are not in the initial registry. */
|
||||||
|
TEXT_CSV = 2,
|
||||||
|
TEXT_HTML = 3,
|
||||||
|
IMAGE_GIF = 21,
|
||||||
|
IMAGE_JPEG = 22,
|
||||||
|
IMAGE_PNG = 23,
|
||||||
|
IMAGE_TIFF = 24,
|
||||||
|
AUDIO_RAW = 25,
|
||||||
|
VIDEO_RAW = 26,
|
||||||
|
APPLICATION_LINK_FORMAT = 40, /* Actually not in registry!? */
|
||||||
|
APPLICATION_XML = 41,
|
||||||
|
APPLICATION_OCTET_STREAM = 42,
|
||||||
|
APPLICATION_RDF_XML = 43,
|
||||||
|
APPLICATION_SOAP_XML = 44,
|
||||||
|
APPLICATION_ATOM_XML = 45,
|
||||||
|
APPLICATION_XMPP_XML = 46,
|
||||||
|
APPLICATION_EXI = 47,
|
||||||
|
APPLICATION_FASTINFOSET = 48,
|
||||||
|
APPLICATION_SOAP_FASTINFOSET = 49,
|
||||||
|
APPLICATION_JSON = 50,
|
||||||
|
APPLICATION_X_OBIX_BINARY = 51
|
||||||
|
} coap_content_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
|
||||||
|
|
||||||
|
uint8_t version;
|
||||||
|
coap_message_type_t type;
|
||||||
|
uint8_t option_count;
|
||||||
|
uint8_t code;
|
||||||
|
uint16_t tid;
|
||||||
|
|
||||||
|
uint32_t options; /* Bitmap to check if option is set */
|
||||||
|
|
||||||
|
coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */
|
||||||
|
uint32_t max_age;
|
||||||
|
size_t proxy_uri_len;
|
||||||
|
char *proxy_uri;
|
||||||
|
uint8_t etag_len;
|
||||||
|
uint8_t etag[COAP_ETAG_LEN];
|
||||||
|
size_t uri_host_len;
|
||||||
|
char *uri_host;
|
||||||
|
size_t location_path_len;
|
||||||
|
char *location_path;
|
||||||
|
uint16_t uri_port;
|
||||||
|
size_t location_query_len;
|
||||||
|
char *location_query;
|
||||||
|
size_t uri_path_len;
|
||||||
|
char *uri_path;
|
||||||
|
uint16_t observe;
|
||||||
|
uint8_t token_len;
|
||||||
|
uint8_t token[COAP_TOKEN_LEN];
|
||||||
|
uint32_t block2_num;
|
||||||
|
uint8_t block2_more;
|
||||||
|
uint16_t block2_size;
|
||||||
|
uint32_t block2_offset;
|
||||||
|
uint32_t block1_num;
|
||||||
|
uint8_t block1_more;
|
||||||
|
uint16_t block1_size;
|
||||||
|
uint32_t block1_offset;
|
||||||
|
size_t uri_query_len;
|
||||||
|
char *uri_query;
|
||||||
|
|
||||||
|
uint16_t payload_len;
|
||||||
|
uint8_t *payload;
|
||||||
|
|
||||||
|
} coap_packet_t;
|
||||||
|
|
||||||
|
|
||||||
|
/* To store error code and human-readable payload */
|
||||||
|
extern coap_status_t coap_error_code;
|
||||||
|
extern char *coap_error_message;
|
||||||
|
|
||||||
|
void coap_init_connection(uint16_t port);
|
||||||
|
uint16_t coap_get_tid(void);
|
||||||
|
|
||||||
|
void coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid);
|
||||||
|
size_t coap_serialize_message(void *packet, uint8_t *buffer);
|
||||||
|
void coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length);
|
||||||
|
coap_status_t coap_parse_message(void *request, uint8_t *data, uint16_t data_len);
|
||||||
|
|
||||||
|
int coap_get_query_variable(void *packet, const char *name, const char **output);
|
||||||
|
int coap_get_post_variable(void *packet, const char *name, const char **output);
|
||||||
|
|
||||||
|
unsigned int coap_get_header_content_type(void *packet);
|
||||||
|
int coap_set_header_content_type(void *packet, unsigned int content_type);
|
||||||
|
|
||||||
|
int coap_get_header_max_age(void *packet, uint32_t *age);
|
||||||
|
int coap_set_header_max_age(void *packet, uint32_t age);
|
||||||
|
|
||||||
|
int coap_get_header_etag(void *packet, const uint8_t **etag);
|
||||||
|
int coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len);
|
||||||
|
|
||||||
|
int coap_get_header_token(void *packet, const uint8_t **token);
|
||||||
|
int coap_set_header_token(void *packet, uint8_t *token, size_t token_len);
|
||||||
|
|
||||||
|
int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_proxy_uri(void *packet, char *uri);
|
||||||
|
|
||||||
|
int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_uri_host(void *packet, char *host);
|
||||||
|
|
||||||
|
int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_uri_path(void *packet, char *path);
|
||||||
|
|
||||||
|
int coap_get_header_uri_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_uri_query(void *packet, char *query);
|
||||||
|
|
||||||
|
int coap_get_header_location_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_location_path(void *packet, char *path); /* Also splits optional query into Location-Query option. */
|
||||||
|
|
||||||
|
int coap_get_header_location_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_location_query(void *packet, char *query);
|
||||||
|
|
||||||
|
int coap_get_header_observe(void *packet, uint32_t *observe);
|
||||||
|
int coap_set_header_observe(void *packet, uint32_t observe);
|
||||||
|
|
||||||
|
int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
|
||||||
|
int coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size);
|
||||||
|
|
||||||
|
int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
|
||||||
|
int coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size);
|
||||||
|
|
||||||
|
int coap_get_payload(void *packet, const uint8_t **payload);
|
||||||
|
int coap_set_payload(void *packet, uint8_t *payload, size_t length);
|
||||||
|
|
||||||
|
#endif /* COAP_06_H_ */
|
1
apps/er-coap-07/Makefile.er-coap-07
Normal file
|
@ -0,0 +1 @@
|
||||||
|
er-coap-07_src = er-coap-07-engine.c er-coap-07.c er-coap-07-transactions.c er-coap-07-observing.c er-coap-07-separate.c
|
590
apps/er-coap-07/er-coap-07-engine.c
Normal file
|
@ -0,0 +1,590 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP implementation of the REST Engine
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "er-coap-07-engine.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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 PRINTBITS(buf,len) { \
|
||||||
|
int i,j=0; \
|
||||||
|
for (i=0; i<len; ++i) { \
|
||||||
|
for (j=7; j>=0; --j) { \
|
||||||
|
PRINTF("%c", (((char *)buf)[i] & 1<<j) ? '1' : '0'); \
|
||||||
|
} \
|
||||||
|
PRINTF(" "); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define PRINTF(...)
|
||||||
|
#define PRINT6ADDR(addr)
|
||||||
|
#define PRINTLLADDR(addr)
|
||||||
|
#define PRINTBITS(buf,len)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
PROCESS(coap_receiver, "CoAP Receiver");
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Variables -----------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static service_callback_t service_cbk = NULL;
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
int
|
||||||
|
handle_incoming_data(void)
|
||||||
|
{
|
||||||
|
coap_error_code = NO_ERROR;
|
||||||
|
|
||||||
|
PRINTF("handle_incoming_data(): received uip_datalen=%u \n",(uint16_t)uip_datalen());
|
||||||
|
|
||||||
|
uint8_t *data = uip_appdata + uip_ext_len;
|
||||||
|
uint16_t data_len = uip_datalen() - uip_ext_len;
|
||||||
|
|
||||||
|
/* Static declaration reduces stack peaks and program code size. */
|
||||||
|
static coap_packet_t message[1]; /* This way the packet can be treated as pointer as usual. */
|
||||||
|
static coap_packet_t response[1];
|
||||||
|
static coap_transaction_t *transaction = NULL;
|
||||||
|
|
||||||
|
if (uip_newdata()) {
|
||||||
|
|
||||||
|
PRINTF("receiving UDP datagram from: ");
|
||||||
|
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
|
||||||
|
PRINTF(":%u\n Length: %u\n Data: ", uip_ntohs(UIP_UDP_BUF->srcport), data_len );
|
||||||
|
PRINTBITS(data, data_len);
|
||||||
|
PRINTF("\n");
|
||||||
|
|
||||||
|
coap_error_code = coap_parse_message(message, data, data_len);
|
||||||
|
|
||||||
|
if (coap_error_code==NO_ERROR)
|
||||||
|
{
|
||||||
|
|
||||||
|
/*TODO duplicates suppression, if required */
|
||||||
|
|
||||||
|
PRINTF(" Parsed: v %u, t %u, oc %u, c %u, tid %u\n", message->version, message->type, message->option_count, message->code, message->tid);
|
||||||
|
PRINTF(" URL: %.*s\n", message->uri_path_len, message->uri_path);
|
||||||
|
PRINTF(" Payload: %.*s\n", message->payload_len, message->payload);
|
||||||
|
|
||||||
|
/* Handle requests. */
|
||||||
|
if (message->code >= COAP_GET && message->code <= COAP_DELETE)
|
||||||
|
{
|
||||||
|
/* Use transaction buffer for response to confirmable request. */
|
||||||
|
if ( (transaction = coap_new_transaction(message->tid, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport)) )
|
||||||
|
{
|
||||||
|
static uint32_t block_num = 0;
|
||||||
|
static uint16_t block_size = REST_MAX_CHUNK_SIZE;
|
||||||
|
static uint32_t block_offset = 0;
|
||||||
|
static int32_t new_offset = 0;
|
||||||
|
|
||||||
|
/* prepare response */
|
||||||
|
if (message->type==COAP_TYPE_CON)
|
||||||
|
{
|
||||||
|
/* Reliable CON requests are answered with an ACK. */
|
||||||
|
coap_init_message(response, COAP_TYPE_ACK, CONTENT_2_05, message->tid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Unreliable NON requests are answered with a NON as well. */
|
||||||
|
coap_init_message(response, COAP_TYPE_NON, CONTENT_2_05, coap_get_tid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* resource handlers must take care of different handling (e.g., TOKEN_OPTION_REQUIRED_240) */
|
||||||
|
if (IS_OPTION(message, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
coap_set_header_token(response, message->token, message->token_len);
|
||||||
|
SET_OPTION(response, COAP_OPTION_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* get offset for blockwise transfers */
|
||||||
|
if (coap_get_header_block2(message, &block_num, NULL, &block_size, &block_offset))
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: block request %lu (%u/%u) @ %lu bytes\n", block_num, block_size, REST_MAX_CHUNK_SIZE, block_offset);
|
||||||
|
block_size = MIN(block_size, REST_MAX_CHUNK_SIZE);
|
||||||
|
new_offset = block_offset;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Invoke resource handler. */
|
||||||
|
if (service_cbk)
|
||||||
|
{
|
||||||
|
/* Call REST framework and check if found and allowed. */
|
||||||
|
if (service_cbk(message, response, transaction->packet+COAP_MAX_HEADER_SIZE, block_size, &new_offset))
|
||||||
|
{
|
||||||
|
/* Apply blockwise transfers. */
|
||||||
|
if ( IS_OPTION(message, COAP_OPTION_BLOCK2) )
|
||||||
|
{
|
||||||
|
/* unchanged new_offset indicates that resource is unaware of blockwise transfer */
|
||||||
|
if (new_offset==block_offset)
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: unaware resource with payload length %u/%u\n", response->payload_len, block_size);
|
||||||
|
if (block_offset >= response->payload_len)
|
||||||
|
{
|
||||||
|
PRINTF("handle_incoming_data(): block_offset >= response->payload_len\n");
|
||||||
|
|
||||||
|
response->code = BAD_OPTION_4_02;
|
||||||
|
coap_set_payload(response, (uint8_t*)"BlockOutOfScope", 15);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_set_header_block2(response, block_num, response->payload_len - block_offset > block_size, block_size);
|
||||||
|
coap_set_payload(response, response->payload+block_offset, MIN(response->payload_len - block_offset, block_size));
|
||||||
|
} /* if (valid offset) */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* resource provides chunk-wise data */
|
||||||
|
PRINTF("Blockwise: blockwise resource, new offset %ld\n", new_offset);
|
||||||
|
coap_set_header_block2(response, block_num, new_offset!=-1 || response->payload_len > block_size, block_size);
|
||||||
|
if (response->payload_len > block_size) coap_set_payload(response, response->payload, block_size);
|
||||||
|
} /* if (resource aware of blockwise) */
|
||||||
|
}
|
||||||
|
else if (new_offset!=0)
|
||||||
|
{
|
||||||
|
PRINTF("Blockwise: no block option for blockwise resource, using block size %u\n", REST_MAX_CHUNK_SIZE);
|
||||||
|
|
||||||
|
coap_set_header_block2(response, 0, new_offset!=-1, REST_MAX_CHUNK_SIZE);
|
||||||
|
coap_set_payload(response, response->payload, MIN(response->payload_len, REST_MAX_CHUNK_SIZE));
|
||||||
|
} /* if (blockwise request) */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_error_code = INTERNAL_SERVER_ERROR_5_00;
|
||||||
|
coap_error_message = "Service callback undefined";
|
||||||
|
} /* if (service callback) */
|
||||||
|
|
||||||
|
/* serialize Response. */
|
||||||
|
if ((transaction->packet_len = coap_serialize_message(response, transaction->packet))==0)
|
||||||
|
{
|
||||||
|
coap_error_code = PACKET_SERIALIZATION_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
coap_error_code = MEMORY_ALLOC_ERR;
|
||||||
|
coap_error_message = "Transaction buffer allocation failed";
|
||||||
|
} /* if (transaction buffer) */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Responses */
|
||||||
|
|
||||||
|
if (message->type==COAP_TYPE_ACK)
|
||||||
|
{
|
||||||
|
PRINTF("Received ACK\n");
|
||||||
|
}
|
||||||
|
else if (message->type==COAP_TYPE_RST)
|
||||||
|
{
|
||||||
|
PRINTF("Received RST\n");
|
||||||
|
/* Cancel possible subscriptions. */
|
||||||
|
if (IS_OPTION(message, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
PRINTF(" Token 0x%02X%02X\n", message->token[0], message->token[1]);
|
||||||
|
coap_remove_observer_by_token(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, message->token, message->token_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (transaction = coap_get_transaction_by_tid(message->tid)) )
|
||||||
|
{
|
||||||
|
/* Free transaction memory before callback, as it may create a new transaction. */
|
||||||
|
restful_response_handler callback = transaction->callback;
|
||||||
|
void *callback_data = transaction->callback_data;
|
||||||
|
coap_clear_transaction(transaction);
|
||||||
|
|
||||||
|
/* Check if someone registered for the response */
|
||||||
|
if (callback) {
|
||||||
|
callback(callback_data, message);
|
||||||
|
}
|
||||||
|
} /* if (ACKed transaction) */
|
||||||
|
transaction = NULL;
|
||||||
|
}
|
||||||
|
} /* if (parsed correctly) */
|
||||||
|
|
||||||
|
if (coap_error_code==NO_ERROR) {
|
||||||
|
if (transaction) coap_send_transaction(transaction);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("ERROR %u: %s\n", coap_error_code, coap_error_message);
|
||||||
|
coap_clear_transaction(transaction);
|
||||||
|
|
||||||
|
/* Set to sendable error code. */
|
||||||
|
if (coap_error_code >= 192)
|
||||||
|
{
|
||||||
|
coap_error_code = INTERNAL_SERVER_ERROR_5_00;
|
||||||
|
}
|
||||||
|
/* Reuse input buffer for error message. */
|
||||||
|
coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->tid);
|
||||||
|
coap_set_payload(message, (uint8_t *) coap_error_message, strlen(coap_error_message));
|
||||||
|
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, data, coap_serialize_message(message, data));
|
||||||
|
}
|
||||||
|
} /* if (new data) */
|
||||||
|
|
||||||
|
return coap_error_code;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_receiver_init()
|
||||||
|
{
|
||||||
|
process_start(&coap_receiver, NULL);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_set_service_callback(service_callback_t callback)
|
||||||
|
{
|
||||||
|
service_cbk = callback;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
rest_resource_flags_t
|
||||||
|
coap_get_rest_method(void *packet)
|
||||||
|
{
|
||||||
|
return (rest_resource_flags_t)(1 << (((coap_packet_t *)packet)->code - 1));
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
int
|
||||||
|
coap_set_rest_status(void *packet, unsigned int code)
|
||||||
|
{
|
||||||
|
if (code <= 0xFF)
|
||||||
|
{
|
||||||
|
((coap_packet_t *)packet)->code = (uint8_t) code;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Server part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* The discover resource is automatically included for CoAP. */
|
||||||
|
RESOURCE(well_known_core, METHOD_GET, ".well-known/core", "");
|
||||||
|
void
|
||||||
|
well_known_core_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
|
{
|
||||||
|
size_t strpos = 0;
|
||||||
|
size_t bufpos = 0;
|
||||||
|
size_t tmplen = 0;
|
||||||
|
resource_t* resource = NULL;
|
||||||
|
|
||||||
|
for (resource = (resource_t*)list_head(rest_get_resources()); resource; resource = resource->next)
|
||||||
|
{
|
||||||
|
PRINTF("res: /%s (%p)\npos: s%d, o%d, b%d\n", resource->url, resource, strpos, *offset, bufpos);
|
||||||
|
|
||||||
|
if (strpos >= *offset && bufpos < preferred_size)
|
||||||
|
{
|
||||||
|
buffer[bufpos++] = '<';
|
||||||
|
}
|
||||||
|
++strpos;
|
||||||
|
|
||||||
|
if (strpos >= *offset && bufpos < preferred_size)
|
||||||
|
{
|
||||||
|
buffer[bufpos++] = '/';
|
||||||
|
}
|
||||||
|
++strpos;
|
||||||
|
|
||||||
|
tmplen = strlen(resource->url);
|
||||||
|
if (strpos+tmplen > *offset)
|
||||||
|
{
|
||||||
|
bufpos += snprintf((char *) buffer + bufpos, preferred_size - bufpos + 1,
|
||||||
|
"%s", resource->url + ((*offset-(int32_t)strpos > 0) ? (*offset-(int32_t)strpos) : 0));
|
||||||
|
/* minimal-net requires these casts */
|
||||||
|
}
|
||||||
|
strpos += tmplen;
|
||||||
|
|
||||||
|
if (strpos >= *offset && bufpos < preferred_size)
|
||||||
|
{
|
||||||
|
buffer[bufpos++] = '>';
|
||||||
|
}
|
||||||
|
++strpos;
|
||||||
|
|
||||||
|
if (resource->attributes[0])
|
||||||
|
{
|
||||||
|
if (strpos >= *offset && bufpos < preferred_size)
|
||||||
|
{
|
||||||
|
buffer[bufpos++] = ';';
|
||||||
|
}
|
||||||
|
++strpos;
|
||||||
|
|
||||||
|
tmplen = strlen(resource->attributes);
|
||||||
|
if (strpos+tmplen > *offset)
|
||||||
|
{
|
||||||
|
bufpos += snprintf((char *) buffer + bufpos, preferred_size - bufpos + 1,
|
||||||
|
"%s", resource->attributes + (*offset-(int32_t)strpos > 0 ? *offset-(int32_t)strpos : 0));
|
||||||
|
}
|
||||||
|
strpos += tmplen;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource->next)
|
||||||
|
{
|
||||||
|
if (strpos >= *offset && bufpos < preferred_size)
|
||||||
|
{
|
||||||
|
buffer[bufpos++] = ',';
|
||||||
|
}
|
||||||
|
++strpos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* buffer full, but resource not completed yet; or: do not break if resource exactly fills buffer. */
|
||||||
|
if (bufpos >= preferred_size && strpos-bufpos > *offset)
|
||||||
|
{
|
||||||
|
PRINTF("res: BREAK at %s (%p)\n", resource->url, resource);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bufpos>0) {
|
||||||
|
PRINTF("BUF %d: %.*s\n", bufpos, bufpos, (char *) buffer);
|
||||||
|
|
||||||
|
coap_set_payload(response, buffer, bufpos );
|
||||||
|
coap_set_header_content_type(response, APPLICATION_LINK_FORMAT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("well_known_core_handler(): bufpos<=0\n");
|
||||||
|
|
||||||
|
coap_set_rest_status(response, BAD_OPTION_4_02);
|
||||||
|
coap_set_payload(response, (uint8_t*)"BlockOutOfScope", 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource==NULL) {
|
||||||
|
PRINTF("res: DONE\n");
|
||||||
|
*offset = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("res: MORE at %s (%p)\n", resource->url, resource);
|
||||||
|
*offset += bufpos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PROCESS_THREAD(coap_receiver, ev, data)
|
||||||
|
{
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
PRINTF("Starting CoAP-07 receiver...\n");
|
||||||
|
|
||||||
|
rest_activate_resource(&resource_well_known_core);
|
||||||
|
|
||||||
|
coap_register_as_transaction_handler();
|
||||||
|
coap_init_connection(SERVER_LISTEN_PORT);
|
||||||
|
PRINTF("Listening on port %u\n", UIP_HTONS(SERVER_LISTEN_PORT));
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PROCESS_YIELD();
|
||||||
|
|
||||||
|
if(ev == tcpip_event) {
|
||||||
|
handle_incoming_data();
|
||||||
|
} else if (ev == PROCESS_EVENT_TIMER) {
|
||||||
|
/* retransmissions are handled here */
|
||||||
|
coap_check_transactions();
|
||||||
|
}
|
||||||
|
} /* while (1) */
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Client part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void blocking_request_callback(void *callback_data, void *response) {
|
||||||
|
struct request_state_t *state = (struct request_state_t *) callback_data;
|
||||||
|
state->response = (coap_packet_t*) response;
|
||||||
|
process_poll(state->process);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
|
||||||
|
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
|
||||||
|
coap_packet_t *request,
|
||||||
|
blocking_response_handler request_callback)) {
|
||||||
|
PT_BEGIN(&state->pt);
|
||||||
|
|
||||||
|
static uint8_t more;
|
||||||
|
static uint32_t res_block;
|
||||||
|
static uint8_t block_error;
|
||||||
|
|
||||||
|
state->block_num = 0;
|
||||||
|
state->response = NULL;
|
||||||
|
state->process = PROCESS_CURRENT();
|
||||||
|
|
||||||
|
more = 0;
|
||||||
|
res_block = 0;
|
||||||
|
block_error = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
request->tid = coap_get_tid();
|
||||||
|
if ((state->transaction = coap_new_transaction(request->tid, remote_ipaddr, remote_port)))
|
||||||
|
{
|
||||||
|
state->transaction->callback = blocking_request_callback;
|
||||||
|
state->transaction->callback_data = state;
|
||||||
|
|
||||||
|
if (state->block_num>0)
|
||||||
|
{
|
||||||
|
coap_set_header_block2(request, state->block_num, 0, REST_MAX_CHUNK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
state->transaction->packet_len = coap_serialize_message(request, state->transaction->packet);
|
||||||
|
|
||||||
|
coap_send_transaction(state->transaction);
|
||||||
|
PRINTF("Requested #%lu (TID %u)\n", state->block_num, request->tid);
|
||||||
|
|
||||||
|
PT_YIELD_UNTIL(&state->pt, ev == PROCESS_EVENT_POLL);
|
||||||
|
|
||||||
|
if (!state->response)
|
||||||
|
{
|
||||||
|
PRINTF("Server not responding\n");
|
||||||
|
PT_EXIT(&state->pt);
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
|
||||||
|
|
||||||
|
PRINTF("Received #%lu%s (%u bytes)\n", res_block, more ? "+" : "", state->response->payload_len);
|
||||||
|
|
||||||
|
if (res_block==state->block_num)
|
||||||
|
{
|
||||||
|
request_callback(state->response);
|
||||||
|
++(state->block_num);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("WRONG BLOCK %lu/%lu\n", res_block, state->block_num);
|
||||||
|
++block_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("Could not allocate transaction buffer");
|
||||||
|
PT_EXIT(&state->pt);
|
||||||
|
}
|
||||||
|
} while (more && block_error<COAP_MAX_ATTEMPTS);
|
||||||
|
|
||||||
|
PT_END(&state->pt);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Engine Interface ----------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
const struct rest_implementation coap_rest_implementation = {
|
||||||
|
"CoAP-07",
|
||||||
|
|
||||||
|
coap_receiver_init,
|
||||||
|
coap_set_service_callback,
|
||||||
|
|
||||||
|
coap_get_header_uri_path,
|
||||||
|
coap_set_header_uri_path,
|
||||||
|
coap_get_rest_method,
|
||||||
|
coap_set_rest_status,
|
||||||
|
|
||||||
|
coap_get_header_content_type,
|
||||||
|
coap_set_header_content_type,
|
||||||
|
coap_get_header_accept,
|
||||||
|
coap_get_header_max_age,
|
||||||
|
coap_set_header_max_age,
|
||||||
|
coap_set_header_etag,
|
||||||
|
coap_get_header_if_match,
|
||||||
|
coap_get_header_if_none_match,
|
||||||
|
coap_get_header_uri_host,
|
||||||
|
coap_set_header_location_path,
|
||||||
|
|
||||||
|
coap_get_payload,
|
||||||
|
coap_set_payload,
|
||||||
|
|
||||||
|
coap_get_header_uri_query,
|
||||||
|
coap_get_query_variable,
|
||||||
|
coap_get_post_variable,
|
||||||
|
|
||||||
|
coap_notify_observers,
|
||||||
|
(restful_post_handler) coap_observe_handler,
|
||||||
|
|
||||||
|
NULL, /* default pre-handler (set separate handler after activation if needed) */
|
||||||
|
NULL, /* default post-handler for non-observable resources */
|
||||||
|
|
||||||
|
{
|
||||||
|
CONTENT_2_05,
|
||||||
|
CREATED_2_01,
|
||||||
|
CHANGED_2_04,
|
||||||
|
DELETED_2_02,
|
||||||
|
VALID_2_03,
|
||||||
|
BAD_REQUEST_4_00,
|
||||||
|
UNAUTHORIZED_4_01,
|
||||||
|
BAD_OPTION_4_02,
|
||||||
|
FORBIDDEN_4_03,
|
||||||
|
NOT_FOUND_4_04,
|
||||||
|
METHOD_NOT_ALLOWED_4_05,
|
||||||
|
REQUEST_ENTITY_TOO_LARGE_4_13,
|
||||||
|
UNSUPPORTED_MADIA_TYPE_4_15,
|
||||||
|
INTERNAL_SERVER_ERROR_5_00,
|
||||||
|
NOT_IMPLEMENTED_5_01,
|
||||||
|
BAD_GATEWAY_5_02,
|
||||||
|
SERVICE_UNAVAILABLE_5_03,
|
||||||
|
GATEWAY_TIMEOUT_5_04,
|
||||||
|
PROXYING_NOT_SUPPORTED_5_05
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
TEXT_PLAIN,
|
||||||
|
TEXT_XML,
|
||||||
|
TEXT_CSV,
|
||||||
|
TEXT_HTML,
|
||||||
|
IMAGE_GIF,
|
||||||
|
IMAGE_JPEG,
|
||||||
|
IMAGE_PNG,
|
||||||
|
IMAGE_TIFF,
|
||||||
|
AUDIO_RAW,
|
||||||
|
VIDEO_RAW,
|
||||||
|
APPLICATION_LINK_FORMAT,
|
||||||
|
APPLICATION_XML,
|
||||||
|
APPLICATION_OCTET_STREAM,
|
||||||
|
APPLICATION_RDF_XML,
|
||||||
|
APPLICATION_SOAP_XML,
|
||||||
|
APPLICATION_ATOM_XML,
|
||||||
|
APPLICATION_XMPP_XML,
|
||||||
|
APPLICATION_EXI,
|
||||||
|
APPLICATION_FASTINFOSET,
|
||||||
|
APPLICATION_SOAP_FASTINFOSET,
|
||||||
|
APPLICATION_JSON,
|
||||||
|
APPLICATION_X_OBIX_BINARY
|
||||||
|
}
|
||||||
|
};
|
92
apps/er-coap-07/er-coap-07-engine.h
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP implementation of the REST Engine
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_SERVER_H_
|
||||||
|
#define COAP_SERVER_H_
|
||||||
|
|
||||||
|
#if !defined(REST)
|
||||||
|
#error "Define REST to \"coap_rest_implementation\""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "er-coap-07.h"
|
||||||
|
#include "er-coap-07-transactions.h"
|
||||||
|
#include "er-coap-07-observing.h"
|
||||||
|
#include "er-coap-07-separate.h"
|
||||||
|
|
||||||
|
#include "pt.h"
|
||||||
|
|
||||||
|
/* Declare server process */
|
||||||
|
PROCESS_NAME(coap_receiver);
|
||||||
|
|
||||||
|
#define SERVER_LISTEN_PORT UIP_HTONS(COAP_SERVER_PORT)
|
||||||
|
|
||||||
|
typedef coap_packet_t rest_request_t;
|
||||||
|
typedef coap_packet_t rest_response_t;
|
||||||
|
|
||||||
|
extern const struct rest_implementation coap_rest_implementation;
|
||||||
|
|
||||||
|
void coap_receiver_init(void);
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/*- Client part ---------------------------------------------------------------------*/
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
struct request_state_t {
|
||||||
|
struct pt pt;
|
||||||
|
struct process *process;
|
||||||
|
coap_transaction_t *transaction;
|
||||||
|
coap_packet_t *response;
|
||||||
|
uint32_t block_num;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*blocking_response_handler) (void* response);
|
||||||
|
|
||||||
|
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
|
||||||
|
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
|
||||||
|
coap_packet_t *request,
|
||||||
|
blocking_response_handler request_callback));
|
||||||
|
|
||||||
|
#define COAP_BLOCKING_REQUEST(server_addr, server_port, request, chunk_handler) \
|
||||||
|
static struct request_state_t request_state; \
|
||||||
|
PT_SPAWN(process_pt, &request_state.pt, \
|
||||||
|
coap_blocking_request(&request_state, ev, \
|
||||||
|
server_addr, server_port, \
|
||||||
|
request, chunk_handler) \
|
||||||
|
);
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#endif /* COAP_SERVER_H_ */
|
218
apps/er-coap-07/er-coap-07-observing.c
Normal file
|
@ -0,0 +1,218 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for observing resources
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "er-coap-07-observing.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
|
||||||
|
LIST(observers_list);
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
coap_observer_t *
|
||||||
|
coap_add_observer(const char *url, uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
coap_observer_t *o = memb_alloc(&observers_memb);
|
||||||
|
|
||||||
|
if (o)
|
||||||
|
{
|
||||||
|
o->url = url;
|
||||||
|
uip_ipaddr_copy(&o->addr, addr);
|
||||||
|
o->port = port;
|
||||||
|
o->token_len = token_len;
|
||||||
|
memcpy(o->token, token, token_len);
|
||||||
|
|
||||||
|
stimer_set(&o->refresh_timer, COAP_OBSERVING_REFRESH_INTERVAL);
|
||||||
|
|
||||||
|
PRINTF("Adding observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
||||||
|
list_add(observers_list, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_remove_observer(coap_observer_t *o)
|
||||||
|
{
|
||||||
|
PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
||||||
|
|
||||||
|
memb_free(&observers_memb, o);
|
||||||
|
list_remove(observers_list, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check client ");
|
||||||
|
PRINT6ADDR(addr);
|
||||||
|
PRINTF(":%u\n", port);
|
||||||
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]);
|
||||||
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port && memcmp(obs->token, token, token_len)==0)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
int
|
||||||
|
coap_remove_observer_by_url(const char *url)
|
||||||
|
{
|
||||||
|
int removed = 0;
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
PRINTF("Remove check URL %p\n", url);
|
||||||
|
if (obs->url==url || memcmp(obs->url, url, strlen(obs->url))==0)
|
||||||
|
{
|
||||||
|
coap_remove_observer(obs);
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
coap_observer_t* obs = NULL;
|
||||||
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
||||||
|
{
|
||||||
|
if (obs->url==url) /* using RESOURCE url pointer as handle */
|
||||||
|
{
|
||||||
|
coap_transaction_t *transaction = NULL;
|
||||||
|
|
||||||
|
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */
|
||||||
|
|
||||||
|
if ( (transaction = coap_new_transaction(coap_get_tid(), &obs->addr, obs->port)) )
|
||||||
|
{
|
||||||
|
/* Use CON to check whether client is still there/interested after COAP_OBSERVING_REFRESH_INTERVAL. */
|
||||||
|
if (stimer_expired(&obs->refresh_timer))
|
||||||
|
{
|
||||||
|
PRINTF("Observing: Refresh client with CON\n");
|
||||||
|
type = COAP_TYPE_CON;
|
||||||
|
stimer_restart(&obs->refresh_timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prepare response */
|
||||||
|
coap_packet_t push[1]; /* This way the packet can be treated as pointer as usual. */
|
||||||
|
coap_init_message(push, (coap_message_type_t)type, CONTENT_2_05, transaction->tid );
|
||||||
|
coap_set_header_observe(push, observe);
|
||||||
|
coap_set_header_token(push, obs->token, obs->token_len);
|
||||||
|
coap_set_payload(push, payload, payload_len);
|
||||||
|
transaction->packet_len = coap_serialize_message(push, transaction->packet);
|
||||||
|
|
||||||
|
PRINTF("Observing: Notify from /%s for ", url);
|
||||||
|
PRINT6ADDR(&obs->addr);
|
||||||
|
PRINTF(":%u\n", obs->port);
|
||||||
|
PRINTF(" %.*s\n", payload_len, payload);
|
||||||
|
|
||||||
|
coap_send_transaction(transaction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
coap_observe_handler(resource_t *resource, void *request, void *response)
|
||||||
|
{
|
||||||
|
static char content[26];
|
||||||
|
|
||||||
|
if (response && ((coap_packet_t *)response)->code<128) /* response without error code */
|
||||||
|
{
|
||||||
|
if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_OBSERVE))
|
||||||
|
{
|
||||||
|
if (!IS_OPTION((coap_packet_t *)request, COAP_OPTION_TOKEN))
|
||||||
|
{
|
||||||
|
/* Set default token. */
|
||||||
|
coap_set_header_token(request, (uint8_t *)"", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (coap_add_observer(resource->url, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, ((coap_packet_t *)request)->token, ((coap_packet_t *)request)->token_len))
|
||||||
|
{
|
||||||
|
coap_set_header_observe(response, 0);
|
||||||
|
coap_set_payload(response, (uint8_t *)content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((coap_packet_t *)response)->code = SERVICE_UNAVAILABLE_5_03;
|
||||||
|
coap_set_payload(response, (uint8_t *)"Too many observers", 18);
|
||||||
|
} /* if (added observer) */
|
||||||
|
}
|
||||||
|
else /* if (observe) */
|
||||||
|
{
|
||||||
|
/* Remove client if it is currently observing. */
|
||||||
|
coap_remove_observer_by_client(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport);
|
||||||
|
} /* if (observe) */
|
||||||
|
}
|
||||||
|
}
|
80
apps/er-coap-07/er-coap-07-observing.h
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for observing resources
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_OBSERVING_H_
|
||||||
|
#define COAP_OBSERVING_H_
|
||||||
|
|
||||||
|
#include "er-coap-07.h"
|
||||||
|
#include "er-coap-07-transactions.h"
|
||||||
|
|
||||||
|
#ifndef COAP_MAX_OBSERVERS
|
||||||
|
#define COAP_MAX_OBSERVERS 4
|
||||||
|
#endif /* COAP_MAX_OBSERVERS */
|
||||||
|
|
||||||
|
/* Interval in seconds in which NON notifies are changed to CON notifies to check client. */
|
||||||
|
#define COAP_OBSERVING_REFRESH_INTERVAL 60
|
||||||
|
|
||||||
|
#if COAP_MAX_OPEN_TRANSACTIONS<COAP_MAX_OBSERVERS
|
||||||
|
#warning "COAP_MAX_OPEN_TRANSACTIONS smaller than COAP_MAX_OBSERVERS: cannot handle CON notifications"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct coap_observer {
|
||||||
|
struct coap_observer *next; /* for LIST */
|
||||||
|
|
||||||
|
const char *url;
|
||||||
|
uip_ipaddr_t addr;
|
||||||
|
uint16_t port;
|
||||||
|
uint8_t token_len;
|
||||||
|
uint8_t token[COAP_TOKEN_LEN];
|
||||||
|
struct stimer refresh_timer;
|
||||||
|
} coap_observer_t;
|
||||||
|
|
||||||
|
list_t coap_get_observers(void);
|
||||||
|
|
||||||
|
coap_observer_t *coap_add_observer(const char *url, uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len);
|
||||||
|
void coap_remove_observer(coap_observer_t *o);
|
||||||
|
|
||||||
|
int coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port);
|
||||||
|
int coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len);
|
||||||
|
int coap_remove_observer_by_url(const char *url);
|
||||||
|
|
||||||
|
void coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len);
|
||||||
|
|
||||||
|
void coap_observe_handler(resource_t *resource, void *request, void *response);
|
||||||
|
|
||||||
|
#endif /* COAP_OBSERVING_H_ */
|
69
apps/er-coap-07/er-coap-07-separate.c
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for separate responses
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "er-coap-07-separate.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void coap_separate_handler(resource_t *resource, void *request, void *response)
|
||||||
|
{
|
||||||
|
PRINTF("Separate response for /%s \n", resource->url);
|
||||||
|
/* send separate ACK. */
|
||||||
|
coap_packet_t ack[1];
|
||||||
|
/* ACK with empty code (0) */
|
||||||
|
coap_init_message(ack, COAP_TYPE_ACK, 0, ((coap_packet_t *)request)->tid);
|
||||||
|
/* Should only overwrite Header which is already parsed to request. */
|
||||||
|
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata + uip_ext_len), coap_serialize_message(ack, (uip_appdata + uip_ext_len)));
|
||||||
|
|
||||||
|
/* Change response to separate response. */
|
||||||
|
((coap_packet_t *)response)->type = COAP_TYPE_CON;
|
||||||
|
((coap_packet_t *)response)->tid = coap_get_tid();
|
||||||
|
}
|
46
apps/er-coap-07/er-coap-07-separate.h
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for separate responses
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_SEPARATE_H_
|
||||||
|
#define COAP_SEPARATE_H_
|
||||||
|
|
||||||
|
#include "er-coap-07.h"
|
||||||
|
|
||||||
|
void coap_separate_handler(resource_t *resource, void *request, void *response);
|
||||||
|
|
||||||
|
#endif /* COAP_SEPARATE_H_ */
|
195
apps/er-coap-07/er-coap-07-transactions.c
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for reliable transport
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "er-coap-07-transactions.h"
|
||||||
|
#include "er-coap-07-observing.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Modulo mask (+1 and +0.5 for rounding) for a random number to get the tick number for the random
|
||||||
|
* retransmission time between COAP_RESPONSE_TIMEOUT and COAP_RESPONSE_TIMEOUT*COAP_RESPONSE_RANDOM_FACTOR.
|
||||||
|
*/
|
||||||
|
#define COAP_RESPONSE_TIMEOUT_TICKS (CLOCK_SECOND * COAP_RESPONSE_TIMEOUT)
|
||||||
|
#define COAP_RESPONSE_TIMEOUT_BACKOFF_MASK ((CLOCK_SECOND * COAP_RESPONSE_TIMEOUT * (COAP_RESPONSE_RANDOM_FACTOR - 1)) + 1.5)
|
||||||
|
|
||||||
|
#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]", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
|
||||||
|
MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
|
||||||
|
LIST(transactions_list);
|
||||||
|
|
||||||
|
|
||||||
|
static struct process *transaction_handler_process = NULL;
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_register_as_transaction_handler()
|
||||||
|
{
|
||||||
|
transaction_handler_process = PROCESS_CURRENT();
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_transaction_t *
|
||||||
|
coap_new_transaction(uint16_t tid, uip_ipaddr_t *addr, uint16_t port)
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = memb_alloc(&transactions_memb);
|
||||||
|
|
||||||
|
if (t)
|
||||||
|
{
|
||||||
|
t->tid = tid;
|
||||||
|
t->retrans_counter = 0;
|
||||||
|
|
||||||
|
/* save client address */
|
||||||
|
uip_ipaddr_copy(&t->addr, addr);
|
||||||
|
t->port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_send_transaction(coap_transaction_t *t)
|
||||||
|
{
|
||||||
|
PRINTF("Sending transaction %u\n", t->tid);
|
||||||
|
|
||||||
|
coap_send_message(&t->addr, t->port, t->packet, t->packet_len);
|
||||||
|
|
||||||
|
if (COAP_TYPE_CON==((COAP_HEADER_TYPE_MASK & t->packet[0])>>COAP_HEADER_TYPE_POSITION))
|
||||||
|
{
|
||||||
|
if (t->retrans_counter<COAP_MAX_RETRANSMIT)
|
||||||
|
{
|
||||||
|
PRINTF("Keeping transaction %u\n", t->tid);
|
||||||
|
|
||||||
|
if (t->retrans_counter==0)
|
||||||
|
{
|
||||||
|
t->retrans_timer.timer.interval = COAP_RESPONSE_TIMEOUT_TICKS + (random_rand() % (clock_time_t) COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
|
||||||
|
PRINTF("Initial interval %f\n", (float)t->retrans_timer.timer.interval/CLOCK_SECOND);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
t->retrans_timer.timer.interval <<= 1; /* double */
|
||||||
|
PRINTF("Doubled (%u) interval %f\n", t->retrans_counter, (float)t->retrans_timer.timer.interval/CLOCK_SECOND);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*FIXME hack, maybe there is a better way, but avoid posting everything to the process */
|
||||||
|
struct process *process_actual = PROCESS_CURRENT();
|
||||||
|
process_current = transaction_handler_process;
|
||||||
|
etimer_restart(&t->retrans_timer); /* interval updated above */
|
||||||
|
process_current = process_actual;
|
||||||
|
|
||||||
|
list_add(transactions_list, t); /* List itself makes sure same element is not added twice. */
|
||||||
|
|
||||||
|
t = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* timeout */
|
||||||
|
PRINTF("Timeout\n");
|
||||||
|
restful_response_handler callback = t->callback;
|
||||||
|
void *callback_data = t->callback_data;
|
||||||
|
|
||||||
|
/* handle observers */
|
||||||
|
coap_remove_observer_by_client(&t->addr, t->port);
|
||||||
|
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback(callback_data, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coap_clear_transaction(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_clear_transaction(coap_transaction_t *t)
|
||||||
|
{
|
||||||
|
if (t)
|
||||||
|
{
|
||||||
|
PRINTF("Freeing transaction %u: %p\n", t->tid, t);
|
||||||
|
|
||||||
|
etimer_stop(&t->retrans_timer);
|
||||||
|
list_remove(transactions_list, t);
|
||||||
|
memb_free(&transactions_memb, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
coap_transaction_t *
|
||||||
|
coap_get_transaction_by_tid(uint16_t tid)
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = NULL;
|
||||||
|
|
||||||
|
for (t = (coap_transaction_t*)list_head(transactions_list); t; t = t->next)
|
||||||
|
{
|
||||||
|
if (t->tid==tid)
|
||||||
|
{
|
||||||
|
PRINTF("Found transaction for TID %u: %p\n", t->tid, t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
coap_check_transactions()
|
||||||
|
{
|
||||||
|
coap_transaction_t *t = NULL;
|
||||||
|
|
||||||
|
for (t = (coap_transaction_t*)list_head(transactions_list); t; t = t->next)
|
||||||
|
{
|
||||||
|
if (etimer_expired(&t->retrans_timer))
|
||||||
|
{
|
||||||
|
++(t->retrans_counter);
|
||||||
|
PRINTF("Retransmitting %u (%u)\n", t->tid, t->retrans_counter);
|
||||||
|
coap_send_transaction(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
78
apps/er-coap-07/er-coap-07-transactions.h
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* CoAP module for reliable transport
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_TRANSACTIONS_H_
|
||||||
|
#define COAP_TRANSACTIONS_H_
|
||||||
|
|
||||||
|
#include "er-coap-07.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The number of concurrent messages that can be stored for retransmission in the transaction layer.
|
||||||
|
*/
|
||||||
|
#ifndef COAP_MAX_OPEN_TRANSACTIONS
|
||||||
|
#define COAP_MAX_OPEN_TRANSACTIONS 4
|
||||||
|
#endif /* COAP_MAX_OPEN_TRANSACTIONS */
|
||||||
|
|
||||||
|
/* container for transactions with message buffer and retransmission info */
|
||||||
|
typedef struct coap_transaction {
|
||||||
|
struct coap_transaction *next; /* for LIST */
|
||||||
|
|
||||||
|
uint16_t tid;
|
||||||
|
struct etimer retrans_timer;
|
||||||
|
uint8_t retrans_counter;
|
||||||
|
|
||||||
|
uip_ipaddr_t addr;
|
||||||
|
uint16_t port;
|
||||||
|
|
||||||
|
restful_response_handler callback;
|
||||||
|
void *callback_data;
|
||||||
|
|
||||||
|
uint16_t packet_len;
|
||||||
|
uint8_t packet[COAP_MAX_PACKET_SIZE+1]; /* +1 for the terminating '\0' to simply and savely use snprintf(buf, len+1, "", ...) in the resource handler. */
|
||||||
|
} coap_transaction_t;
|
||||||
|
|
||||||
|
void coap_register_as_transaction_handler();
|
||||||
|
|
||||||
|
coap_transaction_t *coap_new_transaction(uint16_t tid, uip_ipaddr_t *addr, uint16_t port);
|
||||||
|
void coap_send_transaction(coap_transaction_t *t);
|
||||||
|
void coap_clear_transaction(coap_transaction_t *t);
|
||||||
|
coap_transaction_t *coap_get_transaction_by_tid(uint16_t tid);
|
||||||
|
|
||||||
|
void coap_check_transactions();
|
||||||
|
|
||||||
|
#endif /* COAP_TRANSACTIONS_H_ */
|
1098
apps/er-coap-07/er-coap-07.c
Normal file
319
apps/er-coap-07/er-coap-07.h
Normal file
|
@ -0,0 +1,319 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An implementation of the Constrained Application Protocol (draft 07)
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COAP_07_H_
|
||||||
|
#define COAP_07_H_
|
||||||
|
|
||||||
|
#include <stddef.h> /* for size_t */
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "erbium.h"
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_PORT 5683
|
||||||
|
|
||||||
|
#ifndef COAP_SERVER_PORT
|
||||||
|
#define COAP_SERVER_PORT COAP_DEFAULT_PORT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_MAX_AGE 60
|
||||||
|
#define COAP_RESPONSE_TIMEOUT 2
|
||||||
|
#define COAP_RESPONSE_RANDOM_FACTOR 1.5
|
||||||
|
#define COAP_MAX_RETRANSMIT 4
|
||||||
|
|
||||||
|
#define COAP_HEADER_LEN 4 /* | oc:0xF0 type:0x0C version:0x03 | code | tid:0x00FF | tid:0xFF00 | */
|
||||||
|
#define COAP_ETAG_LEN 8 /* The maximum number of bytes for the ETag */
|
||||||
|
#define COAP_TOKEN_LEN 8 /* The maximum number of bytes for the Token */
|
||||||
|
#define COAP_MAX_ACCEPT_NUM 2 /* The maximum number of accept preferences to parse/store */
|
||||||
|
|
||||||
|
#define COAP_HEADER_VERSION_MASK 0xC0
|
||||||
|
#define COAP_HEADER_VERSION_POSITION 6
|
||||||
|
#define COAP_HEADER_TYPE_MASK 0x30
|
||||||
|
#define COAP_HEADER_TYPE_POSITION 4
|
||||||
|
#define COAP_HEADER_OPTION_COUNT_MASK 0x0F
|
||||||
|
#define COAP_HEADER_OPTION_COUNT_POSITION 0
|
||||||
|
|
||||||
|
#define COAP_HEADER_OPTION_DELTA_MASK 0xF0
|
||||||
|
#define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Conservative size limit, as not all options have to be set at the same time.
|
||||||
|
*/
|
||||||
|
/* Hdr CoT Age Tag Obs Tok Blo strings */
|
||||||
|
#define COAP_MAX_HEADER_SIZE (4 + 3 + 5 + 1+COAP_ETAG_LEN + 3 + 1+COAP_TOKEN_LEN + 4 + 10) /* 50 */
|
||||||
|
#define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
|
||||||
|
/* 0/14 48 for IPv6 (28 for IPv4) */
|
||||||
|
#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN)
|
||||||
|
#error "UIP_CONF_BUFFER_SIZE too small for REST_MAX_CHUNK_SIZE"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Maximum number of failed request attempts before action
|
||||||
|
*/
|
||||||
|
#ifndef COAP_MAX_ATTEMPTS
|
||||||
|
#define COAP_MAX_ATTEMPTS 4
|
||||||
|
#endif /* COAP_MAX_ATTEMPTS */
|
||||||
|
|
||||||
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||||
|
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
|
||||||
|
|
||||||
|
#define SET_OPTION(packet, opt) ((packet)->options |= 1L<<opt)
|
||||||
|
#define IS_OPTION(packet, opt) ((packet)->options & 1L<<opt)
|
||||||
|
|
||||||
|
#ifndef MIN
|
||||||
|
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
||||||
|
#endif /* MIN */
|
||||||
|
|
||||||
|
/* CoAP message types */
|
||||||
|
typedef enum {
|
||||||
|
COAP_TYPE_CON, /* confirmables */
|
||||||
|
COAP_TYPE_NON, /* non-confirmables */
|
||||||
|
COAP_TYPE_ACK, /* acknowledgements */
|
||||||
|
COAP_TYPE_RST /* reset */
|
||||||
|
} coap_message_type_t;
|
||||||
|
|
||||||
|
/* CoAP request method codes */
|
||||||
|
typedef enum {
|
||||||
|
COAP_GET = 1,
|
||||||
|
COAP_POST,
|
||||||
|
COAP_PUT,
|
||||||
|
COAP_DELETE
|
||||||
|
} coap_method_t;
|
||||||
|
|
||||||
|
/* CoAP response codes */
|
||||||
|
typedef enum {
|
||||||
|
NO_ERROR = 0,
|
||||||
|
|
||||||
|
CREATED_2_01 = 65, /* CREATED */
|
||||||
|
DELETED_2_02 = 66, /* DELETED */
|
||||||
|
VALID_2_03 = 67, /* NOT_MODIFIED */
|
||||||
|
CHANGED_2_04 = 68, /* CHANGED */
|
||||||
|
CONTENT_2_05 = 69, /* OK */
|
||||||
|
|
||||||
|
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
|
||||||
|
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
|
||||||
|
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
|
||||||
|
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
|
||||||
|
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
|
||||||
|
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
|
||||||
|
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
|
||||||
|
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
|
||||||
|
UNSUPPORTED_MADIA_TYPE_4_15 = 143, /* UNSUPPORTED_MADIA_TYPE */
|
||||||
|
|
||||||
|
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
|
||||||
|
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
|
||||||
|
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
|
||||||
|
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
|
||||||
|
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
|
||||||
|
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
|
||||||
|
|
||||||
|
/* Memory errors */
|
||||||
|
IMPLEMENTATION_ERROR = 192,
|
||||||
|
MEMORY_ALLOC_ERR = 193,
|
||||||
|
MEMORY_BOUNDARY_EXCEEDED = 194,
|
||||||
|
|
||||||
|
/* CoAP errors */
|
||||||
|
UNIMPLEMENTED_CRITICAL_OPTION,
|
||||||
|
UNKNOWN_CRITICAL_OPTION,
|
||||||
|
PACKET_SERIALIZATION_ERROR
|
||||||
|
} coap_status_t;
|
||||||
|
|
||||||
|
/* CoAP header options */
|
||||||
|
typedef enum {
|
||||||
|
COAP_OPTION_CONTENT_TYPE = 1, /* 0-2 B */
|
||||||
|
COAP_OPTION_MAX_AGE = 2, /* 0-4 B */
|
||||||
|
COAP_OPTION_PROXY_URI = 3, /* 1-270 B */
|
||||||
|
COAP_OPTION_ETAG = 4, /* 1-8 B */
|
||||||
|
COAP_OPTION_URI_HOST = 5, /* 1-270 B */
|
||||||
|
COAP_OPTION_LOCATION_PATH = 6, /* 1-270 B */
|
||||||
|
COAP_OPTION_URI_PORT = 7, /* 0-2 B */
|
||||||
|
COAP_OPTION_LOCATION_QUERY = 8, /* 1-270 B */
|
||||||
|
COAP_OPTION_URI_PATH = 9, /* 1-270 B */
|
||||||
|
COAP_OPTION_OBSERVE = 10, /* 0-2 B */
|
||||||
|
COAP_OPTION_TOKEN = 11, /* 1-8 B */
|
||||||
|
COAP_OPTION_ACCEPT = 12, /* 0-2 B */
|
||||||
|
COAP_OPTION_IF_MATCH = 13, /* 0-8 B */
|
||||||
|
COAP_OPTION_FENCE_POST = 14, /* 0 B */
|
||||||
|
COAP_OPTION_URI_QUERY = 15, /* 1-270 B */
|
||||||
|
COAP_OPTION_BLOCK2 = 17, /* 1-3 B */
|
||||||
|
COAP_OPTION_BLOCK1 = 19, /* 1-3 B */
|
||||||
|
COAP_OPTION_IF_NONE_MATCH = 21 /* 0 B */
|
||||||
|
} coap_option_t;
|
||||||
|
|
||||||
|
/* CoAP content-types */
|
||||||
|
typedef enum {
|
||||||
|
TEXT_PLAIN = 0,
|
||||||
|
TEXT_XML = 1, /* Intented types are not in the initial registry. */
|
||||||
|
TEXT_CSV = 2,
|
||||||
|
TEXT_HTML = 3,
|
||||||
|
IMAGE_GIF = 21,
|
||||||
|
IMAGE_JPEG = 22,
|
||||||
|
IMAGE_PNG = 23,
|
||||||
|
IMAGE_TIFF = 24,
|
||||||
|
AUDIO_RAW = 25,
|
||||||
|
VIDEO_RAW = 26,
|
||||||
|
APPLICATION_LINK_FORMAT = 40, /* Actually not in registry!? */
|
||||||
|
APPLICATION_XML = 41,
|
||||||
|
APPLICATION_OCTET_STREAM = 42,
|
||||||
|
APPLICATION_RDF_XML = 43,
|
||||||
|
APPLICATION_SOAP_XML = 44,
|
||||||
|
APPLICATION_ATOM_XML = 45,
|
||||||
|
APPLICATION_XMPP_XML = 46,
|
||||||
|
APPLICATION_EXI = 47,
|
||||||
|
APPLICATION_FASTINFOSET = 48,
|
||||||
|
APPLICATION_SOAP_FASTINFOSET = 49,
|
||||||
|
APPLICATION_JSON = 50,
|
||||||
|
APPLICATION_X_OBIX_BINARY = 51
|
||||||
|
} coap_content_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
|
||||||
|
|
||||||
|
uint8_t version;
|
||||||
|
coap_message_type_t type;
|
||||||
|
uint8_t option_count;
|
||||||
|
uint8_t code;
|
||||||
|
uint16_t tid;
|
||||||
|
|
||||||
|
uint32_t options; /* Bitmap to check if option is set */
|
||||||
|
|
||||||
|
coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */
|
||||||
|
uint32_t max_age;
|
||||||
|
size_t proxy_uri_len;
|
||||||
|
char *proxy_uri;
|
||||||
|
uint8_t etag_len;
|
||||||
|
uint8_t etag[COAP_ETAG_LEN];
|
||||||
|
size_t uri_host_len;
|
||||||
|
char *uri_host;
|
||||||
|
size_t location_path_len;
|
||||||
|
char *location_path;
|
||||||
|
uint16_t uri_port;
|
||||||
|
size_t location_query_len;
|
||||||
|
char *location_query;
|
||||||
|
size_t uri_path_len;
|
||||||
|
char *uri_path;
|
||||||
|
uint16_t observe;
|
||||||
|
uint8_t token_len;
|
||||||
|
uint8_t token[COAP_TOKEN_LEN];
|
||||||
|
uint8_t accept_num;
|
||||||
|
uint16_t accept[COAP_MAX_ACCEPT_NUM];
|
||||||
|
uint8_t if_match_len;
|
||||||
|
uint8_t if_match[COAP_ETAG_LEN];
|
||||||
|
uint32_t block2_num;
|
||||||
|
uint8_t block2_more;
|
||||||
|
uint16_t block2_size;
|
||||||
|
uint32_t block2_offset;
|
||||||
|
uint32_t block1_num;
|
||||||
|
uint8_t block1_more;
|
||||||
|
uint16_t block1_size;
|
||||||
|
uint32_t block1_offset;
|
||||||
|
size_t uri_query_len;
|
||||||
|
char *uri_query;
|
||||||
|
uint8_t if_none_match;
|
||||||
|
|
||||||
|
uint16_t payload_len;
|
||||||
|
uint8_t *payload;
|
||||||
|
|
||||||
|
} coap_packet_t;
|
||||||
|
|
||||||
|
|
||||||
|
/* To store error code and human-readable payload */
|
||||||
|
extern coap_status_t coap_error_code;
|
||||||
|
extern char *coap_error_message;
|
||||||
|
|
||||||
|
void coap_init_connection(uint16_t port);
|
||||||
|
uint16_t coap_get_tid(void);
|
||||||
|
|
||||||
|
void coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid);
|
||||||
|
size_t coap_serialize_message(void *packet, uint8_t *buffer);
|
||||||
|
void coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length);
|
||||||
|
coap_status_t coap_parse_message(void *request, uint8_t *data, uint16_t data_len);
|
||||||
|
|
||||||
|
int coap_get_query_variable(void *packet, const char *name, const char **output);
|
||||||
|
int coap_get_post_variable(void *packet, const char *name, const char **output);
|
||||||
|
|
||||||
|
unsigned int coap_get_header_content_type(void *packet);
|
||||||
|
int coap_set_header_content_type(void *packet, unsigned int content_type);
|
||||||
|
|
||||||
|
int coap_get_header_accept(void *packet, uint16_t **accept);
|
||||||
|
int coap_set_header_accept(void *packet, uint16_t accept);
|
||||||
|
|
||||||
|
int coap_get_header_max_age(void *packet, uint32_t *age);
|
||||||
|
int coap_set_header_max_age(void *packet, uint32_t age);
|
||||||
|
|
||||||
|
int coap_get_header_etag(void *packet, const uint8_t **etag);
|
||||||
|
int coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len);
|
||||||
|
|
||||||
|
int coap_get_header_if_match(void *packet, const uint8_t **etag);
|
||||||
|
int coap_set_header_if_match(void *packet, uint8_t *etag, size_t etag_len);
|
||||||
|
|
||||||
|
int coap_get_header_if_none_match(void *packet);
|
||||||
|
int coap_set_header_if_none_match(void *packet);
|
||||||
|
|
||||||
|
int coap_get_header_token(void *packet, const uint8_t **token);
|
||||||
|
int coap_set_header_token(void *packet, uint8_t *token, size_t token_len);
|
||||||
|
|
||||||
|
int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_proxy_uri(void *packet, char *uri);
|
||||||
|
|
||||||
|
int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_uri_host(void *packet, char *host);
|
||||||
|
|
||||||
|
int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_uri_path(void *packet, char *path);
|
||||||
|
|
||||||
|
int coap_get_header_uri_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_uri_query(void *packet, char *query);
|
||||||
|
|
||||||
|
int coap_get_header_location_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_location_path(void *packet, char *path); /* Also splits optional query into Location-Query option. */
|
||||||
|
|
||||||
|
int coap_get_header_location_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
|
||||||
|
int coap_set_header_location_query(void *packet, char *query);
|
||||||
|
|
||||||
|
int coap_get_header_observe(void *packet, uint32_t *observe);
|
||||||
|
int coap_set_header_observe(void *packet, uint32_t observe);
|
||||||
|
|
||||||
|
int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
|
||||||
|
int coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size);
|
||||||
|
|
||||||
|
int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
|
||||||
|
int coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size);
|
||||||
|
|
||||||
|
int coap_get_payload(void *packet, const uint8_t **payload);
|
||||||
|
int coap_set_payload(void *packet, uint8_t *payload, size_t length);
|
||||||
|
|
||||||
|
#endif /* COAP_07_H_ */
|
1
apps/erbium/Makefile.erbium
Normal file
|
@ -0,0 +1 @@
|
||||||
|
erbium_src = erbium.c
|
263
apps/erbium/erbium.c
Normal file
|
@ -0,0 +1,263 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An abstraction layer for RESTful Web services
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include <string.h> /*for string operations in match_addresses*/
|
||||||
|
#include <stdio.h> /*for sprintf in rest_set_header_**/
|
||||||
|
|
||||||
|
#include "erbium.h"
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#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 ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
||||||
|
|
||||||
|
PROCESS_NAME(rest_manager_process);
|
||||||
|
|
||||||
|
LIST(restful_services);
|
||||||
|
LIST(restful_periodic_services);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WITH_HTTP
|
||||||
|
|
||||||
|
char *
|
||||||
|
rest_to_http_max_age(uint32_t age)
|
||||||
|
{
|
||||||
|
/* Cache-Control: max-age=age for HTTP */
|
||||||
|
static char temp_age[19];
|
||||||
|
snprintf(temp_age, sizeof(temp_age), "max-age=%lu", age);
|
||||||
|
return temp_age;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
rest_to_http_etag(uint8_t *etag, uint8_t etag_len)
|
||||||
|
{
|
||||||
|
static char temp_etag[17];
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
for (index = 0; index<sizeof(temp_etag) && index<etag_len; ++index) {
|
||||||
|
snprintf(temp_etag+2*index, sizeof(temp_etag), "%02x", etag[index]);
|
||||||
|
}
|
||||||
|
temp_etag[2*index] = '\0';
|
||||||
|
|
||||||
|
return temp_etag;
|
||||||
|
}
|
||||||
|
#endif /*WITH_COAP*/
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_init_framework(void)
|
||||||
|
{
|
||||||
|
list_init(restful_services);
|
||||||
|
|
||||||
|
REST.set_service_callback(rest_invoke_restful_service);
|
||||||
|
|
||||||
|
/* Start the RESTful server implementation. */
|
||||||
|
REST.init();
|
||||||
|
|
||||||
|
/*Start rest manager process*/
|
||||||
|
process_start(&rest_manager_process, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_activate_resource(resource_t* resource)
|
||||||
|
{
|
||||||
|
PRINTF("Activating: %s", resource->url);
|
||||||
|
|
||||||
|
if (!resource->pre_handler)
|
||||||
|
{
|
||||||
|
rest_set_pre_handler(resource, REST.default_pre_handler);
|
||||||
|
}
|
||||||
|
if (!resource->post_handler)
|
||||||
|
{
|
||||||
|
rest_set_post_handler(resource, REST.default_post_handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
list_add(restful_services, resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_activate_periodic_resource(periodic_resource_t* periodic_resource)
|
||||||
|
{
|
||||||
|
list_add(restful_periodic_services, periodic_resource);
|
||||||
|
rest_activate_resource(periodic_resource->resource);
|
||||||
|
|
||||||
|
rest_set_post_handler(periodic_resource->resource, REST.subscription_handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_activate_event_resource(resource_t* resource)
|
||||||
|
{
|
||||||
|
rest_activate_resource(resource);
|
||||||
|
rest_set_post_handler(resource, REST.subscription_handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
list_t
|
||||||
|
rest_get_resources(void)
|
||||||
|
{
|
||||||
|
return restful_services;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
|
rest_get_user_data(resource_t* resource)
|
||||||
|
{
|
||||||
|
return resource->user_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_set_user_data(resource_t* resource, void* user_data)
|
||||||
|
{
|
||||||
|
resource->user_data = user_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_set_pre_handler(resource_t* resource, restful_pre_handler pre_handler)
|
||||||
|
{
|
||||||
|
resource->pre_handler = pre_handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_set_post_handler(resource_t* resource, restful_post_handler post_handler)
|
||||||
|
{
|
||||||
|
resource->post_handler = post_handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rest_set_special_flags(resource_t* resource, rest_resource_flags_t flags)
|
||||||
|
{
|
||||||
|
resource->flags |= flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
rest_invoke_restful_service(void* request, void* response, uint8_t *buffer, uint16_t buffer_size, int32_t *offset)
|
||||||
|
{
|
||||||
|
uint8_t found = 0;
|
||||||
|
uint8_t allowed = 0;
|
||||||
|
|
||||||
|
PRINTF("rest_invoke_restful_service url /%.*s -->\n", url_len, url);
|
||||||
|
|
||||||
|
resource_t* resource = NULL;
|
||||||
|
const char *url = NULL;
|
||||||
|
|
||||||
|
for (resource = (resource_t*)list_head(restful_services); resource; resource = resource->next)
|
||||||
|
{
|
||||||
|
/*if the web service handles that kind of requests and urls matches*/
|
||||||
|
if ((REST.get_url(request, &url)==strlen(resource->url) || (REST.get_url(request, &url)>strlen(resource->url) && (resource->flags & HAS_SUB_RESOURCES)))
|
||||||
|
&& strncmp(resource->url, url, strlen(resource->url)) == 0)
|
||||||
|
{
|
||||||
|
found = 1;
|
||||||
|
rest_resource_flags_t method = REST.get_method_type(request);
|
||||||
|
|
||||||
|
PRINTF("method %u, resource->flags %u\n", (uint16_t)method, resource->flags);
|
||||||
|
|
||||||
|
if (resource->flags & method)
|
||||||
|
{
|
||||||
|
allowed = 1;
|
||||||
|
|
||||||
|
/*call pre handler if it exists*/
|
||||||
|
if (!resource->pre_handler || resource->pre_handler(resource, request, response))
|
||||||
|
{
|
||||||
|
/* call handler function*/
|
||||||
|
resource->handler(request, response, buffer, buffer_size, offset);
|
||||||
|
|
||||||
|
/*call post handler if it exists*/
|
||||||
|
if (resource->post_handler)
|
||||||
|
{
|
||||||
|
resource->post_handler(resource, request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
REST.set_response_status(response, REST.status.METHOD_NOT_ALLOWED);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
REST.set_response_status(response, REST.status.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return found & allowed;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
PROCESS(rest_manager_process, "Rest Process");
|
||||||
|
|
||||||
|
PROCESS_THREAD(rest_manager_process, ev, data)
|
||||||
|
{
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
|
||||||
|
PROCESS_PAUSE();
|
||||||
|
|
||||||
|
/* Initialize the PERIODIC_RESOURCE timers, which will be handled by this process. */
|
||||||
|
periodic_resource_t* periodic_resource = NULL;
|
||||||
|
for (periodic_resource = (periodic_resource_t*) list_head(restful_periodic_services); periodic_resource; periodic_resource = periodic_resource->next) {
|
||||||
|
if (periodic_resource->period) {
|
||||||
|
PRINTF("Periodic: Set timer for %s to %lu\n", periodic_resource->resource->url, periodic_resource->period);
|
||||||
|
etimer_set(&periodic_resource->periodic_timer, periodic_resource->period);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
PROCESS_WAIT_EVENT();
|
||||||
|
if (ev == PROCESS_EVENT_TIMER) {
|
||||||
|
for (periodic_resource = (periodic_resource_t*)list_head(restful_periodic_services);periodic_resource;periodic_resource = periodic_resource->next) {
|
||||||
|
if (periodic_resource->period && etimer_expired(&periodic_resource->periodic_timer)) {
|
||||||
|
|
||||||
|
PRINTF("Periodic: etimer expired for /%s (period: %lu)\n", periodic_resource->resource->url, periodic_resource->period);
|
||||||
|
|
||||||
|
/* Call the periodic_handler function if it exists. */
|
||||||
|
if (periodic_resource->periodic_handler) {
|
||||||
|
(periodic_resource->periodic_handler)(periodic_resource->resource);
|
||||||
|
}
|
||||||
|
etimer_reset(&periodic_resource->periodic_timer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
|
331
apps/erbium/erbium.h
Normal file
|
@ -0,0 +1,331 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 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
|
||||||
|
* An abstraction layer for RESTful Web services
|
||||||
|
* \author
|
||||||
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ERBIUM_H_
|
||||||
|
#define ERBIUM_H_
|
||||||
|
|
||||||
|
/*includes*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "contiki-lib.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The maximum buffer size that is provided for resource responses and must be respected due to the limited IP buffer.
|
||||||
|
* Larger data must be handled by the resource and will be sent chunk-wise through a TCP stream or CoAP blocks.
|
||||||
|
*/
|
||||||
|
#ifndef REST_MAX_CHUNK_SIZE
|
||||||
|
#define REST_MAX_CHUNK_SIZE 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MIN
|
||||||
|
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
||||||
|
#endif /* MIN */
|
||||||
|
|
||||||
|
/* REST method types */
|
||||||
|
typedef enum {
|
||||||
|
/* methods to handle */
|
||||||
|
METHOD_GET = (1 << 0),
|
||||||
|
METHOD_POST = (1 << 1),
|
||||||
|
METHOD_PUT = (1 << 2),
|
||||||
|
METHOD_DELETE = (1 << 3),
|
||||||
|
|
||||||
|
/* special flags */
|
||||||
|
HAS_SUB_RESOURCES = (1<<7)
|
||||||
|
} rest_resource_flags_t;
|
||||||
|
|
||||||
|
struct resource_s;
|
||||||
|
struct periodic_resource_s;
|
||||||
|
|
||||||
|
/* Signatures of handler functions. */
|
||||||
|
typedef void (*restful_handler) (void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||||
|
typedef int (*restful_pre_handler) (struct resource_s *resource, void* request, void* response);
|
||||||
|
typedef void (*restful_post_handler) (struct resource_s *resource, void* request, void* response);
|
||||||
|
typedef int (*restful_periodic_handler) (struct resource_s* resource);
|
||||||
|
typedef void (*restful_response_handler) (void *data, void* response);
|
||||||
|
|
||||||
|
/* Signature of the rest-engine service function. */
|
||||||
|
typedef int (* service_callback_t)(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The structure of a MAC protocol driver in Contiki.
|
||||||
|
*/
|
||||||
|
struct rest_implementation_status
|
||||||
|
{
|
||||||
|
const unsigned int OK; /* CONTENT_2_05, OK_200 */
|
||||||
|
const unsigned int CREATED; /* CREATED_2_01, CREATED_201 */
|
||||||
|
const unsigned int CHANGED; /* CHANGED_2_04, NO_CONTENT_204 */
|
||||||
|
const unsigned int DELETED; /* DELETED_2_02, NO_CONTENT_204 */
|
||||||
|
const unsigned int NOT_MODIFIED; /* VALID_2_03, NOT_MODIFIED_304 */
|
||||||
|
|
||||||
|
const unsigned int BAD_REQUEST; /* BAD_REQUEST_4_00, BAD_REQUEST_400 */
|
||||||
|
const unsigned int UNAUTHORIZED; /* UNAUTHORIZED_4_01, UNAUTHORIZED_401 */
|
||||||
|
const unsigned int BAD_OPTION; /* BAD_OPTION_4_02, BAD_REQUEST_400 */
|
||||||
|
const unsigned int FORBIDDEN; /* FORBIDDEN_4_03, FORBIDDEN_403 */
|
||||||
|
const unsigned int NOT_FOUND; /* NOT_FOUND_4_04, NOT_FOUND_404 */
|
||||||
|
const unsigned int METHOD_NOT_ALLOWED; /* METHOD_NOT_ALLOWED_4_05, METHOD_NOT_ALLOWED_405 */
|
||||||
|
const unsigned int REQUEST_ENTITY_TOO_LARGE; /* REQUEST_ENTITY_TOO_LARGE_4_13, REQUEST_ENTITY_TOO_LARGE_413 */
|
||||||
|
const unsigned int UNSUPPORTED_MADIA_TYPE; /* UNSUPPORTED_MADIA_TYPE_4_15, UNSUPPORTED_MADIA_TYPE_415 */
|
||||||
|
|
||||||
|
const unsigned int INTERNAL_SERVER_ERROR; /* INTERNAL_SERVER_ERROR_5_00, INTERNAL_SERVER_ERROR_500 */
|
||||||
|
const unsigned int NOT_IMPLEMENTED; /* NOT_IMPLEMENTED_5_01, NOT_IMPLEMENTED_501 */
|
||||||
|
const unsigned int BAD_GATEWAY; /* BAD_GATEWAY_5_02, BAD_GATEWAY_502 */
|
||||||
|
const unsigned int SERVICE_UNAVAILABLE; /* SERVICE_UNAVAILABLE_5_03, SERVICE_UNAVAILABLE_503 */
|
||||||
|
const unsigned int GATEWAY_TIMEOUT; /* GATEWAY_TIMEOUT_5_04, GATEWAY_TIMEOUT_504 */
|
||||||
|
const unsigned int PROXYING_NOT_SUPPORTED; /* PROXYING_NOT_SUPPORTED_5_05, INTERNAL_SERVER_ERROR_500 */
|
||||||
|
};
|
||||||
|
struct rest_implementation_type
|
||||||
|
{
|
||||||
|
unsigned int TEXT_PLAIN;
|
||||||
|
unsigned int TEXT_XML;
|
||||||
|
unsigned int TEXT_CSV;
|
||||||
|
unsigned int TEXT_HTML;
|
||||||
|
unsigned int IMAGE_GIF;
|
||||||
|
unsigned int IMAGE_JPEG;
|
||||||
|
unsigned int IMAGE_PNG;
|
||||||
|
unsigned int IMAGE_TIFF;
|
||||||
|
unsigned int AUDIO_RAW;
|
||||||
|
unsigned int VIDEO_RAW;
|
||||||
|
unsigned int APPLICATION_LINK_FORMAT;
|
||||||
|
unsigned int APPLICATION_XML;
|
||||||
|
unsigned int APPLICATION_OCTET_STREAM;
|
||||||
|
unsigned int APPLICATION_RDF_XML;
|
||||||
|
unsigned int APPLICATION_SOAP_XML;
|
||||||
|
unsigned int APPLICATION_ATOM_XML;
|
||||||
|
unsigned int APPLICATION_XMPP_XML;
|
||||||
|
unsigned int APPLICATION_EXI;
|
||||||
|
unsigned int APPLICATION_FASTINFOSET;
|
||||||
|
unsigned int APPLICATION_SOAP_FASTINFOSET;
|
||||||
|
unsigned int APPLICATION_JSON;
|
||||||
|
unsigned int APPLICATION_X_OBIX_BINARY;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct rest_implementation {
|
||||||
|
char *name;
|
||||||
|
|
||||||
|
/** Initialize the REST implementation. */
|
||||||
|
void (* init)(void);
|
||||||
|
|
||||||
|
/** Register the RESTful service callback at implementation */
|
||||||
|
void (* set_service_callback)(service_callback_t callback);
|
||||||
|
|
||||||
|
/** Get request URI path */
|
||||||
|
int (* get_url)(void *request, const char **url);
|
||||||
|
|
||||||
|
int (* set_url)(void *request, char *url);
|
||||||
|
|
||||||
|
/** Get the method of a request. */
|
||||||
|
rest_resource_flags_t (* get_method_type)(void *request);
|
||||||
|
|
||||||
|
/** Set the status code of a response. */
|
||||||
|
int (* set_response_status)(void *response, unsigned int code);
|
||||||
|
|
||||||
|
/** Get the content-type of a request. */
|
||||||
|
unsigned int (* get_header_content_type)(void *request);
|
||||||
|
|
||||||
|
/** Set the content-type of a response. */
|
||||||
|
int (* set_header_content_type)(void *response, unsigned int content_type);
|
||||||
|
|
||||||
|
int (* get_header_accept)(void *request, uint16_t **accept);
|
||||||
|
|
||||||
|
/** Get the Max-Age option of a request. */
|
||||||
|
int (* get_header_max_age)(void *request, uint32_t *age);
|
||||||
|
|
||||||
|
/** Set the Max-Age option of a response. */
|
||||||
|
int (* set_header_max_age)(void *response, uint32_t age);
|
||||||
|
|
||||||
|
/** Set the ETag option of a response. */
|
||||||
|
int (* set_header_etag)(void *response, uint8_t *etag, size_t length);
|
||||||
|
|
||||||
|
/** Get the If-Match option of a request. */
|
||||||
|
int (* get_header_if_match)(void *request, const uint8_t **etag);
|
||||||
|
|
||||||
|
/** Get the If-Match option of a request. */
|
||||||
|
int (* get_header_if_none_match)(void *request);
|
||||||
|
|
||||||
|
/** Get the Host option of a request. */
|
||||||
|
int (* get_header_host)(void *request, const char **host);
|
||||||
|
|
||||||
|
/** Set the location option of a response. */
|
||||||
|
int (* set_header_location)(void *response, char *location);
|
||||||
|
|
||||||
|
/** Get the payload option of a request. */
|
||||||
|
int (* get_request_payload)(void *request, const uint8_t **payload);
|
||||||
|
|
||||||
|
/** Set the payload option of a response. */
|
||||||
|
int (* set_response_payload)(void *response, uint8_t *payload, size_t length);
|
||||||
|
|
||||||
|
/** Get the query string of a request. */
|
||||||
|
int (* get_query)(void *request, const char **value);
|
||||||
|
|
||||||
|
/** Get the value of a request query key-value pair. */
|
||||||
|
int (* get_query_variable)(void *request, const char *name, const char **value);
|
||||||
|
|
||||||
|
/** Get the value of a request POST key-value pair. */
|
||||||
|
int (* get_post_variable)(void *request, const char *name, const char **value);
|
||||||
|
|
||||||
|
/** Send the payload to all subscribers of the resource at url. */
|
||||||
|
void (* notify_subscribers)(const char *url, int implementation_secific_mode, uint32_t counter, uint8_t *payload, size_t payload_len);
|
||||||
|
|
||||||
|
/** The handler for resource subscriptions. */
|
||||||
|
restful_post_handler subscription_handler;
|
||||||
|
|
||||||
|
/** A default pre-handler that is assigned with the RESOURCE macro. */
|
||||||
|
restful_pre_handler default_pre_handler;
|
||||||
|
|
||||||
|
/** A default post-handler that is assigned with the RESOURCE macro. */
|
||||||
|
restful_post_handler default_post_handler;
|
||||||
|
|
||||||
|
/* REST status codes. */
|
||||||
|
const struct rest_implementation_status status;
|
||||||
|
|
||||||
|
/* REST content-types. */
|
||||||
|
const struct rest_implementation_type type;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Instance of REST implementation
|
||||||
|
*/
|
||||||
|
extern const struct rest_implementation REST;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Data structure representing a resource in REST.
|
||||||
|
*/
|
||||||
|
struct resource_s {
|
||||||
|
struct resource_s *next; /* for LIST, points to next resource defined */
|
||||||
|
rest_resource_flags_t flags; /* handled RESTful methods */
|
||||||
|
const char* url; /*handled URL*/
|
||||||
|
const char* attributes; /* link-format attributes */
|
||||||
|
restful_handler handler; /* handler function */
|
||||||
|
restful_pre_handler pre_handler; /* to be called before handler, may perform initializations */
|
||||||
|
restful_post_handler post_handler; /* to be called after handler, may perform finalizations (cleanup, etc) */
|
||||||
|
void* user_data; /* pointer to user specific data */
|
||||||
|
unsigned int benchmark; /* to benchmark resource handler, used for separate response */
|
||||||
|
};
|
||||||
|
typedef struct resource_s resource_t;
|
||||||
|
|
||||||
|
struct periodic_resource_s {
|
||||||
|
struct periodic_resource_s *next; /* for LIST, points to next resource defined */
|
||||||
|
resource_t *resource;
|
||||||
|
uint32_t period;
|
||||||
|
struct etimer periodic_timer;
|
||||||
|
restful_periodic_handler periodic_handler;
|
||||||
|
};
|
||||||
|
typedef struct periodic_resource_s periodic_resource_t;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro to define a Resource
|
||||||
|
* Resources are statically defined for the sake of efficiency and better memory management.
|
||||||
|
*/
|
||||||
|
#define RESOURCE(name, flags, url, attributes) \
|
||||||
|
void name##_handler(void *, void *, uint8_t *, uint16_t, int32_t *); \
|
||||||
|
resource_t resource_##name = {NULL, flags, url, attributes, name##_handler, NULL, NULL, NULL}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro to define an event resource
|
||||||
|
* Like periodic resources, event resources have a post_handler that manages a subscriber list.
|
||||||
|
* Instead of a periodic_handler, an event_callback must be provided.
|
||||||
|
*/
|
||||||
|
#define EVENT_RESOURCE(name, flags, url, attributes) \
|
||||||
|
void name##_handler(void *, void *, uint8_t *, uint16_t, int32_t *); \
|
||||||
|
resource_t resource_##name = {NULL, flags, url, attributes, name##_handler, NULL, NULL, NULL}; \
|
||||||
|
int name##_event_handler(resource_t*)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro to define a periodic resource
|
||||||
|
* The corresponding [name]_periodic_handler() function will be called every period.
|
||||||
|
* For instance polling a sensor and publishing a changed value to subscribed clients would be done there.
|
||||||
|
* The subscriber list will be maintained by the post_handler rest_subscription_handler() (see rest-mapping header file).
|
||||||
|
*/
|
||||||
|
#define PERIODIC_RESOURCE(name, flags, url, attributes, period) \
|
||||||
|
void name##_handler(void *, void *, uint8_t *, uint16_t, int32_t *); \
|
||||||
|
resource_t resource_##name = {NULL, flags, url, attributes, name##_handler, NULL, NULL, NULL}; \
|
||||||
|
int name##_periodic_handler(resource_t*); \
|
||||||
|
periodic_resource_t periodic_resource_##name = {NULL, &resource_##name, period, {{0}}, name##_periodic_handler}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initializes REST framework and starts HTTP or COAP process
|
||||||
|
*/
|
||||||
|
void rest_init_framework(void);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Resources wanted to be accessible should be activated with the following code.
|
||||||
|
*/
|
||||||
|
void rest_activate_resource(resource_t* resource);
|
||||||
|
void rest_activate_periodic_resource(periodic_resource_t* periodic_resource);
|
||||||
|
void rest_activate_event_resource(resource_t* resource);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* To be called by HTTP/COAP server as a callback function when a new service request appears.
|
||||||
|
* This function dispatches the corresponding RESTful service.
|
||||||
|
*/
|
||||||
|
int rest_invoke_restful_service(void* request, void* response, uint8_t *buffer, uint16_t buffer_size, int32_t *offset);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns the resource list
|
||||||
|
*/
|
||||||
|
list_t rest_get_resources(void);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Getter and setter methods for user specific data.
|
||||||
|
*/
|
||||||
|
void* rest_get_user_data(resource_t* resource);
|
||||||
|
void rest_set_user_data(resource_t* resource, void* user_data);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sets the pre handler function of the Resource.
|
||||||
|
* If set, this function will be called just before the original handler function.
|
||||||
|
* Can be used to setup work before resource handling.
|
||||||
|
*/
|
||||||
|
void rest_set_pre_handler(resource_t* resource, restful_pre_handler pre_handler);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sets the post handler function of the Resource.
|
||||||
|
* If set, this function will be called just after the original handler function.
|
||||||
|
* Can be used to do cleanup (deallocate memory, etc) after resource handling.
|
||||||
|
*/
|
||||||
|
void rest_set_post_handler(resource_t* resource, restful_post_handler post_handler);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sets resource flags for special properties, e.g., handling of sub-resources of URI-path.
|
||||||
|
*/
|
||||||
|
void rest_set_special_flags(resource_t* resource, rest_resource_flags_t flags);
|
||||||
|
|
||||||
|
#endif /*ERBIUM_H_*/
|
|
@ -52,6 +52,9 @@ struct powertrace_sniff_stats {
|
||||||
uint32_t num_input, num_output;
|
uint32_t num_input, num_output;
|
||||||
uint32_t input_txtime, input_rxtime;
|
uint32_t input_txtime, input_rxtime;
|
||||||
uint32_t output_txtime, output_rxtime;
|
uint32_t output_txtime, output_rxtime;
|
||||||
|
#if UIP_CONF_IPV6
|
||||||
|
uint16_t proto; /* includes proto + possibly flags */
|
||||||
|
#endif
|
||||||
uint16_t channel;
|
uint16_t channel;
|
||||||
uint32_t last_input_txtime, last_input_rxtime;
|
uint32_t last_input_txtime, last_input_rxtime;
|
||||||
uint32_t last_output_txtime, last_output_rxtime;
|
uint32_t last_output_txtime, last_output_rxtime;
|
||||||
|
@ -132,6 +135,8 @@ powertrace_print(char *str)
|
||||||
(int)((10000L * listen) / time - (100L * listen / time) * 100));
|
(int)((10000L * listen) / time - (100L * listen / time) * 100));
|
||||||
|
|
||||||
for(s = list_head(stats_list); s != NULL; s = list_item_next(s)) {
|
for(s = list_head(stats_list); s != NULL; s = list_item_next(s)) {
|
||||||
|
|
||||||
|
#if ! UIP_CONF_IPV6
|
||||||
printf("%s %lu SP %d.%d %lu %u %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu (channel %d radio %d.%02d%% / %d.%02d%%)\n",
|
printf("%s %lu SP %d.%d %lu %u %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu (channel %d radio %d.%02d%% / %d.%02d%%)\n",
|
||||||
str, clock_time(), rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], seqno,
|
str, clock_time(), rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], seqno,
|
||||||
s->channel,
|
s->channel,
|
||||||
|
@ -154,7 +159,30 @@ powertrace_print(char *str)
|
||||||
(s->last_input_rxtime + s->last_input_txtime +
|
(s->last_input_rxtime + s->last_input_txtime +
|
||||||
s->last_output_rxtime + s->last_output_txtime))) /
|
s->last_output_rxtime + s->last_output_txtime))) /
|
||||||
radio));
|
radio));
|
||||||
|
#else
|
||||||
|
printf("%s %lu SP %d.%d %lu %u %u %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu (proto %u(%u) radio %d.%02d%% / %d.%02d%%)\n",
|
||||||
|
str, clock_time(), rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], seqno,
|
||||||
|
s->proto, s->channel,
|
||||||
|
s->num_input, s->input_txtime, s->input_rxtime,
|
||||||
|
s->input_txtime - s->last_input_txtime,
|
||||||
|
s->input_rxtime - s->last_input_rxtime,
|
||||||
|
s->num_output, s->output_txtime, s->output_rxtime,
|
||||||
|
s->output_txtime - s->last_output_txtime,
|
||||||
|
s->output_rxtime - s->last_output_rxtime,
|
||||||
|
s->proto, s->channel,
|
||||||
|
(int)((100L * (s->input_rxtime + s->input_txtime + s->output_rxtime + s->output_txtime)) / all_radio),
|
||||||
|
(int)((10000L * (s->input_rxtime + s->input_txtime + s->output_rxtime + s->output_txtime)) / all_radio),
|
||||||
|
(int)((100L * (s->input_rxtime + s->input_txtime +
|
||||||
|
s->output_rxtime + s->output_txtime -
|
||||||
|
(s->last_input_rxtime + s->last_input_txtime +
|
||||||
|
s->last_output_rxtime + s->last_output_txtime))) /
|
||||||
|
radio),
|
||||||
|
(int)((10000L * (s->input_rxtime + s->input_txtime +
|
||||||
|
s->output_rxtime + s->output_txtime -
|
||||||
|
(s->last_input_rxtime + s->last_input_txtime +
|
||||||
|
s->last_output_rxtime + s->last_output_txtime))) /
|
||||||
|
radio));
|
||||||
|
#endif
|
||||||
s->last_input_txtime = s->input_txtime;
|
s->last_input_txtime = s->input_txtime;
|
||||||
s->last_input_rxtime = s->input_rxtime;
|
s->last_input_rxtime = s->input_rxtime;
|
||||||
s->last_output_txtime = s->output_txtime;
|
s->last_output_txtime = s->output_txtime;
|
||||||
|
@ -198,29 +226,6 @@ powertrace_stop(void)
|
||||||
process_exit(&powertrace_process);
|
process_exit(&powertrace_process);
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
#if ! UIP_CONF_IPV6
|
|
||||||
static void
|
|
||||||
sniffprint(char *prefix, int seqno)
|
|
||||||
{
|
|
||||||
const rimeaddr_t *sender, *receiver, *esender, *ereceiver;
|
|
||||||
|
|
||||||
sender = packetbuf_addr(PACKETBUF_ADDR_SENDER);
|
|
||||||
receiver = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
|
||||||
esender = packetbuf_addr(PACKETBUF_ADDR_ESENDER);
|
|
||||||
ereceiver = packetbuf_addr(PACKETBUF_ADDR_ERECEIVER);
|
|
||||||
|
|
||||||
|
|
||||||
printf("%lu %s %d %u %d %d %d.%d %u %u\n",
|
|
||||||
clock_time(),
|
|
||||||
prefix,
|
|
||||||
rimeaddr_node_addr.u8[0], seqno,
|
|
||||||
packetbuf_attr(PACKETBUF_ATTR_CHANNEL),
|
|
||||||
packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE),
|
|
||||||
esender->u8[0], esender->u8[1],
|
|
||||||
packetbuf_attr(PACKETBUF_ATTR_TRANSMIT_TIME),
|
|
||||||
packetbuf_attr(PACKETBUF_ATTR_LISTEN_TIME));
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
static void
|
static void
|
||||||
add_stats(struct powertrace_sniff_stats *s, int input_or_output)
|
add_stats(struct powertrace_sniff_stats *s, int input_or_output)
|
||||||
{
|
{
|
||||||
|
@ -244,7 +249,11 @@ add_packet_stats(int input_or_output)
|
||||||
of the packet. If we don't find one, we allocate a new one and
|
of the packet. If we don't find one, we allocate a new one and
|
||||||
put it on the list. */
|
put it on the list. */
|
||||||
for(s = list_head(stats_list); s != NULL; s = list_item_next(s)) {
|
for(s = list_head(stats_list); s != NULL; s = list_item_next(s)) {
|
||||||
if(s->channel == packetbuf_attr(PACKETBUF_ATTR_CHANNEL)) {
|
if(s->channel == packetbuf_attr(PACKETBUF_ATTR_CHANNEL)
|
||||||
|
#if UIP_CONF_IPV6
|
||||||
|
&& s->proto == packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID)
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
add_stats(s, input_or_output);
|
add_stats(s, input_or_output);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -254,6 +263,9 @@ add_packet_stats(int input_or_output)
|
||||||
if(s != NULL) {
|
if(s != NULL) {
|
||||||
memset(s, 0, sizeof(struct powertrace_sniff_stats));
|
memset(s, 0, sizeof(struct powertrace_sniff_stats));
|
||||||
s->channel = packetbuf_attr(PACKETBUF_ATTR_CHANNEL);
|
s->channel = packetbuf_attr(PACKETBUF_ATTR_CHANNEL);
|
||||||
|
#if UIP_CONF_IPV6
|
||||||
|
s->proto = packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID);
|
||||||
|
#endif
|
||||||
list_add(stats_list, s);
|
list_add(stats_list, s);
|
||||||
add_stats(s, input_or_output);
|
add_stats(s, input_or_output);
|
||||||
}
|
}
|
||||||
|
@ -272,6 +284,29 @@ output_sniffer(int mac_status)
|
||||||
add_packet_stats(OUTPUT);
|
add_packet_stats(OUTPUT);
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
#if ! UIP_CONF_IPV6
|
||||||
|
static void
|
||||||
|
sniffprint(char *prefix, int seqno)
|
||||||
|
{
|
||||||
|
const rimeaddr_t *sender, *receiver, *esender, *ereceiver;
|
||||||
|
|
||||||
|
sender = packetbuf_addr(PACKETBUF_ADDR_SENDER);
|
||||||
|
receiver = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
||||||
|
esender = packetbuf_addr(PACKETBUF_ADDR_ESENDER);
|
||||||
|
ereceiver = packetbuf_addr(PACKETBUF_ADDR_ERECEIVER);
|
||||||
|
|
||||||
|
|
||||||
|
printf("%lu %s %d %u %d %d %d.%d %u %u\n",
|
||||||
|
clock_time(),
|
||||||
|
prefix,
|
||||||
|
rimeaddr_node_addr.u8[0], seqno,
|
||||||
|
packetbuf_attr(PACKETBUF_ATTR_CHANNEL),
|
||||||
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE),
|
||||||
|
esender->u8[0], esender->u8[1],
|
||||||
|
packetbuf_attr(PACKETBUF_ATTR_TRANSMIT_TIME),
|
||||||
|
packetbuf_attr(PACKETBUF_ATTR_LISTEN_TIME));
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
input_printsniffer(void)
|
input_printsniffer(void)
|
||||||
{
|
{
|
||||||
|
@ -299,23 +334,9 @@ output_printsniffer(int mac_status)
|
||||||
sniffprint("O", seqno++);
|
sniffprint("O", seqno++);
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
RIME_SNIFFER(powersniff, input_sniffer, output_sniffer);
|
|
||||||
RIME_SNIFFER(printsniff, input_printsniffer, output_printsniffer);
|
RIME_SNIFFER(printsniff, input_printsniffer, output_printsniffer);
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
powertrace_sniff(powertrace_onoff_t onoff)
|
|
||||||
{
|
|
||||||
switch(onoff) {
|
|
||||||
case POWERTRACE_ON:
|
|
||||||
rime_sniffer_add(&powersniff);
|
|
||||||
break;
|
|
||||||
case POWERTRACE_OFF:
|
|
||||||
rime_sniffer_remove(&powersniff);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void
|
|
||||||
powertrace_printsniff(powertrace_onoff_t onoff)
|
powertrace_printsniff(powertrace_onoff_t onoff)
|
||||||
{
|
{
|
||||||
switch(onoff) {
|
switch(onoff) {
|
||||||
|
@ -327,10 +348,19 @@ powertrace_printsniff(powertrace_onoff_t onoff)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
RIME_SNIFFER(powersniff, input_sniffer, output_sniffer);
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
#else /* ! UIP_CONF_IPV6 */
|
|
||||||
void
|
void
|
||||||
powertrace_sniff(powertrace_onoff_t onoff)
|
powertrace_sniff(powertrace_onoff_t onoff)
|
||||||
{
|
{
|
||||||
|
switch(onoff) {
|
||||||
|
case POWERTRACE_ON:
|
||||||
|
rime_sniffer_add(&powersniff);
|
||||||
|
break;
|
||||||
|
case POWERTRACE_OFF:
|
||||||
|
rime_sniffer_remove(&powersniff);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif /* ! UIP_CONF_IPV6 */
|
|
||||||
|
|
|
@ -90,7 +90,7 @@ update_processwindow(void)
|
||||||
CTK_WIDGET_ADD(&processwindow, &processidlabels[i]);
|
CTK_WIDGET_ADD(&processwindow, &processidlabels[i]);
|
||||||
|
|
||||||
CTK_LABEL_NEW(&processnamelabels[i],
|
CTK_LABEL_NEW(&processnamelabels[i],
|
||||||
4, i + 1, 22, 1, (char *)p->name);
|
4, i + 1, 22, 1, PROCESS_NAME_STRING(p));
|
||||||
CTK_WIDGET_ADD(&processwindow, &processnamelabels[i]);
|
CTK_WIDGET_ADD(&processwindow, &processnamelabels[i]);
|
||||||
|
|
||||||
++i;
|
++i;
|
||||||
|
|
|
@ -67,7 +67,14 @@ shell_default_output(const char *text1, int len1, const char *text2, int len2)
|
||||||
text2 = "";
|
text2 = "";
|
||||||
len2 = 0;
|
len2 = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Workaround for absence of "%.*s" format in avr-libc */
|
||||||
|
#if defined (__AVR__)
|
||||||
|
printf("%s %s\r\n", text1, text2);
|
||||||
|
#else
|
||||||
printf("%.*s%.*s\r\n", len1, text1, len2, text2);
|
printf("%.*s%.*s\r\n", len1, text1, len2, text2);
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
|
|
|
@ -86,6 +86,8 @@ PROCESS(servreg_hack_process, "Service regstry hack");
|
||||||
|
|
||||||
static struct etimer sendtimer;
|
static struct etimer sendtimer;
|
||||||
|
|
||||||
|
static uint8_t started = 0;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
/* Go through the list of registrations and remove those that are too
|
/* Go through the list of registrations and remove those that are too
|
||||||
old. */
|
old. */
|
||||||
|
@ -117,15 +119,18 @@ purge_registrations(void)
|
||||||
void
|
void
|
||||||
servreg_hack_init(void)
|
servreg_hack_init(void)
|
||||||
{
|
{
|
||||||
list_init(others_services);
|
if(started == 0) {
|
||||||
list_init(own_services);
|
list_init(others_services);
|
||||||
memb_init(®istrations);
|
list_init(own_services);
|
||||||
|
memb_init(®istrations);
|
||||||
|
|
||||||
process_start(&servreg_hack_process, NULL);
|
process_start(&servreg_hack_process, NULL);
|
||||||
|
started = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
servreg_hack_register(servreg_hack_id_t id)
|
servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr)
|
||||||
{
|
{
|
||||||
servreg_hack_item_t *t;
|
servreg_hack_item_t *t;
|
||||||
struct servreg_hack_registration *r;
|
struct servreg_hack_registration *r;
|
||||||
|
@ -134,6 +139,8 @@ servreg_hack_register(servreg_hack_id_t id)
|
||||||
list. If we cannot allocate a service registration, we reuse one
|
list. If we cannot allocate a service registration, we reuse one
|
||||||
from the service registrations made by others. */
|
from the service registrations made by others. */
|
||||||
|
|
||||||
|
servreg_hack_init();
|
||||||
|
|
||||||
for(t = list_head(own_services);
|
for(t = list_head(own_services);
|
||||||
t != NULL;
|
t != NULL;
|
||||||
t = list_item_next(t)) {
|
t = list_item_next(t)) {
|
||||||
|
@ -149,6 +156,7 @@ servreg_hack_register(servreg_hack_id_t id)
|
||||||
}
|
}
|
||||||
r->id = id;
|
r->id = id;
|
||||||
r->seqno = 1;
|
r->seqno = 1;
|
||||||
|
uip_ipaddr_copy(&r->addr, addr);
|
||||||
timer_set(&r->timer, LIFETIME / 2);
|
timer_set(&r->timer, LIFETIME / 2);
|
||||||
list_push(own_services, r);
|
list_push(own_services, r);
|
||||||
|
|
||||||
|
@ -183,6 +191,8 @@ servreg_hack_lookup(servreg_hack_id_t id)
|
||||||
{
|
{
|
||||||
servreg_hack_item_t *t;
|
servreg_hack_item_t *t;
|
||||||
|
|
||||||
|
servreg_hack_init();
|
||||||
|
|
||||||
purge_registrations();
|
purge_registrations();
|
||||||
|
|
||||||
for(t = servreg_hack_list_head(); t != NULL; t = list_item_next(t)) {
|
for(t = servreg_hack_list_head(); t != NULL; t = list_item_next(t)) {
|
||||||
|
@ -212,8 +222,6 @@ handle_incoming_reg(const uip_ipaddr_t *owner, servreg_hack_id_t id, uint8_t seq
|
||||||
now - we might later choose to discard the oldest registration
|
now - we might later choose to discard the oldest registration
|
||||||
that we have). */
|
that we have). */
|
||||||
|
|
||||||
/* printf("Handle incoming reg id %d seqno %d\n", id, seqno);*/
|
|
||||||
|
|
||||||
for(t = servreg_hack_list_head();
|
for(t = servreg_hack_list_head();
|
||||||
t != NULL;
|
t != NULL;
|
||||||
t = list_item_next(t)) {
|
t = list_item_next(t)) {
|
||||||
|
@ -276,32 +284,26 @@ send_udp_packet(struct uip_udp_conn *conn)
|
||||||
uint8_t buf[MAX_BUFSIZE];
|
uint8_t buf[MAX_BUFSIZE];
|
||||||
int bufptr;
|
int bufptr;
|
||||||
servreg_hack_item_t *t;
|
servreg_hack_item_t *t;
|
||||||
uip_ds6_addr_t *addr;
|
|
||||||
|
|
||||||
addr = uip_ds6_get_global(-1);
|
|
||||||
|
|
||||||
buf[MSG_FLAGS_OFFSET] = 0;
|
buf[MSG_FLAGS_OFFSET] = 0;
|
||||||
|
|
||||||
numregs = 0;
|
numregs = 0;
|
||||||
bufptr = MSG_ADDRS_OFFSET;
|
bufptr = MSG_ADDRS_OFFSET;
|
||||||
|
|
||||||
|
for(t = list_head(own_services);
|
||||||
|
(bufptr + MSG_ADDRS_LEN <= MAX_BUFSIZE) && t != NULL;
|
||||||
|
t = list_item_next(t)) {
|
||||||
|
|
||||||
if(addr != NULL) {
|
uip_ipaddr_copy((uip_ipaddr_t *)&buf[bufptr + MSG_IPADDR_SUBOFFSET],
|
||||||
for(t = list_head(own_services);
|
servreg_hack_item_address(t));
|
||||||
(bufptr + MSG_ADDRS_LEN <= MAX_BUFSIZE) && t != NULL;
|
buf[bufptr + MSG_REGS_SUBOFFSET] =
|
||||||
t = list_item_next(t)) {
|
servreg_hack_item_id(t);
|
||||||
|
buf[bufptr + MSG_REGS_SUBOFFSET + 1] =
|
||||||
|
buf[bufptr + MSG_REGS_SUBOFFSET + 2] = 0;
|
||||||
|
buf[bufptr + MSG_SEQNO_SUBOFFSET] = ((struct servreg_hack_registration *)t)->seqno;
|
||||||
|
|
||||||
uip_ipaddr_copy((uip_ipaddr_t *)&buf[bufptr + MSG_IPADDR_SUBOFFSET],
|
bufptr += MSG_ADDRS_LEN;
|
||||||
&addr->ipaddr);
|
++numregs;
|
||||||
buf[bufptr + MSG_REGS_SUBOFFSET] =
|
|
||||||
servreg_hack_item_id(t);
|
|
||||||
buf[bufptr + MSG_REGS_SUBOFFSET + 1] =
|
|
||||||
buf[bufptr + MSG_REGS_SUBOFFSET + 2] = 0;
|
|
||||||
buf[bufptr + MSG_SEQNO_SUBOFFSET] = ((struct servreg_hack_registration *)t)->seqno;
|
|
||||||
|
|
||||||
bufptr += MSG_ADDRS_LEN;
|
|
||||||
++numregs;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(t = servreg_hack_list_head();
|
for(t = servreg_hack_list_head();
|
||||||
|
@ -338,7 +340,7 @@ parse_incoming_packet(const u8_t *buf, int len)
|
||||||
numregs = buf[MSG_NUMREGS_OFFSET];
|
numregs = buf[MSG_NUMREGS_OFFSET];
|
||||||
flags = buf[MSG_FLAGS_OFFSET];
|
flags = buf[MSG_FLAGS_OFFSET];
|
||||||
|
|
||||||
/* printf("Numregs %d flags %d\n", numregs, flags);*/
|
/* printf("parse_incoming_packet Numregs %d flags %d\n", numregs, flags);*/
|
||||||
|
|
||||||
bufptr = MSG_ADDRS_OFFSET;
|
bufptr = MSG_ADDRS_OFFSET;
|
||||||
for(i = 0; i < numregs; ++i) {
|
for(i = 0; i < numregs; ++i) {
|
||||||
|
|
|
@ -81,6 +81,7 @@ void servreg_hack_init(void);
|
||||||
/**
|
/**
|
||||||
* \brief Register that this node provides a service
|
* \brief Register that this node provides a service
|
||||||
* \param service_id The 8-bit ID for the service
|
* \param service_id The 8-bit ID for the service
|
||||||
|
* \param addr The address associated with the service
|
||||||
*
|
*
|
||||||
* This function is used to register a
|
* This function is used to register a
|
||||||
* service. Registering a service means that other nodes
|
* service. Registering a service means that other nodes
|
||||||
|
@ -91,7 +92,7 @@ void servreg_hack_init(void);
|
||||||
* reached: this is up to the application that uses the
|
* reached: this is up to the application that uses the
|
||||||
* servreg-hack application.
|
* servreg-hack application.
|
||||||
*/
|
*/
|
||||||
void servreg_hack_register(servreg_hack_id_t service_id);
|
void servreg_hack_register(servreg_hack_id_t service_id, const uip_ipaddr_t *addr);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -58,7 +58,9 @@ PROCESS_THREAD(shell_ps_process, ev, data)
|
||||||
|
|
||||||
shell_output_str(&ps_command, "Processes:", "");
|
shell_output_str(&ps_command, "Processes:", "");
|
||||||
for(p = PROCESS_LIST(); p != NULL; p = p->next) {
|
for(p = PROCESS_LIST(); p != NULL; p = p->next) {
|
||||||
shell_output_str(&ps_command, (char *)p->name, "");
|
char namebuf[30];
|
||||||
|
strncpy(namebuf, PROCESS_NAME_STRING(p), sizeof(namebuf));
|
||||||
|
shell_output_str(&ps_command, namebuf, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCESS_END();
|
PROCESS_END();
|
||||||
|
|
12
apps/webserver-nano/Makefile.webserver-nano
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
webserver-nano_src = webserver-nogui.c httpd.c psock.c memb.c httpd-fs.c httpd-cgi.c
|
||||||
|
webserver-nano_dsc = webserver-dsc.c
|
||||||
|
|
||||||
|
#Run makefsdata to regenerate httpd-fsdata.c when web content has been edited. This requires PERL.
|
||||||
|
# Note: Deleting files or transferring pages from makefsdata.ignore will not trigger this rule
|
||||||
|
# when there is no change in modification dates.
|
||||||
|
$(CONTIKI)/apps/webserver-nano/httpd-fsdata.c : $(CONTIKI)/apps/webserver-nano/httpd-fs/*.*
|
||||||
|
$(CONTIKI)/tools/makefsdata -A HTTPD_STRING_ATTR -d $(CONTIKI)/apps/webserver-nano/httpd-fs -o $(CONTIKI)/apps/webserver-nano/httpd-fsdata.c
|
||||||
|
|
||||||
|
#Rebuild httpd-fs.c when makefsdata has changed httpd-fsdata.c
|
||||||
|
$(CONTIKI)/apps/webserver-nano/httpd-fs.c: $(CONTIKI)/apps/webserver-nano/httpd-fsdata.c
|
||||||
|
touch $(CONTIKI)/apps/webserver-nano/httpd-fs.c
|
280
apps/webserver-nano/httpd-cfs.c
Normal file
|
@ -0,0 +1,280 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004, Adam Dunkels.
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Author: Adam Dunkels <adam@sics.se>
|
||||||
|
*
|
||||||
|
* $Id: httpd-cfs.c,v 1.26 2011/01/25 20:13:41 oliverschmidt Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#ifndef HAVE_SNPRINTF
|
||||||
|
int snprintf(char *str, size_t size, const char *format, ...);
|
||||||
|
#endif /* HAVE_SNPRINTF */
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#include "webserver.h"
|
||||||
|
#include "cfs/cfs.h"
|
||||||
|
#include "lib/petsciiconv.h"
|
||||||
|
#include "http-strings.h"
|
||||||
|
#include "urlconv.h"
|
||||||
|
|
||||||
|
#include "httpd-cfs.h"
|
||||||
|
|
||||||
|
#ifndef WEBSERVER_CONF_CFS_CONNS
|
||||||
|
#define CONNS UIP_CONNS
|
||||||
|
#else /* WEBSERVER_CONF_CFS_CONNS */
|
||||||
|
#define CONNS WEBSERVER_CONF_CFS_CONNS
|
||||||
|
#endif /* WEBSERVER_CONF_CFS_CONNS */
|
||||||
|
|
||||||
|
#ifndef WEBSERVER_CONF_CFS_URLCONV
|
||||||
|
#define URLCONV 1
|
||||||
|
#else /* WEBSERVER_CONF_CFS_URLCONV */
|
||||||
|
#define URLCONV WEBSERVER_CONF_CFS_URLCONV
|
||||||
|
#endif /* WEBSERVER_CONF_CFS_URLCONV */
|
||||||
|
|
||||||
|
#define STATE_WAITING 0
|
||||||
|
#define STATE_OUTPUT 1
|
||||||
|
|
||||||
|
#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, strlen(str))
|
||||||
|
MEMB(conns, struct httpd_state, CONNS);
|
||||||
|
|
||||||
|
#define ISO_nl 0x0a
|
||||||
|
#define ISO_space 0x20
|
||||||
|
#define ISO_period 0x2e
|
||||||
|
#define ISO_slash 0x2f
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
PT_THREAD(send_file(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
PSOCK_BEGIN(&s->sout);
|
||||||
|
|
||||||
|
do {
|
||||||
|
/* Read data from file system into buffer */
|
||||||
|
s->len = cfs_read(s->fd, s->outputbuf, sizeof(s->outputbuf));
|
||||||
|
|
||||||
|
/* If there is data in the buffer, send it */
|
||||||
|
if(s->len > 0) {
|
||||||
|
PSOCK_SEND(&s->sout, (uint8_t *)s->outputbuf, s->len);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while(s->len > 0);
|
||||||
|
|
||||||
|
PSOCK_END(&s->sout);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
PT_THREAD(send_string(struct httpd_state *s, const char *str))
|
||||||
|
{
|
||||||
|
PSOCK_BEGIN(&s->sout);
|
||||||
|
|
||||||
|
SEND_STRING(&s->sout, str);
|
||||||
|
|
||||||
|
PSOCK_END(&s->sout);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
|
||||||
|
{
|
||||||
|
const char *ptr;
|
||||||
|
|
||||||
|
PSOCK_BEGIN(&s->sout);
|
||||||
|
|
||||||
|
SEND_STRING(&s->sout, statushdr);
|
||||||
|
|
||||||
|
ptr = strrchr(s->filename, ISO_period);
|
||||||
|
if(ptr == NULL) {
|
||||||
|
ptr = http_content_type_plain;
|
||||||
|
} else if(strcmp(http_htm, ptr) == 0) {
|
||||||
|
ptr = http_content_type_html;
|
||||||
|
} else if(strcmp(http_css, ptr) == 0) {
|
||||||
|
ptr = http_content_type_css;
|
||||||
|
} else if(strcmp(http_png, ptr) == 0) {
|
||||||
|
ptr = http_content_type_png;
|
||||||
|
} else if(strcmp(http_gif, ptr) == 0) {
|
||||||
|
ptr = http_content_type_gif;
|
||||||
|
} else if(strcmp(http_jpg, ptr) == 0) {
|
||||||
|
ptr = http_content_type_jpg;
|
||||||
|
} else {
|
||||||
|
ptr = http_content_type_binary;
|
||||||
|
}
|
||||||
|
SEND_STRING(&s->sout, ptr);
|
||||||
|
PSOCK_END(&s->sout);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
PT_THREAD(handle_output(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
PT_BEGIN(&s->outputpt);
|
||||||
|
|
||||||
|
petsciiconv_topetscii(s->filename, sizeof(s->filename));
|
||||||
|
s->fd = cfs_open(&s->filename[1], CFS_READ);
|
||||||
|
petsciiconv_toascii(s->filename, sizeof(s->filename));
|
||||||
|
if(s->fd < 0) {
|
||||||
|
strcpy(s->filename, "/notfound.htm");
|
||||||
|
s->fd = cfs_open(&s->filename[1], CFS_READ);
|
||||||
|
petsciiconv_toascii(s->filename, sizeof(s->filename));
|
||||||
|
PT_WAIT_THREAD(&s->outputpt,
|
||||||
|
send_headers(s, http_header_404));
|
||||||
|
if(s->fd < 0) {
|
||||||
|
PT_WAIT_THREAD(&s->outputpt,
|
||||||
|
send_string(s, "not found"));
|
||||||
|
uip_close();
|
||||||
|
webserver_log_file(&uip_conn->ripaddr, "404 (no notfound.htm)");
|
||||||
|
PT_EXIT(&s->outputpt);
|
||||||
|
}
|
||||||
|
webserver_log_file(&uip_conn->ripaddr, "404 - notfound.htm");
|
||||||
|
} else {
|
||||||
|
PT_WAIT_THREAD(&s->outputpt,
|
||||||
|
send_headers(s, http_header_200));
|
||||||
|
}
|
||||||
|
PT_WAIT_THREAD(&s->outputpt, send_file(s));
|
||||||
|
cfs_close(s->fd);
|
||||||
|
s->fd = -1;
|
||||||
|
PSOCK_CLOSE(&s->sout);
|
||||||
|
PT_END(&s->outputpt);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
PT_THREAD(handle_input(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
PSOCK_BEGIN(&s->sin);
|
||||||
|
|
||||||
|
PSOCK_READTO(&s->sin, ISO_space);
|
||||||
|
|
||||||
|
if(strncmp(s->inputbuf, http_get, 4) != 0) {
|
||||||
|
PSOCK_CLOSE_EXIT(&s->sin);
|
||||||
|
}
|
||||||
|
PSOCK_READTO(&s->sin, ISO_space);
|
||||||
|
|
||||||
|
if(s->inputbuf[0] != ISO_slash) {
|
||||||
|
PSOCK_CLOSE_EXIT(&s->sin);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if URLCONV
|
||||||
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
|
||||||
|
urlconv_tofilename(s->filename, s->inputbuf, sizeof(s->filename));
|
||||||
|
#else /* URLCONV */
|
||||||
|
if(s->inputbuf[1] == ISO_space) {
|
||||||
|
strncpy(s->filename, http_index_htm, sizeof(s->filename));
|
||||||
|
} else {
|
||||||
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
|
||||||
|
strncpy(s->filename, s->inputbuf, sizeof(s->filename));
|
||||||
|
}
|
||||||
|
#endif /* URLCONV */
|
||||||
|
|
||||||
|
petsciiconv_topetscii(s->filename, sizeof(s->filename));
|
||||||
|
webserver_log_file(&uip_conn->ripaddr, s->filename);
|
||||||
|
petsciiconv_toascii(s->filename, sizeof(s->filename));
|
||||||
|
s->state = STATE_OUTPUT;
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PSOCK_READTO(&s->sin, ISO_nl);
|
||||||
|
|
||||||
|
if(strncmp(s->inputbuf, http_referer, 8) == 0) {
|
||||||
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
|
||||||
|
petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
|
||||||
|
webserver_log(s->inputbuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PSOCK_END(&s->sin);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static void
|
||||||
|
handle_connection(struct httpd_state *s)
|
||||||
|
{
|
||||||
|
handle_input(s);
|
||||||
|
if(s->state == STATE_OUTPUT) {
|
||||||
|
handle_output(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
httpd_appcall(void *state)
|
||||||
|
{
|
||||||
|
struct httpd_state *s = (struct httpd_state *)state;
|
||||||
|
|
||||||
|
if(uip_closed() || uip_aborted() || uip_timedout()) {
|
||||||
|
if(s != NULL) {
|
||||||
|
if(s->fd >= 0) {
|
||||||
|
cfs_close(s->fd);
|
||||||
|
s->fd = -1;
|
||||||
|
}
|
||||||
|
memb_free(&conns, s);
|
||||||
|
}
|
||||||
|
} else if(uip_connected()) {
|
||||||
|
s = (struct httpd_state *)memb_alloc(&conns);
|
||||||
|
if(s == NULL) {
|
||||||
|
uip_abort();
|
||||||
|
webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tcp_markconn(uip_conn, s);
|
||||||
|
PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
|
||||||
|
PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
|
||||||
|
PT_INIT(&s->outputpt);
|
||||||
|
s->fd = -1;
|
||||||
|
s->state = STATE_WAITING;
|
||||||
|
timer_set(&s->timer, CLOCK_SECOND * 10);
|
||||||
|
handle_connection(s);
|
||||||
|
} else if(s != NULL) {
|
||||||
|
if(uip_poll()) {
|
||||||
|
if(timer_expired(&s->timer)) {
|
||||||
|
uip_abort();
|
||||||
|
if(s->fd >= 0) {
|
||||||
|
cfs_close(s->fd);
|
||||||
|
s->fd = -1;
|
||||||
|
}
|
||||||
|
memb_free(&conns, s);
|
||||||
|
webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
timer_restart(&s->timer);
|
||||||
|
}
|
||||||
|
handle_connection(s);
|
||||||
|
} else {
|
||||||
|
uip_abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
httpd_init(void)
|
||||||
|
{
|
||||||
|
tcp_listen(UIP_HTONS(80));
|
||||||
|
memb_init(&conns);
|
||||||
|
#if URLCONV
|
||||||
|
urlconv_init();
|
||||||
|
#endif /* URLCONV */
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
62
apps/webserver-nano/httpd-cfs.h
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2001, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 uIP TCP/IP stack.
|
||||||
|
*
|
||||||
|
* $Id: httpd-cfs.h,v 1.3 2010/04/11 20:10:12 oliverschmidt Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __HTTPD_CFS_H__
|
||||||
|
#define __HTTPD_CFS_H__
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
#ifndef WEBSERVER_CONF_CFS_PATHLEN
|
||||||
|
#define HTTPD_PATHLEN 80
|
||||||
|
#else /* WEBSERVER_CONF_CFS_CONNS */
|
||||||
|
#define HTTPD_PATHLEN WEBSERVER_CONF_CFS_PATHLEN
|
||||||
|
#endif /* WEBSERVER_CONF_CFS_CONNS */
|
||||||
|
|
||||||
|
struct httpd_state {
|
||||||
|
struct timer timer;
|
||||||
|
struct psock sin, sout;
|
||||||
|
struct pt outputpt;
|
||||||
|
char inputbuf[HTTPD_PATHLEN + 30];
|
||||||
|
char outputbuf[UIP_TCP_MSS];
|
||||||
|
char filename[HTTPD_PATHLEN];
|
||||||
|
char state;
|
||||||
|
int fd;
|
||||||
|
int len;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void httpd_init(void);
|
||||||
|
void httpd_appcall(void *state);
|
||||||
|
|
||||||
|
#endif /* __HTTPD_CFS_H__ */
|
1317
apps/webserver-nano/httpd-cgi.c
Normal file
57
apps/webserver-nano/httpd-cgi.h
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2001, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 uIP TCP/IP stack.
|
||||||
|
*
|
||||||
|
* $Id: httpd-cgi.h,v 1.3 2008/10/14 11:07:57 adamdunkels Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __HTTPD_CGI_H__
|
||||||
|
#define __HTTPD_CGI_H__
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
|
||||||
|
typedef PT_THREAD((* httpd_cgifunction)(struct httpd_state *, char *));
|
||||||
|
|
||||||
|
httpd_cgifunction httpd_cgi(char *name);
|
||||||
|
|
||||||
|
struct httpd_cgi_call {
|
||||||
|
struct httpd_cgi_call *next;
|
||||||
|
const char *name;
|
||||||
|
httpd_cgifunction function;
|
||||||
|
};
|
||||||
|
|
||||||
|
void httpd_cgi_add(struct httpd_cgi_call *c);
|
||||||
|
|
||||||
|
#define HTTPD_CGI_CALL(name, str, function) \
|
||||||
|
static struct httpd_cgi_call name = {NULL, str, function}
|
||||||
|
|
||||||
|
void httpd_cgi_init(void);
|
||||||
|
#endif /* __HTTPD_CGI_H__ */
|
135
apps/webserver-nano/httpd-fs.c
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2001, Swedish Institute of Computer Science.
|
||||||
|
* 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 lwIP TCP/IP stack.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
#include "httpd-fs.h"
|
||||||
|
#include "httpd-fsdata.h"
|
||||||
|
|
||||||
|
#include <string.h> /* for memcpy */
|
||||||
|
|
||||||
|
/* httpd-fsdata.c contains the web content.
|
||||||
|
* It is generated using the PERL script /tools/makefsdata
|
||||||
|
*/
|
||||||
|
#include "httpd-fsdata.c"
|
||||||
|
|
||||||
|
#if WEBSERVER_CONF_FILESTATS==1
|
||||||
|
u16_t httpd_filecount[HTTPD_FS_NUMFILES];
|
||||||
|
#endif
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void *
|
||||||
|
httpd_fs_get_root()
|
||||||
|
{
|
||||||
|
return (void *)HTTPD_FS_ROOT;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
u16_t
|
||||||
|
httpd_fs_get_size()
|
||||||
|
{
|
||||||
|
return HTTPD_FS_SIZE;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
uint16_t
|
||||||
|
httpd_fs_open(const char *name, struct httpd_fs_file *file)
|
||||||
|
{
|
||||||
|
#if WEBSERVER_CONF_FILESTATS
|
||||||
|
u16_t i = 0;
|
||||||
|
#endif
|
||||||
|
struct httpd_fsdata_file_noconst *f,fram;
|
||||||
|
|
||||||
|
for(f = (struct httpd_fsdata_file_noconst *)HTTPD_FS_ROOT;
|
||||||
|
f != NULL;
|
||||||
|
f = (struct httpd_fsdata_file_noconst *)fram.next) {
|
||||||
|
|
||||||
|
/*Get the linked list entry into ram from wherever it is */
|
||||||
|
httpd_memcpy(&fram,f,sizeof(fram));
|
||||||
|
|
||||||
|
/*Compare name passed in RAM with name in whatever flash the file is in */
|
||||||
|
/*makefsdata no longer adds an extra zero byte at the end of the file */
|
||||||
|
if(httpd_fs_strcmp((char *)name, fram.name) == 0) {
|
||||||
|
if (file) {
|
||||||
|
file->data = fram.data;
|
||||||
|
// file->len = fram.len-1;
|
||||||
|
file->len = fram.len;
|
||||||
|
#if WEBSERVER_CONF_FILESTATS==2 //increment count in linked list field if it is in RAM
|
||||||
|
f->count++;
|
||||||
|
}
|
||||||
|
return f->count;
|
||||||
|
}
|
||||||
|
++i
|
||||||
|
#elif WEBSERVER_CONF_FILESTATS==1 //increment count in RAM array when linked list is in flash
|
||||||
|
++httpd_filecount[i];
|
||||||
|
}
|
||||||
|
return httpd_filecount[i];
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
#else //no file statistics
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif /* WEBSERVER_CONF_FILESTATS */
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
httpd_fs_init(void)
|
||||||
|
{
|
||||||
|
#if WEBSERVER_CONF_FILESTATS==1
|
||||||
|
u16_t i;
|
||||||
|
for(i = 0; i < HTTPD_FS_NUMFILES; i++) {
|
||||||
|
httpd_filecount[i] = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
#if WEBSERVER_CONF_FILESTATS && 0
|
||||||
|
u16_t
|
||||||
|
httpd_fs_count(char *name)
|
||||||
|
{
|
||||||
|
struct httpd_fsdata_file_noconst *f;
|
||||||
|
u16_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
for(f = (struct httpd_fsdata_file_noconst *)HTTPD_FS_ROOT;
|
||||||
|
f != NULL;
|
||||||
|
f = (struct httpd_fsdata_file_noconst *)f->next) {
|
||||||
|
|
||||||
|
if(httpd_fs_strcmp(name, f->name) == 0) {
|
||||||
|
return httpd_filecount[i];
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
54
apps/webserver-nano/httpd-fs.h
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2001, Swedish Institute of Computer Science.
|
||||||
|
* 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 lwIP TCP/IP stack.
|
||||||
|
*/
|
||||||
|
#ifndef __HTTPD_FS_H__
|
||||||
|
#define __HTTPD_FS_H__
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
|
||||||
|
struct httpd_fs_file {
|
||||||
|
char *data;
|
||||||
|
int len;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* file must be allocated by caller and will be filled in
|
||||||
|
by the function. */
|
||||||
|
uint16_t httpd_fs_open(const char *name, struct httpd_fs_file *file);
|
||||||
|
|
||||||
|
#if WEBSERVER_CONF_FILESTATS
|
||||||
|
extern u16_t httpd_filecount[];
|
||||||
|
u16_t httpd_fs_count(char *name);
|
||||||
|
void* httpd_fs_get_root(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void httpd_fs_init(void);
|
||||||
|
|
||||||
|
#endif /* __HTTPD_FS_H__ */
|
1
apps/webserver-nano/httpd-fs/404.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<html><body>404 - file not found - go to <a href="/">index</a></body></html>
|
4
apps/webserver-nano/httpd-fs/files.shtml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
%! header.html
|
||||||
|
<h1>File statistics</h1><br><table width="100%"><tr align="left"><th>Name</th><th>Accesses</th></tr>
|
||||||
|
%! file-stats *
|
||||||
|
</table></body></html>
|
3
apps/webserver-nano/httpd-fs/index.shtml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
%! header
|
||||||
|
Welcome to the <a href="http://www.sics.se/contiki/">Contiki</a> nano web server!
|
||||||
|
%! file-stats .
|
1
apps/webserver-nano/httpd-fs/makefsdata.ignore/404.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<html><body>404 - file not found - go to <a href="/">index</a></body></html>
|
|
@ -0,0 +1,5 @@
|
||||||
|
Move the desired pages into httpd-fs and regenerate httpd-fsdata.c using the PERL script makefsdata.
|
||||||
|
Stage unused pages in this directory; anything here will be ignored by makefsdata.
|
||||||
|
When using non-ram storage it must be invoked with the HTTPD_STRING_ATTR!
|
||||||
|
E.g. cd ~/contiki/apps/webserver (or webserver-nano, -micro, -mini, ...)
|
||||||
|
../../tools/makefsdata -A HTTPD_STRING_ATTR
|
26
apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.js
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<script>function lo(){ls();}window.onload=lo;</script>
|
||||||
|
<script type="text/javascript">var sts=new Date(),r,x,b,j;
|
||||||
|
function e(el){return document.getElementById(el);}
|
||||||
|
function ls(){try{r=new XMLHttpRequest();}catch(e){alert("Browswer does not support Ajax");return false;}r.open("GET","/ajaxdata.shtml",true);
|
||||||
|
x=0;r.onreadystatechange=function(){e('date').innerHTML=(new Date()).toTimeString();if(r.readyState>0){j=r.responseText.substr(x);x+=j.length;eval(j);}};r.send(null);}
|
||||||
|
function s(el,n,max,text){e(el).innerHTML='<table width=504 border=0 cellpadding=1 cellspacing=0>'+'<tr><td width=200>'+text+'</td>'+'<td width='+(10+300*n/max)+' bgcolor="gray"> </td>'+'<td width='+(310-300*n/max)+' bgcolor="lightgray"> </td>'+'</table>';}
|
||||||
|
function wt(m){document.title=m;e('v').innerHTML=m;}
|
||||||
|
function dc(n,d){return n.toFixed(d);}
|
||||||
|
function t(m){n=dc(m/10,1);s('temp',n,50,'Temperature '+n+' °C');}
|
||||||
|
function b(m){s('batt',m,5000,'Battery '+dc(m/1000,3)+' volts');}
|
||||||
|
function ax(m){s('ax',m+2000,4000,'Acceleration (X-axis) '+m+'mg');}
|
||||||
|
function ay(m){s('ay',m+2000,4000,'Acceleration (Y-axis) '+m+'mg');}
|
||||||
|
function az(m){s('az',m+2000,4000,'Acceleration (Z-axis) '+m+'mg');}
|
||||||
|
function rs(m){s('rs',n,100,'RSSI '+n);}
|
||||||
|
function p(c,l,t,r){cp=0.136*c;lt=0.478*t;lr=0.412*r;n=cp+lt+lr;//128rfa1@3v3
|
||||||
|
s('p',n,200,'Power consumption '+dc(n,2)+' mW');
|
||||||
|
s('pc',cp,100,'CPU power '+dc(cp,2)+' mW');
|
||||||
|
s('pr',lr,200,'Radio RX power '+dc(lr,2)+' mW');
|
||||||
|
s('pt',lt,200,'Radio TX power '+dc(lt,2)+' mW');}
|
||||||
|
function adc(a0,a1,a2,a3,a4,a5,a6,a7){var t=e('csv'),s=t.scrollTop;
|
||||||
|
t.value+=dc(((new Date()).getTime()-sts.getTime())/1000,1)+','+a0+','+a1+','+a2+','+a3+','+a4+','+a5+','+a6+','+a7+'\r';
|
||||||
|
if((s+t.clientHeight+30)<t.scrollHeight)t.scrollTop=s;else t.scrollTop=s+42;}
|
||||||
|
function rime(tx,rx,tl,rl){e('rims').innerHTML='<em>RIMESTATS:</em> Tx= '+tx+' Rx= '+rx+' TxL= '+tl+' RxL= '+rl;}
|
||||||
|
function ener(cp,tx,rx) {e('ener').innerHTML='<em>ENERGEST :</em> CPU= '+dc(cp/100,2)+'% Tx= '+dc(tx/100,2)+'% Rx= '+dc(rx/100,2)+'%';}
|
||||||
|
function v(n){e('v').innerHTML='Iterations = '+n;}
|
||||||
|
</script>
|
11
apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.shtml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
%! header
|
||||||
|
%!: /ajax.js
|
||||||
|
<input type="button" value="RUN" onclick="ls()"><input type="button" value="STOP" onclick="r.abort()"><br><span id="date"></span>
|
||||||
|
<div id="v"></div>
|
||||||
|
<pre><span id="rims"></span><div id="ener"></div></pre>
|
||||||
|
<h2>Sensors</h2>
|
||||||
|
<div id="temp"></div><div id="batt"></div>
|
||||||
|
<div id="ax"></div><div id="ay"></div><div id="az"></div>
|
||||||
|
<div id="p"></div><div id="pc"></div><div id="pr"></div><div id="pt"></div>
|
||||||
|
<br><textarea id="csv" class="example-code" READONLY style="height:150px;width:500px;overflow:auto;onclick="this.focus();"></textarea>
|
||||||
|
%! file-stats .
|
|
@ -0,0 +1 @@
|
||||||
|
%! ajaxdata
|
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/favicon.gif
Normal file
After Width: | Height: | Size: 97 B |
|
@ -0,0 +1,4 @@
|
||||||
|
%! header.html
|
||||||
|
<h1>File statistics</h1><br><table width="100%"><tr align="left"><th>Name</th><th>Accesses</th></tr>
|
||||||
|
%! file-stats *
|
||||||
|
</table></body></html>
|
|
@ -0,0 +1 @@
|
||||||
|
</body></html>
|
|
@ -0,0 +1 @@
|
||||||
|
<html><head><title>Contiki-nano</title></head><pre><a href="/">Front page</a>|<a href="status.shtml">Status</a>|<a href="tcp.shtml">Network connections</a>|<a href="processes.shtml">System processes</a>|<a href="files.shtml">File statistics</a></pre>
|
|
@ -0,0 +1 @@
|
||||||
|
<html><head><title>Contiki-nano</title> </head>Welcome to the <a href="http://www.sics.se/contiki/">Contiki</a> nano web server!
|
|
@ -0,0 +1,3 @@
|
||||||
|
%! header
|
||||||
|
Welcome to the <a href="http://www.sics.se/contiki/">Contiki</a> nano web server!
|
||||||
|
%! file-stats .
|
|
@ -0,0 +1,6 @@
|
||||||
|
%! header
|
||||||
|
<h1>System processes</h1><br><table width="100%">
|
||||||
|
<tr><th>ID</th><th>Name</th><th>Thread</th><th>Process state</th></tr>
|
||||||
|
%! processes
|
||||||
|
</table>
|
||||||
|
%! file-stats .
|
|
@ -0,0 +1,2 @@
|
||||||
|
user-agent: *
|
||||||
|
Disallow: /
|
10
apps/webserver-nano/httpd-fs/makefsdata.ignore/status.shtml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
%! header
|
||||||
|
<h4>Addresses</h4>
|
||||||
|
%! addresses
|
||||||
|
<h4>Neighbors</h4>
|
||||||
|
%! neighbors
|
||||||
|
<h4>Routes</h4>
|
||||||
|
%! routes
|
||||||
|
<h4>Sensors</h4>
|
||||||
|
%! sensors
|
||||||
|
%! file-stats .
|
6
apps/webserver-nano/httpd-fs/makefsdata.ignore/tcp.shtml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
%! header.html
|
||||||
|
<h1>Current connections</h1><br><table width="100%">
|
||||||
|
<tr><th>Local</th><th>Remote</th><th>State</th><th>Retransmissions</th><th>Timer</th><th>Flags</th></tr>
|
||||||
|
%! tcp-connections
|
||||||
|
</table>
|
||||||
|
%! file-stats .
|
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/b.gif
Normal file
After Width: | Height: | Size: 70 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/bc.gif
Normal file
After Width: | Height: | Size: 125 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/bh.gif
Normal file
After Width: | Height: | Size: 81 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/bv.gif
Normal file
After Width: | Height: | Size: 117 B |
|
@ -0,0 +1 @@
|
||||||
|
<html><head><title>Contiki-nano</title> </head>Welcome to the <a href="http://www.sics.se/contiki/">Contiki</a> nano web server!
|
|
@ -0,0 +1,3 @@
|
||||||
|
<html><head><link rel="stylesheet" type="text/css" href="s.css"></head><body><center>
|
||||||
|
%! tictac
|
||||||
|
</center></body></html>
|
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/o.gif
Normal file
After Width: | Height: | Size: 145 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/oc.gif
Normal file
After Width: | Height: | Size: 192 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/oh.gif
Normal file
After Width: | Height: | Size: 147 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/ov.gif
Normal file
After Width: | Height: | Size: 188 B |
1
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/s.css
Normal file
|
@ -0,0 +1 @@
|
||||||
|
img{border-style: none;}
|
|
@ -0,0 +1,3 @@
|
||||||
|
<html><head><link rel="stylesheet" type="text/css" href="s.css"></head><body><center>
|
||||||
|
%! tictac
|
||||||
|
</center></body></html>
|
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/x.gif
Normal file
After Width: | Height: | Size: 148 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/xc.gif
Normal file
After Width: | Height: | Size: 189 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/xh.gif
Normal file
After Width: | Height: | Size: 153 B |
BIN
apps/webserver-nano/httpd-fs/makefsdata.ignore/ttt/xv.gif
Normal file
After Width: | Height: | Size: 192 B |
|
@ -0,0 +1,8 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<form action="upload.html" enctype="multipart/form-data" method="post">
|
||||||
|
<input name="userfile" type="file" size="50" />
|
||||||
|
<input value="Upload" type="submit" />
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
10
apps/webserver-nano/httpd-fs/status.shtml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
%! header
|
||||||
|
<h4>Addresses</h4>
|
||||||
|
%! addresses
|
||||||
|
<h4>Neighbors</h4>
|
||||||
|
%! neighbors
|
||||||
|
<h4>Routes</h4>
|
||||||
|
%! routes
|
||||||
|
<h4>Sensors</h4>
|
||||||
|
%! sensors
|
||||||
|
%! file-stats .
|
6
apps/webserver-nano/httpd-fs/tcp.shtml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
%! header.html
|
||||||
|
<h1>Current connections</h1><br><table width="100%">
|
||||||
|
<tr><th>Local</th><th>Remote</th><th>State</th><th>Retransmissions</th><th>Timer</th><th>Flags</th></tr>
|
||||||
|
%! tcp-connections
|
||||||
|
</table>
|
||||||
|
%! file-stats .
|
116
apps/webserver-nano/httpd-fsdata.c
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
/*********Generated by contiki/tools/makefsdata on 2011-09-10*********/
|
||||||
|
|
||||||
|
|
||||||
|
const char data_404_html[86] HTTPD_STRING_ATTR = {
|
||||||
|
/* /404.html */
|
||||||
|
0x2f, 0x34, 0x30, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x00,
|
||||||
|
0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x3c, 0x62, 0x6f, 0x64,
|
||||||
|
0x79, 0x3e, 0x34, 0x30, 0x34, 0x20, 0x2d, 0x20, 0x66, 0x69,
|
||||||
|
0x6c, 0x65, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x66, 0x6f, 0x75,
|
||||||
|
0x6e, 0x64, 0x20, 0x2d, 0x20, 0x67, 0x6f, 0x20, 0x74, 0x6f,
|
||||||
|
0x20, 0x3c, 0x61, 0x20, 0x68, 0x72, 0x65, 0x66, 0x3d, 0x22,
|
||||||
|
0x2f, 0x22, 0x3e, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x3c, 0x2f,
|
||||||
|
0x61, 0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
|
||||||
|
0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e};
|
||||||
|
|
||||||
|
const char data_files_shtml[168] HTTPD_STRING_ATTR = {
|
||||||
|
/* /files.shtml */
|
||||||
|
0x2f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x73, 0x68, 0x74, 0x6d, 0x6c, 0x00,
|
||||||
|
0x25, 0x21, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x2e,
|
||||||
|
0x68, 0x74, 0x6d, 0x6c, 0x0a, 0x3c, 0x68, 0x31, 0x3e, 0x46,
|
||||||
|
0x69, 0x6c, 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73,
|
||||||
|
0x74, 0x69, 0x63, 0x73, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0x3c,
|
||||||
|
0x62, 0x72, 0x3e, 0x3c, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20,
|
||||||
|
0x77, 0x69, 0x64, 0x74, 0x68, 0x3d, 0x22, 0x31, 0x30, 0x30,
|
||||||
|
0x25, 0x22, 0x3e, 0x3c, 0x74, 0x72, 0x20, 0x61, 0x6c, 0x69,
|
||||||
|
0x67, 0x6e, 0x3d, 0x22, 0x6c, 0x65, 0x66, 0x74, 0x22, 0x3e,
|
||||||
|
0x3c, 0x74, 0x68, 0x3e, 0x4e, 0x61, 0x6d, 0x65, 0x3c, 0x2f,
|
||||||
|
0x74, 0x68, 0x3e, 0x3c, 0x74, 0x68, 0x3e, 0x41, 0x63, 0x63,
|
||||||
|
0x65, 0x73, 0x73, 0x65, 0x73, 0x3c, 0x2f, 0x74, 0x68, 0x3e,
|
||||||
|
0x3c, 0x2f, 0x74, 0x72, 0x3e, 0x0a, 0x25, 0x21, 0x20, 0x66,
|
||||||
|
0x69, 0x6c, 0x65, 0x2d, 0x73, 0x74, 0x61, 0x74, 0x73, 0x20,
|
||||||
|
0x2a, 0x0a, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e,
|
||||||
|
0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c, 0x2f, 0x68,
|
||||||
|
0x74, 0x6d, 0x6c, 0x3e, 0x0a};
|
||||||
|
|
||||||
|
const char data_index_shtml[120] HTTPD_STRING_ATTR = {
|
||||||
|
/* /index.shtml */
|
||||||
|
0x2f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2e, 0x73, 0x68, 0x74, 0x6d, 0x6c, 0x00,
|
||||||
|
0x25, 0x21, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x0a,
|
||||||
|
0x57, 0x65, 0x6c, 0x63, 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x6f,
|
||||||
|
0x20, 0x74, 0x68, 0x65, 0x20, 0x3c, 0x61, 0x20, 0x68, 0x72,
|
||||||
|
0x65, 0x66, 0x3d, 0x22, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
|
||||||
|
0x2f, 0x77, 0x77, 0x77, 0x2e, 0x73, 0x69, 0x63, 0x73, 0x2e,
|
||||||
|
0x73, 0x65, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x69, 0x6b, 0x69,
|
||||||
|
0x2f, 0x22, 0x3e, 0x43, 0x6f, 0x6e, 0x74, 0x69, 0x6b, 0x69,
|
||||||
|
0x3c, 0x2f, 0x61, 0x3e, 0x20, 0x6e, 0x61, 0x6e, 0x6f, 0x20,
|
||||||
|
0x77, 0x65, 0x62, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||||
|
0x21, 0x0a, 0x25, 0x21, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2d,
|
||||||
|
0x73, 0x74, 0x61, 0x74, 0x73, 0x20, 0x2e};
|
||||||
|
|
||||||
|
const char data_status_shtml[158] HTTPD_STRING_ATTR = {
|
||||||
|
/* /status.shtml */
|
||||||
|
0x2f, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x2e, 0x73, 0x68, 0x74, 0x6d, 0x6c, 0x00,
|
||||||
|
0x25, 0x21, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x0a,
|
||||||
|
0x3c, 0x68, 0x34, 0x3e, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73,
|
||||||
|
0x73, 0x65, 0x73, 0x3c, 0x2f, 0x68, 0x34, 0x3e, 0x0a, 0x25,
|
||||||
|
0x21, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x65,
|
||||||
|
0x73, 0x0a, 0x3c, 0x68, 0x34, 0x3e, 0x4e, 0x65, 0x69, 0x67,
|
||||||
|
0x68, 0x62, 0x6f, 0x72, 0x73, 0x3c, 0x2f, 0x68, 0x34, 0x3e,
|
||||||
|
0x0a, 0x25, 0x21, 0x20, 0x6e, 0x65, 0x69, 0x67, 0x68, 0x62,
|
||||||
|
0x6f, 0x72, 0x73, 0x0a, 0x3c, 0x68, 0x34, 0x3e, 0x52, 0x6f,
|
||||||
|
0x75, 0x74, 0x65, 0x73, 0x3c, 0x2f, 0x68, 0x34, 0x3e, 0x0a,
|
||||||
|
0x25, 0x21, 0x20, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x0a,
|
||||||
|
0x3c, 0x68, 0x34, 0x3e, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72,
|
||||||
|
0x73, 0x3c, 0x2f, 0x68, 0x34, 0x3e, 0x0a, 0x25, 0x21, 0x20,
|
||||||
|
0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x0a, 0x25, 0x21,
|
||||||
|
0x20, 0x66, 0x69, 0x6c, 0x65, 0x2d, 0x73, 0x74, 0x61, 0x74,
|
||||||
|
0x73, 0x20, 0x2e, 0x0a};
|
||||||
|
|
||||||
|
const char data_tcp_shtml[227] HTTPD_STRING_ATTR = {
|
||||||
|
/* /tcp.shtml */
|
||||||
|
0x2f, 0x74, 0x63, 0x70, 0x2e, 0x73, 0x68, 0x74, 0x6d, 0x6c, 0x00,
|
||||||
|
0x25, 0x21, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x2e,
|
||||||
|
0x68, 0x74, 0x6d, 0x6c, 0x0a, 0x3c, 0x68, 0x31, 0x3e, 0x43,
|
||||||
|
0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x20, 0x63, 0x6f, 0x6e,
|
||||||
|
0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x3c, 0x2f,
|
||||||
|
0x68, 0x31, 0x3e, 0x3c, 0x62, 0x72, 0x3e, 0x3c, 0x74, 0x61,
|
||||||
|
0x62, 0x6c, 0x65, 0x20, 0x77, 0x69, 0x64, 0x74, 0x68, 0x3d,
|
||||||
|
0x22, 0x31, 0x30, 0x30, 0x25, 0x22, 0x3e, 0x0a, 0x3c, 0x74,
|
||||||
|
0x72, 0x3e, 0x3c, 0x74, 0x68, 0x3e, 0x4c, 0x6f, 0x63, 0x61,
|
||||||
|
0x6c, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0x3c, 0x74, 0x68, 0x3e,
|
||||||
|
0x52, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x3c, 0x2f, 0x74, 0x68,
|
||||||
|
0x3e, 0x3c, 0x74, 0x68, 0x3e, 0x53, 0x74, 0x61, 0x74, 0x65,
|
||||||
|
0x3c, 0x2f, 0x74, 0x68, 0x3e, 0x3c, 0x74, 0x68, 0x3e, 0x52,
|
||||||
|
0x65, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6d, 0x69, 0x73, 0x73,
|
||||||
|
0x69, 0x6f, 0x6e, 0x73, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0x3c,
|
||||||
|
0x74, 0x68, 0x3e, 0x54, 0x69, 0x6d, 0x65, 0x72, 0x3c, 0x2f,
|
||||||
|
0x74, 0x68, 0x3e, 0x3c, 0x74, 0x68, 0x3e, 0x46, 0x6c, 0x61,
|
||||||
|
0x67, 0x73, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0x3c, 0x2f, 0x74,
|
||||||
|
0x72, 0x3e, 0x0a, 0x25, 0x21, 0x20, 0x74, 0x63, 0x70, 0x2d,
|
||||||
|
0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
|
||||||
|
0x73, 0x0a, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e,
|
||||||
|
0x0a, 0x25, 0x21, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2d, 0x73,
|
||||||
|
0x74, 0x61, 0x74, 0x73, 0x20, 0x2e};
|
||||||
|
|
||||||
|
|
||||||
|
/* Structure of linked list (all offsets relative to start of section):
|
||||||
|
struct httpd_fsdata_file {
|
||||||
|
const struct httpd_fsdata_file *next; //actual flash address of next link
|
||||||
|
const char *name; //offset to coffee file name
|
||||||
|
const char *data; //offset to coffee file data
|
||||||
|
const int len; //length of file data
|
||||||
|
#if HTTPD_FS_STATISTICS == 1 //not enabled since list is in PROGMEM
|
||||||
|
u16_t count; //storage for file statistics
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
const struct httpd_fsdata_file file_404_html[] HTTPD_STRING_ATTR ={{ NULL, data_404_html , data_404_html +10, sizeof(data_404_html) -10}};
|
||||||
|
const struct httpd_fsdata_file file_files_shtml[] HTTPD_STRING_ATTR ={{ file_404_html, data_files_shtml , data_files_shtml +13, sizeof(data_files_shtml) -13}};
|
||||||
|
const struct httpd_fsdata_file file_index_shtml[] HTTPD_STRING_ATTR ={{ file_files_shtml, data_index_shtml , data_index_shtml +13, sizeof(data_index_shtml) -13}};
|
||||||
|
const struct httpd_fsdata_file file_status_shtml[] HTTPD_STRING_ATTR ={{ file_index_shtml, data_status_shtml , data_status_shtml +14, sizeof(data_status_shtml) -14}};
|
||||||
|
const struct httpd_fsdata_file file_tcp_shtml[] HTTPD_STRING_ATTR ={{ file_status_shtml, data_tcp_shtml , data_tcp_shtml +11, sizeof(data_tcp_shtml) -11}};
|
||||||
|
|
||||||
|
#define HTTPD_FS_ROOT file_tcp_shtml
|
||||||
|
#define HTTPD_FS_NUMFILES 5
|
||||||
|
#define HTTPD_FS_SIZE 759
|
64
apps/webserver-nano/httpd-fsdata.h
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2001, Swedish Institute of Computer Science.
|
||||||
|
* 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 lwIP TCP/IP stack.
|
||||||
|
*
|
||||||
|
* Author: Adam Dunkels <adam@sics.se>
|
||||||
|
*
|
||||||
|
* $Id: httpd-fsdata.h,v 1.1 2006/06/17 22:41:14 adamdunkels Exp $
|
||||||
|
*/
|
||||||
|
#ifndef __HTTPD_FSDATA_H__
|
||||||
|
#define __HTTPD_FSDATA_H__
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
struct httpd_fsdata_file {
|
||||||
|
const struct httpd_fsdata_file *next;
|
||||||
|
const char *name;
|
||||||
|
const char *data;
|
||||||
|
const int len;
|
||||||
|
#ifdef HTTPD_FS_STATISTICS
|
||||||
|
#if HTTPD_FS_STATISTICS == 1
|
||||||
|
u16_t count;
|
||||||
|
#endif /* HTTPD_FS_STATISTICS */
|
||||||
|
#endif /* HTTPD_FS_STATISTICS */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct httpd_fsdata_file_noconst {
|
||||||
|
struct httpd_fsdata_file *next;
|
||||||
|
char *name;
|
||||||
|
char *data;
|
||||||
|
int len;
|
||||||
|
#ifdef HTTPD_FS_STATISTICS
|
||||||
|
#if HTTPD_FS_STATISTICS == 1
|
||||||
|
u16_t count;
|
||||||
|
#endif /* HTTPD_FS_STATISTICS */
|
||||||
|
#endif /* HTTPD_FS_STATISTICS */
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* __HTTPD_FSDATA_H__ */
|
516
apps/webserver-nano/httpd.c
Normal file
|
@ -0,0 +1,516 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004, Adam Dunkels.
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Configurable Contiki Webserver - see httpd.h */
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
|
||||||
|
#include "webserver.h"
|
||||||
|
#include "httpd-fs.h"
|
||||||
|
|
||||||
|
#if WEBSERVER_CONF_CGI
|
||||||
|
#include "httpd-cgi.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "lib/petsciiconv.h"
|
||||||
|
|
||||||
|
#if COFFEE_FILES
|
||||||
|
#include "cfs-coffee-arch.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#define PRINTD PRINTA
|
||||||
|
#else
|
||||||
|
#define PRINTD(...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define STATE_WAITING 0
|
||||||
|
#define STATE_OUTPUT 1
|
||||||
|
/* Allocate memory for the tcp connections */
|
||||||
|
MEMB(conns, struct httpd_state, WEBSERVER_CONF_CONNS);
|
||||||
|
|
||||||
|
#define ISO_tab 0x09
|
||||||
|
#define ISO_nl 0x0a
|
||||||
|
#define ISO_cr 0x0d
|
||||||
|
#define ISO_space 0x20
|
||||||
|
#define ISO_bang 0x21
|
||||||
|
#define ISO_percent 0x25
|
||||||
|
#define ISO_period 0x2e
|
||||||
|
#define ISO_slash 0x2f
|
||||||
|
#define ISO_colon 0x3a
|
||||||
|
#define ISO_qmark 0x3f
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static unsigned short
|
||||||
|
generate(void *state)
|
||||||
|
{
|
||||||
|
struct httpd_state *s = (struct httpd_state *)state;
|
||||||
|
|
||||||
|
if(s->file.len > uip_mss()) {
|
||||||
|
s->len = uip_mss();
|
||||||
|
} else {
|
||||||
|
s->len = s->file.len;
|
||||||
|
}
|
||||||
|
httpd_fs_cpy(uip_appdata, s->file.data, s->len);
|
||||||
|
return s->len;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static
|
||||||
|
PT_THREAD(send_file(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
PSOCK_BEGIN(&s->sout);
|
||||||
|
|
||||||
|
do {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate, s);
|
||||||
|
s->file.len -= s->len;
|
||||||
|
s->file.data += s->len;
|
||||||
|
} while(s->file.len > 0);
|
||||||
|
|
||||||
|
PSOCK_END(&s->sout);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
static
|
||||||
|
PT_THREAD(send_part_of_file(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
PSOCK_BEGIN(&s->sout);
|
||||||
|
|
||||||
|
static int oldfilelen, oldlen;
|
||||||
|
static char * olddata;
|
||||||
|
|
||||||
|
//Store stuff that gets clobbered...
|
||||||
|
oldfilelen = s->file.len;
|
||||||
|
oldlen = s->len;
|
||||||
|
olddata = s->file.data;
|
||||||
|
|
||||||
|
s->file.len = s->len;
|
||||||
|
do {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate, s);
|
||||||
|
s->file.len -= s->len;
|
||||||
|
s->file.data += s->len;
|
||||||
|
} while(s->file.len > 0);
|
||||||
|
|
||||||
|
s->len = oldlen;
|
||||||
|
s->file.len = oldfilelen;
|
||||||
|
s->file.data = olddata;
|
||||||
|
|
||||||
|
PSOCK_END(&s->sout);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static void
|
||||||
|
next_scriptstate(struct httpd_state *s)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
/* Skip over any script parameters to the beginning of the next line */
|
||||||
|
if((p = (char *)httpd_fs_strchr(s->scriptptr, ISO_nl)) != NULL) {
|
||||||
|
p += 1;
|
||||||
|
s->scriptlen -= (unsigned short)(p - s->scriptptr);
|
||||||
|
s->scriptptr = p;
|
||||||
|
} else {
|
||||||
|
s->scriptlen = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
char *
|
||||||
|
get_scriptname(char *dest, char *fromfile)
|
||||||
|
{
|
||||||
|
uint8_t i=0,skip=1;
|
||||||
|
/* Extract a file or cgi name, trim leading spaces and replace termination with zero */
|
||||||
|
/* Returns number of characters processed up to the next non-tab or space */
|
||||||
|
do {
|
||||||
|
dest[i]=httpd_fs_getchar(fromfile++);
|
||||||
|
if (dest[i]==ISO_colon) {if (!skip) break;} //allow leading colons
|
||||||
|
else if (dest[i]==ISO_tab ) {if (skip) continue;else break;}//skip leading tabs
|
||||||
|
else if (dest[i]==ISO_space) {if (skip) continue;else break;}//skip leading spaces
|
||||||
|
else if (dest[i]==ISO_nl ) break; //nl is preferred delimiter
|
||||||
|
else if (dest[i]==ISO_cr ) break; //some editors insert cr
|
||||||
|
else if (dest[i]==0 ) break; //files are terminated with null
|
||||||
|
else skip=0;
|
||||||
|
i++;
|
||||||
|
} while (i<(MAX_SCRIPT_NAME_LENGTH+1));
|
||||||
|
fromfile--;
|
||||||
|
while ((dest[i]==ISO_space) || (dest[i]==ISO_tab)) dest[i]=httpd_fs_getchar(++fromfile);
|
||||||
|
dest[i]=0;
|
||||||
|
return (fromfile);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
PT_THREAD(handle_script(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
/* Note script includes will attach a leading : to the filename and a trailing zero */
|
||||||
|
static char scriptname[MAX_SCRIPT_NAME_LENGTH+1],*pptr;
|
||||||
|
static uint16_t filelength;
|
||||||
|
|
||||||
|
PT_BEGIN(&s->scriptpt);
|
||||||
|
|
||||||
|
filelength=s->file.len;
|
||||||
|
while(s->file.len > 0) {
|
||||||
|
/* Sanity check */
|
||||||
|
if (s->file.len > filelength) break;
|
||||||
|
|
||||||
|
/* Check if we should start executing a script, flagged by %! */
|
||||||
|
if(httpd_fs_getchar(s->file.data) == ISO_percent &&
|
||||||
|
httpd_fs_getchar(s->file.data + 1) == ISO_bang) {
|
||||||
|
|
||||||
|
/* Extract name, if starts with colon include file else call cgi */
|
||||||
|
s->scriptptr=get_scriptname(scriptname,s->file.data+2);
|
||||||
|
s->scriptlen=s->file.len-(s->scriptptr-s->file.data);
|
||||||
|
PRINTD("httpd: Handle script named %s\n",scriptname);
|
||||||
|
if(scriptname[0] == ISO_colon) {
|
||||||
|
#if WEBSERVER_CONF_INCLUDE
|
||||||
|
if (httpd_fs_open(&scriptname[1], &s->file)) {
|
||||||
|
PT_WAIT_THREAD(&s->scriptpt, send_file(s));
|
||||||
|
}
|
||||||
|
/*TODO dont print anything if file not found */
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
#if WEBSERVER_CONF_CGI
|
||||||
|
PT_WAIT_THREAD(&s->scriptpt,httpd_cgi(scriptname)(s, s->scriptptr));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
next_scriptstate(s);
|
||||||
|
|
||||||
|
/* Reset the pointers and continue sending the current file. */
|
||||||
|
s->file.data = s->scriptptr;
|
||||||
|
s->file.len = s->scriptlen;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
/* Send file up to the next potential script */
|
||||||
|
if(s->file.len > uip_mss()) {
|
||||||
|
s->len = uip_mss();
|
||||||
|
} else {
|
||||||
|
s->len = s->file.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(httpd_fs_getchar(s->file.data) == ISO_percent) {
|
||||||
|
pptr = (char *) httpd_fs_strchr(s->file.data + 1, ISO_percent);
|
||||||
|
} else {
|
||||||
|
pptr = (char *) httpd_fs_strchr(s->file.data, ISO_percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pptr != NULL && pptr != s->file.data) {
|
||||||
|
s->len = (int)(pptr - s->file.data);
|
||||||
|
if(s->len >= uip_mss()) {
|
||||||
|
s->len = uip_mss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PRINTD("httpd: Sending %u bytes from 0x%04x\n",s->file.len,(unsigned int)s->file.data);
|
||||||
|
PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s));
|
||||||
|
s->file.data += s->len;
|
||||||
|
s->file.len -= s->len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PT_END(&s->scriptpt);
|
||||||
|
}
|
||||||
|
#endif /* WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI */
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
const char httpd_http[] HTTPD_STRING_ATTR = "HTTP/1.0 ";
|
||||||
|
const char httpd_server[] HTTPD_STRING_ATTR = "\r\nServer: Contiki/2.0 http://www.sics.se/contiki/\r\nConnection: close\r\n";
|
||||||
|
static unsigned short
|
||||||
|
generate_status(void *sstr)
|
||||||
|
{
|
||||||
|
uint8_t slen=httpd_strlen((char *)sstr);
|
||||||
|
httpd_memcpy(uip_appdata, httpd_http, sizeof(httpd_http)-1);
|
||||||
|
httpd_memcpy(uip_appdata+sizeof(httpd_http)-1, (char *)sstr, slen);
|
||||||
|
slen+=sizeof(httpd_http)-1;
|
||||||
|
httpd_memcpy(uip_appdata+slen, httpd_server, sizeof(httpd_server)-1);
|
||||||
|
return slen+sizeof(httpd_server)-1;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
const char httpd_content[] HTTPD_STRING_ATTR = "Content-type: ";
|
||||||
|
const char httpd_crlf[] HTTPD_STRING_ATTR = "\r\n\r\n";
|
||||||
|
static unsigned short
|
||||||
|
generate_header(void *hstr)
|
||||||
|
{
|
||||||
|
uint8_t slen=httpd_strlen((char *)hstr);
|
||||||
|
httpd_memcpy(uip_appdata,httpd_content,sizeof(httpd_content)-1);
|
||||||
|
httpd_memcpy(uip_appdata+sizeof(httpd_content)-1, (char *)hstr, slen);
|
||||||
|
slen+=sizeof(httpd_content)-1;
|
||||||
|
httpd_memcpy(uip_appdata+slen,httpd_crlf,sizeof(httpd_crlf)-1);
|
||||||
|
return slen+4;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
const char httpd_mime_htm[] HTTPD_STRING_ATTR = "text/html";
|
||||||
|
#if WEBSERVER_CONF_CSS
|
||||||
|
const char httpd_mime_css[] HTTPD_STRING_ATTR = "text/css";
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_PNG
|
||||||
|
const char httpd_mime_png[] HTTPD_STRING_ATTR = "image/png";
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_GIF
|
||||||
|
const char httpd_mime_gif[] HTTPD_STRING_ATTR = "image/gif";
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_JPG
|
||||||
|
const char httpd_mime_jpg[] HTTPD_STRING_ATTR = "image/jpeg";
|
||||||
|
const char httpd_jpg [] HTTPD_STRING_ATTR = "jpg";
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_TXT
|
||||||
|
const char httpd_mime_txt[] HTTPD_STRING_ATTR = "text/plain";
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_BIN
|
||||||
|
const char httpd_mime_bin[] HTTPD_STRING_ATTR = "application/octet-stream";
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
const char httpd_shtml [] HTTPD_STRING_ATTR = ".shtml";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static
|
||||||
|
PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
|
||||||
|
{
|
||||||
|
char *ptr;
|
||||||
|
PSOCK_BEGIN(&s->sout);
|
||||||
|
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_status, (char *)statushdr);
|
||||||
|
|
||||||
|
ptr = strrchr(s->filename, ISO_period);
|
||||||
|
if (httpd_strncmp("4", statushdr, 1)==0) { //404
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm );
|
||||||
|
} else if(ptr == NULL) {
|
||||||
|
#if WEBSERVER_CONF_BIN
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_bin );
|
||||||
|
#else
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm );
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
ptr++;
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
if(httpd_strncmp(ptr, &httpd_mime_htm[5],3)== 0 ||httpd_strncmp(ptr, &httpd_shtml[1], 4) == 0) {
|
||||||
|
#else
|
||||||
|
if(httpd_strncmp(ptr, &httpd_mime_htm[5],3)== 0) {
|
||||||
|
#endif
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm );
|
||||||
|
#if WEBSEVER_CONF_CSS
|
||||||
|
} else if(httpd_strcmp(ptr, &httpd_mime_css[5]) == 0) {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_css );
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_PNG
|
||||||
|
} else if(httpd_strcmp(ptr, &httpd_mime_png[6]) == 0) {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_png );
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_GIF
|
||||||
|
} else if(httpd_strcmp(ptr, &httpd_mime_gif[6])== 0) {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_gif );
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_JPG
|
||||||
|
} else if(httpd_strcmp(ptr, httpd_mime_jpg) == 0) {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_jpg );
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_TXT
|
||||||
|
} else {
|
||||||
|
PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_txt);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PSOCK_END(&s->sout);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
const char httpd_indexfn [] HTTPD_STRING_ATTR = "/index.html";
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
const char httpd_indexsfn [] HTTPD_STRING_ATTR = "/index.shtml";
|
||||||
|
#endif
|
||||||
|
const char httpd_404fn [] HTTPD_STRING_ATTR = "/404.html";
|
||||||
|
const char httpd_404notf [] HTTPD_STRING_ATTR = "404 Not found";
|
||||||
|
const char httpd_200ok [] HTTPD_STRING_ATTR = "200 OK";
|
||||||
|
static
|
||||||
|
PT_THREAD(handle_output(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
char *ptr;
|
||||||
|
|
||||||
|
PT_BEGIN(&s->outputpt);
|
||||||
|
#if DEBUGLOGIC
|
||||||
|
httpd_strcpy(s->filename,httpd_indexfn);
|
||||||
|
#endif
|
||||||
|
if(!httpd_fs_open(s->filename, &s->file)) {
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
/* If index.html not found try index.shtml */
|
||||||
|
if (httpd_strcmp(s->filename,httpd_indexfn)==0) httpd_strcpy(s->filename,httpd_indexsfn);
|
||||||
|
if (httpd_fs_open(s->filename, &s->file)) goto sendfile;
|
||||||
|
#endif
|
||||||
|
httpd_strcpy(s->filename, httpd_404fn);
|
||||||
|
httpd_fs_open(s->filename, &s->file);
|
||||||
|
PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_404notf));
|
||||||
|
PT_WAIT_THREAD(&s->outputpt, send_file(s));
|
||||||
|
} else {
|
||||||
|
sendfile:
|
||||||
|
PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_200ok));
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
ptr = strchr(s->filename, ISO_period);
|
||||||
|
if((ptr != NULL && httpd_strncmp(ptr, httpd_shtml, 6) == 0) || httpd_strcmp(s->filename,httpd_indexfn)==0) {
|
||||||
|
PT_INIT(&s->scriptpt);
|
||||||
|
PT_WAIT_THREAD(&s->outputpt, handle_script(s));
|
||||||
|
} else {
|
||||||
|
#else
|
||||||
|
if (1)
|
||||||
|
#endif
|
||||||
|
PT_WAIT_THREAD(&s->outputpt, send_file(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PSOCK_CLOSE(&s->sout);
|
||||||
|
PT_END(&s->outputpt);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
#if WEBSERVER_CONF_PASSQUERY
|
||||||
|
char httpd_query[WEBSERVER_CONF_PASSQUERY];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const char httpd_get[] HTTPD_STRING_ATTR = "GET ";
|
||||||
|
const char httpd_ref[] HTTPD_STRING_ATTR = "Referer:";
|
||||||
|
static
|
||||||
|
PT_THREAD(handle_input(struct httpd_state *s))
|
||||||
|
{
|
||||||
|
|
||||||
|
PSOCK_BEGIN(&s->sin);
|
||||||
|
|
||||||
|
PSOCK_READTO(&s->sin, ISO_space);
|
||||||
|
|
||||||
|
if(httpd_strncmp(s->inputbuf, httpd_get, 4) != 0) {
|
||||||
|
PSOCK_CLOSE_EXIT(&s->sin);
|
||||||
|
}
|
||||||
|
PSOCK_READTO(&s->sin, ISO_space);
|
||||||
|
|
||||||
|
if(s->inputbuf[0] != ISO_slash) {
|
||||||
|
PSOCK_CLOSE_EXIT(&s->sin);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(s->inputbuf[1] == ISO_space) {
|
||||||
|
httpd_strcpy(s->filename, httpd_indexfn);
|
||||||
|
} else {
|
||||||
|
uint8_t i;
|
||||||
|
for (i=0;i<sizeof(s->filename)+1;i++) {
|
||||||
|
if (i >= (PSOCK_DATALEN(&s->sin)-1)) break;
|
||||||
|
if (s->inputbuf[i]==ISO_space) break;
|
||||||
|
#if WEBSERVER_CONF_PASSQUERY
|
||||||
|
/* Query string is left in the httpd_query buffer until zeroed by the application! */
|
||||||
|
if (s->inputbuf[i]==ISO_qmark) {
|
||||||
|
strncpy(httpd_query,&s->inputbuf[i+1],sizeof(httpd_query));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
s->filename[i]=s->inputbuf[i];
|
||||||
|
}
|
||||||
|
s->filename[i]=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if WEBSERVER_CONF_LOG
|
||||||
|
webserver_log_file(&uip_conn->ripaddr, s->filename);
|
||||||
|
// webserver_log(httpd_query);
|
||||||
|
#endif
|
||||||
|
s->state = STATE_OUTPUT;
|
||||||
|
while(1) {
|
||||||
|
PSOCK_READTO(&s->sin, ISO_nl);
|
||||||
|
#if WEBSERVER_CONF_LOG && WEBSERVER_CONF_REFERER
|
||||||
|
if(httpd_strncmp(s->inputbuf, httpd_ref, 8) == 0) {
|
||||||
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
|
||||||
|
petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
|
||||||
|
webserver_log(s->inputbuf);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
PSOCK_END(&s->sin);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static void
|
||||||
|
handle_connection(struct httpd_state *s)
|
||||||
|
{
|
||||||
|
#if DEBUGLOGIC
|
||||||
|
handle_output(s);
|
||||||
|
#endif
|
||||||
|
handle_input(s);
|
||||||
|
if(s->state == STATE_OUTPUT) {
|
||||||
|
handle_output(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
httpd_appcall(void *state)
|
||||||
|
{
|
||||||
|
#if DEBUGLOGIC
|
||||||
|
struct httpd_state *s; //Enter here for debugging with output directed to TCPBUF
|
||||||
|
s = sg = (struct httpd_state *)memb_alloc(&conns);
|
||||||
|
if (1) {
|
||||||
|
#else
|
||||||
|
struct httpd_state *s = (struct httpd_state *)state;
|
||||||
|
if(uip_closed() || uip_aborted() || uip_timedout()) {
|
||||||
|
if(s != NULL) {
|
||||||
|
memb_free(&conns, s);
|
||||||
|
}
|
||||||
|
} else if(uip_connected()) {
|
||||||
|
s = (struct httpd_state *)memb_alloc(&conns);
|
||||||
|
if(s == NULL) {
|
||||||
|
uip_abort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
tcp_markconn(uip_conn, s);
|
||||||
|
PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
|
||||||
|
PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
|
||||||
|
PT_INIT(&s->outputpt);
|
||||||
|
s->state = STATE_WAITING;
|
||||||
|
s->timer = 0;
|
||||||
|
handle_connection(s);
|
||||||
|
} else if(s != NULL) {
|
||||||
|
if(uip_poll()) {
|
||||||
|
++s->timer;
|
||||||
|
if(s->timer >= 20) {
|
||||||
|
uip_abort();
|
||||||
|
memb_free(&conns, s);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
s->timer = 0;
|
||||||
|
}
|
||||||
|
handle_connection(s);
|
||||||
|
} else {
|
||||||
|
uip_abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
httpd_init(void)
|
||||||
|
{
|
||||||
|
tcp_listen(UIP_HTONS(80));
|
||||||
|
memb_init(&conns);
|
||||||
|
PRINTD(" sizof(struct httpd_state) = %d\n",sizeof(struct httpd_state));
|
||||||
|
PRINTA(" %d bytes used for httpd state storage\n",conns.size * conns.num);
|
||||||
|
#if WEBSERVER_CONF_CGI
|
||||||
|
httpd_cgi_init();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
250
apps/webserver-nano/httpd.h
Normal file
|
@ -0,0 +1,250 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2001-2005, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 uIP TCP/IP stack.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __HTTPD_H__
|
||||||
|
#define __HTTPD_H__
|
||||||
|
/*
|
||||||
|
* The default number of simultaneous server connections is 4. Multiple connections can be triggered
|
||||||
|
* by requests for embedded images, style sheets, icons, etc. and a TCP RESET is issued when no more
|
||||||
|
* connections are available.
|
||||||
|
* The Firefox default connections per server is 15. It can be reduced to 1 in about:config and generally
|
||||||
|
* will improve throughput in the half-duplex 6LoWPAN link as embedded files will be requested sequentially.
|
||||||
|
* Tictactoe is a good test for multiple connections; it can use as many as 9, but should also work when
|
||||||
|
* Firefox network.http.max-connections-per-server is set to a lower number.
|
||||||
|
* The RAM needed for each entry depends on script enabling and buffer sizes; see struct httpd_state below.
|
||||||
|
* Typical range is 100 - 200 bytes per connection
|
||||||
|
* cgi's that use PSOCK_GENERATOR_SEND will have truncated output if UIP_CONF_RECEIVE_WINDOW and UIP_CONF_TCP_MSS
|
||||||
|
* are not large enough. The header-menu cgi needs ~340 bytes if all options are enabled, while the file-stats * cgi
|
||||||
|
* can exceed any MSS if there are enough files to display (e.g. tic-tac-toe).
|
||||||
|
* The advertised MSS is easily seen in wireshark.
|
||||||
|
* Some example set a small MSS by default. rpl-border-router for example uses a receive window of 60.
|
||||||
|
*/
|
||||||
|
#ifndef WEBSERVER_CONF_NANO
|
||||||
|
#if CONTIKI_TARGET_SKY || CONTIKI_TARGET_STK500
|
||||||
|
#define WEBSERVER_CONF_NANO 1
|
||||||
|
#else
|
||||||
|
#define WEBSERVER_CONF_NANO 3
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBSERVER_CONF_NANO==1
|
||||||
|
/* nano-size for constrained MCUs */
|
||||||
|
#define WEBSERVER_CONF_CONNS 2
|
||||||
|
#define WEBSERVER_CONF_NAMESIZE 16
|
||||||
|
#define WEBSERVER_CONF_BUFSIZE 40
|
||||||
|
/* Allow include in .shtml pages, e.g. %!: /header.html */
|
||||||
|
#define WEBSERVER_CONF_INCLUDE 1
|
||||||
|
/* Allow cgi in .shtml pages, e.g. %! file-stats . */
|
||||||
|
#define WEBSERVER_CONF_CGI 1
|
||||||
|
/* MAX_SCRIPT_NAME_LENGTH should be at least the maximum file name length+2 for %!: includes */
|
||||||
|
#define MAX_SCRIPT_NAME_LENGTH WEBSERVER_CONF_NAMESIZE+2
|
||||||
|
/* Enable specific cgi's */
|
||||||
|
#define WEBSERVER_CONF_HEADER 1
|
||||||
|
//#define WEBSERVER_CONF_HEADER_W3C 1 //Proper header
|
||||||
|
#define WEBSERVER_CONF_HEADER_MENU 1 //with links to other pages
|
||||||
|
//#define WEBSERVER_CONF_HEADER_ICON 1 //with favicon
|
||||||
|
#define WEBSERVER_CONF_FILESTATS 1
|
||||||
|
#define WEBSERVER_CONF_TCPSTATS 0
|
||||||
|
#define WEBSERVER_CONF_PROCESSES 0
|
||||||
|
#define WEBSERVER_CONF_ADDRESSES 1
|
||||||
|
#define WEBSERVER_CONF_NEIGHBORS 1
|
||||||
|
#define WEBSERVER_CONF_ROUTES 1
|
||||||
|
#define WEBSERVER_CONF_SENSORS 0
|
||||||
|
#define WEBSERVER_CONF_TICTACTOE 0 //Needs passquery of at least 10 chars
|
||||||
|
#define WEBSERVER_CONF_AJAX 0
|
||||||
|
//#define WEBSERVER_CONF_PASSQUERY 10
|
||||||
|
#if WEBSERVER_CONF_PASSQUERY
|
||||||
|
extern char httpd_query[WEBSERVER_CONF_PASSQUERY];
|
||||||
|
#endif
|
||||||
|
/* Enable specific file types */
|
||||||
|
#define WEBSERVER_CONF_JPG 0
|
||||||
|
#define WEBSERVER_CONF_PNG 0
|
||||||
|
#define WEBSERVER_CONF_GIF 0
|
||||||
|
#define WEBSERVER_CONF_TXT 1
|
||||||
|
#define WEBSERVER_CONF_CSS 0
|
||||||
|
#define WEBSERVER_CONF_BIN 0
|
||||||
|
|
||||||
|
/* Log page accesses */
|
||||||
|
#define WEBSERVER_CONF_LOG 0
|
||||||
|
/* Include referrer in log */
|
||||||
|
#define WEBSERVER_CONF_REFERER 0
|
||||||
|
|
||||||
|
#elif WEBSERVER_CONF_NANO==2
|
||||||
|
/* webserver-mini having more content */
|
||||||
|
#error webserver-micro not implemented
|
||||||
|
|
||||||
|
#elif WEBSERVER_CONF_NANO==3
|
||||||
|
/* webserver-mini having all content */
|
||||||
|
#define WEBSERVER_CONF_CONNS 6
|
||||||
|
#define WEBSERVER_CONF_NAMESIZE 20
|
||||||
|
#define WEBSERVER_CONF_BUFSIZE 40
|
||||||
|
/* Allow include in .shtml pages, e.g. %!: /header.html */
|
||||||
|
#define WEBSERVER_CONF_INCLUDE 1
|
||||||
|
/* Allow cgi in .shtml pages, e.g. %! file-stats . */
|
||||||
|
#define WEBSERVER_CONF_CGI 1
|
||||||
|
/* MAX_SCRIPT_NAME_LENGTH should be at least the maximum file name length+2 for %!: includes */
|
||||||
|
#define MAX_SCRIPT_NAME_LENGTH WEBSERVER_CONF_NAMESIZE+2
|
||||||
|
/* Enable specific cgi's */
|
||||||
|
#define WEBSERVER_CONF_HEADER 1
|
||||||
|
//#define WEBSERVER_CONF_HEADER_W3C 1 //Proper header
|
||||||
|
#define WEBSERVER_CONF_HEADER_MENU 1 //with links to other pages
|
||||||
|
//#define WEBSERVER_CONF_HEADER_ICON 1 //with favicon
|
||||||
|
#define WEBSERVER_CONF_FILESTATS 1
|
||||||
|
#define WEBSERVER_CONF_TCPSTATS 1
|
||||||
|
#define WEBSERVER_CONF_PROCESSES 1
|
||||||
|
#define WEBSERVER_CONF_ADDRESSES 1
|
||||||
|
#define WEBSERVER_CONF_NEIGHBORS 1
|
||||||
|
#define WEBSERVER_CONF_ROUTES 1
|
||||||
|
#define WEBSERVER_CONF_SENSORS 1
|
||||||
|
#define WEBSERVER_CONF_TICTACTOE 1 //Needs passquery of at least 10 chars
|
||||||
|
#define WEBSERVER_CONF_AJAX 1
|
||||||
|
#define WEBSERVER_CONF_PASSQUERY 10
|
||||||
|
#if WEBSERVER_CONF_PASSQUERY
|
||||||
|
extern char httpd_query[WEBSERVER_CONF_PASSQUERY];
|
||||||
|
#endif
|
||||||
|
/* Enable specific file types */
|
||||||
|
#define WEBSERVER_CONF_JPG 1
|
||||||
|
#define WEBSERVER_CONF_PNG 1
|
||||||
|
#define WEBSERVER_CONF_GIF 1
|
||||||
|
#define WEBSERVER_CONF_TXT 1
|
||||||
|
#define WEBSERVER_CONF_CSS 1
|
||||||
|
#define WEBSERVER_CONF_BIN 1
|
||||||
|
|
||||||
|
/* Log page accesses */
|
||||||
|
#define WEBSERVER_CONF_LOG 1
|
||||||
|
/* Include referrer in log */
|
||||||
|
#define WEBSERVER_CONF_REFERER 1
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error Specified WEBSERVER_CONF_NANO configuration not supported.
|
||||||
|
#endif /* WEBSERVER_CONF_NANO */
|
||||||
|
|
||||||
|
/* Address printing used by cgi's and logging, but it can be turned off if desired */
|
||||||
|
#if WEBSERVER_CONF_LOG || WEBSERVER_CONF_ADDRESSES || WEBSERVER_CONF_NEIGHBORS || WEBSERVER_CONF_ROUTES
|
||||||
|
extern uip_ds6_netif_t uip_ds6_if;
|
||||||
|
#define WEBSERVER_CONF_PRINTADDR 1
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_PRINTADDR
|
||||||
|
uint8_t httpd_cgi_sprint_ip6(uip_ip6addr_t addr, char * result);
|
||||||
|
#else
|
||||||
|
#define httpd_cgi_sprint_ip6(...) 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
#include "httpd-fs.h"
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
/* When using non-ram storage httpd-fsdata.c must be generated with the HTTPD_STRING_ATTR, eg
|
||||||
|
* ../../tools/makefsdata -A HTTPD_STRING_ATTR
|
||||||
|
*/
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#define HTTPD_STRING_ATTR PROGMEM
|
||||||
|
#define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
|
||||||
|
/* These will fail if the server strings are above 64K in program flash */
|
||||||
|
#define httpd_memcpy memcpy_P
|
||||||
|
#define httpd_strcpy strcpy_P
|
||||||
|
#define httpd_strcmp strcmp_P
|
||||||
|
#define httpd_strncmp strncmp_P
|
||||||
|
#define httpd_strlen strlen_P
|
||||||
|
#define httpd_snprintf snprintf_P
|
||||||
|
#define httpd_fs_cpy memcpy_P
|
||||||
|
#define httpd_fs_strcmp strcmp_P
|
||||||
|
#define httpd_fs_strchr strchr_P
|
||||||
|
#define httpd_fs_getchar(x) pgm_read_byte(x)
|
||||||
|
|
||||||
|
#else /* All storage in RAM */
|
||||||
|
#define HTTPD_STRING_ATTR
|
||||||
|
#define PRINTA(FORMAT,args...) printf(FORMAT,##args)
|
||||||
|
#define httpd_snprintf snprintf
|
||||||
|
#define httpd_fs_cpy memcpy
|
||||||
|
#define httpd_memcpy memcpy
|
||||||
|
#define httpd_strcpy strcpy
|
||||||
|
#define httpd_strcmp strcmp
|
||||||
|
#define httpd_strncmp strncmp
|
||||||
|
#define httpd_strlen strlen
|
||||||
|
#define httpd_fs_strchr strchr
|
||||||
|
#define httpd_fs_strcmp strcmp
|
||||||
|
#define httpd_fs_getchar(c) *(c)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct httpd_state {
|
||||||
|
unsigned char timer;
|
||||||
|
struct psock sin, sout;
|
||||||
|
struct pt outputpt;
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
struct pt scriptpt;
|
||||||
|
#endif
|
||||||
|
char inputbuf[WEBSERVER_CONF_BUFSIZE];
|
||||||
|
char filename[WEBSERVER_CONF_NAMESIZE];
|
||||||
|
char state;
|
||||||
|
struct httpd_fs_file file;
|
||||||
|
int len;
|
||||||
|
#if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
|
||||||
|
char *scriptptr;
|
||||||
|
int scriptlen;
|
||||||
|
#endif
|
||||||
|
#if WEBSERVER_CONF_CGI
|
||||||
|
union {
|
||||||
|
unsigned short count;
|
||||||
|
void *ptr;
|
||||||
|
} u;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
void httpd_init(void);
|
||||||
|
void httpd_appcall(void *state);
|
||||||
|
|
||||||
|
/* DEBUGLOGIC is a convenient way to debug without a tcp/ip connection.
|
||||||
|
* After initialization, break the program and set the program counter
|
||||||
|
* to the beginning of httpd.c::httpd_appcall:
|
||||||
|
* s = sg = (struct httpd_state *)memb_alloc(&conns);
|
||||||
|
* The global sg points to the s data to get around "not in scope" optimization.
|
||||||
|
* The input file is forced to /index.html and the output directed to TCPBUF.
|
||||||
|
* However the socket will hang on acknowledgement when any data is sent.
|
||||||
|
* To prevent this just add a break to /core/net/psock.c::psock_generator_send
|
||||||
|
* ...
|
||||||
|
* // Wait until all data is sent and acknowledged.
|
||||||
|
* break; //<---add this line
|
||||||
|
* PT_YIELD_UNTIL(&s->psockpt, uip_acked() || uip_rexmit());
|
||||||
|
* ...
|
||||||
|
*/
|
||||||
|
#define DEBUGLOGIC 0
|
||||||
|
#if DEBUGLOGIC
|
||||||
|
struct httpd_state *sg;
|
||||||
|
#define uip_mss(...) 512
|
||||||
|
#define uip_appdata TCPBUF
|
||||||
|
char TCPBUF[512];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* __HTTPD_H__ */
|
149
apps/webserver-nano/urlconv.c
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010, Kajtar Zsolt <soci@c64.rulez.org>.
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Author: Kajtar Zsolt <soci@c64.rulez.org>
|
||||||
|
*
|
||||||
|
* $Id: urlconv.c,v 1.4 2010/09/29 09:35:56 oliverschmidt Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "cfs/cfs.h"
|
||||||
|
#include "http-strings.h"
|
||||||
|
|
||||||
|
#define ISO_number 0x23
|
||||||
|
#define ISO_percent 0x25
|
||||||
|
#define ISO_period 0x2e
|
||||||
|
#define ISO_slash 0x2f
|
||||||
|
#define ISO_question 0x3f
|
||||||
|
|
||||||
|
static char wwwroot[40];
|
||||||
|
static unsigned char wwwrootlen;
|
||||||
|
|
||||||
|
void
|
||||||
|
urlconv_init(void)
|
||||||
|
{
|
||||||
|
int fd = cfs_open("wwwroot.cfg", CFS_READ);
|
||||||
|
int rd = cfs_read(fd, wwwroot, sizeof(wwwroot));
|
||||||
|
cfs_close(fd);
|
||||||
|
if(rd != -1) wwwrootlen = rd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* URL to filename conversion
|
||||||
|
*
|
||||||
|
* prepends wwwroot prefix
|
||||||
|
* normalizes path by removing "/./"
|
||||||
|
* interprets "/../" and calculates path accordingly
|
||||||
|
* resulting path is always absolute
|
||||||
|
* replaces "%AB" notation with characters
|
||||||
|
* strips "#fragment" and "?query" from end
|
||||||
|
* replaces multiple slashes with a single one
|
||||||
|
* rejects non-ASCII characters
|
||||||
|
*
|
||||||
|
* MAXLEN is including trailing zero!
|
||||||
|
* input and output is ASCII
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
urlconv_tofilename(char *dest, char *source, unsigned char maxlen)
|
||||||
|
{
|
||||||
|
static unsigned char len;
|
||||||
|
static unsigned char c, hex1;
|
||||||
|
static unsigned char *from, *to;
|
||||||
|
|
||||||
|
*dest = ISO_slash;
|
||||||
|
strncpy(dest + 1, wwwroot, wwwrootlen);
|
||||||
|
len = 0;
|
||||||
|
from = source; to = dest + wwwrootlen;
|
||||||
|
maxlen -= 2 + wwwrootlen;
|
||||||
|
do {
|
||||||
|
c = *(from++);
|
||||||
|
switch(c) {
|
||||||
|
case ISO_number:
|
||||||
|
case ISO_question:
|
||||||
|
c = 0;
|
||||||
|
break;
|
||||||
|
case ISO_percent:
|
||||||
|
c = 0;
|
||||||
|
hex1 = (*(from++) | 0x20) ^ 0x30; // ascii only!
|
||||||
|
if(hex1 > 0x50 && hex1 < 0x57)
|
||||||
|
hex1 -= 0x47;
|
||||||
|
else
|
||||||
|
if(hex1 > 9)
|
||||||
|
break; // invalid hex
|
||||||
|
c = (*(from++) | 0x20) ^ 0x30; // ascii only!
|
||||||
|
if(c > 0x50 && c < 0x57)
|
||||||
|
c -= 0x47;
|
||||||
|
else
|
||||||
|
if(c > 9)
|
||||||
|
break; // invalid hex
|
||||||
|
c |= hex1 << 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(c < 0x20 || c > 0x7e)
|
||||||
|
c = 0; // non ascii?!
|
||||||
|
if(len >= maxlen)
|
||||||
|
c = 0; // too long?
|
||||||
|
|
||||||
|
if(c == ISO_slash || !c) {
|
||||||
|
switch(*to) {
|
||||||
|
case ISO_slash:
|
||||||
|
continue; // no repeated slash
|
||||||
|
case ISO_period:
|
||||||
|
switch(to[-1]) {
|
||||||
|
case ISO_slash: // handle "./"
|
||||||
|
--to; --len;
|
||||||
|
continue;
|
||||||
|
case ISO_period:
|
||||||
|
if(to[-2] == ISO_slash) {
|
||||||
|
to -= 2; len -= 2;
|
||||||
|
if(len) {
|
||||||
|
do {
|
||||||
|
--to; --len;
|
||||||
|
} while(*to != ISO_slash);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(c) {
|
||||||
|
++to; ++len;
|
||||||
|
*to = c;
|
||||||
|
}
|
||||||
|
} while(c);
|
||||||
|
if(*to == ISO_slash && (len + sizeof(http_index_htm) - 3) < maxlen) {
|
||||||
|
strcpy(to, http_index_htm); // add index.htm
|
||||||
|
} else {
|
||||||
|
++to;
|
||||||
|
*to = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
40
apps/webserver-nano/urlconv.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010, Kajtar Zsolt <soci@c64.rulez.org>.
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* $Id: urlconv.h,v 1.3 2010/08/30 19:44:38 oliverschmidt Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __URLCONV_H__
|
||||||
|
#define __URLCONV_H__
|
||||||
|
|
||||||
|
void urlconv_init(void);
|
||||||
|
void urlconv_tofilename(char *dest, char *source, unsigned char maxlen);
|
||||||
|
|
||||||
|
#endif /* __URLCONV_H__ */
|
72
apps/webserver-nano/webserver-dsc.c
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2003, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 desktop environment
|
||||||
|
*
|
||||||
|
* $Id: webserver-dsc.c,v 1.3 2007/09/06 01:36:12 matsutsuka Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "sys/dsc.h"
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
#if CTK_CONF_ICON_BITMAPS
|
||||||
|
static unsigned char webservericon_bitmap[3*3*8] = {
|
||||||
|
0x00, 0x7f, 0x40, 0x41, 0x44, 0x48, 0x40, 0x50,
|
||||||
|
0x00, 0xff, 0x5a, 0x00, 0x00, 0x00, 0x3c, 0x81,
|
||||||
|
0x00, 0xfe, 0x02, 0x82, 0x22, 0x12, 0x02, 0x0a,
|
||||||
|
|
||||||
|
0x41, 0x60, 0x42, 0x62, 0x62, 0x42, 0x60, 0x41,
|
||||||
|
0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18,
|
||||||
|
0x82, 0x06, 0x42, 0x46, 0x46, 0x42, 0x06, 0x82,
|
||||||
|
|
||||||
|
0x50, 0x40, 0x48, 0x44, 0x41, 0x40, 0x7e, 0x00,
|
||||||
|
0xc5, 0x34, 0x3c, 0x52, 0x7a, 0x7e, 0xa1, 0xfd,
|
||||||
|
0x0a, 0x02, 0x12, 0x22, 0x82, 0x02, 0x7e, 0x00
|
||||||
|
};
|
||||||
|
#endif /* CTK_CONF_ICON_BITMAPS */
|
||||||
|
|
||||||
|
#if CTK_CONF_ICON_TEXTMAPS
|
||||||
|
static char webservericon_textmap[9] = {
|
||||||
|
'+', '-', '+',
|
||||||
|
'|', ')', '|',
|
||||||
|
'+', '-', '+'
|
||||||
|
};
|
||||||
|
#endif /* CTK_CONF_ICON_TEXTMAPS */
|
||||||
|
|
||||||
|
#if CTK_CONF_ICONS
|
||||||
|
static struct ctk_icon webserver_icon =
|
||||||
|
{CTK_ICON("Web server", webservericon_bitmap, webservericon_textmap)};
|
||||||
|
#endif /* CTK_CONF_ICONS */
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
DSC(webserver_dsc,
|
||||||
|
"The Contiki web server",
|
||||||
|
"webserver.prg",
|
||||||
|
webserver_process,
|
||||||
|
&webserver_icon);
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
42
apps/webserver-nano/webserver-dsc.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2003, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 desktop environment
|
||||||
|
*
|
||||||
|
* $Id: webserver-dsc.h,v 1.1 2006/06/17 22:41:14 adamdunkels Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef __WEBSERVER_DSC_H__
|
||||||
|
#define __WEBSERVER_DSC_H__
|
||||||
|
|
||||||
|
#include "sys/dsc.h"
|
||||||
|
|
||||||
|
DSC_HEADER(webserver_dsc);
|
||||||
|
|
||||||
|
#endif /* __WEBSERVER_DSC_H__ */
|
92
apps/webserver-nano/webserver-nogui.c
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2002, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 OS.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
|
||||||
|
#include "webserver-nogui.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
|
||||||
|
PROCESS(webserver_nogui_process, "Web server");
|
||||||
|
|
||||||
|
AUTOSTART_PROCESSES(&webserver_nogui_process);
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
PROCESS_THREAD(webserver_nogui_process, ev, data)
|
||||||
|
{
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
|
||||||
|
httpd_init();
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
|
||||||
|
httpd_appcall(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
#if WEBSERVER_CONF_LOG
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
webserver_log_file(uip_ipaddr_t *requester, char *file)
|
||||||
|
{
|
||||||
|
/* Print out IP address of requesting host. */
|
||||||
|
|
||||||
|
#if UIP_CONF_IPV6
|
||||||
|
#if WEBSERVER_CONF_ADDRESSES
|
||||||
|
char buf[48];
|
||||||
|
uint8_t j;
|
||||||
|
j=httpd_cgi_sprint_ip6((uip_ip6addr_t)*requester, buf);
|
||||||
|
buf[j]=':';buf[j+1]=' ';buf[j+2]=0;
|
||||||
|
#else
|
||||||
|
char buf[2];
|
||||||
|
buf[0]=' ';buf[1]=0;
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
char buf[20];
|
||||||
|
sprintf(buf, "%d.%d.%d.%d: ", requester->u8[0], requester->u8[1],
|
||||||
|
requester->u8[2], requester->u8[3]);
|
||||||
|
#endif /* UIP_CONF_IPV6 */
|
||||||
|
//log_message(buf, file);
|
||||||
|
printf("%s%s\n", buf, file);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
webserver_log(char *msg)
|
||||||
|
{
|
||||||
|
//log_message(msg, "");
|
||||||
|
printf("%s\n", msg);
|
||||||
|
}
|
||||||
|
#endif /* WEBSERVER_CONF_LOG */
|
45
apps/webserver-nano/webserver-nogui.h
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2002, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 OS
|
||||||
|
*
|
||||||
|
* $Id: webserver-nogui.h,v 1.4 2008/10/14 11:07:57 adamdunkels Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef __WEBSERVER_NOGUI_H__
|
||||||
|
#define __WEBSERVER_NOGUI_H__
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
PROCESS_NAME(webserver_nogui_process);
|
||||||
|
|
||||||
|
void webserver_log(char *msg);
|
||||||
|
void webserver_log_file(uip_ipaddr_t *requester, char *file);
|
||||||
|
|
||||||
|
#endif /* __WEBSERVER_H__ */
|
127
apps/webserver-nano/webserver.c
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2002, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 desktop environment for the C64.
|
||||||
|
*
|
||||||
|
* $Id: webserver.c,v 1.5 2008/02/08 22:53:32 oliverschmidt Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "ctk/ctk.h"
|
||||||
|
|
||||||
|
#include "http-strings.h"
|
||||||
|
#include "webserver.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
|
||||||
|
/* The main window. */
|
||||||
|
static struct ctk_window mainwindow;
|
||||||
|
|
||||||
|
static struct ctk_label message =
|
||||||
|
{CTK_LABEL(0, 0, 15, 1, "Latest requests")};
|
||||||
|
|
||||||
|
PROCESS(webserver_process, "Web server");
|
||||||
|
|
||||||
|
AUTOSTART_PROCESSES(&webserver_process);
|
||||||
|
|
||||||
|
#define LOG_WIDTH 38
|
||||||
|
#define LOG_HEIGHT 16
|
||||||
|
static char log[LOG_WIDTH*LOG_HEIGHT];
|
||||||
|
|
||||||
|
static struct ctk_label loglabel =
|
||||||
|
{CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)};
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
PROCESS_THREAD(webserver_process, ev, data)
|
||||||
|
{
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
|
||||||
|
ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server");
|
||||||
|
|
||||||
|
CTK_WIDGET_ADD(&mainwindow, &message);
|
||||||
|
CTK_WIDGET_ADD(&mainwindow, &loglabel);
|
||||||
|
|
||||||
|
httpd_init();
|
||||||
|
|
||||||
|
ctk_window_open(&mainwindow);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PROCESS_WAIT_EVENT();
|
||||||
|
|
||||||
|
if(ev == ctk_signal_window_close ||
|
||||||
|
ev == PROCESS_EVENT_EXIT) {
|
||||||
|
ctk_window_close(&mainwindow);
|
||||||
|
process_exit(&webserver_process);
|
||||||
|
LOADER_UNLOAD();
|
||||||
|
} else if(ev == tcpip_event) {
|
||||||
|
httpd_appcall(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
webserver_log_file(uip_ipaddr_t *requester, char *file)
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
|
||||||
|
/* Scroll previous entries upwards */
|
||||||
|
memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
|
||||||
|
|
||||||
|
/* Print out IP address of requesting host. */
|
||||||
|
size = sprintf(&log[LOG_WIDTH * (LOG_HEIGHT - 1)],
|
||||||
|
"%d.%d.%d.%d: ",
|
||||||
|
requester->u8[0],
|
||||||
|
requester->u8[1],
|
||||||
|
requester->u8[2],
|
||||||
|
requester->u8[3]);
|
||||||
|
|
||||||
|
/* Copy filename into last line. */
|
||||||
|
strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
|
||||||
|
|
||||||
|
/* Update log display. */
|
||||||
|
CTK_WIDGET_REDRAW(&loglabel);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
webserver_log(char *msg)
|
||||||
|
{
|
||||||
|
/* Scroll previous entries upwards */
|
||||||
|
memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
|
||||||
|
|
||||||
|
/* Copy filename into last line. */
|
||||||
|
strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1)], msg, LOG_WIDTH);
|
||||||
|
|
||||||
|
/* Update log display. */
|
||||||
|
CTK_WIDGET_REDRAW(&loglabel);
|
||||||
|
}
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
45
apps/webserver-nano/webserver.h
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2002, Adam Dunkels.
|
||||||
|
* 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. The name of the author may not be used to endorse or promote
|
||||||
|
* products derived from this software without specific prior
|
||||||
|
* written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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
|
||||||
|
*
|
||||||
|
* $Id: webserver.h,v 1.2 2007/04/23 21:19:55 oliverschmidt Exp $
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef __WEBSERVER_H__
|
||||||
|
#define __WEBSERVER_H__
|
||||||
|
|
||||||
|
#include "contiki-net.h"
|
||||||
|
|
||||||
|
PROCESS_NAME(webserver_process);
|
||||||
|
|
||||||
|
void webserver_log(char *msg);
|
||||||
|
void webserver_log_file(uip_ipaddr_t *requester, char *file);
|
||||||
|
|
||||||
|
#endif /* __WEBSERVER_H__ */
|