1 /******************************************************************************
3 * Copyright (C) 2001-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
30 #include "osi/include/config.h"
33 #include "osi/include/log.h"
34 #include "stack_config.h"
37 #if (AVDT_INCLUDED==TRUE)
41 #if (AVDT_INCLUDED==TRUE)
44 #if (A2D_INCLUDED==TRUE)
47 #if (BNEP_INCLUDED==TRUE)
52 #if (PAN_INCLUDED==TRUE)
57 #if (BLE_INCLUDED==TRUE)
62 #define LOGI0(t,s) __android_log_write(ANDROID_LOG_INFO, t, s)
63 #define LOGD0(t,s) __android_log_write(ANDROID_LOG_DEBUG, t, s)
64 #define LOGW0(t,s) __android_log_write(ANDROID_LOG_WARN, t, s)
65 #define LOGE0(t,s) __android_log_write(ANDROID_LOG_ERROR, t, s)
67 #ifndef DEFAULT_CONF_TRACE_LEVEL
68 #define DEFAULT_CONF_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
71 #ifndef BTE_LOG_BUF_SIZE
72 #define BTE_LOG_BUF_SIZE 1024
75 #define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12)
77 #define MSG_BUFFER_OFFSET 0
79 /* LayerIDs for BTA, currently everything maps onto appl_trace_level */
80 static const char * const bt_layer_tags[] = {
131 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level);
132 static uint8_t BTIF_SetTraceLevel(uint8_t new_level);
133 static uint8_t BTU_SetTraceLevel(uint8_t new_level);
135 /* make sure list is order by increasing layer id!!! */
136 static tBTTRC_FUNC_MAP bttrc_set_level_map[] = {
137 {BTTRC_ID_STK_BTU, BTTRC_ID_STK_HCI, BTU_SetTraceLevel, "TRC_HCI", DEFAULT_CONF_TRACE_LEVEL},
138 {BTTRC_ID_STK_L2CAP, BTTRC_ID_STK_L2CAP, L2CA_SetTraceLevel, "TRC_L2CAP", DEFAULT_CONF_TRACE_LEVEL},
139 {BTTRC_ID_STK_RFCOMM, BTTRC_ID_STK_RFCOMM_DATA, PORT_SetTraceLevel, "TRC_RFCOMM", DEFAULT_CONF_TRACE_LEVEL},
140 #if (AVDT_INCLUDED==TRUE)
141 {BTTRC_ID_STK_AVDT, BTTRC_ID_STK_AVDT, AVDT_SetTraceLevel, "TRC_AVDT", DEFAULT_CONF_TRACE_LEVEL},
143 {BTTRC_ID_STK_AVRC, BTTRC_ID_STK_AVRC, AVRC_SetTraceLevel, "TRC_AVRC", DEFAULT_CONF_TRACE_LEVEL},
144 #if (AVDT_INCLUDED==TRUE)
145 //{BTTRC_ID_AVDT_SCB, BTTRC_ID_AVDT_CCB, NULL, "TRC_AVDT_SCB", DEFAULT_CONF_TRACE_LEVEL},
147 #if (A2D_INCLUDED==TRUE)
148 {BTTRC_ID_STK_A2D, BTTRC_ID_STK_A2D, A2D_SetTraceLevel, "TRC_A2D", DEFAULT_CONF_TRACE_LEVEL},
150 #if (BNEP_INCLUDED==TRUE)
151 {BTTRC_ID_STK_BNEP, BTTRC_ID_STK_BNEP, BNEP_SetTraceLevel, "TRC_BNEP", DEFAULT_CONF_TRACE_LEVEL},
153 {BTTRC_ID_STK_BTM_ACL, BTTRC_ID_STK_BTM_SEC, BTM_SetTraceLevel, "TRC_BTM", DEFAULT_CONF_TRACE_LEVEL},
154 {BTTRC_ID_STK_GAP, BTTRC_ID_STK_GAP, GAP_SetTraceLevel, "TRC_GAP", DEFAULT_CONF_TRACE_LEVEL},
155 #if (PAN_INCLUDED==TRUE)
156 {BTTRC_ID_STK_PAN, BTTRC_ID_STK_PAN, PAN_SetTraceLevel, "TRC_PAN", DEFAULT_CONF_TRACE_LEVEL},
158 {BTTRC_ID_STK_SDP, BTTRC_ID_STK_SDP, SDP_SetTraceLevel, "TRC_SDP", DEFAULT_CONF_TRACE_LEVEL},
159 #if (BLE_INCLUDED==TRUE)
160 {BTTRC_ID_STK_GATT, BTTRC_ID_STK_GATT, GATT_SetTraceLevel, "TRC_GATT", DEFAULT_CONF_TRACE_LEVEL},
161 {BTTRC_ID_STK_SMP, BTTRC_ID_STK_SMP, SMP_SetTraceLevel, "TRC_SMP", DEFAULT_CONF_TRACE_LEVEL},
164 /* LayerIDs for BTA, currently everything maps onto appl_trace_level.
166 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTAPP_SetTraceLevel, "TRC_BTAPP", DEFAULT_CONF_TRACE_LEVEL},
167 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTIF_SetTraceLevel, "TRC_BTIF", DEFAULT_CONF_TRACE_LEVEL},
169 {0, 0, NULL, NULL, DEFAULT_CONF_TRACE_LEVEL}
172 static const UINT16 bttrc_map_size = sizeof(bttrc_set_level_map)/sizeof(tBTTRC_FUNC_MAP);
174 void LogMsg(uint32_t trace_set_mask, const char *fmt_str, ...) {
175 static char buffer[BTE_LOG_BUF_SIZE];
176 int trace_layer = TRACE_GET_LAYER(trace_set_mask);
177 if (trace_layer >= TRACE_LAYER_MAX_NUM)
181 va_start(ap, fmt_str);
182 vsnprintf(&buffer[MSG_BUFFER_OFFSET], BTE_LOG_MAX_SIZE, fmt_str, ap);
185 switch ( TRACE_GET_TYPE(trace_set_mask) ) {
186 case TRACE_TYPE_ERROR:
187 LOGE0(bt_layer_tags[trace_layer], buffer);
189 case TRACE_TYPE_WARNING:
190 LOGW0(bt_layer_tags[trace_layer], buffer);
193 case TRACE_TYPE_EVENT:
194 LOGI0(bt_layer_tags[trace_layer], buffer);
196 case TRACE_TYPE_DEBUG:
197 LOGD0(bt_layer_tags[trace_layer], buffer);
200 LOGE0(bt_layer_tags[trace_layer], buffer); /* we should never get this */
205 /* this function should go into BTAPP_DM for example */
206 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level) {
207 if (new_level != 0xFF)
208 appl_trace_level = new_level;
210 return appl_trace_level;
213 static uint8_t BTIF_SetTraceLevel(uint8_t new_level) {
214 if (new_level != 0xFF)
215 btif_trace_level = new_level;
217 return btif_trace_level;
220 static uint8_t BTU_SetTraceLevel(uint8_t new_level) {
221 if (new_level != 0xFF)
222 btu_cb.trace_level = new_level;
224 return btu_cb.trace_level;
227 static void load_levels_from_config(const config_t *config) {
228 assert(config != NULL);
230 for (tBTTRC_FUNC_MAP *functions = &bttrc_set_level_map[0]; functions->trc_name; ++functions) {
231 LOG_INFO("BTE_InitTraceLevels -- %s", functions->trc_name);
232 int value = config_get_int(config, CONFIG_DEFAULT_SECTION, functions->trc_name, -1);
234 functions->trace_level = value;
237 functions->p_f(functions->trace_level);
241 static future_t *init(void) {
242 const stack_config_t *stack_config = stack_config_get_interface();
243 if (!stack_config->get_trace_config_enabled()) {
244 LOG_INFO("[bttrc] using compile default trace settings");
248 load_levels_from_config(stack_config->get_all());
252 const module_t bte_logmsg_module = {
253 .name = BTE_LOGMSG_MODULE,