mqtt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /**
  2. * Copyright (c) 2015 - 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 mqtt.c
  41. *
  42. * @brief MQTT Client API Implementation.
  43. */
  44. #include "mqtt.h"
  45. #include "mem_manager.h"
  46. #include "mqtt_transport.h"
  47. #include "mqtt_internal.h"
  48. #include "iot_timer.h"
  49. #if MQTT_CONFIG_LOG_ENABLED
  50. #define NRF_LOG_MODULE_NAME mqtt
  51. #define NRF_LOG_LEVEL MQTT_CONFIG_LOG_LEVEL
  52. #define NRF_LOG_INFO_COLOR MQTT_CONFIG_INFO_COLOR
  53. #define NRF_LOG_DEBUG_COLOR MQTT_CONFIG_DEBUG_COLOR
  54. #include "nrf_log.h"
  55. NRF_LOG_MODULE_REGISTER();
  56. #define MQTT_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  57. #define MQTT_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  58. #define MQTT_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  59. #define MQTT_ENTRY() MQTT_TRC(">> %s", __func__)
  60. #define MQTT_EXIT() MQTT_TRC("<< %s", __func__)
  61. #else // MQTT_CONFIG_LOG_ENABLED
  62. #define MQTT_TRC(...) /**< Disables traces. */
  63. #define MQTT_DUMP(...) /**< Disables dumping of octet streams. */
  64. #define MQTT_ERR(...) /**< Disables error logs. */
  65. #define MQTT_ENTRY(...)
  66. #define MQTT_EXIT(...)
  67. #endif // MQTT_CONFIG_LOG_ENABLED
  68. /**< Never changing ping request, needed for Keep Alive. */
  69. static const uint8_t m_ping_packet[MQTT_PKT_HEADER_SIZE] = \
  70. {MQTT_PKT_TYPE_PINGREQ, \
  71. 0x00};
  72. /**< Never changing disconnect request. */
  73. static const uint8_t m_disc_packet[MQTT_PKT_HEADER_SIZE] = \
  74. {MQTT_PKT_TYPE_DISCONNECT, \
  75. 0x00};
  76. static mqtt_client_t * m_mqtt_client[MQTT_MAX_CLIENTS]; /**< MQTT Client table. */
  77. SDK_MUTEX_DEFINE(m_mqtt_mutex) /**< Mutex variable for the module, currently unused. */
  78. static uint32_t get_client_index(mqtt_client_t * const p_client)
  79. {
  80. for (uint32_t index = 0; index < MQTT_MAX_CLIENTS; index++)
  81. {
  82. if (m_mqtt_client[index] == p_client)
  83. {
  84. return index;
  85. }
  86. }
  87. return MQTT_MAX_CLIENTS;
  88. }
  89. void client_free(mqtt_client_t * const p_client)
  90. {
  91. MQTT_STATE_INIT(p_client);
  92. // Free memory used for TX packets and reset the pointer.
  93. nrf_free(p_client->p_packet);
  94. p_client->p_packet = NULL;
  95. // Free TLS instance and reset the instance.
  96. UNUSED_VARIABLE(nrf_tls_free(&p_client->tls_instance));
  97. NRF_TLS_INTSANCE_INIT(&p_client->tls_instance);
  98. }
  99. void client_init(mqtt_client_t * const p_client)
  100. {
  101. memset(p_client, 0, sizeof(*p_client));
  102. MQTT_STATE_INIT(p_client);
  103. p_client->protocol_version = MQTT_VERSION_3_1_0;
  104. p_client->clean_session = 1;
  105. NRF_TLS_INTSANCE_INIT(&p_client->tls_instance);
  106. }
  107. /**@brief Notifies event to the application.
  108. *
  109. * @param[in] p_client Identifies the client for which the procedure is requested.
  110. * @param[in] p_evt Reason for disconnection.
  111. */
  112. void event_notify(mqtt_client_t * const p_client, const mqtt_evt_t * p_evt, uint32_t flags)
  113. {
  114. const mqtt_evt_cb_t evt_cb = p_client->evt_cb;
  115. if (evt_cb != NULL)
  116. {
  117. MQTT_MUTEX_UNLOCK();
  118. evt_cb(p_client, p_evt);
  119. MQTT_MUTEX_LOCK();
  120. if (IS_SET(flags,MQTT_EVT_FLAG_INSTANCE_RESET))
  121. {
  122. client_init(p_client);
  123. }
  124. }
  125. }
  126. /**@brief Notifies disconnection event to the application.
  127. *
  128. * @param[in] p_client Identifies the client for which the procedure is requested.
  129. * @param[in] result Reason for disconnection.
  130. */
  131. void disconnect_event_notify(mqtt_client_t * p_client, uint32_t result)
  132. {
  133. mqtt_evt_t evt;
  134. const uint32_t client_index = get_client_index(p_client);
  135. // Remove the client from internal table.
  136. if (client_index != MQTT_MAX_CLIENTS)
  137. {
  138. m_mqtt_client[client_index] = NULL;
  139. }
  140. // Determine appropriate event to generate.
  141. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED) ||
  142. MQTT_VERIFY_STATE(p_client, MQTT_STATE_DISCONNECTING))
  143. {
  144. evt.id = MQTT_EVT_DISCONNECT;
  145. evt.result = result;
  146. }
  147. else
  148. {
  149. evt.id = MQTT_EVT_CONNACK;
  150. evt.result = MQTT_CONNECTION_FAILED;
  151. }
  152. // Free the instance.
  153. client_free(p_client);
  154. // Notify application.
  155. event_notify(p_client, &evt, MQTT_EVT_FLAG_INSTANCE_RESET);
  156. }
  157. uint32_t mqtt_init(void)
  158. {
  159. SDK_MUTEX_INIT(m_mqtt_mutex);
  160. MQTT_MUTEX_LOCK();
  161. memset(m_mqtt_client, 0, sizeof(m_mqtt_client));
  162. MQTT_MUTEX_UNLOCK();
  163. return nrf_tls_init();
  164. }
  165. void mqtt_client_init(mqtt_client_t * const p_client)
  166. {
  167. NULL_PARAM_CHECK_VOID(p_client);
  168. MQTT_MUTEX_LOCK();
  169. client_init(p_client);
  170. MQTT_MUTEX_UNLOCK();
  171. }
  172. uint32_t mqtt_connect(mqtt_client_t * const p_client)
  173. {
  174. // Look for a free instance if available.
  175. uint32_t err_code = NRF_SUCCESS;
  176. uint32_t client_index = 0;
  177. NULL_PARAM_CHECK(p_client);
  178. NULL_PARAM_CHECK(p_client->client_id.p_utf_str);
  179. MQTT_MUTEX_LOCK();
  180. for (client_index = 0; client_index < MQTT_MAX_CLIENTS; client_index++)
  181. {
  182. if (m_mqtt_client[client_index] == NULL)
  183. {
  184. // Found a free instance.
  185. m_mqtt_client[client_index] = p_client;
  186. // Allocate buffer packets in TX path.
  187. p_client->p_packet = nrf_malloc(MQTT_MAX_PACKET_LENGTH);
  188. break;
  189. }
  190. }
  191. if ((client_index == MQTT_MAX_CLIENTS) || (p_client->p_packet == NULL))
  192. {
  193. err_code = (NRF_ERROR_NO_MEM | IOT_MQTT_ERR_BASE);
  194. }
  195. else
  196. {
  197. err_code = tcp_request_connection(p_client);
  198. if (err_code != NRF_SUCCESS)
  199. {
  200. // Free the instance.
  201. m_mqtt_client[client_index] = NULL;
  202. nrf_free(p_client->p_packet);
  203. err_code = MQTT_ERR_TCP_PROC_FAILED;
  204. }
  205. }
  206. UNUSED_VARIABLE(p_client);
  207. MQTT_MUTEX_UNLOCK();
  208. return err_code;
  209. }
  210. uint32_t mqtt_publish(mqtt_client_t * const p_client,
  211. mqtt_publish_param_t const * const p_param)
  212. {
  213. uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
  214. uint32_t offset = 0;
  215. uint32_t mqtt_packetlen = 0;
  216. uint8_t * p_payload;
  217. NULL_PARAM_CHECK(p_client);
  218. NULL_PARAM_CHECK(p_param);
  219. MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Topic size 0x%08x, Data size 0x%08x",
  220. p_client,
  221. p_client->state,
  222. __func__,
  223. p_param->message.topic.topic.utf_strlen,
  224. p_param->message.payload.bin_strlen);
  225. MQTT_MUTEX_LOCK();
  226. p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
  227. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
  228. {
  229. err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  230. }
  231. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
  232. {
  233. memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
  234. // Pack topic.
  235. err_code = pack_utf8_str(&p_param->message.topic.topic,
  236. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  237. p_payload,
  238. &offset);
  239. if (err_code == NRF_SUCCESS)
  240. {
  241. if (p_param->message.topic.qos)
  242. {
  243. err_code = pack_uint16(p_param->message_id,
  244. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  245. p_payload,
  246. &offset);
  247. }
  248. }
  249. if (err_code == NRF_SUCCESS)
  250. {
  251. // Pack message on the topic.
  252. err_code = pack_bin_str(&p_param->message.payload,
  253. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  254. p_payload,
  255. &offset);
  256. }
  257. if (err_code == NRF_SUCCESS)
  258. {
  259. const uint8_t message_type = MQTT_MESSAGES_OPTIONS(MQTT_PKT_TYPE_PUBLISH,
  260. 0, // Duplicate flag not set.
  261. p_param->message.topic.qos,
  262. 0); // Retain flag not set.
  263. mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type
  264. offset, // Payload size without the fixed header
  265. &p_payload); // Address where the p_payload is contained.
  266. // Publish message.
  267. err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
  268. }
  269. }
  270. MQTT_TRC("<< %s", (uint32_t)__func__);
  271. MQTT_MUTEX_UNLOCK();
  272. return err_code;
  273. }
  274. /**@brief Encodes and sends messages that contain only message id in the variable header.
  275. *
  276. * @param[in] p_client Identifies the client for which the procedure is requested.
  277. * @param[in] op_code Opcode for the message.
  278. * @param[in] message_id Message id to be encoded in the variable header.
  279. *
  280. * @retval NRF_SUCCESS or an error code indicating a reason for failure.
  281. */
  282. uint32_t mqtt_message_id_only_enc_n_send(mqtt_client_t * const p_client,
  283. uint8_t opcode,
  284. uint16_t message_id)
  285. {
  286. uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
  287. uint32_t offset = 0;
  288. uint32_t mqtt_packetlen = 0;
  289. uint8_t * p_payload;
  290. p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
  291. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
  292. {
  293. err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  294. }
  295. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
  296. {
  297. memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
  298. err_code = pack_uint16(message_id,
  299. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  300. p_payload,
  301. &offset);
  302. if (err_code == NRF_SUCCESS)
  303. {
  304. const uint8_t message_type = MQTT_MESSAGES_OPTIONS(opcode,
  305. 0, // Duplicate flag not set.
  306. 0, // QoS unused.
  307. 0); // Retain flag not set.
  308. mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type
  309. offset, // Payload size without the fixed header
  310. &p_payload); // Address where the p_payload is contained.
  311. // Publish message.
  312. err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
  313. }
  314. }
  315. return err_code;
  316. }
  317. /**@brief Sends raw message to the peer.
  318. *
  319. * @param[in] p_client Identifies the client for which the procedure is requested.
  320. * @param[in] p_message Raw message to be sent to the peer.
  321. * @param[in] message_id Message id to be encoded in the variable header.
  322. *
  323. * @retval NRF_SUCCESS or an error code indicating a reason for failure.
  324. */
  325. uint32_t mqtt_raw_message_send(mqtt_client_t * const p_client,
  326. const uint8_t * p_message,
  327. uint16_t message_len)
  328. {
  329. uint32_t err_code;
  330. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
  331. {
  332. err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  333. }
  334. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
  335. {
  336. err_code = mqtt_transport_write(p_client, p_message, message_len);
  337. }
  338. else
  339. {
  340. err_code = MQTT_ERR_NOT_CONNECTED;
  341. }
  342. return err_code;
  343. }
  344. uint32_t mqtt_publish_ack(mqtt_client_t * const p_client,
  345. mqtt_puback_param_t const * p_param)
  346. {
  347. NULL_PARAM_CHECK(p_client);
  348. NULL_PARAM_CHECK(p_param);
  349. MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
  350. p_client,
  351. p_client->state,
  352. __func__,
  353. p_param->message_id);
  354. MQTT_MUTEX_LOCK();
  355. uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
  356. MQTT_PKT_TYPE_PUBACK,
  357. p_param->message_id);
  358. MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
  359. p_client,
  360. p_client->state,
  361. __func__,
  362. err_code);
  363. MQTT_MUTEX_UNLOCK();
  364. return err_code;
  365. }
  366. uint32_t mqtt_publish_receive(mqtt_client_t * const p_client,
  367. mqtt_pubrec_param_t const * const p_param)
  368. {
  369. NULL_PARAM_CHECK(p_client);
  370. NULL_PARAM_CHECK(p_param);
  371. MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
  372. p_client,
  373. p_client->state,
  374. __func__,
  375. p_param->message_id);
  376. MQTT_MUTEX_LOCK();
  377. uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
  378. MQTT_PKT_TYPE_PUBREC,
  379. p_param->message_id);
  380. MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
  381. p_client,
  382. p_client->state,
  383. __func__,
  384. err_code);
  385. MQTT_MUTEX_UNLOCK();
  386. return err_code;
  387. }
  388. uint32_t mqtt_publish_release(mqtt_client_t * const p_client,
  389. mqtt_pubrel_param_t const * const p_param)
  390. {
  391. NULL_PARAM_CHECK(p_client);
  392. NULL_PARAM_CHECK(p_param);
  393. MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
  394. p_client,
  395. p_client->state,
  396. __func__,
  397. p_param->message_id);
  398. MQTT_MUTEX_LOCK();
  399. uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
  400. MQTT_PKT_TYPE_PUBREL,
  401. p_param->message_id);
  402. MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
  403. p_client,
  404. p_client->state,
  405. __func__,
  406. err_code);
  407. MQTT_MUTEX_UNLOCK();
  408. return err_code;
  409. }
  410. uint32_t mqtt_publish_complete(mqtt_client_t * const p_client,
  411. mqtt_pubcomp_param_t const * const p_param)
  412. {
  413. NULL_PARAM_CHECK(p_client);
  414. NULL_PARAM_CHECK(p_param);
  415. MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
  416. p_client,
  417. p_client->state,
  418. __func__,
  419. p_param->message_id);
  420. MQTT_MUTEX_LOCK();
  421. uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
  422. MQTT_PKT_TYPE_PUBCOMP,
  423. p_param->message_id);
  424. MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
  425. p_client,
  426. p_client->state,
  427. __func__,
  428. err_code);
  429. MQTT_MUTEX_UNLOCK();
  430. return err_code;
  431. }
  432. uint32_t mqtt_disconnect(mqtt_client_t * const p_client)
  433. {
  434. uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
  435. NULL_PARAM_CHECK(p_client);
  436. MQTT_MUTEX_LOCK();
  437. err_code = mqtt_raw_message_send(p_client, m_disc_packet, MQTT_FIXED_HEADER_SIZE);
  438. if (err_code == NRF_SUCCESS)
  439. {
  440. MQTT_SET_STATE_EXCLUSIVE(p_client, MQTT_STATE_DISCONNECTING);
  441. }
  442. MQTT_MUTEX_UNLOCK();
  443. return err_code;
  444. }
  445. uint32_t mqtt_subscribe(mqtt_client_t * const p_client,
  446. mqtt_subscription_list_t const * const p_param)
  447. {
  448. uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
  449. uint32_t offset = 0;
  450. uint32_t count = 0;
  451. uint32_t mqtt_packetlen = 0;
  452. uint8_t * p_payload;
  453. NULL_PARAM_CHECK(p_client);
  454. NULL_PARAM_CHECK(p_param);
  455. MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s message id 0x%04x topic count 0x%04x",
  456. p_client,
  457. p_client->state,
  458. __func__,
  459. p_param->message_id,
  460. p_param->list_count);
  461. MQTT_MUTEX_LOCK();
  462. p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
  463. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
  464. {
  465. err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  466. }
  467. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
  468. {
  469. memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
  470. err_code = pack_uint16(p_param->message_id,
  471. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  472. p_payload,
  473. &offset);
  474. if (err_code == NRF_SUCCESS)
  475. {
  476. do
  477. {
  478. err_code = pack_utf8_str(&p_param->p_list[count].topic,
  479. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  480. p_payload,
  481. &offset);
  482. if (err_code == NRF_SUCCESS)
  483. {
  484. err_code = pack_uint8(p_param->p_list[count].qos,
  485. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  486. p_payload,
  487. &offset);
  488. }
  489. count++;
  490. } while ((err_code != NRF_SUCCESS) || (count < p_param->list_count));
  491. }
  492. if (err_code == NRF_SUCCESS)
  493. {
  494. const uint8_t message_type = MQTT_MESSAGES_OPTIONS(MQTT_PKT_TYPE_SUBSCRIBE, 0, 1, 0);
  495. // Rewind the packet to encode the packet correctly.
  496. mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type, Duplicate Flag, QoS and retain flag setting.
  497. offset, // p_payload size without the fixed header
  498. &p_payload); // Address where the p_payload is contained. Header will encoded by rewinding the location.
  499. // Send message.
  500. err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
  501. }
  502. }
  503. MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
  504. p_client,
  505. p_client->state,
  506. __func__,
  507. err_code);
  508. MQTT_MUTEX_UNLOCK();
  509. return err_code;
  510. }
  511. uint32_t mqtt_unsubscribe(mqtt_client_t * const p_client,
  512. mqtt_subscription_list_t const * const p_param)
  513. {
  514. uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
  515. uint32_t count = 0;
  516. uint32_t offset = 0;
  517. uint32_t mqtt_packetlen = 0;
  518. uint8_t * p_payload;
  519. NULL_PARAM_CHECK(p_client);
  520. NULL_PARAM_CHECK(p_param);
  521. MQTT_MUTEX_LOCK();
  522. p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
  523. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
  524. {
  525. err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
  526. }
  527. else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
  528. {
  529. memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
  530. err_code = pack_uint16(p_param->message_id,
  531. MQTT_MAX_PACKET_LENGTH,
  532. p_payload,
  533. &offset);
  534. if (err_code == NRF_SUCCESS)
  535. {
  536. do
  537. {
  538. err_code = pack_utf8_str(&p_param->p_list[count].topic,
  539. MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
  540. p_payload,
  541. &offset);
  542. count++;
  543. } while ((err_code != NRF_SUCCESS) || (count < p_param->list_count));
  544. }
  545. if (err_code == NRF_SUCCESS)
  546. {
  547. const uint8_t message_type = MQTT_MESSAGES_OPTIONS(MQTT_PKT_TYPE_UNSUBSCRIBE,
  548. 0, // Duplicate flag.
  549. MQTT_QoS_1_ATLEAST_ONCE,
  550. 0); // Retain flag.
  551. // Rewind the packet to encode the packet correctly.
  552. mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type, Duplicate Flag, QoS and retain flag setting.
  553. offset, // Payload size without the fixed header
  554. &p_payload); // Address where the p_payload is contained. Header will encoded by rewinding the location.
  555. // Send message.
  556. err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
  557. }
  558. }
  559. MQTT_MUTEX_UNLOCK();
  560. return err_code;
  561. }
  562. uint32_t mqtt_ping(mqtt_client_t * const p_client)
  563. {
  564. uint32_t err_code;
  565. NULL_PARAM_CHECK(p_client);
  566. MQTT_MUTEX_LOCK();
  567. err_code = mqtt_raw_message_send(p_client, m_ping_packet, MQTT_PKT_HEADER_SIZE);
  568. MQTT_MUTEX_UNLOCK();
  569. return err_code;
  570. }
  571. uint32_t mqtt_abort(mqtt_client_t * const p_client)
  572. {
  573. MQTT_MUTEX_LOCK();
  574. NULL_PARAM_CHECK(p_client);
  575. if (p_client->state != MQTT_STATE_IDLE)
  576. {
  577. mqtt_client_tcp_abort(p_client);
  578. }
  579. MQTT_MUTEX_UNLOCK();
  580. return NRF_SUCCESS;
  581. }
  582. uint32_t mqtt_live(void)
  583. {
  584. iot_timer_time_in_ms_t elapsed_time;
  585. uint32_t index;
  586. // Note: The module should not be locked when calling this TLS API.
  587. nrf_tls_process();
  588. MQTT_MUTEX_LOCK();
  589. for (index = 0; index < MQTT_MAX_CLIENTS; index++)
  590. {
  591. mqtt_client_t * p_client = m_mqtt_client[index];
  592. if (p_client != NULL)
  593. {
  594. UNUSED_VARIABLE(iot_timer_wall_clock_delta_get(&p_client->last_activity,
  595. &elapsed_time));
  596. if ((MQTT_KEEPALIVE > 0) && (elapsed_time > ((MQTT_KEEPALIVE - 2) * 1000)))
  597. {
  598. UNUSED_VARIABLE(mqtt_ping(p_client));
  599. }
  600. if (p_client->p_pending_packet != NULL)
  601. {
  602. uint32_t err;
  603. err = mqtt_transport_write(p_client, p_client->p_pending_packet,
  604. p_client->pending_packetlen);
  605. if (err == NRF_SUCCESS)
  606. {
  607. p_client->p_pending_packet = NULL;
  608. p_client->pending_packetlen = 0;
  609. }
  610. }
  611. }
  612. }
  613. MQTT_MUTEX_UNLOCK();
  614. return NRF_SUCCESS;
  615. }
  616. uint32_t mqtt_input(mqtt_client_t * p_client, uint32_t timeout)
  617. {
  618. uint32_t err_code;
  619. NULL_PARAM_CHECK(p_client);
  620. MQTT_MUTEX_LOCK();
  621. MQTT_TRC("%s: 0x%08x", __func__, p_client->state);
  622. if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_TCP_CONNECTED) ||
  623. MQTT_VERIFY_STATE(p_client, MQTT_STATE_DISCONNECTING))
  624. {
  625. err_code = tcp_receive_packet(p_client, timeout);
  626. }
  627. else
  628. {
  629. err_code = (NRF_ERROR_INVALID_STATE | IOT_MQTT_ERR_BASE);
  630. }
  631. MQTT_MUTEX_UNLOCK();
  632. return err_code;
  633. }