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