sdk_mapped_flags.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_mapped_flags.h"
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <stddef.h>
  44. #include "compiler_abstraction.h"
  45. // Test whether the flag collection type is large enough to hold all the flags. If this fails,
  46. // reduce SDK_MAPPED_FLAGS_N_KEYS or increase the size of sdk_mapped_flags_t.
  47. STATIC_ASSERT((sizeof(sdk_mapped_flags_t) * SDK_MAPPED_FLAGS_N_KEYS_PER_BYTE) >= SDK_MAPPED_FLAGS_N_KEYS);
  48. /**@brief Function for setting the state of a flag to true.
  49. *
  50. * @note This function does not check whether the index is valid.
  51. *
  52. * @param[in] p_flags The collection of flags to modify.
  53. * @param[in] index The index of the flag to modify.
  54. */
  55. static __INLINE void sdk_mapped_flags_set_by_index(sdk_mapped_flags_t * p_flags, uint16_t index)
  56. {
  57. *p_flags |= (1U << index);
  58. }
  59. /**@brief Function for setting the state of a flag to false.
  60. *
  61. * @note This function does not check whether the index is valid.
  62. *
  63. * @param[in] p_flags The collection of flags to modify.
  64. * @param[in] index The index of the flag to modify.
  65. */
  66. static __INLINE void sdk_mapped_flags_clear_by_index(sdk_mapped_flags_t * p_flags, uint16_t index)
  67. {
  68. *p_flags &= ~(1U << index);
  69. }
  70. /**@brief Function for getting the state of a flag.
  71. *
  72. * @note This function does not check whether the index is valid.
  73. *
  74. * @param[in] p_flags The collection of flags to read.
  75. * @param[in] index The index of the flag to get.
  76. */
  77. static __INLINE bool sdk_mapped_flags_get_by_index(sdk_mapped_flags_t flags, uint16_t index)
  78. {
  79. return ((flags & (1 << index)) != 0);
  80. }
  81. uint16_t sdk_mapped_flags_first_key_index_get(sdk_mapped_flags_t flags)
  82. {
  83. for (uint16_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  84. {
  85. if (sdk_mapped_flags_get_by_index(flags, i))
  86. {
  87. return i;
  88. }
  89. }
  90. return SDK_MAPPED_FLAGS_INVALID_INDEX;
  91. }
  92. void sdk_mapped_flags_update_by_key(uint16_t * p_keys,
  93. sdk_mapped_flags_t * p_flags,
  94. uint16_t key,
  95. bool value)
  96. {
  97. sdk_mapped_flags_bulk_update_by_key(p_keys, p_flags, 1, key, value);
  98. }
  99. void sdk_mapped_flags_bulk_update_by_key(uint16_t * p_keys,
  100. sdk_mapped_flags_t * p_flags,
  101. uint32_t n_flag_collections,
  102. uint16_t key,
  103. bool value)
  104. {
  105. if ((p_keys != NULL) && (p_flags != NULL) && (n_flag_collections > 0))
  106. {
  107. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  108. {
  109. if (p_keys[i] == key)
  110. {
  111. for (uint32_t j = 0; j < n_flag_collections; j++)
  112. {
  113. if (value)
  114. {
  115. sdk_mapped_flags_set_by_index(&p_flags[j], i);
  116. }
  117. else
  118. {
  119. sdk_mapped_flags_clear_by_index(&p_flags[j], i);
  120. }
  121. }
  122. return;
  123. }
  124. }
  125. }
  126. }
  127. bool sdk_mapped_flags_get_by_key_w_idx(uint16_t * p_keys,
  128. sdk_mapped_flags_t flags,
  129. uint16_t key,
  130. uint8_t * p_index)
  131. {
  132. if (p_keys != NULL)
  133. {
  134. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  135. {
  136. if (p_keys[i] == key)
  137. {
  138. if (p_index != NULL)
  139. {
  140. *p_index = i;
  141. }
  142. return sdk_mapped_flags_get_by_index(flags, i);
  143. }
  144. }
  145. }
  146. if (p_index != NULL)
  147. {
  148. *p_index = SDK_MAPPED_FLAGS_N_KEYS;
  149. }
  150. return false;
  151. }
  152. bool sdk_mapped_flags_get_by_key(uint16_t * p_keys, sdk_mapped_flags_t flags, uint16_t key)
  153. {
  154. if (p_keys != NULL)
  155. {
  156. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  157. {
  158. if (p_keys[i] == key)
  159. {
  160. return sdk_mapped_flags_get_by_index(flags, i);
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. sdk_mapped_flags_key_list_t sdk_mapped_flags_key_list_get(uint16_t * p_keys,
  167. sdk_mapped_flags_t flags)
  168. {
  169. sdk_mapped_flags_key_list_t key_list;
  170. key_list.len = 0;
  171. if (p_keys != NULL)
  172. {
  173. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  174. {
  175. if (sdk_mapped_flags_get_by_index(flags, i))
  176. {
  177. key_list.flag_keys[key_list.len++] = p_keys[i];
  178. }
  179. }
  180. }
  181. return key_list;
  182. }
  183. uint32_t sdk_mapped_flags_n_flags_set(sdk_mapped_flags_t flags)
  184. {
  185. uint32_t n_flags_set = 0;
  186. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  187. {
  188. if (sdk_mapped_flags_get_by_index(flags, i))
  189. {
  190. n_flags_set += 1;
  191. }
  192. }
  193. return n_flags_set;
  194. }