Demo application for using the AT&T IoT Starter Kit Powered by AWS.

Dependencies:   SDFileSystem

Fork of ATT_AWS_IoT_demo by Anthony Phillips

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTClient.h Source File

MQTTClient.h

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 #ifndef __MQTT_CLIENT_C_
00018 #define __MQTT_CLIENT_C_
00019 
00020 /* Library Header files */
00021 #include "stdio.h"
00022 #include "stdint.h"
00023 #include "stddef.h"
00024 
00025 /* MQTT Specific header files */
00026 #include "MQTTReturnCodes.h"
00027 #include "MQTTMessage.h"
00028 #include "MQTTPacket.h"
00029 
00030 /* AWS Specific header files */
00031 #include "aws_iot_config.h"
00032 
00033 /* Platform specific implementation header files */
00034 #include "network_interface.h"
00035 #include "timer_interface.h"
00036 
00037 #define MAX_PACKET_ID 65535
00038 #define MAX_MESSAGE_HANDLERS AWS_IOT_MQTT_NUM_SUBSCRIBE_HANDLERS
00039 
00040 #define MIN_RECONNECT_WAIT_INTERVAL AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
00041 #define MAX_RECONNECT_WAIT_INTERVAL AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
00042 
00043 void NewTimer(Timer *);
00044 
00045 typedef struct Client Client;
00046 
00047 typedef struct MessageData MessageData;
00048 
00049 typedef void (*messageHandler)(MessageData *);
00050 typedef void (*pApplicationHandler_t)(void);
00051 typedef void (*disconnectHandler_t)(void);
00052 typedef int (*networkInitHandler_t)(Network *);
00053 
00054 struct MessageData {
00055     MQTTMessage *message;
00056     MQTTString *topicName;
00057     pApplicationHandler_t applicationHandler;
00058 };
00059 
00060 MQTTReturnCode MQTTConnect(Client *c, MQTTPacket_connectData *options);
00061 MQTTReturnCode MQTTPublish (Client *, const char *, MQTTMessage *);
00062 MQTTReturnCode MQTTSubscribe(Client *c, const char *topicFilter, QoS qos,
00063                              messageHandler messageHandler, pApplicationHandler_t applicationHandler);
00064 MQTTReturnCode MQTTResubscribe(Client *c);
00065 MQTTReturnCode MQTTUnsubscribe(Client *c, const char *topicFilter);
00066 MQTTReturnCode MQTTDisconnect (Client *);
00067 MQTTReturnCode MQTTYield (Client *, uint32_t);
00068 MQTTReturnCode MQTTAttemptReconnect(Client *c);
00069 
00070 uint8_t MQTTIsConnected(Client *);
00071 uint8_t MQTTIsAutoReconnectEnabled(Client *c);
00072 
00073 void setDefaultMessageHandler(Client *, messageHandler);
00074 MQTTReturnCode setDisconnectHandler(Client *c, disconnectHandler_t disconnectHandler);
00075 MQTTReturnCode setAutoReconnectEnabled(Client *c, uint8_t value);
00076 
00077 MQTTReturnCode MQTTClient(Client *, uint32_t, unsigned char *, size_t, unsigned char *,
00078                           size_t, uint8_t, networkInitHandler_t, TLSConnectParams *);
00079 
00080 uint32_t MQTTGetNetworkDisconnectedCount(Client *c);
00081 void MQTTResetNetworkDisconnectedCount(Client *c);
00082 
00083 struct Client {
00084     uint8_t isConnected;
00085     uint8_t wasManuallyDisconnected;
00086     uint8_t isPingOutstanding;
00087     uint8_t isAutoReconnectEnabled;
00088 
00089     uint16_t nextPacketId;
00090 
00091     uint32_t commandTimeoutMs;
00092     uint32_t keepAliveInterval;
00093     uint32_t currentReconnectWaitInterval;
00094     uint32_t counterNetworkDisconnected;
00095 
00096     size_t bufSize;
00097     size_t readBufSize;
00098 
00099     unsigned char *buf;  
00100     unsigned char *readbuf;
00101 
00102     TLSConnectParams tlsConnectParams;
00103     MQTTPacket_connectData options;
00104 
00105     Network networkStack;
00106     Timer pingTimer;
00107     Timer reconnectDelayTimer;
00108 
00109     struct MessageHandlers {
00110         const char *topicFilter;
00111         void (*fp) (MessageData *);
00112         pApplicationHandler_t applicationHandler;
00113         QoS qos;
00114     } messageHandlers[MAX_MESSAGE_HANDLERS];      /* Message handlers are indexed by subscription topic */
00115     
00116     void (* defaultMessageHandler) (MessageData *);
00117     disconnectHandler_t disconnectHandler;
00118     networkInitHandler_t networkInitHandler;
00119 };
00120 
00121 #define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0}
00122 
00123 #endif
00124 
00125