peer_id.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Copyright (c) 2015 - 2019, 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(PEER_MANAGER)
  42. #include "peer_id.h"
  43. #include <stdint.h>
  44. #include <string.h>
  45. #include "sdk_errors.h"
  46. #include "peer_manager_types.h"
  47. #include "nrf_atflags.h"
  48. typedef struct
  49. {
  50. NRF_ATFLAGS_DEF_MEMBER(used_peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); /**< Bitmap designating which peer IDs are in use. */
  51. NRF_ATFLAGS_DEF_MEMBER(deleted_peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); /**< Bitmap designating which peer IDs are marked for deletion. */
  52. } pi_t;
  53. static pi_t m_pi = {{0}, {0}};
  54. static void internal_state_reset(pi_t * p_pi)
  55. {
  56. memset(p_pi, 0, sizeof(pi_t));
  57. }
  58. void peer_id_init(void)
  59. {
  60. internal_state_reset(&m_pi);
  61. }
  62. static pm_peer_id_t claim(pm_peer_id_t peer_id, nrf_atflags_t * p_peer_id_flags)
  63. {
  64. pm_peer_id_t allocated_peer_id = PM_PEER_ID_INVALID;
  65. if (peer_id == PM_PEER_ID_INVALID)
  66. {
  67. allocated_peer_id = nrf_atflags_find_and_set_flag(p_peer_id_flags, PM_PEER_ID_N_AVAILABLE_IDS);
  68. if (allocated_peer_id == PM_PEER_ID_N_AVAILABLE_IDS)
  69. {
  70. allocated_peer_id = PM_PEER_ID_INVALID;
  71. }
  72. }
  73. else if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
  74. {
  75. bool lock_success = !nrf_atflags_fetch_set(p_peer_id_flags, peer_id);
  76. allocated_peer_id = lock_success ? peer_id : PM_PEER_ID_INVALID;
  77. }
  78. return allocated_peer_id;
  79. }
  80. static void release(pm_peer_id_t peer_id, nrf_atflags_t * p_peer_id_flags)
  81. {
  82. if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
  83. {
  84. nrf_atflags_clear(p_peer_id_flags, peer_id);
  85. }
  86. }
  87. pm_peer_id_t peer_id_allocate(pm_peer_id_t peer_id)
  88. {
  89. return claim(peer_id, m_pi.used_peer_ids);
  90. }
  91. bool peer_id_delete(pm_peer_id_t peer_id)
  92. {
  93. pm_peer_id_t deleted_peer_id;
  94. if (peer_id == PM_PEER_ID_INVALID)
  95. {
  96. return false;
  97. }
  98. deleted_peer_id = claim(peer_id, m_pi.deleted_peer_ids);
  99. return (deleted_peer_id == peer_id);
  100. }
  101. void peer_id_free(pm_peer_id_t peer_id)
  102. {
  103. release(peer_id, m_pi.used_peer_ids);
  104. release(peer_id, m_pi.deleted_peer_ids);
  105. }
  106. bool peer_id_is_allocated(pm_peer_id_t peer_id)
  107. {
  108. if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
  109. {
  110. return nrf_atflags_get(m_pi.used_peer_ids, peer_id);
  111. }
  112. return false;
  113. }
  114. bool peer_id_is_deleted(pm_peer_id_t peer_id)
  115. {
  116. if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
  117. {
  118. return nrf_atflags_get(m_pi.deleted_peer_ids, peer_id);
  119. }
  120. return false;
  121. }
  122. pm_peer_id_t next_id_get(pm_peer_id_t prev_peer_id, nrf_atflags_t * p_peer_id_flags)
  123. {
  124. pm_peer_id_t i = (prev_peer_id == PM_PEER_ID_INVALID) ? 0 : (prev_peer_id + 1);
  125. for (; i < PM_PEER_ID_N_AVAILABLE_IDS; i++)
  126. {
  127. if (nrf_atflags_get(p_peer_id_flags, i))
  128. {
  129. return i;
  130. }
  131. }
  132. return PM_PEER_ID_INVALID;
  133. }
  134. pm_peer_id_t peer_id_get_next_used(pm_peer_id_t peer_id)
  135. {
  136. peer_id = next_id_get(peer_id, m_pi.used_peer_ids);
  137. while (peer_id != PM_PEER_ID_INVALID)
  138. {
  139. if (!peer_id_is_deleted(peer_id))
  140. {
  141. return peer_id;
  142. }
  143. peer_id = next_id_get(peer_id, m_pi.used_peer_ids);
  144. }
  145. return peer_id;
  146. }
  147. pm_peer_id_t peer_id_get_next_deleted(pm_peer_id_t prev_peer_id)
  148. {
  149. return next_id_get(prev_peer_id, m_pi.deleted_peer_ids);
  150. }
  151. uint32_t peer_id_n_ids(void)
  152. {
  153. uint32_t n_ids = 0;
  154. for (pm_peer_id_t i = 0; i < PM_PEER_ID_N_AVAILABLE_IDS; i++)
  155. {
  156. n_ids += nrf_atflags_get(m_pi.used_peer_ids, i);
  157. }
  158. return n_ids;
  159. }
  160. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)