• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 1999-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 #pragma once
20 
21 #include <stdint.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /*****************************************************************************/
28 
29 /* Define trace levels */
30 typedef enum {
31   BT_TRACE_LEVEL_NONE = 0,    /* No trace messages to be generated    */
32   BT_TRACE_LEVEL_ERROR = 1,   /* Error condition trace messages       */
33   BT_TRACE_LEVEL_WARNING = 2, /* Warning condition trace messages     */
34   BT_TRACE_LEVEL_API = 3,     /* API traces                           */
35   BT_TRACE_LEVEL_EVENT = 4,   /* Debug messages for events            */
36   BT_TRACE_LEVEL_DEBUG = 5,   /* Full debug messages                  */
37   BT_TRACE_LEVEL_VERBOSE = 6, /* Verbose debug messages               */
38 } tLEGACY_TRACE_LEVEL;
39 
40 #define TRACE_CTRL_GENERAL 0x00000000
41 
42 #define TRACE_LAYER_MASK 0x00ff0000
43 #define TRACE_GET_LAYER(x) ((((uint32_t)(x)) & TRACE_LAYER_MASK) >> 16)
44 
45 #define TRACE_LAYER_NONE 0x00000000
46 #define TRACE_LAYER_HCI 0x00070000
47 #define TRACE_LAYER_L2CAP 0x00080000
48 #define TRACE_LAYER_RFCOMM 0x00090000
49 #define TRACE_LAYER_SDP 0x000a0000
50 #define TRACE_LAYER_BTM 0x000d0000
51 #define TRACE_LAYER_BNEP 0x001b0000
52 #define TRACE_LAYER_PAN 0x001c0000
53 #define TRACE_LAYER_HID 0x001e0000
54 #define TRACE_LAYER_AVP 0x00200000
55 #define TRACE_LAYER_A2DP 0x00210000
56 #define TRACE_LAYER_SMP 0x00260000
57 
58 #define TRACE_LAYER_MAX_NUM 0x0031
59 
60 #define TRACE_ORG_MASK 0x0000ff00
61 #define TRACE_GET_ORG(x) ((((uint32_t)(x)) & TRACE_ORG_MASK) >> 8)
62 
63 #define TRACE_ORG_STACK 0x00000000
64 #define TRACE_ORG_APPL 0x00000500
65 #define TRACE_ORG_USER_SCR 0x00000800
66 
67 #define TRACE_TYPE_MASK 0x000000ff
68 #define TRACE_GET_TYPE(x) (((uint32_t)(x)) & TRACE_TYPE_MASK)
69 
70 #define TRACE_TYPE_ERROR 0x00000000
71 #define TRACE_TYPE_WARNING 0x00000001
72 #define TRACE_TYPE_API 0x00000002
73 #define TRACE_TYPE_EVENT 0x00000003
74 #define TRACE_TYPE_DEBUG 0x00000004
75 #define TRACE_TYPE_INFO 0x00000005
76 
77 static const char BTE_LOGMSG_MODULE[] = "bte_logmsg_module";
78 
79 /* BTE tracing IDs for debug purposes */
80 /* LayerIDs for stack */
81 #define BTTRC_ID_STK_GKI 1
82 #define BTTRC_ID_STK_BTU 2
83 #define BTTRC_ID_STK_HCI 3
84 #define BTTRC_ID_STK_L2CAP 4
85 #define BTTRC_ID_STK_RFCM_MX 5
86 #define BTTRC_ID_STK_RFCM_PRT 6
87 #define BTTRC_ID_STK_OBEX_C 7
88 #define BTTRC_ID_STK_OBEX_S 8
89 #define BTTRC_ID_STK_AVCT 9
90 #define BTTRC_ID_STK_AVDT 10
91 #define BTTRC_ID_STK_AVRC 11
92 #define BTTRC_ID_STK_BIC 12
93 #define BTTRC_ID_STK_BIS 13
94 #define BTTRC_ID_STK_BNEP 14
95 #define BTTRC_ID_STK_BPP 15
96 #define BTTRC_ID_STK_BTM_ACL 16
97 #define BTTRC_ID_STK_BTM_PM 17
98 #define BTTRC_ID_STK_BTM_DEV_CTRL 18
99 #define BTTRC_ID_STK_BTM_SVC_DSC 19
100 #define BTTRC_ID_STK_BTM_INQ 20
101 #define BTTRC_ID_STK_BTM_SCO 21
102 #define BTTRC_ID_STK_BTM_SEC 22
103 #define BTTRC_ID_STK_HID 24
104 #define BTTRC_ID_STK_HSP2 25
105 #define BTTRC_ID_STK_CTP 26
106 #define BTTRC_ID_STK_FTC 27
107 #define BTTRC_ID_STK_FTS 28
108 #define BTTRC_ID_STK_HCRP 31
109 #define BTTRC_ID_STK_ICP 32
110 #define BTTRC_ID_STK_OPC 33
111 #define BTTRC_ID_STK_OPS 34
112 #define BTTRC_ID_STK_PAN 35
113 #define BTTRC_ID_STK_SAP 36
114 #define BTTRC_ID_STK_SDP 37
115 #define BTTRC_ID_STK_SLIP 38
116 #define BTTRC_ID_STK_SPP 39
117 #define BTTRC_ID_STK_TCS 40
118 #define BTTRC_ID_STK_VDP 41
119 #define BTTRC_ID_STK_MCAP 42 /* OBSOLETE */
120 #define BTTRC_ID_STK_GATT 43
121 #define BTTRC_ID_STK_SMP 44
122 #define BTTRC_ID_STK_NFC 45
123 #define BTTRC_ID_STK_NCI 46
124 #define BTTRC_ID_STK_IDEP 47
125 #define BTTRC_ID_STK_NDEP 48
126 #define BTTRC_ID_STK_LLCP 49
127 #define BTTRC_ID_STK_RW 50
128 #define BTTRC_ID_STK_CE 51
129 #define BTTRC_ID_STK_SNEP 52
130 #define BTTRC_ID_STK_NDEF 53
131 #define BTTRC_ID_STK_HIDD 54
132 
133 /* LayerIDs for BTA */
134 #define BTTRC_ID_BTA_ACC 55 /* Advanced Camera Client */
135 #define BTTRC_ID_BTA_AG 56  /* audio gateway */
136 #define BTTRC_ID_BTA_AV 57  /* Advanced audio */
137 #define BTTRC_ID_BTA_BIC 58 /* Basic Imaging Client */
138 #define BTTRC_ID_BTA_BIS 59 /* Basic Imaging Server */
139 #define BTTRC_ID_BTA_BP 60  /* Basic Printing Client */
140 #define BTTRC_ID_BTA_CG 61
141 #define BTTRC_ID_BTA_CT 62      /* cordless telephony terminal */
142 #define BTTRC_ID_BTA_DG 63      /* data gateway */
143 #define BTTRC_ID_BTA_DM 64      /* device manager */
144 #define BTTRC_ID_BTA_DM_SRCH 65 /* device manager search */
145 #define BTTRC_ID_BTA_DM_SEC 66  /* device manager security */
146 #define BTTRC_ID_BTA_FM 67
147 #define BTTRC_ID_BTA_FTC 68 /* file transfer client */
148 #define BTTRC_ID_BTA_FTS 69 /* file transfer server */
149 #define BTTRC_ID_BTA_HIDH 70
150 #define BTTRC_ID_BTA_HIDD 71
151 #define BTTRC_ID_BTA_JV 72
152 #define BTTRC_ID_BTA_OPC 73  /* object push client */
153 #define BTTRC_ID_BTA_OPS 74  /* object push server */
154 #define BTTRC_ID_BTA_PAN 75  /* Personal Area Networking */
155 #define BTTRC_ID_BTA_PR 76   /* Printer client */
156 #define BTTRC_ID_BTA_SC 77   /* SIM Card Access server */
157 #define BTTRC_ID_BTA_SS 78   /* synchronization server */
158 #define BTTRC_ID_BTA_SYS 79  /* system manager */
159 #define BTTRC_ID_AVDT_SCB 80 /* avdt scb */
160 #define BTTRC_ID_AVDT_CCB 81 /* avdt ccb */
161 
162 /* LayerIDs added for BTL-A. Probably should modify bte_logmsg.cc in future. */
163 #define BTTRC_ID_STK_RFCOMM 82
164 #define BTTRC_ID_STK_RFCOMM_DATA 83
165 #define BTTRC_ID_STK_OBEX 84
166 #define BTTRC_ID_STK_A2DP 85
167 #define BTTRC_ID_STK_BIP 86
168 
169 /* LayerIDs for BT APP */
170 #define BTTRC_ID_BTAPP 87
171 /* this is a temporary solution to allow dynamic enable/disable of
172  * BT_PROTOCOL_TRACE */
173 #define BTTRC_ID_BT_PROTOCOL 88
174 #define BTTRC_ID_MAX_ID BTTRC_ID_BT_PROTOCOL
175 #define BTTRC_ID_ALL_LAYERS 0xFF /* all trace layers */
176 
177 /******************************************************************************
178  *
179  * Trace Levels
180  *
181  * The following values may be used for different levels:
182  *      BT_TRACE_LEVEL_NONE    0        * No trace messages to be generated
183  *      BT_TRACE_LEVEL_ERROR   1        * Error condition trace messages
184  *      BT_TRACE_LEVEL_WARNING 2        * Warning condition trace messages
185  *      BT_TRACE_LEVEL_API     3        * API traces
186  *      BT_TRACE_LEVEL_EVENT   4        * Debug messages for events
187  *      BT_TRACE_LEVEL_DEBUG   5        * Debug messages (general)
188  *****************************************************************************/
189 
190 /* Core Stack default trace levels */
191 #ifndef HCI_INITIAL_TRACE_LEVEL
192 #define HCI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
193 #endif
194 
195 #ifndef BTM_INITIAL_TRACE_LEVEL
196 #define BTM_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
197 #endif
198 
199 #ifndef L2CAP_INITIAL_TRACE_LEVEL
200 #define L2CAP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
201 #endif
202 
203 #ifndef RFCOMM_INITIAL_TRACE_LEVEL
204 #define RFCOMM_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
205 #endif
206 
207 #ifndef BNEP_INITIAL_TRACE_LEVEL
208 #define BNEP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
209 #endif
210 
211 #ifndef PAN_INITIAL_TRACE_LEVEL
212 #define PAN_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
213 #endif
214 
215 #ifndef A2DP_INITIAL_TRACE_LEVEL
216 #define A2DP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
217 #endif
218 
219 #ifndef AVDT_INITIAL_TRACE_LEVEL
220 #define AVDT_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
221 #endif
222 
223 #ifndef AVCT_INITIAL_TRACE_LEVEL
224 #define AVCT_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
225 #endif
226 
227 #ifndef AVRC_INITIAL_TRACE_LEVEL
228 #define AVRC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
229 #endif
230 
231 #ifndef HID_INITIAL_TRACE_LEVEL
232 #define HID_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
233 #endif
234 
235 #ifndef APPL_INITIAL_TRACE_LEVEL
236 #define APPL_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
237 #endif
238 
239 #ifndef GATT_INITIAL_TRACE_LEVEL
240 #define GATT_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
241 #endif
242 
243 #ifndef SMP_INITIAL_TRACE_LEVEL
244 #define SMP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
245 #endif
246 
247 #define BT_TRACE(l, t, ...) \
248   LogMsg((TRACE_CTRL_GENERAL | (l) | TRACE_ORG_STACK | (t)), ##__VA_ARGS__)
249 
250 /* Define tracing for the HCI unit */
251 #define HCI_TRACE_ERROR(...)                                      \
252   {                                                               \
253     if (btu_trace_level >= BT_TRACE_LEVEL_ERROR)                  \
254       BT_TRACE(TRACE_LAYER_HCI, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
255   }
256 #define HCI_TRACE_WARNING(...)                                      \
257   {                                                                 \
258     if (btu_trace_level >= BT_TRACE_LEVEL_WARNING)                  \
259       BT_TRACE(TRACE_LAYER_HCI, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
260   }
261 #define HCI_TRACE_EVENT(...)                                      \
262   {                                                               \
263     if (btu_trace_level >= BT_TRACE_LEVEL_EVENT)                  \
264       BT_TRACE(TRACE_LAYER_HCI, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
265   }
266 #define HCI_TRACE_DEBUG(...)                                      \
267   {                                                               \
268     if (btu_trace_level >= BT_TRACE_LEVEL_DEBUG)                  \
269       BT_TRACE(TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
270   }
271 
272 /* Define tracing for BTM */
273 #define BTM_TRACE_ERROR(...)                                      \
274   {                                                               \
275     if (btm_cb.trace_level >= BT_TRACE_LEVEL_ERROR)               \
276       BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
277   }
278 #define BTM_TRACE_WARNING(...)                                      \
279   {                                                                 \
280     if (btm_cb.trace_level >= BT_TRACE_LEVEL_WARNING)               \
281       BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
282   }
283 #define BTM_TRACE_API(...)                                      \
284   {                                                             \
285     if (btm_cb.trace_level >= BT_TRACE_LEVEL_API)               \
286       BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_API, ##__VA_ARGS__); \
287   }
288 #define BTM_TRACE_EVENT(...)                                      \
289   {                                                               \
290     if (btm_cb.trace_level >= BT_TRACE_LEVEL_EVENT)               \
291       BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
292   }
293 #define BTM_TRACE_DEBUG(...)                                      \
294   {                                                               \
295     if (btm_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)               \
296       BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
297   }
298 
299 /* Define tracing for the L2CAP unit */
300 #define L2CAP_TRACE_ERROR(...)                                      \
301   {                                                                 \
302     if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_ERROR)             \
303       BT_TRACE(TRACE_LAYER_L2CAP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
304   }
305 #define L2CAP_TRACE_WARNING(...)                                      \
306   {                                                                   \
307     if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_WARNING)             \
308       BT_TRACE(TRACE_LAYER_L2CAP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
309   }
310 #define L2CAP_TRACE_API(...)                                      \
311   {                                                               \
312     if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_API)             \
313       BT_TRACE(TRACE_LAYER_L2CAP, TRACE_TYPE_API, ##__VA_ARGS__); \
314   }
315 #define L2CAP_TRACE_EVENT(...)                                      \
316   {                                                                 \
317     if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_EVENT)             \
318       BT_TRACE(TRACE_LAYER_L2CAP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
319   }
320 #define L2CAP_TRACE_DEBUG(...)                                      \
321   {                                                                 \
322     if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_DEBUG)             \
323       BT_TRACE(TRACE_LAYER_L2CAP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
324   }
325 
326 /* Define tracing for the SDP unit */
327 #define SDP_TRACE_ERROR(...)                                      \
328   {                                                               \
329     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_ERROR)               \
330       BT_TRACE(TRACE_LAYER_SDP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
331   }
332 #define SDP_TRACE_WARNING(...)                                      \
333   {                                                                 \
334     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_WARNING)               \
335       BT_TRACE(TRACE_LAYER_SDP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
336   }
337 #define SDP_TRACE_API(...)                                      \
338   {                                                             \
339     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_API)               \
340       BT_TRACE(TRACE_LAYER_SDP, TRACE_TYPE_API, ##__VA_ARGS__); \
341   }
342 #define SDP_TRACE_EVENT(...)                                      \
343   {                                                               \
344     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_EVENT)               \
345       BT_TRACE(TRACE_LAYER_SDP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
346   }
347 #define SDP_TRACE_DEBUG(...)                                      \
348   {                                                               \
349     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)               \
350       BT_TRACE(TRACE_LAYER_SDP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
351   }
352 #define SDP_TRACE_INFO(...)                                      \
353   {                                                              \
354     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_WARNING)            \
355       BT_TRACE(TRACE_LAYER_SDP, TRACE_TYPE_INFO, ##__VA_ARGS__); \
356   }
357 
358 /* Define tracing for the RFCOMM unit */
359 #define RFCOMM_TRACE_ERROR(...)                                      \
360   {                                                                  \
361     if (rfc_cb.trace_level >= BT_TRACE_LEVEL_ERROR)                  \
362       BT_TRACE(TRACE_LAYER_RFCOMM, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
363   }
364 #define RFCOMM_TRACE_WARNING(...)                                      \
365   {                                                                    \
366     if (rfc_cb.trace_level >= BT_TRACE_LEVEL_WARNING)                  \
367       BT_TRACE(TRACE_LAYER_RFCOMM, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
368   }
369 #define RFCOMM_TRACE_API(...)                                      \
370   {                                                                \
371     if (rfc_cb.trace_level >= BT_TRACE_LEVEL_API)                  \
372       BT_TRACE(TRACE_LAYER_RFCOMM, TRACE_TYPE_API, ##__VA_ARGS__); \
373   }
374 #define RFCOMM_TRACE_EVENT(...)                                      \
375   {                                                                  \
376     if (rfc_cb.trace_level >= BT_TRACE_LEVEL_EVENT)                  \
377       BT_TRACE(TRACE_LAYER_RFCOMM, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
378   }
379 #define RFCOMM_TRACE_DEBUG(...)                                      \
380   {                                                                  \
381     if (rfc_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)                  \
382       BT_TRACE(TRACE_LAYER_RFCOMM, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
383   }
384 
385 /* define traces for HID Host */
386 #define HIDH_TRACE_ERROR(...)                                     \
387   {                                                               \
388     if (hh_cb.trace_level >= BT_TRACE_LEVEL_ERROR)                \
389       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
390   }
391 #define HIDH_TRACE_WARNING(...)                                     \
392   {                                                                 \
393     if (hh_cb.trace_level >= BT_TRACE_LEVEL_WARNING)                \
394       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
395   }
396 #define HIDH_TRACE_API(...)                                     \
397   {                                                             \
398     if (hh_cb.trace_level >= BT_TRACE_LEVEL_API)                \
399       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_API, ##__VA_ARGS__); \
400   }
401 #define HIDH_TRACE_EVENT(...)                                     \
402   {                                                               \
403     if (hh_cb.trace_level >= BT_TRACE_LEVEL_EVENT)                \
404       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
405   }
406 #define HIDH_TRACE_DEBUG(...)                                     \
407   {                                                               \
408     if (hh_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)                \
409       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
410   }
411 
412 /* define traces for HID Device */
413 #define HIDD_TRACE_ERROR(...)                                     \
414   {                                                               \
415     if (hd_cb.trace_level >= BT_TRACE_LEVEL_ERROR)                \
416       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
417   }
418 #define HIDD_TRACE_WARNING(...)                                     \
419   {                                                                 \
420     if (hd_cb.trace_level >= BT_TRACE_LEVEL_WARNING)                \
421       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
422   }
423 #define HIDD_TRACE_API(...)                                     \
424   {                                                             \
425     if (hd_cb.trace_level >= BT_TRACE_LEVEL_API)                \
426       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_API, ##__VA_ARGS__); \
427   }
428 #define HIDD_TRACE_EVENT(...)                                     \
429   {                                                               \
430     if (hd_cb.trace_level >= BT_TRACE_LEVEL_EVENT)                \
431       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
432   }
433 #define HIDD_TRACE_DEBUG(...)                                     \
434   {                                                               \
435     if (hd_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)                \
436       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
437   }
438 #define HIDD_TRACE_VERBOSE(...)                                   \
439   {                                                               \
440     if (hd_cb.trace_level >= BT_TRACE_LEVEL_VERBOSE)              \
441       BT_TRACE(TRACE_LAYER_HID, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
442   }
443 
444 /* define traces for BNEP */
445 #define BNEP_TRACE_ERROR(...)                                      \
446   {                                                                \
447     if (bnep_cb.trace_level >= BT_TRACE_LEVEL_ERROR)               \
448       BT_TRACE(TRACE_LAYER_BNEP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
449   }
450 #define BNEP_TRACE_WARNING(...)                                      \
451   {                                                                  \
452     if (bnep_cb.trace_level >= BT_TRACE_LEVEL_WARNING)               \
453       BT_TRACE(TRACE_LAYER_BNEP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
454   }
455 #define BNEP_TRACE_API(...)                                      \
456   {                                                              \
457     if (bnep_cb.trace_level >= BT_TRACE_LEVEL_API)               \
458       BT_TRACE(TRACE_LAYER_BNEP, TRACE_TYPE_API, ##__VA_ARGS__); \
459   }
460 #define BNEP_TRACE_EVENT(...)                                      \
461   {                                                                \
462     if (bnep_cb.trace_level >= BT_TRACE_LEVEL_EVENT)               \
463       BT_TRACE(TRACE_LAYER_BNEP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
464   }
465 #define BNEP_TRACE_DEBUG(...)                                      \
466   {                                                                \
467     if (bnep_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)               \
468       BT_TRACE(TRACE_LAYER_BNEP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
469   }
470 
471 /* define traces for PAN */
472 #define PAN_TRACE_ERROR(...)                                      \
473   {                                                               \
474     if (pan_cb.trace_level >= BT_TRACE_LEVEL_ERROR)               \
475       BT_TRACE(TRACE_LAYER_PAN, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
476   }
477 #define PAN_TRACE_WARNING(...)                                      \
478   {                                                                 \
479     if (pan_cb.trace_level >= BT_TRACE_LEVEL_WARNING)               \
480       BT_TRACE(TRACE_LAYER_PAN, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
481   }
482 #define PAN_TRACE_API(...)                                      \
483   {                                                             \
484     if (pan_cb.trace_level >= BT_TRACE_LEVEL_API)               \
485       BT_TRACE(TRACE_LAYER_PAN, TRACE_TYPE_API, ##__VA_ARGS__); \
486   }
487 #define PAN_TRACE_EVENT(...)                                      \
488   {                                                               \
489     if (pan_cb.trace_level >= BT_TRACE_LEVEL_EVENT)               \
490       BT_TRACE(TRACE_LAYER_PAN, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
491   }
492 #define PAN_TRACE_DEBUG(...)                                      \
493   {                                                               \
494     if (pan_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)               \
495       BT_TRACE(TRACE_LAYER_PAN, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
496   }
497 
498 /* Define tracing for the A2DP profile */
499 #define A2DP_TRACE_ERROR(...)                                      \
500   {                                                                \
501     if (a2dp_cb.trace_level >= BT_TRACE_LEVEL_ERROR)               \
502       BT_TRACE(TRACE_LAYER_A2DP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
503   }
504 #define A2DP_TRACE_WARNING(...)                                      \
505   {                                                                  \
506     if (a2dp_cb.trace_level >= BT_TRACE_LEVEL_WARNING)               \
507       BT_TRACE(TRACE_LAYER_A2DP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
508   }
509 #define A2DP_TRACE_EVENT(...)                                      \
510   {                                                                \
511     if (a2dp_cb.trace_level >= BT_TRACE_LEVEL_EVENT)               \
512       BT_TRACE(TRACE_LAYER_A2DP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
513   }
514 #define A2DP_TRACE_DEBUG(...)                                      \
515   {                                                                \
516     if (a2dp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)               \
517       BT_TRACE(TRACE_LAYER_A2DP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
518   }
519 #define A2DP_TRACE_API(...)                                      \
520   {                                                              \
521     if (a2dp_cb.trace_level >= BT_TRACE_LEVEL_API)               \
522       BT_TRACE(TRACE_LAYER_A2DP, TRACE_TYPE_API, ##__VA_ARGS__); \
523   }
524 
525 /* AVDTP */
526 #define AVDT_TRACE_ERROR(...)                                     \
527   {                                                               \
528     if (avdtp_cb.TraceLevel() >= BT_TRACE_LEVEL_ERROR)            \
529       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
530   }
531 #define AVDT_TRACE_WARNING(...)                                     \
532   {                                                                 \
533     if (avdtp_cb.TraceLevel() >= BT_TRACE_LEVEL_WARNING)            \
534       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
535   }
536 #define AVDT_TRACE_EVENT(...)                                     \
537   {                                                               \
538     if (avdtp_cb.TraceLevel() >= BT_TRACE_LEVEL_EVENT)            \
539       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
540   }
541 #define AVDT_TRACE_DEBUG(...)                                     \
542   {                                                               \
543     if (avdtp_cb.TraceLevel() >= BT_TRACE_LEVEL_DEBUG)            \
544       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
545   }
546 #define AVDT_TRACE_API(...)                                     \
547   {                                                             \
548     if (avdtp_cb.TraceLevel() >= BT_TRACE_LEVEL_API)            \
549       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_API, ##__VA_ARGS__); \
550   }
551 
552 /* Define tracing for the AVCTP protocol */
553 #define AVCT_TRACE_ERROR(...)                                     \
554   {                                                               \
555     if (avct_cb.trace_level >= BT_TRACE_LEVEL_ERROR)              \
556       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
557   }
558 #define AVCT_TRACE_WARNING(...)                                     \
559   {                                                                 \
560     if (avct_cb.trace_level >= BT_TRACE_LEVEL_WARNING)              \
561       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
562   }
563 #define AVCT_TRACE_EVENT(...)                                     \
564   {                                                               \
565     if (avct_cb.trace_level >= BT_TRACE_LEVEL_EVENT)              \
566       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
567   }
568 #define AVCT_TRACE_DEBUG(...)                                     \
569   {                                                               \
570     if (avct_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)              \
571       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
572   }
573 #define AVCT_TRACE_API(...)                                     \
574   {                                                             \
575     if (avct_cb.trace_level >= BT_TRACE_LEVEL_API)              \
576       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_API, ##__VA_ARGS__); \
577   }
578 
579 /* Define tracing for the AVRCP profile */
580 #define AVRC_TRACE_ERROR(...)                                     \
581   {                                                               \
582     if (avrc_cb.trace_level >= BT_TRACE_LEVEL_ERROR)              \
583       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
584   }
585 #define AVRC_TRACE_WARNING(...)                                     \
586   {                                                                 \
587     if (avrc_cb.trace_level >= BT_TRACE_LEVEL_WARNING)              \
588       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
589   }
590 #define AVRC_TRACE_EVENT(...)                                     \
591   {                                                               \
592     if (avrc_cb.trace_level >= BT_TRACE_LEVEL_EVENT)              \
593       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
594   }
595 #define AVRC_TRACE_DEBUG(...)                                     \
596   {                                                               \
597     if (avrc_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)              \
598       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
599   }
600 #define AVRC_TRACE_API(...)                                     \
601   {                                                             \
602     if (avrc_cb.trace_level >= BT_TRACE_LEVEL_API)              \
603       BT_TRACE(TRACE_LAYER_AVP, TRACE_TYPE_API, ##__VA_ARGS__); \
604   }
605 
606 /* Define tracing for the SMP unit */
607 #define SMP_TRACE_ERROR(...)                                      \
608   {                                                               \
609     if (smp_cb.trace_level >= BT_TRACE_LEVEL_ERROR)               \
610       BT_TRACE(TRACE_LAYER_SMP, TRACE_TYPE_ERROR, ##__VA_ARGS__); \
611   }
612 #define SMP_TRACE_WARNING(...)                                      \
613   {                                                                 \
614     if (smp_cb.trace_level >= BT_TRACE_LEVEL_WARNING)               \
615       BT_TRACE(TRACE_LAYER_SMP, TRACE_TYPE_WARNING, ##__VA_ARGS__); \
616   }
617 #define SMP_TRACE_API(...)                                      \
618   {                                                             \
619     if (smp_cb.trace_level >= BT_TRACE_LEVEL_API)               \
620       BT_TRACE(TRACE_LAYER_SMP, TRACE_TYPE_API, ##__VA_ARGS__); \
621   }
622 #define SMP_TRACE_EVENT(...)                                      \
623   {                                                               \
624     if (smp_cb.trace_level >= BT_TRACE_LEVEL_EVENT)               \
625       BT_TRACE(TRACE_LAYER_SMP, TRACE_TYPE_EVENT, ##__VA_ARGS__); \
626   }
627 #define SMP_TRACE_DEBUG(...)                                      \
628   {                                                               \
629     if (smp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG)               \
630       BT_TRACE(TRACE_LAYER_SMP, TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
631   }
632 
633 extern uint8_t btif_trace_level;
634 
635 /* define traces for application */
636 #define BTIF_TRACE_ERROR(...)                                         \
637   {                                                                   \
638     if (btif_trace_level >= BT_TRACE_LEVEL_ERROR)                     \
639       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
640                  TRACE_TYPE_ERROR,                                    \
641              ##__VA_ARGS__);                                          \
642   }
643 #define BTIF_TRACE_WARNING(...)                                       \
644   {                                                                   \
645     if (btif_trace_level >= BT_TRACE_LEVEL_WARNING)                   \
646       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
647                  TRACE_TYPE_WARNING,                                  \
648              ##__VA_ARGS__);                                          \
649   }
650 #define BTIF_TRACE_API(...)                                           \
651   {                                                                   \
652     if (btif_trace_level >= BT_TRACE_LEVEL_API)                       \
653       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
654                  TRACE_TYPE_API,                                      \
655              ##__VA_ARGS__);                                          \
656   }
657 #define BTIF_TRACE_EVENT(...)                                         \
658   {                                                                   \
659     if (btif_trace_level >= BT_TRACE_LEVEL_EVENT)                     \
660       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
661                  TRACE_TYPE_EVENT,                                    \
662              ##__VA_ARGS__);                                          \
663   }
664 #define BTIF_TRACE_DEBUG(...)                                         \
665   {                                                                   \
666     if (btif_trace_level >= BT_TRACE_LEVEL_DEBUG)                     \
667       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
668                  TRACE_TYPE_DEBUG,                                    \
669              ##__VA_ARGS__);                                          \
670   }
671 #define BTIF_TRACE_VERBOSE(...)                                       \
672   {                                                                   \
673     if (btif_trace_level >= BT_TRACE_LEVEL_VERBOSE)                   \
674       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
675                  TRACE_TYPE_DEBUG,                                    \
676              ##__VA_ARGS__);                                          \
677   }
678 
679 /* define traces for application */
680 #define APPL_TRACE_ERROR(...)                                         \
681   {                                                                   \
682     if (appl_trace_level >= BT_TRACE_LEVEL_ERROR)                     \
683       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
684                  TRACE_TYPE_ERROR,                                    \
685              ##__VA_ARGS__);                                          \
686   }
687 #define APPL_TRACE_WARNING(...)                                       \
688   {                                                                   \
689     if (appl_trace_level >= BT_TRACE_LEVEL_WARNING)                   \
690       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
691                  TRACE_TYPE_WARNING,                                  \
692              ##__VA_ARGS__);                                          \
693   }
694 #define APPL_TRACE_API(...)                                           \
695   {                                                                   \
696     if (appl_trace_level >= BT_TRACE_LEVEL_API)                       \
697       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
698                  TRACE_TYPE_API,                                      \
699              ##__VA_ARGS__);                                          \
700   }
701 #define APPL_TRACE_EVENT(...)                                         \
702   {                                                                   \
703     if (appl_trace_level >= BT_TRACE_LEVEL_EVENT)                     \
704       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
705                  TRACE_TYPE_EVENT,                                    \
706              ##__VA_ARGS__);                                          \
707   }
708 #define APPL_TRACE_DEBUG(...)                                         \
709   {                                                                   \
710     if (appl_trace_level >= BT_TRACE_LEVEL_DEBUG)                     \
711       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
712                  TRACE_TYPE_DEBUG,                                    \
713              ##__VA_ARGS__);                                          \
714   }
715 #define APPL_TRACE_VERBOSE(...)                                       \
716   {                                                                   \
717     if (appl_trace_level >= BT_TRACE_LEVEL_VERBOSE)                   \
718       LogMsg(TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
719                  TRACE_TYPE_DEBUG,                                    \
720              ##__VA_ARGS__);                                          \
721   }
722 
723 typedef uint8_t tBTTRC_LAYER_ID;
724 typedef uint8_t(tBTTRC_SET_TRACE_LEVEL)(uint8_t);
725 
726 typedef struct {
727   const tBTTRC_LAYER_ID layer_id_start;
728   const tBTTRC_LAYER_ID layer_id_end;
729   tBTTRC_SET_TRACE_LEVEL* p_f;
730   const char* trc_name;
731   uint8_t trace_level;
732 } tBTTRC_FUNC_MAP;
733 
734 /* External declaration for appl_trace_level here to avoid to add the
735  * declaration in all the files using APPL_TRACExxx macros */
736 extern uint8_t appl_trace_level;
737 
738 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...);
739 
740 #ifdef __cplusplus
741 }
742 #endif
743 
744 #ifdef __cplusplus
745 
746 #include <array>
747 #include <iomanip>
748 #include <sstream>
749 #include <type_traits>
750 
751 #include "check.h"
752 #include "os/logging/log_adapter.h"
753 
754 /* Prints integral parameter x as hex string, with '0' fill */
755 template <typename T>
loghex(T x)756 std::string loghex(T x) {
757   static_assert(std::is_integral<T>::value,
758                 "loghex parameter must be integral.");
759   std::stringstream tmp;
760   tmp << std::showbase << std::internal << std::hex << std::setfill('0')
761       << std::setw((sizeof(T) * 2) + 2) << +x;
762   return tmp.str();
763 }
764 
765 /* Prints integral array as hex string, with '0' fill */
766 template <typename T, size_t N>
loghex(std::array<T,N> array)767 std::string loghex(std::array<T, N> array) {
768   static_assert(std::is_integral<T>::value,
769                 "type stored in array must be integral.");
770   std::stringstream tmp;
771   for (const auto& x : array) {
772     tmp << std::internal << std::hex << std::setfill('0')
773         << std::setw((sizeof(uint8_t) * 2) + 2) << +x;
774   }
775   return tmp.str();
776 }
777 
778 /**
779  * Obtains the string representation of a boolean value.
780  *
781  * @param value the boolean value to use
782  * @return the string representation of the boolean value: "true" or "false"
783  */
logbool(bool value)784 inline std::string logbool(bool value) {
785   std::stringstream tmp;
786   tmp << std::boolalpha << value;
787   return tmp.str();
788 }
789 
790 /**
791  * Append a field name to a string.
792  *
793  * The field names are added to the string with "|" in between.
794  *
795  * @param p_result a pointer to the result string to add the field name to
796  * @param append if true the field name will be added
797  * @param name the field name to add
798  * @return the result string
799  */
AppendField(std::string * p_result,bool append,const std::string & name)800 inline std::string& AppendField(std::string* p_result, bool append,
801                                 const std::string& name) {
802   CHECK(p_result != nullptr);
803   if (!append) return *p_result;
804   if (!p_result->empty()) *p_result += "|";
805   *p_result += name;
806   return *p_result;
807 }
808 
809 // This object puts the stream in a state where every time that a new line
810 // occurs, the next line is indented a certain number of spaces. The stream is
811 // reset to its previous state when the object is destroyed.
812 class ScopedIndent {
813  public:
814   ScopedIndent(std::ostream& stream, int indent_size = DEFAULT_TAB)
indented_buf_(stream,indent_size)815       : indented_buf_(stream, indent_size) {
816     old_stream_ = &stream;
817     old_stream_buf_ = stream.rdbuf();
818     stream.rdbuf(&indented_buf_);
819   }
820 
~ScopedIndent()821   ~ScopedIndent() { old_stream_->rdbuf(old_stream_buf_); }
822 
823   static const size_t DEFAULT_TAB = 2;
824 
825  private:
826   class IndentedStreamBuf : public std::streambuf {
827    public:
IndentedStreamBuf(std::ostream & stream,int indent_size)828     IndentedStreamBuf(std::ostream& stream, int indent_size)
829         : wrapped_buf_(stream.rdbuf()),
830           indent_size_(indent_size),
831           indent_next_line_(true){};
832 
833    protected:
overflow(int character)834     virtual int overflow(int character) override {
835       if (indent_next_line_ && character != '\n') {
836         for (int i = 0; i < indent_size_; i++) wrapped_buf_->sputc(' ');
837       }
838 
839       indent_next_line_ = false;
840       if (character == '\n') {
841         indent_next_line_ = true;
842       }
843 
844       return wrapped_buf_->sputc(character);
845     }
846 
847    private:
848     std::streambuf* wrapped_buf_;
849     int indent_size_;
850     bool indent_next_line_;
851   };
852 
853   std::ostream* old_stream_;
854   std::streambuf* old_stream_buf_;
855   IndentedStreamBuf indented_buf_;
856 };
857 
858 #endif
859