mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-22 23:35:13 +03:00
Update code to build LwIP and Contiki from the examples directory
Update code to use new APIs [Some code changes modelled on PR #202, some code being moved between files] Remove #ifdef WITH_LWIP from coap_io.c as file is not used Add in resource time to lwip Update for Travis build Report on Contiki memory allocate failures to ease debugging Make the different memory type counts configurable by using the -D option included in a CFLAGS definition to the ./configure command line .travis.yml BUILDING Makefile.libcoap examples/contiki/Makefile examples/contiki/README new examples/contiki/server.c examples/contiki/.gitignore (new) examples/lwip/Makefile examples/lwip/README examples/lwip/lwipopts.h examples/lwip/server-coap.c examples/lwip/server.c include/coap/address.h include/coap/coap_io.h include/coap/lwippools.h include/coap/pdu.h scripts/build.sh src/address.c src/coap_io.c src/coap_io_lwip.c src/coap_session.c src/mem.c src/net.c src/pdu.c
This commit is contained in:
1 parent
ccb01d0257
commit
d766f9d8ee
26 files changed
+537
-159
No files matched your search
+6
-2
@@ -12,8 +12,8 @@ env:
|
||||
- PLATFORM=posix TESTS=yes TLS=no
|
||||
- PLATFORM=posix TESTS=yes TLS=openssl
|
||||
- PLATFORM=posix TESTS=yes TLS=tinydtls
|
||||
# - PLATFORM=contiki TLS=no
|
||||
# - PLATFORM=lwip TLS=no
|
||||
- PLATFORM=contiki TLS=no
|
||||
- PLATFORM=lwip TLS=no
|
||||
|
||||
matrix:
|
||||
exclude:
|
||||
@@ -21,6 +21,10 @@ matrix:
|
||||
env: PLATFORM=posix TESTS=yes TLS=no
|
||||
- compiler: clang
|
||||
env: PLATFORM=posix TESTS=yes TLS=tinydtls
|
||||
- compiler: clang
|
||||
env: PLATFORM=contiki TLS=no
|
||||
- compiler: clang
|
||||
env: PLATFORM=lwip TLS=no
|
||||
|
||||
before_install:
|
||||
- docker build -t obgm/libcoap:travis-env .
|
||||
|
||||
@@ -3,12 +3,15 @@ To build the libcoap libraries, you need to do the following
|
||||
Unpack the distribution package file
|
||||
Change into the top level directory of the unpackaged files
|
||||
|
||||
Run the following commands
|
||||
Run the following commands (not for LwIP or Contiki - see below)
|
||||
|
||||
General Building
|
||||
================
|
||||
|
||||
./autogen.sh
|
||||
./configure
|
||||
make
|
||||
make install
|
||||
sudo make install
|
||||
|
||||
./autogen.sh will fail if there is a required package for buildling libcoap
|
||||
that is missing. Install the missing package and try ./autogen.sh again.
|
||||
@@ -47,3 +50,23 @@ function changes, run
|
||||
make update-map-file
|
||||
|
||||
prior to running 'make'.
|
||||
|
||||
LwIP
|
||||
====
|
||||
|
||||
./autogen.sh
|
||||
./configure --disable-tests --disable-documentation --disable-examples --disable-dtls
|
||||
cd examples/lwip
|
||||
make
|
||||
|
||||
Executable is ./server. See examples/lwip/README for further information
|
||||
|
||||
Contiki
|
||||
=======
|
||||
|
||||
./autogen.sh
|
||||
./configure --disable-tests --disable-documentation --disable-examples --disable-dtls
|
||||
cd examples/contiki
|
||||
make
|
||||
|
||||
Executable is ./server.minimal-net. See examples/contiki/README for further information
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
libcoap_src = pdu.c net.c debug.c encode.c uri.c subscribe.c resource.c hashkey.c str.c option.c async.c block.c mem.c coap_io.c session.c
|
||||
libcoap_src = pdu.c net.c debug.c encode.c uri.c subscribe.c resource.c str.c option.c async.c block.c mem.c coap_io.c coap_session.c coap_notls.c
|
||||
|
||||
libcoap_dir := $(filter %libcoap,$(APPDS))
|
||||
vpath %c $(libcoap_dir)/src
|
||||
|
||||
+21
-3
@@ -1,5 +1,5 @@
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_
|
||||
#ifndef _COAP_CONFIG_H_
|
||||
#define _COAP_CONFIG_H_
|
||||
|
||||
#include "contiki.h"
|
||||
#include "contiki-lib.h"
|
||||
@@ -10,17 +10,25 @@
|
||||
#define PACKAGE_STRING "libcoap"
|
||||
#define PACKAGE_NAME "libcoap"
|
||||
|
||||
#ifndef COAP_DEFAULT_PDU_SIZE
|
||||
#define COAP_DEFAULT_PDU_SIZE 64
|
||||
#endif /* COAP_DEFAULT_PDU_SIZE */
|
||||
#ifndef COAP_RXBUFFER_SIZE
|
||||
#define COAP_RXBUFFER_SIZE 64
|
||||
#endif /* COAP_RXBUFFER_SIZE */
|
||||
|
||||
/** Number of resources that can be handled by a CoAP server in addition to
|
||||
* @c /.well-known/core */
|
||||
#ifndef COAP_MAX_RESOURCES
|
||||
#define COAP_MAX_RESOURCES 3
|
||||
#endif /* COAP_MAX_RESOURCES */
|
||||
|
||||
/** Number of attributes that can be handled (should be at least
|
||||
* @c 2 * COAP_MAX_RESOURCES. to carry the content type and the
|
||||
* resource type. */
|
||||
#ifndef COAP_MAX_ATTRIBUTES
|
||||
#define COAP_MAX_ATTRIBUTES 4
|
||||
#endif /* COAP_MAX_ATTRIBUTES */
|
||||
|
||||
/**
|
||||
* Number of PDUs that can be stored simultaneously. This number
|
||||
@@ -28,32 +36,42 @@
|
||||
* PDUs received. Beware that choosing a too small value can lead to
|
||||
* many retransmissions to be dealt with.
|
||||
*/
|
||||
#ifndef COAP_PDU_MAXCNT
|
||||
#define COAP_PDU_MAXCNT 4
|
||||
#endif /* COAP_PDU_MAXCNT */
|
||||
|
||||
/**
|
||||
* Maximum number of sessions.
|
||||
*/
|
||||
#ifndef COAP_MAX_SESSIONS
|
||||
#define COAP_MAX_SESSIONS 2
|
||||
#endif /* COAP_MAX_SESSIONS */
|
||||
|
||||
/**
|
||||
* Maximum number of subscriptions. Every additional subscriber costs
|
||||
* 36 B.
|
||||
*/
|
||||
#ifndef COAP_MAX_SUBSCRIBERS
|
||||
#define COAP_MAX_SUBSCRIBERS 3
|
||||
#endif /* COAP_MAX_SUBSCRIBERS */
|
||||
|
||||
/**
|
||||
* Number of notifications that may be sent non-confirmable before a
|
||||
* confirmable message is sent to detect if observers are alive. The
|
||||
* maximum allowed value here is @c 15.
|
||||
*/
|
||||
#ifndef COAP_OBS_MAX_NON
|
||||
#define COAP_OBS_MAX_NON 5
|
||||
#endif /* COAP_OBS_MAX_NON */
|
||||
|
||||
/**
|
||||
* Number of confirmable notifications that may fail (i.e. time out
|
||||
* without being ACKed) before an observer is removed. The maximum
|
||||
* value for COAP_OBS_MAX_FAIL is @c 3.
|
||||
*/
|
||||
#ifndef COAP_OBS_MAX_FAIL
|
||||
#define COAP_OBS_MAX_FAIL 3
|
||||
#endif /* COAP_OBS_MAX_FAIL */
|
||||
|
||||
#ifndef DEBUG
|
||||
# define DEBUG DEBUG_PRINT
|
||||
@@ -142,5 +160,5 @@ typedef void FILE;
|
||||
#include <stdio.h>
|
||||
#define coap_log(fd, ...) printf(__VA_ARGS__)
|
||||
|
||||
#endif /* _CONFIG_H_ */
|
||||
#endif /* _COAP_CONFIG_H_ */
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
contiki-minimal-net.a
|
||||
contiki-minimal-net.map
|
||||
contiki/
|
||||
obj_minimal-net/
|
||||
server.minimal-net
|
||||
symbols.c
|
||||
symbols.h
|
||||
@@ -9,3 +9,7 @@ $(CONTIKI):
|
||||
|
||||
server: $(CONTIKI)
|
||||
$(MAKE) -f Makefile.contiki V=1 CONTIKI=$(CONTIKI) TARGET=$(TARGET) NODE_ADDR=0x1000 server
|
||||
|
||||
clean:
|
||||
$(MAKE) -f Makefile.contiki V=1 CONTIKI=$(CONTIKI) TARGET=$(TARGET) clean
|
||||
rm -f server.minimal-net symbols.c symbols.h
|
||||
@@ -0,0 +1,22 @@
|
||||
Example of libcoap running on contiki
|
||||
=====================================
|
||||
|
||||
To run the example, do
|
||||
|
||||
$ make
|
||||
$ sudo ./server.minimal-net
|
||||
|
||||
(and in a second terminal)
|
||||
|
||||
$ sudo ip -6 a a aaaa::1/64 dev tap0
|
||||
|
||||
and query `coap://[aaaa::1000]/` with any coap tool.
|
||||
|
||||
This will
|
||||
|
||||
* download contike from the upstream git sources
|
||||
* build the server application
|
||||
* run the server application, creating a virtual network device tap0 (unless
|
||||
that exists)
|
||||
* configure your network interface to make the server accessible.
|
||||
|
||||
+17
-15
@@ -38,7 +38,6 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "coap.h"
|
||||
|
||||
static coap_context_t *coap_context;
|
||||
@@ -93,11 +92,10 @@ init_coap_server(coap_context_t **ctx) {
|
||||
|
||||
void
|
||||
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, coap_binary_t *token,
|
||||
coap_session_t *session,
|
||||
coap_pdu_t *request, coap_binary_t *token,
|
||||
coap_string_t *query,
|
||||
coap_pdu_t *response) {
|
||||
coap_opt_iterator_t opt_iter;
|
||||
coap_opt_t *option;
|
||||
unsigned char buf[40];
|
||||
size_t len;
|
||||
coap_tick_t now;
|
||||
@@ -107,10 +105,10 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
* when query ?ticks is given. */
|
||||
|
||||
/* if my_clock_base was deleted, we pretend to have no such resource */
|
||||
response->hdr->code =
|
||||
response->code =
|
||||
my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);
|
||||
|
||||
if (coap_find_observer(resource, peer, token)) {
|
||||
if (coap_find_observer(resource, session, token)) {
|
||||
coap_add_option(response, COAP_OPTION_OBSERVE,
|
||||
coap_encode_var_safe(buf, sizeof(buf),
|
||||
resource->observe),
|
||||
@@ -132,10 +130,9 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
coap_ticks(&t);
|
||||
now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
|
||||
|
||||
if (request != NULL
|
||||
&& (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))
|
||||
&& memcmp(COAP_OPT_VALUE(option), "ticks",
|
||||
min(5, COAP_OPT_LENGTH(option))) == 0) {
|
||||
|
||||
if (query != NULL
|
||||
&& coap_string_equal(query, coap_make_str_const("ticks"))) {
|
||||
/* output ticks */
|
||||
len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now);
|
||||
coap_add_data(response, len, buf);
|
||||
@@ -161,7 +158,7 @@ init_coap_resources(coap_context_t *ctx) {
|
||||
if (!r)
|
||||
goto error;
|
||||
|
||||
r->observable = 1;
|
||||
coap_resource_set_get_observable(r, 1);
|
||||
time_resource = r;
|
||||
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
|
||||
#if 0
|
||||
@@ -218,10 +215,15 @@ PROCESS_THREAD(coap_server_process, ev, data)
|
||||
while(1) {
|
||||
PROCESS_YIELD();
|
||||
if(ev == tcpip_event) {
|
||||
coap_read(coap_context); /* read received data */
|
||||
/* coap_dispatch(coap_context); /\* and dispatch PDUs from receivequeue *\/ */
|
||||
coap_tick_t now;
|
||||
|
||||
coap_ticks(&now);
|
||||
/* There is something to read on the endpoint */
|
||||
coap_context->endpoint->sock.flags |= COAP_SOCKET_CAN_READ;
|
||||
/* read in, and send off any responses */
|
||||
coap_read(coap_context, now); /* read received data */
|
||||
} else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) {
|
||||
coap_resource_set_dirty(time_resource, NULL);
|
||||
coap_resource_notify_observers(time_resource, NULL);
|
||||
etimer_reset(&dirty_timer);
|
||||
}
|
||||
}
|
||||
|
||||
+19
-9
@@ -1,23 +1,31 @@
|
||||
top_srcdir = ../..
|
||||
|
||||
WITH_BRANCH=-b STABLE-2_0_0_RC1
|
||||
WITH_LWIP_BRANCH=STABLE-2_0_3_RELEASE
|
||||
WITH_LWIP_CONTRIB_BRANCH=STABLE-2_0_1_RELEASE
|
||||
|
||||
# Need to determine which library clock_gettime() resides in (as found by ./autogen.sh)
|
||||
LDLIBS := $(shell if [ ../../config.log ] ; then grep ac_cv_search_clock_gettime=- ../../config.log|cut -d= -f2 ; fi)
|
||||
|
||||
all: server
|
||||
|
||||
lwip:
|
||||
git clone --depth 1 git://git.savannah.nongnu.org/lwip.git $(WITH_BRANCH)
|
||||
git clone --depth 1 git://git.savannah.nongnu.org/lwip.git -b $(WITH_LWIP_BRANCH)
|
||||
(cd lwip ; git checkout $(WITH_LWIP_BRANCH))
|
||||
$(MAKE)
|
||||
|
||||
lwip-contrib:
|
||||
git clone --depth 1 git://git.savannah.nongnu.org/lwip/lwip-contrib.git $(WITH_BRANCH)
|
||||
git clone --depth 1 git://git.savannah.nongnu.org/lwip/lwip-contrib.git -b $(WITH_LWIP_CONTRIB_BRANCH)
|
||||
(cd lwip-contrib ; git checkout $(WITH_LWIP_CONTRIB_BRANCH))
|
||||
$(MAKE)
|
||||
|
||||
# lwip and coap opts (include early to shadow the lwip-contrib/ports/unix/proj/minimal/ file and any ../../config.h)
|
||||
CFLAGS += -DWITH_LWIP -iquote.
|
||||
|
||||
# lwip library
|
||||
|
||||
LWIPOBJS = def.o init.o tapif.o etharp.o netif.o timers.o stats.o mem.o memp.o udp.o tcp.o pbuf.o ip4_addr.o ip4.o inet_chksum.o tcp_in.o tcp_out.o icmp.o raw.o ip_frag.o sys_arch.o ethernet.o ip.o
|
||||
vpath %.c lwip/src/core/ lwip-contrib/ports/unix/proj/minimal/ lwip/src/netif/ lwip/src/core/ipv4/ lwip-contrib/ports/unix/ lwip-contrib/ports/unix/netif/
|
||||
CFLAGS += -DLWIP_UNIX_LINUX
|
||||
LWIPOBJS = def.o init.o tapif.o etharp.o netif.o timeouts.o stats.o mem.o memp.o udp.o tcp.o pbuf.o ip4_addr.o ip4.o inet_chksum.o tcp_in.o tcp_out.o icmp.o raw.o ip4_frag.o sys_arch.o ethernet.o ip.o
|
||||
vpath %.c lwip/src/core/ lwip-contrib/ports/unix/proj/minimal/ lwip/src/netif/ lwip/src/core/ipv4/ lwip-contrib/ports/unix/port/ lwip-contrib/ports/unix/port/netif/
|
||||
# CFLAGS += -DLWIP_UNIX_LINUX
|
||||
|
||||
# if ipv6 is used
|
||||
vpath %.c lwip/src/core/ipv6/
|
||||
@@ -31,24 +39,26 @@ CFLAGS += -I$(top_srcdir)/include/coap
|
||||
|
||||
vpath %.c $(top_srcdir)/src
|
||||
|
||||
COAPOBJS = net.o debug.o option.o resource.o hashkey.o pdu.o encode.o subscribe.o coap_io_lwip.o block.o uri.o
|
||||
COAPOBJS = net.o debug.o option.o resource.o pdu.o encode.o subscribe.o coap_io_lwip.o block.o uri.o str.o coap_session.o coap_notls.o
|
||||
|
||||
CFLAGS += -g3 -Wall -Wextra -pedantic -O0 -fpack-struct
|
||||
# not sorted out yet
|
||||
CFLAGS += -Wno-unused-parameter
|
||||
|
||||
CFLAGS += -Ilwip/src/include/ -Ilwip/src/include/ipv4/ -Ilwip-contrib/ports/unix/include/ -Ilwip-contrib/ports/unix/proj/minimal/
|
||||
CFLAGS += -Ilwip/src/include/ -Ilwip/src/include/ipv4/ -Ilwip-contrib/ports/unix/port/include/ -Ilwip-contrib/ports/unix/proj/minimal/
|
||||
|
||||
# making an executable out of all of it
|
||||
|
||||
OBJS = server.o server-coap.o ${LWIPOBJS} ${COAPOBJS}
|
||||
|
||||
../../include/coap/coap.h:
|
||||
@echo "Error: coap.h not present. Run teh autotools chain (\`./autogen.sh && ./configure\`) in the project root directory to build the required coap.h file."
|
||||
@echo "Error: coap.h not present. Run the autotools chain (\`./autogen.sh && ./configure\`) in the project root directory to build the required coap.h file."
|
||||
@exit 1
|
||||
|
||||
${OBJS}: lwip lwip-contrib ../../include/coap/coap.h
|
||||
|
||||
server: ${OBJS}
|
||||
# $(CC) ${OBJS} -o server -lrt
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS}
|
||||
|
||||
@@ -8,20 +8,20 @@ To run the example, do
|
||||
|
||||
(and in a second terminal)
|
||||
|
||||
$ sudo ip a a dev tap0 192.168.0.1/24
|
||||
$ sudo ip a a dev tap0 192.168.113.1/24
|
||||
|
||||
and query `coap://192.168.0.2/` with any coap tool.
|
||||
and query `coap://192.168.113.2/` with any coap tool.
|
||||
|
||||
This will
|
||||
|
||||
* download lwip and lwip-contrib from the upstream git sources
|
||||
* apply a required patch to lwip
|
||||
* build the server application
|
||||
* run the server application, creating a virtual network device tap0 (unless
|
||||
that exists)
|
||||
* configure your network interface to make the server accessible.
|
||||
|
||||
The server does not create any resources (it exposes an empty
|
||||
`.well-known/core`), but the work flow for adding more resources does not
|
||||
differ from regular libcoap usage. If you seem to run out of memory creating
|
||||
the resources, tweak the number of pre-allocated resources in `lwippools.h`.
|
||||
The server creates a resource for 'time' with a query 'ticks'. This is
|
||||
reported for `.well-known/core`. The work flow for adding more resources does
|
||||
not differ from regular libcoap usage. If you seem to run out of memory
|
||||
creating the resources, tweak the number of pre-allocated resources
|
||||
in `include/coap/lwippools.h`.
|
||||
@@ -1,11 +1,25 @@
|
||||
#define NO_SYS 1
|
||||
typedef unsigned int sys_prot_t;
|
||||
|
||||
/* they'd require NO_SYS=0, but are enabled by default */
|
||||
#define LWIP_SOCKET 0
|
||||
#define LWIP_NETCONN 0
|
||||
#define NO_SYS 1
|
||||
#define LWIP_SOCKET (NO_SYS==0)
|
||||
#define LWIP_NETCONN (NO_SYS==0)
|
||||
#define LWIP_NETIF_API (NO_SYS==0)
|
||||
|
||||
#define LWIP_IPV6 1
|
||||
#define LWIP_IPV6_REASS 0
|
||||
|
||||
#ifndef LWIP_RAND
|
||||
#define LWIP_RAND() ((u32_t)rand())
|
||||
#endif
|
||||
|
||||
#ifndef netif_get_index
|
||||
#define netif_get_index(netif) ((u8_t)((netif)->num + 1))
|
||||
#endif
|
||||
|
||||
#if NO_SYS
|
||||
#include <pthread.h>
|
||||
extern pthread_mutex_t lwprot_mutex;
|
||||
extern pthread_t lwprot_thread;
|
||||
extern int lwprot_count;
|
||||
#endif
|
||||
#define MEMP_USE_CUSTOM_POOLS 1
|
||||
+128
-8
@@ -3,22 +3,142 @@
|
||||
|
||||
coap_context_t *main_coap_context;
|
||||
|
||||
static coap_time_t clock_offset;
|
||||
/* changeable clock base (see handle_put_time()) */
|
||||
static coap_time_t my_clock_base = 0;
|
||||
static coap_resource_t *time_resource = NULL; /* just for testing */
|
||||
|
||||
#ifndef min
|
||||
# define min(a,b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
void
|
||||
hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
coap_session_t *session,
|
||||
coap_pdu_t *request, coap_binary_t *token,
|
||||
coap_string_t *query,
|
||||
coap_pdu_t *response) {
|
||||
unsigned char buf[40];
|
||||
size_t len;
|
||||
coap_tick_t now;
|
||||
coap_tick_t t;
|
||||
|
||||
/* FIXME: return time, e.g. in human-readable by default and ticks
|
||||
* when query ?ticks is given. */
|
||||
|
||||
/* if my_clock_base was deleted, we pretend to have no such resource */
|
||||
response->code =
|
||||
my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);
|
||||
|
||||
if (coap_find_observer(resource, session, token)) {
|
||||
coap_add_option(response, COAP_OPTION_OBSERVE,
|
||||
coap_encode_var_safe(buf, sizeof(buf),
|
||||
resource->observe),
|
||||
buf);
|
||||
}
|
||||
|
||||
if (my_clock_base)
|
||||
coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
|
||||
coap_encode_var_safe(buf, sizeof(buf),
|
||||
COAP_MEDIATYPE_TEXT_PLAIN),
|
||||
buf);
|
||||
|
||||
coap_add_option(response, COAP_OPTION_MAXAGE,
|
||||
coap_encode_var_safe(buf, sizeof(buf), 0x01), buf);
|
||||
|
||||
if (my_clock_base) {
|
||||
|
||||
/* calculate current time */
|
||||
coap_ticks(&t);
|
||||
now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
|
||||
|
||||
|
||||
if (query != NULL
|
||||
&& coap_string_equal(query, coap_make_str_const("ticks"))) {
|
||||
/* output ticks */
|
||||
len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now);
|
||||
coap_add_data(response, len, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
init_coap_resources(coap_context_t *ctx) {
|
||||
coap_resource_t *r;
|
||||
#if 0
|
||||
r = coap_resource_init(NULL, 0, 0);
|
||||
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index);
|
||||
|
||||
coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
|
||||
coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"General Info\""), 0);
|
||||
coap_add_resource(ctx, r);
|
||||
#endif
|
||||
/* store clock base to use in /time */
|
||||
my_clock_base = clock_offset;
|
||||
|
||||
r = coap_resource_init(coap_make_str_const("time"), 0);
|
||||
if (!r)
|
||||
goto error;
|
||||
|
||||
coap_resource_set_get_observable(r, 1);
|
||||
time_resource = r;
|
||||
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
|
||||
#if 0
|
||||
coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time);
|
||||
coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
|
||||
#endif
|
||||
coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
|
||||
/* coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"Internal Clock\""), 0); */
|
||||
coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"ticks\""), 0);
|
||||
coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""), 0);
|
||||
|
||||
coap_add_resource(ctx, r);
|
||||
#if 0
|
||||
#ifndef WITHOUT_ASYNC
|
||||
r = coap_resource_init(coap_make_str_const("async"), 0);
|
||||
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async);
|
||||
|
||||
coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
|
||||
coap_add_resource(ctx, r);
|
||||
#endif /* WITHOUT_ASYNC */
|
||||
#endif
|
||||
|
||||
return;
|
||||
error:
|
||||
coap_log(LOG_CRIT, "cannot create resource\n");
|
||||
}
|
||||
|
||||
void server_coap_init(void)
|
||||
{
|
||||
coap_address_t listenaddress;
|
||||
coap_address_t listenaddress;
|
||||
|
||||
coap_address_init(&listenaddress);
|
||||
coap_address_init(&listenaddress);
|
||||
|
||||
/* looks like a server address, but is used as end point for clients too */
|
||||
listenaddress.addr = *(IP_ANY_TYPE);
|
||||
listenaddress.port = COAP_DEFAULT_PORT;
|
||||
/* looks like a server address, but is used as end point for clients too */
|
||||
listenaddress.addr = *(IP_ANY_TYPE);
|
||||
listenaddress.port = COAP_DEFAULT_PORT;
|
||||
|
||||
main_coap_context = coap_new_context(&listenaddress);
|
||||
coap_set_log_level(LOG_DEBUG);
|
||||
main_coap_context = coap_new_context(&listenaddress);
|
||||
|
||||
LWIP_ASSERT("Failed to initialize context", main_coap_context != NULL);
|
||||
LWIP_ASSERT("Failed to initialize context", main_coap_context != NULL);
|
||||
clock_offset = 1; /* Need a non-zero value */
|
||||
init_coap_resources(main_coap_context);
|
||||
}
|
||||
|
||||
void server_coap_poll(void)
|
||||
{
|
||||
coap_check_notify(main_coap_context);
|
||||
static coap_time_t last_time = 0;
|
||||
coap_tick_t ticks_now;
|
||||
coap_time_t time_now;
|
||||
|
||||
coap_ticks(&ticks_now);
|
||||
time_now = coap_ticks_to_rt(ticks_now);
|
||||
|
||||
if (last_time != time_now) {
|
||||
/* This takes place once a second */
|
||||
last_time = time_now;
|
||||
coap_resource_notify_observers(time_resource, NULL);
|
||||
}
|
||||
coap_check_notify(main_coap_context);
|
||||
}
|
||||
@@ -37,7 +37,7 @@
|
||||
#include "server-coap.h"
|
||||
|
||||
#include <lwip/init.h>
|
||||
#include <lwip/timers.h>
|
||||
#include <lwip/timeouts.h>
|
||||
|
||||
#include <netif/etharp.h>
|
||||
#include <netif/tapif.h>
|
||||
@@ -55,8 +55,8 @@ main(int argc, char **argv)
|
||||
* hard-coded v4 even in presence of v6, which does auto-discovery and
|
||||
* should thus wind up with an address of fe80::12:34ff:fe56:78ab%tap0
|
||||
* */
|
||||
IP4_ADDR(&gw, 192,168,0,1);
|
||||
IP4_ADDR(&ipaddr, 192,168,0,2);
|
||||
IP4_ADDR(&gw, 192,168,113,1);
|
||||
IP4_ADDR(&ipaddr, 192,168,113,2);
|
||||
IP4_ADDR(&netmask, 255,255,255,0);
|
||||
|
||||
lwip_init();
|
||||
|
||||
@@ -30,7 +30,9 @@ typedef struct coap_address_t {
|
||||
ip_addr_t addr;
|
||||
} coap_address_t;
|
||||
|
||||
#define _coap_address_equals_impl(A, B) (!!ip_addr_cmp(&(A)->addr,&(B)->addr))
|
||||
#define _coap_address_equals_impl(A, B) \
|
||||
((A)->port == (B)->port \
|
||||
&& (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
|
||||
|
||||
#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
|
||||
|
||||
|
||||
@@ -186,8 +186,11 @@ struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
|
||||
*/
|
||||
struct coap_packet_t {
|
||||
struct pbuf *pbuf;
|
||||
const coap_endpoint_t *local_interface;
|
||||
uint16_t srcport;
|
||||
const struct coap_endpoint_t *local_interface;
|
||||
coap_address_t src; /**< the packet's source address */
|
||||
coap_address_t dst; /**< the packet's destination address */
|
||||
int ifindex; /**< the interface index */
|
||||
// uint16_t srcport;
|
||||
};
|
||||
#else
|
||||
struct coap_packet_t {
|
||||
|
||||
@@ -119,7 +119,10 @@ void coap_log_impl(coap_log_t level, const char *format, ...);
|
||||
*
|
||||
* @param level One of the LOG_* values.
|
||||
*/
|
||||
#define coap_log(level, ...) do { if ((level)<=(int)coap_get_log_level()) coap_log_impl((level), __VA_ARGS__); } while(0)
|
||||
#define coap_log(level, ...) do { \
|
||||
if ((int)((level))<=(int)coap_get_log_level()) \
|
||||
coap_log_impl((level), __VA_ARGS__); \
|
||||
} while(0)
|
||||
#endif
|
||||
|
||||
/* A set of convenience macros for common log levels. */
|
||||
|
||||
@@ -51,6 +51,22 @@
|
||||
#define MEMP_NUM_COAPRESOURCEATTR 20
|
||||
#endif
|
||||
|
||||
#ifndef MEMP_NUM_COAPOPTLIST
|
||||
#define MEMP_NUM_COAPOPTLIST 1
|
||||
#endif
|
||||
|
||||
#ifndef MEMP_LEN_COAPOPTLIST
|
||||
#define MEMP_LEN_COAPOPTLIST 12
|
||||
#endif
|
||||
|
||||
#ifndef MEMP_NUM_COAPSTRING
|
||||
#define MEMP_NUM_COAPSTRING 10
|
||||
#endif
|
||||
|
||||
#ifndef MEMP_LEN_COAPSTRING
|
||||
#define MEMP_LEN_COAPSTRING 32
|
||||
#endif
|
||||
|
||||
LWIP_MEMPOOL(COAP_CONTEXT, MEMP_NUM_COAPCONTEXT, sizeof(coap_context_t), "COAP_CONTEXT")
|
||||
LWIP_MEMPOOL(COAP_ENDPOINT, MEMP_NUM_COAPENDPOINT, sizeof(coap_endpoint_t), "COAP_ENDPOINT")
|
||||
LWIP_MEMPOOL(COAP_PACKET, MEMP_NUM_COAPPACKET, sizeof(coap_packet_t), "COAP_PACKET")
|
||||
@@ -60,3 +76,6 @@ LWIP_MEMPOOL(COAP_SESSION, MEMP_NUM_COAPSESSION, sizeof(coap_session_t), "COAP_S
|
||||
LWIP_MEMPOOL(COAP_subscription, MEMP_NUM_COAP_SUBSCRIPTION, sizeof(coap_subscription_t), "COAP_subscription")
|
||||
LWIP_MEMPOOL(COAP_RESOURCE, MEMP_NUM_COAPRESOURCE, sizeof(coap_resource_t), "COAP_RESOURCE")
|
||||
LWIP_MEMPOOL(COAP_RESOURCEATTR, MEMP_NUM_COAPRESOURCEATTR, sizeof(coap_attr_t), "COAP_RESOURCEATTR")
|
||||
LWIP_MEMPOOL(COAP_OPTLIST, MEMP_NUM_COAPOPTLIST, sizeof(coap_optlist_t)+MEMP_LEN_COAPOPTLIST, "COAP_OPTLIST")
|
||||
LWIP_MEMPOOL(COAP_STRING, MEMP_NUM_COAPSTRING, sizeof(coap_string_t)+MEMP_LEN_COAPSTRING, "COAP_STRING")
|
||||
|
||||
+1
-1
@@ -284,7 +284,7 @@ typedef struct coap_pdu_t {
|
||||
size_t max_size; /**< maximum size for token, options and payload, or zero for variable size pdu */
|
||||
uint8_t *token; /**< first byte of token, if any, or options */
|
||||
uint8_t *data; /**< first byte of payload, if any */
|
||||
#ifdef LWIP
|
||||
#ifdef WITH_LWIP
|
||||
struct pbuf *pbuf; /**< lwIP PBUF. The package data will always reside
|
||||
* inside the pbuf's payload, but this pointer
|
||||
* has to be kept because no exact offset can be
|
||||
|
||||
+1
-2
@@ -26,8 +26,7 @@ case "${PLATFORM}" in
|
||||
make -C examples/contiki
|
||||
;;
|
||||
lwip) config "--disable-tests --disable-documentation --disable-examples $WITH_TLS" && \
|
||||
make -C examples/lwip lwip lwip-contrib
|
||||
make -C examples/lwip LDLIBS=`grep ac_cv_search_clock_gettime=- config.log|cut -d= -f2`
|
||||
make -C examples/lwip
|
||||
;;
|
||||
posix|*) config "$WITH_TESTS --disable-documentation --enable-examples $WITH_TLS" && \
|
||||
make
|
||||
|
||||
+7
-3
@@ -67,9 +67,13 @@ int coap_is_mcast(const coap_address_t *a) {
|
||||
}
|
||||
#else /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
|
||||
|
||||
/* make compilers happy that do not like empty modules */
|
||||
COAP_STATIC_INLINE void dummy(void)
|
||||
{
|
||||
#ifdef __clang__
|
||||
/* Make compilers happy that do not like empty modules. As this function is
|
||||
* never used, we ignore -Wunused-function at the end of compiling this file
|
||||
*/
|
||||
#pragma GCC diagnostic ignored "-Wunused-function"
|
||||
#endif
|
||||
static inline void dummy(void) {
|
||||
}
|
||||
|
||||
#endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
|
||||
+26
-58
@@ -54,7 +54,7 @@
|
||||
#include "utlist.h"
|
||||
#include "resource.h"
|
||||
|
||||
#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP)
|
||||
#if !defined(WITH_CONTIKI)
|
||||
/* define generic PKTINFO for IPv4 */
|
||||
#if defined(IP_PKTINFO)
|
||||
# define GEN_IP_PKTINFO IP_PKTINFO
|
||||
@@ -107,9 +107,9 @@ coap_socket_bind_udp(coap_socket_t *sock,
|
||||
return 0;
|
||||
}
|
||||
|
||||
coap_address_init(&bound_addr);
|
||||
coap_address_init(bound_addr);
|
||||
uip_ipaddr_copy(&bound_addr->addr, &listen_addr->addr);
|
||||
bound_addr->addr.port = listen_addr->addr.port;
|
||||
bound_addr->port = listen_addr->port;
|
||||
udp_bind((struct uip_udp_conn *)sock->conn, bound_addr->port);
|
||||
return 1;
|
||||
}
|
||||
@@ -653,7 +653,7 @@ coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
|
||||
|
||||
#endif /* WITH_CONTIKI */
|
||||
|
||||
#if (!defined(WITH_CONTIKI) && !defined(WITH_LWIP)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) )
|
||||
#if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) )
|
||||
/* define struct in6_pktinfo and struct in_pktinfo if not available
|
||||
FIXME: check with configure
|
||||
*/
|
||||
@@ -669,7 +669,7 @@ struct in_pktinfo {
|
||||
};
|
||||
#endif
|
||||
|
||||
#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(SOL_IP)
|
||||
#if !defined(WITH_CONTIKI) && !defined(SOL_IP)
|
||||
/* Solaris expects level IPPROTO_IP for ancillary data. */
|
||||
#define SOL_IP IPPROTO_IP
|
||||
#endif
|
||||
@@ -708,11 +708,13 @@ coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint
|
||||
|
||||
if (!coap_debug_send_packet()) {
|
||||
bytes_written = (ssize_t)datalen;
|
||||
#ifndef WITH_CONTIKI
|
||||
} else if (sock->flags & COAP_SOCKET_CONNECTED) {
|
||||
#ifdef _WIN32
|
||||
bytes_written = send(sock->fd, (const char *)data, (int)datalen, 0);
|
||||
#else
|
||||
bytes_written = send(sock->fd, data, datalen, 0);
|
||||
#endif
|
||||
#endif
|
||||
} else {
|
||||
#ifndef WITH_CONTIKI
|
||||
@@ -831,18 +833,6 @@ coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint
|
||||
|
||||
#define SIN6(A) ((struct sockaddr_in6 *)(A))
|
||||
|
||||
#ifdef WITH_CONTIKI
|
||||
COAP_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 */
|
||||
|
||||
void
|
||||
coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length) {
|
||||
*address = packet->payload;
|
||||
@@ -868,6 +858,7 @@ coap_network_read(coap_socket_t *sock, coap_packet_t *packet) {
|
||||
sock->flags &= ~COAP_SOCKET_CAN_READ;
|
||||
}
|
||||
|
||||
#ifndef WITH_CONTIKI
|
||||
if (sock->flags & COAP_SOCKET_CONNECTED) {
|
||||
#ifdef _WIN32
|
||||
len = recv(sock->fd, (char *)packet->payload, COAP_RXBUFFER_SIZE, 0);
|
||||
@@ -890,11 +881,12 @@ coap_network_read(coap_socket_t *sock, coap_packet_t *packet) {
|
||||
packet->length = (size_t)len;
|
||||
}
|
||||
} else {
|
||||
#endif /* WITH_CONTIKI */
|
||||
#if defined(_WIN32)
|
||||
DWORD dwNumberOfBytesRecvd = 0;
|
||||
int r;
|
||||
#endif
|
||||
#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP)
|
||||
#if !defined(WITH_CONTIKI)
|
||||
/* a buffer large enough to hold all packet info types, ipv6 is the largest */
|
||||
char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
|
||||
struct msghdr mhdr;
|
||||
@@ -991,21 +983,21 @@ coap_network_read(coap_socket_t *sock, coap_packet_t *packet) {
|
||||
#endif /* IP_PKTINFO */
|
||||
}
|
||||
}
|
||||
#endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
|
||||
#endif /* !defined(WITH_CONTIKI) */
|
||||
#ifdef WITH_CONTIKI
|
||||
/* FIXME: untested, make this work */
|
||||
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
|
||||
|
||||
if (uip_newdata()) {
|
||||
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;
|
||||
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 > coap_get_max_packetlength(*packet)) {
|
||||
if (len > COAP_RXBUFFER_SIZE) {
|
||||
/* FIXME: we might want to send back a response */
|
||||
warn("discarded oversized packet\n");
|
||||
return -1;
|
||||
@@ -1019,33 +1011,32 @@ coap_network_read(coap_socket_t *sock, coap_packet_t *packet) {
|
||||
#endif
|
||||
unsigned char addr_str[INET6_ADDRSTRLEN + 8];
|
||||
|
||||
if (coap_print_addr(&(*packet)->src, addr_str, INET6_ADDRSTRLEN + 8)) {
|
||||
if (coap_print_addr(&packet->src, addr_str, INET6_ADDRSTRLEN + 8)) {
|
||||
debug("received %zd bytes from %s\n", len, addr_str);
|
||||
}
|
||||
}
|
||||
#endif /* NDEBUG */
|
||||
|
||||
(*packet)->length = len;
|
||||
memcpy(&(*packet)->payload, uip_appdata, len);
|
||||
packet->length = len;
|
||||
memcpy(&packet->payload, uip_appdata, len);
|
||||
}
|
||||
|
||||
#undef UIP_IP_BUF
|
||||
#undef UIP_UDP_BUF
|
||||
#endif /* WITH_CONTIKI */
|
||||
#ifdef WITH_LWIP
|
||||
#error "coap_network_read() not implemented on this platform"
|
||||
#endif
|
||||
#ifndef WITH_CONTIKI
|
||||
}
|
||||
#endif /* WITH_CONTIKI */
|
||||
|
||||
if (len >= 0)
|
||||
return len;
|
||||
#if !defined(WITH_LWIP)
|
||||
#if !defined(WITH_CONTIKI)
|
||||
error:
|
||||
return -1;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
|
||||
#if !defined(WITH_CONTIKI)
|
||||
|
||||
unsigned int
|
||||
coap_write(coap_context_t *ctx,
|
||||
@@ -1265,44 +1256,21 @@ const char *coap_socket_strerror(void) {
|
||||
return coap_socket_format_errno(WSAGetLastError());
|
||||
}
|
||||
#else
|
||||
#ifndef WITH_CONTIKI
|
||||
static const char *coap_socket_format_errno(int error) {
|
||||
return strerror(error);
|
||||
}
|
||||
#endif /* WITH_CONTIKI */
|
||||
|
||||
const char *coap_socket_strerror(void) {
|
||||
return strerror(errno);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(WITH_LWIP)
|
||||
|
||||
ssize_t
|
||||
coap_socket_send_pdu(coap_socket_t *sock, coap_session_t *session,
|
||||
coap_pdu_t *pdu) {
|
||||
/* FIXME: we can't check this here with the existing infrastructure, but we
|
||||
* should actually check that the pdu is not held by anyone but us. the
|
||||
* respective pbuf is already exclusively owned by the pdu. */
|
||||
|
||||
pbuf_realloc(pdu->pbuf, pdu->length);
|
||||
udp_sendto(sock->pcb, pdu->pbuf, &session->remote_addr.addr,
|
||||
session->remote_addr.port);
|
||||
return pdu->length;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
coap_socket_send(coap_socket_t *sock, coap_session_t *session,
|
||||
/* Not implemented, use coap_socket_send_pdu instead */
|
||||
return -1;
|
||||
}
|
||||
|
||||
#else /* defined(WITH_LWIP) */
|
||||
|
||||
ssize_t
|
||||
coap_socket_send(coap_socket_t *sock, coap_session_t *session,
|
||||
const uint8_t *data, size_t data_len) {
|
||||
return session->context->network_send(sock, session, data, data_len);
|
||||
}
|
||||
|
||||
#endif /* defined(WITH_LWIP) */
|
||||
|
||||
#undef SIN6
|
||||
+145
-18
@@ -12,11 +12,19 @@
|
||||
#include "coap_io.h"
|
||||
#include <lwip/udp.h>
|
||||
|
||||
#if NO_SYS
|
||||
pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_t lwprot_thread = (pthread_t)0xDEAD;
|
||||
int lwprot_count = 0;
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
void coap_packet_copy_source(coap_packet_t *packet, coap_address_t *target)
|
||||
{
|
||||
target->port = packet->srcport;
|
||||
memcpy(&target->addr, ip_current_src_addr(), sizeof(ip_addr_t));
|
||||
}
|
||||
#endif
|
||||
void coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length)
|
||||
{
|
||||
LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", packet->pbuf->tot_len == packet->pbuf->len);
|
||||
@@ -40,7 +48,7 @@ struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet)
|
||||
|
||||
/** Callback from lwIP when a package was received.
|
||||
*
|
||||
* The current implementation deals this to coap_handle_dgram immediately, but
|
||||
* The current implementation deals this to coap_dispatch immediately, but
|
||||
* other mechanisms (as storing the package in a queue and later fetching it
|
||||
* when coap_read is called) can be envisioned.
|
||||
*
|
||||
@@ -48,39 +56,74 @@ struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet)
|
||||
*/
|
||||
static void coap_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
|
||||
{
|
||||
coap_endpoint_t *ep = (coap_endpoint_t*)arg;
|
||||
coap_packet_t *packet = coap_malloc_type(COAP_PACKET, sizeof(coap_packet_t));
|
||||
/* this is fatal because due to the short life of the packet, never should there be more than one coap_packet_t required */
|
||||
LWIP_ASSERT("Insufficient coap_packet_t resources.", packet != NULL);
|
||||
packet->pbuf = p;
|
||||
packet->srcport = port;
|
||||
coap_endpoint_t *ep = (coap_endpoint_t*)arg;
|
||||
coap_pdu_t *pdu = NULL;
|
||||
coap_session_t *session;
|
||||
coap_tick_t now;
|
||||
coap_packet_t *packet = coap_malloc_type(COAP_PACKET, sizeof(coap_packet_t));
|
||||
|
||||
/** FIXME derive the context without changing endpoint definition */
|
||||
coap_handle_dgram(ep->context, packet);
|
||||
/* this is fatal because due to the short life of the packet, never should there be more than one coap_packet_t required */
|
||||
LWIP_ASSERT("Insufficient coap_packet_t resources.", packet != NULL);
|
||||
packet->pbuf = p;
|
||||
packet->src.port = port;
|
||||
packet->src.addr = *addr;
|
||||
packet->dst.port = upcb->local_port;
|
||||
packet->dst.addr = *ip_current_dest_addr();
|
||||
packet->ifindex = netif_get_index(ip_current_netif());
|
||||
|
||||
coap_free_packet(packet);
|
||||
pdu = coap_pdu_from_pbuf(p);
|
||||
if (!pdu)
|
||||
goto error;
|
||||
|
||||
if (!coap_pdu_parse(ep->proto, p->payload, p->len, pdu)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
coap_ticks(&now);
|
||||
session = coap_endpoint_get_session(ep, packet, now);
|
||||
if (!session)
|
||||
goto error;
|
||||
LWIP_ASSERT("Proto not supported for LWIP", COAP_PROTO_NOT_RELIABLE(session->proto));
|
||||
coap_dispatch(ep->context, session, pdu);
|
||||
|
||||
coap_delete_pdu(pdu);
|
||||
packet->pbuf = NULL;
|
||||
coap_free_packet(packet);
|
||||
return;
|
||||
|
||||
error:
|
||||
/* FIXME: send back RST? */
|
||||
if (pdu) coap_delete_pdu(pdu);
|
||||
if (packet) {
|
||||
packet->pbuf = NULL;
|
||||
coap_free_packet(packet);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
coap_endpoint_t *coap_new_endpoint(const coap_address_t *addr, int flags) {
|
||||
coap_endpoint_t *
|
||||
coap_new_endpoint(coap_context_t *context, const coap_address_t *addr, coap_proto_t proto) {
|
||||
coap_endpoint_t *result;
|
||||
err_t err;
|
||||
|
||||
LWIP_ASSERT("Flags not supported for LWIP endpoints", flags == COAP_ENDPOINT_NOSEC);
|
||||
LWIP_ASSERT("Proto not supported for LWIP endpoints", proto == COAP_PROTO_UDP);
|
||||
|
||||
result = coap_malloc_type(COAP_ENDPOINT, sizeof(coap_endpoint_t));
|
||||
if (!result) return NULL;
|
||||
|
||||
result->pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
||||
if (result->pcb == NULL) goto error;
|
||||
result->sock.pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
||||
if (result->sock.pcb == NULL) goto error;
|
||||
|
||||
udp_recv(result->pcb, coap_recv, (void*)result);
|
||||
err = udp_bind(result->pcb, &addr->addr, addr->port);
|
||||
udp_recv(result->sock.pcb, coap_recv, (void*)result);
|
||||
err = udp_bind(result->sock.pcb, &addr->addr, addr->port);
|
||||
if (err) {
|
||||
udp_remove(result->pcb);
|
||||
udp_remove(result->sock.pcb);
|
||||
goto error;
|
||||
}
|
||||
|
||||
result->default_mtu = COAP_DEFAULT_MTU;
|
||||
result->context = context;
|
||||
result->proto = proto;
|
||||
|
||||
return result;
|
||||
|
||||
@@ -91,6 +134,90 @@ error:
|
||||
|
||||
void coap_free_endpoint(coap_endpoint_t *ep)
|
||||
{
|
||||
udp_remove(ep->pcb);
|
||||
udp_remove(ep->sock.pcb);
|
||||
coap_free_type(COAP_ENDPOINT, ep);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
coap_socket_send_pdu(coap_socket_t *sock, coap_session_t *session,
|
||||
coap_pdu_t *pdu) {
|
||||
/* FIXME: we can't check this here with the existing infrastructure, but we
|
||||
* should actually check that the pdu is not held by anyone but us. the
|
||||
* respective pbuf is already exclusively owned by the pdu. */
|
||||
|
||||
pbuf_realloc(pdu->pbuf, pdu->used_size + coap_pdu_parse_header_size(session->proto, pdu->pbuf->payload));
|
||||
udp_sendto(sock->pcb, pdu->pbuf, &session->remote_addr.addr,
|
||||
session->remote_addr.port);
|
||||
return pdu->used_size;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
coap_socket_send(coap_socket_t *sock, coap_session_t *session,
|
||||
const uint8_t *data, size_t data_len ) {
|
||||
/* Not implemented, use coap_socket_send_pdu instead */
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
coap_socket_bind_udp(coap_socket_t *sock,
|
||||
const coap_address_t *listen_addr,
|
||||
coap_address_t *bound_addr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
coap_socket_connect_udp(coap_socket_t *sock,
|
||||
const coap_address_t *local_if,
|
||||
const coap_address_t *server,
|
||||
int default_port,
|
||||
coap_address_t *local_addr,
|
||||
coap_address_t *remote_addr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
coap_socket_connect_tcp1(coap_socket_t *sock,
|
||||
const coap_address_t *local_if,
|
||||
const coap_address_t *server,
|
||||
int default_port,
|
||||
coap_address_t *local_addr,
|
||||
coap_address_t *remote_addr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
coap_socket_connect_tcp2(coap_socket_t *sock,
|
||||
coap_address_t *local_addr,
|
||||
coap_address_t *remote_addr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
coap_socket_bind_tcp(coap_socket_t *sock,
|
||||
const coap_address_t *listen_addr,
|
||||
coap_address_t *bound_addr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
coap_socket_accept_tcp(coap_socket_t *server,
|
||||
coap_socket_t *new_client,
|
||||
coap_address_t *local_addr,
|
||||
coap_address_t *remote_addr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void coap_socket_close(coap_socket_t *sock) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "encode.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
void
|
||||
coap_session_set_max_retransmit (coap_session_t *session, unsigned int value) {
|
||||
if (value > 0)
|
||||
@@ -740,6 +741,7 @@ error:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifndef WITH_LWIP
|
||||
coap_endpoint_t *
|
||||
coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
|
||||
struct coap_endpoint_t *ep = NULL;
|
||||
@@ -849,6 +851,7 @@ coap_free_endpoint(coap_endpoint_t *ep) {
|
||||
coap_mfree_endpoint(ep);
|
||||
}
|
||||
}
|
||||
#endif /* WITH_LWIP */
|
||||
|
||||
coap_session_t *
|
||||
coap_session_get_by_peer(coap_context_t *ctx,
|
||||
|
||||
@@ -60,10 +60,10 @@ coap_free_type(coap_memory_tag_t type, void *p) {
|
||||
* fixed-size memory blocks.
|
||||
*/
|
||||
#ifndef COAP_MAX_STRINGS
|
||||
#define COAP_MAX_STRINGS 8
|
||||
#define COAP_MAX_STRINGS 10
|
||||
#endif /* COAP_MAX_STRINGS */
|
||||
|
||||
struct coap_string_t {
|
||||
struct coap_stringbuf_t {
|
||||
char data[COAP_MAX_STRING_SIZE];
|
||||
};
|
||||
|
||||
@@ -75,14 +75,16 @@ struct coap_string_t {
|
||||
#include "coap_session.h"
|
||||
|
||||
#define COAP_MAX_PACKET_SIZE (sizeof(coap_packet_t) + COAP_RXBUFFER_SIZE)
|
||||
#ifndef COAP_MAX_PACKETS
|
||||
#define COAP_MAX_PACKETS 2
|
||||
#endif /* COAP_MAX_PACKETS */
|
||||
|
||||
typedef union {
|
||||
coap_pdu_t packet; /* try to convince the compiler to word-align this structure */
|
||||
char buf[COAP_MAX_PACKET_SIZE];
|
||||
} coap_packetbuf_t;
|
||||
|
||||
MEMB(string_storage, struct coap_string_t, COAP_MAX_STRINGS);
|
||||
MEMB(string_storage, struct coap_stringbuf_t, COAP_MAX_STRINGS);
|
||||
MEMB(packet_storage, coap_packetbuf_t, COAP_MAX_PACKETS);
|
||||
MEMB(session_storage, coap_session_t, COAP_MAX_SESSIONS);
|
||||
MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
|
||||
@@ -121,15 +123,23 @@ coap_memory_init(void) {
|
||||
void *
|
||||
coap_malloc_type(coap_memory_tag_t type, size_t size) {
|
||||
struct memb *container = get_container(type);
|
||||
void *ptr;
|
||||
|
||||
assert(container);
|
||||
|
||||
if (size > container->size) {
|
||||
debug("coap_malloc_type: Requested memory exceeds maximum object size\n");
|
||||
coap_log(LOG_WARNING,
|
||||
"coap_malloc_type: Requested memory exceeds maximum object size (type %d, size %d, max %d)\n",
|
||||
type, (int)size, container->size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return memb_alloc(container);
|
||||
ptr = memb_alloc(container);
|
||||
if (!ptr)
|
||||
coap_log(LOG_WARNING,
|
||||
"coap_malloc_type: Failure (no free blocks) for type %d\n",
|
||||
type);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
#ifdef WITH_LWIP
|
||||
#include <lwip/pbuf.h>
|
||||
#include <lwip/udp.h>
|
||||
#include <lwip/timers.h>
|
||||
#include <lwip/timeouts.h>
|
||||
#endif
|
||||
|
||||
#include "libcoap.h"
|
||||
@@ -641,7 +641,7 @@ static ssize_t
|
||||
coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) {
|
||||
ssize_t bytes_written;
|
||||
|
||||
#ifdef LWIP
|
||||
#ifdef WITH_LWIP
|
||||
|
||||
coap_socket_t *sock = &session->sock;
|
||||
if (sock->flags == COAP_SOCKET_EMPTY) {
|
||||
@@ -653,7 +653,7 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) {
|
||||
|
||||
bytes_written = coap_socket_send_pdu(sock, session, pdu);
|
||||
if (LOG_DEBUG <= coap_get_log_level()) {
|
||||
coap_show_pdu(pdu);
|
||||
coap_show_pdu(LOG_DEBUG, pdu);
|
||||
}
|
||||
coap_ticks(&session->last_rx_tx);
|
||||
|
||||
@@ -976,9 +976,9 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) {
|
||||
|
||||
#ifdef WITH_LWIP
|
||||
/* WITH_LWIP, this is handled by coap_recv in a different way */
|
||||
int
|
||||
coap_read(coap_context_t *ctx) {
|
||||
return -1;
|
||||
void
|
||||
coap_read(coap_context_t *ctx, coap_tick_t now) {
|
||||
return;
|
||||
}
|
||||
#else /* WITH_LWIP */
|
||||
|
||||
@@ -1068,6 +1068,18 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_CONTIKI
|
||||
COAP_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 */
|
||||
|
||||
static void
|
||||
coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) {
|
||||
#ifdef WITH_CONTIKI
|
||||
@@ -1214,6 +1226,10 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t n
|
||||
coap_address_copy(&packet->dst, &endpoint->bind_addr);
|
||||
bytes_read = ctx->network_read(&endpoint->sock, packet);
|
||||
}
|
||||
else {
|
||||
coap_log(LOG_WARNING, "* %s: Packet allocation failed\n", coap_endpoint_str(endpoint));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bytes_read < 0) {
|
||||
warn("* %s: read failed\n", coap_endpoint_str(endpoint));
|
||||
@@ -1289,7 +1305,6 @@ coap_read(coap_context_t *ctx, coap_tick_t now) {
|
||||
coap_write_session(ctx, s, now);
|
||||
}
|
||||
}
|
||||
#endif /* not WITH_LWIP */
|
||||
|
||||
int
|
||||
coap_handle_dgram(coap_context_t *ctx, coap_session_t *session,
|
||||
@@ -1299,11 +1314,7 @@ coap_handle_dgram(coap_context_t *ctx, coap_session_t *session,
|
||||
|
||||
assert(COAP_PROTO_NOT_RELIABLE(session->proto));
|
||||
|
||||
#ifdef WITH_LWIP
|
||||
pdu = coap_pdu_from_pbuf(coap_packet_extract_pbuf(packet));
|
||||
#else
|
||||
pdu = coap_pdu_init(0, 0, 0, msg_len - 4);
|
||||
#endif
|
||||
if (!pdu)
|
||||
goto error;
|
||||
|
||||
@@ -1321,6 +1332,7 @@ error:
|
||||
coap_delete_pdu(pdu);
|
||||
return -1;
|
||||
}
|
||||
#endif /* not WITH_LWIP */
|
||||
|
||||
int
|
||||
coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_tid_t id, coap_queue_t **node) {
|
||||
|
||||
@@ -79,7 +79,7 @@ coap_pdu_from_pbuf( struct pbuf *pbuf )
|
||||
|
||||
pdu->max_hdr_size = COAP_PDU_MAX_UDP_HEADER_SIZE;
|
||||
pdu->pbuf = pbuf;
|
||||
pdu->token = pbuf->payload + pdu->max_hdr_size;
|
||||
pdu->token = (uint8_t *)pbuf->payload + pdu->max_hdr_size;
|
||||
pdu->alloc_size = pbuf->tot_len - pdu->max_hdr_size;
|
||||
coap_pdu_clear(pdu, pdu->alloc_size);
|
||||
|
||||
@@ -90,7 +90,6 @@ coap_pdu_from_pbuf( struct pbuf *pbuf )
|
||||
coap_pdu_t *
|
||||
coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size) {
|
||||
coap_pdu_t *pdu;
|
||||
uint8_t *buf;
|
||||
|
||||
pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
|
||||
if (!pdu) return NULL;
|
||||
@@ -110,8 +109,9 @@ coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size) {
|
||||
coap_free_type(COAP_PDU, pdu);
|
||||
return NULL;
|
||||
}
|
||||
pdu->token = pdu->pbuf->payload + pdu->max_hdr_size;
|
||||
pdu->token = (uint8_t *)pdu->pbuf->payload + pdu->max_hdr_size;
|
||||
#else /* WITH_LWIP */
|
||||
uint8_t *buf;
|
||||
pdu->alloc_size = min(size, 256);
|
||||
buf = coap_malloc_type(COAP_PDU_BUF, pdu->alloc_size + pdu->max_hdr_size);
|
||||
if (buf == NULL) {
|
||||
@@ -153,8 +153,10 @@ coap_delete_pdu(coap_pdu_t *pdu) {
|
||||
int
|
||||
coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
|
||||
if (new_size > pdu->alloc_size) {
|
||||
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
|
||||
uint8_t *new_hdr;
|
||||
size_t offset;
|
||||
#endif
|
||||
if (pdu->max_size && new_size > pdu->max_size) {
|
||||
warn( "coap_pdu_resize: pdu too big\n" );
|
||||
return 0;
|
||||
@@ -565,7 +567,9 @@ coap_pdu_parse(coap_proto_t proto,
|
||||
return 0;
|
||||
if (!coap_pdu_resize(pdu, length - hdr_size))
|
||||
return 0;
|
||||
#ifndef WITH_LWIP
|
||||
memcpy(pdu->token - hdr_size, data, length);
|
||||
#endif
|
||||
pdu->hdr_size = (uint8_t)hdr_size;
|
||||
pdu->used_size = length - hdr_size;
|
||||
return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu);
|
||||
|
||||
Reference in new issue
Block a user