mqtt_transport_lwip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /** @file
  41. *
  42. * @brief MQTT Client Implementation over LwIP Stack port on nRF.
  43. *
  44. * This file contains the source code for MQTT Protocol over LwIP Stack 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 "lwip/opt.h"
  51. #include "lwip/stats.h"
  52. #include "lwip/sys.h"
  53. #include "lwip/pbuf.h"
  54. /*lint -save -e607 */
  55. #include "lwip/tcp.h"
  56. /*lint -restore -e607 */
  57. #if MQTT_CONFIG_LOG_ENABLED
  58. #define NRF_LOG_MODULE_NAME mqtt_lwip
  59. #define NRF_LOG_LEVEL MQTT_CONFIG_LOG_LEVEL
  60. #define NRF_LOG_INFO_COLOR MQTT_CONFIG_INFO_COLOR
  61. #define NRF_LOG_DEBUG_COLOR MQTT_CONFIG_DEBUG_COLOR
  62. #include "nrf_log.h"
  63. NRF_LOG_MODULE_REGISTER();
  64. #define MQTT_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  65. #define MQTT_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  66. #define MQTT_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  67. #define MQTT_ENTRY() MQTT_TRC(">> %s", __func__)
  68. #define MQTT_EXIT() MQTT_TRC("<< %s", __func__)
  69. #else // MQTT_CONFIG_LOG_ENABLED
  70. #define MQTT_TRC(...) /**< Disables traces. */
  71. #define MQTT_DUMP(...) /**< Disables dumping of octet streams. */
  72. #define MQTT_ERR(...) /**< Disables error logs. */
  73. #define MQTT_ENTRY(...)
  74. #define MQTT_EXIT(...)
  75. #endif // MQTT_CONFIG_LOG_ENABLED
  76. void disconnect_event_notify(mqtt_client_t * p_client, uint32_t result);
  77. /**@brief Close TCP connection and clean up client instance.
  78. *
  79. * @param[in] p_client Identifies the client for which the procedure is requested.
  80. */
  81. static void tcp_close_connection(const mqtt_client_t * p_client)
  82. {
  83. tcp_arg((struct tcp_pcb *)p_client->tcp_id, NULL);
  84. UNUSED_VARIABLE(tcp_output((struct tcp_pcb *)p_client->tcp_id));
  85. tcp_recv((struct tcp_pcb *)p_client->tcp_id, NULL);
  86. UNUSED_VARIABLE(tcp_close((struct tcp_pcb *)p_client->tcp_id));
  87. }
  88. err_t tcp_write_complete_cb(void *p_arg, struct tcp_pcb *ttcp_id, u16_t len)
  89. {
  90. MQTT_MUTEX_LOCK();
  91. mqtt_client_t *p_client = (mqtt_client_t *)(p_arg);
  92. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_DISCONNECTING))
  93. {
  94. MQTT_TRC("[%p]: Closing TCP connection.", p_client);
  95. tcp_close_connection(p_client);
  96. disconnect_event_notify(p_client, NRF_SUCCESS);
  97. }
  98. else
  99. {
  100. MQTT_RESET_STATE(p_client, MQTT_STATE_PENDING_WRITE);
  101. MQTT_TRC("[%p]: TCP Write Complete.", p_client);
  102. }
  103. MQTT_MUTEX_UNLOCK();
  104. return NRF_SUCCESS;
  105. }
  106. uint32_t mqtt_client_tcp_write(mqtt_client_t * p_client, uint8_t const * data, uint32_t datalen)
  107. {
  108. uint32_t retval = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  109. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
  110. {
  111. retval = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  112. }
  113. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_TCP_CONNECTED))
  114. {
  115. tcp_sent((struct tcp_pcb *)p_client->tcp_id, tcp_write_complete_cb);
  116. MQTT_MUTEX_UNLOCK ();
  117. uint32_t err = tcp_write((struct tcp_pcb *)p_client->tcp_id,
  118. data,
  119. datalen,
  120. TCP_WRITE_FLAG_COPY);
  121. MQTT_MUTEX_LOCK ();
  122. if (err == ERR_OK)
  123. {
  124. MQTT_SET_STATE(p_client, MQTT_STATE_PENDING_WRITE);
  125. UNUSED_VARIABLE(iot_timer_wall_clock_get(&p_client->last_activity));
  126. MQTT_TRC("[%p]: TCP Write in Progress, length 0x%08x.", p_client, datalen);
  127. retval = NRF_SUCCESS;
  128. }
  129. else
  130. {
  131. MQTT_TRC("[%p]: TCP write failed, err = %d", err);
  132. retval = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  133. }
  134. }
  135. return retval;
  136. }
  137. uint32_t mqtt_client_tcp_read(mqtt_client_t * p_id, uint8_t * p_data, uint32_t datalen)
  138. {
  139. return mqtt_handle_rx_data( p_id, p_data, datalen);
  140. }
  141. /**@brief Callback registered with TCP to handle incoming data on the connection. */
  142. err_t recv_callback(void * p_arg, struct tcp_pcb * p_tcp_id, struct pbuf * p_buffer, err_t err)
  143. {
  144. MQTT_MUTEX_LOCK();
  145. mqtt_client_t * p_client = (mqtt_client_t *)(p_arg);
  146. MQTT_TRC(">> %s, result 0x%08x, buffer %p", __func__, err, p_buffer);
  147. if (err == ERR_OK && p_buffer != NULL)
  148. {
  149. MQTT_TRC(">> Packet buffer length 0x%08x ", p_buffer->tot_len);
  150. tcp_recved(p_tcp_id, p_buffer->tot_len);
  151. UNUSED_VARIABLE(mqtt_transport_read(p_client, p_buffer->payload, p_buffer->tot_len));
  152. }
  153. else
  154. {
  155. MQTT_TRC("Error receiving data, closing connection");
  156. tcp_close_connection(p_client);
  157. disconnect_event_notify(p_client, MQTT_ERR_TRANSPORT_CLOSED);
  158. }
  159. UNUSED_VARIABLE(pbuf_free(p_buffer));
  160. MQTT_MUTEX_UNLOCK();
  161. return ERR_OK;
  162. }
  163. uint32_t mqtt_client_tcp_connect(mqtt_client_t * p_client)
  164. {
  165. connect_request_encode(p_client, &p_client->p_pending_packet, &p_client->pending_packetlen);
  166. // Send MQTT identification message to broker.
  167. uint32_t err = mqtt_client_tcp_write(p_client, p_client->p_pending_packet,
  168. p_client->pending_packetlen);
  169. if (err != ERR_OK)
  170. {
  171. mqtt_client_tcp_abort(p_client);
  172. }
  173. else
  174. {
  175. p_client->p_pending_packet = NULL;
  176. p_client->pending_packetlen = 0;
  177. }
  178. return err;
  179. }
  180. /**@brief TCP Connection Callback. MQTT Connection */
  181. err_t tcp_connection_callback(void * p_arg, struct tcp_pcb * p_tcp_id, err_t err)
  182. {
  183. MQTT_MUTEX_LOCK();
  184. mqtt_client_t * p_client = (mqtt_client_t *)p_arg;
  185. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_TCP_CONNECTING) &&
  186. (err == ERR_OK))
  187. {
  188. MQTT_SET_STATE(p_client, MQTT_STATE_TCP_CONNECTED);
  189. // Register callback.
  190. tcp_recv(p_tcp_id, recv_callback);
  191. uint32_t err_code = mqtt_transport_connect(p_client);
  192. if (err_code != NRF_SUCCESS)
  193. {
  194. MQTT_TRC("Transport connect handler returned %08x", err_code);
  195. disconnect_event_notify(p_client, MQTT_CONNECTION_FAILED);
  196. }
  197. }
  198. MQTT_MUTEX_UNLOCK();
  199. return err;
  200. }
  201. void mqtt_client_tcp_abort(mqtt_client_t * p_client)
  202. {
  203. tcp_abort((struct tcp_pcb *)p_client->tcp_id);
  204. disconnect_event_notify(p_client, MQTT_ERR_TCP_PROC_FAILED);
  205. MQTT_STATE_INIT(p_client);
  206. }
  207. void tcp_error_handler(void * p_arg, err_t err)
  208. {
  209. MQTT_MUTEX_LOCK();
  210. mqtt_client_t * p_client = (mqtt_client_t *)(p_arg);
  211. disconnect_event_notify(p_client, err);
  212. MQTT_STATE_INIT(p_client);
  213. MQTT_MUTEX_UNLOCK();
  214. }
  215. err_t tcp_connection_poll(void * p_arg, struct tcp_pcb * p_tcp_id)
  216. {
  217. MQTT_MUTEX_LOCK();
  218. mqtt_client_t * p_client = (mqtt_client_t *)(p_arg);
  219. p_client->poll_abort_counter++;
  220. MQTT_MUTEX_UNLOCK();
  221. return ERR_OK;
  222. }
  223. uint32_t tcp_request_connection(mqtt_client_t * p_client)
  224. {
  225. p_client->poll_abort_counter = 0;
  226. p_client->tcp_id = (uint32_t)tcp_new_ip6();
  227. err_t err = tcp_connect((struct tcp_pcb *)p_client->tcp_id,
  228. (ip_addr_t *)&p_client->broker_addr,
  229. p_client->broker_port,
  230. tcp_connection_callback);
  231. if (err != ERR_OK)
  232. {
  233. UNUSED_VARIABLE(mqtt_abort(p_client));
  234. }
  235. else
  236. {
  237. tcp_arg((struct tcp_pcb *)p_client->tcp_id, p_client);
  238. tcp_err((struct tcp_pcb *)p_client->tcp_id, tcp_error_handler);
  239. tcp_poll((struct tcp_pcb *)p_client->tcp_id, tcp_connection_poll, 10);
  240. tcp_accept((struct tcp_pcb *)p_client->tcp_id, tcp_connection_callback);
  241. MQTT_SET_STATE(p_client, MQTT_STATE_TCP_CONNECTING);
  242. }
  243. return err;
  244. }
  245. uint32_t mqtt_client_tcp_disconnect(mqtt_client_t * p_client)
  246. {
  247. uint32_t err_code = NRF_ERROR_INVALID_STATE;
  248. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
  249. {
  250. const uint8_t packet[] = {MQTT_PKT_TYPE_DISCONNECT, 0x00};
  251. UNUSED_VARIABLE(tcp_write((struct tcp_pcb *)p_client->tcp_id,
  252. (void *)packet,
  253. sizeof(packet),
  254. 1));
  255. tcp_close_connection(p_client);
  256. err_code = NRF_SUCCESS;
  257. }
  258. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_TCP_CONNECTED))
  259. {
  260. tcp_close_connection(p_client);
  261. err_code = NRF_SUCCESS;
  262. }
  263. return err_code;
  264. }
  265. uint32_t tcp_receive_packet(mqtt_client_t * p_client, uint32_t timeout)
  266. {
  267. // This is not used in the lwip transport implementation.
  268. return NRF_ERROR_NOT_SUPPORTED | IOT_MQTT_ERR_BASE;
  269. }