Implements the user-facing functions in core_mqtt.h. More...
Functions | |
static int32_t | sendPacket (MQTTContext_t *pContext, const uint8_t *pBufferToSend, size_t bytesToSend) |
Sends provided buffer to network using transport send. More... | |
static uint32_t | calculateElapsedTime (uint32_t later, uint32_t start) |
Calculate the interval between two millisecond timestamps, including when the later value has overflowed. More... | |
static MQTTPubAckType_t | getAckFromPacketType (uint8_t packetType) |
Convert a byte indicating a publish ack type to an MQTTPubAckType_t. More... | |
static int32_t | recvExact (const MQTTContext_t *pContext, size_t bytesToRecv, uint32_t timeoutMs) |
Receive bytes into the network buffer, with a timeout. More... | |
static MQTTStatus_t | discardPacket (const MQTTContext_t *pContext, size_t remainingLength, uint32_t timeoutMs) |
Discard a packet from the transport interface. More... | |
static MQTTStatus_t | receivePacket (const MQTTContext_t *pContext, MQTTPacketInfo_t incomingPacket, uint32_t remainingTimeMs) |
Receive a packet from the transport interface. More... | |
static uint8_t | getAckTypeToSend (MQTTPublishState_t state) |
Get the correct ack type to send. More... | |
static MQTTStatus_t | sendPublishAcks (MQTTContext_t *pContext, uint16_t packetId, MQTTPublishState_t publishState) |
Send acks for received QoS 1/2 publishes. More... | |
static MQTTStatus_t | handleKeepAlive (MQTTContext_t *pContext) |
Send a keep alive PINGREQ if the keep alive interval has elapsed. More... | |
static MQTTStatus_t | handleIncomingPublish (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket) |
Handle received MQTT PUBLISH packet. More... | |
static MQTTStatus_t | handlePublishAcks (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket) |
Handle received MQTT publish acks. More... | |
static MQTTStatus_t | handleIncomingAck (MQTTContext_t *pContext, MQTTPacketInfo_t *pIncomingPacket, bool manageKeepAlive) |
Handle received MQTT ack. More... | |
static MQTTStatus_t | receiveSingleIteration (MQTTContext_t *pContext, uint32_t remainingTimeMs, bool manageKeepAlive) |
Run a single iteration of the receive loop. More... | |
static MQTTStatus_t | validateSubscribeUnsubscribeParams (const MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId) |
Validates parameters of MQTT_Subscribe or MQTT_Unsubscribe. More... | |
static MQTTStatus_t | sendPublish (MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, size_t headerSize) |
Send serialized publish packet using transport send. More... | |
static MQTTStatus_t | receiveConnack (const MQTTContext_t *pContext, uint32_t timeoutMs, bool cleanSession, MQTTPacketInfo_t *pIncomingPacket, bool *pSessionPresent) |
Receives a CONNACK MQTT packet. More... | |
static MQTTStatus_t | handleSessionResumption (MQTTContext_t *pContext, bool sessionPresent) |
Resends pending acks for a re-established MQTT session, or clears existing state records for a clean session. More... | |
static MQTTStatus_t | serializePublish (const MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, size_t *const pHeaderSize) |
Serializes a PUBLISH message. More... | |
static MQTTStatus_t | validatePublishParams (const MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId) |
Function to validate MQTT_Publish parameters. More... | |
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. More... | |
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. More... | |
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. More... | |
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... | |
Implements the user-facing functions in core_mqtt.h.
|
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 |
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, with a timeout.
[in] | pContext | Initialized MQTT Context. |
[in] | bytesToRecv | Number of bytes to receive. |
[in] | timeoutMs | Time remaining to receive the packet. |
|
static |
Discard a packet from the transport interface.
[in] | pContext | MQTT Connection context. |
[in] | remainingLength | Remaining length of the packet to dump. |
[in] | timeoutMs | Time remaining to discard the packet. |
|
static |
Receive a packet from the transport interface.
[in] | pContext | MQTT Connection context. |
[in] | incomingPacket | packet struct with remaining length. |
[in] | remainingTimeMs | Time remaining to receive the packet. |
|
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] | remainingTimeMs | Remaining time for the loop in milliseconds. |
[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. |
|
static |
Send serialized publish packet using transport send.
param[in] pContext Initialized MQTT context.
param[in] pPublishInfo MQTT PUBLISH packet parameters.
param[in] headerSize Header size of the PUBLISH packet.
|
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, or clears existing state records for a clean session.
[in] | pContext | Initialized MQTT context. |
[in] | sessionPresent | Session present flag received from the MQTT broker. |
|
static |
Serializes a PUBLISH message.
param[in] pContext Initialized MQTT context.
param[in] pPublishInfo MQTT PUBLISH packet parameters.
param[in] packetId Packet Id of the publish packet.
param[out] pHeaderSize Size of the serialized PUBLISH header.
|
static |
Function to validate MQTT_Publish parameters.
param[in] pContext Initialized MQTT context.
param[in] pPublishInfo MQTT PUBLISH packet parameters.
param[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. 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. |
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. |
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. |