Skip to content

Commit

Permalink
Change the reboot-max value semantics and code style and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SenRamakri committed Nov 16, 2018
1 parent 254062f commit a07a373
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cmsis/TARGET_CORTEX_M/mbed_fault_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ void print_context_info(void);

#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
//Global for populating the context in exception handler
mbed_fault_context_t *mbed_fault_context=(mbed_fault_context_t *)((uint32_t)FAULT_CONTEXT_LOCATION);
mbed_fault_context_t *const mbed_fault_context=(mbed_fault_context_t *)(FAULT_CONTEXT_LOCATION);
#else
mbed_fault_context_t fault_context;
mbed_fault_context_t *mbed_fault_context=(mbed_fault_context_t *)&fault_context;
mbed_fault_context_t *const mbed_fault_context=(mbed_fault_context_t *)&fault_context;
#endif

//This is a handler function called from Fault handler to print the error information out.
Expand Down
36 changes: 17 additions & 19 deletions platform/mbed_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static mbed_error_ctx first_error_ctx = {0};

#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
//Global for populating the context in exception handler
static mbed_error_ctx *report_error_ctx=(mbed_error_ctx *)((uint32_t)ERROR_CONTEXT_LOCATION);
static mbed_error_ctx *const report_error_ctx=(mbed_error_ctx *)(ERROR_CONTEXT_LOCATION);
static bool is_reboot_error_valid = false;
#endif

Expand All @@ -66,13 +66,13 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
//we dont have many uses cases to create a C wrapper for MbedCRC and the data
//we calculate CRC on in this context is very less we will use a local
//implementation here.
static unsigned int compute_crc32(unsigned char *data, int datalen)
static unsigned int compute_crc32(void *data, int datalen)
{
const unsigned int polynomial = 0x04C11DB7; /* divisor is 32bit */
unsigned int crc = 0; /* CRC value is 32bit */

for( ;datalen>=0; datalen-- ) {
unsigned char b = *data;
for ( ;datalen>=0; datalen-- ) {
unsigned char b = (*(unsigned char *)data);
crc ^= (unsigned int )(b << 24); /* move byte into upper 8bit */
for (int i = 0; i < 8; i++) {
/* is MSB 1 */
Expand Down Expand Up @@ -200,9 +200,9 @@ mbed_error_status_t mbed_error_initialize(void)
{
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
uint32_t crc_val = 0;
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
//Read report_error_ctx and check if CRC is correct for report_error_ctx
if((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0)) {
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
//Read report_error_ctx and check if CRC is correct, and with valid status code
if ((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0) && (report_error_ctx->error_status < 0)) {
is_reboot_error_valid = true;
#if MBED_CONF_PLATFORM_REBOOT_CRASH_REPORT_ENABLED && !defined(NDEBUG)
//Report the error info
Expand All @@ -214,14 +214,14 @@ mbed_error_status_t mbed_error_initialize(void)

//Enforce max-reboot only if auto reboot is enabled
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED
if( report_error_ctx->error_reboot_count > MBED_CONF_PLATFORM_ERROR_REBOOT_MAX ) {
if ( report_error_ctx->error_reboot_count >= MBED_CONF_PLATFORM_ERROR_REBOOT_MAX ) {
//We have rebooted more than enough, hold the system here.
mbed_error_printf("\n== Reboot count(=%ld) exceeded maximum, system halting ==\n", report_error_ctx->error_reboot_count);
mbed_halt_system();
}
#endif
report_error_ctx->is_error_processed = 1;//Set the flag that we already processed this error
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
report_error_ctx->crc_error_ctx = crc_val;
}
#endif
Expand Down Expand Up @@ -270,28 +270,26 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat

#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
uint32_t crc_val = 0;
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
//Read report_error_ctx and check if CRC is correct for report_error_ctx
if(report_error_ctx->crc_error_ctx == crc_val) {
if (report_error_ctx->crc_error_ctx == crc_val) {
uint32_t current_reboot_count = report_error_ctx->error_reboot_count;
last_error_ctx.error_reboot_count = current_reboot_count + 1;
} else {
last_error_ctx.error_reboot_count = 1;
}
last_error_ctx.is_error_processed = 0;//Set the flag that this is a new error
//Update the struct with crc
last_error_ctx.crc_error_ctx = compute_crc32( (unsigned char *)&last_error_ctx, ((uint32_t)&(last_error_ctx.crc_error_ctx) - (uint32_t)&last_error_ctx) );
last_error_ctx.crc_error_ctx = compute_crc32( &last_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
//Protect report_error_ctx while we update it
core_util_critical_section_enter();
memcpy(report_error_ctx, &last_error_ctx, sizeof(mbed_error_ctx));
core_util_critical_section_exit();
//We need not call delete_mbed_crc(crc_obj) here as we are going to reset the system anyway, and calling delete while handling a fatal error may cause nested exception
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED && (MBED_CONF_PLATFORM_ERROR_REBOOT_MAX > 0)
system_reset();//do a system reset to get the system rebooted
while(1);
#endif
#endif

mbed_halt_system();

return MBED_ERROR_FAILED_OPERATION;
Expand Down Expand Up @@ -325,12 +323,12 @@ mbed_error_status_t mbed_reset_reboot_error_info()
mbed_error_status_t mbed_reset_reboot_count()
{
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
if(is_reboot_error_valid) {
if (is_reboot_error_valid) {
uint32_t crc_val = 0;
core_util_critical_section_enter();
report_error_ctx->error_reboot_count = 0;//Set reboot count to 0
//Update CRC
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
report_error_ctx->crc_error_ctx = crc_val;
core_util_critical_section_exit();
return MBED_SUCCESS;
Expand All @@ -345,7 +343,7 @@ mbed_error_status_t mbed_get_reboot_error_info(mbed_error_ctx *error_info)
mbed_error_status_t status = MBED_ERROR_ITEM_NOT_FOUND;
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
if (is_reboot_error_valid) {
if(error_info != NULL) {
if (error_info != NULL) {
memcpy(error_info, report_error_ctx, sizeof(mbed_error_ctx));
status = MBED_SUCCESS;
} else {
Expand Down Expand Up @@ -519,7 +517,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
#endif

#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
if(print_thread_info == true) {
if (print_thread_info) {
mbed_error_printf("\nNext:");
print_thread(osRtxInfo.thread.run.next);

Expand Down
2 changes: 1 addition & 1 deletion platform/mbed_lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
},
"error-reboot-max": {
"help": "Maximum number of auto reboots permitted when an error happens.",
"value": 0
"value": 1
},
"fatal-error-auto-reboot-enabled": {
"help": "Setting this to true enables auto-reboot on a fatal error.",
Expand Down

0 comments on commit a07a373

Please sign in to comment.