sdk_mapped_flags.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "sdk_mapped_flags.h"
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <stddef.h>
  16. #include "compiler_abstraction.h"
  17. /**@brief Function for setting the state of a flag to true.
  18. *
  19. * @note This function does not check whether the index is valid.
  20. *
  21. * @param[in] p_flags The collection of flags to modify.
  22. * @param[in] index The index of the flag to modify.
  23. */
  24. static __INLINE void sdk_mapped_flags_set_by_index(sdk_mapped_flags_t * p_flags, uint16_t index)
  25. {
  26. *p_flags |= (1U << index);
  27. }
  28. /**@brief Function for setting the state of a flag to false.
  29. *
  30. * @note This function does not check whether the index is valid.
  31. *
  32. * @param[in] p_flags The collection of flags to modify.
  33. * @param[in] index The index of the flag to modify.
  34. */
  35. static __INLINE void sdk_mapped_flags_clear_by_index(sdk_mapped_flags_t * p_flags, uint16_t index)
  36. {
  37. *p_flags &= ~(1U << index);
  38. }
  39. /**@brief Function for getting the state of a flag.
  40. *
  41. * @note This function does not check whether the index is valid.
  42. *
  43. * @param[in] p_flags The collection of flags to read.
  44. * @param[in] index The index of the flag to get.
  45. */
  46. static __INLINE bool sdk_mapped_flags_get_by_index(sdk_mapped_flags_t flags, uint16_t index)
  47. {
  48. return ((flags & (1 << index)) != 0);
  49. }
  50. uint16_t sdk_mapped_flags_first_key_index_get(sdk_mapped_flags_t flags)
  51. {
  52. for (uint16_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  53. {
  54. if (sdk_mapped_flags_get_by_index(flags, i))
  55. {
  56. return i;
  57. }
  58. }
  59. return SDK_MAPPED_FLAGS_INVALID_INDEX;
  60. }
  61. void sdk_mapped_flags_update_by_key(uint16_t * p_keys,
  62. sdk_mapped_flags_t * p_flags,
  63. uint16_t key,
  64. bool value)
  65. {
  66. sdk_mapped_flags_bulk_update_by_key(p_keys, p_flags, 1, key, value);
  67. }
  68. void sdk_mapped_flags_bulk_update_by_key(uint16_t * p_keys,
  69. sdk_mapped_flags_t * p_flags,
  70. uint32_t n_flag_collections,
  71. uint16_t key,
  72. bool value)
  73. {
  74. if ((p_keys != NULL) && (p_flags != NULL) && (n_flag_collections > 0))
  75. {
  76. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  77. {
  78. if (p_keys[i] == key)
  79. {
  80. for (uint32_t j = 0; j < n_flag_collections; j++)
  81. {
  82. if (value)
  83. {
  84. sdk_mapped_flags_set_by_index(&p_flags[j], i);
  85. }
  86. else
  87. {
  88. sdk_mapped_flags_clear_by_index(&p_flags[j], i);
  89. }
  90. }
  91. return;
  92. }
  93. }
  94. }
  95. }
  96. bool sdk_mapped_flags_get_by_key(uint16_t * p_keys, sdk_mapped_flags_t flags, uint16_t key)
  97. {
  98. if (p_keys != NULL)
  99. {
  100. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  101. {
  102. if (p_keys[i] == key)
  103. {
  104. return sdk_mapped_flags_get_by_index(flags, i);
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. sdk_mapped_flags_key_list_t sdk_mapped_flags_key_list_get(uint16_t * p_keys,
  111. sdk_mapped_flags_t flags)
  112. {
  113. sdk_mapped_flags_key_list_t key_list;
  114. key_list.len = 0;
  115. if (p_keys != NULL)
  116. {
  117. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  118. {
  119. if (sdk_mapped_flags_get_by_index(flags, i))
  120. {
  121. key_list.flag_keys[key_list.len++] = p_keys[i];
  122. }
  123. }
  124. }
  125. return key_list;
  126. }
  127. uint32_t sdk_mapped_flags_n_flags_set(sdk_mapped_flags_t flags)
  128. {
  129. uint32_t n_flags_set = 0;
  130. for (uint32_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
  131. {
  132. if (sdk_mapped_flags_get_by_index(flags, i))
  133. {
  134. n_flags_set += 1;
  135. }
  136. }
  137. return n_flags_set;
  138. }