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 "OverrideLog.h"
19 #include "android_logmsg.h"
20 #include "nfc_target.h"
21 #include "buildcfg.h"
22 #include <cutils/log.h>
23 
24 
25 extern UINT32 ScrProtocolTraceFlag;
26 #define MAX_NCI_PACKET_SIZE 259
27 #define BTE_LOG_BUF_SIZE 1024
28 #define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12)
29 #define MAX_LOGCAT_LINE 4096
30 #define PRINT(s) __android_log_write (ANDROID_LOG_DEBUG, "BrcmNci", s)
31 static char log_line [MAX_LOGCAT_LINE];
32 static const char* sTable = "0123456789abcdef";
33 static BOOLEAN sIsUseRaw = FALSE;
34 static void ToHex (const UINT8* data, UINT16 len, char* hexString, UINT16 hexStringSize);
35 static void dumpbin (const char* data, int size, UINT32 trace_layer, UINT32 trace_type);
36 static inline void word2hex (const char* data, char** hex);
37 static inline void byte2char (const char* data, char** str);
38 static inline void byte2hex (const char* data, char** str);
39 
40 
BTDISP_LOCK_LOG()41 void BTDISP_LOCK_LOG()
42 {
43 }
44 
45 
BTDISP_UNLOCK_LOG()46 void BTDISP_UNLOCK_LOG()
47 {
48 }
49 
50 
BTDISP_INIT_LOCK()51 void BTDISP_INIT_LOCK()
52 {
53 }
54 
55 
BTDISP_UNINIT_LOCK()56 void BTDISP_UNINIT_LOCK()
57 {
58 }
59 
60 
ProtoDispAdapterUseRawOutput(BOOLEAN isUseRaw)61 void ProtoDispAdapterUseRawOutput (BOOLEAN isUseRaw)
62 {
63     sIsUseRaw = isUseRaw;
64 }
65 
66 
ProtoDispAdapterDisplayNciPacket(UINT8 * nciPacket,UINT16 nciPacketLen,BOOLEAN is_recv)67 void ProtoDispAdapterDisplayNciPacket (UINT8 *nciPacket, UINT16 nciPacketLen, BOOLEAN is_recv)
68 {
69     //Protocol decoder is not available, so decode NCI packet into hex numbers.
70     if (!(ScrProtocolTraceFlag & SCR_PROTO_TRACE_NCI))
71         return;
72     char line_buf [(MAX_NCI_PACKET_SIZE*2)+1];
73     ToHex (nciPacket, nciPacketLen, line_buf, sizeof(line_buf));
74     __android_log_write (ANDROID_LOG_DEBUG, (is_recv) ? "BrcmNciR": "BrcmNciX", line_buf);
75 }
76 
77 
ToHex(const UINT8 * data,UINT16 len,char * hexString,UINT16 hexStringSize)78 void ToHex (const UINT8* data, UINT16 len, char* hexString, UINT16 hexStringSize)
79 {
80     int i=0, j=0;
81     for(i = 0, j = 0; i < len && j < hexStringSize-3; i++)
82     {
83         hexString [j++] = sTable [(*data >> 4) & 0xf];
84         hexString [j++] = sTable [*data & 0xf];
85         data++;
86     }
87     hexString [j] = '\0';
88 }
89 
90 
91 //Protodisp code calls ScrLog() to print decoded texts.
ScrLog(UINT32 trace_set_mask,const char * fmt_str,...)92 void ScrLog (UINT32 trace_set_mask, const char *fmt_str, ...)
93 {
94     static char buffer [BTE_LOG_BUF_SIZE];
95     va_list ap;
96 
97     va_start (ap, fmt_str);
98     vsnprintf (buffer, BTE_LOG_MAX_SIZE, fmt_str, ap);
99     va_end (ap);
100     __android_log_write(ANDROID_LOG_INFO, "BrcmNci", buffer);
101 }
102 
103 
scru_dump_hex(UINT8 * p,char * pTitle,UINT32 len,UINT32 layer,UINT32 type)104 UINT8 *scru_dump_hex (UINT8 *p, char *pTitle, UINT32 len, UINT32 layer, UINT32 type)
105 {
106     if(pTitle && *pTitle)
107         PRINT(pTitle);
108     dumpbin((char*) p, len, layer, type);
109     return p;
110 }
111 
112 
dumpbin(const char * data,int size,UINT32 trace_layer,UINT32 trace_type)113 void dumpbin(const char* data, int size, UINT32 trace_layer, UINT32 trace_type)
114 {
115     char line_buff[256];
116     char *line;
117     int i, j, addr;
118     const int width = 16;
119     if(size <= 0)
120         return;
121     for(i = 0; i < size / width; i++)
122     {
123         line = line_buff;
124         //write address:
125         addr = i*width;
126         word2hex((const char*)&addr, &line);
127         *line++ = ':'; *line++ = ' ';
128         //write hex of data
129         for(j = 0; j < width; j++)
130         {
131             byte2hex(&data[j], &line);
132             *line++ = ' ';
133         }
134         //write char of data
135         for(j = 0; j < width; j++)
136             byte2char(data++, &line);
137         //wirte the end of line
138         *line = 0;
139         //output the line
140         PRINT(line_buff);
141     }
142     //last line of left over if any
143     int leftover = size % width;
144     if(leftover > 0)
145     {
146         line = line_buff;
147         //write address:
148         addr = i*width;
149         word2hex((const char*)&addr, &line);
150         *line++ = ':'; *line++ = ' ';
151         //write hex of data
152         for(j = 0; j < leftover; j++)
153         {
154             byte2hex(&data[j], &line);
155             *line++ = ' ';
156         }
157         //write hex padding
158         for(; j < width; j++)
159         {
160             *line++ = ' ';
161             *line++ = ' ';
162             *line++ = ' ';
163         }
164         //write char of data
165         for(j = 0; j < leftover; j++)
166             byte2char(data++, &line);
167         //write the end of line
168         *line = 0;
169         //output the line
170         PRINT(line_buff);
171     }
172 }
173 
174 
word2hex(const char * data,char ** hex)175 inline void word2hex (const char* data, char** hex)
176 {
177     byte2hex(&data[1], hex);
178     byte2hex(&data[0], hex);
179 }
180 
181 
byte2char(const char * data,char ** str)182 inline void byte2char (const char* data, char** str)
183 {
184     **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data;
185     ++(*str);
186 }
187 
188 
byte2hex(const char * data,char ** str)189 inline void byte2hex (const char* data, char** str)
190 {
191     **str = sTable[(*data >> 4) & 0xf];
192     ++*str;
193     **str = sTable[*data & 0xf];
194     ++*str;
195 }
196 
197 
198     //Decode a few Bluetooth HCI packets into hex numbers.
DispHciCmd(BT_HDR * p_buf)199     void DispHciCmd (BT_HDR *p_buf)
200     {
201         UINT32 nBytes = ((BT_HDR_SIZE + p_buf->offset + p_buf->len)*2)+1;
202         UINT8* data = (UINT8*) p_buf;
203         int data_len = BT_HDR_SIZE + p_buf->offset + p_buf->len;
204 
205         if (appl_trace_level < BT_TRACE_LEVEL_DEBUG)
206             return;
207 
208         if (nBytes > sizeof(log_line))
209             return;
210 
211         ToHex (data, data_len, log_line, sizeof(log_line));
212         __android_log_write (ANDROID_LOG_DEBUG, "BrcmHciX", log_line);
213     }
214 
215 
216     //Decode a few Bluetooth HCI packets into hex numbers.
DispHciEvt(BT_HDR * p_buf)217     void DispHciEvt (BT_HDR *p_buf)
218     {
219         UINT32 nBytes = ((BT_HDR_SIZE + p_buf->offset + p_buf->len)*2)+1;
220         UINT8* data = (UINT8*) p_buf;
221         int data_len = BT_HDR_SIZE + p_buf->offset + p_buf->len;
222 
223         if (appl_trace_level < BT_TRACE_LEVEL_DEBUG)
224             return;
225 
226         if (nBytes > sizeof(log_line))
227             return;
228 
229         ToHex (data, data_len, log_line, sizeof(log_line));
230         __android_log_write (ANDROID_LOG_DEBUG, "BrcmHciR", log_line);
231     }
232 
233 
234     /*******************************************************************************
235     **
236     ** Function         DispLLCP
237     **
238     ** Description      Log LLCP packet as hex-ascii bytes.
239     **
240     ** Returns          None.
241     **
242     *******************************************************************************/
DispLLCP(BT_HDR * p_buf,BOOLEAN is_recv)243     void DispLLCP (BT_HDR *p_buf, BOOLEAN is_recv)
244     {
245         UINT32 nBytes = ((BT_HDR_SIZE + p_buf->offset + p_buf->len)*2)+1;
246         UINT8 * data = (UINT8*) p_buf;
247         int data_len = BT_HDR_SIZE + p_buf->offset + p_buf->len;
248 
249         if (appl_trace_level < BT_TRACE_LEVEL_DEBUG)
250             return;
251 
252         if (nBytes > sizeof(log_line))
253             return;
254 
255         ToHex (data, data_len, log_line, sizeof(log_line));
256         __android_log_write (ANDROID_LOG_DEBUG, (is_recv) ? "BrcmLlcpR": "BrcmLlcpX", log_line);
257     }
258 
259 
260     /*******************************************************************************
261     **
262     ** Function         DispHcp
263     **
264     ** Description      Log raw HCP packet as hex-ascii bytes
265     **
266     ** Returns          None.
267     **
268     *******************************************************************************/
DispHcp(UINT8 * data,UINT16 len,BOOLEAN is_recv)269     void DispHcp (UINT8 *data, UINT16 len, BOOLEAN is_recv)
270     {
271         UINT32 nBytes = (len*2)+1;
272 
273         if (appl_trace_level < BT_TRACE_LEVEL_DEBUG)
274             return;
275 
276         // Only trace HCP if we're tracing HCI as well
277         if (!(ScrProtocolTraceFlag & SCR_PROTO_TRACE_HCI_SUMMARY))
278             return;
279 
280         if (nBytes > sizeof(log_line))
281             return;
282 
283         ToHex (data, len, log_line, sizeof(log_line));
284         __android_log_write (ANDROID_LOG_DEBUG, (is_recv) ? "BrcmHcpR": "BrcmHcpX", log_line);
285     }
286 
287 
DispSNEP(UINT8 local_sap,UINT8 remote_sap,BT_HDR * p_buf,BOOLEAN is_first,BOOLEAN is_rx)288     void DispSNEP (UINT8 local_sap, UINT8 remote_sap, BT_HDR *p_buf, BOOLEAN is_first, BOOLEAN is_rx) {}
DispCHO(UINT8 * pMsg,UINT32 MsgLen,BOOLEAN is_rx)289     void DispCHO (UINT8 *pMsg, UINT32 MsgLen, BOOLEAN is_rx) {}
DispT3TagMessage(BT_HDR * p_msg,BOOLEAN is_rx)290     void DispT3TagMessage(BT_HDR *p_msg, BOOLEAN is_rx) {}
DispRWT4Tags(BT_HDR * p_buf,BOOLEAN is_rx)291     void DispRWT4Tags (BT_HDR *p_buf, BOOLEAN is_rx) {}
DispCET4Tags(BT_HDR * p_buf,BOOLEAN is_rx)292     void DispCET4Tags (BT_HDR *p_buf, BOOLEAN is_rx) {}
DispRWI93Tag(BT_HDR * p_buf,BOOLEAN is_rx,UINT8 command_to_respond)293     void DispRWI93Tag (BT_HDR *p_buf, BOOLEAN is_rx, UINT8 command_to_respond) {}
DispNDEFMsg(UINT8 * pMsg,UINT32 MsgLen,BOOLEAN is_recv)294     void DispNDEFMsg (UINT8 *pMsg, UINT32 MsgLen, BOOLEAN is_recv) {}
295 
296 
297 /*******************************************************************************
298 **
299 ** Function:        LogMsg
300 **
301 ** Description:     Print messages from NFC stack.
302 **
303 ** Returns:         None.
304 **
305 *******************************************************************************/
LogMsg(UINT32 trace_set_mask,const char * fmt_str,...)306 void LogMsg (UINT32 trace_set_mask, const char *fmt_str, ...)
307 {
308     static char buffer [BTE_LOG_BUF_SIZE];
309     va_list ap;
310     UINT32 trace_type = trace_set_mask & 0x07; //lower 3 bits contain trace type
311     int android_log_type = ANDROID_LOG_INFO;
312 
313     va_start (ap, fmt_str);
314     vsnprintf (buffer, BTE_LOG_MAX_SIZE, fmt_str, ap);
315     va_end (ap);
316     if (trace_type == TRACE_TYPE_ERROR)
317         android_log_type = ANDROID_LOG_ERROR;
318     __android_log_write (android_log_type, LOGMSG_TAG_NAME, buffer);
319 }
320 
321 
LogMsg_0(UINT32 maskTraceSet,const char * p_str)322 void LogMsg_0 (UINT32 maskTraceSet, const char *p_str)
323 {
324     LogMsg (maskTraceSet, p_str);
325 }
326 
327 
LogMsg_1(UINT32 maskTraceSet,const char * fmt_str,UINT32 p1)328 void LogMsg_1 (UINT32 maskTraceSet, const char *fmt_str, UINT32 p1)
329 {
330     LogMsg (maskTraceSet, fmt_str, p1);
331 }
332 
333 
LogMsg_2(UINT32 maskTraceSet,const char * fmt_str,UINT32 p1,UINT32 p2)334 void LogMsg_2 (UINT32 maskTraceSet, const char *fmt_str, UINT32 p1, UINT32 p2)
335 {
336     LogMsg (maskTraceSet, fmt_str, p1, p2);
337 }
338 
339 
LogMsg_3(UINT32 maskTraceSet,const char * fmt_str,UINT32 p1,UINT32 p2,UINT32 p3)340 void LogMsg_3 (UINT32 maskTraceSet, const char *fmt_str, UINT32 p1, UINT32 p2, UINT32 p3)
341 {
342     LogMsg (maskTraceSet, fmt_str, p1, p2, p3);
343 }
344 
345 
LogMsg_4(UINT32 maskTraceSet,const char * fmt_str,UINT32 p1,UINT32 p2,UINT32 p3,UINT32 p4)346 void LogMsg_4 (UINT32 maskTraceSet, const char *fmt_str, UINT32 p1, UINT32 p2, UINT32 p3, UINT32 p4)
347 {
348     LogMsg (maskTraceSet, fmt_str, p1, p2, p3, p4);
349 }
350 
LogMsg_5(UINT32 maskTraceSet,const char * fmt_str,UINT32 p1,UINT32 p2,UINT32 p3,UINT32 p4,UINT32 p5)351 void LogMsg_5 (UINT32 maskTraceSet, const char *fmt_str, UINT32 p1, UINT32 p2, UINT32 p3, UINT32 p4, UINT32 p5)
352 {
353     LogMsg (maskTraceSet, fmt_str, p1, p2, p3, p4, p5);
354 }
355 
356 
LogMsg_6(UINT32 maskTraceSet,const char * fmt_str,UINT32 p1,UINT32 p2,UINT32 p3,UINT32 p4,UINT32 p5,UINT32 p6)357 void LogMsg_6 (UINT32 maskTraceSet, const char *fmt_str, UINT32 p1, UINT32 p2, UINT32 p3, UINT32 p4, UINT32 p5, UINT32 p6)
358 {
359     LogMsg (maskTraceSet, fmt_str, p1, p2, p3, p4, p5, p6);
360 }
361