USE YHTANG AWS ACCOUNT TO TEST MQTT
Dependents: NuMaker-mbed-AWS-IoT-example
MQTTClient.h@6:4d312a49200b, 2014-04-08 (annotated)
- Committer:
- icraggs
- Date:
- Tue Apr 08 22:54:37 2014 +0000
- Revision:
- 6:4d312a49200b
- Parent:
- 5:389ccac5a50c
- Child:
- 8:c46930bd6c82
Subscribing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 6:4d312a49200b | 1 | /******************************************************************************* |
icraggs | 6:4d312a49200b | 2 | * Copyright (c) 2014 IBM Corp. |
sam_grove | 0:fe461e4d7afe | 3 | * |
icraggs | 6:4d312a49200b | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 6:4d312a49200b | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 6:4d312a49200b | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
sam_grove | 0:fe461e4d7afe | 7 | * |
icraggs | 6:4d312a49200b | 8 | * The Eclipse Public License is available at |
icraggs | 6:4d312a49200b | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 6:4d312a49200b | 10 | * and the Eclipse Distribution License is available at |
icraggs | 6:4d312a49200b | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
sam_grove | 0:fe461e4d7afe | 12 | * |
icraggs | 6:4d312a49200b | 13 | * Contributors: |
icraggs | 6:4d312a49200b | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 6:4d312a49200b | 15 | *******************************************************************************/ |
sam_grove | 0:fe461e4d7afe | 16 | |
icraggs | 2:dcfdd2abfe71 | 17 | #if !defined(MQTTCLIENT_H) |
icraggs | 2:dcfdd2abfe71 | 18 | #define MQTTCLIENT_H |
icraggs | 2:dcfdd2abfe71 | 19 | |
icraggs | 2:dcfdd2abfe71 | 20 | #include "FP.h" |
icraggs | 3:dbff6b768d28 | 21 | #include "MQTTPacket.h" |
icraggs | 6:4d312a49200b | 22 | #include "stdio.h" |
icraggs | 2:dcfdd2abfe71 | 23 | |
icraggs | 3:dbff6b768d28 | 24 | namespace MQTT |
icraggs | 3:dbff6b768d28 | 25 | { |
icraggs | 6:4d312a49200b | 26 | |
icraggs | 6:4d312a49200b | 27 | const int MAX_PACKET_ID = 65535; |
icraggs | 3:dbff6b768d28 | 28 | |
icraggs | 2:dcfdd2abfe71 | 29 | |
icraggs | 2:dcfdd2abfe71 | 30 | enum QoS { QOS0, QOS1, QOS2 }; |
sam_grove | 0:fe461e4d7afe | 31 | |
icraggs | 2:dcfdd2abfe71 | 32 | |
icraggs | 3:dbff6b768d28 | 33 | struct Message |
icraggs | 2:dcfdd2abfe71 | 34 | { |
icraggs | 2:dcfdd2abfe71 | 35 | enum QoS qos; |
icraggs | 2:dcfdd2abfe71 | 36 | bool retained; |
icraggs | 2:dcfdd2abfe71 | 37 | bool dup; |
icraggs | 2:dcfdd2abfe71 | 38 | unsigned short msgid; |
icraggs | 2:dcfdd2abfe71 | 39 | void *payload; |
icraggs | 2:dcfdd2abfe71 | 40 | size_t payloadlen; |
sam_grove | 0:fe461e4d7afe | 41 | }; |
sam_grove | 0:fe461e4d7afe | 42 | |
icraggs | 6:4d312a49200b | 43 | template<class Network, class Timer, class Thread> class Client; |
icraggs | 4:4ef00243708e | 44 | |
icraggs | 4:4ef00243708e | 45 | class Result |
icraggs | 4:4ef00243708e | 46 | { |
icraggs | 4:4ef00243708e | 47 | /* success or failure result data */ |
icraggs | 6:4d312a49200b | 48 | Client<class Network, class Timer, class Thread>* client; |
icraggs | 4:4ef00243708e | 49 | }; |
icraggs | 4:4ef00243708e | 50 | |
icraggs | 2:dcfdd2abfe71 | 51 | |
icraggs | 6:4d312a49200b | 52 | template<class Network, class Timer, class Thread> class Client |
icraggs | 2:dcfdd2abfe71 | 53 | { |
icraggs | 2:dcfdd2abfe71 | 54 | |
sam_grove | 0:fe461e4d7afe | 55 | public: |
icraggs | 4:4ef00243708e | 56 | |
icraggs | 6:4d312a49200b | 57 | Client(Network* network, Timer* timer, const int buffer_size = 100, const int command_timeout = 30); |
icraggs | 2:dcfdd2abfe71 | 58 | |
icraggs | 4:4ef00243708e | 59 | int connect(MQTTPacket_connectData* options = 0, FP<void, Result*> *resultHandler = 0); |
icraggs | 2:dcfdd2abfe71 | 60 | |
icraggs | 6:4d312a49200b | 61 | int publish(const char* topic, Message* message, FP<void, Result*> *resultHandler = 0); |
icraggs | 2:dcfdd2abfe71 | 62 | |
icraggs | 6:4d312a49200b | 63 | int subscribe(const char* topicFilter, enum QoS qos, FP<void, Message*> messageHandler, FP<void, Result*> *resultHandler = 0); |
sam_grove | 0:fe461e4d7afe | 64 | |
icraggs | 4:4ef00243708e | 65 | int unsubscribe(char* topicFilter, FP<void, Result*> *resultHandler = 0); |
icraggs | 2:dcfdd2abfe71 | 66 | |
icraggs | 4:4ef00243708e | 67 | int disconnect(int timeout, FP<void, Result*> *resultHandler = 0); |
sam_grove | 0:fe461e4d7afe | 68 | |
icraggs | 2:dcfdd2abfe71 | 69 | private: |
icraggs | 2:dcfdd2abfe71 | 70 | |
icraggs | 6:4d312a49200b | 71 | int getPacketId(); |
icraggs | 4:4ef00243708e | 72 | int cycle(); |
icraggs | 2:dcfdd2abfe71 | 73 | |
icraggs | 3:dbff6b768d28 | 74 | int decodePacket(int* value, int timeout); |
icraggs | 4:4ef00243708e | 75 | int readPacket(int timeout = -1); |
icraggs | 6:4d312a49200b | 76 | int sendPacket(int length, int timeout = -1); |
icraggs | 3:dbff6b768d28 | 77 | |
icraggs | 4:4ef00243708e | 78 | Thread* thread; |
icraggs | 4:4ef00243708e | 79 | Network* ipstack; |
icraggs | 6:4d312a49200b | 80 | Timer* timer; |
icraggs | 4:4ef00243708e | 81 | |
icraggs | 2:dcfdd2abfe71 | 82 | char* buf; |
icraggs | 2:dcfdd2abfe71 | 83 | int buflen; |
icraggs | 2:dcfdd2abfe71 | 84 | |
icraggs | 4:4ef00243708e | 85 | char* readbuf; |
icraggs | 4:4ef00243708e | 86 | int readbuflen; |
icraggs | 4:4ef00243708e | 87 | |
icraggs | 6:4d312a49200b | 88 | int command_timeout; // max time to wait for any MQTT command to complete, in seconds |
icraggs | 6:4d312a49200b | 89 | int keepalive; |
icraggs | 6:4d312a49200b | 90 | int packetid; |
icraggs | 4:4ef00243708e | 91 | |
sam_grove | 0:fe461e4d7afe | 92 | }; |
sam_grove | 0:fe461e4d7afe | 93 | |
icraggs | 3:dbff6b768d28 | 94 | } |
icraggs | 3:dbff6b768d28 | 95 | |
sam_grove | 0:fe461e4d7afe | 96 | #endif |