nrf_strerror.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (c) 2011 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_STRERROR)
  42. #include "nrf_strerror.h"
  43. /**
  44. * @brief Macro for adding an entity to the description array.
  45. *
  46. * Macro that helps to create a single entity in the description array.
  47. */
  48. #define NRF_STRERROR_ENTITY(mnemonic) {.code = mnemonic, .name = #mnemonic}
  49. /**
  50. * @brief Array entity element that describes an error.
  51. */
  52. typedef struct
  53. {
  54. ret_code_t code; /**< Error code. */
  55. char const * name; /**< Descriptive name (the same as the internal error mnemonic). */
  56. }nrf_strerror_desc_t;
  57. /**
  58. * @brief Unknown error code.
  59. *
  60. * The constant string used by @ref nrf_strerror_get when the error description was not found.
  61. */
  62. static char const m_unknown_str[] = "Unknown error code";
  63. /**
  64. * @brief Array with error codes.
  65. *
  66. * Array that describes error codes.
  67. *
  68. * @note It is required for this array to have error codes placed in ascending order.
  69. * This condition is checked in automatic unit test before the release.
  70. */
  71. static nrf_strerror_desc_t const nrf_strerror_array[] =
  72. {
  73. NRF_STRERROR_ENTITY(NRF_SUCCESS),
  74. NRF_STRERROR_ENTITY(NRF_ERROR_SVC_HANDLER_MISSING),
  75. NRF_STRERROR_ENTITY(NRF_ERROR_SOFTDEVICE_NOT_ENABLED),
  76. NRF_STRERROR_ENTITY(NRF_ERROR_INTERNAL),
  77. NRF_STRERROR_ENTITY(NRF_ERROR_NO_MEM),
  78. NRF_STRERROR_ENTITY(NRF_ERROR_NOT_FOUND),
  79. NRF_STRERROR_ENTITY(NRF_ERROR_NOT_SUPPORTED),
  80. NRF_STRERROR_ENTITY(NRF_ERROR_INVALID_PARAM),
  81. NRF_STRERROR_ENTITY(NRF_ERROR_INVALID_STATE),
  82. NRF_STRERROR_ENTITY(NRF_ERROR_INVALID_LENGTH),
  83. NRF_STRERROR_ENTITY(NRF_ERROR_INVALID_FLAGS),
  84. NRF_STRERROR_ENTITY(NRF_ERROR_INVALID_DATA),
  85. NRF_STRERROR_ENTITY(NRF_ERROR_DATA_SIZE),
  86. NRF_STRERROR_ENTITY(NRF_ERROR_TIMEOUT),
  87. NRF_STRERROR_ENTITY(NRF_ERROR_NULL),
  88. NRF_STRERROR_ENTITY(NRF_ERROR_FORBIDDEN),
  89. NRF_STRERROR_ENTITY(NRF_ERROR_INVALID_ADDR),
  90. NRF_STRERROR_ENTITY(NRF_ERROR_BUSY),
  91. #ifdef NRF_ERROR_CONN_COUNT
  92. NRF_STRERROR_ENTITY(NRF_ERROR_CONN_COUNT),
  93. #endif
  94. #ifdef NRF_ERROR_RESOURCES
  95. NRF_STRERROR_ENTITY(NRF_ERROR_RESOURCES),
  96. #endif
  97. /* SDK Common errors */
  98. NRF_STRERROR_ENTITY(NRF_ERROR_MODULE_NOT_INITIALIZED),
  99. NRF_STRERROR_ENTITY(NRF_ERROR_MUTEX_INIT_FAILED),
  100. NRF_STRERROR_ENTITY(NRF_ERROR_MUTEX_LOCK_FAILED),
  101. NRF_STRERROR_ENTITY(NRF_ERROR_MUTEX_UNLOCK_FAILED),
  102. NRF_STRERROR_ENTITY(NRF_ERROR_MUTEX_COND_INIT_FAILED),
  103. NRF_STRERROR_ENTITY(NRF_ERROR_MODULE_ALREADY_INITIALIZED),
  104. NRF_STRERROR_ENTITY(NRF_ERROR_STORAGE_FULL),
  105. NRF_STRERROR_ENTITY(NRF_ERROR_API_NOT_IMPLEMENTED),
  106. NRF_STRERROR_ENTITY(NRF_ERROR_FEATURE_NOT_ENABLED),
  107. NRF_STRERROR_ENTITY(NRF_ERROR_IO_PENDING),
  108. /* TWI error codes */
  109. NRF_STRERROR_ENTITY(NRF_ERROR_DRV_TWI_ERR_OVERRUN),
  110. NRF_STRERROR_ENTITY(NRF_ERROR_DRV_TWI_ERR_ANACK),
  111. NRF_STRERROR_ENTITY(NRF_ERROR_DRV_TWI_ERR_DNACK),
  112. /* IPSP error codes */
  113. NRF_STRERROR_ENTITY(NRF_ERROR_BLE_IPSP_RX_PKT_TRUNCATED),
  114. NRF_STRERROR_ENTITY(NRF_ERROR_BLE_IPSP_CHANNEL_ALREADY_EXISTS),
  115. NRF_STRERROR_ENTITY(NRF_ERROR_BLE_IPSP_LINK_DISCONNECTED),
  116. NRF_STRERROR_ENTITY(NRF_ERROR_BLE_IPSP_PEER_REJECTED)
  117. };
  118. char const * nrf_strerror_get(ret_code_t code)
  119. {
  120. char const * p_ret = nrf_strerror_find(code);
  121. return (p_ret == NULL) ? m_unknown_str : p_ret;
  122. }
  123. char const * nrf_strerror_find(ret_code_t code)
  124. {
  125. nrf_strerror_desc_t const * p_start;
  126. nrf_strerror_desc_t const * p_end;
  127. p_start = nrf_strerror_array;
  128. p_end = nrf_strerror_array + ARRAY_SIZE(nrf_strerror_array);
  129. while (p_start < p_end)
  130. {
  131. nrf_strerror_desc_t const * p_mid = p_start + ((p_end - p_start) / 2);
  132. ret_code_t mid_c = p_mid->code;
  133. if (mid_c > code)
  134. {
  135. p_end = p_mid;
  136. }
  137. else if (mid_c < code)
  138. {
  139. p_start = p_mid + 1;
  140. }
  141. else
  142. {
  143. return p_mid->name;
  144. }
  145. }
  146. return NULL;
  147. }
  148. #endif /* NRF_STRERROR enabled */