nfc_ndef_msg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright (c) 2015 - 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)
  42. #include "app_util.h"
  43. #include "nfc_ndef_msg.h"
  44. #include "nordic_common.h"
  45. #include "nrf.h"
  46. /**
  47. * @brief Resolve the value of record location flags of the NFC NDEF record within an NFC NDEF message.
  48. */
  49. __STATIC_INLINE nfc_ndef_record_location_t record_location_get(uint32_t index,
  50. uint32_t record_count)
  51. {
  52. nfc_ndef_record_location_t record_location;
  53. if (index == 0)
  54. {
  55. if (record_count == 1)
  56. {
  57. record_location = NDEF_LONE_RECORD;
  58. }
  59. else
  60. {
  61. record_location = NDEF_FIRST_RECORD;
  62. }
  63. }
  64. else if (record_count == index + 1)
  65. {
  66. record_location = NDEF_LAST_RECORD;
  67. }
  68. else
  69. {
  70. record_location = NDEF_MIDDLE_RECORD;
  71. }
  72. return record_location;
  73. }
  74. ret_code_t nfc_ndef_msg_encode(nfc_ndef_msg_desc_t const * p_ndef_msg_desc,
  75. uint8_t * p_msg_buffer,
  76. uint32_t * const p_msg_len)
  77. {
  78. nfc_ndef_record_location_t record_location;
  79. uint32_t temp_len;
  80. uint32_t i;
  81. uint32_t err_code;
  82. uint32_t sum_of_len = 0;
  83. if ((p_ndef_msg_desc == NULL) || p_msg_len == NULL)
  84. {
  85. return NRF_ERROR_NULL;
  86. }
  87. nfc_ndef_record_desc_t * * pp_record_rec_desc = p_ndef_msg_desc->pp_record;
  88. if (p_ndef_msg_desc->pp_record == NULL)
  89. {
  90. return NRF_ERROR_NULL;
  91. }
  92. #if NFC_NDEF_MSG_TAG_TYPE == TYPE_4_TAG
  93. uint8_t * p_root_msg_buffer = p_msg_buffer;
  94. if (p_msg_buffer != NULL)
  95. {
  96. if (*p_msg_len < NLEN_FIELD_SIZE)
  97. {
  98. return NRF_ERROR_NO_MEM;
  99. }
  100. p_msg_buffer += NLEN_FIELD_SIZE;
  101. }
  102. sum_of_len += NLEN_FIELD_SIZE;
  103. #endif
  104. for (i = 0; i < p_ndef_msg_desc->record_count; i++)
  105. {
  106. record_location = record_location_get(i, p_ndef_msg_desc->record_count);
  107. temp_len = *p_msg_len - sum_of_len;
  108. err_code = nfc_ndef_record_encode(*pp_record_rec_desc,
  109. record_location,
  110. p_msg_buffer,
  111. &temp_len);
  112. if (err_code != NRF_SUCCESS)
  113. {
  114. return err_code;
  115. }
  116. sum_of_len += temp_len;
  117. if (p_msg_buffer != NULL)
  118. {
  119. p_msg_buffer += temp_len;
  120. }
  121. /* next record */
  122. pp_record_rec_desc++;
  123. }
  124. #if NFC_NDEF_MSG_TAG_TYPE == TYPE_4_TAG
  125. if (p_msg_buffer != NULL)
  126. {
  127. if (sum_of_len - NLEN_FIELD_SIZE > UINT16_MAX)
  128. {
  129. return NRF_ERROR_NOT_SUPPORTED;
  130. }
  131. UNUSED_RETURN_VALUE(uint16_big_encode(sum_of_len - NLEN_FIELD_SIZE, p_root_msg_buffer));
  132. }
  133. #endif
  134. *p_msg_len = sum_of_len;
  135. return NRF_SUCCESS;
  136. }
  137. void nfc_ndef_msg_clear(nfc_ndef_msg_desc_t * p_msg)
  138. {
  139. p_msg->record_count = 0;
  140. }
  141. ret_code_t nfc_ndef_msg_record_add(nfc_ndef_msg_desc_t * const p_msg,
  142. nfc_ndef_record_desc_t * const p_record)
  143. {
  144. if (p_msg->record_count >= p_msg->max_record_count)
  145. {
  146. return NRF_ERROR_NO_MEM;
  147. }
  148. p_msg->pp_record[p_msg->record_count] = p_record;
  149. p_msg->record_count++;
  150. return NRF_SUCCESS;
  151. }
  152. #endif // NRF_MODULE_ENABLED(NFC_NDEF_MSG)