MQTTClient

Dependents:   IoTGateway_Basic test

Committer:
SomeRandomBloke
Date:
Wed Mar 21 21:49:28 2012 +0000
Revision:
2:92b9dd336375
Parent:
0:260fb10c0755
Child:
3:ac326c5b065c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 2:92b9dd336375 1 /**
SomeRandomBloke 0:260fb10c0755 2 MQTTClient.cpp
SomeRandomBloke 0:260fb10c0755 3 Based on MQTTClient from http://ceit.uq.edu.au/content/mqttclient-mbed-version-20
SomeRandomBloke 0:260fb10c0755 4 A simple MQTT client for mbed, version 2.0
SomeRandomBloke 0:260fb10c0755 5 By Yilun FAN, @CEIT, @JAN 2011
SomeRandomBloke 0:260fb10c0755 6
SomeRandomBloke 0:260fb10c0755 7 Bug fixes and additions by Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:260fb10c0755 8
SomeRandomBloke 0:260fb10c0755 9 Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:260fb10c0755 10 of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:260fb10c0755 11 in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:260fb10c0755 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:260fb10c0755 13 copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:260fb10c0755 14 furnished to do so, subject to the following conditions:
SomeRandomBloke 0:260fb10c0755 15
SomeRandomBloke 0:260fb10c0755 16 The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:260fb10c0755 17 all copies or substantial portions of the Software.
SomeRandomBloke 0:260fb10c0755 18
SomeRandomBloke 0:260fb10c0755 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:260fb10c0755 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:260fb10c0755 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:260fb10c0755 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:260fb10c0755 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:260fb10c0755 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:260fb10c0755 25 THE SOFTWARE.
SomeRandomBloke 0:260fb10c0755 26 */
SomeRandomBloke 0:260fb10c0755 27
SomeRandomBloke 0:260fb10c0755 28 #ifndef MQTT_CLIENT_H
SomeRandomBloke 0:260fb10c0755 29 #define MQTT_CLIENT_H
SomeRandomBloke 0:260fb10c0755 30
SomeRandomBloke 0:260fb10c0755 31 #include "mbed.h"
SomeRandomBloke 0:260fb10c0755 32 #include "TCPSocket.h"
SomeRandomBloke 0:260fb10c0755 33
SomeRandomBloke 0:260fb10c0755 34 #define MQTTCONNECT 1<<4
SomeRandomBloke 0:260fb10c0755 35 #define MQTTCONNACK 2<<4
SomeRandomBloke 0:260fb10c0755 36 #define MQTTPUBLISH 3<<4
SomeRandomBloke 0:260fb10c0755 37 #define MQTTSUBSCRIBE 8<<4
SomeRandomBloke 0:260fb10c0755 38
SomeRandomBloke 0:260fb10c0755 39 #define MAX_PACKET_SIZE 128
SomeRandomBloke 0:260fb10c0755 40 #define KEEPALIVE 15000
SomeRandomBloke 0:260fb10c0755 41
SomeRandomBloke 2:92b9dd336375 42 /** MQTTClient class
SomeRandomBloke 2:92b9dd336375 43 *
SomeRandomBloke 2:92b9dd336375 44 */
SomeRandomBloke 0:260fb10c0755 45 class MQTTClient
SomeRandomBloke 0:260fb10c0755 46 {
SomeRandomBloke 0:260fb10c0755 47 public:
SomeRandomBloke 2:92b9dd336375 48
SomeRandomBloke 2:92b9dd336375 49 /** Default Constructor
SomeRandomBloke 2:92b9dd336375 50 */
SomeRandomBloke 0:260fb10c0755 51 MQTTClient();
SomeRandomBloke 2:92b9dd336375 52
SomeRandomBloke 2:92b9dd336375 53 /** Default Destructor
SomeRandomBloke 2:92b9dd336375 54 */
SomeRandomBloke 0:260fb10c0755 55 ~MQTTClient();
SomeRandomBloke 2:92b9dd336375 56
SomeRandomBloke 2:92b9dd336375 57 /** Alternative Constructor with parameters
SomeRandomBloke 2:92b9dd336375 58 *
SomeRandomBloke 2:92b9dd336375 59 * Allow object to be constructed with minimum parameters.
SomeRandomBloke 2:92b9dd336375 60 *
SomeRandomBloke 2:92b9dd336375 61 * @param server The IP address of the server to connect to
SomeRandomBloke 2:92b9dd336375 62 * @param port The TCP/IP port on the server to connect to
SomeRandomBloke 2:92b9dd336375 63 * @param callback Callback function to handle subscription to topics
SomeRandomBloke 2:92b9dd336375 64 */
SomeRandomBloke 2:92b9dd336375 65 MQTTClient(IpAddr server, int port, void (*callback)(char*, char*));
SomeRandomBloke 0:260fb10c0755 66 void init(IpAddr *server, int port, void (*callback)(char*, char*));
SomeRandomBloke 0:260fb10c0755 67 void init(IpAddr *server, int port, char *userName, char *password, void (*callback)(char*, char*));
SomeRandomBloke 0:260fb10c0755 68 int connect(char *);
SomeRandomBloke 0:260fb10c0755 69 void disconnect();
SomeRandomBloke 0:260fb10c0755 70 int publish(char *, char *);
SomeRandomBloke 0:260fb10c0755 71 int subscribe(char *);
SomeRandomBloke 0:260fb10c0755 72 void live();
SomeRandomBloke 0:260fb10c0755 73
SomeRandomBloke 0:260fb10c0755 74 private:
SomeRandomBloke 0:260fb10c0755 75 int open_session(char* id);
SomeRandomBloke 0:260fb10c0755 76 void read_open_session();
SomeRandomBloke 0:260fb10c0755 77 int send_data(const char* msg, int size);
SomeRandomBloke 0:260fb10c0755 78 void read_data();
SomeRandomBloke 0:260fb10c0755 79
SomeRandomBloke 0:260fb10c0755 80 char* clientId;
SomeRandomBloke 0:260fb10c0755 81 char* userName;
SomeRandomBloke 0:260fb10c0755 82 char *password;
SomeRandomBloke 0:260fb10c0755 83 Timer timer;
SomeRandomBloke 0:260fb10c0755 84 IpAddr serverIp;
SomeRandomBloke 0:260fb10c0755 85 int port;
SomeRandomBloke 0:260fb10c0755 86 bool connected;
SomeRandomBloke 0:260fb10c0755 87 bool sessionOpened;
SomeRandomBloke 0:260fb10c0755 88
SomeRandomBloke 0:260fb10c0755 89 void onTCPSocketEvent(TCPSocketEvent e);
SomeRandomBloke 0:260fb10c0755 90 TCPSocket* pTCPSocket;
SomeRandomBloke 0:260fb10c0755 91 Host host;
SomeRandomBloke 0:260fb10c0755 92
SomeRandomBloke 0:260fb10c0755 93 int lastActivity;
SomeRandomBloke 0:260fb10c0755 94 void (*callback_server)(char*, char*);
SomeRandomBloke 0:260fb10c0755 95 };
SomeRandomBloke 0:260fb10c0755 96
SomeRandomBloke 0:260fb10c0755 97 #endif