niMQTT

Dependencies:   EthernetInterface mbed-rtos

Fork of niMQTT by Guilhem Saurel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers niMQTT.h Source File

niMQTT.h

00001 #ifndef NIMQTT_H
00002 #define NIMQTT_H
00003 
00004 #include "mbed.h"
00005 #include "rtos.h"
00006 #include "EthernetInterface.h"
00007 
00008 enum { // Message Type
00009     ZERO,
00010     CONNECT_NUM,
00011     CONNACK_NUM,
00012     PUBLISH_NUM,
00013     PUBACK_NUM,
00014     PUBREC_NUM,
00015     PUBREL_NUM,
00016     PUBCOMP_NUM,
00017     SUBSCRIBE_NUM,
00018     SUBACK_NUM,
00019     UNSUBSCRIBE_NUM,
00020     UNSUBACK_NUM,
00021     PINGREQ_NUM,
00022     PINGRESP_NUM,
00023     DISCONNECT_NUM
00024 };
00025 
00026 #define CONNECT     CONNECT_NUM     << 4
00027 #define CONNACK     CONNACK_NUM     << 4
00028 #define PUBLISH     PUBLISH_NUM     << 4
00029 #define PUBACK      PUBACK_NUM      << 4
00030 #define PUBREC      PUBREC_NUM      << 4
00031 #define PUBREL      PUBREL_NUM      << 4
00032 #define PUBCOMP     PUBCOMP_NUM     << 4
00033 #define SUBSCRIBE   SUBSCRIBE_NUM   << 4
00034 #define SUBACK      SUBACK_NUM      << 4
00035 #define UNSUBSCRIBE UNSUBSCRIBE_NUM << 4
00036 #define UNSUBACK    UNSUBACK_NUM    << 4
00037 #define PINGREQ     PINGREQ_NUM     << 4
00038 #define PINGRESP    PINGRESP_NUM    << 4
00039 #define DISCONNECT  DISCONNECT_NUM  << 4
00040 
00041 enum { // QoS level
00042     MOST_ONCE_NUM,
00043     LEAST_ONCE_NUM,
00044     EXACTLY_ONCE_NUM
00045 };
00046 
00047 #define MOST_ONCE       MOST_ONCE_NUM       << 1
00048 #define LEAST_ONCE      LEAST_ONCE_NUM      << 1
00049 #define EXACTLY_ONCE    EXACTLY_ONCE_NUM    << 1
00050 
00051 #define KEEP_ALIVE 100 // seconds
00052 #define TIMEOUT 1000 // ms
00053 
00054 #define START_THREAD 1
00055 
00056 class niMQTT {
00057     public:
00058         /** Initialise and launch the MQTT Client
00059          * \param server the address of your server
00060          * \param callback a callback to execute on receiving a PUBLISH
00061          * \param id the id of this client (should be unique)
00062          * \param port the port of your server
00063          * \param username your username for the server
00064          * \param password your password for the server
00065          * \param debug get a more verbose output
00066          */
00067         niMQTT(char *server, void (*callback)(const char *, const char*), char *id="mbed", int port=1883, char *username="", char *password="", bool debug=false);
00068         void reconnect();
00069         ~niMQTT();
00070 
00071         /* Publish a message on a topic
00072          * \param topic the topic
00073          * \param message the message
00074          */
00075         int pub(char *topic, char *message);
00076 
00077         /** Subscribe to a topic
00078          * \param topic the topic
00079          */
00080         int sub(char *topic, bool unsub=false);
00081 
00082     protected:
00083         char *server;
00084         int port;
00085         char *id;
00086         void (*callback)(const char *, const char*);
00087         char *username;
00088         char *password;
00089 
00090         bool debug;
00091         bool connected;
00092         int message_id;
00093 
00094         TCPSocketConnection *socket;
00095 
00096         int remaining_length_length(int remaining_length);
00097         void get_remaining_length(int remaining_length, char *packet);
00098         int send(char *packet, int size);
00099 
00100         int decode_remaining_length();
00101         int recv();
00102 
00103         int init();
00104 
00105         int connect();
00106         int connack();
00107         int suback(bool unsub=false);
00108         int puback();
00109         int puback_received();
00110         int pingreq();
00111         int pingresp();
00112         int disconnect();
00113 
00114         static void ping_thread_starter(void const *p);
00115         static void recv_thread_starter(void const *p);
00116         void ping_thread_worker();
00117         void recv_thread_worker();
00118         Thread ping_thread;
00119         Thread recv_thread;
00120 
00121         void publish_received();
00122         virtual void call_callback(const char *topic, const char *message);
00123 
00124         bool waiting_new_packet, packet_sent;
00125         int waiting_connack, waiting_suback, waiting_pingresp;
00126 };
00127 
00128 #endif