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