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 /******************************************************************************
20 *
21 * Contains the LogMsg wrapper routines for BTE. It routes calls the
22 * appropriate application's LogMsg equivalent.
23 *
24 ******************************************************************************/
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31
32 #include "config.h"
33 #include "gki.h"
34 #include "bte.h"
35
36 #include "bte_appl.h"
37
38 #if MMI_INCLUDED == TRUE
39 #include "mmi.h"
40 #endif
41
42 /* always enable trace framework */
43
44 #include "btu.h"
45 #include "l2c_api.h"
46 #if (RFCOMM_INCLUDED==TRUE)
47 #include "port_api.h"
48 #endif
49 #if (OBX_INCLUDED==TRUE)
50 #include "obx_api.h"
51 #endif
52 #if (AVCT_INCLUDED==TRUE)
53 #include "avct_api.h"
54 #endif
55 #if (AVDT_INCLUDED==TRUE)
56 #include "avdt_api.h"
57 #endif
58 #if (AVRC_INCLUDED==TRUE)
59 #include "avrc_api.h"
60 #endif
61 #if (AVDT_INCLUDED==TRUE)
62 #include "avdt_api.h"
63 #endif
64 #if (A2D_INCLUDED==TRUE)
65 #include "a2d_api.h"
66 #endif
67 #if (BIP_INCLUDED==TRUE)
68 #include "bip_api.h"
69 #endif
70 #if (BNEP_INCLUDED==TRUE)
71 #include "bnep_api.h"
72 #endif
73 #if (BPP_INCLUDED==TRUE)
74 #include "bpp_api.h"
75 #endif
76 #include "btm_api.h"
77 #if (DUN_INCLUDED==TRUE)
78 #include "dun_api.h"
79 #endif
80 #if (GAP_INCLUDED==TRUE)
81 #include "gap_api.h"
82 #endif
83 #if (GOEP_INCLUDED==TRUE)
84 #include "goep_util.h"
85 #endif
86 #if (HCRP_INCLUDED==TRUE)
87 #include "hcrp_api.h"
88 #endif
89 #if (PAN_INCLUDED==TRUE)
90 #include "pan_api.h"
91 #endif
92 #include "sdp_api.h"
93
94 #if (BLE_INCLUDED==TRUE)
95 #include "gatt_api.h"
96 #include "smp_api.h"
97 #endif
98
99 /* LayerIDs for BTA, currently everything maps onto appl_trace_level */
100 #if (BTA_INCLUDED==TRUE)
101 #include "bta_api.h"
102 #endif
103
104
105 #if defined(__CYGWIN__) || defined(__linux__)
106 #undef RPC_INCLUDED
107 #define RPC_INCLUDED TRUE
108
109 #include <sys/time.h>
110 #include <time.h>
111
112 #if (defined(ANDROID_USE_LOGCAT) && (ANDROID_USE_LOGCAT==TRUE))
113 const char * const bt_layer_tags[] = {
114 "bt-btif",
115 "bt-usb",
116 "bt-serial",
117 "bt-socket",
118 "bt-rs232",
119 "bt-lc",
120 "bt-lm",
121 "bt-hci",
122 "bt-l2cap",
123 "bt-rfcomm",
124 "bt-sdp",
125 "bt-tcs",
126 "bt-obex",
127 "bt-btm",
128 "bt-gap",
129 "bt-dun",
130 "bt-goep",
131 "bt-icp",
132 "bt-hsp2",
133 "bt-spp",
134 "bt-ctp",
135 "bt-bpp",
136 "bt-hcrp",
137 "bt-ftp",
138 "bt-opp",
139 "bt-btu",
140 "bt-gki",
141 "bt-bnep",
142 "bt-pan",
143 "bt-hfp",
144 "bt-hid",
145 "bt-bip",
146 "bt-avp",
147 "bt-a2d",
148 "bt-sap",
149 "bt-amp",
150 "bt-mca",
151 "bt-att",
152 "bt-smp",
153 "bt-nfc",
154 "bt-nci",
155 "bt-idep",
156 "bt-ndep",
157 "bt-llcp",
158 "bt-rw",
159 "bt-ce",
160 "bt-snep",
161 "bt-ndef",
162 "bt-nfa",
163 };
164
165 #ifndef LINUX_NATIVE
166 #include <cutils/log.h>
167 #define LOGI0(t,s) __android_log_write(ANDROID_LOG_INFO, t, s)
168 #define LOGD0(t,s) __android_log_write(ANDROID_LOG_DEBUG, t, s)
169 #define LOGW0(t,s) __android_log_write(ANDROID_LOG_WARN, t, s)
170 #define LOGE0(t,s) __android_log_write(ANDROID_LOG_ERROR, t, s)
171
172 #else
173 #undef ANDROID_USE_LOGCAT
174 #endif
175
176 #endif
177
178
179 //#include "btl_cfg.h"
180 #define BTL_GLOBAL_PROP_TRC_FLAG "TRC_BTAPP"
181 #ifndef DEFAULT_CONF_TRACE_LEVEL
182 #define DEFAULT_CONF_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
183 #endif
184
185 #ifndef BTE_LOG_BUF_SIZE
186 #define BTE_LOG_BUF_SIZE 1024
187 #endif
188 #define BTE_LOG_MAX_SIZE (BTE_LOG_BUF_SIZE - 12)
189
190
191 //#define BTE_MAP_TRACE_LEVEL FALSE
192 /* map by default BTE trace levels onto android trace levels */
193 #ifndef BTE_MAP_TRACE_LEVEL
194 #define BTE_MAP_TRACE_LEVEL TRUE
195 #endif
196
197 // #define BTE_ANDROID_INTERNAL_TIMESTAMP TRUE
198 /* by default no internal timestamp. adb logcate -v time allows having timestamps */
199 #ifndef BTE_ANDROID_INTERNAL_TIMESTAMP
200 #define BTE_ANDROID_INTERNAL_TIMESTAMP FALSE
201 #endif
202 #if (BTE_ANDROID_INTERNAL_TIMESTAMP==TRUE)
203 #define MSG_BUFFER_OFFSET strlen(buffer)
204 #else
205 #define MSG_BUFFER_OFFSET 0
206 #endif
207
208 //#define DBG_TRACE
209
210 #if defined( DBG_TRACE )
211 #define DBG_TRACE_API0( m ) BT_TRACE( TRACE_LAYER_HCI, TRACE_TYPE_API, m )
212 #define DBG_TRACE_WARNING2( m, p0, p1 ) BT_TRACE( TRACE_LAYER_BTM, (TRACE_ORG_APPL|TRACE_TYPE_WARNING), m, p0, p1 )
213 #else
214 #define DBG_TRACE_API0( m )
215 #define DBG_TRACE_WARNING2( m, p0, p1 )
216 #endif
217 #define DBG_TRACE_DEBUG2( m, p0, p1 ) BT_TRACE( TRACE_LAYER_BTM, (TRACE_ORG_APPL|TRACE_TYPE_DEBUG), m, p0, p1 )
218
219 void
LogMsg(UINT32 trace_set_mask,const char * fmt_str,...)220 LogMsg(UINT32 trace_set_mask, const char *fmt_str, ...)
221 {
222 static char buffer[BTE_LOG_BUF_SIZE];
223 int trace_layer = TRACE_GET_LAYER(trace_set_mask);
224 if (trace_layer >= TRACE_LAYER_MAX_NUM)
225 trace_layer = 0;
226
227 va_list ap;
228 #if (BTE_ANDROID_INTERNAL_TIMESTAMP==TRUE)
229 struct timeval tv;
230 struct timezone tz;
231 struct tm *tm;
232 time_t t;
233
234 gettimeofday(&tv, &tz);
235 time(&t);
236 tm = localtime(&t);
237
238 sprintf(buffer, "%02d:%02d:%02d.%03d ", tm->tm_hour, tm->tm_min, tm->tm_sec,
239 tv.tv_usec / 1000);
240 #endif
241 va_start(ap, fmt_str);
242 vsnprintf(&buffer[MSG_BUFFER_OFFSET], BTE_LOG_MAX_SIZE, fmt_str, ap);
243 va_end(ap);
244
245 #if (defined(ANDROID_USE_LOGCAT) && (ANDROID_USE_LOGCAT==TRUE))
246 #if (BTE_MAP_TRACE_LEVEL==TRUE)
247 switch ( TRACE_GET_TYPE(trace_set_mask) )
248 {
249 case TRACE_TYPE_ERROR:
250 LOGE0(bt_layer_tags[trace_layer], buffer);
251 break;
252 case TRACE_TYPE_WARNING:
253 LOGW0(bt_layer_tags[trace_layer], buffer);
254 break;
255 case TRACE_TYPE_API:
256 case TRACE_TYPE_EVENT:
257 LOGI0(bt_layer_tags[trace_layer], buffer);
258 break;
259 case TRACE_TYPE_DEBUG:
260 LOGD0(bt_layer_tags[trace_layer], buffer);
261 break;
262 default:
263 LOGE0(bt_layer_tags[trace_layer], buffer); /* we should never get this */
264 break;
265 }
266 #else
267 LOGI0(bt_layer_tags[trace_layer], buffer);
268 #endif
269 #else
270 write(2, buffer, strlen(buffer));
271 write(2, "\n", 1);
272 #endif
273 }
274
275 void
ScrLog(UINT32 trace_set_mask,const char * fmt_str,...)276 ScrLog(UINT32 trace_set_mask, const char *fmt_str, ...)
277 {
278 static char buffer[BTE_LOG_BUF_SIZE];
279
280 va_list ap;
281 struct timeval tv;
282 struct timezone tz;
283 struct tm *tm;
284 time_t t;
285 int trace_layer = TRACE_GET_LAYER(trace_set_mask);
286 if (trace_layer >= TRACE_LAYER_MAX_NUM)
287 trace_layer = 0;
288 gettimeofday(&tv, &tz);
289 time(&t);
290 tm = localtime(&t);
291
292 sprintf(buffer, "%02d:%02d:%02d.%03ld ", tm->tm_hour, tm->tm_min, tm->tm_sec,
293 tv.tv_usec / 1000);
294
295 va_start(ap, fmt_str);
296 vsnprintf(&buffer[strlen(buffer)], BTE_LOG_MAX_SIZE, fmt_str, ap);
297 va_end(ap);
298
299 #if (defined(ANDROID_USE_LOGCAT) && (ANDROID_USE_LOGCAT==TRUE))
300 LOGI0(bt_layer_tags[trace_layer], buffer);
301 #else
302 write(2, buffer, strlen(buffer));
303 write(2, "\n", 1);
304 #endif
305 }
306
307 /* this function should go into BTAPP_DM for example */
BTAPP_SetTraceLevel(UINT8 new_level)308 BT_API UINT8 BTAPP_SetTraceLevel( UINT8 new_level )
309 {
310 if (new_level != 0xFF)
311 appl_trace_level = new_level;
312
313 return (appl_trace_level);
314 }
315
BTIF_SetTraceLevel(UINT8 new_level)316 BT_API UINT8 BTIF_SetTraceLevel( UINT8 new_level )
317 {
318 if (new_level != 0xFF)
319 btif_trace_level = new_level;
320
321 return (btif_trace_level);
322 }
323
BTU_SetTraceLevel(UINT8 new_level)324 BTU_API UINT8 BTU_SetTraceLevel( UINT8 new_level )
325 {
326 if (new_level != 0xFF)
327 btu_cb.trace_level = new_level;
328
329 return (btu_cb.trace_level);
330 }
331
332 BOOLEAN trace_conf_enabled = FALSE;
333
bte_trace_conf(const char * p_conf_name,const char * p_conf_value)334 void bte_trace_conf(const char *p_conf_name, const char *p_conf_value)
335 {
336 tBTTRC_FUNC_MAP *p_f_map = (tBTTRC_FUNC_MAP *) &bttrc_set_level_map[0];
337
338 while (p_f_map->trc_name != NULL)
339 {
340 if (strcmp(p_f_map->trc_name, (const char *)p_conf_name) == 0)
341 {
342 p_f_map->trace_level = (UINT8) atoi(p_conf_value);
343 break;
344 }
345 p_f_map++;
346 }
347 }
348
bte_trace_conf_config(const config_t * config)349 void bte_trace_conf_config(const config_t *config) {
350 assert(config != NULL);
351
352 for (tBTTRC_FUNC_MAP *functions = &bttrc_set_level_map[0]; functions->trc_name; ++functions) {
353 int value = config_get_int(config, CONFIG_DEFAULT_SECTION, functions->trc_name, -1);
354 if (value != -1)
355 functions->trace_level = value;
356 }
357 }
358
359 /********************************************************************************
360 **
361 ** Function Name: BTA_SysSetTraceLevel
362 **
363 ** Purpose: set or reads the different Trace Levels of layer IDs (see bt_trace.h,
364 ** BTTRC_ID_xxxx
365 **
366 ** Input Parameters: Array with trace layers to set to a given level or read. a layer ID of 0
367 ** defines the end of the list
368 ** WARNING: currently type should be 0-5! or FF for reading!!!!
369 **
370 ** Returns:
371 ** input array with trace levels for given layer id
372 **
373 *********************************************************************************/
BTA_SysSetTraceLevel(tBTTRC_LEVEL * p_levels)374 BT_API tBTTRC_LEVEL * BTA_SysSetTraceLevel(tBTTRC_LEVEL * p_levels)
375 {
376 const tBTTRC_FUNC_MAP *p_f_map;
377 tBTTRC_LEVEL *p_l = p_levels;
378
379 DBG_TRACE_API0( "BTA_SysSetTraceLevel()" );
380
381 while (0 != p_l->layer_id)
382 {
383 p_f_map = &bttrc_set_level_map[0];
384
385 while (0 != p_f_map->layer_id_start)
386 {
387 printf("BTA_SysSetTraceLevel - trace id in map start = %d end= %d, paramter id = %d\r\n", p_f_map->layer_id_start, p_f_map->layer_id_end, p_l->layer_id );
388 /* as p_f_map is ordered by increasing layer id, go to next map entry as long end id
389 * is smaller */
390 //if (p_f_map->layer_id_end < p_l->layer_id)
391 //{
392 //p_f_map++;
393 //}
394 //else
395 {
396 /* check if layer_id actually false into a range or if it is note existing in the map */
397 if ((NULL != p_f_map->p_f) && (p_f_map->layer_id_start <= p_l->layer_id) && (p_f_map->layer_id_end >= p_l->layer_id) )
398 {
399 DBG_TRACE_DEBUG2( "BTA_SysSetTraceLevel( id:%d, level:%d ): setting/reading",
400 p_l->layer_id, p_l->type );
401 p_l->type = p_f_map->p_f(p_l->type);
402 break;
403 }
404 else
405 {
406 DBG_TRACE_WARNING2( "BTA_SysSetTraceLevel( id:%d, level:%d ): MISSING Set function OR ID in map!",
407 p_l->layer_id, p_l->type );
408 }
409 /* set/read next trace level by getting out ot map loop */
410 //p_l++;
411 //break;
412 }
413 p_f_map++;
414 }
415 //if (0 == p_f_map->layer_id_start)
416 {
417 DBG_TRACE_WARNING2( "BTA_SysSetTraceLevel( id:%d, level:%d ): ID NOT FOUND in map. Skip to next",
418 p_l->layer_id, p_l->type );
419 p_l++;
420 }
421 }
422
423 return p_levels;
424 } /* BTA_SysSetTraceLevel() */
425
426 /* make sure list is order by increasing layer id!!! */
427 tBTTRC_FUNC_MAP bttrc_set_level_map[] = {
428 {BTTRC_ID_STK_BTU, BTTRC_ID_STK_HCI, BTU_SetTraceLevel, "TRC_HCI", DEFAULT_CONF_TRACE_LEVEL},
429 {BTTRC_ID_STK_L2CAP, BTTRC_ID_STK_L2CAP, L2CA_SetTraceLevel, "TRC_L2CAP", DEFAULT_CONF_TRACE_LEVEL},
430 #if (RFCOMM_INCLUDED==TRUE)
431 {BTTRC_ID_STK_RFCOMM, BTTRC_ID_STK_RFCOMM_DATA, PORT_SetTraceLevel, "TRC_RFCOMM", DEFAULT_CONF_TRACE_LEVEL},
432 #endif
433 #if (OBX_INCLUDED==TRUE)
434 {BTTRC_ID_STK_OBEX, BTTRC_ID_STK_OBEX, OBX_SetTraceLevel, "TRC_OBEX", DEFAULT_CONF_TRACE_LEVEL},
435 #endif
436 #if (AVCT_INCLUDED==TRUE)
437 //{BTTRC_ID_STK_AVCT, BTTRC_ID_STK_AVCT, NULL, "TRC_AVCT", DEFAULT_CONF_TRACE_LEVEL},
438 #endif
439 #if (AVDT_INCLUDED==TRUE)
440 {BTTRC_ID_STK_AVDT, BTTRC_ID_STK_AVDT, AVDT_SetTraceLevel, "TRC_AVDT", DEFAULT_CONF_TRACE_LEVEL},
441 #endif
442 #if (AVRC_INCLUDED==TRUE)
443 {BTTRC_ID_STK_AVRC, BTTRC_ID_STK_AVRC, AVRC_SetTraceLevel, "TRC_AVRC", DEFAULT_CONF_TRACE_LEVEL},
444 #endif
445 #if (AVDT_INCLUDED==TRUE)
446 //{BTTRC_ID_AVDT_SCB, BTTRC_ID_AVDT_CCB, NULL, "TRC_AVDT_SCB", DEFAULT_CONF_TRACE_LEVEL},
447 #endif
448 #if (A2D_INCLUDED==TRUE)
449 {BTTRC_ID_STK_A2D, BTTRC_ID_STK_A2D, A2D_SetTraceLevel, "TRC_A2D", DEFAULT_CONF_TRACE_LEVEL},
450 #endif
451 #if (BIP_INCLUDED==TRUE)
452 {BTTRC_ID_STK_BIP, BTTRC_ID_STK_BIP, BIP_SetTraceLevel, "TRC_BIP", DEFAULT_CONF_TRACE_LEVEL},
453 #endif
454 #if (BNEP_INCLUDED==TRUE)
455 {BTTRC_ID_STK_BNEP, BTTRC_ID_STK_BNEP, BNEP_SetTraceLevel, "TRC_BNEP", DEFAULT_CONF_TRACE_LEVEL},
456 #endif
457 #if (BPP_INCLUDED==TRUE)
458 {BTTRC_ID_STK_BPP, BTTRC_ID_STK_BPP, BPP_SetTraceLevel, "TRC_BPP", DEFAULT_CONF_TRACE_LEVEL},
459 #endif
460 {BTTRC_ID_STK_BTM_ACL, BTTRC_ID_STK_BTM_SEC, BTM_SetTraceLevel, "TRC_BTM", DEFAULT_CONF_TRACE_LEVEL},
461 #if (DUN_INCLUDED==TRUE)
462 {BTTRC_ID_STK_DUN, BTTRC_ID_STK_DUN, DUN_SetTraceLevel, "TRC_DUN", DEFAULT_CONF_TRACE_LEVEL},
463 #endif
464 #if (GAP_INCLUDED==TRUE)
465 {BTTRC_ID_STK_GAP, BTTRC_ID_STK_GAP, GAP_SetTraceLevel, "TRC_GAP", DEFAULT_CONF_TRACE_LEVEL},
466 #endif
467 #if (GOEP_INCLUDED==TRUE)
468 {BTTRC_ID_STK_GOEP, BTTRC_ID_STK_GOEP, GOEP_SetTraceLevel, "TRC_GOEP", DEFAULT_CONF_TRACE_LEVEL},
469 #endif
470 #if (HCRP_INCLUDED==TRUE)
471 {BTTRC_ID_STK_HCRP, BTTRC_ID_STK_HCRP, HCRP_SetTraceLevel, "TRC_HCRP", DEFAULT_CONF_TRACE_LEVEL},
472 #endif
473 #if (PAN_INCLUDED==TRUE)
474 {BTTRC_ID_STK_PAN, BTTRC_ID_STK_PAN, PAN_SetTraceLevel, "TRC_PAN", DEFAULT_CONF_TRACE_LEVEL},
475 #endif
476 #if (SAP_SERVER_INCLUDED==TRUE)
477 {BTTRC_ID_STK_SAP, BTTRC_ID_STK_SAP, NULL, "TRC_SAP", DEFAULT_CONF_TRACE_LEVEL},
478 #endif
479 {BTTRC_ID_STK_SDP, BTTRC_ID_STK_SDP, SDP_SetTraceLevel, "TRC_SDP", DEFAULT_CONF_TRACE_LEVEL},
480 #if (BLE_INCLUDED==TRUE)
481 {BTTRC_ID_STK_GATT, BTTRC_ID_STK_GATT, GATT_SetTraceLevel, "TRC_GATT", DEFAULT_CONF_TRACE_LEVEL},
482 {BTTRC_ID_STK_SMP, BTTRC_ID_STK_SMP, SMP_SetTraceLevel, "TRC_SMP", DEFAULT_CONF_TRACE_LEVEL},
483 #endif
484
485 #if (BTA_INCLUDED==TRUE)
486 /* LayerIDs for BTA, currently everything maps onto appl_trace_level.
487 * BTL_GLOBAL_PROP_TRC_FLAG serves as flag in conf.
488 */
489 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTAPP_SetTraceLevel, BTL_GLOBAL_PROP_TRC_FLAG, DEFAULT_CONF_TRACE_LEVEL},
490 #endif
491
492 #if (BT_TRACE_BTIF == TRUE)
493 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTIF_SetTraceLevel, "TRC_BTIF", DEFAULT_CONF_TRACE_LEVEL},
494 #endif
495
496 {0, 0, NULL, NULL, DEFAULT_CONF_TRACE_LEVEL}
497 };
498
499 const UINT16 bttrc_map_size = sizeof(bttrc_set_level_map)/sizeof(tBTTRC_FUNC_MAP);
500 #endif
501
502 /********************************************************************************
503 **
504 ** Function Name: BTE_InitTraceLevels
505 **
506 ** Purpose: This function can be used to set the boot time reading it from the
507 ** platform.
508 ** WARNING: it is called under BTU context and it blocks the BTU task
509 ** till it returns (sync call)
510 **
511 ** Input Parameters: None, platform to provide levels
512 ** Returns:
513 ** Newly set levels, if any!
514 **
515 *********************************************************************************/
BTE_InitTraceLevels(void)516 BT_API void BTE_InitTraceLevels( void )
517 {
518 /* read and set trace levels by calling the different XXX_SetTraceLevel().
519 */
520 #if ( BT_USE_TRACES==TRUE )
521 if (trace_conf_enabled == TRUE)
522 {
523 tBTTRC_FUNC_MAP *p_f_map = (tBTTRC_FUNC_MAP *) &bttrc_set_level_map[0];
524
525 while (p_f_map->trc_name != NULL)
526 {
527 ALOGI("BTE_InitTraceLevels -- %s", p_f_map->trc_name);
528
529 if (p_f_map->p_f)
530 p_f_map->p_f(p_f_map->trace_level);
531
532 p_f_map++;
533 }
534 }
535 else
536 {
537 ALOGI("[bttrc] using compile default trace settings");
538 }
539 #endif
540 }
541