An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
icraggs
Date:
Mon Mar 31 15:48:45 2014 +0000
Revision:
3:dbff6b768d28
Parent:
2:dcfdd2abfe71
Child:
4:4ef00243708e
Move parameters around to avoid storing data

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 3:dbff6b768d28 30 #include "MQTTPacket.h"
icraggs 3:dbff6b768d28 31 #include "include_me.h"
icraggs 2:dcfdd2abfe71 32
icraggs 3:dbff6b768d28 33 namespace MQTT
icraggs 3:dbff6b768d28 34 {
icraggs 3:dbff6b768d28 35
icraggs 3:dbff6b768d28 36 class Client;
icraggs 2:dcfdd2abfe71 37
icraggs 2:dcfdd2abfe71 38 enum QoS { QOS0, QOS1, QOS2 };
sam_grove 0:fe461e4d7afe 39
icraggs 3:dbff6b768d28 40 class Result
sam_grove 0:fe461e4d7afe 41 {
icraggs 2:dcfdd2abfe71 42 /* success or failure result data */
icraggs 3:dbff6b768d28 43 Client* client;
icraggs 2:dcfdd2abfe71 44 };
icraggs 2:dcfdd2abfe71 45
icraggs 3:dbff6b768d28 46 struct Message
icraggs 2:dcfdd2abfe71 47 {
icraggs 2:dcfdd2abfe71 48 enum QoS qos;
icraggs 2:dcfdd2abfe71 49 bool retained;
icraggs 2:dcfdd2abfe71 50 bool dup;
icraggs 2:dcfdd2abfe71 51 unsigned short msgid;
icraggs 2:dcfdd2abfe71 52 void *payload;
icraggs 2:dcfdd2abfe71 53 size_t payloadlen;
sam_grove 0:fe461e4d7afe 54 };
sam_grove 0:fe461e4d7afe 55
icraggs 2:dcfdd2abfe71 56
icraggs 3:dbff6b768d28 57 class Client
icraggs 2:dcfdd2abfe71 58 {
icraggs 2:dcfdd2abfe71 59
sam_grove 0:fe461e4d7afe 60 public:
icraggs 2:dcfdd2abfe71 61
icraggs 3:dbff6b768d28 62 static FP<void, Result*> None; // default argument of no result handler to indicate call should be blocking
icraggs 2:dcfdd2abfe71 63
icraggs 3:dbff6b768d28 64 Client(IPStack* ipstack, const int buffer_size = 100);
icraggs 2:dcfdd2abfe71 65
icraggs 3:dbff6b768d28 66 int connect(MQTTPacket_connectData* options = 0, FP<void, Result*> resultHandler = None);
icraggs 2:dcfdd2abfe71 67
icraggs 3:dbff6b768d28 68 int publish(char* topic, Message* message, FP<void, Result*> resultHandler = None);
icraggs 2:dcfdd2abfe71 69
icraggs 3:dbff6b768d28 70 int subscribe(char* topicFilter, int qos, FP<void, Message*> messageHandler, FP<void, Result*> resultHandler = None);
sam_grove 0:fe461e4d7afe 71
icraggs 3:dbff6b768d28 72 int unsubscribe(char* topicFilter, FP<void, Result*> resultHandler = None);
icraggs 2:dcfdd2abfe71 73
icraggs 3:dbff6b768d28 74 int disconnect(int timeout, FP<void, Result*> resultHandler = None);
sam_grove 0:fe461e4d7afe 75
icraggs 2:dcfdd2abfe71 76 private:
icraggs 2:dcfdd2abfe71 77
icraggs 3:dbff6b768d28 78 void cycle();
icraggs 2:dcfdd2abfe71 79
icraggs 3:dbff6b768d28 80 int decodePacket(int* value, int timeout);
icraggs 3:dbff6b768d28 81 int readPacket(char* buf, int buflen, int timeout = -1);
icraggs 3:dbff6b768d28 82 int sendPacket(int length);
icraggs 3:dbff6b768d28 83
icraggs 3:dbff6b768d28 84 IPStack* ipstack;
icraggs 2:dcfdd2abfe71 85 char* buf;
icraggs 2:dcfdd2abfe71 86 int buflen;
icraggs 2:dcfdd2abfe71 87
sam_grove 0:fe461e4d7afe 88 };
sam_grove 0:fe461e4d7afe 89
icraggs 3:dbff6b768d28 90 }
icraggs 3:dbff6b768d28 91
sam_grove 0:fe461e4d7afe 92 #endif