coap_block.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include <stdbool.h>
  41. #include <stddef.h>
  42. #include "coap_block.h"
  43. #include "nrf_error.h"
  44. #include "iot_errors.h"
  45. #include "sdk_config.h"
  46. /**
  47. * @defgroup api_param_check API Parameters check macros.
  48. *
  49. * @details Macros that verify parameters passed to the module in the APIs. These macros
  50. * could be mapped to nothing in final versions of code to save execution and size.
  51. * COAP_DISABLE_API_PARAM_CHECK should be defined to disable these checks.
  52. *
  53. * @{
  54. */
  55. #if (COAP_DISABLE_API_PARAM_CHECK == 0)
  56. /**@brief Verify NULL parameters are not passed to API by application. */
  57. #define NULL_PARAM_CHECK(PARAM) \
  58. if ((PARAM) == NULL) \
  59. { \
  60. return (NRF_ERROR_NULL | IOT_COAP_ERR_BASE); \
  61. }
  62. #else // COAP_DISABLE_API_PARAM_CHECK
  63. #define NULL_PARAM_CHECK(PARAM)
  64. #endif // COAP_DISABLE_API_PARAM_CHECK
  65. /** @} */
  66. #define BLOCK_SIZE_BASE_CONSTANT 4 /**< Block size base exponent. 4 means a base block size of 2^4 = 16 bytes. */
  67. #define BLOCK_SIZE_16 0 /**< Block size of 2^(4+0) = 16 bytes. */
  68. #define BLOCK_SIZE_32 1 /**< Block size of 2^(4+1) = 32 bytes. */
  69. #define BLOCK_SIZE_64 2 /**< Block size of 2^(4+2) = 64 bytes. */
  70. #define BLOCK_SIZE_128 3 /**< Block size of 2^(4+3) = 128 bytes. */
  71. #define BLOCK_SIZE_256 4 /**< Block size of 2^(4+4) = 256 bytes. */
  72. #define BLOCK_SIZE_512 5 /**< Block size of 2^(4+5) = 512 bytes. */
  73. #define BLOCK_SIZE_1024 6 /**< Block size of 2^(4+6) = 1024 bytes. */
  74. #define BLOCK_SIZE_2048_RESERVED 7 /**< Reserved. */
  75. #define BLOCK_MORE_BIT_UNSET 0 /**< Value when more flag is set. */
  76. #define BLOCK_MORE_BIT_SET 1 /**< Value when more flag is not set. */
  77. #define BLOCK_SIZE_POS 0 /**< Bit offset to the size. */
  78. #define BLOCK_MORE_BIT_POS 3 /**< Bit offset to the more bit. */
  79. #define BLOCK_NUMBER_POS 4 /**< Bit offset to the block number. */
  80. #define BLOCK_SIZE_MASK 0x7 /**< Block size mask. */
  81. #define BLOCK_MORE_BIT_MASK (1 << BLOCK_MORE_BIT_POS) /**< More bit mask. */
  82. #define BLOCK_NUMBER_MASK (0xFFFFF << 4) /**< Block number mask. */
  83. #define BLOCK_SIZE_MAX 0x7 /**< Maximum block size number. */
  84. #define BLOCK_MORE_BIT_MAX BLOCK_MORE_BIT_SET /**< Maximum more bit value. */
  85. #define BLOCK_NUMBER_MAX 0xFFFFF /**< Maximum block number. 20 bits max value is (1 << 20) - 1. */
  86. static uint32_t block_opt_encode(uint8_t more,
  87. uint16_t size,
  88. uint32_t number,
  89. uint32_t * p_encoded)
  90. {
  91. if ((number > BLOCK_NUMBER_MAX) || (more > BLOCK_MORE_BIT_MAX))
  92. {
  93. return (NRF_ERROR_INVALID_PARAM | IOT_COAP_ERR_BASE);
  94. }
  95. uint32_t val = 0;
  96. switch (size)
  97. {
  98. case 16:
  99. val = BLOCK_SIZE_16;
  100. break;
  101. case 32:
  102. val = BLOCK_SIZE_32;
  103. break;
  104. case 64:
  105. val = BLOCK_SIZE_64;
  106. break;
  107. case 128:
  108. val = BLOCK_SIZE_128;
  109. break;
  110. case 256:
  111. val = BLOCK_SIZE_256;
  112. break;
  113. case 512:
  114. val = BLOCK_SIZE_512;
  115. break;
  116. case 1024:
  117. val = BLOCK_SIZE_1024;
  118. break;
  119. case 2048:
  120. // Falltrough.
  121. default:
  122. // Break omitted.
  123. return (NRF_ERROR_INVALID_PARAM | IOT_COAP_ERR_BASE);
  124. }
  125. // Value has been initialized.
  126. val |= (more) << BLOCK_MORE_BIT_POS;
  127. val |= (number) << BLOCK_NUMBER_POS;
  128. *p_encoded = val;
  129. return NRF_SUCCESS;
  130. }
  131. static uint32_t block_opt_decode(uint32_t encoded,
  132. uint8_t * p_more,
  133. uint16_t * p_size,
  134. uint32_t * p_number)
  135. {
  136. if ((encoded & BLOCK_SIZE_MASK) == BLOCK_SIZE_2048_RESERVED)
  137. {
  138. return (NRF_ERROR_INVALID_DATA | IOT_COAP_ERR_BASE);
  139. }
  140. if ((encoded >> BLOCK_NUMBER_POS) > BLOCK_NUMBER_MAX)
  141. {
  142. return (NRF_ERROR_INVALID_PARAM | IOT_COAP_ERR_BASE);
  143. }
  144. *p_size = (1 << ((BLOCK_SIZE_BASE_CONSTANT + (encoded & BLOCK_SIZE_MASK))));
  145. *p_more = (encoded & BLOCK_MORE_BIT_MASK) >> BLOCK_MORE_BIT_POS;
  146. *p_number = (encoded & BLOCK_NUMBER_MASK) >> BLOCK_NUMBER_POS;
  147. return NRF_SUCCESS;
  148. }
  149. uint32_t coap_block_opt_block1_encode(uint32_t * p_encoded, coap_block_opt_block1_t * p_opt)
  150. {
  151. NULL_PARAM_CHECK(p_encoded);
  152. NULL_PARAM_CHECK(p_opt);
  153. return block_opt_encode(p_opt->more, p_opt->size, p_opt->number, p_encoded);
  154. }
  155. uint32_t coap_block_opt_block1_decode(coap_block_opt_block1_t * p_opt, uint32_t encoded)
  156. {
  157. NULL_PARAM_CHECK(p_opt);
  158. return block_opt_decode(encoded, &p_opt->more, &p_opt->size, &p_opt->number);
  159. }
  160. uint32_t coap_block_opt_block2_encode(uint32_t * p_encoded, coap_block_opt_block2_t * p_opt)
  161. {
  162. NULL_PARAM_CHECK(p_encoded);
  163. NULL_PARAM_CHECK(p_opt);
  164. return block_opt_encode(p_opt->more, p_opt->size, p_opt->number, p_encoded);
  165. }
  166. uint32_t coap_block_opt_block2_decode(coap_block_opt_block2_t * p_opt, uint32_t encoded)
  167. {
  168. NULL_PARAM_CHECK(p_opt);
  169. return block_opt_decode(encoded, &p_opt->more, &p_opt->size, &p_opt->number);
  170. }