nfc_t2t_parser.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright (c) 2015 - 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 NFC_TYPE_2_TAG_PARSER_H__
  41. #define NFC_TYPE_2_TAG_PARSER_H__
  42. #include <stdint.h>
  43. #include "nfc_tlv_block.h"
  44. #include "sdk_errors.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nfc_type_2_tag Type 2 Tag
  50. * @{
  51. * @ingroup nfc_type_2_tag_parser
  52. *
  53. * @brief Descriptor for a Type 2 Tag.
  54. *
  55. */
  56. /**
  57. * @brief Descriptor for the internal bytes of a Type 2 Tag.
  58. */
  59. typedef struct
  60. {
  61. uint8_t manufacturer_id; ///< Manufacturer ID (the most significant byte of the UID/serial number).
  62. uint16_t serial_number_part_1; ///< Bytes 5-4 of the tag UID.
  63. uint8_t check_byte_0; ///< First block check character byte (XOR of the cascade tag byte, manufacturer ID byte, and the serial_number_part_1 bytes).
  64. uint32_t serial_number_part_2; ///< Bytes 3-0 of the tag UID.
  65. uint8_t check_byte_1; ///< Second block check character byte (XOR of the serial_number_part_2 bytes).
  66. uint8_t internal; ///< Tag internal bytes.
  67. } type_2_tag_serial_number_t;
  68. /**
  69. * @brief Descriptor for the Capability Container (CC) bytes of a Type 2 Tag.
  70. */
  71. typedef struct
  72. {
  73. uint8_t major_version; ///< Major version of the supported Type 2 Tag specification.
  74. uint8_t minor_version; ///< Minor version of the supported Type 2 Tag specification.
  75. uint16_t data_area_size; ///< Size of the data area in bytes.
  76. uint8_t read_access; ///< Read access for the data area.
  77. uint8_t write_access; ///< Write access for the data area.
  78. } type_2_tag_capability_container_t;
  79. /**
  80. * @brief Type 2 Tag descriptor.
  81. */
  82. typedef struct
  83. {
  84. type_2_tag_serial_number_t sn; ///< Values within the serial number area of the tag.
  85. uint16_t lock_bytes; ///< Value of the lock bytes.
  86. type_2_tag_capability_container_t cc; ///< Values within the Capability Container area of the tag.
  87. uint16_t const max_tlv_blocks; ///< Maximum number of TLV blocks that can be stored.
  88. tlv_block_t * p_tlv_block_array; ///< Pointer to the array for TLV blocks.
  89. uint16_t tlv_count; ///< Number of TLV blocks stored in the Type 2 Tag.
  90. } type_2_tag_t;
  91. /**
  92. * @brief Macro for creating and initializing a Type 2 Tag descriptor.
  93. *
  94. * This macro creates and initializes a static instance of a @ref type_2_tag_t structure and
  95. * an array of @ref tlv_block_t descriptors.
  96. *
  97. * Use the macro @ref NFC_TYPE_2_TAG_DESC to access the Type 2 Tag descriptor instance.
  98. *
  99. * @param[in] NAME Name of the created descriptor instance.
  100. * @param[in] MAX_BLOCKS Maximum number of @ref tlv_block_t descriptors that can be stored in the array.
  101. *
  102. */
  103. #define NFC_TYPE_2_TAG_DESC_DEF(NAME, MAX_BLOCKS) \
  104. static tlv_block_t NAME##_tlv_block_array[MAX_BLOCKS]; \
  105. static type_2_tag_t NAME##_type_2_tag = \
  106. { \
  107. .max_tlv_blocks = MAX_BLOCKS, \
  108. .p_tlv_block_array = NAME##_tlv_block_array, \
  109. .tlv_count = 0 \
  110. }
  111. /**
  112. * @brief Macro for accessing the @ref type_2_tag_t instance that was created
  113. * with @ref NFC_TYPE_2_TAG_DESC_DEF.
  114. */
  115. #define NFC_TYPE_2_TAG_DESC(NAME) (NAME##_type_2_tag)
  116. #define T2T_NFC_FORUM_DEFINED_DATA 0xE1 ///< Value indicating that the Type 2 Tag contains NFC Forum defined data.
  117. #define T2T_UID_BCC_CASCADE_BYTE 0x88 ///< Value used for calculating the first BCC byte of a Type 2 Tag serial number.
  118. #define T2T_SUPPORTED_MAJOR_VERSION 1 ///< Supported major version of the Type 2 Tag specification.
  119. #define T2T_SUPPORTED_MINOR_VERSION 2 ///< Supported minor version of the Type 2 Tag specification.
  120. #define T2T_BLOCK_SIZE 4 ///< Type 2 Tag block size in bytes.
  121. #define T2T_CC_BLOCK_OFFSET 12 ///< Offset of the Capability Container area in the Type 2 Tag.
  122. #define T2T_FIRST_DATA_BLOCK_OFFSET 16 ///< Offset of the data area in the Type 2 Tag.
  123. /**
  124. * @}
  125. */
  126. /**
  127. * @defgroup nfc_type_2_tag_parser NFC Type 2 Tag parser
  128. * @{
  129. * @ingroup nfc_t2t
  130. *
  131. * @brief Parser for Type 2 Tag data.
  132. *
  133. */
  134. /**
  135. * @brief Function for clearing the @ref type_2_tag_t structure.
  136. *
  137. * @param[in,out] p_type_2_tag Pointer to the structure that should be cleared.
  138. *
  139. */
  140. void type_2_tag_clear(type_2_tag_t * p_type_2_tag);
  141. /**
  142. * @brief Function for parsing raw data read from a Type 2 Tag.
  143. *
  144. * This function parses the header and the following TLV blocks of a Type 2 Tag. The data is read
  145. * from a buffer and stored in a @ref type_2_tag_t structure.
  146. *
  147. * @param[out] p_type_2_tag Pointer to the structure that will be filled with parsed data.
  148. * @param[in] p_raw_data Pointer to the buffer with raw data from the tag (should
  149. * point at the first byte of the first block of the tag).
  150. *
  151. * @retval NRF_SUCCESS If the data was parsed successfully.
  152. * @retval NRF_ERROR_NO_MEM If there is not enough memory to store all of the TLV blocks.
  153. * @retval Other If an error occurred during the parsing operation.
  154. *
  155. */
  156. ret_code_t type_2_tag_parse(type_2_tag_t * p_type_2_tag, uint8_t * p_raw_data);
  157. /**
  158. * @brief Function for printing parsed contents of the Type 2 Tag.
  159. *
  160. * @param[in] p_type_2_tag Pointer to the structure that should be printed.
  161. *
  162. */
  163. void type_2_tag_printout(type_2_tag_t * p_type_2_tag);
  164. /**
  165. * @}
  166. */
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif /* NFC_TYPE_2_TAG_PARSER_H__ */