sdk_mapped_flags.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef SDK_MAPPED_FLAGS_H__
  13. #define SDK_MAPPED_FLAGS_H__
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "app_util.h"
  17. #include "compiler_abstraction.h"
  18. /**
  19. * @file
  20. * @defgroup sdk_mapped_flags Mapped flags
  21. * @ingroup app_common
  22. * @{
  23. * @brief Module for writing and reading flags that are associated
  24. * with keys.
  25. *
  26. * @details The flags are represented as bits in a bitmap called a <i>flag collection</i>. The keys
  27. * are uint16_t. Each flag collection contains all flags of the same type, one flag for
  28. * each key.
  29. *
  30. * The mapped flags module does not keep the flag states, nor the list of keys. These are
  31. * provided in the API calls. A key's index in the key list determines which bit in the
  32. * flag collection is associated with it. This module does not ever edit the key list, and
  33. * does not edit flags except in function calls that take the flag collection as a pointer.
  34. *
  35. */
  36. #define SDK_MAPPED_FLAGS_N_KEYS 8 /**< 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. */
  37. #define SDK_MAPPED_FLAGS_N_KEYS_PER_BYTE 8 /**< The number of flags that fit in one byte. */
  38. #define SDK_MAPPED_FLAGS_INVALID_INDEX 0xFFFF /**< A flag index guaranteed to be invalid. */
  39. typedef uint8_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. */
  40. // Test whether the flag collection type is large enough to hold all the flags. If this fails,
  41. // reduce SDK_MAPPED_FLAGS_N_KEYS or increase the size of sdk_mapped_flags_t.
  42. STATIC_ASSERT((
  43. sizeof(sdk_mapped_flags_t)*SDK_MAPPED_FLAGS_N_KEYS_PER_BYTE) >= SDK_MAPPED_FLAGS_N_KEYS);
  44. /**@brief Type used to present a subset of the registered keys.
  45. */
  46. typedef struct
  47. {
  48. uint32_t len; /**< The length of the list. */
  49. uint16_t flag_keys[SDK_MAPPED_FLAGS_N_KEYS]; /**< The list of keys. */
  50. } sdk_mapped_flags_key_list_t;
  51. /**@brief Function for getting the first index at which the flag is true in the provided
  52. * collection.
  53. *
  54. * @param[in] flags The flag collection to search for a flag set to true.
  55. *
  56. * @return The first index that has its flag set to true. If none were found, the
  57. * function returns @ref SDK_MAPPED_FLAGS_INVALID_INDEX.
  58. */
  59. uint16_t sdk_mapped_flags_first_key_index_get(sdk_mapped_flags_t flags);
  60. /**@brief Function for updating the state of a flag.
  61. *
  62. * @param[in] p_keys The list of associated keys (assumed to have a length of
  63. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  64. * @param[out] p_flags The flag collection to modify.
  65. * @param[in] key The key to modify the flag of.
  66. * @param[in] value The state to set the flag to.
  67. */
  68. void sdk_mapped_flags_update_by_key(uint16_t * p_keys,
  69. sdk_mapped_flags_t * p_flags,
  70. uint16_t key,
  71. bool value);
  72. /**@brief Function for updating the state of the same flag in multiple flag collections.
  73. *
  74. * @details The key and value are the same for all flag collections in the p_flags array.
  75. *
  76. * @param[in] p_keys The list of associated keys (assumed to have a length of
  77. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  78. * @param[out] p_flags The flag collections to modify.
  79. * @param[out] n_flag_collections The number of flag collections in p_flags.
  80. * @param[in] key The key to modify the flag of.
  81. * @param[in] value The state to set the flag to.
  82. */
  83. void sdk_mapped_flags_bulk_update_by_key(uint16_t * p_keys,
  84. sdk_mapped_flags_t * p_flags,
  85. uint32_t n_flag_collections,
  86. uint16_t key,
  87. bool value);
  88. /**@brief Function for getting the state of a specific flag.
  89. *
  90. * @param[in] p_keys The list of associated keys (assumed to have a length of
  91. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  92. * @param[in] flags The flag collection to read from.
  93. * @param[in] key The key to get the flag for.
  94. *
  95. * @return The state of the flag.
  96. */
  97. bool sdk_mapped_flags_get_by_key(uint16_t * p_keys, sdk_mapped_flags_t flags, uint16_t key);
  98. /**@brief Function for getting a list of all keys that have a specific flag set to true.
  99. *
  100. * @param[in] p_keys The list of associated keys (assumed to have a length of
  101. * @ref SDK_MAPPED_FLAGS_N_KEYS).
  102. * @param[in] flags The flag collection to search.
  103. *
  104. * @return The list of keys.
  105. */
  106. sdk_mapped_flags_key_list_t sdk_mapped_flags_key_list_get(uint16_t * p_keys,
  107. sdk_mapped_flags_t flags);
  108. /**@brief Function for getting the number of keys that have a specific flag set to true.
  109. *
  110. * @param[in] flags The flag collection to search.
  111. *
  112. * @return The number of keys.
  113. */
  114. uint32_t sdk_mapped_flags_n_flags_set(sdk_mapped_flags_t flags);
  115. /**@brief Function for querying whether any flags in the collection are set.
  116. *
  117. * @param[in] flags The flag collection to query.
  118. *
  119. * @retval true If one or more flags are set to true.
  120. * @retval false Otherwise.
  121. */
  122. static __INLINE bool sdk_mapped_flags_any_set(sdk_mapped_flags_t flags)
  123. {
  124. return (flags != 0);
  125. }
  126. /** @} */
  127. #endif /* SDK_MAPPED_FLAGS_H__ */