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