AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
aws_iot_mqtt_client.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 // Based on Eclipse Paho.
17 /*******************************************************************************
18  * Copyright (c) 2014 IBM Corp.
19  *
20  * All rights reserved. This program and the accompanying materials
21  * are made available under the terms of the Eclipse Public License v1.0
22  * and Eclipse Distribution License v1.0 which accompany this distribution.
23  *
24  * The Eclipse Public License is available at
25  * http://www.eclipse.org/legal/epl-v10.html
26  * and the Eclipse Distribution License is available at
27  * http://www.eclipse.org/org/documents/edl-v10.php.
28  *
29  * Contributors:
30  * Ian Craggs - initial API and implementation and/or initial documentation
31  * Xiang Rong - 442039 Add makefile to Embedded C client
32  *******************************************************************************/
33 
39 #ifndef AWS_IOT_SDK_SRC_IOT_MQTT_CLIENT_H
40 #define AWS_IOT_SDK_SRC_IOT_MQTT_CLIENT_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* Library Header files */
47 #include "stdio.h"
48 #include "stdbool.h"
49 #include "stdint.h"
50 #include "stddef.h"
51 
52 /* AWS Specific header files */
53 #include "aws_iot_error.h"
54 #include "aws_iot_config.h"
55 
56 /* Platform specific implementation header files */
57 #include "network_interface.h"
58 #include "timer_interface.h"
59 
60 #ifdef _ENABLE_THREAD_SUPPORT_
61 #include "threads_interface.h"
62 #endif
63 
65 #define MAX_PACKET_ID 65535
66 
67 typedef struct _Client AWS_IoT_Client;
68 
76 typedef enum QoS {
77  QOS0 = 0,
78  QOS1 = 1
79 } QoS;
80 
87 typedef struct {
88  QoS qos;
89  uint8_t isRetained;
90  uint8_t isDup;
91  uint16_t id;
92  void *payload;
93  size_t payloadLen;
95 
102 typedef enum {
104 } MQTT_Ver_t;
105 
113 typedef struct {
114  char struct_id[4];
115  char *pTopicName;
116  uint16_t topicNameLen;
117  char *pMessage;
118  uint16_t msgLen;
119  bool isRetained;
124 
126 #define IoT_MQTT_Will_Options_Initializer { {'M', 'Q', 'T', 'W'}, NULL, 0, NULL, 0, false, QOS0 }
127 
134 typedef struct {
135  char struct_id[4];
137  char *pClientID;
138  uint16_t clientIDLen;
143  char *pUsername;
144  uint16_t usernameLen;
145  char *pPassword;
146  uint16_t passwordLen;
150 
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 }
154 
161 typedef void (*iot_disconnect_handler)(AWS_IoT_Client *, void *);
162 
170 typedef struct {
172  char *pHostURL;
173  uint16_t port;
183 #ifdef _ENABLE_THREAD_SUPPORT_
185 #endif
189 
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 }
193 #else
194 #define IoT_Client_Init_Params_initializer { true, NULL, 0, NULL, NULL, NULL, 2000, 20000, 5000, true, NULL, NULL }
195 #endif
196 
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
218 } ClientState;
219 
227 typedef void (*pApplicationHandler_t)(AWS_IoT_Client *pClient, char *pTopicName, uint16_t topicNameLen,
228  IoT_Publish_Message_Params *pParams, void *pClientData);
229 
237 typedef struct _MessageHandlers {
238  const char *topicName;
239  uint16_t topicNameLen;
244 } MessageHandlers; /* Message handlers are indexed by subscription topic */
245 
253 typedef struct _ClientStatus {
257 } ClientStatus;
258 
266 typedef struct _ClientData {
267  uint16_t nextPacketId;
268 
269  uint32_t packetTimeoutMs;
270  uint32_t commandTimeoutMs;
271  uint16_t keepAliveInterval;
274 
275  /* The below values are initialized with the
276  * lengths of the TX/RX buffers and never modified
277  * afterwards */
278  size_t writeBufSize;
279  size_t readBufSize;
280  size_t readBufIndex;
281  unsigned char writeBuf[AWS_IOT_MQTT_TX_BUF_LEN];
282  unsigned char readBuf[AWS_IOT_MQTT_RX_BUF_LEN];
283 
284 #ifdef _ENABLE_THREAD_SUPPORT_
286  IoT_Mutex_t state_change_mutex;
287  IoT_Mutex_t tls_read_mutex;
288  IoT_Mutex_t tls_write_mutex;
289 #endif
290 
292 
293  MessageHandlers messageHandlers[AWS_IOT_MQTT_NUM_SUBSCRIBE_HANDLERS];
296 } ClientData;
297 
304 struct _Client {
305  Timer pingReqTimer;
308 
311  Network networkStack;
312 };
313 
324 uint16_t aws_iot_mqtt_get_next_packet_id(AWS_IoT_Client *pClient);
325 
338 IoT_Error_t aws_iot_mqtt_set_connect_params(AWS_IoT_Client *pClient, IoT_Client_Connect_Params *pNewConnectParams);
339 
350 bool aws_iot_mqtt_is_client_connected(AWS_IoT_Client *pClient);
351 
361 ClientState aws_iot_mqtt_get_client_state(AWS_IoT_Client *pClient);
362 
373 bool aws_iot_is_autoreconnect_enabled(AWS_IoT_Client *pClient);
374 
387 IoT_Error_t aws_iot_mqtt_set_disconnect_handler(AWS_IoT_Client *pClient, iot_disconnect_handler pDisconnectHandler,
388  void *pDisconnectHandlerData);
389 
400 IoT_Error_t aws_iot_mqtt_autoreconnect_set_status(AWS_IoT_Client *pClient, bool newStatus);
401 
411 uint32_t aws_iot_mqtt_get_network_disconnected_count(AWS_IoT_Client *pClient);
412 
420 void aws_iot_mqtt_reset_network_disconnected_count(AWS_IoT_Client *pClient);
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 
426 #endif
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