MQTT API Reference
MQTT 3.1.1 client library
|
Go to the documentation of this file.
28 #ifndef IOT_MQTT_INTERNAL_H_
29 #define IOT_MQTT_INTERNAL_H_
32 #include "iot_config.h"
52 #if IOT_MQTT_ENABLE_ASSERTS == 1
53 #ifndef IotMqtt_Assert
54 #ifdef Iot_DefaultAssert
55 #define IotMqtt_Assert( expression ) Iot_DefaultAssert( expression )
57 #error "Asserts are enabled for MQTT, but IotMqtt_Assert is not defined"
61 #define IotMqtt_Assert( expression )
65 #ifdef IOT_LOG_LEVEL_MQTT
66 #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_MQTT
68 #ifdef IOT_LOG_LEVEL_GLOBAL
69 #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
71 #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
75 #define LIBRARY_LOG_NAME ( "MQTT" )
82 #if IOT_STATIC_MEMORY_ONLY == 1
104 #define IotMqtt_MallocMessage Iot_MallocMessageBuffer
111 #define IotMqtt_FreeMessage Iot_FreeMessageBuffer
141 #ifndef IotMqtt_MallocConnection
142 #ifdef Iot_DefaultMalloc
143 #define IotMqtt_MallocConnection Iot_DefaultMalloc
145 #error "No malloc function defined for IotMqtt_MallocConnection"
149 #ifndef IotMqtt_FreeConnection
150 #ifdef Iot_DefaultFree
151 #define IotMqtt_FreeConnection Iot_DefaultFree
153 #error "No free function defined for IotMqtt_FreeConnection"
157 #ifndef IotMqtt_MallocMessage
158 #ifdef Iot_DefaultMalloc
159 #define IotMqtt_MallocMessage Iot_DefaultMalloc
161 #error "No malloc function defined for IotMqtt_MallocMessage"
165 #ifndef IotMqtt_FreeMessage
166 #ifdef Iot_DefaultFree
167 #define IotMqtt_FreeMessage Iot_DefaultFree
169 #error "No free function defined for IotMqtt_FreeMessage"
173 #ifndef IotMqtt_MallocOperation
174 #ifdef Iot_DefaultMalloc
175 #define IotMqtt_MallocOperation Iot_DefaultMalloc
177 #error "No malloc function defined for IotMqtt_MallocOperation"
181 #ifndef IotMqtt_FreeOperation
182 #ifdef Iot_DefaultFree
183 #define IotMqtt_FreeOperation Iot_DefaultFree
185 #error "No free function defined for IotMqtt_FreeOperation"
189 #ifndef IotMqtt_MallocSubscription
190 #ifdef Iot_DefaultMalloc
191 #define IotMqtt_MallocSubscription Iot_DefaultMalloc
193 #error "No malloc function defined for IotMqtt_MallocSubscription"
197 #ifndef IotMqtt_FreeSubscription
198 #ifdef Iot_DefaultFree
199 #define IotMqtt_FreeSubscription Iot_DefaultFree
201 #error "No free function defined for IotMqtt_FreeSubscription"
212 #ifndef AWS_IOT_MQTT_ENABLE_METRICS
213 #define AWS_IOT_MQTT_ENABLE_METRICS ( 1 )
215 #ifndef IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES
216 #define IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES ( 0 )
218 #ifndef IOT_MQTT_RESPONSE_WAIT_MS
219 #define IOT_MQTT_RESPONSE_WAIT_MS ( 1000 )
221 #ifndef IOT_MQTT_RETRY_MS_CEILING
222 #define IOT_MQTT_RETRY_MS_CEILING ( 60000 )
233 #ifndef EMPTY_ELSE_MARKER
234 #define EMPTY_ELSE_MARKER
245 #define AWS_IOT_MQTT_SERVER_MAX_CLIENTID ( 128 )
246 #define AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH ( 256 )
247 #define AWS_IOT_MQTT_SERVER_MAX_TOPIC_FILTERS_PER_SUBSCRIBE ( 8 )
256 #define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
257 #define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
258 #define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
259 #define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
260 #define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
261 #define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
262 #define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xa2U )
263 #define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xb0U )
264 #define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xc0U )
265 #define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xd0U )
266 #define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xe0U )
273 #define MQTT_REMAINING_LENGTH_INVALID ( ( size_t ) 268435456 )
283 #define MQTT_INTERNAL_FLAG_BLOCK_ON_SEND ( 0x80000000 )
293 struct _mqttConnection;
302 typedef struct _mqttOperation
350 uint32_t nextPeriodMs;
367 typedef struct _mqttConnection
375 #if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
394 typedef struct _mqttSubscription
428 typedef struct _mqttPacket
539 uint8_t ** pConnectPacket,
540 size_t * pPacketSize );
569 uint8_t ** pPublishPacket,
570 size_t * pPacketSize,
571 uint16_t * pPacketIdentifier,
572 uint8_t ** pPacketIdentifierHigh );
587 uint8_t * pPacketIdentifierHigh,
588 uint16_t * pNewPacketIdentifier );
614 uint8_t ** pPubackPacket,
615 size_t * pPacketSize );
642 size_t subscriptionCount,
643 uint8_t ** pSubscribePacket,
644 size_t * pPacketSize,
645 uint16_t * pPacketIdentifier );
672 size_t subscriptionCount,
673 uint8_t ** pUnsubscribePacket,
674 size_t * pPacketSize,
675 uint16_t * pPacketIdentifier );
699 size_t * pPacketSize );
723 size_t * pPacketSize );
846 const uint16_t * pPacketIdentifier );
873 uint16_t subscribePacketIdentifier,
875 size_t subscriptionCount );
899 uint16_t packetIdentifier,
912 size_t subscriptionCount );
948 uint8_t * pIncomingByte );
void _IotMqtt_ProcessKeepAlive(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pKeepAliveJob, void *pContext)
Task pool routine for processing an MQTT connection's keep-alive.
Definition: iot_mqtt_operation.c:672
IotMqttError_t _IotMqtt_DeserializePublish(_mqttPacket_t *pPublish)
Deserialize a PUBLISH packet received from the server.
Definition: iot_mqtt_serialize.c:1191
size_t order
Order in the packet's list of subscriptions.
Definition: iot_mqtt_internal.h:413
void _IotMqtt_DecrementConnectionReferences(_mqttConnection_t *pMqttConnection)
Decrement the reference count of an MQTT connection.
Definition: iot_mqtt_api.c:864
void(* IotTaskPoolRoutine_t)(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext)
int32_t jobReference
Tracks if a job is using this operation. Must always be 0, 1, or 2.
Definition: iot_mqtt_internal.h:319
uint32_t keepAliveMs
Keep-alive interval in milliseconds. Its max value (per spec) is 65,535,000.
Definition: iot_mqtt_internal.h:349
void IotMqtt_FreeOperation(void *ptr)
Free an _mqttOperation_t. This function should have the same signature as free.
IotMqttError_t _IotMqtt_DeserializePingresp(_mqttPacket_t *pPingresp)
Deserialize a PINGRESP packet.
Definition: iot_mqtt_serialize.c:1851
void _IotMqtt_RemoveSubscriptionByPacket(_mqttConnection_t *pMqttConnection, uint16_t packetIdentifier, int32_t order)
Remove a single subscription from the subscription manager by packetIdentifier and order.
Definition: iot_mqtt_subscription.c:503
uint8_t * pPacketIdentifierHigh
The location of the high byte of the packet identifier in the MQTT packet.
Definition: iot_mqtt_internal.h:326
bool _IotMqtt_ValidatePublish(bool awsIotMqttMode, const IotMqttPublishInfo_t *pPublishInfo)
Check that an IotMqttPublishInfo_t is valid.
Definition: iot_mqtt_validate.c:153
IotMqttCallbackInfo_t disconnectCallback
A function to invoke when this connection is disconnected.
Definition: iot_mqtt_internal.h:373
bool incomingPublish
Set to true if this operation an incoming PUBLISH.
Definition: iot_mqtt_internal.h:307
void _IotMqtt_CloseNetworkConnection(IotMqttDisconnectReason_t disconnectReason, _mqttConnection_t *pMqttConnection)
Closes the network connection associated with an MQTT connection.
Definition: iot_mqtt_network.c:800
_mqttOperation_t pingreq
Operation used for MQTT keep-alive.
Definition: iot_mqtt_internal.h:388
uint8_t * pMqttPacket
The MQTT packet to send over the network.
Definition: iot_mqtt_internal.h:325
IotMqttError_t _IotMqtt_SerializePuback(uint16_t packetIdentifier, uint8_t **pPubackPacket, size_t *pPacketSize)
Generate a PUBACK packet for the given packet identifier.
Definition: iot_mqtt_serialize.c:1392
IotLink_t link
List link member.
Definition: iot_mqtt_internal.h:305
IotListDouble_t subscriptionList
Holds subscriptions associated with this connection.
Definition: iot_mqtt_internal.h:385
_mqttOperation_t * pIncomingPublish
(Output) Operation representing an incoming PUBLISH. Only used when deserializing PUBLISHes.
Definition: iot_mqtt_internal.h:442
IotTaskPoolJob_t job
Task pool job associated with this operation.
Definition: iot_mqtt_internal.h:311
Represents a subscription stored in an MQTT connection.
Definition: iot_mqtt_internal.h:394
IotMqttError_t
Return codes of MQTT functions.
Definition: iot_mqtt_types.h:103
IotMqttError_t _IotMqtt_SerializeConnect(const IotMqttConnectInfo_t *pConnectInfo, uint8_t **pConnectPacket, size_t *pPacketSize)
Generate a CONNECT packet from the given parameters.
Definition: iot_mqtt_serialize.c:668
Information on a user-provided MQTT callback function.
Definition: iot_mqtt_types.h:516
_mqttOperation_t * _IotMqtt_FindOperation(_mqttConnection_t *pMqttConnection, IotMqttOperationType_t type, const uint16_t *pPacketIdentifier)
Search a list of MQTT operations pending responses using an operation name and packet identifier....
Definition: iot_mqtt_operation.c:1132
uint16_t identifier
Packet identifier.
Definition: iot_mqtt_internal.h:412
IotMqttError_t _IotMqtt_DeserializePuback(_mqttPacket_t *pPuback)
Deserialize a PUBACK packet.
Definition: iot_mqtt_serialize.c:1428
uint16_t topicFilterLength
Length of _mqttSubscription_t.pTopicFilter.
Definition: iot_mqtt_internal.h:418
bool _IotMqtt_ValidateSubscriptionList(IotMqttOperationType_t operation, bool awsIotMqttMode, const IotMqttSubscription_t *pListStart, size_t listSize)
Check that a list of IotMqttSubscription_t is valid.
Definition: iot_mqtt_validate.c:319
struct _taskPool * IotTaskPool_t
void _IotMqtt_InvokeSubscriptionCallback(_mqttConnection_t *pMqttConnection, IotMqttCallbackParam_t *pCallbackParam)
Process a received PUBLISH from the server, invoking any subscription callbacks that have a matching ...
Definition: iot_mqtt_subscription.c:396
IotSemaphore_t waitSemaphore
Semaphore to be used with IotMqtt_Wait.
Definition: iot_mqtt_internal.h:332
IotMqttCallbackInfo_t callback
User-provided callback function and parameter.
Definition: iot_mqtt_internal.h:333
struct _mqttOperation * IotMqttOperation_t
Opaque handle that references an in-progress MQTT operation.
Definition: iot_mqtt_types.h:88
IotMqttError_t _IotMqtt_ScheduleOperation(_mqttOperation_t *pOperation, IotTaskPoolRoutine_t jobRoutine, uint32_t delay)
Schedule an operation for immediate processing.
Definition: iot_mqtt_operation.c:1084
uint8_t _IotMqtt_GetPacketType(void *pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the MQTT packet type from a stream of bytes off the network.
Definition: iot_mqtt_serialize.c:596
Internal structure representing a single MQTT operation, such as CONNECT, SUBSCRIBE,...
Definition: iot_mqtt_internal.h:302
size_t _IotMqtt_GetRemainingLength(void *pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the remaining length from a stream of bytes off the network.
Definition: iot_mqtt_serialize.c:611
IotMqttOperationType_t type
What operation this structure represents.
Definition: iot_mqtt_internal.h:320
IotMqttDisconnectReason_t
The reason that an MQTT connection (and its associated network connection) was disconnected.
Definition: iot_mqtt_types.h:320
void _IotMqtt_ProcessSend(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pSendJob, void *pContext)
Task pool routine for processing an MQTT operation to send.
Definition: iot_mqtt_operation.c:863
uint16_t packetIdentifier
(Output) MQTT packet identifier.
Definition: iot_mqtt_internal.h:447
IotListDouble_t pendingResponse
List of processed operations awaiting a server response.
Definition: iot_mqtt_internal.h:383
User-facing functions of the MQTT 3.1.1 library.
IotMqttError_t _IotMqtt_DeserializeSuback(_mqttPacket_t *pSuback)
Deserialize a SUBACK packet.
Definition: iot_mqtt_serialize.c:1579
void IotMqtt_FreeSubscription(void *ptr)
Free an _mqttSubscription_t. This function should have the same signature as free.
Represents an MQTT connection.
Definition: iot_mqtt_internal.h:367
const IotMqttSerializer_t * pSerializer
MQTT packet serializer overrides.
Definition: iot_mqtt_internal.h:376
uint8_t * pRemainingData
(Input) The remaining data in MQTT packet.
Definition: iot_mqtt_internal.h:445
struct _mqttConnection * pMqttConnection
MQTT connection associated with this operation.
Definition: iot_mqtt_internal.h:308
const void * pReceivedData
Any buffer associated with this PUBLISH that should be freed.
Definition: iot_mqtt_internal.h:359
Function pointers for MQTT packet serializer overrides.
Definition: iot_mqtt_types.h:715
uint32_t failure
Flag tracking keep-alive status.
Definition: iot_mqtt_internal.h:348
bool ownNetworkConnection
Whether this MQTT connection owns its network connection.
Definition: iot_mqtt_internal.h:370
void _IotMqtt_ProcessIncomingPublish(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pPublishJob, void *pContext)
Task pool routine for processing an incoming PUBLISH message.
Definition: iot_mqtt_operation.c:817
IotMqttError_t _IotMqtt_SerializeSubscribe(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pSubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
Generate a SUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_serialize.c:1485
uint32_t nextPeriodMs
Next retry period.
Definition: iot_mqtt_internal.h:343
bool _IotMqtt_DecrementOperationReferences(_mqttOperation_t *pOperation, bool cancelJob)
Decrement the job reference count of an MQTT operation and optionally cancel its job.
Definition: iot_mqtt_operation.c:495
IotMqttError_t _IotMqtt_DeserializeConnack(_mqttPacket_t *pConnack)
Deserialize a CONNACK packet.
Definition: iot_mqtt_serialize.c:887
uint8_t type
(Input) A value identifying the packet type.
Definition: iot_mqtt_internal.h:448
void _IotMqtt_PublishSetDup(uint8_t *pPublishPacket, uint8_t *pPacketIdentifierHigh, uint16_t *pNewPacketIdentifier)
Set the DUP bit in a QoS 1 PUBLISH packet.
Definition: iot_mqtt_serialize.c:1156
IotMqttError_t _IotMqtt_DeserializeUnsuback(_mqttPacket_t *pUnsuback)
Deserialize a UNSUBACK packet.
Definition: iot_mqtt_serialize.c:1772
bool _IotMqtt_ValidateConnect(const IotMqttConnectInfo_t *pConnectInfo)
Check that an IotMqttConnectInfo_t is valid.
Definition: iot_mqtt_validate.c:38
bool disconnected
Tracks if this connection has been disconnected.
Definition: iot_mqtt_internal.h:379
bool unsubscribed
Tracks whether an unsubscribe function has been called for this subscription.
Definition: iot_mqtt_internal.h:408
int32_t references
Counts callbacks and operations using this connection.
Definition: iot_mqtt_internal.h:381
_mqttConnection_t * pMqttConnection
(Input) MQTT connection associated with this packet. Only used when deserializing SUBACKs.
Definition: iot_mqtt_internal.h:436
Represents an MQTT packet received from the network.
Definition: iot_mqtt_internal.h:428
IotMqttOperationType_t
Types of MQTT operations.
Definition: iot_mqtt_types.h:277
void _IotMqtt_FreePacket(uint8_t *pPacket)
Free a packet generated by the serializer.
Definition: iot_mqtt_serialize.c:1912
IotListDouble_t pendingProcessing
List of operations waiting to be processed by a task pool routine.
Definition: iot_mqtt_internal.h:382
void * IotMqtt_MallocOperation(size_t size)
Allocate an _mqttOperation_t. This function should have the same signature as malloc.
IotMqttError_t status
Result of this operation. This is reported once a response is received.
Definition: iot_mqtt_internal.h:335
Information on an MQTT subscription.
Definition: iot_mqtt_types.h:550
void * IotMqtt_MallocConnection(size_t size)
Allocate an _mqttConnection_t. This function should have the same signature as malloc.
int32_t references
How many subscription callbacks are using this subscription.
Definition: iot_mqtt_internal.h:398
void _IotMqtt_DestroyOperation(_mqttOperation_t *pOperation)
Free resources used to record an MQTT operation. This is called when the operation completes.
Definition: iot_mqtt_operation.c:566
IotMqttPublishInfo_t publishInfo
Deserialized PUBLISH.
Definition: iot_mqtt_internal.h:358
bool _IotMqtt_ValidateOperation(IotMqttOperation_t operation)
Check that an IotMqttOperation_t is valid and waitable.
Definition: iot_mqtt_validate.c:286
uint32_t flags
Flags passed to the function that created this operation.
Definition: iot_mqtt_internal.h:321
bool awsIotMqttMode
Specifies if this connection is to an AWS IoT MQTT server.
Definition: iot_mqtt_internal.h:369
IotMutex_t subscriptionMutex
Grants exclusive access to the subscription list.
Definition: iot_mqtt_internal.h:386
IotTaskPoolJobStorage_t jobStorage
Task pool job storage associated with this operation.
Definition: iot_mqtt_internal.h:310
IotMqttCallbackInfo_t callback
Callback information for this subscription.
Definition: iot_mqtt_internal.h:416
const IotNetworkInterface_t * pNetworkInterface
Network interface provided to IotMqtt_Connect.
Definition: iot_mqtt_internal.h:372
void _IotMqtt_RemoveSubscriptionByTopicFilter(_mqttConnection_t *pMqttConnection, const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount)
Remove an array of subscriptions from the subscription manager by topic filter.
Definition: iot_mqtt_subscription.c:524
IotMqttError_t _IotMqtt_SerializeUnsubscribe(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pUnsubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
Generate an UNSUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_serialize.c:1682
Information on a PUBLISH message.
Definition: iot_mqtt_types.h:397
IotMqttError_t _IotMqtt_SerializeDisconnect(uint8_t **pDisconnectPacket, size_t *pPacketSize)
Generate a DISCONNECT packet.
Definition: iot_mqtt_serialize.c:1890
uint16_t packetIdentifier
The packet identifier used with this operation.
Definition: iot_mqtt_internal.h:322
Parameter to an MQTT callback function.
Definition: iot_mqtt_types.h:443
void _IotMqtt_ProcessCompletedOperation(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pOperationJob, void *pContext)
Task pool routine for processing a completed MQTT operation.
Definition: iot_mqtt_operation.c:1045
uint32_t limit
Maximum number of retries allowed.
Definition: iot_mqtt_internal.h:342
IotMqttError_t _IotMqtt_CreateOperation(_mqttConnection_t *pMqttConnection, uint32_t flags, const IotMqttCallbackInfo_t *pCallbackInfo, _mqttOperation_t **pNewOperation)
Create a record for a new in-progress MQTT operation.
Definition: iot_mqtt_operation.c:348
IotMutex_t referencesMutex
Recursive mutex. Grants access to connection state and operation lists.
Definition: iot_mqtt_internal.h:380
IotMqttError_t _IotMqtt_SerializePublish(const IotMqttPublishInfo_t *pPublishInfo, uint8_t **pPublishPacket, size_t *pPacketSize, uint16_t *pPacketIdentifier, uint8_t **pPacketIdentifierHigh)
Generate a PUBLISH packet from the given parameters.
Definition: iot_mqtt_serialize.c:1018
IotLink_t link
List link member.
Definition: iot_mqtt_internal.h:396
IotMqttError_t _IotMqtt_SerializePingreq(uint8_t **pPingreqPacket, size_t *pPacketSize)
Generate a PINGREQ packet.
Definition: iot_mqtt_serialize.c:1829
void * pNetworkConnection
References the transport-layer network connection.
Definition: iot_mqtt_internal.h:371
void * IotMqtt_MallocSubscription(size_t size)
Allocate an _mqttSubscription_t. This function should have the same signature as malloc.
size_t remainingLength
(Input) Length of the remaining data in the MQTT packet.
Definition: iot_mqtt_internal.h:446
bool _IotMqtt_GetNextByte(void *pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface, uint8_t *pIncomingByte)
Read the next available byte on a network connection.
Definition: iot_mqtt_network.c:770
bool _IotMqtt_IncrementConnectionReferences(_mqttConnection_t *pMqttConnection)
Attempt to increment the reference count of an MQTT connection.
Definition: iot_mqtt_api.c:830
struct _taskPoolJob * IotTaskPoolJob_t
size_t packetSize
Size of pMqttPacket.
Definition: iot_mqtt_internal.h:327
IotMqttError_t _IotMqtt_AddSubscriptions(_mqttConnection_t *pMqttConnection, uint16_t subscribePacketIdentifier, const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount)
Add an array of subscriptions to the subscription manager.
Definition: iot_mqtt_subscription.c:309
uint32_t count
Current number of retries.
Definition: iot_mqtt_internal.h:341
void _IotMqtt_Notify(_mqttOperation_t *pOperation)
Notify of a completed MQTT operation.
Definition: iot_mqtt_operation.c:1245
Information on a new MQTT connection.
Definition: iot_mqtt_types.h:585
void IotMqtt_FreeConnection(void *ptr)
Free an _mqttConnection_t. This function should have the same signature as free.