OSDN Git Service

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