• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  ****************************************************************************************
17  *
18  * @file dbg_assert.h
19  *
20  * @brief File containing the definitions of the assertion macros.
21  *
22  ****************************************************************************************
23  */
24 
25 #ifndef _DBG_ASSERT_H_
26 #define _DBG_ASSERT_H_
27 
28 /**
29  ****************************************************************************************
30  * @defgroup ASSERT ASSERT
31  * @ingroup DEBUG
32  * @brief Assertion management module
33  * @{
34  ****************************************************************************************
35  */
36 
37 /*
38  * INCLUDE FILES
39  ****************************************************************************************
40  */
41 #include "plf.h"
42 #include "compiler.h"
43 #include "ll.h"
44 
45 /*
46  * DEFINES
47  ****************************************************************************************
48  */
49 #if 0 //NX_TRACE
50 /// Line number: Include file id if trace is enabled
51 #define LINE_NB ((TRACE_FILE_ID << 24) + (__LINE__ & 0xffffff))
52 #else
53 /// Line number
54 #define LINE_NB __LINE__
55 #endif
56 
57 /*
58  * ASSERTION CHECK
59  ****************************************************************************************
60  */
61 
62 extern int dbg_assert_block;
63 
64 /**
65  ****************************************************************************************
66  * @brief Print the assertion error reason and trigger the recovery procedure.
67  *
68  * @param[in] condition C string containing the condition.
69  * @param[in] file C string containing file where the assertion is located.
70  * @param[in] line Line number in the file where the assertion is located.
71  ****************************************************************************************
72  */
73 void dbg_assert_rec(const char *condition, const char * file, int line);
74 
75 /**
76  ****************************************************************************************
77  * @brief Print the assertion error reason and loop forever.
78  *
79  * @param[in] condition C string containing the condition.
80  * @param[in] file C string containing file where the assertion is located.
81  * @param[in] line Line number in the file where the assertion is located.
82  ****************************************************************************************
83  */
84 void dbg_assert_err(const char *condition, const char * file, int line);
85 
86 /**
87  ****************************************************************************************
88  * @brief Print the assertion warning reason.
89  *
90  * @param[in] condition C string containing the condition.
91  * @param[in] file C string containing file where the assertion is located.
92  * @param[in] line Line number in the file where the assertion is located.
93  ****************************************************************************************
94  */
95 void dbg_assert_warn(const char *condition, const char * file, int line);
96 
97 /// Macro defining the format of the assertion calls
98 #ifdef CFG_DBG
99 #define DBG_ASSERT(type, cond) dbg_assert_##type(cond, __MODULE__, LINE_NB)
100 #else
101 #define DBG_ASSERT(type, cond) dbg_assert_##type("", "", 0)
102 #endif
103 
104 #ifdef CFG_DBG
105 /// Assertions showing a critical error that could require a full system reset
106 #define ASSERT_ERR(cond)                                                                 \
107     do {                                                                                 \
108         if (!(cond)) {                                                                   \
109             DBG_ASSERT(err, #cond);                                                          \
110         }                                                                                \
111     } while(0)
112 
113 /// Assertions showing a critical error that could require a full system reset
114 
115 #define ASSERT_ERR2B(fmt, ...) dbg_test_print(fmt, ##__VA_ARGS__)
116 
117 #define ASSERT_ERR2A(fmt, ...) \
118     do { \
119         GLOBAL_INT_STOP(); \
120         ASSERT_ERR2B(fmt, ##__VA_ARGS__); \
121         while (dbg_assert_block); \
122     } while (0)
123 
124 #define ASSERT_ERR2(cond, fmt, ...) \
125     do { \
126         if (!(cond)) { \
127             ASSERT_ERR2A(D_ERR "ASSERT_ERR (%s) at %s:%d "fmt, #cond, __MODULE__, LINE_NB, ##__VA_ARGS__); \
128         } \
129     } while (0)
130 
131 /// Assertions showing a non-critical problem that has to be fixed by the SW
132 #define ASSERT_WARN(cond)                                                                \
133     do {                                                                                 \
134         if (!(cond)) {                                                                   \
135             DBG_ASSERT(warn, #cond);                                                         \
136         }                                                                                \
137     } while(0)
138 
139 #ifdef CFG_BLE_STACK
140 #define ASSERT_INFO(cond, param0, param1)            { if (!(cond)) { TRACE("line is %d file is %s\r\n", __LINE__, __FILE__);} }
141 #else
142 #define ASSERT_INFO(cond, param0, param1)            { if (!(cond)) { TRACE("line is %d file is %s", __LINE__, __FILE__); ASSERT_ERR(0);} }
143 #endif
144 
145 #else
146 /// Assertions showing a critical error that could require a full system reset
147 #define ASSERT_ERR(cond)
148 /// Assertions showing a critical error that could require a full system reset
149 #define ASSERT_ERR2(cond, fmt, ...)
150 /// Assertions showing a non-critical problem that has to be fixed by the SW
151 #define ASSERT_WARN(cond)
152 #define ASSERT_INFO(cond, param0, param1)
153 #endif
154 
155 #if 0
156 /// Assertions that trigger the automatic recovery mechanism and return void
157 #define ASSERT_REC(cond)                                                                 \
158     ({                                                                                   \
159         if (!(cond)) {                                                                   \
160             ASSERT(rec, #cond);                                                          \
161             return;                                                                      \
162         }                                                                                \
163     })
164 
165 /// Assertions that trigger the automatic recovery mechanism and return a value
166 #define ASSERT_REC_VAL(cond, ret)                                                        \
167     ({                                                                                   \
168         if (!(cond)) {                                                                   \
169             ASSERT(rec, #cond);                                                          \
170             return (ret);                                                                \
171         }                                                                                \
172     })
173 
174 /// Assertions that trigger the automatic recovery mechanism and do not return
175 #define ASSERT_REC_NO_RET(cond)                                                          \
176     ({                                                                                   \
177         if (!(cond)) {                                                                   \
178             ASSERT(rec, #cond);                                                          \
179         }                                                                                \
180     })
181 #else
182 /// Assertions that trigger the automatic recovery mechanism and return void
183 #define ASSERT_REC(cond)             ASSERT_ERR(cond)
184 
185 /// Assertions that trigger the automatic recovery mechanism and return a value
186 #define ASSERT_REC_VAL(cond, ret)    ASSERT_ERR(cond)
187 
188 /// Assertions that trigger the automatic recovery mechanism and do not return
189 #define ASSERT_REC_NO_RET(cond)      ASSERT_ERR(cond)
190 #endif
191 
192 
193 /// @}  // end of group ASSERT
194 
195 #endif // _DBG_ASSERT_H_
196