nfc_t2t_parser.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NFC_TYPE_2_TAG_PARSER_H__
  13. #define NFC_TYPE_2_TAG_PARSER_H__
  14. #include <stdint.h>
  15. #include "nfc_tlv_block.h"
  16. #include "sdk_errors.h"
  17. /**
  18. * @defgroup nfc_type_2_tag Type 2 Tag
  19. * @{
  20. * @ingroup nfc_type_2_tag_parser
  21. *
  22. * @brief Descriptor for a Type 2 Tag.
  23. *
  24. */
  25. /**
  26. * @brief Descriptor for the internal bytes of a Type 2 Tag.
  27. */
  28. typedef struct
  29. {
  30. uint8_t manufacturer_id; ///< Manufacturer ID (the most significant byte of the UID/serial number).
  31. uint16_t serial_number_part_1; ///< Bytes 5-4 of the tag UID.
  32. 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).
  33. uint32_t serial_number_part_2; ///< Bytes 3-0 of the tag UID.
  34. uint8_t check_byte_1; ///< Second block check character byte (XOR of the serial_number_part_2 bytes).
  35. uint8_t internal; ///< Tag internal bytes.
  36. } type_2_tag_serial_number_t;
  37. /**
  38. * @brief Descriptor for the Capability Container (CC) bytes of a Type 2 Tag.
  39. */
  40. typedef struct
  41. {
  42. uint8_t major_version; ///< Major version of the supported Type 2 Tag specification.
  43. uint8_t minor_version; ///< Minor version of the supported Type 2 Tag specification.
  44. uint16_t data_area_size; ///< Size of the data area in bytes.
  45. uint8_t read_access; ///< Read access for the data area.
  46. uint8_t write_access; ///< Write access for the data area.
  47. } type_2_tag_capability_container_t;
  48. /**
  49. * @brief Type 2 Tag descriptor.
  50. */
  51. typedef struct
  52. {
  53. type_2_tag_serial_number_t sn; ///< Values within the serial number area of the tag.
  54. uint16_t lock_bytes; ///< Value of the lock bytes.
  55. type_2_tag_capability_container_t cc; ///< Values within the Capability Container area of the tag.
  56. uint16_t const max_tlv_blocks; ///< Maximum number of TLV blocks that can be stored.
  57. tlv_block_t * p_tlv_block_array; ///< Pointer to the array for TLV blocks.
  58. uint16_t tlv_count; ///< Number of TLV blocks stored in the Type 2 Tag.
  59. } type_2_tag_t;
  60. /**
  61. * @brief Macro for creating and initializing a Type 2 Tag descriptor.
  62. *
  63. * This macro creates and initializes a static instance of a @ref type_2_tag_t structure and
  64. * an array of @ref tlv_block_t descriptors.
  65. *
  66. * Use the macro @ref NFC_TYPE_2_TAG_DESC to access the Type 2 Tag descriptor instance.
  67. *
  68. * @param[in] NAME Name of the created descriptor instance.
  69. * @param[in] MAX_BLOCKS Maximum number of @ref tlv_block_t descriptors that can be stored in the array.
  70. *
  71. */
  72. #define NFC_TYPE_2_TAG_DESC_DEF(NAME, MAX_BLOCKS) \
  73. static tlv_block_t NAME##_tlv_block_array[MAX_BLOCKS]; \
  74. static type_2_tag_t NAME##_type_2_tag = \
  75. { \
  76. .max_tlv_blocks = MAX_BLOCKS, \
  77. .p_tlv_block_array = NAME##_tlv_block_array, \
  78. .tlv_count = 0 \
  79. }
  80. /**
  81. * @brief Macro for accessing the @ref type_2_tag_t instance that was created
  82. * with @ref NFC_TYPE_2_TAG_DESC_DEF.
  83. */
  84. #define NFC_TYPE_2_TAG_DESC(NAME) (NAME##_type_2_tag)
  85. #define T2T_NFC_FORUM_DEFINED_DATA 0xE1 ///< Value indicating that the Type 2 Tag contains NFC Forum defined data.
  86. #define T2T_UID_BCC_CASCADE_BYTE 0x88 ///< Value used for calculating the first BCC byte of a Type 2 Tag serial number.
  87. #define T2T_SUPPORTED_MAJOR_VERSION 1 ///< Supported major version of the Type 2 Tag specification.
  88. #define T2T_SUPPORTED_MINOR_VERSION 2 ///< Supported minor version of the Type 2 Tag specification.
  89. #define T2T_BLOCK_SIZE 4 ///< Type 2 Tag block size in bytes.
  90. #define T2T_CC_BLOCK_OFFSET 12 ///< Offset of the Capability Container area in the Type 2 Tag.
  91. #define T2T_FIRST_DATA_BLOCK_OFFSET 16 ///< Offset of the data area in the Type 2 Tag.
  92. /**
  93. * @}
  94. */
  95. /**
  96. * @defgroup nfc_type_2_tag_parser NFC Type 2 Tag parser
  97. * @{
  98. * @ingroup nfc_library
  99. *
  100. * @brief Parser for Type 2 Tag data.
  101. *
  102. */
  103. /**
  104. * @brief Function for clearing the @ref type_2_tag_t structure.
  105. *
  106. * @param[in,out] p_type_2_tag Pointer to the structure that should be cleared.
  107. *
  108. */
  109. void type_2_tag_clear(type_2_tag_t * p_type_2_tag);
  110. /**
  111. * @brief Function for parsing raw data read from a Type 2 Tag.
  112. *
  113. * This function parses the header and the following TLV blocks of a Type 2 Tag. The data is read
  114. * from a buffer and stored in a @ref type_2_tag_t structure.
  115. *
  116. * @param[out] p_type_2_tag Pointer to the structure that will be filled with parsed data.
  117. * @param[in] p_raw_data Pointer to the buffer with raw data from the tag (should
  118. * point at the first byte of the first block of the tag).
  119. *
  120. * @retval NRF_SUCCESS If the data was parsed successfully.
  121. * @retval NRF_ERROR_NO_MEM If there is not enough memory to store all of the TLV blocks.
  122. * @retval Other If an error occurred during the parsing operation.
  123. *
  124. */
  125. ret_code_t type_2_tag_parse(type_2_tag_t * p_type_2_tag, uint8_t * p_raw_data);
  126. /**
  127. * @brief Function for printing parsed contents of the Type 2 Tag.
  128. *
  129. * @param[in] p_type_2_tag Pointer to the structure that should be printed.
  130. *
  131. */
  132. void type_2_tag_printout(type_2_tag_t * p_type_2_tag);
  133. /**
  134. * @}
  135. */
  136. #endif /* NFC_TYPE_2_TAG_PARSER_H__ */