1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "esp_log.h"
11 #include "soc/soc_memory_types.h"  // for esp_ptr_byte_accessible
12 
13 
14 //print number of bytes per line for esp_log_buffer_char and esp_log_buffer_hex
15 #define BYTES_PER_LINE 16
16 
esp_log_buffer_hex_internal(const char * tag,const void * buffer,uint16_t buff_len,esp_log_level_t log_level)17 void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t buff_len,
18                                  esp_log_level_t log_level)
19 {
20     if (buff_len == 0) {
21         return;
22     }
23     char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
24     char hex_buffer[3 * BYTES_PER_LINE + 1];
25     const char *ptr_line;
26     int bytes_cur_line;
27 
28     do {
29         if (buff_len > BYTES_PER_LINE) {
30             bytes_cur_line = BYTES_PER_LINE;
31         } else {
32             bytes_cur_line = buff_len;
33         }
34         if (!esp_ptr_byte_accessible(buffer)) {
35             //use memcpy to get around alignment issue
36             memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
37             ptr_line = temp_buffer;
38         } else {
39             ptr_line = buffer;
40         }
41 
42         for (int i = 0; i < bytes_cur_line; i ++) {
43             sprintf(hex_buffer + 3 * i, "%02x ", ptr_line[i]);
44         }
45         ESP_LOG_LEVEL(log_level, tag, "%s", hex_buffer);
46         buffer += bytes_cur_line;
47         buff_len -= bytes_cur_line;
48     } while (buff_len);
49 }
50 
esp_log_buffer_char_internal(const char * tag,const void * buffer,uint16_t buff_len,esp_log_level_t log_level)51 void esp_log_buffer_char_internal(const char *tag, const void *buffer, uint16_t buff_len,
52                                   esp_log_level_t log_level)
53 {
54     if (buff_len == 0) {
55         return;
56     }
57     char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
58     char char_buffer[BYTES_PER_LINE + 1];
59     const char *ptr_line;
60     int bytes_cur_line;
61 
62     do {
63         if (buff_len > BYTES_PER_LINE) {
64             bytes_cur_line = BYTES_PER_LINE;
65         } else {
66             bytes_cur_line = buff_len;
67         }
68         if (!esp_ptr_byte_accessible(buffer)) {
69             //use memcpy to get around alignment issue
70             memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
71             ptr_line = temp_buffer;
72         } else {
73             ptr_line = buffer;
74         }
75 
76         for (int i = 0; i < bytes_cur_line; i ++) {
77             sprintf(char_buffer + i, "%c", ptr_line[i]);
78         }
79         ESP_LOG_LEVEL(log_level, tag, "%s", char_buffer);
80         buffer += bytes_cur_line;
81         buff_len -= bytes_cur_line;
82     } while (buff_len);
83 }
84 
esp_log_buffer_hexdump_internal(const char * tag,const void * buffer,uint16_t buff_len,esp_log_level_t log_level)85 void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
86 {
87 
88     if (buff_len == 0) {
89         return;
90     }
91     char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
92     const char *ptr_line;
93     //format: field[length]
94     // ADDR[10]+"   "+DATA_HEX[8*3]+" "+DATA_HEX[8*3]+"  |"+DATA_CHAR[8]+"|"
95     char hd_buffer[10 + 3 + BYTES_PER_LINE * 3 + 3 + BYTES_PER_LINE + 1 + 1];
96     char *ptr_hd;
97     int bytes_cur_line;
98 
99     do {
100         if (buff_len > BYTES_PER_LINE) {
101             bytes_cur_line = BYTES_PER_LINE;
102         } else {
103             bytes_cur_line = buff_len;
104         }
105         if (!esp_ptr_byte_accessible(buffer)) {
106             //use memcpy to get around alignment issue
107             memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
108             ptr_line = temp_buffer;
109         } else {
110             ptr_line = buffer;
111         }
112         ptr_hd = hd_buffer;
113 
114         ptr_hd += sprintf(ptr_hd, "%p ", buffer);
115         for (int i = 0; i < BYTES_PER_LINE; i ++) {
116             if ((i & 7) == 0) {
117                 ptr_hd += sprintf(ptr_hd, " ");
118             }
119             if (i < bytes_cur_line) {
120                 ptr_hd += sprintf(ptr_hd, " %02x", ptr_line[i]);
121             } else {
122                 ptr_hd += sprintf(ptr_hd, "   ");
123             }
124         }
125         ptr_hd += sprintf(ptr_hd, "  |");
126         for (int i = 0; i < bytes_cur_line; i ++) {
127             if (isprint((int)ptr_line[i])) {
128                 ptr_hd += sprintf(ptr_hd, "%c", ptr_line[i]);
129             } else {
130                 ptr_hd += sprintf(ptr_hd, ".");
131             }
132         }
133         ptr_hd += sprintf(ptr_hd, "|");
134 
135         ESP_LOG_LEVEL(log_level, tag, "%s", hd_buffer);
136         buffer += bytes_cur_line;
137         buff_len -= bytes_cur_line;
138     } while (buff_len);
139 }
140