esp_tls_error_capture.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_tls.h"
  14. #include "esp_tls_error_capture_internal.h"
  15. typedef struct esp_tls_error_storage {
  16. struct esp_tls_last_error parent; /*!< standard esp-tls last error container */
  17. int sock_errno; /*!< last socket error captured in esp-tls */
  18. } esp_tls_error_storage_t;
  19. void esp_tls_internal_event_tracker_capture(esp_tls_error_handle_t h, uint32_t type, int code)
  20. {
  21. if (h) {
  22. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  23. if (type == ESP_TLS_ERR_TYPE_ESP) {
  24. storage->parent.last_error = code;
  25. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
  26. type == ESP_TLS_ERR_TYPE_WOLFSSL) {
  27. storage->parent.esp_tls_error_code = code;
  28. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
  29. type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
  30. storage->parent.esp_tls_flags = code;
  31. } else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
  32. storage->sock_errno = code;
  33. }
  34. }
  35. }
  36. esp_tls_error_handle_t esp_tls_internal_event_tracker_create(void)
  37. {
  38. // Allocating internal error storage which extends the parent type
  39. // `esp_tls_last_error` defined at interface level
  40. struct esp_tls_error_storage* storage =
  41. calloc(1, sizeof(struct esp_tls_error_storage));
  42. return &storage->parent;
  43. }
  44. void esp_tls_internal_event_tracker_destroy(esp_tls_error_handle_t h)
  45. {
  46. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  47. free(storage);
  48. }
  49. esp_err_t esp_tls_get_and_clear_error_type(esp_tls_error_handle_t h, esp_tls_error_type_t type, int *code)
  50. {
  51. if (h && type < ESP_TLS_ERR_TYPE_MAX && code) {
  52. esp_tls_error_storage_t * storage = __containerof(h, esp_tls_error_storage_t, parent);
  53. if (type == ESP_TLS_ERR_TYPE_ESP) {
  54. *code = storage->parent.last_error;
  55. storage->parent.last_error = 0;
  56. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS ||
  57. type == ESP_TLS_ERR_TYPE_WOLFSSL) {
  58. *code = storage->parent.esp_tls_error_code;
  59. storage->parent.esp_tls_error_code = 0;
  60. } else if (type == ESP_TLS_ERR_TYPE_MBEDTLS_CERT_FLAGS ||
  61. type == ESP_TLS_ERR_TYPE_WOLFSSL_CERT_FLAGS) {
  62. *code = storage->parent.esp_tls_flags;
  63. storage->parent.esp_tls_flags = 0;
  64. } else if (type == ESP_TLS_ERR_TYPE_SYSTEM) {
  65. *code = storage->sock_errno;
  66. storage->sock_errno = 0;
  67. } else {
  68. return ESP_ERR_INVALID_ARG;
  69. }
  70. return ESP_OK;
  71. }
  72. return ESP_ERR_INVALID_ARG;
  73. }