coreMQTT v2.3.1+
MQTT 3.1.1 Client Library
 
Loading...
Searching...
No Matches
core_mqtt_serializer.h
Go to the documentation of this file.
1/*
2 * coreMQTT <DEVELOPMENT BRANCH>
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#include "transport_interface.h"
46
47/* MQTT packet types. */
48
53#define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
54#define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
55#define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
56#define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
57#define MQTT_PACKET_TYPE_PUBREC ( ( uint8_t ) 0x50U )
58#define MQTT_PACKET_TYPE_PUBREL ( ( uint8_t ) 0x62U )
59#define MQTT_PACKET_TYPE_PUBCOMP ( ( uint8_t ) 0x70U )
60#define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
61#define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
62#define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xA2U )
63#define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xB0U )
64#define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xC0U )
65#define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xD0U )
66#define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xE0U )
73#define MQTT_PUBLISH_ACK_PACKET_SIZE ( 4UL )
74
75/* Structures defined in this file. */
76struct MQTTFixedBuffer;
77struct MQTTConnectInfo;
78struct MQTTSubscribeInfo;
79struct MQTTPublishInfo;
80struct MQTTPacketInfo;
81
86typedef enum MQTTStatus
87{
110
115typedef enum MQTTQoS
116{
119 MQTTQoS2 = 2
121
129typedef struct MQTTFixedBuffer
130{
131 uint8_t * pBuffer;
132 size_t size;
134
139typedef struct MQTTConnectInfo
140{
145
150
154 const char * pClientIdentifier;
155
160
164 const char * pUserName;
165
170
174 const char * pPassword;
175
181
186typedef struct MQTTSubscribeInfo
187{
192
196 const char * pTopicFilter;
197
203
208typedef struct MQTTPublishInfo
209{
214
218 bool retain;
219
223 bool dup;
224
228 const char * pTopicName;
229
234
238 const void * pPayload;
239
245
250typedef struct MQTTPacketInfo
251{
255 uint8_t type;
256
260 uint8_t * pRemainingData;
261
266
272
320/* @[declare_mqtt_getconnectpacketsize] */
322 const MQTTPublishInfo_t * pWillInfo,
323 size_t * pRemainingLength,
324 size_t * pPacketSize );
325/* @[declare_mqtt_getconnectpacketsize] */
326
376/* @[declare_mqtt_serializeconnect] */
378 const MQTTPublishInfo_t * pWillInfo,
379 size_t remainingLength,
380 const MQTTFixedBuffer_t * pFixedBuffer );
381/* @[declare_mqtt_serializeconnect] */
382
434/* @[declare_mqtt_getsubscribepacketsize] */
436 size_t subscriptionCount,
437 size_t * pRemainingLength,
438 size_t * pPacketSize );
439/* @[declare_mqtt_getsubscribepacketsize] */
440
500/* @[declare_mqtt_serializesubscribe] */
502 size_t subscriptionCount,
503 uint16_t packetId,
504 size_t remainingLength,
505 const MQTTFixedBuffer_t * pFixedBuffer );
506/* @[declare_mqtt_serializesubscribe] */
507
551/* @[declare_mqtt_getunsubscribepacketsize] */
553 size_t subscriptionCount,
554 size_t * pRemainingLength,
555 size_t * pPacketSize );
556/* @[declare_mqtt_getunsubscribepacketsize] */
557
617/* @[declare_mqtt_serializeunsubscribe] */
619 size_t subscriptionCount,
620 uint16_t packetId,
621 size_t remainingLength,
622 const MQTTFixedBuffer_t * pFixedBuffer );
623/* @[declare_mqtt_serializeunsubscribe] */
624
671/* @[declare_mqtt_getpublishpacketsize] */
673 size_t * pRemainingLength,
674 size_t * pPacketSize );
675/* @[declare_mqtt_getpublishpacketsize] */
676
739/* @[declare_mqtt_serializepublish] */
741 uint16_t packetId,
742 size_t remainingLength,
743 const MQTTFixedBuffer_t * pFixedBuffer );
744/* @[declare_mqtt_serializepublish] */
745
760 size_t remainingLength,
761 uint8_t * pBuffer,
762 size_t * headerSize );
763
837/* @[declare_mqtt_serializepublishheader] */
839 uint16_t packetId,
840 size_t remainingLength,
841 const MQTTFixedBuffer_t * pFixedBuffer,
842 size_t * pHeaderSize );
843/* @[declare_mqtt_serializepublishheader] */
844
886/* @[declare_mqtt_serializeack] */
888 uint8_t packetType,
889 uint16_t packetId );
890/* @[declare_mqtt_serializeack] */
891
916/* @[declare_mqtt_getdisconnectpacketsize] */
917MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
918/* @[declare_mqtt_getdisconnectpacketsize] */
919
957/* @[declare_mqtt_serializedisconnect] */
959/* @[declare_mqtt_serializedisconnect] */
960
985/* @[declare_mqtt_getpingreqpacketsize] */
986MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
987/* @[declare_mqtt_getpingreqpacketsize] */
988
1026/* @[declare_mqtt_serializepingreq] */
1028/* @[declare_mqtt_serializepingreq] */
1029
1087/* @[declare_mqtt_deserializepublish] */
1089 uint16_t * pPacketId,
1090 MQTTPublishInfo_t * pPublishInfo );
1091/* @[declare_mqtt_deserializepublish] */
1092
1131/* @[declare_mqtt_deserializeack] */
1132MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
1133 uint16_t * pPacketId,
1134 bool * pSessionPresent );
1135/* @[declare_mqtt_deserializeack] */
1136
1197/* @[declare_mqtt_getincomingpackettypeandlength] */
1199 NetworkContext_t * pNetworkContext,
1200 MQTTPacketInfo_t * pIncomingPacket );
1201/* @[declare_mqtt_getincomingpackettypeandlength] */
1202
1221 /* @[declare_mqtt_processincomingpackettypeandlength] */
1223 const size_t * pIndex,
1224 MQTTPacketInfo_t * pIncomingPacket );
1225/* @[declare_mqtt_processincomingpackettypeandlength] */
1226
1236 /* @[declare_mqtt_updateduplicatepublishflag] */
1237MQTTStatus_t MQTT_UpdateDuplicatePublishFlag( uint8_t * pHeader , bool set);
1238/* @[declare_mqtt_updateduplicatepublishflag] */
1239
1258uint8_t * MQTT_SerializeConnectFixedHeader( uint8_t * pIndex,
1259 const MQTTConnectInfo_t * pConnectInfo,
1260 const MQTTPublishInfo_t * pWillInfo,
1261 size_t remainingLength );
1281uint8_t * MQTT_SerializeSubscribeHeader( size_t remainingLength,
1282 uint8_t * pIndex,
1283 uint16_t packetId );
1303uint8_t * MQTT_SerializeUnsubscribeHeader( size_t remainingLength,
1304 uint8_t * pIndex,
1305 uint16_t packetId );
1308/* *INDENT-OFF* */
1309#ifdef __cplusplus
1310 }
1311#endif
1312/* *INDENT-ON* */
1313
1314#endif /* ifndef CORE_MQTT_SERIALIZER_H */
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:1915
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:1558
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:1891
MQTTStatus_t MQTT_GetPingreqPacketSize(size_t *pPacketSize)
Get the size of an MQTT PINGREQ packet.
Definition: core_mqtt_serializer.c:2389
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:2271
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:1937
MQTTStatus_t MQTT_UpdateDuplicatePublishFlag(uint8_t *pHeader, bool set)
Update the duplicate publish flag within the given header of the publish packet.
Definition: core_mqtt_serializer.c:2631
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:1983
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:641
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2452
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:1695
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:2188
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:2346
MQTTStatus_t MQTT_GetDisconnectPacketSize(size_t *pPacketSize)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:2326
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:2658
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:2566
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:2062
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:1795
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:2021
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:1853
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:2103
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:2489
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:2409
int32_t(* TransportRecv_t)(NetworkContext_t *pNetworkContext, void *pBuffer, size_t bytesToRecv)
Transport interface for receiving data on the network.
Definition: transport_interface.h:218
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:87
MQTTQoS_t
MQTT Quality of Service values.
Definition: core_mqtt_serializer.h:116
@ MQTTStatusNotConnected
Definition: core_mqtt_serializer.h:103
@ MQTTKeepAliveTimeout
Definition: core_mqtt_serializer.h:98
@ MQTTStatusDisconnectPending
Definition: core_mqtt_serializer.h:104
@ MQTTServerRefused
Definition: core_mqtt_serializer.h:94
@ MQTTPublishRetrieveFailed
Definition: core_mqtt_serializer.h:107
@ MQTTSuccess
Definition: core_mqtt_serializer.h:88
@ MQTTPublishStoreFailed
Definition: core_mqtt_serializer.h:105
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:95
@ MQTTIllegalState
Definition: core_mqtt_serializer.h:96
@ MQTTStateCollision
Definition: core_mqtt_serializer.h:97
@ MQTTStatusConnected
Definition: core_mqtt_serializer.h:102
@ MQTTRecvFailed
Definition: core_mqtt_serializer.h:92
@ MQTTBadParameter
Definition: core_mqtt_serializer.h:89
@ MQTTBadResponse
Definition: core_mqtt_serializer.h:93
@ MQTTNeedMoreBytes
Definition: core_mqtt_serializer.h:99
@ MQTTNoMemory
Definition: core_mqtt_serializer.h:90
@ MQTTSendFailed
Definition: core_mqtt_serializer.h:91
@ MQTTQoS1
Definition: core_mqtt_serializer.h:118
@ MQTTQoS2
Definition: core_mqtt_serializer.h:119
@ MQTTQoS0
Definition: core_mqtt_serializer.h:117
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:140
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:154
const char * pUserName
MQTT user name. Set to NULL if not used.
Definition: core_mqtt_serializer.h:164
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:144
uint16_t userNameLength
Length of MQTT user name. Set to 0 if not used.
Definition: core_mqtt_serializer.h:169
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:149
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:159
uint16_t passwordLength
Length of MQTT password. Set to 0 if not used.
Definition: core_mqtt_serializer.h:179
const char * pPassword
MQTT password. Set to NULL if not used.
Definition: core_mqtt_serializer.h:174
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:130
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:132
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:131
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:251
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:265
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:255
size_t headerLength
The length of the MQTT header including the type and length.
Definition: core_mqtt_serializer.h:270
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:260
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:209
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:213
bool retain
Whether this is a retained message.
Definition: core_mqtt_serializer.h:218
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:233
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:243
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:223
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:228
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:238
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:187
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:191
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:201
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:196
Transport interface definitions to send and receive data over the network.