nfc_ndef_msg_parser_local.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 NFC_NDEF_MSG_PARSER_LOCAL_H__
  41. #define NFC_NDEF_MSG_PARSER_LOCAL_H__
  42. /**@file
  43. *
  44. * @defgroup nfc_ndef_msg_parser_local NDEF message parser (internal)
  45. * @{
  46. * @ingroup nfc_ndef_msg_parser
  47. *
  48. * @brief Internal part of the parser for NFC NDEF messages.
  49. *
  50. */
  51. #include <stdint.h>
  52. #include "nfc_ndef_msg.h"
  53. #include "nfc_ndef_record_parser.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * @brief Type for holding descriptors that are used by the NDEF parser.
  59. */
  60. typedef struct
  61. {
  62. nfc_ndef_msg_desc_t * p_msg_desc; ///< Pointer to the message descriptor.
  63. nfc_ndef_bin_payload_desc_t * p_bin_pay_desc; ///< Pointer to the array of binary payload descriptors.
  64. nfc_ndef_record_desc_t * p_rec_desc; ///< Pointer to the array of record descriptors.
  65. } nfc_ndef_parser_memo_desc_t;
  66. /**
  67. * @brief Memory allocated for a one-record message.
  68. */
  69. typedef struct
  70. {
  71. nfc_ndef_msg_desc_t msg_desc;
  72. nfc_ndef_record_desc_t * p_record_desc_array[1];
  73. nfc_ndef_bin_payload_desc_t bin_pay_desc[1];
  74. nfc_ndef_record_desc_t rec_desc[1];
  75. } parsed_ndef_msg_1_t;
  76. /**
  77. * @brief Memory allocated for a two-record message.
  78. */
  79. typedef struct
  80. {
  81. nfc_ndef_msg_desc_t msg_desc;
  82. nfc_ndef_record_desc_t * p_record_desc_array[2];
  83. nfc_ndef_bin_payload_desc_t bin_pay_desc[2];
  84. nfc_ndef_record_desc_t rec_desc[2];
  85. } parsed_ndef_msg_2_t;
  86. /**
  87. * @brief Amount of memory that is required per record in addition to the memory allocated for the message descriptor.
  88. */
  89. #define NFC_PARSER_M_DELTA (sizeof(parsed_ndef_msg_2_t) - sizeof(parsed_ndef_msg_1_t))
  90. /**
  91. * @brief Function for resolving data instances in the provided buffer according
  92. * to requirements of the function @ref internal_ndef_msg_parser.
  93. *
  94. * This internal function distributes the provided memory between certain data instances that are required
  95. * by @ref internal_ndef_msg_parser.
  96. *
  97. * This function should not be used directly.
  98. *
  99. * @param[in] p_result_buf Pointer to the buffer that will be used to allocate
  100. * data instances.
  101. * @param[in,out] p_result_buf_len As input: size of the buffer specified by @p p_result_buf.
  102. * As output: size of the reserved (used) part of the buffer specified by
  103. * @p p_result_buf.
  104. * @param[out] p_parser_memo_desc Pointer to the structure for holding descriptors of the allocated data
  105. * instances.
  106. *
  107. * @retval NRF_SUCCESS If the function completed successfully.
  108. * @retval NRF_ERROR_NO_MEM If the provided buffer is too small to hold a one-record message.
  109. */
  110. ret_code_t ndef_parser_memo_resolve(uint8_t * const p_result_buf,
  111. uint32_t * const p_result_buf_len,
  112. nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc);
  113. /**
  114. * @brief Function for parsing NFC NDEF messages.
  115. *
  116. * This internal function parses NDEF messages into certain data instances.
  117. *
  118. * This function should not be used directly.
  119. *
  120. * @param[in,out] p_parser_memo_desc Pointer to the structure that holds descriptors of the allocated data
  121. * instances for the parser. This structure contains the following fields: @n
  122. * .p_msg_desc Pointer to the message descriptor that will
  123. * be filled with parsed data. @n
  124. * .p_bin_pay_desc Pointer to the array of binary payload
  125. * descriptors that will be filled with parsed
  126. * data. @n
  127. * .p_rec_desc Pointer to the array of record descriptors
  128. * that will be filled with parsed data. @n
  129. * The arrays specified by @p .p_bin_pay_desc and @p .p_rec_desc must not
  130. * contain more elements than the message descriptor
  131. * specified by \p .p_msg_desc can hold.
  132. *
  133. * @param[in] p_nfc_data Pointer to the data to be parsed.
  134. * @param[in,out] p_nfc_data_len As input: size of the NFC data in the @p p_nfc_data buffer.
  135. * As output: size of the parsed message.
  136. *
  137. * @retval NRF_SUCCESS If the function completed successfully.
  138. * @retval NRF_ERROR_INVALID_LENGTH If the expected message length is bigger than the amount of provided input data.
  139. * @retval NRF_ERROR_INVALID_DATA If the message is not a valid NDEF message.
  140. * @retval NRF_ERROR_NO_MEM If the provided memory resources are too small to hold the parsing result.
  141. */
  142. ret_code_t internal_ndef_msg_parser(nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc,
  143. uint8_t const * p_nfc_data,
  144. uint32_t * const p_nfc_data_len);
  145. /**
  146. * @}
  147. */
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif // NFC_NDEF_MSG_PARSER_LOCAL_H__