coreMQTT v2.3.0
MQTT 3.1.1 Client Library
 
Loading...
Searching...
No Matches
core_mqtt_serializer.h
Go to the documentation of this file.
1/*
2 * coreMQTT v2.3.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#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{
103
108typedef enum MQTTQoS
109{
112 MQTTQoS2 = 2
114
122typedef struct MQTTFixedBuffer
123{
124 uint8_t * pBuffer;
125 size_t size;
127
132typedef struct MQTTConnectInfo
133{
138
143
147 const char * pClientIdentifier;
148
153
157 const char * pUserName;
158
163
167 const char * pPassword;
168
174
179typedef struct MQTTSubscribeInfo
180{
185
189 const char * pTopicFilter;
190
196
201typedef struct MQTTPublishInfo
202{
207
211 bool retain;
212
216 bool dup;
217
221 const char * pTopicName;
222
227
231 const void * pPayload;
232
238
243typedef struct MQTTPacketInfo
244{
248 uint8_t type;
249
253 uint8_t * pRemainingData;
254
259
265
313/* @[declare_mqtt_getconnectpacketsize] */
315 const MQTTPublishInfo_t * pWillInfo,
316 size_t * pRemainingLength,
317 size_t * pPacketSize );
318/* @[declare_mqtt_getconnectpacketsize] */
319
369/* @[declare_mqtt_serializeconnect] */
371 const MQTTPublishInfo_t * pWillInfo,
372 size_t remainingLength,
373 const MQTTFixedBuffer_t * pFixedBuffer );
374/* @[declare_mqtt_serializeconnect] */
375
427/* @[declare_mqtt_getsubscribepacketsize] */
429 size_t subscriptionCount,
430 size_t * pRemainingLength,
431 size_t * pPacketSize );
432/* @[declare_mqtt_getsubscribepacketsize] */
433
493/* @[declare_mqtt_serializesubscribe] */
495 size_t subscriptionCount,
496 uint16_t packetId,
497 size_t remainingLength,
498 const MQTTFixedBuffer_t * pFixedBuffer );
499/* @[declare_mqtt_serializesubscribe] */
500
544/* @[declare_mqtt_getunsubscribepacketsize] */
546 size_t subscriptionCount,
547 size_t * pRemainingLength,
548 size_t * pPacketSize );
549/* @[declare_mqtt_getunsubscribepacketsize] */
550
610/* @[declare_mqtt_serializeunsubscribe] */
612 size_t subscriptionCount,
613 uint16_t packetId,
614 size_t remainingLength,
615 const MQTTFixedBuffer_t * pFixedBuffer );
616/* @[declare_mqtt_serializeunsubscribe] */
617
664/* @[declare_mqtt_getpublishpacketsize] */
666 size_t * pRemainingLength,
667 size_t * pPacketSize );
668/* @[declare_mqtt_getpublishpacketsize] */
669
732/* @[declare_mqtt_serializepublish] */
734 uint16_t packetId,
735 size_t remainingLength,
736 const MQTTFixedBuffer_t * pFixedBuffer );
737/* @[declare_mqtt_serializepublish] */
738
753 size_t remainingLength,
754 uint8_t * pBuffer,
755 size_t * headerSize );
756
830/* @[declare_mqtt_serializepublishheader] */
832 uint16_t packetId,
833 size_t remainingLength,
834 const MQTTFixedBuffer_t * pFixedBuffer,
835 size_t * pHeaderSize );
836/* @[declare_mqtt_serializepublishheader] */
837
879/* @[declare_mqtt_serializeack] */
881 uint8_t packetType,
882 uint16_t packetId );
883/* @[declare_mqtt_serializeack] */
884
909/* @[declare_mqtt_getdisconnectpacketsize] */
910MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
911/* @[declare_mqtt_getdisconnectpacketsize] */
912
950/* @[declare_mqtt_serializedisconnect] */
952/* @[declare_mqtt_serializedisconnect] */
953
978/* @[declare_mqtt_getpingreqpacketsize] */
979MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
980/* @[declare_mqtt_getpingreqpacketsize] */
981
1019/* @[declare_mqtt_serializepingreq] */
1021/* @[declare_mqtt_serializepingreq] */
1022
1080/* @[declare_mqtt_deserializepublish] */
1082 uint16_t * pPacketId,
1083 MQTTPublishInfo_t * pPublishInfo );
1084/* @[declare_mqtt_deserializepublish] */
1085
1124/* @[declare_mqtt_deserializeack] */
1125MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
1126 uint16_t * pPacketId,
1127 bool * pSessionPresent );
1128/* @[declare_mqtt_deserializeack] */
1129
1190/* @[declare_mqtt_getincomingpackettypeandlength] */
1192 NetworkContext_t * pNetworkContext,
1193 MQTTPacketInfo_t * pIncomingPacket );
1194/* @[declare_mqtt_getincomingpackettypeandlength] */
1195
1214 /* @[declare_mqtt_processincomingpackettypeandlength] */
1216 const size_t * pIndex,
1217 MQTTPacketInfo_t * pIncomingPacket );
1218/* @[declare_mqtt_processincomingpackettypeandlength] */
1219
1238uint8_t * MQTT_SerializeConnectFixedHeader( uint8_t * pIndex,
1239 const MQTTConnectInfo_t * pConnectInfo,
1240 const MQTTPublishInfo_t * pWillInfo,
1241 size_t remainingLength );
1261uint8_t * MQTT_SerializeSubscribeHeader( size_t remainingLength,
1262 uint8_t * pIndex,
1263 uint16_t packetId );
1283uint8_t * MQTT_SerializeUnsubscribeHeader( size_t remainingLength,
1284 uint8_t * pIndex,
1285 uint16_t packetId );
1288/* *INDENT-OFF* */
1289#ifdef __cplusplus
1290 }
1291#endif
1292/* *INDENT-ON* */
1293
1294#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:1910
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:1553
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:1886
MQTTStatus_t MQTT_GetPingreqPacketSize(size_t *pPacketSize)
Get the size of an MQTT PINGREQ packet.
Definition: core_mqtt_serializer.c:2384
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:2266
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:1932
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:1978
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:636
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:2447
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:1690
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:2183
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:2341
MQTTStatus_t MQTT_GetDisconnectPacketSize(size_t *pPacketSize)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:2321
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:2626
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:2561
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:2057
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:1790
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:2016
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:1848
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:2098
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:2484
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:2404
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:109
@ MQTTKeepAliveTimeout
Definition: core_mqtt_serializer.h:98
@ MQTTServerRefused
Definition: core_mqtt_serializer.h:94
@ MQTTSuccess
Definition: core_mqtt_serializer.h:88
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:95
@ MQTTIllegalState
Definition: core_mqtt_serializer.h:96
@ MQTTStateCollision
Definition: core_mqtt_serializer.h:97
@ 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:111
@ MQTTQoS2
Definition: core_mqtt_serializer.h:112
@ MQTTQoS0
Definition: core_mqtt_serializer.h:110
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:133
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:147
const char * pUserName
MQTT user name. Set to NULL if not used.
Definition: core_mqtt_serializer.h:157
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:137
uint16_t userNameLength
Length of MQTT user name. Set to 0 if not used.
Definition: core_mqtt_serializer.h:162
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:142
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:152
uint16_t passwordLength
Length of MQTT password. Set to 0 if not used.
Definition: core_mqtt_serializer.h:172
const char * pPassword
MQTT password. Set to NULL if not used.
Definition: core_mqtt_serializer.h:167
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:123
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:125
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:124
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:244
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:258
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:248
size_t headerLength
The length of the MQTT header including the type and length.
Definition: core_mqtt_serializer.h:263
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:253
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:202
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:206
bool retain
Whether this is a retained message.
Definition: core_mqtt_serializer.h:211
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:226
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:236
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:216
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:221
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:231
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:180
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:184
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:194
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:189
Transport interface definitions to send and receive data over the network.