AWS IoT Embedded C Device SDK
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 /* Library Header files */
43 #include "stdio.h"
44 #include "stdbool.h"
45 #include "stdint.h"
46 #include "stddef.h"
47 
48 /* AWS Specific header files */
49 #include "aws_iot_error.h"
50 #include "aws_iot_config.h"
51 
52 /* Platform specific implementation header files */
53 #include "network_interface.h"
54 #include "timer_interface.h"
55 
56 #ifdef _ENABLE_THREAD_SUPPORT_
57 #include "threads_interface.h"
58 #endif
59 
60 #define MAX_PACKET_ID 65535
61 
62 typedef struct _Client AWS_IoT_Client;
63 
71 typedef enum QoS {
72  QOS0 = 0,
73  QOS1 = 1
74 } QoS;
75 
82 typedef struct {
83  QoS qos;
84  uint8_t isRetained;
85  uint8_t isDup;
86  uint16_t id;
87  void *payload;
88  size_t payloadLen;
90 
97 typedef enum {
99 } MQTT_Ver_t;
100 
108 typedef struct {
109  char struct_id[4];
110  char *pTopicName;
111  uint16_t topicNameLen;
112  char *pMessage;
113  uint16_t msgLen;
114  bool isRetained;
117 extern const IoT_MQTT_Will_Options iotMqttWillOptionsDefault;
118 
119 #define IoT_MQTT_Will_Options_Initializer { {'M', 'Q', 'T', 'W'}, NULL, 0, NULL, 0, false, QOS0 }
120 
127 typedef struct {
128  char struct_id[4];
130  char *pClientID;
131  uint16_t clientIDLen;
136  char *pUsername;
137  uint16_t usernameLen;
138  char *pPassword;
139  uint16_t passwordLen;
141 extern const IoT_Client_Connect_Params iotClientConnectParamsDefault;
142 
143 #define IoT_Client_Connect_Params_initializer { {'M', 'Q', 'T', 'C'}, MQTT_3_1_1, NULL, 0, 60, true, false, \
144  IoT_MQTT_Will_Options_Initializer, NULL, 0, NULL, 0 }
145 
152 typedef void (*iot_disconnect_handler)(AWS_IoT_Client *, void *);
153 
161 typedef struct {
163  char *pHostURL;
164  uint16_t port;
169 #ifdef _ENABLE_THREAD_SUPPORT_
170  bool isBlockOnThreadLockEnabled;
171 #endif
177 extern const IoT_Client_Init_Params iotClientInitParamsDefault;
178 
185 typedef enum _ClientState {
186  CLIENT_STATE_INVALID = 0,
187  CLIENT_STATE_INITIALIZED = 1,
188  CLIENT_STATE_CONNECTING = 2,
189  CLIENT_STATE_CONNECTED_IDLE = 3,
190  CLIENT_STATE_CONNECTED_YIELD_IN_PROGRESS = 4,
191  CLIENT_STATE_CONNECTED_PUBLISH_IN_PROGRESS = 5,
192  CLIENT_STATE_CONNECTED_SUBSCRIBE_IN_PROGRESS = 6,
193  CLIENT_STATE_CONNECTED_UNSUBSCRIBE_IN_PROGRESS = 7,
194  CLIENT_STATE_CONNECTED_RESUBSCRIBE_IN_PROGRESS = 8,
195  CLIENT_STATE_CONNECTED_WAIT_FOR_CB_RETURN = 9,
196  CLIENT_STATE_DISCONNECTING = 10,
197  CLIENT_STATE_DISCONNECTED_ERROR = 11,
198  CLIENT_STATE_DISCONNECTED_MANUALLY = 12,
199  CLIENT_STATE_PENDING_RECONNECT = 13
200 } ClientState;
201 
209 typedef void (*pApplicationHandler_t)(AWS_IoT_Client *pClient, char *pTopicName, uint16_t topicNameLen,
210  IoT_Publish_Message_Params *pParams, void *pClientData);
211 
219 typedef struct _MessageHandlers {
220  const char *topicName;
221  uint16_t topicNameLen;
222  QoS qos;
223  pApplicationHandler_t pApplicationHandler;
224  void *pApplicationHandlerData;
225 } MessageHandlers; /* Message handlers are indexed by subscription topic */
226 
234 typedef struct _ClientStatus {
235  ClientState clientState;
236  bool isPingOutstanding;
237  bool isAutoReconnectEnabled;
238 } ClientStatus;
239 
247 typedef struct _ClientData {
248  uint16_t nextPacketId;
249 
250  uint32_t commandTimeoutMs;
251  uint16_t keepAliveInterval;
252  uint32_t currentReconnectWaitInterval;
253  uint32_t counterNetworkDisconnected;
254 
255  /* The below values are initialized with the
256  * lengths of the TX/RX buffers and never modified
257  * afterwards */
258  size_t writeBufSize;
259  size_t readBufSize;
260 
261  unsigned char writeBuf[AWS_IOT_MQTT_TX_BUF_LEN];
262  unsigned char readBuf[AWS_IOT_MQTT_RX_BUF_LEN];
263 
264 #ifdef _ENABLE_THREAD_SUPPORT_
265  bool isBlockOnThreadLockEnabled;
266  IoT_Mutex_t state_change_mutex;
267  IoT_Mutex_t tls_read_mutex;
268  IoT_Mutex_t tls_write_mutex;
269 #endif
270 
272 
273  MessageHandlers messageHandlers[AWS_IOT_MQTT_NUM_SUBSCRIBE_HANDLERS];
274  iot_disconnect_handler disconnectHandler;
275 
276  void *disconnectHandlerData;
277 } ClientData;
278 
285 struct _Client {
286  Timer pingTimer;
287  Timer reconnectDelayTimer;
288 
289  ClientStatus clientStatus;
290  ClientData clientData;
291  Network networkStack;
292 };
293 
305 
319 
331 
342 
354 
368  void *pDisconnectHandlerData);
369 
381 
392 
401 
402 #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:278
bool isWillMsgPresent
Is there a LWT associated with this connection?
Definition: aws_iot_mqtt_client.h:134
bool isSSLHostnameVerify
Client should perform server certificate hostname validation.
Definition: aws_iot_mqtt_client.h:173
uint16_t msgLen
The length of the Message, 16 bit unsinged integer.
Definition: aws_iot_mqtt_client.h:113
bool aws_iot_mqtt_is_client_connected(AWS_IoT_Client *pClient)
Is the MQTT client currently connected?
Definition: aws_iot_mqtt_client.c:222
MQTT Client.
Definition: aws_iot_mqtt_client.h:285
char * pMessage
Message to be delivered as LWT.
Definition: aws_iot_mqtt_client.h:112
Definition of error types for the SDK.
Timer interface definition for MQTT client.
QoS qos
Message Quality of Service.
Definition: aws_iot_mqtt_client.h:83
ClientState aws_iot_mqtt_get_client_state(AWS_IoT_Client *pClient)
Get the current state of the client.
Definition: aws_iot_mqtt_client.c:48
MQTT_Ver_t
MQTT Version Type.
Definition: aws_iot_mqtt_client.h:97
void(* iot_disconnect_handler)(AWS_IoT_Client *, void *)
Disconnect Callback Handler Type.
Definition: aws_iot_mqtt_client.h:152
uint16_t clientIDLen
Client Id Length. 16 bit unsigned integer.
Definition: aws_iot_mqtt_client.h:131
uint16_t passwordLen
Password Length. 16 bit unsigned integer.
Definition: aws_iot_mqtt_client.h:139
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:209
uint32_t mqttCommandTimeout_ms
Timeout for MQTT blocking calls. In milliseconds.
Definition: aws_iot_mqtt_client.h:168
MQTT Connection Parameters.
Definition: aws_iot_mqtt_client.h:127
MQTT_Ver_t MQTTVersion
Desired MQTT version used during connection.
Definition: aws_iot_mqtt_client.h:129
void * payload
Pointer to MQTT message payload (bytes).
Definition: aws_iot_mqtt_client.h:87
IoT_Error_t
IoT Error enum.
Definition: aws_iot_error.h:36
iot_disconnect_handler disconnectHandler
Callback to be invoked upon connection loss.
Definition: aws_iot_mqtt_client.h:174
size_t payloadLen
Length of MQTT payload.
Definition: aws_iot_mqtt_client.h:88
Thread interface definition for MQTT client.
bool aws_iot_is_autoreconnect_enabled(AWS_IoT_Client *pClient)
Is the MQTT client set to reconnect automatically?
Definition: aws_iot_mqtt_client.c:259
bool enableAutoReconnect
Set to true to enable auto reconnect.
Definition: aws_iot_mqtt_client.h:162
Definition: timer_platform.h:29
uint8_t isDup
Is this message a duplicate QoS > 0 message? Handled automatically by the MQTT client.
Definition: aws_iot_mqtt_client.h:85
QoS qos
QoS of LWT message.
Definition: aws_iot_mqtt_client.h:115
_ClientState
MQTT Client State Type.
Definition: aws_iot_mqtt_client.h:185
enum _ClientState ClientState
MQTT Client State Type.
Network Structure.
Definition: network_interface.h:61
Publish Message Parameters Type.
Definition: aws_iot_mqtt_client.h:82
struct _MessageHandlers MessageHandlers
MQTT Message Handler.
uint16_t port
MQTT service listening port.
Definition: aws_iot_mqtt_client.h:164
uint32_t aws_iot_mqtt_get_network_disconnected_count(AWS_IoT_Client *pClient)
Get count of Network Disconnects.
Definition: aws_iot_mqtt_client.c:290
uint16_t usernameLen
Username Length. 16 bit unsigned integer.
Definition: aws_iot_mqtt_client.h:137
MQTT 3.1.1 (protocol message byte = 4)
Definition: aws_iot_mqtt_client.h:98
char * pTopicName
The LWT topic to which the LWT message will be published.
Definition: aws_iot_mqtt_client.h:110
uint16_t topicNameLen
The length of the LWT topic, 16 bit unsinged integer.
Definition: aws_iot_mqtt_client.h:111
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:133
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:124
struct _ClientStatus ClientStatus
MQTT Client Status.
void * disconnectHandlerData
Data to pass as argument when disconnect handler is called.
Definition: aws_iot_mqtt_client.h:175
char * pUsername
Not used in the AWS IoT Service, will need to be cstring if used.
Definition: aws_iot_mqtt_client.h:136
char * pDevicePrivateKeyLocation
Pointer to a string defining the device private key file (full file, not path)
Definition: aws_iot_mqtt_client.h:167
uint16_t keepAliveIntervalInSec
MQTT keep alive interval in seconds. Defines inactivity time allowed before determining the connectio...
Definition: aws_iot_mqtt_client.h:132
char * pPassword
Not used in the AWS IoT Service, will need to be cstring if used.
Definition: aws_iot_mqtt_client.h:138
MQTT Client Data.
Definition: aws_iot_mqtt_client.h:247
MQTT Initialization Parameters.
Definition: aws_iot_mqtt_client.h:161
IoT_MQTT_Will_Options will
MQTT LWT parameters.
Definition: aws_iot_mqtt_client.h:135
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:269
uint32_t tlsHandshakeTimeout_ms
TLS handshake timeout. In milliseconds.
Definition: aws_iot_mqtt_client.h:172
void aws_iot_mqtt_reset_network_disconnected_count(AWS_IoT_Client *pClient)
Reset Network Disconnect conter.
Definition: aws_iot_mqtt_client.c:294
MQTT Message Handler.
Definition: aws_iot_mqtt_client.h:219
bool isRetained
NOT supported. The retained flag for the LWT message (see MQTTAsync_message.retained) ...
Definition: aws_iot_mqtt_client.h:114
struct _ClientData ClientData
MQTT Client Data.
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:130
char * pDeviceCertLocation
Pointer to a string defining the device identity certificate file (full file, not path) ...
Definition: aws_iot_mqtt_client.h:166
char * pRootCALocation
Pointer to a string defining the Root CA file (full file, not path)
Definition: aws_iot_mqtt_client.h:165
Last Will and Testament Definition.
Definition: aws_iot_mqtt_client.h:108
MQTT Client Status.
Definition: aws_iot_mqtt_client.h:234
char * pHostURL
Pointer to a string defining the endpoint for the MQTT service.
Definition: aws_iot_mqtt_client.h:163
uint16_t id
Message sequence identifier. Handled automatically by the MQTT client.
Definition: aws_iot_mqtt_client.h:86
Network interface definition for MQTT client.
QoS
Quality of Service Type.
Definition: aws_iot_mqtt_client.h:71
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:217
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:84