esp_tls_error_capture.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. typedef struct esp_tls_error_storage {
  9. struct esp_tls_last_error parent; /*!< standard esp-tls last error container */
  10. int sock_errno; /*!< last socket error captured in esp-tls */
  11. } esp_tls_error_storage_t;
  12. void esp_tls_internal_event_tracker_capture(esp_tls_error_handle_t h, uint32_t type, int code)
  13. {
  14. if (h) {
  15. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  16. if (type == ESP_TLS_ERR_TYPE_ESP) {
  17. storage->parent.last_error = code;
  18. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
  19. type == ESP_TLS_ERR_TYPE_WOLFSSL) {
  20. storage->parent.esp_tls_error_code = code;
  21. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
  22. type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
  23. storage->parent.esp_tls_flags = code;
  24. } else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
  25. storage->sock_errno = code;
  26. }
  27. }
  28. }
  29. esp_tls_error_handle_t esp_tls_internal_event_tracker_create(void)
  30. {
  31. // Allocating internal error storage which extends the parent type
  32. // `esp_tls_last_error` defined at interface level
  33. struct esp_tls_error_storage* storage =
  34. calloc(1, sizeof(struct esp_tls_error_storage));
  35. return &storage->parent;
  36. }
  37. void esp_tls_internal_event_tracker_destroy(esp_tls_error_handle_t h)
  38. {
  39. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  40. free(storage);
  41. }
  42. esp_err_t esp_tls_get_and_clear_error_type(esp_tls_error_handle_t h, esp_tls_error_type_t type, int *code)
  43. {
  44. if (h && type < ESP_TLS_ERR_TYPE_MAX && code) {
  45. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  46. if (type == ESP_TLS_ERR_TYPE_ESP) {
  47. *code = storage->parent.last_error;
  48. storage->parent.last_error = 0;
  49. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
  50. type == ESP_TLS_ERR_TYPE_WOLFSSL) {
  51. *code = storage->parent.esp_tls_error_code;
  52. storage->parent.esp_tls_error_code = 0;
  53. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
  54. type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
  55. *code = storage->parent.esp_tls_flags;
  56. storage->parent.esp_tls_flags = 0;
  57. } else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
  58. *code = storage->sock_errno;
  59. storage->sock_errno = 0;
  60. } else {
  61. return ESP_ERR_INVALID_ARG;
  62. }
  63. return ESP_OK;
  64. }
  65. return ESP_ERR_INVALID_ARG;
  66. }