bte_logmsg.cc revision 9891f32471b5c01cf58e4d7dadb04cb3024a9a88
1/****************************************************************************** 2 * 3 * Copyright (C) 2001-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 19#include <assert.h> 20#include <cutils/log.h> 21#include <stdio.h> 22#include <stdlib.h> 23#include <string.h> 24#include <stdarg.h> 25#include <sys/time.h> 26#include <time.h> 27 28#include "bte.h" 29#include "bta_api.h" 30#include "bte_appl.h" 31#include "btu.h" 32#include "config.h" 33#include "gki.h" 34#include "l2c_api.h" 35#include "stack_config.h" 36 37#if (RFCOMM_INCLUDED==TRUE) 38#include "port_api.h" 39#endif 40#if (OBX_INCLUDED==TRUE) 41#include "obx_api.h" 42#endif 43#if (AVCT_INCLUDED==TRUE) 44#include "avct_api.h" 45#endif 46#if (AVDT_INCLUDED==TRUE) 47#include "avdt_api.h" 48#endif 49#if (AVRC_INCLUDED==TRUE) 50#include "avrc_api.h" 51#endif 52#if (AVDT_INCLUDED==TRUE) 53#include "avdt_api.h" 54#endif 55#if (A2D_INCLUDED==TRUE) 56#include "a2d_api.h" 57#endif 58#if (BIP_INCLUDED==TRUE) 59#include "bip_api.h" 60#endif 61#if (BNEP_INCLUDED==TRUE) 62#include "bnep_api.h" 63#endif 64#if (BPP_INCLUDED==TRUE) 65#include "bpp_api.h" 66#endif 67#include "btm_api.h" 68#if (DUN_INCLUDED==TRUE) 69#include "dun_api.h" 70#endif 71#if (GAP_INCLUDED==TRUE) 72#include "gap_api.h" 73#endif 74#if (GOEP_INCLUDED==TRUE) 75#include "goep_util.h" 76#endif 77#if (HCRP_INCLUDED==TRUE) 78#include "hcrp_api.h" 79#endif 80#if (PAN_INCLUDED==TRUE) 81#include "pan_api.h" 82#endif 83#include "sdp_api.h" 84 85#if (BLE_INCLUDED==TRUE) 86#include "gatt_api.h" 87#include "smp_api.h" 88#endif 89 90#define LOGI0(t,s) __android_log_write(ANDROID_LOG_INFO, t, s) 91#define LOGD0(t,s) __android_log_write(ANDROID_LOG_DEBUG, t, s) 92#define LOGW0(t,s) __android_log_write(ANDROID_LOG_WARN, t, s) 93#define LOGE0(t,s) __android_log_write(ANDROID_LOG_ERROR, t, s) 94 95#ifndef DEFAULT_CONF_TRACE_LEVEL 96#define DEFAULT_CONF_TRACE_LEVEL BT_TRACE_LEVEL_WARNING 97#endif 98 99#ifndef BTE_LOG_BUF_SIZE 100#define BTE_LOG_BUF_SIZE 1024 101#endif 102 103#define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12) 104 105#define MSG_BUFFER_OFFSET 0 106 107/* LayerIDs for BTA, currently everything maps onto appl_trace_level */ 108static const char * const bt_layer_tags[] = { 109 "bt-btif", 110 "bt-usb", 111 "bt-serial", 112 "bt-socket", 113 "bt-rs232", 114 "bt-lc", 115 "bt-lm", 116 "bt-hci", 117 "bt-l2cap", 118 "bt-rfcomm", 119 "bt-sdp", 120 "bt-tcs", 121 "bt-obex", 122 "bt-btm", 123 "bt-gap", 124 "bt-dun", 125 "bt-goep", 126 "bt-icp", 127 "bt-hsp2", 128 "bt-spp", 129 "bt-ctp", 130 "bt-bpp", 131 "bt-hcrp", 132 "bt-ftp", 133 "bt-opp", 134 "bt-btu", 135 "bt-gki", 136 "bt-bnep", 137 "bt-pan", 138 "bt-hfp", 139 "bt-hid", 140 "bt-bip", 141 "bt-avp", 142 "bt-a2d", 143 "bt-sap", 144 "bt-amp", 145 "bt-mca", 146 "bt-att", 147 "bt-smp", 148 "bt-nfc", 149 "bt-nci", 150 "bt-idep", 151 "bt-ndep", 152 "bt-llcp", 153 "bt-rw", 154 "bt-ce", 155 "bt-snep", 156 "bt-ndef", 157 "bt-nfa", 158}; 159 160static uint8_t BTAPP_SetTraceLevel(uint8_t new_level); 161static uint8_t BTIF_SetTraceLevel(uint8_t new_level); 162static uint8_t BTU_SetTraceLevel(uint8_t new_level); 163 164/* make sure list is order by increasing layer id!!! */ 165static tBTTRC_FUNC_MAP bttrc_set_level_map[] = { 166 {BTTRC_ID_STK_BTU, BTTRC_ID_STK_HCI, BTU_SetTraceLevel, "TRC_HCI", DEFAULT_CONF_TRACE_LEVEL}, 167 {BTTRC_ID_STK_L2CAP, BTTRC_ID_STK_L2CAP, L2CA_SetTraceLevel, "TRC_L2CAP", DEFAULT_CONF_TRACE_LEVEL}, 168#if (RFCOMM_INCLUDED==TRUE) 169 {BTTRC_ID_STK_RFCOMM, BTTRC_ID_STK_RFCOMM_DATA, PORT_SetTraceLevel, "TRC_RFCOMM", DEFAULT_CONF_TRACE_LEVEL}, 170#endif 171#if (OBX_INCLUDED==TRUE) 172 {BTTRC_ID_STK_OBEX, BTTRC_ID_STK_OBEX, OBX_SetTraceLevel, "TRC_OBEX", DEFAULT_CONF_TRACE_LEVEL}, 173#endif 174#if (AVCT_INCLUDED==TRUE) 175 //{BTTRC_ID_STK_AVCT, BTTRC_ID_STK_AVCT, NULL, "TRC_AVCT", DEFAULT_CONF_TRACE_LEVEL}, 176#endif 177#if (AVDT_INCLUDED==TRUE) 178 {BTTRC_ID_STK_AVDT, BTTRC_ID_STK_AVDT, AVDT_SetTraceLevel, "TRC_AVDT", DEFAULT_CONF_TRACE_LEVEL}, 179#endif 180#if (AVRC_INCLUDED==TRUE) 181 {BTTRC_ID_STK_AVRC, BTTRC_ID_STK_AVRC, AVRC_SetTraceLevel, "TRC_AVRC", DEFAULT_CONF_TRACE_LEVEL}, 182#endif 183#if (AVDT_INCLUDED==TRUE) 184 //{BTTRC_ID_AVDT_SCB, BTTRC_ID_AVDT_CCB, NULL, "TRC_AVDT_SCB", DEFAULT_CONF_TRACE_LEVEL}, 185#endif 186#if (A2D_INCLUDED==TRUE) 187 {BTTRC_ID_STK_A2D, BTTRC_ID_STK_A2D, A2D_SetTraceLevel, "TRC_A2D", DEFAULT_CONF_TRACE_LEVEL}, 188#endif 189#if (BIP_INCLUDED==TRUE) 190 {BTTRC_ID_STK_BIP, BTTRC_ID_STK_BIP, BIP_SetTraceLevel, "TRC_BIP", DEFAULT_CONF_TRACE_LEVEL}, 191#endif 192#if (BNEP_INCLUDED==TRUE) 193 {BTTRC_ID_STK_BNEP, BTTRC_ID_STK_BNEP, BNEP_SetTraceLevel, "TRC_BNEP", DEFAULT_CONF_TRACE_LEVEL}, 194#endif 195#if (BPP_INCLUDED==TRUE) 196 {BTTRC_ID_STK_BPP, BTTRC_ID_STK_BPP, BPP_SetTraceLevel, "TRC_BPP", DEFAULT_CONF_TRACE_LEVEL}, 197#endif 198 {BTTRC_ID_STK_BTM_ACL, BTTRC_ID_STK_BTM_SEC, BTM_SetTraceLevel, "TRC_BTM", DEFAULT_CONF_TRACE_LEVEL}, 199#if (DUN_INCLUDED==TRUE) 200 {BTTRC_ID_STK_DUN, BTTRC_ID_STK_DUN, DUN_SetTraceLevel, "TRC_DUN", DEFAULT_CONF_TRACE_LEVEL}, 201#endif 202#if (GAP_INCLUDED==TRUE) 203 {BTTRC_ID_STK_GAP, BTTRC_ID_STK_GAP, GAP_SetTraceLevel, "TRC_GAP", DEFAULT_CONF_TRACE_LEVEL}, 204#endif 205#if (GOEP_INCLUDED==TRUE) 206 {BTTRC_ID_STK_GOEP, BTTRC_ID_STK_GOEP, GOEP_SetTraceLevel, "TRC_GOEP", DEFAULT_CONF_TRACE_LEVEL}, 207#endif 208#if (HCRP_INCLUDED==TRUE) 209 {BTTRC_ID_STK_HCRP, BTTRC_ID_STK_HCRP, HCRP_SetTraceLevel, "TRC_HCRP", DEFAULT_CONF_TRACE_LEVEL}, 210#endif 211#if (PAN_INCLUDED==TRUE) 212 {BTTRC_ID_STK_PAN, BTTRC_ID_STK_PAN, PAN_SetTraceLevel, "TRC_PAN", DEFAULT_CONF_TRACE_LEVEL}, 213#endif 214#if (SAP_SERVER_INCLUDED==TRUE) 215 {BTTRC_ID_STK_SAP, BTTRC_ID_STK_SAP, NULL, "TRC_SAP", DEFAULT_CONF_TRACE_LEVEL}, 216#endif 217 {BTTRC_ID_STK_SDP, BTTRC_ID_STK_SDP, SDP_SetTraceLevel, "TRC_SDP", DEFAULT_CONF_TRACE_LEVEL}, 218#if (BLE_INCLUDED==TRUE) 219 {BTTRC_ID_STK_GATT, BTTRC_ID_STK_GATT, GATT_SetTraceLevel, "TRC_GATT", DEFAULT_CONF_TRACE_LEVEL}, 220 {BTTRC_ID_STK_SMP, BTTRC_ID_STK_SMP, SMP_SetTraceLevel, "TRC_SMP", DEFAULT_CONF_TRACE_LEVEL}, 221#endif 222 223 /* LayerIDs for BTA, currently everything maps onto appl_trace_level. 224 */ 225 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTAPP_SetTraceLevel, "TRC_BTAPP", DEFAULT_CONF_TRACE_LEVEL}, 226 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTIF_SetTraceLevel, "TRC_BTIF", DEFAULT_CONF_TRACE_LEVEL}, 227 228 {0, 0, NULL, NULL, DEFAULT_CONF_TRACE_LEVEL} 229}; 230 231static const UINT16 bttrc_map_size = sizeof(bttrc_set_level_map)/sizeof(tBTTRC_FUNC_MAP); 232 233void LogMsg(uint32_t trace_set_mask, const char *fmt_str, ...) { 234 static char buffer[BTE_LOG_BUF_SIZE]; 235 int trace_layer = TRACE_GET_LAYER(trace_set_mask); 236 if (trace_layer >= TRACE_LAYER_MAX_NUM) 237 trace_layer = 0; 238 239 va_list ap; 240 va_start(ap, fmt_str); 241 vsnprintf(&buffer[MSG_BUFFER_OFFSET], BTE_LOG_MAX_SIZE, fmt_str, ap); 242 va_end(ap); 243 244 switch ( TRACE_GET_TYPE(trace_set_mask) ) { 245 case TRACE_TYPE_ERROR: 246 LOGE0(bt_layer_tags[trace_layer], buffer); 247 break; 248 case TRACE_TYPE_WARNING: 249 LOGW0(bt_layer_tags[trace_layer], buffer); 250 break; 251 case TRACE_TYPE_API: 252 case TRACE_TYPE_EVENT: 253 LOGI0(bt_layer_tags[trace_layer], buffer); 254 break; 255 case TRACE_TYPE_DEBUG: 256 LOGD0(bt_layer_tags[trace_layer], buffer); 257 break; 258 default: 259 LOGE0(bt_layer_tags[trace_layer], buffer); /* we should never get this */ 260 break; 261 } 262} 263 264/* this function should go into BTAPP_DM for example */ 265static uint8_t BTAPP_SetTraceLevel(uint8_t new_level) { 266 if (new_level != 0xFF) 267 appl_trace_level = new_level; 268 269 return appl_trace_level; 270} 271 272static uint8_t BTIF_SetTraceLevel(uint8_t new_level) { 273 if (new_level != 0xFF) 274 btif_trace_level = new_level; 275 276 return btif_trace_level; 277} 278 279static uint8_t BTU_SetTraceLevel(uint8_t new_level) { 280 if (new_level != 0xFF) 281 btu_cb.trace_level = new_level; 282 283 return btu_cb.trace_level; 284} 285 286static void load_levels_from_config(const config_t *config) { 287 assert(config != NULL); 288 289 for (tBTTRC_FUNC_MAP *functions = &bttrc_set_level_map[0]; functions->trc_name; ++functions) { 290 ALOGI("BTE_InitTraceLevels -- %s", functions->trc_name); 291 int value = config_get_int(config, CONFIG_DEFAULT_SECTION, functions->trc_name, -1); 292 if (value != -1) 293 functions->trace_level = value; 294 295 if (functions->p_f) 296 functions->p_f(functions->trace_level); 297 } 298} 299 300static future_t *init(void) { 301 const stack_config_t *stack_config = stack_config_get_interface(); 302 if (!stack_config->get_trace_config_enabled()) { 303 ALOGI("[bttrc] using compile default trace settings"); 304 return NULL; 305 } 306 307 load_levels_from_config(stack_config->get_all()); 308 return NULL; 309} 310 311const module_t bte_logmsg_module = { 312 .name = BTE_LOGMSG_MODULE, 313 .init = init, 314 .start_up = NULL, 315 .shut_down = NULL, 316 .clean_up = NULL, 317 .dependencies = { 318 STACK_CONFIG_MODULE, 319 NULL 320 } 321}; 322