nfc_ac_rec.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Copyright (c) 2015 - 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 NFC_AC_REC_H__
  41. #define NFC_AC_REC_H__
  42. /**@file
  43. *
  44. * @defgroup nfc_ac_rec ac (Alternative carrier) records
  45. * @{
  46. * @ingroup nfc_ble_pair_msg
  47. *
  48. * @brief Generation of NFC NDEF Alternative Carrier records for NDEF messages.
  49. *
  50. */
  51. #include <stdint.h>
  52. #include "nfc_ndef_record.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #define AC_REC_CPS_BYTE_SIZE 1 ///< Size of the field with CPS data.
  57. #define AC_REC_DATA_REF_LEN_SIZE 1 ///< Size of the Data Reference Length field.
  58. #define AC_REC_AUX_DATA_REF_COUNT_SIZE 1 ///< Size of the Data Reference Length field.
  59. /**
  60. * @brief Carrier Power State.
  61. *
  62. * Possible Carrier Power State field values in an Alternative Carrier record.
  63. */
  64. typedef enum
  65. {
  66. NFC_AC_CPS_INACTIVE = 0x00, ///< Alternative Carrier inactive.
  67. NFC_AC_CPS_ACTIVE = 0x01, ///< Alternative Carrier active.
  68. NFC_AC_CPS_ACTIVATING = 0x02, ///< Alternative Carrier activating.
  69. NFC_AC_CPS_UNKNOWN = 0x03 ///< Alternative Carrier power status unknown.
  70. } nfc_ac_rec_cps_t;
  71. #define NFC_AC_CPS_MASK (NFC_AC_CPS_UNKNOWN) ///< Mask of Carrier Power State bits in a first ac record byte.
  72. /**
  73. * @brief Carrier Data Reference and Auxiliary Data Reference descriptor.
  74. */
  75. typedef struct
  76. {
  77. uint8_t length; ///< Length of the data field.
  78. uint8_t * p_data; ///< Pointer to the Data Reference characters. Not relevant if length is 0.
  79. } nfc_ac_rec_data_ref_t;
  80. /**
  81. * @brief Alternative Carrier record payload descriptor.
  82. */
  83. typedef struct
  84. {
  85. nfc_ac_rec_cps_t cps; ///< Carrier Power State value.
  86. nfc_ac_rec_data_ref_t carrier_data_ref; ///< Carrier Data Reference.
  87. uint8_t const max_aux_data_ref; ///< Maximum number of Auxiliary Data Reference fields.
  88. uint8_t aux_data_ref_count; ///< Number of Auxiliary Data Reference fields.
  89. nfc_ac_rec_data_ref_t * p_aux_data_ref; ///< Pointer to the Auxiliary Data Reference fields.
  90. } nfc_ac_rec_payload_desc_t;
  91. /**
  92. * @brief Constructor for an NFC NDEF Alternative Carrier record payload.
  93. *
  94. * This function encodes the payload of an Alternative Carrier record as specified in the Connection
  95. * Handover standard. It implements an API compatible with @ref p_payload_constructor_t.
  96. */
  97. ret_code_t nfc_ac_rec_payload_constructor(nfc_ac_rec_payload_desc_t * p_nfc_rec_ac_payload_desc,
  98. uint8_t * p_buff,
  99. uint32_t * p_len);
  100. /**
  101. * @brief External reference to the type field of the Alternative Carrier record, defined in the
  102. * file @c nfc_ac_rec.c. It is used in the @ref NFC_NDEF_AC_RECORD_DESC_DEF macro.
  103. */
  104. extern const uint8_t nfc_ac_rec_type_field[2];
  105. /**
  106. * @brief Size of the type field of the Alternative Carrier record, defined in the
  107. * file @c nfc_ac_rec.c. It is used in the @ref NFC_NDEF_AC_RECORD_DESC_DEF macro.
  108. */
  109. #define NFC_AC_REC_TYPE_LENGTH 2
  110. /**
  111. *@brief Macro for creating and initializing an NFC NDEF record descriptor for an Alternative Carrier record.
  112. *
  113. * This macro creates and initializes an instance of type @ref nfc_ndef_record_desc_t and
  114. * an instance of type @ref nfc_ac_rec_payload_desc_t, which together constitute an instance of an Alternative Carrier record.
  115. *
  116. * Use the macro @ref NFC_NDEF_AC_RECORD_DESC to access the NDEF Alternative Carrier record descriptor instance.
  117. *
  118. * @note The record descriptor is declared as automatic variable, which implies that
  119. * the NDEF message encoding (see @ref nfc_ble_full_handover_select_msg_encode)
  120. * must be done in the same variable scope.
  121. *
  122. * @param[in] NAME Name of the created record descriptor instance.
  123. * @param[in] CPS Carrier Power State value.
  124. * @param[in] CARR_DATA_REF_LEN Length of the Carrier Data Reference field.
  125. * @param[in] P_CARR_DATA_REF Pointer to the Carrier Data Reference field.
  126. * @param[in] MAX_AUX_DATA_REF Maximum number of Auxiliary Data Reference fields.
  127. */
  128. #define NFC_NDEF_AC_RECORD_DESC_DEF(NAME, \
  129. CPS, \
  130. CARR_DATA_REF_LEN, \
  131. P_CARR_DATA_REF, \
  132. MAX_AUX_DATA_REF) \
  133. nfc_ac_rec_data_ref_t NAME##_nfc_ac_rec_aux_data_ref_array[MAX_AUX_DATA_REF]; \
  134. nfc_ac_rec_payload_desc_t NAME##_nfc_ac_rec_payload_desc = \
  135. { \
  136. .cps = CPS, \
  137. .carrier_data_ref = {CARR_DATA_REF_LEN, P_CARR_DATA_REF}, \
  138. .max_aux_data_ref = MAX_AUX_DATA_REF, \
  139. .aux_data_ref_count = 0, \
  140. .p_aux_data_ref = NAME##_nfc_ac_rec_aux_data_ref_array \
  141. }; \
  142. NFC_NDEF_GENERIC_RECORD_DESC_DEF(NAME, \
  143. TNF_WELL_KNOWN, \
  144. 0, \
  145. 0, \
  146. nfc_ac_rec_type_field, \
  147. NFC_AC_REC_TYPE_LENGTH, \
  148. nfc_ac_rec_payload_constructor, \
  149. &(NAME##_nfc_ac_rec_payload_desc))
  150. /**
  151. * @brief Macro for accessing the NFC NDEF Alternative Carrier record descriptor
  152. * instance that was created with @ref NFC_NDEF_AC_RECORD_DESC_DEF.
  153. */
  154. #define NFC_NDEF_AC_RECORD_DESC(NAME) NFC_NDEF_GENERIC_RECORD_DESC(NAME)
  155. /**
  156. * @brief Function for clearing an Auxiliary Data Reference in an NFC NDEF Alternative Carrier record.
  157. *
  158. * This function clears the Auxiliary Data References from the Alternative Carrier record.
  159. *
  160. * @param[in, out] p_ac_rec Pointer to the Alternative Carrier record descriptor.
  161. */
  162. void nfc_ac_rec_auxiliary_data_ref_clear(nfc_ndef_record_desc_t * p_ac_rec);
  163. /**
  164. * @brief Function for adding an Auxiliary Data Reference to an NFC NDEF Alternative Carrier record.
  165. *
  166. * @param[in, out] p_ac_rec Pointer to an ac record.
  167. * @param[in] p_aux_data Pointer to the Auxiliary Data Reference data buffer.
  168. * @param[in] aux_length Length of the Auxiliary Data Reference data.
  169. *
  170. * @retval NRF_SUCCESS If the Auxiliary Data Reference was added successfully.
  171. * @retval NRF_ERROR_NO_MEM If the record already contains the maximum number of Auxiliary Data References.
  172. */
  173. ret_code_t nfc_ac_rec_auxiliary_data_ref_add(nfc_ndef_record_desc_t * p_ac_rec,
  174. uint8_t * p_aux_data,
  175. uint8_t aux_length);
  176. /** @} */
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif // NFC_AC_REC_H__