sdk_mapped_flags.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef SDK_MAPPED_FLAGS_H__
  41. #define SDK_MAPPED_FLAGS_H__
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include "app_util.h"
  45. #include "compiler_abstraction.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @file
  51. * @defgroup sdk_mapped_flags Mapped flags
  52. * @ingroup app_common
  53. * @{
  54. * @brief Module for writing and reading flags that are associated
  55. * with keys.
  56. *
  57. * @details The flags are represented as bits in a bitmap called a <i>flag collection</i>. The keys
  58. * are uint16_t. Each flag collection contains all flags of the same type, one flag for
  59. * each key.
  60. *
  61. * The mapped flags module does not keep the flag states, nor the list of keys. These are
  62. * provided in the API calls. A key's index in the key list determines which bit in the
  63. * flag collection is associated with it. This module does not ever edit the key list, and
  64. * does not edit flags except in function calls that take the flag collection as a pointer.
  65. *
  66. */
  67. #define SDK_MAPPED_FLAGS_N_KEYS 32 /**< The number of keys to keep flags for. This is also the number of flags in a flag collection. If changing this value, you might also need change the width of the sdk_mapped_flags_t type. */
  68. #define SDK_MAPPED_FLAGS_N_KEYS_PER_BYTE 8 /**< The number of flags that fit in one byte. */
  69. #define SDK_MAPPED_FLAGS_INVALID_INDEX 0xFFFF /**< A flag index guaranteed to be invalid. */
  70. typedef uint32_t sdk_mapped_flags_t; /**< The bitmap to hold flags. Each flag is one bit, and each bit represents the flag state associated with one key. */
  71. /**@brief Type used to present a subset of the registered keys.
  72. */
  73. typedef struct
  74. {
  75. uint32_t len; /**< The length of the list. */
  76. uint16_t flag_keys[SDK_MAPPED_FLAGS_N_KEYS]; /**< The list of keys. */
  77. } sdk_mapped_flags_key_list_t;
  78. /**@brief Function for getting the first index at which the flag is true in the provided
  79. * collection.
  80. *
  81. * @param[in] flags The flag collection to search for a flag set to true.
  82. *
  83. * @return The first index that has its flag set to true. If none were found, the
  84. * function returns @ref SDK_MAPPED_FLAGS_INVALID_INDEX.
  85. */
  86. uint16_t sdk_mapped_flags_first_key_index_get(sdk_mapped_flags_t flags);
  87. /**@brief Function for updating the state of a flag.
  88. *
  89. * @param[in] p_keys The list of associated keys (assumed to have a length of
  90. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  91. * @param[out] p_flags The flag collection to modify.
  92. * @param[in] key The key to modify the flag of.
  93. * @param[in] value The state to set the flag to.
  94. */
  95. void sdk_mapped_flags_update_by_key(uint16_t * p_keys,
  96. sdk_mapped_flags_t * p_flags,
  97. uint16_t key,
  98. bool value);
  99. /**@brief Function for updating the state of the same flag in multiple flag collections.
  100. *
  101. * @details The key and value are the same for all flag collections in the p_flags array.
  102. *
  103. * @param[in] p_keys The list of associated keys (assumed to have a length of
  104. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  105. * @param[out] p_flags The flag collections to modify.
  106. * @param[out] n_flag_collections The number of flag collections in p_flags.
  107. * @param[in] key The key to modify the flag of.
  108. * @param[in] value The state to set the flag to.
  109. */
  110. void sdk_mapped_flags_bulk_update_by_key(uint16_t * p_keys,
  111. sdk_mapped_flags_t * p_flags,
  112. uint32_t n_flag_collections,
  113. uint16_t key,
  114. bool value);
  115. /**@brief Function for getting the state of a specific flag.
  116. *
  117. * @param[in] p_keys The list of associated keys (assumed to have a length of
  118. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  119. * @param[in] flags The flag collection to read from.
  120. * @param[in] key The key to get the flag for.
  121. *
  122. * @return The state of the flag.
  123. */
  124. bool sdk_mapped_flags_get_by_key(uint16_t * p_keys, sdk_mapped_flags_t flags, uint16_t key);
  125. /**@brief Function for getting the state of a specific flag.
  126. *
  127. * @param[in] p_keys The list of associated keys (assumed to have a length of
  128. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  129. * @param[in] flags The flag collection from which to read.
  130. * @param[in] key The key for which to get the flag.
  131. * @param[out] p_index If not NULL, the index of the key.
  132. *
  133. * @return The state of the flag.
  134. */
  135. bool sdk_mapped_flags_get_by_key_w_idx(uint16_t * p_keys,
  136. sdk_mapped_flags_t flags,
  137. uint16_t key,
  138. uint8_t * p_index);
  139. /**@brief Function for getting a list of all keys that have a specific flag set to true.
  140. *
  141. * @param[in] p_keys The list of associated keys (assumed to have a length of
  142. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  143. * @param[in] flags The flag collection to search.
  144. *
  145. * @return The list of keys.
  146. */
  147. sdk_mapped_flags_key_list_t sdk_mapped_flags_key_list_get(uint16_t * p_keys,
  148. sdk_mapped_flags_t flags);
  149. /**@brief Function for getting the number of keys that have a specific flag set to true.
  150. *
  151. * @param[in] flags The flag collection to search.
  152. *
  153. * @return The number of keys.
  154. */
  155. uint32_t sdk_mapped_flags_n_flags_set(sdk_mapped_flags_t flags);
  156. /**@brief Function for querying whether any flags in the collection are set.
  157. *
  158. * @param[in] flags The flag collection to query.
  159. *
  160. * @retval true If one or more flags are set to true.
  161. * @retval false Otherwise.
  162. */
  163. static __INLINE bool sdk_mapped_flags_any_set(sdk_mapped_flags_t flags)
  164. {
  165. return (flags != 0);
  166. }
  167. /** @} */
  168. #ifdef __cplusplus
  169. }
  170. #endif
  171. #endif /* SDK_MAPPED_FLAGS_H__ */