A Threaded Secure MQTT Client example. Uses MBED TLS for SSL/TLS connection. QoS0 only for now. Example has been tested with K64F connected via Ethernet.

Dependencies:   FP MQTTPacket

Fork of HelloMQTT by MQTT

Committer:
vpcola
Date:
Mon Mar 27 14:08:46 2017 +0000
Revision:
27:c90092f35d79
Parent:
26:4b21de8043a5
Child:
30:b2aed80037db
Child:
31:d34f6adb7a53
Minor comments added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 23:06fac173529e 1 #ifndef _MQTT_THREADED_CLIENT_H_
vpcola 23:06fac173529e 2 #define _MQTT_THREADED_CLIENT_H_
vpcola 23:06fac173529e 3
vpcola 23:06fac173529e 4 #include "mbed.h"
vpcola 23:06fac173529e 5 #include "rtos.h"
vpcola 23:06fac173529e 6 #include "MQTTPacket.h"
vpcola 23:06fac173529e 7 #include "NetworkInterface.h"
vpcola 23:06fac173529e 8 #include "FP.h"
vpcola 23:06fac173529e 9
vpcola 25:326f00faa092 10
vpcola 23:06fac173529e 11 #include <cstdio>
vpcola 23:06fac173529e 12 #include <string>
vpcola 23:06fac173529e 13 #include <map>
vpcola 23:06fac173529e 14
vpcola 23:06fac173529e 15 #define COMMAND_TIMEOUT 5000
vpcola 23:06fac173529e 16 #define DEFAULT_SOCKET_TIMEOUT 1000
vpcola 23:06fac173529e 17 #define MAX_MQTT_PACKET_SIZE 200
vpcola 23:06fac173529e 18 #define MAX_MQTT_PAYLOAD_SIZE 100
vpcola 23:06fac173529e 19
vpcola 23:06fac173529e 20 typedef enum { QOS0, QOS1, QOS2 } QoS;
vpcola 23:06fac173529e 21
vpcola 23:06fac173529e 22 // all failure return codes must be negative
vpcola 23:06fac173529e 23 typedef enum { BUFFER_OVERFLOW = -3, TIMEOUT = -2, FAILURE = -1, SUCCESS = 0 } returnCode;
vpcola 23:06fac173529e 24
vpcola 23:06fac173529e 25
vpcola 23:06fac173529e 26 typedef struct
vpcola 23:06fac173529e 27 {
vpcola 23:06fac173529e 28 QoS qos;
vpcola 23:06fac173529e 29 bool retained;
vpcola 23:06fac173529e 30 bool dup;
vpcola 23:06fac173529e 31 unsigned short id;
vpcola 23:06fac173529e 32 void *payload;
vpcola 23:06fac173529e 33 size_t payloadlen;
vpcola 23:06fac173529e 34 }Message, *pMessage;
vpcola 23:06fac173529e 35
vpcola 23:06fac173529e 36 // TODO:
vpcola 23:06fac173529e 37 // Merge this struct with the one above, in order to use the same
vpcola 23:06fac173529e 38 // data structure for sending and receiving. I need to simplify
vpcola 23:06fac173529e 39 // the PubMessage to not contain pointers like the one above.
vpcola 23:06fac173529e 40 typedef struct
vpcola 23:06fac173529e 41 {
vpcola 23:06fac173529e 42 char topic[100];
vpcola 23:06fac173529e 43 QoS qos;
vpcola 23:06fac173529e 44 unsigned short id;
vpcola 23:06fac173529e 45 size_t payloadlen;
vpcola 23:06fac173529e 46 char payload[MAX_MQTT_PAYLOAD_SIZE];
vpcola 23:06fac173529e 47 }PubMessage, *pPubMessage;
vpcola 23:06fac173529e 48
vpcola 23:06fac173529e 49 struct MessageData
vpcola 23:06fac173529e 50 {
vpcola 23:06fac173529e 51 MessageData(MQTTString &aTopicName, Message &aMessage) : message(aMessage), topicName(aTopicName)
vpcola 23:06fac173529e 52 { }
vpcola 23:06fac173529e 53 Message &message;
vpcola 23:06fac173529e 54 MQTTString &topicName;
vpcola 23:06fac173529e 55 };
vpcola 23:06fac173529e 56
vpcola 23:06fac173529e 57 class PacketId
vpcola 23:06fac173529e 58 {
vpcola 23:06fac173529e 59 public:
vpcola 23:06fac173529e 60 PacketId()
vpcola 23:06fac173529e 61 {
vpcola 23:06fac173529e 62 next = 0;
vpcola 23:06fac173529e 63 }
vpcola 23:06fac173529e 64
vpcola 23:06fac173529e 65 int getNext()
vpcola 23:06fac173529e 66 {
vpcola 23:06fac173529e 67 return next = (next == MAX_PACKET_ID) ? 1 : ++next;
vpcola 23:06fac173529e 68 }
vpcola 23:06fac173529e 69
vpcola 23:06fac173529e 70 private:
vpcola 23:06fac173529e 71 static const int MAX_PACKET_ID = 65535;
vpcola 23:06fac173529e 72 int next;
vpcola 23:06fac173529e 73 };
vpcola 23:06fac173529e 74
vpcola 23:06fac173529e 75
vpcola 23:06fac173529e 76
vpcola 23:06fac173529e 77 class MQTTThreadedClient
vpcola 23:06fac173529e 78 {
vpcola 23:06fac173529e 79 public:
vpcola 25:326f00faa092 80 MQTTThreadedClient(NetworkInterface * aNetwork, const char * pem = NULL)
vpcola 23:06fac173529e 81 : network(aNetwork),
vpcola 25:326f00faa092 82 ssl_ca_pem(pem),
vpcola 26:4b21de8043a5 83 port((pem != NULL) ? 8883 : 1883),
vpcola 23:06fac173529e 84 queue(32 * EVENTS_EVENT_SIZE),
vpcola 26:4b21de8043a5 85 isConnected(false),
vpcola 26:4b21de8043a5 86 hasSavedSession(false),
vpcola 26:4b21de8043a5 87 useTLS(pem != NULL)
vpcola 25:326f00faa092 88 {
vpcola 25:326f00faa092 89 DRBG_PERS = "mbed TLS MQTT client";
vpcola 23:06fac173529e 90 tcpSocket = new TCPSocket();
vpcola 25:326f00faa092 91 setupTLS();
vpcola 25:326f00faa092 92 }
vpcola 25:326f00faa092 93
vpcola 25:326f00faa092 94 ~MQTTThreadedClient()
vpcola 25:326f00faa092 95 {
vpcola 25:326f00faa092 96 // TODO: signal the thread to shutdown
vpcola 25:326f00faa092 97 freeTLS();
vpcola 25:326f00faa092 98
vpcola 25:326f00faa092 99 if (isConnected)
vpcola 25:326f00faa092 100 disconnect();
vpcola 25:326f00faa092 101
vpcola 23:06fac173529e 102 }
vpcola 27:c90092f35d79 103 /**
vpcola 27:c90092f35d79 104 * Sets the connection parameters. Must be called before running the startListener as a thread.
vpcola 27:c90092f35d79 105 *
vpcola 27:c90092f35d79 106 * @param host - pointer to the host where the MQTT server is running
vpcola 27:c90092f35d79 107 * @param port - the port number to connect, 1883 for non secure connections, 8883 for
vpcola 27:c90092f35d79 108 * secure connections
vpcola 27:c90092f35d79 109 * @param options - the connect data used for logging into the MQTT server.
vpcola 27:c90092f35d79 110 */
vpcola 26:4b21de8043a5 111 void setConnectionParameters(const char * host, uint16_t port, MQTTPacket_connectData & options);
vpcola 23:06fac173529e 112 int publish(PubMessage& message);
vpcola 25:326f00faa092 113
vpcola 25:326f00faa092 114 void addTopicHandler(const char * topic, void (*function)(MessageData &));
vpcola 25:326f00faa092 115 template<typename T>
vpcola 25:326f00faa092 116 void addTopicHandler(const char * topic, T *object, void (T::*member)(MessageData &))
vpcola 25:326f00faa092 117 {
vpcola 25:326f00faa092 118 FP<void,MessageData &> fp;
vpcola 25:326f00faa092 119 fp.attach(object, member);
vpcola 25:326f00faa092 120
vpcola 26:4b21de8043a5 121 topicCBMap.insert(std::pair<std::string, FP<void,MessageData &> >(std::string(topic),fp));
vpcola 25:326f00faa092 122 }
vpcola 25:326f00faa092 123
vpcola 27:c90092f35d79 124 // TODO: Add unsubscribe functionality.
vpcola 27:c90092f35d79 125
vpcola 26:4b21de8043a5 126 // Start the listener thread and start polling
vpcola 26:4b21de8043a5 127 // MQTT server.
vpcola 23:06fac173529e 128 void startListener();
vpcola 26:4b21de8043a5 129 // Stop the listerner thread and closes connection
vpcola 26:4b21de8043a5 130 void stopListener();
vpcola 23:06fac173529e 131
vpcola 23:06fac173529e 132 protected:
vpcola 26:4b21de8043a5 133
vpcola 23:06fac173529e 134 int handlePublishMsg();
vpcola 26:4b21de8043a5 135 void disconnect();
vpcola 26:4b21de8043a5 136 int connect();
vpcola 23:06fac173529e 137
vpcola 23:06fac173529e 138
vpcola 23:06fac173529e 139 private:
vpcola 23:06fac173529e 140 NetworkInterface * network;
vpcola 25:326f00faa092 141 const char * ssl_ca_pem;
vpcola 23:06fac173529e 142 TCPSocket * tcpSocket;
vpcola 23:06fac173529e 143 PacketId packetid;
vpcola 25:326f00faa092 144 const char *DRBG_PERS;
vpcola 25:326f00faa092 145 nsapi_error_t _error;
vpcola 25:326f00faa092 146 // Connection options
vpcola 25:326f00faa092 147 std::string host;
vpcola 25:326f00faa092 148 uint16_t port;
vpcola 25:326f00faa092 149 MQTTPacket_connectData connect_options;
vpcola 23:06fac173529e 150 // Event queue
vpcola 23:06fac173529e 151 EventQueue queue;
vpcola 23:06fac173529e 152 bool isConnected;
vpcola 26:4b21de8043a5 153 bool hasSavedSession;
vpcola 23:06fac173529e 154
vpcola 23:06fac173529e 155 // TODO: Because I'm using a map, I can only have one handler
vpcola 23:06fac173529e 156 // for each topic (one that's mapped to the topic string).
vpcola 23:06fac173529e 157 // Attaching another handler on the same topic is not possible.
vpcola 23:06fac173529e 158 // In the future, use a vector instead of maps to allow multiple
vpcola 23:06fac173529e 159 // handlers for the same topic.
vpcola 23:06fac173529e 160 std::map<std::string, FP<void, MessageData &> > topicCBMap;
vpcola 25:326f00faa092 161
vpcola 23:06fac173529e 162 unsigned char sendbuf[MAX_MQTT_PACKET_SIZE];
vpcola 23:06fac173529e 163 unsigned char readbuf[MAX_MQTT_PACKET_SIZE];
vpcola 23:06fac173529e 164
vpcola 23:06fac173529e 165 unsigned int keepAliveInterval;
vpcola 23:06fac173529e 166 Timer comTimer;
vpcola 23:06fac173529e 167
vpcola 25:326f00faa092 168 // SSL/TLS functions
vpcola 26:4b21de8043a5 169 bool useTLS;
vpcola 25:326f00faa092 170 void setupTLS();
vpcola 25:326f00faa092 171 int initTLS();
vpcola 25:326f00faa092 172 void freeTLS();
vpcola 25:326f00faa092 173 int doTLSHandshake();
vpcola 25:326f00faa092 174
vpcola 25:326f00faa092 175 int processSubscriptions();
vpcola 23:06fac173529e 176 int readPacket();
vpcola 23:06fac173529e 177 int sendPacket(size_t length);
vpcola 23:06fac173529e 178 int readPacketLength(int* value);
vpcola 23:06fac173529e 179 int readUntil(int packetType, int timeout);
vpcola 23:06fac173529e 180 int readBytesToBuffer(char * buffer, size_t size, int timeout);
vpcola 23:06fac173529e 181 int sendBytesFromBuffer(char * buffer, size_t size, int timeout);
vpcola 23:06fac173529e 182 bool isTopicMatched(char* topic, MQTTString& topicName);
vpcola 23:06fac173529e 183 int sendPublish(PubMessage& message);
vpcola 23:06fac173529e 184 void resetConnectionTimer();
vpcola 23:06fac173529e 185 void sendPingRequest();
vpcola 23:06fac173529e 186 bool hasConnectionTimedOut();
vpcola 26:4b21de8043a5 187 int login();
vpcola 23:06fac173529e 188 };
vpcola 23:06fac173529e 189
vpcola 23:06fac173529e 190 #endif