coreMQTT v1.1.0
MQTT 3.1.1 Client Library
core_mqtt.h
Go to the documentation of this file.
1/*
2 * coreMQTT v1.1.0
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
27#ifndef CORE_MQTT_H
28#define CORE_MQTT_H
29
30/* MQTT_DO_NOT_USE_CUSTOM_CONFIG allows building the MQTT library
31 * without a custom config. If a custom config is provided, the
32 * MQTT_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
33#ifndef MQTT_DO_NOT_USE_CUSTOM_CONFIG
34 /* Include custom config file before other headers. */
35 #include "core_mqtt_config.h"
36#endif
37
38/* Include config defaults header to get default values of configs not
39 * defined in core_mqtt_config.h file. */
41
42/* Include MQTT serializer library. */
44
45/* Include transport interface. */
46#include "transport_interface.h"
47
52#define MQTT_LIBRARY_VERSION "v1.1.0"
61#define MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U )
62
63/* Structures defined in this file. */
64struct MQTTPubAckInfo;
65struct MQTTContext;
66struct MQTTDeserializedInfo;
67
75typedef uint32_t (* MQTTGetCurrentTimeFunc_t )( void );
76
90typedef void (* MQTTEventCallback_t )( struct MQTTContext * pContext,
91 struct MQTTPacketInfo * pPacketInfo,
92 struct MQTTDeserializedInfo * pDeserializedInfo );
93
98typedef enum MQTTConnectionStatus
99{
103
108typedef enum MQTTPublishState
109{
122
127typedef enum MQTTPubAckType
128{
134
139typedef enum MQTTSubAckStatus
140{
144 MQTTSubAckFailure = 0x80
146
151typedef struct MQTTPubAckInfo
152{
153 uint16_t packetId;
157
162typedef struct MQTTContext
163{
168
173
178
183
187 uint16_t nextPacketId;
188
193
198
203
208
214
215 /* Keep alive members. */
220
226typedef struct MQTTDeserializedInfo
227{
232
298/* @[declare_mqtt_init] */
300 const TransportInterface_t * pTransportInterface,
301 MQTTGetCurrentTimeFunc_t getTimeFunction,
302 MQTTEventCallback_t userCallback,
303 const MQTTFixedBuffer_t * pNetworkBuffer );
304/* @[declare_mqtt_init] */
305
408/* @[declare_mqtt_connect] */
410 const MQTTConnectInfo_t * pConnectInfo,
411 const MQTTPublishInfo_t * pWillInfo,
412 uint32_t timeoutMs,
413 bool * pSessionPresent );
414/* @[declare_mqtt_connect] */
415
465/* @[declare_mqtt_subscribe] */
467 const MQTTSubscribeInfo_t * pSubscriptionList,
468 size_t subscriptionCount,
469 uint16_t packetId );
470/* @[declare_mqtt_subscribe] */
471
513/* @[declare_mqtt_publish] */
515 const MQTTPublishInfo_t * pPublishInfo,
516 uint16_t packetId );
517/* @[declare_mqtt_publish] */
518
529/* @[declare_mqtt_ping] */
531/* @[declare_mqtt_ping] */
532
581/* @[declare_mqtt_unsubscribe] */
583 const MQTTSubscribeInfo_t * pSubscriptionList,
584 size_t subscriptionCount,
585 uint16_t packetId );
586/* @[declare_mqtt_unsubscribe] */
587
599/* @[declare_mqtt_disconnect] */
601/* @[declare_mqtt_disconnect] */
602
660/* @[declare_mqtt_processloop] */
662 uint32_t timeoutMs );
663/* @[declare_mqtt_processloop] */
664
727/* @[declare_mqtt_receiveloop] */
729 uint32_t timeoutMs );
730/* @[declare_mqtt_receiveloop] */
731
739/* @[declare_mqtt_getpacketid] */
740uint16_t MQTT_GetPacketId( MQTTContext_t * pContext );
741/* @[declare_mqtt_getpacketid] */
742
785MQTTStatus_t MQTT_MatchTopic( const char * pTopicName,
786 const uint16_t topicNameLength,
787 const char * pTopicFilter,
788 const uint16_t topicFilterLength,
789 bool * pIsMatch );
790
873/* @[declare_mqtt_getsubackstatuscodes] */
875 uint8_t ** pPayloadStart,
876 size_t * pPayloadSize );
877/* @[declare_mqtt_getsubackstatuscodes] */
878
886/* @[declare_mqtt_status_strerror] */
887const char * MQTT_Status_strerror( MQTTStatus_t status );
888/* @[declare_mqtt_status_strerror] */
889
890#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:2270
const char * MQTT_Status_strerror(MQTTStatus_t status)
Error code to string conversion for MQTT statuses.
Definition: core_mqtt.c:2423
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:1907
MQTTStatus_t MQTT_ReceiveLoop(MQTTContext_t *pContext, uint32_t timeoutMs)
Loop to receive packets from the transport interface. Does not handle keep alive.
Definition: core_mqtt.c:2215
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:1847
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:2295
MQTTStatus_t MQTT_Ping(MQTTContext_t *pContext)
Sends an MQTT PINGREQ to broker.
Definition: core_mqtt.c:1981
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:2042
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:2364
MQTTStatus_t MQTT_Disconnect(MQTTContext_t *pContext)
Disconnect an MQTT session.
Definition: core_mqtt.c:2102
MQTTStatus_t MQTT_ProcessLoop(MQTTContext_t *pContext, uint32_t timeoutMs)
Loop to receive packets from the transport interface. Handles keep alive.
Definition: core_mqtt.c:2158
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:1691
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:1750
This represents the default values for the configuration macros for the MQTT library.
#define MQTT_STATE_ARRAY_MAX_COUNT
Determines the maximum number of MQTT PUBLISH messages, pending acknowledgment at a time,...
Definition: core_mqtt_config_defaults.h:76
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:90
uint32_t(* MQTTGetCurrentTimeFunc_t)(void)
Application provided function to query the current time in milliseconds.
Definition: core_mqtt.h:75
MQTTPublishState_t
The state of QoS 1 or QoS 2 MQTT publishes, used in the state engine.
Definition: core_mqtt.h:109
MQTTSubAckStatus_t
The status codes in the SUBACK response to a subscription request.
Definition: core_mqtt.h:140
MQTTPubAckType_t
Packet types used in acknowledging QoS 1 or QoS 2 publishes.
Definition: core_mqtt.h:128
MQTTConnectionStatus_t
Values indicating if an MQTT connection exists.
Definition: core_mqtt.h:99
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:91
MQTTQoS_t
MQTT Quality of Service values.
Definition: core_mqtt_serializer.h:110
@ MQTTPubRecSend
The library will send a PUBREC for a received PUBLISH.
Definition: core_mqtt.h:113
@ MQTTPubRecPending
The library is awaiting a PUBREC for an outgoing PUBLISH.
Definition: core_mqtt.h:117
@ MQTTPubCompPending
The library is awaiting a PUBCOMP for an outgoing PUBLISH.
Definition: core_mqtt.h:119
@ MQTTPubRelSend
The library will send a PUBREL for a received PUBREC.
Definition: core_mqtt.h:114
@ MQTTPubAckSend
The library will send a PUBACK for a received PUBLISH.
Definition: core_mqtt.h:112
@ MQTTPubRelPending
The library is awaiting a PUBREL for an incoming PUBLISH.
Definition: core_mqtt.h:118
@ MQTTPubCompSend
The library will send a PUBCOMP for a received PUBREL.
Definition: core_mqtt.h:115
@ MQTTStateNull
An empty state with no corresponding PUBLISH.
Definition: core_mqtt.h:110
@ MQTTPublishSend
The library will send an outgoing PUBLISH packet.
Definition: core_mqtt.h:111
@ MQTTPubAckPending
The library is awaiting a PUBACK for an outgoing PUBLISH.
Definition: core_mqtt.h:116
@ MQTTPublishDone
The PUBLISH has been completed.
Definition: core_mqtt.h:120
@ MQTTSubAckSuccessQos2
Success with a maximum delivery at QoS 2.
Definition: core_mqtt.h:143
@ MQTTSubAckSuccessQos1
Success with a maximum delivery at QoS 1.
Definition: core_mqtt.h:142
@ MQTTSubAckSuccessQos0
Success with a maximum delivery at QoS 0 .
Definition: core_mqtt.h:141
@ MQTTSubAckFailure
Failure.
Definition: core_mqtt.h:144
@ MQTTPuback
PUBACKs are sent in response to a QoS 1 PUBLISH.
Definition: core_mqtt.h:129
@ MQTTPubrec
PUBRECs are sent in response to a QoS 2 PUBLISH.
Definition: core_mqtt.h:130
@ MQTTPubcomp
PUBCOMPs are sent in response to a PUBREL.
Definition: core_mqtt.h:132
@ MQTTPubrel
PUBRELs are sent in response to a PUBREC.
Definition: core_mqtt.h:131
@ MQTTNotConnected
MQTT Connection is inactive.
Definition: core_mqtt.h:100
@ MQTTConnected
MQTT Connection is active.
Definition: core_mqtt.h:101
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:134
A struct representing an MQTT connection.
Definition: core_mqtt.h:163
MQTTFixedBuffer_t networkBuffer
The buffer used in sending and receiving packets from the network.
Definition: core_mqtt.h:182
MQTTConnectionStatus_t connectStatus
Whether the context currently has a connection to the broker.
Definition: core_mqtt.h:192
MQTTEventCallback_t appCallback
Callback function used to give deserialized MQTT packets to the application.
Definition: core_mqtt.h:202
TransportInterface_t transportInterface
The transport interface used by the MQTT connection.
Definition: core_mqtt.h:177
MQTTGetCurrentTimeFunc_t getTime
Function used to get millisecond timestamps.
Definition: core_mqtt.h:197
uint32_t lastPacketTime
Timestamp of the last packet sent by the library.
Definition: core_mqtt.h:207
bool waitingForPingResp
If the library is currently awaiting a PINGRESP.
Definition: core_mqtt.h:218
uint32_t pingReqSendTimeMs
Timestamp of the last sent PINGREQ.
Definition: core_mqtt.h:217
uint16_t nextPacketId
The next available ID for outgoing MQTT packets.
Definition: core_mqtt.h:187
bool controlPacketSent
Whether the library sent a packet during a call of MQTT_ProcessLoop or MQTT_ReceiveLoop.
Definition: core_mqtt.h:213
uint16_t keepAliveIntervalSec
Keep Alive interval.
Definition: core_mqtt.h:216
Struct to hold deserialized packet information for an MQTTEventCallback_t callback.
Definition: core_mqtt.h:227
MQTTStatus_t deserializationResult
Return code of deserialization.
Definition: core_mqtt.h:230
MQTTPublishInfo_t * pPublishInfo
Pointer to deserialized publish info.
Definition: core_mqtt.h:229
uint16_t packetIdentifier
Packet ID of deserialized packet.
Definition: core_mqtt.h:228
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:124
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:245
An element of the state engine records for QoS 1 or Qos 2 publishes.
Definition: core_mqtt.h:152
MQTTQoS_t qos
The QoS of the original PUBLISH.
Definition: core_mqtt.h:154
MQTTPublishState_t publishState
The current state of the publish process.
Definition: core_mqtt.h:155
uint16_t packetId
The packet ID of the original PUBLISH.
Definition: core_mqtt.h:153
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:203
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:181
The transport layer interface.
Definition: transport_interface.h:246
Transport interface definitions to send and receive data over the network.