coreMQTT v2.0.0
MQTT 3.1.1 Client Library
core_mqtt_serializer.h
Go to the documentation of this file.
1/*
2 * coreMQTT v2.0.0
3 * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
30#ifndef CORE_MQTT_SERIALIZER_H
31#define CORE_MQTT_SERIALIZER_H
32
33#include <stddef.h>
34#include <stdint.h>
35#include <stdbool.h>
36
37/* *INDENT-OFF* */
38#ifdef __cplusplus
39 extern "C" {
40#endif
41/* *INDENT-ON */
42
43/* MQTT_DO_NOT_USE_CUSTOM_CONFIG allows building the MQTT library
44 * without a custom config. If a custom config is provided, the
45 * MQTT_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
46#ifndef MQTT_DO_NOT_USE_CUSTOM_CONFIG
47 /* Include custom config file before other headers. */
48 #include "core_mqtt_config.h"
49#endif
50
51/* Include config defaults header to get default values of configs not
52 * defined in core_mqtt_config.h file. */
54
55#include "transport_interface.h"
56
57/* MQTT packet types. */
58
63#define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
64#define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
65#define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
66#define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
67#define MQTT_PACKET_TYPE_PUBREC ( ( uint8_t ) 0x50U )
68#define MQTT_PACKET_TYPE_PUBREL ( ( uint8_t ) 0x62U )
69#define MQTT_PACKET_TYPE_PUBCOMP ( ( uint8_t ) 0x70U )
70#define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
71#define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
72#define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xA2U )
73#define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xB0U )
74#define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xC0U )
75#define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xD0U )
76#define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xE0U )
83#define MQTT_PUBLISH_ACK_PACKET_SIZE ( 4UL )
84
85/* Structures defined in this file. */
86struct MQTTFixedBuffer;
87struct MQTTConnectInfo;
88struct MQTTSubscribeInfo;
89struct MQTTPublishInfo;
90struct MQTTPacketInfo;
91
96typedef enum MQTTStatus
97{
113
118typedef enum MQTTQoS
119{
122 MQTTQoS2 = 2
124
132typedef struct MQTTFixedBuffer
133{
134 uint8_t * pBuffer;
135 size_t size;
137
142typedef struct MQTTConnectInfo
143{
148
153
157 const char * pClientIdentifier;
158
163
167 const char * pUserName;
168
173
177 const char * pPassword;
178
184
189typedef struct MQTTSubscribeInfo
190{
195
199 const char * pTopicFilter;
200
206
211typedef struct MQTTPublishInfo
212{
217
221 bool retain;
222
226 bool dup;
227
231 const char * pTopicName;
232
237
241 const void * pPayload;
242
248
253typedef struct MQTTPacketInfo
254{
258 uint8_t type;
259
263 uint8_t * pRemainingData;
264
269
275
323/* @[declare_mqtt_getconnectpacketsize] */
325 const MQTTPublishInfo_t * pWillInfo,
326 size_t * pRemainingLength,
327 size_t * pPacketSize );
328/* @[declare_mqtt_getconnectpacketsize] */
329
379/* @[declare_mqtt_serializeconnect] */
381 const MQTTPublishInfo_t * pWillInfo,
382 size_t remainingLength,
383 const MQTTFixedBuffer_t * pFixedBuffer );
384/* @[declare_mqtt_serializeconnect] */
385
437/* @[declare_mqtt_getsubscribepacketsize] */
439 size_t subscriptionCount,
440 size_t * pRemainingLength,
441 size_t * pPacketSize );
442/* @[declare_mqtt_getsubscribepacketsize] */
443
503/* @[declare_mqtt_serializesubscribe] */
505 size_t subscriptionCount,
506 uint16_t packetId,
507 size_t remainingLength,
508 const MQTTFixedBuffer_t * pFixedBuffer );
509/* @[declare_mqtt_serializesubscribe] */
510
554/* @[declare_mqtt_getunsubscribepacketsize] */
556 size_t subscriptionCount,
557 size_t * pRemainingLength,
558 size_t * pPacketSize );
559/* @[declare_mqtt_getunsubscribepacketsize] */
560
620/* @[declare_mqtt_serializeunsubscribe] */
622 size_t subscriptionCount,
623 uint16_t packetId,
624 size_t remainingLength,
625 const MQTTFixedBuffer_t * pFixedBuffer );
626/* @[declare_mqtt_serializeunsubscribe] */
627
674/* @[declare_mqtt_getpublishpacketsize] */
676 size_t * pRemainingLength,
677 size_t * pPacketSize );
678/* @[declare_mqtt_getpublishpacketsize] */
679
742/* @[declare_mqtt_serializepublish] */
744 uint16_t packetId,
745 size_t remainingLength,
746 const MQTTFixedBuffer_t * pFixedBuffer );
747/* @[declare_mqtt_serializepublish] */
748
763 size_t remainingLength,
764 uint8_t * pBuffer,
765 size_t * headerSize );
766
840/* @[declare_mqtt_serializepublishheader] */
842 uint16_t packetId,
843 size_t remainingLength,
844 const MQTTFixedBuffer_t * pFixedBuffer,
845 size_t * pHeaderSize );
846/* @[declare_mqtt_serializepublishheader] */
847
889/* @[declare_mqtt_serializeack] */
891 uint8_t packetType,
892 uint16_t packetId );
893/* @[declare_mqtt_serializeack] */
894
919/* @[declare_mqtt_getdisconnectpacketsize] */
920MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
921/* @[declare_mqtt_getdisconnectpacketsize] */
922
960/* @[declare_mqtt_serializedisconnect] */
962/* @[declare_mqtt_serializedisconnect] */
963
988/* @[declare_mqtt_getpingreqpacketsize] */
989MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
990/* @[declare_mqtt_getpingreqpacketsize] */
991
1029/* @[declare_mqtt_serializepingreq] */
1031/* @[declare_mqtt_serializepingreq] */
1032
1090/* @[declare_mqtt_deserializepublish] */
1092 uint16_t * pPacketId,
1093 MQTTPublishInfo_t * pPublishInfo );
1094/* @[declare_mqtt_deserializepublish] */
1095
1134/* @[declare_mqtt_deserializeack] */
1135MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
1136 uint16_t * pPacketId,
1137 bool * pSessionPresent );
1138/* @[declare_mqtt_deserializeack] */
1139
1200/* @[declare_mqtt_getincomingpackettypeandlength] */
1202 NetworkContext_t * pNetworkContext,
1203 MQTTPacketInfo_t * pIncomingPacket );
1204/* @[declare_mqtt_getincomingpackettypeandlength] */
1205
1224 /* @[declare_mqtt_processincomingpackettypeandlength] */
1226 const size_t * pIndex,
1227 MQTTPacketInfo_t * pIncomingPacket );
1228/* @[declare_mqtt_processincomingpackettypeandlength] */
1229
1248uint8_t * MQTT_SerializeConnectFixedHeader( uint8_t * pIndex,
1249 const MQTTConnectInfo_t * pConnectInfo,
1250 const MQTTPublishInfo_t * pWillInfo,
1251 size_t remainingLength );
1271uint8_t * MQTT_SerializeSubscribeHeader( size_t remainingLength,
1272 uint8_t * pIndex,
1273 uint16_t packetId );
1293uint8_t * MQTT_SerializeUnsubscribeHeader( size_t remainingLength,
1294 uint8_t * pIndex,
1295 uint16_t packetId );
1298/* *INDENT-OFF* */
1299#ifdef __cplusplus
1300 }
1301#endif
1302/* *INDENT-ON* */
1303
1304#endif /* ifndef CORE_MQTT_SERIALIZER_H */
This represents the default values for the configuration macros for the MQTT library.
uint8_t * MQTT_SerializeUnsubscribeHeader(size_t remainingLength, uint8_t *pIndex, uint16_t packetId)
Serialize the fixed part of the unsubscribe packet header.
Definition: core_mqtt_serializer.c:1912
uint8_t * MQTT_SerializeConnectFixedHeader(uint8_t *pIndex, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, size_t remainingLength)
Serialize the fixed part of the connect packet header.
Definition: core_mqtt_serializer.c:1555
uint8_t * MQTT_SerializeSubscribeHeader(size_t remainingLength, uint8_t *pIndex, uint16_t packetId)
Serialize the fixed part of the subscribe packet header.
Definition: core_mqtt_serializer.c:1888
MQTTStatus_t MQTT_GetPingreqPacketSize(size_t *pPacketSize)
Get the size of an MQTT PINGREQ packet.
Definition: core_mqtt_serializer.c:2386
MQTTStatus_t MQTT_SerializeAck(const MQTTFixedBuffer_t *pFixedBuffer, uint8_t packetType, uint16_t packetId)
Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given buffer.
Definition: core_mqtt_serializer.c:2268
MQTTStatus_t MQTT_SerializeSubscribe(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT SUBSCRIBE packet in the given buffer.
Definition: core_mqtt_serializer.c:1934
MQTTStatus_t MQTT_GetUnsubscribePacketSize(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, size_t *pRemainingLength, size_t *pPacketSize)
Get packet size and Remaining Length of an MQTT UNSUBSCRIBE packet.
Definition: core_mqtt_serializer.c:1980
MQTTStatus_t MQTT_SerializePublishHeaderWithoutTopic(const MQTTPublishInfo_t *pPublishInfo, size_t remainingLength, uint8_t *pBuffer, size_t *headerSize)
Serialize an MQTT PUBLISH packet header without the topic string in the given buffer....
Definition: core_mqtt_serializer.c:638
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2449
MQTTStatus_t MQTT_GetConnectPacketSize(const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, size_t *pRemainingLength, size_t *pPacketSize)
Get the size and Remaining Length of an MQTT CONNECT packet.
Definition: core_mqtt_serializer.c:1692
MQTTStatus_t MQTT_SerializePublishHeader(const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer, size_t *pHeaderSize)
Serialize an MQTT PUBLISH packet header in the given buffer.
Definition: core_mqtt_serializer.c:2185
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:2343
MQTTStatus_t MQTT_GetDisconnectPacketSize(size_t *pPacketSize)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:2323
MQTTStatus_t MQTT_ProcessIncomingPacketTypeAndLength(const uint8_t *pBuffer, const size_t *pIndex, MQTTPacketInfo_t *pIncomingPacket)
Extract the MQTT packet type and length from incoming packet.
Definition: core_mqtt_serializer.c:2628
MQTTStatus_t MQTT_GetIncomingPacketTypeAndLength(TransportRecv_t readFunc, NetworkContext_t *pNetworkContext, MQTTPacketInfo_t *pIncomingPacket)
Extract the MQTT packet type and length from incoming packet.
Definition: core_mqtt_serializer.c:2563
MQTTStatus_t MQTT_GetPublishPacketSize(const MQTTPublishInfo_t *pPublishInfo, size_t *pRemainingLength, size_t *pPacketSize)
Get the packet size and remaining length of an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2059
MQTTStatus_t MQTT_SerializeConnect(const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT CONNECT packet in the given fixed buffer pFixedBuffer.
Definition: core_mqtt_serializer.c:1792
MQTTStatus_t MQTT_SerializeUnsubscribe(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT UNSUBSCRIBE packet in the given buffer.
Definition: core_mqtt_serializer.c:2018
MQTTStatus_t MQTT_GetSubscribePacketSize(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, size_t *pRemainingLength, size_t *pPacketSize)
Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.
Definition: core_mqtt_serializer.c:1850
MQTTStatus_t MQTT_SerializePublish(const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PUBLISH packet in the given buffer.
Definition: core_mqtt_serializer.c:2100
MQTTStatus_t MQTT_DeserializeAck(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, bool *pSessionPresent)
Deserialize an MQTT CONNACK, SUBACK, UNSUBACK, PUBACK, PUBREC, PUBREL, PUBCOMP, or PINGRESP.
Definition: core_mqtt_serializer.c:2486
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:2406
int32_t(* TransportRecv_t)(NetworkContext_t *pNetworkContext, void *pBuffer, size_t bytesToRecv)
Transport interface for receiving data on the network.
Definition: transport_interface.h:219
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:97
MQTTQoS_t
MQTT Quality of Service values.
Definition: core_mqtt_serializer.h:119
@ MQTTKeepAliveTimeout
Definition: core_mqtt_serializer.h:108
@ MQTTServerRefused
Definition: core_mqtt_serializer.h:104
@ MQTTSuccess
Definition: core_mqtt_serializer.h:98
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:105
@ MQTTIllegalState
Definition: core_mqtt_serializer.h:106
@ MQTTStateCollision
Definition: core_mqtt_serializer.h:107
@ MQTTRecvFailed
Definition: core_mqtt_serializer.h:102
@ MQTTBadParameter
Definition: core_mqtt_serializer.h:99
@ MQTTBadResponse
Definition: core_mqtt_serializer.h:103
@ MQTTNeedMoreBytes
Definition: core_mqtt_serializer.h:109
@ MQTTNoMemory
Definition: core_mqtt_serializer.h:100
@ MQTTSendFailed
Definition: core_mqtt_serializer.h:101
@ MQTTQoS1
Definition: core_mqtt_serializer.h:121
@ MQTTQoS2
Definition: core_mqtt_serializer.h:122
@ MQTTQoS0
Definition: core_mqtt_serializer.h:120
struct NetworkContext NetworkContext_t
The NetworkContext is an incomplete type. An implementation of this interface must define struct Netw...
Definition: transport_interface.h:189
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:143
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:157
const char * pUserName
MQTT user name. Set to NULL if not used.
Definition: core_mqtt_serializer.h:167
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:147
uint16_t userNameLength
Length of MQTT user name. Set to 0 if not used.
Definition: core_mqtt_serializer.h:172
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:152
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:162
uint16_t passwordLength
Length of MQTT password. Set to 0 if not used.
Definition: core_mqtt_serializer.h:182
const char * pPassword
MQTT password. Set to NULL if not used.
Definition: core_mqtt_serializer.h:177
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:133
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:135
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:134
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:254
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:268
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:258
size_t headerLength
The length of the MQTT header including the type and length.
Definition: core_mqtt_serializer.h:273
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:263
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:212
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:216
bool retain
Whether this is a retained message.
Definition: core_mqtt_serializer.h:221
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:236
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:246
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:226
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:231
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:241
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:190
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:194
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:204
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:199
Transport interface definitions to send and receive data over the network.