MQTT

Dependencies:   MQTTPacket FP

Committer:
icraggs
Date:
Fri Mar 28 13:39:25 2014 +0000
Revision:
2:dcfdd2abfe71
Parent:
MQTTPubSub.h@0:fe461e4d7afe
Child:
3:dbff6b768d28
Structure of API

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:fe461e4d7afe 1 /**
sam_grove 0:fe461e4d7afe 2 * @file MQTTPubSub.h
sam_grove 0:fe461e4d7afe 3 * @brief API - for MQTT
sam_grove 0:fe461e4d7afe 4 * @author
sam_grove 0:fe461e4d7afe 5 * @version 1.0
sam_grove 0:fe461e4d7afe 6 * @see
sam_grove 0:fe461e4d7afe 7 *
sam_grove 0:fe461e4d7afe 8 * Copyright (c) 2014
sam_grove 0:fe461e4d7afe 9 *
sam_grove 0:fe461e4d7afe 10 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:fe461e4d7afe 11 * you may not use this file except in compliance with the License.
sam_grove 0:fe461e4d7afe 12 * You may obtain a copy of the License at
sam_grove 0:fe461e4d7afe 13 *
sam_grove 0:fe461e4d7afe 14 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:fe461e4d7afe 15 *
sam_grove 0:fe461e4d7afe 16 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:fe461e4d7afe 17 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:fe461e4d7afe 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:fe461e4d7afe 19 * See the License for the specific language governing permissions and
sam_grove 0:fe461e4d7afe 20 * limitations under the License.
sam_grove 0:fe461e4d7afe 21 */
sam_grove 0:fe461e4d7afe 22
icraggs 2:dcfdd2abfe71 23 #if !defined(MQTTCLIENT_H)
icraggs 2:dcfdd2abfe71 24 #define MQTTCLIENT_H
icraggs 2:dcfdd2abfe71 25
icraggs 2:dcfdd2abfe71 26 #include <vector>
sam_grove 0:fe461e4d7afe 27
icraggs 2:dcfdd2abfe71 28 #include "mbed.h"
icraggs 2:dcfdd2abfe71 29 #include "FP.h"
icraggs 2:dcfdd2abfe71 30
icraggs 2:dcfdd2abfe71 31 class MQTTClient;
icraggs 2:dcfdd2abfe71 32
icraggs 2:dcfdd2abfe71 33 enum QoS { QOS0, QOS1, QOS2 };
sam_grove 0:fe461e4d7afe 34
icraggs 2:dcfdd2abfe71 35 class MQTTResult
sam_grove 0:fe461e4d7afe 36 {
icraggs 2:dcfdd2abfe71 37 /* success or failure result data */
icraggs 2:dcfdd2abfe71 38 MQTTClient* client;
icraggs 2:dcfdd2abfe71 39 };
icraggs 2:dcfdd2abfe71 40
icraggs 2:dcfdd2abfe71 41 struct MQTTMessage
icraggs 2:dcfdd2abfe71 42 {
icraggs 2:dcfdd2abfe71 43 enum QoS qos;
icraggs 2:dcfdd2abfe71 44 bool retained;
icraggs 2:dcfdd2abfe71 45 bool dup;
icraggs 2:dcfdd2abfe71 46 unsigned short msgid;
icraggs 2:dcfdd2abfe71 47 void *payload;
icraggs 2:dcfdd2abfe71 48 size_t payloadlen;
sam_grove 0:fe461e4d7afe 49 };
sam_grove 0:fe461e4d7afe 50
icraggs 2:dcfdd2abfe71 51 struct MQTTConnectOptions
sam_grove 0:fe461e4d7afe 52 {
icraggs 2:dcfdd2abfe71 53 unsigned short keepAliveInterval;
icraggs 2:dcfdd2abfe71 54 bool cleansession;
icraggs 2:dcfdd2abfe71 55 char* username;
icraggs 2:dcfdd2abfe71 56 char* password;
icraggs 2:dcfdd2abfe71 57 int timeout;
icraggs 2:dcfdd2abfe71 58 std::vector<char*> serverURIs;
icraggs 2:dcfdd2abfe71 59 };
sam_grove 0:fe461e4d7afe 60
icraggs 2:dcfdd2abfe71 61
icraggs 2:dcfdd2abfe71 62 class MQTTClient
icraggs 2:dcfdd2abfe71 63 {
icraggs 2:dcfdd2abfe71 64
sam_grove 0:fe461e4d7afe 65 public:
icraggs 2:dcfdd2abfe71 66
icraggs 2:dcfdd2abfe71 67 static FP<void, MQTTResult*> None; // default argument of no result handler to indicate call should be blocking
icraggs 2:dcfdd2abfe71 68
icraggs 2:dcfdd2abfe71 69 MQTTClient(char* serverURI, char* clientId = "", const int buffer_size = 100);
icraggs 2:dcfdd2abfe71 70
icraggs 2:dcfdd2abfe71 71 int connect(MQTTConnectOptions* options = 0, FP<void, MQTTResult*> resultHandler = None);
icraggs 2:dcfdd2abfe71 72
icraggs 2:dcfdd2abfe71 73 int publish(char* topic, MQTTMessage* message, FP<void, MQTTResult*> resultHandler = None);
icraggs 2:dcfdd2abfe71 74
icraggs 2:dcfdd2abfe71 75 int subscribe(char* topicFilter, int qos, FP<void, MQTTMessage*> messageHandler, FP<void, MQTTResult*> resultHandler = None);
sam_grove 0:fe461e4d7afe 76
icraggs 2:dcfdd2abfe71 77 int unsubscribe(char* topicFilter, FP<void, MQTTResult*> resultHandler = None);
icraggs 2:dcfdd2abfe71 78
icraggs 2:dcfdd2abfe71 79 int disconnect(int timeout, FP<void, MQTTResult*> resultHandler = None);
sam_grove 0:fe461e4d7afe 80
icraggs 2:dcfdd2abfe71 81 private:
icraggs 2:dcfdd2abfe71 82
icraggs 2:dcfdd2abfe71 83 int sendPacket(char* buf, int buflen);
icraggs 2:dcfdd2abfe71 84
icraggs 2:dcfdd2abfe71 85 char* clientId;
icraggs 2:dcfdd2abfe71 86 char* serverURI;
icraggs 2:dcfdd2abfe71 87 char* buf;
icraggs 2:dcfdd2abfe71 88 int buflen;
icraggs 2:dcfdd2abfe71 89
sam_grove 0:fe461e4d7afe 90 };
sam_grove 0:fe461e4d7afe 91
sam_grove 0:fe461e4d7afe 92 #endif