Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ot_br/components/ot_br_web_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
idf_component_register(
SRCS "ot_br_web_api.c" "ot_br_web_api_auth.c" "ot_br_web_api_handlers.c" "ot_br_web_api_ota.c" "ot_br_web_api_static.c" "ot_br_web_api_setup_mode.c" "ot_br_web_api_command.c" "ot_br_web_api_coap_client.c"
INCLUDE_DIRS "include"
REQUIRES esp_http_server openthread nvs_flash esp_hw_support esp_app_format mbedtls espressif__cjson app_update esp_https_ota esp_http_client joltwallet__littlefs esp_driver_gpio esp_timer
REQUIRES esp_http_server openthread nvs_flash esp_hw_support esp_app_format mbedtls espressif__cjson app_update esp_https_ota esp_http_client joltwallet__littlefs esp_driver_gpio esp_timer ot_coap_client
)
9 changes: 6 additions & 3 deletions ot_br/components/ot_br_web_api/ot_br_web_api_coap_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "openthread/netdiag.h"
#include "freertos/semphr.h"

#include "ot_coap_client.h"

static const char *TAG = "ot_br_coap_client";
static SemaphoreHandle_t s_response_sem = NULL;
static otCoapCode s_last_response_code = 0;
Expand Down Expand Up @@ -43,12 +45,13 @@ void ot_br_coap_client_init(void)
s_response_sem = xSemaphoreCreateBinary();

otInstance *instance = esp_openthread_get_instance();

esp_openthread_lock_acquire(portMAX_DELAY);
otError err = otCoapStart(instance, OT_DEFAULT_COAP_PORT);
esp_err_t err = ot_coap_client_ensure_started(instance);
esp_openthread_lock_release();

if (err != OT_ERROR_NONE && err != OT_ERROR_ALREADY) {
ESP_LOGE("ot_br_coap_client", "otCoapStart fehlgeschlagen: %d", err);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to start CoAP-Client");
}
}

Expand Down
5 changes: 5 additions & 0 deletions ot_br/components/ot_coap_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(
SRCS "ot_coap_client.c"
INCLUDE_DIRS "include"
REQUIRES driver openthread
)
31 changes: 31 additions & 0 deletions ot_br/components/ot_coap_client/include/ot_coap_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "esp_err.h"
#include "openthread/instance.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Ensure the OpenThread CoAP layer's UDP socket is open.
*
* otCoapSendRequest() requires otCoapStart() to have been called at
* least once -- even for pure client usage that never registers a
* resource -- otherwise it fails with OT_ERROR_INVALID_STATE. This
* wrapper makes that call exactly once per boot and is safe to call
* from every place that is about to send a CoAP request.
*
* Must be called with the OpenThread lock already held by the caller
* (esp_openthread_lock_acquire()), same as any other otXxx() call.
*
* @param instance A valid OpenThread instance (e.g. from
* esp_openthread_get_instance()).
* @return ESP_OK if the CoAP layer is started (either just now or
* already before), ESP_FAIL if otCoapStart() itself failed.
*/
esp_err_t ot_coap_client_ensure_started(otInstance *instance);

#ifdef __cplusplus
}
#endif
24 changes: 24 additions & 0 deletions ot_br/components/ot_coap_client/ot_coap_client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "ot_coap_client.h"

#include "esp_log.h"
#include "openthread/coap.h"

static const char *TAG = "ot_coap_client";
static bool s_coap_started = false;

esp_err_t ot_coap_client_ensure_started(otInstance *instance)
{
if (s_coap_started) {
return ESP_OK;
}

otError error = otCoapStart(instance, OT_DEFAULT_COAP_PORT);
if (error != OT_ERROR_NONE && error != OT_ERROR_ALREADY) {
ESP_LOGE(TAG, "otCoapStart failed: %s", otThreadErrorToString(error));
return ESP_FAIL;
}

s_coap_started = true;
ESP_LOGI(TAG, "CoAP client layer started on port %d", OT_DEFAULT_COAP_PORT);
return ESP_OK;
}