pm_buffer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "pm_buffer.h"
  43. #include <stdbool.h>
  44. #include <string.h>
  45. #include "nrf_error.h"
  46. #include "nrf_atflags.h"
  47. #define BUFFER_IS_VALID(p_buffer) ((p_buffer != NULL) \
  48. && (p_buffer->p_memory != NULL) \
  49. && (p_buffer->p_mutex != NULL))
  50. static bool mutex_lock(nrf_atflags_t * p_mutex, uint32_t mutex_id)
  51. {
  52. bool locked = !nrf_atflags_fetch_set(p_mutex, mutex_id);
  53. __DMB();
  54. return locked;
  55. }
  56. static void mutex_unlock(nrf_atflags_t * p_mutex, uint32_t mutex_id)
  57. {
  58. __DMB();
  59. nrf_atflags_clear(p_mutex, mutex_id);
  60. }
  61. static bool mutex_lock_status_get(nrf_atflags_t * p_mutex, uint32_t mutex_id)
  62. {
  63. __DMB();
  64. return nrf_atflags_get(p_mutex, mutex_id);
  65. }
  66. ret_code_t pm_buffer_init(pm_buffer_t * p_buffer,
  67. uint8_t * p_buffer_memory,
  68. uint32_t buffer_memory_size,
  69. nrf_atflags_t * p_mutex_memory,
  70. uint32_t n_blocks,
  71. uint32_t block_size)
  72. {
  73. if ( (p_buffer != NULL)
  74. && (p_buffer_memory != NULL)
  75. && (p_mutex_memory != NULL)
  76. && (buffer_memory_size >= (n_blocks * block_size))
  77. && (n_blocks != 0)
  78. && (block_size != 0))
  79. {
  80. p_buffer->p_memory = p_buffer_memory;
  81. p_buffer->p_mutex = p_mutex_memory;
  82. p_buffer->n_blocks = n_blocks;
  83. p_buffer->block_size = block_size;
  84. return NRF_SUCCESS;
  85. }
  86. else
  87. {
  88. return NRF_ERROR_INVALID_PARAM;
  89. }
  90. }
  91. uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks)
  92. {
  93. if (!BUFFER_IS_VALID(p_buffer))
  94. {
  95. return ( PM_BUFFER_INVALID_ID );
  96. }
  97. uint8_t first_locked_mutex = PM_BUFFER_INVALID_ID;
  98. for (uint8_t i = 0; i < p_buffer->n_blocks; i++)
  99. {
  100. if (mutex_lock(p_buffer->p_mutex, i))
  101. {
  102. if (first_locked_mutex == PM_BUFFER_INVALID_ID)
  103. {
  104. first_locked_mutex = i;
  105. }
  106. if ((i - first_locked_mutex + 1) == n_blocks)
  107. {
  108. return first_locked_mutex;
  109. }
  110. }
  111. else if (first_locked_mutex != PM_BUFFER_INVALID_ID)
  112. {
  113. for (uint8_t j = first_locked_mutex; j < i; j++)
  114. {
  115. pm_buffer_release(p_buffer, j);
  116. }
  117. first_locked_mutex = PM_BUFFER_INVALID_ID;
  118. }
  119. }
  120. return ( PM_BUFFER_INVALID_ID );
  121. }
  122. uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id)
  123. {
  124. if (!BUFFER_IS_VALID(p_buffer))
  125. {
  126. return ( NULL );
  127. }
  128. if ( (id != PM_BUFFER_INVALID_ID)
  129. && mutex_lock_status_get(p_buffer->p_mutex, id) )
  130. {
  131. return ( &p_buffer->p_memory[id * p_buffer->block_size] );
  132. }
  133. else
  134. {
  135. return ( NULL );
  136. }
  137. }
  138. void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id)
  139. {
  140. if ( BUFFER_IS_VALID(p_buffer)
  141. && (id != PM_BUFFER_INVALID_ID)
  142. && mutex_lock_status_get(p_buffer->p_mutex, id))
  143. {
  144. mutex_unlock(p_buffer->p_mutex, id);
  145. }
  146. }
  147. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)