Implements the functions in core_mqtt_state.h. More...
#include <assert.h>#include <string.h>#include "core_mqtt_state.h"#include "core_mqtt_config_defaults.h"#include "core_mqtt_default_logging.h"Macros | |
| #define | MQTT_INVALID_STATE_COUNT ( ~ZERO_SIZE_T ) |
| This macro depicts the invalid value for the state publishes. | |
| #define | UINT16_BITMAP_BIT_SET_AT(position) ( ( uint16_t ) 0x01U << ( ( uint16_t ) position ) ) |
| Create a 16-bit bitmap with bit set at specified position. More... | |
| #define | UINT16_SET_BIT(x, position) ( ( x ) = ( uint16_t ) ( ( x ) | ( UINT16_BITMAP_BIT_SET_AT( position ) ) ) ) |
| Set a bit in an 16-bit unsigned integer. More... | |
| #define | UINT16_CHECK_BIT(x, position) ( ( ( x ) & ( UINT16_BITMAP_BIT_SET_AT( position ) ) ) == ( UINT16_BITMAP_BIT_SET_AT( position ) ) ) |
| Macro for checking if a bit is set in a 16-bit unsigned integer. More... | |
Functions | |
| static bool | validateTransitionPublish (MQTTPublishState_t currentState, MQTTPublishState_t newState, MQTTStateOperation_t opType, MQTTQoS_t qos) |
| Test if a transition to new state is possible, when dealing with PUBLISHes. More... | |
| static bool | validateTransitionAck (MQTTPublishState_t currentState, MQTTPublishState_t newState) |
| Test if a transition to a new state is possible, when dealing with acks. More... | |
| static bool | isPublishOutgoing (MQTTPubAckType_t packetType, MQTTStateOperation_t opType) |
| Test if the publish corresponding to an ack is outgoing or incoming. More... | |
| static size_t | findInRecord (const MQTTPubAckInfo_t *records, size_t recordCount, uint16_t packetId, MQTTQoS_t *pQos, MQTTPublishState_t *pCurrentState) |
| Find a packet ID in the state record. More... | |
| static void | compactRecords (MQTTPubAckInfo_t *records, size_t recordCount) |
| Compact records. More... | |
| static MQTTStatus_t | addRecord (MQTTPubAckInfo_t *records, size_t recordCount, uint16_t packetId, MQTTQoS_t qos, MQTTPublishState_t publishState) |
| Store a new entry in the state record. More... | |
| static void | updateRecord (MQTTPubAckInfo_t *records, size_t recordIndex, MQTTPublishState_t newState, bool shouldDelete) |
| Update and possibly delete an entry in the state record. More... | |
| static uint16_t | stateSelect (const MQTTContext_t *pMqttContext, uint16_t searchStates, MQTTStateCursor_t *pCursor) |
| Get the packet ID and index of an outgoing publish in specified states. More... | |
| static MQTTStatus_t | updateStateAck (MQTTPubAckInfo_t *records, size_t maxRecordCount, size_t recordIndex, uint16_t packetId, MQTTPublishState_t currentState, MQTTPublishState_t newState) |
| Update the state records for an ACK after state transition validations. More... | |
| static MQTTStatus_t | updateStatePublish (const MQTTContext_t *pMqttContext, size_t recordIndex, uint16_t packetId, MQTTStateOperation_t opType, MQTTQoS_t qos, MQTTPublishState_t currentState, MQTTPublishState_t newState) |
| Update the state record for a PUBLISH packet after validating the state transitions. More... | |
| MQTTPublishState_t | MQTT_CalculateStateAck (MQTTPubAckType_t packetType, MQTTStateOperation_t opType, MQTTQoS_t qos) |
| Calculate the state from a PUBACK, PUBREC, PUBREL, or PUBCOMP. More... | |
| MQTTStatus_t | MQTT_ReserveState (const MQTTContext_t *pMqttContext, uint16_t packetId, MQTTQoS_t qos) |
| Reserve an entry for an outgoing QoS 1 or Qos 2 publish. More... | |
| MQTTPublishState_t | MQTT_CalculateStatePublish (MQTTStateOperation_t opType, MQTTQoS_t qos) |
| Calculate the new state for a publish from its qos and operation type. More... | |
| MQTTStatus_t | MQTT_UpdateStatePublish (const MQTTContext_t *pMqttContext, uint16_t packetId, MQTTStateOperation_t opType, MQTTQoS_t qos, MQTTPublishState_t *pNewState) |
| Update the state record for a PUBLISH packet. More... | |
| MQTTStatus_t | MQTT_RemoveStateRecord (const MQTTContext_t *pMqttContext, uint16_t packetId) |
| Remove the state record for a PUBLISH packet. More... | |
| MQTTStatus_t | MQTT_UpdateStateAck (const MQTTContext_t *pMqttContext, uint16_t packetId, MQTTPubAckType_t packetType, MQTTStateOperation_t opType, MQTTPublishState_t *pNewState) |
| Update the state record for an ACKed publish. More... | |
| uint16_t | MQTT_PubrelToResend (const MQTTContext_t *pMqttContext, MQTTStateCursor_t *pCursor, MQTTPublishState_t *pState) |
| Get the packet ID of next pending PUBREL ack to be resent. More... | |
| uint16_t | MQTT_PublishToResend (const MQTTContext_t *pMqttContext, MQTTStateCursor_t *pCursor) |
| Get the packet ID of next pending publish to be resent. More... | |
| const char * | MQTT_State_strerror (MQTTPublishState_t state) |
| State to string conversion for state engine. More... | |
Variables | |
| static const size_t | ZERO_SIZE_T = 0U |
| A global static variable used to generate the macro MQTT_INVALID_STATE_COUNT of size_t length. | |
Implements the functions in core_mqtt_state.h.
| #define UINT16_BITMAP_BIT_SET_AT | ( | position | ) | ( ( uint16_t ) 0x01U << ( ( uint16_t ) position ) ) |
Create a 16-bit bitmap with bit set at specified position.
| [in] | position | The position at which the bit need to be set. |
| #define UINT16_SET_BIT | ( | x, | |
| position | |||
| ) | ( ( x ) = ( uint16_t ) ( ( x ) | ( UINT16_BITMAP_BIT_SET_AT( position ) ) ) ) |
Set a bit in an 16-bit unsigned integer.
| [in] | x | The 16-bit unsigned integer to set a bit. |
| [in] | position | The position at which the bit need to be set. |
| #define UINT16_CHECK_BIT | ( | x, | |
| position | |||
| ) | ( ( ( x ) & ( UINT16_BITMAP_BIT_SET_AT( position ) ) ) == ( UINT16_BITMAP_BIT_SET_AT( position ) ) ) |
Macro for checking if a bit is set in a 16-bit unsigned integer.
| [in] | x | The unsigned 16-bit integer to check. |
| [in] | position | Which bit to check. |
|
static |
Test if a transition to new state is possible, when dealing with PUBLISHes.
| [in] | currentState | The current state. |
| [in] | newState | State to transition to. |
| [in] | opType | Reserve, Send, or Receive. |
| [in] | qos | 0, 1, or 2. |
true if transition is possible, else false
|
static |
Test if a transition to a new state is possible, when dealing with acks.
| [in] | currentState | The current state. |
| [in] | newState | State to transition to. |
true if transition is possible, else false.
|
static |
Test if the publish corresponding to an ack is outgoing or incoming.
| [in] | packetType | PUBACK, PUBREC, PUBREL, or PUBCOMP. |
| [in] | opType | Send, or Receive. |
true if corresponds to outgoing publish, else false.
|
static |
Find a packet ID in the state record.
| [in] | records | State record array. |
| [in] | recordCount | Length of record array. |
| [in] | packetId | packet ID to search for. |
| [out] | pQos | QoS retrieved from record. |
| [out] | pCurrentState | state retrieved from record. |
|
static |
Compact records.
Records are arranged in the relative order to maintain message ordering. This will lead to fragmentation and this function will help in defragmenting the records array.
| [in] | records | State record array. |
| [in] | recordCount | Length of record array. |
|
static |
Store a new entry in the state record.
| [in] | records | State record array. |
| [in] | recordCount | Length of record array. |
| [in] | packetId | Packet ID of new entry. |
| [in] | qos | QoS of new entry. |
| [in] | publishState | State of new entry. |
|
static |
Update and possibly delete an entry in the state record.
| [in] | records | State record array. |
| [in] | recordIndex | index of record to update. |
| [in] | newState | New state to update. |
| [in] | shouldDelete | Whether an existing entry should be deleted. |
|
static |
Get the packet ID and index of an outgoing publish in specified states.
| [in] | pMqttContext | Initialized MQTT context. |
| [in] | searchStates | The states to search for in 2-byte bit map. |
| [in,out] | pCursor | Index at which to start searching. |
|
static |
Update the state records for an ACK after state transition validations.
| [in] | records | State records pointer. |
| [in] | maxRecordCount | The maximum number of records. |
| [in] | recordIndex | Index at which the record is stored. |
| [in] | packetId | Packet id of the packet. |
| [in] | currentState | Current state of the publish record. |
| [in] | newState | New state of the publish. |
|
static |
Update the state record for a PUBLISH packet after validating the state transitions.
| [in] | pMqttContext | Initialized MQTT context. |
| [in] | recordIndex | Index in state records at which publish record exists. |
| [in] | packetId | ID of the PUBLISH packet. |
| [in] | opType | Send or Receive. |
| [in] | qos | 0, 1, or 2. |
| [in] | currentState | Current state of the publish record. |
| [in] | newState | New state of the publish record. |
| MQTTPublishState_t MQTT_CalculateStateAck | ( | MQTTPubAckType_t | packetType, |
| MQTTStateOperation_t | opType, | ||
| MQTTQoS_t | qos | ||
| ) |
Calculate the state from a PUBACK, PUBREC, PUBREL, or PUBCOMP.
| [in] | packetType | PUBACK, PUBREC, PUBREL, or PUBCOMP. |
| [in] | opType | Send or Receive. |
| [in] | qos | 1 or 2. |
| MQTTStatus_t MQTT_ReserveState | ( | const MQTTContext_t * | pMqttContext, |
| uint16_t | packetId, | ||
| MQTTQoS_t | qos | ||
| ) |
Reserve an entry for an outgoing QoS 1 or Qos 2 publish.
| [in] | pMqttContext | Initialized MQTT context. |
| [in] | packetId | The ID of the publish packet. |
| [in] | qos | 1 or 2. |
| MQTTPublishState_t MQTT_CalculateStatePublish | ( | MQTTStateOperation_t | opType, |
| MQTTQoS_t | qos | ||
| ) |
Calculate the new state for a publish from its qos and operation type.
| [in] | opType | Send or Receive. |
| [in] | qos | 0, 1, or 2. |
| MQTTStatus_t MQTT_UpdateStatePublish | ( | const MQTTContext_t * | pMqttContext, |
| uint16_t | packetId, | ||
| MQTTStateOperation_t | opType, | ||
| MQTTQoS_t | qos, | ||
| MQTTPublishState_t * | pNewState | ||
| ) |
Update the state record for a PUBLISH packet.
| [in] | pMqttContext | Initialized MQTT context. |
| [in] | packetId | ID of the PUBLISH packet. |
| [in] | opType | Send or Receive. |
| [in] | qos | 0, 1, or 2. |
| [out] | pNewState | Updated state of the publish. |
| MQTTStatus_t MQTT_RemoveStateRecord | ( | const MQTTContext_t * | pMqttContext, |
| uint16_t | packetId | ||
| ) |
Remove the state record for a PUBLISH packet.
| [in] | pMqttContext | Initialized MQTT context. |
| [in] | packetId | ID of the PUBLISH packet. |
| MQTTStatus_t MQTT_UpdateStateAck | ( | const MQTTContext_t * | pMqttContext, |
| uint16_t | packetId, | ||
| MQTTPubAckType_t | packetType, | ||
| MQTTStateOperation_t | opType, | ||
| MQTTPublishState_t * | pNewState | ||
| ) |
Update the state record for an ACKed publish.
| [in] | pMqttContext | Initialized MQTT context. |
| [in] | packetId | ID of the ack packet. |
| [in] | packetType | PUBACK, PUBREC, PUBREL, or PUBCOMP. |
| [in] | opType | Send or Receive. |
| [out] | pNewState | Updated state of the publish. |
| uint16_t MQTT_PubrelToResend | ( | const MQTTContext_t * | pMqttContext, |
| MQTTStateCursor_t * | pCursor, | ||
| MQTTPublishState_t * | pState | ||
| ) |
Get the packet ID of next pending PUBREL ack to be resent.
This function will need to be called to get the packet for which a PUBREL need to be sent when a session is reestablished. Calling this function repeatedly until packet id is 0 will give all the packets for which a PUBREL need to be resent in the correct order.
| [in] | pMqttContext | Initialized MQTT context. |
| [in,out] | pCursor | Index at which to start searching. |
| [out] | pState | State indicating that PUBREL packet need to be sent. |
| uint16_t MQTT_PublishToResend | ( | const MQTTContext_t * | pMqttContext, |
| MQTTStateCursor_t * | pCursor | ||
| ) |
Get the packet ID of next pending publish to be resent.
This function will need to be called to get the packet for which a publish need to be sent when a session is reestablished. Calling this function repeatedly until packet id is 0 will give all the packets for which a publish need to be resent in the correct order.
| [in] | pMqttContext | Initialized MQTT context. |
| [in,out] | pCursor | Index at which to start searching. |
Example
| const char * MQTT_State_strerror | ( | MQTTPublishState_t | state | ) |
State to string conversion for state engine.
| [in] | state | The state to convert to a string. |