mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
coap: Update examples to use latest features of libcoap component
Support libcoap build with Client Only or Server Only code.
This commit is contained in:
parent
36f49f361c
commit
98d346a81e
@ -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.
|
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
|
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with
|
||||||
constrained nodes and constrained networks in the Internet of Things.
|
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
|
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 --->
|
Example Connection Configuration --->
|
||||||
* Set WiFi SSID under Example Configuration
|
* Set WiFi SSID
|
||||||
* Set WiFi Password under Example Configuration
|
* Set WiFi Password
|
||||||
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)
|
|
||||||
Component config --->
|
Component config --->
|
||||||
CoAP Configuration --->
|
CoAP Configuration --->
|
||||||
* Set encryption method definition, PSK (default) or PKI
|
* Set encryption method definition, PSK (default) or PKI
|
||||||
* Enable CoAP debugging if required
|
* Enable CoAP debugging if required
|
||||||
High resolution timer (esp_timer) --->
|
* Disable CoAP using TCP if this is not required (TCP needed for TLS)
|
||||||
* Hardware timer to use for esp_timer - change if required (FRC2 for QEMU)
|
* 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
|
### Build and Flash
|
||||||
|
|
||||||
@ -103,8 +101,9 @@ These can be raised at [libcoap Issues](https://github.com/obgm/libcoap/issues).
|
|||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
* Please make sure Target Url includes valid `host`, optional `port`,
|
* Please make sure Target Url includes valid `host`, optional `port`,
|
||||||
optional `path`, and begins with `coap://`, `coaps://` or `coap+tcp://`
|
optional `path`, and begins with `coap://`, `coaps://`, `coap+tcp://` or `coaps+tcp://`
|
||||||
for a coap server that supports TCP
|
(not all hosts support TCP/TLS including coap+tcp://californium.eclipseprojects.io).
|
||||||
(not all do 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.
|
||||||
|
@ -33,6 +33,11 @@
|
|||||||
|
|
||||||
#include "coap3/coap.h"
|
#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
|
#define COAP_DEFAULT_TIME_SEC 60
|
||||||
|
|
||||||
/* The examples use simple Pre-Shared-Key configuration that you can set via
|
/* 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;
|
optlist = NULL;
|
||||||
|
|
||||||
if (uri->scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
if (uri->scheme == COAP_URI_SCHEME_COAPS_TCP && !coap_tls_is_supported()) {
|
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;
|
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
|
* Note that if the URI starts with just coap:// (not coaps://) the
|
||||||
* session will still be plain text.
|
* 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) {
|
if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP) {
|
||||||
#ifndef CONFIG_MBEDTLS_TLS_CLIENT
|
#ifndef CONFIG_MBEDTLS_TLS_CLIENT
|
||||||
|
@ -2,3 +2,4 @@ CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
|
|||||||
CONFIG_MBEDTLS_PSK_MODES=y
|
CONFIG_MBEDTLS_PSK_MODES=y
|
||||||
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y
|
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y
|
||||||
CONFIG_LWIP_NETBUF_RECVINFO=y
|
CONFIG_LWIP_NETBUF_RECVINFO=y
|
||||||
|
CONFIG_COAP_CLIENT_SUPPORT=y
|
||||||
|
@ -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
|
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.
|
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
|
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with
|
||||||
constrained nodes and constrained networks in the Internet of Things.
|
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
|
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 --->
|
Example Connection Configuration --->
|
||||||
* Set WiFi SSID under Example Configuration
|
* Set WiFi SSID
|
||||||
* Set WiFi Password under Example Configuration
|
* Set WiFi Password
|
||||||
Example CoAP Client Configuration --->
|
|
||||||
* If PSK, Set CoAP Preshared Key to use in connection to the server
|
|
||||||
Component config --->
|
Component config --->
|
||||||
CoAP Configuration --->
|
CoAP Configuration --->
|
||||||
* Set encryption method definition, PSK (default) or PKI
|
* Set encryption method definition, PSK (default) or PKI
|
||||||
* Enable CoAP debugging if required
|
* Enable CoAP debugging if required
|
||||||
High resolution timer (esp_timer) --->
|
* Disable CoAP using TCP if this is not required (TCP needed for TLS)
|
||||||
* Hardware timer to use for esp_timer - change if required (FRC2 for QEMU)
|
* 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
|
### 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
|
* Please make sure CoAP client fetchs or puts data under path: `/Espressif` or
|
||||||
fetches `/.well-known/core`
|
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.
|
||||||
|
@ -31,6 +31,10 @@
|
|||||||
|
|
||||||
#include "coap3/coap.h"
|
#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
|
/* The examples use simple Pre-Shared-Key configuration that you can set via
|
||||||
'idf.py menuconfig'.
|
'idf.py menuconfig'.
|
||||||
|
|
||||||
@ -187,6 +191,7 @@ static void coap_example_server(void *p)
|
|||||||
while (1) {
|
while (1) {
|
||||||
coap_endpoint_t *ep = NULL;
|
coap_endpoint_t *ep = NULL;
|
||||||
unsigned wait_ms;
|
unsigned wait_ms;
|
||||||
|
int have_dtls = 0;
|
||||||
|
|
||||||
/* Prepare the CoAP server socket */
|
/* Prepare the CoAP server socket */
|
||||||
coap_address_init(&serv_addr);
|
coap_address_init(&serv_addr);
|
||||||
@ -258,16 +263,18 @@ static void coap_example_server(void *p)
|
|||||||
ESP_LOGE(TAG, "udp: coap_new_endpoint() failed");
|
ESP_LOGE(TAG, "udp: coap_new_endpoint() failed");
|
||||||
goto clean_up;
|
goto clean_up;
|
||||||
}
|
}
|
||||||
ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP);
|
if (coap_tcp_is_supported()) {
|
||||||
if (!ep) {
|
ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP);
|
||||||
ESP_LOGE(TAG, "tcp: coap_new_endpoint() failed");
|
if (!ep) {
|
||||||
goto clean_up;
|
ESP_LOGE(TAG, "tcp: coap_new_endpoint() failed");
|
||||||
|
goto clean_up;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#if defined(CONFIG_COAP_MBEDTLS_PSK) || defined(CONFIG_COAP_MBEDTLS_PKI)
|
#if defined(CONFIG_COAP_MBEDTLS_PSK) || defined(CONFIG_COAP_MBEDTLS_PKI)
|
||||||
if (coap_dtls_is_supported()) {
|
if (coap_dtls_is_supported()) {
|
||||||
#ifndef CONFIG_MBEDTLS_TLS_SERVER
|
#ifndef CONFIG_MBEDTLS_TLS_SERVER
|
||||||
/* This is not critical as unencrypted support is still available */
|
/* 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 */
|
#else /* CONFIG_MBEDTLS_TLS_SERVER */
|
||||||
serv_addr.addr.sin6.sin6_port = htons(COAPS_DEFAULT_PORT);
|
serv_addr.addr.sin6.sin6_port = htons(COAPS_DEFAULT_PORT);
|
||||||
ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_DTLS);
|
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");
|
ESP_LOGE(TAG, "dtls: coap_new_endpoint() failed");
|
||||||
goto clean_up;
|
goto clean_up;
|
||||||
}
|
}
|
||||||
|
have_dtls = 1;
|
||||||
#endif /* CONFIG_MBEDTLS_TLS_SERVER */
|
#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 */
|
/* This is not critical as unencrypted support is still available */
|
||||||
ESP_LOGI(TAG, "MbedTLS (D)TLS Server Mode not configured");
|
ESP_LOGI(TAG, "MbedTLS (D)TLS Server Mode not configured");
|
||||||
}
|
}
|
||||||
|
@ -2,3 +2,4 @@ CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
|
|||||||
CONFIG_MBEDTLS_PSK_MODES=y
|
CONFIG_MBEDTLS_PSK_MODES=y
|
||||||
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y
|
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y
|
||||||
CONFIG_LWIP_NETBUF_RECVINFO=y
|
CONFIG_LWIP_NETBUF_RECVINFO=y
|
||||||
|
CONFIG_COAP_SERVER_SUPPORT=y
|
||||||
|
Loading…
x
Reference in New Issue
Block a user