1 /*
2  * SPDX-FileCopyrightText: 2017 Nordic Semiconductor ASA
3  * SPDX-FileCopyrightText: 2015-2016 Intel Corporation
4  * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef _BLE_MESH_TRACE_H_
10 #define _BLE_MESH_TRACE_H_
11 
12 #include <assert.h>
13 #include "esp_log.h"
14 #include "mesh_util.h"
15 #include "esp_rom_sys.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /* Define common tracing for all */
22 #ifndef BLE_MESH_LOG_LEVEL_ERROR
23 #define BLE_MESH_LOG_LEVEL_ERROR            1
24 #endif /* BLE_MESH_LOG_LEVEL_ERROR */
25 
26 #ifndef BLE_MESH_LOG_LEVEL_WARN
27 #define BLE_MESH_LOG_LEVEL_WARN             2
28 #endif /* BLE_MESH_LOG_LEVEL_WARN */
29 
30 #ifndef BLE_MESH_LOG_LEVEL_INFO
31 #define BLE_MESH_LOG_LEVEL_INFO             3
32 #endif /* BLE_MESH_LOG_LEVEL_INFO */
33 
34 #ifndef BLE_MESH_LOG_LEVEL_DEBUG
35 #define BLE_MESH_LOG_LEVEL_DEBUG            4
36 #endif /* BLE_MESH_LOG_LEVEL_DEBUG */
37 
38 #ifndef BLE_MESH_LOG_LEVEL_VERBOSE
39 #define BLE_MESH_LOG_LEVEL_VERBOSE          5
40 #endif /*BLE_MESH_LOG_LEVEL_VERBOSE */
41 
42 #ifdef CONFIG_BLE_MESH_STACK_TRACE_LEVEL
43 #define BLE_MESH_LOG_LEVEL                  CONFIG_BLE_MESH_STACK_TRACE_LEVEL
44 #else
45 #define BLE_MESH_LOG_LEVEL                  BLE_MESH_LOG_LEVEL_WARN
46 #endif
47 
48 #ifdef CONFIG_BLE_MESH_NET_BUF_TRACE_LEVEL
49 #define BLE_MESH_NET_BUF_LOG_LEVEL          CONFIG_BLE_MESH_NET_BUF_TRACE_LEVEL
50 #else
51 #define BLE_MESH_NET_BUF_LOG_LEVEL          BLE_MESH_LOG_LEVEL_WARN
52 #endif
53 
54 #define BLE_MESH_TRACE_TAG                  "BLE_MESH"
55 
56 #if (LOG_LOCAL_LEVEL >= 4)
57 #define BLE_MESH_LOG_LOCAL_LEVEL_MAPPING    (LOG_LOCAL_LEVEL + 1)
58 #else
59 #define BLE_MESH_LOG_LOCAL_LEVEL_MAPPING    LOG_LOCAL_LEVEL
60 #endif
61 
62 #define BLE_MESH_LOG_LEVEL_CHECK(LAYER, LEVEL)  (MAX(LAYER##_LOG_LEVEL, BLE_MESH_LOG_LOCAL_LEVEL_MAPPING) >= BLE_MESH_LOG_LEVEL_##LEVEL)
63 
64 #define BLE_MESH_PRINT_E(tag, format, ...)  {esp_log_write(ESP_LOG_ERROR,   tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
65 #define BLE_MESH_PRINT_W(tag, format, ...)  {esp_log_write(ESP_LOG_WARN,    tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
66 #define BLE_MESH_PRINT_I(tag, format, ...)  {esp_log_write(ESP_LOG_INFO,    tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
67 #define BLE_MESH_PRINT_D(tag, format, ...)  {esp_log_write(ESP_LOG_DEBUG,   tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
68 #define BLE_MESH_PRINT_V(tag, format, ...)  {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
69 
70 #define printk          esp_rom_printf
71 
72 #define _STRINGIFY(x)   #x
73 #define STRINGIFY(s)    _STRINGIFY(s)
74 
75 #ifndef __ASSERT
76 #define __ASSERT(test, str) assert(test)
77 #endif
78 
79 #ifndef __ASSERT_NO_MSG
80 #define __ASSERT_NO_MSG(x) assert(x)
81 #endif
82 
83 #if !CONFIG_BLE_MESH_NO_LOG
84 #define BT_ERR(fmt, args...)    do {if ((BLE_MESH_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH, ERROR)) BLE_MESH_PRINT_E(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
85 #define BT_WARN(fmt, args...)   do {if ((BLE_MESH_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_WARN)  && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH, WARN))  BLE_MESH_PRINT_W(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
86 #define BT_INFO(fmt, args...)   do {if ((BLE_MESH_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_INFO)  && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH, INFO))  BLE_MESH_PRINT_I(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
87 #define BT_DBG(fmt, args...)    do {if ((BLE_MESH_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_DEBUG) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH, DEBUG)) BLE_MESH_PRINT_D(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
88 #else
89 #define BT_ERR(fmt, args...)
90 #define BT_WARN(fmt, args...)
91 #define BT_INFO(fmt, args...)
92 #define BT_DBG(fmt, args...)
93 #endif
94 
95 #if defined(CONFIG_BLE_MESH_NET_BUF_LOG) && (!CONFIG_BLE_MESH_NO_LOG)
96 #define NET_BUF_ERR(fmt, args...)   do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, ERROR)) BLE_MESH_PRINT_E(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
97 #define NET_BUF_WARN(fmt, args...)  do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_WARN)  && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, WARN))  BLE_MESH_PRINT_W(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
98 #define NET_BUF_INFO(fmt, args...)  do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_INFO)  && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, INFO))  BLE_MESH_PRINT_I(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
99 #define NET_BUF_DBG(fmt, args...)   do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_DEBUG) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, DEBUG)) BLE_MESH_PRINT_D(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
100 #define NET_BUF_ASSERT(cond)        __ASSERT_NO_MSG(cond)
101 #else
102 #define NET_BUF_ERR(fmt, args...)
103 #define NET_BUF_WARN(fmt, args...)
104 #define NET_BUF_INFO(fmt, args...)
105 #define NET_BUF_DBG(fmt, args...)
106 #define NET_BUF_ASSERT(cond)
107 #endif
108 
109 #if defined(CONFIG_BLE_MESH_NET_BUF_SIMPLE_LOG) && (!CONFIG_BLE_MESH_NO_LOG)
110 #define NET_BUF_SIMPLE_ERR(fmt, args...)    do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, ERROR)) BLE_MESH_PRINT_E(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
111 #define NET_BUF_SIMPLE_WARN(fmt, args...)   do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_WARN)  && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, WARN))  BLE_MESH_PRINT_W(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
112 #define NET_BUF_SIMPLE_INFO(fmt, args...)   do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_INFO)  && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, INFO))  BLE_MESH_PRINT_I(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
113 #define NET_BUF_SIMPLE_DBG(fmt, args...)    do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_DEBUG) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, DEBUG)) BLE_MESH_PRINT_D(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0)
114 #define NET_BUF_SIMPLE_ASSERT(cond)         __ASSERT_NO_MSG(cond)
115 #else
116 #define NET_BUF_SIMPLE_ERR(fmt, args...)
117 #define NET_BUF_SIMPLE_WARN(fmt, args...)
118 #define NET_BUF_SIMPLE_INFO(fmt, args...)
119 #define NET_BUF_SIMPLE_DBG(fmt, args...)
120 #define NET_BUF_SIMPLE_ASSERT(cond)
121 #endif
122 
123 #if CONFIG_BLE_MESH_BQB_TEST_LOG
124 /**
125  * For example, the test case "MESH/NODE/TNPT/BV-01-C"
126  * could use BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, "msg %s", msg)
127  * to print some message.
128  */
129 enum BLE_MESH_BQB_TEST_LOG_LEVEL {
130     BLE_MESH_BQB_TEST_LOG_LEVEL_OUTPUT_ALL           = 0,        /* Output all BQB related test log */
131     BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE      = BIT(0),
132     BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_PVNR      = BIT(1),
133     BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_CFGCL     = BIT(2),
134     BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_SR        = BIT(3),
135     BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_CL        = BIT(4),
136     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_PBADV         = BIT(5),
137     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_MPS           = BIT(6),
138     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_PROV          = BIT(7),
139     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_BCN           = BIT(8),
140     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_NET           = BIT(9),
141     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_RLY           = BIT(10),
142     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT          = BIT(11),
143     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_IVU           = BIT(12),
144     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_KR            = BIT(13),
145     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_FRND_FN       = BIT(14),
146     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_FRND_LPN      = BIT(15),
147     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_PROX          = BIT(16),
148     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_MPXS          = BIT(17),
149     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_CFG           = BIT(18),
150     BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_HM            = BIT(19),
151 };
152 
153 #define BLE_MESH_BQB_TEST_LOG_LEVEL_OUTPUT_NONE      0x000FFFFF
154 
155 #endif /* CONFIG_BLE_MESH_BQB_TEST_LOG */
156 
157 #if (CONFIG_BLE_MESH_BQB_TEST_LOG && !CONFIG_BLE_MESH_NO_LOG)
158 extern bool bt_mesh_bqb_test_flag_check(uint32_t flag_mask);
159 extern int bt_mesh_bqb_test_flag_set(uint32_t value);
160 #define BT_BQB(flag_mask, fmt, args...)                             \
161             do {                                                    \
162                 if (bt_mesh_bqb_test_flag_check(flag_mask))         \
163                     BLE_MESH_PRINT_I("BLE_MESH_BQB", fmt, ## args); \
164             } while (0)
165 #else
166 #define BT_BQB(flag_mask, fmt, args...)
167 #endif
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif /* _BLE_MESH_TRACE_H_ */
174