1 /**
2 *****************************************************************************************
3 *
4 * @file app_assert.c
5 *
6 * @brief App Assert Implementation.
7 *
8 *****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 *****************************************************************************************
36 */
37
38 #define APP_LOG_TAG "app_assert.c"
39
40 /*
41 * INCLUDE FILES
42 *****************************************************************************************
43 */
44 #include <cmsis_compiler.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdarg.h>
48 #include "app_log.h"
49 #include "app_assert.h"
50
51 /*
52 * DEFINITIONS
53 *****************************************************************************************
54 */
55 #define APP_ASSERT_FILE_NAME_LEN 64 /**< Length of file name. */
56 #define APP_ASSERT_EXPR_NAME_LEN 64 /**< Length of expression. */
57 #define APP_ASSERT_ERR_MAGIC_1 0xAAAAAAAA /**< App assert error magic 1. */
58 #define APP_ASSERT_ERR_MAGIC_2 0xBBBBBBBB /**< App assert error magic 2. */
59 #define APP_ASSERT_ERR_MAGIC_3 0xCCCCCCCC /**< App assert error magic 3. */
60 #define APP_ASSERT_ERR_MAGIC_4 0xDDDDDDDD /**< App assert error magic 4. */
61 #define APP_ASSERT_PARAM_MAGIC_1 0x55555555 /**< App assert parameter magic 1. */
62 #define APP_ASSERT_PARAM_MAGIC_2 0x66666666 /**< App assert parameter magic 2. */
63 #define APP_ASSERT_PARAM_MAGIC_3 0x77777777 /**< App assert parameter magic 3. */
64 #define APP_ASSERT_PARAM_MAGIC_4 0x88888888 /**< App assert parameter magic 4. */
65 #define APP_ASSERT_WARN_MAGIC_1 0x11111111 /**< App assert error magic 1. */
66 #define APP_ASSERT_WARN_MAGIC_2 0x22222222 /**< App assert error magic 2. */
67 #define APP_ASSERT_WARN_MAGIC_3 0x33333333 /**< App assert error magic 3. */
68 #define APP_ASSERT_WARN_MAGIC_4 0x44444444 /**< App assert error magic 4. */
69
70 #define APP_ASSERT_ERROR 0x00 /**< Assert type: Error. */
71 #define APP_ASSERT_WARNING 0x01 /**< Assert type: Waring. */
72 #define APP_ASSERT_PARAM 0x02 /**< Assert type: Parameter check. */
73
74 /*
75 * STRUCTURES
76 *****************************************************************************************
77 */
78 /**@brief Assert information save. */
79 struct app_asser_info_t {
80 char file_name[APP_ASSERT_FILE_NAME_LEN];
81 int magic1;
82 int file_line;
83 int magic2;
84 int param0;
85 int magic3;
86 int param1;
87 int magic4;
88 char expr[APP_ASSERT_EXPR_NAME_LEN];
89 };
90
91 /*
92 * LOCAL VARIABLE DEFINITIONS
93 *****************************************************************************************
94 */
95 static struct app_asser_info_t s_assert_info;
96
97 static sys_assert_cb_t s_assert_cbs = {
98 .assert_err_cb = app_assert_err_cb,
99 .assert_param_cb = app_assert_param_cb,
100 .assert_warn_cb = app_assert_warn_cb,
101 };
102
103 /*
104 * LOCAL VARIABLE DEFINITIONS
105 *****************************************************************************************
106 */
107 /**
108 *****************************************************************************************
109 * @brief App assert infomation output.
110 *
111 * @param[in] assert_type: Assert type.
112 *****************************************************************************************
113 */
app_assert_info_output(uint8_t assert_type)114 static void app_assert_info_output(uint8_t assert_type)
115 {
116 char assert_info[1024] = {0};
117 uint32_t ret;
118
119 s_assert_info.file_name[APP_ASSERT_FILE_NAME_LEN - 1] = ' ';
120 s_assert_info.expr[APP_ASSERT_FILE_NAME_LEN - 1] = ' ';
121
122 if (assert_type == APP_ASSERT_ERROR) {
123 ret = sprintf_s(assert_info, sizeof(assert_info), "[ERROR] %s", s_assert_info.expr);
124 if (ret < 0) {
125 return;
126 }
127 } else if (assert_type == APP_ASSERT_WARNING) {
128 ret = sprintf_s(assert_info, sizeof(assert_info), "[WARNING] Param0:%d,Param1:%d", s_assert_info.param0,
129 s_assert_info.param1);
130 if (ret < 0) {
131 return;
132 }
133 } else if (assert_type == APP_ASSERT_PARAM) {
134 ret = sprintf_s(assert_info, sizeof(assert_info), "[PARAM] Param0:%d,Param1:%d", \
135 s_assert_info.param0, s_assert_info.param1);
136 if (ret < 0) {
137 return;
138 }
139 }
140
141 app_log_output(APP_LOG_LVL_ERROR,
142 APP_LOG_TAG,
143 s_assert_info.file_name,
144 "",
145 s_assert_info.file_line,
146 "%s",
147 assert_info);
148
149 app_log_flush();
150 }
151
152
153 /*
154 * GLOBAL FUNCTION DEFINITIONS
155 *****************************************************************************************
156 */
app_assert_warn_cb(int param0,int param1,const char * file,int line)157 __WEAK void app_assert_warn_cb(int param0, int param1, const char *file, int line)
158 {
159 uint32_t file_name_len;
160
161 file_name_len = (APP_ASSERT_FILE_NAME_LEN < strlen(file)) ? APP_ASSERT_FILE_NAME_LEN : strlen(file);
162
163 memset_s(&s_assert_info, sizeof(s_assert_info), 0, sizeof(s_assert_info));
164 memcpy_s(s_assert_info.file_name, sizeof (s_assert_info.file_name), file, file_name_len);
165
166 s_assert_info.magic1 = APP_ASSERT_WARN_MAGIC_1;
167 s_assert_info.file_line = line;
168 s_assert_info.magic2 = APP_ASSERT_WARN_MAGIC_2;
169 s_assert_info.param0 = param0;
170 s_assert_info.magic3 = APP_ASSERT_WARN_MAGIC_3;
171 s_assert_info.param1 = param1;
172 s_assert_info.magic4 = APP_ASSERT_WARN_MAGIC_4;
173
174 // Also can store assert info to flash
175 app_assert_info_output(APP_ASSERT_WARNING);
176 }
177
app_assert_param_cb(int param0,int param1,const char * file,int line)178 __WEAK void app_assert_param_cb(int param0, int param1, const char *file, int line)
179 {
180 __disable_irq();
181
182 uint32_t file_name_len;
183
184 file_name_len = (APP_ASSERT_FILE_NAME_LEN < strlen(file)) ? APP_ASSERT_FILE_NAME_LEN : strlen(file);
185
186 memset_s(&s_assert_info, sizeof (s_assert_info), 0, sizeof(s_assert_info));
187 memcpy_s(s_assert_info.file_name, sizeof (s_assert_info.file_name), file, file_name_len);
188
189 s_assert_info.magic1 = (int)APP_ASSERT_PARAM_MAGIC_1;
190 s_assert_info.file_line = line;
191 s_assert_info.magic2 = (int)APP_ASSERT_PARAM_MAGIC_2;
192 s_assert_info.param0 = param0;
193 s_assert_info.magic3 = (int)APP_ASSERT_PARAM_MAGIC_3;
194 s_assert_info.param1 = param1;
195 s_assert_info.magic4 = (int)APP_ASSERT_PARAM_MAGIC_4;
196
197 // Also can store assert info to flash
198 app_assert_info_output(APP_ASSERT_PARAM);
199 while (1) {
200 };
201 }
202
app_assert_err_cb(const char * expr,const char * file,int line)203 __WEAK void app_assert_err_cb(const char *expr, const char *file, int line)
204 {
205 __disable_irq();
206
207 uint32_t file_name_len;
208 uint32_t expre_len;
209
210 file_name_len = (APP_ASSERT_FILE_NAME_LEN < strlen(file)) ? APP_ASSERT_FILE_NAME_LEN : strlen(file);
211 expre_len = (APP_ASSERT_EXPR_NAME_LEN < strlen(expr)) ? APP_ASSERT_EXPR_NAME_LEN : strlen(expr);
212
213 memset_s(&s_assert_info, sizeof(s_assert_info), 0, sizeof(s_assert_info));
214 memcpy_s(s_assert_info.file_name, sizeof(s_assert_info.file_name), file, file_name_len);
215 memcpy_s(s_assert_info.expr, sizeof(s_assert_info.expr), expr, expre_len);
216 s_assert_info.magic1 = (int)APP_ASSERT_ERR_MAGIC_1;
217 s_assert_info.file_line = line;
218 s_assert_info.magic2 = (int)APP_ASSERT_ERR_MAGIC_2;
219 s_assert_info.magic3 = (int)APP_ASSERT_ERR_MAGIC_3;
220 s_assert_info.magic4 = (int)APP_ASSERT_ERR_MAGIC_4;
221
222 // Also can store assert info to flash
223 app_assert_info_output(APP_ASSERT_ERROR);
224 while (1) {
225 };
226 }
227
app_assert_init(void)228 void app_assert_init(void)
229 {
230 sys_assert_cb_register(&s_assert_cbs);
231 }
232
app_assert_handler(const char * expr,const char * file,int line)233 void app_assert_handler(const char *expr, const char *file, int line)
234 {
235 if (s_assert_cbs.assert_err_cb) {
236 s_assert_cbs.assert_err_cb(expr, file, line);
237 }
238 }
239