diff --git a/examples/coap/coap-example-client.c b/examples/coap/coap-example-client.c index 27efbbf6e..3fe261cf7 100644 --- a/examples/coap/coap-example-client.c +++ b/examples/coap/coap-example-client.c @@ -92,9 +92,6 @@ PROCESS_THREAD(er_example_client, ev, data) coap_endpoint_parse(SERVER_EP, strlen(SERVER_EP), &server_ep); - /* receives all CoAP messages */ - coap_engine_init(); - etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); #if PLATFORM_HAS_BUTTON diff --git a/examples/coap/coap-example-server.c b/examples/coap/coap-example-server.c index 776ebcd5c..900662324 100644 --- a/examples/coap/coap-example-server.c +++ b/examples/coap/coap-example-server.c @@ -112,15 +112,12 @@ PROCESS_THREAD(er_example_server, ev, data) PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN); PRINTF("CoAP max chunk: %u\n", COAP_MAX_CHUNK_SIZE); - /* Initialize the REST engine. */ - coap_engine_init(); - /* * Bind the resources to their Uri-Path. * WARNING: Activating twice only means alternate path, not two instances! * All static variables are the same for each URI path. */ - coap_activate_resource(&res_hello, "test/hello"); + coap_activate_resource(&res_hello, "test/hello"); coap_activate_resource(&res_mirror, "debug/mirror"); coap_activate_resource(&res_chunks, "test/chunks"); coap_activate_resource(&res_separate, "test/separate"); diff --git a/examples/coap/plugtest-server.c b/examples/coap/plugtest-server.c index b0d08c023..561a70b41 100644 --- a/examples/coap/plugtest-server.c +++ b/examples/coap/plugtest-server.c @@ -87,9 +87,6 @@ PROCESS_THREAD(plugtest_server, ev, data) PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN); PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE); - /* Initialize the REST engine. */ - coap_engine_init(); - /* Activate the application-specific resources. */ coap_activate_resource(&res_plugtest_test, "test"); coap_activate_resource(&res_plugtest_validate, "validate"); diff --git a/examples/ipso-objects/example-server.c b/examples/ipso-objects/example-server.c index 4eac38f69..658c8b24e 100644 --- a/examples/ipso-objects/example-server.c +++ b/examples/ipso-objects/example-server.c @@ -267,9 +267,6 @@ PROCESS_THREAD(router_process, ev, data) PROCESS_PAUSE(); - /* receives all CoAP messages */ - coap_engine_init(); - setup_network(); while(1) { diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c b/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c index 54289f102..37b9a295d 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c +++ b/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c @@ -133,9 +133,6 @@ PROCESS_THREAD(coap_server_process, ev, data) printf("CC26XX CoAP Server\n"); - /* Initialize the REST engine. */ - coap_engine_init(); - coap_activate_resource(&res_batmon_temp, "sen/batmon/temp"); coap_activate_resource(&res_batmon_volt, "sen/batmon/voltage"); diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c b/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c index cac8033b7..ebd55712b 100644 --- a/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c +++ b/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c @@ -344,8 +344,6 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data) event_new_config = process_alloc_event(); - coap_engine_init(); - readings_resource.flags += IS_OBSERVABLE; coap_activate_resource(&readings_resource, "sen/readings"); coap_activate_resource(&very_sleepy_conf, "very_sleepy_config"); diff --git a/examples/platform-specific/jn516x/rpl/coap-dongle-node/dongle-node.c b/examples/platform-specific/jn516x/rpl/coap-dongle-node/dongle-node.c index 2fb2d7761..9c7cbb7aa 100644 --- a/examples/platform-specific/jn516x/rpl/coap-dongle-node/dongle-node.c +++ b/examples/platform-specific/jn516x/rpl/coap-dongle-node/dongle-node.c @@ -135,7 +135,6 @@ PROCESS_THREAD(start_app, ev, data) NETSTACK_MAC.on(); printf("Starting RPL node\n"); - coap_engine_init(); coap_activate_resource(&resource_led_toggle, "Dongle/LED-toggle"); PROCESS_END(); diff --git a/examples/platform-specific/jn516x/rpl/coap-dr1175-node/dr1175-node.c b/examples/platform-specific/jn516x/rpl/coap-dr1175-node/dr1175-node.c index 443cf4fbe..4a8432099 100644 --- a/examples/platform-specific/jn516x/rpl/coap-dr1175-node/dr1175-node.c +++ b/examples/platform-specific/jn516x/rpl/coap-dr1175-node/dr1175-node.c @@ -355,7 +355,6 @@ PROCESS_THREAD(start_app, ev, data) NETSTACK_MAC.on(); printf("Starting RPL node\n"); - coap_engine_init(); coap_activate_resource(&resource_light_sensor_value, "DR1175/LightSensor/Value"); coap_activate_resource(&resource_light_sensor_unit, "DR1175/LightSensor/Unit"); coap_activate_resource(&resource_temperature_unit, "DR1175/Temperature/Unit"); diff --git a/examples/platform-specific/jn516x/rpl/coap-dr1199-node/dr1199-node.c b/examples/platform-specific/jn516x/rpl/coap-dr1199-node/dr1199-node.c index d341170ad..d493636b5 100644 --- a/examples/platform-specific/jn516x/rpl/coap-dr1199-node/dr1199-node.c +++ b/examples/platform-specific/jn516x/rpl/coap-dr1199-node/dr1199-node.c @@ -356,7 +356,6 @@ PROCESS_THREAD(start_app, ev, data) NETSTACK_MAC.on(); printf("Starting RPL node\n"); - coap_engine_init(); coap_activate_resource(&resource_switch_sw1, "DR1199/Switch/SW1"); coap_activate_resource(&resource_switch_sw2, "DR1199/Switch/SW2"); coap_activate_resource(&resource_switch_sw3, "DR1199/Switch/SW3"); diff --git a/examples/platform-specific/jn516x/tsch/tx-power-verification/node/node.c b/examples/platform-specific/jn516x/tsch/tx-power-verification/node/node.c index 995dc3304..2230f4831 100644 --- a/examples/platform-specific/jn516x/tsch/tx-power-verification/node/node.c +++ b/examples/platform-specific/jn516x/tsch/tx-power-verification/node/node.c @@ -110,7 +110,6 @@ PROCESS_THREAD(start_app, ev, data) NETSTACK_MAC.on(); printf("Starting RPL node\n"); - coap_engine_init(); coap_activate_resource(&resource_set_tx_power, "Set-TX-Power"); coap_activate_resource(&resource_get_tx_power, "Get-TX-Power"); diff --git a/examples/platform-specific/jn516x/tsch/uart1-test-node/uart1-test-node.c b/examples/platform-specific/jn516x/tsch/uart1-test-node/uart1-test-node.c index dd647fa21..8759e8307 100644 --- a/examples/platform-specific/jn516x/tsch/uart1-test-node/uart1-test-node.c +++ b/examples/platform-specific/jn516x/tsch/uart1-test-node/uart1-test-node.c @@ -151,7 +151,6 @@ PROCESS_THREAD(start_app, ev, data) NETSTACK_MAC.on(); printf("Starting RPL node\n"); - coap_engine_init(); coap_activate_resource(&resource_coap_rx_uart1, "UART1-RX"); coap_activate_resource(&resource_coap_tx_uart1, "UART1-TX"); diff --git a/examples/platform-specific/nrf52dk/coap-demo/coap-client/coap-client.c b/examples/platform-specific/nrf52dk/coap-demo/coap-client/coap-client.c index 4effcc876..916c06603 100644 --- a/examples/platform-specific/nrf52dk/coap-demo/coap-client/coap-client.c +++ b/examples/platform-specific/nrf52dk/coap-demo/coap-client/coap-client.c @@ -148,9 +148,6 @@ PROCESS_THREAD(er_example_observe_client, ev, data) static coap_endpoint_t server_endpoint; coap_endpoint_parse(SERVER_IPV6_EP, strlen(SERVER_IPV6_EP), &server_endpoint); - /* receives all CoAP messages */ - coap_engine_init(); - #if PLATFORM_HAS_BUTTON SENSORS_ACTIVATE(button_1); SENSORS_ACTIVATE(button_2); diff --git a/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c b/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c index 6b4025cd3..584f54318 100644 --- a/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c +++ b/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c @@ -103,8 +103,6 @@ PROCESS_THREAD(er_example_server, ev, data) print_local_addresses(); - /* Initialize the REST engine. */ - coap_engine_init(); coap_activate_resource(&res_led3, "lights/led3"); SENSORS_ACTIVATE(button_1); diff --git a/os/contiki-main.c b/os/contiki-main.c index 86c50f7ec..8f9c3cd0b 100644 --- a/os/contiki-main.c +++ b/os/contiki-main.c @@ -47,6 +47,7 @@ #include "sys/stack-check.h" #include "dev/watchdog.h" +#include "net/app-layer/coap/coap-engine.h" #include "services/rpl-border-router/rpl-border-router.h" #include "services/orchestra/orchestra.h" #include "services/shell/serial-shell.h" @@ -132,6 +133,11 @@ main(void) LOG_DBG("With Shell\n"); #endif /* BUILD_WITH_SHELL */ +#if BUILD_WITH_COAP + coap_engine_init(); + LOG_DBG("With CoAP\n"); +#endif /* BUILD_WITH_SHELL */ + autostart_start(autostart_processes); watchdog_start(); diff --git a/os/net/app-layer/coap/coap-engine.c b/os/net/app-layer/coap/coap-engine.c index fb85b5b15..816393ec0 100644 --- a/os/net/app-layer/coap/coap-engine.c +++ b/os/net/app-layer/coap/coap-engine.c @@ -363,7 +363,6 @@ coap_engine_init(void) { /* avoid initializing twice */ if(is_initialized) { - LOG_DBG("already running - double initialization?\n"); return; } is_initialized = 1; diff --git a/os/net/app-layer/coap/coap.c b/os/net/app-layer/coap/coap.c index 42a2d5cef..c28e2d8b5 100644 --- a/os/net/app-layer/coap/coap.c +++ b/os/net/app-layer/coap/coap.c @@ -1115,13 +1115,10 @@ coap_set_header_size1(coap_message_t *coap_pkt, uint32_t size) int coap_get_payload(coap_message_t *coap_pkt, const uint8_t **payload) { - if(coap_pkt->payload) { + if(payload != NULL) { *payload = coap_pkt->payload; - return coap_pkt->payload_len; - } else { - *payload = NULL; - return 0; } + return coap_pkt->payload != NULL ? coap_pkt->payload_len : 0; } int coap_set_payload(coap_message_t *coap_pkt, const void *payload, size_t length) diff --git a/os/net/app-layer/coap/module-macros.h b/os/net/app-layer/coap/module-macros.h new file mode 100644 index 000000000..053040e50 --- /dev/null +++ b/os/net/app-layer/coap/module-macros.h @@ -0,0 +1 @@ +#define BUILD_WITH_COAP 1 diff --git a/os/services/lwm2m/lwm2m-engine.c b/os/services/lwm2m/lwm2m-engine.c index b96674215..d40154bfa 100644 --- a/os/services/lwm2m/lwm2m-engine.c +++ b/os/services/lwm2m/lwm2m-engine.c @@ -543,6 +543,9 @@ lwm2m_engine_init(void) #endif /* LWM2M_ENGINE_CLIENT_ENDPOINT_NAME */ + /* Initialize CoAP engine. Contiki-NG already does that from the main, + * but for standalone use of lwm2m, this is required here. coap_engine_init() + * checks for double-initialization and can be called twice safely. */ coap_engine_init(); /* Register the CoAP handler for lightweight object handling */