mbed_error_hist.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED
  24. #include "platform/mbed_error_hist.h"
  25. static mbed_error_ctx mbed_error_ctx_log[MBED_CONF_PLATFORM_ERROR_HIST_SIZE] = {0};
  26. static int error_log_count = -1;
  27. mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx)
  28. {
  29. //Return error if error_ctx is NULL
  30. if (NULL == error_ctx) {
  31. return MBED_ERROR_INVALID_ARGUMENT;
  32. }
  33. core_util_critical_section_enter();
  34. error_log_count++;
  35. memcpy(&mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], error_ctx, sizeof(mbed_error_ctx));
  36. core_util_critical_section_exit();
  37. return MBED_SUCCESS;
  38. }
  39. mbed_error_status_t mbed_error_hist_get(int index, mbed_error_ctx *error_ctx)
  40. {
  41. //Return error if index is more than max log size
  42. if (index >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
  43. return MBED_ERROR_INVALID_ARGUMENT;
  44. }
  45. core_util_critical_section_enter();
  46. //calculate the index where we want to pick the ctx
  47. if (error_log_count >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
  48. index = (error_log_count + index + 1) % MBED_CONF_PLATFORM_ERROR_HIST_SIZE;
  49. }
  50. core_util_critical_section_exit();
  51. memcpy(error_ctx, &mbed_error_ctx_log[index % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx));
  52. return MBED_SUCCESS;
  53. }
  54. mbed_error_ctx *mbed_error_hist_get_entry(void)
  55. {
  56. core_util_critical_section_enter();
  57. error_log_count++;
  58. mbed_error_ctx *ctx = &mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE];
  59. core_util_critical_section_exit();
  60. return ctx;
  61. }
  62. mbed_error_status_t mbed_error_hist_get_last_error(mbed_error_ctx *error_ctx)
  63. {
  64. if (-1 == error_log_count) {
  65. return MBED_ERROR_ITEM_NOT_FOUND;
  66. }
  67. core_util_critical_section_enter();
  68. memcpy(error_ctx, &mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx));
  69. core_util_critical_section_exit();
  70. return MBED_SUCCESS;
  71. }
  72. int mbed_error_hist_get_count()
  73. {
  74. return (error_log_count >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE ? MBED_CONF_PLATFORM_ERROR_HIST_SIZE : error_log_count + 1);
  75. }
  76. mbed_error_status_t mbed_error_hist_reset()
  77. {
  78. core_util_critical_section_enter();
  79. error_log_count = -1;
  80. core_util_critical_section_exit();
  81. return MBED_SUCCESS;
  82. }
  83. #endif