iot_timer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright (c) 2015 - 2018, 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 <string.h>
  41. #include "iot_timer.h"
  42. #include "sdk_common.h"
  43. #include "sdk_config.h"
  44. #include "iot_errors.h"
  45. /**
  46. * @defgroup api_param_check API Parameters check macros.
  47. *
  48. * @details Macros that verify parameters passed to the module in the APIs. These macros
  49. * could be mapped to nothing in final versions of code to save execution and size.
  50. * IOT_TIMER_DISABLE_API_PARAM_CHECK should be defined to disable these checks.
  51. *
  52. * @{
  53. */
  54. #if (IOT_TIMER_DISABLE_API_PARAM_CHECK == 0)
  55. /**
  56. * @brief Verify NULL parameters are not passed to API by application.
  57. */
  58. #define NULL_PARAM_CHECK(PARAM) \
  59. if ((PARAM) == NULL) \
  60. { \
  61. return (NRF_ERROR_NULL | IOT_TIMER_ERR_BASE); \
  62. }
  63. #define VERIFY_CLIENT_LIST_IS_VALID(PARAM) \
  64. if ((PARAM) != NULL) \
  65. { \
  66. uint8_t i; \
  67. for (i = 0; i < (PARAM)->client_list_length; i++) \
  68. { \
  69. if (((PARAM)->p_client_list[i].iot_timer_callback) == NULL) \
  70. { \
  71. return (NRF_ERROR_INVALID_PARAM | IOT_TIMER_ERR_BASE); \
  72. } \
  73. if (((PARAM)->p_client_list[i].cb_interval == 0) || \
  74. ((PARAM)->p_client_list[i].cb_interval < IOT_TIMER_RESOLUTION_IN_MS) || \
  75. (((PARAM)->p_client_list[i].cb_interval % IOT_TIMER_RESOLUTION_IN_MS) != 0)) \
  76. { \
  77. return (NRF_ERROR_INVALID_PARAM | IOT_TIMER_ERR_BASE); \
  78. } \
  79. } \
  80. }
  81. #define VERIFY_WALL_CLOCK_VALUE_IS_VALID(PARAM) \
  82. if ((PARAM) != NULL) \
  83. { \
  84. if ((*PARAM % IOT_TIMER_RESOLUTION_IN_MS) != 0) \
  85. { \
  86. return (NRF_ERROR_INVALID_PARAM | IOT_TIMER_ERR_BASE); \
  87. } \
  88. }
  89. /**
  90. * @brief Verify NULL parameters are not passed to API by application.
  91. */
  92. #define NULL_PARAM_CHECK(PARAM) \
  93. if ((PARAM) == NULL) \
  94. { \
  95. return (NRF_ERROR_NULL | IOT_TIMER_ERR_BASE); \
  96. }
  97. #else // IOT_TIMER_DISABLE_API_PARAM_CHECK
  98. #define NULL_PARAM_CHECK(PARAM)
  99. #define VERIFY_CLIENT_LIST_IS_VALID(PARAM)
  100. #define VERIFY_WALL_CLOCK_VALUE_IS_VALID(PARAM)
  101. #endif //IOT_TIMER_DISABLE_API_PARAM_CHECK
  102. /** @} */
  103. static iot_timer_time_in_ms_t m_wall_clock = 0;
  104. static const iot_timer_clients_list_t * m_clients = NULL;
  105. uint32_t iot_timer_client_list_set(const iot_timer_clients_list_t * p_list_of_clients)
  106. {
  107. VERIFY_CLIENT_LIST_IS_VALID(p_list_of_clients);
  108. m_clients = p_list_of_clients;
  109. return NRF_SUCCESS;
  110. }
  111. uint32_t iot_timer_update(void)
  112. {
  113. m_wall_clock += IOT_TIMER_RESOLUTION_IN_MS;
  114. if ((0xFFFFFFFFUL - m_wall_clock) < IOT_TIMER_RESOLUTION_IN_MS)
  115. {
  116. m_wall_clock = IOT_TIMER_RESOLUTION_IN_MS;
  117. }
  118. if (m_clients != NULL)
  119. {
  120. uint8_t index;
  121. for (index = 0; index < m_clients->client_list_length; index++)
  122. {
  123. if ((m_wall_clock % m_clients->p_client_list[index].cb_interval) == 0)
  124. {
  125. m_clients->p_client_list[index].iot_timer_callback(m_wall_clock);
  126. }
  127. }
  128. }
  129. return NRF_SUCCESS;
  130. }
  131. uint32_t iot_timer_wall_clock_get(iot_timer_time_in_ms_t * p_elapsed_time)
  132. {
  133. NULL_PARAM_CHECK(p_elapsed_time);
  134. *p_elapsed_time = m_wall_clock;
  135. return NRF_SUCCESS;
  136. }
  137. uint32_t iot_timer_wall_clock_delta_get(iot_timer_time_in_ms_t * p_past_time, \
  138. iot_timer_time_in_ms_t * p_delta_time)
  139. {
  140. NULL_PARAM_CHECK(p_past_time);
  141. NULL_PARAM_CHECK(p_delta_time);
  142. VERIFY_WALL_CLOCK_VALUE_IS_VALID(p_past_time);
  143. if (*p_past_time == m_wall_clock)
  144. {
  145. *p_delta_time = 0;
  146. }
  147. else if (*p_past_time < m_wall_clock)
  148. {
  149. *p_delta_time = m_wall_clock - *p_past_time;
  150. }
  151. else
  152. {
  153. // An integer overflow of the wall clock occured since *p_past_time.
  154. iot_timer_time_in_ms_t max_wall_clock = (0xFFFFFFFFUL / IOT_TIMER_RESOLUTION_IN_MS) \
  155. * IOT_TIMER_RESOLUTION_IN_MS;
  156. *p_delta_time = max_wall_clock - *p_past_time; // Before overflow.
  157. *p_delta_time += m_wall_clock; // After overflow.
  158. *p_delta_time -= IOT_TIMER_RESOLUTION_IN_MS; // Because of handling of wall clock integer overflow, see above.
  159. }
  160. return NRF_SUCCESS;
  161. }