lwm2m_tlv.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Copyright (c) 2015 - 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 lwm2m_tlv.h
  41. *
  42. * @defgroup iot_sdk_lwm2m_tlv_api LWM2M TLV interface
  43. * @ingroup iot_sdk_lwm2m
  44. * @{
  45. * @brief TLV encoding and decoding interface for the LWM2M protocol.
  46. */
  47. #ifndef LWM2M_TLV_H__
  48. #define LWM2M_TLV_H__
  49. #include <stdint.h>
  50. #include "lwm2m_objects.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**
  55. * TLV type masks
  56. */
  57. #define TLV_TYPE_BIT_POS 6
  58. #define TLV_ID_LEN_BIT_POS 5
  59. #define TLV_LEN_TYPE_BIT_POS 3
  60. #define TLV_VAL_LEN_BIT_POS 0
  61. #define TLV_TYPE_MASK (0x3 << TLV_TYPE_BIT_POS) /**< Type bitmask, bit 7-6 (0b11000000). */
  62. #define TLV_ID_LEN_MASK (0x1 << TLV_ID_LEN_BIT_POS) /**< Length bitmask, bit 5 (0b00100000). */
  63. #define TLV_LEN_TYPE_MASK (0x3 << TLV_LEN_TYPE_BIT_POS) /**< Length type bitmask, bit 4-3 (0b00011000). */
  64. #define TLV_LEN_VAL_MASK (0x7 << TLV_VAL_LEN_BIT_POS) /**< Length of the value bitmask, bit 2-0 (0b00000111). */
  65. #define TLV_TYPE_OBJECT 0x00
  66. #define TLV_TYPE_RESOURCE_INSTANCE 0x01
  67. #define TLV_TYPE_MULTI_RESOURCE 0x02
  68. #define TLV_TYPE_RESOURCE_VAL 0x03
  69. #define TLV_ID_LEN_8BIT 0x00
  70. #define TLV_ID_LEN_16BIT 0x01
  71. #define TLV_LEN_TYPE_3BIT 0x00
  72. #define TLV_LEN_TYPE_8BIT 0x01
  73. #define TLV_LEN_TYPE_16BIT 0x02
  74. #define TLV_LEN_TYPE_24BIT 0x03
  75. typedef struct
  76. {
  77. uint16_t id_type; /**< Identifier type. */
  78. uint16_t id; /**< Identifier ID. */
  79. uint32_t length; /**< Length of the value in the TLV. */
  80. uint8_t * value; /**< Value of the TLV. */
  81. } lwm2m_tlv_t;
  82. /**@brief Decode a LWM2M TLV byte buffer into a TLV structure.
  83. *
  84. * @param[out] p_tlv This struct will be filled with id, length, type and pointer to value.
  85. * @param[inout] p_index Index to start decoding from.
  86. * @param[in] p_buffer The buffer to decode from.
  87. * @param[in] buffer_len The length of the buffer.
  88. *
  89. * @retval NRF_SUCCESS If decoding was successful.
  90. * @retval IOT_LWM2M_ERR_BASE | NRF_INVALID_DATA
  91. */
  92. uint32_t lwm2m_tlv_decode(lwm2m_tlv_t * p_tlv, uint32_t * p_index, uint8_t * p_buffer, uint16_t buffer_len);
  93. /**@brief Encode a TLV structure into a LWM2M TLV byte buffer.
  94. *
  95. * @details Encode using the provided tlv, if the buffer provided is to small an NRF_ERROR_DATA_SIZE will be returned.
  96. *
  97. * Maximum buffer size requirement: value_length + 6 (1 for type byte, 2 for id bytes, 3 for length bytes).
  98. *
  99. * @param[out] p_buffer Buffer to put the encoded tlv into.
  100. * @param[inout] buffer_len Length of input buffer out: length of the encoded buffer.
  101. * @param[in] p_tlv The tlv to use.
  102. *
  103. * @retval NRF_SUCCESS If decoding was successful.
  104. * @retval IOT_LWM2M_ERR_BASE | NRF_ERROR_DATA_SIZE
  105. */
  106. uint32_t lwm2m_tlv_encode(uint8_t * p_buffer, uint32_t * p_buffer_len, lwm2m_tlv_t * p_tlv);
  107. /**@brief Encode a byte buffer into a uint32_t.
  108. *
  109. * @param[in] p_buffer Buffer which holds a serialized version of the uint32_t.
  110. * @param[in] val_len Length of the value in the buffer.
  111. * @param[out] p_result By reference pointer to the result uint32_t.
  112. *
  113. * @retval NRF_SUCCESS If the conversion from byte buffer to uint32_t value was successful.
  114. */
  115. uint32_t lwm2m_tlv_bytebuffer_to_uint32(uint8_t * p_buffer, uint8_t val_len, uint32_t * p_result);
  116. /**@brief Encode a byte buffer into a uint16_t.
  117. *
  118. * @param[in] p_buffer Buffer which holds a serialized version of the uint16_t.
  119. * @param[in] val_len Length of the value in the buffer.
  120. * @param[out] p_result By reference pointer to the result uint16_t.
  121. *
  122. * @retval NRF_SUCCESS If the conversion from byte buffer to uint32_t value was successful.
  123. */
  124. uint32_t lwm2m_tlv_bytebuffer_to_uint16(uint8_t * p_buffer, uint8_t val_len, uint16_t * p_result);
  125. /**@brief Decode a uint32_t into a byte buffer.
  126. *
  127. * @param[out] p_buffer Buffer which holds a serialized version of the uint32_t.
  128. * @param[out] p_len By reference pointer to hold the length of the serialized value in the buffer.
  129. * @param[in] value Value to convert serialize into a byte buffer.
  130. *
  131. * @retval NRF_SUCCESS If the conversion from uint32_t value to byte buffer was successful.
  132. */
  133. void lwm2m_tlv_uint32_to_bytebuffer(uint8_t * p_buffer, uint8_t * p_len, uint32_t value);
  134. /**@brief Decode a uint16_t into a byte buffer.
  135. *
  136. * @param[out] p_buffer Buffer which holds a serialized version of the uint16_t.
  137. * @param[out] p_len By reference pointer to hold the length of the serialized value in the buffer.
  138. * @param[in] value Value to convert serialize into a byte buffer.
  139. *
  140. * @retval NRF_SUCCESS If the conversion from uint16_t value to byte buffer was successful.
  141. */
  142. void lwm2m_tlv_uint16_to_bytebuffer(uint8_t * p_buffer, uint8_t * p_len, uint16_t value);
  143. /**@brief Set a uint32_t value to a lwm2m_tlv_t structure.
  144. *
  145. * @param[out] p_tlv TLV containing the uint32_t tlv.
  146. * @param[in] value Value to set.
  147. * @param[in] id Resource Id associated with the value.
  148. */
  149. void lwm2m_tlv_uint32_set(lwm2m_tlv_t * p_tlv, uint32_t value, uint16_t id);
  150. /**@brief Set a uint16_t value to a lwm2m_tlv_t structure.
  151. *
  152. * @param[out] p_tlv TLV containing the uint16_t tlv.
  153. * @param[in] value Value to set.
  154. * @param[in] id Resource Id associated with the value.
  155. */
  156. void lwm2m_tlv_uint16_set(lwm2m_tlv_t * p_tlv, uint16_t value, uint16_t id);
  157. /**@brief Set a boolean value to a lwm2m_tlv_t structure.
  158. *
  159. * @param[out] p_tlv TLV containing the boolean tlv.
  160. * @param[in] value Value to set.
  161. * @param[in] id Resource Id associated with the value.
  162. */
  163. void lwm2m_tlv_bool_set(lwm2m_tlv_t * p_tlv, bool value, uint16_t id);
  164. /**@brief Set a string value to a lwm2m_tlv_t structure.
  165. *
  166. * @param[out] p_tlv TLV containing the string tlv.
  167. * @param[in] value Value to set.
  168. * @param[in] id Resource Id associated with the value.
  169. */
  170. void lwm2m_tlv_string_set(lwm2m_tlv_t * p_tlv, lwm2m_string_t string, uint16_t id);
  171. /**@brief Set a opaque value to a lwm2m_tlv_t structure.
  172. *
  173. * @param[out] p_tlv TLV containing the opaque tlv.
  174. * @param[in] value Value to set.
  175. * @param[in] id Resource Id associated with the value.
  176. */
  177. void lwm2m_tlv_opaque_set(lwm2m_tlv_t * p_tlv, lwm2m_opaque_t opaque, uint16_t id);
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif // LWM2M_TLV_H__
  182. /** @} */