Implements the user-facing functions in core_mqtt.h. More...
#include <string.h>#include <assert.h>#include <stdbool.h>#include <stdint.h>#include <inttypes.h>#include <stddef.h>#include "core_mqtt.h"#include "core_mqtt_serializer.h"#include "transport_interface.h"#include "core_mqtt_state.h"#include "private/core_mqtt_serializer_private.h"#include "core_mqtt_config_defaults.h"Data Structures | |
| struct | MQTTVec_t |
| An opaque structure provided by the library to the MQTTStorePacketForRetransmit function when using MQTTStorePacketForRetransmit. More... | |
| struct | IoVecState_t |
| Tracks the state of building a scatter-gather IO vector list. More... | |
Macros | |
| #define | MQTT_PRE_STATE_UPDATE_HOOK(pContext) |
| Hook called just before an update to the MQTT state is made. | |
| #define | MQTT_POST_STATE_UPDATE_HOOK(pContext) |
| Hook called just after an update to the MQTT state has been made. | |
| #define | CORE_MQTT_SERIALIZED_LENGTH_FIELD_BYTES ( 2U ) |
| Bytes required to encode any string length in an MQTT packet header. Length is always encoded in two bytes according to the MQTT specification. | |
| #define | CORE_MQTT_SUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 3U ) |
| Number of vectors required to encode one topic filter in a subscribe request. Three vectors are required as there are three fields in the subscribe request namely: | |
| #define | CORE_MQTT_UNSUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 2U ) |
| Number of vectors required to encode one topic filter in an unsubscribe request. Two vectors are required as there are two fields in the unsubscribe request namely: | |
| #define | SET_INCOMING_PUB_FLAG(packetID) ( ( uint32_t ) ( ( ( uint32_t ) packetID ) | ( ( ( uint32_t ) 1U ) << 16U ) ) ) |
| Set flag in the packet ID just beyond the actual packet ID. | |
Functions | |
| static int32_t | sendBuffer (MQTTContext_t *pContext, const uint8_t *pBufferToSend, size_t bytesToSend) |
| Sends provided buffer to network using transport send. | |
| static MQTTStatus_t | sendConnectWithoutCopy (MQTTContext_t *pContext, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t remainingLength, const MQTTPropBuilder_t *pPropertyBuilder, const MQTTPropBuilder_t *pWillPropertyBuilder) |
| Sends MQTT connect without copying the users data into any buffer. | |
| static int32_t | sendMessageVector (MQTTContext_t *pContext, TransportOutVector_t *pIoVec, size_t ioVecCount) |
| Sends the vector array passed through the parameters over the network. | |
| static size_t | addEncodedStringToVector (uint8_t serializedLength[CORE_MQTT_SERIALIZED_LENGTH_FIELD_BYTES], const char *const string, uint16_t length, TransportOutVector_t *iterator, uint32_t *updatedLength) |
| Add a string and its length after serializing it in a manner outlined by the MQTT specification. | |
| static MQTTStatus_t | sendSubscribeWithoutCopy (MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, uint32_t remainingLength, const MQTTPropBuilder_t *pPropertyBuilder) |
| Send Subscribe without copying the users data into any buffer. | |
| static MQTTStatus_t | sendUnsubscribeWithoutCopy (MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, uint32_t remainingLength, const MQTTPropBuilder_t *pPropertyBuilder) |
| Send Unsubscribe without copying the users data into any buffer. | |
| static uint32_t | calculateElapsedTime (uint32_t later, uint32_t start) |
| Calculate the interval between two millisecond timestamps, including when the later value has overflowed. | |
| static MQTTPubAckType_t | getAckFromPacketType (uint8_t packetType) |
| Convert a byte indicating a publish ack type to an MQTTPubAckType_t. | |
| static int32_t | recvExact (MQTTContext_t *pContext, size_t bytesToRecv) |
| Receive bytes into the network buffer. | |
| static MQTTStatus_t | receiveConnackPacket (MQTTContext_t *pContext, MQTTPacketInfo_t incomingPacket) |
| Receive a CONNACK packet from the transport interface. | |
| static uint8_t | getAckTypeToSend (MQTTPublishState_t state) |
| Get the correct ack type to send. | |
| static MQTTStatus_t | sendPublishAcks (MQTTContext_t *pContext, uint16_t packetId, MQTTPublishState_t publishState) |
| Send acks for received QoS 1/2 publishes. | |
| static MQTTStatus_t | handleKeepAlive (MQTTContext_t *pContext) |
| Send a keep alive PINGREQ if the keep alive interval has elapsed. | |
| static MQTTStatus_t | handleIncomingPublish (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket) |
| Handle received MQTT PUBLISH packet. | |
| static MQTTStatus_t | handlePublishAcks (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket) |
| Handle received MQTT publish acks. | |
| static MQTTStatus_t | handleIncomingAck (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket, bool manageKeepAlive) |
| Handle received MQTT ack. | |
| static MQTTStatus_t | receiveSingleIteration (MQTTContext_t *pContext, bool manageKeepAlive) |
| Run a single iteration of the receive loop. | |
| static MQTTStatus_t | validateSubscribeUnsubscribeParams (const MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, MQTTSubscriptionType_t subscriptionType) |
| Validates parameters of MQTT_Subscribe or MQTT_Unsubscribe. | |
| static MQTTStatus_t | receiveConnack (MQTTContext_t *pContext, uint32_t timeoutMs, bool cleanSession, MQTTPacketInfo_t *pIncomingPacket, bool *pSessionPresent) |
| Receives a CONNACK MQTT packet. | |
| static MQTTStatus_t | handleUncleanSessionResumption (MQTTContext_t *pContext) |
| Resends pending acks for a re-established MQTT session. | |
| static MQTTStatus_t | handleCleanSession (MQTTContext_t *pContext) |
| Clears existing state records for a clean session. | |
| static MQTTStatus_t | sendPublishWithoutCopy (MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint8_t *pMqttHeader, size_t headerSize, uint16_t packetId, const MQTTPropBuilder_t *pPropertyBuilder) |
| Send the publish packet without copying the topic string and payload in the buffer. | |
| static MQTTStatus_t | validatePublishParams (const MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId) |
| Function to validate MQTT_Publish parameters. | |
| static bool | matchEndWildcardsSpecialCases (const char *pTopicFilter, uint16_t topicFilterLength, uint16_t filterIndex) |
| Performs matching for special cases when a topic filter ends with a wildcard character. | |
| static bool | matchWildcards (const char *pTopicName, uint16_t topicNameLength, const char *pTopicFilter, uint16_t topicFilterLength, uint16_t *pNameIndex, uint16_t *pFilterIndex, bool *pMatch) |
| Attempt to match topic name with a topic filter starting with a wildcard. | |
| static bool | matchTopicFilter (const char *pTopicName, uint16_t topicNameLength, const char *pTopicFilter, uint16_t topicFilterLength) |
| Match a topic name and topic filter allowing the use of wildcards. | |
| static MQTTStatus_t | validateTopicFilter (const MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t iterator, MQTTSubscriptionType_t subscriptionType) |
| Validate the topic filter in a subscription. | |
| static bool | checkWildcardSubscriptions (uint8_t isWildcardAvailable, const MQTTSubscribeInfo_t *pSubscriptionList, size_t iterator) |
| Check if wildcard subscriptions are allowed and valid. | |
| static MQTTStatus_t | validateSharedSubscriptions (const MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, const size_t iterator) |
| Validate Shared Subscriptions. | |
| static void | addSubscriptionOptions (const MQTTSubscribeInfo_t subscriptionInfo, uint8_t *pSubscriptionOptionsArray, size_t currentOptionIndex) |
| Add subscription options to the options array. | |
| static MQTTStatus_t | handleSubUnsubAck (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket) |
| Handle Incoming Subscribe ACK. | |
| static MQTTStatus_t | sendPublishAcksWithoutProperty (MQTTContext_t *pContext, uint16_t packetId, MQTTPublishState_t publishState) |
| Send acks for received QoS 1/2 publishes. This function is used to send Publish Acks without any properties or reason codes. | |
| static MQTTStatus_t | sendPublishAcksWithProperty (MQTTContext_t *pContext, uint16_t packetId, MQTTPublishState_t publishState, MQTTSuccessFailReasonCode_t reasonCode) |
| Send acks for received QoS 1/2 publishes with properties. | |
| static MQTTStatus_t | validatePublishAckReasonCode (MQTTSuccessFailReasonCode_t reasonCode, uint8_t packetType) |
| Validate Publish Ack Reason Code. | |
| static MQTTStatus_t | sendDisconnectWithoutCopy (MQTTContext_t *pContext, const MQTTSuccessFailReasonCode_t *pReasonCode, uint32_t remainingLength, const MQTTPropBuilder_t *pPropertyBuilder) |
| Send the disconnect packet without copying the reason code and properties in the buffer. | |
| static MQTTStatus_t | handleIncomingDisconnect (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket) |
| Handle Incoming Disconnect. | |
| static MQTTStatus_t | buildAndSendAckWithProps (MQTTContext_t *pContext, uint8_t packetTypeByte, uint16_t packetId, MQTTSuccessFailReasonCode_t reasonCode, uint32_t remainingLength, size_t ackPropertyLength) |
| Build the IO vector for a publish ACK with properties and send it. Returns MQTTSuccess, MQTTSendFailed, MQTTBadParameter, or MQTTPublishStoreFailed. | |
| static MQTTStatus_t | addStringToVectorChecked (uint8_t *pLenBuf, const char *pStr, uint16_t strLen, IoVecState_t *pVecState) |
| Add an encoded string to the IO vector if it fits within maxPacketSize. Returns MQTTBadParameter if the addition would overflow. | |
| static MQTTStatus_t | appendWillVectors (const MQTTPublishInfo_t *pWillInfo, const MQTTPropBuilder_t *pWillPropertyBuilder, IoVecState_t *pVecState, uint8_t *pWillPropertyLength, uint8_t *pSerializedTopicLength, uint8_t *pSerializedPayloadLength) |
| Append will properties, topic, and payload vectors to the IO vector. | |
| 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_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_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_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. | |
Implements the user-facing functions in core_mqtt.h.
| #define CORE_MQTT_SUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 3U ) |
Number of vectors required to encode one topic filter in a subscribe request. Three vectors are required as there are three fields in the subscribe request namely:
| #define CORE_MQTT_UNSUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 2U ) |
Number of vectors required to encode one topic filter in an unsubscribe request. Two vectors are required as there are two fields in the unsubscribe request namely:
|
static |
Sends provided buffer to network using transport send.
param[in] pContext Initialized MQTT context.
param[in] pBufferToSend Buffer to be sent to network.
param[in] bytesToSend Number of bytes to be sent.
|
static |
Sends MQTT connect without copying the users data into any buffer.
| [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] | remainingLength | the length of the connect packet. |
| [in] | pPropertyBuilder | Property builder containing CONNECT properties. |
| [in] | pWillPropertyBuilder | Property builder containing Last Will And Testament properties. |
Maximum number of bytes to send the Property Length. Property Length 0 + 4 = 4
|
static |
Sends the vector array passed through the parameters over the network.
| [in] | pContext | Initialized MQTT context. |
| [in] | pIoVec | The vector array to be sent. |
| [in] | ioVecCount | The number of elements in the array. |
|
static |
Add a string and its length after serializing it in a manner outlined by the MQTT specification.
| [in] | serializedLength | Array of two bytes to which the vector will point. The array must remain in scope until the message has been sent. |
| [in] | string | The string to be serialized. |
| [in] | length | The length of the string to be serialized. |
| [in] | iterator | The iterator pointing to the first element in the transport interface IO array. |
| [out] | updatedLength | This parameter will be added to with the number of bytes added to the vector. |
|
static |
Send Subscribe without copying the users data into any buffer.
| [in] | pContext | Initialized MQTT context. |
| [in] | pSubscriptionList | List of MQTT subscription info. |
| [in] | subscriptionCount | Number of elements in pSubscriptionList. |
| [in] | packetId | Packet identifier. |
| [in] | remainingLength | Remaining length of the packet. |
| [in] | pPropertyBuilder | MQTT property builder. |
Fixed Size Properties
Maximum number of bytes by the fixed header of a SUBSCRIBE packet. MQTT Control Byte 0 + 1 = 1 Remaining Length + 4 = 5 Packet Id + 2 = 7
Maximum number of bytes to send the Property Length. Property Length 0 + 4 = 4
Sending Property Buffer
|
static |
Send Unsubscribe without copying the users data into any buffer.
| [in] | pContext | Initialized MQTT context. |
| [in] | pSubscriptionList | List of MQTT subscription info. |
| [in] | subscriptionCount | Number of elements in pSubscriptionList. |
| [in] | packetId | Packet identifier. |
| [in] | remainingLength | Remaining length of the packet. |
| [in] | pPropertyBuilder | MQTT property builder. |
Fixed Size Properties
Maximum number of bytes by the fixed header of a SUBSCRIBE packet. MQTT Control Byte 0 + 1 = 1 Remaining Length + 4 = 5 Packet Id + 2 = 7
Maximum number of bytes to send the Property Length. Property Length 0 + 4 = 4
Sending Property Buffer
|
static |
Calculate the interval between two millisecond timestamps, including when the later value has overflowed.
| [in] | later | The later time stamp, in milliseconds. |
| [in] | start | The earlier time stamp, in milliseconds. |
|
static |
Convert a byte indicating a publish ack type to an MQTTPubAckType_t.
| [in] | packetType | First byte of fixed header. |
|
static |
Receive bytes into the network buffer.
| [in] | pContext | Initialized MQTT Context. |
| [in] | bytesToRecv | Number of bytes to receive. |
OR
|
static |
Receive a CONNACK packet from the transport interface.
| [in] | pContext | MQTT Connection context. |
| [in] | incomingPacket | packet struct with remaining length. |
|
static |
Get the correct ack type to send.
| [in] | state | Current state of publish. |
|
static |
Send acks for received QoS 1/2 publishes.
| [in] | pContext | MQTT Connection context. |
| [in] | packetId | packet ID of original PUBLISH. |
| [in] | publishState | Current publish state in record. |
|
static |
Send a keep alive PINGREQ if the keep alive interval has elapsed.
| [in] | pContext | Initialized MQTT Context. |
|
static |
Handle received MQTT PUBLISH packet.
| [in] | pContext | MQTT Connection context. |
| [in] | pIncomingPacket | Incoming packet. |
|
static |
Handle received MQTT publish acks.
| [in] | pContext | MQTT Connection context. |
| [in] | pIncomingPacket | Incoming packet. |
|
static |
Handle received MQTT ack.
| [in] | pContext | MQTT Connection context. |
| [in] | pIncomingPacket | Incoming packet. |
| [in] | manageKeepAlive | Flag indicating if PINGRESPs should not be given to the application |
|
static |
Run a single iteration of the receive loop.
| [in] | pContext | MQTT Connection context. |
| [in] | manageKeepAlive | Flag indicating if keep alive should be handled. |
|
static |
Validates parameters of MQTT_Subscribe or MQTT_Unsubscribe.
| [in] | pContext | Initialized MQTT context. |
| [in] | pSubscriptionList | List of MQTT subscription info. |
| [in] | subscriptionCount | The number of elements in pSubscriptionList. |
| [in] | packetId | Packet identifier. |
| [in] | subscriptionType | Either MQTT_TYPE_SUBSCRIBE or MQTT_TYPE_UNSUBSCRIBE. |
|
static |
Receives a CONNACK MQTT packet.
| [in] | pContext | Initialized MQTT context. |
| [in] | timeoutMs | Timeout for waiting for CONNACK packet. |
| [in] | cleanSession | Clean session flag set by application. |
| [out] | pIncomingPacket | List of MQTT subscription info. |
| [out] | pSessionPresent | Whether a previous session was present. Only relevant if not establishing a clean session. |
MQTTSuccess otherwise.
|
static |
Resends pending acks for a re-established MQTT session.
| [in] | pContext | Initialized MQTT context. |
|
static |
Clears existing state records for a clean session.
| [in] | pContext | Initialized MQTT context. |
|
static |
Send the publish packet without copying the topic string and payload in the buffer.
| [in] | pContext | Initialized MQTT context. |
| [in] | pPublishInfo | MQTT PUBLISH packet parameters. |
| [in] | pMqttHeader | the serialized MQTT header with the header byte; the encoded length of the packet; and the encoded length of the topic string. |
| [in] | headerSize | Size of the serialized PUBLISH header. |
| [in] | packetId | Packet Id of the publish packet. |
| [in] | pPropertyBuilder | MQTT Publish property builder. |
Maximum number of bytes to send the Property Length. Property Length 0 + 4 = 4
|
static |
Function to validate MQTT_Publish parameters.
| [in] | pContext | Initialized MQTT context. |
| [in] | pPublishInfo | MQTT PUBLISH packet parameters. |
| [in] | packetId | Packet Id for the MQTT PUBLISH packet. |
|
static |
Performs matching for special cases when a topic filter ends with a wildcard character.
When the topic name has been consumed but there are remaining characters to to match in topic filter, this function handles the following 2 cases:
| [in] | pTopicFilter | The topic filter containing the wildcard. |
| [in] | topicFilterLength | Length of the topic filter being examined. |
| [in] | filterIndex | Index of the topic filter being examined. |
|
static |
Attempt to match topic name with a topic filter starting with a wildcard.
If the topic filter starts with a '+' (single-level) wildcard, the function advances the pNameIndex by a level in the topic name. If the topic filter starts with a '#' (multi-level) wildcard, the function concludes that both the topic name and topic filter match.
| [in] | pTopicName | The topic name to match. |
| [in] | topicNameLength | Length of the topic name. |
| [in] | pTopicFilter | The topic filter to match. |
| [in] | topicFilterLength | Length of the topic filter. |
| [in,out] | pNameIndex | Current index in the topic name being examined. It is advanced by one level for + wildcards. |
| [in,out] | pFilterIndex | Current index in the topic filter being examined. It is advanced to position of '/' level separator for '+' wildcard. |
| [out] | pMatch | Whether the topic filter and topic name match. |
true if the caller of this function should exit; false if the caller should continue parsing the topics.
|
static |
Match a topic name and topic filter allowing the use of wildcards.
| [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. |
true if the topic name and topic filter match; false otherwise.
|
static |
Validate the topic filter in a subscription.
| [in] | pContext | MQTT Connection context. |
| [in] | pSubscriptionList | List of MQTT subscription info. |
| [in] | iterator | The iterator pointing to a topic filter in pSubscriptionList. |
| [in] | subscriptionType | The type of subscription, either MQTT_TYPE_SUBSCRIBE or MQTT_TYPE_UNSUBSCRIBE. |
|
static |
Check if wildcard subscriptions are allowed and valid.
| [in] | isWildcardAvailable | Flag indicating if wildcard subscriptions are supported. |
| [in] | pSubscriptionList | List of MQTT subscription info. |
| [in] | iterator | The iterator pointing to a topic filter in pSubscriptionList. |
|
static |
Validate Shared Subscriptions.
| [in] | pContext | MQTT Connection context. |
| [in] | pSubscriptionList | List of MQTT subscription info. |
| [in] | iterator | The iterator pointing to a topic filter in pSubscriptionList. |
|
static |
Add subscription options to the options array.
| [in] | subscriptionInfo | MQTT subscription information. |
| [out] | pSubscriptionOptionsArray | Array to store subscription options. |
| [in] | currentOptionIndex | Current index in the options array. |
|
static |
Handle Incoming Subscribe ACK.
| [in] | pContext | MQTT Connection context. |
| [in] | pIncomingPacket | Information of incoming packet |
|
static |
Send acks for received QoS 1/2 publishes. This function is used to send Publish Acks without any properties or reason codes.
| [in] | pContext | MQTT Connection context. |
| [in] | packetId | packet ID of original PUBLISH. |
| [in] | publishState | Current publish state in record. |
|
static |
Send acks for received QoS 1/2 publishes with properties.
| [in] | pContext | MQTT Connection context. |
| [in] | packetId | packet ID of original PUBLISH. |
| [in] | publishState | Current publish state in record. |
| [in] | reasonCode | Reason code to be sent in the Publish Ack. |
|
static |
Validate Publish Ack Reason Code.
| [in] | reasonCode | Reason Code to validate |
| [in] | packetType | Packet Type byte of the publish ack packet. (PUBACK, PUBREC, PUBREL, PUBCOMP) |
|
static |
Send the disconnect packet without copying the reason code and properties in the buffer.
| [in] | pContext | MQTT Connection context. |
| [in] | pReasonCode | Optional reason code to be sent in the Disconnect packet. |
| [in] | remainingLength | Remaining length of the packet. |
| [in] | pPropertyBuilder | MQTT Disconnect property builder. |
Maximum number of bytes to send the Property Length. Property Length 0 + 4 = 4
|
static |
Handle Incoming Disconnect.
| [in] | pContext | MQTT Connection context. |
| [in] | pIncomingPacket | Information of incoming packet |
| 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_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_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_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