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
24extern 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)
30static char log_line[MAX_LOGCAT_LINE];
31static const char* sTable = "0123456789abcdef";
32static bool sIsUseRaw = FALSE;
33static void ToHex(const uint8_t* data, uint16_t len, char* hexString,
34                  uint16_t hexStringSize);
35static void dumpbin(const char* data, int size, uint32_t trace_layer,
36                    uint32_t trace_type);
37static inline void word2hex(const char* data, char** hex);
38static inline void byte2char(const char* data, char** str);
39static inline void byte2hex(const char* data, char** str);
40
41void BTDISP_LOCK_LOG() {}
42
43void BTDISP_UNLOCK_LOG() {}
44
45void BTDISP_INIT_LOCK() {}
46
47void BTDISP_UNINIT_LOCK() {}
48
49void ProtoDispAdapterUseRawOutput(bool isUseRaw) { sIsUseRaw = isUseRaw; }
50
51void 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
61void 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.
73void 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
83uint8_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
90void 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
145inline void word2hex(const char* data, char** hex) {
146  byte2hex(&data[1], hex);
147  byte2hex(&data[0], hex);
148}
149
150inline void byte2char(const char* data, char** str) {
151  **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data;
152  ++(*str);
153}
154
155inline 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.
163void 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.
177void 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*******************************************************************************/
199void 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*******************************************************************************/
222void 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
237void DispSNEP(uint8_t local_sap, uint8_t remote_sap, NFC_HDR* p_buf,
238              bool is_first, bool is_rx) {}
239void DispCHO(uint8_t* pMsg, uint32_t MsgLen, bool is_rx) {}
240void DispT3TagMessage(NFC_HDR* p_msg, bool is_rx) {}
241void DispRWT4Tags(NFC_HDR* p_buf, bool is_rx) {}
242void DispCET4Tags(NFC_HDR* p_buf, bool is_rx) {}
243void DispRWI93Tag(NFC_HDR* p_buf, bool is_rx, uint8_t command_to_respond) {}
244void 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*******************************************************************************/
255void 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
269void LogMsg_0(uint32_t maskTraceSet, const char* p_str) {
270  LogMsg(maskTraceSet, p_str);
271}
272
273void LogMsg_1(uint32_t maskTraceSet, const char* fmt_str, uintptr_t p1) {
274  LogMsg(maskTraceSet, fmt_str, p1);
275}
276
277void 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
282void 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
287void 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
292void 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
297void 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