1 | /* mbed Microcontroller Library
|
---|
2 | * Copyright (c) 2006-2013 ARM Limited
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <stdarg.h>
|
---|
18 | #include "device.h"
|
---|
19 | #include "platform/mbed_error.h"
|
---|
20 | #include "platform/mbed_toolchain.h"
|
---|
21 | #include "platform/mbed_critical.h"
|
---|
22 | #include "platform/mbed_interface.h"
|
---|
23 |
|
---|
24 | #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED
|
---|
25 | #include "platform/mbed_error_hist.h"
|
---|
26 |
|
---|
27 | static mbed_error_ctx mbed_error_ctx_log[MBED_CONF_PLATFORM_ERROR_HIST_SIZE] = {0};
|
---|
28 | static int error_log_count = -1;
|
---|
29 |
|
---|
30 | mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx)
|
---|
31 | {
|
---|
32 | //Return error if error_ctx is NULL
|
---|
33 | if (NULL == error_ctx) {
|
---|
34 | return MBED_ERROR_INVALID_ARGUMENT;
|
---|
35 | }
|
---|
36 |
|
---|
37 | core_util_critical_section_enter();
|
---|
38 | error_log_count++;
|
---|
39 | memcpy(&mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], error_ctx, sizeof(mbed_error_ctx));
|
---|
40 | core_util_critical_section_exit();
|
---|
41 |
|
---|
42 | return MBED_SUCCESS;
|
---|
43 | }
|
---|
44 |
|
---|
45 | mbed_error_status_t mbed_error_hist_get(int index, mbed_error_ctx *error_ctx)
|
---|
46 | {
|
---|
47 | //Return error if index is more than max log size
|
---|
48 | if (index >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
|
---|
49 | return MBED_ERROR_INVALID_ARGUMENT;
|
---|
50 | }
|
---|
51 |
|
---|
52 | core_util_critical_section_enter();
|
---|
53 | //calculate the index where we want to pick the ctx
|
---|
54 | if (error_log_count >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
|
---|
55 | index = (error_log_count + index + 1) % MBED_CONF_PLATFORM_ERROR_HIST_SIZE;
|
---|
56 | }
|
---|
57 | core_util_critical_section_exit();
|
---|
58 | memcpy(error_ctx, &mbed_error_ctx_log[index % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx));
|
---|
59 |
|
---|
60 | return MBED_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 | mbed_error_ctx *mbed_error_hist_get_entry(void)
|
---|
64 | {
|
---|
65 | core_util_critical_section_enter();
|
---|
66 | error_log_count++;
|
---|
67 | mbed_error_ctx *ctx = &mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE];
|
---|
68 | core_util_critical_section_exit();
|
---|
69 |
|
---|
70 | return ctx;
|
---|
71 | }
|
---|
72 |
|
---|
73 | mbed_error_status_t mbed_error_hist_get_last_error(mbed_error_ctx *error_ctx)
|
---|
74 | {
|
---|
75 | if (-1 == error_log_count) {
|
---|
76 | return MBED_ERROR_ITEM_NOT_FOUND;
|
---|
77 | }
|
---|
78 | core_util_critical_section_enter();
|
---|
79 | memcpy(error_ctx, &mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx));
|
---|
80 | core_util_critical_section_exit();
|
---|
81 |
|
---|
82 | return MBED_SUCCESS;
|
---|
83 | }
|
---|
84 |
|
---|
85 | int mbed_error_hist_get_count()
|
---|
86 | {
|
---|
87 | return (error_log_count >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE ? MBED_CONF_PLATFORM_ERROR_HIST_SIZE : error_log_count + 1);
|
---|
88 | }
|
---|
89 |
|
---|
90 | mbed_error_status_t mbed_error_hist_reset()
|
---|
91 | {
|
---|
92 | core_util_critical_section_enter();
|
---|
93 | error_log_count = -1;
|
---|
94 | core_util_critical_section_exit();
|
---|
95 |
|
---|
96 | return MBED_SUCCESS;
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endif
|
---|