nfc_ndef_msg_parser_local.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_common.h"
  41. #if NRF_MODULE_ENABLED(NFC_NDEF_MSG_PARSER)
  42. #include "nfc_ndef_msg_parser_local.h"
  43. ret_code_t internal_ndef_msg_parser(nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc,
  44. uint8_t const * p_nfc_data,
  45. uint32_t * const p_nfc_data_len)
  46. {
  47. nfc_ndef_record_location_t record_location;
  48. ret_code_t ret_code;
  49. uint32_t nfc_data_left = *p_nfc_data_len;
  50. uint32_t temp_nfc_data_len = 0;
  51. // want to modify -> use local copy
  52. nfc_ndef_bin_payload_desc_t * p_bin_pay_desc = p_parser_memo_desc->p_bin_pay_desc;
  53. nfc_ndef_record_desc_t * p_rec_desc = p_parser_memo_desc->p_rec_desc;
  54. while (nfc_data_left > 0)
  55. {
  56. temp_nfc_data_len = nfc_data_left;
  57. ret_code = ndef_record_parser(p_bin_pay_desc,
  58. p_rec_desc,
  59. &record_location,
  60. p_nfc_data,
  61. &temp_nfc_data_len);
  62. if (ret_code != NRF_SUCCESS)
  63. {
  64. return ret_code;
  65. }
  66. // verify the records location flags
  67. if (p_parser_memo_desc->p_msg_desc->record_count == 0)
  68. {
  69. if ((record_location != NDEF_FIRST_RECORD) && (record_location != NDEF_LONE_RECORD))
  70. {
  71. return NRF_ERROR_INVALID_DATA;
  72. }
  73. }
  74. else
  75. {
  76. if ((record_location != NDEF_MIDDLE_RECORD) && (record_location != NDEF_LAST_RECORD))
  77. {
  78. return NRF_ERROR_INVALID_DATA;
  79. }
  80. }
  81. ret_code = nfc_ndef_msg_record_add(p_parser_memo_desc->p_msg_desc, p_rec_desc);
  82. if (ret_code != NRF_SUCCESS)
  83. {
  84. return ret_code;
  85. }
  86. nfc_data_left -= temp_nfc_data_len;
  87. if ((record_location == NDEF_LAST_RECORD) || (record_location == NDEF_LONE_RECORD))
  88. {
  89. *p_nfc_data_len = *p_nfc_data_len - nfc_data_left;
  90. return NRF_SUCCESS;
  91. }
  92. else
  93. {
  94. if (p_parser_memo_desc->p_msg_desc->record_count ==
  95. p_parser_memo_desc->p_msg_desc->max_record_count)
  96. {
  97. return NRF_ERROR_NO_MEM;
  98. }
  99. p_nfc_data += temp_nfc_data_len;
  100. p_bin_pay_desc++;
  101. p_rec_desc++;
  102. }
  103. }
  104. return NRF_ERROR_INVALID_DATA;
  105. }
  106. ret_code_t ndef_parser_memo_resolve(uint8_t * const p_result_buf,
  107. uint32_t * const p_result_buf_len,
  108. nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc)
  109. {
  110. uint32_t max_rec_num;
  111. uint32_t memory_last;
  112. uint8_t * p_end;
  113. nfc_ndef_record_desc_t * * pp_record_desc_array;
  114. if (*p_result_buf_len < sizeof(parsed_ndef_msg_1_t))
  115. {
  116. return NRF_ERROR_NO_MEM;
  117. }
  118. memory_last = (*p_result_buf_len) - sizeof(parsed_ndef_msg_1_t);
  119. max_rec_num = (memory_last / (NFC_PARSER_M_DELTA)) + 1;
  120. p_parser_memo_desc->p_msg_desc = (nfc_ndef_msg_desc_t *) p_result_buf;
  121. pp_record_desc_array =
  122. (nfc_ndef_record_desc_t * *) &p_parser_memo_desc->p_msg_desc[1];
  123. p_parser_memo_desc->p_bin_pay_desc =
  124. (nfc_ndef_bin_payload_desc_t *) &pp_record_desc_array[max_rec_num];
  125. p_parser_memo_desc->p_rec_desc =
  126. (nfc_ndef_record_desc_t *) &p_parser_memo_desc->p_bin_pay_desc[max_rec_num];
  127. // initialize message description
  128. p_parser_memo_desc->p_msg_desc->pp_record = pp_record_desc_array;
  129. p_parser_memo_desc->p_msg_desc->max_record_count = max_rec_num;
  130. p_parser_memo_desc->p_msg_desc->record_count = 0;
  131. p_end = (uint8_t *) &p_parser_memo_desc->p_rec_desc[max_rec_num];
  132. *p_result_buf_len = p_end - p_result_buf;
  133. return NRF_SUCCESS;
  134. }
  135. #endif // NRF_MODULE_ENABLED(NFC_NDEF_MSG_PARSER)