timer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * SPDX-FileCopyrightText: 2016 Wind River Systems, Inc.
  3. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #ifndef _BLE_MESH_TIMER_H_
  8. #define _BLE_MESH_TIMER_H_
  9. #include "mesh/types.h"
  10. #include "mesh/slist.h"
  11. #include "mesh/atomic.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* number of nsec per usec */
  16. #define NSEC_PER_USEC 1000
  17. /* number of microseconds per millisecond */
  18. #define USEC_PER_MSEC 1000
  19. /* number of milliseconds per second */
  20. #define MSEC_PER_SEC 1000
  21. /* number of microseconds per second */
  22. #define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
  23. /* number of nanoseconds per second */
  24. #define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
  25. /* timeout is not in use */
  26. #define _INACTIVE (-1)
  27. struct k_work;
  28. /**
  29. * @typedef k_work_handler_t
  30. * @brief Work item handler function type.
  31. *
  32. * A work item's handler function is executed by a workqueue's thread
  33. * when the work item is processed by the workqueue.
  34. *
  35. * @param work Address of the work item.
  36. *
  37. * @return N/A
  38. */
  39. typedef void (*k_work_handler_t)(struct k_work *work);
  40. struct k_work {
  41. k_work_handler_t handler;
  42. int index;
  43. void *user_data;
  44. };
  45. #define _K_WORK_INITIALIZER(work_handler) \
  46. { \
  47. .handler = work_handler, \
  48. .user_data = NULL, \
  49. }
  50. /**
  51. * @brief Generate null timeout delay.
  52. *
  53. * This macro generates a timeout delay that that instructs a kernel API
  54. * not to wait if the requested operation cannot be performed immediately.
  55. *
  56. * @return Timeout delay value.
  57. */
  58. #define K_NO_WAIT 0
  59. /**
  60. * @brief Generate timeout delay from milliseconds.
  61. *
  62. * This macro generates a timeout delay that that instructs a kernel API
  63. * to wait up to @a ms milliseconds to perform the requested operation.
  64. *
  65. * @param ms Duration in milliseconds.
  66. *
  67. * @return Timeout delay value.
  68. */
  69. #define K_MSEC(ms) (ms)
  70. /**
  71. * @brief Generate timeout delay from seconds.
  72. *
  73. * This macro generates a timeout delay that that instructs a kernel API
  74. * to wait up to @a s seconds to perform the requested operation.
  75. *
  76. * @param s Duration in seconds.
  77. *
  78. * @return Timeout delay value.
  79. */
  80. #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
  81. /**
  82. * @brief Generate timeout delay from minutes.
  83. *
  84. * This macro generates a timeout delay that that instructs a kernel API
  85. * to wait up to @a m minutes to perform the requested operation.
  86. *
  87. * @param m Duration in minutes.
  88. *
  89. * @return Timeout delay value.
  90. */
  91. #define K_MINUTES(m) K_SECONDS((m) * 60)
  92. /**
  93. * @brief Generate timeout delay from hours.
  94. *
  95. * This macro generates a timeout delay that that instructs a kernel API
  96. * to wait up to @a h hours to perform the requested operation.
  97. *
  98. * @param h Duration in hours.
  99. *
  100. * @return Timeout delay value.
  101. */
  102. #define K_HOURS(h) K_MINUTES((h) * 60)
  103. /**
  104. * @brief Generate timeout delay from days.
  105. *
  106. * This macro generates a timeout delay that that instructs a kernel API
  107. * to wait up to @a d days to perform the requested operation.
  108. *
  109. * @param d Duration in days.
  110. *
  111. * @return Timeout delay value.
  112. */
  113. #define K_DAYS(d) K_HOURS((d) * 24)
  114. /**
  115. * @brief Generate infinite timeout delay.
  116. *
  117. * This macro generates a timeout delay that that instructs a kernel API
  118. * to wait as long as necessary to perform the requested operation.
  119. *
  120. * @return Timeout delay value.
  121. */
  122. #define K_FOREVER (-1)
  123. /**
  124. * @brief Get system uptime (32-bit version).
  125. *
  126. * This routine returns the lower 32-bits of the elapsed time since the system
  127. * booted, in milliseconds.
  128. *
  129. * This routine can be more efficient than k_uptime_get(), as it reduces the
  130. * need for interrupt locking and 64-bit math. However, the 32-bit result
  131. * cannot hold a system uptime time larger than approximately 50 days, so the
  132. * caller must handle possible rollovers.
  133. *
  134. * @return Current uptime.
  135. */
  136. uint32_t k_uptime_get_32(void);
  137. struct k_delayed_work {
  138. struct k_work work;
  139. };
  140. /**
  141. * @brief Submit a delayed work item to the system workqueue.
  142. *
  143. * This routine schedules work item @a work to be processed by the system
  144. * workqueue after a delay of @a delay milliseconds. The routine initiates
  145. * an asynchronous countdown for the work item and then returns to the caller.
  146. * Only when the countdown completes is the work item actually submitted to
  147. * the workqueue and becomes pending.
  148. *
  149. * Submitting a previously submitted delayed work item that is still
  150. * counting down cancels the existing submission and restarts the countdown
  151. * using the new delay. If the work item is currently pending on the
  152. * workqueue's queue because the countdown has completed it is too late to
  153. * resubmit the item, and resubmission fails without impacting the work item.
  154. * If the work item has already been processed, or is currently being processed,
  155. * its work is considered complete and the work item can be resubmitted.
  156. *
  157. * @warning
  158. * Work items submitted to the system workqueue should avoid using handlers
  159. * that block or yield since this may prevent the system workqueue from
  160. * processing other work items in a timely manner.
  161. *
  162. * @note Can be called by ISRs.
  163. *
  164. * @param work Address of delayed work item.
  165. * @param delay Delay before submitting the work item (in milliseconds).
  166. *
  167. * @retval 0 Work item countdown started.
  168. * @retval -EINPROGRESS Work item is already pending.
  169. * @retval -EINVAL Work item is being processed or has completed its work.
  170. * @retval -EADDRINUSE Work item is pending on a different workqueue.
  171. */
  172. int k_delayed_work_submit(struct k_delayed_work *work, int32_t delay);
  173. int k_delayed_work_submit_periodic(struct k_delayed_work *work, int32_t period);
  174. /**
  175. * @brief Get time remaining before a delayed work gets scheduled.
  176. *
  177. * This routine computes the (approximate) time remaining before a
  178. * delayed work gets executed. If the delayed work is not waiting to be
  179. * scheduled, it returns zero.
  180. *
  181. * @param work Delayed work item.
  182. *
  183. * @return Remaining time (in milliseconds).
  184. */
  185. int32_t k_delayed_work_remaining_get(struct k_delayed_work *work);
  186. /**
  187. * @brief Submit a work item to the system workqueue.
  188. *
  189. * This routine submits work item @a work to be processed by the system
  190. * workqueue. If the work item is already pending in the workqueue's queue
  191. * as a result of an earlier submission, this routine has no effect on the
  192. * work item. If the work item has already been processed, or is currently
  193. * being processed, its work is considered complete and the work item can be
  194. * resubmitted.
  195. *
  196. * @warning
  197. * Work items submitted to the system workqueue should avoid using handlers
  198. * that block or yield since this may prevent the system workqueue from
  199. * processing other work items in a timely manner.
  200. *
  201. * @note Can be called by ISRs.
  202. *
  203. * @param work Address of work item.
  204. *
  205. * @return N/A
  206. */
  207. static inline void k_work_submit(struct k_work *work)
  208. {
  209. if (work && work->handler) {
  210. work->handler(work);
  211. }
  212. }
  213. /**
  214. * @brief Initialize a work item.
  215. *
  216. * This routine initializes a workqueue work item, prior to its first use.
  217. *
  218. * @param work Address of work item.
  219. * @param handler Function to invoke each time work item is processed.
  220. *
  221. * @return N/A
  222. */
  223. static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
  224. {
  225. work->handler = handler;
  226. }
  227. int k_delayed_work_cancel(struct k_delayed_work *work);
  228. int k_delayed_work_free(struct k_delayed_work *work);
  229. int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler);
  230. /**
  231. * @brief Get system uptime.
  232. *
  233. * This routine returns the elapsed time since the system booted,
  234. * in milliseconds.
  235. *
  236. * @return Current uptime.
  237. */
  238. int64_t k_uptime_get(void);
  239. void bt_mesh_timer_init(void);
  240. void bt_mesh_timer_deinit(void);
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif /* _BLE_MESH_TIMER_H_ */