es_slot.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Copyright (c) 2016 - 2020, 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_SLOT_H__
  41. #define ES_SLOT_H__
  42. #include <stdint.h>
  43. #include "es_app_config.h"
  44. #include "nrf_ble_escs.h"
  45. /**
  46. * @file
  47. * @defgroup eddystone_slot Slots
  48. * @brief Types and functions for handling Eddystone slots.
  49. * @ingroup eddystone
  50. * @{
  51. */
  52. /**@brief Advertisable frame types that can be passed in to the advertising
  53. * data during non-connectable slot advertising. */
  54. typedef struct
  55. {
  56. union
  57. {
  58. es_uid_frame_t uid; //!< UID frame.
  59. es_url_frame_t url; //!< URL frame.
  60. es_tlm_frame_t tlm; //!< TLM frame.
  61. es_eid_frame_t eid; //!< EID frame.
  62. es_etlm_frame_t etlm; //!< eTLM frame.
  63. } frame;
  64. es_frame_type_t type; //!< Type defined twice for convenience (because the other one is inside a union).
  65. uint8_t length;
  66. }es_adv_frame_t;
  67. /**@brief Slot. */
  68. typedef struct
  69. {
  70. uint8_t slot_no; //!< Identifier for the slot, indexed at 0.
  71. nrf_ble_escs_radio_tx_pwr_t radio_tx_pwr; //!< Radio TX power (in dB).
  72. nrf_ble_escs_eid_id_key_t encrypted_eid_id_key; //!< EID key for the slot.
  73. es_adv_frame_t adv_frame; //!< Frame structure to be passed in for advertising data.
  74. bool adv_custom_tx_power; //!< Flag that specifies if the client has written to the 'Advertised TX Power' field of this slot.
  75. nrf_ble_escs_radio_tx_pwr_t custom_tx_power; //!< Custom TX power to advertise (only if @ref adv_custom_tx_power is true).
  76. bool configured; //!< Is this slot configured and active.
  77. uint8_t k_scaler;
  78. uint32_t seconds;
  79. uint8_t ik[ESCS_AES_KEY_SIZE];
  80. } es_slot_t;
  81. /**@brief Slot registry. */
  82. typedef struct
  83. {
  84. es_slot_t slots[APP_MAX_ADV_SLOTS];
  85. uint8_t num_configured_slots;
  86. uint8_t num_configured_eid_slots;
  87. uint8_t slots_configured[APP_MAX_ADV_SLOTS];
  88. uint8_t eid_slots_configured[APP_MAX_EID_SLOTS];
  89. uint8_t tlm_slot;
  90. bool tlm_configured;
  91. uint8_t scaler_k;
  92. uint8_t enc_key[ESCS_AES_KEY_SIZE];
  93. } es_slot_reg_t;
  94. /**@brief Function for initializing the Eddystone slots with default values.
  95. *
  96. * @details This function synchronizes all slots with the initial values.
  97. *
  98. * @param[in] p_default_slot Pointer to the default parameters for a slot.
  99. */
  100. void es_slots_init(const es_slot_t * p_default_slot);
  101. /**@brief Function for setting the advertising interval of the specified slot.
  102. *
  103. * For compatibility with the Eddystone specifications, @p p_adv_interval must point to
  104. * a 16-bit big endian value (coming from the characteristic write request),
  105. * which is then converted to a little endian value inside the function before
  106. * it is written into the variable in the slot.
  107. *
  108. * @parameternoteslot
  109. * @parameternoteadv
  110. *
  111. * @param[in] slot_no The index of the slot.
  112. * @param[in,out] p_adv_interval Pointer to the advertisement interval (in ms) to set.
  113. * @param[in] global Flag that should be set if the beacon does not support variable advertising intervals.
  114. */
  115. void es_slot_adv_interval_set(uint8_t slot_no,
  116. nrf_ble_escs_adv_interval_t * p_adv_interval,
  117. bool global);
  118. /**@brief Function for setting the TX power of the specified slot.
  119. *
  120. * @parameternoteslot
  121. * @parameternotetxpower
  122. *
  123. * @param[in] slot_no The index of the slot.
  124. * @param[in,out] radio_tx_pwr TX power value to set.
  125. */
  126. void es_slot_radio_tx_pwr_set(uint8_t slot_no, nrf_ble_escs_radio_tx_pwr_t radio_tx_pwr);
  127. /**@brief Function for setting the R/W ADV of the specified slot.
  128. *
  129. * @parameternoteslot
  130. *
  131. * @param[in] slot_no The index of the slot.
  132. * @param[in,out] length The length of the data written or read.
  133. * @param[in,out] p_frame_data Pointer to the data.
  134. *
  135. */
  136. void es_slot_on_write(uint8_t slot_no, uint8_t length, uint8_t const * p_frame_data);
  137. /**@brief Function for writing the slot's configuration to flash.
  138. *
  139. * @param[in] slot_no The index of the slot.
  140. */
  141. ret_code_t es_slot_write_to_flash(uint8_t slot_no);
  142. /**@brief Function for setting the slot's encrypted EID Identity Key to be displayed in the EID Identity Key characteristic.
  143. *
  144. * @parameternoteslot
  145. *
  146. * @param[in] slot_no The index of the slot.
  147. * @param[in,out] p_eid_id_key Pointer to a @ref nrf_ble_escs_eid_id_key_t structure from where the key will be written.
  148. */
  149. void es_slot_encrypted_eid_id_key_set(uint8_t slot_no, nrf_ble_escs_eid_id_key_t * p_eid_id_key);
  150. /**@brief Function for marking an EID slot as ready for populating.
  151. *
  152. * @details Call this function when an EID has been generated and the advertisement frame can be populated with the EID.
  153. *
  154. * @param[in] slot_no The index of the slot.
  155. */
  156. void es_slot_eid_ready(uint8_t slot_no);
  157. /**@brief Function for updating the TLM slot with updated data. */
  158. void es_slot_tlm_update(void);
  159. /**@brief Function for updating the TLM slot with eTLM data.
  160. *
  161. * @details This function uses the EID identity key from the given EID slot number to update the TLM slot.
  162. *
  163. * @param[in] eid_slot_no EID slot to get EID identity key from.
  164. */
  165. void es_slot_etlm_update(uint8_t eid_slot_no);
  166. /**@brief Function for getting a pointer to the slot registry.
  167. *
  168. * @return A pointer to the slot registry.
  169. */
  170. const es_slot_reg_t * es_slot_get_registry(void);
  171. /**@brief Function for setting a custom advertisement TX power for a given slot.
  172. *
  173. * @parameternoteslot
  174. * @parameternotetxpower
  175. *
  176. * @param[in] slot_no The index of the slot.
  177. * @param[in] tx_pwr Advertised TX power to be set.
  178. */
  179. void es_slot_set_adv_custom_tx_power(uint8_t slot_no, nrf_ble_escs_adv_tx_pwr_t tx_pwr);
  180. /**
  181. * @}
  182. */
  183. #endif // ES_SLOT_H__