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 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <sys/time.h>
25 #include <time.h>
26 
27 #include "bte.h"
28 #include "bta_api.h"
29 #include "btu.h"
30 #include "osi/include/config.h"
31 #include "gki.h"
32 #include "l2c_api.h"
33 #include "osi/include/log.h"
34 #include "stack_config.h"
35 
36 #include "port_api.h"
37 #if (AVDT_INCLUDED==TRUE)
38 #include "avdt_api.h"
39 #endif
40 #include "avrc_api.h"
41 #if (AVDT_INCLUDED==TRUE)
42 #include "avdt_api.h"
43 #endif
44 #if (A2D_INCLUDED==TRUE)
45 #include "a2d_api.h"
46 #endif
47 #if (BNEP_INCLUDED==TRUE)
48 #include "bnep_api.h"
49 #endif
50 #include "btm_api.h"
51 #include "gap_api.h"
52 #if (PAN_INCLUDED==TRUE)
53 #include "pan_api.h"
54 #endif
55 #include "sdp_api.h"
56 
57 #if (BLE_INCLUDED==TRUE)
58 #include "gatt_api.h"
59 #include "smp_api.h"
60 #endif
61 
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)
66 
67 #ifndef DEFAULT_CONF_TRACE_LEVEL
68 #define DEFAULT_CONF_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
69 #endif
70 
71 #ifndef BTE_LOG_BUF_SIZE
72 #define BTE_LOG_BUF_SIZE  1024
73 #endif
74 
75 #define BTE_LOG_MAX_SIZE  (BTE_LOG_BUF_SIZE - 12)
76 
77 #define MSG_BUFFER_OFFSET 0
78 
79 /* LayerIDs for BTA, currently everything maps onto appl_trace_level */
80 static const char * const bt_layer_tags[] = {
81   "bt_btif",
82   "bt_usb",
83   "bt_serial",
84   "bt_socket",
85   "bt_rs232",
86   "bt_lc",
87   "bt_lm",
88   "bt_hci",
89   "bt_l2cap",
90   "bt_rfcomm",
91   "bt_sdp",
92   "bt_tcs",
93   "bt_obex",
94   "bt_btm",
95   "bt_gap",
96   "UNUSED",
97   "UNUSED",
98   "bt_icp",
99   "bt_hsp2",
100   "bt_spp",
101   "bt_ctp",
102   "bt_bpp",
103   "bt_hcrp",
104   "bt_ftp",
105   "bt_opp",
106   "bt_btu",
107   "bt_gki",
108   "bt_bnep",
109   "bt_pan",
110   "bt_hfp",
111   "bt_hid",
112   "bt_bip",
113   "bt_avp",
114   "bt_a2d",
115   "bt_sap",
116   "bt_amp",
117   "bt_mca",
118   "bt_att",
119   "bt_smp",
120   "bt_nfc",
121   "bt_nci",
122   "bt_idep",
123   "bt_ndep",
124   "bt_llcp",
125   "bt_rw",
126   "bt_ce",
127   "bt_snep",
128   "bt_ndef",
129   "bt_nfa",
130 };
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);
134 
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},
142 #endif
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},
146 #endif
147 #if (A2D_INCLUDED==TRUE)
148   {BTTRC_ID_STK_A2D, BTTRC_ID_STK_A2D, A2D_SetTraceLevel, "TRC_A2D", DEFAULT_CONF_TRACE_LEVEL},
149 #endif
150 #if (BNEP_INCLUDED==TRUE)
151   {BTTRC_ID_STK_BNEP, BTTRC_ID_STK_BNEP, BNEP_SetTraceLevel, "TRC_BNEP", DEFAULT_CONF_TRACE_LEVEL},
152 #endif
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},
157 #endif
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},
162 #endif
163 
164   /* LayerIDs for BTA, currently everything maps onto appl_trace_level.
165    */
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},
168 
169   {0, 0, NULL, NULL, DEFAULT_CONF_TRACE_LEVEL}
170 };
171 
172 static const UINT16 bttrc_map_size = sizeof(bttrc_set_level_map)/sizeof(tBTTRC_FUNC_MAP);
173 
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)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)
178     trace_layer = 0;
179 
180   va_list ap;
181   va_start(ap, fmt_str);
182   vsnprintf(&buffer[MSG_BUFFER_OFFSET], BTE_LOG_MAX_SIZE, fmt_str, ap);
183   va_end(ap);
184 
185   switch ( TRACE_GET_TYPE(trace_set_mask) ) {
186     case TRACE_TYPE_ERROR:
187       LOGE0(bt_layer_tags[trace_layer], buffer);
188       break;
189     case TRACE_TYPE_WARNING:
190       LOGW0(bt_layer_tags[trace_layer], buffer);
191       break;
192     case TRACE_TYPE_API:
193     case TRACE_TYPE_EVENT:
194       LOGI0(bt_layer_tags[trace_layer], buffer);
195       break;
196     case TRACE_TYPE_DEBUG:
197       LOGD0(bt_layer_tags[trace_layer], buffer);
198       break;
199     default:
200       LOGE0(bt_layer_tags[trace_layer], buffer);      /* we should never get this */
201       break;
202   }
203 }
204 
205 /* this function should go into BTAPP_DM for example */
BTAPP_SetTraceLevel(uint8_t new_level)206 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level) {
207   if (new_level != 0xFF)
208     appl_trace_level = new_level;
209 
210   return appl_trace_level;
211 }
212 
BTIF_SetTraceLevel(uint8_t new_level)213 static uint8_t BTIF_SetTraceLevel(uint8_t new_level) {
214   if (new_level != 0xFF)
215     btif_trace_level = new_level;
216 
217   return btif_trace_level;
218 }
219 
BTU_SetTraceLevel(uint8_t new_level)220 static uint8_t BTU_SetTraceLevel(uint8_t new_level) {
221   if (new_level != 0xFF)
222     btu_cb.trace_level = new_level;
223 
224   return btu_cb.trace_level;
225 }
226 
load_levels_from_config(const config_t * config)227 static void load_levels_from_config(const config_t *config) {
228   assert(config != NULL);
229 
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);
233     if (value != -1)
234       functions->trace_level = value;
235 
236     if (functions->p_f)
237       functions->p_f(functions->trace_level);
238   }
239 }
240 
init(void)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");
245     return NULL;
246   }
247 
248   load_levels_from_config(stack_config->get_all());
249   return NULL;
250 }
251 
252 const module_t bte_logmsg_module = {
253   .name = BTE_LOGMSG_MODULE,
254   .init = init,
255   .start_up = NULL,
256   .shut_down = NULL,
257   .clean_up = NULL,
258   .dependencies = {
259     STACK_CONFIG_MODULE,
260     NULL
261   }
262 };
263