39 #ifndef AWS_IOT_SDK_SRC_IOT_MQTT_CLIENT_H 40 #define AWS_IOT_SDK_SRC_IOT_MQTT_CLIENT_H 53 #include "aws_iot_error.h" 54 #include "aws_iot_config.h" 57 #include "network_interface.h" 58 #include "timer_interface.h" 60 #ifdef _ENABLE_THREAD_SUPPORT_ 61 #include "threads_interface.h" 65 #define MAX_PACKET_ID 65535 67 typedef struct _Client AWS_IoT_Client;
126 #define IoT_MQTT_Will_Options_Initializer { {'M', 'Q', 'T', 'W'}, NULL, 0, NULL, 0, false, QOS0 } 152 #define IoT_Client_Connect_Params_initializer { {'M', 'Q', 'T', 'C'}, MQTT_3_1_1, NULL, 0, 60, true, false, \ 153 IoT_MQTT_Will_Options_Initializer, NULL, 0, NULL, 0 } 183 #ifdef _ENABLE_THREAD_SUPPORT_ 191 #ifdef _ENABLE_THREAD_SUPPORT_ 192 #define IoT_Client_Init_Params_initializer { true, NULL, 0, NULL, NULL, NULL, 2000, 20000, 5000, true, NULL, NULL, false } 194 #define IoT_Client_Init_Params_initializer { true, NULL, 0, NULL, NULL, NULL, 2000, 20000, 5000, true, NULL, NULL } 203 typedef enum _ClientState {
204 CLIENT_STATE_INVALID = 0,
205 CLIENT_STATE_INITIALIZED = 1,
206 CLIENT_STATE_CONNECTING = 2,
207 CLIENT_STATE_CONNECTED_IDLE = 3,
208 CLIENT_STATE_CONNECTED_YIELD_IN_PROGRESS = 4,
209 CLIENT_STATE_CONNECTED_PUBLISH_IN_PROGRESS = 5,
210 CLIENT_STATE_CONNECTED_SUBSCRIBE_IN_PROGRESS = 6,
211 CLIENT_STATE_CONNECTED_UNSUBSCRIBE_IN_PROGRESS = 7,
212 CLIENT_STATE_CONNECTED_RESUBSCRIBE_IN_PROGRESS = 8,
213 CLIENT_STATE_CONNECTED_WAIT_FOR_CB_RETURN = 9,
214 CLIENT_STATE_DISCONNECTING = 10,
215 CLIENT_STATE_DISCONNECTED_ERROR = 11,
216 CLIENT_STATE_DISCONNECTED_MANUALLY = 12,
217 CLIENT_STATE_PENDING_RECONNECT = 13
237 typedef struct _MessageHandlers {
253 typedef struct _ClientStatus {
266 typedef struct _ClientData {
281 unsigned char writeBuf[AWS_IOT_MQTT_TX_BUF_LEN];
282 unsigned char readBuf[AWS_IOT_MQTT_RX_BUF_LEN];
284 #ifdef _ENABLE_THREAD_SUPPORT_ 388 void *pDisconnectHandlerData);
IoT_Error_t aws_iot_mqtt_set_disconnect_handler(AWS_IoT_Client *pClient, iot_disconnect_handler pDisconnectHandler, void *pDisconnectHandlerData)
Set the IoT Client disconnect handler.
Definition: aws_iot_mqtt_client.c:375
bool isWillMsgPresent
Is there a LWT associated with this connection?
Definition: aws_iot_mqtt_client.h:141
bool isSSLHostnameVerify
Client should perform server certificate hostname validation.
Definition: aws_iot_mqtt_client.h:180
uint16_t nextPacketId
Packet ID to use for the next generated packet.
Definition: aws_iot_mqtt_client.h:267
uint32_t mqttPacketTimeout_ms
Timeout for reading a complete MQTT packet. In milliseconds.
Definition: aws_iot_mqtt_client.h:177
uint16_t keepAliveInterval
Maximum interval between control packets.
Definition: aws_iot_mqtt_client.h:271
uint16_t msgLen
The length of the Message, 16 bit unsinged integer.
Definition: aws_iot_mqtt_client.h:118
MQTT Message Handler.
Definition: aws_iot_mqtt_client.h:237
bool aws_iot_mqtt_is_client_connected(AWS_IoT_Client *pClient)
Is the MQTT client currently connected?
Definition: aws_iot_mqtt_client.c:319
void * pApplicationHandlerData
Context to pass to application handler.
Definition: aws_iot_mqtt_client.h:243
MQTT Client.
Definition: aws_iot_mqtt_client.h:304
char * pMessage
Message to be delivered as LWT.
Definition: aws_iot_mqtt_client.h:117
const char * topicName
Topic name of subscription.
Definition: aws_iot_mqtt_client.h:238
IoT_Client_Connect_Params options
Options passed when the client was initialized.
Definition: aws_iot_mqtt_client.h:291
QoS qos
Message Quality of Service.
Definition: aws_iot_mqtt_client.h:88
const IoT_Client_Init_Params iotClientInitParamsDefault
Definition: aws_iot_mqtt_client.c:62
ClientState aws_iot_mqtt_get_client_state(AWS_IoT_Client *pClient)
Get the current state of the client.
Definition: aws_iot_mqtt_client.c:66
IoT_Mutex_t tls_write_mutex
Mutex protecting outgoing data.
Definition: aws_iot_mqtt_client.h:288
MQTT_Ver_t
MQTT Version Type.
Definition: aws_iot_mqtt_client.h:102
void(* iot_disconnect_handler)(AWS_IoT_Client *, void *)
Disconnect Callback Handler Type.
Definition: aws_iot_mqtt_client.h:161
uint16_t clientIDLen
Client Id Length. 16 bit unsigned integer.
Definition: aws_iot_mqtt_client.h:138
uint16_t passwordLen
Password Length. 16 bit unsigned integer.
Definition: aws_iot_mqtt_client.h:146
void(* pApplicationHandler_t)(AWS_IoT_Client *pClient, char *pTopicName, uint16_t topicNameLen, IoT_Publish_Message_Params *pParams, void *pClientData)
Application Callback Handler Type.
Definition: aws_iot_mqtt_client.h:227
size_t writeBufSize
Size of this client's outgoing data buffer.
Definition: aws_iot_mqtt_client.h:278
uint32_t mqttCommandTimeout_ms
Timeout for MQTT blocking calls. In milliseconds.
Definition: aws_iot_mqtt_client.h:178
MQTT Connection Parameters.
Definition: aws_iot_mqtt_client.h:134
Timer pingReqTimer
Timer to keep track of when to send next PINGREQ.
Definition: aws_iot_mqtt_client.h:305
void * disconnectHandlerData
Context for disconnect handler.
Definition: aws_iot_mqtt_client.h:295
MQTT_Ver_t MQTTVersion
Desired MQTT version used during connection.
Definition: aws_iot_mqtt_client.h:136
const IoT_MQTT_Will_Options iotMqttWillOptionsDefault
Definition: aws_iot_mqtt_client.c:63
void * payload
Pointer to MQTT message payload (bytes).
Definition: aws_iot_mqtt_client.h:92
iot_disconnect_handler disconnectHandler
Callback to be invoked upon connection loss.
Definition: aws_iot_mqtt_client.h:181
size_t payloadLen
Length of MQTT payload.
Definition: aws_iot_mqtt_client.h:93
bool aws_iot_is_autoreconnect_enabled(AWS_IoT_Client *pClient)
Is the MQTT client set to reconnect automatically?
Definition: aws_iot_mqtt_client.c:356
bool enableAutoReconnect
Set to true to enable auto reconnect.
Definition: aws_iot_mqtt_client.h:171
QoS qos
QoS of subscription.
Definition: aws_iot_mqtt_client.h:241
ClientState
MQTT Client State Type.
Definition: aws_iot_mqtt_client.h:203
uint8_t isDup
Is this message a duplicate QoS > 0 message? Handled automatically by the MQTT client.
Definition: aws_iot_mqtt_client.h:90
ClientStatus clientStatus
Client state information.
Definition: aws_iot_mqtt_client.h:309
uint32_t commandTimeoutMs
Timeout for processing outgoing MQTT packets.
Definition: aws_iot_mqtt_client.h:270
ClientData clientData
Client context.
Definition: aws_iot_mqtt_client.h:310
QoS qos
QoS of LWT message.
Definition: aws_iot_mqtt_client.h:120
size_t readBufSize
Size of this client's incoming data buffer.
Definition: aws_iot_mqtt_client.h:279
Publish Message Parameters Type.
Definition: aws_iot_mqtt_client.h:87
MQTT Client Status.
Definition: aws_iot_mqtt_client.h:253
uint32_t currentReconnectWaitInterval
Current backoff period for reconnect.
Definition: aws_iot_mqtt_client.h:272
ClientState clientState
The current state of the client's state machine.
Definition: aws_iot_mqtt_client.h:254
uint16_t port
MQTT service listening port.
Definition: aws_iot_mqtt_client.h:173
IoT_Mutex_t state_change_mutex
Mutex protecting the client's state machine.
Definition: aws_iot_mqtt_client.h:286
uint32_t aws_iot_mqtt_get_network_disconnected_count(AWS_IoT_Client *pClient)
Get count of Network Disconnects.
Definition: aws_iot_mqtt_client.c:387
uint16_t usernameLen
Username Length. 16 bit unsigned integer.
Definition: aws_iot_mqtt_client.h:144
MQTT 3.1.1 (protocol message byte = 4)
Definition: aws_iot_mqtt_client.h:103
bool isBlockOnThreadLockEnabled
Whether to use nonblocking or blocking mutex APIs.
Definition: aws_iot_mqtt_client.h:285
uint32_t packetTimeoutMs
Timeout for reading incoming packets from the network.
Definition: aws_iot_mqtt_client.h:269
char * pTopicName
The LWT topic to which the LWT message will be published.
Definition: aws_iot_mqtt_client.h:115
IoT_Mutex_t tls_read_mutex
Mutex protecting incoming data.
Definition: aws_iot_mqtt_client.h:287
uint16_t topicNameLen
The length of the LWT topic, 16 bit unsinged integer.
Definition: aws_iot_mqtt_client.h:116
bool isCleanSession
MQTT clean session. True = this session is to be treated as clean. Previous server state is cleared a...
Definition: aws_iot_mqtt_client.h:140
IoT_Error_t aws_iot_mqtt_set_connect_params(AWS_IoT_Client *pClient, IoT_Client_Connect_Params *pNewConnectParams)
Set the connection parameters for the IoT Client.
Definition: aws_iot_mqtt_client.c:167
Network networkStack
Table of network function pointers.
Definition: aws_iot_mqtt_client.h:311
Timer reconnectDelayTimer
Timer for backoff on reconnect.
Definition: aws_iot_mqtt_client.h:307
void * disconnectHandlerData
Data to pass as argument when disconnect handler is called.
Definition: aws_iot_mqtt_client.h:182
char * pUsername
Not used in the AWS IoT Service, will need to be cstring if used.
Definition: aws_iot_mqtt_client.h:143
char * pDevicePrivateKeyLocation
Pointer to a string defining the device private key file (full file, not path)
Definition: aws_iot_mqtt_client.h:176
uint16_t keepAliveIntervalInSec
MQTT keep alive interval in seconds. Defines inactivity time allowed before determining the connectio...
Definition: aws_iot_mqtt_client.h:139
MQTT Client Data.
Definition: aws_iot_mqtt_client.h:266
const IoT_Client_Connect_Params iotClientConnectParamsDefault
Definition: aws_iot_mqtt_client.c:64
char * pPassword
Not used in the AWS IoT Service, will need to be cstring if used.
Definition: aws_iot_mqtt_client.h:145
MQTT Initialization Parameters.
Definition: aws_iot_mqtt_client.h:170
IoT_MQTT_Will_Options will
MQTT LWT parameters.
Definition: aws_iot_mqtt_client.h:142
bool isPingOutstanding
Whether this client is waiting for a ping response.
Definition: aws_iot_mqtt_client.h:255
IoT_Error_t aws_iot_mqtt_autoreconnect_set_status(AWS_IoT_Client *pClient, bool newStatus)
Enable or Disable AutoReconnect on Network Disconnect.
Definition: aws_iot_mqtt_client.c:366
size_t readBufIndex
Current offset into the incoming data buffer.
Definition: aws_iot_mqtt_client.h:280
uint32_t tlsHandshakeTimeout_ms
TLS handshake timeout. In milliseconds.
Definition: aws_iot_mqtt_client.h:179
void aws_iot_mqtt_reset_network_disconnected_count(AWS_IoT_Client *pClient)
Reset Network Disconnect conter.
Definition: aws_iot_mqtt_client.c:391
iot_disconnect_handler disconnectHandler
Callback when a disconnection is detected.
Definition: aws_iot_mqtt_client.h:294
bool isRetained
NOT supported. The retained flag for the LWT message (see MQTTAsync_message.retained) ...
Definition: aws_iot_mqtt_client.h:119
bool isBlockOnThreadLockEnabled
Timeout for Thread blocking calls. Set to 0 to block until lock is obtained. In milliseconds.
Definition: aws_iot_mqtt_client.h:184
char * pClientID
Pointer to a string defining the MQTT client ID (this needs to be unique per device across your AWS a...
Definition: aws_iot_mqtt_client.h:137
bool isAutoReconnectEnabled
Whether auto-reconnect is enabled for this client.
Definition: aws_iot_mqtt_client.h:256
char * pDeviceCertLocation
Pointer to a string defining the device identity certificate file (full file, not path) ...
Definition: aws_iot_mqtt_client.h:175
char * pRootCALocation
Pointer to a string defining the Root CA file (full file, not path)
Definition: aws_iot_mqtt_client.h:174
Last Will and Testament Definition.
Definition: aws_iot_mqtt_client.h:113
Timer pingRespTimer
Timer to ensure that PINGRESP is received timely.
Definition: aws_iot_mqtt_client.h:306
pApplicationHandler_t pApplicationHandler
Application function to invoke.
Definition: aws_iot_mqtt_client.h:242
uint32_t counterNetworkDisconnected
How many times this client detected a disconnection.
Definition: aws_iot_mqtt_client.h:273
char * pHostURL
Pointer to a string defining the endpoint for the MQTT service.
Definition: aws_iot_mqtt_client.h:172
uint16_t id
Message sequence identifier. Handled automatically by the MQTT client.
Definition: aws_iot_mqtt_client.h:91
QoS
Quality of Service Type.
Definition: aws_iot_mqtt_client.h:76
uint16_t aws_iot_mqtt_get_next_packet_id(AWS_IoT_Client *pClient)
What is the next available packet Id.
Definition: aws_iot_mqtt_client.c:314
char resubscribed
Whether this handler was successfully resubscribed in the reconnect workflow.
Definition: aws_iot_mqtt_client.h:240
uint16_t topicNameLen
Length of topic name.
Definition: aws_iot_mqtt_client.h:239
uint8_t isRetained
Retained messages are NOT supported by the AWS IoT Service at the time of this SDK release...
Definition: aws_iot_mqtt_client.h:89