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