User-facing functions of the MQTT 3.1.1 library. More...
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 time elapsed since a given epoch 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_InitStatefulQoS (MQTTContext_t *pContext, MQTTPubAckInfo_t *pOutgoingPublishRecords, size_t outgoingPublishCount, MQTTPubAckInfo_t *pIncomingPublishRecords, size_t incomingPublishCount) |
Initialize an MQTT context for QoS > 0. 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_CancelCallback (const MQTTContext_t *pContext, uint16_t packetId) |
Cancels an outgoing publish callback (only for QoS > QoS0) by removing it from the pending ACK list. 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) |
Loop to receive packets from the transport interface. Handles keep alive. More... | |
MQTTStatus_t | MQTT_ReceiveLoop (MQTTContext_t *pContext) |
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 an 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 which can return the amount of time (in milliseconds) elapsed since a given epoch. This function will be used to ensure that timeouts in the API calls are met and keep-alive messages are sent on time. |
[in] | userCallback | The user callback to use with the context to notify about incoming packet events. |
[in] | pNetworkBuffer | Network buffer provided for the context. This buffer will be used to receive incoming messages from the broker. This buffer must remain valid and in scope for the entire lifetime of the pContext and must not be used by another context and/or application. |
Example
MQTTStatus_t MQTT_InitStatefulQoS | ( | MQTTContext_t * | pContext, |
MQTTPubAckInfo_t * | pOutgoingPublishRecords, | ||
size_t | outgoingPublishCount, | ||
MQTTPubAckInfo_t * | pIncomingPublishRecords, | ||
size_t | incomingPublishCount | ||
) |
Initialize an MQTT context for QoS > 0.
This function must be called on an MQTTContext_t after MQTT_Init and before any other function.
[in] | pContext | The context to initialize. |
[in] | pOutgoingPublishRecords | Pointer to memory which will be used to store state of outgoing publishes. |
[in] | outgoingPublishCount | Maximum number of records which can be kept in the memory pointed to by pOutgoingPublishRecords . |
[in] | pIncomingPublishRecords | Pointer to memory which will be used to store state of incoming publishes. |
[in] | incomingPublishCount | Maximum number of records which can be kept in the memory pointed to by pIncomingPublishRecords . |
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 | This value will be set to true if a previous session was present; otherwise it will be set to false. It is 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 | Array of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in @ pSubscriptionList array. |
[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_CancelCallback | ( | const MQTTContext_t * | pContext, |
uint16_t | packetId | ||
) |
Cancels an outgoing publish callback (only for QoS > QoS0) by removing it from the pending ACK list.
[in] | pContext | Initialized MQTT context. |
[in] | packetId | packet ID corresponding to the outstanding publish. |
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 | ) |
Loop to receive packets from the transport interface. Handles keep alive.
[in] | pContext | Initialized and connected MQTT context. |
Example
MQTTStatus_t MQTT_ReceiveLoop | ( | MQTTContext_t * | pContext | ) |
Loop to receive packets from the transport interface. Does not handle keep alive.
[in] | pContext | Initialized and connected MQTT context. |
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 | If the match is performed without any error, that is if the return value is MQTTSuccess, then and only then the value in this parameter is valid and updated. In such a case, if the topic filter and the topic name match, then this value is set to true; otherwise if there is no match then it is set to false. |
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. |