mqtt_rx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 mqtt_rx.c
  41. *
  42. * @brief Handles packet receive on transport TCP or TLS.
  43. */
  44. #include "mqtt_internal.h"
  45. void event_notify(mqtt_client_t * const p_client, const mqtt_evt_t * p_evt, uint32_t flags);
  46. void disconnect_event_notify(mqtt_client_t * p_client, uint32_t result);
  47. #if MQTT_CONFIG_LOG_ENABLED
  48. #define NRF_LOG_MODULE_NAME mqtt_rx
  49. #define NRF_LOG_LEVEL MQTT_CONFIG_LOG_LEVEL
  50. #define NRF_LOG_INFO_COLOR MQTT_CONFIG_INFO_COLOR
  51. #define NRF_LOG_DEBUG_COLOR MQTT_CONFIG_DEBUG_COLOR
  52. #include "nrf_log.h"
  53. NRF_LOG_MODULE_REGISTER();
  54. #define MQTT_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  55. #define MQTT_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  56. #define MQTT_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  57. #define MQTT_ENTRY() MQTT_TRC(">> %s", __func__)
  58. #define MQTT_EXIT() MQTT_TRC("<< %s", __func__)
  59. #else // MQTT_CONFIG_LOG_ENABLED
  60. #define MQTT_TRC(...) /**< Disables traces. */
  61. #define MQTT_DUMP(...) /**< Disables dumping of octet streams. */
  62. #define MQTT_ERR(...) /**< Disables error logs. */
  63. #define MQTT_ENTRY(...)
  64. #define MQTT_EXIT(...)
  65. #endif // MQTT_CONFIG_LOG_ENABLED
  66. static uint32_t mqtt_handle_packet(mqtt_client_t * p_client,
  67. uint8_t * p_data,
  68. uint32_t datalen,
  69. uint32_t offset)
  70. {
  71. mqtt_evt_t evt;
  72. uint32_t err_code = NRF_SUCCESS;
  73. bool notify_event = true;
  74. // Success by default, overwritten in special cases.
  75. evt.result = NRF_SUCCESS;
  76. switch (p_data[0] & 0xF0)
  77. {
  78. case MQTT_PKT_TYPE_CONNACK:
  79. {
  80. MQTT_TRC("[%p]: Received MQTT_PKT_TYPE_CONNACK!", p_client);
  81. if (p_client->protocol_version == MQTT_VERSION_3_1_1)
  82. {
  83. evt.param.connack.session_present_flag = p_data[2] & MQTT_HEADER_CONNACK_MASK;
  84. MQTT_TRC("[%p]: session_present_flag: %d",
  85. p_client,
  86. evt.param.connack.session_present_flag);
  87. }
  88. evt.param.connack.return_code =
  89. (mqtt_conn_return_code_t)(p_data[3] & MQTT_HEADER_CONNACK_MASK);
  90. MQTT_TRC("[%p]: return_code: %d",
  91. p_client,
  92. evt.param.connack.return_code);
  93. if (evt.param.connack.return_code == MQTT_CONNECTION_ACCEPTED)
  94. {
  95. // Set state.
  96. MQTT_SET_STATE(p_client, MQTT_STATE_CONNECTED);
  97. }
  98. evt.result = evt.param.connack.return_code;
  99. evt.id = MQTT_EVT_CONNACK;
  100. break;
  101. }
  102. case MQTT_PKT_TYPE_PUBLISH:
  103. {
  104. evt.param.publish.dup_flag = p_data[0] & MQTT_HEADER_DUP_MASK;
  105. evt.param.publish.retain_flag = p_data[0] & MQTT_HEADER_RETAIN_MASK;
  106. evt.param.publish.message.topic.qos = ((p_data[0] & MQTT_HEADER_QOS_MASK) >> 1);
  107. MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PUBLISH, QoS:%02x",
  108. p_client, evt.param.publish.message.topic.qos);
  109. err_code = unpack_utf8_str(&evt.param.publish.message.topic.topic,
  110. datalen,
  111. p_data,
  112. &offset);
  113. if (err_code == NRF_SUCCESS)
  114. {
  115. if (evt.param.publish.message.topic.qos)
  116. {
  117. err_code = unpack_uint16(&evt.param.publish.message_id,
  118. datalen,
  119. p_data,
  120. &offset);
  121. }
  122. }
  123. if (err_code == NRF_SUCCESS)
  124. {
  125. err_code = unpack_bin_str(&evt.param.publish.message.payload,
  126. datalen,
  127. p_data,
  128. &offset);
  129. // Zero length publish messages are permitted.
  130. if (err_code != NRF_SUCCESS)
  131. {
  132. evt.param.publish.message.payload.p_bin_str = NULL;
  133. evt.param.publish.message.payload.bin_strlen = 0;
  134. }
  135. }
  136. MQTT_TRC("PUB message len %08x, topic len %08x",
  137. evt.param.publish.message.payload.bin_strlen,
  138. evt.param.publish.message.topic.topic.utf_strlen);
  139. evt.id = MQTT_EVT_PUBLISH;
  140. evt.result = err_code;
  141. UNUSED_VARIABLE(iot_timer_wall_clock_get(&p_client->last_activity));
  142. break;
  143. }
  144. case MQTT_PKT_TYPE_PUBACK:
  145. {
  146. MQTT_TRC("Received MQTT_PKT_TYPE_PUBACK!");
  147. evt.id = MQTT_EVT_PUBACK;
  148. err_code = unpack_uint16(&evt.param.puback.message_id,
  149. datalen,
  150. p_data,
  151. &offset);
  152. evt.result = err_code;
  153. break;
  154. }
  155. case MQTT_PKT_TYPE_PUBREC:
  156. {
  157. MQTT_TRC("Received MQTT_PKT_TYPE_PUBREC!");
  158. evt.id = MQTT_EVT_PUBREC;
  159. err_code = unpack_uint16(&evt.param.pubrec.message_id,
  160. datalen,
  161. p_data,
  162. &offset);
  163. evt.result = err_code;
  164. break;
  165. }
  166. case MQTT_PKT_TYPE_PUBREL:
  167. {
  168. MQTT_TRC("Received MQTT_PKT_TYPE_PUBREL!");
  169. evt.id = MQTT_EVT_PUBREL;
  170. err_code = unpack_uint16(&evt.param.pubrel.message_id,
  171. datalen,
  172. p_data,
  173. &offset);
  174. evt.result = err_code;
  175. break;
  176. }
  177. case MQTT_PKT_TYPE_PUBCOMP:
  178. {
  179. MQTT_TRC("Received MQTT_PKT_TYPE_PUBCOMP!");
  180. evt.id = MQTT_EVT_PUBCOMP;
  181. err_code = unpack_uint16(&evt.param.pubcomp.message_id,
  182. datalen,
  183. p_data,
  184. &offset);
  185. evt.result = err_code;
  186. break;
  187. }
  188. case MQTT_PKT_TYPE_SUBACK:
  189. {
  190. MQTT_TRC("Received MQTT_PKT_TYPE_SUBACK!");
  191. evt.id = MQTT_EVT_SUBACK;
  192. err_code = unpack_uint16(&evt.param.pubrec.message_id,
  193. datalen,
  194. p_data,
  195. &offset);
  196. evt.result = err_code;
  197. break;
  198. }
  199. case MQTT_PKT_TYPE_UNSUBACK:
  200. {
  201. MQTT_TRC("Received MQTT_PKT_TYPE_UNSUBACK!");
  202. evt.id = MQTT_EVT_UNSUBACK;
  203. err_code = unpack_uint16(&evt.param.pubrec.message_id,
  204. datalen,
  205. p_data,
  206. &offset);
  207. evt.result = err_code;
  208. break;
  209. }
  210. case MQTT_PKT_TYPE_PINGRSP:
  211. {
  212. MQTT_TRC("Received MQTT_PKT_TYPE_PINGRSP!");
  213. // No notification of Ping response to application.
  214. notify_event = false;
  215. break;
  216. }
  217. default:
  218. {
  219. // Nothing to notify.
  220. notify_event = false;
  221. break;
  222. }
  223. }
  224. if (notify_event == true)
  225. {
  226. event_notify(p_client, &evt, MQTT_EVT_FLAG_NONE);
  227. }
  228. return err_code;
  229. }
  230. uint32_t mqtt_handle_rx_data(mqtt_client_t * p_client, uint8_t * p_data, uint32_t datalen)
  231. {
  232. uint32_t err_code = NRF_SUCCESS;
  233. uint32_t offset = 0;
  234. while (offset < datalen)
  235. {
  236. uint32_t start = offset;
  237. uint32_t remaining_length = 0;
  238. offset = 1; // Skip first byte to offset MQTT packet length.
  239. err_code = packet_length_decode(p_data + start,
  240. datalen - start,
  241. &remaining_length,
  242. &offset);
  243. if (err_code != NRF_SUCCESS)
  244. {
  245. return err_code;
  246. }
  247. uint32_t packet_length = offset + remaining_length;
  248. if (start + packet_length > datalen)
  249. {
  250. return NRF_ERROR_INVALID_LENGTH;
  251. }
  252. err_code = mqtt_handle_packet(p_client,
  253. p_data + start,
  254. packet_length,
  255. offset);
  256. if (err_code != NRF_SUCCESS)
  257. {
  258. return err_code;
  259. }
  260. offset = start + packet_length;
  261. }
  262. return err_code;
  263. }