AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_mqtt_types.h
Go to the documentation of this file.
1 /*
2  * IoT MQTT V2.1.0
3  * Copyright (C) 2019 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 
28 #ifndef IOT_MQTT_TYPES_H_
29 #define IOT_MQTT_TYPES_H_
30 
31 /* The config header is always included first. */
32 #include "iot_config.h"
33 
34 /* Standard includes. */
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <stddef.h>
38 
39 /* Type includes. */
41 
42 /* Platform network include. */
43 #include "platform/iot_network.h"
44 
45 /*---------------------------- MQTT handle types ----------------------------*/
46 
65 typedef struct _mqttConnection * IotMqttConnection_t;
66 
87 typedef struct _mqttOperation * IotMqttOperation_t;
88 
89 /*-------------------------- MQTT enumerated types --------------------------*/
90 
102 typedef enum IotMqttError
103 {
118  IOT_MQTT_SUCCESS = 0,
119 
129 
137 
149 
159  IOT_MQTT_NO_MEMORY = 4,
160 
177 
188 
205 
216  IOT_MQTT_TIMEOUT = 8,
217 
238 
252 
268 
276 typedef enum IotMqttOperationType
277 {
302 typedef enum IotMqttQos
303 {
304  IOT_MQTT_QOS_0 = 0,
305  IOT_MQTT_QOS_1 = 1,
307 } IotMqttQos_t;
319 typedef enum IotMqttDisconnectReason
320 {
326 /*------------------------- MQTT parameter structs --------------------------*/
387 typedef struct IotMqttPublishInfo
388 {
389  IotMqttQos_t qos;
390  bool retain;
392  const char * pTopicName;
393  uint16_t topicNameLength;
395  const void * pPayload;
396  size_t payloadLength;
398  uint32_t retryMs;
399  uint32_t retryLimit;
433 typedef struct IotMqttCallbackParam
434 {
445  IotMqttConnection_t mqttConnection;
446 
447  /* MISRA rule 19.2 doesn't allow usage of union
448  * but it is intentionally used here to reduce the size of struct. */
449  /* coverity[misra_c_2012_rule_19_2_violation] */
450  union
451  {
452  /* Valid for completed operations. */
453  struct
454  {
456  IotMqttOperation_t reference;
457  IotMqttError_t result;
458  } operation;
459 
460  /* Valid for incoming PUBLISH messages. */
461  struct
462  {
463  const char * pTopicFilter;
464  uint16_t topicFilterLength;
466  } message;
467 
468  /* Valid when a connection is disconnected. */
469  IotMqttDisconnectReason_t disconnectReason;
470  } u;
509 typedef struct IotMqttCallbackInfo
510 {
511  void * pCallbackContext;
522  void ( * function )( void * pCallbackContext,
523  IotMqttCallbackParam_t * pCallbackParam );
525 
543 typedef struct IotMqttSubscription
544 {
550  IotMqttQos_t qos;
551 
552  const char * pTopicFilter;
553  uint16_t topicFilterLength;
578 typedef struct IotMqttConnectInfo
579 {
593  bool awsIotMqttMode;
594 
624  bool cleanSession;
625 
640  const IotMqttSubscription_t * pPreviousSubscriptions;
641 
652  size_t previousSubscriptionCount;
653 
667  const IotMqttPublishInfo_t * pWillInfo;
668 
669  uint16_t keepAliveSeconds;
671  const char * pClientIdentifier;
672  uint16_t clientIdentifierLength;
681  const char * pUserName;
682  uint16_t userNameLength;
683  const char * pPassword;
684  uint16_t passwordLength;
686 
701 typedef struct IotMqttPacketInfo
702 {
703  uint8_t * pRemainingData;
704  size_t remainingLength;
705  uint16_t packetIdentifier;
706  uint8_t type;
707  IotMqttPublishInfo_t pubInfo;
709 
716 struct _mqttPacket;
726 typedef uint8_t ( * IotMqttGetPacketType_t )( IotNetworkConnection_t pNetworkConnection,
727  const IotNetworkInterface_t * pNetworkInterface );
728 
736 typedef size_t ( * IotMqttGetRemainingLength_t )( IotNetworkConnection_t pNetworkConnection,
737  const IotNetworkInterface_t * pNetworkInterface );
745 typedef void ( * IotMqttFreePacket_t )( uint8_t * pPacket );
746 
753 typedef IotMqttError_t ( * IotMqttSerializeConnect_t )( const IotMqttConnectInfo_t * pConnectInfo,
754  uint8_t ** pConnectPacket,
755  size_t * pPacketSize );
756 
762 typedef IotMqttError_t ( * IotMqttSerializePingreq_t )( uint8_t ** pPingreqPacket,
763  size_t * pPacketSize );
764 
775  uint8_t ** pPublishPacket,
776  size_t * pPacketSize,
777  uint16_t * pPacketIdentifier,
778  uint8_t ** pPacketIdentifierHigh );
779 
788 typedef IotMqttError_t ( * IotMqttSerializeSubscribe_t )( const IotMqttSubscription_t * pSubscriptionList,
789  size_t subscriptionCount,
790  uint8_t ** pSubscribePacket,
791  size_t * pPacketSize,
792  uint16_t * pPacketIdentifier );
793 
799 typedef IotMqttError_t ( * IotMqttSerializeDisconnect_t )( uint8_t ** pDisconnectPacket,
800  size_t * pPacketSize );
801 
806 typedef IotMqttError_t ( * IotMqttDeserialize_t )( struct _mqttPacket * pMqttPacket );
807 
814 typedef IotMqttError_t ( * IotMqttSerializePuback_t )( uint16_t packetIdentifier,
815  uint8_t ** pPubackPacket,
816  size_t * pPacketSize );
817 
824 typedef void ( * IotMqttPublishSetDup_t )( uint8_t * pPublishPacket,
825  uint8_t * pPacketIdentifierHigh,
826  uint16_t * pNewPacketIdentifier );
827 
833 typedef IotMqttError_t (* IotMqttGetNextByte_t)( IotNetworkConnection_t pNetworkContext,
834  uint8_t * pNextByte );
835 
836 #if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
837 
855  typedef struct IotMqttSerializer
856  {
861  IotMqttGetPacketType_t getPacketType;
862 
867  IotMqttGetRemainingLength_t getRemainingLength;
868 
874  IotMqttFreePacket_t freePacket;
875 
876  struct
877  {
884 
891 
897  IotMqttPublishSetDup_t publishSetDup;
898 
905 
911  IotMqttSerializeSubscribe_t subscribe;
912 
918  IotMqttSerializeSubscribe_t unsubscribe;
919 
926 
932  IotMqttSerializeDisconnect_t disconnect;
933  } serialize;
935  struct
936  {
942  IotMqttDeserialize_t connack;
943 
949  IotMqttDeserialize_t publish;
950 
956  IotMqttDeserialize_t puback;
957 
963  IotMqttDeserialize_t suback;
964 
970  IotMqttDeserialize_t unsuback;
971 
977  IotMqttDeserialize_t pingresp;
978  } deserialize;
980 
981 #else /* if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1 */
983 /* When MQTT packet serializer overrides are disabled, this struct is an
984  * incomplete type. */
985  typedef struct IotMqttSerializer IotMqttSerializer_t;
986 
987 #endif /* if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1 */
988 
1000 typedef struct IotMqttNetworkInfo
1001 {
1017  bool createNetworkConnection;
1018 
1019  /* MISRA rule 19.2 doesn't allow usage of union
1020  * but it is intentionally used here to reduce the size of struct. */
1021  /* coverity[misra_c_2012_rule_19_2_violation] */
1022  union
1023  {
1024  struct
1025  {
1034  IotNetworkServerInfo_t pNetworkServerInfo;
1035 
1044  IotNetworkCredentials_t pNetworkCredentialInfo;
1045  } setup;
1046 
1054  IotNetworkConnection_t pNetworkConnection;
1055  } u ;
1056 
1063  const IotNetworkInterface_t * pNetworkInterface;
1064 
1068  IotMqttCallbackInfo_t disconnectCallback;
1069 
1070  #if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
1071 
1078  const IotMqttSerializer_t * pMqttSerializer;
1079  #endif
1082 /*------------------------- MQTT defined constants --------------------------*/
1083 
1128 /* @[define_mqtt_initializers] */
1130 #define IOT_MQTT_NETWORK_INFO_INITIALIZER { .createNetworkConnection = true }
1131 
1132 #define IOT_MQTT_SERIALIZER_INITIALIZER { 0 }
1133 
1134 #define IOT_MQTT_CONNECT_INFO_INITIALIZER { .cleanSession = true }
1135 
1136 #define IOT_MQTT_PUBLISH_INFO_INITIALIZER { .qos = IOT_MQTT_QOS_0 }
1137 
1138 #define IOT_MQTT_SUBSCRIPTION_INITIALIZER { .qos = IOT_MQTT_QOS_0 }
1139 
1140 #define IOT_MQTT_CALLBACK_INFO_INITIALIZER { 0 }
1141 
1142 #define IOT_MQTT_CONNECTION_INITIALIZER NULL
1143 
1144 #define IOT_MQTT_OPERATION_INITIALIZER NULL
1145 
1146 #define IOT_MQTT_PACKET_INFO_INITIALIZER { .pRemainingData = NULL, remainingLength = 0, packetIdentifier = 0, .type = 0 }
1147 /* @[define_mqtt_initializers] */
1148 
1162 #define IOT_MQTT_FLAG_WAITABLE ( 0x00000001U )
1172 #define IOT_MQTT_FLAG_CLEANUP_ONLY ( 0x00000001UL )
1173 
1174 #endif /* ifndef IOT_MQTT_TYPES_H_ */
IotMqttError_t
Return codes of MQTT functions.
Definition: iot_mqtt_types.h:106
_IotNetworkCredentials_t IotNetworkCredentials_t
MQTT callback function and context.
Definition: iot_mqtt_types.h:518
A blocking MQTT operation timed out.
Definition: iot_mqtt_types.h:220
MQTT operation completed successfully.
Definition: iot_mqtt_types.h:122
Definition: iot_mqtt_types.h:284
uint8_t(* IotMqttGetPacketType_t)(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the MQTT packet type from a stream of bytes off the network.
Definition: iot_mqtt_types.h:738
IotMqttError_t(* IotMqttSerializeSubscribe_t)(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pSubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
SUBSCRIBE/UNSUBSCRIBE packet serializer function.
Definition: iot_mqtt_types.h:800
Definition: iot_mqtt_types.h:283
Definition: iot_mqtt_types.h:325
IotMqttError_t(* IotMqtt_SerializePublish_t)(const IotMqttPublishInfo_t *pPublishInfo, uint8_t **pPublishPacket, size_t *pPacketSize, uint16_t *pPacketIdentifier, uint8_t **pPacketIdentifierHigh)
PUBLISH packet serializer function.
Definition: iot_mqtt_types.h:786
MQTT packet details.
Definition: iot_mqtt_types.h:713
MQTT operation queued, awaiting result.
Definition: iot_mqtt_types.h:132
Definition: iot_mqtt_types.h:282
Function pointers for MQTT packet serializer overrides.
Definition: iot_mqtt_types.h:867
_IotNetworkConnection_t IotNetworkConnection_t
IotMqttError_t(* IotMqttDeserialize_t)(struct _mqttPacket *pMqttPacket)
MQTT packet deserializer function.
Definition: iot_mqtt_types.h:818
void(* IotMqttPublishSetDup_t)(uint8_t *pPublishPacket, uint8_t *pPacketIdentifierHigh, uint16_t *pNewPacketIdentifier)
Set the DUP bit in a QoS 1 PUBLISH packet.
Definition: iot_mqtt_types.h:836
IotMqttError_t(* IotMqttGetNextByte_t)(IotNetworkConnection_t pNetworkContext, uint8_t *pNextByte)
Function pointer to read the next available byte on a network connection.
Definition: iot_mqtt_types.h:845
IotMqttQos_t
Quality of service levels for MQTT PUBLISH messages.
Definition: iot_mqtt_types.h:306
Information on a PUBLISH message.
Definition: iot_mqtt_types.h:395
Definition: iot_mqtt_types.h:327
An API function was called before IotMqtt_Init.
Definition: iot_mqtt_types.h:270
void(* IotMqttFreePacket_t)(uint8_t *pPacket)
Free a packet generated by the serializer.
Definition: iot_mqtt_types.h:757
struct _mqttConnection * IotMqttConnection_t
Opaque handle of an MQTT connection.
Definition: iot_mqtt_types.h:67
Definition: iot_mqtt_types.h:288
MQTT operation failed because of memory allocation failure.
Definition: iot_mqtt_types.h:163
static IotMqttConnection_t _mqttConnection
An MQTT connection to share among the tests.
Definition: iot_tests_mqtt_system.c:194
MQTT subscription.
Definition: iot_mqtt_types.h:553
At least one parameter is invalid.
Definition: iot_mqtt_types.h:152
size_t(* IotMqttGetRemainingLength_t)(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the remaining length from a stream of bytes off the network.
Definition: iot_mqtt_types.h:748
Definition: iot_mqtt_types.h:285
IotMqttError_t(* IotMqttSerializeDisconnect_t)(uint8_t **pDisconnectPacket, size_t *pPacketSize)
DISCONNECT packet serializer function.
Definition: iot_mqtt_types.h:811
MQTT connection details.
Definition: iot_mqtt_types.h:589
Definition: iot_mqtt_types.h:308
A CONNECT or at least one subscription was refused by the server.
Definition: iot_mqtt_types.h:241
IotMqttDisconnectReason_t
The reason that an MQTT connection (and its associated network connection) was disconnected.
Definition: iot_mqtt_types.h:323
MQTT operation could not be scheduled, i.e. enqueued for sending.
Definition: iot_mqtt_types.h:191
MQTT operation failed because the network was unusable.
Definition: iot_mqtt_types.h:180
IotMqttError_t(* IotMqttSerializePingreq_t)(uint8_t **pPingreqPacket, size_t *pPacketSize)
PINGREQ packet serializer function.
Definition: iot_mqtt_types.h:774
Definition: iot_mqtt_types.h:286
struct _mqttOperation * IotMqttOperation_t
Opaque handle that references an in-progress MQTT operation.
Definition: iot_mqtt_types.h:90
IotMqttError_t(* IotMqttSerializePuback_t)(uint16_t packetIdentifier, uint8_t **pPubackPacket, size_t *pPacketSize)
PUBACK packet serializer function.
Definition: iot_mqtt_types.h:826
MQTT network connection details.
Definition: iot_mqtt_types.h:1013
IotMqttOperationType_t
Types of MQTT operations.
Definition: iot_mqtt_types.h:280
Initialization failed.
Definition: iot_mqtt_types.h:140
IotMqttError_t(* IotMqttSerializeConnect_t)(const IotMqttConnectInfo_t *pConnectInfo, uint8_t **pConnectPacket, size_t *pPacketSize)
CONNECT packet serializer function.
Definition: iot_mqtt_types.h:765
_IotNetworkServerInfo_t IotNetworkServerInfo_t
Definition: iot_mqtt_types.h:310
Definition: iot_mqtt_types.h:309
MQTT response packet received from the network is malformed.
Definition: iot_mqtt_types.h:208
Definition: iot_mqtt_types.h:287
A QoS 1 PUBLISH received no response and the retry limit was reached.
Definition: iot_mqtt_types.h:255
Definition: iot_mqtt_types.h:326
Parameter to an MQTT callback function.
Definition: iot_mqtt_types.h:441