1 /******************************************************************************
2  *
3  *  Copyright 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 #define LOG_TAG "bt_bte"
20 
21 #include <base/logging.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <time.h>
28 
29 #include "avrc_api.h"
30 #include "bt_common.h"
31 #include "bta_api.h"
32 #include "bte.h"
33 #include "btm_api.h"
34 #include "btu.h"
35 #include "l2c_api.h"
36 #include "main_int.h"
37 #include "osi/include/config.h"
38 #include "osi/include/log.h"
39 #include "port_api.h"
40 #include "sdp_api.h"
41 #include "stack_config.h"
42 
43 #include "avdt_api.h"
44 #include "a2dp_api.h"
45 #if (BNEP_INCLUDED == TRUE)
46 #include "bnep_api.h"
47 #endif
48 #if (PAN_INCLUDED == TRUE)
49 #include "pan_api.h"
50 #endif
51 #if (HID_HOST_INCLUDED == TRUE)
52 #include "hidh_api.h"
53 #endif
54 #if (HID_DEV_INCLUDED == TRUE)
55 #include "hidd_api.h"
56 #endif
57 
58 #include "smp_api.h"
59 
60 #ifndef DEFAULT_CONF_TRACE_LEVEL
61 #define DEFAULT_CONF_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
62 #endif
63 
64 #ifndef BTE_LOG_BUF_SIZE
65 #define BTE_LOG_BUF_SIZE 256
66 #endif
67 
68 #define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12)
69 
70 #define MSG_BUFFER_OFFSET 0
71 
72 /* LayerIDs for BTA, currently everything maps onto appl_trace_level */
73 static const char* const bt_layer_tags[] = {
74     "bt_btif", "bt_usb",  "bt_serial", "bt_socket", "bt_rs232", "bt_lc",
75     "bt_lm",   "bt_hci",  "bt_l2cap",  "bt_rfcomm", "bt_sdp",   "bt_tcs",
76     "bt_obex", "bt_btm",  "bt_gap",    "UNUSED",    "UNUSED",   "bt_icp",
77     "bt_hsp2", "bt_spp",  "bt_ctp",    "bt_bpp",    "bt_hcrp",  "bt_ftp",
78     "bt_opp",  "bt_btu",  "bt_gki", /* OBSOLETED */
79     "bt_bnep", "bt_pan",  "bt_hfp",    "bt_hid",    "bt_bip",   "bt_avp",
80     "bt_a2d",  "bt_sap",  "bt_amp",    "bt_mca",    "bt_att",   "bt_smp",
81     "bt_nfc",  "bt_nci",  "bt_idep",   "bt_ndep",   "bt_llcp",  "bt_rw",
82     "bt_ce",   "bt_snep", "bt_ndef",   "bt_nfa",
83 };
84 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level);
85 static uint8_t BTIF_SetTraceLevel(uint8_t new_level);
86 static uint8_t BTU_SetTraceLevel(uint8_t new_level);
87 
88 /* make sure list is order by increasing layer id!!! */
89 static tBTTRC_FUNC_MAP bttrc_set_level_map[] = {
90     {BTTRC_ID_STK_BTU, BTTRC_ID_STK_HCI, BTU_SetTraceLevel, "TRC_HCI",
91      DEFAULT_CONF_TRACE_LEVEL},
92     {BTTRC_ID_STK_L2CAP, BTTRC_ID_STK_L2CAP, L2CA_SetTraceLevel, "TRC_L2CAP",
93      DEFAULT_CONF_TRACE_LEVEL},
94     {BTTRC_ID_STK_RFCOMM, BTTRC_ID_STK_RFCOMM_DATA, PORT_SetTraceLevel,
95      "TRC_RFCOMM", DEFAULT_CONF_TRACE_LEVEL},
96     {BTTRC_ID_STK_AVDT, BTTRC_ID_STK_AVDT, AVDT_SetTraceLevel, "TRC_AVDT",
97      DEFAULT_CONF_TRACE_LEVEL},
98     {BTTRC_ID_STK_AVRC, BTTRC_ID_STK_AVRC, AVRC_SetTraceLevel, "TRC_AVRC",
99      DEFAULT_CONF_TRACE_LEVEL},
100     {BTTRC_ID_STK_A2DP, BTTRC_ID_STK_A2DP, A2DP_SetTraceLevel, "TRC_A2D",
101      DEFAULT_CONF_TRACE_LEVEL},
102 #if (BNEP_INCLUDED == TRUE)
103     {BTTRC_ID_STK_BNEP, BTTRC_ID_STK_BNEP, BNEP_SetTraceLevel, "TRC_BNEP",
104      DEFAULT_CONF_TRACE_LEVEL},
105 #endif
106     {BTTRC_ID_STK_BTM_ACL, BTTRC_ID_STK_BTM_SEC, BTM_SetTraceLevel, "TRC_BTM",
107      DEFAULT_CONF_TRACE_LEVEL},
108 #if (HID_HOST_INCLUDED == TRUE)
109     {BTTRC_ID_STK_HID, BTTRC_ID_STK_HID, HID_HostSetTraceLevel, "TRC_HID_HOST",
110      DEFAULT_CONF_TRACE_LEVEL},
111 #endif
112 #if (PAN_INCLUDED == TRUE)
113     {BTTRC_ID_STK_PAN, BTTRC_ID_STK_PAN, PAN_SetTraceLevel, "TRC_PAN",
114      DEFAULT_CONF_TRACE_LEVEL},
115 #endif
116     {BTTRC_ID_STK_SDP, BTTRC_ID_STK_SDP, SDP_SetTraceLevel, "TRC_SDP",
117      DEFAULT_CONF_TRACE_LEVEL},
118     {BTTRC_ID_STK_SMP, BTTRC_ID_STK_SMP, SMP_SetTraceLevel, "TRC_SMP",
119      DEFAULT_CONF_TRACE_LEVEL},
120 #if (HID_DEV_INCLUDED == TRUE)
121     {BTTRC_ID_STK_HIDD, BTTRC_ID_STK_HIDD, HID_DevSetTraceLevel, "TRC_HID_DEV",
122      DEFAULT_CONF_TRACE_LEVEL},
123 #endif
124 
125     /* LayerIDs for BTA, currently everything maps onto appl_trace_level.
126      */
127     {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTAPP_SetTraceLevel, "TRC_BTAPP",
128      DEFAULT_CONF_TRACE_LEVEL},
129     {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTIF_SetTraceLevel, "TRC_BTIF",
130      DEFAULT_CONF_TRACE_LEVEL},
131 
132     {0, 0, NULL, NULL, DEFAULT_CONF_TRACE_LEVEL}};
133 
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)134 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {
135   char buffer[BTE_LOG_BUF_SIZE];
136   int trace_layer = TRACE_GET_LAYER(trace_set_mask);
137   if (trace_layer >= TRACE_LAYER_MAX_NUM) trace_layer = 0;
138 
139   va_list ap;
140   va_start(ap, fmt_str);
141   vsnprintf(&buffer[MSG_BUFFER_OFFSET], BTE_LOG_MAX_SIZE, fmt_str, ap);
142   va_end(ap);
143 
144   switch (TRACE_GET_TYPE(trace_set_mask)) {
145     case TRACE_TYPE_ERROR:
146       LOG_ERROR(bt_layer_tags[trace_layer], "%s", buffer);
147       break;
148     case TRACE_TYPE_WARNING:
149       LOG_WARN(bt_layer_tags[trace_layer], "%s", buffer);
150       break;
151     case TRACE_TYPE_API:
152     case TRACE_TYPE_EVENT:
153       LOG_INFO(bt_layer_tags[trace_layer], "%s", buffer);
154       break;
155     case TRACE_TYPE_DEBUG:
156       LOG_DEBUG(bt_layer_tags[trace_layer], "%s", buffer);
157       break;
158     default:
159       /* we should never get this */
160       LOG_ERROR(bt_layer_tags[trace_layer], "!BAD TRACE TYPE! %s", buffer);
161       CHECK(TRACE_GET_TYPE(trace_set_mask) == TRACE_TYPE_ERROR);
162       break;
163   }
164 }
165 
166 /* this function should go into BTAPP_DM for example */
BTAPP_SetTraceLevel(uint8_t new_level)167 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level) {
168   if (new_level != 0xFF) appl_trace_level = new_level;
169 
170   return appl_trace_level;
171 }
172 
BTIF_SetTraceLevel(uint8_t new_level)173 static uint8_t BTIF_SetTraceLevel(uint8_t new_level) {
174   if (new_level != 0xFF) btif_trace_level = new_level;
175 
176   return btif_trace_level;
177 }
178 
BTU_SetTraceLevel(uint8_t new_level)179 static uint8_t BTU_SetTraceLevel(uint8_t new_level) {
180   if (new_level != 0xFF) btu_trace_level = new_level;
181 
182   return btu_trace_level;
183 }
184 
load_levels_from_config(const config_t * config)185 static void load_levels_from_config(const config_t* config) {
186   CHECK(config != NULL);
187 
188   for (tBTTRC_FUNC_MAP* functions = &bttrc_set_level_map[0];
189        functions->trc_name; ++functions) {
190     int value = config_get_int(*config, CONFIG_DEFAULT_SECTION,
191                                functions->trc_name, -1);
192     if (value != -1) functions->trace_level = value;
193     LOG_INFO(LOG_TAG, "BTE_InitTraceLevels -- %s : Level %d",
194              functions->trc_name, functions->trace_level);
195     if (functions->p_f) functions->p_f(functions->trace_level);
196   }
197 }
198 
init(void)199 static future_t* init(void) {
200   const stack_config_t* stack_config = stack_config_get_interface();
201   if (!stack_config->get_trace_config_enabled()) {
202     LOG_INFO(LOG_TAG, "using compile default trace settings");
203     return NULL;
204   }
205 
206   init_cpp_logging(stack_config->get_all());
207 
208   load_levels_from_config(stack_config->get_all());
209   return NULL;
210 }
211 
212 EXPORT_SYMBOL extern const module_t bte_logmsg_module = {
213     .name = BTE_LOGMSG_MODULE,
214     .init = init,
215     .start_up = NULL,
216     .shut_down = NULL,
217     .clean_up = NULL,
218     .dependencies = {STACK_CONFIG_MODULE, NULL}};
219