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