coap_resource.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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_resource.h"
  42. #include "coap_api.h"
  43. #include "iot_common.h"
  44. #include "sdk_config.h"
  45. #define COAP_RESOURCE_MAX_AGE_INIFINITE 0xFFFFFFFF
  46. static coap_resource_t * mp_root_resource = NULL;
  47. static char m_scratch_buffer[(COAP_RESOURCE_MAX_NAME_LEN + 1) * COAP_RESOURCE_MAX_DEPTH + 6];
  48. #if (COAP_DISABLE_API_PARAM_CHECK == 0)
  49. /**@brief Verify NULL parameters are not passed to API by application. */
  50. #define NULL_PARAM_CHECK(PARAM) \
  51. if ((PARAM) == NULL) \
  52. { \
  53. return (NRF_ERROR_NULL | IOT_COAP_ERR_BASE); \
  54. }
  55. #else
  56. #define NULL_PARAM_CHECK(PARAM)
  57. #endif // COAP_DISABLE_API_PARAM_CHECK
  58. uint32_t coap_resource_init(void)
  59. {
  60. mp_root_resource = NULL;
  61. return NRF_SUCCESS;
  62. }
  63. uint32_t coap_resource_create(coap_resource_t * p_resource, const char * name)
  64. {
  65. NULL_PARAM_CHECK(p_resource);
  66. NULL_PARAM_CHECK(name);
  67. if (strlen(name) > COAP_RESOURCE_MAX_NAME_LEN)
  68. {
  69. return (NRF_ERROR_DATA_SIZE | IOT_COAP_ERR_BASE);
  70. }
  71. memcpy(p_resource->name, name, strlen(name));
  72. if (mp_root_resource == NULL)
  73. {
  74. mp_root_resource = p_resource;
  75. }
  76. p_resource->max_age = COAP_RESOURCE_MAX_AGE_INIFINITE;
  77. return NRF_SUCCESS;
  78. }
  79. uint32_t coap_resource_child_add(coap_resource_t * p_parent, coap_resource_t * p_child)
  80. {
  81. NULL_PARAM_CHECK(p_parent);
  82. NULL_PARAM_CHECK(p_child);
  83. if (p_parent->child_count == 0)
  84. {
  85. p_parent->p_front = p_child;
  86. p_parent->p_tail = p_child;
  87. }
  88. else
  89. {
  90. coap_resource_t * p_last_sibling = p_parent->p_tail;
  91. p_last_sibling->p_sibling = p_child;
  92. p_parent->p_tail = p_child;
  93. }
  94. p_parent->child_count++;
  95. return NRF_SUCCESS;
  96. }
  97. static uint32_t generate_path(uint16_t buffer_pos, coap_resource_t * p_current_resource, char * parent_path, uint8_t * string, uint16_t * length)
  98. {
  99. uint32_t err_code = NRF_SUCCESS;
  100. if (parent_path == NULL)
  101. {
  102. m_scratch_buffer[buffer_pos++] = '<';
  103. if (p_current_resource->p_front != NULL)
  104. {
  105. coap_resource_t * next_child = p_current_resource->p_front;
  106. do
  107. {
  108. err_code = generate_path(buffer_pos, next_child, m_scratch_buffer, string, length);
  109. if (err_code != NRF_SUCCESS)
  110. {
  111. return err_code;
  112. }
  113. next_child = next_child->p_sibling;
  114. } while (next_child != NULL);
  115. }
  116. }
  117. else
  118. {
  119. uint16_t size = strlen(p_current_resource->name);
  120. m_scratch_buffer[buffer_pos++] = '/';
  121. memcpy(&m_scratch_buffer[buffer_pos], p_current_resource->name, size);
  122. buffer_pos += size;
  123. if (p_current_resource->p_front != NULL)
  124. {
  125. coap_resource_t * next_child = p_current_resource->p_front;
  126. do
  127. {
  128. err_code = generate_path(buffer_pos, next_child, m_scratch_buffer, string, length);
  129. if (err_code != NRF_SUCCESS)
  130. {
  131. return err_code;
  132. }
  133. next_child = next_child->p_sibling;
  134. } while (next_child != NULL);
  135. }
  136. m_scratch_buffer[buffer_pos++] = '>';
  137. // If the resource is observable, append 'obs;' token.
  138. if ((p_current_resource->permission & COAP_PERM_OBSERVE) > 0)
  139. {
  140. memcpy(&m_scratch_buffer[buffer_pos], ";obs", 4);
  141. buffer_pos += 4;
  142. }
  143. m_scratch_buffer[buffer_pos++] = ',';
  144. if (buffer_pos <= (*length))
  145. {
  146. *length -= buffer_pos;
  147. memcpy(&string[strlen((char *)string)], m_scratch_buffer, buffer_pos);
  148. }
  149. else
  150. {
  151. return (NRF_ERROR_DATA_SIZE | IOT_COAP_ERR_BASE);
  152. }
  153. }
  154. return err_code;
  155. }
  156. uint32_t coap_resource_well_known_generate(uint8_t * string, uint16_t * length)
  157. {
  158. NULL_PARAM_CHECK(string);
  159. NULL_PARAM_CHECK(length);
  160. if (mp_root_resource == NULL)
  161. {
  162. return (NRF_ERROR_INVALID_STATE | IOT_COAP_ERR_BASE);
  163. }
  164. memset(string, 0, *length);
  165. uint32_t err_code = generate_path(0, mp_root_resource, NULL, string, length);
  166. string[strlen((char *)string) - 1] = '\0'; // remove the last comma
  167. return err_code;
  168. }
  169. static coap_resource_t * coap_resource_child_resolve(coap_resource_t * p_parent,
  170. char * p_path)
  171. {
  172. coap_resource_t * result = NULL;
  173. if (p_parent->p_front != NULL)
  174. {
  175. coap_resource_t * sibling_in_question = p_parent->p_front;
  176. do {
  177. // Check if the sibling name match.
  178. size_t size = strlen(sibling_in_question->name);
  179. if (strncmp(sibling_in_question->name, p_path, size) == 0)
  180. {
  181. return sibling_in_question;
  182. }
  183. else
  184. {
  185. sibling_in_question = sibling_in_question->p_sibling;
  186. }
  187. } while (sibling_in_question != NULL);
  188. }
  189. return result;
  190. }
  191. uint32_t coap_resource_get(coap_resource_t ** p_resource, uint8_t ** pp_uri_pointers, uint8_t num_of_uris)
  192. {
  193. if (mp_root_resource == NULL)
  194. {
  195. // Make sure pointer is set to NULL before returning.
  196. *p_resource = NULL;
  197. return (NRF_ERROR_INVALID_STATE | IOT_COAP_ERR_BASE);
  198. }
  199. coap_resource_t * p_current_resource = mp_root_resource;
  200. // Every node should start at root.
  201. for (uint8_t i = 0; i < num_of_uris; i++)
  202. {
  203. p_current_resource = coap_resource_child_resolve(p_current_resource, (char *)pp_uri_pointers[i]);
  204. if (p_current_resource == NULL)
  205. {
  206. // Stop looping as this direction will not give anything more.
  207. break;
  208. }
  209. }
  210. if (p_current_resource != NULL)
  211. {
  212. *p_resource = p_current_resource;
  213. return NRF_SUCCESS;
  214. }
  215. // If nothing has been found.
  216. *p_resource = NULL;
  217. return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
  218. }
  219. uint32_t coap_resource_root_get(coap_resource_t ** pp_resource)
  220. {
  221. NULL_PARAM_CHECK(pp_resource);
  222. if (mp_root_resource == NULL)
  223. {
  224. return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
  225. }
  226. *pp_resource = mp_root_resource;
  227. return NRF_SUCCESS;
  228. }