An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   Cellular_HelloMQTT IoTStarterKit GSwifiInterface_HelloMQTT IBMIoTClientEthernetExample ... more

This library is part of the EclipseTM Paho project; specifically the embedded client.

The goals of this API are:

  1. to be independent of any system library: hence templates parameters for networking, timer and threading classes
  2. not to rely on heap storage, only automatic (I think this is a good thing)
  3. to limit memory use, for instance by defining the size of the buffers and arrays used at object creation time

Files at this revision

API Documentation at this revision

Comitter:
icraggs
Date:
Fri Mar 28 13:39:25 2014 +0000
Parent:
1:1a6016a0cf66
Child:
3:dbff6b768d28
Commit message:
Structure of API

Changed in this revision

MQTT.cpp Show diff for this revision Revisions of this file
MQTT.h Show diff for this revision Revisions of this file
MQTTClient.cpp Show annotated file Show diff for this revision Revisions of this file
MQTTClient.h Show annotated file Show diff for this revision Revisions of this file
MQTTConnection.cpp Show diff for this revision Revisions of this file
MQTTConnection.h Show diff for this revision Revisions of this file
MQTTPubSub.cpp Show diff for this revision Revisions of this file
MQTTPubSub.h Show diff for this revision Revisions of this file
--- a/MQTT.cpp	Sat Mar 08 00:36:11 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-/**
- * @file    MQTT.cpp
- * @brief   API - for MQTT
- * @author  
- * @version 1.0
- * @see     
- *
- * Copyright (c) 2014
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 
-#include "MQTT.h"
-
--- a/MQTT.h	Sat Mar 08 00:36:11 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-/**
- * @file    MQTT.h
- * @brief   API - for MQTT
- * @author  
- * @version 1.0
- * @see     
- *
- * Copyright (c) 2014
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef MQTT_H
-#define MQTT_H
-
-/** Example using the MQTT API class
- * @code
- *  #include "mbed.h"
- *  #include "MQTT.h"
- *  
- *  DigitalOut myled(LED1);
- *  
- *  int main()
- *  {
- *      while(1) 
- *      {
- *          myled = 1;
- *          wait(0.2);
- *          myled = 0;
- *          wait(0.2);
- *      }
- *  }
- * @endcode
- */
-
-#include "FP.h"
-#include "MQTTPubSub.h"
-#include "mbed.h"
-
-typedef struct MQTTInfo MQTTInfo;
-struct MQTTInfo
-{
-    char *topic;
-    char *payload;
-    int   length;
-};
-
-class MQTT : public MQTTPubSub
-{
-public:
-    MQTT(){}
-    
-    char *mqttStream(void){return 0;}
-    int   mqttStreamLength(void){return 0;}
-    
-    FP <void,void*>callback;
-};
-
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTClient.cpp	Fri Mar 28 13:39:25 2014 +0000
@@ -0,0 +1,61 @@
+/**
+ * @file    MQTTPubSub.cpp
+ * @brief   API - for MQTT
+ * @author  
+ * @version 1.0
+ * @see     
+ *
+ * Copyright (c) 2014
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ 
+#include "MQTTClient.h"
+#include "MQTTPacket.h"
+
+MQTTClient::MQTTClient(char* serverURI, char* clientId, const int buffer_size)
+{
+    
+   buf = new char[buffer_size];
+   this->clientId = clientId;
+   this->serverURI = serverURI;
+}
+
+
+int MQTTClient::connect(MQTTConnectOptions* options, FP<void, MQTTResult*> resultHandler)
+{
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+     
+    data.clientID.cstring = clientId;
+    if (options)
+    {
+        data.keepAliveInterval = options->keepAliveInterval;
+        data.cleansession = options->cleansession;
+        data.username.cstring = options->username;
+        data.password.cstring = options->password;
+    }
+     
+    int len = MQTTSerialize_connect(buf, buflen, &data);
+    
+    sendPacket(buf, buflen); // send the connect packet
+    
+    /* how to make this check work?
+    if (resultHandler == None)
+    {
+        // this will be a blocking call, wait for the connack
+        
+    }*/
+    
+    return len;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTClient.h	Fri Mar 28 13:39:25 2014 +0000
@@ -0,0 +1,92 @@
+/**
+ * @file    MQTTPubSub.h
+ * @brief   API - for MQTT
+ * @author  
+ * @version 1.0
+ * @see     
+ *
+ * Copyright (c) 2014
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if !defined(MQTTCLIENT_H)
+#define MQTTCLIENT_H
+
+#include <vector>
+
+#include "mbed.h"
+#include "FP.h"
+
+class MQTTClient;
+
+enum QoS { QOS0, QOS1, QOS2 };
+
+class MQTTResult
+{
+    /* success or failure result data */
+    MQTTClient* client;
+};
+
+struct MQTTMessage
+{
+    enum QoS qos;
+    bool retained;
+    bool dup;
+    unsigned short msgid;
+    void *payload;
+    size_t payloadlen;
+};
+
+struct MQTTConnectOptions
+{
+    unsigned short keepAliveInterval;
+    bool cleansession;
+    char* username;
+    char* password;
+    int timeout;
+    std::vector<char*> serverURIs;
+};
+
+  
+class MQTTClient
+{
+    
+public:    
+
+    static FP<void, MQTTResult*> None;   // default argument of no result handler to indicate call should be blocking
+    
+    MQTTClient(char* serverURI, char* clientId = "", const int buffer_size = 100); 
+       
+    int connect(MQTTConnectOptions* options = 0, FP<void, MQTTResult*> resultHandler = None);
+        
+    int publish(char* topic, MQTTMessage* message, FP<void, MQTTResult*> resultHandler = None);
+    
+    int subscribe(char* topicFilter, int qos, FP<void, MQTTMessage*> messageHandler, FP<void, MQTTResult*> resultHandler = None);
+    
+    int unsubscribe(char* topicFilter, FP<void, MQTTResult*> resultHandler = None);
+    
+    int disconnect(int timeout, FP<void, MQTTResult*> resultHandler = None);
+    
+private:
+
+    int sendPacket(char* buf, int buflen);
+
+    char* clientId;
+    char* serverURI;
+    char* buf;
+    int buflen;
+    
+};
+
+#endif
--- a/MQTTConnection.cpp	Sat Mar 08 00:36:11 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-/**
- * @file    MQTTConnection.cpp
- * @brief   API - for MQTT
- * @author  
- * @version 1.0
- * @see     
- *
- * Copyright (c) 2014
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 
-#include "MQTTConnection.h"
-
--- a/MQTTConnection.h	Sat Mar 08 00:36:11 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-/**
- * @file    MQTTConnection.h
- * @brief   API - for MQTT
- * @author  
- * @version 1.0
- * @see     
- *
- * Copyright (c) 2014
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 
-#ifndef MQTTCONNECTION_H
-#define MQTTCONNECTION_H
-
-#include "MQTTPacket.h"
-
-class MQTTConnect
-{
-    MQTTPacket_connectData connect_data;
-    char *host;
-    int   port;
-    char *serialize_buffer;
-};
-
-#endif
--- a/MQTTPubSub.cpp	Sat Mar 08 00:36:11 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-/**
- * @file    MQTTPubSub.cpp
- * @brief   API - for MQTT
- * @author  
- * @version 1.0
- * @see     
- *
- * Copyright (c) 2014
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 
-#include "MQTTPubSub.h"
-
--- a/MQTTPubSub.h	Sat Mar 08 00:36:11 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/**
- * @file    MQTTPubSub.h
- * @brief   API - for MQTT
- * @author  
- * @version 1.0
- * @see     
- *
- * Copyright (c) 2014
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef MQTTPUBSUB_H
-#define MQTTPUBSUB_H
-
-#include "MQTTConnection.h"
-
-class MQTTTopic
-{
-    MQTTString topic;
-    char *msg_buffer;
-    int   msg_size;
-};
-
-class MQTTPubSub : private MQTTConnect
-{
-private:
-    MQTTTopic topic;
-
-public:    
-    enum {
-        TOPIC_PUBLISH, TOPIC_SUBSCRIBE
-    }Message;
-    
-    MQTTPubSub(){}
-    
-    void publish(){}
-    void subscribe(){}    
-};
-
-#endif