123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821 |
- /**
- * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form, except as embedded into a Nordic
- * Semiconductor ASA integrated circuit in a product or a software update for
- * such product, must reproduce the above copyright notice, this list of
- * conditions and the following disclaimer in the documentation and/or other
- * materials provided with the distribution.
- *
- * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * 4. This software, with or without modification, must only be used with a
- * Nordic Semiconductor ASA integrated circuit.
- *
- * 5. Any software provided in binary form under this license must not be reverse
- * engineered, decompiled, modified and/or disassembled.
- *
- * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- /** @file mqtt.c
- *
- * @brief MQTT Client API Implementation.
- */
- #include "mqtt.h"
- #include "mem_manager.h"
- #include "mqtt_transport.h"
- #include "mqtt_internal.h"
- #include "iot_timer.h"
- #if MQTT_CONFIG_LOG_ENABLED
- #define NRF_LOG_MODULE_NAME mqtt
- #define NRF_LOG_LEVEL MQTT_CONFIG_LOG_LEVEL
- #define NRF_LOG_INFO_COLOR MQTT_CONFIG_INFO_COLOR
- #define NRF_LOG_DEBUG_COLOR MQTT_CONFIG_DEBUG_COLOR
- #include "nrf_log.h"
- NRF_LOG_MODULE_REGISTER();
- #define MQTT_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
- #define MQTT_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
- #define MQTT_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
- #define MQTT_ENTRY() MQTT_TRC(">> %s", __func__)
- #define MQTT_EXIT() MQTT_TRC("<< %s", __func__)
- #else // MQTT_CONFIG_LOG_ENABLED
- #define MQTT_TRC(...) /**< Disables traces. */
- #define MQTT_DUMP(...) /**< Disables dumping of octet streams. */
- #define MQTT_ERR(...) /**< Disables error logs. */
- #define MQTT_ENTRY(...)
- #define MQTT_EXIT(...)
- #endif // MQTT_CONFIG_LOG_ENABLED
- /**< Never changing ping request, needed for Keep Alive. */
- static const uint8_t m_ping_packet[MQTT_PKT_HEADER_SIZE] = \
- {MQTT_PKT_TYPE_PINGREQ, \
- 0x00};
- /**< Never changing disconnect request. */
- static const uint8_t m_disc_packet[MQTT_PKT_HEADER_SIZE] = \
- {MQTT_PKT_TYPE_DISCONNECT, \
- 0x00};
- static mqtt_client_t * m_mqtt_client[MQTT_MAX_CLIENTS]; /**< MQTT Client table. */
- SDK_MUTEX_DEFINE(m_mqtt_mutex) /**< Mutex variable for the module, currently unused. */
- static uint32_t get_client_index(mqtt_client_t * const p_client)
- {
- for (uint32_t index = 0; index < MQTT_MAX_CLIENTS; index++)
- {
- if (m_mqtt_client[index] == p_client)
- {
- return index;
- }
- }
- return MQTT_MAX_CLIENTS;
- }
- void client_free(mqtt_client_t * const p_client)
- {
- MQTT_STATE_INIT(p_client);
- // Free memory used for TX packets and reset the pointer.
- nrf_free(p_client->p_packet);
- p_client->p_packet = NULL;
- // Free TLS instance and reset the instance.
- UNUSED_VARIABLE(nrf_tls_free(&p_client->tls_instance));
- NRF_TLS_INTSANCE_INIT(&p_client->tls_instance);
- }
- void client_init(mqtt_client_t * const p_client)
- {
- memset(p_client, 0, sizeof(*p_client));
- MQTT_STATE_INIT(p_client);
- p_client->protocol_version = MQTT_VERSION_3_1_0;
- p_client->clean_session = 1;
- NRF_TLS_INTSANCE_INIT(&p_client->tls_instance);
- }
- /**@brief Notifies event to the application.
- *
- * @param[in] p_client Identifies the client for which the procedure is requested.
- * @param[in] p_evt Reason for disconnection.
- */
- void event_notify(mqtt_client_t * const p_client, const mqtt_evt_t * p_evt, uint32_t flags)
- {
- const mqtt_evt_cb_t evt_cb = p_client->evt_cb;
- if (evt_cb != NULL)
- {
- MQTT_MUTEX_UNLOCK();
- evt_cb(p_client, p_evt);
- MQTT_MUTEX_LOCK();
- if (IS_SET(flags,MQTT_EVT_FLAG_INSTANCE_RESET))
- {
- client_init(p_client);
- }
- }
- }
- /**@brief Notifies disconnection event to the application.
- *
- * @param[in] p_client Identifies the client for which the procedure is requested.
- * @param[in] result Reason for disconnection.
- */
- void disconnect_event_notify(mqtt_client_t * p_client, uint32_t result)
- {
- mqtt_evt_t evt;
- const uint32_t client_index = get_client_index(p_client);
- // Remove the client from internal table.
- if (client_index != MQTT_MAX_CLIENTS)
- {
- m_mqtt_client[client_index] = NULL;
- }
- // Determine appropriate event to generate.
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED) ||
- MQTT_VERIFY_STATE(p_client, MQTT_STATE_DISCONNECTING))
- {
- evt.id = MQTT_EVT_DISCONNECT;
- evt.result = result;
- }
- else
- {
- evt.id = MQTT_EVT_CONNACK;
- evt.result = MQTT_CONNECTION_FAILED;
- }
- // Free the instance.
- client_free(p_client);
- // Notify application.
- event_notify(p_client, &evt, MQTT_EVT_FLAG_INSTANCE_RESET);
- }
- uint32_t mqtt_init(void)
- {
- SDK_MUTEX_INIT(m_mqtt_mutex);
- MQTT_MUTEX_LOCK();
- memset(m_mqtt_client, 0, sizeof(m_mqtt_client));
- MQTT_MUTEX_UNLOCK();
- return nrf_tls_init();
- }
- void mqtt_client_init(mqtt_client_t * const p_client)
- {
- NULL_PARAM_CHECK_VOID(p_client);
- MQTT_MUTEX_LOCK();
- client_init(p_client);
- MQTT_MUTEX_UNLOCK();
- }
- uint32_t mqtt_connect(mqtt_client_t * const p_client)
- {
- // Look for a free instance if available.
- uint32_t err_code = NRF_SUCCESS;
- uint32_t client_index = 0;
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_client->client_id.p_utf_str);
- MQTT_MUTEX_LOCK();
- for (client_index = 0; client_index < MQTT_MAX_CLIENTS; client_index++)
- {
- if (m_mqtt_client[client_index] == NULL)
- {
- // Found a free instance.
- m_mqtt_client[client_index] = p_client;
- // Allocate buffer packets in TX path.
- p_client->p_packet = nrf_malloc(MQTT_MAX_PACKET_LENGTH);
- break;
- }
- }
- if ((client_index == MQTT_MAX_CLIENTS) || (p_client->p_packet == NULL))
- {
- err_code = (NRF_ERROR_NO_MEM | IOT_MQTT_ERR_BASE);
- }
- else
- {
- err_code = tcp_request_connection(p_client);
- if (err_code != NRF_SUCCESS)
- {
- // Free the instance.
- m_mqtt_client[client_index] = NULL;
- nrf_free(p_client->p_packet);
- err_code = MQTT_ERR_TCP_PROC_FAILED;
- }
- }
- UNUSED_VARIABLE(p_client);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_publish(mqtt_client_t * const p_client,
- mqtt_publish_param_t const * const p_param)
- {
- uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
- uint32_t offset = 0;
- uint32_t mqtt_packetlen = 0;
- uint8_t * p_payload;
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Topic size 0x%08x, Data size 0x%08x",
- p_client,
- p_client->state,
- __func__,
- p_param->message.topic.topic.utf_strlen,
- p_param->message.payload.bin_strlen);
- MQTT_MUTEX_LOCK();
- p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
- {
- err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
- }
- else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
- {
- memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
- // Pack topic.
- err_code = pack_utf8_str(&p_param->message.topic.topic,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- if (err_code == NRF_SUCCESS)
- {
- if (p_param->message.topic.qos)
- {
- err_code = pack_uint16(p_param->message_id,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- }
- }
- if (err_code == NRF_SUCCESS)
- {
- // Pack message on the topic.
- err_code = pack_bin_str(&p_param->message.payload,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- }
- if (err_code == NRF_SUCCESS)
- {
- const uint8_t message_type = MQTT_MESSAGES_OPTIONS(MQTT_PKT_TYPE_PUBLISH,
- 0, // Duplicate flag not set.
- p_param->message.topic.qos,
- 0); // Retain flag not set.
- mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type
- offset, // Payload size without the fixed header
- &p_payload); // Address where the p_payload is contained.
- // Publish message.
- err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
- }
- }
- MQTT_TRC("<< %s", (uint32_t)__func__);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- /**@brief Encodes and sends messages that contain only message id in the variable header.
- *
- * @param[in] p_client Identifies the client for which the procedure is requested.
- * @param[in] op_code Opcode for the message.
- * @param[in] message_id Message id to be encoded in the variable header.
- *
- * @retval NRF_SUCCESS or an error code indicating a reason for failure.
- */
- uint32_t mqtt_message_id_only_enc_n_send(mqtt_client_t * const p_client,
- uint8_t opcode,
- uint16_t message_id)
- {
- uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
- uint32_t offset = 0;
- uint32_t mqtt_packetlen = 0;
- uint8_t * p_payload;
- p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
- {
- err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
- }
- else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
- {
- memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
- err_code = pack_uint16(message_id,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- if (err_code == NRF_SUCCESS)
- {
- const uint8_t message_type = MQTT_MESSAGES_OPTIONS(opcode,
- 0, // Duplicate flag not set.
- 0, // QoS unused.
- 0); // Retain flag not set.
- mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type
- offset, // Payload size without the fixed header
- &p_payload); // Address where the p_payload is contained.
- // Publish message.
- err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
- }
- }
- return err_code;
- }
- /**@brief Sends raw message to the peer.
- *
- * @param[in] p_client Identifies the client for which the procedure is requested.
- * @param[in] p_message Raw message to be sent to the peer.
- * @param[in] message_id Message id to be encoded in the variable header.
- *
- * @retval NRF_SUCCESS or an error code indicating a reason for failure.
- */
- uint32_t mqtt_raw_message_send(mqtt_client_t * const p_client,
- const uint8_t * p_message,
- uint16_t message_len)
- {
- uint32_t err_code;
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
- {
- err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
- }
- else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
- {
- err_code = mqtt_transport_write(p_client, p_message, message_len);
- }
- else
- {
- err_code = MQTT_ERR_NOT_CONNECTED;
- }
- return err_code;
- }
- uint32_t mqtt_publish_ack(mqtt_client_t * const p_client,
- mqtt_puback_param_t const * p_param)
- {
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
- p_client,
- p_client->state,
- __func__,
- p_param->message_id);
- MQTT_MUTEX_LOCK();
- uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
- MQTT_PKT_TYPE_PUBACK,
- p_param->message_id);
- MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
- p_client,
- p_client->state,
- __func__,
- err_code);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_publish_receive(mqtt_client_t * const p_client,
- mqtt_pubrec_param_t const * const p_param)
- {
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
- p_client,
- p_client->state,
- __func__,
- p_param->message_id);
- MQTT_MUTEX_LOCK();
- uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
- MQTT_PKT_TYPE_PUBREC,
- p_param->message_id);
- MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
- p_client,
- p_client->state,
- __func__,
- err_code);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_publish_release(mqtt_client_t * const p_client,
- mqtt_pubrel_param_t const * const p_param)
- {
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
- p_client,
- p_client->state,
- __func__,
- p_param->message_id);
- MQTT_MUTEX_LOCK();
- uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
- MQTT_PKT_TYPE_PUBREL,
- p_param->message_id);
- MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
- p_client,
- p_client->state,
- __func__,
- err_code);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_publish_complete(mqtt_client_t * const p_client,
- mqtt_pubcomp_param_t const * const p_param)
- {
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s Message id 0x%04x",
- p_client,
- p_client->state,
- __func__,
- p_param->message_id);
- MQTT_MUTEX_LOCK();
- uint32_t err_code = mqtt_message_id_only_enc_n_send(p_client,
- MQTT_PKT_TYPE_PUBCOMP,
- p_param->message_id);
- MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
- p_client,
- p_client->state,
- __func__,
- err_code);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_disconnect(mqtt_client_t * const p_client)
- {
- uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
- NULL_PARAM_CHECK(p_client);
- MQTT_MUTEX_LOCK();
- err_code = mqtt_raw_message_send(p_client, m_disc_packet, MQTT_FIXED_HEADER_SIZE);
- if (err_code == NRF_SUCCESS)
- {
- MQTT_SET_STATE_EXCLUSIVE(p_client, MQTT_STATE_DISCONNECTING);
- }
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_subscribe(mqtt_client_t * const p_client,
- mqtt_subscription_list_t const * const p_param)
- {
- uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
- uint32_t offset = 0;
- uint32_t count = 0;
- uint32_t mqtt_packetlen = 0;
- uint8_t * p_payload;
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_TRC("[CID %p]:[State 0x%02x]: >> %s message id 0x%04x topic count 0x%04x",
- p_client,
- p_client->state,
- __func__,
- p_param->message_id,
- p_param->list_count);
- MQTT_MUTEX_LOCK();
- p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
- {
- err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
- }
- else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
- {
- memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
- err_code = pack_uint16(p_param->message_id,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- if (err_code == NRF_SUCCESS)
- {
- do
- {
- err_code = pack_utf8_str(&p_param->p_list[count].topic,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- if (err_code == NRF_SUCCESS)
- {
- err_code = pack_uint8(p_param->p_list[count].qos,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- }
- count++;
- } while ((err_code != NRF_SUCCESS) || (count < p_param->list_count));
- }
- if (err_code == NRF_SUCCESS)
- {
- const uint8_t message_type = MQTT_MESSAGES_OPTIONS(MQTT_PKT_TYPE_SUBSCRIBE, 0, 1, 0);
- // Rewind the packet to encode the packet correctly.
- mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type, Duplicate Flag, QoS and retain flag setting.
- offset, // p_payload size without the fixed header
- &p_payload); // Address where the p_payload is contained. Header will encoded by rewinding the location.
- // Send message.
- err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
- }
- }
- MQTT_TRC("[CID %p]:[State 0x%02x]: << %s result 0x%08x",
- p_client,
- p_client->state,
- __func__,
- err_code);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_unsubscribe(mqtt_client_t * const p_client,
- mqtt_subscription_list_t const * const p_param)
- {
- uint32_t err_code = MQTT_ERR_NOT_CONNECTED;
- uint32_t count = 0;
- uint32_t offset = 0;
- uint32_t mqtt_packetlen = 0;
- uint8_t * p_payload;
- NULL_PARAM_CHECK(p_client);
- NULL_PARAM_CHECK(p_param);
- MQTT_MUTEX_LOCK();
- p_payload = &p_client->p_packet[MQTT_FIXED_HEADER_EXTENDED_SIZE];
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_PENDING_WRITE))
- {
- err_code = (NRF_ERROR_BUSY | IOT_MQTT_ERR_BASE);
- }
- else if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_CONNECTED))
- {
- memset(p_payload, 0, MQTT_MAX_PACKET_LENGTH);
- err_code = pack_uint16(p_param->message_id,
- MQTT_MAX_PACKET_LENGTH,
- p_payload,
- &offset);
- if (err_code == NRF_SUCCESS)
- {
- do
- {
- err_code = pack_utf8_str(&p_param->p_list[count].topic,
- MQTT_MAX_VARIABLE_HEADER_N_PAYLOAD,
- p_payload,
- &offset);
- count++;
- } while ((err_code != NRF_SUCCESS) || (count < p_param->list_count));
- }
- if (err_code == NRF_SUCCESS)
- {
- const uint8_t message_type = MQTT_MESSAGES_OPTIONS(MQTT_PKT_TYPE_UNSUBSCRIBE,
- 0, // Duplicate flag.
- MQTT_QoS_1_ATLEAST_ONCE,
- 0); // Retain flag.
- // Rewind the packet to encode the packet correctly.
- mqtt_packetlen = mqtt_encode_fixed_header(message_type, // Message type, Duplicate Flag, QoS and retain flag setting.
- offset, // Payload size without the fixed header
- &p_payload); // Address where the p_payload is contained. Header will encoded by rewinding the location.
- // Send message.
- err_code = mqtt_transport_write(p_client, p_payload, mqtt_packetlen);
- }
- }
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_ping(mqtt_client_t * const p_client)
- {
- uint32_t err_code;
- NULL_PARAM_CHECK(p_client);
- MQTT_MUTEX_LOCK();
- err_code = mqtt_raw_message_send(p_client, m_ping_packet, MQTT_PKT_HEADER_SIZE);
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t mqtt_abort(mqtt_client_t * const p_client)
- {
- MQTT_MUTEX_LOCK();
- NULL_PARAM_CHECK(p_client);
- if (p_client->state != MQTT_STATE_IDLE)
- {
- mqtt_client_tcp_abort(p_client);
- }
- MQTT_MUTEX_UNLOCK();
- return NRF_SUCCESS;
- }
- uint32_t mqtt_live(void)
- {
- iot_timer_time_in_ms_t elapsed_time;
- uint32_t index;
- // Note: The module should not be locked when calling this TLS API.
- nrf_tls_process();
- MQTT_MUTEX_LOCK();
- for (index = 0; index < MQTT_MAX_CLIENTS; index++)
- {
- mqtt_client_t * p_client = m_mqtt_client[index];
- if (p_client != NULL)
- {
- UNUSED_VARIABLE(iot_timer_wall_clock_delta_get(&p_client->last_activity,
- &elapsed_time));
- if ((MQTT_KEEPALIVE > 0) && (elapsed_time > ((MQTT_KEEPALIVE - 2) * 1000)))
- {
- UNUSED_VARIABLE(mqtt_ping(p_client));
- }
- if (p_client->p_pending_packet != NULL)
- {
- uint32_t err;
- err = mqtt_transport_write(p_client, p_client->p_pending_packet,
- p_client->pending_packetlen);
- if (err == NRF_SUCCESS)
- {
- p_client->p_pending_packet = NULL;
- p_client->pending_packetlen = 0;
- }
- }
- }
- }
- MQTT_MUTEX_UNLOCK();
- return NRF_SUCCESS;
- }
- uint32_t mqtt_input(mqtt_client_t * p_client, uint32_t timeout)
- {
- uint32_t err_code;
- NULL_PARAM_CHECK(p_client);
- MQTT_MUTEX_LOCK();
- MQTT_TRC("%s: 0x%08x", __func__, p_client->state);
- if (MQTT_VERIFY_STATE(p_client, MQTT_STATE_TCP_CONNECTED) ||
- MQTT_VERIFY_STATE(p_client, MQTT_STATE_DISCONNECTING))
- {
- err_code = tcp_receive_packet(p_client, timeout);
- }
- else
- {
- err_code = (NRF_ERROR_INVALID_STATE | IOT_MQTT_ERR_BASE);
- }
- MQTT_MUTEX_UNLOCK();
- return err_code;
- }
|