nfc_text_rec.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_text_rec.h"
  14. #include "nrf_error.h"
  15. #define TEXT_REC_STATUS_SIZE 1 ///< Size of the status.
  16. #define TEXT_REC_STATUS_UTF_POS 7 ///< Position of a character encoding type.
  17. #define TEXT_REC_RESERVED_POS 6 ///< Reserved position.
  18. const uint8_t nfc_text_rec_type_field[] = {'T'};
  19. /**
  20. * @brief Function for calculating payload size.
  21. */
  22. __STATIC_INLINE uint32_t nfc_text_rec_payload_size_get(nfc_text_rec_payload_desc_t * p_nfc_rec_text_payload_desc)
  23. {
  24. return (TEXT_REC_STATUS_SIZE
  25. + p_nfc_rec_text_payload_desc->lang_code_len
  26. + p_nfc_rec_text_payload_desc->data_len);
  27. }
  28. ret_code_t nfc_text_rec_payload_constructor(nfc_text_rec_payload_desc_t * p_nfc_rec_text_payload_desc,
  29. uint8_t * p_buff,
  30. uint32_t * p_len)
  31. {
  32. if ((p_nfc_rec_text_payload_desc->lang_code_len == 0)
  33. || (p_nfc_rec_text_payload_desc->lang_code_len & (1 << TEXT_REC_RESERVED_POS))
  34. || (p_nfc_rec_text_payload_desc->lang_code_len & (1 << TEXT_REC_STATUS_UTF_POS))
  35. || (p_nfc_rec_text_payload_desc->p_lang_code == NULL)
  36. || (p_nfc_rec_text_payload_desc->data_len == 0)
  37. || (p_nfc_rec_text_payload_desc->p_data == NULL)
  38. || (p_buff == NULL)
  39. || (p_len == NULL))
  40. {
  41. return NRF_ERROR_INVALID_PARAM;
  42. }
  43. uint32_t payload_size = nfc_text_rec_payload_size_get(p_nfc_rec_text_payload_desc);
  44. if (payload_size > *p_len)
  45. {
  46. return NRF_ERROR_NO_MEM;
  47. }
  48. *p_buff = (p_nfc_rec_text_payload_desc->lang_code_len
  49. + (p_nfc_rec_text_payload_desc->utf << TEXT_REC_STATUS_UTF_POS));
  50. p_buff += TEXT_REC_STATUS_SIZE;
  51. memcpy(p_buff,
  52. p_nfc_rec_text_payload_desc->p_lang_code,
  53. p_nfc_rec_text_payload_desc->lang_code_len);
  54. p_buff += p_nfc_rec_text_payload_desc->lang_code_len;
  55. memcpy(p_buff,
  56. p_nfc_rec_text_payload_desc->p_data,
  57. p_nfc_rec_text_payload_desc->data_len);
  58. *p_len = payload_size;
  59. return NRF_SUCCESS;
  60. }