Connecting a Multi-Tech Systems Dragonfly™ to Twilio's Sync for IoT Quickstart. Blink a dev board LED.

Dependencies:   MQTT MbedJSONValue mbed mtsas

Fork of DragonflyMQTT by miao zhicheng

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TlsMQTTClient.hpp Source File

TlsMQTTClient.hpp

00001 #pragma once
00002 
00003 #include <ssl.h>
00004 #include <mbed.h>
00005 #include <mtsas.h>
00006 #include <TCPSocketConnection.h>
00007 #include <MQTTClient.h>
00008 #include <MQTTmbed.h>
00009 
00010 class TlsMQTTClient {
00011 public:
00012     TlsMQTTClient();
00013     ~TlsMQTTClient();
00014 
00015 private:
00016     typedef MQTT::Client<
00017         TlsMQTTClient, 
00018         Countdown, 
00019         1024 /* MAX_MQTT_PACKET_SIZE */
00020     > MQTTClient;
00021 
00022     // MQTT Operations
00023 public:
00024     /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
00025      *  The nework object must be connected to the network endpoint before calling this
00026      *  @param host - host of the mqtt server
00027      *  @param port - port of the mqtt server
00028      *  @param certificates - CA certs to be verified, can be NULL to bypass CA checking(not recommended)
00029      *  @param options - connect options
00030      *  @return success code -
00031      */
00032     int connect(
00033         const char* host, 
00034         const int port,
00035         const char* certificates,
00036         MQTTPacket_connectData& options
00037     );
00038 
00039     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
00040      *  @param topic - the topic to publish to
00041      *  @param message - the message to send
00042      *  @return success code -
00043      */
00044     int publish(
00045         const char* topicName, 
00046         MQTT::Message& message
00047     );
00048    
00049     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
00050      *  @param topic - the topic to publish to
00051      *  @param payload - the data to send
00052      *  @param payloadlen - the length of the data
00053      *  @param qos - the QoS to send the publish at
00054      *  @param retained - whether the message should be retained
00055      *  @return success code -
00056      */
00057     int publish(
00058         const char* topicName, 
00059         void* payload, 
00060         size_t payloadlen, 
00061         enum MQTT::QoS qos = MQTT::QOS0, 
00062         bool retained = false
00063     );
00064   
00065     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
00066      *  @param topic - the topic to publish to
00067      *  @param payload - the data to send
00068      *  @param payloadlen - the length of the data
00069      *  @param id - the packet id used - returned 
00070      *  @param qos - the QoS to send the publish at
00071      *  @param retained - whether the message should be retained
00072      *  @return success code -
00073      */
00074     int publish(
00075         const char* topicName, 
00076         void* payload, 
00077         size_t payloadlen, 
00078         unsigned short& id, 
00079         enum MQTT::QoS qos = MQTT::QOS1, 
00080         bool retained = false
00081     );
00082 
00083     typedef void (*MessageHandler)(MQTT::MessageData&);
00084 
00085     /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
00086      *  @param topicFilter - a topic pattern which can include wildcards
00087      *  @param qos - the MQTT QoS to subscribe at
00088      *  @param mh - the callback function to be invoked when a message is received for this subscription
00089      *  @return success code -
00090      */
00091     int subscribe(
00092         const char* topicFilter, 
00093         enum MQTT::QoS qos, 
00094         MessageHandler mh
00095     );
00096 
00097     /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
00098      *  @param topicFilter - a topic pattern which can include wildcards
00099      *  @return success code -
00100      */
00101     int unsubscribe(const char* topicFilter);
00102 
00103     /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state
00104      *  @return success code -
00105      */
00106     int disconnect();
00107 
00108     /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive
00109      *  yield can be called if no other MQTT operation is needed.  This will also allow messages to be
00110      *  received.
00111      *  @param timeout_ms the time to wait, in milliseconds
00112      *  @return success code - on failure, this means the client has disconnected
00113      */
00114     int yield(unsigned long timeout_ms = 1000L);
00115 
00116     /** Is the client connected?
00117      *  @return flag - is the client connected or not?
00118      */
00119     bool isConnected();
00120 
00121     // Network interface for MQTT::Client
00122     int read(unsigned char* data, int max, int timeout = -1);
00123     int write(const unsigned char* data, int length, int timeout = -1);
00124 
00125 private:
00126     void cleanupTransport();
00127     static int ioRecv(CYASSL* ssl, char *buf, int sz, void *ctx);
00128     static int ioSend(CYASSL* ssl, char *buf, int sz, void *ctx);
00129     TCPSocketConnection* tcp;
00130     CYASSL_CTX* ctx;
00131     CYASSL *ssl;
00132     MQTTClient* mqttClient;
00133 };