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-base/stringprintf.h> 19 #include <base/logging.h> 20 #include <cutils/log.h> 21 22 #include "android_logmsg.h" 23 #include "buildcfg.h" 24 25 using android::base::StringPrintf; 26 27 extern bool nfc_debug_enabled; 28 29 #define MAX_NCI_PACKET_SIZE 259 30 #define BTE_LOG_BUF_SIZE 1024 31 #define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12) 32 #define MAX_LOGCAT_LINE 4096 33 #define PRINT(s) __android_log_write(ANDROID_LOG_DEBUG, "BrcmNci", s) 34 static char log_line[MAX_LOGCAT_LINE]; 35 static const char* sTable = "0123456789abcdef"; 36 37 static void ToHex(const uint8_t* data, uint16_t len, char* hexString, 38 uint16_t hexStringSize); 39 40 void ProtoDispAdapterDisplayNciPacket(uint8_t* nciPacket, uint16_t nciPacketLen, 41 bool is_recv) { 42 if (!nfc_debug_enabled) return; 43 44 char line_buf[(MAX_NCI_PACKET_SIZE * 2) + 1]; 45 ToHex(nciPacket, nciPacketLen, line_buf, sizeof(line_buf)); 46 DLOG_IF(INFO, nfc_debug_enabled) 47 << StringPrintf("%s:%s", is_recv ? "NciR" : "NciX", line_buf); 48 } 49 50 void ToHex(const uint8_t* data, uint16_t len, char* hexString, 51 uint16_t hexStringSize) { 52 int i = 0, j = 0; 53 for (i = 0, j = 0; i < len && j < hexStringSize - 3; i++) { 54 hexString[j++] = sTable[(*data >> 4) & 0xf]; 55 hexString[j++] = sTable[*data & 0xf]; 56 data++; 57 } 58 hexString[j] = '\0'; 59 } 60 61 inline void byte2hex(const char* data, char** str) { 62 **str = sTable[(*data >> 4) & 0xf]; 63 ++*str; 64 **str = sTable[*data & 0xf]; 65 ++*str; 66 } 67 68 /*************************************************************************** 69 ** 70 ** Function DispLLCP 71 ** 72 ** Description Log LLCP packet as hex-ascii bytes. 73 ** 74 ** Returns None. 75 ** 76 ***************************************************************************/ 77 void DispLLCP(NFC_HDR* p_buf, bool is_recv) { 78 if (!nfc_debug_enabled) return; 79 80 uint32_t nBytes = ((NFC_HDR_SIZE + p_buf->offset + p_buf->len) * 2) + 1; 81 if (nBytes > sizeof(log_line)) return; 82 83 uint8_t* data = (uint8_t*)p_buf; 84 int data_len = NFC_HDR_SIZE + p_buf->offset + p_buf->len; 85 ToHex(data, data_len, log_line, sizeof(log_line)); 86 DLOG_IF(INFO, nfc_debug_enabled) 87 << StringPrintf("%s:%s", is_recv ? "LlcpR" : "LlcpX", log_line); 88 } 89 90 /*************************************************************************** 91 ** 92 ** Function DispHcp 93 ** 94 ** Description Log raw HCP packet as hex-ascii bytes 95 ** 96 ** Returns None. 97 ** 98 ***************************************************************************/ 99 void DispHcp(uint8_t* data, uint16_t len, bool is_recv) { 100 if (!nfc_debug_enabled) return; 101 102 uint32_t nBytes = (len * 2) + 1; 103 if (nBytes > sizeof(log_line)) return; 104 105 ToHex(data, len, log_line, sizeof(log_line)); 106 DLOG_IF(INFO, nfc_debug_enabled) 107 << StringPrintf("%s:%s", is_recv ? "HcpR" : "HcpX", log_line); 108 } 109