Fix crc calculation error, code-style issues and other fixes

pull/8702/head
Senthil Ramakrishnan 2018-11-16 12:26:23 -06:00
parent f1926c0ea9
commit 423b52e1c3
4 changed files with 85 additions and 78 deletions

View File

@ -29,7 +29,8 @@
#define MSG_KEY_DEVICE_READY "crash_reporting_ready"
#define MSG_KEY_DEVICE_ERROR "crash_reporting_inject_error"
void mbed_error_reboot_callback(mbed_error_ctx *error_context) {
void mbed_error_reboot_callback(mbed_error_ctx *error_context)
{
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_OUT_OF_MEMORY, error_context->error_status);
TEST_ASSERT_EQUAL_UINT(1, error_context->error_reboot_count);
mbed_reset_reboot_error_info();
@ -46,12 +47,15 @@ void test_crash_reporting()
static char _key[MSG_KEY_LEN + 1] = { };
static char _value[MSG_VALUE_LEN + 1] = { };
printf("\nWaiting for inject error\n");
greentea_parse_kv(_key, _value, MSG_KEY_LEN, MSG_VALUE_LEN);
printf("\nGot inject error\n");
if (strcmp(_key, MSG_KEY_DEVICE_ERROR) == 0) {
printf("\nErroring...\n");
MBED_ERROR1(MBED_ERROR_OUT_OF_MEMORY, "Executing crash reporting test.", 0xDEADBAD);
TEST_ASSERT_MESSAGE(0, "crash_reporting() error call failed.");
}
printf("\nWaiting for inject error");
TEST_ASSERT_MESSAGE(0, "Unexpected message received.");
}

View File

@ -40,7 +40,7 @@
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line, print_thread_info) print_error_report(ctx, error_msg, error_filename, error_line, print_thread_info)
static void print_error_report(const mbed_error_ctx *ctx, const char *, const char *error_filename, int error_line, bool print_thread_info);
#else
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) ((void) 0)
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line, print_thread_info) ((void) 0)
#endif
static core_util_atomic_flag error_in_progress = CORE_UTIL_ATOMIC_FLAG_INIT;
@ -66,13 +66,14 @@ 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(void *data, int datalen)
static unsigned int compute_crc32(const void *data, int datalen)
{
const unsigned int polynomial = 0x04C11DB7; /* divisor is 32bit */
unsigned int crc = 0; /* CRC value is 32bit */
unsigned char *buf = (unsigned char *)data;//use a temp variable to make code readable and to avoid typecasting issues.
for ( ;datalen>=0; datalen-- ) {
unsigned char b = (*(unsigned char *)data);
for (; datalen>0; datalen-- ) {
unsigned char b = *buf++;
crc ^= (unsigned int)(b << 24); /* move byte into upper 8bit */
for (int i = 0; i < 8; i++) {
/* is MSB 1 */
@ -191,7 +192,8 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
return MBED_SUCCESS;
}
WEAK void mbed_error_reboot_callback(mbed_error_ctx *error_context) {
WEAK void mbed_error_reboot_callback(mbed_error_ctx *error_context)
{
//Dont do anything here, let application override this if required.
}
@ -200,9 +202,12 @@ mbed_error_status_t mbed_error_initialize(void)
{
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
uint32_t crc_val = 0;
//Just check if we have valid value for error_status, if error_status is positive(which is not valid), no need to check crc
if (report_error_ctx->error_status < 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)) {
if ((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0)) {
is_reboot_error_valid = true;
#if MBED_CONF_PLATFORM_REBOOT_CRASH_REPORT_ENABLED && !defined(NDEBUG)
//Report the error info
@ -224,8 +229,8 @@ mbed_error_status_t mbed_error_initialize(void)
crc_val = compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
report_error_ctx->crc_error_ctx = crc_val;
}
}
#endif
return MBED_SUCCESS;
}
@ -291,8 +296,6 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat
#endif
#endif
mbed_halt_system();
return MBED_ERROR_FAILED_OPERATION;
}
//Register an application defined callback with error handling