nfc_uri_msg.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <string.h>
  13. #include "nfc_uri_msg.h"
  14. /** @brief Function for generating a description of an NFC NDEF URI message.
  15. *
  16. * This function declares and initializes a static instance of an NFC NDEF message description
  17. * and NFC NDEF records descriptions.
  18. *
  19. * @param[in] uri_id_code URI identifier code that defines the protocol field of the URI.
  20. * @param[in] p_uri_data Pointer to the URI string.
  21. * This string should not contain the protocol field if the protocol
  22. * was specified in @p uri_id_code
  23. * @param[in] uri_data_len Length of the URI string.
  24. * @param[out] pp_uri_msg_desc Pointer to pointer to the NDEF message description.
  25. *
  26. * @retval NRF_SUCCESS If the description was successfully created.
  27. * @retval NRF_ERROR_INVALID_PARAM If the URI string was invalid (equal to NULL).
  28. */
  29. static ret_code_t nfc_uri_msg_declare( nfc_uri_id_t uri_id_code,
  30. uint8_t const * const p_uri_data,
  31. uint8_t uri_data_len,
  32. nfc_ndef_msg_desc_t ** pp_uri_msg_desc)
  33. {
  34. ret_code_t err_code;
  35. nfc_ndef_record_desc_t * p_uri_rec;
  36. /* Create NFC NDEF message description, capacity - 1 record */
  37. NFC_NDEF_MSG_DEF(nfc_uri_msg, 1);
  38. /* The message description is static, therefore */
  39. /* you must clear the message (needed for supporting multiple calls) */
  40. nfc_ndef_msg_clear(&NFC_NDEF_MSG(nfc_uri_msg));
  41. if(p_uri_data != NULL)
  42. {
  43. /* Create NFC NDEF URI Record description */
  44. p_uri_rec = nfc_uri_rec_declare(uri_id_code,
  45. p_uri_data,
  46. uri_data_len);
  47. /* Add URI record as lone record to message */
  48. err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_uri_msg), p_uri_rec);
  49. if (err_code != NRF_SUCCESS)
  50. {
  51. return err_code;
  52. }
  53. }
  54. else
  55. {
  56. return NRF_ERROR_INVALID_PARAM;
  57. }
  58. *pp_uri_msg_desc = &NFC_NDEF_MSG(nfc_uri_msg);
  59. return NRF_SUCCESS;
  60. }
  61. ret_code_t nfc_uri_msg_encode( nfc_uri_id_t uri_id_code,
  62. uint8_t const * const p_uri_data,
  63. uint8_t uri_data_len,
  64. uint8_t * p_buf,
  65. uint32_t * p_len)
  66. {
  67. ret_code_t err_code;
  68. nfc_ndef_msg_desc_t * p_uri_msg_desc;
  69. /* Create NFC NDEF message description with URI record */
  70. err_code = nfc_uri_msg_declare( uri_id_code,
  71. p_uri_data,
  72. uri_data_len,
  73. &p_uri_msg_desc);
  74. if(err_code != NRF_SUCCESS)
  75. {
  76. return err_code;
  77. }
  78. /* Encode whole message into buffer */
  79. err_code = nfc_ndef_msg_encode(p_uri_msg_desc,
  80. p_buf,
  81. p_len);
  82. return err_code;
  83. }