OSDN Git Service

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