Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_error_hist.c Source File

mbed_error_hist.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include <stdlib.h>
00017 #include <stdarg.h>
00018 #include "device.h"
00019 #include "platform/mbed_error.h"
00020 #include "platform/mbed_toolchain.h"
00021 #include "platform/mbed_critical.h"
00022 #include "platform/mbed_interface.h"
00023 
00024 #ifndef MBED_CONF_ERROR_HIST_DISABLED
00025 #include "platform/mbed_error_hist.h"
00026 
00027 static mbed_error_ctx mbed_error_ctx_log[MBED_CONF_ERROR_HIST_SIZE] = {0};
00028 static int error_log_count = -1;
00029 
00030 mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx)
00031 {
00032     //Return error if error_ctx is NULL
00033     if(NULL == error_ctx) {
00034         return MBED_ERROR_INVALID_ARGUMENT;
00035     }
00036     
00037     core_util_critical_section_enter();
00038     error_log_count++;
00039     memcpy(&mbed_error_ctx_log[error_log_count % MBED_CONF_ERROR_HIST_SIZE], error_ctx, sizeof(mbed_error_ctx) );
00040     core_util_critical_section_exit(); 
00041     
00042     return MBED_SUCCESS;
00043 }
00044 
00045 mbed_error_status_t mbed_error_hist_get(int index, mbed_error_ctx *error_ctx)
00046 {
00047     //Return error if index is more than max log size
00048     if(index >= MBED_CONF_ERROR_HIST_SIZE) {
00049         return MBED_ERROR_INVALID_ARGUMENT;
00050     }
00051     
00052     core_util_critical_section_enter();
00053     //calculate the index where we want to pick the ctx
00054     if(error_log_count >= MBED_CONF_ERROR_HIST_SIZE) {
00055         index = (error_log_count + index + 1) % MBED_CONF_ERROR_HIST_SIZE;
00056     }
00057     core_util_critical_section_exit(); 
00058     memcpy(error_ctx, &mbed_error_ctx_log[index % MBED_CONF_ERROR_HIST_SIZE], sizeof(mbed_error_ctx) );
00059         
00060     return MBED_SUCCESS;
00061 }
00062 
00063 mbed_error_ctx *mbed_error_hist_get_entry(void)
00064 {
00065     core_util_critical_section_enter();
00066     error_log_count++;
00067     mbed_error_ctx *ctx = &mbed_error_ctx_log[error_log_count % MBED_CONF_ERROR_HIST_SIZE];
00068     core_util_critical_section_exit(); 
00069     
00070     return ctx;
00071 }
00072 
00073 mbed_error_status_t mbed_error_hist_get_last_error(mbed_error_ctx *error_ctx)
00074 {
00075     if(-1 == error_log_count) {
00076         return MBED_ERROR_ITEM_NOT_FOUND;
00077     }
00078     core_util_critical_section_enter();
00079     memcpy(error_ctx, &mbed_error_ctx_log[error_log_count % MBED_CONF_ERROR_HIST_SIZE], sizeof(mbed_error_ctx) );
00080     core_util_critical_section_exit(); 
00081     
00082     return MBED_SUCCESS;
00083 }
00084 
00085 int mbed_error_hist_get_count()
00086 {
00087     return (error_log_count >= MBED_CONF_ERROR_HIST_SIZE? MBED_CONF_ERROR_HIST_SIZE:error_log_count+1);
00088 }
00089 
00090 mbed_error_status_t mbed_error_hist_reset()
00091 {
00092     core_util_critical_section_enter();
00093     error_log_count = -1;
00094     core_util_critical_section_exit(); 
00095     
00096     return MBED_SUCCESS;
00097 }
00098 
00099 #endif