nfc_launchapp_msg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <stdint.h>
  13. #include <string.h>
  14. #include "nfc_launchapp_rec.h"
  15. #include "nfc_launchapp_msg.h"
  16. #include "nrf_error.h"
  17. /** @brief Function for generating a description of an NFC NDEF launch application message.
  18. *
  19. * This function declares and initializes a static instance of an NFC NDEF message description
  20. * and the NFC NDEF record descriptions that are referenced by this message description.
  21. *
  22. * @param[in] p_android_package_name Pointer to the Android package name string.
  23. * If NULL, the Android Application Record will be skipped.
  24. * @param[in] android_package_name_length Length of the Android package name.
  25. * @param[in] p_win_app_id Pointer to the Windows application ID string (GUID).
  26. * If NULL, the Windows LaunchApp Record will be skipped.
  27. * @param[in] win_app_id_length Length of the Windows application ID.
  28. * @param[out] pp_launchapp_msg_desc Pointer to pointer to the NDEF message description.
  29. *
  30. * @retval NRF_SUCCESS If the description was successfully created.
  31. * @retval NRF_ERROR_INVALID_PARAM If both p_android_package_name and windows_application_id were
  32. * invalid (equal to NULL).
  33. */
  34. __STATIC_INLINE ret_code_t nfc_launchapp_msg_declare(uint8_t const * p_android_package_name,
  35. uint8_t android_package_name_length,
  36. uint8_t const * p_win_app_id,
  37. uint8_t win_app_id_length,
  38. nfc_ndef_msg_desc_t ** pp_launchapp_msg_desc)
  39. {
  40. uint32_t err_code;
  41. nfc_ndef_record_desc_t * p_win_rec, * p_android_rec;
  42. /* Create NFC NDEF message description, capacity - 2 records */
  43. NFC_NDEF_MSG_DEF(nfc_launchapp_msg, 2);
  44. /* The message description is static, therefore you must */
  45. /* clear the message (needed for supporting multiple calls). */
  46. nfc_ndef_msg_clear(&NFC_NDEF_MSG(nfc_launchapp_msg));
  47. if (p_win_app_id != NULL)
  48. {
  49. /* Create NFC NDEF Windows Phone LaunchApp Record description */
  50. p_win_rec = nfc_windows_launchapp_rec_declare(p_win_app_id,
  51. win_app_id_length);
  52. /* Add Windows LaunchApp record as first record to message */
  53. err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_launchapp_msg), p_win_rec);
  54. if (err_code != NRF_SUCCESS)
  55. return err_code;
  56. }
  57. if (p_android_package_name != NULL)
  58. {
  59. /* Create NFC NDEF Android Application Record description */
  60. p_android_rec = nfc_android_application_rec_declare(p_android_package_name,
  61. android_package_name_length);
  62. /* Add Android App Record as second record to message */
  63. err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_launchapp_msg), p_android_rec);
  64. if (err_code != NRF_SUCCESS)
  65. return err_code;
  66. }
  67. if (NFC_NDEF_MSG(nfc_launchapp_msg).record_count == 0)
  68. {
  69. return NRF_ERROR_INVALID_PARAM;
  70. }
  71. *pp_launchapp_msg_desc = &NFC_NDEF_MSG(nfc_launchapp_msg);
  72. return NRF_SUCCESS;
  73. }
  74. ret_code_t nfc_launchapp_msg_encode(uint8_t const * p_android_package_name,
  75. uint8_t android_package_name_length,
  76. uint8_t const * p_win_app_id,
  77. uint8_t win_app_id_length,
  78. uint8_t * p_buf,
  79. uint32_t * p_len)
  80. {
  81. nfc_ndef_msg_desc_t * p_launchapp_msg_desc;
  82. ret_code_t err_code;
  83. err_code = nfc_launchapp_msg_declare(p_android_package_name,
  84. android_package_name_length,
  85. p_win_app_id,
  86. win_app_id_length,
  87. &p_launchapp_msg_desc);
  88. if (err_code != NRF_SUCCESS)
  89. return err_code;
  90. /* Encode whole message into buffer */
  91. err_code = nfc_ndef_msg_encode(p_launchapp_msg_desc,
  92. p_buf,
  93. p_len);
  94. return err_code;
  95. }