User-facing functions of the MQTT 3.1.1 library. More...
#include "core_mqtt_config.h"
#include "core_mqtt_config_defaults.h"
#include "core_mqtt_serializer.h"
#include "transport_interface.h"
Go to the source code of this file.
Data Structures | |
struct | MQTTPubAckInfo_t |
An element of the state engine records for QoS 1 or Qos 2 publishes. More... | |
struct | MQTTContext_t |
A struct representing an MQTT connection. More... | |
struct | MQTTDeserializedInfo_t |
Struct to hold deserialized packet information for an MQTTEventCallback_t callback. More... | |
Macros | |
#define | MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U ) |
Invalid packet identifier. More... | |
Typedefs | |
typedef uint32_t(* | MQTTGetCurrentTimeFunc_t) (void) |
Application provided function to query the current time in milliseconds. More... | |
typedef void(* | MQTTEventCallback_t) (struct MQTTContext *pContext, struct MQTTPacketInfo *pPacketInfo, struct MQTTDeserializedInfo *pDeserializedInfo) |
Application callback for receiving incoming publishes and incoming acks. More... | |
Enumerations | |
enum | MQTTConnectionStatus_t { MQTTNotConnected , MQTTConnected } |
Values indicating if an MQTT connection exists. More... | |
enum | MQTTPublishState_t { MQTTStateNull = 0 , MQTTPublishSend , MQTTPubAckSend , MQTTPubRecSend , MQTTPubRelSend , MQTTPubCompSend , MQTTPubAckPending , MQTTPubRecPending , MQTTPubRelPending , MQTTPubCompPending , MQTTPublishDone } |
The state of QoS 1 or QoS 2 MQTT publishes, used in the state engine. More... | |
enum | MQTTPubAckType_t { MQTTPuback , MQTTPubrec , MQTTPubrel , MQTTPubcomp } |
Packet types used in acknowledging QoS 1 or QoS 2 publishes. More... | |
enum | MQTTSubAckStatus_t { MQTTSubAckSuccessQos0 = 0x00 , MQTTSubAckSuccessQos1 = 0x01 , MQTTSubAckSuccessQos2 = 0x02 , MQTTSubAckFailure = 0x80 } |
The status codes in the SUBACK response to a subscription request. More... | |
Functions | |
MQTTStatus_t | MQTT_Init (MQTTContext_t *pContext, const TransportInterface_t *pTransportInterface, MQTTGetCurrentTimeFunc_t getTimeFunction, MQTTEventCallback_t userCallback, const MQTTFixedBuffer_t *pNetworkBuffer) |
Initialize an MQTT context. More... | |
MQTTStatus_t | MQTT_Connect (MQTTContext_t *pContext, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t timeoutMs, bool *pSessionPresent) |
Establish an MQTT session. More... | |
MQTTStatus_t | MQTT_Subscribe (MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId) |
Sends MQTT SUBSCRIBE for the given list of topic filters to the broker. More... | |
MQTTStatus_t | MQTT_Publish (MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId) |
Publishes a message to the given topic name. More... | |
MQTTStatus_t | MQTT_Ping (MQTTContext_t *pContext) |
Sends an MQTT PINGREQ to broker. More... | |
MQTTStatus_t | MQTT_Unsubscribe (MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId) |
Sends MQTT UNSUBSCRIBE for the given list of topic filters to the broker. More... | |
MQTTStatus_t | MQTT_Disconnect (MQTTContext_t *pContext) |
Disconnect an MQTT session. More... | |
MQTTStatus_t | MQTT_ProcessLoop (MQTTContext_t *pContext, uint32_t timeoutMs) |
Loop to receive packets from the transport interface. Handles keep alive. More... | |
MQTTStatus_t | MQTT_ReceiveLoop (MQTTContext_t *pContext, uint32_t timeoutMs) |
Loop to receive packets from the transport interface. Does not handle keep alive. More... | |
uint16_t | MQTT_GetPacketId (MQTTContext_t *pContext) |
Get a packet ID that is valid according to the MQTT 3.1.1 spec. More... | |
MQTTStatus_t | MQTT_MatchTopic (const char *pTopicName, const uint16_t topicNameLength, const char *pTopicFilter, const uint16_t topicFilterLength, bool *pIsMatch) |
A utility function that determines whether the passed topic filter and topic name match according to the MQTT 3.1.1 protocol specification. More... | |
MQTTStatus_t | MQTT_GetSubAckStatusCodes (const MQTTPacketInfo_t *pSubackPacket, uint8_t **pPayloadStart, size_t *pPayloadSize) |
Parses the payload of an MQTT SUBACK packet that contains status codes corresponding to topic filter subscription requests from the original subscribe packet. More... | |
const char * | MQTT_Status_strerror (MQTTStatus_t status) |
Error code to string conversion for MQTT statuses. More... | |
User-facing functions of the MQTT 3.1.1 library.
MQTTStatus_t MQTT_Init | ( | MQTTContext_t * | pContext, |
const TransportInterface_t * | pTransportInterface, | ||
MQTTGetCurrentTimeFunc_t | getTimeFunction, | ||
MQTTEventCallback_t | userCallback, | ||
const MQTTFixedBuffer_t * | pNetworkBuffer | ||
) |
Initialize an MQTT context.
This function must be called on a MQTTContext_t before any other function.
[in] | pContext | The context to initialize. |
[in] | pTransportInterface | The transport interface to use with the context. |
[in] | getTimeFunction | The time utility function to use with the context. |
[in] | userCallback | The user callback to use with the context to notify about incoming packet events. |
[in] | pNetworkBuffer | Network buffer provided for the context. |
Example
MQTTStatus_t MQTT_Connect | ( | MQTTContext_t * | pContext, |
const MQTTConnectInfo_t * | pConnectInfo, | ||
const MQTTPublishInfo_t * | pWillInfo, | ||
uint32_t | timeoutMs, | ||
bool * | pSessionPresent | ||
) |
Establish an MQTT session.
This function will send MQTT CONNECT packet and receive a CONNACK packet. The send and receive from the network is done through the transport interface.
The maximum time this function waits for a CONNACK is decided in one of the following ways:
timeoutMs
is greater than 0: MQTTContext_t.getTime is used to ensure that the function does not wait more than timeoutMs
for CONNACK.timeoutMs
is 0: The network receive for CONNACK is retried up to the number of times configured by MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT.[in] | pContext | Initialized MQTT context. |
[in] | pConnectInfo | MQTT CONNECT packet information. |
[in] | pWillInfo | Last Will and Testament. Pass NULL if Last Will and Testament is not used. |
[in] | timeoutMs | Maximum time in milliseconds to wait for a CONNACK packet. A zero timeout makes use of the retries for receiving CONNACK as configured with MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT. |
[out] | pSessionPresent | Whether a previous session was present. Only relevant if not establishing a clean session. |
timeoutMs
for CONNACK; MQTTSuccess otherwise.Example
MQTTStatus_t MQTT_Subscribe | ( | MQTTContext_t * | pContext, |
const MQTTSubscribeInfo_t * | pSubscriptionList, | ||
size_t | subscriptionCount, | ||
uint16_t | packetId | ||
) |
Sends MQTT SUBSCRIBE for the given list of topic filters to the broker.
[in] | pContext | Initialized MQTT context. |
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[in] | packetId | Packet ID generated by MQTT_GetPacketId. |
Example
MQTTStatus_t MQTT_Publish | ( | MQTTContext_t * | pContext, |
const MQTTPublishInfo_t * | pPublishInfo, | ||
uint16_t | packetId | ||
) |
Publishes a message to the given topic name.
[in] | pContext | Initialized MQTT context. |
[in] | pPublishInfo | MQTT PUBLISH packet parameters. |
[in] | packetId | packet ID generated by MQTT_GetPacketId. |
Example
MQTTStatus_t MQTT_Ping | ( | MQTTContext_t * | pContext | ) |
Sends an MQTT PINGREQ to broker.
[in] | pContext | Initialized and connected MQTT context. |
MQTTStatus_t MQTT_Unsubscribe | ( | MQTTContext_t * | pContext, |
const MQTTSubscribeInfo_t * | pSubscriptionList, | ||
size_t | subscriptionCount, | ||
uint16_t | packetId | ||
) |
Sends MQTT UNSUBSCRIBE for the given list of topic filters to the broker.
[in] | pContext | Initialized MQTT context. |
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[in] | packetId | packet ID generated by MQTT_GetPacketId. |
Example
MQTTStatus_t MQTT_Disconnect | ( | MQTTContext_t * | pContext | ) |
Disconnect an MQTT session.
[in] | pContext | Initialized and connected MQTT context. |
MQTTStatus_t MQTT_ProcessLoop | ( | MQTTContext_t * | pContext, |
uint32_t | timeoutMs | ||
) |
Loop to receive packets from the transport interface. Handles keep alive.
[in] | pContext | Initialized and connected MQTT context. |
[in] | timeoutMs | Minimum time in milliseconds that the receive loop will run, unless an error occurs. |
timeoutMs
, the configuration macros, MQTT_RECV_POLLING_TIMEOUT_MS and MQTT_SEND_RETRY_TIMEOUT_MS, and the underlying transport interface implementation timeouts, unless an error occurs. The blocking period also depends on the execution time of the MQTTEventCallback_t callback supplied to the library. It is recommended that the supplied MQTTEventCallback_t callback does not contain blocking operations to prevent potential non-deterministic blocking period of the MQTT_ProcessLoop API call.Example
MQTTStatus_t MQTT_ReceiveLoop | ( | MQTTContext_t * | pContext, |
uint32_t | timeoutMs | ||
) |
Loop to receive packets from the transport interface. Does not handle keep alive.
[in] | pContext | Initialized and connected MQTT context. |
[in] | timeoutMs | Minimum time in milliseconds that the receive loop will run, unless an error occurs. |
timeoutMs
, the configuration macros, MQTT_RECV_POLLING_TIMEOUT_MS and MQTT_SEND_RETRY_TIMEOUT_MS, and the underlying transport interface implementation timeouts, unless an error occurs. The blocking period also depends on the execution time of the MQTTEventCallback_t callback supplied to the library. It is recommended that the supplied MQTTEventCallback_t callback does not contain blocking operations to prevent potential non-deterministic blocking period of the MQTT_ReceiveLoop API call.Example
uint16_t MQTT_GetPacketId | ( | MQTTContext_t * | pContext | ) |
Get a packet ID that is valid according to the MQTT 3.1.1 spec.
[in] | pContext | Initialized MQTT context. |
MQTTStatus_t MQTT_MatchTopic | ( | const char * | pTopicName, |
const uint16_t | topicNameLength, | ||
const char * | pTopicFilter, | ||
const uint16_t | topicFilterLength, | ||
bool * | pIsMatch | ||
) |
A utility function that determines whether the passed topic filter and topic name match according to the MQTT 3.1.1 protocol specification.
[in] | pTopicName | The topic name to check. |
[in] | topicNameLength | Length of the topic name. |
[in] | pTopicFilter | The topic filter to check. |
[in] | topicFilterLength | Length of topic filter. |
[out] | pIsMatch | This is filled with the whether there exists a match or not. |
Example
MQTTStatus_t MQTT_GetSubAckStatusCodes | ( | const MQTTPacketInfo_t * | pSubackPacket, |
uint8_t ** | pPayloadStart, | ||
size_t * | pPayloadSize | ||
) |
Parses the payload of an MQTT SUBACK packet that contains status codes corresponding to topic filter subscription requests from the original subscribe packet.
Each return code in the SUBACK packet corresponds to a topic filter in the SUBSCRIBE Packet being acknowledged. The status codes can be one of the following:
[in] | pSubackPacket | The SUBACK packet whose payload is to be parsed. |
[out] | pPayloadStart | This is populated with the starting address of the payload (or return codes for topic filters) in the SUBACK packet. |
[out] | pPayloadSize | This is populated with the size of the payload in the SUBACK packet. It represents the number of topic filters whose SUBACK status is present in the packet. |
Example
const char * MQTT_Status_strerror | ( | MQTTStatus_t | status | ) |
Error code to string conversion for MQTT statuses.
[in] | status | The status to convert to a string. |