AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_mqtt_internal.h
Go to the documentation of this file.
1 /*
2  * IoT MQTT V2.1.0
3  * Copyright (C) 2018 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 
29 #ifndef IOT_MQTT_INTERNAL_H_
30 #define IOT_MQTT_INTERNAL_H_
31 
32 /* The config header is always included first. */
33 #include "iot_config.h"
34 
35 /* Linear containers (lists and queues) include. */
36 #include "iot_linear_containers.h"
37 
38 /* MQTT include. */
39 #include "iot_mqtt.h"
40 
41 /* Task pool include. */
42 #include "iot_taskpool.h"
43 
53 #if IOT_MQTT_ENABLE_ASSERTS == 1
54  #ifndef IotMqtt_Assert
55  #ifdef Iot_DefaultAssert
56  #define IotMqtt_Assert( expression ) Iot_DefaultAssert( expression )
57  #else
58  #error "Asserts are enabled for MQTT, but IotMqtt_Assert is not defined"
59  #endif
60  #endif
61 #else
62  #define IotMqtt_Assert( expression )
63 #endif
64 
65 /* Configure logs for MQTT functions. */
66 #ifdef IOT_LOG_LEVEL_MQTT
67  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_MQTT
68 #else
69  #ifdef IOT_LOG_LEVEL_GLOBAL
70  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
71  #else
72  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
73  #endif
74 #endif
75 
76 #define LIBRARY_LOG_NAME ( "MQTT" )
77 #include "iot_logging_setup.h"
78 
79 /*
80  * Provide default values for undefined memory allocation functions based on
81  * the usage of dynamic memory allocation.
82  */
83 #if IOT_STATIC_MEMORY_ONLY == 1
84  #include "iot_static_memory.h"
85 
91  void * IotMqtt_MallocConnection( size_t size );
92 
98  void IotMqtt_FreeConnection( void * ptr );
99 
105  #define IotMqtt_MallocMessage Iot_MallocMessageBuffer
106 
112  #define IotMqtt_FreeMessage Iot_FreeMessageBuffer
113 
119  void * IotMqtt_MallocOperation( size_t size );
120 
126  void IotMqtt_FreeOperation( void * ptr );
127 
133  void * IotMqtt_MallocSubscription( size_t size );
134 
140  void IotMqtt_FreeSubscription( void * ptr );
141 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
142  #ifndef IotMqtt_MallocConnection
143  #ifdef Iot_DefaultMalloc
144  #define IotMqtt_MallocConnection Iot_DefaultMalloc
145  #else
146  #error "No malloc function defined for IotMqtt_MallocConnection"
147  #endif
148  #endif
149 
150  #ifndef IotMqtt_FreeConnection
151  #ifdef Iot_DefaultFree
152  #define IotMqtt_FreeConnection Iot_DefaultFree
153  #else
154  #error "No free function defined for IotMqtt_FreeConnection"
155  #endif
156  #endif
157 
158  #ifndef IotMqtt_MallocMessage
159  #ifdef Iot_DefaultMalloc
160  #define IotMqtt_MallocMessage Iot_DefaultMalloc
161  #else
162  #error "No malloc function defined for IotMqtt_MallocMessage"
163  #endif
164  #endif
165 
166  #ifndef IotMqtt_FreeMessage
167  #ifdef Iot_DefaultFree
168  #define IotMqtt_FreeMessage Iot_DefaultFree
169  #else
170  #error "No free function defined for IotMqtt_FreeMessage"
171  #endif
172  #endif
173 
174  #ifndef IotMqtt_MallocOperation
175  #ifdef Iot_DefaultMalloc
176  #define IotMqtt_MallocOperation Iot_DefaultMalloc
177  #else
178  #error "No malloc function defined for IotMqtt_MallocOperation"
179  #endif
180  #endif
181 
182  #ifndef IotMqtt_FreeOperation
183  #ifdef Iot_DefaultFree
184  #define IotMqtt_FreeOperation Iot_DefaultFree
185  #else
186  #error "No free function defined for IotMqtt_FreeOperation"
187  #endif
188  #endif
189 
190  #ifndef IotMqtt_MallocSubscription
191  #ifdef Iot_DefaultMalloc
192  #define IotMqtt_MallocSubscription Iot_DefaultMalloc
193  #else
194  #error "No malloc function defined for IotMqtt_MallocSubscription"
195  #endif
196  #endif
197 
198  #ifndef IotMqtt_FreeSubscription
199  #ifdef Iot_DefaultFree
200  #define IotMqtt_FreeSubscription Iot_DefaultFree
201  #else
202  #error "No free function defined for IotMqtt_FreeSubscription"
203  #endif
204  #endif
205 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
206 
213 #ifndef AWS_IOT_MQTT_ENABLE_METRICS
214  #define AWS_IOT_MQTT_ENABLE_METRICS ( 1 )
215 #endif
216 #ifndef IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES
217  #define IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES ( 0 )
218 #endif
219 #ifndef IOT_MQTT_RESPONSE_WAIT_MS
220  #define IOT_MQTT_RESPONSE_WAIT_MS ( 1000U )
221 #endif
222 #ifndef IOT_MQTT_RETRY_MS_CEILING
223  #define IOT_MQTT_RETRY_MS_CEILING ( 60000U )
224 #endif
225 
227 #define MQTT_SERVER_MAX_CLIENTID_LENGTH ( ( uint16_t ) 23 )
228 #define MQTT_SERVER_MAX_PUBLISH_PAYLOAD_LENGTH ( ( size_t ) ( 268435456 ) )
229 #define MQTT_SERVER_MAX_LWT_PAYLOAD_LENGTH ( ( size_t ) UINT16_MAX )
231 /*
232  * Constants related to limits defined in AWS Service Limits.
233  *
234  * For details, see
235  * https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html
236  *
237  * Used to validate parameters if when connecting to an AWS IoT MQTT server.
238  */
239 #define AWS_IOT_MQTT_SERVER_MAX_CLIENTID_LENGTH ( ( uint16_t ) 128 )
240 #define AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH ( ( uint16_t ) 256 )
241 #define AWS_IOT_MQTT_SERVER_MAX_TOPIC_FILTERS_PER_SUBSCRIBE ( ( size_t ) 8 )
242 #define AWS_IOT_MQTT_SERVER_MAX_PUBLISH_PAYLOAD_LENGTH ( ( size_t ) ( 131072 ) )
244 /*
245  * MQTT control packet type and flags. Always the first byte of an MQTT
246  * packet.
247  *
248  * For details, see
249  * http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html#_Toc385349757
250  */
251 #define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
252 #define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
253 #define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
254 #define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
255 #define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
256 #define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
257 #define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xa2U )
258 #define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xb0U )
259 #define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xc0U )
260 #define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xd0U )
261 #define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xe0U )
268 #define MQTT_REMAINING_LENGTH_INVALID ( ( size_t ) 268435456 )
269 
278 #define MQTT_INTERNAL_FLAG_BLOCK_ON_SEND ( 0x80000000U )
279 
289 #define MQTT_REMOVE_ALL_SUBSCRIPTIONS ( -1 )
290 
291 /*---------------------- MQTT internal data structures ----------------------*/
292 
299 struct _mqttConnection;
308 typedef struct _mqttOperation
309 {
310  /* Pointers to neighboring queue elements. */
319  /* MISRA rule 19.2 doesn't allow usage of union
320  * but it is intentionally used here to reduce the size of struct. */
321  /* coverity[misra_c_2012_rule_19_2_violation] */
322  union
323  {
324  /* If incomingPublish is false, this struct is valid. */
325  struct
326  {
327  /* Basic operation information. */
328  int32_t jobReference;
330  uint32_t flags;
331  uint16_t packetIdentifier;
333  /* Serialized packet and size. */
334  uint8_t * pMqttPacket;
336  size_t packetSize;
338  /* How to notify of an operation's completion. */
339 
340  /* MISRA rule 19.2 doesn't allow usage of union
341  * but it is intentionally used here to reduce the size of struct. */
342  /* coverity[misra_c_2012_rule_19_2_violation] */
343  union
344  {
347  } notify;
350  /* MISRA rule 19.2 doesn't allow usage of union
351  * but it is intentionally used here to reduce the size of struct. */
352  /* coverity[misra_c_2012_rule_19_2_violation] */
353  union
354  {
355  struct
356  {
357  uint32_t count;
358  uint32_t limit;
359  uint32_t nextPeriodMs;
360  } retry;
362  struct
363  {
364  uint32_t failure;
365  uint32_t keepAliveMs;
366  uint32_t nextPeriodMs;
367  } ping;
368  } periodic;
369  } operation;
370 
371  /* If incomingPublish is true, this struct is valid. */
372  struct
373  {
375  void * pReceivedData;
376  } publish;
377  } u;
379 
383 typedef struct _mqttConnection
384 {
395  int32_t references;
402  uint64_t lastMessageTime;
405 
409 typedef struct _mqttSubscription
410 {
413  int32_t references;
424 
425  struct
426  {
427  uint16_t identifier;
428  size_t order;
429  } packetInfo;
433  uint16_t topicFilterLength;
435  /* A flexible length array is used here so that the topic filter may
436  * be of an arbitrary length. */
437  /* coverity[misra_c_2012_rule_18_7_violation] */
438  char pTopicFilter[];
440 
447 typedef struct _mqttPacket
448 {
449  /* MISRA rule 19.2 doesn't allow usage of union
450  * but it is intentionally used here to reduce the size of struct. */
451  /* coverity[misra_c_2012_rule_19_2_violation] */
452  union
453  {
459 
465  } u;
467  uint8_t * pRemainingData;
469  uint16_t packetIdentifier;
470  uint8_t type;
471 } _mqttPacket_t;
472 
473 /*-------------------- MQTT struct validation functions ---------------------*/
474 
482 bool _IotMqtt_ValidateConnect( const IotMqttConnectInfo_t * pConnectInfo );
483 
496 bool _IotMqtt_ValidatePublish( bool awsIotMqttMode,
497  const IotMqttPublishInfo_t * pPublishInfo,
498  uint32_t flags,
499  const IotMqttCallbackInfo_t * pCallbackInfo,
500  const IotMqttOperation_t * const pPublishOperation );
501 
511 bool _IotMqtt_ValidateLwtPublish( bool awsIotMqttMode,
512  const IotMqttPublishInfo_t * pLwtPublishInfo );
513 
522 
536  bool awsIotMqttMode,
537  const IotMqttSubscription_t * pListStart,
538  size_t listSize );
539 
540 /*-------------------- MQTT packet serializer functions ---------------------*/
541 
554 uint8_t _IotMqtt_GetPacketType( IotNetworkConnection_t pNetworkConnection,
555  const IotNetworkInterface_t * pNetworkInterface );
556 
566 size_t _IotMqtt_GetRemainingLength( IotNetworkConnection_t pNetworkConnection,
567  const IotNetworkInterface_t * pNetworkInterface );
578  uint8_t ** pConnectPacket,
579  size_t * pPacketSize );
580 
594 
608  uint8_t ** pPublishPacket,
609  size_t * pPacketSize,
610  uint16_t * pPacketIdentifier,
611  uint8_t ** pPacketIdentifierHigh );
612 
625 void _IotMqtt_PublishSetDup( uint8_t * pPublishPacket,
626  uint8_t * pPacketIdentifierHigh,
627  uint16_t * pNewPacketIdentifier );
628 
642 
652 IotMqttError_t _IotMqtt_SerializePuback( uint16_t packetIdentifier,
653  uint8_t ** pPubackPacket,
654  size_t * pPacketSize );
655 
668 
681  size_t subscriptionCount,
682  uint8_t ** pSubscribePacket,
683  size_t * pPacketSize,
684  uint16_t * pPacketIdentifier );
685 
698 
711  size_t subscriptionCount,
712  uint8_t ** pUnsubscribePacket,
713  size_t * pPacketSize,
714  uint16_t * pPacketIdentifier );
715 
728 
737 IotMqttError_t _IotMqtt_SerializePingreq( uint8_t ** pPingreqPacket,
738  size_t * pPacketSize );
739 
752 
761 IotMqttError_t _IotMqtt_SerializeDisconnect( uint8_t ** pDisconnectPacket,
762  size_t * pPacketSize );
763 
769 void _IotMqtt_FreePacket( uint8_t * pPacket );
770 
771 /*-------------------- MQTT operation record functions ----------------------*/
772 
784  uint32_t flags,
785  const IotMqttCallbackInfo_t * pCallbackInfo,
786  _mqttOperation_t ** pNewOperation );
787 
800  bool cancelJob );
801 
808 void _IotMqtt_DestroyOperation( _mqttOperation_t * pOperation );
809 
818  IotTaskPoolJob_t pKeepAliveJob,
819  void * pContext );
820 
830  IotTaskPoolJob_t pPublishJob,
831  void * pContext );
832 
841 void _IotMqtt_ProcessSend( IotTaskPool_t pTaskPool,
842  IotTaskPoolJob_t pSendJob,
843  void * pContext );
844 
854  IotTaskPoolJob_t pOperationJob,
855  void * pContext );
856 
870  IotTaskPoolRoutine_t jobRoutine,
871  uint32_t delay );
872 
885  const uint16_t * pPacketIdentifier );
886 
896 void _IotMqtt_Notify( _mqttOperation_t * pOperation );
897 
898 /*----------------- MQTT subscription management functions ------------------*/
899 
912  uint16_t subscribePacketIdentifier,
913  const IotMqttSubscription_t * pSubscriptionList,
914  size_t subscriptionCount );
915 
925  IotMqttCallbackParam_t * pCallbackParam );
926 
938  uint16_t packetIdentifier,
939  int32_t order );
940 
950  const IotMqttSubscription_t * pSubscriptionList,
951  size_t subscriptionCount );
952 
953 /*------------------ MQTT connection management functions -------------------*/
954 
964 
973 
985 bool _IotMqtt_GetNextByte( IotNetworkConnection_t pNetworkConnection,
986  const IotNetworkInterface_t * pNetworkInterface,
987  uint8_t * pIncomingByte );
988 
1001  _mqttConnection_t * pMqttConnection );
1002 
1003 #if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
1004 
1008  #define SERIALIZER_OVERRIDE_SELECTOR( funcType_t, funcName, defaultFunc, serializerMember ) \
1009  static funcType_t funcName( const IotMqttSerializer_t * pSerializer ); \
1010  static funcType_t funcName( const IotMqttSerializer_t * pSerializer ) \
1011  { \
1012  funcType_t returnValue = defaultFunc; \
1013  if( pSerializer != NULL ) \
1014  { \
1015  if( pSerializer->serializerMember != NULL ) \
1016  { \
1017  returnValue = pSerializer->serializerMember; \
1018  } \
1019  } \
1020  \
1021  return returnValue; \
1022  }
1023 #endif /* if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1 */
1024 
1025 #endif /* ifndef IOT_MQTT_INTERNAL_H_ */
IotMqttError_t _IotMqtt_DeserializePingresp(_mqttPacket_t *pPingresp)
Deserialize a PINGRESP packet.
Definition: iot_mqtt_serialize.c:963
IotMqttCallbackInfo_t disconnectCallback
A function to invoke when this connection is disconnected.
Definition: iot_mqtt_internal.h:389
IotMqttError_t
Return codes of MQTT functions.
Definition: iot_mqtt_types.h:106
void IotMqtt_FreeOperation(void *ptr)
Free an _mqttOperation_t. This function should have the same signature as free.
Definition: iot_mqtt_static_memory.c:157
bool awsIotMqttMode
Specifies if this connection is to an AWS IoT MQTT server.
Definition: iot_mqtt_internal.h:385
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:571
void IotMqtt_FreeConnection(void *ptr)
Free an _mqttConnection_t. This function should have the same signature as free.
Definition: iot_mqtt_static_memory.c:122
MQTT callback function and context.
Definition: iot_mqtt_types.h:518
void _IotMqtt_CloseNetworkConnection(IotMqttDisconnectReason_t disconnectReason, _mqttConnection_t *pMqttConnection)
Closes the network connection associated with an MQTT connection.
Definition: iot_mqtt_network.c:725
size_t order
Order in the packet's list of subscriptions.
Definition: iot_mqtt_internal.h:428
_mqttOperation_t pingreq
Operation used for MQTT keep-alive.
Definition: iot_mqtt_internal.h:403
_mqttConnection_t * pMqttConnection
(Input) MQTT connection associated with this packet. Only used when deserializing SUBACKs...
Definition: iot_mqtt_internal.h:458
Represents an MQTT connection.
Definition: iot_mqtt_internal.h:383
uint8_t _IotMqtt_GetPacketType(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the MQTT packet type from a stream of bytes off the network.
Definition: iot_mqtt_serialize.c:210
IotMqttError_t status
Result of this operation. This is reported once a response is received.
Definition: iot_mqtt_internal.h:348
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:630
void * pReceivedData
Any buffer associated with this PUBLISH that should be freed.
Definition: iot_mqtt_internal.h:375
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:281
uint16_t identifier
Packet identifier.
Definition: iot_mqtt_internal.h:427
IotListDouble_t subscriptionList
Holds subscriptions associated with this connection.
Definition: iot_mqtt_internal.h:399
IotTaskPoolJob_t job
Task pool job associated with this operation.
Definition: iot_mqtt_internal.h:317
_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:1193
IotMqttError_t _IotMqtt_DeserializePuback(_mqttPacket_t *pPuback)
Deserialize a PUBACK packet.
Definition: iot_mqtt_serialize.c:666
IotListDouble_t pendingResponse
List of processed operations awaiting a server response.
Definition: iot_mqtt_internal.h:397
struct _taskPoolJob * IotTaskPoolJob_t
uint32_t count
Current number of retries.
Definition: iot_mqtt_internal.h:357
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:716
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:998
uint16_t topicFilterLength
Length of _mqttSubscription_t.pTopicFilter.
Definition: iot_mqtt_internal.h:433
Function pointers for MQTT packet serializer overrides.
Definition: iot_mqtt_types.h:867
IotSemaphore_t waitSemaphore
Semaphore to be used with IotMqtt_Wait.
Definition: iot_mqtt_internal.h:345
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:471
_IotNetworkConnection_t IotNetworkConnection_t
IotMqttError_t _IotMqtt_DeserializeSuback(_mqttPacket_t *pSuback)
Deserialize a SUBACK packet.
Definition: iot_mqtt_serialize.c:776
bool _IotMqtt_ValidatePublish(bool awsIotMqttMode, const IotMqttPublishInfo_t *pPublishInfo, uint32_t flags, const IotMqttCallbackInfo_t *pCallbackInfo, const IotMqttOperation_t *const pPublishOperation)
Check that parameters for an MQTT PUBLISH are valid.
Definition: iot_mqtt_validate.c:614
IotMqttError_t _IotMqtt_ScheduleOperation(_mqttOperation_t *pOperation, IotTaskPoolRoutine_t jobRoutine, uint32_t delay)
Schedule an operation for immediate processing.
Definition: iot_mqtt_operation.c:1144
IotMqttPublishInfo_t publishInfo
Deserialized PUBLISH.
Definition: iot_mqtt_internal.h:374
uint32_t limit
Maximum number of retries allowed.
Definition: iot_mqtt_internal.h:358
uint32_t nextPeriodMs
Next retry period.
Definition: iot_mqtt_internal.h:359
struct _mqttConnection * pMqttConnection
MQTT connection associated with this operation.
Definition: iot_mqtt_internal.h:314
Information on a PUBLISH message.
Definition: iot_mqtt_types.h:395
Represents an MQTT packet received from the network.
Definition: iot_mqtt_internal.h:447
bool incomingPublish
Set to true if this operation is an incoming PUBLISH.
Definition: iot_mqtt_internal.h:313
size_t remainingLength
(Input) Length of the remaining data in the MQTT packet.
Definition: iot_mqtt_internal.h:468
bool _IotMqtt_ValidateLwtPublish(bool awsIotMqttMode, const IotMqttPublishInfo_t *pLwtPublishInfo)
Check that an IotMqttPublishInfo_t is valid for an LWT publish.
Definition: iot_mqtt_validate.c:678
static IotMqttConnection_t _mqttConnection
An MQTT connection to share among the tests.
Definition: iot_tests_mqtt_system.c:194
bool disconnected
Tracks if this connection has been disconnected.
Definition: iot_mqtt_internal.h:393
void IotMqtt_FreeSubscription(void *ptr)
Free an _mqttSubscription_t. This function should have the same signature as free.
Definition: iot_mqtt_static_memory.c:191
IotMqttError_t _IotMqtt_DeserializeConnack(_mqttPacket_t *pConnack)
Deserialize a CONNACK packet.
Definition: iot_mqtt_serialize.c:331
User-facing functions of the MQTT 3.1.1 library.
uint8_t * pMqttPacket
The MQTT packet to send over the network.
Definition: iot_mqtt_internal.h:334
bool ownNetworkConnection
Whether this MQTT connection owns its network connection.
Definition: iot_mqtt_internal.h:386
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:503
MQTT subscription.
Definition: iot_mqtt_types.h:553
IotMutex_t subscriptionMutex
Grants exclusive access to the subscription list.
Definition: iot_mqtt_internal.h:400
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:958
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:715
const IotMqttSerializer_t * pSerializer
MQTT packet serializer overrides.
Definition: iot_mqtt_internal.h:391
MQTT connection details.
Definition: iot_mqtt_types.h:589
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:675
IotMqttError_t _IotMqtt_DeserializeUnsuback(_mqttPacket_t *pUnsuback)
Deserialize a UNSUBACK packet.
Definition: iot_mqtt_serialize.c:886
IotMqttCallbackInfo_t callback
User-provided callback function and parameter.
Definition: iot_mqtt_internal.h:346
uint8_t * pPacketIdentifierHigh
The location of the high byte of the packet identifier in the MQTT packet.
Definition: iot_mqtt_internal.h:335
void * IotMqtt_MallocConnection(size_t size)
Allocate an _mqttConnection_t. This function should have the same signature as malloc.
Definition: iot_mqtt_static_memory.c:99
IotNetworkConnection_t pNetworkConnection
References the transport-layer network connection.
Definition: iot_mqtt_internal.h:387
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:735
_IotSystemSemaphore_t IotSemaphore_t
bool _IotMqtt_ValidateConnect(const IotMqttConnectInfo_t *pConnectInfo)
Check that an IotMqttConnectInfo_t is valid.
Definition: iot_mqtt_validate.c:567
IotListDouble_t pendingProcessing
List of operations waiting to be processed by a task pool routine.
Definition: iot_mqtt_internal.h:396
bool unsubscribed
Tracks whether an unsubscribe function has been called for this subscription.
Definition: iot_mqtt_internal.h:423
IotMutex_t referencesMutex
Recursive mutex. Grants access to connection state and operation lists.
Definition: iot_mqtt_internal.h:394
void _IotMqtt_FreePacket(uint8_t *pPacket)
Free a packet generated by the serializer.
Definition: iot_mqtt_serialize.c:1024
bool _IotMqtt_ValidateOperation(IotMqttOperation_t operation)
Check that an IotMqttOperation_t is valid and waitable.
Definition: iot_mqtt_validate.c:689
IotMqttDisconnectReason_t
The reason that an MQTT connection (and its associated network connection) was disconnected.
Definition: iot_mqtt_types.h:323
size_t packetSize
Size of pMqttPacket.
Definition: iot_mqtt_internal.h:336
uint16_t packetIdentifier
The packet identifier used with this operation.
Definition: iot_mqtt_internal.h:331
struct _taskPool * IotTaskPool_t
int32_t jobReference
Tracks if a job is using this operation. Must always be 0, 1, or 2.
Definition: iot_mqtt_internal.h:328
uint32_t keepAliveMs
Keep-alive interval in milliseconds. Its max value (per spec) is 65,535,000.
Definition: iot_mqtt_internal.h:365
void * IotMqtt_MallocOperation(size_t size)
Allocate an _mqttOperation_t. This function should have the same signature as malloc.
Definition: iot_mqtt_static_memory.c:134
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:592
_mqttOperation_t * pIncomingPublish
(Output) Operation representing an incoming PUBLISH. Only used when deserializing PUBLISHes...
Definition: iot_mqtt_internal.h:464
struct _mqttOperation * IotMqttOperation_t
Opaque handle that references an in-progress MQTT operation.
Definition: iot_mqtt_types.h:90
_IotSystemMutex_t IotMutex_t
uint32_t flags
Flags passed to the function that created this operation.
Definition: iot_mqtt_internal.h:330
IotMqttOperationType_t
Types of MQTT operations.
Definition: iot_mqtt_types.h:280
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:1107
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:580
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:445
uint32_t failure
Flag tracking keep-alive status.
Definition: iot_mqtt_internal.h:364
void * IotMqtt_MallocSubscription(size_t size)
Allocate an _mqttSubscription_t. This function should have the same signature as malloc.
Definition: iot_mqtt_static_memory.c:169
IotMqttCallbackInfo_t callback
Callback information for this subscription.
Definition: iot_mqtt_internal.h:431
const IotNetworkInterface_t * pNetworkInterface
Network interface provided to IotMqtt_Connect.
Definition: iot_mqtt_internal.h:388
uint16_t packetIdentifier
(Output) MQTT packet identifier.
Definition: iot_mqtt_internal.h:469
int32_t references
How many subscription callbacks are using this subscription.
Definition: iot_mqtt_internal.h:413
IotLink_t link
List link member.
Definition: iot_mqtt_internal.h:311
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:825
IotMqttError_t _IotMqtt_SerializePingreq(uint8_t **pPingreqPacket, size_t *pPacketSize)
Generate a PINGREQ packet.
Definition: iot_mqtt_serialize.c:936
uint64_t lastMessageTime
When the most recent message was transmitted.
Definition: iot_mqtt_internal.h:402
int32_t references
Counts callbacks and operations using this connection.
Definition: iot_mqtt_internal.h:395
bool _IotMqtt_IncrementConnectionReferences(_mqttConnection_t *pMqttConnection)
Attempt to increment the reference count of an MQTT connection.
Definition: iot_mqtt_api.c:1085
Represents a subscription stored in an MQTT connection.
Definition: iot_mqtt_internal.h:409
IotMqttOperationType_t type
What operation this structure represents.
Definition: iot_mqtt_internal.h:329
IotMqttError_t _IotMqtt_SerializeDisconnect(uint8_t **pDisconnectPacket, size_t *pPacketSize)
Generate a DISCONNECT packet.
Definition: iot_mqtt_serialize.c:997
void _IotMqtt_Notify(_mqttOperation_t *pOperation)
Notify of a completed MQTT operation.
Definition: iot_mqtt_operation.c:1299
size_t _IotMqtt_GetRemainingLength(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the remaining length from a stream of bytes off the network.
Definition: iot_mqtt_serialize.c:225
IotTaskPoolJobStorage_t jobStorage
Task pool job storage associated with this operation.
Definition: iot_mqtt_internal.h:316
Internal structure representing a single MQTT operation, such as CONNECT, SUBSCRIBE, PUBLISH, etc.
Definition: iot_mqtt_internal.h:308
IotLink_t link
List link member.
Definition: iot_mqtt_internal.h:411
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:816
IotMqttError_t _IotMqtt_DeserializePublish(_mqttPacket_t *pPublish)
Deserialize a PUBLISH packet received from the server.
Definition: iot_mqtt_serialize.c:538
void _IotMqtt_DecrementConnectionReferences(_mqttConnection_t *pMqttConnection)
Decrement the reference count of an MQTT connection.
Definition: iot_mqtt_api.c:1125
uint8_t * pRemainingData
(Input) The remaining data in MQTT packet.
Definition: iot_mqtt_internal.h:467
void(* IotTaskPoolRoutine_t)(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext)
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:386
uint8_t type
(Input) A value identifying the packet type.
Definition: iot_mqtt_internal.h:470
bool _IotMqtt_GetNextByte(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface, uint8_t *pIncomingByte)
Read the next available byte on a network connection.
Definition: iot_mqtt_network.c:695
Parameter to an MQTT callback function.
Definition: iot_mqtt_types.h:441