coap_message.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Copyright (c) 2014 - 2019, 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. /** @file coap_message.h
  41. *
  42. * @defgroup iot_sdk_coap_msg CoAP Message
  43. * @ingroup iot_sdk_coap
  44. * @{
  45. * @brief TODO.
  46. */
  47. #ifndef COAP_MESSAGE_H__
  48. #define COAP_MESSAGE_H__
  49. #include <stdint.h>
  50. #include "coap_api.h"
  51. #include "coap_codes.h"
  52. #include "coap_transport.h"
  53. #include "coap_option.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #define COAP_PAYLOAD_MARKER 0xFF
  58. /**@brief Create a new CoAP message.
  59. *
  60. * @details The function will allocate memory for the message internally and return
  61. * a CoAP message structure. The callback provided will be called if a matching
  62. * message ID/Token occurs in a response message.
  63. * @param[out] p_message Pointer to set the generated coap_message_t structure to.
  64. * @param[in] p_init_config Initial configuration parameters of the message to generate.
  65. *
  66. * @retval NRF_SUCCESS If memory for the new message was allocated and the
  67. * initialization of the message went successfully.
  68. * @retval NRF_ERROR_NULL Either the message or init_config parameter was NULL.
  69. * @retval NRF_ERROR_INVALID_PARAM If port number in the port field is set to 0.
  70. */
  71. uint32_t coap_message_create(coap_message_t * p_message, coap_message_conf_t * p_init_config);
  72. /**@brief Decode a message from a raw buffer.
  73. *
  74. * @details When the underlying transport layer receives a message, it has to
  75. * be decoded into a CoAP message type structure. This functions returns
  76. * a decoded message if decoding was successfully, or NULL otherwise.
  77. *
  78. * @param[out] p_message The generated coap_message_t after decoding the raw message.
  79. * @param[in] p_raw_message Pointer to the encoded message memory buffer.
  80. * @param[in] message_len Length of the p_raw_message.
  81. *
  82. * @retval NRF_SUCCESS If the decoding of the message succeeds.
  83. * @retval NRF_ERROR_NULL If pointer to the p_message or p_raw_message were NULL.
  84. * @retval NRF_ERROR_INVALID_LENGTH If the message is less than 4 bytes, not containing a
  85. * full header.
  86. * @retval COAP_MESSAGE_INVALID_CONTENT If the message could not be decoded successfully. This
  87. * could happen if message length provided is larger than
  88. * what is possible to decode (ex. missing payload marker).
  89. *
  90. */
  91. uint32_t coap_message_decode(coap_message_t * p_message,
  92. const uint8_t * p_raw_message,
  93. uint16_t message_len);
  94. /**@brief Encode a CoAP message into a byte buffer.
  95. *
  96. * @details This functions has two operations. One is the actual encoding into a
  97. * byte buffer. The other is to query the size of a potential encoding.
  98. * If p_buffer variable is omitted, the return value will be the size of a
  99. * potential serialized message. This can be used to get some persistent memory from
  100. * transport layer. The message have to be kept until all potential
  101. * retransmissions has been attempted.
  102. *
  103. * The p_message can be deleted after this point if the function succeeds.
  104. *
  105. * @param[in] p_message Message to encode.
  106. * @param[in] p_buffer Pointer to the byte buffer where to put the encoded message.
  107. * @param[inout] p_length Length of the provided byte buffer passed in by reference.
  108. * If the value 0 is supplied, the encoding will not take place,
  109. * but only the dry run calculating the expected length of the
  110. * encoded message.
  111. *
  112. * @retval NRF_SUCCESS If the encoding of the message succeeds.
  113. * @retval NRF_ERROR_NULL If message or length parameter is NULL pointer.
  114. * @retval NRF_ERROR_NO_MEM If the provided buffer is not sufficient for
  115. * the encoded message.
  116. * @retval COAP_MESSAGE_ERROR_NULL If the message has indicated the length of data,
  117. * but memory pointer is NULL.
  118. */
  119. uint32_t coap_message_encode(coap_message_t * p_message,
  120. uint8_t * p_buffer,
  121. uint16_t * p_length);
  122. /**@brief Get the content format mask of the message.
  123. *
  124. * @param[in] p_message Pointer to the message which to generate the content format mask from.
  125. * Should not be NULL.
  126. * @param[out] p_mask Value by reference to the variable to fill the result mask into.
  127. *
  128. * @retval NRF_SUCCESS If the mask could be generated.
  129. * @retval NRF_ERROR_NULL If the message pointer or the mask pointer given was NULL.
  130. */
  131. uint32_t coap_message_ct_mask_get(coap_message_t * p_message, uint32_t * p_mask);
  132. /**@brief Get the accept mask of the message.
  133. *
  134. * @param[in] p_message Pointer to the message which to generate the accept mask from.
  135. * Should not be NULL.
  136. * @param[out] p_mask Value by reference to the variable to fill the result mask into.
  137. *
  138. * @retval NRF_SUCCESS If the mask could be generated.
  139. * @retval NRF_ERROR_NULL If the message pointer or the mask pointer given was NULL.
  140. */
  141. uint32_t coap_message_accept_mask_get(coap_message_t * p_message, uint32_t * p_mask);
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif // COAP_MESSAGE_H__
  146. /** @} */