1 // Copyright 2015-2016 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 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <strings.h>
16 
17 #include "esp_log.h"
18 #include "mbedtls/platform.h"
19 #include "mbedtls/debug.h"
20 #include "mbedtls/ssl.h"
21 #include "mbedtls/esp_debug.h"
22 
23 #ifdef CONFIG_MBEDTLS_DEBUG
24 static const char *TAG = "mbedtls";
25 
26 static void mbedtls_esp_debug(void *ctx, int level,
27                               const char *file, int line,
28                               const char *str);
29 
mbedtls_esp_enable_debug_log(mbedtls_ssl_config * conf,int threshold)30 void mbedtls_esp_enable_debug_log(mbedtls_ssl_config *conf, int threshold)
31 {
32     esp_log_level_t level = ESP_LOG_NONE;
33     mbedtls_debug_set_threshold(threshold);
34     mbedtls_ssl_conf_dbg(conf, mbedtls_esp_debug, NULL);
35     switch(threshold) {
36     case 1:
37         level = ESP_LOG_WARN;
38         break;
39     case 2:
40         level = ESP_LOG_INFO;
41         break;
42     case 3:
43         level = ESP_LOG_DEBUG;
44         break;
45     case 4:
46         level = ESP_LOG_VERBOSE;
47         break;
48     }
49     esp_log_level_set(TAG, level);
50 }
51 
mbedtls_esp_disable_debug_log(mbedtls_ssl_config * conf)52 void mbedtls_esp_disable_debug_log(mbedtls_ssl_config *conf)
53 {
54     mbedtls_ssl_conf_dbg(conf, NULL, NULL);
55 }
56 
57 
58 /* Default mbedtls debug function that translates mbedTLS debug output
59    to ESP_LOGx debug output.
60 */
mbedtls_esp_debug(void * ctx,int level,const char * file,int line,const char * str)61 static void mbedtls_esp_debug(void *ctx, int level,
62                      const char *file, int line,
63                      const char *str)
64 {
65     char *file_sep;
66 
67     /* Shorten 'file' from the whole file path to just the filename
68 
69        This is a bit wasteful because the macros are compiled in with
70        the full _FILE_ path in each case.
71     */
72     file_sep = rindex(file, '/');
73     if(file_sep)
74         file = file_sep+1;
75 
76     switch(level) {
77     case 1:
78         ESP_LOGW(TAG, "%s:%d %s", file, line, str);
79         break;
80     case 2:
81         ESP_LOGI(TAG, "%s:%d %s", file, line, str);
82         break;
83     case 3:
84         ESP_LOGD(TAG, "%s:%d %s", file, line, str);
85         break;
86     case 4:
87         ESP_LOGV(TAG, "%s:%d %s", file, line, str);
88         break;
89     default:
90         ESP_LOGE(TAG, "Unexpected log level %d: %s", level, str);
91         break;
92     }
93 }
94 #endif
95