23
23
#include "platform/mbed_error_log.h"
24
24
#include "platform/mbed_error_report.h"
25
25
#include "platform/mbed_interface.h"
26
+
26
27
#if DEVICE_STDIO_MESSAGES
27
28
#include <stdio.h>
28
29
#endif
29
30
30
31
static uint8_t error_in_progress = 0 ;
31
32
static int error_count = 0 ;
32
33
static mbed_error_ctx first_error_ctx = {0 };
33
- static mbed_error_ctx current_error_ctx = {0 };
34
+ static mbed_error_ctx last_error_ctx = {0 };
34
35
static MbedErrorHook error_hook = NULL ;
35
36
36
37
//Helper function to get the current SP
@@ -83,18 +84,24 @@ WEAK void error(const char* format, ...) {
83
84
}
84
85
85
86
//Set an error status with the error handling system
86
- MbedErrorStatus set_error (MbedErrorStatus error_status , const char * error_msg , unsigned int error_value , const char * filename , int line_number )
87
+ MbedErrorStatus handle_error (MbedErrorStatus error_status , const char * error_msg , unsigned int error_value , const char * filename , int line_number )
87
88
{
89
+ mbed_error_ctx current_error_ctx ;
90
+
88
91
//Error status should always be < 0
89
92
if (error_status >= 0 ) {
90
- return ERROR_INVALID_ARGUMENT ;
93
+ //This is a weird situation, someone called set_error with invalid error code.
94
+ //We will still handle the situation but change the error code to ERROR_INVALID_ARGUMENT, atleast the context will have info on who called it
95
+ error_status = ERROR_INVALID_ARGUMENT ;
91
96
}
92
97
93
98
//Use critsect here, as we don't want processing more than one error at the same time
94
99
core_util_critical_section_enter ();
95
100
96
101
//Increment error count
97
102
error_count ++ ;
103
+ //Use critsect here, as we don't want processing more than one error at the same time
104
+ core_util_critical_section_exit ();
98
105
99
106
//Clear the context capturing buffer
100
107
memset (& current_error_ctx , sizeof (mbed_error_ctx ), 0 );
@@ -125,16 +132,13 @@ MbedErrorStatus set_error(MbedErrorStatus error_status, const char *error_msg, u
125
132
current_error_ctx .thread_stack_mem = (uint32_t )current_thread -> stack_mem ;
126
133
current_error_ctx .thread_current_sp = get_current_sp ();
127
134
128
- //Call the error hook if available
129
- if (error_hook != NULL ) {
130
- error_hook (& current_error_ctx );
131
- }
132
-
133
135
#ifndef MBED_CONF_ERROR_LOG_DISABLED
134
136
//Log the error with error log
135
137
mbed_log_put_error (& current_error_ctx );
136
138
#endif
137
139
140
+ //Use critsect here, as we don't want processing more than one error at the same time
141
+ core_util_critical_section_enter ();
138
142
//Report the error
139
143
mbed_report_error (& current_error_ctx , (char * )error_msg );
140
144
@@ -143,9 +147,17 @@ MbedErrorStatus set_error(MbedErrorStatus error_status, const char *error_msg, u
143
147
memcpy (& first_error_ctx , & current_error_ctx , sizeof (mbed_error_ctx ));
144
148
}
145
149
150
+ //copy this error to last error
151
+ memcpy (& last_error_ctx , & current_error_ctx , sizeof (mbed_error_ctx ));
152
+
146
153
//Use critsect here, as we don't want processing more than one error at the same time
147
154
core_util_critical_section_exit ();
148
-
155
+
156
+ //Call the error hook if available
157
+ if (error_hook != NULL ) {
158
+ error_hook (& last_error_ctx );
159
+ }
160
+
149
161
return ERROR_SUCCESS ;
150
162
}
151
163
@@ -160,7 +172,7 @@ MbedErrorStatus get_first_error(void)
160
172
MbedErrorStatus get_last_error (void )
161
173
{
162
174
//return the last error recorded
163
- return current_error_ctx .error_status ;
175
+ return last_error_ctx .error_status ;
164
176
}
165
177
166
178
//Gets the current error count
@@ -171,10 +183,16 @@ int get_error_count(void)
171
183
}
172
184
173
185
//Sets a fatal error
174
- MbedErrorStatus set_error_fatal (MbedErrorStatus error_status , const char * error_msg , unsigned int error_value , const char * filename , int line_number )
186
+ MbedErrorStatus set_warning (MbedErrorStatus error_status , const char * error_msg , unsigned int error_value , const char * filename , int line_number )
187
+ {
188
+ return handle_error (error_status , error_msg , error_value , filename , line_number );
189
+ }
190
+
191
+ //Sets a fatal error
192
+ MbedErrorStatus set_error (MbedErrorStatus error_status , const char * error_msg , unsigned int error_value , const char * filename , int line_number )
175
193
{
176
194
//set the error reported and then halt the system
177
- if ( ERROR_SUCCESS != set_error (error_status , error_msg , error_value , filename , line_number ) )
195
+ if ( ERROR_SUCCESS != handle_error (error_status , error_msg , error_value , filename , line_number ) )
178
196
return ERROR_FAILED_OPERATION ;
179
197
mbed_halt_system ();
180
198
@@ -203,12 +221,12 @@ MbedErrorStatus get_first_error_log_info (mbed_error_ctx *error_info)
203
221
//Retrieve the last error context from error log
204
222
MbedErrorStatus get_last_error_log_info (mbed_error_ctx * error_info )
205
223
{
206
- memcpy (error_info , & current_error_ctx , sizeof (mbed_error_ctx ));
224
+ memcpy (error_info , & last_error_ctx , sizeof (mbed_error_ctx ));
207
225
return ERROR_SUCCESS ;
208
226
}
209
227
210
228
//Makes an MbedErrorStatus value
211
- MbedErrorStatus make_mbed_error (MbedErrorType error_type , MbedEntityType entity , MbedErrorCode error_code )
229
+ MbedErrorStatus make_mbed_error (MbedErrorType error_type , MbedModuleType entity , MbedErrorCode error_code )
212
230
{
213
231
switch (error_type )
214
232
{
@@ -232,7 +250,7 @@ MbedErrorStatus make_mbed_error(MbedErrorType error_type, MbedEntityType entity,
232
250
}
233
251
234
252
//If we are passed incorrect values return a generic system error
235
- return MAKE_MBED_ERROR (ERROR_TYPE_SYSTEM , ENTITY_UNKNOWN , ERROR_CODE_UNKNOWN );
253
+ return MAKE_MBED_ERROR (ERROR_TYPE_SYSTEM , MODULE_UNKNOWN , ERROR_CODE_UNKNOWN );
236
254
}
237
255
238
256
/**
@@ -245,7 +263,7 @@ MbedErrorStatus clear_all_errors(void)
245
263
MbedErrorStatus status = ERROR_SUCCESS ;
246
264
247
265
//Clear the error and context capturing buffer
248
- memset (& current_error_ctx , sizeof (mbed_error_ctx ), 0 );
266
+ memset (& last_error_ctx , sizeof (mbed_error_ctx ), 0 );
249
267
//reset error count to 0
250
268
error_count = 0 ;
251
269
#ifndef MBED_CONF_ERROR_LOG_DISABLED
@@ -276,13 +294,13 @@ MbedErrorStatus save_error_log(const char *path)
276
294
277
295
//Ensure path is valid
278
296
if (path == NULL ) {
279
- ret = MAKE_ERROR (ENTITY_PLATFORM , ERROR_CODE_INVALID_ARGUMENT );
297
+ ret = MAKE_ERROR (MODULE_PLATFORM , ERROR_CODE_INVALID_ARGUMENT );
280
298
goto exit ;
281
299
}
282
300
283
301
//Open the file for saving the error log info
284
302
if ((error_log_file = fopen ( path , "w" ) ) == NULL ){
285
- ret = MAKE_ERROR (ENTITY_PLATFORM , ERROR_CODE_OPEN_FAILED );
303
+ ret = MAKE_ERROR (MODULE_PLATFORM , ERROR_CODE_OPEN_FAILED );
286
304
goto exit ;
287
305
}
288
306
@@ -292,16 +310,16 @@ MbedErrorStatus save_error_log(const char *path)
292
310
(unsigned int )first_error_ctx .thread_id ,
293
311
(unsigned int )first_error_ctx .error_address ,
294
312
(unsigned int )first_error_ctx .error_value ) <= 0 ) {
295
- ret = MAKE_ERROR (ENTITY_PLATFORM , ERROR_CODE_WRITE_FAILED );
313
+ ret = MAKE_ERROR (MODULE_PLATFORM , ERROR_CODE_WRITE_FAILED );
296
314
goto exit ;
297
315
}
298
316
299
317
if (fprintf (error_log_file , "\nLast Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
300
- (unsigned int )current_error_ctx .error_status ,
301
- (unsigned int )current_error_ctx .thread_id ,
302
- (unsigned int )current_error_ctx .error_address ,
303
- (unsigned int )current_error_ctx .error_value ) <= 0 ) {
304
- ret = MAKE_ERROR (ENTITY_PLATFORM , ERROR_CODE_WRITE_FAILED );
318
+ (unsigned int )last_error_ctx .error_status ,
319
+ (unsigned int )last_error_ctx .thread_id ,
320
+ (unsigned int )last_error_ctx .error_address ,
321
+ (unsigned int )last_error_ctx .error_value ) <= 0 ) {
322
+ ret = MAKE_ERROR (MODULE_PLATFORM , ERROR_CODE_WRITE_FAILED );
305
323
goto exit ;
306
324
}
307
325
@@ -315,7 +333,7 @@ MbedErrorStatus save_error_log(const char *path)
315
333
(unsigned int )ctx .thread_id ,
316
334
(unsigned int )ctx .error_address ,
317
335
(unsigned int )ctx .error_value ) <= 0 ) {
318
- ret = MAKE_ERROR (ENTITY_PLATFORM , ERROR_CODE_WRITE_FAILED );
336
+ ret = MAKE_ERROR (MODULE_PLATFORM , ERROR_CODE_WRITE_FAILED );
319
337
goto exit ;
320
338
}
321
339
}
0 commit comments