coreMQTT v5.0.0
MQTT 5.0 Client Library
 
Loading...
Searching...
No Matches
core_mqtt_serializer_private.c File Reference

Implements private functions used by serializer and deserializer. DO NOT use these functions in your application. More...

#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <inttypes.h>
#include "core_mqtt_serializer.h"
#include "private/core_mqtt_serializer_private.h"
#include "core_mqtt_config_defaults.h"

Macros

#define MQTT_VERSION_5   ( 5U )
 Version 5 has the value 5.
 

Functions

uint32_t variableLengthEncodedSize (uint32_t length)
 Retrieve the size of the remaining length if it were to be encoded.
 
uint8_t * encodeString (uint8_t *pDestination, const char *pSource, uint16_t sourceLength)
 Encode a string whose size is at maximum 16 bits in length.
 
MQTTStatus_t decodeUserProp (const char **pPropertyKey, size_t *pPropertyKeyLen, const char **pPropertyValue, size_t *pPropertyValueLen, uint32_t *pPropertyLength, uint8_t **pIndex)
 Validate the length and decode a user property.
 
MQTTStatus_t decodeUint32t (uint32_t *pProperty, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
 Validate the length and decode a 4 byte value.
 
MQTTStatus_t decodeUint16t (uint16_t *pProperty, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
 Validate the length and decode a 2 byte value.
 
MQTTStatus_t decodeUint8t (uint8_t *pProperty, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
 Validate the length and decode a 1 byte value.
 
MQTTStatus_t decodeUtf8 (const char **pProperty, size_t *pLength, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
 Validate the length and decode a utf 8 string.
 
MQTTStatus_t decodeVariableLength (const uint8_t *pBuffer, size_t bufferLength, uint32_t *pLength)
 Decodes the variable length by reading a single byte at a time.
 
uint8_t * encodeVariableLength (uint8_t *pDestination, uint32_t length)
 Encodes the remaining length of the packet using the variable length encoding scheme provided in the MQTT 5.0 specification.
 
uint8_t * serializeAckFixed (uint8_t *pIndex, uint8_t packetType, uint16_t packetId, uint32_t remainingLength, MQTTSuccessFailReasonCode_t reasonCode)
 Serialize the fixed size part of the ack packet header.
 
uint8_t * serializeConnectFixedHeader (uint8_t *pIndex, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t remainingLength)
 Serialize the fixed part of the connect packet header.
 
uint8_t * serializeSubscribeHeader (uint32_t remainingLength, uint8_t *pIndex, uint16_t packetId)
 Serialize the fixed part of the subscribe packet header.
 
uint8_t * serializeUnsubscribeHeader (uint32_t remainingLength, uint8_t *pIndex, uint16_t packetId)
 Serialize the fixed part of the unsubscribe packet header.
 
uint8_t * serializeDisconnectFixed (uint8_t *pIndex, const MQTTSuccessFailReasonCode_t *pReasonCode, uint32_t remainingLength)
 Serialize the fixed size part of the disconnect packet header.
 
MQTTStatus_t decodeSubackPropertyLength (const uint8_t *pIndex, uint32_t remainingLength, uint32_t *subackPropertyLength)
 Decodes the property length field in a SUBACK packet.
 

Detailed Description

Implements private functions used by serializer and deserializer. DO NOT use these functions in your application.

Note
These functions should not be called by the application or relied upon since their implementation can change. These are for internal use by the library only.

Function Documentation

◆ variableLengthEncodedSize()

uint32_t variableLengthEncodedSize ( uint32_t  length)

Retrieve the size of the remaining length if it were to be encoded.

Parameters
[in]lengthThe remaining length to be encoded.
Note
The length MUST be less than 268,435,455 as directed by the spec.
Returns
The size of the remaining length if it were to be encoded.

◆ encodeString()

uint8_t * encodeString ( uint8_t *  pDestination,
const char *  pSource,
uint16_t  sourceLength 
)

Encode a string whose size is at maximum 16 bits in length.

Parameters
[out]pDestinationDestination buffer for the encoding.
[in]pSourceThe source string to encode.
[in]sourceLengthThe length of the source string to encode.
Returns
A pointer to the end of the encoded string.

◆ decodeUserProp()

MQTTStatus_t decodeUserProp ( const char **  pPropertyKey,
size_t *  pPropertyKeyLen,
const char **  pPropertyValue,
size_t *  pPropertyValueLen,
uint32_t *  pPropertyLength,
uint8_t **  pIndex 
)

Validate the length and decode a user property.

Parameters
[out]pPropertyKeyTo store the decoded key.
[out]pPropertyKeyLenTo store the decoded key length.
[out]pPropertyValueTo store the decoded value.
[out]pPropertyValueLenTo store the decoded value length.
[in,out]pPropertyLengthValue of the remaining property length.
[in,out]pIndexPointer to the current index of the buffer.
Returns
MQTTSuccess, MQTTBadResponse

◆ decodeUint32t()

MQTTStatus_t decodeUint32t ( uint32_t *  pProperty,
uint32_t *  pPropertyLength,
bool *  pUsed,
uint8_t **  pIndex 
)

Validate the length and decode a 4 byte value.

Parameters
[out]pPropertyTo store the decoded property.
[in,out]pPropertyLengthValue of the remaining property length.
[in,out]pUsedWhether the property is decoded before.
[in,out]pIndexPointer to the current index of the buffer.
Returns
MQTTSuccess, MQTTBadResponse

◆ decodeUint16t()

MQTTStatus_t decodeUint16t ( uint16_t *  pProperty,
uint32_t *  pPropertyLength,
bool *  pUsed,
uint8_t **  pIndex 
)

Validate the length and decode a 2 byte value.

Parameters
[out]pPropertyTo store the decoded property.
[in,out]pPropertyLengthValue of the remaining property length.
[in,out]pUsedWhether the property is decoded before.
[in,out]pIndexPointer to the current index of the buffer.
Returns
MQTTSuccess, MQTTBadResponse

◆ decodeUint8t()

MQTTStatus_t decodeUint8t ( uint8_t *  pProperty,
uint32_t *  pPropertyLength,
bool *  pUsed,
uint8_t **  pIndex 
)

Validate the length and decode a 1 byte value.

Parameters
[out]pPropertyTo store the decoded property.
[in,out]pPropertyLengthValue of the remaining property length.
[in,out]pUsedWhether the property is decoded before.
[in,out]pIndexPointer to the current index of the buffer.
Returns
MQTTSuccess, MQTTBadResponse

◆ decodeUtf8()

MQTTStatus_t decodeUtf8 ( const char **  pProperty,
size_t *  pLength,
uint32_t *  pPropertyLength,
bool *  pUsed,
uint8_t **  pIndex 
)

Validate the length and decode a utf 8 string.

Parameters
[out]pPropertyTo store the decoded string.
[out]pLengthSize of the decoded utf-8 string.
[in,out]pPropertyLengthValue of the remaining property length.
[in,out]pUsedWhether the property is decoded before.
[in,out]pIndexPointer to the current index of the buffer.
Returns
MQTTSuccess, MQTTBadResponse

◆ decodeVariableLength()

MQTTStatus_t decodeVariableLength ( const uint8_t *  pBuffer,
size_t  bufferLength,
uint32_t *  pLength 
)

Decodes the variable length by reading a single byte at a time.

Uses the algorithm provided in the spec.

Parameters
[in]pBufferPointer to the buffer.
[in]bufferLengthLength of the remaining buffer.
[out]pLengthDecoded variable length
Returns
MQTTSuccess if variable length and paramters are valid else MQTTBadResponse.

◆ encodeVariableLength()

uint8_t * encodeVariableLength ( uint8_t *  pDestination,
uint32_t  length 
)

Encodes the remaining length of the packet using the variable length encoding scheme provided in the MQTT 5.0 specification.

Parameters
[out]pDestinationThe destination buffer to store the encoded remaining length.
[in]lengthThe remaining length to encode.
Returns
The location of the byte following the encoded value.

◆ serializeAckFixed()

uint8_t * serializeAckFixed ( uint8_t *  pIndex,
uint8_t  packetType,
uint16_t  packetId,
uint32_t  remainingLength,
MQTTSuccessFailReasonCode_t  reasonCode 
)

Serialize the fixed size part of the ack packet header.

Parameters
[out]pIndexPointer to the buffer where the header is to be serialized.
[in]packetTypeType of publish ack
[in]packetIdPacked identifier of the ack packet.
[in]remainingLengthRemaining length of the ack packet.
[in]reasonCodeReason code for the ack packet.
Returns
A pointer to the end of the encoded string.

◆ serializeConnectFixedHeader()

uint8_t * serializeConnectFixedHeader ( uint8_t *  pIndex,
const MQTTConnectInfo_t pConnectInfo,
const MQTTPublishInfo_t pWillInfo,
uint32_t  remainingLength 
)

Serialize the fixed part of the connect packet header.

Parameters
[out]pIndexPointer to the buffer where the header is to be serialized.
[in]pConnectInfoThe connect information.
[in]pWillInfoThe last will and testament information.
[in]remainingLengthThe remaining length of the packet to be serialized.
Returns
A pointer to the end of the encoded string.

◆ serializeSubscribeHeader()

uint8_t * serializeSubscribeHeader ( uint32_t  remainingLength,
uint8_t *  pIndex,
uint16_t  packetId 
)

Serialize the fixed part of the subscribe packet header.

Parameters
[in]remainingLengthThe remaining length of the packet to be serialized.
[in]pIndexPointer to the buffer where the header is to be serialized.
[in]packetIdThe packet ID to be serialized.
Returns
A pointer to the end of the encoded string.

◆ serializeUnsubscribeHeader()

uint8_t * serializeUnsubscribeHeader ( uint32_t  remainingLength,
uint8_t *  pIndex,
uint16_t  packetId 
)

Serialize the fixed part of the unsubscribe packet header.

Parameters
[in]remainingLengthThe remaining length of the packet to be serialized.
[in]pIndexPointer to the buffer where the header is to be serialized.
[in]packetIdThe packet ID to be serialized.
Returns
A pointer to the end of the encoded string.

◆ serializeDisconnectFixed()

uint8_t * serializeDisconnectFixed ( uint8_t *  pIndex,
const MQTTSuccessFailReasonCode_t pReasonCode,
uint32_t  remainingLength 
)

Serialize the fixed size part of the disconnect packet header.

Parameters
[out]pIndexPointer to the buffer where the header is to be serialized.
[in]pReasonCodeReason code for the disconnect packet.
[in]remainingLengthRemaining length of the disconnect packet.
Returns
A pointer to the end of the encoded string.

◆ decodeSubackPropertyLength()

MQTTStatus_t decodeSubackPropertyLength ( const uint8_t *  pIndex,
uint32_t  remainingLength,
uint32_t *  subackPropertyLength 
)

Decodes the property length field in a SUBACK packet.

Parameters
[in]pIndexPointer to the start of the properties in the SUBACK packet.
[in]remainingLengthThe remaining length of the MQTT packet being parsed, without Packet ID.
[out]subackPropertyLengthThe decoded property length including the size of its encoded representation.
Returns
MQTTSuccess if the property length is successfully decoded; MQTTBadResponse if the decoded property length is greater than the remaining length.