mqtt_transport_tls.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2016 - 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. /** @file
  41. *
  42. * @brief MQTT Client Implementation TLS layer.
  43. *
  44. * This file contains the source code for MQTT Protocol TLS layer for a nRF device.
  45. * The implementation is limited to MQTT Client role only.
  46. */
  47. #include "mqtt_transport.h"
  48. #include "mqtt_internal.h"
  49. #include "mqtt_rx.h"
  50. #include "mem_manager.h"
  51. #if MQTT_CONFIG_LOG_ENABLED
  52. #define NRF_LOG_MODULE_NAME mqtt_tls
  53. #define NRF_LOG_LEVEL MQTT_CONFIG_LOG_LEVEL
  54. #define NRF_LOG_INFO_COLOR MQTT_CONFIG_INFO_COLOR
  55. #define NRF_LOG_DEBUG_COLOR MQTT_CONFIG_DEBUG_COLOR
  56. #include "nrf_log.h"
  57. NRF_LOG_MODULE_REGISTER();
  58. #define MQTT_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  59. #define MQTT_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  60. #define MQTT_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  61. #define MQTT_ENTRY() MQTT_TRC(">> %s", __func__)
  62. #define MQTT_EXIT() MQTT_TRC("<< %s", __func__)
  63. #else // MQTT_CONFIG_LOG_ENABLED
  64. #define MQTT_TRC(...) /**< Disables traces. */
  65. #define MQTT_DUMP(...) /**< Disables dumping of octet streams. */
  66. #define MQTT_ERR(...) /**< Disables error logs. */
  67. #define MQTT_ENTRY(...)
  68. #define MQTT_EXIT(...)
  69. #endif // MQTT_CONFIG_LOG_ENABLED
  70. uint32_t mqtt_client_tls_output_handler(nrf_tls_instance_t const * p_instance,
  71. uint8_t const * p_data,
  72. uint32_t datalen)
  73. {
  74. NULL_PARAM_CHECK(p_instance);
  75. uint32_t err_code = NRF_ERROR_INTERNAL;
  76. mqtt_client_t * p_client = (mqtt_client_t *)p_instance->transport_id;
  77. MQTT_MUTEX_LOCK();
  78. MQTT_TRC(">> %s, client %p", __func__, p_client);
  79. if (p_client != NULL)
  80. {
  81. err_code = mqtt_client_tcp_write(p_client, p_data, datalen);
  82. }
  83. MQTT_TRC("<< %s, client %p, result 0x%08x", __func__,
  84. p_client, err_code);
  85. MQTT_MUTEX_UNLOCK();
  86. return err_code;
  87. }
  88. uint32_t mqtt_client_tls_connect(mqtt_client_t * p_client)
  89. {
  90. const nrf_tls_options_t tls_option =
  91. {
  92. .output_fn = mqtt_client_tls_output_handler,
  93. .transport_type = NRF_TLS_TYPE_STREAM,
  94. .role = NRF_TLS_ROLE_CLIENT,
  95. .p_key_settings = p_client->p_security_settings
  96. };
  97. connect_request_encode(p_client,
  98. &p_client->p_pending_packet,
  99. &p_client->pending_packetlen);
  100. p_client->tls_instance.transport_id = (uint32_t)p_client;
  101. MQTT_MUTEX_UNLOCK ();
  102. uint32_t err_code = nrf_tls_alloc(&p_client->tls_instance, &tls_option);
  103. MQTT_MUTEX_LOCK ();
  104. return err_code;
  105. }
  106. uint32_t mqtt_client_tls_write(mqtt_client_t * p_client,
  107. uint8_t const * p_data,
  108. uint32_t datalen)
  109. {
  110. MQTT_MUTEX_UNLOCK ();
  111. uint32_t err_code = nrf_tls_write(&p_client->tls_instance, p_data, &datalen);
  112. MQTT_MUTEX_LOCK ();
  113. return err_code;
  114. }
  115. uint32_t mqtt_client_tls_read(mqtt_client_t * p_client, uint8_t * p_data, uint32_t datalen)
  116. {
  117. uint32_t err = nrf_tls_input(&p_client->tls_instance, p_data, datalen);
  118. if ((err == NRF_SUCCESS) && (p_client->p_pending_packet == NULL))
  119. {
  120. uint32_t rx_datalen = 1024;
  121. uint8_t * p_mqtt_data = nrf_malloc(1024);
  122. if (p_data != NULL)
  123. {
  124. MQTT_MUTEX_UNLOCK ();
  125. err = nrf_tls_read(&p_client->tls_instance,
  126. p_mqtt_data,
  127. &rx_datalen);
  128. MQTT_MUTEX_LOCK ();
  129. if ((err == NRF_SUCCESS) && (rx_datalen > 0))
  130. {
  131. err = mqtt_handle_rx_data(p_client, p_mqtt_data, rx_datalen);
  132. }
  133. nrf_free(p_mqtt_data);
  134. }
  135. }
  136. return err;
  137. }
  138. uint32_t mqtt_client_tls_disconnect(mqtt_client_t * p_client)
  139. {
  140. return mqtt_client_tcp_disconnect(p_client);
  141. }