coreMQTT v1.1.0
MQTT 3.1.1 Client Library
core_mqtt_serializer.h
Go to the documentation of this file.
1/*
2 * coreMQTT v1.1.0
3 * Copyright (C) 2020 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/* MQTT_DO_NOT_USE_CUSTOM_CONFIG allows building the MQTT library
38 * without a custom config. If a custom config is provided, the
39 * MQTT_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
40#ifndef MQTT_DO_NOT_USE_CUSTOM_CONFIG
41 /* Include custom config file before other headers. */
42 #include "core_mqtt_config.h"
43#endif
44
45/* Include config defaults header to get default values of configs not
46 * defined in core_mqtt_config.h file. */
48
49#include "transport_interface.h"
50
51/* MQTT packet types. */
52
57#define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
58#define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
59#define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
60#define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
61#define MQTT_PACKET_TYPE_PUBREC ( ( uint8_t ) 0x50U )
62#define MQTT_PACKET_TYPE_PUBREL ( ( uint8_t ) 0x62U )
63#define MQTT_PACKET_TYPE_PUBCOMP ( ( uint8_t ) 0x70U )
64#define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
65#define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
66#define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xA2U )
67#define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xB0U )
68#define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xC0U )
69#define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xD0U )
70#define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xE0U )
77#define MQTT_PUBLISH_ACK_PACKET_SIZE ( 4UL )
78
79/* Structures defined in this file. */
80struct MQTTFixedBuffer;
81struct MQTTConnectInfo;
82struct MQTTSubscribeInfo;
83struct MQTTPublishInfo;
84struct MQTTPacketInfo;
85
90typedef enum MQTTStatus
91{
104
109typedef enum MQTTQoS
110{
113 MQTTQoS2 = 2
115
123typedef struct MQTTFixedBuffer
124{
125 uint8_t * pBuffer;
126 size_t size;
128
133typedef struct MQTTConnectInfo
134{
139
144
148 const char * pClientIdentifier;
149
154
158 const char * pUserName;
159
164
168 const char * pPassword;
169
175
180typedef struct MQTTSubscribeInfo
181{
186
190 const char * pTopicFilter;
191
197
202typedef struct MQTTPublishInfo
203{
208
212 bool retain;
213
217 bool dup;
218
222 const char * pTopicName;
223
228
232 const void * pPayload;
233
239
244typedef struct MQTTPacketInfo
245{
249 uint8_t type;
250
254 uint8_t * pRemainingData;
255
261
309/* @[declare_mqtt_getconnectpacketsize] */
311 const MQTTPublishInfo_t * pWillInfo,
312 size_t * pRemainingLength,
313 size_t * pPacketSize );
314/* @[declare_mqtt_getconnectpacketsize] */
315
365/* @[declare_mqtt_serializeconnect] */
367 const MQTTPublishInfo_t * pWillInfo,
368 size_t remainingLength,
369 const MQTTFixedBuffer_t * pFixedBuffer );
370/* @[declare_mqtt_serializeconnect] */
371
423/* @[declare_mqtt_getsubscribepacketsize] */
425 size_t subscriptionCount,
426 size_t * pRemainingLength,
427 size_t * pPacketSize );
428/* @[declare_mqtt_getsubscribepacketsize] */
429
489/* @[declare_mqtt_serializesubscribe] */
491 size_t subscriptionCount,
492 uint16_t packetId,
493 size_t remainingLength,
494 const MQTTFixedBuffer_t * pFixedBuffer );
495/* @[declare_mqtt_serializesubscribe] */
496
540/* @[declare_mqtt_getunsubscribepacketsize] */
542 size_t subscriptionCount,
543 size_t * pRemainingLength,
544 size_t * pPacketSize );
545/* @[declare_mqtt_getunsubscribepacketsize] */
546
606/* @[declare_mqtt_serializeunsubscribe] */
608 size_t subscriptionCount,
609 uint16_t packetId,
610 size_t remainingLength,
611 const MQTTFixedBuffer_t * pFixedBuffer );
612/* @[declare_mqtt_serializeunsubscribe] */
613
660/* @[declare_mqtt_getpublishpacketsize] */
662 size_t * pRemainingLength,
663 size_t * pPacketSize );
664/* @[declare_mqtt_getpublishpacketsize] */
665
728/* @[declare_mqtt_serializepublish] */
730 uint16_t packetId,
731 size_t remainingLength,
732 const MQTTFixedBuffer_t * pFixedBuffer );
733/* @[declare_mqtt_serializepublish] */
734
808/* @[declare_mqtt_serializepublishheader] */
810 uint16_t packetId,
811 size_t remainingLength,
812 const MQTTFixedBuffer_t * pFixedBuffer,
813 size_t * pHeaderSize );
814/* @[declare_mqtt_serializepublishheader] */
815
857/* @[declare_mqtt_serializeack] */
859 uint8_t packetType,
860 uint16_t packetId );
861/* @[declare_mqtt_serializeack] */
862
887/* @[declare_mqtt_getdisconnectpacketsize] */
888MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
889/* @[declare_mqtt_getdisconnectpacketsize] */
890
928/* @[declare_mqtt_serializedisconnect] */
930/* @[declare_mqtt_serializedisconnect] */
931
956/* @[declare_mqtt_getpingreqpacketsize] */
957MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
958/* @[declare_mqtt_getpingreqpacketsize] */
959
997/* @[declare_mqtt_serializepingreq] */
999/* @[declare_mqtt_serializepingreq] */
1000
1058/* @[declare_mqtt_deserializepublish] */
1060 uint16_t * pPacketId,
1061 MQTTPublishInfo_t * pPublishInfo );
1062/* @[declare_mqtt_deserializepublish] */
1063
1102/* @[declare_mqtt_deserializeack] */
1103MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
1104 uint16_t * pPacketId,
1105 bool * pSessionPresent );
1106/* @[declare_mqtt_deserializeack] */
1107
1168/* @[declare_mqtt_getincomingpackettypeandlength] */
1170 NetworkContext_t * pNetworkContext,
1171 MQTTPacketInfo_t * pIncomingPacket );
1172/* @[declare_mqtt_getincomingpackettypeandlength] */
1173
1174#endif /* ifndef CORE_MQTT_SERIALIZER_H */
This represents the default values for the configuration macros for the MQTT library.
MQTTStatus_t MQTT_GetPingreqPacketSize(size_t *pPacketSize)
Get the size of an MQTT PINGREQ packet.
Definition: core_mqtt_serializer.c:2167
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:2049
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:1697
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:1751
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2230
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:1501
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:1966
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:2124
MQTTStatus_t MQTT_GetDisconnectPacketSize(size_t *pPacketSize)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:2104
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:2344
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:1840
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:1601
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:1789
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:1659
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:1881
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:2267
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:2187
int32_t(* TransportRecv_t)(NetworkContext_t *pNetworkContext, void *pBuffer, size_t bytesToRecv)
Transport interface for receiving data on the network.
Definition: transport_interface.h:213
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:91
MQTTQoS_t
MQTT Quality of Service values.
Definition: core_mqtt_serializer.h:110
@ MQTTKeepAliveTimeout
Definition: core_mqtt_serializer.h:102
@ MQTTServerRefused
Definition: core_mqtt_serializer.h:98
@ MQTTSuccess
Definition: core_mqtt_serializer.h:92
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:99
@ MQTTIllegalState
Definition: core_mqtt_serializer.h:100
@ MQTTStateCollision
Definition: core_mqtt_serializer.h:101
@ MQTTRecvFailed
Definition: core_mqtt_serializer.h:96
@ MQTTBadParameter
Definition: core_mqtt_serializer.h:93
@ MQTTBadResponse
Definition: core_mqtt_serializer.h:97
@ MQTTNoMemory
Definition: core_mqtt_serializer.h:94
@ MQTTSendFailed
Definition: core_mqtt_serializer.h:95
@ MQTTQoS1
Definition: core_mqtt_serializer.h:112
@ MQTTQoS2
Definition: core_mqtt_serializer.h:113
@ MQTTQoS0
Definition: core_mqtt_serializer.h:111
struct NetworkContext NetworkContext_t
The NetworkContext is an incomplete type. An implementation of this interface must define struct Netw...
Definition: transport_interface.h:183
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:134
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:148
const char * pUserName
MQTT user name. Set to NULL if not used.
Definition: core_mqtt_serializer.h:158
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:138
uint16_t userNameLength
Length of MQTT user name. Set to 0 if not used.
Definition: core_mqtt_serializer.h:163
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:143
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:153
uint16_t passwordLength
Length of MQTT password. Set to 0 if not used.
Definition: core_mqtt_serializer.h:173
const char * pPassword
MQTT password. Set to NULL if not used.
Definition: core_mqtt_serializer.h:168
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:124
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:126
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:125
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:245
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:259
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:249
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:254
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:203
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:207
bool retain
Whether this is a retained message.
Definition: core_mqtt_serializer.h:212
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:227
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:237
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:217
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:222
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:232
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:181
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:185
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:195
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:190
Transport interface definitions to send and receive data over the network.