coap_option.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_option.h
  41. *
  42. * @defgroup iot_sdk_coap_option CoAP Option
  43. * @ingroup iot_sdk_coap
  44. * @{
  45. * @brief Nordic's CoAP Option APIs.
  46. */
  47. #ifndef COAP_OPTION_H__
  48. #define COAP_OPTION_H__
  49. /*
  50. +-----+---+---+---+---+----------------+--------+--------+----------+
  51. | No. | C | U | N | R | Name | Format | Length | Default |
  52. +-----+---+---+---+---+----------------+--------+--------+----------+
  53. | 1 | x | | | x | If-Match | opaque | 0-8 | (none) |
  54. | 3 | x | x | - | | Uri-Host | string | 1-255 | (see |
  55. | | | | | | | | | below) |
  56. | 4 | | | | x | ETag | opaque | 1-8 | (none) |
  57. | 5 | x | | | | If-None-Match | empty | 0 | (none) |
  58. | 7 | x | x | - | | Uri-Port | uint | 0-2 | (see |
  59. | | | | | | | | | below) |
  60. | 8 | | | | x | Location-Path | string | 0-255 | (none) |
  61. | 11 | x | x | - | x | Uri-Path | string | 0-255 | (none) |
  62. | 12 | | | | | Content-Format | uint | 0-2 | (none) |
  63. | 14 | | x | - | | Max-Age | uint | 0-4 | 60 |
  64. | 15 | x | x | - | x | Uri-Query | string | 0-255 | (none) |
  65. | 17 | x | | | | Accept | uint | 0-2 | (none) |
  66. | 20 | | | | x | Location-Query | string | 0-255 | (none) |
  67. | 23 | x | x | - | - | Block2 | uint | 0-3 | (none) |
  68. | 27 | x | x | - | - | Block1 | uint | 0-3 | (none) |
  69. | 28 | | | x | | Size2 | uint | 0-4 | (none) |
  70. | 35 | x | x | - | | Proxy-Uri | string | 1-1034 | (none) |
  71. | 39 | x | x | - | | Proxy-Scheme | string | 1-255 | (none) |
  72. | 60 | | | x | | Size1 | uint | 0-4 | (none) |
  73. +-----+---+---+---+---+----------------+--------+--------+----------+
  74. */
  75. #include <stdint.h>
  76. #include "coap_api.h"
  77. #include "nrf_error.h"
  78. #include "sdk_errors.h"
  79. #ifdef __cplusplus
  80. extern "C" {
  81. #endif
  82. typedef enum
  83. {
  84. COAP_OPT_FORMAT_EMPTY = 0,
  85. COAP_OPT_FORMAT_STRING = 1,
  86. COAP_OPT_FORMAT_OPAQUE = 2,
  87. COAP_OPT_FORMAT_UINT = 3
  88. } coap_opt_format_t;
  89. /**@brief Encode zero-terminated string into utf-8 encoded string.
  90. *
  91. * @param[out] p_encoded Pointer to buffer that will be used to fill the
  92. * encoded string into.
  93. * @param[inout] p_length Length of the buffer provided. Will also be used to
  94. * return the size of the used buffer.
  95. * @param[in] p_string String to encode.
  96. * @param[in] str_len Length of the string to encode.
  97. *
  98. * @retval NRF_SUCCESS Indicates that encoding was successful.
  99. * @retval NRF_ERROR_DATA_SIZE Indicates that the buffer provided was not sufficient to
  100. * successfully encode the data.
  101. */
  102. uint32_t coap_opt_string_encode(uint8_t * p_encoded, uint16_t * p_length, uint8_t * p_string, uint16_t str_len);
  103. /**@brief Decode a utf-8 string into a zero-terminated string.
  104. *
  105. * @param[out] p_string Pointer to the string buffer where the decoded
  106. * string will be placed.
  107. * @param[inout] p_length p_length of the encoded string. Returns the size of the buffer
  108. * used in bytes.
  109. * @param[in] p_encoded Buffer to decode.
  110. *
  111. * @retval NRF_SUCCESS Indicates that decoding was successful.
  112. * @retval NRF_ERROR_DATA_SIZE Indicates that the buffer provided was not sufficient to
  113. * successfully dencode the data.
  114. */
  115. uint32_t coap_opt_string_decode(uint8_t * p_string, uint16_t * p_length, uint8_t * p_encoded);
  116. /**@brief Encode a uint value into a uint8_t buffer in network byte order.
  117. *
  118. * @param[out] p_encoded Pointer to buffer that will be used to fill the
  119. * encoded uint into.
  120. * @param[inout] p_length Length of the buffer provided. Will also be used to
  121. * return the size of the used buffer.
  122. * @param[in] data uint value which could be anything from 1 to 4 bytes.
  123. *
  124. * @retval NRF_SUCCESS Indicates that encoding was successful.
  125. * @retval NRF_ERROR_DATA_SIZE Indicates that the buffer provided was not sufficient to
  126. * successfully encode the data.
  127. */
  128. uint32_t coap_opt_uint_encode(uint8_t * p_encoded, uint16_t * p_length, uint32_t data);
  129. /**@brief Decode a uint encoded value in network byte order to a uint32_t value.
  130. *
  131. * @param[out] p_data Pointer to the uint32_t value where the decoded uint will
  132. * be placed.
  133. * @param[inout] length Size of the encoded value.
  134. * @param[in] p_encoded uint value to be decoded into a uint32_t value.
  135. *
  136. * @retval NRF_SUCCESS Indicates that decoding was successful.
  137. * @retval NRF_ERROR_NULL If p_data or p_encoded pointer is NULL.
  138. * @retval NRF_ERROR_INVALID_LENGTH If buffer was greater than uint32_t.
  139. */
  140. uint32_t coap_opt_uint_decode(uint32_t * p_data, uint16_t length, uint8_t * p_encoded);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif // COAP_OPTION_H__
  145. /** @} */