es_flash.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * Copyright (c) 2016 - 2018, 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 ES_FLASH_H__
  41. #define ES_FLASH_H__
  42. #include <stdbool.h>
  43. #include <stdint.h>
  44. #include "es_slot.h"
  45. /**
  46. * @file
  47. * @defgroup eddystone_flash Flash access
  48. * @brief Types and functions to access the flash of the Eddystone beacon.
  49. * @ingroup eddystone
  50. * @{
  51. */
  52. #define WORD_SIZE 4
  53. #define FLASH_ACCES_ERROR_CHECK_ALLOW_NOT_FOUND(err_code) \
  54. if (err_code != (FDS_ERR_NOT_FOUND)) \
  55. APP_ERROR_CHECK(err_code);
  56. #define FLASH_OP_WAIT() \
  57. uint32_t pending_ops = es_flash_num_pending_ops(); \
  58. while (pending_ops != 0) \
  59. { \
  60. pending_ops = es_flash_num_pending_ops(); \
  61. }
  62. /**@brief Beacon configuration. */
  63. typedef struct
  64. {
  65. nrf_ble_escs_adv_interval_t adv_interval; //!< Advertising interval.
  66. bool remain_connectable; //!< Flag that specifies if the beacon should remain connectable.
  67. } es_flash_beacon_config_t;
  68. /**@brief Structure for keeping track of which slot has a configuration that must be restored upon reboot.
  69. * @details The size of this structure must be word aligned and match the flash block size of 32 bytes.
  70. */
  71. typedef struct
  72. {
  73. bool slot_is_empty[APP_MAX_ADV_SLOTS]; //!< Flag that indicates whether the slot is empty.
  74. uint8_t padding[WORD_SIZE - ((APP_MAX_ADV_SLOTS + 1) % WORD_SIZE)]; //!< Padding used to ensure word alignment.
  75. } es_flash_flags_t;
  76. /**@brief Flash access types.
  77. */
  78. typedef enum
  79. {
  80. ES_FLASH_ACCESS_READ, //!< Read data.
  81. ES_FLASH_ACCESS_WRITE, //!< Write data.
  82. ES_FLASH_ACCESS_CLEAR //!< Clear data.
  83. } es_flash_access_t;
  84. /**@brief Function for accessing beacon configurations.
  85. *
  86. * @param[out,in] p_config Pointer to the beacon configuration buffer.
  87. * @param[in] access_type Access type (see @ref es_flash_access_t).
  88. * @return For possible return values, see:
  89. * - @ref fds_record_find_by_key
  90. * - @ref fds_record_open
  91. * - @ref fds_record_close
  92. * - @ref fds_record_write
  93. * - @ref fds_record_update
  94. * - @ref fds_record_delete
  95. */
  96. ret_code_t es_flash_access_beacon_config(es_flash_beacon_config_t * p_config,
  97. es_flash_access_t access_type);
  98. /**@brief Function for accessing slot configuration from flash.
  99. *
  100. * @param[in] slot_no Slot index.
  101. * @param[out,in] p_slot Pointer to the slot configuration buffer.
  102. * @param[in] access_type Access type (see @ref es_flash_access_t).
  103. * @return For possible return values, see:
  104. * - @ref fds_record_find_by_key
  105. * - @ref fds_record_open
  106. * - @ref fds_record_close
  107. * - @ref fds_record_write
  108. * - @ref fds_record_update
  109. * - @ref fds_record_delete
  110. */
  111. ret_code_t es_flash_access_slot_configs(uint8_t slot_no,
  112. es_slot_t * p_slot,
  113. es_flash_access_t access_type);
  114. /**@brief Function for accessing the beacon lock key from flash.
  115. *
  116. * @param[out,in] p_lock_key Pointer to the lock key buffer.
  117. * @param[in] access_type Access type (see @ref es_flash_access_t).
  118. * @return For possible return values, see:
  119. * - @ref fds_record_find_by_key
  120. * - @ref fds_record_open
  121. * - @ref fds_record_close
  122. * - @ref fds_record_write
  123. * - @ref fds_record_update
  124. * - @ref fds_record_delete
  125. */
  126. ret_code_t es_flash_access_lock_key(uint8_t * p_lock_key, es_flash_access_t access_type);
  127. /**@brief Function for accessing the flash configuration flag from flash.
  128. *
  129. * @param[out,in] p_flags Pointer to the flag buffer.
  130. * @param[in] access_type Access type (see @ref es_flash_access_t).
  131. * @return For possible return values, see:
  132. * - @ref fds_record_find_by_key
  133. * - @ref fds_record_open
  134. * - @ref fds_record_close
  135. * - @ref fds_record_write
  136. * - @ref fds_record_update
  137. * - @ref fds_record_delete
  138. */
  139. ret_code_t es_flash_access_flags(es_flash_flags_t * p_flags, es_flash_access_t access_type);
  140. /**@brief Function for retrieving the number of queued operations.
  141. * @return The number of operations that are queued.
  142. */
  143. uint32_t es_flash_num_pending_ops(void);
  144. /**@brief Function for performing a factory reset.
  145. * @return FDS return code.
  146. */
  147. ret_code_t es_flash_factory_reset(void);
  148. void es_flash_on_ble_evt(ble_evt_t const * p_evt);
  149. /**@brief Function for initializing the flash module.
  150. *
  151. * @return See @ref fds_init for possible return values.
  152. */
  153. ret_code_t es_flash_init(void);
  154. /**
  155. * @}
  156. */
  157. #endif // ES_FLASH_H__