1 /*
2 * wpa_supplicant/hostapd / Debug prints
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8 #ifdef ESP_SUPPLICANT
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "utils/wpa_debug.h"
12
13 static inline int
_wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len,int uppercase,int whitespace)14 _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len,
15 int uppercase, int whitespace)
16 {
17 size_t i;
18 char *pos = buf, *end = buf + buf_size;
19 int ret;
20
21 static const char *fmt_upper = "%02X";
22 static const char *fmt_lower = "%02x";
23 static const char *fmt_upper_ws = "%02X ";
24 static const char *fmt_lower_ws = "%02x ";
25 const char *fmt = uppercase ? (whitespace ? fmt_upper_ws : fmt_upper) :
26 (whitespace ? fmt_lower_ws : fmt_lower);
27
28 if (buf_size == 0)
29 return 0;
30
31 for (i = 0; i < len; i++) {
32 ret = snprintf(pos, end - pos, fmt, data[i]);
33 if (ret < 0 || ret >= end - pos) {
34 end[-1] = '\0';
35 return pos - buf;
36 }
37 pos += ret;
38 }
39 end[-1]='\0';
40 return pos - buf;
41 }
42
wpa_snprintf_hex_uppercase(char * buf,size_t buf_size,const u8 * data,size_t len)43 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, size_t len)
44 {
45 return _wpa_snprintf_hex(buf, buf_size, data, len, 1, 0);
46 }
47
wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len)48 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
49 {
50 return _wpa_snprintf_hex(buf, buf_size, data, len, 0, 0);
51 }
52
53 #ifdef DEBUG_PRINT
wpa_dump_mem(char * desc,uint8_t * addr,uint16_t len)54 void wpa_dump_mem(char* desc, uint8_t *addr, uint16_t len)
55 {
56 char output[50];
57 wpa_printf(MSG_DEBUG, "%s", desc);
58 if (addr){
59 uint16_t i=0;
60 for (i = 0; i < len / 16; i++) {
61 _wpa_snprintf_hex(output, 50, addr + i * 16, 16, 0, 1);
62 wpa_printf(MSG_DEBUG, "%s", output);
63 }
64 if (len % 16) {
65 int bytes_printed = (len / 16) * 16;
66 _wpa_snprintf_hex(output, 50, addr + bytes_printed,
67 len - bytes_printed, 0, 1);
68 wpa_printf(MSG_DEBUG, "%s", output);
69 }
70 }
71 }
72
wpa_debug_print_timestamp(void)73 void wpa_debug_print_timestamp(void)
74 {
75 #ifdef DEBUG_PRINT
76 struct os_time tv;
77 os_get_time(&tv);
78 wpa_printf(MSG_DEBUG, "%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
79 #endif
80 }
81
wpa_hexdump(int level,const char * title,const u8 * buf,size_t len)82 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
83 {
84 #ifdef DEBUG_PRINT
85 size_t i;
86 char output[50];
87
88 if (level >= MSG_ERROR)
89 return;
90
91 wpa_printf(level, "%s - hexdump(len=%lu):", title, (unsigned long) len);
92 if (buf == NULL) {
93 wpa_printf(level, " [NULL]");
94 } else {
95 for (i = 0; i < len / 16; i++) {
96 _wpa_snprintf_hex(output, 50, buf + i * 16, 16, 0, 1);
97 wpa_printf(level, "%s", output);
98 }
99 if (len % 16) {
100 int bytes_printed = (len / 16) * 16;
101 _wpa_snprintf_hex(output, 50, buf + bytes_printed,
102 len - bytes_printed, 0, 1);
103 wpa_printf(level, "%s", output);
104 }
105 }
106 #endif
107 }
108
wpa_hexdump_key(int level,const char * title,const u8 * buf,size_t len)109 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
110 {
111 wpa_hexdump(level, title, buf, len);
112 }
113 #endif
114
115 #endif // ESP_SUPPLICANT
116