An MQTT Client for the new etherNet Interface.
Dependencies: EthernetInterface mbed-rtos
Dependents: AV_MQTT niMQTT_example
Diff: niMQTT.h
- Revision:
- 0:d4dfed20c6ea
- Child:
- 1:4faa96fa4350
diff -r 000000000000 -r d4dfed20c6ea niMQTT.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/niMQTT.h Wed Aug 07 12:57:21 2013 +0000 @@ -0,0 +1,123 @@ +#ifndef NIMQTT_H +#define NIMQTT_H + +#include "mbed.h" +#include "rtos.h" +#include "EthernetInterface.h" + +enum { // Message Type + ZERO, + CONNECT_NUM, + CONNACK_NUM, + PUBLISH_NUM, + PUBACK_NUM, + PUBREC_NUM, + PUBREL_NUM, + PUBCOMP_NUM, + SUBSCRIBE_NUM, + SUBACK_NUM, + UNSUBSCRIBE_NUM, + UNSUBACK_NUM, + PINGREQ_NUM, + PINGRESP_NUM, + DISCONNECT_NUM +}; + +#define CONNECT CONNECT_NUM << 4 +#define CONNACK CONNACK_NUM << 4 +#define PUBLISH PUBLISH_NUM << 4 +#define PUBACK PUBACK_NUM << 4 +#define PUBREC PUBREC_NUM << 4 +#define PUBREL PUBREL_NUM << 4 +#define PUBCOMP PUBCOMP_NUM << 4 +#define SUBSCRIBE SUBSCRIBE_NUM << 4 +#define SUBACK SUBACK_NUM << 4 +#define UNSUBSCRIBE UNSUBSCRIBE_NUM << 4 +#define UNSUBACK UNSUBACK_NUM << 4 +#define PINGREQ PINGREQ_NUM << 4 +#define PINGRESP PINGRESP_NUM << 4 +#define DISCONNECT DISCONNECT_NUM << 4 + +enum { // QoS level + MOST_ONCE_NUM, + LEAST_ONCE_NUM, + EXACTLY_ONCE_NUM +}; + +#define MOST_ONCE MOST_ONCE_NUM << 1 +#define LEAST_ONCE LEAST_ONCE_NUM << 1 +#define EXACTLY_ONCE EXACTLY_ONCE_NUM << 1 + +#define KEEP_ALIVE 300 // seconds +#define TIMEOUT 1000 // ms + +#define START_THREAD 1 + +class niMQTT { + public: + /** Initialise and launch the MQTT Client + * \param server the address of your server + * \param port the port of your server + * \param id the id of this client (should be unique) + * \param callback a callback to execute on receiving a PUBLISH + * \param username your username for the server + * \param password your password for the server + * \param debug get a more verbose output + */ + niMQTT(char *server, int port=1884, char *id="mbed", void (*callback)(char *, char*), char *username="", char *password="", bool debug=false); + ~niMQTT(); + + /* Publish a message on a topic + * \param topic the topic + * \param message the message + */ + int pub(char *topic, char *message); + + /** Subscribe to a topic + * \param topic the topic + */ + int sub(char *topic, bool unsub=false); + + private: + char *server; + int port; + char *id; + void (*callback)(char *, char*); + char *username; + char *password; + + bool debug; + bool connected; + int message_id; + + TCPSocketConnection *socket; + + int remaining_length_length(int remaining_length); + void get_remaining_length(int remaining_length, char *packet); + int send(char *packet, int size); + + int decode_remaining_length(); + int recv(); + + int init(); + + int connect(); + int connack(); + int suback(bool unsub=false); + int puback(); + int puback_received(); + int pingreq(); + int pingresp(); + int disconnect(); + + static void thread_starter(void const *p); + void thread_worker(); + Thread thread; + + void publish_received(); + + bool waiting_new_packet, packet_sent; + int waiting_connack, waiting_suback, waiting_pingresp; +}; + +#endif