From 77e505e20a3b7bda86f2b6be97895ed2e57e5ef8 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Mon, 4 Sep 2023 14:41:46 +0300 Subject: [PATCH 1/5] Add coap support Signed-off-by: 1998-felix --- targets/esp32/coap/.gitignore | 3 + targets/esp32/coap/CMakeLists.txt | 6 + targets/esp32/coap/README.md | 0 targets/esp32/coap/platformio.ini | 14 ++ targets/esp32/coap/src/CMakeLists.txt | 6 + targets/esp32/coap/src/idf_component.yml | 5 + targets/esp32/coap/src/main.c | 290 +++++++++++++++++++++++ 7 files changed, 324 insertions(+) create mode 100644 targets/esp32/coap/.gitignore create mode 100644 targets/esp32/coap/CMakeLists.txt create mode 100644 targets/esp32/coap/README.md create mode 100644 targets/esp32/coap/platformio.ini create mode 100644 targets/esp32/coap/src/CMakeLists.txt create mode 100644 targets/esp32/coap/src/idf_component.yml create mode 100644 targets/esp32/coap/src/main.c diff --git a/targets/esp32/coap/.gitignore b/targets/esp32/coap/.gitignore new file mode 100644 index 0000000..469705b --- /dev/null +++ b/targets/esp32/coap/.gitignore @@ -0,0 +1,3 @@ +.pio +.vscode +sdkconfig.esp32doit-devkit-v1 diff --git a/targets/esp32/coap/CMakeLists.txt b/targets/esp32/coap/CMakeLists.txt new file mode 100644 index 0000000..e776a83 --- /dev/null +++ b/targets/esp32/coap/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(coap_client) diff --git a/targets/esp32/coap/README.md b/targets/esp32/coap/README.md new file mode 100644 index 0000000..e69de29 diff --git a/targets/esp32/coap/platformio.ini b/targets/esp32/coap/platformio.ini new file mode 100644 index 0000000..6bcbd9b --- /dev/null +++ b/targets/esp32/coap/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = espidf diff --git a/targets/esp32/coap/src/CMakeLists.txt b/targets/esp32/coap/src/CMakeLists.txt new file mode 100644 index 0000000..ab3ad38 --- /dev/null +++ b/targets/esp32/coap/src/CMakeLists.txt @@ -0,0 +1,6 @@ +# This file was automatically generated for projects +# without default 'CMakeLists.txt' file. + +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources}) diff --git a/targets/esp32/coap/src/idf_component.yml b/targets/esp32/coap/src/idf_component.yml new file mode 100644 index 0000000..d99d1f9 --- /dev/null +++ b/targets/esp32/coap/src/idf_component.yml @@ -0,0 +1,5 @@ +dependencies: + espressif/coap: + version: ^4.3.0 +description: CoAP Client +version: 1.0.0 diff --git a/targets/esp32/coap/src/main.c b/targets/esp32/coap/src/main.c new file mode 100644 index 0000000..04810a7 --- /dev/null +++ b/targets/esp32/coap/src/main.c @@ -0,0 +1,290 @@ +#include +#include +#include +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_log.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "nvs_flash.h" + +#include "protocol_examples_common.h" + +#include "coap3/coap.h" + + +#ifndef CONFIG_COAP_CLIENT_SUPPORT +#error COAP_CLIENT_SUPPORT needs to be enabled +#endif + +#define COAP_DEFAULT_TIME_SEC 60 +e EXAMPLE_COAP_PSK_KEY CONFIG_EXAMPLE_COAP_PSK_KEY +#define EXAMPLE_COAP_PSK_IDENTITY CONFIG_EXAMPLE_COAP_PSK_IDENTITY +#define EXAMPLE_COAP_LOG_DEFAULT_LEVEL CONFIG_COAP_LOG_DEFAULT_LEVEL +#define COAP_DEFAULT_DEMO_URI CONFIG_EXAMPLE_TARGET_DOMAIN_URI + +const static char *TAG = "CoAP_client"; + +static int resp_wait = 1; +static coap_optlist_t *optlist = NULL; +static int wait_ms; + +static coap_response_t +message_handler(coap_session_t *session, + const coap_pdu_t *sent, + const coap_pdu_t *received, + const coap_mid_t mid) +{ + const unsigned char *data = NULL; + size_t data_len; + size_t offset; + size_t total; + coap_pdu_code_t rcvd_code = coap_pdu_get_code(received); + + if (COAP_RESPONSE_CLASS(rcvd_code) == 2) { + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { + if (data_len != total) { + printf("Unexpected partial data received offset %u, length %u\n", offset, data_len); + } + printf("Received:\n%.*s\n", (int)data_len, data); + resp_wait = 0; + } + return COAP_RESPONSE_OK; + } + printf("%d.%02d", (rcvd_code >> 5), rcvd_code & 0x1F); + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { + printf(": "); + while (data_len--) { + printf("%c", isprint(*data) ? *data : '.'); + data++; + } + } + printf("\n"); + resp_wait = 0; + return COAP_RESPONSE_OK; +} + +static void +coap_log_handler (coap_log_t level, const char *message) +{ + uint32_t esp_level = ESP_LOG_INFO; + const char *cp = strchr(message, '\n'); + + while (cp) { + ESP_LOG_LEVEL(esp_level, TAG, "%.*s", (int)(cp - message), message); + message = cp + 1; + cp = strchr(message, '\n'); + } + if (message[0] != '\000') { + ESP_LOG_LEVEL(esp_level, TAG, "%s", message); + } +} + +static int +coap_build_optlist(coap_uri_t *uri) +{ +#define BUFSIZE 40 + unsigned char _buf[BUFSIZE]; + unsigned char *buf; + size_t buflen; + int res; + + optlist = NULL; + + if (uri->scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) { + 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, "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; + } + + if (uri->path.length) { + buflen = BUFSIZE; + buf = _buf; + res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); + + while (res--) { + coap_insert_optlist(&optlist, + coap_new_optlist(COAP_OPTION_URI_PATH, + coap_opt_length(buf), + coap_opt_value(buf))); + + buf += coap_opt_size(buf); + } + } + + if (uri->query.length) { + buflen = BUFSIZE; + buf = _buf; + res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); + + while (res--) { + coap_insert_optlist(&optlist, + coap_new_optlist(COAP_OPTION_URI_QUERY, + coap_opt_length(buf), + coap_opt_value(buf))); + + buf += coap_opt_size(buf); + } + } + return 1; +} + +static void coap_example_client(void *p) +{ + coap_address_t dst_addr; + static coap_uri_t uri; + const char *server_uri = COAP_DEFAULT_DEMO_URI; + coap_context_t *ctx = NULL; + coap_session_t *session = NULL; + coap_pdu_t *request = NULL; + unsigned char token[8]; + size_t tokenlength; + coap_addr_info_t *info_list = NULL; + coap_proto_t proto; + char tmpbuf[INET6_ADDRSTRLEN]; + + /* Initialize libcoap library */ + coap_startup(); + + /* Set up the CoAP logging */ + coap_set_log_handler(coap_log_handler); + coap_set_log_level(EXAMPLE_COAP_LOG_DEFAULT_LEVEL); + + /* Set up the CoAP context */ + ctx = coap_new_context(NULL); + if (!ctx) { + ESP_LOGE(TAG, "coap_new_context() failed"); + goto clean_up; + } + coap_context_set_block_mode(ctx, + COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); + + coap_register_response_handler(ctx, message_handler); + + if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) { + ESP_LOGE(TAG, "CoAP server uri %s error", server_uri); + goto clean_up; + } + if (!coap_build_optlist(&uri)) { + goto clean_up; + } + + info_list = coap_resolve_address_info(&uri.host, uri.port, uri.port, + uri.port, uri.port, + 0, + 1 << uri.scheme, + COAP_RESOLVE_TYPE_REMOTE); + + if (info_list == NULL) { + ESP_LOGE(TAG, "failed to resolve address"); + goto clean_up; + } + proto = info_list->proto; + memcpy(&dst_addr, &info_list->addr, sizeof(dst_addr)); + coap_free_address_info(info_list); + + /* This is to keep the test suites happy */ + coap_print_ip_addr(&dst_addr, tmpbuf, sizeof(tmpbuf)); + ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", tmpbuf); + + /* + * Note that if the URI starts with just coap:// (not coaps://) the + * session will still be plain text. + */ + if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) { +#ifndef CONFIG_MBEDTLS_TLS_CLIENT + ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); + goto clean_up; +#endif /* CONFIG_MBEDTLS_TLS_CLIENT */ + +#ifdef CONFIG_COAP_MBEDTLS_PSK + session = coap_start_psk_session(ctx, &dst_addr, &uri, proto); +#endif /* CONFIG_COAP_MBEDTLS_PSK */ + +#ifdef CONFIG_COAP_MBEDTLS_PKI + session = coap_start_pki_session(ctx, &dst_addr, &uri, proto); +#endif /* CONFIG_COAP_MBEDTLS_PKI */ + } else { + session = coap_new_client_session(ctx, NULL, &dst_addr, proto); + } + if (!session) { + ESP_LOGE(TAG, "coap_new_client_session() failed"); + goto clean_up; + } +#ifdef CONFIG_COAP_WEBSOCKETS + if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) { + coap_ws_set_host_request(session, &uri.host); + } +#endif /* CONFIG_COAP_WEBSOCKETS */ + + while (1) { + request = coap_new_pdu(coap_is_mcast(&dst_addr) ? COAP_MESSAGE_NON : COAP_MESSAGE_CON, + COAP_REQUEST_CODE_GET, session); + if (!request) { + ESP_LOGE(TAG, "coap_new_pdu() failed"); + goto clean_up; + } + /* Add in an unique token */ + coap_session_new_token(session, &tokenlength, token); + coap_add_token(request, tokenlength, token); + coap_add_optlist_pdu(request, &optlist); + + resp_wait = 1; + coap_send(session, request); + + wait_ms = COAP_DEFAULT_TIME_SEC * 1000; + + while (resp_wait) { + int result = coap_io_process(ctx, wait_ms > 1000 ? 1000 : wait_ms); + if (result >= 0) { + if (result >= wait_ms) { + ESP_LOGE(TAG, "No response from server"); + break; + } else { + wait_ms -= result; + } + } + } + for (int countdown = 10; countdown >= 0; countdown--) { + ESP_LOGI(TAG, "%d... ", countdown); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + ESP_LOGI(TAG, "Starting again!"); + } + +clean_up: + if (optlist) { + coap_delete_optlist(optlist); + optlist = NULL; + } + if (session) { + coap_session_release(session); + } + if (ctx) { + coap_free_context(ctx); + } + coap_cleanup(); + + ESP_LOGI(TAG, "Finished"); + vTaskDelete(NULL); +} + +void app_main(void) +{ + ESP_ERROR_CHECK( nvs_flash_init() ); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(example_connect()); + + xTaskCreate(coap_example_client, "coap", 8 * 1024, NULL, 5, NULL); +} From 8e1b68f1d933ed465cb7afcbce4f447f2489c544 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Wed, 6 Sep 2023 13:16:13 +0300 Subject: [PATCH 2/5] Add config file Signed-off-by: 1998-felix --- targets/esp32/coap/include/config.h | 13 +++ targets/esp32/coap/src/main.c | 128 ++++++++++++++++------------ 2 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 targets/esp32/coap/include/config.h diff --git a/targets/esp32/coap/include/config.h b/targets/esp32/coap/include/config.h new file mode 100644 index 0000000..10354bf --- /dev/null +++ b/targets/esp32/coap/include/config.h @@ -0,0 +1,13 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define CLIENTID "" + +const char *mfThingId = " "; +const char *mfThingKey = " "; +const char *mfChannelId = " "; +char mfTopic[150]; + +const char *server = " "; + +#endif \ No newline at end of file diff --git a/targets/esp32/coap/src/main.c b/targets/esp32/coap/src/main.c index 04810a7..3535dc2 100644 --- a/targets/esp32/coap/src/main.c +++ b/targets/esp32/coap/src/main.c @@ -15,16 +15,7 @@ #include "coap3/coap.h" - -#ifndef CONFIG_COAP_CLIENT_SUPPORT -#error COAP_CLIENT_SUPPORT needs to be enabled -#endif - #define COAP_DEFAULT_TIME_SEC 60 -e EXAMPLE_COAP_PSK_KEY CONFIG_EXAMPLE_COAP_PSK_KEY -#define EXAMPLE_COAP_PSK_IDENTITY CONFIG_EXAMPLE_COAP_PSK_IDENTITY -#define EXAMPLE_COAP_LOG_DEFAULT_LEVEL CONFIG_COAP_LOG_DEFAULT_LEVEL -#define COAP_DEFAULT_DEMO_URI CONFIG_EXAMPLE_TARGET_DOMAIN_URI const static char *TAG = "CoAP_client"; @@ -32,11 +23,10 @@ static int resp_wait = 1; static coap_optlist_t *optlist = NULL; static int wait_ms; -static coap_response_t -message_handler(coap_session_t *session, - const coap_pdu_t *sent, - const coap_pdu_t *received, - const coap_mid_t mid) +static coap_response_t message_handler(coap_session_t *session, + const coap_pdu_t *sent, + const coap_pdu_t *received, + const coap_mid_t mid) { const unsigned char *data = NULL; size_t data_len; @@ -44,9 +34,12 @@ message_handler(coap_session_t *session, size_t total; coap_pdu_code_t rcvd_code = coap_pdu_get_code(received); - if (COAP_RESPONSE_CLASS(rcvd_code) == 2) { - if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { - if (data_len != total) { + if (COAP_RESPONSE_CLASS(rcvd_code) == 2) + { + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) + { + if (data_len != total) + { printf("Unexpected partial data received offset %u, length %u\n", offset, data_len); } printf("Received:\n%.*s\n", (int)data_len, data); @@ -55,9 +48,11 @@ message_handler(coap_session_t *session, return COAP_RESPONSE_OK; } printf("%d.%02d", (rcvd_code >> 5), rcvd_code & 0x1F); - if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) + { printf(": "); - while (data_len--) { + while (data_len--) + { printf("%c", isprint(*data) ? *data : '.'); data++; } @@ -67,24 +62,24 @@ message_handler(coap_session_t *session, return COAP_RESPONSE_OK; } -static void -coap_log_handler (coap_log_t level, const char *message) +static void coap_log_handler(coap_log_t level, const char *message) { uint32_t esp_level = ESP_LOG_INFO; const char *cp = strchr(message, '\n'); - while (cp) { + while (cp) + { ESP_LOG_LEVEL(esp_level, TAG, "%.*s", (int)(cp - message), message); message = cp + 1; cp = strchr(message, '\n'); } - if (message[0] != '\000') { + if (message[0] != '\000') + { ESP_LOG_LEVEL(esp_level, TAG, "%s", message); } } -static int -coap_build_optlist(coap_uri_t *uri) +static int coap_build_optlist(coap_uri_t *uri) { #define BUFSIZE 40 unsigned char _buf[BUFSIZE]; @@ -94,25 +89,30 @@ coap_build_optlist(coap_uri_t *uri) 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 DTLS Client Mode not configured"); 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, "MbedTLS TLS Client Mode not configured"); return 0; } - if (uri->scheme == COAP_URI_SCHEME_COAP_TCP && !coap_tcp_is_supported()) { + if (uri->scheme == COAP_URI_SCHEME_COAP_TCP && !coap_tcp_is_supported()) + { ESP_LOGE(TAG, "TCP Client Mode not configured"); return 0; } - if (uri->path.length) { + if (uri->path.length) + { buflen = BUFSIZE; buf = _buf; res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); - while (res--) { + while (res--) + { coap_insert_optlist(&optlist, coap_new_optlist(COAP_OPTION_URI_PATH, coap_opt_length(buf), @@ -122,12 +122,14 @@ coap_build_optlist(coap_uri_t *uri) } } - if (uri->query.length) { + if (uri->query.length) + { buflen = BUFSIZE; buf = _buf; res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); - while (res--) { + while (res--) + { coap_insert_optlist(&optlist, coap_new_optlist(COAP_OPTION_URI_QUERY, coap_opt_length(buf), @@ -141,9 +143,9 @@ coap_build_optlist(coap_uri_t *uri) static void coap_example_client(void *p) { - coap_address_t dst_addr; + coap_address_t dst_addr; static coap_uri_t uri; - const char *server_uri = COAP_DEFAULT_DEMO_URI; + const char *server_uri = COAP_DEFAULT_DEMO_URI; coap_context_t *ctx = NULL; coap_session_t *session = NULL; coap_pdu_t *request = NULL; @@ -162,7 +164,8 @@ static void coap_example_client(void *p) /* Set up the CoAP context */ ctx = coap_new_context(NULL); - if (!ctx) { + if (!ctx) + { ESP_LOGE(TAG, "coap_new_context() failed"); goto clean_up; } @@ -171,11 +174,13 @@ static void coap_example_client(void *p) coap_register_response_handler(ctx, message_handler); - if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) { + if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) + { ESP_LOGE(TAG, "CoAP server uri %s error", server_uri); goto clean_up; } - if (!coap_build_optlist(&uri)) { + if (!coap_build_optlist(&uri)) + { goto clean_up; } @@ -185,7 +190,8 @@ static void coap_example_client(void *p) 1 << uri.scheme, COAP_RESOLVE_TYPE_REMOTE); - if (info_list == NULL) { + if (info_list == NULL) + { ESP_LOGE(TAG, "failed to resolve address"); goto clean_up; } @@ -201,7 +207,8 @@ 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. */ - if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) { + if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) + { #ifndef CONFIG_MBEDTLS_TLS_CLIENT ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); goto clean_up; @@ -214,23 +221,29 @@ static void coap_example_client(void *p) #ifdef CONFIG_COAP_MBEDTLS_PKI session = coap_start_pki_session(ctx, &dst_addr, &uri, proto); #endif /* CONFIG_COAP_MBEDTLS_PKI */ - } else { + } + else + { session = coap_new_client_session(ctx, NULL, &dst_addr, proto); } - if (!session) { + if (!session) + { ESP_LOGE(TAG, "coap_new_client_session() failed"); goto clean_up; } #ifdef CONFIG_COAP_WEBSOCKETS - if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) { + if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) + { coap_ws_set_host_request(session, &uri.host); } #endif /* CONFIG_COAP_WEBSOCKETS */ - while (1) { + while (1) + { request = coap_new_pdu(coap_is_mcast(&dst_addr) ? COAP_MESSAGE_NON : COAP_MESSAGE_CON, COAP_REQUEST_CODE_GET, session); - if (!request) { + if (!request) + { ESP_LOGE(TAG, "coap_new_pdu() failed"); goto clean_up; } @@ -244,18 +257,24 @@ static void coap_example_client(void *p) wait_ms = COAP_DEFAULT_TIME_SEC * 1000; - while (resp_wait) { + while (resp_wait) + { int result = coap_io_process(ctx, wait_ms > 1000 ? 1000 : wait_ms); - if (result >= 0) { - if (result >= wait_ms) { + if (result >= 0) + { + if (result >= wait_ms) + { ESP_LOGE(TAG, "No response from server"); break; - } else { + } + else + { wait_ms -= result; } } } - for (int countdown = 10; countdown >= 0; countdown--) { + for (int countdown = 10; countdown >= 0; countdown--) + { ESP_LOGI(TAG, "%d... ", countdown); vTaskDelay(1000 / portTICK_PERIOD_MS); } @@ -263,14 +282,17 @@ static void coap_example_client(void *p) } clean_up: - if (optlist) { + if (optlist) + { coap_delete_optlist(optlist); optlist = NULL; } - if (session) { + if (session) + { coap_session_release(session); } - if (ctx) { + if (ctx) + { coap_free_context(ctx); } coap_cleanup(); @@ -281,7 +303,7 @@ static void coap_example_client(void *p) void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); + ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(example_connect()); From 586c7e7ffb67904644e389f690eafa0cb867fcf8 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Mon, 4 Sep 2023 14:41:46 +0300 Subject: [PATCH 3/5] Add coap support Signed-off-by: 1998-felix --- targets/esp32/coap/.gitignore | 3 + targets/esp32/coap/CMakeLists.txt | 6 + targets/esp32/coap/README.md | 0 targets/esp32/coap/platformio.ini | 14 ++ targets/esp32/coap/src/CMakeLists.txt | 6 + targets/esp32/coap/src/idf_component.yml | 5 + targets/esp32/coap/src/main.c | 290 +++++++++++++++++++++++ 7 files changed, 324 insertions(+) create mode 100644 targets/esp32/coap/.gitignore create mode 100644 targets/esp32/coap/CMakeLists.txt create mode 100644 targets/esp32/coap/README.md create mode 100644 targets/esp32/coap/platformio.ini create mode 100644 targets/esp32/coap/src/CMakeLists.txt create mode 100644 targets/esp32/coap/src/idf_component.yml create mode 100644 targets/esp32/coap/src/main.c diff --git a/targets/esp32/coap/.gitignore b/targets/esp32/coap/.gitignore new file mode 100644 index 0000000..469705b --- /dev/null +++ b/targets/esp32/coap/.gitignore @@ -0,0 +1,3 @@ +.pio +.vscode +sdkconfig.esp32doit-devkit-v1 diff --git a/targets/esp32/coap/CMakeLists.txt b/targets/esp32/coap/CMakeLists.txt new file mode 100644 index 0000000..e776a83 --- /dev/null +++ b/targets/esp32/coap/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(coap_client) diff --git a/targets/esp32/coap/README.md b/targets/esp32/coap/README.md new file mode 100644 index 0000000..e69de29 diff --git a/targets/esp32/coap/platformio.ini b/targets/esp32/coap/platformio.ini new file mode 100644 index 0000000..6bcbd9b --- /dev/null +++ b/targets/esp32/coap/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = espidf diff --git a/targets/esp32/coap/src/CMakeLists.txt b/targets/esp32/coap/src/CMakeLists.txt new file mode 100644 index 0000000..ab3ad38 --- /dev/null +++ b/targets/esp32/coap/src/CMakeLists.txt @@ -0,0 +1,6 @@ +# This file was automatically generated for projects +# without default 'CMakeLists.txt' file. + +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources}) diff --git a/targets/esp32/coap/src/idf_component.yml b/targets/esp32/coap/src/idf_component.yml new file mode 100644 index 0000000..d99d1f9 --- /dev/null +++ b/targets/esp32/coap/src/idf_component.yml @@ -0,0 +1,5 @@ +dependencies: + espressif/coap: + version: ^4.3.0 +description: CoAP Client +version: 1.0.0 diff --git a/targets/esp32/coap/src/main.c b/targets/esp32/coap/src/main.c new file mode 100644 index 0000000..04810a7 --- /dev/null +++ b/targets/esp32/coap/src/main.c @@ -0,0 +1,290 @@ +#include +#include +#include +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_log.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "nvs_flash.h" + +#include "protocol_examples_common.h" + +#include "coap3/coap.h" + + +#ifndef CONFIG_COAP_CLIENT_SUPPORT +#error COAP_CLIENT_SUPPORT needs to be enabled +#endif + +#define COAP_DEFAULT_TIME_SEC 60 +e EXAMPLE_COAP_PSK_KEY CONFIG_EXAMPLE_COAP_PSK_KEY +#define EXAMPLE_COAP_PSK_IDENTITY CONFIG_EXAMPLE_COAP_PSK_IDENTITY +#define EXAMPLE_COAP_LOG_DEFAULT_LEVEL CONFIG_COAP_LOG_DEFAULT_LEVEL +#define COAP_DEFAULT_DEMO_URI CONFIG_EXAMPLE_TARGET_DOMAIN_URI + +const static char *TAG = "CoAP_client"; + +static int resp_wait = 1; +static coap_optlist_t *optlist = NULL; +static int wait_ms; + +static coap_response_t +message_handler(coap_session_t *session, + const coap_pdu_t *sent, + const coap_pdu_t *received, + const coap_mid_t mid) +{ + const unsigned char *data = NULL; + size_t data_len; + size_t offset; + size_t total; + coap_pdu_code_t rcvd_code = coap_pdu_get_code(received); + + if (COAP_RESPONSE_CLASS(rcvd_code) == 2) { + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { + if (data_len != total) { + printf("Unexpected partial data received offset %u, length %u\n", offset, data_len); + } + printf("Received:\n%.*s\n", (int)data_len, data); + resp_wait = 0; + } + return COAP_RESPONSE_OK; + } + printf("%d.%02d", (rcvd_code >> 5), rcvd_code & 0x1F); + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { + printf(": "); + while (data_len--) { + printf("%c", isprint(*data) ? *data : '.'); + data++; + } + } + printf("\n"); + resp_wait = 0; + return COAP_RESPONSE_OK; +} + +static void +coap_log_handler (coap_log_t level, const char *message) +{ + uint32_t esp_level = ESP_LOG_INFO; + const char *cp = strchr(message, '\n'); + + while (cp) { + ESP_LOG_LEVEL(esp_level, TAG, "%.*s", (int)(cp - message), message); + message = cp + 1; + cp = strchr(message, '\n'); + } + if (message[0] != '\000') { + ESP_LOG_LEVEL(esp_level, TAG, "%s", message); + } +} + +static int +coap_build_optlist(coap_uri_t *uri) +{ +#define BUFSIZE 40 + unsigned char _buf[BUFSIZE]; + unsigned char *buf; + size_t buflen; + int res; + + optlist = NULL; + + if (uri->scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) { + 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, "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; + } + + if (uri->path.length) { + buflen = BUFSIZE; + buf = _buf; + res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); + + while (res--) { + coap_insert_optlist(&optlist, + coap_new_optlist(COAP_OPTION_URI_PATH, + coap_opt_length(buf), + coap_opt_value(buf))); + + buf += coap_opt_size(buf); + } + } + + if (uri->query.length) { + buflen = BUFSIZE; + buf = _buf; + res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); + + while (res--) { + coap_insert_optlist(&optlist, + coap_new_optlist(COAP_OPTION_URI_QUERY, + coap_opt_length(buf), + coap_opt_value(buf))); + + buf += coap_opt_size(buf); + } + } + return 1; +} + +static void coap_example_client(void *p) +{ + coap_address_t dst_addr; + static coap_uri_t uri; + const char *server_uri = COAP_DEFAULT_DEMO_URI; + coap_context_t *ctx = NULL; + coap_session_t *session = NULL; + coap_pdu_t *request = NULL; + unsigned char token[8]; + size_t tokenlength; + coap_addr_info_t *info_list = NULL; + coap_proto_t proto; + char tmpbuf[INET6_ADDRSTRLEN]; + + /* Initialize libcoap library */ + coap_startup(); + + /* Set up the CoAP logging */ + coap_set_log_handler(coap_log_handler); + coap_set_log_level(EXAMPLE_COAP_LOG_DEFAULT_LEVEL); + + /* Set up the CoAP context */ + ctx = coap_new_context(NULL); + if (!ctx) { + ESP_LOGE(TAG, "coap_new_context() failed"); + goto clean_up; + } + coap_context_set_block_mode(ctx, + COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); + + coap_register_response_handler(ctx, message_handler); + + if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) { + ESP_LOGE(TAG, "CoAP server uri %s error", server_uri); + goto clean_up; + } + if (!coap_build_optlist(&uri)) { + goto clean_up; + } + + info_list = coap_resolve_address_info(&uri.host, uri.port, uri.port, + uri.port, uri.port, + 0, + 1 << uri.scheme, + COAP_RESOLVE_TYPE_REMOTE); + + if (info_list == NULL) { + ESP_LOGE(TAG, "failed to resolve address"); + goto clean_up; + } + proto = info_list->proto; + memcpy(&dst_addr, &info_list->addr, sizeof(dst_addr)); + coap_free_address_info(info_list); + + /* This is to keep the test suites happy */ + coap_print_ip_addr(&dst_addr, tmpbuf, sizeof(tmpbuf)); + ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", tmpbuf); + + /* + * Note that if the URI starts with just coap:// (not coaps://) the + * session will still be plain text. + */ + if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) { +#ifndef CONFIG_MBEDTLS_TLS_CLIENT + ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); + goto clean_up; +#endif /* CONFIG_MBEDTLS_TLS_CLIENT */ + +#ifdef CONFIG_COAP_MBEDTLS_PSK + session = coap_start_psk_session(ctx, &dst_addr, &uri, proto); +#endif /* CONFIG_COAP_MBEDTLS_PSK */ + +#ifdef CONFIG_COAP_MBEDTLS_PKI + session = coap_start_pki_session(ctx, &dst_addr, &uri, proto); +#endif /* CONFIG_COAP_MBEDTLS_PKI */ + } else { + session = coap_new_client_session(ctx, NULL, &dst_addr, proto); + } + if (!session) { + ESP_LOGE(TAG, "coap_new_client_session() failed"); + goto clean_up; + } +#ifdef CONFIG_COAP_WEBSOCKETS + if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) { + coap_ws_set_host_request(session, &uri.host); + } +#endif /* CONFIG_COAP_WEBSOCKETS */ + + while (1) { + request = coap_new_pdu(coap_is_mcast(&dst_addr) ? COAP_MESSAGE_NON : COAP_MESSAGE_CON, + COAP_REQUEST_CODE_GET, session); + if (!request) { + ESP_LOGE(TAG, "coap_new_pdu() failed"); + goto clean_up; + } + /* Add in an unique token */ + coap_session_new_token(session, &tokenlength, token); + coap_add_token(request, tokenlength, token); + coap_add_optlist_pdu(request, &optlist); + + resp_wait = 1; + coap_send(session, request); + + wait_ms = COAP_DEFAULT_TIME_SEC * 1000; + + while (resp_wait) { + int result = coap_io_process(ctx, wait_ms > 1000 ? 1000 : wait_ms); + if (result >= 0) { + if (result >= wait_ms) { + ESP_LOGE(TAG, "No response from server"); + break; + } else { + wait_ms -= result; + } + } + } + for (int countdown = 10; countdown >= 0; countdown--) { + ESP_LOGI(TAG, "%d... ", countdown); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + ESP_LOGI(TAG, "Starting again!"); + } + +clean_up: + if (optlist) { + coap_delete_optlist(optlist); + optlist = NULL; + } + if (session) { + coap_session_release(session); + } + if (ctx) { + coap_free_context(ctx); + } + coap_cleanup(); + + ESP_LOGI(TAG, "Finished"); + vTaskDelete(NULL); +} + +void app_main(void) +{ + ESP_ERROR_CHECK( nvs_flash_init() ); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(example_connect()); + + xTaskCreate(coap_example_client, "coap", 8 * 1024, NULL, 5, NULL); +} From 6cc5aaa8c7d2b25fccadfbb8e702c969d714b0f5 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Wed, 6 Sep 2023 13:16:13 +0300 Subject: [PATCH 4/5] Add config file Signed-off-by: 1998-felix --- targets/esp32/coap/include/config.h | 13 +++ targets/esp32/coap/src/main.c | 128 ++++++++++++++++------------ 2 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 targets/esp32/coap/include/config.h diff --git a/targets/esp32/coap/include/config.h b/targets/esp32/coap/include/config.h new file mode 100644 index 0000000..10354bf --- /dev/null +++ b/targets/esp32/coap/include/config.h @@ -0,0 +1,13 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define CLIENTID "" + +const char *mfThingId = " "; +const char *mfThingKey = " "; +const char *mfChannelId = " "; +char mfTopic[150]; + +const char *server = " "; + +#endif \ No newline at end of file diff --git a/targets/esp32/coap/src/main.c b/targets/esp32/coap/src/main.c index 04810a7..3535dc2 100644 --- a/targets/esp32/coap/src/main.c +++ b/targets/esp32/coap/src/main.c @@ -15,16 +15,7 @@ #include "coap3/coap.h" - -#ifndef CONFIG_COAP_CLIENT_SUPPORT -#error COAP_CLIENT_SUPPORT needs to be enabled -#endif - #define COAP_DEFAULT_TIME_SEC 60 -e EXAMPLE_COAP_PSK_KEY CONFIG_EXAMPLE_COAP_PSK_KEY -#define EXAMPLE_COAP_PSK_IDENTITY CONFIG_EXAMPLE_COAP_PSK_IDENTITY -#define EXAMPLE_COAP_LOG_DEFAULT_LEVEL CONFIG_COAP_LOG_DEFAULT_LEVEL -#define COAP_DEFAULT_DEMO_URI CONFIG_EXAMPLE_TARGET_DOMAIN_URI const static char *TAG = "CoAP_client"; @@ -32,11 +23,10 @@ static int resp_wait = 1; static coap_optlist_t *optlist = NULL; static int wait_ms; -static coap_response_t -message_handler(coap_session_t *session, - const coap_pdu_t *sent, - const coap_pdu_t *received, - const coap_mid_t mid) +static coap_response_t message_handler(coap_session_t *session, + const coap_pdu_t *sent, + const coap_pdu_t *received, + const coap_mid_t mid) { const unsigned char *data = NULL; size_t data_len; @@ -44,9 +34,12 @@ message_handler(coap_session_t *session, size_t total; coap_pdu_code_t rcvd_code = coap_pdu_get_code(received); - if (COAP_RESPONSE_CLASS(rcvd_code) == 2) { - if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { - if (data_len != total) { + if (COAP_RESPONSE_CLASS(rcvd_code) == 2) + { + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) + { + if (data_len != total) + { printf("Unexpected partial data received offset %u, length %u\n", offset, data_len); } printf("Received:\n%.*s\n", (int)data_len, data); @@ -55,9 +48,11 @@ message_handler(coap_session_t *session, return COAP_RESPONSE_OK; } printf("%d.%02d", (rcvd_code >> 5), rcvd_code & 0x1F); - if (coap_get_data_large(received, &data_len, &data, &offset, &total)) { + if (coap_get_data_large(received, &data_len, &data, &offset, &total)) + { printf(": "); - while (data_len--) { + while (data_len--) + { printf("%c", isprint(*data) ? *data : '.'); data++; } @@ -67,24 +62,24 @@ message_handler(coap_session_t *session, return COAP_RESPONSE_OK; } -static void -coap_log_handler (coap_log_t level, const char *message) +static void coap_log_handler(coap_log_t level, const char *message) { uint32_t esp_level = ESP_LOG_INFO; const char *cp = strchr(message, '\n'); - while (cp) { + while (cp) + { ESP_LOG_LEVEL(esp_level, TAG, "%.*s", (int)(cp - message), message); message = cp + 1; cp = strchr(message, '\n'); } - if (message[0] != '\000') { + if (message[0] != '\000') + { ESP_LOG_LEVEL(esp_level, TAG, "%s", message); } } -static int -coap_build_optlist(coap_uri_t *uri) +static int coap_build_optlist(coap_uri_t *uri) { #define BUFSIZE 40 unsigned char _buf[BUFSIZE]; @@ -94,25 +89,30 @@ coap_build_optlist(coap_uri_t *uri) 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 DTLS Client Mode not configured"); 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, "MbedTLS TLS Client Mode not configured"); return 0; } - if (uri->scheme == COAP_URI_SCHEME_COAP_TCP && !coap_tcp_is_supported()) { + if (uri->scheme == COAP_URI_SCHEME_COAP_TCP && !coap_tcp_is_supported()) + { ESP_LOGE(TAG, "TCP Client Mode not configured"); return 0; } - if (uri->path.length) { + if (uri->path.length) + { buflen = BUFSIZE; buf = _buf; res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); - while (res--) { + while (res--) + { coap_insert_optlist(&optlist, coap_new_optlist(COAP_OPTION_URI_PATH, coap_opt_length(buf), @@ -122,12 +122,14 @@ coap_build_optlist(coap_uri_t *uri) } } - if (uri->query.length) { + if (uri->query.length) + { buflen = BUFSIZE; buf = _buf; res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); - while (res--) { + while (res--) + { coap_insert_optlist(&optlist, coap_new_optlist(COAP_OPTION_URI_QUERY, coap_opt_length(buf), @@ -141,9 +143,9 @@ coap_build_optlist(coap_uri_t *uri) static void coap_example_client(void *p) { - coap_address_t dst_addr; + coap_address_t dst_addr; static coap_uri_t uri; - const char *server_uri = COAP_DEFAULT_DEMO_URI; + const char *server_uri = COAP_DEFAULT_DEMO_URI; coap_context_t *ctx = NULL; coap_session_t *session = NULL; coap_pdu_t *request = NULL; @@ -162,7 +164,8 @@ static void coap_example_client(void *p) /* Set up the CoAP context */ ctx = coap_new_context(NULL); - if (!ctx) { + if (!ctx) + { ESP_LOGE(TAG, "coap_new_context() failed"); goto clean_up; } @@ -171,11 +174,13 @@ static void coap_example_client(void *p) coap_register_response_handler(ctx, message_handler); - if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) { + if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) + { ESP_LOGE(TAG, "CoAP server uri %s error", server_uri); goto clean_up; } - if (!coap_build_optlist(&uri)) { + if (!coap_build_optlist(&uri)) + { goto clean_up; } @@ -185,7 +190,8 @@ static void coap_example_client(void *p) 1 << uri.scheme, COAP_RESOLVE_TYPE_REMOTE); - if (info_list == NULL) { + if (info_list == NULL) + { ESP_LOGE(TAG, "failed to resolve address"); goto clean_up; } @@ -201,7 +207,8 @@ 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. */ - if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) { + if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) + { #ifndef CONFIG_MBEDTLS_TLS_CLIENT ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); goto clean_up; @@ -214,23 +221,29 @@ static void coap_example_client(void *p) #ifdef CONFIG_COAP_MBEDTLS_PKI session = coap_start_pki_session(ctx, &dst_addr, &uri, proto); #endif /* CONFIG_COAP_MBEDTLS_PKI */ - } else { + } + else + { session = coap_new_client_session(ctx, NULL, &dst_addr, proto); } - if (!session) { + if (!session) + { ESP_LOGE(TAG, "coap_new_client_session() failed"); goto clean_up; } #ifdef CONFIG_COAP_WEBSOCKETS - if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) { + if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) + { coap_ws_set_host_request(session, &uri.host); } #endif /* CONFIG_COAP_WEBSOCKETS */ - while (1) { + while (1) + { request = coap_new_pdu(coap_is_mcast(&dst_addr) ? COAP_MESSAGE_NON : COAP_MESSAGE_CON, COAP_REQUEST_CODE_GET, session); - if (!request) { + if (!request) + { ESP_LOGE(TAG, "coap_new_pdu() failed"); goto clean_up; } @@ -244,18 +257,24 @@ static void coap_example_client(void *p) wait_ms = COAP_DEFAULT_TIME_SEC * 1000; - while (resp_wait) { + while (resp_wait) + { int result = coap_io_process(ctx, wait_ms > 1000 ? 1000 : wait_ms); - if (result >= 0) { - if (result >= wait_ms) { + if (result >= 0) + { + if (result >= wait_ms) + { ESP_LOGE(TAG, "No response from server"); break; - } else { + } + else + { wait_ms -= result; } } } - for (int countdown = 10; countdown >= 0; countdown--) { + for (int countdown = 10; countdown >= 0; countdown--) + { ESP_LOGI(TAG, "%d... ", countdown); vTaskDelay(1000 / portTICK_PERIOD_MS); } @@ -263,14 +282,17 @@ static void coap_example_client(void *p) } clean_up: - if (optlist) { + if (optlist) + { coap_delete_optlist(optlist); optlist = NULL; } - if (session) { + if (session) + { coap_session_release(session); } - if (ctx) { + if (ctx) + { coap_free_context(ctx); } coap_cleanup(); @@ -281,7 +303,7 @@ static void coap_example_client(void *p) void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); + ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(example_connect()); From 99b3b963ceed1073d6f6d2ec50395a5d4d73ef15 Mon Sep 17 00:00:00 2001 From: "felix.gateru" Date: Fri, 13 Oct 2023 13:15:08 +0300 Subject: [PATCH 5/5] Append CoAP config Signed-off-by: felix.gateru --- targets/esp32/coap/include/cnetwork.h | 0 targets/esp32/coap/src/CMakeLists.txt | 6 - targets/esp32/coap/src/cnetwork.c | 0 targets/esp32/coap/src/idf_component.yml | 5 - targets/esp32/coap/src/main.c | 156 +++++------------------ 5 files changed, 29 insertions(+), 138 deletions(-) create mode 100644 targets/esp32/coap/include/cnetwork.h delete mode 100644 targets/esp32/coap/src/CMakeLists.txt create mode 100644 targets/esp32/coap/src/cnetwork.c diff --git a/targets/esp32/coap/include/cnetwork.h b/targets/esp32/coap/include/cnetwork.h new file mode 100644 index 0000000..e69de29 diff --git a/targets/esp32/coap/src/CMakeLists.txt b/targets/esp32/coap/src/CMakeLists.txt deleted file mode 100644 index ab3ad38..0000000 --- a/targets/esp32/coap/src/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# This file was automatically generated for projects -# without default 'CMakeLists.txt' file. - -FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) - -idf_component_register(SRCS ${app_sources}) diff --git a/targets/esp32/coap/src/cnetwork.c b/targets/esp32/coap/src/cnetwork.c new file mode 100644 index 0000000..e69de29 diff --git a/targets/esp32/coap/src/idf_component.yml b/targets/esp32/coap/src/idf_component.yml index d99d1f9..e69de29 100644 --- a/targets/esp32/coap/src/idf_component.yml +++ b/targets/esp32/coap/src/idf_component.yml @@ -1,5 +0,0 @@ -dependencies: - espressif/coap: - version: ^4.3.0 -description: CoAP Client -version: 1.0.0 diff --git a/targets/esp32/coap/src/main.c b/targets/esp32/coap/src/main.c index 3535dc2..c4407a7 100644 --- a/targets/esp32/coap/src/main.c +++ b/targets/esp32/coap/src/main.c @@ -12,17 +12,27 @@ #include "nvs_flash.h" #include "protocol_examples_common.h" - #include "coap3/coap.h" +#include "config.h" +#include "cnetwork.h" -#define COAP_DEFAULT_TIME_SEC 60 - -const static char *TAG = "CoAP_client"; +const static char *TAG = CLIENTID; static int resp_wait = 1; static coap_optlist_t *optlist = NULL; static int wait_ms; + +void format_mainflux_message_topic(void) +{ + const char *_preId = "channels/"; + const char *_postId = "/messages"; + strcpy(mfTopic, _preId); + strcat(mfTopic, mfChannelId); + strcat(mfTopic, _postId); +} + + static coap_response_t message_handler(coap_session_t *session, const coap_pdu_t *sent, const coap_pdu_t *received, @@ -79,73 +89,11 @@ static void coap_log_handler(coap_log_t level, const char *message) } } -static int coap_build_optlist(coap_uri_t *uri) -{ -#define BUFSIZE 40 - unsigned char _buf[BUFSIZE]; - unsigned char *buf; - size_t buflen; - int res; - - optlist = NULL; - - if (uri->scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) - { - 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, "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; - } - - if (uri->path.length) - { - buflen = BUFSIZE; - buf = _buf; - res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); - - while (res--) - { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PATH, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } - } - - if (uri->query.length) - { - buflen = BUFSIZE; - buf = _buf; - res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); - - while (res--) - { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_QUERY, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } - } - return 1; -} - -static void coap_example_client(void *p) +static void coap_client(void *p) { - coap_address_t dst_addr; - static coap_uri_t uri; - const char *server_uri = COAP_DEFAULT_DEMO_URI; + coap_address_t uri; + static coap_uri_t dst_addr; + const char *server_uri = server; coap_context_t *ctx = NULL; coap_session_t *session = NULL; coap_pdu_t *request = NULL; @@ -155,14 +103,11 @@ static void coap_example_client(void *p) coap_proto_t proto; char tmpbuf[INET6_ADDRSTRLEN]; - /* Initialize libcoap library */ coap_startup(); - /* Set up the CoAP logging */ coap_set_log_handler(coap_log_handler); - coap_set_log_level(EXAMPLE_COAP_LOG_DEFAULT_LEVEL); + coap_set_log_level(COAP_LOG_DEFAULT_LEVEL); - /* Set up the CoAP context */ ctx = coap_new_context(NULL); if (!ctx) { @@ -179,10 +124,6 @@ static void coap_example_client(void *p) ESP_LOGE(TAG, "CoAP server uri %s error", server_uri); goto clean_up; } - if (!coap_build_optlist(&uri)) - { - goto clean_up; - } info_list = coap_resolve_address_info(&uri.host, uri.port, uri.port, uri.port, uri.port, @@ -193,52 +134,19 @@ static void coap_example_client(void *p) if (info_list == NULL) { ESP_LOGE(TAG, "failed to resolve address"); - goto clean_up; + clean_up(); } proto = info_list->proto; memcpy(&dst_addr, &info_list->addr, sizeof(dst_addr)); coap_free_address_info(info_list); - - /* This is to keep the test suites happy */ - coap_print_ip_addr(&dst_addr, tmpbuf, sizeof(tmpbuf)); - ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", tmpbuf); - - /* - * Note that if the URI starts with just coap:// (not coaps://) the - * session will still be plain text. - */ - if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP || uri.scheme == COAP_URI_SCHEME_COAPS_WS) - { -#ifndef CONFIG_MBEDTLS_TLS_CLIENT - ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); - goto clean_up; -#endif /* CONFIG_MBEDTLS_TLS_CLIENT */ - -#ifdef CONFIG_COAP_MBEDTLS_PSK - session = coap_start_psk_session(ctx, &dst_addr, &uri, proto); -#endif /* CONFIG_COAP_MBEDTLS_PSK */ - -#ifdef CONFIG_COAP_MBEDTLS_PKI - session = coap_start_pki_session(ctx, &dst_addr, &uri, proto); -#endif /* CONFIG_COAP_MBEDTLS_PKI */ - } - else - { - session = coap_new_client_session(ctx, NULL, &dst_addr, proto); - } + session = coap_new_client_session(ctx, NULL, &dst_addr, proto); if (!session) { ESP_LOGE(TAG, "coap_new_client_session() failed"); - goto clean_up; - } -#ifdef CONFIG_COAP_WEBSOCKETS - if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) - { - coap_ws_set_host_request(session, &uri.host); + clean_up(); } -#endif /* CONFIG_COAP_WEBSOCKETS */ - while (1) + while (true) { request = coap_new_pdu(coap_is_mcast(&dst_addr) ? COAP_MESSAGE_NON : COAP_MESSAGE_CON, COAP_REQUEST_CODE_GET, session); @@ -247,7 +155,6 @@ static void coap_example_client(void *p) ESP_LOGE(TAG, "coap_new_pdu() failed"); goto clean_up; } - /* Add in an unique token */ coap_session_new_token(session, &tokenlength, token); coap_add_token(request, tokenlength, token); coap_add_optlist_pdu(request, &optlist); @@ -255,7 +162,7 @@ static void coap_example_client(void *p) resp_wait = 1; coap_send(session, request); - wait_ms = COAP_DEFAULT_TIME_SEC * 1000; + wait_ms = COAP_DEFAULT_TIME_SEC * MS_COUNT; while (resp_wait) { @@ -280,13 +187,9 @@ static void coap_example_client(void *p) } ESP_LOGI(TAG, "Starting again!"); } - -clean_up: - if (optlist) - { - coap_delete_optlist(optlist); - optlist = NULL; - } +} +void clean_up() +{ if (session) { coap_session_release(session); @@ -300,13 +203,12 @@ static void coap_example_client(void *p) ESP_LOGI(TAG, "Finished"); vTaskDelete(NULL); } - void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); - ESP_ERROR_CHECK(example_connect()); + ESP_ERROR_CHECK(wifi_init_softap()); - xTaskCreate(coap_example_client, "coap", 8 * 1024, NULL, 5, NULL); + xTaskCreate(coap_client, "coap", COAP_BUFFSIZE, NULL, COAP_TIMEOUT, NULL); }