lwm2m_coap_util.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <string.h>
  41. #include "coap_api.h"
  42. #include "coap_message.h"
  43. #include "coap_codes.h"
  44. #include "lwm2m.h"
  45. uint32_t lwm2m_respond_with_code(coap_msg_code_t code, coap_message_t * p_request)
  46. {
  47. NULL_PARAM_CHECK(p_request);
  48. // Application helper function, no need for mutex.
  49. coap_message_conf_t response_config;
  50. memset (&response_config, 0, sizeof(coap_message_conf_t));
  51. if (p_request->header.type == COAP_TYPE_NON)
  52. {
  53. response_config.type = COAP_TYPE_NON;
  54. }
  55. else if (p_request->header.type == COAP_TYPE_CON)
  56. {
  57. response_config.type = COAP_TYPE_ACK;
  58. }
  59. // PIGGY BACKED RESPONSE
  60. response_config.code = code;
  61. response_config.id = p_request->header.id;
  62. response_config.port.port_number = p_request->port.port_number;
  63. // Copy token.
  64. memcpy(&response_config.token[0], &p_request->token[0], p_request->header.token_len);
  65. // Copy token length.
  66. response_config.token_len = p_request->header.token_len;
  67. coap_message_t * p_response;
  68. uint32_t err_code = coap_message_new(&p_response, &response_config);
  69. if (err_code != NRF_SUCCESS)
  70. {
  71. return err_code;
  72. }
  73. err_code = coap_message_remote_addr_set(p_response, &p_request->remote);
  74. if (err_code != NRF_SUCCESS)
  75. {
  76. (void)coap_message_delete(p_response);
  77. return err_code;
  78. }
  79. memcpy(&p_response->remote, &p_request->remote, sizeof(coap_remote_t));
  80. uint32_t msg_handle;
  81. err_code = coap_message_send(&msg_handle, p_response);
  82. if (err_code != NRF_SUCCESS)
  83. {
  84. (void)coap_message_delete(p_response);
  85. return err_code;
  86. }
  87. err_code = coap_message_delete(p_response);
  88. return err_code;
  89. }
  90. uint32_t lwm2m_respond_with_payload(uint8_t * p_payload, uint16_t payload_len, coap_message_t * p_request)
  91. {
  92. NULL_PARAM_CHECK(p_request);
  93. NULL_PARAM_CHECK(p_payload);
  94. // Application helper function, no need for mutex.
  95. coap_message_conf_t response_config;
  96. memset (&response_config, 0, sizeof(coap_message_conf_t));
  97. if (p_request->header.type == COAP_TYPE_NON)
  98. {
  99. response_config.type = COAP_TYPE_NON;
  100. }
  101. else if (p_request->header.type == COAP_TYPE_CON)
  102. {
  103. response_config.type = COAP_TYPE_ACK;
  104. }
  105. // PIGGY BACKED RESPONSE
  106. response_config.code = COAP_CODE_205_CONTENT;
  107. response_config.id = p_request->header.id;
  108. response_config.port.port_number = p_request->port.port_number;
  109. // Copy token.
  110. memcpy(&response_config.token[0], &p_request->token[0], p_request->header.token_len);
  111. // Copy token length.
  112. response_config.token_len = p_request->header.token_len;
  113. coap_message_t * p_response;
  114. uint32_t err_code = coap_message_new(&p_response, &response_config);
  115. if (err_code != NRF_SUCCESS)
  116. {
  117. return err_code;
  118. }
  119. err_code = coap_message_payload_set(p_response, p_payload, payload_len);
  120. if (err_code != NRF_SUCCESS)
  121. {
  122. (void)coap_message_delete(p_response);
  123. return err_code;
  124. }
  125. err_code = coap_message_remote_addr_set(p_response, &p_request->remote);
  126. if (err_code != NRF_SUCCESS)
  127. {
  128. (void)coap_message_delete(p_response);
  129. return err_code;
  130. }
  131. memcpy(&p_response->remote, &p_request->remote, sizeof(coap_remote_t));
  132. uint32_t msg_handle;
  133. err_code = coap_message_send(&msg_handle, p_response);
  134. if (err_code != NRF_SUCCESS)
  135. {
  136. (void)coap_message_delete(p_response);
  137. return err_code;
  138. }
  139. err_code = coap_message_delete(p_response);
  140. return err_code;
  141. }