diff --git a/Makefile.contiki b/Makefile.contiki index 6450ba6a..61646064 100644 --- a/Makefile.contiki +++ b/Makefile.contiki @@ -1,3 +1,22 @@ +######################################################################## +# platform-specific options + +ifeq ($(TARGET), econotag) +CFLAGS += -DUIP_CONF_TCP=0 +endif + +ifeq ($(TARGET), mbxxx) +CFLAGS += -DUIP_CONF_TCP=0 +endif + +ifeq ($(TARGET), minimal-net) +UIP_CONF_IPV6_RPL=0 +CFLAGS += -DUIP_CONF_IPV6_RPL=0 -DRPL_BORDER_ROUTER=0 +endif + +# usually, you should not need changing anything beyond this line +######################################################################## + all: coap-server #coap-client $(MAKE) $(MAKEFLAGS) ROLE=server coap-server # $(MAKE) $(MAKEFLAGS) clean @@ -9,27 +28,17 @@ CONTIKI=../.. WITH_UIP6=1 UIP_CONF_IPV6=1 -#CFLAGS += -DUIP_CONF_IPV6_RPL=1 - -ifeq ($(TARGET), minimal-net) -CFLAGS += -DHAVE_ASSERT_H -DWITH_UIP6=1 -ifneq ($(ROLE),client) - UIP_LLADDR="{0x00,0x06,0x98,0x00,0x02,0x32}" - CFLAGS+= -DUIP_CONF_LLADDR=$(UIP_LLADDR) -else - UIP_LLADDR="{0x00,0x06,0x98,0x00,0x02,0x30}" - CFLAGS+= -DUDP_CONNECTION_ADDR="fe80::206:98ff:fe00:232" \ - -DUIP_CONF_LLADDR=$(UIP_LLADDR) -endif -endif - -ifeq ($(TARGET), mbxxx) -CFLAGS += -DUIP_CONF_TCP=0 -endif - -ifeq ($(TARGET), redbee-econotag) -CFLAGS += -DUIP_CONF_TCP=0 -endif +# ifeq ($(TARGET), minimal-net) +# CFLAGS += -DHAVE_ASSERT_H -DWITH_UIP6=1 +# ifneq ($(ROLE),client) +# UIP_LLADDR="{0x00,0x06,0x98,0x00,0x02,0x32}" +# CFLAGS+= -DUIP_CONF_LLADDR=$(UIP_LLADDR) +# else +# UIP_LLADDR="{0x00,0x06,0x98,0x00,0x02,0x30}" +# CFLAGS+= -DUDP_CONNECTION_ADDR="fe80::206:98ff:fe00:232" \ +# -DUIP_CONF_LLADDR=$(UIP_LLADDR) +# endif +# endif CFLAGS += -ffunction-sections LDFLAGS += -Wl,--gc-sections,--undefined=_reset_vector__,--undefined=InterruptVectors,--undefined=_copy_data_init__,--undefined=_clear_bss_init__,--undefined=_end_of_init__ diff --git a/Makefile.libcoap b/Makefile.libcoap index b2df60da..6e654e20 100644 --- a/Makefile.libcoap +++ b/Makefile.libcoap @@ -1 +1,2 @@ -libcoap_src = pdu.c net.c debug.c encode.c uri.c coap_list.c subscribe.c resource.c hashkey.c str.c option.c async.c block.c mem.c +libcoap_src = pdu.c net.c debug.c encode.c uri.c coap_list.c subscribe.c resource.c hashkey.c str.c option.c async.c block.c mem.c coap_io.c +#libcoap_src = app.c pdu.c net.c debug.c encode.c uri.c coap_list.c subscribe.c resource.c hashkey.c str.c option.c async.c block.c mem.c diff --git a/coap-server.c b/coap-server.c index 5fa6485b..e5f9f328 100644 --- a/coap-server.c +++ b/coap-server.c @@ -93,7 +93,8 @@ init_coap() { #endif void -hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, +hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, + const coap_endpoint_t *local_interface, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { unsigned char buf[2]; @@ -188,7 +189,7 @@ PROCESS_THREAD(coap_server_process, ev, data) PROCESS_YIELD(); if(ev == tcpip_event) { coap_read(coap_context); /* read received data */ - coap_dispatch(coap_context); /* and dispatch PDUs from receivequeue */ + /* coap_dispatch(coap_context); /\* and dispatch PDUs from receivequeue *\/ */ } else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) { time_resource->dirty = 1; etimer_reset(&dirty_timer); diff --git a/coap_io.c b/coap_io.c index 16f70862..410ac3e4 100644 --- a/coap_io.c +++ b/coap_io.c @@ -40,7 +40,7 @@ #ifndef CUSTOM_COAP_NETWORK_ENDPOINT #ifdef WITH_CONTIKI -static ep_initialized = 0; +static int ep_initialized = 0; static inline struct coap_endpoint_t * coap_malloc_contiki_endpoint() { @@ -64,7 +64,7 @@ coap_new_endpoint(const coap_address_t *addr, int flags) { struct coap_endpoint_t *ep = coap_malloc_contiki_endpoint(); if (ep) { - memset(ep, 0, sizeof(struct coap_contiki_endpoint_t)); + memset(ep, 0, sizeof(struct coap_endpoint_t)); ep->handle.conn = udp_new(NULL, 0, NULL); if (!ep->handle.conn) { @@ -72,8 +72,8 @@ coap_new_endpoint(const coap_address_t *addr, int flags) { return NULL; } - memcpy(ep->addr, addr, sizeof(coap_address_t)); - udp_bind(ep->handle.conn, addr->port); + uip_ipaddr_copy(&ep->addr.addr, &addr->addr); + udp_bind((struct uip_udp_conn *)ep->handle.conn, addr->port); } return ep; } @@ -82,7 +82,7 @@ void coap_free_endpoint(coap_endpoint_t *ep) { if (ep) { if (ep->handle.conn) { - uip_udp_remove(ep->handle.conn); + uip_udp_remove((struct uip_udp_conn *)ep->handle.conn); } coap_free_contiki_endpoint(ep); } @@ -182,6 +182,7 @@ coap_free_endpoint(coap_endpoint_t *ep) { #ifndef CUSTOM_COAP_NETWORK_SEND +#ifdef WITH_POSIX /* define struct in6_pktinfo and struct in_pktinfo if not available FIXME: check with configure */ @@ -195,6 +196,7 @@ struct in_pktinfo { struct in_addr ipi_spec_dst; struct in_addr ipi_addr; }; +#endif #ifdef __GNUC__ #define UNUSED_PARAM __attribute__ ((unused)) @@ -208,9 +210,11 @@ coap_network_send(struct coap_context_t *context UNUSED_PARAM, const coap_address_t *dst, unsigned char *data, size_t datalen) { -#ifndef WITH_CONTIKI + struct coap_endpoint_t *ep = (struct coap_endpoint_t *)local_interface; + +#ifndef WITH_CONTIKI /* a buffer large enough to hold all protocol address types */ char buf[CMSG_LEN(sizeof(struct sockaddr_storage))]; struct msghdr mhdr; @@ -281,7 +285,7 @@ coap_network_send(struct coap_context_t *context UNUSED_PARAM, #else /* WITH_CONTIKI */ /* FIXME: untested */ /* FIXME: is there a way to check if send was successful? */ - uip_udp_packet_sendto((uip_udp_conn *)interface, data, datalen, + uip_udp_packet_sendto((struct uip_udp_conn *)ep->handle.conn, data, datalen, &dst->addr, dst->port); return datalen; #endif /* WITH_CONTIKI */ @@ -312,7 +316,15 @@ coap_free_packet(coap_packet_t *packet) { } #endif /* WITH_POSIX */ #ifdef WITH_CONTIKI -/* FIXME: implement coap_malloc_packet and coap_free_packet */ +static inline coap_packet_t * +coap_malloc_packet(void) { + return (coap_packet_t *)coap_malloc_type(COAP_PACKET, 0); +} + +void +coap_free_packet(coap_packet_t *packet) { + coap_free_type(COAP_PACKET, packet); +} #endif /* WITH_CONTIKI */ #ifdef WITH_LWIP /* FIXME: implement coap_malloc_packet and coap_free_packet */ @@ -435,37 +447,37 @@ coap_network_read(coap_endpoint_t *ep, coap_packet_t **packet) { #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN]) if(uip_newdata()) { - uip_ipaddr_copy(&remote->addr, &UIP_IP_BUF->srcipaddr); - remote->port = UIP_UDP_BUF->srcport; - uip_ipaddr_copy(&local->addr, &UIP_IP_BUF->destipaddr); - local->port = UIP_UDP_BUF->destport; + uip_ipaddr_copy(&(*packet)->src.addr, &UIP_IP_BUF->srcipaddr); + (*packet)->src.port = UIP_UDP_BUF->srcport; + uip_ipaddr_copy(&(*packet)->dst.addr, &UIP_IP_BUF->destipaddr); + (*packet)->dst.port = UIP_UDP_BUF->destport; len = uip_datalen(); - - if (len > buflen) { + + if (len > coap_get_max_packetlength(*packet)) { + /* FIXME: we might want to send back a response */ + warn("discarded oversized packet\n"); return -1; } - - memcpy(buf, uip_appdata, len); - PRINTF("Server received %d bytes from [", (int)len); - PRINT6ADDR(remote->addr); - PRINTF("]:%d\n", uip_ntohs(remote->port)); - } -#if 0 - if(uip_newdata()) { - uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr); - src.port = UIP_UDP_BUF->srcport; - uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr); - dst.port = UIP_UDP_BUF->destport; - - bytes_read = uip_datalen(); - ((char *)uip_appdata)[bytes_read] = 0; - PRINTF("Server received %d bytes from [", (int)bytes_read); - PRINT6ADDR(&src.addr); - PRINTF("]:%d\n", uip_ntohs(src.port)); - } + ((char *)uip_appdata)[len] = 0; +#ifndef NDEBUG + if (LOG_DEBUG <= coap_get_log_level()) { +#ifndef INET6_ADDRSTRLEN +#define INET6_ADDRSTRLEN 40 #endif + unsigned char addr_str[INET6_ADDRSTRLEN+8]; + + if (coap_print_addr(&(*packet)->src, addr_str, INET6_ADDRSTRLEN+8)) { + debug("received %d bytes from %s\n", len, addr_str); + } + } +#endif /* NDEBUG */ + + (*packet)->length = len; + memcpy(&(*packet)->payload, uip_appdata, len); + } + #undef UIP_IP_BUF #undef UIP_UDP_BUF #endif /* WITH_CONTIKI */ diff --git a/mem.c b/mem.c index 37153803..8d0199ef 100644 --- a/mem.c +++ b/mem.c @@ -43,6 +43,7 @@ coap_free_type(coap_memory_tag_t type UNUSED_PARAM, void *p) { #else /* HAVE_MALLOC */ #ifdef WITH_CONTIKI + #define COAP_MAX_STRING_SIZE 12 #define COAP_MAX_STRINGS 8 @@ -50,19 +51,33 @@ struct coap_string_t { char data[COAP_MAX_STRING_SIZE]; }; +#include "pdu.h" +#include "coap_io.h" + +#define COAP_MAX_PACKET_SIZE (sizeof(coap_packet_t) + COAP_MAX_PDU_SIZE) +#define COAP_MAX_PACKETS 2 + +struct coap_packetbuf_t { + char data[COAP_MAX_PACKET_SIZE]; +}; + + MEMB(string_storage, struct coap_string_t, COAP_MAX_STRINGS); +MEMB(packet_storage, struct coap_packetbuf_t, COAP_MAX_PACKETS); static struct memb * get_container(coap_memory_tag_t type) { switch(type) { default: return &string_storage; + return &packet_storage; } } void coap_memory_init(void) { memb_init(&string_storage); + memb_init(&packet_storage); } void * diff --git a/mem.h b/mem.h index b6b4ffcf..961af7b2 100644 --- a/mem.h +++ b/mem.h @@ -26,7 +26,7 @@ void coap_memory_init(void); * memory management on constrained devices. */ typedef enum { - COAP_STRING, COAP_ATTRIBUTE_NAME, COAP_ATTRIBUTE_VALUE + COAP_STRING, COAP_ATTRIBUTE_NAME, COAP_ATTRIBUTE_VALUE, COAP_PACKET } coap_memory_tag_t; /** diff --git a/net.c b/net.c index 1a5275f3..312464d4 100644 --- a/net.c +++ b/net.c @@ -540,7 +540,7 @@ coap_send_ack(coap_context_t *context, return result; } -#ifdef WITH_POSIX +#if defined(WITH_POSIX) || defined(WITH_CONTIKI) /* releases space allocated by PDU if free_pdu is set */ coap_tid_t coap_send_impl(coap_context_t *context, @@ -819,9 +819,6 @@ coap_read( coap_context_t *ctx ) { coap_address_t src; int result = -1; /* the value to be returned */ -#ifdef WITH_CONTIKI - buf = uip_appdata; -#endif /* WITH_CONTIKI */ #ifdef WITH_LWIP LWIP_ASSERT("No package pending", ctx->pending_package != NULL); LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);