mesh_timer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. void *_reserved;
  42. k_work_handler_t handler;
  43. int index;
  44. };
  45. #define _K_WORK_INITIALIZER(work_handler) \
  46. { \
  47. ._reserved = NULL, \
  48. .handler = work_handler, \
  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 infinite timeout delay.
  105. *
  106. * This macro generates a timeout delay that that instructs a kernel API
  107. * to wait as long as necessary to perform the requested operation.
  108. *
  109. * @return Timeout delay value.
  110. */
  111. #define K_FOREVER (-1)
  112. /**
  113. * @brief Get system uptime (32-bit version).
  114. *
  115. * This routine returns the lower 32-bits of the elapsed time since the system
  116. * booted, in milliseconds.
  117. *
  118. * This routine can be more efficient than k_uptime_get(), as it reduces the
  119. * need for interrupt locking and 64-bit math. However, the 32-bit result
  120. * cannot hold a system uptime time larger than approximately 50 days, so the
  121. * caller must handle possible rollovers.
  122. *
  123. * @return Current uptime.
  124. */
  125. uint32_t k_uptime_get_32(void);
  126. struct k_delayed_work {
  127. struct k_work work;
  128. };
  129. /**
  130. * @brief Submit a delayed work item to the system workqueue.
  131. *
  132. * This routine schedules work item @a work to be processed by the system
  133. * workqueue after a delay of @a delay milliseconds. The routine initiates
  134. * an asynchronous countdown for the work item and then returns to the caller.
  135. * Only when the countdown completes is the work item actually submitted to
  136. * the workqueue and becomes pending.
  137. *
  138. * Submitting a previously submitted delayed work item that is still
  139. * counting down cancels the existing submission and restarts the countdown
  140. * using the new delay. If the work item is currently pending on the
  141. * workqueue's queue because the countdown has completed it is too late to
  142. * resubmit the item, and resubmission fails without impacting the work item.
  143. * If the work item has already been processed, or is currently being processed,
  144. * its work is considered complete and the work item can be resubmitted.
  145. *
  146. * @warning
  147. * Work items submitted to the system workqueue should avoid using handlers
  148. * that block or yield since this may prevent the system workqueue from
  149. * processing other work items in a timely manner.
  150. *
  151. * @note Can be called by ISRs.
  152. *
  153. * @param work Address of delayed work item.
  154. * @param delay Delay before submitting the work item (in milliseconds).
  155. *
  156. * @retval 0 Work item countdown started.
  157. * @retval -EINPROGRESS Work item is already pending.
  158. * @retval -EINVAL Work item is being processed or has completed its work.
  159. * @retval -EADDRINUSE Work item is pending on a different workqueue.
  160. */
  161. int k_delayed_work_submit(struct k_delayed_work *work, int32_t delay);
  162. int k_delayed_work_submit_periodic(struct k_delayed_work *work, int32_t period);
  163. /**
  164. * @brief Get time remaining before a delayed work gets scheduled.
  165. *
  166. * This routine computes the (approximate) time remaining before a
  167. * delayed work gets executed. If the delayed work is not waiting to be
  168. * scheduled, it returns zero.
  169. *
  170. * @param work Delayed work item.
  171. *
  172. * @return Remaining time (in milliseconds).
  173. */
  174. int32_t k_delayed_work_remaining_get(struct k_delayed_work *work);
  175. /**
  176. * @brief Submit a work item to the system workqueue.
  177. *
  178. * This routine submits work item @a work to be processed by the system
  179. * workqueue. If the work item is already pending in the workqueue's queue
  180. * as a result of an earlier submission, this routine has no effect on the
  181. * work item. If the work item has already been processed, or is currently
  182. * being processed, its work is considered complete and the work item can be
  183. * resubmitted.
  184. *
  185. * @warning
  186. * Work items submitted to the system workqueue should avoid using handlers
  187. * that block or yield since this may prevent the system workqueue from
  188. * processing other work items in a timely manner.
  189. *
  190. * @note Can be called by ISRs.
  191. *
  192. * @param work Address of work item.
  193. *
  194. * @return N/A
  195. */
  196. static inline void k_work_submit(struct k_work *work)
  197. {
  198. if (work && work->handler) {
  199. work->handler(work);
  200. }
  201. }
  202. /**
  203. * @brief Initialize a work item.
  204. *
  205. * This routine initializes a workqueue work item, prior to its first use.
  206. *
  207. * @param work Address of work item.
  208. * @param handler Function to invoke each time work item is processed.
  209. *
  210. * @return N/A
  211. */
  212. static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
  213. {
  214. work->handler = handler;
  215. }
  216. int k_delayed_work_cancel(struct k_delayed_work *work);
  217. int k_delayed_work_free(struct k_delayed_work *work);
  218. int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler);
  219. /**
  220. * @brief Get system uptime.
  221. *
  222. * This routine returns the elapsed time since the system booted,
  223. * in milliseconds.
  224. *
  225. * @return Current uptime.
  226. */
  227. int64_t k_uptime_get(void);
  228. void bt_mesh_timer_init(void);
  229. void bt_mesh_timer_deinit(void);
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* _BLE_MESH_TIMER_H_ */