coreMQTT v2.3.1+
MQTT 3.1.1 Client Library
 
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Loading...
Searching...
No Matches
core_mqtt.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
29#ifndef CORE_MQTT_H
30#define CORE_MQTT_H
31
32/* *INDENT-OFF* */
33#ifdef __cplusplus
34 extern "C" {
35#endif
36/* *INDENT-ON* */
37
38/* Include MQTT serializer library. */
40
41/* Include transport interface. */
42#include "transport_interface.h"
43
51#define MQTT_LIBRARY_VERSION "v2.3.1+"
60#define MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U )
61
62/* Structures defined in this file. */
63struct MQTTPubAckInfo;
64struct MQTTContext;
65struct MQTTDeserializedInfo;
66
71typedef struct MQTTVec MQTTVec_t;
72
91typedef uint32_t (* MQTTGetCurrentTimeFunc_t )( void );
92
106typedef void (* MQTTEventCallback_t )( struct MQTTContext * pContext,
107 struct MQTTPacketInfo * pPacketInfo,
108 struct MQTTDeserializedInfo * pDeserializedInfo );
109
122/* @[define_mqtt_retransmitstorepacket] */
123typedef bool ( * MQTTStorePacketForRetransmit)( struct MQTTContext * pContext,
124 uint16_t packetId,
125 MQTTVec_t * pMqttVec );
126/* @[define_mqtt_retransmitstorepacket] */
127
142/* @[define_mqtt_retransmitretrievepacket] */
143typedef bool ( * MQTTRetrievePacketForRetransmit)( struct MQTTContext * pContext,
144 uint16_t packetId,
145 uint8_t ** pSerializedMqttVec,
146 size_t * pSerializedMqttVecLen );
147/* @[define_mqtt_retransmitretrievepacket] */
148
156/* @[define_mqtt_retransmitclearpacket] */
157typedef void (* MQTTClearPacketForRetransmit)( struct MQTTContext * pContext,
158 uint16_t packetId );
159/* @[define_mqtt_retransmitclearpacket] */
160
165typedef enum MQTTConnectionStatus
166{
171
176typedef enum MQTTPublishState
177{
190
195typedef enum MQTTPubAckType
196{
202
207typedef enum MQTTSubAckStatus
208{
212 MQTTSubAckFailure = 0x80
214
219typedef struct MQTTPubAckInfo
220{
221 uint16_t packetId;
225
230typedef struct MQTTContext
231{
236
241
246
251
256
261
265 uint16_t nextPacketId;
266
271
276
281
286
291
297
301 size_t index;
302
303 /* Keep alive members. */
312
317
323
329typedef struct MQTTDeserializedInfo
330{
335
409/* @[declare_mqtt_init] */
411 const TransportInterface_t * pTransportInterface,
412 MQTTGetCurrentTimeFunc_t getTimeFunction,
413 MQTTEventCallback_t userCallback,
414 const MQTTFixedBuffer_t * pNetworkBuffer );
415/* @[declare_mqtt_init] */
416
482/* @[declare_mqtt_initstatefulqos] */
484 MQTTPubAckInfo_t * pOutgoingPublishRecords,
485 size_t outgoingPublishCount,
486 MQTTPubAckInfo_t * pIncomingPublishRecords,
487 size_t incomingPublishCount );
488/* @[declare_mqtt_initstatefulqos] */
489
575/* @[declare_mqtt_initretransmits] */
577 MQTTStorePacketForRetransmit storeFunction,
578 MQTTRetrievePacketForRetransmit retrieveFunction,
579 MQTTClearPacketForRetransmit clearFunction );
580/* @[declare_mqtt_initretransmits] */
581
597/* @[declare_mqtt_checkconnectstatus] */
599/* @[declare_mqtt_checkconnectstatus] */
600
709/* @[declare_mqtt_connect] */
711 const MQTTConnectInfo_t * pConnectInfo,
712 const MQTTPublishInfo_t * pWillInfo,
713 uint32_t timeoutMs,
714 bool * pSessionPresent );
715/* @[declare_mqtt_connect] */
716
770/* @[declare_mqtt_subscribe] */
772 const MQTTSubscribeInfo_t * pSubscriptionList,
773 size_t subscriptionCount,
774 uint16_t packetId );
775/* @[declare_mqtt_subscribe] */
776
823/* @[declare_mqtt_publish] */
825 const MQTTPublishInfo_t * pPublishInfo,
826 uint16_t packetId );
827/* @[declare_mqtt_publish] */
828
846/* @[declare_mqtt_cancelcallback] */
848 uint16_t packetId );
849/* @[declare_mqtt_cancelcallback] */
850
864/* @[declare_mqtt_ping] */
866/* @[declare_mqtt_ping] */
867
919/* @[declare_mqtt_unsubscribe] */
921 const MQTTSubscribeInfo_t * pSubscriptionList,
922 size_t subscriptionCount,
923 uint16_t packetId );
924/* @[declare_mqtt_unsubscribe] */
925
938/* @[declare_mqtt_disconnect] */
940/* @[declare_mqtt_disconnect] */
941
1000/* @[declare_mqtt_processloop] */
1002/* @[declare_mqtt_processloop] */
1003
1064/* @[declare_mqtt_receiveloop] */
1066/* @[declare_mqtt_receiveloop] */
1067
1075/* @[declare_mqtt_getpacketid] */
1076uint16_t MQTT_GetPacketId( MQTTContext_t * pContext );
1077/* @[declare_mqtt_getpacketid] */
1078
1123MQTTStatus_t MQTT_MatchTopic( const char * pTopicName,
1124 const uint16_t topicNameLength,
1125 const char * pTopicFilter,
1126 const uint16_t topicFilterLength,
1127 bool * pIsMatch );
1128
1211/* @[declare_mqtt_getsubackstatuscodes] */
1213 uint8_t ** pPayloadStart,
1214 size_t * pPayloadSize );
1215/* @[declare_mqtt_getsubackstatuscodes] */
1216
1224/* @[declare_mqtt_status_strerror] */
1225const char * MQTT_Status_strerror( MQTTStatus_t status );
1226/* @[declare_mqtt_status_strerror] */
1227
1235/* @[declare_mqtt_getbytesinmqttvec] */
1236size_t MQTT_GetBytesInMQTTVec( const MQTTVec_t * pVec );
1237/* @[declare_mqtt_getbytesinmqttvec] */
1238
1245/* @[declare_mqtt_serializemqttvec] */
1246void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem,
1247 const MQTTVec_t * pVec );
1248/* @[declare_mqtt_serializemqttvec] */
1249
1250/* *INDENT-OFF* */
1251#ifdef __cplusplus
1252 }
1253#endif
1254/* *INDENT-ON* */
1255
1256#endif /* ifndef CORE_MQTT_H */
uint16_t MQTT_GetPacketId(MQTTContext_t *pContext)
Get a packet ID that is valid according to the MQTT 3.1.1 spec.
Definition: core_mqtt.c:3491
const char * MQTT_Status_strerror(MQTTStatus_t status)
Error code to string conversion for MQTT statuses.
Definition: core_mqtt.c:3648
MQTTStatus_t MQTT_Publish(MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId)
Publishes a message to the given topic name.
Definition: core_mqtt.c:3090
MQTTStatus_t MQTT_CancelCallback(const MQTTContext_t *pContext, uint16_t packetId)
Cancels an outgoing publish callback (only for QoS > QoS0) by removing it from the pending ACK list.
Definition: core_mqtt.c:2835
size_t MQTT_GetBytesInMQTTVec(const MQTTVec_t *pVec)
Get the bytes in a MQTTVec pointer which can store the whole array as a an MQTT packet when calling M...
Definition: core_mqtt.c:3732
bool(* MQTTRetrievePacketForRetransmit)(struct MQTTContext *pContext, uint16_t packetId, uint8_t **pSerializedMqttVec, size_t *pSerializedMqttVecLen)
User defined callback used to retreive a copied publish for resend operation. Used to track any publi...
Definition: core_mqtt.h:143
MQTTStatus_t MQTT_Subscribe(MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId)
Sends MQTT SUBSCRIBE for the given list of topic filters to the broker.
Definition: core_mqtt.c:3035
MQTTStatus_t MQTT_MatchTopic(const char *pTopicName, const uint16_t topicNameLength, const char *pTopicFilter, const uint16_t topicFilterLength, bool *pIsMatch)
A utility function that determines whether the passed topic filter and topic name match according to ...
Definition: core_mqtt.c:3520
MQTTStatus_t MQTT_Ping(MQTTContext_t *pContext)
Sends an MQTT PINGREQ to broker.
Definition: core_mqtt.c:3211
MQTTStatus_t MQTT_Unsubscribe(MQTTContext_t *pContext, const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId)
Sends MQTT UNSUBSCRIBE for the given list of topic filters to the broker.
Definition: core_mqtt.c:3300
void(* MQTTClearPacketForRetransmit)(struct MQTTContext *pContext, uint16_t packetId)
User defined callback used to clear a particular copied publish packet. Used to track any publish ret...
Definition: core_mqtt.h:157
MQTTStatus_t MQTT_CheckConnectStatus(const MQTTContext_t *pContext)
Checks the MQTT connection status with the broker.
Definition: core_mqtt.c:2867
void MQTT_SerializeMQTTVec(uint8_t *pAllocatedMem, const MQTTVec_t *pVec)
Serialize the bytes in an array of MQTTVec in the provided pAllocatedMem.
Definition: core_mqtt.c:3749
MQTTStatus_t MQTT_ProcessLoop(MQTTContext_t *pContext)
Loop to receive packets from the transport interface. Handles keep alive.
Definition: core_mqtt.c:3438
MQTTStatus_t MQTT_GetSubAckStatusCodes(const MQTTPacketInfo_t *pSubackPacket, uint8_t **pPayloadStart, size_t *pPayloadSize)
Parses the payload of an MQTT SUBACK packet that contains status codes corresponding to topic filter ...
Definition: core_mqtt.c:3589
MQTTStatus_t MQTT_Disconnect(MQTTContext_t *pContext)
Disconnect an MQTT session.
Definition: core_mqtt.c:3355
bool(* MQTTStorePacketForRetransmit)(struct MQTTContext *pContext, uint16_t packetId, MQTTVec_t *pMqttVec)
User defined callback used to store outgoing publishes. Used to track any publish retransmit on an un...
Definition: core_mqtt.h:123
MQTTStatus_t MQTT_Init(MQTTContext_t *pContext, const TransportInterface_t *pTransportInterface, MQTTGetCurrentTimeFunc_t getTimeFunction, MQTTEventCallback_t userCallback, const MQTTFixedBuffer_t *pNetworkBuffer)
Initialize an MQTT context.
Definition: core_mqtt.c:2679
MQTTStatus_t MQTT_ReceiveLoop(MQTTContext_t *pContext)
Loop to receive packets from the transport interface. Does not handle keep alive.
Definition: core_mqtt.c:3465
MQTTStatus_t MQTT_Connect(MQTTContext_t *pContext, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t timeoutMs, bool *pSessionPresent)
Establish an MQTT session.
Definition: core_mqtt.c:2908
MQTTStatus_t MQTT_InitRetransmits(MQTTContext_t *pContext, MQTTStorePacketForRetransmit storeFunction, MQTTRetrievePacketForRetransmit retrieveFunction, MQTTClearPacketForRetransmit clearFunction)
Initialize an MQTT context for publish retransmits for QoS > 0.
Definition: core_mqtt.c:2795
MQTTStatus_t MQTT_InitStatefulQoS(MQTTContext_t *pContext, MQTTPubAckInfo_t *pOutgoingPublishRecords, size_t outgoingPublishCount, MQTTPubAckInfo_t *pIncomingPublishRecords, size_t incomingPublishCount)
Initialize an MQTT context for QoS > 0.
Definition: core_mqtt.c:2738
User-facing functions for serializing and deserializing MQTT 3.1.1 packets. This header should be inc...
void(* MQTTEventCallback_t)(struct MQTTContext *pContext, struct MQTTPacketInfo *pPacketInfo, struct MQTTDeserializedInfo *pDeserializedInfo)
Application callback for receiving incoming publishes and incoming acks.
Definition: core_mqtt.h:106
uint32_t(* MQTTGetCurrentTimeFunc_t)(void)
Application provided function to query the time elapsed since a given epoch in milliseconds.
Definition: core_mqtt.h:91
MQTTPublishState_t
The state of QoS 1 or QoS 2 MQTT publishes, used in the state engine.
Definition: core_mqtt.h:177
MQTTSubAckStatus_t
The status codes in the SUBACK response to a subscription request.
Definition: core_mqtt.h:208
MQTTPubAckType_t
Packet types used in acknowledging QoS 1 or QoS 2 publishes.
Definition: core_mqtt.h:196
MQTTConnectionStatus_t
Values indicating if an MQTT connection exists.
Definition: core_mqtt.h:166
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
@ MQTTPubRecSend
The library will send a PUBREC for a received PUBLISH.
Definition: core_mqtt.h:181
@ MQTTPubRecPending
The library is awaiting a PUBREC for an outgoing PUBLISH.
Definition: core_mqtt.h:185
@ MQTTPubCompPending
The library is awaiting a PUBCOMP for an outgoing PUBLISH.
Definition: core_mqtt.h:187
@ MQTTPubRelSend
The library will send a PUBREL for a received PUBREC.
Definition: core_mqtt.h:182
@ MQTTPubAckSend
The library will send a PUBACK for a received PUBLISH.
Definition: core_mqtt.h:180
@ MQTTPubRelPending
The library is awaiting a PUBREL for an incoming PUBLISH.
Definition: core_mqtt.h:186
@ MQTTPubCompSend
The library will send a PUBCOMP for a received PUBREL.
Definition: core_mqtt.h:183
@ MQTTStateNull
An empty state with no corresponding PUBLISH.
Definition: core_mqtt.h:178
@ MQTTPublishSend
The library will send an outgoing PUBLISH packet.
Definition: core_mqtt.h:179
@ MQTTPubAckPending
The library is awaiting a PUBACK for an outgoing PUBLISH.
Definition: core_mqtt.h:184
@ MQTTPublishDone
The PUBLISH has been completed.
Definition: core_mqtt.h:188
@ MQTTSubAckSuccessQos2
Success with a maximum delivery at QoS 2.
Definition: core_mqtt.h:211
@ MQTTSubAckSuccessQos1
Success with a maximum delivery at QoS 1.
Definition: core_mqtt.h:210
@ MQTTSubAckSuccessQos0
Success with a maximum delivery at QoS 0.
Definition: core_mqtt.h:209
@ MQTTSubAckFailure
Failure.
Definition: core_mqtt.h:212
@ MQTTPuback
PUBACKs are sent in response to a QoS 1 PUBLISH.
Definition: core_mqtt.h:197
@ MQTTPubrec
PUBRECs are sent in response to a QoS 2 PUBLISH.
Definition: core_mqtt.h:198
@ MQTTPubcomp
PUBCOMPs are sent in response to a PUBREL.
Definition: core_mqtt.h:200
@ MQTTPubrel
PUBRELs are sent in response to a PUBREC.
Definition: core_mqtt.h:199
@ MQTTNotConnected
MQTT Connection is inactive.
Definition: core_mqtt.h:167
@ MQTTDisconnectPending
MQTT Connection needs to be disconnected as a transport error has occurred.
Definition: core_mqtt.h:169
@ MQTTConnected
MQTT Connection is active.
Definition: core_mqtt.h:168
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:140
A struct representing an MQTT connection.
Definition: core_mqtt.h:231
uint32_t lastPacketTxTime
Timestamp of the last packet sent by the library.
Definition: core_mqtt.h:285
MQTTFixedBuffer_t networkBuffer
The buffer used in sending and receiving packets from the network.
Definition: core_mqtt.h:260
MQTTRetrievePacketForRetransmit retrieveFunction
User defined API used to retreive a copied publish for resend operation.
Definition: core_mqtt.h:316
size_t outgoingPublishRecordMaxCount
The maximum number of outgoing publish records.
Definition: core_mqtt.h:245
MQTTClearPacketForRetransmit clearFunction
User defined API used to clear a particular copied publish packet.
Definition: core_mqtt.h:321
size_t index
Index to keep track of the number of bytes received in network buffer.
Definition: core_mqtt.h:301
MQTTConnectionStatus_t connectStatus
Whether the context currently has a connection to the broker.
Definition: core_mqtt.h:270
MQTTPubAckInfo_t * outgoingPublishRecords
State engine records for outgoing publishes.
Definition: core_mqtt.h:235
uint32_t lastPacketRxTime
Timestamp of the last packet received by the library.
Definition: core_mqtt.h:290
MQTTEventCallback_t appCallback
Callback function used to give deserialized MQTT packets to the application.
Definition: core_mqtt.h:280
TransportInterface_t transportInterface
The transport interface used by the MQTT connection.
Definition: core_mqtt.h:255
size_t incomingPublishRecordMaxCount
The maximum number of incoming publish records.
Definition: core_mqtt.h:250
MQTTGetCurrentTimeFunc_t getTime
Function used to get millisecond timestamps.
Definition: core_mqtt.h:275
MQTTStorePacketForRetransmit storeFunction
User defined API used to store outgoing publishes.
Definition: core_mqtt.h:311
bool waitingForPingResp
If the library is currently awaiting a PINGRESP.
Definition: core_mqtt.h:306
uint32_t pingReqSendTimeMs
Timestamp of the last sent PINGREQ.
Definition: core_mqtt.h:305
uint16_t nextPacketId
The next available ID for outgoing MQTT packets.
Definition: core_mqtt.h:265
bool controlPacketSent
Whether the library sent a packet during a call of MQTT_ProcessLoop or MQTT_ReceiveLoop.
Definition: core_mqtt.h:296
MQTTPubAckInfo_t * incomingPublishRecords
State engine records for incoming publishes.
Definition: core_mqtt.h:240
uint16_t keepAliveIntervalSec
Keep Alive interval.
Definition: core_mqtt.h:304
Struct to hold deserialized packet information for an MQTTEventCallback_t callback.
Definition: core_mqtt.h:330
MQTTStatus_t deserializationResult
Return code of deserialization.
Definition: core_mqtt.h:333
MQTTPublishInfo_t * pPublishInfo
Pointer to deserialized publish info.
Definition: core_mqtt.h:332
uint16_t packetIdentifier
Packet ID of deserialized packet.
Definition: core_mqtt.h:331
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:130
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:251
An element of the state engine records for QoS 1 or Qos 2 publishes.
Definition: core_mqtt.h:220
MQTTQoS_t qos
The QoS of the original PUBLISH.
Definition: core_mqtt.h:222
MQTTPublishState_t publishState
The current state of the publish process.
Definition: core_mqtt.h:223
uint16_t packetId
The packet ID of the original PUBLISH.
Definition: core_mqtt.h:221
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:209
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:187
An opaque structure provided by the library to the MQTTStorePacketForRetransmit function when using M...
Definition: core_mqtt.c:94
The transport layer interface.
Definition: transport_interface.h:299
Transport interface definitions to send and receive data over the network.