From 98d346a81ede3341a15b15fd4dd865da315791bd Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 27 Jul 2022 10:47:46 +0000 Subject: [PATCH] coap: Update examples to use latest features of libcoap component Support libcoap build with Client Only or Server Only code. --- examples/protocols/coap_client/README.md | 27 +++++++-------- .../main/coap_client_example_main.c | 17 ++++++---- .../protocols/coap_client/sdkconfig.defaults | 1 + examples/protocols/coap_server/README.md | 20 +++++------ .../main/coap_server_example_main.c | 34 +++++++++++++++---- .../protocols/coap_server/sdkconfig.defaults | 1 + 6 files changed, 63 insertions(+), 37 deletions(-) diff --git a/examples/protocols/coap_client/README.md b/examples/protocols/coap_client/README.md index ab8c9eadc4..6a3ea197d5 100644 --- a/examples/protocols/coap_client/README.md +++ b/examples/protocols/coap_client/README.md @@ -18,8 +18,6 @@ CoAP server needs to know about. If the URI is prefixed with coap+tcp://, then the CoAP will try to use TCP for the communication. -NOTE: coaps+tcp:// is not currently supported, even though both libcoap and MbedTLS support it. - The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and @@ -36,18 +34,18 @@ idf.py menuconfig ``` Example Connection Configuration ---> - * Set WiFi SSID under Example Configuration - * Set WiFi Password under Example Configuration -Example CoAP Client Configuration ---> - * Set CoAP Target Uri - * If PSK, Set CoAP Preshared Key to use in connection to the server - * If PSK, Set CoAP PSK Client identity (username) + * Set WiFi SSID + * Set WiFi Password Component config ---> CoAP Configuration ---> * Set encryption method definition, PSK (default) or PKI * Enable CoAP debugging if required - High resolution timer (esp_timer) ---> - * Hardware timer to use for esp_timer - change if required (FRC2 for QEMU) + * Disable CoAP using TCP if this is not required (TCP needed for TLS) + * Disable CoAP server functionality to reduce code size +Example CoAP Client Configuration ---> + * Set CoAP Target Uri + * If PSK, Set CoAP Preshared Key to use in connection to the server + * If PSK, Set CoAP PSK Client identity (username) ### Build and Flash @@ -103,8 +101,9 @@ These can be raised at [libcoap Issues](https://github.com/obgm/libcoap/issues). ## Troubleshooting * Please make sure Target Url includes valid `host`, optional `port`, -optional `path`, and begins with `coap://`, `coaps://` or `coap+tcp://` -for a coap server that supports TCP -(not all do including coap+tcp://californium.eclipseprojects.io). +optional `path`, and begins with `coap://`, `coaps://`, `coap+tcp://` or `coaps+tcp://` +(not all hosts support TCP/TLS including coap+tcp://californium.eclipseprojects.io). -* CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration' and setting appropriate log level +* CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration -> Enable CoAP debugging' +and setting appropriate log level. If Mbed TLS logging is required, this needs to be configured separately under mbedTLS +Component Configuration and the CoAP logging level set to mbedTLS. diff --git a/examples/protocols/coap_client/main/coap_client_example_main.c b/examples/protocols/coap_client/main/coap_client_example_main.c index 6b071f573c..3d75b58532 100644 --- a/examples/protocols/coap_client/main/coap_client_example_main.c +++ b/examples/protocols/coap_client/main/coap_client_example_main.c @@ -33,6 +33,11 @@ #include "coap3/coap.h" + +#ifndef CONFIG_COAP_CLIENT_SUPPORT +#error COAP_CLIENT_SUPPORT needs to be enabled +#endif /* COAP_CLIENT_SUPPORT */ + #define COAP_DEFAULT_TIME_SEC 60 /* The examples use simple Pre-Shared-Key configuration that you can set via @@ -224,11 +229,15 @@ coap_build_optlist(coap_uri_t *uri) optlist = NULL; if (uri->scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) { - ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); + ESP_LOGE(TAG, "MbedTLS DTLS Client Mode not configured"); return 0; } if (uri->scheme == COAP_URI_SCHEME_COAPS_TCP && !coap_tls_is_supported()) { - ESP_LOGE(TAG, "CoAP server uri->+tcp:// scheme is not supported"); + ESP_LOGE(TAG, "MbedTLS TLS Client Mode not configured"); + return 0; + } + if (uri->scheme == COAP_URI_SCHEME_COAP_TCP && !coap_tcp_is_supported()) { + ESP_LOGE(TAG, "TCP Client Mode not configured"); return 0; } @@ -389,10 +398,6 @@ static void coap_example_client(void *p) /* * Note that if the URI starts with just coap:// (not coaps://) the * session will still be plain text. - * - * coaps+tcp:// is NOT yet supported by the libcoap->mbedtls interface - * so COAP_URI_SCHEME_COAPS_TCP will have failed in a test above, - * but the code is left in for completeness. */ if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP) { #ifndef CONFIG_MBEDTLS_TLS_CLIENT diff --git a/examples/protocols/coap_client/sdkconfig.defaults b/examples/protocols/coap_client/sdkconfig.defaults index 308ca1c49a..2cec21be75 100644 --- a/examples/protocols/coap_client/sdkconfig.defaults +++ b/examples/protocols/coap_client/sdkconfig.defaults @@ -2,3 +2,4 @@ CONFIG_MBEDTLS_SSL_PROTO_DTLS=y CONFIG_MBEDTLS_PSK_MODES=y CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y CONFIG_LWIP_NETBUF_RECVINFO=y +CONFIG_COAP_CLIENT_SUPPORT=y diff --git a/examples/protocols/coap_server/README.md b/examples/protocols/coap_server/README.md index 2e82efe026..8fc63f5b5b 100644 --- a/examples/protocols/coap_server/README.md +++ b/examples/protocols/coap_server/README.md @@ -16,9 +16,6 @@ try to establish a DTLS session using the previously defined Pre-Shared Key (PSK must be the same as the one that the CoAP client is using, or Public Key Infrastructure (PKI) where the PKI information must match as requested. -NOTE: Client sessions trying to use coaps+tcp:// are not currently supported, even though both -libcoap and MbedTLS support it. - The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and @@ -35,17 +32,16 @@ idf.py menuconfig ``` Example Connection Configuration ---> - * Set WiFi SSID under Example Configuration - * Set WiFi Password under Example Configuration -Example CoAP Client Configuration ---> - * If PSK, Set CoAP Preshared Key to use in connection to the server + * Set WiFi SSID + * Set WiFi Password Component config ---> CoAP Configuration ---> * Set encryption method definition, PSK (default) or PKI * Enable CoAP debugging if required - High resolution timer (esp_timer) ---> - * Hardware timer to use for esp_timer - change if required (FRC2 for QEMU) - + * Disable CoAP using TCP if this is not required (TCP needed for TLS) + * Disable CoAP client functionality to reduce code size unless this server is a proxy +Example CoAP Server Configuration ---> + * If PSK, Set CoAP Preshared Key to use for connections to the server ### Build and Flash @@ -93,4 +89,6 @@ These can be raised at [libcoap Issues](https://github.com/obgm/libcoap/issues). * Please make sure CoAP client fetchs or puts data under path: `/Espressif` or fetches `/.well-known/core` -* CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration' and setting appropriate log level +* CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration -> Enable CoAP debugging' +and setting appropriate log level. If Mbed TLS logging is required, this needs to be configured separately under mbedTLS +Component Configuration and the CoAP logging level set to mbedTLS. diff --git a/examples/protocols/coap_server/main/coap_server_example_main.c b/examples/protocols/coap_server/main/coap_server_example_main.c index ed96cc6e21..66ea76f61f 100644 --- a/examples/protocols/coap_server/main/coap_server_example_main.c +++ b/examples/protocols/coap_server/main/coap_server_example_main.c @@ -31,6 +31,10 @@ #include "coap3/coap.h" +#ifndef CONFIG_COAP_SERVER_SUPPORT +#error COAP_SERVER_SUPPORT needs to be enabled +#endif /* COAP_SERVER_SUPPORT */ + /* The examples use simple Pre-Shared-Key configuration that you can set via 'idf.py menuconfig'. @@ -187,6 +191,7 @@ static void coap_example_server(void *p) while (1) { coap_endpoint_t *ep = NULL; unsigned wait_ms; + int have_dtls = 0; /* Prepare the CoAP server socket */ coap_address_init(&serv_addr); @@ -258,16 +263,18 @@ static void coap_example_server(void *p) ESP_LOGE(TAG, "udp: coap_new_endpoint() failed"); goto clean_up; } - ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP); - if (!ep) { - ESP_LOGE(TAG, "tcp: coap_new_endpoint() failed"); - goto clean_up; + if (coap_tcp_is_supported()) { + ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP); + if (!ep) { + ESP_LOGE(TAG, "tcp: coap_new_endpoint() failed"); + goto clean_up; + } } #if defined(CONFIG_COAP_MBEDTLS_PSK) || defined(CONFIG_COAP_MBEDTLS_PKI) if (coap_dtls_is_supported()) { #ifndef CONFIG_MBEDTLS_TLS_SERVER /* This is not critical as unencrypted support is still available */ - ESP_LOGI(TAG, "MbedTLS (D)TLS Server Mode not configured"); + ESP_LOGI(TAG, "MbedTLS DTLS Server Mode not configured"); #else /* CONFIG_MBEDTLS_TLS_SERVER */ serv_addr.addr.sin6.sin6_port = htons(COAPS_DEFAULT_PORT); ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_DTLS); @@ -275,8 +282,23 @@ static void coap_example_server(void *p) ESP_LOGE(TAG, "dtls: coap_new_endpoint() failed"); goto clean_up; } + have_dtls = 1; #endif /* CONFIG_MBEDTLS_TLS_SERVER */ - } else { + } + if (coap_tls_is_supported()) { +#ifndef CONFIG_MBEDTLS_TLS_SERVER + /* This is not critical as unencrypted support is still available */ + ESP_LOGI(TAG, "MbedTLS TLS Server Mode not configured"); +#else /* CONFIG_MBEDTLS_TLS_SERVER */ + serv_addr.addr.sin6.sin6_port = htons(COAPS_DEFAULT_PORT); + ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TLS); + if (!ep) { + ESP_LOGE(TAG, "tls: coap_new_endpoint() failed"); + goto clean_up; + } +#endif /* CONFIG_MBEDTLS_TLS_SERVER */ + } + if (!have_dtls) { /* This is not critical as unencrypted support is still available */ ESP_LOGI(TAG, "MbedTLS (D)TLS Server Mode not configured"); } diff --git a/examples/protocols/coap_server/sdkconfig.defaults b/examples/protocols/coap_server/sdkconfig.defaults index 308ca1c49a..8bc64e0867 100644 --- a/examples/protocols/coap_server/sdkconfig.defaults +++ b/examples/protocols/coap_server/sdkconfig.defaults @@ -2,3 +2,4 @@ CONFIG_MBEDTLS_SSL_PROTO_DTLS=y CONFIG_MBEDTLS_PSK_MODES=y CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y CONFIG_LWIP_NETBUF_RECVINFO=y +CONFIG_COAP_SERVER_SUPPORT=y