nfc_ac_rec.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_AC_REC)
  42. #include "nfc_ac_rec.h"
  43. #include <string.h>
  44. #include "nrf_error.h"
  45. #include "nrf.h"
  46. #define AC_REC_CPS_BYTE_SIZE 1 ///< Size of the field with CPS data.
  47. #define AC_REC_DATA_REF_LEN_SIZE 1 ///< Size of the Data Reference Length field.
  48. #define AC_REC_AUX_DATA_REF_COUNT_SIZE 1 ///< Size of the Data Reference Length field.
  49. const uint8_t nfc_ac_rec_type_field[2] = {'a', 'c'}; ///< Alternative Carrier Record type.
  50. /**
  51. * @brief Function for calculating the payload length of the NFC NDEF Alternative Carrier record.
  52. */
  53. static uint32_t nfc_ac_rec_payload_size_get(nfc_ac_rec_payload_desc_t const * p_ac_rec_payload_desc)
  54. {
  55. int32_t i = 0;
  56. // Initialize with size of byte with CPS.
  57. uint32_t payload_size = AC_REC_CPS_BYTE_SIZE;
  58. // Add Carrier Data Reference size.
  59. payload_size += p_ac_rec_payload_desc->carrier_data_ref.length + AC_REC_DATA_REF_LEN_SIZE;
  60. // Add Auxiliary Data Reference Count size.
  61. payload_size += AC_REC_AUX_DATA_REF_COUNT_SIZE;
  62. for (i = 0; i < p_ac_rec_payload_desc->aux_data_ref_count; i++)
  63. {
  64. // Add Auxiliary Data Reference size.
  65. payload_size += p_ac_rec_payload_desc->p_aux_data_ref[i].length + AC_REC_DATA_REF_LEN_SIZE;
  66. }
  67. return payload_size;
  68. }
  69. ret_code_t nfc_ac_rec_payload_constructor(nfc_ac_rec_payload_desc_t * p_nfc_rec_ac_payload_desc,
  70. uint8_t * p_buff,
  71. uint32_t * p_len)
  72. {
  73. int32_t i = 0;
  74. uint32_t payload_size = nfc_ac_rec_payload_size_get(p_nfc_rec_ac_payload_desc);
  75. if (p_buff != NULL)
  76. {
  77. // Not enough space in the buffer, return an error.
  78. if (payload_size > *p_len)
  79. {
  80. return NRF_ERROR_NO_MEM;
  81. }
  82. // Invalid CPS value.
  83. if ( p_nfc_rec_ac_payload_desc->cps & ~NFC_AC_CPS_MASK )
  84. {
  85. return NRF_ERROR_INVALID_PARAM;
  86. }
  87. // Copy CPS.
  88. *p_buff = p_nfc_rec_ac_payload_desc->cps;
  89. p_buff += AC_REC_CPS_BYTE_SIZE;
  90. // Copy Carrier Data Reference.
  91. *p_buff = p_nfc_rec_ac_payload_desc->carrier_data_ref.length;
  92. p_buff += AC_REC_DATA_REF_LEN_SIZE;
  93. memcpy( p_buff,
  94. p_nfc_rec_ac_payload_desc->carrier_data_ref.p_data,
  95. p_nfc_rec_ac_payload_desc->carrier_data_ref.length );
  96. p_buff += p_nfc_rec_ac_payload_desc->carrier_data_ref.length;
  97. // Copy Auxiliary Data Reference.
  98. *p_buff = p_nfc_rec_ac_payload_desc->aux_data_ref_count;
  99. p_buff += AC_REC_AUX_DATA_REF_COUNT_SIZE;
  100. for (i = 0; i < p_nfc_rec_ac_payload_desc->aux_data_ref_count; i++)
  101. {
  102. *p_buff = p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].length;
  103. p_buff += AC_REC_DATA_REF_LEN_SIZE;
  104. memcpy( p_buff,
  105. p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].p_data,
  106. p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].length );
  107. p_buff += p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].length;
  108. }
  109. }
  110. // Assign payload size to the return buffer.
  111. *p_len = payload_size;
  112. return NRF_SUCCESS;
  113. }
  114. void nfc_ac_rec_auxiliary_data_ref_clear(nfc_ndef_record_desc_t * p_ac_rec)
  115. {
  116. nfc_ac_rec_payload_desc_t * p_ac_rec_payload =
  117. (nfc_ac_rec_payload_desc_t*)p_ac_rec->p_payload_descriptor;
  118. p_ac_rec_payload->aux_data_ref_count = 0;
  119. }
  120. ret_code_t nfc_ac_rec_auxiliary_data_ref_add(nfc_ndef_record_desc_t * p_ac_rec,
  121. uint8_t * p_aux_data,
  122. uint8_t aux_length)
  123. {
  124. nfc_ac_rec_payload_desc_t * p_ac_rec_payload =
  125. (nfc_ac_rec_payload_desc_t *)p_ac_rec->p_payload_descriptor;
  126. if (p_ac_rec_payload->aux_data_ref_count >= p_ac_rec_payload->max_aux_data_ref)
  127. {
  128. return NRF_ERROR_NO_MEM;
  129. }
  130. p_ac_rec_payload->p_aux_data_ref[p_ac_rec_payload->aux_data_ref_count].p_data = p_aux_data;
  131. p_ac_rec_payload->p_aux_data_ref[p_ac_rec_payload->aux_data_ref_count].length = aux_length;
  132. p_ac_rec_payload->aux_data_ref_count++;
  133. return NRF_SUCCESS;
  134. }
  135. #endif // NRF_MODULE_ENABLED(NFC_AC_REC)