FreeRTOS
aws_mqtt_lib.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS
3  * Copyright (C) 2017 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  * http://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
26 
32 #ifndef _AWS_MQTT_LIB_H_
33 #define _AWS_MQTT_LIB_H_
34 
35 /* This ifndef enables the core MQTT library to be used without
36  * providing MQTTConfig.h. All the config values in this case are
37  * taken from MQTTConfigDefaults.h. */
38 #ifndef mqttDO_NOT_USE_CUSTOM_CONFIG
39  #include "aws_mqtt_config.h"
40 #endif
41 #include "aws_mqtt_config_defaults.h"
42 
43 /* MQTT buffer includes. */
44 #include "aws_mqtt_buffer.h"
45 
56 #define mqttREMAINING_LENGTH_MAX_BYTES 4
57 #define mqttREMAINING_LENGTH_MIN_BYTES 1
58 #define mqttFIXED_HEADER_MAX_SIZE ( 1 + mqttREMAINING_LENGTH_MAX_BYTES )
59 #define mqttFIXED_HEADER_MIN_SIZE ( 1 + mqttREMAINING_LENGTH_MIN_BYTES )
60 
65 typedef enum
66 {
67  eMQTTFalse = 0,
68  eMQTTTrue = 1
69 } MQTTBool_t;
70 
74 typedef enum
75 {
86 
93 typedef enum
94 {
99 
106 typedef enum
107 {
111 
119 typedef enum
120 {
135 
142 typedef enum
143 {
149 
153 typedef enum
154 {
155  eMQTTQoS0 = 0,
156  eMQTTQoS1 = 1,
158 } MQTTQoS_t;
159 
163 typedef enum
164 {
169 
176 typedef enum
177 {
185 
192 typedef enum
193 {
199 
203 typedef enum
204 {
209 
214 typedef struct MQTTConnACKData
215 {
219 
224 typedef struct MQTTSubACKData
225 {
229 
234 typedef struct MQTTUnSubACKData
235 {
238 
243 typedef struct MQTTPubACKData
244 {
247 
252 typedef struct MQTTPublishData
253 {
255  const uint8_t * pucTopic;
256  uint16_t usTopicLength;
257  const void * pvData;
258  uint32_t ulDataLength;
259  MQTTBufferHandle_t xBuffer;
261 
266 typedef struct MQTTTimeoutData
267 {
270 
275 typedef struct MQTTDisconnectData
276 {
279 
283 typedef struct MQTTCallbackParams
284 {
287  /* Only one of the following is meaningful
288  * based on the value of xEventType. */
289  union
290  {
298  } u;
300 
322 typedef MQTTBool_t ( * MQTTEventCallback_t ) ( void * pvCallbackContext,
323  const MQTTEventCallbackParams_t * const pxParams );
324 
349 #if ( mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT == 1 )
350 
351  typedef MQTTBool_t ( * MQTTPublishCallback_t ) ( void * pvPublishCallbackContext,
352  const MQTTPublishData_t * const pxPublishData );
353 
354 #endif /* mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT */
355 
368 typedef uint32_t ( * MQTTSend_t ) ( void * pvSendContext,
369  const uint8_t * const pucData,
370  uint32_t ulDataLength );
371 
381 typedef void ( * MQTTGetTicks_t ) ( uint64_t * pxCurrentTickCount );
382 
399 typedef uint8_t * ( * MQTTGetFreeBuffer_t ) ( uint32_t * pulBufferLength );
400 
411 typedef void ( * MQTTReturnBuffer_t ) ( uint8_t * pucBuffer );
412 
416 #if ( mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT == 1 )
417 
418  typedef struct MQTTSubscription
419  {
420  uint8_t ucTopicFilter[ mqttconfigSUBSCRIPTION_MANAGER_MAX_TOPIC_LENGTH ];
421  uint16_t usTopicFilterLength;
422  void * pvPublishCallbackContext;
423  MQTTPublishCallback_t pxPublishCallback;
424  MQTTBool_t xInUse;
425  MQTTTopicFilterType_t xTopicFilterType;
426  } MQTTSubscription_t;
427 
428 #endif /* mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT */
429 
434 #if ( mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT == 1 )
435 
436  typedef struct MQTTSubscriptionManager
437  {
438  MQTTSubscription_t xSubscriptions[ mqttconfigSUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS ];
439  uint32_t ulInUseSubscriptions;
440  } MQTTSubscriptionManager_t;
441 
442 #endif /* mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT */
443 
451 {
455 
459 typedef struct MQTTRxMessageState
460 {
466 
473 typedef struct MQTTContext
474 {
476  MQTTBufferHandle_t xRxBuffer;
478  uint8_t ucRxFixedHeaderBuffer[ mqttFIXED_HEADER_MAX_SIZE ];
482  void * pvSendContext;
492  #if ( mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT == 1 )
493  MQTTSubscriptionManager_t xSubscriptionManager;
494  #endif /* mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT */
495 } MQTTContext_t;
496 
502 typedef struct MQTTInitParams
503 {
506  void * pvSendContext;
511 
517 typedef struct MQTTConnectParams
518 {
522  const uint8_t * pucClientId;
523  uint16_t usClientIdLength;
524  const uint8_t * pucUserName;
525  uint16_t usUserNameLength;
527  uint32_t ulTimeoutTicks;
529 
535 typedef struct MQTTSubscribeParams
536 {
537  const uint8_t * pucTopic;
538  uint16_t usTopicLength;
541  uint32_t ulTimeoutTicks;
542  #if ( mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT == 1 )
543  void * pvPublishCallbackContext;
544  MQTTPublishCallback_t pxPublishCallback;
548  #endif /* mqttconfigENABLE_SUBSCRIPTION_MANAGEMENT */
550 
556 typedef struct MQTTUnsubscribeParams
557 {
558  const uint8_t * pucTopic;
559  uint16_t usTopicLength;
561  uint32_t ulTimeoutTicks;
563 
569 typedef struct MQTTPublishParams
570 {
571  const uint8_t * pucTopic;
572  uint16_t usTopicLength;
574  const void * pvData;
575  uint32_t ulDataLength;
577  uint32_t ulTimeoutTicks;
579 
591 MQTTReturnCode_t MQTT_Init( MQTTContext_t * pxMQTTContext,
592  const MQTTInitParams_t * const pxInitParams );
593 
607  const MQTTConnectParams_t * const pxConnectParams );
608 
622 
656  const MQTTSubscribeParams_t * const pxSubscribeParams );
657 
671  const MQTTUnsubscribeParams_t * const pxUnsubscribeParams );
672 
686  const MQTTPublishParams_t * const pxPublishParams );
687 
702  const uint8_t * pucReceivedData,
703  size_t xReceivedDataLength );
704 
719  MQTTBufferHandle_t xBufferHandle );
720 
734 uint32_t MQTT_Periodic( MQTTContext_t * pxMQTTContext,
735  uint64_t xCurrentTickCount );
736 
737 #endif /* _AWS_MQTT_LIB_H_ */
const uint8_t * pucTopic
Definition: aws_mqtt_lib.h:571
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:236
MQTTReturnCode_t MQTT_Publish(MQTTContext_t *pxMQTTContext, const MQTTPublishParams_t *const pxPublishParams)
Initiates the Publish operation.
struct MQTTSubACKData MQTTSubACKData_t
The data sent by the MQTT library in the user supplied callback when a SUBACK message is received...
The data sent by the MQTT library in the user supplied callback when a PUBACK message is received...
Definition: aws_mqtt_lib.h:243
MQTTEventCallback_t pxCallback
Definition: aws_mqtt_lib.h:505
MQTT Init Parameters.
Definition: aws_mqtt_lib.h:502
struct MQTTPublishParams MQTTPublishParams_t
MQTT Publish Parameters.
MQTTEventType_t
Various events reported by the library in the user supplied callback.
Definition: aws_mqtt_lib.h:119
MQTTGetTicks_t pxGetTicksFxn
Definition: aws_mqtt_lib.h:508
Represents one connection to an MQTT broker.
Definition: aws_mqtt_lib.h:473
void(* MQTTGetTicks_t)(uint64_t *pxCurrentTickCount)
Signature of the callback to get the current tick count.
Definition: aws_mqtt_lib.h:381
MQTT Subscribe Parameters.
Definition: aws_mqtt_lib.h:535
MQTTSend_t pxMQTTSendFxn
Definition: aws_mqtt_lib.h:507
uint16_t usClientIdLength
Definition: aws_mqtt_lib.h:523
MQTTSubACKData_t xMQTTSubACKData
Definition: aws_mqtt_lib.h:292
uint32_t ulRxMessageReceivedLength
Definition: aws_mqtt_lib.h:479
struct MQTTUnsubscribeParams MQTTUnsubscribeParams_t
MQTT Unsubscribe Parameters.
MQTTReturnCode_t MQTT_ParseReceivedData(MQTTContext_t *pxMQTTContext, const uint8_t *pucReceivedData, size_t xReceivedDataLength)
Decodes the incoming messages.
MQTTUnSubACKData_t xMQTTUnSubACKData
Definition: aws_mqtt_lib.h:293
Link_t xTxBufferListHead
Definition: aws_mqtt_lib.h:475
The data sent by the MQTT library in the user supplied callback when a publish message from the broke...
Definition: aws_mqtt_lib.h:252
MQTTConnACKData_t xMQTTConnACKData
Definition: aws_mqtt_lib.h:291
MQTTReturnCode_t MQTT_Connect(MQTTContext_t *pxMQTTContext, const MQTTConnectParams_t *const pxConnectParams)
Initiates the Connect operation.
struct MQTTContext MQTTContext_t
Represents one connection to an MQTT broker.
MQTTBool_t
Boolean type.
Definition: aws_mqtt_lib.h:65
MQTTRxMessageState_t xRxMessageState
Definition: aws_mqtt_lib.h:477
uint16_t usKeepAliveIntervalSeconds
Definition: aws_mqtt_lib.h:519
uint32_t ulDataLength
Definition: aws_mqtt_lib.h:258
MQTTQoS_t
Quality of Service (QoS).
Definition: aws_mqtt_lib.h:153
uint32_t ulPingRequestTimeoutTicks
Definition: aws_mqtt_lib.h:490
MQTTRxNextByte_t xRxNextByte
Definition: aws_mqtt_lib.h:461
void * pvSendContext
Definition: aws_mqtt_lib.h:482
const uint8_t * pucClientId
Definition: aws_mqtt_lib.h:522
MQTTRxNextByte_t
The next expected byte.
Definition: aws_mqtt_lib.h:93
struct MQTTTimeoutData MQTTTimeoutData_t
The data sent by the MQTT library in the user supplied callback when an operation times out...
MQTTBufferPoolInterface_t xBufferPoolInterface
Definition: aws_mqtt_lib.h:485
The data sent by the MQTT library in the user supplied callback when a CONNACK message is received...
Definition: aws_mqtt_lib.h:214
uint32_t ulPingRequestTimeoutTicks
Definition: aws_mqtt_lib.h:521
MQTTRxMessageAction_t xRxMessageAction
Definition: aws_mqtt_lib.h:462
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:217
MQTTSend_t pxMQTTSendFxn
Definition: aws_mqtt_lib.h:483
void * pvSendContext
Definition: aws_mqtt_lib.h:506
uint32_t ulKeepAliveActualIntervalTicks
Definition: aws_mqtt_lib.h:520
MQTTBufferPoolInterface_t xBufferPoolInterface
Definition: aws_mqtt_lib.h:509
MQTTTopicFilterType_t
Represents the type of the topic filter.
Definition: aws_mqtt_lib.h:203
const uint8_t * pucTopic
Definition: aws_mqtt_lib.h:558
struct MQTTDisconnectData MQTTDisconnectData_t
The data sent by the MQTT library in the user supplied callback when the client is disconnected...
struct MQTTRxMessageState MQTTRxMessageState_t
Represents the state of the message currently being received.
struct MQTTBufferPoolInterface MQTTBufferPoolInterface_t
Represents a subscription entry in the subscription manager.
struct MQTTConnACKData MQTTConnACKData_t
The data sent by the MQTT library in the user supplied callback when a CONNACK message is received...
const void * pvData
Definition: aws_mqtt_lib.h:574
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:540
const uint8_t * pucUserName
Definition: aws_mqtt_lib.h:524
MQTTEventType_t xEventType
Definition: aws_mqtt_lib.h:285
The parameters sent by the MQTT library in the user supplied callback.
Definition: aws_mqtt_lib.h:283
MQTTPublishData_t xPublishData
Definition: aws_mqtt_lib.h:295
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:268
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:245
MQTTDisconnectReason_t
The reason of disconnect.
Definition: aws_mqtt_lib.h:142
uint32_t ulTotalMessageLength
Definition: aws_mqtt_lib.h:464
MQTTBool_t(* MQTTEventCallback_t)(void *pvCallbackContext, const MQTTEventCallbackParams_t *const pxParams)
Signature of the user supplied callback to get notified of various events.
Definition: aws_mqtt_lib.h:322
MQTTDisconnectReason_t xDisconnectReason
Definition: aws_mqtt_lib.h:277
Represents a subscription entry in the subscription manager.
Definition: aws_mqtt_lib.h:450
uint32_t MQTT_Periodic(MQTTContext_t *pxMQTTContext, uint64_t xCurrentTickCount)
Manages timeout and keep alive messages.
const uint8_t * pucTopic
Definition: aws_mqtt_lib.h:537
The data sent by the MQTT library in the user supplied callback when a SUBACK message is received...
Definition: aws_mqtt_lib.h:224
The data sent by the MQTT library in the user supplied callback when an operation times out...
Definition: aws_mqtt_lib.h:266
uint32_t(* MQTTSend_t)(void *pvSendContext, const uint8_t *const pucData, uint32_t ulDataLength)
Signature of the user supplied topic specific publish callback which gets called whenever a publish m...
Definition: aws_mqtt_lib.h:368
MQTTReturnBuffer_t pxReturnBufferFxn
Definition: aws_mqtt_lib.h:453
The data sent by the MQTT library in the user supplied callback when an UNSUBACK message is received...
Definition: aws_mqtt_lib.h:234
uint32_t ulDataLength
Definition: aws_mqtt_lib.h:575
The data sent by the MQTT library in the user supplied callback when the client is disconnected...
Definition: aws_mqtt_lib.h:275
MQTT Connect Parameters.
Definition: aws_mqtt_lib.h:517
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:227
MQTTGetFreeBuffer_t pxGetBufferFxn
Definition: aws_mqtt_lib.h:452
MQTTReturnCode_t MQTT_Init(MQTTContext_t *pxMQTTContext, const MQTTInitParams_t *const pxInitParams)
Initializes the given MQTT Context.
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:576
struct MQTTCallbackParams MQTTEventCallbackParams_t
The parameters sent by the MQTT library in the user supplied callback.
MQTTReturnCode_t MQTT_ReturnBuffer(MQTTContext_t *pxMQTTContext, MQTTBufferHandle_t xBufferHandle)
Returns the buffer provided in the publish callback.
struct MQTTSubscribeParams MQTTSubscribeParams_t
MQTT Subscribe Parameters.
MQTTBool_t xWaitingForPingResp
Definition: aws_mqtt_lib.h:491
MQTTSubACKReturnCode_t
Return codes sent by the broker in the SUBACK message.
Definition: aws_mqtt_lib.h:192
MQTT Publish Parameters.
Definition: aws_mqtt_lib.h:569
MQTTConnACKReturnCode_t
Return codes sent by the broker in the CONNACK message.
Definition: aws_mqtt_lib.h:176
MQTTDisconnectData_t xDisconnectData
Definition: aws_mqtt_lib.h:297
const void * pvData
Definition: aws_mqtt_lib.h:257
uint16_t usTopicLength
Definition: aws_mqtt_lib.h:256
uint32_t ulTimeoutTicks
Definition: aws_mqtt_lib.h:527
MQTTConnectionState_t
The connection state.
Definition: aws_mqtt_lib.h:163
uint16_t usUserNameLength
Definition: aws_mqtt_lib.h:525
MQTTBufferHandle_t xRxBuffer
Definition: aws_mqtt_lib.h:476
const uint8_t * pucTopic
Definition: aws_mqtt_lib.h:255
uint8_t ucRemaingingLengthFieldBytes
Definition: aws_mqtt_lib.h:463
MQTTGetTicks_t pxGetTicksFxn
Definition: aws_mqtt_lib.h:484
Represents the state of the message currently being received.
Definition: aws_mqtt_lib.h:459
struct MQTTPublishData MQTTPublishData_t
The data sent by the MQTT library in the user supplied callback when a publish message from the broke...
MQTTTimeoutData_t xTimeoutData
Definition: aws_mqtt_lib.h:296
MQTTConnectionState_t xConnectionState
Definition: aws_mqtt_lib.h:486
MQTTReturnCode_t MQTT_Subscribe(MQTTContext_t *pxMQTTContext, const MQTTSubscribeParams_t *const pxSubscribeParams)
Initiates the Subscribe operation.
struct MQTTPubACKData MQTTPubACKData_t
The data sent by the MQTT library in the user supplied callback when a PUBACK message is received...
MQTTPubACKData_t xMQTTPubACKData
Definition: aws_mqtt_lib.h:294
MQTT Unsubscribe Parameters.
Definition: aws_mqtt_lib.h:556
void(* MQTTReturnBuffer_t)(uint8_t *pucBuffer)
Signature of the callback supplied by the user as part of MQTTBufferPoolInterface_t to return the buf...
Definition: aws_mqtt_lib.h:411
struct MQTTConnectParams MQTTConnectParams_t
MQTT Connect Parameters.
uint16_t usPacketIdentifier
Definition: aws_mqtt_lib.h:526
struct MQTTUnSubACKData MQTTUnSubACKData_t
The data sent by the MQTT library in the user supplied callback when an UNSUBACK message is received...
MQTTReturnCode_t MQTT_Unsubscribe(MQTTContext_t *pxMQTTContext, const MQTTUnsubscribeParams_t *const pxUnsubscribeParams)
Initiates the Unsubscribe operation.
MQTTQoS_t xQos
Definition: aws_mqtt_lib.h:254
uint64_t xLastSentMessageTimestamp
Definition: aws_mqtt_lib.h:487
uint32_t ulKeepAliveActualIntervalTicks
Definition: aws_mqtt_lib.h:489
void * pvCallbackContext
Definition: aws_mqtt_lib.h:504
uint32_t ulNextPeriodicInvokeTicks
Definition: aws_mqtt_lib.h:488
MQTTReturnCode_t
Return codes from various APIs.
Definition: aws_mqtt_lib.h:74
uint8_t *(* MQTTGetFreeBuffer_t)(uint32_t *pulBufferLength)
Signature of the callback supplied by the user as part of MQTTBufferPoolInterface_t to get a free buf...
Definition: aws_mqtt_lib.h:399
MQTTRxMessageAction_t
The action taken on the message being received.
Definition: aws_mqtt_lib.h:106
uint16_t usTopicLength
Definition: aws_mqtt_lib.h:572
MQTTBufferHandle_t xBuffer
Definition: aws_mqtt_lib.h:259
struct MQTTInitParams MQTTInitParams_t
MQTT Init Parameters.
MQTTConnACKReturnCode_t xConnACKReturnCode
Definition: aws_mqtt_lib.h:216
MQTTReturnCode_t MQTT_Disconnect(MQTTContext_t *pxMQTTContext)
Disconnects the connection to the broker.
uint32_t ulTimeoutTicks
Definition: aws_mqtt_lib.h:577
void * pvCallbackContext
Definition: aws_mqtt_lib.h:480
MQTTSubACKReturnCode_t xSubACKReturnCode
Definition: aws_mqtt_lib.h:226
MQTTEventCallback_t pxCallback
Definition: aws_mqtt_lib.h:481