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