nfc_uri_rec.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_rec.h"
  14. #include "nrf_error.h"
  15. /**
  16. * @brief Type of description of the payload of a URI record.
  17. */
  18. typedef struct
  19. {
  20. nfc_uri_id_t uri_id_code; ///< URI identifier code.
  21. uint8_t const * p_uri_data; ///< Pointer to a URI string.
  22. uint8_t uri_data_len; ///< Length of the URI string.
  23. } uri_payload_desc_t;
  24. /**
  25. * @brief Function for constructing the payload for a URI record.
  26. *
  27. * This function encodes the payload according to the URI record definition. It implements an API
  28. * compatible with @ref p_payload_constructor_t.
  29. *
  30. * @param[in] p_input Pointer to the description of the payload.
  31. * @param[out] p_buff Pointer to payload destination.
  32. *
  33. * @param[in,out] p_len Size of available memory to write as input. Size of generated
  34. * payload as output.
  35. *
  36. * @retval NRF_SUCCESS If the payload was encoded successfully.
  37. * @retval NRF_ERROR_NO_MEM If the predicted payload size is bigger than the provided buffer space.
  38. */
  39. static ret_code_t nfc_uri_payload_constructor( uri_payload_desc_t * p_input,
  40. uint8_t * p_buff,
  41. uint32_t * p_len)
  42. {
  43. /* Verify if there is enough available memory */
  44. if(p_input->uri_data_len >= *p_len)
  45. {
  46. return NRF_ERROR_NO_MEM;
  47. }
  48. /* Copy descriptor content into the buffer */
  49. *p_len = p_input->uri_data_len + 1;
  50. *(p_buff++) = p_input->uri_id_code;
  51. memcpy(p_buff, p_input->p_uri_data, p_input->uri_data_len );
  52. return NRF_SUCCESS;
  53. }
  54. nfc_ndef_record_desc_t * nfc_uri_rec_declare( nfc_uri_id_t uri_id_code,
  55. uint8_t const * const p_uri_data,
  56. uint8_t uri_data_len)
  57. {
  58. static uri_payload_desc_t uri_payload_desc;
  59. static const uint8_t static_uri_type = 'U';
  60. NFC_NDEF_GENERIC_RECORD_DESC_DEF( uri_rec,
  61. TNF_WELL_KNOWN, // tnf <- well-known
  62. NULL,
  63. 0, // no id
  64. &static_uri_type,
  65. 1, // type size 1B
  66. nfc_uri_payload_constructor,
  67. &uri_payload_desc);
  68. uri_payload_desc.uri_id_code = uri_id_code;
  69. uri_payload_desc.p_uri_data = p_uri_data;
  70. uri_payload_desc.uri_data_len = uri_data_len;
  71. return &NFC_NDEF_GENERIC_RECORD_DESC( uri_rec);
  72. }