coreMQTT v2.1.0
MQTT 3.1.1 Client Library
core_mqtt_serializer.h
Go to the documentation of this file.
1/*
2 * coreMQTT v2.1.0
3 * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
32#ifndef CORE_MQTT_SERIALIZER_H
33#define CORE_MQTT_SERIALIZER_H
34
35#include <stddef.h>
36#include <stdint.h>
37#include <stdbool.h>
38
39/* *INDENT-OFF* */
40#ifdef __cplusplus
41 extern "C" {
42#endif
43/* *INDENT-ON */
44
45/* MQTT_DO_NOT_USE_CUSTOM_CONFIG allows building the MQTT library
46 * without a custom config. If a custom config is provided, the
47 * MQTT_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
48#ifndef MQTT_DO_NOT_USE_CUSTOM_CONFIG
49 /* Include custom config file before other headers. */
50 #include "core_mqtt_config.h"
51#endif
52
53/* Include config defaults header to get default values of configs not
54 * defined in core_mqtt_config.h file. */
56
57#include "transport_interface.h"
58
59/* MQTT packet types. */
60
65#define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
66#define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
67#define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
68#define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
69#define MQTT_PACKET_TYPE_PUBREC ( ( uint8_t ) 0x50U )
70#define MQTT_PACKET_TYPE_PUBREL ( ( uint8_t ) 0x62U )
71#define MQTT_PACKET_TYPE_PUBCOMP ( ( uint8_t ) 0x70U )
72#define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
73#define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
74#define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xA2U )
75#define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xB0U )
76#define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xC0U )
77#define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xD0U )
78#define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xE0U )
85#define MQTT_PUBLISH_ACK_PACKET_SIZE ( 4UL )
86
87/* Structures defined in this file. */
88struct MQTTFixedBuffer;
89struct MQTTConnectInfo;
90struct MQTTSubscribeInfo;
91struct MQTTPublishInfo;
92struct MQTTPacketInfo;
93
98typedef enum MQTTStatus
99{
115
120typedef enum MQTTQoS
121{
124 MQTTQoS2 = 2
126
134typedef struct MQTTFixedBuffer
135{
136 uint8_t * pBuffer;
137 size_t size;
139
144typedef struct MQTTConnectInfo
145{
150
155
159 const char * pClientIdentifier;
160
165
169 const char * pUserName;
170
175
179 const char * pPassword;
180
186
191typedef struct MQTTSubscribeInfo
192{
197
201 const char * pTopicFilter;
202
208
213typedef struct MQTTPublishInfo
214{
219
223 bool retain;
224
228 bool dup;
229
233 const char * pTopicName;
234
239
243 const void * pPayload;
244
250
255typedef struct MQTTPacketInfo
256{
260 uint8_t type;
261
265 uint8_t * pRemainingData;
266
271
277
325/* @[declare_mqtt_getconnectpacketsize] */
327 const MQTTPublishInfo_t * pWillInfo,
328 size_t * pRemainingLength,
329 size_t * pPacketSize );
330/* @[declare_mqtt_getconnectpacketsize] */
331
381/* @[declare_mqtt_serializeconnect] */
383 const MQTTPublishInfo_t * pWillInfo,
384 size_t remainingLength,
385 const MQTTFixedBuffer_t * pFixedBuffer );
386/* @[declare_mqtt_serializeconnect] */
387
439/* @[declare_mqtt_getsubscribepacketsize] */
441 size_t subscriptionCount,
442 size_t * pRemainingLength,
443 size_t * pPacketSize );
444/* @[declare_mqtt_getsubscribepacketsize] */
445
505/* @[declare_mqtt_serializesubscribe] */
507 size_t subscriptionCount,
508 uint16_t packetId,
509 size_t remainingLength,
510 const MQTTFixedBuffer_t * pFixedBuffer );
511/* @[declare_mqtt_serializesubscribe] */
512
556/* @[declare_mqtt_getunsubscribepacketsize] */
558 size_t subscriptionCount,
559 size_t * pRemainingLength,
560 size_t * pPacketSize );
561/* @[declare_mqtt_getunsubscribepacketsize] */
562
622/* @[declare_mqtt_serializeunsubscribe] */
624 size_t subscriptionCount,
625 uint16_t packetId,
626 size_t remainingLength,
627 const MQTTFixedBuffer_t * pFixedBuffer );
628/* @[declare_mqtt_serializeunsubscribe] */
629
676/* @[declare_mqtt_getpublishpacketsize] */
678 size_t * pRemainingLength,
679 size_t * pPacketSize );
680/* @[declare_mqtt_getpublishpacketsize] */
681
744/* @[declare_mqtt_serializepublish] */
746 uint16_t packetId,
747 size_t remainingLength,
748 const MQTTFixedBuffer_t * pFixedBuffer );
749/* @[declare_mqtt_serializepublish] */
750
765 size_t remainingLength,
766 uint8_t * pBuffer,
767 size_t * headerSize );
768
842/* @[declare_mqtt_serializepublishheader] */
844 uint16_t packetId,
845 size_t remainingLength,
846 const MQTTFixedBuffer_t * pFixedBuffer,
847 size_t * pHeaderSize );
848/* @[declare_mqtt_serializepublishheader] */
849
891/* @[declare_mqtt_serializeack] */
893 uint8_t packetType,
894 uint16_t packetId );
895/* @[declare_mqtt_serializeack] */
896
921/* @[declare_mqtt_getdisconnectpacketsize] */
922MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
923/* @[declare_mqtt_getdisconnectpacketsize] */
924
962/* @[declare_mqtt_serializedisconnect] */
964/* @[declare_mqtt_serializedisconnect] */
965
990/* @[declare_mqtt_getpingreqpacketsize] */
991MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
992/* @[declare_mqtt_getpingreqpacketsize] */
993
1031/* @[declare_mqtt_serializepingreq] */
1033/* @[declare_mqtt_serializepingreq] */
1034
1092/* @[declare_mqtt_deserializepublish] */
1094 uint16_t * pPacketId,
1095 MQTTPublishInfo_t * pPublishInfo );
1096/* @[declare_mqtt_deserializepublish] */
1097
1136/* @[declare_mqtt_deserializeack] */
1137MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
1138 uint16_t * pPacketId,
1139 bool * pSessionPresent );
1140/* @[declare_mqtt_deserializeack] */
1141
1202/* @[declare_mqtt_getincomingpackettypeandlength] */
1204 NetworkContext_t * pNetworkContext,
1205 MQTTPacketInfo_t * pIncomingPacket );
1206/* @[declare_mqtt_getincomingpackettypeandlength] */
1207
1226 /* @[declare_mqtt_processincomingpackettypeandlength] */
1228 const size_t * pIndex,
1229 MQTTPacketInfo_t * pIncomingPacket );
1230/* @[declare_mqtt_processincomingpackettypeandlength] */
1231
1250uint8_t * MQTT_SerializeConnectFixedHeader( uint8_t * pIndex,
1251 const MQTTConnectInfo_t * pConnectInfo,
1252 const MQTTPublishInfo_t * pWillInfo,
1253 size_t remainingLength );
1273uint8_t * MQTT_SerializeSubscribeHeader( size_t remainingLength,
1274 uint8_t * pIndex,
1275 uint16_t packetId );
1295uint8_t * MQTT_SerializeUnsubscribeHeader( size_t remainingLength,
1296 uint8_t * pIndex,
1297 uint16_t packetId );
1300/* *INDENT-OFF* */
1301#ifdef __cplusplus
1302 }
1303#endif
1304/* *INDENT-ON* */
1305
1306#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:1914
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:1557
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:1890
MQTTStatus_t MQTT_GetPingreqPacketSize(size_t *pPacketSize)
Get the size of an MQTT PINGREQ packet.
Definition: core_mqtt_serializer.c:2388
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:2270
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:1936
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:1982
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:640
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2451
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:1694
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:2187
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:2345
MQTTStatus_t MQTT_GetDisconnectPacketSize(size_t *pPacketSize)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:2325
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:2630
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:2565
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:2061
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:1794
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:2020
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:1852
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:2102
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:2488
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:2408
int32_t(* TransportRecv_t)(NetworkContext_t *pNetworkContext, void *pBuffer, size_t bytesToRecv)
Transport interface for receiving data on the network.
Definition: transport_interface.h:221
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:99
MQTTQoS_t
MQTT Quality of Service values.
Definition: core_mqtt_serializer.h:121
@ MQTTKeepAliveTimeout
Definition: core_mqtt_serializer.h:110
@ MQTTServerRefused
Definition: core_mqtt_serializer.h:106
@ MQTTSuccess
Definition: core_mqtt_serializer.h:100
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:107
@ MQTTIllegalState
Definition: core_mqtt_serializer.h:108
@ MQTTStateCollision
Definition: core_mqtt_serializer.h:109
@ MQTTRecvFailed
Definition: core_mqtt_serializer.h:104
@ MQTTBadParameter
Definition: core_mqtt_serializer.h:101
@ MQTTBadResponse
Definition: core_mqtt_serializer.h:105
@ MQTTNeedMoreBytes
Definition: core_mqtt_serializer.h:111
@ MQTTNoMemory
Definition: core_mqtt_serializer.h:102
@ MQTTSendFailed
Definition: core_mqtt_serializer.h:103
@ MQTTQoS1
Definition: core_mqtt_serializer.h:123
@ MQTTQoS2
Definition: core_mqtt_serializer.h:124
@ MQTTQoS0
Definition: core_mqtt_serializer.h:122
struct NetworkContext NetworkContext_t
The NetworkContext is an incomplete type. An implementation of this interface must define struct Netw...
Definition: transport_interface.h:191
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:145
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:159
const char * pUserName
MQTT user name. Set to NULL if not used.
Definition: core_mqtt_serializer.h:169
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:149
uint16_t userNameLength
Length of MQTT user name. Set to 0 if not used.
Definition: core_mqtt_serializer.h:174
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:154
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:164
uint16_t passwordLength
Length of MQTT password. Set to 0 if not used.
Definition: core_mqtt_serializer.h:184
const char * pPassword
MQTT password. Set to NULL if not used.
Definition: core_mqtt_serializer.h:179
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:135
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:137
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:136
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:256
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:270
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:260
size_t headerLength
The length of the MQTT header including the type and length.
Definition: core_mqtt_serializer.h:275
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:265
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:214
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:218
bool retain
Whether this is a retained message.
Definition: core_mqtt_serializer.h:223
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:238
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:248
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:228
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:233
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:243
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:192
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:196
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:206
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:201
Transport interface definitions to send and receive data over the network.