nfc_t4t_cc_file.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #include "sdk_config.h"
  41. #if NFC_T4T_CC_FILE_PARSER_ENABLED
  42. #include <string.h>
  43. #include "nfc_t4t_cc_file.h"
  44. #include "sdk_macros.h"
  45. #include "nordic_common.h"
  46. #include "app_util.h"
  47. #define NRF_LOG_MODULE_NAME nfc_t4t_cc_file_parser
  48. #if NFC_T4T_CC_FILE_PARSER_LOG_ENABLED
  49. #define NRF_LOG_LEVEL NFC_T4T_CC_FILE_PARSER_LOG_LEVEL
  50. #define NRF_LOG_INFO_COLOR NFC_T4T_CC_FILE_PARSER_INFO_COLOR
  51. #include "nrf_log.h"
  52. NRF_LOG_MODULE_REGISTER();
  53. #else // NFC_T4T_CC_FILE_PARSER_LOG_ENABLED
  54. #define NRF_LOG_LEVEL 0
  55. #include "nrf_log.h"
  56. #endif // NFC_T4T_CC_FILE_PARSER_LOG_ENABLED
  57. /**
  58. * @brief Valid value range for CCLEN field.
  59. */
  60. #define CC_LEN_MIN_VALUE 0x000F
  61. #define CC_LEN_MAX_VALUE 0xFFFE
  62. /**
  63. * @brief Valid major versions of Type 4 Tag specification.
  64. */
  65. #define NFC_T4T_EXTENDED_MAJOR_VER 0x03 ///< Major version number allowing first TLV block to be Extended NDEF File Control TLV
  66. #define NFC_T4T_REGULAR_MAJOR_VER 0x02 ///< Major version number allowing first TLV block to be NDEF File Control TLV
  67. /**
  68. * @brief Valid value range for MLe field.
  69. */
  70. #define MLE_LEN_MIN_VALUE 0x000F
  71. #define MLE_LEN_MAX_VALUE 0xFFFF
  72. /**
  73. * @brief Valid value range for MLc field.
  74. */
  75. #define MLC_LEN_MIN_VALUE 0x0001
  76. #define MLC_LEN_MAX_VALUE 0xFFFF
  77. /**
  78. * @brief Field sizes that are present in CC file.
  79. */
  80. #define CC_LEN_FIELD_SIZE 2U
  81. #define MAP_VER_FIELD_SIZE 1U
  82. #define MLE_FIELD_SIZE 2U
  83. #define MLC_FIELD_SIZE 2U
  84. /// Gets least significant nibble (a 4-bit value) from a byte.
  85. #define LSN_GET(val) (val & 0x0F)
  86. /// Gets most significant nibble (a 4-bit value) from a byte.
  87. #define MSN_GET(val) ((val >> 4) & 0x0F)
  88. /**
  89. * @brief Function for validating arguments used by CC file parsing procedure.
  90. */
  91. __STATIC_INLINE ret_code_t nfc_t4t_cc_args_validate(nfc_t4t_capability_container_t * p_t4t_cc_file,
  92. uint8_t * p_raw_data,
  93. uint16_t len)
  94. {
  95. if ( (p_t4t_cc_file == NULL)
  96. || (p_t4t_cc_file->p_tlv_block_array == NULL)
  97. || (p_raw_data == NULL) )
  98. {
  99. return NRF_ERROR_NULL;
  100. }
  101. if ( (len < CC_LEN_MIN_VALUE) || (len > CC_LEN_MAX_VALUE) )
  102. {
  103. return NRF_ERROR_INVALID_LENGTH;
  104. }
  105. if (p_t4t_cc_file->max_tlv_blocks == 0)
  106. {
  107. return NRF_ERROR_NO_MEM;
  108. }
  109. return NRF_SUCCESS;
  110. }
  111. /**
  112. * @brief Function for validating CC file descriptor content.
  113. */
  114. __STATIC_INLINE ret_code_t nfc_t4t_cc_file_validate(nfc_t4t_capability_container_t * p_t4t_cc_file)
  115. {
  116. uint16_t type = p_t4t_cc_file->p_tlv_block_array[0].type;
  117. if ( (p_t4t_cc_file->major_version == NFC_T4T_EXTENDED_MAJOR_VER
  118. && type == EXTENDED_NDEF_FILE_CONTROL_TLV) ||
  119. (p_t4t_cc_file->major_version == NFC_T4T_REGULAR_MAJOR_VER
  120. && type == NDEF_FILE_CONTROL_TLV) )
  121. {
  122. return NRF_SUCCESS;
  123. }
  124. return NRF_ERROR_INVALID_DATA;
  125. }
  126. /**
  127. * @brief Function for clearing all TLV blocks from CC file descriptor.
  128. */
  129. __STATIC_INLINE void nfc_t4t_cc_file_clear(nfc_t4t_capability_container_t * p_t4t_cc_file)
  130. {
  131. p_t4t_cc_file->tlv_count = 0;
  132. }
  133. /**
  134. * @brief Function for adding a TLV block to the CC file descriptor.
  135. */
  136. static ret_code_t nfc_t4t_tlv_block_insert(nfc_t4t_capability_container_t * p_t4t_cc_file,
  137. nfc_t4t_tlv_block_t * p_tlv_block)
  138. {
  139. if (p_t4t_cc_file->tlv_count == p_t4t_cc_file->max_tlv_blocks)
  140. {
  141. return NRF_ERROR_NO_MEM;
  142. }
  143. // Copy contents of the source block.
  144. p_t4t_cc_file->p_tlv_block_array[p_t4t_cc_file->tlv_count] = *p_tlv_block;
  145. p_t4t_cc_file->tlv_count++;
  146. return NRF_SUCCESS;
  147. }
  148. ret_code_t nfc_t4t_cc_file_parse(nfc_t4t_capability_container_t * p_t4t_cc_file,
  149. uint8_t * p_raw_data,
  150. uint16_t len)
  151. {
  152. ret_code_t err_code = nfc_t4t_cc_args_validate(p_t4t_cc_file, p_raw_data, len);
  153. VERIFY_SUCCESS(err_code);
  154. uint8_t * p_offset = p_raw_data;
  155. nfc_t4t_cc_file_clear(p_t4t_cc_file);
  156. p_t4t_cc_file->len = uint16_big_decode(p_offset);
  157. p_offset += CC_LEN_FIELD_SIZE;
  158. p_t4t_cc_file->major_version = MSN_GET(*p_offset);
  159. p_t4t_cc_file->minor_version = LSN_GET(*p_offset);
  160. p_offset += MAP_VER_FIELD_SIZE;
  161. p_t4t_cc_file->max_rapdu_size = uint16_big_decode(p_offset);
  162. p_offset += MLE_FIELD_SIZE;
  163. p_t4t_cc_file->max_capdu_size = uint16_big_decode(p_offset);
  164. p_offset += MLC_FIELD_SIZE;
  165. nfc_t4t_tlv_block_t new_block;
  166. len -= (p_offset - p_raw_data);
  167. while (len > 0)
  168. {
  169. uint16_t tlv_len = len;
  170. err_code = nfc_t4t_file_control_tlv_parse(&new_block, p_offset, &tlv_len);
  171. VERIFY_SUCCESS(err_code);
  172. p_offset += tlv_len;
  173. len -= tlv_len;
  174. err_code = nfc_t4t_tlv_block_insert(p_t4t_cc_file, &new_block);
  175. VERIFY_SUCCESS(err_code);
  176. }
  177. return nfc_t4t_cc_file_validate(p_t4t_cc_file);
  178. }
  179. nfc_t4t_tlv_block_t * nfc_t4t_file_content_get(nfc_t4t_capability_container_t * p_t4t_cc_file,
  180. uint16_t file_id)
  181. {
  182. nfc_t4t_tlv_block_t * p_tlv_array = p_t4t_cc_file->p_tlv_block_array;
  183. for (uint8_t i = 0; i < p_t4t_cc_file->tlv_count; i++)
  184. {
  185. nfc_t4t_file_control_val_t * p_tlv_value = &p_tlv_array[i].value;
  186. if (p_tlv_value->file_id == file_id)
  187. {
  188. return (p_tlv_array + i);
  189. }
  190. }
  191. return NULL;
  192. }
  193. ret_code_t nfc_t4t_file_content_set(nfc_t4t_capability_container_t * p_t4t_cc_file,
  194. nfc_t4t_file_t file,
  195. uint16_t file_id)
  196. {
  197. nfc_t4t_tlv_block_t * p_tlv_block;
  198. p_tlv_block = nfc_t4t_file_content_get(p_t4t_cc_file, file_id);
  199. if (p_tlv_block != NULL)
  200. {
  201. p_tlv_block->value.file = file;
  202. return NRF_SUCCESS;
  203. }
  204. return NRF_ERROR_NOT_FOUND;
  205. }
  206. void nfc_t4t_cc_file_printout(nfc_t4t_capability_container_t * p_t4t_cc_file)
  207. {
  208. NRF_LOG_INFO("Capability Container File content: ")
  209. NRF_LOG_INFO("CCLEN: %d ", p_t4t_cc_file->len);
  210. NRF_LOG_INFO("Mapping Version: %d.%d ",
  211. p_t4t_cc_file->major_version,
  212. p_t4t_cc_file->minor_version);
  213. NRF_LOG_INFO("MLe: %d ", p_t4t_cc_file->max_rapdu_size)
  214. NRF_LOG_INFO("MLc: %d ", p_t4t_cc_file->max_capdu_size)
  215. NRF_LOG_INFO("Capability Container File contains %d File Control TLV block(s).",
  216. p_t4t_cc_file->tlv_count);
  217. for (uint8_t i = 0; i < p_t4t_cc_file->tlv_count; i++)
  218. {
  219. nfc_t4t_file_control_tlv_printout(i, &p_t4t_cc_file->p_tlv_block_array[i]);
  220. }
  221. }
  222. #endif // NFC_T4T_CC_FILE_PARSER_ENABLED