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