nimble_npl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #ifndef _NIMBLE_NPL_H_
  20. #define _NIMBLE_NPL_H_
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. struct ble_npl_event;
  28. typedef void ble_npl_event_fn(struct ble_npl_event *ev);
  29. enum ble_npl_error {
  30. BLE_NPL_OK = 0,
  31. BLE_NPL_ENOMEM = 1,
  32. BLE_NPL_EINVAL = 2,
  33. BLE_NPL_INVALID_PARAM = 3,
  34. BLE_NPL_MEM_NOT_ALIGNED = 4,
  35. BLE_NPL_BAD_MUTEX = 5,
  36. BLE_NPL_TIMEOUT = 6,
  37. BLE_NPL_ERR_IN_ISR = 7,
  38. BLE_NPL_ERR_PRIV = 8,
  39. BLE_NPL_OS_NOT_STARTED = 9,
  40. BLE_NPL_ENOENT = 10,
  41. BLE_NPL_EBUSY = 11,
  42. BLE_NPL_ERROR = 12,
  43. };
  44. typedef enum ble_npl_error ble_npl_error_t;
  45. /* Include OS-specific definitions */
  46. #include "nimble/nimble_npl_os.h"
  47. /*
  48. * Generic
  49. */
  50. bool ble_npl_os_started(void);
  51. void *ble_npl_get_current_task_id(void);
  52. /*
  53. * Event queue
  54. */
  55. void ble_npl_eventq_init(struct ble_npl_eventq *evq);
  56. void ble_npl_eventq_deinit(struct ble_npl_eventq *evq);
  57. struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq,
  58. ble_npl_time_t tmo);
  59. void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev);
  60. void ble_npl_eventq_remove(struct ble_npl_eventq *evq,
  61. struct ble_npl_event *ev);
  62. void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
  63. void *arg);
  64. bool ble_npl_event_is_queued(struct ble_npl_event *ev);
  65. void *ble_npl_event_get_arg(struct ble_npl_event *ev);
  66. void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg);
  67. bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq);
  68. void ble_npl_event_run(struct ble_npl_event *ev);
  69. /*
  70. * Mutexes
  71. */
  72. ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu);
  73. ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu,
  74. ble_npl_time_t timeout);
  75. ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu);
  76. ble_npl_error_t ble_npl_mutex_deinit(struct ble_npl_mutex *mu);
  77. /*
  78. * Semaphores
  79. */
  80. ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens);
  81. ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem,
  82. ble_npl_time_t timeout);
  83. ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem);
  84. ble_npl_error_t ble_npl_sem_deinit(struct ble_npl_sem *sem);
  85. uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem);
  86. /*
  87. * Callouts
  88. */
  89. int ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
  90. ble_npl_event_fn *ev_cb, void *ev_arg);
  91. ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *co,
  92. ble_npl_time_t ticks);
  93. void ble_npl_callout_stop(struct ble_npl_callout *co);
  94. bool ble_npl_callout_is_active(struct ble_npl_callout *co);
  95. ble_npl_time_t ble_npl_callout_get_ticks(struct ble_npl_callout *co);
  96. ble_npl_time_t ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
  97. ble_npl_time_t time);
  98. void ble_npl_callout_set_arg(struct ble_npl_callout *co,
  99. void *arg);
  100. /*
  101. * Time functions
  102. */
  103. ble_npl_time_t ble_npl_time_get(void);
  104. ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks);
  105. ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms);
  106. ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms);
  107. uint32_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks);
  108. void ble_npl_time_delay(ble_npl_time_t ticks);
  109. /*
  110. * Hardware-specific
  111. *
  112. * These symbols should be most likely defined by application since they are
  113. * specific to hardware, not to OS.
  114. */
  115. #if NIMBLE_CFG_CONTROLLER
  116. void ble_npl_hw_set_isr(int irqn, uint32_t addr);
  117. #endif
  118. uint32_t ble_npl_hw_enter_critical(void);
  119. void ble_npl_hw_exit_critical(uint32_t ctx);
  120. bool ble_npl_hw_is_in_critical(void);
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* _NIMBLE_NPL_H_ */