coreMQTT v1.0.1
MQTT 3.1.1 Client Library
core_mqtt_serializer.h
Go to the documentation of this file.
1/*
2 * coreMQTT v1.0.1
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
41/* bool is defined in only C99+. */
42#if defined( __cplusplus ) || ( defined( __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901L ) )
43 #include <stdbool.h>
44#elif !defined( bool ) && !defined( false ) && !defined( true )
45 #define bool int8_t
46 #define false ( int8_t ) 0
47 #define true ( int8_t ) 1
48#endif
51/* MQTT_DO_NOT_USE_CUSTOM_CONFIG allows building the MQTT library
52 * without a custom config. If a custom config is provided, the
53 * MQTT_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
54#ifndef MQTT_DO_NOT_USE_CUSTOM_CONFIG
55 /* Include custom config file before other headers. */
56 #include "core_mqtt_config.h"
57#endif
58
59/* Include config defaults header to get default values of configs not
60 * defined in core_mqtt_config.h file. */
62
63#include "transport_interface.h"
64
65/* MQTT packet types. */
66
71#define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
72#define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
73#define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
74#define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
75#define MQTT_PACKET_TYPE_PUBREC ( ( uint8_t ) 0x50U )
76#define MQTT_PACKET_TYPE_PUBREL ( ( uint8_t ) 0x62U )
77#define MQTT_PACKET_TYPE_PUBCOMP ( ( uint8_t ) 0x70U )
78#define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
79#define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
80#define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xA2U )
81#define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xB0U )
82#define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xC0U )
83#define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xD0U )
84#define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xE0U )
91#define MQTT_PUBLISH_ACK_PACKET_SIZE ( 4UL )
92
93/* Structures defined in this file. */
94struct MQTTFixedBuffer;
95struct MQTTConnectInfo;
96struct MQTTSubscribeInfo;
97struct MQTTPublishInfo;
98struct MQTTPacketInfo;
99
104typedef enum MQTTStatus
105{
118
123typedef enum MQTTQoS
124{
127 MQTTQoS2 = 2
129
137typedef struct MQTTFixedBuffer
138{
139 uint8_t * pBuffer;
140 size_t size;
142
147typedef struct MQTTConnectInfo
148{
153
158
162 const char * pClientIdentifier;
163
168
172 const char * pUserName;
173
178
182 const char * pPassword;
183
189
194typedef struct MQTTSubscribeInfo
195{
200
204 const char * pTopicFilter;
205
211
216typedef struct MQTTPublishInfo
217{
222
226 bool retain;
227
231 bool dup;
232
236 const char * pTopicName;
237
242
246 const void * pPayload;
247
253
258typedef struct MQTTPacketInfo
259{
263 uint8_t type;
264
268 uint8_t * pRemainingData;
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
822/* @[declare_mqtt_serializepublishheader] */
824 uint16_t packetId,
825 size_t remainingLength,
826 const MQTTFixedBuffer_t * pFixedBuffer,
827 size_t * pHeaderSize );
828/* @[declare_mqtt_serializepublishheader] */
829
871/* @[declare_mqtt_serializeack] */
873 uint8_t packetType,
874 uint16_t packetId );
875/* @[declare_mqtt_serializeack] */
876
901/* @[declare_mqtt_getdisconnectpacketsize] */
902MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
903/* @[declare_mqtt_getdisconnectpacketsize] */
904
942/* @[declare_mqtt_serializedisconnect] */
944/* @[declare_mqtt_serializedisconnect] */
945
970/* @[declare_mqtt_getpingreqpacketsize] */
971MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
972/* @[declare_mqtt_getpingreqpacketsize] */
973
1011/* @[declare_mqtt_serializepingreq] */
1013/* @[declare_mqtt_serializepingreq] */
1014
1072/* @[declare_mqtt_deserializepublish] */
1074 uint16_t * pPacketId,
1075 MQTTPublishInfo_t * pPublishInfo );
1076/* @[declare_mqtt_deserializepublish] */
1077
1116/* @[declare_mqtt_deserializeack] */
1117MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
1118 uint16_t * pPacketId,
1119 bool * pSessionPresent );
1120/* @[declare_mqtt_deserializeack] */
1121
1182/* @[declare_mqtt_getincomingpackettypeandlength] */
1184 NetworkContext_t * pNetworkContext,
1185 MQTTPacketInfo_t * pIncomingPacket );
1186/* @[declare_mqtt_getincomingpackettypeandlength] */
1187
1188#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:2165
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:2047
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:1688
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:1742
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2228
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:1485
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:1964
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:2122
MQTTStatus_t MQTT_GetDisconnectPacketSize(size_t *pPacketSize)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:2102
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:2341
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:1838
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:1585
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:1787
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:1643
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:1879
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:2265
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:2185
int32_t(* TransportRecv_t)(NetworkContext_t *pNetworkContext, void *pBuffer, size_t bytesToRecv)
Transport interface for receiving data on the network.
Definition: transport_interface.h:169
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:105
MQTTQoS_t
MQTT Quality of Service values.
Definition: core_mqtt_serializer.h:124
@ MQTTKeepAliveTimeout
Definition: core_mqtt_serializer.h:116
@ MQTTServerRefused
Definition: core_mqtt_serializer.h:112
@ MQTTSuccess
Definition: core_mqtt_serializer.h:106
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:113
@ MQTTIllegalState
Definition: core_mqtt_serializer.h:114
@ MQTTStateCollision
Definition: core_mqtt_serializer.h:115
@ MQTTRecvFailed
Definition: core_mqtt_serializer.h:110
@ MQTTBadParameter
Definition: core_mqtt_serializer.h:107
@ MQTTBadResponse
Definition: core_mqtt_serializer.h:111
@ MQTTNoMemory
Definition: core_mqtt_serializer.h:108
@ MQTTSendFailed
Definition: core_mqtt_serializer.h:109
@ MQTTQoS1
Definition: core_mqtt_serializer.h:126
@ MQTTQoS2
Definition: core_mqtt_serializer.h:127
@ MQTTQoS0
Definition: core_mqtt_serializer.h:125
struct NetworkContext NetworkContext_t
The NetworkContext is an incomplete type. An implementation of this interface must define struct Netw...
Definition: transport_interface.h:155
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:148
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:162
const char * pUserName
MQTT user name. Set to NULL if not used.
Definition: core_mqtt_serializer.h:172
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:152
uint16_t userNameLength
Length of MQTT user name. Set to 0 if not used.
Definition: core_mqtt_serializer.h:177
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:157
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:167
uint16_t passwordLength
Length of MQTT password. Set to 0 if not used.
Definition: core_mqtt_serializer.h:187
const char * pPassword
MQTT password. Set to NULL if not used.
Definition: core_mqtt_serializer.h:182
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:138
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:140
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:139
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:259
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:273
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:263
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:268
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:217
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:221
bool retain
Whether this is a retained message.
Definition: core_mqtt_serializer.h:226
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:241
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:251
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:231
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:236
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:246
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:195
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:199
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:209
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:204
Transport interface definitions to send and receive data over the network.