esp_tls_error_capture.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_tls.h"
  7. #include "esp_tls_error_capture_internal.h"
  8. #if CONFIG_IDF_TARGET_LINUX
  9. #include "esp_linux_helper.h"
  10. #endif
  11. typedef struct esp_tls_error_storage {
  12. struct esp_tls_last_error parent; /*!< standard esp-tls last error container */
  13. int sock_errno; /*!< last socket error captured in esp-tls */
  14. } esp_tls_error_storage_t;
  15. void esp_tls_internal_event_tracker_capture(esp_tls_error_handle_t h, uint32_t type, int code)
  16. {
  17. if (h) {
  18. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  19. if (type == ESP_TLS_ERR_TYPE_ESP) {
  20. storage->parent.last_error = code;
  21. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
  22. type == ESP_TLS_ERR_TYPE_WOLFSSL) {
  23. storage->parent.esp_tls_error_code = code;
  24. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
  25. type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
  26. storage->parent.esp_tls_flags = code;
  27. } else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
  28. storage->sock_errno = code;
  29. }
  30. }
  31. }
  32. esp_tls_error_handle_t esp_tls_internal_event_tracker_create(void)
  33. {
  34. // Allocating internal error storage which extends the parent type
  35. // `esp_tls_last_error` defined at interface level
  36. struct esp_tls_error_storage* storage =
  37. calloc(1, sizeof(struct esp_tls_error_storage));
  38. return &storage->parent;
  39. }
  40. void esp_tls_internal_event_tracker_destroy(esp_tls_error_handle_t h)
  41. {
  42. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  43. free(storage);
  44. }
  45. esp_err_t esp_tls_get_and_clear_error_type(esp_tls_error_handle_t h, esp_tls_error_type_t type, int *code)
  46. {
  47. if (h && type < ESP_TLS_ERR_TYPE_MAX && code) {
  48. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  49. if (type == ESP_TLS_ERR_TYPE_ESP) {
  50. *code = storage->parent.last_error;
  51. storage->parent.last_error = 0;
  52. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
  53. type == ESP_TLS_ERR_TYPE_WOLFSSL) {
  54. *code = storage->parent.esp_tls_error_code;
  55. storage->parent.esp_tls_error_code = 0;
  56. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
  57. type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
  58. *code = storage->parent.esp_tls_flags;
  59. storage->parent.esp_tls_flags = 0;
  60. } else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
  61. *code = storage->sock_errno;
  62. storage->sock_errno = 0;
  63. } else {
  64. return ESP_ERR_INVALID_ARG;
  65. }
  66. return ESP_OK;
  67. }
  68. return ESP_ERR_INVALID_ARG;
  69. }