lwm2m_bootstrap.c 6.8 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 <string.h>
  41. #include <stdio.h>
  42. #include <stdint.h>
  43. #include "lwm2m_api.h"
  44. #include "lwm2m_bootstrap.h"
  45. #include "lwm2m.h"
  46. #include "coap_api.h"
  47. #include "coap_message.h"
  48. #include "coap_codes.h"
  49. #include "sdk_config.h"
  50. #include "app_util.h"
  51. #if LWM2M_CONFIG_LOG_ENABLED
  52. #define NRF_LOG_MODULE_NAME lwm2m
  53. #define NRF_LOG_LEVEL LWM2M_CONFIG_LOG_LEVEL
  54. #define NRF_LOG_INFO_COLOR LWM2M_CONFIG_INFO_COLOR
  55. #define NRF_LOG_DEBUG_COLOR LWM2M_CONFIG_DEBUG_COLOR
  56. #include "nrf_log.h"
  57. #define LWM2M_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  58. #define LWM2M_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  59. #define LWM2M_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  60. #define LWM2M_ENTRY() LWM2M_TRC(">> %s", __func__)
  61. #define LWM2M_EXIT() LWM2M_TRC("<< %s", __func__)
  62. #else // LWM2M_CONFIG_LOG_ENABLED
  63. #define LWM2M_TRC(...) /**< Disables traces. */
  64. #define LWM2M_DUMP(...) /**< Disables dumping of octet streams. */
  65. #define LWM2M_ERR(...) /**< Disables error logs. */
  66. #define LWM2M_ENTRY(...)
  67. #define LWM2M_EXIT(...)
  68. #endif // LWM2M_CONFIG_LOG_ENABLED
  69. #define LWM2M_BOOTSTRAP_URI_PATH "bs"
  70. #define TOKEN_START 0x012A
  71. static uint16_t m_token = TOKEN_START;
  72. static uint32_t internal_message_new(coap_message_t ** pp_msg,
  73. coap_msg_code_t code,
  74. coap_response_callback_t callback,
  75. uint16_t local_port)
  76. {
  77. uint32_t err_code;
  78. coap_message_conf_t conf;
  79. memset (&conf, 0, sizeof(coap_message_conf_t));
  80. conf.type = COAP_TYPE_CON;
  81. conf.code = code;
  82. conf.response_callback = callback;
  83. conf.port.port_number = local_port;
  84. conf.token_len = uint16_encode(m_token, conf.token);
  85. m_token++;
  86. err_code = coap_message_new(pp_msg, &conf);
  87. return err_code;
  88. }
  89. /**@brief Function to be used as callback function upon a bootstrap request. */
  90. static void lwm2m_bootstrap_cb(uint32_t status, void * p_arg, coap_message_t * p_message)
  91. {
  92. LWM2M_TRC("[Bootstrap]: lwm2m_bootstrap_cb, status: %ul, coap code: %u",
  93. status,
  94. p_message->header.code);
  95. (void)lwm2m_notification(LWM2M_NOTIFCATION_TYPE_BOOTSTRAP,
  96. &p_message->remote,
  97. p_message->header.code);
  98. }
  99. uint32_t internal_lwm2m_bootstrap_init(void)
  100. {
  101. m_token = TOKEN_START;
  102. return NRF_SUCCESS;
  103. }
  104. uint32_t lwm2m_bootstrap(lwm2m_remote_t * p_remote, lwm2m_client_identity_t * p_id, uint16_t local_port)
  105. {
  106. LWM2M_ENTRY();
  107. NULL_PARAM_CHECK(p_remote);
  108. NULL_PARAM_CHECK(p_id);
  109. LWM2M_MUTEX_LOCK();
  110. uint32_t err_code;
  111. coap_message_t * p_msg;
  112. lwm2m_string_t endpoint;
  113. endpoint.p_val = LWM2M_BOOTSTRAP_URI_PATH;
  114. endpoint.len = 2;
  115. err_code = internal_message_new(&p_msg, COAP_CODE_POST, lwm2m_bootstrap_cb, local_port);
  116. if (err_code != NRF_SUCCESS)
  117. {
  118. LWM2M_MUTEX_UNLOCK();
  119. return err_code;
  120. }
  121. if (err_code == NRF_SUCCESS)
  122. {
  123. err_code = coap_message_remote_addr_set(p_msg, p_remote);
  124. }
  125. if (err_code == NRF_SUCCESS)
  126. {
  127. err_code = coap_message_opt_str_add(p_msg,
  128. COAP_OPT_URI_PATH,
  129. (uint8_t *)endpoint.p_val,
  130. endpoint.len); // end_point length is always 2
  131. }
  132. if (err_code == NRF_SUCCESS)
  133. {
  134. char buffer[40];
  135. buffer[0] = 'e';
  136. buffer[1] = 'p';
  137. buffer[2] = '=';
  138. memcpy(buffer + 3, &p_id->value, p_id->type);
  139. err_code = coap_message_opt_str_add(p_msg,
  140. COAP_OPT_URI_QUERY,
  141. (uint8_t *)buffer,
  142. p_id->type + 3);
  143. }
  144. if (err_code == NRF_SUCCESS)
  145. {
  146. uint32_t msg_handle;
  147. err_code = coap_message_send(&msg_handle, p_msg);
  148. }
  149. if (err_code == NRF_SUCCESS)
  150. {
  151. err_code = coap_message_delete(p_msg);
  152. }
  153. else
  154. {
  155. // If we have hit an error try to clean up.
  156. // Return the original error code.
  157. (void)coap_message_delete(p_msg);
  158. }
  159. LWM2M_EXIT();
  160. LWM2M_MUTEX_UNLOCK();
  161. return err_code;
  162. }