USE YHTANG AWS ACCOUNT TO TEST MQTT
Dependents: NuMaker-mbed-AWS-IoT-example
MQTTClient.h@5:389ccac5a50c, 2014-04-07 (annotated)
- Committer:
- Ian Craggs
- Date:
- Mon Apr 07 23:52:57 2014 +0100
- Revision:
- 5:389ccac5a50c
- Parent:
- 4:4ef00243708e
- Child:
- 6:4d312a49200b
Latest updates
Who changed what in which revision?
User | Revision | Line number | New 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 "FP.h" |
icraggs | 3:dbff6b768d28 | 27 | #include "MQTTPacket.h" |
icraggs | 2:dcfdd2abfe71 | 28 | |
icraggs | 3:dbff6b768d28 | 29 | namespace MQTT |
icraggs | 3:dbff6b768d28 | 30 | { |
icraggs | 3:dbff6b768d28 | 31 | |
icraggs | 2:dcfdd2abfe71 | 32 | |
icraggs | 2:dcfdd2abfe71 | 33 | enum QoS { QOS0, QOS1, QOS2 }; |
sam_grove | 0:fe461e4d7afe | 34 | |
icraggs | 2:dcfdd2abfe71 | 35 | |
icraggs | 3:dbff6b768d28 | 36 | struct Message |
icraggs | 2:dcfdd2abfe71 | 37 | { |
icraggs | 2:dcfdd2abfe71 | 38 | enum QoS qos; |
icraggs | 2:dcfdd2abfe71 | 39 | bool retained; |
icraggs | 2:dcfdd2abfe71 | 40 | bool dup; |
icraggs | 2:dcfdd2abfe71 | 41 | unsigned short msgid; |
icraggs | 2:dcfdd2abfe71 | 42 | void *payload; |
icraggs | 2:dcfdd2abfe71 | 43 | size_t payloadlen; |
sam_grove | 0:fe461e4d7afe | 44 | }; |
sam_grove | 0:fe461e4d7afe | 45 | |
icraggs | 4:4ef00243708e | 46 | template<class Network, class Thread> class Client; |
icraggs | 4:4ef00243708e | 47 | |
icraggs | 4:4ef00243708e | 48 | class Result |
icraggs | 4:4ef00243708e | 49 | { |
icraggs | 4:4ef00243708e | 50 | /* success or failure result data */ |
icraggs | 4:4ef00243708e | 51 | Client<class Network, class Thread>* client; |
icraggs | 4:4ef00243708e | 52 | }; |
icraggs | 4:4ef00243708e | 53 | |
icraggs | 2:dcfdd2abfe71 | 54 | |
icraggs | 4:4ef00243708e | 55 | template<class Network, class Thread> class Client |
icraggs | 2:dcfdd2abfe71 | 56 | { |
icraggs | 2:dcfdd2abfe71 | 57 | |
sam_grove | 0:fe461e4d7afe | 58 | public: |
icraggs | 4:4ef00243708e | 59 | |
icraggs | 4:4ef00243708e | 60 | Client(Network* network, const int buffer_size = 100, const int command_timeout = 30); |
icraggs | 2:dcfdd2abfe71 | 61 | |
icraggs | 4:4ef00243708e | 62 | int connect(MQTTPacket_connectData* options = 0, FP<void, Result*> *resultHandler = 0); |
icraggs | 2:dcfdd2abfe71 | 63 | |
icraggs | 4:4ef00243708e | 64 | int publish(char* topic, Message* message, FP<void, Result*> *resultHandler = 0); |
icraggs | 2:dcfdd2abfe71 | 65 | |
icraggs | 4:4ef00243708e | 66 | int subscribe(char* topicFilter, int qos, FP<void, Message*> messageHandler, FP<void, Result*> *resultHandler = 0); |
sam_grove | 0:fe461e4d7afe | 67 | |
icraggs | 4:4ef00243708e | 68 | int unsubscribe(char* topicFilter, FP<void, Result*> *resultHandler = 0); |
icraggs | 2:dcfdd2abfe71 | 69 | |
icraggs | 4:4ef00243708e | 70 | int disconnect(int timeout, FP<void, Result*> *resultHandler = 0); |
sam_grove | 0:fe461e4d7afe | 71 | |
icraggs | 2:dcfdd2abfe71 | 72 | private: |
icraggs | 2:dcfdd2abfe71 | 73 | |
icraggs | 4:4ef00243708e | 74 | int cycle(); |
icraggs | 2:dcfdd2abfe71 | 75 | |
icraggs | 3:dbff6b768d28 | 76 | int decodePacket(int* value, int timeout); |
icraggs | 4:4ef00243708e | 77 | int readPacket(int timeout = -1); |
icraggs | 3:dbff6b768d28 | 78 | int sendPacket(int length); |
icraggs | 3:dbff6b768d28 | 79 | |
icraggs | 4:4ef00243708e | 80 | Thread* thread; |
icraggs | 4:4ef00243708e | 81 | Network* ipstack; |
icraggs | 4:4ef00243708e | 82 | |
icraggs | 2:dcfdd2abfe71 | 83 | char* buf; |
icraggs | 2:dcfdd2abfe71 | 84 | int buflen; |
icraggs | 2:dcfdd2abfe71 | 85 | |
icraggs | 4:4ef00243708e | 86 | char* readbuf; |
icraggs | 4:4ef00243708e | 87 | int readbuflen; |
icraggs | 4:4ef00243708e | 88 | |
icraggs | 4:4ef00243708e | 89 | int command_timeout; |
icraggs | 4:4ef00243708e | 90 | |
sam_grove | 0:fe461e4d7afe | 91 | }; |
sam_grove | 0:fe461e4d7afe | 92 | |
icraggs | 3:dbff6b768d28 | 93 | } |
icraggs | 3:dbff6b768d28 | 94 | |
sam_grove | 0:fe461e4d7afe | 95 | #endif |