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