1 /******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 #include "android_logmsg.h"
19 #include <cutils/log.h>
20 #include "_OverrideLog.h"
21 #include "buildcfg.h"
22 #include "nfc_target.h"
23
24 extern uint32_t ScrProtocolTraceFlag;
25 #define MAX_NCI_PACKET_SIZE 259
26 #define BTE_LOG_BUF_SIZE 1024
27 #define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12)
28 #define MAX_LOGCAT_LINE 4096
29 #define PRINT(s) __android_log_write(ANDROID_LOG_DEBUG, "BrcmNci", s)
30 static char log_line[MAX_LOGCAT_LINE];
31 static const char* sTable = "0123456789abcdef";
32 static bool sIsUseRaw = FALSE;
33 static void ToHex(const uint8_t* data, uint16_t len, char* hexString,
34 uint16_t hexStringSize);
35 static void dumpbin(const char* data, int size, uint32_t trace_layer,
36 uint32_t trace_type);
37 static inline void word2hex(const char* data, char** hex);
38 static inline void byte2char(const char* data, char** str);
39 static inline void byte2hex(const char* data, char** str);
40
BTDISP_LOCK_LOG()41 void BTDISP_LOCK_LOG() {}
42
BTDISP_UNLOCK_LOG()43 void BTDISP_UNLOCK_LOG() {}
44
BTDISP_INIT_LOCK()45 void BTDISP_INIT_LOCK() {}
46
BTDISP_UNINIT_LOCK()47 void BTDISP_UNINIT_LOCK() {}
48
ProtoDispAdapterUseRawOutput(bool isUseRaw)49 void ProtoDispAdapterUseRawOutput(bool isUseRaw) { sIsUseRaw = isUseRaw; }
50
ProtoDispAdapterDisplayNciPacket(uint8_t * nciPacket,uint16_t nciPacketLen,bool is_recv)51 void ProtoDispAdapterDisplayNciPacket(uint8_t* nciPacket, uint16_t nciPacketLen,
52 bool is_recv) {
53 // Protocol decoder is not available, so decode NCI packet into hex numbers.
54 if (!(ScrProtocolTraceFlag & SCR_PROTO_TRACE_NCI)) return;
55 char line_buf[(MAX_NCI_PACKET_SIZE * 2) + 1];
56 ToHex(nciPacket, nciPacketLen, line_buf, sizeof(line_buf));
57 __android_log_write(ANDROID_LOG_DEBUG, (is_recv) ? "BrcmNciR" : "BrcmNciX",
58 line_buf);
59 }
60
ToHex(const uint8_t * data,uint16_t len,char * hexString,uint16_t hexStringSize)61 void ToHex(const uint8_t* data, uint16_t len, char* hexString,
62 uint16_t hexStringSize) {
63 int i = 0, j = 0;
64 for (i = 0, j = 0; i < len && j < hexStringSize - 3; i++) {
65 hexString[j++] = sTable[(*data >> 4) & 0xf];
66 hexString[j++] = sTable[*data & 0xf];
67 data++;
68 }
69 hexString[j] = '\0';
70 }
71
72 // Protodisp code calls ScrLog() to print decoded texts.
ScrLog(uint32_t trace_set_mask,const char * fmt_str,...)73 void ScrLog(uint32_t trace_set_mask, const char* fmt_str, ...) {
74 static char buffer[BTE_LOG_BUF_SIZE];
75 va_list ap;
76
77 va_start(ap, fmt_str);
78 vsnprintf(buffer, BTE_LOG_MAX_SIZE, fmt_str, ap);
79 va_end(ap);
80 __android_log_write(ANDROID_LOG_INFO, "BrcmNci", buffer);
81 }
82
scru_dump_hex(uint8_t * p,char * pTitle,uint32_t len,uint32_t layer,uint32_t type)83 uint8_t* scru_dump_hex(uint8_t* p, char* pTitle, uint32_t len, uint32_t layer,
84 uint32_t type) {
85 if (pTitle && *pTitle) PRINT(pTitle);
86 dumpbin((char*)p, len, layer, type);
87 return p;
88 }
89
dumpbin(const char * data,int size,uint32_t trace_layer,uint32_t trace_type)90 void dumpbin(const char* data, int size, uint32_t trace_layer,
91 uint32_t trace_type) {
92 char line_buff[256];
93 char* line;
94 int i, j, addr;
95 const int width = 16;
96 if (size <= 0) return;
97 for (i = 0; i < size / width; i++) {
98 line = line_buff;
99 // write address:
100 addr = i * width;
101 word2hex((const char*)&addr, &line);
102 *line++ = ':';
103 *line++ = ' ';
104 // write hex of data
105 for (j = 0; j < width; j++) {
106 byte2hex(&data[j], &line);
107 *line++ = ' ';
108 }
109 // write char of data
110 for (j = 0; j < width; j++) byte2char(data++, &line);
111 // wirte the end of line
112 *line = 0;
113 // output the line
114 PRINT(line_buff);
115 }
116 // last line of left over if any
117 int leftover = size % width;
118 if (leftover > 0) {
119 line = line_buff;
120 // write address:
121 addr = i * width;
122 word2hex((const char*)&addr, &line);
123 *line++ = ':';
124 *line++ = ' ';
125 // write hex of data
126 for (j = 0; j < leftover; j++) {
127 byte2hex(&data[j], &line);
128 *line++ = ' ';
129 }
130 // write hex padding
131 for (; j < width; j++) {
132 *line++ = ' ';
133 *line++ = ' ';
134 *line++ = ' ';
135 }
136 // write char of data
137 for (j = 0; j < leftover; j++) byte2char(data++, &line);
138 // write the end of line
139 *line = 0;
140 // output the line
141 PRINT(line_buff);
142 }
143 }
144
word2hex(const char * data,char ** hex)145 inline void word2hex(const char* data, char** hex) {
146 byte2hex(&data[1], hex);
147 byte2hex(&data[0], hex);
148 }
149
byte2char(const char * data,char ** str)150 inline void byte2char(const char* data, char** str) {
151 **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data;
152 ++(*str);
153 }
154
byte2hex(const char * data,char ** str)155 inline void byte2hex(const char* data, char** str) {
156 **str = sTable[(*data >> 4) & 0xf];
157 ++*str;
158 **str = sTable[*data & 0xf];
159 ++*str;
160 }
161
162 // Decode a few Bluetooth HCI packets into hex numbers.
DispHciCmd(NFC_HDR * p_buf)163 void DispHciCmd(NFC_HDR* p_buf) {
164 uint32_t nBytes = ((NFC_HDR_SIZE + p_buf->offset + p_buf->len) * 2) + 1;
165 uint8_t* data = (uint8_t*)p_buf;
166 int data_len = NFC_HDR_SIZE + p_buf->offset + p_buf->len;
167
168 if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
169
170 if (nBytes > sizeof(log_line)) return;
171
172 ToHex(data, data_len, log_line, sizeof(log_line));
173 __android_log_write(ANDROID_LOG_DEBUG, "BrcmHciX", log_line);
174 }
175
176 // Decode a few Bluetooth HCI packets into hex numbers.
DispHciEvt(NFC_HDR * p_buf)177 void DispHciEvt(NFC_HDR* p_buf) {
178 uint32_t nBytes = ((NFC_HDR_SIZE + p_buf->offset + p_buf->len) * 2) + 1;
179 uint8_t* data = (uint8_t*)p_buf;
180 int data_len = NFC_HDR_SIZE + p_buf->offset + p_buf->len;
181
182 if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
183
184 if (nBytes > sizeof(log_line)) return;
185
186 ToHex(data, data_len, log_line, sizeof(log_line));
187 __android_log_write(ANDROID_LOG_DEBUG, "BrcmHciR", log_line);
188 }
189
190 /*******************************************************************************
191 **
192 ** Function DispLLCP
193 **
194 ** Description Log LLCP packet as hex-ascii bytes.
195 **
196 ** Returns None.
197 **
198 *******************************************************************************/
DispLLCP(NFC_HDR * p_buf,bool is_recv)199 void DispLLCP(NFC_HDR* p_buf, bool is_recv) {
200 uint32_t nBytes = ((NFC_HDR_SIZE + p_buf->offset + p_buf->len) * 2) + 1;
201 uint8_t* data = (uint8_t*)p_buf;
202 int data_len = NFC_HDR_SIZE + p_buf->offset + p_buf->len;
203
204 if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
205
206 if (nBytes > sizeof(log_line)) return;
207
208 ToHex(data, data_len, log_line, sizeof(log_line));
209 __android_log_write(ANDROID_LOG_DEBUG, (is_recv) ? "BrcmLlcpR" : "BrcmLlcpX",
210 log_line);
211 }
212
213 /*******************************************************************************
214 **
215 ** Function DispHcp
216 **
217 ** Description Log raw HCP packet as hex-ascii bytes
218 **
219 ** Returns None.
220 **
221 *******************************************************************************/
DispHcp(uint8_t * data,uint16_t len,bool is_recv)222 void DispHcp(uint8_t* data, uint16_t len, bool is_recv) {
223 uint32_t nBytes = (len * 2) + 1;
224
225 if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
226
227 // Only trace HCP if we're tracing HCI as well
228 if (!(ScrProtocolTraceFlag & SCR_PROTO_TRACE_HCI_SUMMARY)) return;
229
230 if (nBytes > sizeof(log_line)) return;
231
232 ToHex(data, len, log_line, sizeof(log_line));
233 __android_log_write(ANDROID_LOG_DEBUG, (is_recv) ? "BrcmHcpR" : "BrcmHcpX",
234 log_line);
235 }
236
DispSNEP(uint8_t local_sap,uint8_t remote_sap,NFC_HDR * p_buf,bool is_first,bool is_rx)237 void DispSNEP(uint8_t local_sap, uint8_t remote_sap, NFC_HDR* p_buf,
238 bool is_first, bool is_rx) {}
DispCHO(uint8_t * pMsg,uint32_t MsgLen,bool is_rx)239 void DispCHO(uint8_t* pMsg, uint32_t MsgLen, bool is_rx) {}
DispT3TagMessage(NFC_HDR * p_msg,bool is_rx)240 void DispT3TagMessage(NFC_HDR* p_msg, bool is_rx) {}
DispRWT4Tags(NFC_HDR * p_buf,bool is_rx)241 void DispRWT4Tags(NFC_HDR* p_buf, bool is_rx) {}
DispCET4Tags(NFC_HDR * p_buf,bool is_rx)242 void DispCET4Tags(NFC_HDR* p_buf, bool is_rx) {}
DispRWI93Tag(NFC_HDR * p_buf,bool is_rx,uint8_t command_to_respond)243 void DispRWI93Tag(NFC_HDR* p_buf, bool is_rx, uint8_t command_to_respond) {}
DispNDEFMsg(uint8_t * pMsg,uint32_t MsgLen,bool is_recv)244 void DispNDEFMsg(uint8_t* pMsg, uint32_t MsgLen, bool is_recv) {}
245
246 /*******************************************************************************
247 **
248 ** Function: LogMsg
249 **
250 ** Description: Print messages from NFC stack.
251 **
252 ** Returns: None.
253 **
254 *******************************************************************************/
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)255 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {
256 static char buffer[BTE_LOG_BUF_SIZE];
257 va_list ap;
258 uint32_t trace_type =
259 trace_set_mask & 0x07; // lower 3 bits contain trace type
260 int android_log_type = ANDROID_LOG_INFO;
261
262 va_start(ap, fmt_str);
263 vsnprintf(buffer, BTE_LOG_MAX_SIZE, fmt_str, ap);
264 va_end(ap);
265 if (trace_type == TRACE_TYPE_ERROR) android_log_type = ANDROID_LOG_ERROR;
266 __android_log_write(android_log_type, LOGMSG_TAG_NAME, buffer);
267 }
268
LogMsg_0(uint32_t maskTraceSet,const char * p_str)269 void LogMsg_0(uint32_t maskTraceSet, const char* p_str) {
270 LogMsg(maskTraceSet, p_str);
271 }
272
LogMsg_1(uint32_t maskTraceSet,const char * fmt_str,uintptr_t p1)273 void LogMsg_1(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1) {
274 LogMsg(maskTraceSet, fmt_str, p1);
275 }
276
LogMsg_2(uint32_t maskTraceSet,const char * fmt_str,uintptr_t p1,uintptr_t p2)277 void LogMsg_2(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1,
278 uintptr_t p2) {
279 LogMsg(maskTraceSet, fmt_str, p1, p2);
280 }
281
LogMsg_3(uint32_t maskTraceSet,const char * fmt_str,uintptr_t p1,uintptr_t p2,uintptr_t p3)282 void LogMsg_3(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1,
283 uintptr_t p2, uintptr_t p3) {
284 LogMsg(maskTraceSet, fmt_str, p1, p2, p3);
285 }
286
LogMsg_4(uint32_t maskTraceSet,const char * fmt_str,uintptr_t p1,uintptr_t p2,uintptr_t p3,uintptr_t p4)287 void LogMsg_4(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1,
288 uintptr_t p2, uintptr_t p3, uintptr_t p4) {
289 LogMsg(maskTraceSet, fmt_str, p1, p2, p3, p4);
290 }
291
LogMsg_5(uint32_t maskTraceSet,const char * fmt_str,uintptr_t p1,uintptr_t p2,uintptr_t p3,uintptr_t p4,uintptr_t p5)292 void LogMsg_5(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1,
293 uintptr_t p2, uintptr_t p3, uintptr_t p4, uintptr_t p5) {
294 LogMsg(maskTraceSet, fmt_str, p1, p2, p3, p4, p5);
295 }
296
LogMsg_6(uint32_t maskTraceSet,const char * fmt_str,uintptr_t p1,uintptr_t p2,uintptr_t p3,uintptr_t p4,uintptr_t p5,uintptr_t p6)297 void LogMsg_6(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1,
298 uintptr_t p2, uintptr_t p3, uintptr_t p4, uintptr_t p5,
299 uintptr_t p6) {
300 LogMsg(maskTraceSet, fmt_str, p1, p2, p3, p4, p5, p6);
301 }
302