esp-idf/components/esp-tls/esp_tls_error_capture.c
David Cermak ce519ee783 tcp_transport: Extend transport error storage for socket error
Everytime we report error and log errno, we also capture the actual
errno to an internal storage so that user application can retrieve
its value.
2020-11-12 12:46:22 +00:00

82 lines
3.2 KiB
C

// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_tls.h"
#include "esp_tls_error_capture_internal.h"
typedef struct esp_tls_error_storage {
struct esp_tls_last_error parent; /*!< standard esp-tls last error container */
int sock_errno; /*!< last socket error captured in esp-tls */
} esp_tls_error_storage_t;
void esp_tls_internal_event_tracker_capture(esp_tls_error_handle_t h, uint32_t type, int code)
{
if (h) {
esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
if (type == ESP_TLS_ERR_TYPE_ESP) {
storage->parent.last_error = code;
} else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
type == ESP_TLS_ERR_TYPE_WOLFSSL) {
storage->parent.esp_tls_error_code = code;
} else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
storage->parent.esp_tls_flags = code;
} else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
storage->sock_errno = code;
}
}
}
esp_tls_error_handle_t esp_tls_internal_event_tracker_create(void)
{
// Allocating internal error storage which extends the parent type
// `esp_tls_last_error` defined at interface level
struct esp_tls_error_storage* storage =
calloc(1, sizeof(struct esp_tls_error_storage));
return &storage->parent;
}
void esp_tls_internal_event_tracker_destroy(esp_tls_error_handle_t h)
{
esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
free(storage);
}
esp_err_t esp_tls_get_and_clear_error_type(esp_tls_error_handle_t h, esp_tls_error_type_t type, int *code)
{
if (h && type < ESP_TLS_ERR_TYPE_MAX && code) {
esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
if (type == ESP_TLS_ERR_TYPE_ESP) {
*code = storage->parent.last_error;
storage->parent.last_error = 0;
} else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
type == ESP_TLS_ERR_TYPE_WOLFSSL) {
*code = storage->parent.esp_tls_error_code;
storage->parent.esp_tls_error_code = 0;
} else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
*code = storage->parent.esp_tls_flags;
storage->parent.esp_tls_flags = 0;
} else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
*code = storage->sock_errno;
storage->sock_errno = 0;
} else {
return ESP_ERR_INVALID_ARG;
}
return ESP_OK;
}
return ESP_ERR_INVALID_ARG;
}