cc
MQTTClient.h@50:3a01977eeebc, 2022-06-10 (annotated)
- Committer:
- badri_rym
- Date:
- Fri Jun 10 12:51:45 2022 +0000
- Revision:
- 50:3a01977eeebc
- Parent:
- 49:c66fdbb9eb83
cc
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 | this->command_timeout_ms = command_timeout_ms; |
icraggs | 46:e335fcc1a663 | 280 | cleanSession(); |
icraggs | 46:e335fcc1a663 | 281 | } |
icraggs | 43:21da1f744243 | 282 | |
icraggs | 43:21da1f744243 | 283 | |
icraggs | 43:21da1f744243 | 284 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 285 | template<class Network, class Timer, int a, int b> |
icraggs | 43:21da1f744243 | 286 | bool MQTT::Client<Network, Timer, a, b>::isQoS2msgidFree(unsigned short id) |
icraggs | 43:21da1f744243 | 287 | { |
icraggs | 43:21da1f744243 | 288 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 43:21da1f744243 | 289 | { |
icraggs | 43:21da1f744243 | 290 | if (incomingQoS2messages[i] == id) |
icraggs | 43:21da1f744243 | 291 | return false; |
icraggs | 43:21da1f744243 | 292 | } |
icraggs | 43:21da1f744243 | 293 | return true; |
icraggs | 11:db15da110a37 | 294 | } |
icraggs | 11:db15da110a37 | 295 | |
icraggs | 11:db15da110a37 | 296 | |
icraggs | 43:21da1f744243 | 297 | template<class Network, class Timer, int a, int b> |
icraggs | 43:21da1f744243 | 298 | bool MQTT::Client<Network, Timer, a, b>::useQoS2msgid(unsigned short id) |
icraggs | 43:21da1f744243 | 299 | { |
icraggs | 43:21da1f744243 | 300 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 43:21da1f744243 | 301 | { |
icraggs | 43:21da1f744243 | 302 | if (incomingQoS2messages[i] == 0) |
icraggs | 43:21da1f744243 | 303 | { |
icraggs | 43:21da1f744243 | 304 | incomingQoS2messages[i] = id; |
icraggs | 43:21da1f744243 | 305 | return true; |
icraggs | 43:21da1f744243 | 306 | } |
icraggs | 43:21da1f744243 | 307 | } |
icraggs | 43:21da1f744243 | 308 | return false; |
icraggs | 43:21da1f744243 | 309 | } |
icraggs | 46:e335fcc1a663 | 310 | |
icraggs | 46:e335fcc1a663 | 311 | |
icraggs | 46:e335fcc1a663 | 312 | template<class Network, class Timer, int a, int b> |
icraggs | 46:e335fcc1a663 | 313 | void MQTT::Client<Network, Timer, a, b>::freeQoS2msgid(unsigned short id) |
icraggs | 46:e335fcc1a663 | 314 | { |
icraggs | 46:e335fcc1a663 | 315 | for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i) |
icraggs | 46:e335fcc1a663 | 316 | { |
icraggs | 46:e335fcc1a663 | 317 | if (incomingQoS2messages[i] == id) |
icraggs | 46:e335fcc1a663 | 318 | { |
icraggs | 46:e335fcc1a663 | 319 | incomingQoS2messages[i] = 0; |
icraggs | 46:e335fcc1a663 | 320 | return; |
icraggs | 46:e335fcc1a663 | 321 | } |
icraggs | 46:e335fcc1a663 | 322 | } |
icraggs | 46:e335fcc1a663 | 323 | } |
icraggs | 43:21da1f744243 | 324 | #endif |
icraggs | 43:21da1f744243 | 325 | |
icraggs | 43:21da1f744243 | 326 | |
icraggs | 43:21da1f744243 | 327 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 328 | int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer) |
icraggs | 8:c46930bd6c82 | 329 | { |
icraggs | 43:21da1f744243 | 330 | int rc = FAILURE, |
icraggs | 23:05fc7de97d4a | 331 | sent = 0; |
icraggs | 43:21da1f744243 | 332 | |
icraggs | 23:05fc7de97d4a | 333 | while (sent < length && !timer.expired()) |
icraggs | 23:05fc7de97d4a | 334 | { |
icraggs | 46:e335fcc1a663 | 335 | rc = ipstack.write(&sendbuf[sent], length - sent, timer.left_ms()); |
icraggs | 26:2658bb87c53d | 336 | if (rc < 0) // there was an error writing the data |
icraggs | 26:2658bb87c53d | 337 | break; |
icraggs | 26:2658bb87c53d | 338 | sent += rc; |
icraggs | 23:05fc7de97d4a | 339 | } |
icraggs | 20:cad3d54d7ecf | 340 | if (sent == length) |
icraggs | 23:05fc7de97d4a | 341 | { |
icraggs | 43:21da1f744243 | 342 | if (this->keepAliveInterval > 0) |
icraggs | 43:21da1f744243 | 343 | last_sent.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet |
icraggs | 23:05fc7de97d4a | 344 | rc = SUCCESS; |
icraggs | 23:05fc7de97d4a | 345 | } |
icraggs | 23:05fc7de97d4a | 346 | else |
icraggs | 23:05fc7de97d4a | 347 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 348 | |
icraggs | 43:21da1f744243 | 349 | #if defined(MQTT_DEBUG) |
icraggs | 46:e335fcc1a663 | 350 | char printbuf[150]; |
icraggs | 46:e335fcc1a663 | 351 | DEBUG("Rc %d from sending packet %s\n", rc, MQTTFormat_toServerString(printbuf, sizeof(printbuf), sendbuf, length)); |
icraggs | 43:21da1f744243 | 352 | #endif |
icraggs | 23:05fc7de97d4a | 353 | return rc; |
icraggs | 8:c46930bd6c82 | 354 | } |
icraggs | 8:c46930bd6c82 | 355 | |
icraggs | 8:c46930bd6c82 | 356 | |
icraggs | 43:21da1f744243 | 357 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 358 | int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout) |
icraggs | 8:c46930bd6c82 | 359 | { |
icraggs | 36:2f1ada427e56 | 360 | unsigned char c; |
icraggs | 8:c46930bd6c82 | 361 | int multiplier = 1; |
icraggs | 8:c46930bd6c82 | 362 | int len = 0; |
icraggs | 20:cad3d54d7ecf | 363 | const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4; |
icraggs | 8:c46930bd6c82 | 364 | |
icraggs | 8:c46930bd6c82 | 365 | *value = 0; |
icraggs | 8:c46930bd6c82 | 366 | do |
icraggs | 8:c46930bd6c82 | 367 | { |
icraggs | 8:c46930bd6c82 | 368 | int rc = MQTTPACKET_READ_ERROR; |
icraggs | 8:c46930bd6c82 | 369 | |
icraggs | 8:c46930bd6c82 | 370 | if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES) |
icraggs | 8:c46930bd6c82 | 371 | { |
icraggs | 8:c46930bd6c82 | 372 | rc = MQTTPACKET_READ_ERROR; /* bad data */ |
icraggs | 8:c46930bd6c82 | 373 | goto exit; |
icraggs | 8:c46930bd6c82 | 374 | } |
icraggs | 23:05fc7de97d4a | 375 | rc = ipstack.read(&c, 1, timeout); |
icraggs | 8:c46930bd6c82 | 376 | if (rc != 1) |
icraggs | 8:c46930bd6c82 | 377 | goto exit; |
icraggs | 8:c46930bd6c82 | 378 | *value += (c & 127) * multiplier; |
icraggs | 8:c46930bd6c82 | 379 | multiplier *= 128; |
icraggs | 8:c46930bd6c82 | 380 | } while ((c & 128) != 0); |
icraggs | 8:c46930bd6c82 | 381 | exit: |
icraggs | 8:c46930bd6c82 | 382 | return len; |
icraggs | 8:c46930bd6c82 | 383 | } |
icraggs | 8:c46930bd6c82 | 384 | |
icraggs | 8:c46930bd6c82 | 385 | |
icraggs | 8:c46930bd6c82 | 386 | /** |
icraggs | 8:c46930bd6c82 | 387 | * If any read fails in this method, then we should disconnect from the network, as on reconnect |
icraggs | 43:21da1f744243 | 388 | * the packets can be retried. |
icraggs | 8:c46930bd6c82 | 389 | * @param timeout the max time to wait for the packet read to complete, in milliseconds |
icraggs | 8:c46930bd6c82 | 390 | * @return the MQTT packet type, or -1 if none |
icraggs | 8:c46930bd6c82 | 391 | */ |
icraggs | 46:e335fcc1a663 | 392 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 46:e335fcc1a663 | 393 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::readPacket(Timer& timer) |
icraggs | 8:c46930bd6c82 | 394 | { |
icraggs | 26:2658bb87c53d | 395 | int rc = FAILURE; |
icraggs | 8:c46930bd6c82 | 396 | MQTTHeader header = {0}; |
icraggs | 8:c46930bd6c82 | 397 | int len = 0; |
icraggs | 8:c46930bd6c82 | 398 | int rem_len = 0; |
icraggs | 8:c46930bd6c82 | 399 | |
icraggs | 8:c46930bd6c82 | 400 | /* 1. read the header byte. This has the packet type in it */ |
icraggs | 23:05fc7de97d4a | 401 | if (ipstack.read(readbuf, 1, timer.left_ms()) != 1) |
icraggs | 8:c46930bd6c82 | 402 | goto exit; |
icraggs | 8:c46930bd6c82 | 403 | |
icraggs | 8:c46930bd6c82 | 404 | len = 1; |
icraggs | 8:c46930bd6c82 | 405 | /* 2. read the remaining length. This is variable in itself */ |
icraggs | 20:cad3d54d7ecf | 406 | decodePacket(&rem_len, timer.left_ms()); |
icraggs | 43:21da1f744243 | 407 | len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length into the buffer */ |
icraggs | 8:c46930bd6c82 | 408 | |
icraggs | 46:e335fcc1a663 | 409 | if (rem_len > (MAX_MQTT_PACKET_SIZE - len)) |
icraggs | 46:e335fcc1a663 | 410 | { |
icraggs | 46:e335fcc1a663 | 411 | rc = BUFFER_OVERFLOW; |
icraggs | 46:e335fcc1a663 | 412 | goto exit; |
icraggs | 46:e335fcc1a663 | 413 | } |
icraggs | 46:e335fcc1a663 | 414 | |
icraggs | 8:c46930bd6c82 | 415 | /* 3. read the rest of the buffer using a callback to supply the rest of the data */ |
icraggs | 43:21da1f744243 | 416 | if (rem_len > 0 && (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len)) |
icraggs | 8:c46930bd6c82 | 417 | goto exit; |
icraggs | 8:c46930bd6c82 | 418 | |
icraggs | 8:c46930bd6c82 | 419 | header.byte = readbuf[0]; |
icraggs | 8:c46930bd6c82 | 420 | rc = header.bits.type; |
icraggs | 43:21da1f744243 | 421 | if (this->keepAliveInterval > 0) |
icraggs | 43:21da1f744243 | 422 | last_received.countdown(this->keepAliveInterval); // record the fact that we have successfully received a packet |
icraggs | 8:c46930bd6c82 | 423 | exit: |
icraggs | 43:21da1f744243 | 424 | |
icraggs | 43:21da1f744243 | 425 | #if defined(MQTT_DEBUG) |
icraggs | 46:e335fcc1a663 | 426 | if (rc >= 0) |
icraggs | 46:e335fcc1a663 | 427 | { |
icraggs | 46:e335fcc1a663 | 428 | char printbuf[50]; |
icraggs | 46:e335fcc1a663 | 429 | DEBUG("Rc %d from receiving packet %s\n", rc, MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len)); |
icraggs | 46:e335fcc1a663 | 430 | } |
icraggs | 43:21da1f744243 | 431 | #endif |
icraggs | 8:c46930bd6c82 | 432 | return rc; |
icraggs | 3:dbff6b768d28 | 433 | } |
icraggs | 3:dbff6b768d28 | 434 | |
icraggs | 8:c46930bd6c82 | 435 | |
icraggs | 26:2658bb87c53d | 436 | // assume topic filter and name is in correct format |
icraggs | 26:2658bb87c53d | 437 | // # can only be at end |
icraggs | 26:2658bb87c53d | 438 | // + and # can only be next to separator |
icraggs | 43:21da1f744243 | 439 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 440 | bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName) |
icraggs | 26:2658bb87c53d | 441 | { |
icraggs | 26:2658bb87c53d | 442 | char* curf = topicFilter; |
icraggs | 26:2658bb87c53d | 443 | char* curn = topicName.lenstring.data; |
icraggs | 26:2658bb87c53d | 444 | char* curn_end = curn + topicName.lenstring.len; |
icraggs | 43:21da1f744243 | 445 | |
icraggs | 26:2658bb87c53d | 446 | while (*curf && curn < curn_end) |
icraggs | 26:2658bb87c53d | 447 | { |
icraggs | 26:2658bb87c53d | 448 | if (*curn == '/' && *curf != '/') |
icraggs | 26:2658bb87c53d | 449 | break; |
icraggs | 26:2658bb87c53d | 450 | if (*curf != '+' && *curf != '#' && *curf != *curn) |
icraggs | 26:2658bb87c53d | 451 | break; |
icraggs | 26:2658bb87c53d | 452 | if (*curf == '+') |
icraggs | 26:2658bb87c53d | 453 | { // skip until we meet the next separator, or end of string |
icraggs | 26:2658bb87c53d | 454 | char* nextpos = curn + 1; |
icraggs | 26:2658bb87c53d | 455 | while (nextpos < curn_end && *nextpos != '/') |
icraggs | 26:2658bb87c53d | 456 | nextpos = ++curn + 1; |
icraggs | 26:2658bb87c53d | 457 | } |
icraggs | 26:2658bb87c53d | 458 | else if (*curf == '#') |
icraggs | 26:2658bb87c53d | 459 | curn = curn_end - 1; // skip until end of string |
icraggs | 26:2658bb87c53d | 460 | curf++; |
icraggs | 26:2658bb87c53d | 461 | curn++; |
icraggs | 26:2658bb87c53d | 462 | }; |
icraggs | 43:21da1f744243 | 463 | |
icraggs | 26:2658bb87c53d | 464 | return (curn == curn_end) && (*curf == '\0'); |
icraggs | 26:2658bb87c53d | 465 | } |
icraggs | 26:2658bb87c53d | 466 | |
icraggs | 26:2658bb87c53d | 467 | |
icraggs | 26:2658bb87c53d | 468 | |
icraggs | 43:21da1f744243 | 469 | template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> |
icraggs | 26:2658bb87c53d | 470 | int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message) |
icraggs | 15:64a57183aa03 | 471 | { |
icraggs | 26:2658bb87c53d | 472 | int rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 473 | |
icraggs | 20:cad3d54d7ecf | 474 | // we have to find the right message handler - indexed by topic |
icraggs | 23:05fc7de97d4a | 475 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 20:cad3d54d7ecf | 476 | { |
icraggs | 26:2658bb87c53d | 477 | if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) || |
icraggs | 26:2658bb87c53d | 478 | isTopicMatched((char*)messageHandlers[i].topicFilter, topicName))) |
icraggs | 20:cad3d54d7ecf | 479 | { |
icraggs | 26:2658bb87c53d | 480 | if (messageHandlers[i].fp.attached()) |
icraggs | 26:2658bb87c53d | 481 | { |
icraggs | 31:a51dd239b78e | 482 | MessageData md(topicName, message); |
icraggs | 31:a51dd239b78e | 483 | messageHandlers[i].fp(md); |
icraggs | 26:2658bb87c53d | 484 | rc = SUCCESS; |
icraggs | 26:2658bb87c53d | 485 | } |
icraggs | 20:cad3d54d7ecf | 486 | } |
icraggs | 20:cad3d54d7ecf | 487 | } |
icraggs | 43:21da1f744243 | 488 | |
icraggs | 43:21da1f744243 | 489 | if (rc == FAILURE && defaultMessageHandler.attached()) |
icraggs | 26:2658bb87c53d | 490 | { |
icraggs | 31:a51dd239b78e | 491 | MessageData md(topicName, message); |
icraggs | 31:a51dd239b78e | 492 | defaultMessageHandler(md); |
icraggs | 26:2658bb87c53d | 493 | rc = SUCCESS; |
icraggs | 43:21da1f744243 | 494 | } |
icraggs | 43:21da1f744243 | 495 | |
icraggs | 20:cad3d54d7ecf | 496 | return rc; |
icraggs | 15:64a57183aa03 | 497 | } |
icraggs | 15:64a57183aa03 | 498 | |
icraggs | 15:64a57183aa03 | 499 | |
icraggs | 20:cad3d54d7ecf | 500 | |
icraggs | 43:21da1f744243 | 501 | template<class Network, class Timer, int a, int b> |
icraggs | 43:21da1f744243 | 502 | int MQTT::Client<Network, Timer, a, b>::yield(unsigned long timeout_ms) |
icraggs | 20:cad3d54d7ecf | 503 | { |
icraggs | 26:2658bb87c53d | 504 | int rc = SUCCESS; |
icraggs | 20:cad3d54d7ecf | 505 | Timer timer = Timer(); |
icraggs | 43:21da1f744243 | 506 | |
icraggs | 23:05fc7de97d4a | 507 | timer.countdown_ms(timeout_ms); |
icraggs | 20:cad3d54d7ecf | 508 | while (!timer.expired()) |
icraggs | 26:2658bb87c53d | 509 | { |
icraggs | 46:e335fcc1a663 | 510 | if (cycle(timer) < 0) |
icraggs | 26:2658bb87c53d | 511 | { |
icraggs | 26:2658bb87c53d | 512 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 513 | break; |
icraggs | 26:2658bb87c53d | 514 | } |
icraggs | 26:2658bb87c53d | 515 | } |
icraggs | 43:21da1f744243 | 516 | |
icraggs | 26:2658bb87c53d | 517 | return rc; |
icraggs | 20:cad3d54d7ecf | 518 | } |
icraggs | 20:cad3d54d7ecf | 519 | |
icraggs | 20:cad3d54d7ecf | 520 | |
icraggs | 43:21da1f744243 | 521 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 522 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer) |
icraggs | 8:c46930bd6c82 | 523 | { |
icraggs | 8:c46930bd6c82 | 524 | /* get one piece of work off the wire and one pass through */ |
icraggs | 20:cad3d54d7ecf | 525 | |
Ian Craggs |
12:cc7f2d62a393 | 526 | // read the socket, see what work is due |
icraggs | 46:e335fcc1a663 | 527 | int packet_type = readPacket(timer); |
icraggs | 43:21da1f744243 | 528 | |
icraggs | 26:2658bb87c53d | 529 | int len = 0, |
icraggs | 26:2658bb87c53d | 530 | rc = SUCCESS; |
icraggs | 28:8b2abe9bd814 | 531 | |
icraggs | 8:c46930bd6c82 | 532 | switch (packet_type) |
icraggs | 8:c46930bd6c82 | 533 | { |
icraggs | 46:e335fcc1a663 | 534 | case FAILURE: |
icraggs | 46:e335fcc1a663 | 535 | case BUFFER_OVERFLOW: |
icraggs | 46:e335fcc1a663 | 536 | rc = packet_type; |
icraggs | 46:e335fcc1a663 | 537 | break; |
icraggs | 15:64a57183aa03 | 538 | case CONNACK: |
icraggs | 8:c46930bd6c82 | 539 | case PUBACK: |
icraggs | 8:c46930bd6c82 | 540 | case SUBACK: |
icraggs | 8:c46930bd6c82 | 541 | break; |
icraggs | 15:64a57183aa03 | 542 | case PUBLISH: |
icraggs | 46:e335fcc1a663 | 543 | { |
icraggs | 46:e335fcc1a663 | 544 | MQTTString topicName = MQTTString_initializer; |
icraggs | 20:cad3d54d7ecf | 545 | Message msg; |
icraggs | 46:e335fcc1a663 | 546 | int intQoS; |
icraggs | 46:e335fcc1a663 | 547 | if (MQTTDeserialize_publish((unsigned char*)&msg.dup, &intQoS, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName, |
icraggs | 36:2f1ada427e56 | 548 | (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 549 | goto exit; |
icraggs | 46:e335fcc1a663 | 550 | msg.qos = (enum QoS)intQoS; |
icraggs | 43:21da1f744243 | 551 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 552 | if (msg.qos != QOS2) |
icraggs | 43:21da1f744243 | 553 | #endif |
icraggs | 31:a51dd239b78e | 554 | deliverMessage(topicName, msg); |
icraggs | 43:21da1f744243 | 555 | #if MQTTCLIENT_QOS2 |
icraggs | 31:a51dd239b78e | 556 | else if (isQoS2msgidFree(msg.id)) |
icraggs | 31:a51dd239b78e | 557 | { |
icraggs | 43:21da1f744243 | 558 | if (useQoS2msgid(msg.id)) |
icraggs | 43:21da1f744243 | 559 | deliverMessage(topicName, msg); |
icraggs | 43:21da1f744243 | 560 | else |
icraggs | 43:21da1f744243 | 561 | WARN("Maximum number of incoming QoS2 messages exceeded"); |
icraggs | 46:e335fcc1a663 | 562 | } |
icraggs | 31:a51dd239b78e | 563 | #endif |
icraggs | 43:21da1f744243 | 564 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 20:cad3d54d7ecf | 565 | if (msg.qos != QOS0) |
icraggs | 20:cad3d54d7ecf | 566 | { |
icraggs | 20:cad3d54d7ecf | 567 | if (msg.qos == QOS1) |
icraggs | 43:21da1f744243 | 568 | len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id); |
icraggs | 20:cad3d54d7ecf | 569 | else if (msg.qos == QOS2) |
icraggs | 43:21da1f744243 | 570 | len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id); |
icraggs | 26:2658bb87c53d | 571 | if (len <= 0) |
icraggs | 26:2658bb87c53d | 572 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 573 | else |
icraggs | 26:2658bb87c53d | 574 | rc = sendPacket(len, timer); |
icraggs | 26:2658bb87c53d | 575 | if (rc == FAILURE) |
icraggs | 20:cad3d54d7ecf | 576 | goto exit; // there was a problem |
icraggs | 20:cad3d54d7ecf | 577 | } |
Ian Craggs |
12:cc7f2d62a393 | 578 | break; |
icraggs | 43:21da1f744243 | 579 | #endif |
icraggs | 46:e335fcc1a663 | 580 | } |
icraggs | 43:21da1f744243 | 581 | #if MQTTCLIENT_QOS2 |
icraggs | 15:64a57183aa03 | 582 | case PUBREC: |
icraggs | 46:e335fcc1a663 | 583 | case PUBREL: |
icraggs | 36:2f1ada427e56 | 584 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 585 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 586 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 587 | rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 588 | else if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, |
icraggs | 46:e335fcc1a663 | 589 | (packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0) |
icraggs | 26:2658bb87c53d | 590 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 591 | else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet |
icraggs | 26:2658bb87c53d | 592 | rc = FAILURE; // there was a problem |
icraggs | 26:2658bb87c53d | 593 | if (rc == FAILURE) |
icraggs | 20:cad3d54d7ecf | 594 | goto exit; // there was a problem |
icraggs | 46:e335fcc1a663 | 595 | if (packet_type == PUBREL) |
icraggs | 46:e335fcc1a663 | 596 | freeQoS2msgid(mypacketid); |
icraggs | 8:c46930bd6c82 | 597 | break; |
icraggs | 46:e335fcc1a663 | 598 | |
icraggs | 8:c46930bd6c82 | 599 | case PUBCOMP: |
icraggs | 8:c46930bd6c82 | 600 | break; |
icraggs | 43:21da1f744243 | 601 | #endif |
icraggs | 15:64a57183aa03 | 602 | case PINGRESP: |
icraggs | 20:cad3d54d7ecf | 603 | ping_outstanding = false; |
icraggs | 8:c46930bd6c82 | 604 | break; |
icraggs | 15:64a57183aa03 | 605 | } |
icraggs | 20:cad3d54d7ecf | 606 | keepalive(); |
Ian Craggs |
12:cc7f2d62a393 | 607 | exit: |
icraggs | 26:2658bb87c53d | 608 | if (rc == SUCCESS) |
icraggs | 26:2658bb87c53d | 609 | rc = packet_type; |
icraggs | 26:2658bb87c53d | 610 | return rc; |
icraggs | 15:64a57183aa03 | 611 | } |
icraggs | 15:64a57183aa03 | 612 | |
icraggs | 15:64a57183aa03 | 613 | |
icraggs | 23:05fc7de97d4a | 614 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 615 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive() |
icraggs | 15:64a57183aa03 | 616 | { |
icraggs | 26:2658bb87c53d | 617 | int rc = FAILURE; |
icraggs | 15:64a57183aa03 | 618 | |
icraggs | 20:cad3d54d7ecf | 619 | if (keepAliveInterval == 0) |
icraggs | 26:2658bb87c53d | 620 | { |
icraggs | 26:2658bb87c53d | 621 | rc = SUCCESS; |
icraggs | 20:cad3d54d7ecf | 622 | goto exit; |
icraggs | 26:2658bb87c53d | 623 | } |
icraggs | 15:64a57183aa03 | 624 | |
icraggs | 43:21da1f744243 | 625 | if (last_sent.expired() || last_received.expired()) |
icraggs | 20:cad3d54d7ecf | 626 | { |
icraggs | 26:2658bb87c53d | 627 | if (!ping_outstanding) |
icraggs | 20:cad3d54d7ecf | 628 | { |
icraggs | 46:e335fcc1a663 | 629 | Timer timer(1000); |
icraggs | 43:21da1f744243 | 630 | int len = MQTTSerialize_pingreq(sendbuf, MAX_MQTT_PACKET_SIZE); |
icraggs | 26:2658bb87c53d | 631 | if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet |
icraggs | 20:cad3d54d7ecf | 632 | ping_outstanding = true; |
icraggs | 20:cad3d54d7ecf | 633 | } |
icraggs | 20:cad3d54d7ecf | 634 | } |
icraggs | 15:64a57183aa03 | 635 | |
icraggs | 15:64a57183aa03 | 636 | exit: |
icraggs | 20:cad3d54d7ecf | 637 | return rc; |
icraggs | 8:c46930bd6c82 | 638 | } |
icraggs | 8:c46930bd6c82 | 639 | |
icraggs | 8:c46930bd6c82 | 640 | |
icraggs | 16:91c2f9a144d4 | 641 | // only used in single-threaded mode where one command at a time is in process |
icraggs | 43:21da1f744243 | 642 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 643 | int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer) |
icraggs | 15:64a57183aa03 | 644 | { |
icraggs | 26:2658bb87c53d | 645 | int rc = FAILURE; |
icraggs | 43:21da1f744243 | 646 | |
icraggs | 20:cad3d54d7ecf | 647 | do |
icraggs | 16:91c2f9a144d4 | 648 | { |
icraggs | 43:21da1f744243 | 649 | if (timer.expired()) |
icraggs | 20:cad3d54d7ecf | 650 | break; // we timed out |
icraggs | 20:cad3d54d7ecf | 651 | } |
icraggs | 43:21da1f744243 | 652 | while ((rc = cycle(timer)) != packet_type); |
icraggs | 43:21da1f744243 | 653 | |
icraggs | 20:cad3d54d7ecf | 654 | return rc; |
icraggs | 16:91c2f9a144d4 | 655 | } |
icraggs | 16:91c2f9a144d4 | 656 | |
icraggs | 16:91c2f9a144d4 | 657 | |
icraggs | 43:21da1f744243 | 658 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 659 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData& options) |
icraggs | 16:91c2f9a144d4 | 660 | { |
icraggs | 46:e335fcc1a663 | 661 | Timer connect_timer(command_timeout_ms); |
icraggs | 26:2658bb87c53d | 662 | int rc = FAILURE; |
icraggs | 31:a51dd239b78e | 663 | int len = 0; |
icraggs | 43:21da1f744243 | 664 | |
icraggs | 31:a51dd239b78e | 665 | if (isconnected) // don't send connect packet again if we are already connected |
icraggs | 31:a51dd239b78e | 666 | goto exit; |
icraggs | 8:c46930bd6c82 | 667 | |
icraggs | 43:21da1f744243 | 668 | this->keepAliveInterval = options.keepAliveInterval; |
icraggs | 43:21da1f744243 | 669 | this->cleansession = options.cleansession; |
icraggs | 43:21da1f744243 | 670 | if ((len = MQTTSerialize_connect(sendbuf, MAX_MQTT_PACKET_SIZE, &options)) <= 0) |
icraggs | 26:2658bb87c53d | 671 | goto exit; |
icraggs | 26:2658bb87c53d | 672 | if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet |
icraggs | 20:cad3d54d7ecf | 673 | goto exit; // there was a problem |
icraggs | 43:21da1f744243 | 674 | |
icraggs | 43:21da1f744243 | 675 | if (this->keepAliveInterval > 0) |
icraggs | 43:21da1f744243 | 676 | last_received.countdown(this->keepAliveInterval); |
icraggs | 20:cad3d54d7ecf | 677 | // this will be a blocking call, wait for the connack |
icraggs | 20:cad3d54d7ecf | 678 | if (waitfor(CONNACK, connect_timer) == CONNACK) |
icraggs | 15:64a57183aa03 | 679 | { |
icraggs | 36:2f1ada427e56 | 680 | unsigned char connack_rc = 255; |
icraggs | 37:e3d64f9b986c | 681 | bool sessionPresent = false; |
icraggs | 37:e3d64f9b986c | 682 | if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 20:cad3d54d7ecf | 683 | rc = connack_rc; |
icraggs | 26:2658bb87c53d | 684 | else |
icraggs | 26:2658bb87c53d | 685 | rc = FAILURE; |
icraggs | 8:c46930bd6c82 | 686 | } |
icraggs | 26:2658bb87c53d | 687 | else |
icraggs | 26:2658bb87c53d | 688 | rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 689 | |
icraggs | 43:21da1f744243 | 690 | #if MQTTCLIENT_QOS2 |
icraggs | 46:e335fcc1a663 | 691 | // resend any inflight publish |
icraggs | 46:e335fcc1a663 | 692 | if (inflightMsgid > 0 && inflightQoS == QOS2 && pubrel) |
icraggs | 43:21da1f744243 | 693 | { |
icraggs | 43:21da1f744243 | 694 | if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, inflightMsgid)) <= 0) |
icraggs | 43:21da1f744243 | 695 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 696 | else |
icraggs | 43:21da1f744243 | 697 | rc = publish(len, connect_timer, inflightQoS); |
icraggs | 43:21da1f744243 | 698 | } |
icraggs | 43:21da1f744243 | 699 | else |
icraggs | 43:21da1f744243 | 700 | #endif |
icraggs | 43:21da1f744243 | 701 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 702 | if (inflightMsgid > 0) |
icraggs | 43:21da1f744243 | 703 | { |
icraggs | 43:21da1f744243 | 704 | memcpy(sendbuf, pubbuf, MAX_MQTT_PACKET_SIZE); |
icraggs | 43:21da1f744243 | 705 | rc = publish(inflightLen, connect_timer, inflightQoS); |
icraggs | 43:21da1f744243 | 706 | } |
icraggs | 43:21da1f744243 | 707 | #endif |
icraggs | 43:21da1f744243 | 708 | |
icraggs | 15:64a57183aa03 | 709 | exit: |
icraggs | 26:2658bb87c53d | 710 | if (rc == SUCCESS) |
icraggs | 26:2658bb87c53d | 711 | isconnected = true; |
icraggs | 8:c46930bd6c82 | 712 | return rc; |
icraggs | 8:c46930bd6c82 | 713 | } |
icraggs | 8:c46930bd6c82 | 714 | |
icraggs | 8:c46930bd6c82 | 715 | |
icraggs | 43:21da1f744243 | 716 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 717 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect() |
icraggs | 43:21da1f744243 | 718 | { |
icraggs | 43:21da1f744243 | 719 | MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer; |
icraggs | 43:21da1f744243 | 720 | return connect(default_options); |
icraggs | 43:21da1f744243 | 721 | } |
icraggs | 43:21da1f744243 | 722 | |
icraggs | 43:21da1f744243 | 723 | |
icraggs | 43:21da1f744243 | 724 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 725 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler) |
icraggs | 43:21da1f744243 | 726 | { |
icraggs | 43:21da1f744243 | 727 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 728 | Timer timer(command_timeout_ms); |
icraggs | 26:2658bb87c53d | 729 | int len = 0; |
icraggs | 46:e335fcc1a663 | 730 | MQTTString topic = {(char*)topicFilter, {0, 0}}; |
icraggs | 43:21da1f744243 | 731 | |
icraggs | 26:2658bb87c53d | 732 | if (!isconnected) |
icraggs | 20:cad3d54d7ecf | 733 | goto exit; |
icraggs | 43:21da1f744243 | 734 | |
icraggs | 43:21da1f744243 | 735 | len = MQTTSerialize_subscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos); |
icraggs | 26:2658bb87c53d | 736 | if (len <= 0) |
icraggs | 26:2658bb87c53d | 737 | goto exit; |
icraggs | 23:05fc7de97d4a | 738 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet |
icraggs | 26:2658bb87c53d | 739 | goto exit; // there was a problem |
icraggs | 43:21da1f744243 | 740 | |
icraggs | 43:21da1f744243 | 741 | if (waitfor(SUBACK, timer) == SUBACK) // wait for suback |
icraggs | 8:c46930bd6c82 | 742 | { |
icraggs | 36:2f1ada427e56 | 743 | int count = 0, grantedQoS = -1; |
icraggs | 36:2f1ada427e56 | 744 | unsigned short mypacketid; |
icraggs | 23:05fc7de97d4a | 745 | if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 43:21da1f744243 | 746 | rc = grantedQoS; // 0, 1, 2 or 0x80 |
icraggs | 20:cad3d54d7ecf | 747 | if (rc != 0x80) |
icraggs | 8:c46930bd6c82 | 748 | { |
icraggs | 23:05fc7de97d4a | 749 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 16:91c2f9a144d4 | 750 | { |
icraggs | 26:2658bb87c53d | 751 | if (messageHandlers[i].topicFilter == 0) |
icraggs | 20:cad3d54d7ecf | 752 | { |
icraggs | 26:2658bb87c53d | 753 | messageHandlers[i].topicFilter = topicFilter; |
icraggs | 20:cad3d54d7ecf | 754 | messageHandlers[i].fp.attach(messageHandler); |
icraggs | 20:cad3d54d7ecf | 755 | rc = 0; |
icraggs | 20:cad3d54d7ecf | 756 | break; |
icraggs | 20:cad3d54d7ecf | 757 | } |
icraggs | 16:91c2f9a144d4 | 758 | } |
icraggs | 8:c46930bd6c82 | 759 | } |
icraggs | 8:c46930bd6c82 | 760 | } |
icraggs | 43:21da1f744243 | 761 | else |
icraggs | 26:2658bb87c53d | 762 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 763 | |
icraggs | 15:64a57183aa03 | 764 | exit: |
icraggs | 43:21da1f744243 | 765 | if (rc != SUCCESS) |
icraggs | 46:e335fcc1a663 | 766 | cleanSession(); |
Ian Craggs |
12:cc7f2d62a393 | 767 | return rc; |
icraggs | 15:64a57183aa03 | 768 | } |
icraggs | 15:64a57183aa03 | 769 | |
icraggs | 15:64a57183aa03 | 770 | |
icraggs | 43:21da1f744243 | 771 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 772 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter) |
icraggs | 43:21da1f744243 | 773 | { |
icraggs | 26:2658bb87c53d | 774 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 775 | Timer timer(command_timeout_ms); |
icraggs | 46:e335fcc1a663 | 776 | MQTTString topic = {(char*)topicFilter, {0, 0}}; |
icraggs | 31:a51dd239b78e | 777 | int len = 0; |
icraggs | 43:21da1f744243 | 778 | |
icraggs | 31:a51dd239b78e | 779 | if (!isconnected) |
icraggs | 31:a51dd239b78e | 780 | goto exit; |
icraggs | 43:21da1f744243 | 781 | |
icraggs | 43:21da1f744243 | 782 | if ((len = MQTTSerialize_unsubscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0) |
icraggs | 20:cad3d54d7ecf | 783 | goto exit; |
icraggs | 43:21da1f744243 | 784 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the unsubscribe packet |
icraggs | 20:cad3d54d7ecf | 785 | goto exit; // there was a problem |
icraggs | 43:21da1f744243 | 786 | |
icraggs | 20:cad3d54d7ecf | 787 | if (waitfor(UNSUBACK, timer) == UNSUBACK) |
Ian Craggs |
12:cc7f2d62a393 | 788 | { |
icraggs | 36:2f1ada427e56 | 789 | unsigned short mypacketid; // should be the same as the packetid above |
icraggs | 23:05fc7de97d4a | 790 | if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 46:e335fcc1a663 | 791 | { |
icraggs | 43:21da1f744243 | 792 | rc = 0; |
icraggs | 46:e335fcc1a663 | 793 | |
icraggs | 46:e335fcc1a663 | 794 | // remove the subscription message handler associated with this topic, if there is one |
icraggs | 46:e335fcc1a663 | 795 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 46:e335fcc1a663 | 796 | { |
icraggs | 46:e335fcc1a663 | 797 | if (messageHandlers[i].topicFilter && strcmp(messageHandlers[i].topicFilter, topicFilter) == 0) |
icraggs | 46:e335fcc1a663 | 798 | { |
icraggs | 46:e335fcc1a663 | 799 | messageHandlers[i].topicFilter = 0; |
icraggs | 46:e335fcc1a663 | 800 | break; |
icraggs | 46:e335fcc1a663 | 801 | } |
icraggs | 46:e335fcc1a663 | 802 | } |
icraggs | 46:e335fcc1a663 | 803 | } |
Ian Craggs |
12:cc7f2d62a393 | 804 | } |
icraggs | 26:2658bb87c53d | 805 | else |
icraggs | 26:2658bb87c53d | 806 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 807 | |
icraggs | 15:64a57183aa03 | 808 | exit: |
icraggs | 43:21da1f744243 | 809 | if (rc != SUCCESS) |
icraggs | 46:e335fcc1a663 | 810 | cleanSession(); |
Ian Craggs |
12:cc7f2d62a393 | 811 | return rc; |
icraggs | 15:64a57183aa03 | 812 | } |
icraggs | 15:64a57183aa03 | 813 | |
icraggs | 15:64a57183aa03 | 814 | |
icraggs | 43:21da1f744243 | 815 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 816 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Timer& timer, enum QoS qos) |
icraggs | 20:cad3d54d7ecf | 817 | { |
icraggs | 43:21da1f744243 | 818 | int rc; |
icraggs | 46:e335fcc1a663 | 819 | |
icraggs | 43:21da1f744243 | 820 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the publish packet |
icraggs | 43:21da1f744243 | 821 | goto exit; // there was a problem |
icraggs | 15:64a57183aa03 | 822 | |
icraggs | 46:e335fcc1a663 | 823 | #if MQTTCLIENT_QOS1 |
icraggs | 43:21da1f744243 | 824 | if (qos == QOS1) |
Ian Craggs |
12:cc7f2d62a393 | 825 | { |
icraggs | 20:cad3d54d7ecf | 826 | if (waitfor(PUBACK, timer) == PUBACK) |
icraggs | 20:cad3d54d7ecf | 827 | { |
icraggs | 36:2f1ada427e56 | 828 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 829 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 830 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 831 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 832 | else if (inflightMsgid == mypacketid) |
icraggs | 43:21da1f744243 | 833 | inflightMsgid = 0; |
icraggs | 20:cad3d54d7ecf | 834 | } |
icraggs | 26:2658bb87c53d | 835 | else |
icraggs | 26:2658bb87c53d | 836 | rc = FAILURE; |
Ian Craggs |
12:cc7f2d62a393 | 837 | } |
icraggs | 43:21da1f744243 | 838 | #elif MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 839 | else if (qos == QOS2) |
Ian Craggs |
12:cc7f2d62a393 | 840 | { |
icraggs | 20:cad3d54d7ecf | 841 | if (waitfor(PUBCOMP, timer) == PUBCOMP) |
icraggs | 20:cad3d54d7ecf | 842 | { |
icraggs | 36:2f1ada427e56 | 843 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 844 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 845 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 846 | rc = FAILURE; |
icraggs | 43:21da1f744243 | 847 | else if (inflightMsgid == mypacketid) |
icraggs | 43:21da1f744243 | 848 | inflightMsgid = 0; |
icraggs | 20:cad3d54d7ecf | 849 | } |
icraggs | 26:2658bb87c53d | 850 | else |
icraggs | 26:2658bb87c53d | 851 | rc = FAILURE; |
Ian Craggs |
12:cc7f2d62a393 | 852 | } |
icraggs | 43:21da1f744243 | 853 | #endif |
icraggs | 43:21da1f744243 | 854 | |
icraggs | 43:21da1f744243 | 855 | exit: |
icraggs | 43:21da1f744243 | 856 | if (rc != SUCCESS) |
icraggs | 46:e335fcc1a663 | 857 | cleanSession(); |
icraggs | 43:21da1f744243 | 858 | return rc; |
icraggs | 43:21da1f744243 | 859 | } |
icraggs | 43:21da1f744243 | 860 | |
icraggs | 43:21da1f744243 | 861 | |
icraggs | 43:21da1f744243 | 862 | |
icraggs | 43:21da1f744243 | 863 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 864 | 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 | 865 | { |
icraggs | 43:21da1f744243 | 866 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 867 | Timer timer(command_timeout_ms); |
icraggs | 43:21da1f744243 | 868 | MQTTString topicString = MQTTString_initializer; |
icraggs | 43:21da1f744243 | 869 | int len = 0; |
icraggs | 43:21da1f744243 | 870 | |
icraggs | 43:21da1f744243 | 871 | if (!isconnected) |
icraggs | 43:21da1f744243 | 872 | goto exit; |
icraggs | 46:e335fcc1a663 | 873 | |
icraggs | 43:21da1f744243 | 874 | topicString.cstring = (char*)topicName; |
icraggs | 43:21da1f744243 | 875 | |
icraggs | 43:21da1f744243 | 876 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 877 | if (qos == QOS1 || qos == QOS2) |
icraggs | 43:21da1f744243 | 878 | id = packetid.getNext(); |
icraggs | 43:21da1f744243 | 879 | #endif |
icraggs | 43:21da1f744243 | 880 | |
icraggs | 43:21da1f744243 | 881 | len = MQTTSerialize_publish(sendbuf, MAX_MQTT_PACKET_SIZE, 0, qos, retained, id, |
icraggs | 43:21da1f744243 | 882 | topicString, (unsigned char*)payload, payloadlen); |
icraggs | 43:21da1f744243 | 883 | if (len <= 0) |
icraggs | 43:21da1f744243 | 884 | goto exit; |
icraggs | 46:e335fcc1a663 | 885 | |
icraggs | 43:21da1f744243 | 886 | #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 887 | if (!cleansession) |
icraggs | 43:21da1f744243 | 888 | { |
icraggs | 43:21da1f744243 | 889 | memcpy(pubbuf, sendbuf, len); |
icraggs | 43:21da1f744243 | 890 | inflightMsgid = id; |
icraggs | 43:21da1f744243 | 891 | inflightLen = len; |
icraggs | 43:21da1f744243 | 892 | inflightQoS = qos; |
icraggs | 43:21da1f744243 | 893 | #if MQTTCLIENT_QOS2 |
icraggs | 43:21da1f744243 | 894 | pubrel = false; |
icraggs | 43:21da1f744243 | 895 | #endif |
icraggs | 43:21da1f744243 | 896 | } |
icraggs | 43:21da1f744243 | 897 | #endif |
icraggs | 46:e335fcc1a663 | 898 | |
icraggs | 43:21da1f744243 | 899 | rc = publish(len, timer, qos); |
icraggs | 15:64a57183aa03 | 900 | exit: |
icraggs | 8:c46930bd6c82 | 901 | return rc; |
icraggs | 8:c46930bd6c82 | 902 | } |
icraggs | 8:c46930bd6c82 | 903 | |
icraggs | 8:c46930bd6c82 | 904 | |
icraggs | 43:21da1f744243 | 905 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 906 | 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 | 907 | { |
icraggs | 43:21da1f744243 | 908 | unsigned short id = 0; // dummy - not used for anything |
icraggs | 43:21da1f744243 | 909 | return publish(topicName, payload, payloadlen, id, qos, retained); |
icraggs | 43:21da1f744243 | 910 | } |
icraggs | 43:21da1f744243 | 911 | |
icraggs | 43:21da1f744243 | 912 | |
icraggs | 43:21da1f744243 | 913 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 43:21da1f744243 | 914 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message& message) |
icraggs | 43:21da1f744243 | 915 | { |
icraggs | 43:21da1f744243 | 916 | return publish(topicName, message.payload, message.payloadlen, message.qos, message.retained); |
icraggs | 43:21da1f744243 | 917 | } |
icraggs | 43:21da1f744243 | 918 | |
icraggs | 43:21da1f744243 | 919 | |
icraggs | 43:21da1f744243 | 920 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 921 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect() |
icraggs | 43:21da1f744243 | 922 | { |
icraggs | 26:2658bb87c53d | 923 | int rc = FAILURE; |
icraggs | 46:e335fcc1a663 | 924 | Timer timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete |
icraggs | 44:c299463ae853 | 925 | int len = MQTTSerialize_disconnect(sendbuf, MAX_MQTT_PACKET_SIZE); |
icraggs | 26:2658bb87c53d | 926 | if (len > 0) |
icraggs | 26:2658bb87c53d | 927 | rc = sendPacket(len, timer); // send the disconnect packet |
icraggs | 43:21da1f744243 | 928 | |
icraggs | 46:e335fcc1a663 | 929 | if (cleansession) |
icraggs | 46:e335fcc1a663 | 930 | cleanSession(); |
icraggs | 46:e335fcc1a663 | 931 | else |
icraggs | 46:e335fcc1a663 | 932 | isconnected = false; |
icraggs | 23:05fc7de97d4a | 933 | return rc; |
icraggs | 20:cad3d54d7ecf | 934 | } |
icraggs | 20:cad3d54d7ecf | 935 | |
icraggs | 20:cad3d54d7ecf | 936 | |
icraggs | 46:e335fcc1a663 | 937 | #endif |