test
Fork of MQTT by
MQTTClient.h@56:30f7b745eaaa, 2017-11-27 (annotated)
- Committer:
- DuyLionTran
- Date:
- Mon Nov 27 11:06:38 2017 +0000
- Revision:
- 56:30f7b745eaaa
- Parent:
- 46:e335fcc1a663
- Child:
- 61:36e76acf2166
new version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 6:4d312a49200b | 1 | /******************************************************************************* |
icraggs | 46:e335fcc1a663 | 2 | * Copyright (c) 2014, 2015 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 | 46:e335fcc1a663 | 15 | * Ian Craggs - fix for bug 458512 - QoS 2 messages |
icraggs | 46:e335fcc1a663 | 16 | * Ian Craggs - fix for bug 460389 - send loop uses wrong length |
icraggs | 46:e335fcc1a663 | 17 | * Ian Craggs - fix for bug 464169 - clearing subscriptions |
icraggs | 46:e335fcc1a663 | 18 | * Ian Craggs - fix for bug 464551 - enums and ints can be different size |
icraggs | 6:4d312a49200b | 19 | *******************************************************************************/ |
sam_grove | 0:fe461e4d7afe | 20 | |
icraggs | 2:dcfdd2abfe71 | 21 | #if !defined(MQTTCLIENT_H) |
icraggs | 2:dcfdd2abfe71 | 22 | #define MQTTCLIENT_H |
icraggs | 2:dcfdd2abfe71 | 23 | |
icraggs | 34:e18a166198df | 24 | #include "FP.h" |
icraggs | 3:dbff6b768d28 | 25 | #include "MQTTPacket.h" |
icraggs | 6:4d312a49200b | 26 | #include "stdio.h" |
icraggs | 43:21da1f744243 | 27 | #include "MQTTLogging.h" |
icraggs | 43:21da1f744243 | 28 | |
icraggs | 43:21da1f744243 | 29 | #if !defined(MQTTCLIENT_QOS1) |
icraggs | 43:21da1f744243 | 30 | #define MQTTCLIENT_QOS1 1 |
icraggs | 43:21da1f744243 | 31 | #endif |
icraggs | 43:21da1f744243 | 32 | #if !defined(MQTTCLIENT_QOS2) |
icraggs | 43:21da1f744243 | 33 | #define MQTTCLIENT_QOS2 0 |
icraggs | 43:21da1f744243 | 34 | #endif |
icraggs | 2:dcfdd2abfe71 | 35 | |
icraggs | 3:dbff6b768d28 | 36 | namespace MQTT |
icraggs | 3:dbff6b768d28 | 37 | { |
icraggs | 3:dbff6b768d28 | 38 | |
icraggs | 2:dcfdd2abfe71 | 39 | |
icraggs | 2:dcfdd2abfe71 | 40 | enum QoS { QOS0, QOS1, QOS2 }; |
sam_grove | 0:fe461e4d7afe | 41 | |
icraggs | 31:a51dd239b78e | 42 | // all failure return codes must be negative |
icraggs | 23:05fc7de97d4a | 43 | enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 }; |
icraggs | 23:05fc7de97d4a | 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; |
Ian Craggs |
12:cc7f2d62a393 | 51 | unsigned short id; |
icraggs | 2:dcfdd2abfe71 | 52 | void *payload; |
icraggs | 2:dcfdd2abfe71 | 53 | size_t payloadlen; |
sam_grove | 0:fe461e4d7afe | 54 | }; |
sam_grove | 0:fe461e4d7afe | 55 | |
icraggs | 4:4ef00243708e | 56 | |
icraggs | 20:cad3d54d7ecf | 57 | struct MessageData |
icraggs | 20:cad3d54d7ecf | 58 | { |
icraggs | 31:a51dd239b78e | 59 | MessageData(MQTTString &aTopicName, struct Message &aMessage) : message(aMessage), topicName(aTopicName) |
icraggs | 37:e3d64f9b986c | 60 | { } |
icraggs | 43:21da1f744243 | 61 | |
icraggs | 31:a51dd239b78e | 62 | struct Message &message; |
icraggs | 31:a51dd239b78e | 63 | MQTTString &topicName; |
icraggs | 20:cad3d54d7ecf | 64 | }; |
icraggs | 20:cad3d54d7ecf | 65 | |
icraggs | 20:cad3d54d7ecf | 66 | |
icraggs | 9:01b8cc7d94cc | 67 | class PacketId |
icraggs | 9:01b8cc7d94cc | 68 | { |
icraggs | 9:01b8cc7d94cc | 69 | public: |
icraggs | 23:05fc7de97d4a | 70 | PacketId() |
icraggs | 23:05fc7de97d4a | 71 | { |
icraggs | 23:05fc7de97d4a | 72 | next = 0; |
icraggs | 23:05fc7de97d4a | 73 | } |
icraggs | 43:21da1f744243 | 74 | |
icraggs | 23:05fc7de97d4a | 75 | int getNext() |
icraggs | 23:05fc7de97d4a | 76 | { |
icraggs | 23:05fc7de97d4a | 77 | return next = (next == MAX_PACKET_ID) ? 1 : ++next; |
icraggs | 23:05fc7de97d4a | 78 | } |
icraggs | 43:21da1f744243 | 79 | |
icraggs | 9:01b8cc7d94cc | 80 | private: |
icraggs | 9:01b8cc7d94cc | 81 | static const int MAX_PACKET_ID = 65535; |
icraggs | 9:01b8cc7d94cc | 82 | int next; |
icraggs | 9:01b8cc7d94cc | 83 | }; |
icraggs | 31:a51dd239b78e | 84 | |
icraggs | 31:a51dd239b78e | 85 | |
icraggs | 21:e918525e529d | 86 | /** |
icraggs | 21:e918525e529d | 87 | * @class Client |
icraggs | 22:aadb79d29330 | 88 | * @brief blocking, non-threaded MQTT client API |
icraggs | 43:21da1f744243 | 89 | * |
icraggs | 23:05fc7de97d4a | 90 | * This version of the API blocks on all method calls, until they are complete. This means that only one |
icraggs | 43:21da1f744243 | 91 | * MQTT request can be in process at any one time. |
icraggs | 21:e918525e529d | 92 | * @param Network a network class which supports send, receive |
icraggs | 43:21da1f744243 | 93 | * @param Timer a timer class with the methods: |
icraggs | 43:21da1f744243 | 94 | */ |
icraggs | 31:a51dd239b78e | 95 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5> |
icraggs | 31:a51dd239b78e | 96 | class Client |
icraggs | 2:dcfdd2abfe71 | 97 | { |
icraggs | 43:21da1f744243 | 98 | |
icraggs | 22:aadb79d29330 | 99 | public: |
icraggs | 43:21da1f744243 | 100 | |
icraggs | 31:a51dd239b78e | 101 | typedef void (*messageHandler)(MessageData&); |
icraggs | 23:05fc7de97d4a | 102 | |
icraggs | 23:05fc7de97d4a | 103 | /** Construct the client |
icraggs | 23:05fc7de97d4a | 104 | * @param network - pointer to an instance of the Network class - must be connected to the endpoint |
icraggs | 23:05fc7de97d4a | 105 | * before calling MQTT connect |
icraggs | 23:05fc7de97d4a | 106 | * @param limits an instance of the Limit class - to alter limits as required |
icraggs | 23:05fc7de97d4a | 107 | */ |
icraggs | 43:21da1f744243 | 108 | Client(Network& network, unsigned int command_timeout_ms = 30000); |
icraggs | 43:21da1f744243 | 109 | |
icraggs | 20:cad3d54d7ecf | 110 | /** Set the default message handling callback - used for any message which does not match a subscription message handler |
icraggs | 20:cad3d54d7ecf | 111 | * @param mh - pointer to the callback function |
icraggs | 20:cad3d54d7ecf | 112 | */ |
icraggs | 20:cad3d54d7ecf | 113 | void setDefaultMessageHandler(messageHandler mh) |
icraggs | 20:cad3d54d7ecf | 114 | { |
icraggs | 20:cad3d54d7ecf | 115 | defaultMessageHandler.attach(mh); |
icraggs | 20:cad3d54d7ecf | 116 | } |
icraggs | 42:f5beda831651 | 117 | |
icraggs | 43:21da1f744243 | 118 | /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack |
icraggs | 43:21da1f744243 | 119 | * The nework object must be connected to the network endpoint before calling this |
icraggs | 43:21da1f744243 | 120 | * Default connect options are used |
icraggs | 43:21da1f744243 | 121 | * @return success code - |
icraggs | 43:21da1f744243 | 122 | */ |
icraggs | 43:21da1f744243 | 123 | int connect(); |
icraggs | 42:f5beda831651 | 124 | |
icraggs | 46:e335fcc1a663 | 125 | /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack |
icraggs | 43:21da1f744243 | 126 | * The nework object must be connected to the network endpoint before calling this |
icraggs | 20:cad3d54d7ecf | 127 | * @param options - connect options |
icraggs | 43:21da1f744243 | 128 | * @return success code - |
icraggs | 43:21da1f744243 | 129 | */ |
icraggs | 43:21da1f744243 | 130 | int connect(MQTTPacket_connectData& options); |
icraggs | 43:21da1f744243 | 131 | |
icraggs | 20:cad3d54d7ecf | 132 | /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs |
icraggs | 20:cad3d54d7ecf | 133 | * @param topic - the topic to publish to |
icraggs | 20:cad3d54d7ecf | 134 | * @param message - the message to send |
icraggs | 43:21da1f744243 | 135 | * @return success code - |
icraggs | 43:21da1f744243 | 136 | */ |
icraggs | 43:21da1f744243 | 137 | int publish(const char* topicName, Message& message); |
icraggs | 43:21da1f744243 | 138 | |
icraggs | 43:21da1f744243 | 139 | /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs |
icraggs | 43:21da1f744243 | 140 | * @param topic - the topic to publish to |
icraggs | 43:21da1f744243 | 141 | * @param payload - the data to send |
icraggs | 43:21da1f744243 | 142 | * @param payloadlen - the length of the data |
icraggs | 43:21da1f744243 | 143 | * @param qos - the QoS to send the publish at |
icraggs | 43:21da1f744243 | 144 | * @param retained - whether the message should be retained |
icraggs | 43:21da1f744243 | 145 | * @return success code - |
icraggs | 43:21da1f744243 | 146 | */ |
icraggs | 43:21da1f744243 | 147 | int publish(const char* topicName, void* payload, size_t payloadlen, enum QoS qos = QOS0, bool retained = false); |
icraggs | 43:21da1f744243 | 148 | |
icraggs | 43:21da1f744243 | 149 | /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs |
icraggs | 43:21da1f744243 | 150 | * @param topic - the topic to publish to |
icraggs | 43:21da1f744243 | 151 | * @param payload - the data to send |
icraggs | 43:21da1f744243 | 152 | * @param payloadlen - the length of the data |
icraggs | 43:21da1f744243 | 153 | * @param id - the packet id used - returned |
icraggs | 43:21da1f744243 | 154 | * @param qos - the QoS to send the publish at |
icraggs | 43:21da1f744243 | 155 | * @param retained - whether the message should be retained |
icraggs | 43:21da1f744243 | 156 | * @return success code - |
icraggs | 43:21da1f744243 | 157 | */ |
icraggs | 43:21da1f744243 | 158 | int publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum QoS qos = QOS1, bool retained = false); |
icraggs | 43:21da1f744243 | 159 | |
icraggs | 20:cad3d54d7ecf | 160 | /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback |
icraggs | 20:cad3d54d7ecf | 161 | * @param topicFilter - a topic pattern which can include wildcards |
icraggs | 20:cad3d54d7ecf | 162 | * @param qos - the MQTT QoS to subscribe at |
icraggs | 20:cad3d54d7ecf | 163 | * @param mh - the callback function to be invoked when a message is received for this subscription |
icraggs | 43:21da1f744243 | 164 | * @return success code - |
icraggs | 43:21da1f744243 | 165 | */ |
icraggs | 20:cad3d54d7ecf | 166 | int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh); |
icraggs | 43:21da1f744243 | 167 | |
icraggs | 20:cad3d54d7ecf | 168 | /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback |
icraggs | 20:cad3d54d7ecf | 169 | * @param topicFilter - a topic pattern which can include wildcards |
icraggs | 43:21da1f744243 | 170 | * @return success code - |
icraggs | 43:21da1f744243 | 171 | */ |
icraggs | 20:cad3d54d7ecf | 172 | int unsubscribe(const char* topicFilter); |
icraggs | 43:21da1f744243 | 173 | |
icraggs | 31:a51dd239b78e | 174 | /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state |
icraggs | 43:21da1f744243 | 175 | * @return success code - |
icraggs | 20:cad3d54d7ecf | 176 | */ |
icraggs | 20:cad3d54d7ecf | 177 | int disconnect(); |
icraggs | 43:21da1f744243 | 178 | |
icraggs | 20:cad3d54d7ecf | 179 | /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive |
icraggs | 43:21da1f744243 | 180 | * yield can be called if no other MQTT operation is needed. This will also allow messages to be |
icraggs | 20:cad3d54d7ecf | 181 | * received. |
icraggs | 23:05fc7de97d4a | 182 | * @param timeout_ms the time to wait, in milliseconds |
icraggs | 26:2658bb87c53d | 183 | * @return success code - on failure, this means the client has disconnected |
icraggs | 20:cad3d54d7ecf | 184 | */ |
icraggs | 43:21da1f744243 | 185 | int yield(unsigned long timeout_ms = 1000L); |
icraggs | 43:21da1f744243 | 186 | |
icraggs | 43:21da1f744243 | 187 | /** Is the client connected? |
icraggs | 43:21da1f744243 | 188 | * @return flag - is the client connected or not? |
icraggs | 43:21da1f744243 | 189 | */ |
icraggs | 43:21da1f744243 | 190 | bool isConnected() |
icraggs | 43:21da1f744243 | 191 | { |
icraggs | 43:21da1f744243 | 192 | return isconnected; |
icraggs | 43:21da1f744243 | 193 | } |
icraggs | 43:21da1f744243 | 194 | |
icraggs | 20:cad3d54d7ecf | 195 | private: |
icraggs | 20:cad3d54d7ecf | 196 | |
icraggs | 46:e335fcc1a663 | 197 | void cleanSession(); |
icraggs | 20:cad3d54d7ecf | 198 | int cycle(Timer& timer); |
icraggs | 20:cad3d54d7ecf | 199 | int waitfor(int packet_type, Timer& timer); |
icraggs | 20:cad3d54d7ecf | 200 | int keepalive(); |
icraggs | 43:21da1f744243 | 201 | int publish(int len, Timer& timer, enum QoS qos); |
icraggs | 2:dcfdd2abfe71 | 202 | |
icraggs | 3:dbff6b768d28 | 203 | int decodePacket(int* value, int timeout); |
icraggs | 20:cad3d54d7ecf | 204 | int readPacket(Timer& timer); |
icraggs | 20:cad3d54d7ecf | 205 | int sendPacket(int length, Timer& timer); |
icraggs | 26:2658bb87c53d | 206 | int deliverMessage(MQTTString& topicName, Message& message); |
icraggs | 26:2658bb87c53d | 207 | bool isTopicMatched(char* topicFilter, MQTTString& topicName); |
icraggs | 43:21da1f744243 | 208 | |
icraggs | 23:05fc7de97d4a | 209 | Network& ipstack; |
icraggs | 43:21da1f744243 | 210 | unsigned long command_timeout_ms; |
icraggs | 15:64a57183aa03 | 211 | |
icraggs | 43:21da1f744243 | 212 | unsigned char sendbuf[MAX_MQTT_PACKET_SIZE]; |
icraggs | 43:21da1f744243 | 213 | unsigned char readbuf[MAX_MQTT_PACKET_SIZE]; |
icraggs | 43:21da1f744243 | 214 | |
icraggs | 43:21da1f744243 | 215 | Timer last_sent, last_received; |
icraggs | 15:64a57183aa03 | 216 | unsigned int keepAliveInterval; |
icraggs | 20:cad3d54d7ecf | 217 | bool ping_outstanding; |
icraggs | 43:21da1f744243 | 218 | bool cleansession; |
icraggs | 43:21da1f744243 | 219 | |
icraggs | 9:01b8cc7d94cc | 220 | PacketId packetid; |
icraggs | 43:21da1f744243 | 221 | |
icraggs | 16:91c2f9a144d4 | 222 | struct MessageHandlers |
icraggs | 15:64a57183aa03 | 223 | { |
icraggs | 26:2658bb87c53d | 224 | const char* topicFilter; |
icraggs | 31:a51dd239b78e | 225 | FP<void, MessageData&> fp; |
icraggs | 23:05fc7de97d4a | 226 | } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic |
icraggs | 43:21da1f744243 | 227 | |
icraggs | 31:a51dd239b78e | 228 | FP<void, MessageData&> defaultMessageHandler; |
icraggs | 43:21da1f744243 | 229 | |
icraggs | 26:2658bb87c53d | 230 | bool isconnected; |
icraggs | 43:21da1f744243 | 231 | |
icraggs | 43:21da1f744243 | 232 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 233 | unsigned char pubbuf[MAX_MQTT_PACKET_SIZE]; // store the last publish for sending on reconnect |
icraggs | 43:21da1f744243 | 234 | int inflightLen; |
icraggs | 43:21da1f744243 | 235 | unsigned short inflightMsgid; |
icraggs | 43:21da1f744243 | 236 | enum QoS inflightQoS; |
icraggs | 43:21da1f744243 | 237 | #endif |
icraggs | 43:21da1f744243 | 238 | |
icraggs | 43:21da1f744243 | 239 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 240 | bool pubrel; |
icraggs | 43:21da1f744243 | 241 | #if !defined(MAX_INCOMING_QOS2_MESSAGES) |
icraggs | 43:21da1f744243 | 242 | #define MAX_INCOMING_QOS2_MESSAGES 10 |
icraggs | 43:21da1f744243 | 243 | #endif |
icraggs | 43:21da1f744243 | 244 | unsigned short incomingQoS2messages[MAX_INCOMING_QOS2_MESSAGES]; |
icraggs | 43:21da1f744243 | 245 | bool isQoS2msgidFree(unsigned short id); |
icraggs | 43:21da1f744243 | 246 | bool useQoS2msgid(unsigned short id); |
icraggs | 46:e335fcc1a663 | 247 | void freeQoS2msgid(unsigned short id); |
icraggs | 31:a51dd239b78e | 248 | #endif |
icraggs | 28:8b2abe9bd814 | 249 | |
sam_grove | 0:fe461e4d7afe | 250 | }; |
sam_grove | 0:fe461e4d7afe | 251 | |
icraggs | 15:64a57183aa03 | 252 | } |
icraggs | 15:64a57183aa03 | 253 | |
icraggs | 15:64a57183aa03 | 254 | |
icraggs | 43:21da1f744243 | 255 | template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> |
icraggs | 46:e335fcc1a663 | 256 | void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession() |
icraggs | 46:e335fcc1a663 | 257 | { |
icraggs | 46:e335fcc1a663 | 258 | ping_outstanding = false; |
icraggs | 46:e335fcc1a663 | 259 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 46:e335fcc1a663 | 260 | messageHandlers[i].topicFilter = 0; |
icraggs | 46:e335fcc1a663 | 261 | isconnected = false; |
icraggs | 46:e335fcc1a663 | 262 | |
icraggs | 46:e335fcc1a663 | 263 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 46:e335fcc1a663 | 264 | inflightMsgid = 0; |
icraggs | 46:e335fcc1a663 | 265 | inflightQoS = QOS0; |
icraggs | 46:e335fcc1a663 | 266 | #endif |
icraggs | 46:e335fcc1a663 | 267 | |
icraggs | 46:e335fcc1a663 | 268 | #if MQTTCLIENT_QOS2 |
icraggs | 46:e335fcc1a663 | 269 | pubrel = false; |
icraggs | 46:e335fcc1a663 | 270 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 46:e335fcc1a663 | 271 | incomingQoS2messages[i] = 0; |
icraggs | 46:e335fcc1a663 | 272 | #endif |
icraggs | 46:e335fcc1a663 | 273 | } |
icraggs | 46:e335fcc1a663 | 274 | |
icraggs | 46:e335fcc1a663 | 275 | |
icraggs | 46:e335fcc1a663 | 276 | template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 277 | MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid() |
icraggs | 15:64a57183aa03 | 278 | { |
icraggs | 43:21da1f744243 | 279 | last_sent = Timer(); |
icraggs | 43:21da1f744243 | 280 | last_received = Timer(); |
icraggs | 43:21da1f744243 | 281 | this->command_timeout_ms = command_timeout_ms; |
icraggs | 46:e335fcc1a663 | 282 | cleanSession(); |
icraggs | 46:e335fcc1a663 | 283 | } |
icraggs | 43:21da1f744243 | 284 | |
icraggs | 43:21da1f744243 | 285 | |
icraggs | 43:21da1f744243 | 286 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 287 | template<class Network, class Timer, int a, int b> |
icraggs | 43:21da1f744243 | 288 | bool MQTT::Client<Network, Timer, a, b>::isQoS2msgidFree(unsigned short id) |
icraggs | 43:21da1f744243 | 289 | { |
icraggs | 43:21da1f744243 | 290 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 43:21da1f744243 | 291 | { |
icraggs | 43:21da1f744243 | 292 | if (incomingQoS2messages[i] == id) |
icraggs | 43:21da1f744243 | 293 | return false; |
icraggs | 43:21da1f744243 | 294 | } |
icraggs | 43:21da1f744243 | 295 | return true; |
icraggs | 11:db15da110a37 | 296 | } |
icraggs | 11:db15da110a37 | 297 | |
icraggs | 11:db15da110a37 | 298 | |
icraggs | 43:21da1f744243 | 299 | template<class Network, class Timer, int a, int b> |
icraggs | 43:21da1f744243 | 300 | bool MQTT::Client<Network, Timer, a, b>::useQoS2msgid(unsigned short id) |
icraggs | 43:21da1f744243 | 301 | { |
icraggs | 43:21da1f744243 | 302 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 43:21da1f744243 | 303 | { |
icraggs | 43:21da1f744243 | 304 | if (incomingQoS2messages[i] == 0) |
icraggs | 43:21da1f744243 | 305 | { |
icraggs | 43:21da1f744243 | 306 | incomingQoS2messages[i] = id; |
icraggs | 43:21da1f744243 | 307 | return true; |
icraggs | 43:21da1f744243 | 308 | } |
icraggs | 43:21da1f744243 | 309 | } |
icraggs | 43:21da1f744243 | 310 | return false; |
icraggs | 43:21da1f744243 | 311 | } |
icraggs | 46:e335fcc1a663 | 312 | |
icraggs | 46:e335fcc1a663 | 313 | |
icraggs | 46:e335fcc1a663 | 314 | template<class Network, class Timer, int a, int b> |
icraggs | 46:e335fcc1a663 | 315 | void MQTT::Client<Network, Timer, a, b>::freeQoS2msgid(unsigned short id) |
icraggs | 46:e335fcc1a663 | 316 | { |
icraggs | 46:e335fcc1a663 | 317 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 46:e335fcc1a663 | 318 | { |
icraggs | 46:e335fcc1a663 | 319 | if (incomingQoS2messages[i] == id) |
icraggs | 46:e335fcc1a663 | 320 | { |
icraggs | 46:e335fcc1a663 | 321 | incomingQoS2messages[i] = 0; |
icraggs | 46:e335fcc1a663 | 322 | return; |
icraggs | 46:e335fcc1a663 | 323 | } |
icraggs | 46:e335fcc1a663 | 324 | } |
icraggs | 46:e335fcc1a663 | 325 | } |
icraggs | 43:21da1f744243 | 326 | #endif |
icraggs | 43:21da1f744243 | 327 | |
icraggs | 43:21da1f744243 | 328 | |
icraggs | 43:21da1f744243 | 329 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 330 | int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer) |
icraggs | 8:c46930bd6c82 | 331 | { |
icraggs | 43:21da1f744243 | 332 | int rc = FAILURE, |
icraggs | 23:05fc7de97d4a | 333 | sent = 0; |
icraggs | 43:21da1f744243 | 334 | |
icraggs | 23:05fc7de97d4a | 335 | while (sent < length && !timer.expired()) |
icraggs | 23:05fc7de97d4a | 336 | { |
icraggs | 46:e335fcc1a663 | 337 | rc = ipstack.write(&sendbuf[sent], length - sent, timer.left_ms()); |
icraggs | 26:2658bb87c53d | 338 | if (rc < 0) // there was an error writing the data |
DuyLionTran | 56:30f7b745eaaa | 339 | { |
DuyLionTran | 56:30f7b745eaaa | 340 | printf("sendPacket fail %d\n", rc); |
icraggs | 26:2658bb87c53d | 341 | break; |
DuyLionTran | 56:30f7b745eaaa | 342 | } |
icraggs | 26:2658bb87c53d | 343 | sent += rc; |
icraggs | 23:05fc7de97d4a | 344 | } |
icraggs | 20:cad3d54d7ecf | 345 | if (sent == length) |
icraggs | 23:05fc7de97d4a | 346 | { |
icraggs | 43:21da1f744243 | 347 | if (this->keepAliveInterval > 0) |
icraggs | 43:21da1f744243 | 348 | last_sent.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet |
icraggs | 23:05fc7de97d4a | 349 | rc = SUCCESS; |
icraggs | 23:05fc7de97d4a | 350 | } |
icraggs | 23:05fc7de97d4a | 351 | else |
icraggs | 23:05fc7de97d4a | 352 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 353 | |
icraggs | 43:21da1f744243 | 354 | #if defined(MQTT_DEBUG) |
icraggs | 46:e335fcc1a663 | 355 | char printbuf[150]; |
icraggs | 46:e335fcc1a663 | 356 | DEBUG("Rc %d from sending packet %s\n", rc, MQTTFormat_toServerString(printbuf, sizeof(printbuf), sendbuf, length)); |
icraggs | 43:21da1f744243 | 357 | #endif |
icraggs | 23:05fc7de97d4a | 358 | return rc; |
icraggs | 8:c46930bd6c82 | 359 | } |
icraggs | 8:c46930bd6c82 | 360 | |
icraggs | 8:c46930bd6c82 | 361 | |
icraggs | 43:21da1f744243 | 362 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 363 | int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout) |
icraggs | 8:c46930bd6c82 | 364 | { |
icraggs | 36:2f1ada427e56 | 365 | unsigned char c; |
icraggs | 8:c46930bd6c82 | 366 | int multiplier = 1; |
icraggs | 8:c46930bd6c82 | 367 | int len = 0; |
icraggs | 20:cad3d54d7ecf | 368 | const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4; |
icraggs | 8:c46930bd6c82 | 369 | |
icraggs | 8:c46930bd6c82 | 370 | *value = 0; |
icraggs | 8:c46930bd6c82 | 371 | do |
icraggs | 8:c46930bd6c82 | 372 | { |
icraggs | 8:c46930bd6c82 | 373 | int rc = MQTTPACKET_READ_ERROR; |
icraggs | 8:c46930bd6c82 | 374 | |
icraggs | 8:c46930bd6c82 | 375 | if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES) |
icraggs | 8:c46930bd6c82 | 376 | { |
icraggs | 8:c46930bd6c82 | 377 | rc = MQTTPACKET_READ_ERROR; /* bad data */ |
icraggs | 8:c46930bd6c82 | 378 | goto exit; |
icraggs | 8:c46930bd6c82 | 379 | } |
icraggs | 23:05fc7de97d4a | 380 | rc = ipstack.read(&c, 1, timeout); |
icraggs | 8:c46930bd6c82 | 381 | if (rc != 1) |
icraggs | 8:c46930bd6c82 | 382 | goto exit; |
icraggs | 8:c46930bd6c82 | 383 | *value += (c & 127) * multiplier; |
icraggs | 8:c46930bd6c82 | 384 | multiplier *= 128; |
icraggs | 8:c46930bd6c82 | 385 | } while ((c & 128) != 0); |
icraggs | 8:c46930bd6c82 | 386 | exit: |
icraggs | 8:c46930bd6c82 | 387 | return len; |
icraggs | 8:c46930bd6c82 | 388 | } |
icraggs | 8:c46930bd6c82 | 389 | |
icraggs | 8:c46930bd6c82 | 390 | |
icraggs | 8:c46930bd6c82 | 391 | /** |
icraggs | 8:c46930bd6c82 | 392 | * If any read fails in this method, then we should disconnect from the network, as on reconnect |
icraggs | 43:21da1f744243 | 393 | * the packets can be retried. |
icraggs | 8:c46930bd6c82 | 394 | * @param timeout the max time to wait for the packet read to complete, in milliseconds |
icraggs | 8:c46930bd6c82 | 395 | * @return the MQTT packet type, or -1 if none |
icraggs | 8:c46930bd6c82 | 396 | */ |
icraggs | 46:e335fcc1a663 | 397 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 46:e335fcc1a663 | 398 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::readPacket(Timer& timer) |
icraggs | 8:c46930bd6c82 | 399 | { |
icraggs | 26:2658bb87c53d | 400 | int rc = FAILURE; |
icraggs | 8:c46930bd6c82 | 401 | MQTTHeader header = {0}; |
icraggs | 8:c46930bd6c82 | 402 | int len = 0; |
icraggs | 8:c46930bd6c82 | 403 | int rem_len = 0; |
icraggs | 8:c46930bd6c82 | 404 | |
icraggs | 8:c46930bd6c82 | 405 | /* 1. read the header byte. This has the packet type in it */ |
icraggs | 23:05fc7de97d4a | 406 | if (ipstack.read(readbuf, 1, timer.left_ms()) != 1) |
icraggs | 8:c46930bd6c82 | 407 | goto exit; |
icraggs | 8:c46930bd6c82 | 408 | |
icraggs | 8:c46930bd6c82 | 409 | len = 1; |
icraggs | 8:c46930bd6c82 | 410 | /* 2. read the remaining length. This is variable in itself */ |
icraggs | 20:cad3d54d7ecf | 411 | decodePacket(&rem_len, timer.left_ms()); |
icraggs | 43:21da1f744243 | 412 | len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length into the buffer */ |
icraggs | 8:c46930bd6c82 | 413 | |
icraggs | 46:e335fcc1a663 | 414 | if (rem_len > (MAX_MQTT_PACKET_SIZE - len)) |
icraggs | 46:e335fcc1a663 | 415 | { |
icraggs | 46:e335fcc1a663 | 416 | rc = BUFFER_OVERFLOW; |
icraggs | 46:e335fcc1a663 | 417 | goto exit; |
icraggs | 46:e335fcc1a663 | 418 | } |
icraggs | 46:e335fcc1a663 | 419 | |
icraggs | 8:c46930bd6c82 | 420 | /* 3. read the rest of the buffer using a callback to supply the rest of the data */ |
icraggs | 43:21da1f744243 | 421 | if (rem_len > 0 && (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len)) |
icraggs | 8:c46930bd6c82 | 422 | goto exit; |
icraggs | 8:c46930bd6c82 | 423 | |
icraggs | 8:c46930bd6c82 | 424 | header.byte = readbuf[0]; |
icraggs | 8:c46930bd6c82 | 425 | rc = header.bits.type; |
icraggs | 43:21da1f744243 | 426 | if (this->keepAliveInterval > 0) |
icraggs | 43:21da1f744243 | 427 | last_received.countdown(this->keepAliveInterval); // record the fact that we have successfully received a packet |
icraggs | 8:c46930bd6c82 | 428 | exit: |
icraggs | 43:21da1f744243 | 429 | |
icraggs | 43:21da1f744243 | 430 | #if defined(MQTT_DEBUG) |
icraggs | 46:e335fcc1a663 | 431 | if (rc >= 0) |
icraggs | 46:e335fcc1a663 | 432 | { |
icraggs | 46:e335fcc1a663 | 433 | char printbuf[50]; |
icraggs | 46:e335fcc1a663 | 434 | DEBUG("Rc %d from receiving packet %s\n", rc, MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len)); |
icraggs | 46:e335fcc1a663 | 435 | } |
icraggs | 43:21da1f744243 | 436 | #endif |
icraggs | 8:c46930bd6c82 | 437 | return rc; |
icraggs | 3:dbff6b768d28 | 438 | } |
icraggs | 3:dbff6b768d28 | 439 | |
icraggs | 8:c46930bd6c82 | 440 | |
icraggs | 26:2658bb87c53d | 441 | // assume topic filter and name is in correct format |
icraggs | 26:2658bb87c53d | 442 | // # can only be at end |
icraggs | 26:2658bb87c53d | 443 | // + and # can only be next to separator |
icraggs | 43:21da1f744243 | 444 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 445 | bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName) |
icraggs | 26:2658bb87c53d | 446 | { |
icraggs | 26:2658bb87c53d | 447 | char* curf = topicFilter; |
icraggs | 26:2658bb87c53d | 448 | char* curn = topicName.lenstring.data; |
icraggs | 26:2658bb87c53d | 449 | char* curn_end = curn + topicName.lenstring.len; |
icraggs | 43:21da1f744243 | 450 | |
icraggs | 26:2658bb87c53d | 451 | while (*curf && curn < curn_end) |
icraggs | 26:2658bb87c53d | 452 | { |
icraggs | 26:2658bb87c53d | 453 | if (*curn == '/' && *curf != '/') |
icraggs | 26:2658bb87c53d | 454 | break; |
icraggs | 26:2658bb87c53d | 455 | if (*curf != '+' && *curf != '#' && *curf != *curn) |
icraggs | 26:2658bb87c53d | 456 | break; |
icraggs | 26:2658bb87c53d | 457 | if (*curf == '+') |
icraggs | 26:2658bb87c53d | 458 | { // skip until we meet the next separator, or end of string |
icraggs | 26:2658bb87c53d | 459 | char* nextpos = curn + 1; |
icraggs | 26:2658bb87c53d | 460 | while (nextpos < curn_end && *nextpos != '/') |
icraggs | 26:2658bb87c53d | 461 | nextpos = ++curn + 1; |
icraggs | 26:2658bb87c53d | 462 | } |
icraggs | 26:2658bb87c53d | 463 | else if (*curf == '#') |
icraggs | 26:2658bb87c53d | 464 | curn = curn_end - 1; // skip until end of string |
icraggs | 26:2658bb87c53d | 465 | curf++; |
icraggs | 26:2658bb87c53d | 466 | curn++; |
icraggs | 26:2658bb87c53d | 467 | }; |
icraggs | 43:21da1f744243 | 468 | |
icraggs | 26:2658bb87c53d | 469 | return (curn == curn_end) && (*curf == '\0'); |
icraggs | 26:2658bb87c53d | 470 | } |
icraggs | 26:2658bb87c53d | 471 | |
icraggs | 26:2658bb87c53d | 472 | |
icraggs | 26:2658bb87c53d | 473 | |
icraggs | 43:21da1f744243 | 474 | template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> |
icraggs | 26:2658bb87c53d | 475 | int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message) |
icraggs | 15:64a57183aa03 | 476 | { |
icraggs | 26:2658bb87c53d | 477 | int rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 478 | |
icraggs | 20:cad3d54d7ecf | 479 | // we have to find the right message handler - indexed by topic |
icraggs | 23:05fc7de97d4a | 480 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 20:cad3d54d7ecf | 481 | { |
icraggs | 26:2658bb87c53d | 482 | if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) || |
icraggs | 26:2658bb87c53d | 483 | isTopicMatched((char*)messageHandlers[i].topicFilter, topicName))) |
icraggs | 20:cad3d54d7ecf | 484 | { |
icraggs | 26:2658bb87c53d | 485 | if (messageHandlers[i].fp.attached()) |
icraggs | 26:2658bb87c53d | 486 | { |
icraggs | 31:a51dd239b78e | 487 | MessageData md(topicName, message); |
icraggs | 31:a51dd239b78e | 488 | messageHandlers[i].fp(md); |
icraggs | 26:2658bb87c53d | 489 | rc = SUCCESS; |
icraggs | 26:2658bb87c53d | 490 | } |
icraggs | 20:cad3d54d7ecf | 491 | } |
icraggs | 20:cad3d54d7ecf | 492 | } |
icraggs | 43:21da1f744243 | 493 | |
icraggs | 43:21da1f744243 | 494 | if (rc == FAILURE && defaultMessageHandler.attached()) |
icraggs | 26:2658bb87c53d | 495 | { |
icraggs | 31:a51dd239b78e | 496 | MessageData md(topicName, message); |
icraggs | 31:a51dd239b78e | 497 | defaultMessageHandler(md); |
icraggs | 26:2658bb87c53d | 498 | rc = SUCCESS; |
icraggs | 43:21da1f744243 | 499 | } |
icraggs | 43:21da1f744243 | 500 | |
icraggs | 20:cad3d54d7ecf | 501 | return rc; |
icraggs | 15:64a57183aa03 | 502 | } |
icraggs | 15:64a57183aa03 | 503 | |
icraggs | 15:64a57183aa03 | 504 | |
icraggs | 20:cad3d54d7ecf | 505 | |
icraggs | 43:21da1f744243 | 506 | template<class Network, class Timer, int a, int b> |
icraggs | 43:21da1f744243 | 507 | int MQTT::Client<Network, Timer, a, b>::yield(unsigned long timeout_ms) |
icraggs | 20:cad3d54d7ecf | 508 | { |
icraggs | 26:2658bb87c53d | 509 | int rc = SUCCESS; |
DuyLionTran | 56:30f7b745eaaa | 510 | Timer timer; |
icraggs | 43:21da1f744243 | 511 | |
icraggs | 23:05fc7de97d4a | 512 | timer.countdown_ms(timeout_ms); |
icraggs | 20:cad3d54d7ecf | 513 | while (!timer.expired()) |
icraggs | 26:2658bb87c53d | 514 | { |
icraggs | 46:e335fcc1a663 | 515 | if (cycle(timer) < 0) |
icraggs | 26:2658bb87c53d | 516 | { |
icraggs | 26:2658bb87c53d | 517 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 518 | break; |
icraggs | 26:2658bb87c53d | 519 | } |
icraggs | 26:2658bb87c53d | 520 | } |
icraggs | 43:21da1f744243 | 521 | |
icraggs | 26:2658bb87c53d | 522 | return rc; |
icraggs | 20:cad3d54d7ecf | 523 | } |
icraggs | 20:cad3d54d7ecf | 524 | |
icraggs | 20:cad3d54d7ecf | 525 | |
icraggs | 43:21da1f744243 | 526 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 527 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer) |
icraggs | 8:c46930bd6c82 | 528 | { |
icraggs | 8:c46930bd6c82 | 529 | /* get one piece of work off the wire and one pass through */ |
icraggs | 20:cad3d54d7ecf | 530 | |
Ian Craggs |
12:cc7f2d62a393 | 531 | // read the socket, see what work is due |
icraggs | 46:e335fcc1a663 | 532 | int packet_type = readPacket(timer); |
icraggs | 43:21da1f744243 | 533 | |
icraggs | 26:2658bb87c53d | 534 | int len = 0, |
icraggs | 26:2658bb87c53d | 535 | rc = SUCCESS; |
icraggs | 8:c46930bd6c82 | 536 | switch (packet_type) |
icraggs | 8:c46930bd6c82 | 537 | { |
icraggs | 46:e335fcc1a663 | 538 | case FAILURE: |
icraggs | 46:e335fcc1a663 | 539 | case BUFFER_OVERFLOW: |
icraggs | 46:e335fcc1a663 | 540 | rc = packet_type; |
icraggs | 46:e335fcc1a663 | 541 | break; |
icraggs | 15:64a57183aa03 | 542 | case CONNACK: |
icraggs | 8:c46930bd6c82 | 543 | case PUBACK: |
icraggs | 8:c46930bd6c82 | 544 | case SUBACK: |
icraggs | 8:c46930bd6c82 | 545 | break; |
icraggs | 15:64a57183aa03 | 546 | case PUBLISH: |
icraggs | 46:e335fcc1a663 | 547 | { |
icraggs | 46:e335fcc1a663 | 548 | MQTTString topicName = MQTTString_initializer; |
icraggs | 20:cad3d54d7ecf | 549 | Message msg; |
icraggs | 46:e335fcc1a663 | 550 | int intQoS; |
icraggs | 46:e335fcc1a663 | 551 | if (MQTTDeserialize_publish((unsigned char*)&msg.dup, &intQoS, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName, |
icraggs | 36:2f1ada427e56 | 552 | (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 553 | goto exit; |
icraggs | 46:e335fcc1a663 | 554 | msg.qos = (enum QoS)intQoS; |
icraggs | 43:21da1f744243 | 555 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 556 | if (msg.qos != QOS2) |
icraggs | 43:21da1f744243 | 557 | #endif |
icraggs | 31:a51dd239b78e | 558 | deliverMessage(topicName, msg); |
icraggs | 43:21da1f744243 | 559 | #if MQTTCLIENT_QOS2 |
icraggs | 31:a51dd239b78e | 560 | else if (isQoS2msgidFree(msg.id)) |
icraggs | 31:a51dd239b78e | 561 | { |
icraggs | 43:21da1f744243 | 562 | if (useQoS2msgid(msg.id)) |
icraggs | 43:21da1f744243 | 563 | deliverMessage(topicName, msg); |
icraggs | 43:21da1f744243 | 564 | else |
icraggs | 43:21da1f744243 | 565 | WARN("Maximum number of incoming QoS2 messages exceeded"); |
icraggs | 46:e335fcc1a663 | 566 | } |
icraggs | 31:a51dd239b78e | 567 | #endif |
icraggs | 43:21da1f744243 | 568 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 20:cad3d54d7ecf | 569 | if (msg.qos != QOS0) |
icraggs | 20:cad3d54d7ecf | 570 | { |
icraggs | 20:cad3d54d7ecf | 571 | if (msg.qos == QOS1) |
icraggs | 43:21da1f744243 | 572 | len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id); |
icraggs | 20:cad3d54d7ecf | 573 | else if (msg.qos == QOS2) |
icraggs | 43:21da1f744243 | 574 | len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id); |
icraggs | 26:2658bb87c53d | 575 | if (len <= 0) |
icraggs | 26:2658bb87c53d | 576 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 577 | else |
icraggs | 26:2658bb87c53d | 578 | rc = sendPacket(len, timer); |
icraggs | 26:2658bb87c53d | 579 | if (rc == FAILURE) |
icraggs | 20:cad3d54d7ecf | 580 | goto exit; // there was a problem |
icraggs | 20:cad3d54d7ecf | 581 | } |
Ian Craggs |
12:cc7f2d62a393 | 582 | break; |
icraggs | 43:21da1f744243 | 583 | #endif |
icraggs | 46:e335fcc1a663 | 584 | } |
icraggs | 43:21da1f744243 | 585 | #if MQTTCLIENT_QOS2 |
icraggs | 15:64a57183aa03 | 586 | case PUBREC: |
icraggs | 46:e335fcc1a663 | 587 | case PUBREL: |
icraggs | 36:2f1ada427e56 | 588 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 589 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 590 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 591 | rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 592 | else if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, |
icraggs | 46:e335fcc1a663 | 593 | (packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0) |
icraggs | 26:2658bb87c53d | 594 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 595 | else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet |
icraggs | 26:2658bb87c53d | 596 | rc = FAILURE; // there was a problem |
icraggs | 26:2658bb87c53d | 597 | if (rc == FAILURE) |
icraggs | 20:cad3d54d7ecf | 598 | goto exit; // there was a problem |
icraggs | 46:e335fcc1a663 | 599 | if (packet_type == PUBREL) |
icraggs | 46:e335fcc1a663 | 600 | freeQoS2msgid(mypacketid); |
icraggs | 8:c46930bd6c82 | 601 | break; |
icraggs | 46:e335fcc1a663 | 602 | |
icraggs | 8:c46930bd6c82 | 603 | case PUBCOMP: |
icraggs | 8:c46930bd6c82 | 604 | break; |
icraggs | 43:21da1f744243 | 605 | #endif |
icraggs | 15:64a57183aa03 | 606 | case PINGRESP: |
icraggs | 20:cad3d54d7ecf | 607 | ping_outstanding = false; |
icraggs | 8:c46930bd6c82 | 608 | break; |
icraggs | 15:64a57183aa03 | 609 | } |
icraggs | 20:cad3d54d7ecf | 610 | keepalive(); |
Ian Craggs |
12:cc7f2d62a393 | 611 | exit: |
icraggs | 26:2658bb87c53d | 612 | if (rc == SUCCESS) |
icraggs | 26:2658bb87c53d | 613 | rc = packet_type; |
DuyLionTran | 56:30f7b745eaaa | 614 | |
icraggs | 26:2658bb87c53d | 615 | return rc; |
icraggs | 15:64a57183aa03 | 616 | } |
icraggs | 15:64a57183aa03 | 617 | |
icraggs | 15:64a57183aa03 | 618 | |
icraggs | 23:05fc7de97d4a | 619 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 620 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive() |
icraggs | 15:64a57183aa03 | 621 | { |
icraggs | 26:2658bb87c53d | 622 | int rc = FAILURE; |
icraggs | 15:64a57183aa03 | 623 | |
icraggs | 20:cad3d54d7ecf | 624 | if (keepAliveInterval == 0) |
icraggs | 26:2658bb87c53d | 625 | { |
icraggs | 26:2658bb87c53d | 626 | rc = SUCCESS; |
icraggs | 20:cad3d54d7ecf | 627 | goto exit; |
icraggs | 26:2658bb87c53d | 628 | } |
icraggs | 15:64a57183aa03 | 629 | |
icraggs | 43:21da1f744243 | 630 | if (last_sent.expired() || last_received.expired()) |
icraggs | 20:cad3d54d7ecf | 631 | { |
icraggs | 26:2658bb87c53d | 632 | if (!ping_outstanding) |
icraggs | 20:cad3d54d7ecf | 633 | { |
icraggs | 46:e335fcc1a663 | 634 | Timer timer(1000); |
icraggs | 43:21da1f744243 | 635 | int len = MQTTSerialize_pingreq(sendbuf, MAX_MQTT_PACKET_SIZE); |
icraggs | 26:2658bb87c53d | 636 | if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet |
icraggs | 20:cad3d54d7ecf | 637 | ping_outstanding = true; |
icraggs | 20:cad3d54d7ecf | 638 | } |
icraggs | 20:cad3d54d7ecf | 639 | } |
icraggs | 15:64a57183aa03 | 640 | |
icraggs | 15:64a57183aa03 | 641 | exit: |
icraggs | 20:cad3d54d7ecf | 642 | return rc; |
icraggs | 8:c46930bd6c82 | 643 | } |
icraggs | 8:c46930bd6c82 | 644 | |
icraggs | 8:c46930bd6c82 | 645 | |
icraggs | 16:91c2f9a144d4 | 646 | // only used in single-threaded mode where one command at a time is in process |
icraggs | 43:21da1f744243 | 647 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 648 | int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer) |
icraggs | 15:64a57183aa03 | 649 | { |
icraggs | 26:2658bb87c53d | 650 | int rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 651 | do |
DuyLionTran | 56:30f7b745eaaa | 652 | { |
icraggs | 43:21da1f744243 | 653 | if (timer.expired()) |
icraggs | 20:cad3d54d7ecf | 654 | break; // we timed out |
icraggs | 20:cad3d54d7ecf | 655 | } |
icraggs | 43:21da1f744243 | 656 | while ((rc = cycle(timer)) != packet_type); |
icraggs | 43:21da1f744243 | 657 | |
icraggs | 20:cad3d54d7ecf | 658 | return rc; |
icraggs | 16:91c2f9a144d4 | 659 | } |
icraggs | 16:91c2f9a144d4 | 660 | |
icraggs | 16:91c2f9a144d4 | 661 | |
icraggs | 43:21da1f744243 | 662 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 663 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData& options) |
icraggs | 16:91c2f9a144d4 | 664 | { |
icraggs | 46:e335fcc1a663 | 665 | Timer connect_timer(command_timeout_ms); |
icraggs | 26:2658bb87c53d | 666 | int rc = FAILURE; |
icraggs | 31:a51dd239b78e | 667 | int len = 0; |
icraggs | 43:21da1f744243 | 668 | |
icraggs | 31:a51dd239b78e | 669 | if (isconnected) // don't send connect packet again if we are already connected |
icraggs | 31:a51dd239b78e | 670 | goto exit; |
icraggs | 8:c46930bd6c82 | 671 | |
icraggs | 43:21da1f744243 | 672 | this->keepAliveInterval = options.keepAliveInterval; |
icraggs | 43:21da1f744243 | 673 | this->cleansession = options.cleansession; |
icraggs | 43:21da1f744243 | 674 | if ((len = MQTTSerialize_connect(sendbuf, MAX_MQTT_PACKET_SIZE, &options)) <= 0) |
icraggs | 26:2658bb87c53d | 675 | goto exit; |
icraggs | 26:2658bb87c53d | 676 | if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet |
icraggs | 20:cad3d54d7ecf | 677 | goto exit; // there was a problem |
icraggs | 43:21da1f744243 | 678 | |
icraggs | 43:21da1f744243 | 679 | if (this->keepAliveInterval > 0) |
icraggs | 43:21da1f744243 | 680 | last_received.countdown(this->keepAliveInterval); |
icraggs | 20:cad3d54d7ecf | 681 | // this will be a blocking call, wait for the connack |
icraggs | 20:cad3d54d7ecf | 682 | if (waitfor(CONNACK, connect_timer) == CONNACK) |
icraggs | 15:64a57183aa03 | 683 | { |
icraggs | 36:2f1ada427e56 | 684 | unsigned char connack_rc = 255; |
icraggs | 37:e3d64f9b986c | 685 | bool sessionPresent = false; |
icraggs | 37:e3d64f9b986c | 686 | if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 20:cad3d54d7ecf | 687 | rc = connack_rc; |
icraggs | 26:2658bb87c53d | 688 | else |
icraggs | 26:2658bb87c53d | 689 | rc = FAILURE; |
icraggs | 8:c46930bd6c82 | 690 | } |
icraggs | 26:2658bb87c53d | 691 | else |
icraggs | 26:2658bb87c53d | 692 | rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 693 | |
icraggs | 43:21da1f744243 | 694 | #if MQTTCLIENT_QOS2 |
icraggs | 46:e335fcc1a663 | 695 | // resend any inflight publish |
icraggs | 46:e335fcc1a663 | 696 | if (inflightMsgid > 0 && inflightQoS == QOS2 && pubrel) |
icraggs | 43:21da1f744243 | 697 | { |
icraggs | 43:21da1f744243 | 698 | if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, inflightMsgid)) <= 0) |
icraggs | 43:21da1f744243 | 699 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 700 | else |
icraggs | 43:21da1f744243 | 701 | rc = publish(len, connect_timer, inflightQoS); |
icraggs | 43:21da1f744243 | 702 | } |
icraggs | 43:21da1f744243 | 703 | else |
icraggs | 43:21da1f744243 | 704 | #endif |
icraggs | 43:21da1f744243 | 705 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 706 | if (inflightMsgid > 0) |
icraggs | 43:21da1f744243 | 707 | { |
icraggs | 43:21da1f744243 | 708 | memcpy(sendbuf, pubbuf, MAX_MQTT_PACKET_SIZE); |
icraggs | 43:21da1f744243 | 709 | rc = publish(inflightLen, connect_timer, inflightQoS); |
icraggs | 43:21da1f744243 | 710 | } |
icraggs | 43:21da1f744243 | 711 | #endif |
icraggs | 43:21da1f744243 | 712 | |
icraggs | 15:64a57183aa03 | 713 | exit: |
DuyLionTran | 56:30f7b745eaaa | 714 | if (rc == SUCCESS) { |
icraggs | 26:2658bb87c53d | 715 | isconnected = true; |
DuyLionTran | 56:30f7b745eaaa | 716 | } |
icraggs | 8:c46930bd6c82 | 717 | return rc; |
icraggs | 8:c46930bd6c82 | 718 | } |
icraggs | 8:c46930bd6c82 | 719 | |
icraggs | 8:c46930bd6c82 | 720 | |
icraggs | 43:21da1f744243 | 721 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 722 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect() |
icraggs | 43:21da1f744243 | 723 | { |
icraggs | 43:21da1f744243 | 724 | MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer; |
icraggs | 43:21da1f744243 | 725 | return connect(default_options); |
icraggs | 43:21da1f744243 | 726 | } |
icraggs | 43:21da1f744243 | 727 | |
icraggs | 43:21da1f744243 | 728 | |
icraggs | 43:21da1f744243 | 729 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 730 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler) |
icraggs | 43:21da1f744243 | 731 | { |
icraggs | 43:21da1f744243 | 732 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 733 | Timer timer(command_timeout_ms); |
icraggs | 26:2658bb87c53d | 734 | int len = 0; |
icraggs | 46:e335fcc1a663 | 735 | MQTTString topic = {(char*)topicFilter, {0, 0}}; |
icraggs | 43:21da1f744243 | 736 | |
icraggs | 26:2658bb87c53d | 737 | if (!isconnected) |
DuyLionTran | 56:30f7b745eaaa | 738 | { |
DuyLionTran | 56:30f7b745eaaa | 739 | printf("Not connected\r\n"); |
icraggs | 20:cad3d54d7ecf | 740 | goto exit; |
DuyLionTran | 56:30f7b745eaaa | 741 | } |
icraggs | 43:21da1f744243 | 742 | |
DuyLionTran | 56:30f7b745eaaa | 743 | len = MQTTSerialize_subscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int)qos); |
icraggs | 26:2658bb87c53d | 744 | if (len <= 0) |
DuyLionTran | 56:30f7b745eaaa | 745 | { |
icraggs | 26:2658bb87c53d | 746 | goto exit; |
DuyLionTran | 56:30f7b745eaaa | 747 | } |
DuyLionTran | 56:30f7b745eaaa | 748 | |
icraggs | 23:05fc7de97d4a | 749 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet |
DuyLionTran | 56:30f7b745eaaa | 750 | { |
icraggs | 26:2658bb87c53d | 751 | goto exit; // there was a problem |
DuyLionTran | 56:30f7b745eaaa | 752 | } |
icraggs | 43:21da1f744243 | 753 | |
icraggs | 43:21da1f744243 | 754 | if (waitfor(SUBACK, timer) == SUBACK) // wait for suback |
icraggs | 8:c46930bd6c82 | 755 | { |
icraggs | 36:2f1ada427e56 | 756 | int count = 0, grantedQoS = -1; |
icraggs | 36:2f1ada427e56 | 757 | unsigned short mypacketid; |
icraggs | 23:05fc7de97d4a | 758 | if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 43:21da1f744243 | 759 | rc = grantedQoS; // 0, 1, 2 or 0x80 |
icraggs | 20:cad3d54d7ecf | 760 | if (rc != 0x80) |
icraggs | 8:c46930bd6c82 | 761 | { |
icraggs | 23:05fc7de97d4a | 762 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 16:91c2f9a144d4 | 763 | { |
icraggs | 26:2658bb87c53d | 764 | if (messageHandlers[i].topicFilter == 0) |
icraggs | 20:cad3d54d7ecf | 765 | { |
icraggs | 26:2658bb87c53d | 766 | messageHandlers[i].topicFilter = topicFilter; |
icraggs | 20:cad3d54d7ecf | 767 | messageHandlers[i].fp.attach(messageHandler); |
icraggs | 20:cad3d54d7ecf | 768 | rc = 0; |
icraggs | 20:cad3d54d7ecf | 769 | break; |
icraggs | 20:cad3d54d7ecf | 770 | } |
icraggs | 16:91c2f9a144d4 | 771 | } |
icraggs | 8:c46930bd6c82 | 772 | } |
icraggs | 8:c46930bd6c82 | 773 | } |
icraggs | 43:21da1f744243 | 774 | else |
DuyLionTran | 56:30f7b745eaaa | 775 | { |
icraggs | 26:2658bb87c53d | 776 | rc = FAILURE; |
DuyLionTran | 56:30f7b745eaaa | 777 | } |
icraggs | 43:21da1f744243 | 778 | |
icraggs | 15:64a57183aa03 | 779 | exit: |
icraggs | 43:21da1f744243 | 780 | if (rc != SUCCESS) |
icraggs | 46:e335fcc1a663 | 781 | cleanSession(); |
Ian Craggs |
12:cc7f2d62a393 | 782 | return rc; |
icraggs | 15:64a57183aa03 | 783 | } |
icraggs | 15:64a57183aa03 | 784 | |
icraggs | 15:64a57183aa03 | 785 | |
icraggs | 43:21da1f744243 | 786 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 787 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter) |
icraggs | 43:21da1f744243 | 788 | { |
icraggs | 26:2658bb87c53d | 789 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 790 | Timer timer(command_timeout_ms); |
icraggs | 46:e335fcc1a663 | 791 | MQTTString topic = {(char*)topicFilter, {0, 0}}; |
icraggs | 31:a51dd239b78e | 792 | int len = 0; |
icraggs | 43:21da1f744243 | 793 | |
DuyLionTran | 56:30f7b745eaaa | 794 | if (!isconnected) { |
DuyLionTran | 56:30f7b745eaaa | 795 | |
DuyLionTran | 56:30f7b745eaaa | 796 | goto exit; |
DuyLionTran | 56:30f7b745eaaa | 797 | } |
icraggs | 43:21da1f744243 | 798 | |
icraggs | 43:21da1f744243 | 799 | if ((len = MQTTSerialize_unsubscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0) |
icraggs | 20:cad3d54d7ecf | 800 | goto exit; |
icraggs | 43:21da1f744243 | 801 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the unsubscribe packet |
icraggs | 20:cad3d54d7ecf | 802 | goto exit; // there was a problem |
icraggs | 43:21da1f744243 | 803 | |
icraggs | 20:cad3d54d7ecf | 804 | if (waitfor(UNSUBACK, timer) == UNSUBACK) |
Ian Craggs |
12:cc7f2d62a393 | 805 | { |
icraggs | 36:2f1ada427e56 | 806 | unsigned short mypacketid; // should be the same as the packetid above |
icraggs | 23:05fc7de97d4a | 807 | if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 46:e335fcc1a663 | 808 | { |
icraggs | 43:21da1f744243 | 809 | rc = 0; |
icraggs | 46:e335fcc1a663 | 810 | |
icraggs | 46:e335fcc1a663 | 811 | // remove the subscription message handler associated with this topic, if there is one |
icraggs | 46:e335fcc1a663 | 812 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 46:e335fcc1a663 | 813 | { |
icraggs | 46:e335fcc1a663 | 814 | if (messageHandlers[i].topicFilter && strcmp(messageHandlers[i].topicFilter, topicFilter) == 0) |
icraggs | 46:e335fcc1a663 | 815 | { |
icraggs | 46:e335fcc1a663 | 816 | messageHandlers[i].topicFilter = 0; |
icraggs | 46:e335fcc1a663 | 817 | break; |
icraggs | 46:e335fcc1a663 | 818 | } |
icraggs | 46:e335fcc1a663 | 819 | } |
icraggs | 46:e335fcc1a663 | 820 | } |
Ian Craggs |
12:cc7f2d62a393 | 821 | } |
icraggs | 26:2658bb87c53d | 822 | else |
icraggs | 26:2658bb87c53d | 823 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 824 | |
icraggs | 15:64a57183aa03 | 825 | exit: |
icraggs | 43:21da1f744243 | 826 | if (rc != SUCCESS) |
icraggs | 46:e335fcc1a663 | 827 | cleanSession(); |
Ian Craggs |
12:cc7f2d62a393 | 828 | return rc; |
icraggs | 15:64a57183aa03 | 829 | } |
icraggs | 15:64a57183aa03 | 830 | |
icraggs | 15:64a57183aa03 | 831 | |
icraggs | 43:21da1f744243 | 832 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 833 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Timer& timer, enum QoS qos) |
icraggs | 20:cad3d54d7ecf | 834 | { |
icraggs | 43:21da1f744243 | 835 | int rc; |
icraggs | 46:e335fcc1a663 | 836 | |
icraggs | 43:21da1f744243 | 837 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the publish packet |
icraggs | 43:21da1f744243 | 838 | goto exit; // there was a problem |
icraggs | 15:64a57183aa03 | 839 | |
icraggs | 46:e335fcc1a663 | 840 | #if MQTTCLIENT_QOS1 |
icraggs | 43:21da1f744243 | 841 | if (qos == QOS1) |
Ian Craggs |
12:cc7f2d62a393 | 842 | { |
icraggs | 20:cad3d54d7ecf | 843 | if (waitfor(PUBACK, timer) == PUBACK) |
icraggs | 20:cad3d54d7ecf | 844 | { |
icraggs | 36:2f1ada427e56 | 845 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 846 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 847 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 848 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 849 | else if (inflightMsgid == mypacketid) |
icraggs | 43:21da1f744243 | 850 | inflightMsgid = 0; |
icraggs | 20:cad3d54d7ecf | 851 | } |
icraggs | 26:2658bb87c53d | 852 | else |
icraggs | 26:2658bb87c53d | 853 | rc = FAILURE; |
Ian Craggs |
12:cc7f2d62a393 | 854 | } |
icraggs | 43:21da1f744243 | 855 | #elif MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 856 | else if (qos == QOS2) |
Ian Craggs |
12:cc7f2d62a393 | 857 | { |
icraggs | 20:cad3d54d7ecf | 858 | if (waitfor(PUBCOMP, timer) == PUBCOMP) |
icraggs | 20:cad3d54d7ecf | 859 | { |
icraggs | 36:2f1ada427e56 | 860 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 861 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 862 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 863 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 864 | else if (inflightMsgid == mypacketid) |
icraggs | 43:21da1f744243 | 865 | inflightMsgid = 0; |
icraggs | 20:cad3d54d7ecf | 866 | } |
icraggs | 26:2658bb87c53d | 867 | else |
icraggs | 26:2658bb87c53d | 868 | rc = FAILURE; |
Ian Craggs |
12:cc7f2d62a393 | 869 | } |
icraggs | 43:21da1f744243 | 870 | #endif |
icraggs | 43:21da1f744243 | 871 | |
icraggs | 43:21da1f744243 | 872 | exit: |
icraggs | 43:21da1f744243 | 873 | if (rc != SUCCESS) |
icraggs | 46:e335fcc1a663 | 874 | cleanSession(); |
icraggs | 43:21da1f744243 | 875 | return rc; |
icraggs | 43:21da1f744243 | 876 | } |
icraggs | 43:21da1f744243 | 877 | |
icraggs | 43:21da1f744243 | 878 | |
icraggs | 43:21da1f744243 | 879 | |
icraggs | 43:21da1f744243 | 880 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 881 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum QoS qos, bool retained) |
icraggs | 43:21da1f744243 | 882 | { |
icraggs | 43:21da1f744243 | 883 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 884 | Timer timer(command_timeout_ms); |
icraggs | 43:21da1f744243 | 885 | MQTTString topicString = MQTTString_initializer; |
icraggs | 43:21da1f744243 | 886 | int len = 0; |
icraggs | 43:21da1f744243 | 887 | |
DuyLionTran | 56:30f7b745eaaa | 888 | if (!isconnected) { |
icraggs | 43:21da1f744243 | 889 | goto exit; |
DuyLionTran | 56:30f7b745eaaa | 890 | } |
icraggs | 43:21da1f744243 | 891 | topicString.cstring = (char*)topicName; |
icraggs | 43:21da1f744243 | 892 | |
icraggs | 43:21da1f744243 | 893 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 894 | if (qos == QOS1 || qos == QOS2) |
icraggs | 43:21da1f744243 | 895 | id = packetid.getNext(); |
icraggs | 43:21da1f744243 | 896 | #endif |
icraggs | 43:21da1f744243 | 897 | len = MQTTSerialize_publish(sendbuf, MAX_MQTT_PACKET_SIZE, 0, qos, retained, id, |
icraggs | 43:21da1f744243 | 898 | topicString, (unsigned char*)payload, payloadlen); |
icraggs | 43:21da1f744243 | 899 | if (len <= 0) |
icraggs | 43:21da1f744243 | 900 | goto exit; |
icraggs | 46:e335fcc1a663 | 901 | |
icraggs | 43:21da1f744243 | 902 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 903 | if (!cleansession) |
icraggs | 43:21da1f744243 | 904 | { |
icraggs | 43:21da1f744243 | 905 | memcpy(pubbuf, sendbuf, len); |
icraggs | 43:21da1f744243 | 906 | inflightMsgid = id; |
icraggs | 43:21da1f744243 | 907 | inflightLen = len; |
icraggs | 43:21da1f744243 | 908 | inflightQoS = qos; |
icraggs | 43:21da1f744243 | 909 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 910 | pubrel = false; |
icraggs | 43:21da1f744243 | 911 | #endif |
icraggs | 43:21da1f744243 | 912 | } |
icraggs | 43:21da1f744243 | 913 | #endif |
icraggs | 46:e335fcc1a663 | 914 | |
icraggs | 43:21da1f744243 | 915 | rc = publish(len, timer, qos); |
icraggs | 15:64a57183aa03 | 916 | exit: |
icraggs | 8:c46930bd6c82 | 917 | return rc; |
icraggs | 8:c46930bd6c82 | 918 | } |
icraggs | 8:c46930bd6c82 | 919 | |
icraggs | 8:c46930bd6c82 | 920 | |
icraggs | 43:21da1f744243 | 921 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 922 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, void* payload, size_t payloadlen, enum QoS qos, bool retained) |
icraggs | 43:21da1f744243 | 923 | { |
icraggs | 43:21da1f744243 | 924 | unsigned short id = 0; // dummy - not used for anything |
icraggs | 43:21da1f744243 | 925 | return publish(topicName, payload, payloadlen, id, qos, retained); |
icraggs | 43:21da1f744243 | 926 | } |
icraggs | 43:21da1f744243 | 927 | |
icraggs | 43:21da1f744243 | 928 | |
icraggs | 43:21da1f744243 | 929 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 930 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message& message) |
icraggs | 43:21da1f744243 | 931 | { |
icraggs | 43:21da1f744243 | 932 | return publish(topicName, message.payload, message.payloadlen, message.qos, message.retained); |
icraggs | 43:21da1f744243 | 933 | } |
icraggs | 43:21da1f744243 | 934 | |
icraggs | 43:21da1f744243 | 935 | |
icraggs | 43:21da1f744243 | 936 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 937 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect() |
icraggs | 43:21da1f744243 | 938 | { |
icraggs | 26:2658bb87c53d | 939 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 940 | Timer timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete |
icraggs | 44:c299463ae853 | 941 | int len = MQTTSerialize_disconnect(sendbuf, MAX_MQTT_PACKET_SIZE); |
icraggs | 26:2658bb87c53d | 942 | if (len > 0) |
icraggs | 26:2658bb87c53d | 943 | rc = sendPacket(len, timer); // send the disconnect packet |
icraggs | 43:21da1f744243 | 944 | |
icraggs | 46:e335fcc1a663 | 945 | if (cleansession) |
icraggs | 46:e335fcc1a663 | 946 | cleanSession(); |
icraggs | 46:e335fcc1a663 | 947 | else |
icraggs | 46:e335fcc1a663 | 948 | isconnected = false; |
icraggs | 23:05fc7de97d4a | 949 | return rc; |
icraggs | 20:cad3d54d7ecf | 950 | } |
icraggs | 20:cad3d54d7ecf | 951 | |
icraggs | 20:cad3d54d7ecf | 952 | |
icraggs | 46:e335fcc1a663 | 953 | #endif |