coap_queue.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Copyright (c) 2014 - 2018, 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 <string.h>
  41. #include "coap_queue.h"
  42. #include "iot_common.h"
  43. #include "sdk_config.h"
  44. #if (COAP_DISABLE_API_PARAM_CHECK == 0)
  45. /**@brief Verify NULL parameters are not passed to API by application. */
  46. #define NULL_PARAM_CHECK(PARAM) \
  47. if ((PARAM) == NULL) \
  48. { \
  49. return (NRF_ERROR_NULL | IOT_COAP_ERR_BASE); \
  50. }
  51. #else
  52. #define NULL_PARAM_CHECK(PARAM)
  53. #endif // COAP_DISABLE_API_PARAM_CHECK
  54. static coap_queue_item_t m_queue[COAP_MESSAGE_QUEUE_SIZE];
  55. static uint8_t m_message_queue_count = 0;
  56. uint32_t coap_queue_init(void)
  57. {
  58. for (uint8_t i = 0; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  59. {
  60. memset(&m_queue[i], 0, sizeof(coap_queue_item_t));
  61. m_queue[i].handle = i;
  62. }
  63. m_message_queue_count = 0;
  64. return NRF_SUCCESS;
  65. }
  66. uint32_t coap_queue_add(coap_queue_item_t * item)
  67. {
  68. NULL_PARAM_CHECK(item);
  69. if (m_message_queue_count >= COAP_MESSAGE_QUEUE_SIZE)
  70. {
  71. return (NRF_ERROR_NO_MEM | IOT_COAP_ERR_BASE);
  72. }
  73. else
  74. {
  75. for (uint8_t i = 0; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  76. {
  77. if (m_queue[i].p_buffer == NULL)
  78. {
  79. // Free spot in message queue. Add message here...
  80. memcpy(&m_queue[i], item, sizeof(coap_queue_item_t));
  81. m_message_queue_count++;
  82. return NRF_SUCCESS;
  83. }
  84. }
  85. }
  86. return (NRF_ERROR_DATA_SIZE | IOT_COAP_ERR_BASE);
  87. }
  88. uint32_t coap_queue_remove(coap_queue_item_t * p_item)
  89. {
  90. for (uint8_t i = 0; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  91. {
  92. if (p_item == (coap_queue_item_t *)&m_queue[i])
  93. {
  94. memset(&m_queue[i], 0, sizeof(coap_queue_item_t));
  95. m_message_queue_count--;
  96. return NRF_SUCCESS;
  97. }
  98. }
  99. return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
  100. }
  101. uint32_t coap_queue_item_by_token_get(coap_queue_item_t ** pp_item, uint8_t * p_token, uint8_t token_len)
  102. {
  103. for (uint8_t i = 0; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  104. {
  105. if (m_queue[i].token_len == token_len)
  106. {
  107. if ((0 != m_queue[i].token_len) &&
  108. (memcmp(m_queue[i].token, p_token, m_queue[i].token_len) == 0))
  109. {
  110. *pp_item = &m_queue[i];
  111. return NRF_SUCCESS;
  112. }
  113. }
  114. }
  115. return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
  116. }
  117. uint32_t coap_queue_item_by_mid_get(coap_queue_item_t ** pp_item, uint16_t message_id)
  118. {
  119. for (uint8_t i = 0; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  120. {
  121. if (m_queue[i].mid == message_id)
  122. {
  123. *pp_item = &m_queue[i];
  124. return NRF_SUCCESS;
  125. }
  126. }
  127. return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
  128. }
  129. uint32_t coap_queue_item_next_get(coap_queue_item_t ** pp_item, coap_queue_item_t * p_item)
  130. {
  131. if (p_item == NULL)
  132. {
  133. for (uint8_t i = 0; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  134. {
  135. if (m_queue[i].p_buffer != NULL)
  136. {
  137. (*pp_item) = &m_queue[i];
  138. return NRF_SUCCESS;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. uint8_t index_to_previous = (uint8_t)(((uint32_t)p_item - (uint32_t)m_queue) / (uint32_t)sizeof(coap_queue_item_t));
  145. for (uint8_t i = index_to_previous + 1; i < COAP_MESSAGE_QUEUE_SIZE; i++)
  146. {
  147. if (m_queue[i].p_buffer != NULL)
  148. {
  149. (*pp_item) = &m_queue[i];
  150. return NRF_SUCCESS;
  151. }
  152. }
  153. }
  154. (*pp_item) = NULL;
  155. return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
  156. }