MQTTClient

Dependencies:   mbed-http

Revision:
1:a50b6c866a3b
Parent:
0:260fb10c0755
Child:
2:92b9dd336375
--- a/MQTTClient.cpp	Tue Mar 20 20:53:22 2012 +0000
+++ b/MQTTClient.cpp	Wed Mar 21 21:43:02 2012 +0000
@@ -27,6 +27,22 @@
 
 #include "MQTTClient.h"
 
+/** Default Constructor
+ */
+MQTTClient::MQTTClient() {}
+
+/** Default Destructor
+ */
+MQTTClient::~MQTTClient() {}
+
+/** Constructor with parameters
+ * 
+ * Allow object to be constructed with minimum parameters. 
+ *
+ * @param server The IP address of the server to connect to
+ * @param port   The TCP/IP port on the server to connect to
+ * @param callback Callback function to handle subscription to topics
+ */
 MQTTClient::MQTTClient(IpAddr server, int port, void (*callback)(char*, char*)) {
     this->port = port;
     callback_server = callback;
@@ -36,11 +52,15 @@
     timer.start();
 }
 
-MQTTClient::MQTTClient() {}
 
-MQTTClient::~MQTTClient() {}
-
-
+/** MQTT initialisation method
+ * 
+ * Used when default constructor used and need to specify parameters at runtime
+ *
+ * @param server The IP address of the server to connect to
+ * @param port   The TCP/IP port on the server to connect to
+ * @param callback Callback function to handle subscription to topics
+ */
 void MQTTClient::init(IpAddr *server, int port, void (*callback)(char*, char*)) {
     this->port = port;
     callback_server = callback;
@@ -52,6 +72,17 @@
     timer.start();
 }
 
+/** A brief description of the function foo
+ * 
+ * More details about the function goes here
+ * and here
+ *
+ * @param server The IP address of the server to connect to
+ * @param port   The TCP/IP port on the server to connect to
+ * @param userName Pointer to username string, zero terminated. Null for not used
+ * @param password Pointer to password string, zero terminated. Null for not used
+ * @param callback Callback function to handle subscription to topics
+ */
 void MQTTClient::init(IpAddr *server, int port, char *userName, char *password, void (*callback)(char*, char*)) {
     this->port = port;
     callback_server = callback;
@@ -63,6 +94,12 @@
     timer.start();
 }
 
+/** Send a message of specified size
+ * 
+ * @param msg  message string
+ * @param size Size of the message to send
+ * @returns value to indicate message sent successfully or not, -1 for error, 1 for success.
+ */
 int MQTTClient::send_data(const char* msg, int size) {
     int transLen = pTCPSocket->send(msg, size);
 
@@ -77,6 +114,11 @@
     return 1;
 }
 
+/** Start a MQTT session, build CONNECT packet
+ * 
+ * @param id The client name shown on MQTT server.
+ * @returns -1 for error, 1 for success   
+ */
 int MQTTClient::open_session(char* id) {
     /*variable header*/
     char var_header[] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03,0x02,0x00,KEEPALIVE/500,0x00,strlen(id)};
@@ -121,6 +163,11 @@
     return 1;
 }
 
+/** Open TCP port, connect to server on given IP address.
+ * 
+ * @param id The client name shown on MQTT server.
+ * @returns -1: If connect to server failed. -2: Failed to open session on server.  1: Connection accessed.  
+ */
 int MQTTClient::connect(char* id) {
     clientId = id;
 
@@ -174,6 +221,12 @@
     return 1;
 }
 
+/** Publish a message on a topic.
+ *
+ * @param pub_topic  The topic name the massage will be publish on.
+ * @param msg        The massage to be published.
+ * @returns  -1: Failed to publish message. 1: Publish sucessed.  
+ */
 int MQTTClient::publish(char* pub_topic, char* msg) {
     uint8_t var_header_pub[strlen(pub_topic)+3];
     strcpy((char *)&var_header_pub[2], pub_topic);
@@ -195,7 +248,8 @@
     return 1;
 }
 
-
+/** Disconnect from server
+ */
 void MQTTClient::disconnect() {
     char packet_224[] = {0xe0, 0x00};
     send_data((char*)packet_224, 2);
@@ -203,6 +257,9 @@
     connected = false;
 }
 
+/** Read data from receive packet
+ * Determine what needs to be done with packet. 
+ */
 void MQTTClient::read_data() {
     char buffer[1024];
     int len = 0, readLen;
@@ -257,6 +314,8 @@
     }
 }
 
+/** Check for session opened
+ */
 void MQTTClient::read_open_session() {
     char buffer[32];
     int len = 0, readLen;
@@ -271,6 +330,11 @@
     }
 }
 
+/** Subscribe to a topic
+ * 
+ * @param topic The topic name to be subscribed.
+ * @returns  -1: Failed to subscribe to topic. 1: Subscribe sucessed.  
+ */
 int MQTTClient::subscribe(char* topic) {
 
     if (connected) {
@@ -299,6 +363,9 @@
     return -1;
 }
 
+/** Send heartbeat/keep-alive message
+ * 
+ */
 void MQTTClient::live() {
     if (connected) {
         int t = timer.read_ms();
@@ -312,6 +379,10 @@
     }
 }
 
+/** TCP Socket event handling
+ *
+ * @param e  Event object
+ */
 void MQTTClient::onTCPSocketEvent(TCPSocketEvent e) {
     switch (e) {
         case TCPSOCKET_ACCEPT: