User-facing functions of the MQTT 5.0 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. | |
Typedefs | |
| typedef uint32_t(* | MQTTGetCurrentTimeFunc_t) (void) |
| Application provided function to query the time elapsed since a given epoch in milliseconds. | |
| typedef bool(* | MQTTEventCallback_t) (struct MQTTContext *pContext, struct MQTTPacketInfo *pPacketInfo, struct MQTTDeserializedInfo *pDeserializedInfo, enum MQTTSuccessFailReasonCode *pReasonCode, struct MQTTPropBuilder *pSendPropsBuffer, struct MQTTPropBuilder *pGetPropsBuffer) |
| Application callback for receiving incoming publishes and incoming acks, as well as adding properties to outgoing publish acks. | |
| typedef bool(* | MQTTStorePacketForRetransmit) (struct MQTTContext *pContext, uint32_t handle, MQTTVec_t *pMqttVec) |
| User defined callback used to store packets for retransmits. Used to track any publish/PUBREC retransmit on an unclean session connection. | |
| typedef bool(* | MQTTRetrievePacketForRetransmit) (struct MQTTContext *pContext, uint32_t handle, uint8_t **pSerializedMqttVec, size_t *pSerializedMqttVecLen) |
| User defined callback used to retreive a stored packet for resend operation. Used to track any publish/PUBREC retransmit on an unclean session connection. | |
| typedef void(* | MQTTClearPacketForRetransmit) (struct MQTTContext *pContext, uint32_t handle) |
| User defined callback used to clear a particular stored packet. Used to track any packet retransmit on an unclean session connection. | |
Enumerations | |
| enum | MQTTConnectionStatus_t { MQTTNotConnected , MQTTConnected , MQTTDisconnectPending } |
| 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. | |
| MQTTStatus_t | MQTT_InitStatefulQoS (MQTTContext_t *pContext, MQTTPubAckInfo_t *pOutgoingPublishRecords, size_t outgoingPublishCount, MQTTPubAckInfo_t *pIncomingPublishRecords, size_t incomingPublishCount, uint8_t *pAckPropsBuf, size_t ackPropsBufLength) |
| Initialize an MQTT context for QoS > 0. | |
| MQTTStatus_t | MQTT_InitRetransmits (MQTTContext_t *pContext, MQTTStorePacketForRetransmit storeFunction, MQTTRetrievePacketForRetransmit retrieveFunction, MQTTClearPacketForRetransmit clearFunction) |
| Initialize an MQTT context for publish retransmits for QoS > 0. | |
| MQTTStatus_t | MQTT_CheckConnectStatus (const MQTTContext_t *pContext) |
| Checks the MQTT connection status with the broker. | |
| MQTTStatus_t | MQTT_Connect (MQTTContext_t *pContext, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t timeoutMs, bool *pSessionPresent, MQTTPropBuilder_t *pPropertyBuilder, const MQTTPropBuilder_t *pWillPropertyBuilder) |
| Establish an MQTT session. | |
| MQTTStatus_t | MQTT_Subscribe (MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, const MQTTPropBuilder_t *pPropertyBuilder) |
| Sends MQTT SUBSCRIBE for the given list of topic filters to the broker. | |
| MQTTStatus_t | MQTT_Publish (MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, const MQTTPropBuilder_t *pPropertyBuilder) |
| Publishes a message to the given topic name. | |
| 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. | |
| MQTTStatus_t | MQTT_Ping (MQTTContext_t *pContext) |
| Sends an MQTT PINGREQ to broker. | |
| MQTTStatus_t | MQTT_Unsubscribe (MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, const MQTTPropBuilder_t *pPropertyBuilder) |
| Sends MQTT UNSUBSCRIBE for the given list of topic filters to the broker. | |
| MQTTStatus_t | MQTT_Disconnect (MQTTContext_t *pContext, const MQTTPropBuilder_t *pPropertyBuilder, const MQTTSuccessFailReasonCode_t *pReasonCode) |
| Sends MQTT DISCONNECT for a given reason code. | |
| MQTTStatus_t | MQTT_ProcessLoop (MQTTContext_t *pContext) |
| Loop to receive packets from the transport interface. Handles keep alive. | |
| MQTTStatus_t | MQTT_ReceiveLoop (MQTTContext_t *pContext) |
| Loop to receive packets from the transport interface. Does not handle keep alive. | |
| uint16_t | MQTT_GetPacketId (MQTTContext_t *pContext) |
| Get a packet ID that is valid according to the MQTT 5.0 spec. | |
| MQTTStatus_t | MQTT_MatchTopic (const char *pTopicName, const size_t topicNameLength, const char *pTopicFilter, const size_t topicFilterLength, bool *pIsMatch) |
| A utility function that determines whether the passed topic filter and topic name match according to the MQTT 5.0 protocol specification. | |
| 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. | |
| const char * | MQTT_Status_strerror (MQTTStatus_t status) |
| Error code to string conversion for MQTT statuses. | |
| MQTTStatus_t | MQTT_GetBytesInMQTTVec (const MQTTVec_t *pVec, size_t *pOutput) |
| Get the bytes in a MQTTVec pointer which can store the whole array as a an MQTT packet when calling MQTT_SerializeMQTTVec( void * pAllocatedMem, MQTTVec_t *pVec ) function. | |
| void | MQTT_SerializeMQTTVec (uint8_t *pAllocatedMem, const MQTTVec_t *pVec) |
Serialize the bytes in an array of MQTTVec in the provided pAllocatedMem. | |
| const char * | MQTT_GetPacketTypeString (uint8_t packetType) |
| Get a human-readable string representation of an MQTT packet type. | |
User-facing functions of the MQTT 5.0 library.
| typedef bool(* MQTTStorePacketForRetransmit) (struct MQTTContext *pContext, uint32_t handle, MQTTVec_t *pMqttVec) |
User defined callback used to store packets for retransmits. Used to track any publish/PUBREC retransmit on an unclean session connection.
| [in] | pContext | Initialised MQTT Context. |
| [in] | handle | Unique 32-bit handle to distinguish the packets. Note that these values may or may not be in order. The value is interpreted by the coreMQTT library and might change in pattern. |
| [in] | pMqttVec | Pointer to the opaque mqtt vector structure. Users should use MQTT_GetBytesInMQTTVec and MQTT_SerializeMQTTVec functions to get the memory required and to serialize the MQTTVec_t in the provided memory respectively. |
| typedef bool(* MQTTRetrievePacketForRetransmit) (struct MQTTContext *pContext, uint32_t handle, uint8_t **pSerializedMqttVec, size_t *pSerializedMqttVecLen) |
User defined callback used to retreive a stored packet for resend operation. Used to track any publish/PUBREC retransmit on an unclean session connection.
| [in] | pContext | Initialised MQTT Context. |
| [in] | handle | Unique 32-bit handle to distinguish the packets. Note that these values may or may not be in order. The value is interpreted by the coreMQTT library and might change in pattern. |
| [out] | pSerializedMqttVec | Output parameter to store the pointer to the serialized MQTTVec_t using MQTT_SerializeMQTTVec. |
| [out] | pSerializedMqttVecLen | Output parameter to return the number of bytes used to store the MQTTVec_t. This value should be the same as the one received from MQTT_GetBytesInMQTTVec when storing the packet. |
| typedef void(* MQTTClearPacketForRetransmit) (struct MQTTContext *pContext, uint32_t handle) |
User defined callback used to clear a particular stored packet. Used to track any packet retransmit on an unclean session connection.
| [in] | pContext | Initialised MQTT Context. |
| [in] | handle | Unique 32-bit handle to distinguish the packets. Note that these values may or may not be in order. The value is interpreted by the coreMQTT library and might change in pattern. |
| 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, | ||
| uint8_t * | pAckPropsBuf, | ||
| size_t | ackPropsBufLength | ||
| ) |
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. |
| [in] | pAckPropsBuf | Pointer to memory which will be used to store properties of outgoing publish-ACKS. |
| [in] | ackPropsBufLength | Length of the buffer pointed to by pBuffer. |
Example
| MQTTStatus_t MQTT_InitRetransmits | ( | MQTTContext_t * | pContext, |
| MQTTStorePacketForRetransmit | storeFunction, | ||
| MQTTRetrievePacketForRetransmit | retrieveFunction, | ||
| MQTTClearPacketForRetransmit | clearFunction | ||
| ) |
Initialize an MQTT context for publish retransmits for QoS > 0.
This function must be called on an MQTTContext_t after MQTT_InitstatefulQoS and before any other function.
| [in] | pContext | The context to initialize. |
| [in] | storeFunction | User defined API used to store outgoing publishes. |
| [in] | retrieveFunction | User defined API used to retreive a copied publish for resend operation. |
| [in] | clearFunction | User defined API used to clear a particular copied publish packet. |
Example
| MQTTStatus_t MQTT_CheckConnectStatus | ( | const MQTTContext_t * | pContext | ) |
Checks the MQTT connection status with the broker.
| [in] | pContext | Initialized MQTT context. |
Example
| MQTTStatus_t MQTT_Connect | ( | MQTTContext_t * | pContext, |
| const MQTTConnectInfo_t * | pConnectInfo, | ||
| const MQTTPublishInfo_t * | pWillInfo, | ||
| uint32_t | timeoutMs, | ||
| bool * | pSessionPresent, | ||
| MQTTPropBuilder_t * | pPropertyBuilder, | ||
| const MQTTPropBuilder_t * | pWillPropertyBuilder | ||
| ) |
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. |
| [in] | pPropertyBuilder | Properties to be sent in the outgoing packet. |
| [in] | pWillPropertyBuilder | Will Properties to be sent in the outgoing packet. |
| [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;pPropertyBuilder to NULL), coreMQTT will add a property to the connect packet to limit the maximum packet size that the broker can send to be equal to the application provided buffer to the MQTT_Init API. This prevents the case when the packet is bigger than the buffer at which point, coreMQTT cannot handle the packet, neither can it drop it as MQTT protocol doesn't allow it.Functions to add optional properties to the CONNECT packet are:
Connect Properties:
Will Properties:
Example
Update the maximum number of concurrent incoming and outgoing PUBLISH records based on MQTT 5.0 Receive Maximum property :
Initialize the client's keep-alive timer using the Server Keep Alive value received in the CONNACK. This value overrides the client's original keep-alive setting, as per MQTT v5 specification.
| MQTTStatus_t MQTT_Subscribe | ( | MQTTContext_t * | pContext, |
| const MQTTSubscribeInfo_t * | pSubscriptionList, | ||
| size_t | subscriptionCount, | ||
| uint16_t | packetId, | ||
| const MQTTPropBuilder_t * | pPropertyBuilder | ||
| ) |
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. |
| [in] | pPropertyBuilder | Properties to be sent in the outgoing packet. |
Functions to add optional properties to the SUBSCRIBE packet are:
Example
| MQTTStatus_t MQTT_Publish | ( | MQTTContext_t * | pContext, |
| const MQTTPublishInfo_t * | pPublishInfo, | ||
| uint16_t | packetId, | ||
| const MQTTPropBuilder_t * | pPropertyBuilder | ||
| ) |
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. |
| [in] | pPropertyBuilder | Properties to be sent in the outgoing packet. |
Functions to add optional properties to the PUBLISH packet are:
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, | ||
| const MQTTPropBuilder_t * | pPropertyBuilder | ||
| ) |
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. |
| [in] | pPropertyBuilder | Properties to be sent in the outgoing packet. |
Functions to add optional properties to the UNSUBSCRIBE packet are:
Example
| MQTTStatus_t MQTT_Disconnect | ( | MQTTContext_t * | pContext, |
| const MQTTPropBuilder_t * | pPropertyBuilder, | ||
| const MQTTSuccessFailReasonCode_t * | pReasonCode | ||
| ) |
Sends MQTT DISCONNECT for a given reason code.
| [in] | pContext | Initialized and connected MQTT context. |
| [in] | pPropertyBuilder | Properties to be sent in the outgoing packet. |
| [in] | pReasonCode | Optional reason code to be sent in the DISCONNECT packet. If NULL, then no reason code is sent. |
Functions to add optional properties to the DISCONNECT packet are:
Example
| 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 5.0 spec.
| [in] | pContext | Initialized MQTT context. |
| MQTTStatus_t MQTT_MatchTopic | ( | const char * | pTopicName, |
| const size_t | topicNameLength, | ||
| const char * | pTopicFilter, | ||
| const size_t | topicFilterLength, | ||
| bool * | pIsMatch | ||
| ) |
A utility function that determines whether the passed topic filter and topic name match according to the MQTT 5.0 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:
Refer to MQTTSubAckStatus_t for the status codes.
| [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. |
| MQTTStatus_t MQTT_GetBytesInMQTTVec | ( | const MQTTVec_t * | pVec, |
| size_t * | pOutput | ||
| ) |
Get the bytes in a MQTTVec pointer which can store the whole array as a an MQTT packet when calling MQTT_SerializeMQTTVec( void * pAllocatedMem, MQTTVec_t *pVec ) function.
| [in] | pVec | The MQTTVec pointer given as input to the user defined MQTTStorePacketForRetransmit callback function. Must not be NULL. |
| [out] | pOutput | The number of bytes in the vector. This value is invalid if the status is not MQTTSuccess. |
| void MQTT_SerializeMQTTVec | ( | uint8_t * | pAllocatedMem, |
| const MQTTVec_t * | pVec | ||
| ) |
Serialize the bytes in an array of MQTTVec in the provided pAllocatedMem.
| [in] | pAllocatedMem | Memory in which to serialize the data in the MQTTVec array. It must be of size provided by MQTT_GetBytesInMQTTVec( const MQTTVec_t * pVec, size_t * pOutput ). Must not be NULL. |
| [in] | pVec | The MQTTVec pointer given as input to the user defined MQTTStorePacketForRetransmit callback function. Must not be NULL. |
| const char * MQTT_GetPacketTypeString | ( | uint8_t | packetType | ) |
Get a human-readable string representation of an MQTT packet type.
This function converts an MQTT packet type byte into a corresponding string representation for debugging and logging purposes.
| [in] | packetType | The MQTT packet type byte to convert. |
Example